winemine: Make some functions static.
[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     return DeleteFileW(pwszCatalogFile);
475 }
476
477 /***********************************************************************
478  *      CryptCATAdminResolveCatalogPath  (WINTRUST.@)
479  */
480 BOOL WINAPI CryptCATAdminResolveCatalogPath(HCATADMIN hcatadmin, WCHAR *catalog_file,
481                                             CATALOG_INFO *info, DWORD flags)
482 {
483     static const WCHAR slashW[] = {'\\',0};
484     struct catadmin *ca = hcatadmin;
485
486     TRACE("%p %s %p %x\n", hcatadmin, debugstr_w(catalog_file), info, flags);
487
488     if (!ca || ca->magic != CATADMIN_MAGIC || !info || info->cbStruct != sizeof(*info) || flags)
489     {
490         SetLastError(ERROR_INVALID_PARAMETER);
491         return FALSE;
492     }
493     strcpyW(info->wszCatalogFile, ca->path);
494     strcatW(info->wszCatalogFile, slashW);
495     strcatW(info->wszCatalogFile, catalog_file);
496
497     return TRUE;
498 }
499
500 /***********************************************************************
501  *      CryptCATClose  (WINTRUST.@)
502  */
503 BOOL WINAPI CryptCATClose(HANDLE hCatalog)
504 {
505     struct cryptcat *cc = hCatalog;
506
507     TRACE("(%p)\n", hCatalog);
508
509     if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
510     {
511         SetLastError(ERROR_INVALID_PARAMETER);
512         return FALSE;
513     }
514     HeapFree(GetProcessHeap(), 0, cc->attr);
515     HeapFree(GetProcessHeap(), 0, cc->inner);
516     CryptMsgClose(cc->msg);
517
518     cc->magic = 0;
519     HeapFree(GetProcessHeap(), 0, cc);
520     return TRUE;
521 }
522
523 /***********************************************************************
524  *      CryptCATGetAttrInfo  (WINTRUST.@)
525  */
526 CRYPTCATATTRIBUTE * WINAPI CryptCATGetAttrInfo(HANDLE hCatalog, CRYPTCATMEMBER *member, LPWSTR tag)
527 {
528     struct cryptcat *cc = hCatalog;
529
530     FIXME("%p, %p, %s\n", hCatalog, member, debugstr_w(tag));
531
532     if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
533     {
534         SetLastError(ERROR_INVALID_PARAMETER);
535         return NULL;
536     }
537     SetLastError(CRYPT_E_NOT_FOUND);
538     return NULL;
539 }
540
541 /***********************************************************************
542  *      CryptCATGetCatAttrInfo  (WINTRUST.@)
543  */
544 CRYPTCATATTRIBUTE * WINAPI CryptCATGetCatAttrInfo(HANDLE hCatalog, LPWSTR tag)
545 {
546     struct cryptcat *cc = hCatalog;
547
548     FIXME("%p, %s\n", hCatalog, 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 CRYPTCATMEMBER * WINAPI CryptCATGetMemberInfo(HANDLE hCatalog, LPWSTR tag)
560 {
561     struct cryptcat *cc = hCatalog;
562
563     FIXME("%p, %s\n", hCatalog, debugstr_w(tag));
564
565     if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
566     {
567         SetLastError(ERROR_INVALID_PARAMETER);
568         return NULL;
569     }
570     SetLastError(CRYPT_E_NOT_FOUND);
571     return NULL;
572 }
573
574 /***********************************************************************
575  *      CryptCATEnumerateAttr  (WINTRUST.@)
576  */
577 CRYPTCATATTRIBUTE * WINAPI CryptCATEnumerateAttr(HANDLE hCatalog, CRYPTCATMEMBER *member, CRYPTCATATTRIBUTE *prev)
578 {
579     struct cryptcat *cc = hCatalog;
580
581     FIXME("%p, %p, %p\n", hCatalog, member, prev);
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  *      CryptCATEnumerateCatAttr  (WINTRUST.@)
594  */
595 CRYPTCATATTRIBUTE * WINAPI CryptCATEnumerateCatAttr(HANDLE hCatalog, CRYPTCATATTRIBUTE *prev)
596 {
597     struct cryptcat *cc = hCatalog;
598
599     FIXME("%p, %p\n", hCatalog, 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  *      CryptCATEnumerateMember  (WINTRUST.@)
612  */
613 CRYPTCATMEMBER * WINAPI CryptCATEnumerateMember(HANDLE hCatalog, CRYPTCATMEMBER *prev)
614 {
615     struct cryptcat *cc = hCatalog;
616     CRYPTCATMEMBER *member = prev;
617     CTL_ENTRY *entry;
618     DWORD size, i;
619
620     TRACE("%p, %p\n", hCatalog, prev);
621
622     if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
623     {
624         SetLastError(ERROR_INVALID_PARAMETER);
625         return NULL;
626     }
627
628     /* dumping the contents makes me think that dwReserved is the iteration number */
629     if (!member)
630     {
631         if (!(member = HeapAlloc(GetProcessHeap(), 0, sizeof(*member))))
632         {
633             SetLastError(ERROR_OUTOFMEMORY);
634             return NULL;
635         }
636         member->cbStruct = sizeof(*member);
637         member->pwszFileName = member->pwszReferenceTag = NULL;
638         member->dwReserved = 0;
639         member->hReserved = NULL;
640         member->gSubjectType = cc->subject;
641         member->fdwMemberFlags = 0;
642         member->pIndirectData = NULL;
643         member->dwCertVersion = cc->inner->dwVersion;
644     }
645     else member->dwReserved++;
646
647     if (member->dwReserved >= cc->inner->cCTLEntry)
648     {
649         SetLastError(ERROR_INVALID_PARAMETER);
650         goto error;
651     }
652
653     /* list them backwards, like native */
654     entry = &cc->inner->rgCTLEntry[cc->inner->cCTLEntry - member->dwReserved - 1];
655
656     member->sEncodedIndirectData.cbData = member->sEncodedMemberInfo.cbData = 0;
657     member->sEncodedIndirectData.pbData = member->sEncodedMemberInfo.pbData = NULL;
658     HeapFree(GetProcessHeap(), 0, member->pIndirectData);
659     member->pIndirectData = NULL;
660
661     for (i = 0; i < entry->cAttribute; i++)
662     {
663         CRYPT_ATTRIBUTE *attr = entry->rgAttribute + i;
664
665         if (attr->cValue != 1)
666         {
667             ERR("Can't handle attr->cValue of %u\n", attr->cValue);
668             continue;
669         }
670         if (!strcmp(attr->pszObjId, CAT_MEMBERINFO_OBJID))
671         {
672             CAT_MEMBERINFO *mi;
673             BOOL ret;
674
675             member->sEncodedMemberInfo.cbData = attr->rgValue->cbData;
676             member->sEncodedMemberInfo.pbData = attr->rgValue->pbData;
677
678             CryptDecodeObject(cc->encoding, CAT_MEMBERINFO_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, NULL, &size);
679
680             if (!(mi = HeapAlloc(GetProcessHeap(), 0, size)))
681             {
682                 SetLastError(ERROR_OUTOFMEMORY);
683                 goto error;
684             }
685             ret = CryptDecodeObject(cc->encoding, CAT_MEMBERINFO_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, mi, &size);
686             if (ret)
687             {
688                 UNICODE_STRING guid;
689
690                 member->dwCertVersion = mi->dwCertVersion;
691                 RtlInitUnicodeString(&guid, mi->pwszSubjGuid);
692                 if (RtlGUIDFromString(&guid, &member->gSubjectType))
693                 {
694                     HeapFree(GetProcessHeap(), 0, mi);
695                     goto error;
696                 }
697             }
698             HeapFree(GetProcessHeap(), 0, mi);
699             if (!ret) goto error;
700         }
701         else if (!strcmp(attr->pszObjId, SPC_INDIRECT_DATA_OBJID))
702         {
703             /* SPC_INDIRECT_DATA_CONTENT is equal to SIP_INDIRECT_DATA */
704
705             member->sEncodedIndirectData.cbData = attr->rgValue->cbData;
706             member->sEncodedIndirectData.pbData = attr->rgValue->pbData;
707
708             CryptDecodeObject(cc->encoding, SPC_INDIRECT_DATA_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, NULL, &size);
709
710             if (!(member->pIndirectData = HeapAlloc(GetProcessHeap(), 0, size)))
711             {
712                 SetLastError(ERROR_OUTOFMEMORY);
713                 goto error;
714             }
715             CryptDecodeObject(cc->encoding, SPC_INDIRECT_DATA_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, member->pIndirectData, &size);
716         }
717         else
718             /* this object id should probably be handled in CryptCATEnumerateAttr */
719             FIXME("unhandled object id \"%s\"\n", attr->pszObjId);
720     }
721
722     if (!member->sEncodedMemberInfo.cbData || !member->sEncodedIndirectData.cbData)
723     {
724         ERR("Corrupted catalog entry?\n");
725         SetLastError(CRYPT_E_ATTRIBUTES_MISSING);
726         goto error;
727     }
728     size = (2 * member->pIndirectData->Digest.cbData + 1) * sizeof(WCHAR);
729     if (member->pwszReferenceTag)
730         member->pwszReferenceTag = HeapReAlloc(GetProcessHeap(), 0, member->pwszReferenceTag, size);
731     else
732         member->pwszReferenceTag = HeapAlloc(GetProcessHeap(), 0, size);
733
734     if (!member->pwszReferenceTag)
735     {
736         SetLastError(ERROR_OUTOFMEMORY);
737         goto error;
738     }
739     /* FIXME: reference tag is usually the file hash but doesn't have to be */
740     for (i = 0; i < member->pIndirectData->Digest.cbData; i++)
741     {
742         DWORD sub;
743
744         sub = member->pIndirectData->Digest.pbData[i] >> 4;
745         member->pwszReferenceTag[i * 2] = (sub < 10 ? '0' + sub : 'A' + sub - 10);
746         sub = member->pIndirectData->Digest.pbData[i] & 0xf;
747         member->pwszReferenceTag[i * 2 + 1] = (sub < 10 ? '0' + sub : 'A' + sub - 10);
748     }
749     member->pwszReferenceTag[i * 2] = 0;
750     return member;
751
752 error:
753     HeapFree(GetProcessHeap(), 0, member->pIndirectData);
754     HeapFree(GetProcessHeap(), 0, member->pwszReferenceTag);
755     HeapFree(GetProcessHeap(), 0, member);
756     return NULL;
757 }
758
759 static CTL_INFO *decode_inner_content(HANDLE hmsg, DWORD encoding, DWORD *len)
760 {
761     DWORD size;
762     LPSTR oid = NULL;
763     BYTE *buffer = NULL;
764     CTL_INFO *inner = NULL;
765
766     if (!CryptMsgGetParam(hmsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, NULL, &size)) return NULL;
767     if (!(oid = HeapAlloc(GetProcessHeap(), 0, size)))
768     {
769         SetLastError(ERROR_OUTOFMEMORY);
770         return NULL;
771     }
772     if (!CryptMsgGetParam(hmsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, oid, &size)) goto out;
773     if (!CryptMsgGetParam(hmsg, CMSG_CONTENT_PARAM, 0, NULL, &size)) goto out;
774     if (!(buffer = HeapAlloc(GetProcessHeap(), 0, size)))
775     {
776         SetLastError(ERROR_OUTOFMEMORY);
777         goto out;
778     }
779     if (!CryptMsgGetParam(hmsg, CMSG_CONTENT_PARAM, 0, buffer, &size)) goto out;
780     if (!CryptDecodeObject(encoding, oid, buffer, size, 0, NULL, &size)) goto out;
781     if (!(inner = HeapAlloc(GetProcessHeap(), 0, size)))
782     {
783         SetLastError(ERROR_OUTOFMEMORY);
784         goto out;
785     }
786     if (!CryptDecodeObject(encoding, oid, buffer, size, 0, inner, &size)) goto out;
787     *len = size;
788
789 out:
790     HeapFree(GetProcessHeap(), 0, oid);
791     HeapFree(GetProcessHeap(), 0, buffer);
792     return inner;
793 }
794
795 /***********************************************************************
796  *      CryptCATCatalogInfoFromContext  (WINTRUST.@)
797  */
798 BOOL WINAPI CryptCATCatalogInfoFromContext(HCATINFO hcatinfo, CATALOG_INFO *info, DWORD flags)
799 {
800     struct catinfo *ci = hcatinfo;
801
802     TRACE("%p, %p, %x\n", hcatinfo, info, flags);
803
804     if (!hcatinfo || hcatinfo == INVALID_HANDLE_VALUE || ci->magic != CATINFO_MAGIC ||
805         flags || !info || info->cbStruct != sizeof(*info))
806     {
807         SetLastError(ERROR_INVALID_PARAMETER);
808         return FALSE;
809     }
810     strcpyW(info->wszCatalogFile, ci->file);
811     return TRUE;
812 }
813
814 /***********************************************************************
815  *      CryptCATOpen  (WINTRUST.@)
816  */
817 HANDLE WINAPI CryptCATOpen(LPWSTR pwszFileName, DWORD fdwOpenFlags, HCRYPTPROV hProv,
818                            DWORD dwPublicVersion, DWORD dwEncodingType)
819 {
820     HANDLE file, hmsg;
821     BYTE *buffer = NULL;
822     DWORD size, flags = OPEN_EXISTING;
823     struct cryptcat *cc;
824
825     TRACE("%s, %x, %lx, %x, %x\n", debugstr_w(pwszFileName), fdwOpenFlags,
826           hProv, dwPublicVersion, dwEncodingType);
827
828     if (!pwszFileName)
829     {
830         SetLastError(ERROR_INVALID_PARAMETER);
831         return INVALID_HANDLE_VALUE;
832     }
833
834     if (!dwPublicVersion) dwPublicVersion = 0x00000100;
835     if (!dwEncodingType)  dwEncodingType  = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
836
837     if (fdwOpenFlags & CRYPTCAT_OPEN_ALWAYS)    flags |= OPEN_ALWAYS;
838     if (fdwOpenFlags & CRYPTCAT_OPEN_CREATENEW) flags |= CREATE_NEW;
839
840     file = CreateFileW(pwszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, flags, 0, NULL);
841     if (file == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
842
843     size = GetFileSize(file, NULL);
844     if (!(buffer = HeapAlloc(GetProcessHeap(), 0, size)))
845     {
846         CloseHandle(file);
847         SetLastError(ERROR_OUTOFMEMORY);
848         return INVALID_HANDLE_VALUE;
849     }
850     if (!(hmsg = CryptMsgOpenToDecode(dwEncodingType, 0, 0, hProv, NULL, NULL)))
851     {
852         CloseHandle(file);
853         HeapFree(GetProcessHeap(), 0, buffer);
854         return INVALID_HANDLE_VALUE;
855     }
856     if (!ReadFile(file, buffer, size, &size, NULL) || !CryptMsgUpdate(hmsg, buffer, size, TRUE))
857     {
858         CloseHandle(file);
859         HeapFree(GetProcessHeap(), 0, buffer);
860         CryptMsgClose(hmsg);
861         return INVALID_HANDLE_VALUE;
862     }
863     HeapFree(GetProcessHeap(), 0, buffer);
864     CloseHandle(file);
865
866     size = sizeof(DWORD);
867     if (!(cc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cc))))
868     {
869         CryptMsgClose(hmsg);
870         SetLastError(ERROR_OUTOFMEMORY);
871         return INVALID_HANDLE_VALUE;
872     }
873
874     cc->msg = hmsg;
875     cc->encoding = dwEncodingType;
876     if (CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_COUNT_PARAM, 0, &cc->attr_count, &size))
877     {
878         DWORD i, sum = 0;
879         BYTE *p;
880
881         for (i = 0; i < cc->attr_count; i++)
882         {
883             if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size))
884             {
885                 CryptMsgClose(hmsg);
886                 return INVALID_HANDLE_VALUE;
887             }
888             sum += size;
889         }
890         if (!(cc->attr = HeapAlloc(GetProcessHeap(), 0, sizeof(*cc->attr) * cc->attr_count + sum)))
891         {
892             CryptMsgClose(hmsg);
893             SetLastError(ERROR_OUTOFMEMORY);
894             return INVALID_HANDLE_VALUE;
895         }
896         p = (BYTE *)(cc->attr + cc->attr_count);
897         for (i = 0; i < cc->attr_count; i++)
898         {
899             if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size))
900             {
901                 CryptMsgClose(hmsg);
902                 HeapFree(GetProcessHeap(), 0, cc->attr);
903                 return INVALID_HANDLE_VALUE;
904             }
905             if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, p, &size))
906             {
907                 CryptMsgClose(hmsg);
908                 HeapFree(GetProcessHeap(), 0, cc->attr);
909                 return INVALID_HANDLE_VALUE;
910             }
911             p += size;
912         }
913         cc->inner = decode_inner_content(hmsg, dwEncodingType, &cc->inner_len);
914         if (!cc->inner || !CryptSIPRetrieveSubjectGuid(pwszFileName, NULL, &cc->subject))
915         {
916             CryptMsgClose(hmsg);
917             HeapFree(GetProcessHeap(), 0, cc->attr);
918             HeapFree(GetProcessHeap(), 0, cc->inner);
919             HeapFree(GetProcessHeap(), 0, cc);
920             return INVALID_HANDLE_VALUE;
921         }
922         cc->magic = CRYPTCAT_MAGIC;
923         return cc;
924     }
925     return INVALID_HANDLE_VALUE;
926 }
927
928 /***********************************************************************
929  *      CryptSIPCreateIndirectData  (WINTRUST.@)
930  */
931 BOOL WINAPI CryptSIPCreateIndirectData(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pcbIndirectData,
932                                        SIP_INDIRECT_DATA* pIndirectData)
933 {
934     FIXME("(%p %p %p) stub\n", pSubjectInfo, pcbIndirectData, pIndirectData);
935  
936     return FALSE;
937 }
938
939 static BOOL WINTRUST_GetSignedMsgFromPEFile(SIP_SUBJECTINFO *pSubjectInfo,
940  DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
941  BYTE *pbSignedDataMsg)
942 {
943     BOOL ret;
944     WIN_CERTIFICATE *pCert = NULL;
945
946     TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
947           pcbSignedDataMsg, pbSignedDataMsg);
948  
949     if (!pbSignedDataMsg)
950     {
951         WIN_CERTIFICATE cert;
952
953         /* app hasn't passed buffer, just get the length */
954         ret = ImageGetCertificateHeader(pSubjectInfo->hFile, dwIndex, &cert);
955         if (ret)
956             *pcbSignedDataMsg = cert.dwLength;
957     }
958     else
959     {
960         DWORD len = 0;
961
962         ret = ImageGetCertificateData(pSubjectInfo->hFile, dwIndex, NULL, &len);
963         if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
964             goto error;
965         pCert = HeapAlloc(GetProcessHeap(), 0, len);
966         if (!pCert)
967         {
968             ret = FALSE;
969             goto error;
970         }
971         ret = ImageGetCertificateData(pSubjectInfo->hFile, dwIndex, pCert,
972          &len);
973         if (!ret)
974             goto error;
975         if (*pcbSignedDataMsg < pCert->dwLength)
976         {
977             *pcbSignedDataMsg = pCert->dwLength;
978             SetLastError(ERROR_INSUFFICIENT_BUFFER);
979             ret = FALSE;
980         }
981         else
982         {
983             memcpy(pbSignedDataMsg, pCert->bCertificate, pCert->dwLength);
984             switch (pCert->wCertificateType)
985             {
986             case WIN_CERT_TYPE_X509:
987                 *pdwEncodingType = X509_ASN_ENCODING;
988                 break;
989             case WIN_CERT_TYPE_PKCS_SIGNED_DATA:
990                 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
991                 break;
992             default:
993                 FIXME("don't know what to do for encoding type %d\n",
994                  pCert->wCertificateType);
995                 *pdwEncodingType = 0;
996             }
997         }
998     }
999 error:
1000     HeapFree(GetProcessHeap(), 0, pCert);
1001     return ret;
1002 }
1003
1004 /* structure offsets */
1005 #define cfhead_Signature         (0x00)
1006 #define cfhead_CabinetSize       (0x08)
1007 #define cfhead_MinorVersion      (0x18)
1008 #define cfhead_MajorVersion      (0x19)
1009 #define cfhead_Flags             (0x1E)
1010 #define cfhead_SIZEOF            (0x24)
1011 #define cfheadext_HeaderReserved (0x00)
1012 #define cfheadext_SIZEOF         (0x04)
1013 #define cfsigninfo_CertOffset    (0x04)
1014 #define cfsigninfo_CertSize      (0x08)
1015 #define cfsigninfo_SIZEOF        (0x0C)
1016
1017 /* flags */
1018 #define cfheadRESERVE_PRESENT          (0x0004)
1019
1020 /* endian-neutral reading of little-endian data */
1021 #define EndGetI32(a)  ((((a)[3])<<24)|(((a)[2])<<16)|(((a)[1])<<8)|((a)[0]))
1022 #define EndGetI16(a)  ((((a)[1])<<8)|((a)[0]))
1023
1024 /* For documentation purposes only:  this is the structure in the reserved
1025  * area of a signed cabinet file.  The cert offset indicates where in the
1026  * cabinet file the signature resides, and the count indicates its size.
1027  */
1028 typedef struct _CAB_SIGNINFO
1029 {
1030     WORD unk0; /* always 0? */
1031     WORD unk1; /* always 0x0010? */
1032     DWORD dwCertOffset;
1033     DWORD cbCertBlock;
1034 } CAB_SIGNINFO, *PCAB_SIGNINFO;
1035
1036 static BOOL WINTRUST_GetSignedMsgFromCabFile(SIP_SUBJECTINFO *pSubjectInfo,
1037  DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1038  BYTE *pbSignedDataMsg)
1039 {
1040     int header_resv;
1041     LONG base_offset, cabsize;
1042     USHORT flags;
1043     BYTE buf[64];
1044     DWORD cert_offset, cert_size, dwRead;
1045
1046     TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1047           pcbSignedDataMsg, pbSignedDataMsg);
1048
1049     /*
1050      * FIXME: I just noticed that I am memorizing the initial file pointer
1051      * offset and restoring it before reading in the rest of the header
1052      * information in the cabinet.  Perhaps that's correct -- that is, perhaps
1053      * this API is supposed to support "streaming" cabinets which are embedded
1054      * in other files, or cabinets which begin at file offsets other than zero.
1055      * Otherwise, I should instead go to the absolute beginning of the file.
1056      * (Either way, the semantics of wine's FDICopy require me to leave the
1057      * file pointer where it is afterwards -- If Windows does not do so, we
1058      * ought to duplicate the native behavior in the FDIIsCabinet API, not here.
1059      *
1060      * So, the answer lies in Windows; will native cabinet.dll recognize a
1061      * cabinet "file" embedded in another file?  Note that cabextract.c does
1062      * support this, which implies that Microsoft's might.  I haven't tried it
1063      * yet so I don't know.  ATM, most of wine's FDI cabinet routines (except
1064      * this one) would not work in this way.  To fix it, we could just make the
1065      * various references to absolute file positions in the code relative to an
1066      * initial "beginning" offset.  Because the FDICopy API doesn't take a
1067      * file-handle like this one, we would therein need to search through the
1068      * file for the beginning of the cabinet (as we also do in cabextract.c).
1069      * Note that this limits us to a maximum of one cabinet per. file: the first.
1070      *
1071      * So, in summary: either the code below is wrong, or the rest of fdi.c is
1072      * wrong... I cannot imagine that both are correct ;)  One of these flaws
1073      * should be fixed after determining the behavior on Windows.   We ought
1074      * to check both FDIIsCabinet and FDICopy for the right behavior.
1075      *
1076      * -gmt
1077      */
1078
1079     /* get basic offset & size info */
1080     base_offset = SetFilePointer(pSubjectInfo->hFile, 0L, NULL, SEEK_CUR);
1081
1082     if (SetFilePointer(pSubjectInfo->hFile, 0, NULL, SEEK_END) == INVALID_SET_FILE_POINTER)
1083     {
1084         TRACE("seek error\n");
1085         return FALSE;
1086     }
1087
1088     cabsize = SetFilePointer(pSubjectInfo->hFile, 0L, NULL, SEEK_CUR);
1089     if ((cabsize == -1) || (base_offset == -1) ||
1090      (SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET) == INVALID_SET_FILE_POINTER))
1091     {
1092         TRACE("seek error\n");
1093         return FALSE;
1094     }
1095
1096     /* read in the CFHEADER */
1097     if (!ReadFile(pSubjectInfo->hFile, buf, cfhead_SIZEOF, &dwRead, NULL) ||
1098      dwRead != cfhead_SIZEOF)
1099     {
1100         TRACE("reading header failed\n");
1101         return FALSE;
1102     }
1103
1104     /* check basic MSCF signature */
1105     if (EndGetI32(buf+cfhead_Signature) != 0x4643534d)
1106     {
1107         WARN("cabinet signature not present\n");
1108         return FALSE;
1109     }
1110
1111     /* Ignore the number of folders and files and the set and cabinet IDs */
1112
1113     /* check the header revision */
1114     if ((buf[cfhead_MajorVersion] > 1) ||
1115         (buf[cfhead_MajorVersion] == 1 && buf[cfhead_MinorVersion] > 3))
1116     {
1117         WARN("cabinet format version > 1.3\n");
1118         return FALSE;
1119     }
1120
1121     /* pull the flags out */
1122     flags = EndGetI16(buf+cfhead_Flags);
1123
1124     if (!(flags & cfheadRESERVE_PRESENT))
1125     {
1126         TRACE("no header present, not signed\n");
1127         return FALSE;
1128     }
1129
1130     if (!ReadFile(pSubjectInfo->hFile, buf, cfheadext_SIZEOF, &dwRead, NULL) ||
1131      dwRead != cfheadext_SIZEOF)
1132     {
1133         ERR("bunk reserve-sizes?\n");
1134         return FALSE;
1135     }
1136
1137     header_resv = EndGetI16(buf+cfheadext_HeaderReserved);
1138     if (!header_resv)
1139     {
1140         TRACE("no header_resv, not signed\n");
1141         return FALSE;
1142     }
1143     else if (header_resv < cfsigninfo_SIZEOF)
1144     {
1145         TRACE("header_resv too small, not signed\n");
1146         return FALSE;
1147     }
1148
1149     if (header_resv > 60000)
1150     {
1151         WARN("WARNING; header reserved space > 60000\n");
1152     }
1153
1154     if (!ReadFile(pSubjectInfo->hFile, buf, cfsigninfo_SIZEOF, &dwRead, NULL) ||
1155      dwRead != cfsigninfo_SIZEOF)
1156     {
1157         ERR("couldn't read reserve\n");
1158         return FALSE;
1159     }
1160
1161     cert_offset = EndGetI32(buf+cfsigninfo_CertOffset);
1162     TRACE("cert_offset: %d\n", cert_offset);
1163     cert_size = EndGetI32(buf+cfsigninfo_CertSize);
1164     TRACE("cert_size: %d\n", cert_size);
1165
1166     /* The redundant checks are to avoid wraparound */
1167     if (cert_offset > cabsize || cert_size > cabsize ||
1168      cert_offset + cert_size > cabsize)
1169     {
1170         WARN("offset beyond file, not attempting to read\n");
1171         return FALSE;
1172     }
1173
1174     SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET);
1175     if (!pbSignedDataMsg)
1176     {
1177         *pcbSignedDataMsg = cert_size;
1178         return TRUE;
1179     }
1180     if (*pcbSignedDataMsg < cert_size)
1181     {
1182         *pcbSignedDataMsg = cert_size;
1183         SetLastError(ERROR_INSUFFICIENT_BUFFER);
1184         return FALSE;
1185     }
1186     if (SetFilePointer(pSubjectInfo->hFile, cert_offset, NULL, SEEK_SET) == INVALID_SET_FILE_POINTER)
1187     {
1188         ERR("couldn't seek to cert location\n");
1189         return FALSE;
1190     }
1191     if (!ReadFile(pSubjectInfo->hFile, pbSignedDataMsg, cert_size, &dwRead,
1192      NULL) || dwRead != cert_size)
1193     {
1194         ERR("couldn't read cert\n");
1195         return FALSE;
1196     }
1197     /* The encoding of the files I've seen appears to be in ASN.1
1198      * format, and there isn't a field indicating the type, so assume it
1199      * always is.
1200      */
1201     *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1202     return TRUE;
1203 }
1204
1205 static BOOL WINTRUST_GetSignedMsgFromCatFile(SIP_SUBJECTINFO *pSubjectInfo,
1206  DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1207  BYTE *pbSignedDataMsg)
1208 {
1209     BOOL ret;
1210
1211     TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1212           pcbSignedDataMsg, pbSignedDataMsg);
1213
1214     if (!pbSignedDataMsg)
1215     {
1216         *pcbSignedDataMsg = GetFileSize(pSubjectInfo->hFile, NULL);
1217          ret = TRUE;
1218     }
1219     else
1220     {
1221         DWORD len = GetFileSize(pSubjectInfo->hFile, NULL);
1222
1223         if (*pcbSignedDataMsg < len)
1224         {
1225             *pcbSignedDataMsg = len;
1226             SetLastError(ERROR_INSUFFICIENT_BUFFER);
1227             ret = FALSE;
1228         }
1229         else
1230         {
1231             ret = ReadFile(pSubjectInfo->hFile, pbSignedDataMsg, len,
1232              pcbSignedDataMsg, NULL);
1233             if (ret)
1234                 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1235         }
1236     }
1237     return ret;
1238 }
1239
1240 /***********************************************************************
1241  *      CryptSIPGetSignedDataMsg  (WINTRUST.@)
1242  */
1243 BOOL WINAPI CryptSIPGetSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pdwEncodingType,
1244                                        DWORD dwIndex, DWORD* pcbSignedDataMsg, BYTE* pbSignedDataMsg)
1245 {
1246     static const GUID unknown = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,
1247      0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
1248     static const GUID cabGUID = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,
1249      0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
1250     static const GUID catGUID = { 0xDE351A43, 0x8E59, 0x11D0, { 0x8C,0x47,
1251      0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
1252     BOOL ret;
1253
1254     TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1255           pcbSignedDataMsg, pbSignedDataMsg);
1256
1257     if (!memcmp(pSubjectInfo->pgSubjectType, &unknown, sizeof(unknown)))
1258         ret = WINTRUST_GetSignedMsgFromPEFile(pSubjectInfo, pdwEncodingType,
1259          dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
1260     else if (!memcmp(pSubjectInfo->pgSubjectType, &cabGUID, sizeof(cabGUID)))
1261         ret = WINTRUST_GetSignedMsgFromCabFile(pSubjectInfo, pdwEncodingType,
1262          dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
1263     else if (!memcmp(pSubjectInfo->pgSubjectType, &catGUID, sizeof(catGUID)))
1264         ret = WINTRUST_GetSignedMsgFromCatFile(pSubjectInfo, pdwEncodingType,
1265          dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
1266     else
1267     {
1268         FIXME("unimplemented for subject type %s\n",
1269          debugstr_guid(pSubjectInfo->pgSubjectType));
1270         ret = FALSE;
1271     }
1272
1273     TRACE("returning %d\n", ret);
1274     return ret;
1275 }
1276
1277 /***********************************************************************
1278  *      CryptSIPPutSignedDataMsg  (WINTRUST.@)
1279  */
1280 BOOL WINAPI CryptSIPPutSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD pdwEncodingType,
1281                                        DWORD* pdwIndex, DWORD cbSignedDataMsg, BYTE* pbSignedDataMsg)
1282 {
1283     FIXME("(%p %d %p %d %p) stub\n", pSubjectInfo, pdwEncodingType, pdwIndex,
1284           cbSignedDataMsg, pbSignedDataMsg);
1285  
1286     return FALSE;
1287 }
1288
1289 /***********************************************************************
1290  *      CryptSIPRemoveSignedDataMsg  (WINTRUST.@)
1291  */
1292 BOOL WINAPI CryptSIPRemoveSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo,
1293                                        DWORD dwIndex)
1294 {
1295     FIXME("(%p %d) stub\n", pSubjectInfo, dwIndex);
1296  
1297     return FALSE;
1298 }
1299
1300 /***********************************************************************
1301  *      CryptSIPVerifyIndirectData  (WINTRUST.@)
1302  */
1303 BOOL WINAPI CryptSIPVerifyIndirectData(SIP_SUBJECTINFO* pSubjectInfo,
1304                                        SIP_INDIRECT_DATA* pIndirectData)
1305 {
1306     FIXME("(%p %p) stub\n", pSubjectInfo, pIndirectData);
1307  
1308     return FALSE;
1309 }