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