wintrust: Implement CryptCATAdminResolveCatalogPath and CryptCATCatalogInfoFromContext.
[wine/wine64.git] / dlls / wintrust / crypt.c
blob44ab4bb9be0ba89d0b49941b95d46323f49f5155
1 /*
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
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wintrust.h"
30 #include "mscat.h"
31 #include "mssip.h"
32 #include "imagehlp.h"
33 #include "winternl.h"
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' */
44 struct cryptcat
46 DWORD magic;
47 HANDLE msg;
48 DWORD encoding;
49 CTL_INFO *inner;
50 DWORD inner_len;
51 GUID subject;
52 DWORD attr_count;
53 CRYPTCATATTRIBUTE *attr;
56 struct catadmin
58 DWORD magic;
59 WCHAR path[MAX_PATH];
60 HANDLE find;
63 struct catinfo
65 DWORD magic;
66 WCHAR file[MAX_PATH];
69 static HCATINFO create_catinfo(const WCHAR *filename)
71 struct catinfo *ci;
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;
80 return ci;
83 /***********************************************************************
84 * CryptCATAdminAcquireContext (WINTRUST.@)
86 * Get a catalog administrator context handle.
88 * PARAMS
89 * catAdmin [O] Pointer to the context handle.
90 * sys [I] Pointer to a GUID for the needed subsystem.
91 * dwFlags [I] Reserved.
93 * RETURNS
94 * Success: TRUE. catAdmin contains the context handle.
95 * Failure: FALSE.
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];
112 struct catadmin *ca;
114 TRACE("%p %s %x\n", catAdmin, debugstr_guid(sys), dwFlags);
116 if (!catAdmin)
118 SetLastError(ERROR_INVALID_PARAMETER);
119 return FALSE;
121 if (!(ca = HeapAlloc(GetProcessHeap(), 0, sizeof(*ca))))
123 SetLastError(ERROR_OUTOFMEMORY);
124 return FALSE;
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],
137 sys->Data4[7]);
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;
145 *catAdmin = ca;
146 return TRUE;
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;
157 struct catinfo *ci;
158 WCHAR *target;
159 DWORD len;
161 TRACE("%p %s %s %d\n", catAdmin, debugstr_w(catalogFile),
162 debugstr_w(selectBaseName), flags);
164 if (!selectBaseName)
166 FIXME("NULL basename not handled\n");
167 SetLastError(ERROR_INVALID_PARAMETER);
168 return NULL;
170 if (!ca || ca->magic != CATADMIN_MAGIC || !catalogFile || flags)
172 SetLastError(ERROR_INVALID_PARAMETER);
173 return NULL;
176 len = strlenW(ca->path) + strlenW(selectBaseName) + 2;
177 if (!(target = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
179 SetLastError(ERROR_OUTOFMEMORY);
180 return NULL;
182 strcpyW(target, ca->path);
183 strcatW(target, slashW);
184 strcatW(target, selectBaseName);
186 if (!CopyFileW(catalogFile, target, FALSE))
188 HeapFree(GetProcessHeap(), 0, target);
189 return NULL;
191 if (!(ci = HeapAlloc(GetProcessHeap(), 0, sizeof(*ci))))
193 HeapFree(GetProcessHeap(), 0, target);
194 SetLastError(ERROR_OUTOFMEMORY);
195 return NULL;
197 ci->magic = CATINFO_MAGIC;
198 strcpyW(ci->file, target);
200 HeapFree(GetProcessHeap(), 0, target);
201 return ci;
204 /***********************************************************************
205 * CryptCATAdminCalcHashFromFileHandle (WINTRUST.@)
207 BOOL WINAPI CryptCATAdminCalcHashFromFileHandle(HANDLE hFile, DWORD* pcbHash,
208 BYTE* pbHash, DWORD dwFlags )
210 BOOL ret = FALSE;
212 TRACE("%p %p %p %x\n", hFile, pcbHash, pbHash, dwFlags);
214 if (!hFile || !pcbHash || dwFlags)
216 SetLastError(ERROR_INVALID_PARAMETER);
217 return FALSE;
219 if (*pcbHash < 20)
221 *pcbHash = 20;
222 SetLastError(ERROR_INSUFFICIENT_BUFFER);
223 return TRUE;
226 *pcbHash = 20;
227 if (pbHash)
229 HCRYPTPROV prov;
230 HCRYPTHASH hash;
231 DWORD bytes_read;
232 BYTE *buffer;
234 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, 4096)))
236 SetLastError(ERROR_OUTOFMEMORY);
237 return FALSE;
239 ret = CryptAcquireContextW(&prov, NULL, MS_DEF_PROV_W, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
240 if (!ret)
242 HeapFree(GetProcessHeap(), 0, buffer);
243 return FALSE;
245 ret = CryptCreateHash(prov, CALG_SHA1, 0, 0, &hash);
246 if (!ret)
248 HeapFree(GetProcessHeap(), 0, buffer);
249 CryptReleaseContext(prov, 0);
250 return FALSE;
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);
262 return ret;
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;
278 HCRYPTPROV prov;
279 DWORD size;
280 BOOL ret;
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);
287 return NULL;
289 if (phPrevCatInfo) prev = *phPrevCatInfo;
291 ret = CryptAcquireContextW(&prov, NULL, MS_DEF_PROV_W, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
292 if (!ret) return NULL;
294 if (!prev)
296 WCHAR *path;
298 size = strlenW(ca->path) * sizeof(WCHAR) + sizeof(globW);
299 if (!(path = HeapAlloc(GetProcessHeap(), 0, size)))
301 CryptReleaseContext(prov, 0);
302 SetLastError(ERROR_OUTOFMEMORY);
303 return NULL;
305 strcpyW(path, ca->path);
306 strcatW(path, globW);
308 FindClose(ca->find);
309 ca->find = FindFirstFileW(path, &data);
311 HeapFree(GetProcessHeap(), 0, path);
312 if (ca->find == INVALID_HANDLE_VALUE)
314 CryptReleaseContext(prov, 0);
315 return NULL;
318 else if (!FindNextFileW(ca->find, &data))
320 CryptCATAdminReleaseCatalogContext(hCatAdmin, prev, 0);
321 CryptReleaseContext(prov, 0);
322 return NULL;
325 while (1)
327 WCHAR *filename;
328 CRYPTCATMEMBER *member = NULL;
329 struct catinfo *ci;
330 HANDLE hcat;
332 size = (strlenW(ca->path) + strlenW(data.cFileName) + 2) * sizeof(WCHAR);
333 if (!(filename = HeapAlloc(GetProcessHeap(), 0, size)))
335 SetLastError(ERROR_OUTOFMEMORY);
336 return NULL;
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());
346 continue;
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);
353 continue;
355 if (!memcmp(member->pIndirectData->Digest.pbData, pbHash, cbHash))
357 TRACE("file %s matches\n", debugstr_w(data.cFileName));
359 CryptCATClose(hcat);
360 CryptReleaseContext(prov, 0);
361 if (!phPrevCatInfo)
363 FindClose(ca->find);
364 ca->find = INVALID_HANDLE_VALUE;
366 ci = create_catinfo(filename);
367 HeapFree(GetProcessHeap(), 0, filename);
368 return ci;
371 CryptCATClose(hcat);
372 HeapFree(GetProcessHeap(), 0, filename);
374 if (!FindNextFileW(ca->find, &data))
376 FindClose(ca->find);
377 ca->find = INVALID_HANDLE_VALUE;
378 CryptReleaseContext(prov, 0);
379 return NULL;
382 return NULL;
385 /***********************************************************************
386 * CryptCATAdminReleaseCatalogContext (WINTRUST.@)
388 * Release a catalog context handle.
390 * PARAMS
391 * hCatAdmin [I] Context handle.
392 * hCatInfo [I] Catalog handle.
393 * dwFlags [I] Reserved.
395 * RETURNS
396 * Success: TRUE.
397 * Failure: FALSE.
400 BOOL WINAPI CryptCATAdminReleaseCatalogContext(HCATADMIN hCatAdmin,
401 HCATINFO hCatInfo,
402 DWORD dwFlags)
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);
412 return FALSE;
414 ci->magic = 0;
415 return HeapFree(GetProcessHeap(), 0, ci);
418 /***********************************************************************
419 * CryptCATAdminReleaseContext (WINTRUST.@)
421 * Release a catalog administrator context handle.
423 * PARAMS
424 * catAdmin [I] Context handle.
425 * dwFlags [I] Reserved.
427 * RETURNS
428 * Success: TRUE.
429 * Failure: FALSE.
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);
441 return FALSE;
443 if (ca->find != INVALID_HANDLE_VALUE) FindClose(ca->find);
444 ca->magic = 0;
445 return HeapFree(GetProcessHeap(), 0, ca);
448 /***********************************************************************
449 * CryptCATAdminRemoveCatalog (WINTRUST.@)
451 * Remove a catalog file.
453 * PARAMS
454 * catAdmin [I] Context handle.
455 * pwszCatalogFile [I] Catalog file.
456 * dwFlags [I] Reserved.
458 * RETURNS
459 * Success: TRUE.
460 * Failure: FALSE.
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);
472 return FALSE;
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);
491 return FALSE;
493 strcpyW(info->wszCatalogFile, ca->path);
494 strcatW(info->wszCatalogFile, slashW);
495 strcatW(info->wszCatalogFile, catalog_file);
497 return TRUE;
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);
512 return FALSE;
514 HeapFree(GetProcessHeap(), 0, cc->attr);
515 HeapFree(GetProcessHeap(), 0, cc->inner);
516 CryptMsgClose(cc->msg);
518 cc->magic = 0;
519 HeapFree(GetProcessHeap(), 0, cc);
520 return TRUE;
523 /***********************************************************************
524 * CryptCATEnumerateMember (WINTRUST.@)
526 CRYPTCATMEMBER * WINAPI CryptCATEnumerateMember(HANDLE hCatalog, CRYPTCATMEMBER *prev)
528 struct cryptcat *cc = hCatalog;
529 CRYPTCATMEMBER *member = prev;
530 CTL_ENTRY *entry;
531 DWORD size, i;
533 TRACE("%p, %p\n", hCatalog, prev);
535 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
537 SetLastError(ERROR_INVALID_PARAMETER);
538 return NULL;
541 /* dumping the contents makes me think that dwReserved is the iteration number */
542 if (!member)
544 if (!(member = HeapAlloc(GetProcessHeap(), 0, sizeof(*member))))
546 SetLastError(ERROR_OUTOFMEMORY);
547 return NULL;
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);
563 goto error;
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);
581 continue;
583 if (!strcmp(attr->pszObjId, CAT_MEMBERINFO_OBJID))
585 CAT_MEMBERINFO *mi;
586 BOOL ret;
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);
596 goto error;
598 ret = CryptDecodeObject(cc->encoding, CAT_MEMBERINFO_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, mi, &size);
599 if (ret)
601 UNICODE_STRING guid;
603 member->dwCertVersion = mi->dwCertVersion;
604 RtlInitUnicodeString(&guid, mi->pwszSubjGuid);
605 if (RtlGUIDFromString(&guid, &member->gSubjectType))
607 HeapFree(GetProcessHeap(), 0, mi);
608 goto error;
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);
626 goto error;
628 CryptDecodeObject(cc->encoding, SPC_INDIRECT_DATA_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, member->pIndirectData, &size);
630 else
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);
639 goto error;
641 size = (2 * member->pIndirectData->Digest.cbData + 1) * sizeof(WCHAR);
642 if (member->pwszReferenceTag)
643 member->pwszReferenceTag = HeapReAlloc(GetProcessHeap(), 0, member->pwszReferenceTag, size);
644 else
645 member->pwszReferenceTag = HeapAlloc(GetProcessHeap(), 0, size);
647 if (!member->pwszReferenceTag)
649 SetLastError(ERROR_OUTOFMEMORY);
650 goto error;
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++)
655 DWORD sub;
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;
663 return member;
665 error:
666 HeapFree(GetProcessHeap(), 0, member->pIndirectData);
667 HeapFree(GetProcessHeap(), 0, member->pwszReferenceTag);
668 HeapFree(GetProcessHeap(), 0, member);
669 return NULL;
672 static CTL_INFO *decode_inner_content(HANDLE hmsg, DWORD encoding, DWORD *len)
674 DWORD size;
675 LPSTR oid = NULL;
676 BYTE *buffer = NULL;
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);
683 return NULL;
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);
690 goto out;
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);
697 goto out;
699 if (!CryptDecodeObject(encoding, oid, buffer, size, 0, inner, &size)) goto out;
700 *len = size;
702 out:
703 HeapFree(GetProcessHeap(), 0, oid);
704 HeapFree(GetProcessHeap(), 0, buffer);
705 return inner;
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);
721 return FALSE;
723 strcpyW(info->wszCatalogFile, ci->file);
724 return TRUE;
727 /***********************************************************************
728 * CryptCATOpen (WINTRUST.@)
730 HANDLE WINAPI CryptCATOpen(LPWSTR pwszFileName, DWORD fdwOpenFlags, HCRYPTPROV hProv,
731 DWORD dwPublicVersion, DWORD dwEncodingType)
733 HANDLE file, hmsg;
734 BYTE *buffer = NULL;
735 DWORD size, flags = OPEN_EXISTING;
736 struct cryptcat *cc;
738 TRACE("%s, %x, %lx, %x, %x\n", debugstr_w(pwszFileName), fdwOpenFlags,
739 hProv, dwPublicVersion, dwEncodingType);
741 if (!pwszFileName)
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)))
759 CloseHandle(file);
760 SetLastError(ERROR_OUTOFMEMORY);
761 return INVALID_HANDLE_VALUE;
763 if (!(hmsg = CryptMsgOpenToDecode(dwEncodingType, 0, 0, hProv, NULL, NULL)))
765 CloseHandle(file);
766 HeapFree(GetProcessHeap(), 0, buffer);
767 return INVALID_HANDLE_VALUE;
769 if (!ReadFile(file, buffer, size, &size, NULL) || !CryptMsgUpdate(hmsg, buffer, size, TRUE))
771 CloseHandle(file);
772 HeapFree(GetProcessHeap(), 0, buffer);
773 CryptMsgClose(hmsg);
774 return INVALID_HANDLE_VALUE;
776 HeapFree(GetProcessHeap(), 0, buffer);
777 CloseHandle(file);
779 size = sizeof(DWORD);
780 if (!(cc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cc))))
782 CryptMsgClose(hmsg);
783 SetLastError(ERROR_OUTOFMEMORY);
784 return INVALID_HANDLE_VALUE;
787 cc->msg = hmsg;
788 cc->encoding = dwEncodingType;
789 if (CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_COUNT_PARAM, 0, &cc->attr_count, &size))
791 DWORD i, sum = 0;
792 BYTE *p;
794 for (i = 0; i < cc->attr_count; i++)
796 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size))
798 CryptMsgClose(hmsg);
799 return INVALID_HANDLE_VALUE;
801 sum += size;
803 if (!(cc->attr = HeapAlloc(GetProcessHeap(), 0, sizeof(*cc->attr) * cc->attr_count + sum)))
805 CryptMsgClose(hmsg);
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))
814 CryptMsgClose(hmsg);
815 HeapFree(GetProcessHeap(), 0, cc->attr);
816 return INVALID_HANDLE_VALUE;
818 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, p, &size))
820 CryptMsgClose(hmsg);
821 HeapFree(GetProcessHeap(), 0, cc->attr);
822 return INVALID_HANDLE_VALUE;
824 p += size;
826 cc->inner = decode_inner_content(hmsg, dwEncodingType, &cc->inner_len);
827 if (!cc->inner || !CryptSIPRetrieveSubjectGuid(pwszFileName, NULL, &cc->subject))
829 CryptMsgClose(hmsg);
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;
836 return cc;
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);
849 return FALSE;
852 static BOOL WINTRUST_GetSignedMsgFromPEFile(SIP_SUBJECTINFO *pSubjectInfo,
853 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
854 BYTE *pbSignedDataMsg)
856 BOOL ret;
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);
868 if (ret)
869 *pcbSignedDataMsg = cert.dwLength;
871 else
873 DWORD len = 0;
875 ret = ImageGetCertificateData(pSubjectInfo->hFile, dwIndex, NULL, &len);
876 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
877 goto error;
878 pCert = HeapAlloc(GetProcessHeap(), 0, len);
879 if (!pCert)
881 ret = FALSE;
882 goto error;
884 ret = ImageGetCertificateData(pSubjectInfo->hFile, dwIndex, pCert,
885 &len);
886 if (!ret)
887 goto error;
888 if (*pcbSignedDataMsg < pCert->dwLength)
890 *pcbSignedDataMsg = pCert->dwLength;
891 SetLastError(ERROR_INSUFFICIENT_BUFFER);
892 ret = FALSE;
894 else
896 memcpy(pbSignedDataMsg, pCert->bCertificate, pCert->dwLength);
897 switch (pCert->wCertificateType)
899 case WIN_CERT_TYPE_X509:
900 *pdwEncodingType = X509_ASN_ENCODING;
901 break;
902 case WIN_CERT_TYPE_PKCS_SIGNED_DATA:
903 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
904 break;
905 default:
906 FIXME("don't know what to do for encoding type %d\n",
907 pCert->wCertificateType);
908 *pdwEncodingType = 0;
912 error:
913 HeapFree(GetProcessHeap(), 0, pCert);
914 return ret;
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)
930 /* flags */
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? */
945 DWORD dwCertOffset;
946 DWORD cbCertBlock;
947 } CAB_SIGNINFO, *PCAB_SIGNINFO;
949 static BOOL WINTRUST_GetSignedMsgFromCabFile(SIP_SUBJECTINFO *pSubjectInfo,
950 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
951 BYTE *pbSignedDataMsg)
953 int header_resv;
954 LONG base_offset, cabsize;
955 USHORT flags;
956 BYTE buf[64];
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.
989 * -gmt
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");
998 return FALSE;
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");
1006 return FALSE;
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");
1014 return FALSE;
1017 /* check basic MSCF signature */
1018 if (EndGetI32(buf+cfhead_Signature) != 0x4643534d)
1020 WARN("cabinet signature not present\n");
1021 return FALSE;
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");
1031 return FALSE;
1034 /* pull the flags out */
1035 flags = EndGetI16(buf+cfhead_Flags);
1037 if (!(flags & cfheadRESERVE_PRESENT))
1039 TRACE("no header present, not signed\n");
1040 return FALSE;
1043 if (!ReadFile(pSubjectInfo->hFile, buf, cfheadext_SIZEOF, &dwRead, NULL) ||
1044 dwRead != cfheadext_SIZEOF)
1046 ERR("bunk reserve-sizes?\n");
1047 return FALSE;
1050 header_resv = EndGetI16(buf+cfheadext_HeaderReserved);
1051 if (!header_resv)
1053 TRACE("no header_resv, not signed\n");
1054 return FALSE;
1056 else if (header_resv < cfsigninfo_SIZEOF)
1058 TRACE("header_resv too small, not signed\n");
1059 return FALSE;
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");
1071 return FALSE;
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");
1084 return FALSE;
1087 SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET);
1088 if (!pbSignedDataMsg)
1090 *pcbSignedDataMsg = cert_size;
1091 return TRUE;
1093 if (*pcbSignedDataMsg < cert_size)
1095 *pcbSignedDataMsg = cert_size;
1096 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1097 return FALSE;
1099 if (SetFilePointer(pSubjectInfo->hFile, cert_offset, NULL, SEEK_SET) == INVALID_SET_FILE_POINTER)
1101 ERR("couldn't seek to cert location\n");
1102 return FALSE;
1104 if (!ReadFile(pSubjectInfo->hFile, pbSignedDataMsg, cert_size, &dwRead,
1105 NULL) || dwRead != cert_size)
1107 ERR("couldn't read cert\n");
1108 return FALSE;
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
1112 * always is.
1114 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1115 return TRUE;
1118 static BOOL WINTRUST_GetSignedMsgFromCatFile(SIP_SUBJECTINFO *pSubjectInfo,
1119 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1120 BYTE *pbSignedDataMsg)
1122 BOOL ret;
1124 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1125 pcbSignedDataMsg, pbSignedDataMsg);
1127 if (!pbSignedDataMsg)
1129 *pcbSignedDataMsg = GetFileSize(pSubjectInfo->hFile, NULL);
1130 ret = TRUE;
1132 else
1134 DWORD len = GetFileSize(pSubjectInfo->hFile, NULL);
1136 if (*pcbSignedDataMsg < len)
1138 *pcbSignedDataMsg = len;
1139 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1140 ret = FALSE;
1142 else
1144 ret = ReadFile(pSubjectInfo->hFile, pbSignedDataMsg, len,
1145 pcbSignedDataMsg, NULL);
1146 if (ret)
1147 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1150 return ret;
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 }};
1165 BOOL ret;
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);
1179 else
1181 FIXME("unimplemented for subject type %s\n",
1182 debugstr_guid(pSubjectInfo->pgSubjectType));
1183 ret = FALSE;
1186 TRACE("returning %d\n", ret);
1187 return 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);
1199 return FALSE;
1202 /***********************************************************************
1203 * CryptSIPRemoveSignedDataMsg (WINTRUST.@)
1205 BOOL WINAPI CryptSIPRemoveSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo,
1206 DWORD dwIndex)
1208 FIXME("(%p %d) stub\n", pSubjectInfo, dwIndex);
1210 return FALSE;
1213 /***********************************************************************
1214 * CryptSIPVerifyIndirectData (WINTRUST.@)
1216 BOOL WINAPI CryptSIPVerifyIndirectData(SIP_SUBJECTINFO* pSubjectInfo,
1217 SIP_INDIRECT_DATA* pIndirectData)
1219 FIXME("(%p %p) stub\n", pSubjectInfo, pIndirectData);
1221 return FALSE;