Sync up with trunk r61578.
[reactos.git] / dll / win32 / wintrust / crypt.c
1 /*
2 * WinTrust Cryptography functions
3 *
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
9 *
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.
14 *
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.
19 *
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
23 */
24
25 #include "wintrust_priv.h"
26
27 #include <imagehlp.h>
28
29 WINE_DEFAULT_DEBUG_CHANNEL(wintrust);
30
31 #define CATADMIN_MAGIC 0x43415441 /* 'CATA' */
32 #define CRYPTCAT_MAGIC 0x43415443 /* 'CATC' */
33 #define CATINFO_MAGIC 0x43415449 /* 'CATI' */
34
35 struct cryptcat
36 {
37 DWORD magic;
38 HCRYPTMSG msg;
39 DWORD encoding;
40 CTL_INFO *inner;
41 DWORD inner_len;
42 GUID subject;
43 DWORD attr_count;
44 CRYPTCATATTRIBUTE *attr;
45 };
46
47 struct catadmin
48 {
49 DWORD magic;
50 WCHAR path[MAX_PATH];
51 HANDLE find;
52 };
53
54 struct catinfo
55 {
56 DWORD magic;
57 WCHAR file[MAX_PATH];
58 };
59
60 static HCATINFO create_catinfo(const WCHAR *filename)
61 {
62 struct catinfo *ci;
63
64 if (!(ci = HeapAlloc(GetProcessHeap(), 0, sizeof(*ci))))
65 {
66 SetLastError(ERROR_OUTOFMEMORY);
67 return INVALID_HANDLE_VALUE;
68 }
69 strcpyW(ci->file, filename);
70 ci->magic = CATINFO_MAGIC;
71 return ci;
72 }
73
74 /***********************************************************************
75 * CryptCATAdminAcquireContext (WINTRUST.@)
76 *
77 * Get a catalog administrator context handle.
78 *
79 * PARAMS
80 * catAdmin [O] Pointer to the context handle.
81 * sys [I] Pointer to a GUID for the needed subsystem.
82 * dwFlags [I] Reserved.
83 *
84 * RETURNS
85 * Success: TRUE. catAdmin contains the context handle.
86 * Failure: FALSE.
87 *
88 */
89 BOOL WINAPI CryptCATAdminAcquireContext(HCATADMIN *catAdmin,
90 const GUID *sys, DWORD dwFlags)
91 {
92 static const WCHAR catroot[] =
93 {'\\','c','a','t','r','o','o','t',0};
94 static const WCHAR fmt[] =
95 {'%','s','\\','{','%','0','8','x','-','%','0','4','x','-','%','0',
96 '4','x','-','%','0','2','x','%','0','2','x','-','%','0','2','x',
97 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x',
98 '%','0','2','x','}',0};
99 static const GUID defsys =
100 {0x127d0a1d,0x4ef2,0x11d1,{0x86,0x08,0x00,0xc0,0x4f,0xc2,0x95,0xee}};
101
102 WCHAR catroot_dir[MAX_PATH];
103 struct catadmin *ca;
104
105 TRACE("%p %s %x\n", catAdmin, debugstr_guid(sys), dwFlags);
106
107 if (!catAdmin)
108 {
109 SetLastError(ERROR_INVALID_PARAMETER);
110 return FALSE;
111 }
112 if (!(ca = HeapAlloc(GetProcessHeap(), 0, sizeof(*ca))))
113 {
114 SetLastError(ERROR_OUTOFMEMORY);
115 return FALSE;
116 }
117
118 GetSystemDirectoryW(catroot_dir, MAX_PATH);
119 strcatW(catroot_dir, catroot);
120
121 /* create the directory if it doesn't exist */
122 CreateDirectoryW(catroot_dir, NULL);
123
124 if (!sys) sys = &defsys;
125 sprintfW(ca->path, fmt, catroot_dir, sys->Data1, sys->Data2,
126 sys->Data3, sys->Data4[0], sys->Data4[1], sys->Data4[2],
127 sys->Data4[3], sys->Data4[4], sys->Data4[5], sys->Data4[6],
128 sys->Data4[7]);
129
130 /* create the directory if it doesn't exist */
131 CreateDirectoryW(ca->path, NULL);
132
133 ca->magic = CATADMIN_MAGIC;
134 ca->find = INVALID_HANDLE_VALUE;
135
136 *catAdmin = ca;
137 return TRUE;
138 }
139
140 /***********************************************************************
141 * CryptCATAdminAddCatalog (WINTRUST.@)
142 */
143 HCATINFO WINAPI CryptCATAdminAddCatalog(HCATADMIN catAdmin, PWSTR catalogFile,
144 PWSTR selectBaseName, DWORD flags)
145 {
146 static const WCHAR slashW[] = {'\\',0};
147 struct catadmin *ca = catAdmin;
148 struct catinfo *ci;
149 WCHAR *target;
150 DWORD len;
151
152 TRACE("%p %s %s %d\n", catAdmin, debugstr_w(catalogFile),
153 debugstr_w(selectBaseName), flags);
154
155 if (!selectBaseName)
156 {
157 FIXME("NULL basename not handled\n");
158 SetLastError(ERROR_INVALID_PARAMETER);
159 return NULL;
160 }
161 if (!ca || ca->magic != CATADMIN_MAGIC || !catalogFile || flags)
162 {
163 SetLastError(ERROR_INVALID_PARAMETER);
164 return NULL;
165 }
166
167 len = strlenW(ca->path) + strlenW(selectBaseName) + 2;
168 if (!(target = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
169 {
170 SetLastError(ERROR_OUTOFMEMORY);
171 return NULL;
172 }
173 strcpyW(target, ca->path);
174 strcatW(target, slashW);
175 strcatW(target, selectBaseName);
176
177 if (!CopyFileW(catalogFile, target, FALSE))
178 {
179 HeapFree(GetProcessHeap(), 0, target);
180 return NULL;
181 }
182 SetFileAttributesW(target, FILE_ATTRIBUTE_SYSTEM);
183
184 if (!(ci = HeapAlloc(GetProcessHeap(), 0, sizeof(*ci))))
185 {
186 HeapFree(GetProcessHeap(), 0, target);
187 SetLastError(ERROR_OUTOFMEMORY);
188 return NULL;
189 }
190 ci->magic = CATINFO_MAGIC;
191 strcpyW(ci->file, target);
192
193 HeapFree(GetProcessHeap(), 0, target);
194 return ci;
195 }
196
197 /***********************************************************************
198 * CryptCATAdminCalcHashFromFileHandle (WINTRUST.@)
199 */
200 BOOL WINAPI CryptCATAdminCalcHashFromFileHandle(HANDLE hFile, DWORD* pcbHash,
201 BYTE* pbHash, DWORD dwFlags )
202 {
203 BOOL ret = FALSE;
204
205 TRACE("%p %p %p %x\n", hFile, pcbHash, pbHash, dwFlags);
206
207 if (!hFile || !pcbHash || dwFlags)
208 {
209 SetLastError(ERROR_INVALID_PARAMETER);
210 return FALSE;
211 }
212 if (*pcbHash < 20)
213 {
214 *pcbHash = 20;
215 SetLastError(ERROR_INSUFFICIENT_BUFFER);
216 return TRUE;
217 }
218
219 *pcbHash = 20;
220 if (pbHash)
221 {
222 HCRYPTPROV prov;
223 HCRYPTHASH hash;
224 DWORD bytes_read;
225 BYTE *buffer;
226
227 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, 4096)))
228 {
229 SetLastError(ERROR_OUTOFMEMORY);
230 return FALSE;
231 }
232 ret = CryptAcquireContextW(&prov, NULL, MS_DEF_PROV_W, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
233 if (!ret)
234 {
235 HeapFree(GetProcessHeap(), 0, buffer);
236 return FALSE;
237 }
238 ret = CryptCreateHash(prov, CALG_SHA1, 0, 0, &hash);
239 if (!ret)
240 {
241 HeapFree(GetProcessHeap(), 0, buffer);
242 CryptReleaseContext(prov, 0);
243 return FALSE;
244 }
245 while ((ret = ReadFile(hFile, buffer, 4096, &bytes_read, NULL)) && bytes_read)
246 {
247 CryptHashData(hash, buffer, bytes_read, 0);
248 }
249 if (ret) ret = CryptGetHashParam(hash, HP_HASHVAL, pbHash, pcbHash, 0);
250
251 HeapFree(GetProcessHeap(), 0, buffer);
252 CryptDestroyHash(hash);
253 CryptReleaseContext(prov, 0);
254 }
255 return ret;
256 }
257
258 /***********************************************************************
259 * CryptCATAdminEnumCatalogFromHash (WINTRUST.@)
260 */
261 HCATINFO WINAPI CryptCATAdminEnumCatalogFromHash(HCATADMIN hCatAdmin, BYTE* pbHash,
262 DWORD cbHash, DWORD dwFlags,
263 HCATINFO* phPrevCatInfo )
264 {
265 static const WCHAR slashW[] = {'\\',0};
266 static const WCHAR globW[] = {'\\','*','.','c','a','t',0};
267
268 struct catadmin *ca = hCatAdmin;
269 WIN32_FIND_DATAW data;
270 HCATINFO prev = NULL;
271 HCRYPTPROV prov;
272 DWORD size;
273 BOOL ret;
274
275 TRACE("%p %p %d %x %p\n", hCatAdmin, pbHash, cbHash, dwFlags, phPrevCatInfo);
276
277 if (!ca || ca->magic != CATADMIN_MAGIC || !pbHash || cbHash != 20 || dwFlags)
278 {
279 SetLastError(ERROR_INVALID_PARAMETER);
280 return NULL;
281 }
282 if (phPrevCatInfo) prev = *phPrevCatInfo;
283
284 ret = CryptAcquireContextW(&prov, NULL, MS_DEF_PROV_W, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
285 if (!ret) return NULL;
286
287 if (!prev)
288 {
289 WCHAR *path;
290
291 size = strlenW(ca->path) * sizeof(WCHAR) + sizeof(globW);
292 if (!(path = HeapAlloc(GetProcessHeap(), 0, size)))
293 {
294 CryptReleaseContext(prov, 0);
295 SetLastError(ERROR_OUTOFMEMORY);
296 return NULL;
297 }
298 strcpyW(path, ca->path);
299 strcatW(path, globW);
300
301 FindClose(ca->find);
302 ca->find = FindFirstFileW(path, &data);
303
304 HeapFree(GetProcessHeap(), 0, path);
305 if (ca->find == INVALID_HANDLE_VALUE)
306 {
307 CryptReleaseContext(prov, 0);
308 return NULL;
309 }
310 }
311 else if (!FindNextFileW(ca->find, &data))
312 {
313 CryptCATAdminReleaseCatalogContext(hCatAdmin, prev, 0);
314 CryptReleaseContext(prov, 0);
315 return NULL;
316 }
317
318 while (1)
319 {
320 WCHAR *filename;
321 CRYPTCATMEMBER *member = NULL;
322 struct catinfo *ci;
323 HANDLE hcat;
324
325 size = (strlenW(ca->path) + strlenW(data.cFileName) + 2) * sizeof(WCHAR);
326 if (!(filename = HeapAlloc(GetProcessHeap(), 0, size)))
327 {
328 SetLastError(ERROR_OUTOFMEMORY);
329 return NULL;
330 }
331 strcpyW(filename, ca->path);
332 strcatW(filename, slashW);
333 strcatW(filename, data.cFileName);
334
335 hcat = CryptCATOpen(filename, CRYPTCAT_OPEN_EXISTING, prov, 0, 0);
336 if (hcat == INVALID_HANDLE_VALUE)
337 {
338 WARN("couldn't open %s (%u)\n", debugstr_w(filename), GetLastError());
339 continue;
340 }
341 while ((member = CryptCATEnumerateMember(hcat, member)))
342 {
343 if (member->pIndirectData->Digest.cbData != cbHash)
344 {
345 WARN("amount of hash bytes differs: %u/%u\n", member->pIndirectData->Digest.cbData, cbHash);
346 continue;
347 }
348 if (!memcmp(member->pIndirectData->Digest.pbData, pbHash, cbHash))
349 {
350 TRACE("file %s matches\n", debugstr_w(data.cFileName));
351
352 CryptCATClose(hcat);
353 CryptReleaseContext(prov, 0);
354 if (!phPrevCatInfo)
355 {
356 FindClose(ca->find);
357 ca->find = INVALID_HANDLE_VALUE;
358 }
359 ci = create_catinfo(filename);
360 HeapFree(GetProcessHeap(), 0, filename);
361 return ci;
362 }
363 }
364 CryptCATClose(hcat);
365 HeapFree(GetProcessHeap(), 0, filename);
366
367 if (!FindNextFileW(ca->find, &data))
368 {
369 FindClose(ca->find);
370 ca->find = INVALID_HANDLE_VALUE;
371 CryptReleaseContext(prov, 0);
372 return NULL;
373 }
374 }
375 return NULL;
376 }
377
378 /***********************************************************************
379 * CryptCATAdminReleaseCatalogContext (WINTRUST.@)
380 *
381 * Release a catalog context handle.
382 *
383 * PARAMS
384 * hCatAdmin [I] Context handle.
385 * hCatInfo [I] Catalog handle.
386 * dwFlags [I] Reserved.
387 *
388 * RETURNS
389 * Success: TRUE.
390 * Failure: FALSE.
391 *
392 */
393 BOOL WINAPI CryptCATAdminReleaseCatalogContext(HCATADMIN hCatAdmin,
394 HCATINFO hCatInfo,
395 DWORD dwFlags)
396 {
397 struct catinfo *ci = hCatInfo;
398 struct catadmin *ca = hCatAdmin;
399
400 TRACE("%p %p %x\n", hCatAdmin, hCatInfo, dwFlags);
401
402 if (!ca || ca->magic != CATADMIN_MAGIC || !ci || ci->magic != CATINFO_MAGIC)
403 {
404 SetLastError(ERROR_INVALID_PARAMETER);
405 return FALSE;
406 }
407 ci->magic = 0;
408 return HeapFree(GetProcessHeap(), 0, ci);
409 }
410
411 /***********************************************************************
412 * CryptCATAdminReleaseContext (WINTRUST.@)
413 *
414 * Release a catalog administrator context handle.
415 *
416 * PARAMS
417 * catAdmin [I] Context handle.
418 * dwFlags [I] Reserved.
419 *
420 * RETURNS
421 * Success: TRUE.
422 * Failure: FALSE.
423 *
424 */
425 BOOL WINAPI CryptCATAdminReleaseContext(HCATADMIN hCatAdmin, DWORD dwFlags )
426 {
427 struct catadmin *ca = hCatAdmin;
428
429 TRACE("%p %x\n", hCatAdmin, dwFlags);
430
431 if (!ca || ca->magic != CATADMIN_MAGIC)
432 {
433 SetLastError(ERROR_INVALID_PARAMETER);
434 return FALSE;
435 }
436 if (ca->find != INVALID_HANDLE_VALUE) FindClose(ca->find);
437 ca->magic = 0;
438 return HeapFree(GetProcessHeap(), 0, ca);
439 }
440
441 /***********************************************************************
442 * CryptCATAdminRemoveCatalog (WINTRUST.@)
443 *
444 * Remove a catalog file.
445 *
446 * PARAMS
447 * catAdmin [I] Context handle.
448 * pwszCatalogFile [I] Catalog file.
449 * dwFlags [I] Reserved.
450 *
451 * RETURNS
452 * Success: TRUE.
453 * Failure: FALSE.
454 *
455 */
456 BOOL WINAPI CryptCATAdminRemoveCatalog(HCATADMIN hCatAdmin, LPCWSTR pwszCatalogFile, DWORD dwFlags)
457 {
458 struct catadmin *ca = hCatAdmin;
459
460 TRACE("%p %s %x\n", hCatAdmin, debugstr_w(pwszCatalogFile), dwFlags);
461
462 if (!ca || ca->magic != CATADMIN_MAGIC)
463 {
464 SetLastError(ERROR_INVALID_PARAMETER);
465 return FALSE;
466 }
467
468 /* Only delete when there is a filename and no path */
469 if (pwszCatalogFile && pwszCatalogFile[0] != 0 &&
470 !strchrW(pwszCatalogFile, '\\') && !strchrW(pwszCatalogFile, '/') &&
471 !strchrW(pwszCatalogFile, ':'))
472 {
473 static const WCHAR slashW[] = {'\\',0};
474 WCHAR *target;
475 DWORD len;
476
477 len = strlenW(ca->path) + strlenW(pwszCatalogFile) + 2;
478 if (!(target = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
479 {
480 SetLastError(ERROR_OUTOFMEMORY);
481 return FALSE;
482 }
483 strcpyW(target, ca->path);
484 strcatW(target, slashW);
485 strcatW(target, pwszCatalogFile);
486
487 DeleteFileW(target);
488
489 HeapFree(GetProcessHeap(), 0, target);
490 }
491
492 return TRUE;
493 }
494
495 /***********************************************************************
496 * CryptCATAdminResolveCatalogPath (WINTRUST.@)
497 */
498 BOOL WINAPI CryptCATAdminResolveCatalogPath(HCATADMIN hcatadmin, WCHAR *catalog_file,
499 CATALOG_INFO *info, DWORD flags)
500 {
501 static const WCHAR slashW[] = {'\\',0};
502 struct catadmin *ca = hcatadmin;
503
504 TRACE("%p %s %p %x\n", hcatadmin, debugstr_w(catalog_file), info, flags);
505
506 if (!ca || ca->magic != CATADMIN_MAGIC || !info || info->cbStruct != sizeof(*info) || flags)
507 {
508 SetLastError(ERROR_INVALID_PARAMETER);
509 return FALSE;
510 }
511 strcpyW(info->wszCatalogFile, ca->path);
512 strcatW(info->wszCatalogFile, slashW);
513 strcatW(info->wszCatalogFile, catalog_file);
514
515 return TRUE;
516 }
517
518 /***********************************************************************
519 * CryptCATClose (WINTRUST.@)
520 */
521 BOOL WINAPI CryptCATClose(HANDLE hCatalog)
522 {
523 struct cryptcat *cc = hCatalog;
524
525 TRACE("(%p)\n", hCatalog);
526
527 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
528 {
529 SetLastError(ERROR_INVALID_PARAMETER);
530 return FALSE;
531 }
532 HeapFree(GetProcessHeap(), 0, cc->attr);
533 HeapFree(GetProcessHeap(), 0, cc->inner);
534 CryptMsgClose(cc->msg);
535
536 cc->magic = 0;
537 HeapFree(GetProcessHeap(), 0, cc);
538 return TRUE;
539 }
540
541 /***********************************************************************
542 * CryptCATGetAttrInfo (WINTRUST.@)
543 */
544 CRYPTCATATTRIBUTE * WINAPI CryptCATGetAttrInfo(HANDLE hCatalog, CRYPTCATMEMBER *member, LPWSTR tag)
545 {
546 struct cryptcat *cc = hCatalog;
547
548 FIXME("%p, %p, %s\n", hCatalog, member, debugstr_w(tag));
549
550 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
551 {
552 SetLastError(ERROR_INVALID_PARAMETER);
553 return NULL;
554 }
555 SetLastError(CRYPT_E_NOT_FOUND);
556 return NULL;
557 }
558
559 /***********************************************************************
560 * CryptCATGetCatAttrInfo (WINTRUST.@)
561 */
562 CRYPTCATATTRIBUTE * WINAPI CryptCATGetCatAttrInfo(HANDLE hCatalog, LPWSTR tag)
563 {
564 struct cryptcat *cc = hCatalog;
565
566 FIXME("%p, %s\n", hCatalog, debugstr_w(tag));
567
568 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
569 {
570 SetLastError(ERROR_INVALID_PARAMETER);
571 return NULL;
572 }
573 SetLastError(CRYPT_E_NOT_FOUND);
574 return NULL;
575 }
576
577 CRYPTCATMEMBER * WINAPI CryptCATGetMemberInfo(HANDLE hCatalog, LPWSTR tag)
578 {
579 struct cryptcat *cc = hCatalog;
580
581 FIXME("%p, %s\n", hCatalog, debugstr_w(tag));
582
583 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
584 {
585 SetLastError(ERROR_INVALID_PARAMETER);
586 return NULL;
587 }
588 SetLastError(CRYPT_E_NOT_FOUND);
589 return NULL;
590 }
591
592 /***********************************************************************
593 * CryptCATEnumerateAttr (WINTRUST.@)
594 */
595 CRYPTCATATTRIBUTE * WINAPI CryptCATEnumerateAttr(HANDLE hCatalog, CRYPTCATMEMBER *member, CRYPTCATATTRIBUTE *prev)
596 {
597 struct cryptcat *cc = hCatalog;
598
599 FIXME("%p, %p, %p\n", hCatalog, member, prev);
600
601 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
602 {
603 SetLastError(ERROR_INVALID_PARAMETER);
604 return NULL;
605 }
606 SetLastError(CRYPT_E_NOT_FOUND);
607 return NULL;
608 }
609
610 /***********************************************************************
611 * CryptCATEnumerateCatAttr (WINTRUST.@)
612 */
613 CRYPTCATATTRIBUTE * WINAPI CryptCATEnumerateCatAttr(HANDLE hCatalog, CRYPTCATATTRIBUTE *prev)
614 {
615 struct cryptcat *cc = hCatalog;
616
617 FIXME("%p, %p\n", hCatalog, prev);
618
619 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
620 {
621 SetLastError(ERROR_INVALID_PARAMETER);
622 return NULL;
623 }
624 SetLastError(CRYPT_E_NOT_FOUND);
625 return NULL;
626 }
627
628 /***********************************************************************
629 * CryptCATEnumerateMember (WINTRUST.@)
630 */
631 CRYPTCATMEMBER * WINAPI CryptCATEnumerateMember(HANDLE hCatalog, CRYPTCATMEMBER *prev)
632 {
633 struct cryptcat *cc = hCatalog;
634 CRYPTCATMEMBER *member = prev;
635 CTL_ENTRY *entry;
636 DWORD size, i;
637
638 TRACE("%p, %p\n", hCatalog, prev);
639
640 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
641 {
642 SetLastError(ERROR_INVALID_PARAMETER);
643 return NULL;
644 }
645
646 /* dumping the contents makes me think that dwReserved is the iteration number */
647 if (!member)
648 {
649 if (!(member = HeapAlloc(GetProcessHeap(), 0, sizeof(*member))))
650 {
651 SetLastError(ERROR_OUTOFMEMORY);
652 return NULL;
653 }
654 member->cbStruct = sizeof(*member);
655 member->pwszFileName = member->pwszReferenceTag = NULL;
656 member->dwReserved = 0;
657 member->hReserved = NULL;
658 member->gSubjectType = cc->subject;
659 member->fdwMemberFlags = 0;
660 member->pIndirectData = NULL;
661 member->dwCertVersion = cc->inner->dwVersion;
662 }
663 else member->dwReserved++;
664
665 if (member->dwReserved >= cc->inner->cCTLEntry)
666 {
667 SetLastError(ERROR_INVALID_PARAMETER);
668 goto error;
669 }
670
671 /* list them backwards, like native */
672 entry = &cc->inner->rgCTLEntry[cc->inner->cCTLEntry - member->dwReserved - 1];
673
674 member->sEncodedIndirectData.cbData = member->sEncodedMemberInfo.cbData = 0;
675 member->sEncodedIndirectData.pbData = member->sEncodedMemberInfo.pbData = NULL;
676 HeapFree(GetProcessHeap(), 0, member->pIndirectData);
677 member->pIndirectData = NULL;
678
679 for (i = 0; i < entry->cAttribute; i++)
680 {
681 CRYPT_ATTRIBUTE *attr = entry->rgAttribute + i;
682
683 if (attr->cValue != 1)
684 {
685 ERR("Can't handle attr->cValue of %u\n", attr->cValue);
686 continue;
687 }
688 if (!strcmp(attr->pszObjId, CAT_MEMBERINFO_OBJID))
689 {
690 CAT_MEMBERINFO *mi;
691 BOOL ret;
692
693 member->sEncodedMemberInfo.cbData = attr->rgValue->cbData;
694 member->sEncodedMemberInfo.pbData = attr->rgValue->pbData;
695
696 CryptDecodeObject(cc->encoding, CAT_MEMBERINFO_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, NULL, &size);
697
698 if (!(mi = HeapAlloc(GetProcessHeap(), 0, size)))
699 {
700 SetLastError(ERROR_OUTOFMEMORY);
701 goto error;
702 }
703 ret = CryptDecodeObject(cc->encoding, CAT_MEMBERINFO_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, mi, &size);
704 if (ret)
705 {
706 UNICODE_STRING guid;
707
708 member->dwCertVersion = mi->dwCertVersion;
709 RtlInitUnicodeString(&guid, mi->pwszSubjGuid);
710 if (RtlGUIDFromString(&guid, &member->gSubjectType))
711 {
712 HeapFree(GetProcessHeap(), 0, mi);
713 goto error;
714 }
715 }
716 HeapFree(GetProcessHeap(), 0, mi);
717 if (!ret) goto error;
718 }
719 else if (!strcmp(attr->pszObjId, SPC_INDIRECT_DATA_OBJID))
720 {
721 /* SPC_INDIRECT_DATA_CONTENT is equal to SIP_INDIRECT_DATA */
722
723 member->sEncodedIndirectData.cbData = attr->rgValue->cbData;
724 member->sEncodedIndirectData.pbData = attr->rgValue->pbData;
725
726 CryptDecodeObject(cc->encoding, SPC_INDIRECT_DATA_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, NULL, &size);
727
728 if (!(member->pIndirectData = HeapAlloc(GetProcessHeap(), 0, size)))
729 {
730 SetLastError(ERROR_OUTOFMEMORY);
731 goto error;
732 }
733 CryptDecodeObject(cc->encoding, SPC_INDIRECT_DATA_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, member->pIndirectData, &size);
734 }
735 else
736 /* this object id should probably be handled in CryptCATEnumerateAttr */
737 FIXME("unhandled object id \"%s\"\n", attr->pszObjId);
738 }
739
740 if (!member->sEncodedMemberInfo.cbData || !member->sEncodedIndirectData.cbData)
741 {
742 ERR("Corrupted catalog entry?\n");
743 SetLastError(CRYPT_E_ATTRIBUTES_MISSING);
744 goto error;
745 }
746 size = (2 * member->pIndirectData->Digest.cbData + 1) * sizeof(WCHAR);
747 if (member->pwszReferenceTag)
748 member->pwszReferenceTag = HeapReAlloc(GetProcessHeap(), 0, member->pwszReferenceTag, size);
749 else
750 member->pwszReferenceTag = HeapAlloc(GetProcessHeap(), 0, size);
751
752 if (!member->pwszReferenceTag)
753 {
754 SetLastError(ERROR_OUTOFMEMORY);
755 goto error;
756 }
757 /* FIXME: reference tag is usually the file hash but doesn't have to be */
758 for (i = 0; i < member->pIndirectData->Digest.cbData; i++)
759 {
760 DWORD sub;
761
762 sub = member->pIndirectData->Digest.pbData[i] >> 4;
763 member->pwszReferenceTag[i * 2] = (sub < 10 ? '0' + sub : 'A' + sub - 10);
764 sub = member->pIndirectData->Digest.pbData[i] & 0xf;
765 member->pwszReferenceTag[i * 2 + 1] = (sub < 10 ? '0' + sub : 'A' + sub - 10);
766 }
767 member->pwszReferenceTag[i * 2] = 0;
768 return member;
769
770 error:
771 HeapFree(GetProcessHeap(), 0, member->pIndirectData);
772 HeapFree(GetProcessHeap(), 0, member->pwszReferenceTag);
773 HeapFree(GetProcessHeap(), 0, member);
774 return NULL;
775 }
776
777 static CTL_INFO *decode_inner_content(HANDLE hmsg, DWORD encoding, DWORD *len)
778 {
779 DWORD size;
780 LPSTR oid = NULL;
781 BYTE *buffer = NULL;
782 CTL_INFO *inner = NULL;
783
784 if (!CryptMsgGetParam(hmsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, NULL, &size)) return NULL;
785 if (!(oid = HeapAlloc(GetProcessHeap(), 0, size)))
786 {
787 SetLastError(ERROR_OUTOFMEMORY);
788 return NULL;
789 }
790 if (!CryptMsgGetParam(hmsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, oid, &size)) goto out;
791 if (!CryptMsgGetParam(hmsg, CMSG_CONTENT_PARAM, 0, NULL, &size)) goto out;
792 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, size)))
793 {
794 SetLastError(ERROR_OUTOFMEMORY);
795 goto out;
796 }
797 if (!CryptMsgGetParam(hmsg, CMSG_CONTENT_PARAM, 0, buffer, &size)) goto out;
798 if (!CryptDecodeObject(encoding, oid, buffer, size, 0, NULL, &size)) goto out;
799 if (!(inner = HeapAlloc(GetProcessHeap(), 0, size)))
800 {
801 SetLastError(ERROR_OUTOFMEMORY);
802 goto out;
803 }
804 if (!CryptDecodeObject(encoding, oid, buffer, size, 0, inner, &size)) goto out;
805 *len = size;
806
807 out:
808 HeapFree(GetProcessHeap(), 0, oid);
809 HeapFree(GetProcessHeap(), 0, buffer);
810 return inner;
811 }
812
813 /***********************************************************************
814 * CryptCATCatalogInfoFromContext (WINTRUST.@)
815 */
816 BOOL WINAPI CryptCATCatalogInfoFromContext(HCATINFO hcatinfo, CATALOG_INFO *info, DWORD flags)
817 {
818 struct catinfo *ci = hcatinfo;
819
820 TRACE("%p, %p, %x\n", hcatinfo, info, flags);
821
822 if (!hcatinfo || hcatinfo == INVALID_HANDLE_VALUE || ci->magic != CATINFO_MAGIC ||
823 flags || !info || info->cbStruct != sizeof(*info))
824 {
825 SetLastError(ERROR_INVALID_PARAMETER);
826 return FALSE;
827 }
828 strcpyW(info->wszCatalogFile, ci->file);
829 return TRUE;
830 }
831
832 /***********************************************************************
833 * CryptCATOpen (WINTRUST.@)
834 */
835 HANDLE WINAPI CryptCATOpen(LPWSTR pwszFileName, DWORD fdwOpenFlags, HCRYPTPROV hProv,
836 DWORD dwPublicVersion, DWORD dwEncodingType)
837 {
838 HANDLE file, hmsg;
839 BYTE *buffer = NULL;
840 DWORD size, flags = OPEN_EXISTING;
841 struct cryptcat *cc;
842
843 TRACE("%s, %x, %lx, %x, %x\n", debugstr_w(pwszFileName), fdwOpenFlags,
844 hProv, dwPublicVersion, dwEncodingType);
845
846 if (!pwszFileName)
847 {
848 SetLastError(ERROR_INVALID_PARAMETER);
849 return INVALID_HANDLE_VALUE;
850 }
851
852 if (!dwEncodingType) dwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
853
854 if (fdwOpenFlags & CRYPTCAT_OPEN_ALWAYS) flags |= OPEN_ALWAYS;
855 if (fdwOpenFlags & CRYPTCAT_OPEN_CREATENEW) flags |= CREATE_NEW;
856
857 file = CreateFileW(pwszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, flags, 0, NULL);
858 if (file == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
859
860 size = GetFileSize(file, NULL);
861 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, size)))
862 {
863 CloseHandle(file);
864 SetLastError(ERROR_OUTOFMEMORY);
865 return INVALID_HANDLE_VALUE;
866 }
867 if (!(hmsg = CryptMsgOpenToDecode(dwEncodingType, 0, 0, hProv, NULL, NULL)))
868 {
869 CloseHandle(file);
870 HeapFree(GetProcessHeap(), 0, buffer);
871 return INVALID_HANDLE_VALUE;
872 }
873 if (!ReadFile(file, buffer, size, &size, NULL) || !CryptMsgUpdate(hmsg, buffer, size, TRUE))
874 {
875 CloseHandle(file);
876 HeapFree(GetProcessHeap(), 0, buffer);
877 CryptMsgClose(hmsg);
878 return INVALID_HANDLE_VALUE;
879 }
880 HeapFree(GetProcessHeap(), 0, buffer);
881 CloseHandle(file);
882
883 size = sizeof(DWORD);
884 if (!(cc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cc))))
885 {
886 CryptMsgClose(hmsg);
887 SetLastError(ERROR_OUTOFMEMORY);
888 return INVALID_HANDLE_VALUE;
889 }
890
891 cc->msg = hmsg;
892 cc->encoding = dwEncodingType;
893 if (CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_COUNT_PARAM, 0, &cc->attr_count, &size))
894 {
895 DWORD i, sum = 0;
896 BYTE *p;
897
898 for (i = 0; i < cc->attr_count; i++)
899 {
900 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size))
901 {
902 CryptMsgClose(hmsg);
903 return INVALID_HANDLE_VALUE;
904 }
905 sum += size;
906 }
907 if (!(cc->attr = HeapAlloc(GetProcessHeap(), 0, sizeof(*cc->attr) * cc->attr_count + sum)))
908 {
909 CryptMsgClose(hmsg);
910 SetLastError(ERROR_OUTOFMEMORY);
911 return INVALID_HANDLE_VALUE;
912 }
913 p = (BYTE *)(cc->attr + cc->attr_count);
914 for (i = 0; i < cc->attr_count; i++)
915 {
916 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size))
917 {
918 CryptMsgClose(hmsg);
919 HeapFree(GetProcessHeap(), 0, cc->attr);
920 return INVALID_HANDLE_VALUE;
921 }
922 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, p, &size))
923 {
924 CryptMsgClose(hmsg);
925 HeapFree(GetProcessHeap(), 0, cc->attr);
926 return INVALID_HANDLE_VALUE;
927 }
928 p += size;
929 }
930 cc->inner = decode_inner_content(hmsg, dwEncodingType, &cc->inner_len);
931 if (!cc->inner || !CryptSIPRetrieveSubjectGuid(pwszFileName, NULL, &cc->subject))
932 {
933 CryptMsgClose(hmsg);
934 HeapFree(GetProcessHeap(), 0, cc->attr);
935 HeapFree(GetProcessHeap(), 0, cc->inner);
936 HeapFree(GetProcessHeap(), 0, cc);
937 return INVALID_HANDLE_VALUE;
938 }
939 cc->magic = CRYPTCAT_MAGIC;
940 return cc;
941 }
942 return INVALID_HANDLE_VALUE;
943 }
944
945 /***********************************************************************
946 * CryptSIPCreateIndirectData (WINTRUST.@)
947 */
948 BOOL WINAPI CryptSIPCreateIndirectData(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pcbIndirectData,
949 SIP_INDIRECT_DATA* pIndirectData)
950 {
951 FIXME("(%p %p %p) stub\n", pSubjectInfo, pcbIndirectData, pIndirectData);
952
953 return FALSE;
954 }
955
956
957 /***********************************************************************
958 * CryptCATCDFClose (WINTRUST.@)
959 */
960 BOOL WINAPI CryptCATCDFClose(CRYPTCATCDF *pCDF)
961 {
962 FIXME("(%p) stub\n", pCDF);
963
964 return FALSE;
965 }
966
967 /***********************************************************************
968 * CryptCATCDFEnumCatAttributes (WINTRUST.@)
969 */
970 CRYPTCATATTRIBUTE * WINAPI CryptCATCDFEnumCatAttributes(CRYPTCATCDF *pCDF,
971 CRYPTCATATTRIBUTE *pPrevAttr,
972 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
973 {
974 FIXME("(%p %p %p) stub\n", pCDF, pPrevAttr, pfnParseError);
975
976 return NULL;
977 }
978
979 /***********************************************************************
980 * CryptCATCDFEnumMembersByCDFTagEx (WINTRUST.@)
981 */
982 LPWSTR WINAPI CryptCATCDFEnumMembersByCDFTagEx(CRYPTCATCDF *pCDF, LPWSTR pwszPrevCDFTag,
983 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError,
984 CRYPTCATMEMBER **ppMember, BOOL fContinueOnError,
985 LPVOID pvReserved)
986 {
987 FIXME("(%p %s %p %p %d %p) stub\n", pCDF, debugstr_w(pwszPrevCDFTag), pfnParseError,
988 ppMember, fContinueOnError, pvReserved);
989
990 return NULL;
991 }
992
993 /***********************************************************************
994 * CryptCATCDFOpen (WINTRUST.@)
995 */
996 CRYPTCATCDF * WINAPI CryptCATCDFOpen(LPWSTR pwszFilePath,
997 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
998 {
999 FIXME("(%s %p) stub\n", debugstr_w(pwszFilePath), pfnParseError);
1000
1001 return NULL;
1002 }
1003
1004 static BOOL WINTRUST_GetSignedMsgFromPEFile(SIP_SUBJECTINFO *pSubjectInfo,
1005 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1006 BYTE *pbSignedDataMsg)
1007 {
1008 BOOL ret;
1009 WIN_CERTIFICATE *pCert = NULL;
1010 HANDLE file;
1011
1012 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1013 pcbSignedDataMsg, pbSignedDataMsg);
1014
1015 if(pSubjectInfo->hFile && pSubjectInfo->hFile!=INVALID_HANDLE_VALUE)
1016 file = pSubjectInfo->hFile;
1017 else
1018 {
1019 file = CreateFileW(pSubjectInfo->pwsFileName, GENERIC_READ,
1020 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1021 if(file == INVALID_HANDLE_VALUE)
1022 return FALSE;
1023 }
1024
1025 if (!pbSignedDataMsg)
1026 {
1027 WIN_CERTIFICATE cert;
1028
1029 /* app hasn't passed buffer, just get the length */
1030 ret = ImageGetCertificateHeader(file, dwIndex, &cert);
1031 if (ret)
1032 {
1033 switch (cert.wCertificateType)
1034 {
1035 case WIN_CERT_TYPE_X509:
1036 case WIN_CERT_TYPE_PKCS_SIGNED_DATA:
1037 *pcbSignedDataMsg = cert.dwLength;
1038 break;
1039 default:
1040 WARN("unknown certificate type %d\n", cert.wCertificateType);
1041 ret = FALSE;
1042 }
1043 }
1044 }
1045 else
1046 {
1047 DWORD len = 0;
1048
1049 ret = ImageGetCertificateData(file, dwIndex, NULL, &len);
1050 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1051 goto error;
1052 pCert = HeapAlloc(GetProcessHeap(), 0, len);
1053 if (!pCert)
1054 {
1055 ret = FALSE;
1056 goto error;
1057 }
1058 ret = ImageGetCertificateData(file, dwIndex, pCert, &len);
1059 if (!ret)
1060 goto error;
1061 pCert->dwLength -= FIELD_OFFSET(WIN_CERTIFICATE, bCertificate);
1062 if (*pcbSignedDataMsg < pCert->dwLength)
1063 {
1064 *pcbSignedDataMsg = pCert->dwLength;
1065 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1066 ret = FALSE;
1067 }
1068 else
1069 {
1070 memcpy(pbSignedDataMsg, pCert->bCertificate, pCert->dwLength);
1071 *pcbSignedDataMsg = pCert->dwLength;
1072 switch (pCert->wCertificateType)
1073 {
1074 case WIN_CERT_TYPE_X509:
1075 *pdwEncodingType = X509_ASN_ENCODING;
1076 break;
1077 case WIN_CERT_TYPE_PKCS_SIGNED_DATA:
1078 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1079 break;
1080 default:
1081 WARN("don't know what to do for encoding type %d\n",
1082 pCert->wCertificateType);
1083 *pdwEncodingType = 0;
1084 ret = FALSE;
1085 }
1086 }
1087 }
1088 error:
1089 if(pSubjectInfo->hFile != file)
1090 CloseHandle(file);
1091 HeapFree(GetProcessHeap(), 0, pCert);
1092 return ret;
1093 }
1094
1095 static BOOL WINTRUST_PutSignedMsgToPEFile(SIP_SUBJECTINFO* pSubjectInfo, DWORD pdwEncodingType,
1096 DWORD* pdwIndex, DWORD cbSignedDataMsg, BYTE* pbSignedDataMsg)
1097 {
1098 WIN_CERTIFICATE *cert;
1099 HANDLE file;
1100 DWORD size;
1101 BOOL ret;
1102
1103 if(pSubjectInfo->hFile && pSubjectInfo->hFile!=INVALID_HANDLE_VALUE)
1104 file = pSubjectInfo->hFile;
1105 else
1106 {
1107 file = CreateFileW(pSubjectInfo->pwsFileName, GENERIC_READ|GENERIC_WRITE,
1108 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1109 if(file == INVALID_HANDLE_VALUE)
1110 return FALSE;
1111 }
1112
1113 /* int aligned WIN_CERTIFICATE structure with cbSignedDataMsg+1 bytes of data */
1114 size = FIELD_OFFSET(WIN_CERTIFICATE, bCertificate[cbSignedDataMsg+4]) & (~3);
1115 cert = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1116 if(!cert)
1117 return FALSE;
1118
1119 cert->dwLength = size;
1120 cert->wRevision = WIN_CERT_REVISION_2_0;
1121 cert->wCertificateType = WIN_CERT_TYPE_PKCS_SIGNED_DATA;
1122 memcpy(cert->bCertificate, pbSignedDataMsg, cbSignedDataMsg);
1123 ret = ImageAddCertificate(file, cert, pdwIndex);
1124
1125 HeapFree(GetProcessHeap(), 0, cert);
1126 if(file != pSubjectInfo->hFile)
1127 CloseHandle(file);
1128 return ret;
1129 }
1130
1131 /* structure offsets */
1132 #define cfhead_Signature (0x00)
1133 #define cfhead_CabinetSize (0x08)
1134 #define cfhead_MinorVersion (0x18)
1135 #define cfhead_MajorVersion (0x19)
1136 #define cfhead_Flags (0x1E)
1137 #define cfhead_SIZEOF (0x24)
1138 #define cfheadext_HeaderReserved (0x00)
1139 #define cfheadext_SIZEOF (0x04)
1140 #define cfsigninfo_CertOffset (0x04)
1141 #define cfsigninfo_CertSize (0x08)
1142 #define cfsigninfo_SIZEOF (0x0C)
1143
1144 /* flags */
1145 #define cfheadRESERVE_PRESENT (0x0004)
1146
1147 /* endian-neutral reading of little-endian data */
1148 #define EndGetI32(a) ((((a)[3])<<24)|(((a)[2])<<16)|(((a)[1])<<8)|((a)[0]))
1149 #define EndGetI16(a) ((((a)[1])<<8)|((a)[0]))
1150
1151 /* For documentation purposes only: this is the structure in the reserved
1152 * area of a signed cabinet file. The cert offset indicates where in the
1153 * cabinet file the signature resides, and the count indicates its size.
1154 */
1155 typedef struct _CAB_SIGNINFO
1156 {
1157 WORD unk0; /* always 0? */
1158 WORD unk1; /* always 0x0010? */
1159 DWORD dwCertOffset;
1160 DWORD cbCertBlock;
1161 } CAB_SIGNINFO, *PCAB_SIGNINFO;
1162
1163 static BOOL WINTRUST_GetSignedMsgFromCabFile(SIP_SUBJECTINFO *pSubjectInfo,
1164 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1165 BYTE *pbSignedDataMsg)
1166 {
1167 int header_resv;
1168 LONG base_offset, cabsize;
1169 USHORT flags;
1170 BYTE buf[64];
1171 DWORD cert_offset, cert_size, dwRead;
1172
1173 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1174 pcbSignedDataMsg, pbSignedDataMsg);
1175
1176 /* get basic offset & size info */
1177 base_offset = SetFilePointer(pSubjectInfo->hFile, 0L, NULL, SEEK_CUR);
1178
1179 if (SetFilePointer(pSubjectInfo->hFile, 0, NULL, SEEK_END) == INVALID_SET_FILE_POINTER)
1180 {
1181 TRACE("seek error\n");
1182 return FALSE;
1183 }
1184
1185 cabsize = SetFilePointer(pSubjectInfo->hFile, 0L, NULL, SEEK_CUR);
1186 if ((cabsize == -1) || (base_offset == -1) ||
1187 (SetFilePointer(pSubjectInfo->hFile, 0, NULL, SEEK_SET) == INVALID_SET_FILE_POINTER))
1188 {
1189 TRACE("seek error\n");
1190 return FALSE;
1191 }
1192
1193 /* read in the CFHEADER */
1194 if (!ReadFile(pSubjectInfo->hFile, buf, cfhead_SIZEOF, &dwRead, NULL) ||
1195 dwRead != cfhead_SIZEOF)
1196 {
1197 TRACE("reading header failed\n");
1198 return FALSE;
1199 }
1200
1201 /* check basic MSCF signature */
1202 if (EndGetI32(buf+cfhead_Signature) != 0x4643534d)
1203 {
1204 WARN("cabinet signature not present\n");
1205 return FALSE;
1206 }
1207
1208 /* Ignore the number of folders and files and the set and cabinet IDs */
1209
1210 /* check the header revision */
1211 if ((buf[cfhead_MajorVersion] > 1) ||
1212 (buf[cfhead_MajorVersion] == 1 && buf[cfhead_MinorVersion] > 3))
1213 {
1214 WARN("cabinet format version > 1.3\n");
1215 return FALSE;
1216 }
1217
1218 /* pull the flags out */
1219 flags = EndGetI16(buf+cfhead_Flags);
1220
1221 if (!(flags & cfheadRESERVE_PRESENT))
1222 {
1223 TRACE("no header present, not signed\n");
1224 return FALSE;
1225 }
1226
1227 if (!ReadFile(pSubjectInfo->hFile, buf, cfheadext_SIZEOF, &dwRead, NULL) ||
1228 dwRead != cfheadext_SIZEOF)
1229 {
1230 ERR("bunk reserve-sizes?\n");
1231 return FALSE;
1232 }
1233
1234 header_resv = EndGetI16(buf+cfheadext_HeaderReserved);
1235 if (!header_resv)
1236 {
1237 TRACE("no header_resv, not signed\n");
1238 return FALSE;
1239 }
1240 else if (header_resv < cfsigninfo_SIZEOF)
1241 {
1242 TRACE("header_resv too small, not signed\n");
1243 return FALSE;
1244 }
1245
1246 if (header_resv > 60000)
1247 {
1248 WARN("WARNING; header reserved space > 60000\n");
1249 }
1250
1251 if (!ReadFile(pSubjectInfo->hFile, buf, cfsigninfo_SIZEOF, &dwRead, NULL) ||
1252 dwRead != cfsigninfo_SIZEOF)
1253 {
1254 ERR("couldn't read reserve\n");
1255 return FALSE;
1256 }
1257
1258 cert_offset = EndGetI32(buf+cfsigninfo_CertOffset);
1259 TRACE("cert_offset: %d\n", cert_offset);
1260 cert_size = EndGetI32(buf+cfsigninfo_CertSize);
1261 TRACE("cert_size: %d\n", cert_size);
1262
1263 /* The redundant checks are to avoid wraparound */
1264 if (cert_offset > cabsize || cert_size > cabsize ||
1265 cert_offset + cert_size > cabsize)
1266 {
1267 WARN("offset beyond file, not attempting to read\n");
1268 return FALSE;
1269 }
1270
1271 SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET);
1272 if (!pbSignedDataMsg)
1273 {
1274 *pcbSignedDataMsg = cert_size;
1275 return TRUE;
1276 }
1277 if (*pcbSignedDataMsg < cert_size)
1278 {
1279 *pcbSignedDataMsg = cert_size;
1280 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1281 return FALSE;
1282 }
1283 if (SetFilePointer(pSubjectInfo->hFile, cert_offset, NULL, SEEK_SET) == INVALID_SET_FILE_POINTER)
1284 {
1285 ERR("couldn't seek to cert location\n");
1286 return FALSE;
1287 }
1288 if (!ReadFile(pSubjectInfo->hFile, pbSignedDataMsg, cert_size, &dwRead,
1289 NULL) || dwRead != cert_size)
1290 {
1291 ERR("couldn't read cert\n");
1292 SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET);
1293 return FALSE;
1294 }
1295 /* The encoding of the files I've seen appears to be in ASN.1
1296 * format, and there isn't a field indicating the type, so assume it
1297 * always is.
1298 */
1299 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1300 /* Restore base offset */
1301 SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET);
1302 return TRUE;
1303 }
1304
1305 static BOOL WINTRUST_GetSignedMsgFromCatFile(SIP_SUBJECTINFO *pSubjectInfo,
1306 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1307 BYTE *pbSignedDataMsg)
1308 {
1309 BOOL ret;
1310
1311 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1312 pcbSignedDataMsg, pbSignedDataMsg);
1313
1314 if (!pbSignedDataMsg)
1315 {
1316 *pcbSignedDataMsg = GetFileSize(pSubjectInfo->hFile, NULL);
1317 ret = TRUE;
1318 }
1319 else
1320 {
1321 DWORD len = GetFileSize(pSubjectInfo->hFile, NULL);
1322
1323 if (*pcbSignedDataMsg < len)
1324 {
1325 *pcbSignedDataMsg = len;
1326 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1327 ret = FALSE;
1328 }
1329 else
1330 {
1331 ret = ReadFile(pSubjectInfo->hFile, pbSignedDataMsg, len,
1332 pcbSignedDataMsg, NULL);
1333 if (ret)
1334 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1335 }
1336 }
1337 return ret;
1338 }
1339
1340 /* GUIDs used by CryptSIPGetSignedDataMsg and CryptSIPPutSignedDataMsg */
1341 static const GUID unknown = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,
1342 0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
1343 static const GUID cabGUID = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,
1344 0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
1345 static const GUID catGUID = { 0xDE351A43, 0x8E59, 0x11D0, { 0x8C,0x47,
1346 0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
1347
1348 /***********************************************************************
1349 * CryptSIPGetSignedDataMsg (WINTRUST.@)
1350 */
1351 BOOL WINAPI CryptSIPGetSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pdwEncodingType,
1352 DWORD dwIndex, DWORD* pcbSignedDataMsg, BYTE* pbSignedDataMsg)
1353 {
1354 BOOL ret;
1355
1356 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1357 pcbSignedDataMsg, pbSignedDataMsg);
1358
1359 if(!pSubjectInfo)
1360 {
1361 SetLastError(ERROR_INVALID_PARAMETER);
1362 return FALSE;
1363 }
1364
1365 if (!memcmp(pSubjectInfo->pgSubjectType, &unknown, sizeof(unknown)))
1366 ret = WINTRUST_GetSignedMsgFromPEFile(pSubjectInfo, pdwEncodingType,
1367 dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
1368 else if (!memcmp(pSubjectInfo->pgSubjectType, &cabGUID, sizeof(cabGUID)))
1369 ret = WINTRUST_GetSignedMsgFromCabFile(pSubjectInfo, pdwEncodingType,
1370 dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
1371 else if (!memcmp(pSubjectInfo->pgSubjectType, &catGUID, sizeof(catGUID)))
1372 ret = WINTRUST_GetSignedMsgFromCatFile(pSubjectInfo, pdwEncodingType,
1373 dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
1374 else
1375 {
1376 FIXME("unimplemented for subject type %s\n",
1377 debugstr_guid(pSubjectInfo->pgSubjectType));
1378 ret = FALSE;
1379 }
1380
1381 TRACE("returning %d\n", ret);
1382 return ret;
1383 }
1384
1385 /***********************************************************************
1386 * CryptSIPPutSignedDataMsg (WINTRUST.@)
1387 */
1388 BOOL WINAPI CryptSIPPutSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD pdwEncodingType,
1389 DWORD* pdwIndex, DWORD cbSignedDataMsg, BYTE* pbSignedDataMsg)
1390 {
1391 TRACE("(%p %d %p %d %p)\n", pSubjectInfo, pdwEncodingType, pdwIndex,
1392 cbSignedDataMsg, pbSignedDataMsg);
1393
1394 if(!pSubjectInfo) {
1395 SetLastError(ERROR_INVALID_PARAMETER);
1396 return FALSE;
1397 }
1398
1399 if(!memcmp(pSubjectInfo->pgSubjectType, &unknown, sizeof(unknown)))
1400 return WINTRUST_PutSignedMsgToPEFile(pSubjectInfo, pdwEncodingType,
1401 pdwIndex, cbSignedDataMsg, pbSignedDataMsg);
1402 else
1403 FIXME("unimplemented for subject type %s\n",
1404 debugstr_guid(pSubjectInfo->pgSubjectType));
1405
1406 return FALSE;
1407 }
1408
1409 /***********************************************************************
1410 * CryptSIPRemoveSignedDataMsg (WINTRUST.@)
1411 */
1412 BOOL WINAPI CryptSIPRemoveSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo,
1413 DWORD dwIndex)
1414 {
1415 FIXME("(%p %d) stub\n", pSubjectInfo, dwIndex);
1416
1417 return FALSE;
1418 }
1419
1420 /***********************************************************************
1421 * CryptSIPVerifyIndirectData (WINTRUST.@)
1422 */
1423 BOOL WINAPI CryptSIPVerifyIndirectData(SIP_SUBJECTINFO* pSubjectInfo,
1424 SIP_INDIRECT_DATA* pIndirectData)
1425 {
1426 FIXME("(%p %p) stub\n", pSubjectInfo, pIndirectData);
1427
1428 return FALSE;
1429 }