mf/session: Forward more events to the application.
[wine/zf.git] / dlls / wintrust / crypt.c
blobfcf419c76d13a56a24b11ace3a011a639073dd30
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"
37 WINE_DEFAULT_DEBUG_CHANNEL(wintrust);
39 #define CATADMIN_MAGIC 0x43415441 /* 'CATA' */
40 #define CRYPTCAT_MAGIC 0x43415443 /* 'CATC' */
41 #define CATINFO_MAGIC 0x43415449 /* 'CATI' */
43 struct cryptcat
45 DWORD magic;
46 HCRYPTMSG msg;
47 DWORD encoding;
48 CTL_INFO *inner;
49 DWORD inner_len;
50 GUID subject;
51 DWORD attr_count;
52 CRYPTCATATTRIBUTE *attr;
55 struct catadmin
57 DWORD magic;
58 WCHAR path[MAX_PATH];
59 HANDLE find;
62 struct catinfo
64 DWORD magic;
65 WCHAR file[MAX_PATH];
68 static HCATINFO create_catinfo(const WCHAR *filename)
70 struct catinfo *ci;
72 if (!(ci = HeapAlloc(GetProcessHeap(), 0, sizeof(*ci))))
74 SetLastError(ERROR_OUTOFMEMORY);
75 return INVALID_HANDLE_VALUE;
77 lstrcpyW(ci->file, filename);
78 ci->magic = CATINFO_MAGIC;
79 return ci;
82 /***********************************************************************
83 * CryptCATAdminAcquireContext (WINTRUST.@)
85 * Get a catalog administrator context handle.
87 * PARAMS
88 * catAdmin [O] Pointer to the context handle.
89 * sys [I] Pointer to a GUID for the needed subsystem.
90 * dwFlags [I] Reserved.
92 * RETURNS
93 * Success: TRUE. catAdmin contains the context handle.
94 * Failure: FALSE.
97 BOOL WINAPI CryptCATAdminAcquireContext(HCATADMIN *catAdmin,
98 const GUID *sys, DWORD dwFlags)
100 static const WCHAR catroot[] =
101 {'\\','c','a','t','r','o','o','t',0};
102 static const WCHAR fmt[] =
103 {'%','s','\\','{','%','0','8','x','-','%','0','4','x','-','%','0',
104 '4','x','-','%','0','2','x','%','0','2','x','-','%','0','2','x',
105 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x',
106 '%','0','2','x','}',0};
107 static const GUID defsys =
108 {0x127d0a1d,0x4ef2,0x11d1,{0x86,0x08,0x00,0xc0,0x4f,0xc2,0x95,0xee}};
110 WCHAR catroot_dir[MAX_PATH];
111 struct catadmin *ca;
113 TRACE("%p %s %x\n", catAdmin, debugstr_guid(sys), dwFlags);
115 if (!catAdmin || dwFlags)
117 SetLastError(ERROR_INVALID_PARAMETER);
118 return FALSE;
120 if (!(ca = HeapAlloc(GetProcessHeap(), 0, sizeof(*ca))))
122 SetLastError(ERROR_OUTOFMEMORY);
123 return FALSE;
126 GetSystemDirectoryW(catroot_dir, MAX_PATH);
127 lstrcatW(catroot_dir, catroot);
129 /* create the directory if it doesn't exist */
130 CreateDirectoryW(catroot_dir, NULL);
132 if (!sys) sys = &defsys;
133 swprintf(ca->path, ARRAY_SIZE(ca->path), fmt, catroot_dir, sys->Data1, sys->Data2,
134 sys->Data3, sys->Data4[0], sys->Data4[1], sys->Data4[2],
135 sys->Data4[3], sys->Data4[4], sys->Data4[5], sys->Data4[6],
136 sys->Data4[7]);
138 /* create the directory if it doesn't exist */
139 CreateDirectoryW(ca->path, NULL);
141 ca->magic = CATADMIN_MAGIC;
142 ca->find = INVALID_HANDLE_VALUE;
144 *catAdmin = ca;
145 return TRUE;
148 /***********************************************************************
149 * CryptCATAdminAcquireContext2 (WINTRUST.@)
151 BOOL WINAPI CryptCATAdminAcquireContext2(HCATADMIN *catAdmin, const GUID *sys, const WCHAR *algorithm,
152 const CERT_STRONG_SIGN_PARA *policy, DWORD flags)
154 FIXME("%p %s %s %p %x stub\n", catAdmin, debugstr_guid(sys), debugstr_w(algorithm), policy, flags);
155 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
156 return FALSE;
159 /***********************************************************************
160 * CryptCATAdminAddCatalog (WINTRUST.@)
162 HCATINFO WINAPI CryptCATAdminAddCatalog(HCATADMIN catAdmin, PWSTR catalogFile,
163 PWSTR selectBaseName, DWORD flags)
165 static const WCHAR slashW[] = {'\\',0};
166 struct catadmin *ca = catAdmin;
167 struct catinfo *ci;
168 WCHAR *target;
169 DWORD len;
171 TRACE("%p %s %s %d\n", catAdmin, debugstr_w(catalogFile),
172 debugstr_w(selectBaseName), flags);
174 if (!selectBaseName)
176 FIXME("NULL basename not handled\n");
177 SetLastError(ERROR_INVALID_PARAMETER);
178 return NULL;
180 if (!ca || ca->magic != CATADMIN_MAGIC || !catalogFile || flags)
182 SetLastError(ERROR_INVALID_PARAMETER);
183 return NULL;
186 len = lstrlenW(ca->path) + lstrlenW(selectBaseName) + 2;
187 if (!(target = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
189 SetLastError(ERROR_OUTOFMEMORY);
190 return NULL;
192 lstrcpyW(target, ca->path);
193 lstrcatW(target, slashW);
194 lstrcatW(target, selectBaseName);
196 if (!CopyFileW(catalogFile, target, FALSE))
198 HeapFree(GetProcessHeap(), 0, target);
199 return NULL;
201 SetFileAttributesW(target, FILE_ATTRIBUTE_SYSTEM);
203 if (!(ci = HeapAlloc(GetProcessHeap(), 0, sizeof(*ci))))
205 HeapFree(GetProcessHeap(), 0, target);
206 SetLastError(ERROR_OUTOFMEMORY);
207 return NULL;
209 ci->magic = CATINFO_MAGIC;
210 lstrcpyW(ci->file, target);
212 HeapFree(GetProcessHeap(), 0, target);
213 return ci;
216 /***********************************************************************
217 * CryptCATAdminCalcHashFromFileHandle (WINTRUST.@)
219 BOOL WINAPI CryptCATAdminCalcHashFromFileHandle(HANDLE hFile, DWORD* pcbHash,
220 BYTE* pbHash, DWORD dwFlags )
222 BOOL ret = FALSE;
224 TRACE("%p %p %p %x\n", hFile, pcbHash, pbHash, dwFlags);
226 if (!hFile || !pcbHash || dwFlags)
228 SetLastError(ERROR_INVALID_PARAMETER);
229 return FALSE;
231 if (*pcbHash < 20)
233 *pcbHash = 20;
234 SetLastError(ERROR_INSUFFICIENT_BUFFER);
235 return TRUE;
238 *pcbHash = 20;
239 if (pbHash)
241 HCRYPTPROV prov;
242 HCRYPTHASH hash;
243 DWORD bytes_read;
244 BYTE *buffer;
246 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, 4096)))
248 SetLastError(ERROR_OUTOFMEMORY);
249 return FALSE;
251 ret = CryptAcquireContextW(&prov, NULL, MS_DEF_PROV_W, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
252 if (!ret)
254 HeapFree(GetProcessHeap(), 0, buffer);
255 return FALSE;
257 ret = CryptCreateHash(prov, CALG_SHA1, 0, 0, &hash);
258 if (!ret)
260 HeapFree(GetProcessHeap(), 0, buffer);
261 CryptReleaseContext(prov, 0);
262 return FALSE;
264 while ((ret = ReadFile(hFile, buffer, 4096, &bytes_read, NULL)) && bytes_read)
266 CryptHashData(hash, buffer, bytes_read, 0);
268 if (ret) ret = CryptGetHashParam(hash, HP_HASHVAL, pbHash, pcbHash, 0);
270 HeapFree(GetProcessHeap(), 0, buffer);
271 CryptDestroyHash(hash);
272 CryptReleaseContext(prov, 0);
274 return ret;
277 /***********************************************************************
278 * CryptCATAdminEnumCatalogFromHash (WINTRUST.@)
280 HCATINFO WINAPI CryptCATAdminEnumCatalogFromHash(HCATADMIN hCatAdmin, BYTE* pbHash,
281 DWORD cbHash, DWORD dwFlags,
282 HCATINFO* phPrevCatInfo )
284 static const WCHAR slashW[] = {'\\',0};
285 static const WCHAR globW[] = {'\\','*','.','c','a','t',0};
287 struct catadmin *ca = hCatAdmin;
288 WIN32_FIND_DATAW data;
289 HCATINFO prev = NULL;
290 HCRYPTPROV prov;
291 DWORD size;
292 BOOL ret;
294 TRACE("%p %p %d %x %p\n", hCatAdmin, pbHash, cbHash, dwFlags, phPrevCatInfo);
296 if (!ca || ca->magic != CATADMIN_MAGIC || !pbHash || cbHash != 20 || dwFlags)
298 SetLastError(ERROR_INVALID_PARAMETER);
299 return NULL;
301 if (phPrevCatInfo) prev = *phPrevCatInfo;
303 ret = CryptAcquireContextW(&prov, NULL, MS_DEF_PROV_W, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
304 if (!ret) return NULL;
306 if (!prev)
308 WCHAR *path;
310 size = lstrlenW(ca->path) * sizeof(WCHAR) + sizeof(globW);
311 if (!(path = HeapAlloc(GetProcessHeap(), 0, size)))
313 CryptReleaseContext(prov, 0);
314 SetLastError(ERROR_OUTOFMEMORY);
315 return NULL;
317 lstrcpyW(path, ca->path);
318 lstrcatW(path, globW);
320 FindClose(ca->find);
321 ca->find = FindFirstFileW(path, &data);
323 HeapFree(GetProcessHeap(), 0, path);
324 if (ca->find == INVALID_HANDLE_VALUE)
326 CryptReleaseContext(prov, 0);
327 return NULL;
330 else if (!FindNextFileW(ca->find, &data))
332 CryptCATAdminReleaseCatalogContext(hCatAdmin, prev, 0);
333 CryptReleaseContext(prov, 0);
334 return NULL;
337 while (1)
339 WCHAR *filename;
340 CRYPTCATMEMBER *member = NULL;
341 struct catinfo *ci;
342 HANDLE hcat;
344 size = (lstrlenW(ca->path) + lstrlenW(data.cFileName) + 2) * sizeof(WCHAR);
345 if (!(filename = HeapAlloc(GetProcessHeap(), 0, size)))
347 SetLastError(ERROR_OUTOFMEMORY);
348 return NULL;
350 lstrcpyW(filename, ca->path);
351 lstrcatW(filename, slashW);
352 lstrcatW(filename, data.cFileName);
354 hcat = CryptCATOpen(filename, CRYPTCAT_OPEN_EXISTING, prov, 0, 0);
355 if (hcat == INVALID_HANDLE_VALUE)
357 WARN("couldn't open %s (%u)\n", debugstr_w(filename), GetLastError());
358 continue;
360 while ((member = CryptCATEnumerateMember(hcat, member)))
362 if (member->pIndirectData->Digest.cbData != cbHash)
364 WARN("amount of hash bytes differs: %u/%u\n", member->pIndirectData->Digest.cbData, cbHash);
365 continue;
367 if (!memcmp(member->pIndirectData->Digest.pbData, pbHash, cbHash))
369 TRACE("file %s matches\n", debugstr_w(data.cFileName));
371 CryptCATClose(hcat);
372 CryptReleaseContext(prov, 0);
373 if (!phPrevCatInfo)
375 FindClose(ca->find);
376 ca->find = INVALID_HANDLE_VALUE;
378 ci = create_catinfo(filename);
379 HeapFree(GetProcessHeap(), 0, filename);
380 return ci;
383 CryptCATClose(hcat);
384 HeapFree(GetProcessHeap(), 0, filename);
386 if (!FindNextFileW(ca->find, &data))
388 FindClose(ca->find);
389 ca->find = INVALID_HANDLE_VALUE;
390 CryptReleaseContext(prov, 0);
391 return NULL;
394 return NULL;
397 /***********************************************************************
398 * CryptCATAdminReleaseCatalogContext (WINTRUST.@)
400 * Release a catalog context handle.
402 * PARAMS
403 * hCatAdmin [I] Context handle.
404 * hCatInfo [I] Catalog handle.
405 * dwFlags [I] Reserved.
407 * RETURNS
408 * Success: TRUE.
409 * Failure: FALSE.
412 BOOL WINAPI CryptCATAdminReleaseCatalogContext(HCATADMIN hCatAdmin,
413 HCATINFO hCatInfo,
414 DWORD dwFlags)
416 struct catinfo *ci = hCatInfo;
417 struct catadmin *ca = hCatAdmin;
419 TRACE("%p %p %x\n", hCatAdmin, hCatInfo, dwFlags);
421 if (!ca || ca->magic != CATADMIN_MAGIC || !ci || ci->magic != CATINFO_MAGIC)
423 SetLastError(ERROR_INVALID_PARAMETER);
424 return FALSE;
426 ci->magic = 0;
427 return HeapFree(GetProcessHeap(), 0, ci);
430 /***********************************************************************
431 * CryptCATAdminReleaseContext (WINTRUST.@)
433 * Release a catalog administrator context handle.
435 * PARAMS
436 * catAdmin [I] Context handle.
437 * dwFlags [I] Reserved.
439 * RETURNS
440 * Success: TRUE.
441 * Failure: FALSE.
444 BOOL WINAPI CryptCATAdminReleaseContext(HCATADMIN hCatAdmin, DWORD dwFlags )
446 struct catadmin *ca = hCatAdmin;
448 TRACE("%p %x\n", hCatAdmin, dwFlags);
450 if (!ca || ca->magic != CATADMIN_MAGIC)
452 SetLastError(ERROR_INVALID_PARAMETER);
453 return FALSE;
455 if (ca->find != INVALID_HANDLE_VALUE) FindClose(ca->find);
456 ca->magic = 0;
457 return HeapFree(GetProcessHeap(), 0, ca);
460 /***********************************************************************
461 * CryptCATAdminRemoveCatalog (WINTRUST.@)
463 * Remove a catalog file.
465 * PARAMS
466 * catAdmin [I] Context handle.
467 * pwszCatalogFile [I] Catalog file.
468 * dwFlags [I] Reserved.
470 * RETURNS
471 * Success: TRUE.
472 * Failure: FALSE.
475 BOOL WINAPI CryptCATAdminRemoveCatalog(HCATADMIN hCatAdmin, LPCWSTR pwszCatalogFile, DWORD dwFlags)
477 struct catadmin *ca = hCatAdmin;
479 TRACE("%p %s %x\n", hCatAdmin, debugstr_w(pwszCatalogFile), dwFlags);
481 if (!ca || ca->magic != CATADMIN_MAGIC)
483 SetLastError(ERROR_INVALID_PARAMETER);
484 return FALSE;
487 /* Only delete when there is a filename and no path */
488 if (pwszCatalogFile && pwszCatalogFile[0] != 0 &&
489 !wcschr(pwszCatalogFile, '\\') && !wcschr(pwszCatalogFile, '/') &&
490 !wcschr(pwszCatalogFile, ':'))
492 static const WCHAR slashW[] = {'\\',0};
493 WCHAR *target;
494 DWORD len;
496 len = lstrlenW(ca->path) + lstrlenW(pwszCatalogFile) + 2;
497 if (!(target = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
499 SetLastError(ERROR_OUTOFMEMORY);
500 return FALSE;
502 lstrcpyW(target, ca->path);
503 lstrcatW(target, slashW);
504 lstrcatW(target, pwszCatalogFile);
506 DeleteFileW(target);
508 HeapFree(GetProcessHeap(), 0, target);
511 return TRUE;
514 /***********************************************************************
515 * CryptCATAdminResolveCatalogPath (WINTRUST.@)
517 BOOL WINAPI CryptCATAdminResolveCatalogPath(HCATADMIN hcatadmin, WCHAR *catalog_file,
518 CATALOG_INFO *info, DWORD flags)
520 static const WCHAR slashW[] = {'\\',0};
521 struct catadmin *ca = hcatadmin;
523 TRACE("%p %s %p %x\n", hcatadmin, debugstr_w(catalog_file), info, flags);
525 if (!ca || ca->magic != CATADMIN_MAGIC || !info || info->cbStruct != sizeof(*info) || flags)
527 SetLastError(ERROR_INVALID_PARAMETER);
528 return FALSE;
530 lstrcpyW(info->wszCatalogFile, ca->path);
531 lstrcatW(info->wszCatalogFile, slashW);
532 lstrcatW(info->wszCatalogFile, catalog_file);
534 return TRUE;
537 /***********************************************************************
538 * CryptCATClose (WINTRUST.@)
540 BOOL WINAPI CryptCATClose(HANDLE hCatalog)
542 struct cryptcat *cc = hCatalog;
544 TRACE("(%p)\n", hCatalog);
546 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
548 SetLastError(ERROR_INVALID_PARAMETER);
549 return FALSE;
551 HeapFree(GetProcessHeap(), 0, cc->attr);
552 HeapFree(GetProcessHeap(), 0, cc->inner);
553 CryptMsgClose(cc->msg);
555 cc->magic = 0;
556 HeapFree(GetProcessHeap(), 0, cc);
557 return TRUE;
560 /***********************************************************************
561 * CryptCATGetAttrInfo (WINTRUST.@)
563 CRYPTCATATTRIBUTE * WINAPI CryptCATGetAttrInfo(HANDLE hCatalog, CRYPTCATMEMBER *member, LPWSTR tag)
565 struct cryptcat *cc = hCatalog;
567 FIXME("%p, %p, %s\n", hCatalog, member, debugstr_w(tag));
569 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
571 SetLastError(ERROR_INVALID_PARAMETER);
572 return NULL;
574 SetLastError(CRYPT_E_NOT_FOUND);
575 return NULL;
578 /***********************************************************************
579 * CryptCATGetCatAttrInfo (WINTRUST.@)
581 CRYPTCATATTRIBUTE * WINAPI CryptCATGetCatAttrInfo(HANDLE hCatalog, LPWSTR tag)
583 struct cryptcat *cc = hCatalog;
585 FIXME("%p, %s\n", hCatalog, debugstr_w(tag));
587 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
589 SetLastError(ERROR_INVALID_PARAMETER);
590 return NULL;
592 SetLastError(CRYPT_E_NOT_FOUND);
593 return NULL;
596 CRYPTCATMEMBER * WINAPI CryptCATGetMemberInfo(HANDLE hCatalog, LPWSTR tag)
598 struct cryptcat *cc = hCatalog;
600 FIXME("%p, %s\n", hCatalog, debugstr_w(tag));
602 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
604 SetLastError(ERROR_INVALID_PARAMETER);
605 return NULL;
607 SetLastError(CRYPT_E_NOT_FOUND);
608 return NULL;
611 /***********************************************************************
612 * CryptCATEnumerateAttr (WINTRUST.@)
614 CRYPTCATATTRIBUTE * WINAPI CryptCATEnumerateAttr(HANDLE hCatalog, CRYPTCATMEMBER *member, CRYPTCATATTRIBUTE *prev)
616 struct cryptcat *cc = hCatalog;
618 FIXME("%p, %p, %p\n", hCatalog, member, prev);
620 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
622 SetLastError(ERROR_INVALID_PARAMETER);
623 return NULL;
625 SetLastError(CRYPT_E_NOT_FOUND);
626 return NULL;
629 /***********************************************************************
630 * CryptCATEnumerateCatAttr (WINTRUST.@)
632 CRYPTCATATTRIBUTE * WINAPI CryptCATEnumerateCatAttr(HANDLE hCatalog, CRYPTCATATTRIBUTE *prev)
634 struct cryptcat *cc = hCatalog;
636 FIXME("%p, %p\n", hCatalog, prev);
638 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
640 SetLastError(ERROR_INVALID_PARAMETER);
641 return NULL;
643 SetLastError(CRYPT_E_NOT_FOUND);
644 return NULL;
647 /***********************************************************************
648 * CryptCATEnumerateMember (WINTRUST.@)
650 CRYPTCATMEMBER * WINAPI CryptCATEnumerateMember(HANDLE hCatalog, CRYPTCATMEMBER *prev)
652 struct cryptcat *cc = hCatalog;
653 CRYPTCATMEMBER *member = prev;
654 CTL_ENTRY *entry;
655 DWORD size, i;
657 TRACE("%p, %p\n", hCatalog, prev);
659 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
661 SetLastError(ERROR_INVALID_PARAMETER);
662 return NULL;
665 /* dumping the contents makes me think that dwReserved is the iteration number */
666 if (!member)
668 if (!(member = HeapAlloc(GetProcessHeap(), 0, sizeof(*member))))
670 SetLastError(ERROR_OUTOFMEMORY);
671 return NULL;
673 member->cbStruct = sizeof(*member);
674 member->pwszFileName = member->pwszReferenceTag = NULL;
675 member->dwReserved = 0;
676 member->hReserved = NULL;
677 member->gSubjectType = cc->subject;
678 member->fdwMemberFlags = 0;
679 member->pIndirectData = NULL;
680 member->dwCertVersion = cc->inner->dwVersion;
682 else member->dwReserved++;
684 if (member->dwReserved >= cc->inner->cCTLEntry)
686 SetLastError(ERROR_INVALID_PARAMETER);
687 goto error;
690 /* list them backwards, like native */
691 entry = &cc->inner->rgCTLEntry[cc->inner->cCTLEntry - member->dwReserved - 1];
693 member->sEncodedIndirectData.cbData = member->sEncodedMemberInfo.cbData = 0;
694 member->sEncodedIndirectData.pbData = member->sEncodedMemberInfo.pbData = NULL;
695 HeapFree(GetProcessHeap(), 0, member->pIndirectData);
696 member->pIndirectData = NULL;
698 for (i = 0; i < entry->cAttribute; i++)
700 CRYPT_ATTRIBUTE *attr = entry->rgAttribute + i;
702 if (attr->cValue != 1)
704 ERR("Can't handle attr->cValue of %u\n", attr->cValue);
705 continue;
707 if (!strcmp(attr->pszObjId, CAT_MEMBERINFO_OBJID))
709 CAT_MEMBERINFO *mi;
710 BOOL ret;
712 member->sEncodedMemberInfo.cbData = attr->rgValue->cbData;
713 member->sEncodedMemberInfo.pbData = attr->rgValue->pbData;
715 CryptDecodeObject(cc->encoding, CAT_MEMBERINFO_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, NULL, &size);
717 if (!(mi = HeapAlloc(GetProcessHeap(), 0, size)))
719 SetLastError(ERROR_OUTOFMEMORY);
720 goto error;
722 ret = CryptDecodeObject(cc->encoding, CAT_MEMBERINFO_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, mi, &size);
723 if (ret)
725 UNICODE_STRING guid;
727 member->dwCertVersion = mi->dwCertVersion;
728 RtlInitUnicodeString(&guid, mi->pwszSubjGuid);
729 if (RtlGUIDFromString(&guid, &member->gSubjectType))
731 HeapFree(GetProcessHeap(), 0, mi);
732 goto error;
735 HeapFree(GetProcessHeap(), 0, mi);
736 if (!ret) goto error;
738 else if (!strcmp(attr->pszObjId, SPC_INDIRECT_DATA_OBJID))
740 /* SPC_INDIRECT_DATA_CONTENT is equal to SIP_INDIRECT_DATA */
742 member->sEncodedIndirectData.cbData = attr->rgValue->cbData;
743 member->sEncodedIndirectData.pbData = attr->rgValue->pbData;
745 CryptDecodeObject(cc->encoding, SPC_INDIRECT_DATA_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, NULL, &size);
747 if (!(member->pIndirectData = HeapAlloc(GetProcessHeap(), 0, size)))
749 SetLastError(ERROR_OUTOFMEMORY);
750 goto error;
752 CryptDecodeObject(cc->encoding, SPC_INDIRECT_DATA_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, member->pIndirectData, &size);
754 else
755 /* this object id should probably be handled in CryptCATEnumerateAttr */
756 FIXME("unhandled object id \"%s\"\n", attr->pszObjId);
759 if (!member->sEncodedMemberInfo.cbData || !member->sEncodedIndirectData.cbData)
761 ERR("Corrupted catalog entry?\n");
762 SetLastError(CRYPT_E_ATTRIBUTES_MISSING);
763 goto error;
765 size = (2 * member->pIndirectData->Digest.cbData + 1) * sizeof(WCHAR);
766 if (member->pwszReferenceTag)
767 member->pwszReferenceTag = HeapReAlloc(GetProcessHeap(), 0, member->pwszReferenceTag, size);
768 else
769 member->pwszReferenceTag = HeapAlloc(GetProcessHeap(), 0, size);
771 if (!member->pwszReferenceTag)
773 SetLastError(ERROR_OUTOFMEMORY);
774 goto error;
776 /* FIXME: reference tag is usually the file hash but doesn't have to be */
777 for (i = 0; i < member->pIndirectData->Digest.cbData; i++)
779 DWORD sub;
781 sub = member->pIndirectData->Digest.pbData[i] >> 4;
782 member->pwszReferenceTag[i * 2] = (sub < 10 ? '0' + sub : 'A' + sub - 10);
783 sub = member->pIndirectData->Digest.pbData[i] & 0xf;
784 member->pwszReferenceTag[i * 2 + 1] = (sub < 10 ? '0' + sub : 'A' + sub - 10);
786 member->pwszReferenceTag[i * 2] = 0;
787 return member;
789 error:
790 HeapFree(GetProcessHeap(), 0, member->pIndirectData);
791 HeapFree(GetProcessHeap(), 0, member->pwszReferenceTag);
792 HeapFree(GetProcessHeap(), 0, member);
793 return NULL;
796 static CTL_INFO *decode_inner_content(HANDLE hmsg, DWORD encoding, DWORD *len)
798 DWORD size;
799 LPSTR oid = NULL;
800 BYTE *buffer = NULL;
801 CTL_INFO *inner = NULL;
803 if (!CryptMsgGetParam(hmsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, NULL, &size)) return NULL;
804 if (!(oid = HeapAlloc(GetProcessHeap(), 0, size)))
806 SetLastError(ERROR_OUTOFMEMORY);
807 return NULL;
809 if (!CryptMsgGetParam(hmsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, oid, &size)) goto out;
810 if (!CryptMsgGetParam(hmsg, CMSG_CONTENT_PARAM, 0, NULL, &size)) goto out;
811 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, size)))
813 SetLastError(ERROR_OUTOFMEMORY);
814 goto out;
816 if (!CryptMsgGetParam(hmsg, CMSG_CONTENT_PARAM, 0, buffer, &size)) goto out;
817 if (!CryptDecodeObject(encoding, oid, buffer, size, 0, NULL, &size)) goto out;
818 if (!(inner = HeapAlloc(GetProcessHeap(), 0, size)))
820 SetLastError(ERROR_OUTOFMEMORY);
821 goto out;
823 if (!CryptDecodeObject(encoding, oid, buffer, size, 0, inner, &size)) goto out;
824 *len = size;
826 out:
827 HeapFree(GetProcessHeap(), 0, oid);
828 HeapFree(GetProcessHeap(), 0, buffer);
829 return inner;
832 /***********************************************************************
833 * CryptCATCatalogInfoFromContext (WINTRUST.@)
835 BOOL WINAPI CryptCATCatalogInfoFromContext(HCATINFO hcatinfo, CATALOG_INFO *info, DWORD flags)
837 struct catinfo *ci = hcatinfo;
839 TRACE("%p, %p, %x\n", hcatinfo, info, flags);
841 if (!hcatinfo || hcatinfo == INVALID_HANDLE_VALUE || ci->magic != CATINFO_MAGIC ||
842 flags || !info || info->cbStruct != sizeof(*info))
844 SetLastError(ERROR_INVALID_PARAMETER);
845 return FALSE;
847 lstrcpyW(info->wszCatalogFile, ci->file);
848 return TRUE;
851 /***********************************************************************
852 * CryptCATPutAttrInfo (WINTRUST.@)
854 CRYPTCATATTRIBUTE * WINAPI CryptCATPutAttrInfo(HANDLE catalog, CRYPTCATMEMBER *member,
855 WCHAR *name, DWORD flags, DWORD size, BYTE *data)
857 FIXME("catalog %p, member %p, name %s, flags %#x, size %u, data %p, stub!\n",
858 catalog, member, debugstr_w(name), flags, size, data);
860 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
861 return NULL;
864 /***********************************************************************
865 * CryptCATPutCatAttrInfo (WINTRUST.@)
867 CRYPTCATATTRIBUTE * WINAPI CryptCATPutCatAttrInfo(HANDLE catalog,
868 WCHAR *name, DWORD flags, DWORD size, BYTE *data)
870 FIXME("catalog %p, name %s, flags %#x, size %u, data %p, stub!\n",
871 catalog, debugstr_w(name), flags, size, data);
873 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
874 return NULL;
877 /***********************************************************************
878 * CryptCATPutMemberInfo (WINTRUST.@)
880 CRYPTCATMEMBER * WINAPI CryptCATPutMemberInfo(HANDLE catalog, WCHAR *filename,
881 WCHAR *member, GUID *subject, DWORD version, DWORD size, BYTE *data)
883 FIXME("catalog %p, filename %s, member %s, subject %s, version %u, size %u, data %p, stub!\n",
884 catalog, debugstr_w(filename), debugstr_w(member), debugstr_guid(subject), version, size, data);
886 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
887 return NULL;
890 /***********************************************************************
891 * CryptCATPersistStore (WINTRUST.@)
893 BOOL WINAPI CryptCATPersistStore(HANDLE catalog)
895 FIXME("catalog %p, stub!\n", catalog);
897 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
898 return FALSE;
901 /***********************************************************************
902 * CryptCATOpen (WINTRUST.@)
904 HANDLE WINAPI CryptCATOpen(WCHAR *filename, DWORD flags, HCRYPTPROV hProv,
905 DWORD dwPublicVersion, DWORD dwEncodingType)
907 HANDLE file, hmsg;
908 BYTE *buffer = NULL;
909 DWORD size, open_mode = OPEN_ALWAYS;
910 struct cryptcat *cc;
911 BOOL valid;
913 TRACE("filename %s, flags %#x, provider %#lx, version %#x, type %#x\n",
914 debugstr_w(filename), flags, hProv, dwPublicVersion, dwEncodingType);
916 if (!filename)
918 SetLastError(ERROR_INVALID_PARAMETER);
919 return INVALID_HANDLE_VALUE;
922 if (!dwEncodingType) dwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
924 if (flags == CRYPTCAT_OPEN_EXISTING)
925 open_mode = OPEN_EXISTING;
926 if (flags & CRYPTCAT_OPEN_CREATENEW)
927 open_mode = CREATE_ALWAYS;
929 file = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL, open_mode, 0, NULL);
930 if (file == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
932 size = GetFileSize(file, NULL);
933 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, size)))
935 CloseHandle(file);
936 SetLastError(ERROR_OUTOFMEMORY);
937 return INVALID_HANDLE_VALUE;
939 if (!(hmsg = CryptMsgOpenToDecode(dwEncodingType, 0, 0, hProv, NULL, NULL)))
941 CloseHandle(file);
942 HeapFree(GetProcessHeap(), 0, buffer);
943 return INVALID_HANDLE_VALUE;
945 if (!size) valid = FALSE;
946 else if (!ReadFile(file, buffer, size, &size, NULL))
948 CloseHandle(file);
949 HeapFree(GetProcessHeap(), 0, buffer);
950 CryptMsgClose(hmsg);
951 return INVALID_HANDLE_VALUE;
953 else valid = CryptMsgUpdate(hmsg, buffer, size, TRUE);
954 HeapFree(GetProcessHeap(), 0, buffer);
955 CloseHandle(file);
957 size = sizeof(DWORD);
958 if (!(cc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cc))))
960 CryptMsgClose(hmsg);
961 SetLastError(ERROR_OUTOFMEMORY);
962 return INVALID_HANDLE_VALUE;
965 cc->msg = hmsg;
966 cc->encoding = dwEncodingType;
967 if (!valid)
969 cc->magic = CRYPTCAT_MAGIC;
970 SetLastError(ERROR_SUCCESS);
971 return cc;
973 else if (CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_COUNT_PARAM, 0, &cc->attr_count, &size))
975 DWORD i, sum = 0;
976 BYTE *p;
978 for (i = 0; i < cc->attr_count; i++)
980 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size))
982 CryptMsgClose(hmsg);
983 HeapFree(GetProcessHeap(), 0, cc);
984 return INVALID_HANDLE_VALUE;
986 sum += size;
988 if (!(cc->attr = HeapAlloc(GetProcessHeap(), 0, sizeof(*cc->attr) * cc->attr_count + sum)))
990 CryptMsgClose(hmsg);
991 HeapFree(GetProcessHeap(), 0, cc);
992 SetLastError(ERROR_OUTOFMEMORY);
993 return INVALID_HANDLE_VALUE;
995 p = (BYTE *)(cc->attr + cc->attr_count);
996 for (i = 0; i < cc->attr_count; i++)
998 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size))
1000 CryptMsgClose(hmsg);
1001 HeapFree(GetProcessHeap(), 0, cc->attr);
1002 HeapFree(GetProcessHeap(), 0, cc);
1003 return INVALID_HANDLE_VALUE;
1005 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, p, &size))
1007 CryptMsgClose(hmsg);
1008 HeapFree(GetProcessHeap(), 0, cc->attr);
1009 HeapFree(GetProcessHeap(), 0, cc);
1010 return INVALID_HANDLE_VALUE;
1012 p += size;
1014 cc->inner = decode_inner_content(hmsg, dwEncodingType, &cc->inner_len);
1015 if (!cc->inner || !CryptSIPRetrieveSubjectGuid(filename, NULL, &cc->subject))
1017 CryptMsgClose(hmsg);
1018 HeapFree(GetProcessHeap(), 0, cc->attr);
1019 HeapFree(GetProcessHeap(), 0, cc->inner);
1020 HeapFree(GetProcessHeap(), 0, cc);
1021 return INVALID_HANDLE_VALUE;
1023 cc->magic = CRYPTCAT_MAGIC;
1024 SetLastError(ERROR_SUCCESS);
1025 return cc;
1027 HeapFree(GetProcessHeap(), 0, cc);
1028 return INVALID_HANDLE_VALUE;
1031 /***********************************************************************
1032 * CryptSIPCreateIndirectData (WINTRUST.@)
1034 BOOL WINAPI CryptSIPCreateIndirectData(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pcbIndirectData,
1035 SIP_INDIRECT_DATA* pIndirectData)
1037 FIXME("(%p %p %p) stub\n", pSubjectInfo, pcbIndirectData, pIndirectData);
1039 return FALSE;
1043 /***********************************************************************
1044 * CryptCATCDFClose (WINTRUST.@)
1046 BOOL WINAPI CryptCATCDFClose(CRYPTCATCDF *pCDF)
1048 FIXME("(%p) stub\n", pCDF);
1050 return FALSE;
1053 /***********************************************************************
1054 * CryptCATCDFEnumCatAttributes (WINTRUST.@)
1056 CRYPTCATATTRIBUTE * WINAPI CryptCATCDFEnumCatAttributes(CRYPTCATCDF *pCDF,
1057 CRYPTCATATTRIBUTE *pPrevAttr,
1058 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
1060 FIXME("(%p %p %p) stub\n", pCDF, pPrevAttr, pfnParseError);
1062 return NULL;
1065 /***********************************************************************
1066 * CryptCATCDFEnumMembersByCDFTagEx (WINTRUST.@)
1068 LPWSTR WINAPI CryptCATCDFEnumMembersByCDFTagEx(CRYPTCATCDF *pCDF, LPWSTR pwszPrevCDFTag,
1069 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError,
1070 CRYPTCATMEMBER **ppMember, BOOL fContinueOnError,
1071 LPVOID pvReserved)
1073 FIXME("(%p %s %p %p %d %p) stub\n", pCDF, debugstr_w(pwszPrevCDFTag), pfnParseError,
1074 ppMember, fContinueOnError, pvReserved);
1076 return NULL;
1079 /***********************************************************************
1080 * CryptCATCDFOpen (WINTRUST.@)
1082 CRYPTCATCDF * WINAPI CryptCATCDFOpen(LPWSTR pwszFilePath,
1083 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
1085 FIXME("(%s %p) stub\n", debugstr_w(pwszFilePath), pfnParseError);
1087 return NULL;
1090 static BOOL WINTRUST_GetSignedMsgFromPEFile(SIP_SUBJECTINFO *pSubjectInfo,
1091 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1092 BYTE *pbSignedDataMsg)
1094 BOOL ret;
1095 WIN_CERTIFICATE *pCert = NULL;
1096 HANDLE file;
1098 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1099 pcbSignedDataMsg, pbSignedDataMsg);
1101 if(pSubjectInfo->hFile && pSubjectInfo->hFile!=INVALID_HANDLE_VALUE)
1102 file = pSubjectInfo->hFile;
1103 else
1105 file = CreateFileW(pSubjectInfo->pwsFileName, GENERIC_READ,
1106 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1107 if(file == INVALID_HANDLE_VALUE)
1108 return FALSE;
1111 if (!pbSignedDataMsg)
1113 WIN_CERTIFICATE cert;
1115 /* app hasn't passed buffer, just get the length */
1116 ret = ImageGetCertificateHeader(file, dwIndex, &cert);
1117 if (ret)
1119 switch (cert.wCertificateType)
1121 case WIN_CERT_TYPE_X509:
1122 case WIN_CERT_TYPE_PKCS_SIGNED_DATA:
1123 *pcbSignedDataMsg = cert.dwLength;
1124 break;
1125 default:
1126 WARN("unknown certificate type %d\n", cert.wCertificateType);
1127 ret = FALSE;
1131 else
1133 DWORD len = 0;
1135 ret = ImageGetCertificateData(file, dwIndex, NULL, &len);
1136 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1137 goto error;
1138 pCert = HeapAlloc(GetProcessHeap(), 0, len);
1139 if (!pCert)
1141 ret = FALSE;
1142 goto error;
1144 ret = ImageGetCertificateData(file, dwIndex, pCert, &len);
1145 if (!ret)
1146 goto error;
1147 pCert->dwLength -= FIELD_OFFSET(WIN_CERTIFICATE, bCertificate);
1148 if (*pcbSignedDataMsg < pCert->dwLength)
1150 *pcbSignedDataMsg = pCert->dwLength;
1151 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1152 ret = FALSE;
1154 else
1156 memcpy(pbSignedDataMsg, pCert->bCertificate, pCert->dwLength);
1157 *pcbSignedDataMsg = pCert->dwLength;
1158 switch (pCert->wCertificateType)
1160 case WIN_CERT_TYPE_X509:
1161 *pdwEncodingType = X509_ASN_ENCODING;
1162 break;
1163 case WIN_CERT_TYPE_PKCS_SIGNED_DATA:
1164 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1165 break;
1166 default:
1167 WARN("don't know what to do for encoding type %d\n",
1168 pCert->wCertificateType);
1169 *pdwEncodingType = 0;
1170 ret = FALSE;
1174 error:
1175 if(pSubjectInfo->hFile != file)
1176 CloseHandle(file);
1177 HeapFree(GetProcessHeap(), 0, pCert);
1178 return ret;
1181 static BOOL WINTRUST_PutSignedMsgToPEFile(SIP_SUBJECTINFO* pSubjectInfo, DWORD pdwEncodingType,
1182 DWORD* pdwIndex, DWORD cbSignedDataMsg, BYTE* pbSignedDataMsg)
1184 WIN_CERTIFICATE *cert;
1185 HANDLE file;
1186 DWORD size;
1187 BOOL ret;
1189 if(pSubjectInfo->hFile && pSubjectInfo->hFile!=INVALID_HANDLE_VALUE)
1190 file = pSubjectInfo->hFile;
1191 else
1193 file = CreateFileW(pSubjectInfo->pwsFileName, GENERIC_READ|GENERIC_WRITE,
1194 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1195 if(file == INVALID_HANDLE_VALUE)
1196 return FALSE;
1199 /* int aligned WIN_CERTIFICATE structure with cbSignedDataMsg+1 bytes of data */
1200 size = FIELD_OFFSET(WIN_CERTIFICATE, bCertificate[cbSignedDataMsg+4]) & (~3);
1201 cert = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1202 if(!cert)
1203 return FALSE;
1205 cert->dwLength = size;
1206 cert->wRevision = WIN_CERT_REVISION_2_0;
1207 cert->wCertificateType = WIN_CERT_TYPE_PKCS_SIGNED_DATA;
1208 memcpy(cert->bCertificate, pbSignedDataMsg, cbSignedDataMsg);
1209 ret = ImageAddCertificate(file, cert, pdwIndex);
1211 HeapFree(GetProcessHeap(), 0, cert);
1212 if(file != pSubjectInfo->hFile)
1213 CloseHandle(file);
1214 return ret;
1217 /* structure offsets */
1218 #define cfhead_Signature (0x00)
1219 #define cfhead_CabinetSize (0x08)
1220 #define cfhead_MinorVersion (0x18)
1221 #define cfhead_MajorVersion (0x19)
1222 #define cfhead_Flags (0x1E)
1223 #define cfhead_SIZEOF (0x24)
1224 #define cfheadext_HeaderReserved (0x00)
1225 #define cfheadext_SIZEOF (0x04)
1226 #define cfsigninfo_CertOffset (0x04)
1227 #define cfsigninfo_CertSize (0x08)
1228 #define cfsigninfo_SIZEOF (0x0C)
1230 /* flags */
1231 #define cfheadRESERVE_PRESENT (0x0004)
1233 /* endian-neutral reading of little-endian data */
1234 #define EndGetI32(a) ((((a)[3])<<24)|(((a)[2])<<16)|(((a)[1])<<8)|((a)[0]))
1235 #define EndGetI16(a) ((((a)[1])<<8)|((a)[0]))
1237 /* For documentation purposes only: this is the structure in the reserved
1238 * area of a signed cabinet file. The cert offset indicates where in the
1239 * cabinet file the signature resides, and the count indicates its size.
1241 typedef struct _CAB_SIGNINFO
1243 WORD unk0; /* always 0? */
1244 WORD unk1; /* always 0x0010? */
1245 DWORD dwCertOffset;
1246 DWORD cbCertBlock;
1247 } CAB_SIGNINFO, *PCAB_SIGNINFO;
1249 static BOOL WINTRUST_GetSignedMsgFromCabFile(SIP_SUBJECTINFO *pSubjectInfo,
1250 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1251 BYTE *pbSignedDataMsg)
1253 int header_resv;
1254 LONG base_offset, cabsize;
1255 USHORT flags;
1256 BYTE buf[64];
1257 DWORD cert_offset, cert_size, dwRead;
1259 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1260 pcbSignedDataMsg, pbSignedDataMsg);
1262 /* get basic offset & size info */
1263 base_offset = SetFilePointer(pSubjectInfo->hFile, 0L, NULL, SEEK_CUR);
1265 if (SetFilePointer(pSubjectInfo->hFile, 0, NULL, SEEK_END) == INVALID_SET_FILE_POINTER)
1267 TRACE("seek error\n");
1268 return FALSE;
1271 cabsize = SetFilePointer(pSubjectInfo->hFile, 0L, NULL, SEEK_CUR);
1272 if ((cabsize == -1) || (base_offset == -1) ||
1273 (SetFilePointer(pSubjectInfo->hFile, 0, NULL, SEEK_SET) == INVALID_SET_FILE_POINTER))
1275 TRACE("seek error\n");
1276 return FALSE;
1279 /* read in the CFHEADER */
1280 if (!ReadFile(pSubjectInfo->hFile, buf, cfhead_SIZEOF, &dwRead, NULL) ||
1281 dwRead != cfhead_SIZEOF)
1283 TRACE("reading header failed\n");
1284 return FALSE;
1287 /* check basic MSCF signature */
1288 if (EndGetI32(buf+cfhead_Signature) != 0x4643534d)
1290 WARN("cabinet signature not present\n");
1291 return FALSE;
1294 /* Ignore the number of folders and files and the set and cabinet IDs */
1296 /* check the header revision */
1297 if ((buf[cfhead_MajorVersion] > 1) ||
1298 (buf[cfhead_MajorVersion] == 1 && buf[cfhead_MinorVersion] > 3))
1300 WARN("cabinet format version > 1.3\n");
1301 return FALSE;
1304 /* pull the flags out */
1305 flags = EndGetI16(buf+cfhead_Flags);
1307 if (!(flags & cfheadRESERVE_PRESENT))
1309 TRACE("no header present, not signed\n");
1310 return FALSE;
1313 if (!ReadFile(pSubjectInfo->hFile, buf, cfheadext_SIZEOF, &dwRead, NULL) ||
1314 dwRead != cfheadext_SIZEOF)
1316 ERR("bunk reserve-sizes?\n");
1317 return FALSE;
1320 header_resv = EndGetI16(buf+cfheadext_HeaderReserved);
1321 if (!header_resv)
1323 TRACE("no header_resv, not signed\n");
1324 return FALSE;
1326 else if (header_resv < cfsigninfo_SIZEOF)
1328 TRACE("header_resv too small, not signed\n");
1329 return FALSE;
1332 if (header_resv > 60000)
1334 WARN("WARNING; header reserved space > 60000\n");
1337 if (!ReadFile(pSubjectInfo->hFile, buf, cfsigninfo_SIZEOF, &dwRead, NULL) ||
1338 dwRead != cfsigninfo_SIZEOF)
1340 ERR("couldn't read reserve\n");
1341 return FALSE;
1344 cert_offset = EndGetI32(buf+cfsigninfo_CertOffset);
1345 TRACE("cert_offset: %d\n", cert_offset);
1346 cert_size = EndGetI32(buf+cfsigninfo_CertSize);
1347 TRACE("cert_size: %d\n", cert_size);
1349 /* The redundant checks are to avoid wraparound */
1350 if (cert_offset > cabsize || cert_size > cabsize ||
1351 cert_offset + cert_size > cabsize)
1353 WARN("offset beyond file, not attempting to read\n");
1354 return FALSE;
1357 SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET);
1358 if (!pbSignedDataMsg)
1360 *pcbSignedDataMsg = cert_size;
1361 return TRUE;
1363 if (*pcbSignedDataMsg < cert_size)
1365 *pcbSignedDataMsg = cert_size;
1366 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1367 return FALSE;
1369 if (SetFilePointer(pSubjectInfo->hFile, cert_offset, NULL, SEEK_SET) == INVALID_SET_FILE_POINTER)
1371 ERR("couldn't seek to cert location\n");
1372 return FALSE;
1374 if (!ReadFile(pSubjectInfo->hFile, pbSignedDataMsg, cert_size, &dwRead,
1375 NULL) || dwRead != cert_size)
1377 ERR("couldn't read cert\n");
1378 SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET);
1379 return FALSE;
1381 /* The encoding of the files I've seen appears to be in ASN.1
1382 * format, and there isn't a field indicating the type, so assume it
1383 * always is.
1385 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1386 /* Restore base offset */
1387 SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET);
1388 return TRUE;
1391 static BOOL WINTRUST_GetSignedMsgFromCatFile(SIP_SUBJECTINFO *pSubjectInfo,
1392 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1393 BYTE *pbSignedDataMsg)
1395 BOOL ret;
1397 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1398 pcbSignedDataMsg, pbSignedDataMsg);
1400 if (!pbSignedDataMsg)
1402 *pcbSignedDataMsg = GetFileSize(pSubjectInfo->hFile, NULL);
1403 ret = TRUE;
1405 else
1407 DWORD len = GetFileSize(pSubjectInfo->hFile, NULL);
1409 if (*pcbSignedDataMsg < len)
1411 *pcbSignedDataMsg = len;
1412 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1413 ret = FALSE;
1415 else
1417 ret = ReadFile(pSubjectInfo->hFile, pbSignedDataMsg, len,
1418 pcbSignedDataMsg, NULL);
1419 if (ret)
1420 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1423 return ret;
1426 /* GUIDs used by CryptSIPGetSignedDataMsg and CryptSIPPutSignedDataMsg */
1427 static const GUID unknown = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,
1428 0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
1429 static const GUID cabGUID = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,
1430 0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
1431 static const GUID catGUID = { 0xDE351A43, 0x8E59, 0x11D0, { 0x8C,0x47,
1432 0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
1434 /***********************************************************************
1435 * CryptSIPGetSignedDataMsg (WINTRUST.@)
1437 BOOL WINAPI CryptSIPGetSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pdwEncodingType,
1438 DWORD dwIndex, DWORD* pcbSignedDataMsg, BYTE* pbSignedDataMsg)
1440 BOOL ret;
1442 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1443 pcbSignedDataMsg, pbSignedDataMsg);
1445 if(!pSubjectInfo)
1447 SetLastError(ERROR_INVALID_PARAMETER);
1448 return FALSE;
1451 if (!memcmp(pSubjectInfo->pgSubjectType, &unknown, sizeof(unknown)))
1452 ret = WINTRUST_GetSignedMsgFromPEFile(pSubjectInfo, pdwEncodingType,
1453 dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
1454 else if (!memcmp(pSubjectInfo->pgSubjectType, &cabGUID, sizeof(cabGUID)))
1455 ret = WINTRUST_GetSignedMsgFromCabFile(pSubjectInfo, pdwEncodingType,
1456 dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
1457 else if (!memcmp(pSubjectInfo->pgSubjectType, &catGUID, sizeof(catGUID)))
1458 ret = WINTRUST_GetSignedMsgFromCatFile(pSubjectInfo, pdwEncodingType,
1459 dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
1460 else
1462 FIXME("unimplemented for subject type %s\n",
1463 debugstr_guid(pSubjectInfo->pgSubjectType));
1464 ret = FALSE;
1467 TRACE("returning %d\n", ret);
1468 return ret;
1471 /***********************************************************************
1472 * CryptSIPPutSignedDataMsg (WINTRUST.@)
1474 BOOL WINAPI CryptSIPPutSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD pdwEncodingType,
1475 DWORD* pdwIndex, DWORD cbSignedDataMsg, BYTE* pbSignedDataMsg)
1477 TRACE("(%p %d %p %d %p)\n", pSubjectInfo, pdwEncodingType, pdwIndex,
1478 cbSignedDataMsg, pbSignedDataMsg);
1480 if(!pSubjectInfo) {
1481 SetLastError(ERROR_INVALID_PARAMETER);
1482 return FALSE;
1485 if(!memcmp(pSubjectInfo->pgSubjectType, &unknown, sizeof(unknown)))
1486 return WINTRUST_PutSignedMsgToPEFile(pSubjectInfo, pdwEncodingType,
1487 pdwIndex, cbSignedDataMsg, pbSignedDataMsg);
1488 else
1489 FIXME("unimplemented for subject type %s\n",
1490 debugstr_guid(pSubjectInfo->pgSubjectType));
1492 return FALSE;
1495 /***********************************************************************
1496 * CryptSIPRemoveSignedDataMsg (WINTRUST.@)
1498 BOOL WINAPI CryptSIPRemoveSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo,
1499 DWORD dwIndex)
1501 FIXME("(%p %d) stub\n", pSubjectInfo, dwIndex);
1503 return FALSE;
1506 /***********************************************************************
1507 * CryptSIPVerifyIndirectData (WINTRUST.@)
1509 BOOL WINAPI CryptSIPVerifyIndirectData(SIP_SUBJECTINFO* pSubjectInfo,
1510 SIP_INDIRECT_DATA* pIndirectData)
1512 FIXME("(%p %p) stub\n", pSubjectInfo, pIndirectData);
1514 return FALSE;