Sync with trunk r63743.
[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 || dwFlags)
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 HeapFree(GetProcessHeap(), 0, cc);
904 return INVALID_HANDLE_VALUE;
905 }
906 sum += size;
907 }
908 if (!(cc->attr = HeapAlloc(GetProcessHeap(), 0, sizeof(*cc->attr) * cc->attr_count + sum)))
909 {
910 CryptMsgClose(hmsg);
911 HeapFree(GetProcessHeap(), 0, cc);
912 SetLastError(ERROR_OUTOFMEMORY);
913 return INVALID_HANDLE_VALUE;
914 }
915 p = (BYTE *)(cc->attr + cc->attr_count);
916 for (i = 0; i < cc->attr_count; i++)
917 {
918 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size))
919 {
920 CryptMsgClose(hmsg);
921 HeapFree(GetProcessHeap(), 0, cc->attr);
922 HeapFree(GetProcessHeap(), 0, cc);
923 return INVALID_HANDLE_VALUE;
924 }
925 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, p, &size))
926 {
927 CryptMsgClose(hmsg);
928 HeapFree(GetProcessHeap(), 0, cc->attr);
929 HeapFree(GetProcessHeap(), 0, cc);
930 return INVALID_HANDLE_VALUE;
931 }
932 p += size;
933 }
934 cc->inner = decode_inner_content(hmsg, dwEncodingType, &cc->inner_len);
935 if (!cc->inner || !CryptSIPRetrieveSubjectGuid(pwszFileName, NULL, &cc->subject))
936 {
937 CryptMsgClose(hmsg);
938 HeapFree(GetProcessHeap(), 0, cc->attr);
939 HeapFree(GetProcessHeap(), 0, cc->inner);
940 HeapFree(GetProcessHeap(), 0, cc);
941 return INVALID_HANDLE_VALUE;
942 }
943 cc->magic = CRYPTCAT_MAGIC;
944 return cc;
945 }
946 HeapFree(GetProcessHeap(), 0, cc);
947 return INVALID_HANDLE_VALUE;
948 }
949
950 /***********************************************************************
951 * CryptSIPCreateIndirectData (WINTRUST.@)
952 */
953 BOOL WINAPI CryptSIPCreateIndirectData(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pcbIndirectData,
954 SIP_INDIRECT_DATA* pIndirectData)
955 {
956 FIXME("(%p %p %p) stub\n", pSubjectInfo, pcbIndirectData, pIndirectData);
957
958 return FALSE;
959 }
960
961
962 /***********************************************************************
963 * CryptCATCDFClose (WINTRUST.@)
964 */
965 BOOL WINAPI CryptCATCDFClose(CRYPTCATCDF *pCDF)
966 {
967 FIXME("(%p) stub\n", pCDF);
968
969 return FALSE;
970 }
971
972 /***********************************************************************
973 * CryptCATCDFEnumCatAttributes (WINTRUST.@)
974 */
975 CRYPTCATATTRIBUTE * WINAPI CryptCATCDFEnumCatAttributes(CRYPTCATCDF *pCDF,
976 CRYPTCATATTRIBUTE *pPrevAttr,
977 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
978 {
979 FIXME("(%p %p %p) stub\n", pCDF, pPrevAttr, pfnParseError);
980
981 return NULL;
982 }
983
984 /***********************************************************************
985 * CryptCATCDFEnumMembersByCDFTagEx (WINTRUST.@)
986 */
987 LPWSTR WINAPI CryptCATCDFEnumMembersByCDFTagEx(CRYPTCATCDF *pCDF, LPWSTR pwszPrevCDFTag,
988 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError,
989 CRYPTCATMEMBER **ppMember, BOOL fContinueOnError,
990 LPVOID pvReserved)
991 {
992 FIXME("(%p %s %p %p %d %p) stub\n", pCDF, debugstr_w(pwszPrevCDFTag), pfnParseError,
993 ppMember, fContinueOnError, pvReserved);
994
995 return NULL;
996 }
997
998 /***********************************************************************
999 * CryptCATCDFOpen (WINTRUST.@)
1000 */
1001 CRYPTCATCDF * WINAPI CryptCATCDFOpen(LPWSTR pwszFilePath,
1002 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
1003 {
1004 FIXME("(%s %p) stub\n", debugstr_w(pwszFilePath), pfnParseError);
1005
1006 return NULL;
1007 }
1008
1009 static BOOL WINTRUST_GetSignedMsgFromPEFile(SIP_SUBJECTINFO *pSubjectInfo,
1010 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1011 BYTE *pbSignedDataMsg)
1012 {
1013 BOOL ret;
1014 WIN_CERTIFICATE *pCert = NULL;
1015 HANDLE file;
1016
1017 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1018 pcbSignedDataMsg, pbSignedDataMsg);
1019
1020 if(pSubjectInfo->hFile && pSubjectInfo->hFile!=INVALID_HANDLE_VALUE)
1021 file = pSubjectInfo->hFile;
1022 else
1023 {
1024 file = CreateFileW(pSubjectInfo->pwsFileName, GENERIC_READ,
1025 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1026 if(file == INVALID_HANDLE_VALUE)
1027 return FALSE;
1028 }
1029
1030 if (!pbSignedDataMsg)
1031 {
1032 WIN_CERTIFICATE cert;
1033
1034 /* app hasn't passed buffer, just get the length */
1035 ret = ImageGetCertificateHeader(file, dwIndex, &cert);
1036 if (ret)
1037 {
1038 switch (cert.wCertificateType)
1039 {
1040 case WIN_CERT_TYPE_X509:
1041 case WIN_CERT_TYPE_PKCS_SIGNED_DATA:
1042 *pcbSignedDataMsg = cert.dwLength;
1043 break;
1044 default:
1045 WARN("unknown certificate type %d\n", cert.wCertificateType);
1046 ret = FALSE;
1047 }
1048 }
1049 }
1050 else
1051 {
1052 DWORD len = 0;
1053
1054 ret = ImageGetCertificateData(file, dwIndex, NULL, &len);
1055 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1056 goto error;
1057 pCert = HeapAlloc(GetProcessHeap(), 0, len);
1058 if (!pCert)
1059 {
1060 ret = FALSE;
1061 goto error;
1062 }
1063 ret = ImageGetCertificateData(file, dwIndex, pCert, &len);
1064 if (!ret)
1065 goto error;
1066 pCert->dwLength -= FIELD_OFFSET(WIN_CERTIFICATE, bCertificate);
1067 if (*pcbSignedDataMsg < pCert->dwLength)
1068 {
1069 *pcbSignedDataMsg = pCert->dwLength;
1070 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1071 ret = FALSE;
1072 }
1073 else
1074 {
1075 memcpy(pbSignedDataMsg, pCert->bCertificate, pCert->dwLength);
1076 *pcbSignedDataMsg = pCert->dwLength;
1077 switch (pCert->wCertificateType)
1078 {
1079 case WIN_CERT_TYPE_X509:
1080 *pdwEncodingType = X509_ASN_ENCODING;
1081 break;
1082 case WIN_CERT_TYPE_PKCS_SIGNED_DATA:
1083 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1084 break;
1085 default:
1086 WARN("don't know what to do for encoding type %d\n",
1087 pCert->wCertificateType);
1088 *pdwEncodingType = 0;
1089 ret = FALSE;
1090 }
1091 }
1092 }
1093 error:
1094 if(pSubjectInfo->hFile != file)
1095 CloseHandle(file);
1096 HeapFree(GetProcessHeap(), 0, pCert);
1097 return ret;
1098 }
1099
1100 static BOOL WINTRUST_PutSignedMsgToPEFile(SIP_SUBJECTINFO* pSubjectInfo, DWORD pdwEncodingType,
1101 DWORD* pdwIndex, DWORD cbSignedDataMsg, BYTE* pbSignedDataMsg)
1102 {
1103 WIN_CERTIFICATE *cert;
1104 HANDLE file;
1105 DWORD size;
1106 BOOL ret;
1107
1108 if(pSubjectInfo->hFile && pSubjectInfo->hFile!=INVALID_HANDLE_VALUE)
1109 file = pSubjectInfo->hFile;
1110 else
1111 {
1112 file = CreateFileW(pSubjectInfo->pwsFileName, GENERIC_READ|GENERIC_WRITE,
1113 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1114 if(file == INVALID_HANDLE_VALUE)
1115 return FALSE;
1116 }
1117
1118 /* int aligned WIN_CERTIFICATE structure with cbSignedDataMsg+1 bytes of data */
1119 size = FIELD_OFFSET(WIN_CERTIFICATE, bCertificate[cbSignedDataMsg+4]) & (~3);
1120 cert = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1121 if(!cert)
1122 return FALSE;
1123
1124 cert->dwLength = size;
1125 cert->wRevision = WIN_CERT_REVISION_2_0;
1126 cert->wCertificateType = WIN_CERT_TYPE_PKCS_SIGNED_DATA;
1127 memcpy(cert->bCertificate, pbSignedDataMsg, cbSignedDataMsg);
1128 ret = ImageAddCertificate(file, cert, pdwIndex);
1129
1130 HeapFree(GetProcessHeap(), 0, cert);
1131 if(file != pSubjectInfo->hFile)
1132 CloseHandle(file);
1133 return ret;
1134 }
1135
1136 /* structure offsets */
1137 #define cfhead_Signature (0x00)
1138 #define cfhead_CabinetSize (0x08)
1139 #define cfhead_MinorVersion (0x18)
1140 #define cfhead_MajorVersion (0x19)
1141 #define cfhead_Flags (0x1E)
1142 #define cfhead_SIZEOF (0x24)
1143 #define cfheadext_HeaderReserved (0x00)
1144 #define cfheadext_SIZEOF (0x04)
1145 #define cfsigninfo_CertOffset (0x04)
1146 #define cfsigninfo_CertSize (0x08)
1147 #define cfsigninfo_SIZEOF (0x0C)
1148
1149 /* flags */
1150 #define cfheadRESERVE_PRESENT (0x0004)
1151
1152 /* endian-neutral reading of little-endian data */
1153 #define EndGetI32(a) ((((a)[3])<<24)|(((a)[2])<<16)|(((a)[1])<<8)|((a)[0]))
1154 #define EndGetI16(a) ((((a)[1])<<8)|((a)[0]))
1155
1156 /* For documentation purposes only: this is the structure in the reserved
1157 * area of a signed cabinet file. The cert offset indicates where in the
1158 * cabinet file the signature resides, and the count indicates its size.
1159 */
1160 typedef struct _CAB_SIGNINFO
1161 {
1162 WORD unk0; /* always 0? */
1163 WORD unk1; /* always 0x0010? */
1164 DWORD dwCertOffset;
1165 DWORD cbCertBlock;
1166 } CAB_SIGNINFO, *PCAB_SIGNINFO;
1167
1168 static BOOL WINTRUST_GetSignedMsgFromCabFile(SIP_SUBJECTINFO *pSubjectInfo,
1169 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1170 BYTE *pbSignedDataMsg)
1171 {
1172 int header_resv;
1173 LONG base_offset, cabsize;
1174 USHORT flags;
1175 BYTE buf[64];
1176 DWORD cert_offset, cert_size, dwRead;
1177
1178 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1179 pcbSignedDataMsg, pbSignedDataMsg);
1180
1181 /* get basic offset & size info */
1182 base_offset = SetFilePointer(pSubjectInfo->hFile, 0L, NULL, SEEK_CUR);
1183
1184 if (SetFilePointer(pSubjectInfo->hFile, 0, NULL, SEEK_END) == INVALID_SET_FILE_POINTER)
1185 {
1186 TRACE("seek error\n");
1187 return FALSE;
1188 }
1189
1190 cabsize = SetFilePointer(pSubjectInfo->hFile, 0L, NULL, SEEK_CUR);
1191 if ((cabsize == -1) || (base_offset == -1) ||
1192 (SetFilePointer(pSubjectInfo->hFile, 0, NULL, SEEK_SET) == INVALID_SET_FILE_POINTER))
1193 {
1194 TRACE("seek error\n");
1195 return FALSE;
1196 }
1197
1198 /* read in the CFHEADER */
1199 if (!ReadFile(pSubjectInfo->hFile, buf, cfhead_SIZEOF, &dwRead, NULL) ||
1200 dwRead != cfhead_SIZEOF)
1201 {
1202 TRACE("reading header failed\n");
1203 return FALSE;
1204 }
1205
1206 /* check basic MSCF signature */
1207 if (EndGetI32(buf+cfhead_Signature) != 0x4643534d)
1208 {
1209 WARN("cabinet signature not present\n");
1210 return FALSE;
1211 }
1212
1213 /* Ignore the number of folders and files and the set and cabinet IDs */
1214
1215 /* check the header revision */
1216 if ((buf[cfhead_MajorVersion] > 1) ||
1217 (buf[cfhead_MajorVersion] == 1 && buf[cfhead_MinorVersion] > 3))
1218 {
1219 WARN("cabinet format version > 1.3\n");
1220 return FALSE;
1221 }
1222
1223 /* pull the flags out */
1224 flags = EndGetI16(buf+cfhead_Flags);
1225
1226 if (!(flags & cfheadRESERVE_PRESENT))
1227 {
1228 TRACE("no header present, not signed\n");
1229 return FALSE;
1230 }
1231
1232 if (!ReadFile(pSubjectInfo->hFile, buf, cfheadext_SIZEOF, &dwRead, NULL) ||
1233 dwRead != cfheadext_SIZEOF)
1234 {
1235 ERR("bunk reserve-sizes?\n");
1236 return FALSE;
1237 }
1238
1239 header_resv = EndGetI16(buf+cfheadext_HeaderReserved);
1240 if (!header_resv)
1241 {
1242 TRACE("no header_resv, not signed\n");
1243 return FALSE;
1244 }
1245 else if (header_resv < cfsigninfo_SIZEOF)
1246 {
1247 TRACE("header_resv too small, not signed\n");
1248 return FALSE;
1249 }
1250
1251 if (header_resv > 60000)
1252 {
1253 WARN("WARNING; header reserved space > 60000\n");
1254 }
1255
1256 if (!ReadFile(pSubjectInfo->hFile, buf, cfsigninfo_SIZEOF, &dwRead, NULL) ||
1257 dwRead != cfsigninfo_SIZEOF)
1258 {
1259 ERR("couldn't read reserve\n");
1260 return FALSE;
1261 }
1262
1263 cert_offset = EndGetI32(buf+cfsigninfo_CertOffset);
1264 TRACE("cert_offset: %d\n", cert_offset);
1265 cert_size = EndGetI32(buf+cfsigninfo_CertSize);
1266 TRACE("cert_size: %d\n", cert_size);
1267
1268 /* The redundant checks are to avoid wraparound */
1269 if (cert_offset > cabsize || cert_size > cabsize ||
1270 cert_offset + cert_size > cabsize)
1271 {
1272 WARN("offset beyond file, not attempting to read\n");
1273 return FALSE;
1274 }
1275
1276 SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET);
1277 if (!pbSignedDataMsg)
1278 {
1279 *pcbSignedDataMsg = cert_size;
1280 return TRUE;
1281 }
1282 if (*pcbSignedDataMsg < cert_size)
1283 {
1284 *pcbSignedDataMsg = cert_size;
1285 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1286 return FALSE;
1287 }
1288 if (SetFilePointer(pSubjectInfo->hFile, cert_offset, NULL, SEEK_SET) == INVALID_SET_FILE_POINTER)
1289 {
1290 ERR("couldn't seek to cert location\n");
1291 return FALSE;
1292 }
1293 if (!ReadFile(pSubjectInfo->hFile, pbSignedDataMsg, cert_size, &dwRead,
1294 NULL) || dwRead != cert_size)
1295 {
1296 ERR("couldn't read cert\n");
1297 SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET);
1298 return FALSE;
1299 }
1300 /* The encoding of the files I've seen appears to be in ASN.1
1301 * format, and there isn't a field indicating the type, so assume it
1302 * always is.
1303 */
1304 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1305 /* Restore base offset */
1306 SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET);
1307 return TRUE;
1308 }
1309
1310 static BOOL WINTRUST_GetSignedMsgFromCatFile(SIP_SUBJECTINFO *pSubjectInfo,
1311 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1312 BYTE *pbSignedDataMsg)
1313 {
1314 BOOL ret;
1315
1316 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1317 pcbSignedDataMsg, pbSignedDataMsg);
1318
1319 if (!pbSignedDataMsg)
1320 {
1321 *pcbSignedDataMsg = GetFileSize(pSubjectInfo->hFile, NULL);
1322 ret = TRUE;
1323 }
1324 else
1325 {
1326 DWORD len = GetFileSize(pSubjectInfo->hFile, NULL);
1327
1328 if (*pcbSignedDataMsg < len)
1329 {
1330 *pcbSignedDataMsg = len;
1331 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1332 ret = FALSE;
1333 }
1334 else
1335 {
1336 ret = ReadFile(pSubjectInfo->hFile, pbSignedDataMsg, len,
1337 pcbSignedDataMsg, NULL);
1338 if (ret)
1339 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1340 }
1341 }
1342 return ret;
1343 }
1344
1345 /* GUIDs used by CryptSIPGetSignedDataMsg and CryptSIPPutSignedDataMsg */
1346 static const GUID unknown = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,
1347 0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
1348 static const GUID cabGUID = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,
1349 0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
1350 static const GUID catGUID = { 0xDE351A43, 0x8E59, 0x11D0, { 0x8C,0x47,
1351 0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
1352
1353 /***********************************************************************
1354 * CryptSIPGetSignedDataMsg (WINTRUST.@)
1355 */
1356 BOOL WINAPI CryptSIPGetSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pdwEncodingType,
1357 DWORD dwIndex, DWORD* pcbSignedDataMsg, BYTE* pbSignedDataMsg)
1358 {
1359 BOOL ret;
1360
1361 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1362 pcbSignedDataMsg, pbSignedDataMsg);
1363
1364 if(!pSubjectInfo)
1365 {
1366 SetLastError(ERROR_INVALID_PARAMETER);
1367 return FALSE;
1368 }
1369
1370 if (!memcmp(pSubjectInfo->pgSubjectType, &unknown, sizeof(unknown)))
1371 ret = WINTRUST_GetSignedMsgFromPEFile(pSubjectInfo, pdwEncodingType,
1372 dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
1373 else if (!memcmp(pSubjectInfo->pgSubjectType, &cabGUID, sizeof(cabGUID)))
1374 ret = WINTRUST_GetSignedMsgFromCabFile(pSubjectInfo, pdwEncodingType,
1375 dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
1376 else if (!memcmp(pSubjectInfo->pgSubjectType, &catGUID, sizeof(catGUID)))
1377 ret = WINTRUST_GetSignedMsgFromCatFile(pSubjectInfo, pdwEncodingType,
1378 dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
1379 else
1380 {
1381 FIXME("unimplemented for subject type %s\n",
1382 debugstr_guid(pSubjectInfo->pgSubjectType));
1383 ret = FALSE;
1384 }
1385
1386 TRACE("returning %d\n", ret);
1387 return ret;
1388 }
1389
1390 /***********************************************************************
1391 * CryptSIPPutSignedDataMsg (WINTRUST.@)
1392 */
1393 BOOL WINAPI CryptSIPPutSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD pdwEncodingType,
1394 DWORD* pdwIndex, DWORD cbSignedDataMsg, BYTE* pbSignedDataMsg)
1395 {
1396 TRACE("(%p %d %p %d %p)\n", pSubjectInfo, pdwEncodingType, pdwIndex,
1397 cbSignedDataMsg, pbSignedDataMsg);
1398
1399 if(!pSubjectInfo) {
1400 SetLastError(ERROR_INVALID_PARAMETER);
1401 return FALSE;
1402 }
1403
1404 if(!memcmp(pSubjectInfo->pgSubjectType, &unknown, sizeof(unknown)))
1405 return WINTRUST_PutSignedMsgToPEFile(pSubjectInfo, pdwEncodingType,
1406 pdwIndex, cbSignedDataMsg, pbSignedDataMsg);
1407 else
1408 FIXME("unimplemented for subject type %s\n",
1409 debugstr_guid(pSubjectInfo->pgSubjectType));
1410
1411 return FALSE;
1412 }
1413
1414 /***********************************************************************
1415 * CryptSIPRemoveSignedDataMsg (WINTRUST.@)
1416 */
1417 BOOL WINAPI CryptSIPRemoveSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo,
1418 DWORD dwIndex)
1419 {
1420 FIXME("(%p %d) stub\n", pSubjectInfo, dwIndex);
1421
1422 return FALSE;
1423 }
1424
1425 /***********************************************************************
1426 * CryptSIPVerifyIndirectData (WINTRUST.@)
1427 */
1428 BOOL WINAPI CryptSIPVerifyIndirectData(SIP_SUBJECTINFO* pSubjectInfo,
1429 SIP_INDIRECT_DATA* pIndirectData)
1430 {
1431 FIXME("(%p %p) stub\n", pSubjectInfo, pIndirectData);
1432
1433 return FALSE;
1434 }