ntdll: Translate signal to trap when trap code is 0 on ARM.
[wine.git] / dlls / wintrust / crypt.c
blob1a369654da40c62fd0bf62ff0d197e6657c2b197
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 HCRYPTMSG 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 || dwFlags)
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 * CryptCATAdminAcquireContext2 (WINTRUST.@)
152 BOOL WINAPI CryptCATAdminAcquireContext2(HCATADMIN *catAdmin, const GUID *sys, const WCHAR *algorithm,
153 const CERT_STRONG_SIGN_PARA *policy, DWORD flags)
155 FIXME("%p %s %s %p %x stub\n", catAdmin, debugstr_guid(sys), debugstr_w(algorithm), policy, flags);
156 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
157 return FALSE;
160 /***********************************************************************
161 * CryptCATAdminAddCatalog (WINTRUST.@)
163 HCATINFO WINAPI CryptCATAdminAddCatalog(HCATADMIN catAdmin, PWSTR catalogFile,
164 PWSTR selectBaseName, DWORD flags)
166 static const WCHAR slashW[] = {'\\',0};
167 struct catadmin *ca = catAdmin;
168 struct catinfo *ci;
169 WCHAR *target;
170 DWORD len;
172 TRACE("%p %s %s %d\n", catAdmin, debugstr_w(catalogFile),
173 debugstr_w(selectBaseName), flags);
175 if (!selectBaseName)
177 FIXME("NULL basename not handled\n");
178 SetLastError(ERROR_INVALID_PARAMETER);
179 return NULL;
181 if (!ca || ca->magic != CATADMIN_MAGIC || !catalogFile || flags)
183 SetLastError(ERROR_INVALID_PARAMETER);
184 return NULL;
187 len = strlenW(ca->path) + strlenW(selectBaseName) + 2;
188 if (!(target = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
190 SetLastError(ERROR_OUTOFMEMORY);
191 return NULL;
193 strcpyW(target, ca->path);
194 strcatW(target, slashW);
195 strcatW(target, selectBaseName);
197 if (!CopyFileW(catalogFile, target, FALSE))
199 HeapFree(GetProcessHeap(), 0, target);
200 return NULL;
202 SetFileAttributesW(target, FILE_ATTRIBUTE_SYSTEM);
204 if (!(ci = HeapAlloc(GetProcessHeap(), 0, sizeof(*ci))))
206 HeapFree(GetProcessHeap(), 0, target);
207 SetLastError(ERROR_OUTOFMEMORY);
208 return NULL;
210 ci->magic = CATINFO_MAGIC;
211 strcpyW(ci->file, target);
213 HeapFree(GetProcessHeap(), 0, target);
214 return ci;
217 /***********************************************************************
218 * CryptCATAdminCalcHashFromFileHandle (WINTRUST.@)
220 BOOL WINAPI CryptCATAdminCalcHashFromFileHandle(HANDLE hFile, DWORD* pcbHash,
221 BYTE* pbHash, DWORD dwFlags )
223 BOOL ret = FALSE;
225 TRACE("%p %p %p %x\n", hFile, pcbHash, pbHash, dwFlags);
227 if (!hFile || !pcbHash || dwFlags)
229 SetLastError(ERROR_INVALID_PARAMETER);
230 return FALSE;
232 if (*pcbHash < 20)
234 *pcbHash = 20;
235 SetLastError(ERROR_INSUFFICIENT_BUFFER);
236 return TRUE;
239 *pcbHash = 20;
240 if (pbHash)
242 HCRYPTPROV prov;
243 HCRYPTHASH hash;
244 DWORD bytes_read;
245 BYTE *buffer;
247 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, 4096)))
249 SetLastError(ERROR_OUTOFMEMORY);
250 return FALSE;
252 ret = CryptAcquireContextW(&prov, NULL, MS_DEF_PROV_W, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
253 if (!ret)
255 HeapFree(GetProcessHeap(), 0, buffer);
256 return FALSE;
258 ret = CryptCreateHash(prov, CALG_SHA1, 0, 0, &hash);
259 if (!ret)
261 HeapFree(GetProcessHeap(), 0, buffer);
262 CryptReleaseContext(prov, 0);
263 return FALSE;
265 while ((ret = ReadFile(hFile, buffer, 4096, &bytes_read, NULL)) && bytes_read)
267 CryptHashData(hash, buffer, bytes_read, 0);
269 if (ret) ret = CryptGetHashParam(hash, HP_HASHVAL, pbHash, pcbHash, 0);
271 HeapFree(GetProcessHeap(), 0, buffer);
272 CryptDestroyHash(hash);
273 CryptReleaseContext(prov, 0);
275 return ret;
278 /***********************************************************************
279 * CryptCATAdminEnumCatalogFromHash (WINTRUST.@)
281 HCATINFO WINAPI CryptCATAdminEnumCatalogFromHash(HCATADMIN hCatAdmin, BYTE* pbHash,
282 DWORD cbHash, DWORD dwFlags,
283 HCATINFO* phPrevCatInfo )
285 static const WCHAR slashW[] = {'\\',0};
286 static const WCHAR globW[] = {'\\','*','.','c','a','t',0};
288 struct catadmin *ca = hCatAdmin;
289 WIN32_FIND_DATAW data;
290 HCATINFO prev = NULL;
291 HCRYPTPROV prov;
292 DWORD size;
293 BOOL ret;
295 TRACE("%p %p %d %x %p\n", hCatAdmin, pbHash, cbHash, dwFlags, phPrevCatInfo);
297 if (!ca || ca->magic != CATADMIN_MAGIC || !pbHash || cbHash != 20 || dwFlags)
299 SetLastError(ERROR_INVALID_PARAMETER);
300 return NULL;
302 if (phPrevCatInfo) prev = *phPrevCatInfo;
304 ret = CryptAcquireContextW(&prov, NULL, MS_DEF_PROV_W, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
305 if (!ret) return NULL;
307 if (!prev)
309 WCHAR *path;
311 size = strlenW(ca->path) * sizeof(WCHAR) + sizeof(globW);
312 if (!(path = HeapAlloc(GetProcessHeap(), 0, size)))
314 CryptReleaseContext(prov, 0);
315 SetLastError(ERROR_OUTOFMEMORY);
316 return NULL;
318 strcpyW(path, ca->path);
319 strcatW(path, globW);
321 FindClose(ca->find);
322 ca->find = FindFirstFileW(path, &data);
324 HeapFree(GetProcessHeap(), 0, path);
325 if (ca->find == INVALID_HANDLE_VALUE)
327 CryptReleaseContext(prov, 0);
328 return NULL;
331 else if (!FindNextFileW(ca->find, &data))
333 CryptCATAdminReleaseCatalogContext(hCatAdmin, prev, 0);
334 CryptReleaseContext(prov, 0);
335 return NULL;
338 while (1)
340 WCHAR *filename;
341 CRYPTCATMEMBER *member = NULL;
342 struct catinfo *ci;
343 HANDLE hcat;
345 size = (strlenW(ca->path) + strlenW(data.cFileName) + 2) * sizeof(WCHAR);
346 if (!(filename = HeapAlloc(GetProcessHeap(), 0, size)))
348 SetLastError(ERROR_OUTOFMEMORY);
349 return NULL;
351 strcpyW(filename, ca->path);
352 strcatW(filename, slashW);
353 strcatW(filename, data.cFileName);
355 hcat = CryptCATOpen(filename, CRYPTCAT_OPEN_EXISTING, prov, 0, 0);
356 if (hcat == INVALID_HANDLE_VALUE)
358 WARN("couldn't open %s (%u)\n", debugstr_w(filename), GetLastError());
359 continue;
361 while ((member = CryptCATEnumerateMember(hcat, member)))
363 if (member->pIndirectData->Digest.cbData != cbHash)
365 WARN("amount of hash bytes differs: %u/%u\n", member->pIndirectData->Digest.cbData, cbHash);
366 continue;
368 if (!memcmp(member->pIndirectData->Digest.pbData, pbHash, cbHash))
370 TRACE("file %s matches\n", debugstr_w(data.cFileName));
372 CryptCATClose(hcat);
373 CryptReleaseContext(prov, 0);
374 if (!phPrevCatInfo)
376 FindClose(ca->find);
377 ca->find = INVALID_HANDLE_VALUE;
379 ci = create_catinfo(filename);
380 HeapFree(GetProcessHeap(), 0, filename);
381 return ci;
384 CryptCATClose(hcat);
385 HeapFree(GetProcessHeap(), 0, filename);
387 if (!FindNextFileW(ca->find, &data))
389 FindClose(ca->find);
390 ca->find = INVALID_HANDLE_VALUE;
391 CryptReleaseContext(prov, 0);
392 return NULL;
395 return NULL;
398 /***********************************************************************
399 * CryptCATAdminReleaseCatalogContext (WINTRUST.@)
401 * Release a catalog context handle.
403 * PARAMS
404 * hCatAdmin [I] Context handle.
405 * hCatInfo [I] Catalog handle.
406 * dwFlags [I] Reserved.
408 * RETURNS
409 * Success: TRUE.
410 * Failure: FALSE.
413 BOOL WINAPI CryptCATAdminReleaseCatalogContext(HCATADMIN hCatAdmin,
414 HCATINFO hCatInfo,
415 DWORD dwFlags)
417 struct catinfo *ci = hCatInfo;
418 struct catadmin *ca = hCatAdmin;
420 TRACE("%p %p %x\n", hCatAdmin, hCatInfo, dwFlags);
422 if (!ca || ca->magic != CATADMIN_MAGIC || !ci || ci->magic != CATINFO_MAGIC)
424 SetLastError(ERROR_INVALID_PARAMETER);
425 return FALSE;
427 ci->magic = 0;
428 return HeapFree(GetProcessHeap(), 0, ci);
431 /***********************************************************************
432 * CryptCATAdminReleaseContext (WINTRUST.@)
434 * Release a catalog administrator context handle.
436 * PARAMS
437 * catAdmin [I] Context handle.
438 * dwFlags [I] Reserved.
440 * RETURNS
441 * Success: TRUE.
442 * Failure: FALSE.
445 BOOL WINAPI CryptCATAdminReleaseContext(HCATADMIN hCatAdmin, DWORD dwFlags )
447 struct catadmin *ca = hCatAdmin;
449 TRACE("%p %x\n", hCatAdmin, dwFlags);
451 if (!ca || ca->magic != CATADMIN_MAGIC)
453 SetLastError(ERROR_INVALID_PARAMETER);
454 return FALSE;
456 if (ca->find != INVALID_HANDLE_VALUE) FindClose(ca->find);
457 ca->magic = 0;
458 return HeapFree(GetProcessHeap(), 0, ca);
461 /***********************************************************************
462 * CryptCATAdminRemoveCatalog (WINTRUST.@)
464 * Remove a catalog file.
466 * PARAMS
467 * catAdmin [I] Context handle.
468 * pwszCatalogFile [I] Catalog file.
469 * dwFlags [I] Reserved.
471 * RETURNS
472 * Success: TRUE.
473 * Failure: FALSE.
476 BOOL WINAPI CryptCATAdminRemoveCatalog(HCATADMIN hCatAdmin, LPCWSTR pwszCatalogFile, DWORD dwFlags)
478 struct catadmin *ca = hCatAdmin;
480 TRACE("%p %s %x\n", hCatAdmin, debugstr_w(pwszCatalogFile), dwFlags);
482 if (!ca || ca->magic != CATADMIN_MAGIC)
484 SetLastError(ERROR_INVALID_PARAMETER);
485 return FALSE;
488 /* Only delete when there is a filename and no path */
489 if (pwszCatalogFile && pwszCatalogFile[0] != 0 &&
490 !strchrW(pwszCatalogFile, '\\') && !strchrW(pwszCatalogFile, '/') &&
491 !strchrW(pwszCatalogFile, ':'))
493 static const WCHAR slashW[] = {'\\',0};
494 WCHAR *target;
495 DWORD len;
497 len = strlenW(ca->path) + strlenW(pwszCatalogFile) + 2;
498 if (!(target = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
500 SetLastError(ERROR_OUTOFMEMORY);
501 return FALSE;
503 strcpyW(target, ca->path);
504 strcatW(target, slashW);
505 strcatW(target, pwszCatalogFile);
507 DeleteFileW(target);
509 HeapFree(GetProcessHeap(), 0, target);
512 return TRUE;
515 /***********************************************************************
516 * CryptCATAdminResolveCatalogPath (WINTRUST.@)
518 BOOL WINAPI CryptCATAdminResolveCatalogPath(HCATADMIN hcatadmin, WCHAR *catalog_file,
519 CATALOG_INFO *info, DWORD flags)
521 static const WCHAR slashW[] = {'\\',0};
522 struct catadmin *ca = hcatadmin;
524 TRACE("%p %s %p %x\n", hcatadmin, debugstr_w(catalog_file), info, flags);
526 if (!ca || ca->magic != CATADMIN_MAGIC || !info || info->cbStruct != sizeof(*info) || flags)
528 SetLastError(ERROR_INVALID_PARAMETER);
529 return FALSE;
531 strcpyW(info->wszCatalogFile, ca->path);
532 strcatW(info->wszCatalogFile, slashW);
533 strcatW(info->wszCatalogFile, catalog_file);
535 return TRUE;
538 /***********************************************************************
539 * CryptCATClose (WINTRUST.@)
541 BOOL WINAPI CryptCATClose(HANDLE hCatalog)
543 struct cryptcat *cc = hCatalog;
545 TRACE("(%p)\n", hCatalog);
547 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
549 SetLastError(ERROR_INVALID_PARAMETER);
550 return FALSE;
552 HeapFree(GetProcessHeap(), 0, cc->attr);
553 HeapFree(GetProcessHeap(), 0, cc->inner);
554 CryptMsgClose(cc->msg);
556 cc->magic = 0;
557 HeapFree(GetProcessHeap(), 0, cc);
558 return TRUE;
561 /***********************************************************************
562 * CryptCATGetAttrInfo (WINTRUST.@)
564 CRYPTCATATTRIBUTE * WINAPI CryptCATGetAttrInfo(HANDLE hCatalog, CRYPTCATMEMBER *member, LPWSTR tag)
566 struct cryptcat *cc = hCatalog;
568 FIXME("%p, %p, %s\n", hCatalog, member, debugstr_w(tag));
570 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
572 SetLastError(ERROR_INVALID_PARAMETER);
573 return NULL;
575 SetLastError(CRYPT_E_NOT_FOUND);
576 return NULL;
579 /***********************************************************************
580 * CryptCATGetCatAttrInfo (WINTRUST.@)
582 CRYPTCATATTRIBUTE * WINAPI CryptCATGetCatAttrInfo(HANDLE hCatalog, LPWSTR tag)
584 struct cryptcat *cc = hCatalog;
586 FIXME("%p, %s\n", hCatalog, debugstr_w(tag));
588 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
590 SetLastError(ERROR_INVALID_PARAMETER);
591 return NULL;
593 SetLastError(CRYPT_E_NOT_FOUND);
594 return NULL;
597 CRYPTCATMEMBER * WINAPI CryptCATGetMemberInfo(HANDLE hCatalog, LPWSTR tag)
599 struct cryptcat *cc = hCatalog;
601 FIXME("%p, %s\n", hCatalog, debugstr_w(tag));
603 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
605 SetLastError(ERROR_INVALID_PARAMETER);
606 return NULL;
608 SetLastError(CRYPT_E_NOT_FOUND);
609 return NULL;
612 /***********************************************************************
613 * CryptCATEnumerateAttr (WINTRUST.@)
615 CRYPTCATATTRIBUTE * WINAPI CryptCATEnumerateAttr(HANDLE hCatalog, CRYPTCATMEMBER *member, CRYPTCATATTRIBUTE *prev)
617 struct cryptcat *cc = hCatalog;
619 FIXME("%p, %p, %p\n", hCatalog, member, prev);
621 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
623 SetLastError(ERROR_INVALID_PARAMETER);
624 return NULL;
626 SetLastError(CRYPT_E_NOT_FOUND);
627 return NULL;
630 /***********************************************************************
631 * CryptCATEnumerateCatAttr (WINTRUST.@)
633 CRYPTCATATTRIBUTE * WINAPI CryptCATEnumerateCatAttr(HANDLE hCatalog, CRYPTCATATTRIBUTE *prev)
635 struct cryptcat *cc = hCatalog;
637 FIXME("%p, %p\n", hCatalog, prev);
639 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
641 SetLastError(ERROR_INVALID_PARAMETER);
642 return NULL;
644 SetLastError(CRYPT_E_NOT_FOUND);
645 return NULL;
648 /***********************************************************************
649 * CryptCATEnumerateMember (WINTRUST.@)
651 CRYPTCATMEMBER * WINAPI CryptCATEnumerateMember(HANDLE hCatalog, CRYPTCATMEMBER *prev)
653 struct cryptcat *cc = hCatalog;
654 CRYPTCATMEMBER *member = prev;
655 CTL_ENTRY *entry;
656 DWORD size, i;
658 TRACE("%p, %p\n", hCatalog, prev);
660 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
662 SetLastError(ERROR_INVALID_PARAMETER);
663 return NULL;
666 /* dumping the contents makes me think that dwReserved is the iteration number */
667 if (!member)
669 if (!(member = HeapAlloc(GetProcessHeap(), 0, sizeof(*member))))
671 SetLastError(ERROR_OUTOFMEMORY);
672 return NULL;
674 member->cbStruct = sizeof(*member);
675 member->pwszFileName = member->pwszReferenceTag = NULL;
676 member->dwReserved = 0;
677 member->hReserved = NULL;
678 member->gSubjectType = cc->subject;
679 member->fdwMemberFlags = 0;
680 member->pIndirectData = NULL;
681 member->dwCertVersion = cc->inner->dwVersion;
683 else member->dwReserved++;
685 if (member->dwReserved >= cc->inner->cCTLEntry)
687 SetLastError(ERROR_INVALID_PARAMETER);
688 goto error;
691 /* list them backwards, like native */
692 entry = &cc->inner->rgCTLEntry[cc->inner->cCTLEntry - member->dwReserved - 1];
694 member->sEncodedIndirectData.cbData = member->sEncodedMemberInfo.cbData = 0;
695 member->sEncodedIndirectData.pbData = member->sEncodedMemberInfo.pbData = NULL;
696 HeapFree(GetProcessHeap(), 0, member->pIndirectData);
697 member->pIndirectData = NULL;
699 for (i = 0; i < entry->cAttribute; i++)
701 CRYPT_ATTRIBUTE *attr = entry->rgAttribute + i;
703 if (attr->cValue != 1)
705 ERR("Can't handle attr->cValue of %u\n", attr->cValue);
706 continue;
708 if (!strcmp(attr->pszObjId, CAT_MEMBERINFO_OBJID))
710 CAT_MEMBERINFO *mi;
711 BOOL ret;
713 member->sEncodedMemberInfo.cbData = attr->rgValue->cbData;
714 member->sEncodedMemberInfo.pbData = attr->rgValue->pbData;
716 CryptDecodeObject(cc->encoding, CAT_MEMBERINFO_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, NULL, &size);
718 if (!(mi = HeapAlloc(GetProcessHeap(), 0, size)))
720 SetLastError(ERROR_OUTOFMEMORY);
721 goto error;
723 ret = CryptDecodeObject(cc->encoding, CAT_MEMBERINFO_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, mi, &size);
724 if (ret)
726 UNICODE_STRING guid;
728 member->dwCertVersion = mi->dwCertVersion;
729 RtlInitUnicodeString(&guid, mi->pwszSubjGuid);
730 if (RtlGUIDFromString(&guid, &member->gSubjectType))
732 HeapFree(GetProcessHeap(), 0, mi);
733 goto error;
736 HeapFree(GetProcessHeap(), 0, mi);
737 if (!ret) goto error;
739 else if (!strcmp(attr->pszObjId, SPC_INDIRECT_DATA_OBJID))
741 /* SPC_INDIRECT_DATA_CONTENT is equal to SIP_INDIRECT_DATA */
743 member->sEncodedIndirectData.cbData = attr->rgValue->cbData;
744 member->sEncodedIndirectData.pbData = attr->rgValue->pbData;
746 CryptDecodeObject(cc->encoding, SPC_INDIRECT_DATA_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, NULL, &size);
748 if (!(member->pIndirectData = HeapAlloc(GetProcessHeap(), 0, size)))
750 SetLastError(ERROR_OUTOFMEMORY);
751 goto error;
753 CryptDecodeObject(cc->encoding, SPC_INDIRECT_DATA_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, member->pIndirectData, &size);
755 else
756 /* this object id should probably be handled in CryptCATEnumerateAttr */
757 FIXME("unhandled object id \"%s\"\n", attr->pszObjId);
760 if (!member->sEncodedMemberInfo.cbData || !member->sEncodedIndirectData.cbData)
762 ERR("Corrupted catalog entry?\n");
763 SetLastError(CRYPT_E_ATTRIBUTES_MISSING);
764 goto error;
766 size = (2 * member->pIndirectData->Digest.cbData + 1) * sizeof(WCHAR);
767 if (member->pwszReferenceTag)
768 member->pwszReferenceTag = HeapReAlloc(GetProcessHeap(), 0, member->pwszReferenceTag, size);
769 else
770 member->pwszReferenceTag = HeapAlloc(GetProcessHeap(), 0, size);
772 if (!member->pwszReferenceTag)
774 SetLastError(ERROR_OUTOFMEMORY);
775 goto error;
777 /* FIXME: reference tag is usually the file hash but doesn't have to be */
778 for (i = 0; i < member->pIndirectData->Digest.cbData; i++)
780 DWORD sub;
782 sub = member->pIndirectData->Digest.pbData[i] >> 4;
783 member->pwszReferenceTag[i * 2] = (sub < 10 ? '0' + sub : 'A' + sub - 10);
784 sub = member->pIndirectData->Digest.pbData[i] & 0xf;
785 member->pwszReferenceTag[i * 2 + 1] = (sub < 10 ? '0' + sub : 'A' + sub - 10);
787 member->pwszReferenceTag[i * 2] = 0;
788 return member;
790 error:
791 HeapFree(GetProcessHeap(), 0, member->pIndirectData);
792 HeapFree(GetProcessHeap(), 0, member->pwszReferenceTag);
793 HeapFree(GetProcessHeap(), 0, member);
794 return NULL;
797 static CTL_INFO *decode_inner_content(HANDLE hmsg, DWORD encoding, DWORD *len)
799 DWORD size;
800 LPSTR oid = NULL;
801 BYTE *buffer = NULL;
802 CTL_INFO *inner = NULL;
804 if (!CryptMsgGetParam(hmsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, NULL, &size)) return NULL;
805 if (!(oid = HeapAlloc(GetProcessHeap(), 0, size)))
807 SetLastError(ERROR_OUTOFMEMORY);
808 return NULL;
810 if (!CryptMsgGetParam(hmsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, oid, &size)) goto out;
811 if (!CryptMsgGetParam(hmsg, CMSG_CONTENT_PARAM, 0, NULL, &size)) goto out;
812 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, size)))
814 SetLastError(ERROR_OUTOFMEMORY);
815 goto out;
817 if (!CryptMsgGetParam(hmsg, CMSG_CONTENT_PARAM, 0, buffer, &size)) goto out;
818 if (!CryptDecodeObject(encoding, oid, buffer, size, 0, NULL, &size)) goto out;
819 if (!(inner = HeapAlloc(GetProcessHeap(), 0, size)))
821 SetLastError(ERROR_OUTOFMEMORY);
822 goto out;
824 if (!CryptDecodeObject(encoding, oid, buffer, size, 0, inner, &size)) goto out;
825 *len = size;
827 out:
828 HeapFree(GetProcessHeap(), 0, oid);
829 HeapFree(GetProcessHeap(), 0, buffer);
830 return inner;
833 /***********************************************************************
834 * CryptCATCatalogInfoFromContext (WINTRUST.@)
836 BOOL WINAPI CryptCATCatalogInfoFromContext(HCATINFO hcatinfo, CATALOG_INFO *info, DWORD flags)
838 struct catinfo *ci = hcatinfo;
840 TRACE("%p, %p, %x\n", hcatinfo, info, flags);
842 if (!hcatinfo || hcatinfo == INVALID_HANDLE_VALUE || ci->magic != CATINFO_MAGIC ||
843 flags || !info || info->cbStruct != sizeof(*info))
845 SetLastError(ERROR_INVALID_PARAMETER);
846 return FALSE;
848 strcpyW(info->wszCatalogFile, ci->file);
849 return TRUE;
852 /***********************************************************************
853 * CryptCATOpen (WINTRUST.@)
855 HANDLE WINAPI CryptCATOpen(LPWSTR pwszFileName, DWORD fdwOpenFlags, HCRYPTPROV hProv,
856 DWORD dwPublicVersion, DWORD dwEncodingType)
858 HANDLE file, hmsg;
859 BYTE *buffer = NULL;
860 DWORD size, flags = OPEN_EXISTING;
861 struct cryptcat *cc;
863 TRACE("%s, %x, %lx, %x, %x\n", debugstr_w(pwszFileName), fdwOpenFlags,
864 hProv, dwPublicVersion, dwEncodingType);
866 if (!pwszFileName)
868 SetLastError(ERROR_INVALID_PARAMETER);
869 return INVALID_HANDLE_VALUE;
872 if (!dwEncodingType) dwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
874 if (fdwOpenFlags & CRYPTCAT_OPEN_ALWAYS) flags |= OPEN_ALWAYS;
875 if (fdwOpenFlags & CRYPTCAT_OPEN_CREATENEW) flags |= CREATE_NEW;
877 file = CreateFileW(pwszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, flags, 0, NULL);
878 if (file == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
880 size = GetFileSize(file, NULL);
881 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, size)))
883 CloseHandle(file);
884 SetLastError(ERROR_OUTOFMEMORY);
885 return INVALID_HANDLE_VALUE;
887 if (!(hmsg = CryptMsgOpenToDecode(dwEncodingType, 0, 0, hProv, NULL, NULL)))
889 CloseHandle(file);
890 HeapFree(GetProcessHeap(), 0, buffer);
891 return INVALID_HANDLE_VALUE;
893 if (!ReadFile(file, buffer, size, &size, NULL) || !CryptMsgUpdate(hmsg, buffer, size, TRUE))
895 CloseHandle(file);
896 HeapFree(GetProcessHeap(), 0, buffer);
897 CryptMsgClose(hmsg);
898 return INVALID_HANDLE_VALUE;
900 HeapFree(GetProcessHeap(), 0, buffer);
901 CloseHandle(file);
903 size = sizeof(DWORD);
904 if (!(cc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cc))))
906 CryptMsgClose(hmsg);
907 SetLastError(ERROR_OUTOFMEMORY);
908 return INVALID_HANDLE_VALUE;
911 cc->msg = hmsg;
912 cc->encoding = dwEncodingType;
913 if (CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_COUNT_PARAM, 0, &cc->attr_count, &size))
915 DWORD i, sum = 0;
916 BYTE *p;
918 for (i = 0; i < cc->attr_count; i++)
920 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size))
922 CryptMsgClose(hmsg);
923 HeapFree(GetProcessHeap(), 0, cc);
924 return INVALID_HANDLE_VALUE;
926 sum += size;
928 if (!(cc->attr = HeapAlloc(GetProcessHeap(), 0, sizeof(*cc->attr) * cc->attr_count + sum)))
930 CryptMsgClose(hmsg);
931 HeapFree(GetProcessHeap(), 0, cc);
932 SetLastError(ERROR_OUTOFMEMORY);
933 return INVALID_HANDLE_VALUE;
935 p = (BYTE *)(cc->attr + cc->attr_count);
936 for (i = 0; i < cc->attr_count; i++)
938 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size))
940 CryptMsgClose(hmsg);
941 HeapFree(GetProcessHeap(), 0, cc->attr);
942 HeapFree(GetProcessHeap(), 0, cc);
943 return INVALID_HANDLE_VALUE;
945 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, p, &size))
947 CryptMsgClose(hmsg);
948 HeapFree(GetProcessHeap(), 0, cc->attr);
949 HeapFree(GetProcessHeap(), 0, cc);
950 return INVALID_HANDLE_VALUE;
952 p += size;
954 cc->inner = decode_inner_content(hmsg, dwEncodingType, &cc->inner_len);
955 if (!cc->inner || !CryptSIPRetrieveSubjectGuid(pwszFileName, NULL, &cc->subject))
957 CryptMsgClose(hmsg);
958 HeapFree(GetProcessHeap(), 0, cc->attr);
959 HeapFree(GetProcessHeap(), 0, cc->inner);
960 HeapFree(GetProcessHeap(), 0, cc);
961 return INVALID_HANDLE_VALUE;
963 cc->magic = CRYPTCAT_MAGIC;
964 return cc;
966 HeapFree(GetProcessHeap(), 0, cc);
967 return INVALID_HANDLE_VALUE;
970 /***********************************************************************
971 * CryptSIPCreateIndirectData (WINTRUST.@)
973 BOOL WINAPI CryptSIPCreateIndirectData(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pcbIndirectData,
974 SIP_INDIRECT_DATA* pIndirectData)
976 FIXME("(%p %p %p) stub\n", pSubjectInfo, pcbIndirectData, pIndirectData);
978 return FALSE;
982 /***********************************************************************
983 * CryptCATCDFClose (WINTRUST.@)
985 BOOL WINAPI CryptCATCDFClose(CRYPTCATCDF *pCDF)
987 FIXME("(%p) stub\n", pCDF);
989 return FALSE;
992 /***********************************************************************
993 * CryptCATCDFEnumCatAttributes (WINTRUST.@)
995 CRYPTCATATTRIBUTE * WINAPI CryptCATCDFEnumCatAttributes(CRYPTCATCDF *pCDF,
996 CRYPTCATATTRIBUTE *pPrevAttr,
997 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
999 FIXME("(%p %p %p) stub\n", pCDF, pPrevAttr, pfnParseError);
1001 return NULL;
1004 /***********************************************************************
1005 * CryptCATCDFEnumMembersByCDFTagEx (WINTRUST.@)
1007 LPWSTR WINAPI CryptCATCDFEnumMembersByCDFTagEx(CRYPTCATCDF *pCDF, LPWSTR pwszPrevCDFTag,
1008 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError,
1009 CRYPTCATMEMBER **ppMember, BOOL fContinueOnError,
1010 LPVOID pvReserved)
1012 FIXME("(%p %s %p %p %d %p) stub\n", pCDF, debugstr_w(pwszPrevCDFTag), pfnParseError,
1013 ppMember, fContinueOnError, pvReserved);
1015 return NULL;
1018 /***********************************************************************
1019 * CryptCATCDFOpen (WINTRUST.@)
1021 CRYPTCATCDF * WINAPI CryptCATCDFOpen(LPWSTR pwszFilePath,
1022 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
1024 FIXME("(%s %p) stub\n", debugstr_w(pwszFilePath), pfnParseError);
1026 return NULL;
1029 static BOOL WINTRUST_GetSignedMsgFromPEFile(SIP_SUBJECTINFO *pSubjectInfo,
1030 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1031 BYTE *pbSignedDataMsg)
1033 BOOL ret;
1034 WIN_CERTIFICATE *pCert = NULL;
1035 HANDLE file;
1037 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1038 pcbSignedDataMsg, pbSignedDataMsg);
1040 if(pSubjectInfo->hFile && pSubjectInfo->hFile!=INVALID_HANDLE_VALUE)
1041 file = pSubjectInfo->hFile;
1042 else
1044 file = CreateFileW(pSubjectInfo->pwsFileName, GENERIC_READ,
1045 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1046 if(file == INVALID_HANDLE_VALUE)
1047 return FALSE;
1050 if (!pbSignedDataMsg)
1052 WIN_CERTIFICATE cert;
1054 /* app hasn't passed buffer, just get the length */
1055 ret = ImageGetCertificateHeader(file, dwIndex, &cert);
1056 if (ret)
1058 switch (cert.wCertificateType)
1060 case WIN_CERT_TYPE_X509:
1061 case WIN_CERT_TYPE_PKCS_SIGNED_DATA:
1062 *pcbSignedDataMsg = cert.dwLength;
1063 break;
1064 default:
1065 WARN("unknown certificate type %d\n", cert.wCertificateType);
1066 ret = FALSE;
1070 else
1072 DWORD len = 0;
1074 ret = ImageGetCertificateData(file, dwIndex, NULL, &len);
1075 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1076 goto error;
1077 pCert = HeapAlloc(GetProcessHeap(), 0, len);
1078 if (!pCert)
1080 ret = FALSE;
1081 goto error;
1083 ret = ImageGetCertificateData(file, dwIndex, pCert, &len);
1084 if (!ret)
1085 goto error;
1086 pCert->dwLength -= FIELD_OFFSET(WIN_CERTIFICATE, bCertificate);
1087 if (*pcbSignedDataMsg < pCert->dwLength)
1089 *pcbSignedDataMsg = pCert->dwLength;
1090 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1091 ret = FALSE;
1093 else
1095 memcpy(pbSignedDataMsg, pCert->bCertificate, pCert->dwLength);
1096 *pcbSignedDataMsg = pCert->dwLength;
1097 switch (pCert->wCertificateType)
1099 case WIN_CERT_TYPE_X509:
1100 *pdwEncodingType = X509_ASN_ENCODING;
1101 break;
1102 case WIN_CERT_TYPE_PKCS_SIGNED_DATA:
1103 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1104 break;
1105 default:
1106 WARN("don't know what to do for encoding type %d\n",
1107 pCert->wCertificateType);
1108 *pdwEncodingType = 0;
1109 ret = FALSE;
1113 error:
1114 if(pSubjectInfo->hFile != file)
1115 CloseHandle(file);
1116 HeapFree(GetProcessHeap(), 0, pCert);
1117 return ret;
1120 static BOOL WINTRUST_PutSignedMsgToPEFile(SIP_SUBJECTINFO* pSubjectInfo, DWORD pdwEncodingType,
1121 DWORD* pdwIndex, DWORD cbSignedDataMsg, BYTE* pbSignedDataMsg)
1123 WIN_CERTIFICATE *cert;
1124 HANDLE file;
1125 DWORD size;
1126 BOOL ret;
1128 if(pSubjectInfo->hFile && pSubjectInfo->hFile!=INVALID_HANDLE_VALUE)
1129 file = pSubjectInfo->hFile;
1130 else
1132 file = CreateFileW(pSubjectInfo->pwsFileName, GENERIC_READ|GENERIC_WRITE,
1133 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1134 if(file == INVALID_HANDLE_VALUE)
1135 return FALSE;
1138 /* int aligned WIN_CERTIFICATE structure with cbSignedDataMsg+1 bytes of data */
1139 size = FIELD_OFFSET(WIN_CERTIFICATE, bCertificate[cbSignedDataMsg+4]) & (~3);
1140 cert = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1141 if(!cert)
1142 return FALSE;
1144 cert->dwLength = size;
1145 cert->wRevision = WIN_CERT_REVISION_2_0;
1146 cert->wCertificateType = WIN_CERT_TYPE_PKCS_SIGNED_DATA;
1147 memcpy(cert->bCertificate, pbSignedDataMsg, cbSignedDataMsg);
1148 ret = ImageAddCertificate(file, cert, pdwIndex);
1150 HeapFree(GetProcessHeap(), 0, cert);
1151 if(file != pSubjectInfo->hFile)
1152 CloseHandle(file);
1153 return ret;
1156 /* structure offsets */
1157 #define cfhead_Signature (0x00)
1158 #define cfhead_CabinetSize (0x08)
1159 #define cfhead_MinorVersion (0x18)
1160 #define cfhead_MajorVersion (0x19)
1161 #define cfhead_Flags (0x1E)
1162 #define cfhead_SIZEOF (0x24)
1163 #define cfheadext_HeaderReserved (0x00)
1164 #define cfheadext_SIZEOF (0x04)
1165 #define cfsigninfo_CertOffset (0x04)
1166 #define cfsigninfo_CertSize (0x08)
1167 #define cfsigninfo_SIZEOF (0x0C)
1169 /* flags */
1170 #define cfheadRESERVE_PRESENT (0x0004)
1172 /* endian-neutral reading of little-endian data */
1173 #define EndGetI32(a) ((((a)[3])<<24)|(((a)[2])<<16)|(((a)[1])<<8)|((a)[0]))
1174 #define EndGetI16(a) ((((a)[1])<<8)|((a)[0]))
1176 /* For documentation purposes only: this is the structure in the reserved
1177 * area of a signed cabinet file. The cert offset indicates where in the
1178 * cabinet file the signature resides, and the count indicates its size.
1180 typedef struct _CAB_SIGNINFO
1182 WORD unk0; /* always 0? */
1183 WORD unk1; /* always 0x0010? */
1184 DWORD dwCertOffset;
1185 DWORD cbCertBlock;
1186 } CAB_SIGNINFO, *PCAB_SIGNINFO;
1188 static BOOL WINTRUST_GetSignedMsgFromCabFile(SIP_SUBJECTINFO *pSubjectInfo,
1189 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1190 BYTE *pbSignedDataMsg)
1192 int header_resv;
1193 LONG base_offset, cabsize;
1194 USHORT flags;
1195 BYTE buf[64];
1196 DWORD cert_offset, cert_size, dwRead;
1198 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1199 pcbSignedDataMsg, pbSignedDataMsg);
1201 /* get basic offset & size info */
1202 base_offset = SetFilePointer(pSubjectInfo->hFile, 0L, NULL, SEEK_CUR);
1204 if (SetFilePointer(pSubjectInfo->hFile, 0, NULL, SEEK_END) == INVALID_SET_FILE_POINTER)
1206 TRACE("seek error\n");
1207 return FALSE;
1210 cabsize = SetFilePointer(pSubjectInfo->hFile, 0L, NULL, SEEK_CUR);
1211 if ((cabsize == -1) || (base_offset == -1) ||
1212 (SetFilePointer(pSubjectInfo->hFile, 0, NULL, SEEK_SET) == INVALID_SET_FILE_POINTER))
1214 TRACE("seek error\n");
1215 return FALSE;
1218 /* read in the CFHEADER */
1219 if (!ReadFile(pSubjectInfo->hFile, buf, cfhead_SIZEOF, &dwRead, NULL) ||
1220 dwRead != cfhead_SIZEOF)
1222 TRACE("reading header failed\n");
1223 return FALSE;
1226 /* check basic MSCF signature */
1227 if (EndGetI32(buf+cfhead_Signature) != 0x4643534d)
1229 WARN("cabinet signature not present\n");
1230 return FALSE;
1233 /* Ignore the number of folders and files and the set and cabinet IDs */
1235 /* check the header revision */
1236 if ((buf[cfhead_MajorVersion] > 1) ||
1237 (buf[cfhead_MajorVersion] == 1 && buf[cfhead_MinorVersion] > 3))
1239 WARN("cabinet format version > 1.3\n");
1240 return FALSE;
1243 /* pull the flags out */
1244 flags = EndGetI16(buf+cfhead_Flags);
1246 if (!(flags & cfheadRESERVE_PRESENT))
1248 TRACE("no header present, not signed\n");
1249 return FALSE;
1252 if (!ReadFile(pSubjectInfo->hFile, buf, cfheadext_SIZEOF, &dwRead, NULL) ||
1253 dwRead != cfheadext_SIZEOF)
1255 ERR("bunk reserve-sizes?\n");
1256 return FALSE;
1259 header_resv = EndGetI16(buf+cfheadext_HeaderReserved);
1260 if (!header_resv)
1262 TRACE("no header_resv, not signed\n");
1263 return FALSE;
1265 else if (header_resv < cfsigninfo_SIZEOF)
1267 TRACE("header_resv too small, not signed\n");
1268 return FALSE;
1271 if (header_resv > 60000)
1273 WARN("WARNING; header reserved space > 60000\n");
1276 if (!ReadFile(pSubjectInfo->hFile, buf, cfsigninfo_SIZEOF, &dwRead, NULL) ||
1277 dwRead != cfsigninfo_SIZEOF)
1279 ERR("couldn't read reserve\n");
1280 return FALSE;
1283 cert_offset = EndGetI32(buf+cfsigninfo_CertOffset);
1284 TRACE("cert_offset: %d\n", cert_offset);
1285 cert_size = EndGetI32(buf+cfsigninfo_CertSize);
1286 TRACE("cert_size: %d\n", cert_size);
1288 /* The redundant checks are to avoid wraparound */
1289 if (cert_offset > cabsize || cert_size > cabsize ||
1290 cert_offset + cert_size > cabsize)
1292 WARN("offset beyond file, not attempting to read\n");
1293 return FALSE;
1296 SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET);
1297 if (!pbSignedDataMsg)
1299 *pcbSignedDataMsg = cert_size;
1300 return TRUE;
1302 if (*pcbSignedDataMsg < cert_size)
1304 *pcbSignedDataMsg = cert_size;
1305 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1306 return FALSE;
1308 if (SetFilePointer(pSubjectInfo->hFile, cert_offset, NULL, SEEK_SET) == INVALID_SET_FILE_POINTER)
1310 ERR("couldn't seek to cert location\n");
1311 return FALSE;
1313 if (!ReadFile(pSubjectInfo->hFile, pbSignedDataMsg, cert_size, &dwRead,
1314 NULL) || dwRead != cert_size)
1316 ERR("couldn't read cert\n");
1317 SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET);
1318 return FALSE;
1320 /* The encoding of the files I've seen appears to be in ASN.1
1321 * format, and there isn't a field indicating the type, so assume it
1322 * always is.
1324 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1325 /* Restore base offset */
1326 SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET);
1327 return TRUE;
1330 static BOOL WINTRUST_GetSignedMsgFromCatFile(SIP_SUBJECTINFO *pSubjectInfo,
1331 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1332 BYTE *pbSignedDataMsg)
1334 BOOL ret;
1336 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1337 pcbSignedDataMsg, pbSignedDataMsg);
1339 if (!pbSignedDataMsg)
1341 *pcbSignedDataMsg = GetFileSize(pSubjectInfo->hFile, NULL);
1342 ret = TRUE;
1344 else
1346 DWORD len = GetFileSize(pSubjectInfo->hFile, NULL);
1348 if (*pcbSignedDataMsg < len)
1350 *pcbSignedDataMsg = len;
1351 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1352 ret = FALSE;
1354 else
1356 ret = ReadFile(pSubjectInfo->hFile, pbSignedDataMsg, len,
1357 pcbSignedDataMsg, NULL);
1358 if (ret)
1359 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1362 return ret;
1365 /* GUIDs used by CryptSIPGetSignedDataMsg and CryptSIPPutSignedDataMsg */
1366 static const GUID unknown = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,
1367 0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
1368 static const GUID cabGUID = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,
1369 0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
1370 static const GUID catGUID = { 0xDE351A43, 0x8E59, 0x11D0, { 0x8C,0x47,
1371 0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
1373 /***********************************************************************
1374 * CryptSIPGetSignedDataMsg (WINTRUST.@)
1376 BOOL WINAPI CryptSIPGetSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pdwEncodingType,
1377 DWORD dwIndex, DWORD* pcbSignedDataMsg, BYTE* pbSignedDataMsg)
1379 BOOL ret;
1381 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1382 pcbSignedDataMsg, pbSignedDataMsg);
1384 if(!pSubjectInfo)
1386 SetLastError(ERROR_INVALID_PARAMETER);
1387 return FALSE;
1390 if (!memcmp(pSubjectInfo->pgSubjectType, &unknown, sizeof(unknown)))
1391 ret = WINTRUST_GetSignedMsgFromPEFile(pSubjectInfo, pdwEncodingType,
1392 dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
1393 else if (!memcmp(pSubjectInfo->pgSubjectType, &cabGUID, sizeof(cabGUID)))
1394 ret = WINTRUST_GetSignedMsgFromCabFile(pSubjectInfo, pdwEncodingType,
1395 dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
1396 else if (!memcmp(pSubjectInfo->pgSubjectType, &catGUID, sizeof(catGUID)))
1397 ret = WINTRUST_GetSignedMsgFromCatFile(pSubjectInfo, pdwEncodingType,
1398 dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
1399 else
1401 FIXME("unimplemented for subject type %s\n",
1402 debugstr_guid(pSubjectInfo->pgSubjectType));
1403 ret = FALSE;
1406 TRACE("returning %d\n", ret);
1407 return ret;
1410 /***********************************************************************
1411 * CryptSIPPutSignedDataMsg (WINTRUST.@)
1413 BOOL WINAPI CryptSIPPutSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD pdwEncodingType,
1414 DWORD* pdwIndex, DWORD cbSignedDataMsg, BYTE* pbSignedDataMsg)
1416 TRACE("(%p %d %p %d %p)\n", pSubjectInfo, pdwEncodingType, pdwIndex,
1417 cbSignedDataMsg, pbSignedDataMsg);
1419 if(!pSubjectInfo) {
1420 SetLastError(ERROR_INVALID_PARAMETER);
1421 return FALSE;
1424 if(!memcmp(pSubjectInfo->pgSubjectType, &unknown, sizeof(unknown)))
1425 return WINTRUST_PutSignedMsgToPEFile(pSubjectInfo, pdwEncodingType,
1426 pdwIndex, cbSignedDataMsg, pbSignedDataMsg);
1427 else
1428 FIXME("unimplemented for subject type %s\n",
1429 debugstr_guid(pSubjectInfo->pgSubjectType));
1431 return FALSE;
1434 /***********************************************************************
1435 * CryptSIPRemoveSignedDataMsg (WINTRUST.@)
1437 BOOL WINAPI CryptSIPRemoveSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo,
1438 DWORD dwIndex)
1440 FIXME("(%p %d) stub\n", pSubjectInfo, dwIndex);
1442 return FALSE;
1445 /***********************************************************************
1446 * CryptSIPVerifyIndirectData (WINTRUST.@)
1448 BOOL WINAPI CryptSIPVerifyIndirectData(SIP_SUBJECTINFO* pSubjectInfo,
1449 SIP_INDIRECT_DATA* pIndirectData)
1451 FIXME("(%p %p) stub\n", pSubjectInfo, pIndirectData);
1453 return FALSE;