2 * WinTrust Cryptography functions
4 * Copyright 2006 James Hawkins
5 * Copyright 2000-2002 Stuart Caie
6 * Copyright 2002 Patrik Stridvall
7 * Copyright 2003 Greg Turner
8 * Copyright 2008 Juan Lang
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(wintrust
);
40 #define CATADMIN_MAGIC 0x43415441 /* 'CATA' */
41 #define CRYPTCAT_MAGIC 0x43415443 /* 'CATC' */
42 #define CATINFO_MAGIC 0x43415449 /* 'CATI' */
53 CRYPTCATATTRIBUTE
*attr
;
69 static HCATINFO
create_catinfo(const WCHAR
*filename
)
73 if (!(ci
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ci
))))
75 SetLastError(ERROR_OUTOFMEMORY
);
76 return INVALID_HANDLE_VALUE
;
78 strcpyW(ci
->file
, filename
);
79 ci
->magic
= CATINFO_MAGIC
;
83 /***********************************************************************
84 * CryptCATAdminAcquireContext (WINTRUST.@)
86 * Get a catalog administrator context handle.
89 * catAdmin [O] Pointer to the context handle.
90 * sys [I] Pointer to a GUID for the needed subsystem.
91 * dwFlags [I] Reserved.
94 * Success: TRUE. catAdmin contains the context handle.
98 BOOL WINAPI
CryptCATAdminAcquireContext(HCATADMIN
*catAdmin
,
99 const GUID
*sys
, DWORD dwFlags
)
101 static const WCHAR catroot
[] =
102 {'\\','c','a','t','r','o','o','t',0};
103 static const WCHAR fmt
[] =
104 {'%','s','\\','{','%','0','8','x','-','%','0','4','x','-','%','0',
105 '4','x','-','%','0','2','x','%','0','2','x','-','%','0','2','x',
106 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x',
107 '%','0','2','x','}',0};
108 static const GUID defsys
=
109 {0x127d0a1d,0x4ef2,0x11d1,{0x86,0x08,0x00,0xc0,0x4f,0xc2,0x95,0xee}};
111 WCHAR catroot_dir
[MAX_PATH
];
114 TRACE("%p %s %x\n", catAdmin
, debugstr_guid(sys
), dwFlags
);
118 SetLastError(ERROR_INVALID_PARAMETER
);
121 if (!(ca
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ca
))))
123 SetLastError(ERROR_OUTOFMEMORY
);
127 GetSystemDirectoryW(catroot_dir
, MAX_PATH
);
128 strcatW(catroot_dir
, catroot
);
130 /* create the directory if it doesn't exist */
131 CreateDirectoryW(catroot_dir
, NULL
);
133 if (!sys
) sys
= &defsys
;
134 sprintfW(ca
->path
, fmt
, catroot_dir
, sys
->Data1
, sys
->Data2
,
135 sys
->Data3
, sys
->Data4
[0], sys
->Data4
[1], sys
->Data4
[2],
136 sys
->Data4
[3], sys
->Data4
[4], sys
->Data4
[5], sys
->Data4
[6],
139 /* create the directory if it doesn't exist */
140 CreateDirectoryW(ca
->path
, NULL
);
142 ca
->magic
= CATADMIN_MAGIC
;
143 ca
->find
= INVALID_HANDLE_VALUE
;
149 /***********************************************************************
150 * CryptCATAdminAddCatalog (WINTRUST.@)
152 HCATINFO WINAPI
CryptCATAdminAddCatalog(HCATADMIN catAdmin
, PWSTR catalogFile
,
153 PWSTR selectBaseName
, DWORD flags
)
155 static const WCHAR slashW
[] = {'\\',0};
156 struct catadmin
*ca
= catAdmin
;
161 TRACE("%p %s %s %d\n", catAdmin
, debugstr_w(catalogFile
),
162 debugstr_w(selectBaseName
), flags
);
166 FIXME("NULL basename not handled\n");
167 SetLastError(ERROR_INVALID_PARAMETER
);
170 if (!ca
|| ca
->magic
!= CATADMIN_MAGIC
|| !catalogFile
|| flags
)
172 SetLastError(ERROR_INVALID_PARAMETER
);
176 len
= strlenW(ca
->path
) + strlenW(selectBaseName
) + 2;
177 if (!(target
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
))))
179 SetLastError(ERROR_OUTOFMEMORY
);
182 strcpyW(target
, ca
->path
);
183 strcatW(target
, slashW
);
184 strcatW(target
, selectBaseName
);
186 if (!CopyFileW(catalogFile
, target
, FALSE
))
188 HeapFree(GetProcessHeap(), 0, target
);
191 if (!(ci
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ci
))))
193 HeapFree(GetProcessHeap(), 0, target
);
194 SetLastError(ERROR_OUTOFMEMORY
);
197 ci
->magic
= CATINFO_MAGIC
;
198 strcpyW(ci
->file
, target
);
200 HeapFree(GetProcessHeap(), 0, target
);
204 /***********************************************************************
205 * CryptCATAdminCalcHashFromFileHandle (WINTRUST.@)
207 BOOL WINAPI
CryptCATAdminCalcHashFromFileHandle(HANDLE hFile
, DWORD
* pcbHash
,
208 BYTE
* pbHash
, DWORD dwFlags
)
212 TRACE("%p %p %p %x\n", hFile
, pcbHash
, pbHash
, dwFlags
);
214 if (!hFile
|| !pcbHash
|| dwFlags
)
216 SetLastError(ERROR_INVALID_PARAMETER
);
222 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
234 if (!(buffer
= HeapAlloc(GetProcessHeap(), 0, 4096)))
236 SetLastError(ERROR_OUTOFMEMORY
);
239 ret
= CryptAcquireContextW(&prov
, NULL
, MS_DEF_PROV_W
, PROV_RSA_FULL
, CRYPT_VERIFYCONTEXT
);
242 HeapFree(GetProcessHeap(), 0, buffer
);
245 ret
= CryptCreateHash(prov
, CALG_SHA1
, 0, 0, &hash
);
248 HeapFree(GetProcessHeap(), 0, buffer
);
249 CryptReleaseContext(prov
, 0);
252 while ((ret
= ReadFile(hFile
, buffer
, 4096, &bytes_read
, NULL
)) && bytes_read
)
254 CryptHashData(hash
, buffer
, bytes_read
, 0);
256 if (ret
) ret
= CryptGetHashParam(hash
, HP_HASHVAL
, pbHash
, pcbHash
, 0);
258 HeapFree(GetProcessHeap(), 0, buffer
);
259 CryptDestroyHash(hash
);
260 CryptReleaseContext(prov
, 0);
265 /***********************************************************************
266 * CryptCATAdminEnumCatalogFromHash (WINTRUST.@)
268 HCATINFO WINAPI
CryptCATAdminEnumCatalogFromHash(HCATADMIN hCatAdmin
, BYTE
* pbHash
,
269 DWORD cbHash
, DWORD dwFlags
,
270 HCATINFO
* phPrevCatInfo
)
272 static const WCHAR slashW
[] = {'\\',0};
273 static const WCHAR globW
[] = {'\\','*','.','c','a','t',0};
275 struct catadmin
*ca
= hCatAdmin
;
276 WIN32_FIND_DATAW data
;
277 HCATINFO prev
= NULL
;
282 TRACE("%p %p %d %x %p\n", hCatAdmin
, pbHash
, cbHash
, dwFlags
, phPrevCatInfo
);
284 if (!ca
|| ca
->magic
!= CATADMIN_MAGIC
|| !pbHash
|| cbHash
!= 20 || dwFlags
)
286 SetLastError(ERROR_INVALID_PARAMETER
);
289 if (phPrevCatInfo
) prev
= *phPrevCatInfo
;
291 ret
= CryptAcquireContextW(&prov
, NULL
, MS_DEF_PROV_W
, PROV_RSA_FULL
, CRYPT_VERIFYCONTEXT
);
292 if (!ret
) return NULL
;
298 size
= strlenW(ca
->path
) * sizeof(WCHAR
) + sizeof(globW
);
299 if (!(path
= HeapAlloc(GetProcessHeap(), 0, size
)))
301 CryptReleaseContext(prov
, 0);
302 SetLastError(ERROR_OUTOFMEMORY
);
305 strcpyW(path
, ca
->path
);
306 strcatW(path
, globW
);
309 ca
->find
= FindFirstFileW(path
, &data
);
311 HeapFree(GetProcessHeap(), 0, path
);
312 if (ca
->find
== INVALID_HANDLE_VALUE
)
314 CryptReleaseContext(prov
, 0);
318 else if (!FindNextFileW(ca
->find
, &data
))
320 CryptCATAdminReleaseCatalogContext(hCatAdmin
, prev
, 0);
321 CryptReleaseContext(prov
, 0);
328 CRYPTCATMEMBER
*member
= NULL
;
332 size
= (strlenW(ca
->path
) + strlenW(data
.cFileName
) + 2) * sizeof(WCHAR
);
333 if (!(filename
= HeapAlloc(GetProcessHeap(), 0, size
)))
335 SetLastError(ERROR_OUTOFMEMORY
);
338 strcpyW(filename
, ca
->path
);
339 strcatW(filename
, slashW
);
340 strcatW(filename
, data
.cFileName
);
342 hcat
= CryptCATOpen(filename
, CRYPTCAT_OPEN_EXISTING
, prov
, 0, 0);
343 if (hcat
== INVALID_HANDLE_VALUE
)
345 WARN("couldn't open %s (%u)\n", debugstr_w(filename
), GetLastError());
348 while ((member
= CryptCATEnumerateMember(hcat
, member
)))
350 if (member
->pIndirectData
->Digest
.cbData
!= cbHash
)
352 WARN("amount of hash bytes differs: %u/%u\n", member
->pIndirectData
->Digest
.cbData
, cbHash
);
355 if (!memcmp(member
->pIndirectData
->Digest
.pbData
, pbHash
, cbHash
))
357 TRACE("file %s matches\n", debugstr_w(data
.cFileName
));
360 CryptReleaseContext(prov
, 0);
364 ca
->find
= INVALID_HANDLE_VALUE
;
366 ci
= create_catinfo(filename
);
367 HeapFree(GetProcessHeap(), 0, filename
);
372 HeapFree(GetProcessHeap(), 0, filename
);
374 if (!FindNextFileW(ca
->find
, &data
))
377 ca
->find
= INVALID_HANDLE_VALUE
;
378 CryptReleaseContext(prov
, 0);
385 /***********************************************************************
386 * CryptCATAdminReleaseCatalogContext (WINTRUST.@)
388 * Release a catalog context handle.
391 * hCatAdmin [I] Context handle.
392 * hCatInfo [I] Catalog handle.
393 * dwFlags [I] Reserved.
400 BOOL WINAPI
CryptCATAdminReleaseCatalogContext(HCATADMIN hCatAdmin
,
404 struct catinfo
*ci
= hCatInfo
;
405 struct catadmin
*ca
= hCatAdmin
;
407 TRACE("%p %p %x\n", hCatAdmin
, hCatInfo
, dwFlags
);
409 if (!ca
|| ca
->magic
!= CATADMIN_MAGIC
|| !ci
|| ci
->magic
!= CATINFO_MAGIC
)
411 SetLastError(ERROR_INVALID_PARAMETER
);
415 return HeapFree(GetProcessHeap(), 0, ci
);
418 /***********************************************************************
419 * CryptCATAdminReleaseContext (WINTRUST.@)
421 * Release a catalog administrator context handle.
424 * catAdmin [I] Context handle.
425 * dwFlags [I] Reserved.
432 BOOL WINAPI
CryptCATAdminReleaseContext(HCATADMIN hCatAdmin
, DWORD dwFlags
)
434 struct catadmin
*ca
= hCatAdmin
;
436 TRACE("%p %x\n", hCatAdmin
, dwFlags
);
438 if (!ca
|| ca
->magic
!= CATADMIN_MAGIC
)
440 SetLastError(ERROR_INVALID_PARAMETER
);
443 if (ca
->find
!= INVALID_HANDLE_VALUE
) FindClose(ca
->find
);
445 return HeapFree(GetProcessHeap(), 0, ca
);
448 /***********************************************************************
449 * CryptCATAdminRemoveCatalog (WINTRUST.@)
451 * Remove a catalog file.
454 * catAdmin [I] Context handle.
455 * pwszCatalogFile [I] Catalog file.
456 * dwFlags [I] Reserved.
463 BOOL WINAPI
CryptCATAdminRemoveCatalog(HCATADMIN hCatAdmin
, LPCWSTR pwszCatalogFile
, DWORD dwFlags
)
465 struct catadmin
*ca
= hCatAdmin
;
467 TRACE("%p %s %x\n", hCatAdmin
, debugstr_w(pwszCatalogFile
), dwFlags
);
469 if (!ca
|| ca
->magic
!= CATADMIN_MAGIC
)
471 SetLastError(ERROR_INVALID_PARAMETER
);
474 return DeleteFileW(pwszCatalogFile
);
477 /***********************************************************************
478 * CryptCATAdminResolveCatalogPath (WINTRUST.@)
480 BOOL WINAPI
CryptCATAdminResolveCatalogPath(HCATADMIN hcatadmin
, WCHAR
*catalog_file
,
481 CATALOG_INFO
*info
, DWORD flags
)
483 static const WCHAR slashW
[] = {'\\',0};
484 struct catadmin
*ca
= hcatadmin
;
486 TRACE("%p %s %p %x\n", hcatadmin
, debugstr_w(catalog_file
), info
, flags
);
488 if (!ca
|| ca
->magic
!= CATADMIN_MAGIC
|| !info
|| info
->cbStruct
!= sizeof(*info
) || flags
)
490 SetLastError(ERROR_INVALID_PARAMETER
);
493 strcpyW(info
->wszCatalogFile
, ca
->path
);
494 strcatW(info
->wszCatalogFile
, slashW
);
495 strcatW(info
->wszCatalogFile
, catalog_file
);
500 /***********************************************************************
501 * CryptCATClose (WINTRUST.@)
503 BOOL WINAPI
CryptCATClose(HANDLE hCatalog
)
505 struct cryptcat
*cc
= hCatalog
;
507 TRACE("(%p)\n", hCatalog
);
509 if (!hCatalog
|| hCatalog
== INVALID_HANDLE_VALUE
|| cc
->magic
!= CRYPTCAT_MAGIC
)
511 SetLastError(ERROR_INVALID_PARAMETER
);
514 HeapFree(GetProcessHeap(), 0, cc
->attr
);
515 HeapFree(GetProcessHeap(), 0, cc
->inner
);
516 CryptMsgClose(cc
->msg
);
519 HeapFree(GetProcessHeap(), 0, cc
);
523 /***********************************************************************
524 * CryptCATEnumerateMember (WINTRUST.@)
526 CRYPTCATMEMBER
* WINAPI
CryptCATEnumerateMember(HANDLE hCatalog
, CRYPTCATMEMBER
*prev
)
528 struct cryptcat
*cc
= hCatalog
;
529 CRYPTCATMEMBER
*member
= prev
;
533 TRACE("%p, %p\n", hCatalog
, prev
);
535 if (!hCatalog
|| hCatalog
== INVALID_HANDLE_VALUE
|| cc
->magic
!= CRYPTCAT_MAGIC
)
537 SetLastError(ERROR_INVALID_PARAMETER
);
541 /* dumping the contents makes me think that dwReserved is the iteration number */
544 if (!(member
= HeapAlloc(GetProcessHeap(), 0, sizeof(*member
))))
546 SetLastError(ERROR_OUTOFMEMORY
);
549 member
->cbStruct
= sizeof(*member
);
550 member
->pwszFileName
= member
->pwszReferenceTag
= NULL
;
551 member
->dwReserved
= 0;
552 member
->hReserved
= NULL
;
553 member
->gSubjectType
= cc
->subject
;
554 member
->fdwMemberFlags
= 0;
555 member
->pIndirectData
= NULL
;
556 member
->dwCertVersion
= cc
->inner
->dwVersion
;
558 else member
->dwReserved
++;
560 if (member
->dwReserved
>= cc
->inner
->cCTLEntry
)
562 SetLastError(ERROR_INVALID_PARAMETER
);
566 /* list them backwards, like native */
567 entry
= &cc
->inner
->rgCTLEntry
[cc
->inner
->cCTLEntry
- member
->dwReserved
- 1];
569 member
->sEncodedIndirectData
.cbData
= member
->sEncodedMemberInfo
.cbData
= 0;
570 member
->sEncodedIndirectData
.pbData
= member
->sEncodedMemberInfo
.pbData
= NULL
;
571 HeapFree(GetProcessHeap(), 0, member
->pIndirectData
);
572 member
->pIndirectData
= NULL
;
574 for (i
= 0; i
< entry
->cAttribute
; i
++)
576 CRYPT_ATTRIBUTE
*attr
= entry
->rgAttribute
+ i
;
578 if (attr
->cValue
!= 1)
580 ERR("Can't handle attr->cValue of %u\n", attr
->cValue
);
583 if (!strcmp(attr
->pszObjId
, CAT_MEMBERINFO_OBJID
))
588 member
->sEncodedMemberInfo
.cbData
= attr
->rgValue
->cbData
;
589 member
->sEncodedMemberInfo
.pbData
= attr
->rgValue
->pbData
;
591 CryptDecodeObject(cc
->encoding
, CAT_MEMBERINFO_OBJID
, attr
->rgValue
->pbData
, attr
->rgValue
->cbData
, 0, NULL
, &size
);
593 if (!(mi
= HeapAlloc(GetProcessHeap(), 0, size
)))
595 SetLastError(ERROR_OUTOFMEMORY
);
598 ret
= CryptDecodeObject(cc
->encoding
, CAT_MEMBERINFO_OBJID
, attr
->rgValue
->pbData
, attr
->rgValue
->cbData
, 0, mi
, &size
);
603 member
->dwCertVersion
= mi
->dwCertVersion
;
604 RtlInitUnicodeString(&guid
, mi
->pwszSubjGuid
);
605 if (RtlGUIDFromString(&guid
, &member
->gSubjectType
))
607 HeapFree(GetProcessHeap(), 0, mi
);
611 HeapFree(GetProcessHeap(), 0, mi
);
612 if (!ret
) goto error
;
614 else if (!strcmp(attr
->pszObjId
, SPC_INDIRECT_DATA_OBJID
))
616 /* SPC_INDIRECT_DATA_CONTENT is equal to SIP_INDIRECT_DATA */
618 member
->sEncodedIndirectData
.cbData
= attr
->rgValue
->cbData
;
619 member
->sEncodedIndirectData
.pbData
= attr
->rgValue
->pbData
;
621 CryptDecodeObject(cc
->encoding
, SPC_INDIRECT_DATA_OBJID
, attr
->rgValue
->pbData
, attr
->rgValue
->cbData
, 0, NULL
, &size
);
623 if (!(member
->pIndirectData
= HeapAlloc(GetProcessHeap(), 0, size
)))
625 SetLastError(ERROR_OUTOFMEMORY
);
628 CryptDecodeObject(cc
->encoding
, SPC_INDIRECT_DATA_OBJID
, attr
->rgValue
->pbData
, attr
->rgValue
->cbData
, 0, member
->pIndirectData
, &size
);
631 /* this object id should probably be handled in CryptCATEnumerateAttr */
632 FIXME("unhandled object id \"%s\"\n", attr
->pszObjId
);
635 if (!member
->sEncodedMemberInfo
.cbData
|| !member
->sEncodedIndirectData
.cbData
)
637 ERR("Corrupted catalog entry?\n");
638 SetLastError(CRYPT_E_ATTRIBUTES_MISSING
);
641 size
= (2 * member
->pIndirectData
->Digest
.cbData
+ 1) * sizeof(WCHAR
);
642 if (member
->pwszReferenceTag
)
643 member
->pwszReferenceTag
= HeapReAlloc(GetProcessHeap(), 0, member
->pwszReferenceTag
, size
);
645 member
->pwszReferenceTag
= HeapAlloc(GetProcessHeap(), 0, size
);
647 if (!member
->pwszReferenceTag
)
649 SetLastError(ERROR_OUTOFMEMORY
);
652 /* FIXME: reference tag is usually the file hash but doesn't have to be */
653 for (i
= 0; i
< member
->pIndirectData
->Digest
.cbData
; i
++)
657 sub
= member
->pIndirectData
->Digest
.pbData
[i
] >> 4;
658 member
->pwszReferenceTag
[i
* 2] = (sub
< 10 ? '0' + sub
: 'A' + sub
- 10);
659 sub
= member
->pIndirectData
->Digest
.pbData
[i
] & 0xf;
660 member
->pwszReferenceTag
[i
* 2 + 1] = (sub
< 10 ? '0' + sub
: 'A' + sub
- 10);
662 member
->pwszReferenceTag
[i
* 2] = 0;
666 HeapFree(GetProcessHeap(), 0, member
->pIndirectData
);
667 HeapFree(GetProcessHeap(), 0, member
->pwszReferenceTag
);
668 HeapFree(GetProcessHeap(), 0, member
);
672 static CTL_INFO
*decode_inner_content(HANDLE hmsg
, DWORD encoding
, DWORD
*len
)
677 CTL_INFO
*inner
= NULL
;
679 if (!CryptMsgGetParam(hmsg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0, NULL
, &size
)) return NULL
;
680 if (!(oid
= HeapAlloc(GetProcessHeap(), 0, size
)))
682 SetLastError(ERROR_OUTOFMEMORY
);
685 if (!CryptMsgGetParam(hmsg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0, oid
, &size
)) goto out
;
686 if (!CryptMsgGetParam(hmsg
, CMSG_CONTENT_PARAM
, 0, NULL
, &size
)) goto out
;
687 if (!(buffer
= HeapAlloc(GetProcessHeap(), 0, size
)))
689 SetLastError(ERROR_OUTOFMEMORY
);
692 if (!CryptMsgGetParam(hmsg
, CMSG_CONTENT_PARAM
, 0, buffer
, &size
)) goto out
;
693 if (!CryptDecodeObject(encoding
, oid
, buffer
, size
, 0, NULL
, &size
)) goto out
;
694 if (!(inner
= HeapAlloc(GetProcessHeap(), 0, size
)))
696 SetLastError(ERROR_OUTOFMEMORY
);
699 if (!CryptDecodeObject(encoding
, oid
, buffer
, size
, 0, inner
, &size
)) goto out
;
703 HeapFree(GetProcessHeap(), 0, oid
);
704 HeapFree(GetProcessHeap(), 0, buffer
);
708 /***********************************************************************
709 * CryptCATCatalogInfoFromContext (WINTRUST.@)
711 BOOL WINAPI
CryptCATCatalogInfoFromContext(HCATINFO hcatinfo
, CATALOG_INFO
*info
, DWORD flags
)
713 struct catinfo
*ci
= hcatinfo
;
715 TRACE("%p, %p, %x\n", hcatinfo
, info
, flags
);
717 if (!hcatinfo
|| hcatinfo
== INVALID_HANDLE_VALUE
|| ci
->magic
!= CATINFO_MAGIC
||
718 flags
|| !info
|| info
->cbStruct
!= sizeof(*info
))
720 SetLastError(ERROR_INVALID_PARAMETER
);
723 strcpyW(info
->wszCatalogFile
, ci
->file
);
727 /***********************************************************************
728 * CryptCATOpen (WINTRUST.@)
730 HANDLE WINAPI
CryptCATOpen(LPWSTR pwszFileName
, DWORD fdwOpenFlags
, HCRYPTPROV hProv
,
731 DWORD dwPublicVersion
, DWORD dwEncodingType
)
735 DWORD size
, flags
= OPEN_EXISTING
;
738 TRACE("%s, %x, %lx, %x, %x\n", debugstr_w(pwszFileName
), fdwOpenFlags
,
739 hProv
, dwPublicVersion
, dwEncodingType
);
743 SetLastError(ERROR_INVALID_PARAMETER
);
744 return INVALID_HANDLE_VALUE
;
747 if (!dwPublicVersion
) dwPublicVersion
= 0x00000100;
748 if (!dwEncodingType
) dwEncodingType
= X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
;
750 if (fdwOpenFlags
& CRYPTCAT_OPEN_ALWAYS
) flags
|= OPEN_ALWAYS
;
751 if (fdwOpenFlags
& CRYPTCAT_OPEN_CREATENEW
) flags
|= CREATE_NEW
;
753 file
= CreateFileW(pwszFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, flags
, 0, NULL
);
754 if (file
== INVALID_HANDLE_VALUE
) return INVALID_HANDLE_VALUE
;
756 size
= GetFileSize(file
, NULL
);
757 if (!(buffer
= HeapAlloc(GetProcessHeap(), 0, size
)))
760 SetLastError(ERROR_OUTOFMEMORY
);
761 return INVALID_HANDLE_VALUE
;
763 if (!(hmsg
= CryptMsgOpenToDecode(dwEncodingType
, 0, 0, hProv
, NULL
, NULL
)))
766 HeapFree(GetProcessHeap(), 0, buffer
);
767 return INVALID_HANDLE_VALUE
;
769 if (!ReadFile(file
, buffer
, size
, &size
, NULL
) || !CryptMsgUpdate(hmsg
, buffer
, size
, TRUE
))
772 HeapFree(GetProcessHeap(), 0, buffer
);
774 return INVALID_HANDLE_VALUE
;
776 HeapFree(GetProcessHeap(), 0, buffer
);
779 size
= sizeof(DWORD
);
780 if (!(cc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*cc
))))
783 SetLastError(ERROR_OUTOFMEMORY
);
784 return INVALID_HANDLE_VALUE
;
788 cc
->encoding
= dwEncodingType
;
789 if (CryptMsgGetParam(hmsg
, CMSG_ATTR_CERT_COUNT_PARAM
, 0, &cc
->attr_count
, &size
))
794 for (i
= 0; i
< cc
->attr_count
; i
++)
796 if (!CryptMsgGetParam(hmsg
, CMSG_ATTR_CERT_PARAM
, i
, NULL
, &size
))
799 return INVALID_HANDLE_VALUE
;
803 if (!(cc
->attr
= HeapAlloc(GetProcessHeap(), 0, sizeof(*cc
->attr
) * cc
->attr_count
+ sum
)))
806 SetLastError(ERROR_OUTOFMEMORY
);
807 return INVALID_HANDLE_VALUE
;
809 p
= (BYTE
*)(cc
->attr
+ cc
->attr_count
);
810 for (i
= 0; i
< cc
->attr_count
; i
++)
812 if (!CryptMsgGetParam(hmsg
, CMSG_ATTR_CERT_PARAM
, i
, NULL
, &size
))
815 HeapFree(GetProcessHeap(), 0, cc
->attr
);
816 return INVALID_HANDLE_VALUE
;
818 if (!CryptMsgGetParam(hmsg
, CMSG_ATTR_CERT_PARAM
, i
, p
, &size
))
821 HeapFree(GetProcessHeap(), 0, cc
->attr
);
822 return INVALID_HANDLE_VALUE
;
826 cc
->inner
= decode_inner_content(hmsg
, dwEncodingType
, &cc
->inner_len
);
827 if (!cc
->inner
|| !CryptSIPRetrieveSubjectGuid(pwszFileName
, NULL
, &cc
->subject
))
830 HeapFree(GetProcessHeap(), 0, cc
->attr
);
831 HeapFree(GetProcessHeap(), 0, cc
->inner
);
832 HeapFree(GetProcessHeap(), 0, cc
);
833 return INVALID_HANDLE_VALUE
;
835 cc
->magic
= CRYPTCAT_MAGIC
;
838 return INVALID_HANDLE_VALUE
;
841 /***********************************************************************
842 * CryptSIPCreateIndirectData (WINTRUST.@)
844 BOOL WINAPI
CryptSIPCreateIndirectData(SIP_SUBJECTINFO
* pSubjectInfo
, DWORD
* pcbIndirectData
,
845 SIP_INDIRECT_DATA
* pIndirectData
)
847 FIXME("(%p %p %p) stub\n", pSubjectInfo
, pcbIndirectData
, pIndirectData
);
852 static BOOL
WINTRUST_GetSignedMsgFromPEFile(SIP_SUBJECTINFO
*pSubjectInfo
,
853 DWORD
*pdwEncodingType
, DWORD dwIndex
, DWORD
*pcbSignedDataMsg
,
854 BYTE
*pbSignedDataMsg
)
857 WIN_CERTIFICATE
*pCert
= NULL
;
859 TRACE("(%p %p %d %p %p)\n", pSubjectInfo
, pdwEncodingType
, dwIndex
,
860 pcbSignedDataMsg
, pbSignedDataMsg
);
862 if (!pbSignedDataMsg
)
864 WIN_CERTIFICATE cert
;
866 /* app hasn't passed buffer, just get the length */
867 ret
= ImageGetCertificateHeader(pSubjectInfo
->hFile
, dwIndex
, &cert
);
869 *pcbSignedDataMsg
= cert
.dwLength
;
875 ret
= ImageGetCertificateData(pSubjectInfo
->hFile
, dwIndex
, NULL
, &len
);
876 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
878 pCert
= HeapAlloc(GetProcessHeap(), 0, len
);
884 ret
= ImageGetCertificateData(pSubjectInfo
->hFile
, dwIndex
, pCert
,
888 if (*pcbSignedDataMsg
< pCert
->dwLength
)
890 *pcbSignedDataMsg
= pCert
->dwLength
;
891 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
896 memcpy(pbSignedDataMsg
, pCert
->bCertificate
, pCert
->dwLength
);
897 switch (pCert
->wCertificateType
)
899 case WIN_CERT_TYPE_X509
:
900 *pdwEncodingType
= X509_ASN_ENCODING
;
902 case WIN_CERT_TYPE_PKCS_SIGNED_DATA
:
903 *pdwEncodingType
= X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
;
906 FIXME("don't know what to do for encoding type %d\n",
907 pCert
->wCertificateType
);
908 *pdwEncodingType
= 0;
913 HeapFree(GetProcessHeap(), 0, pCert
);
917 /* structure offsets */
918 #define cfhead_Signature (0x00)
919 #define cfhead_CabinetSize (0x08)
920 #define cfhead_MinorVersion (0x18)
921 #define cfhead_MajorVersion (0x19)
922 #define cfhead_Flags (0x1E)
923 #define cfhead_SIZEOF (0x24)
924 #define cfheadext_HeaderReserved (0x00)
925 #define cfheadext_SIZEOF (0x04)
926 #define cfsigninfo_CertOffset (0x04)
927 #define cfsigninfo_CertSize (0x08)
928 #define cfsigninfo_SIZEOF (0x0C)
931 #define cfheadRESERVE_PRESENT (0x0004)
933 /* endian-neutral reading of little-endian data */
934 #define EndGetI32(a) ((((a)[3])<<24)|(((a)[2])<<16)|(((a)[1])<<8)|((a)[0]))
935 #define EndGetI16(a) ((((a)[1])<<8)|((a)[0]))
937 /* For documentation purposes only: this is the structure in the reserved
938 * area of a signed cabinet file. The cert offset indicates where in the
939 * cabinet file the signature resides, and the count indicates its size.
941 typedef struct _CAB_SIGNINFO
943 WORD unk0
; /* always 0? */
944 WORD unk1
; /* always 0x0010? */
947 } CAB_SIGNINFO
, *PCAB_SIGNINFO
;
949 static BOOL
WINTRUST_GetSignedMsgFromCabFile(SIP_SUBJECTINFO
*pSubjectInfo
,
950 DWORD
*pdwEncodingType
, DWORD dwIndex
, DWORD
*pcbSignedDataMsg
,
951 BYTE
*pbSignedDataMsg
)
954 LONG base_offset
, cabsize
;
957 DWORD cert_offset
, cert_size
, dwRead
;
959 TRACE("(%p %p %d %p %p)\n", pSubjectInfo
, pdwEncodingType
, dwIndex
,
960 pcbSignedDataMsg
, pbSignedDataMsg
);
963 * FIXME: I just noticed that I am memorizing the initial file pointer
964 * offset and restoring it before reading in the rest of the header
965 * information in the cabinet. Perhaps that's correct -- that is, perhaps
966 * this API is supposed to support "streaming" cabinets which are embedded
967 * in other files, or cabinets which begin at file offsets other than zero.
968 * Otherwise, I should instead go to the absolute beginning of the file.
969 * (Either way, the semantics of wine's FDICopy require me to leave the
970 * file pointer where it is afterwards -- If Windows does not do so, we
971 * ought to duplicate the native behavior in the FDIIsCabinet API, not here.
973 * So, the answer lies in Windows; will native cabinet.dll recognize a
974 * cabinet "file" embedded in another file? Note that cabextract.c does
975 * support this, which implies that Microsoft's might. I haven't tried it
976 * yet so I don't know. ATM, most of wine's FDI cabinet routines (except
977 * this one) would not work in this way. To fix it, we could just make the
978 * various references to absolute file positions in the code relative to an
979 * initial "beginning" offset. Because the FDICopy API doesn't take a
980 * file-handle like this one, we would therein need to search through the
981 * file for the beginning of the cabinet (as we also do in cabextract.c).
982 * Note that this limits us to a maximum of one cabinet per. file: the first.
984 * So, in summary: either the code below is wrong, or the rest of fdi.c is
985 * wrong... I cannot imagine that both are correct ;) One of these flaws
986 * should be fixed after determining the behavior on Windows. We ought
987 * to check both FDIIsCabinet and FDICopy for the right behavior.
992 /* get basic offset & size info */
993 base_offset
= SetFilePointer(pSubjectInfo
->hFile
, 0L, NULL
, SEEK_CUR
);
995 if (SetFilePointer(pSubjectInfo
->hFile
, 0, NULL
, SEEK_END
) == INVALID_SET_FILE_POINTER
)
997 TRACE("seek error\n");
1001 cabsize
= SetFilePointer(pSubjectInfo
->hFile
, 0L, NULL
, SEEK_CUR
);
1002 if ((cabsize
== -1) || (base_offset
== -1) ||
1003 (SetFilePointer(pSubjectInfo
->hFile
, base_offset
, NULL
, SEEK_SET
) == INVALID_SET_FILE_POINTER
))
1005 TRACE("seek error\n");
1009 /* read in the CFHEADER */
1010 if (!ReadFile(pSubjectInfo
->hFile
, buf
, cfhead_SIZEOF
, &dwRead
, NULL
) ||
1011 dwRead
!= cfhead_SIZEOF
)
1013 TRACE("reading header failed\n");
1017 /* check basic MSCF signature */
1018 if (EndGetI32(buf
+cfhead_Signature
) != 0x4643534d)
1020 WARN("cabinet signature not present\n");
1024 /* Ignore the number of folders and files and the set and cabinet IDs */
1026 /* check the header revision */
1027 if ((buf
[cfhead_MajorVersion
] > 1) ||
1028 (buf
[cfhead_MajorVersion
] == 1 && buf
[cfhead_MinorVersion
] > 3))
1030 WARN("cabinet format version > 1.3\n");
1034 /* pull the flags out */
1035 flags
= EndGetI16(buf
+cfhead_Flags
);
1037 if (!(flags
& cfheadRESERVE_PRESENT
))
1039 TRACE("no header present, not signed\n");
1043 if (!ReadFile(pSubjectInfo
->hFile
, buf
, cfheadext_SIZEOF
, &dwRead
, NULL
) ||
1044 dwRead
!= cfheadext_SIZEOF
)
1046 ERR("bunk reserve-sizes?\n");
1050 header_resv
= EndGetI16(buf
+cfheadext_HeaderReserved
);
1053 TRACE("no header_resv, not signed\n");
1056 else if (header_resv
< cfsigninfo_SIZEOF
)
1058 TRACE("header_resv too small, not signed\n");
1062 if (header_resv
> 60000)
1064 WARN("WARNING; header reserved space > 60000\n");
1067 if (!ReadFile(pSubjectInfo
->hFile
, buf
, cfsigninfo_SIZEOF
, &dwRead
, NULL
) ||
1068 dwRead
!= cfsigninfo_SIZEOF
)
1070 ERR("couldn't read reserve\n");
1074 cert_offset
= EndGetI32(buf
+cfsigninfo_CertOffset
);
1075 TRACE("cert_offset: %d\n", cert_offset
);
1076 cert_size
= EndGetI32(buf
+cfsigninfo_CertSize
);
1077 TRACE("cert_size: %d\n", cert_size
);
1079 /* The redundant checks are to avoid wraparound */
1080 if (cert_offset
> cabsize
|| cert_size
> cabsize
||
1081 cert_offset
+ cert_size
> cabsize
)
1083 WARN("offset beyond file, not attempting to read\n");
1087 SetFilePointer(pSubjectInfo
->hFile
, base_offset
, NULL
, SEEK_SET
);
1088 if (!pbSignedDataMsg
)
1090 *pcbSignedDataMsg
= cert_size
;
1093 if (*pcbSignedDataMsg
< cert_size
)
1095 *pcbSignedDataMsg
= cert_size
;
1096 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1099 if (SetFilePointer(pSubjectInfo
->hFile
, cert_offset
, NULL
, SEEK_SET
) == INVALID_SET_FILE_POINTER
)
1101 ERR("couldn't seek to cert location\n");
1104 if (!ReadFile(pSubjectInfo
->hFile
, pbSignedDataMsg
, cert_size
, &dwRead
,
1105 NULL
) || dwRead
!= cert_size
)
1107 ERR("couldn't read cert\n");
1110 /* The encoding of the files I've seen appears to be in ASN.1
1111 * format, and there isn't a field indicating the type, so assume it
1114 *pdwEncodingType
= X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
;
1118 static BOOL
WINTRUST_GetSignedMsgFromCatFile(SIP_SUBJECTINFO
*pSubjectInfo
,
1119 DWORD
*pdwEncodingType
, DWORD dwIndex
, DWORD
*pcbSignedDataMsg
,
1120 BYTE
*pbSignedDataMsg
)
1124 TRACE("(%p %p %d %p %p)\n", pSubjectInfo
, pdwEncodingType
, dwIndex
,
1125 pcbSignedDataMsg
, pbSignedDataMsg
);
1127 if (!pbSignedDataMsg
)
1129 *pcbSignedDataMsg
= GetFileSize(pSubjectInfo
->hFile
, NULL
);
1134 DWORD len
= GetFileSize(pSubjectInfo
->hFile
, NULL
);
1136 if (*pcbSignedDataMsg
< len
)
1138 *pcbSignedDataMsg
= len
;
1139 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1144 ret
= ReadFile(pSubjectInfo
->hFile
, pbSignedDataMsg
, len
,
1145 pcbSignedDataMsg
, NULL
);
1147 *pdwEncodingType
= X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
;
1153 /***********************************************************************
1154 * CryptSIPGetSignedDataMsg (WINTRUST.@)
1156 BOOL WINAPI
CryptSIPGetSignedDataMsg(SIP_SUBJECTINFO
* pSubjectInfo
, DWORD
* pdwEncodingType
,
1157 DWORD dwIndex
, DWORD
* pcbSignedDataMsg
, BYTE
* pbSignedDataMsg
)
1159 static const GUID unknown
= { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,
1160 0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
1161 static const GUID cabGUID
= { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,
1162 0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
1163 static const GUID catGUID
= { 0xDE351A43, 0x8E59, 0x11D0, { 0x8C,0x47,
1164 0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
1167 TRACE("(%p %p %d %p %p)\n", pSubjectInfo
, pdwEncodingType
, dwIndex
,
1168 pcbSignedDataMsg
, pbSignedDataMsg
);
1170 if (!memcmp(pSubjectInfo
->pgSubjectType
, &unknown
, sizeof(unknown
)))
1171 ret
= WINTRUST_GetSignedMsgFromPEFile(pSubjectInfo
, pdwEncodingType
,
1172 dwIndex
, pcbSignedDataMsg
, pbSignedDataMsg
);
1173 else if (!memcmp(pSubjectInfo
->pgSubjectType
, &cabGUID
, sizeof(cabGUID
)))
1174 ret
= WINTRUST_GetSignedMsgFromCabFile(pSubjectInfo
, pdwEncodingType
,
1175 dwIndex
, pcbSignedDataMsg
, pbSignedDataMsg
);
1176 else if (!memcmp(pSubjectInfo
->pgSubjectType
, &catGUID
, sizeof(catGUID
)))
1177 ret
= WINTRUST_GetSignedMsgFromCatFile(pSubjectInfo
, pdwEncodingType
,
1178 dwIndex
, pcbSignedDataMsg
, pbSignedDataMsg
);
1181 FIXME("unimplemented for subject type %s\n",
1182 debugstr_guid(pSubjectInfo
->pgSubjectType
));
1186 TRACE("returning %d\n", ret
);
1190 /***********************************************************************
1191 * CryptSIPPutSignedDataMsg (WINTRUST.@)
1193 BOOL WINAPI
CryptSIPPutSignedDataMsg(SIP_SUBJECTINFO
* pSubjectInfo
, DWORD pdwEncodingType
,
1194 DWORD
* pdwIndex
, DWORD cbSignedDataMsg
, BYTE
* pbSignedDataMsg
)
1196 FIXME("(%p %d %p %d %p) stub\n", pSubjectInfo
, pdwEncodingType
, pdwIndex
,
1197 cbSignedDataMsg
, pbSignedDataMsg
);
1202 /***********************************************************************
1203 * CryptSIPRemoveSignedDataMsg (WINTRUST.@)
1205 BOOL WINAPI
CryptSIPRemoveSignedDataMsg(SIP_SUBJECTINFO
* pSubjectInfo
,
1208 FIXME("(%p %d) stub\n", pSubjectInfo
, dwIndex
);
1213 /***********************************************************************
1214 * CryptSIPVerifyIndirectData (WINTRUST.@)
1216 BOOL WINAPI
CryptSIPVerifyIndirectData(SIP_SUBJECTINFO
* pSubjectInfo
,
1217 SIP_INDIRECT_DATA
* pIndirectData
)
1219 FIXME("(%p %p) stub\n", pSubjectInfo
, pIndirectData
);