2 * WinTrust Cryptography functions
4 * Copyright 2006 James Hawkins
5 * Copyright 2000-2002 Stuart Caie
6 * Copyright 2002 Patrik Stridvall
7 * Copyright 2003 Greg Turner
8 * Copyright 2008 Juan Lang
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(wintrust);
40 #define CATADMIN_MAGIC 0x43415441 /* 'CATA' */
41 #define CRYPTCAT_MAGIC 0x43415443 /* 'CATC' */
42 #define CATINFO_MAGIC 0x43415449 /* 'CATI' */
53 CRYPTCATATTRIBUTE *attr;
69 static HCATINFO create_catinfo(const WCHAR *filename)
73 if (!(ci = HeapAlloc(GetProcessHeap(), 0, sizeof(*ci))))
75 SetLastError(ERROR_OUTOFMEMORY);
76 return INVALID_HANDLE_VALUE;
78 strcpyW(ci->file, filename);
79 ci->magic = CATINFO_MAGIC;
83 /***********************************************************************
84 * CryptCATAdminAcquireContext (WINTRUST.@)
86 * Get a catalog administrator context handle.
89 * catAdmin [O] Pointer to the context handle.
90 * sys [I] Pointer to a GUID for the needed subsystem.
91 * dwFlags [I] Reserved.
94 * Success: TRUE. catAdmin contains the context handle.
98 BOOL WINAPI CryptCATAdminAcquireContext(HCATADMIN *catAdmin,
99 const GUID *sys, DWORD dwFlags)
101 static const WCHAR catroot[] =
102 {'\\','c','a','t','r','o','o','t',0};
103 static const WCHAR fmt[] =
104 {'%','s','\\','{','%','0','8','x','-','%','0','4','x','-','%','0',
105 '4','x','-','%','0','2','x','%','0','2','x','-','%','0','2','x',
106 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x',
107 '%','0','2','x','}',0};
108 static const GUID defsys =
109 {0x127d0a1d,0x4ef2,0x11d1,{0x86,0x08,0x00,0xc0,0x4f,0xc2,0x95,0xee}};
111 WCHAR catroot_dir[MAX_PATH];
114 TRACE("%p %s %x\n", catAdmin, debugstr_guid(sys), dwFlags);
118 SetLastError(ERROR_INVALID_PARAMETER);
121 if (!(ca = HeapAlloc(GetProcessHeap(), 0, sizeof(*ca))))
123 SetLastError(ERROR_OUTOFMEMORY);
127 GetSystemDirectoryW(catroot_dir, MAX_PATH);
128 strcatW(catroot_dir, catroot);
130 /* create the directory if it doesn't exist */
131 CreateDirectoryW(catroot_dir, NULL);
133 if (!sys) sys = &defsys;
134 sprintfW(ca->path, fmt, catroot_dir, sys->Data1, sys->Data2,
135 sys->Data3, sys->Data4[0], sys->Data4[1], sys->Data4[2],
136 sys->Data4[3], sys->Data4[4], sys->Data4[5], sys->Data4[6],
139 /* create the directory if it doesn't exist */
140 CreateDirectoryW(ca->path, NULL);
142 ca->magic = CATADMIN_MAGIC;
143 ca->find = INVALID_HANDLE_VALUE;
149 /***********************************************************************
150 * CryptCATAdminAddCatalog (WINTRUST.@)
152 HCATINFO WINAPI CryptCATAdminAddCatalog(HCATADMIN catAdmin, PWSTR catalogFile,
153 PWSTR selectBaseName, DWORD flags)
155 static const WCHAR slashW[] = {'\\',0};
156 struct catadmin *ca = catAdmin;
161 TRACE("%p %s %s %d\n", catAdmin, debugstr_w(catalogFile),
162 debugstr_w(selectBaseName), flags);
166 FIXME("NULL basename not handled\n");
167 SetLastError(ERROR_INVALID_PARAMETER);
170 if (!ca || ca->magic != CATADMIN_MAGIC || !catalogFile || flags)
172 SetLastError(ERROR_INVALID_PARAMETER);
176 len = strlenW(ca->path) + strlenW(selectBaseName) + 2;
177 if (!(target = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
179 SetLastError(ERROR_OUTOFMEMORY);
182 strcpyW(target, ca->path);
183 strcatW(target, slashW);
184 strcatW(target, selectBaseName);
186 if (!CopyFileW(catalogFile, target, FALSE))
188 HeapFree(GetProcessHeap(), 0, target);
191 if (!(ci = HeapAlloc(GetProcessHeap(), 0, sizeof(*ci))))
193 HeapFree(GetProcessHeap(), 0, target);
194 SetLastError(ERROR_OUTOFMEMORY);
197 ci->magic = CATINFO_MAGIC;
198 strcpyW(ci->file, target);
200 HeapFree(GetProcessHeap(), 0, target);
204 /***********************************************************************
205 * CryptCATAdminCalcHashFromFileHandle (WINTRUST.@)
207 BOOL WINAPI CryptCATAdminCalcHashFromFileHandle(HANDLE hFile, DWORD* pcbHash,
208 BYTE* pbHash, DWORD dwFlags )
212 TRACE("%p %p %p %x\n", hFile, pcbHash, pbHash, dwFlags);
214 if (!hFile || !pcbHash || dwFlags)
216 SetLastError(ERROR_INVALID_PARAMETER);
222 SetLastError(ERROR_INSUFFICIENT_BUFFER);
234 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, 4096)))
236 SetLastError(ERROR_OUTOFMEMORY);
239 ret = CryptAcquireContextW(&prov, NULL, MS_DEF_PROV_W, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
242 HeapFree(GetProcessHeap(), 0, buffer);
245 ret = CryptCreateHash(prov, CALG_SHA1, 0, 0, &hash);
248 HeapFree(GetProcessHeap(), 0, buffer);
249 CryptReleaseContext(prov, 0);
252 while ((ret = ReadFile(hFile, buffer, 4096, &bytes_read, NULL)) && bytes_read)
254 CryptHashData(hash, buffer, bytes_read, 0);
256 if (ret) ret = CryptGetHashParam(hash, HP_HASHVAL, pbHash, pcbHash, 0);
258 HeapFree(GetProcessHeap(), 0, buffer);
259 CryptDestroyHash(hash);
260 CryptReleaseContext(prov, 0);
265 /***********************************************************************
266 * CryptCATAdminEnumCatalogFromHash (WINTRUST.@)
268 HCATINFO WINAPI CryptCATAdminEnumCatalogFromHash(HCATADMIN hCatAdmin, BYTE* pbHash,
269 DWORD cbHash, DWORD dwFlags,
270 HCATINFO* phPrevCatInfo )
272 static const WCHAR slashW[] = {'\\',0};
273 static const WCHAR globW[] = {'\\','*','.','c','a','t',0};
275 struct catadmin *ca = hCatAdmin;
276 WIN32_FIND_DATAW data;
277 HCATINFO prev = NULL;
282 TRACE("%p %p %d %x %p\n", hCatAdmin, pbHash, cbHash, dwFlags, phPrevCatInfo);
284 if (!ca || ca->magic != CATADMIN_MAGIC || !pbHash || cbHash != 20 || dwFlags)
286 SetLastError(ERROR_INVALID_PARAMETER);
289 if (phPrevCatInfo) prev = *phPrevCatInfo;
291 ret = CryptAcquireContextW(&prov, NULL, MS_DEF_PROV_W, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
292 if (!ret) return NULL;
298 size = strlenW(ca->path) * sizeof(WCHAR) + sizeof(globW);
299 if (!(path = HeapAlloc(GetProcessHeap(), 0, size)))
301 CryptReleaseContext(prov, 0);
302 SetLastError(ERROR_OUTOFMEMORY);
305 strcpyW(path, ca->path);
306 strcatW(path, globW);
309 ca->find = FindFirstFileW(path, &data);
311 HeapFree(GetProcessHeap(), 0, path);
312 if (ca->find == INVALID_HANDLE_VALUE)
314 CryptReleaseContext(prov, 0);
318 else if (!FindNextFileW(ca->find, &data))
320 CryptCATAdminReleaseCatalogContext(hCatAdmin, prev, 0);
321 CryptReleaseContext(prov, 0);
328 CRYPTCATMEMBER *member = NULL;
332 size = (strlenW(ca->path) + strlenW(data.cFileName) + 2) * sizeof(WCHAR);
333 if (!(filename = HeapAlloc(GetProcessHeap(), 0, size)))
335 SetLastError(ERROR_OUTOFMEMORY);
338 strcpyW(filename, ca->path);
339 strcatW(filename, slashW);
340 strcatW(filename, data.cFileName);
342 hcat = CryptCATOpen(filename, CRYPTCAT_OPEN_EXISTING, prov, 0, 0);
343 if (hcat == INVALID_HANDLE_VALUE)
345 WARN("couldn't open %s (%u)\n", debugstr_w(filename), GetLastError());
348 while ((member = CryptCATEnumerateMember(hcat, member)))
350 if (member->pIndirectData->Digest.cbData != cbHash)
352 WARN("amount of hash bytes differs: %u/%u\n", member->pIndirectData->Digest.cbData, cbHash);
355 if (!memcmp(member->pIndirectData->Digest.pbData, pbHash, cbHash))
357 TRACE("file %s matches\n", debugstr_w(data.cFileName));
360 CryptReleaseContext(prov, 0);
364 ca->find = INVALID_HANDLE_VALUE;
366 ci = create_catinfo(filename);
367 HeapFree(GetProcessHeap(), 0, filename);
372 HeapFree(GetProcessHeap(), 0, filename);
374 if (!FindNextFileW(ca->find, &data))
377 ca->find = INVALID_HANDLE_VALUE;
378 CryptReleaseContext(prov, 0);
385 /***********************************************************************
386 * CryptCATAdminReleaseCatalogContext (WINTRUST.@)
388 * Release a catalog context handle.
391 * hCatAdmin [I] Context handle.
392 * hCatInfo [I] Catalog handle.
393 * dwFlags [I] Reserved.
400 BOOL WINAPI CryptCATAdminReleaseCatalogContext(HCATADMIN hCatAdmin,
404 struct catinfo *ci = hCatInfo;
405 struct catadmin *ca = hCatAdmin;
407 TRACE("%p %p %x\n", hCatAdmin, hCatInfo, dwFlags);
409 if (!ca || ca->magic != CATADMIN_MAGIC || !ci || ci->magic != CATINFO_MAGIC)
411 SetLastError(ERROR_INVALID_PARAMETER);
415 return HeapFree(GetProcessHeap(), 0, ci);
418 /***********************************************************************
419 * CryptCATAdminReleaseContext (WINTRUST.@)
421 * Release a catalog administrator context handle.
424 * catAdmin [I] Context handle.
425 * dwFlags [I] Reserved.
432 BOOL WINAPI CryptCATAdminReleaseContext(HCATADMIN hCatAdmin, DWORD dwFlags )
434 struct catadmin *ca = hCatAdmin;
436 TRACE("%p %x\n", hCatAdmin, dwFlags);
438 if (!ca || ca->magic != CATADMIN_MAGIC)
440 SetLastError(ERROR_INVALID_PARAMETER);
443 if (ca->find != INVALID_HANDLE_VALUE) FindClose(ca->find);
445 return HeapFree(GetProcessHeap(), 0, ca);
448 /***********************************************************************
449 * CryptCATAdminRemoveCatalog (WINTRUST.@)
451 * Remove a catalog file.
454 * catAdmin [I] Context handle.
455 * pwszCatalogFile [I] Catalog file.
456 * dwFlags [I] Reserved.
463 BOOL WINAPI CryptCATAdminRemoveCatalog(HCATADMIN hCatAdmin, LPCWSTR pwszCatalogFile, DWORD dwFlags)
465 struct catadmin *ca = hCatAdmin;
467 TRACE("%p %s %x\n", hCatAdmin, debugstr_w(pwszCatalogFile), dwFlags);
469 if (!ca || ca->magic != CATADMIN_MAGIC)
471 SetLastError(ERROR_INVALID_PARAMETER);
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, ':'))
480 static const WCHAR slashW[] = {'\\',0};
484 len = strlenW(ca->path) + strlenW(pwszCatalogFile) + 2;
485 if (!(target = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
487 SetLastError(ERROR_OUTOFMEMORY);
490 strcpyW(target, ca->path);
491 strcatW(target, slashW);
492 strcatW(target, pwszCatalogFile);
496 HeapFree(GetProcessHeap(), 0, target);
502 /***********************************************************************
503 * CryptCATAdminResolveCatalogPath (WINTRUST.@)
505 BOOL WINAPI CryptCATAdminResolveCatalogPath(HCATADMIN hcatadmin, WCHAR *catalog_file,
506 CATALOG_INFO *info, DWORD flags)
508 static const WCHAR slashW[] = {'\\',0};
509 struct catadmin *ca = hcatadmin;
511 TRACE("%p %s %p %x\n", hcatadmin, debugstr_w(catalog_file), info, flags);
513 if (!ca || ca->magic != CATADMIN_MAGIC || !info || info->cbStruct != sizeof(*info) || flags)
515 SetLastError(ERROR_INVALID_PARAMETER);
518 strcpyW(info->wszCatalogFile, ca->path);
519 strcatW(info->wszCatalogFile, slashW);
520 strcatW(info->wszCatalogFile, catalog_file);
525 /***********************************************************************
526 * CryptCATClose (WINTRUST.@)
528 BOOL WINAPI CryptCATClose(HANDLE hCatalog)
530 struct cryptcat *cc = hCatalog;
532 TRACE("(%p)\n", hCatalog);
534 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
536 SetLastError(ERROR_INVALID_PARAMETER);
539 HeapFree(GetProcessHeap(), 0, cc->attr);
540 HeapFree(GetProcessHeap(), 0, cc->inner);
541 CryptMsgClose(cc->msg);
544 HeapFree(GetProcessHeap(), 0, cc);
548 /***********************************************************************
549 * CryptCATGetAttrInfo (WINTRUST.@)
551 CRYPTCATATTRIBUTE * WINAPI CryptCATGetAttrInfo(HANDLE hCatalog, CRYPTCATMEMBER *member, LPWSTR tag)
553 struct cryptcat *cc = hCatalog;
555 FIXME("%p, %p, %s\n", hCatalog, member, debugstr_w(tag));
557 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
559 SetLastError(ERROR_INVALID_PARAMETER);
562 SetLastError(CRYPT_E_NOT_FOUND);
566 /***********************************************************************
567 * CryptCATGetCatAttrInfo (WINTRUST.@)
569 CRYPTCATATTRIBUTE * WINAPI CryptCATGetCatAttrInfo(HANDLE hCatalog, LPWSTR tag)
571 struct cryptcat *cc = hCatalog;
573 FIXME("%p, %s\n", hCatalog, debugstr_w(tag));
575 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
577 SetLastError(ERROR_INVALID_PARAMETER);
580 SetLastError(CRYPT_E_NOT_FOUND);
584 CRYPTCATMEMBER * WINAPI CryptCATGetMemberInfo(HANDLE hCatalog, LPWSTR tag)
586 struct cryptcat *cc = hCatalog;
588 FIXME("%p, %s\n", hCatalog, debugstr_w(tag));
590 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
592 SetLastError(ERROR_INVALID_PARAMETER);
595 SetLastError(CRYPT_E_NOT_FOUND);
599 /***********************************************************************
600 * CryptCATEnumerateAttr (WINTRUST.@)
602 CRYPTCATATTRIBUTE * WINAPI CryptCATEnumerateAttr(HANDLE hCatalog, CRYPTCATMEMBER *member, CRYPTCATATTRIBUTE *prev)
604 struct cryptcat *cc = hCatalog;
606 FIXME("%p, %p, %p\n", hCatalog, member, prev);
608 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
610 SetLastError(ERROR_INVALID_PARAMETER);
613 SetLastError(CRYPT_E_NOT_FOUND);
617 /***********************************************************************
618 * CryptCATEnumerateCatAttr (WINTRUST.@)
620 CRYPTCATATTRIBUTE * WINAPI CryptCATEnumerateCatAttr(HANDLE hCatalog, CRYPTCATATTRIBUTE *prev)
622 struct cryptcat *cc = hCatalog;
624 FIXME("%p, %p\n", hCatalog, prev);
626 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
628 SetLastError(ERROR_INVALID_PARAMETER);
631 SetLastError(CRYPT_E_NOT_FOUND);
635 /***********************************************************************
636 * CryptCATEnumerateMember (WINTRUST.@)
638 CRYPTCATMEMBER * WINAPI CryptCATEnumerateMember(HANDLE hCatalog, CRYPTCATMEMBER *prev)
640 struct cryptcat *cc = hCatalog;
641 CRYPTCATMEMBER *member = prev;
645 TRACE("%p, %p\n", hCatalog, prev);
647 if (!hCatalog || hCatalog == INVALID_HANDLE_VALUE || cc->magic != CRYPTCAT_MAGIC)
649 SetLastError(ERROR_INVALID_PARAMETER);
653 /* dumping the contents makes me think that dwReserved is the iteration number */
656 if (!(member = HeapAlloc(GetProcessHeap(), 0, sizeof(*member))))
658 SetLastError(ERROR_OUTOFMEMORY);
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;
670 else member->dwReserved++;
672 if (member->dwReserved >= cc->inner->cCTLEntry)
674 SetLastError(ERROR_INVALID_PARAMETER);
678 /* list them backwards, like native */
679 entry = &cc->inner->rgCTLEntry[cc->inner->cCTLEntry - member->dwReserved - 1];
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;
686 for (i = 0; i < entry->cAttribute; i++)
688 CRYPT_ATTRIBUTE *attr = entry->rgAttribute + i;
690 if (attr->cValue != 1)
692 ERR("Can't handle attr->cValue of %u\n", attr->cValue);
695 if (!strcmp(attr->pszObjId, CAT_MEMBERINFO_OBJID))
700 member->sEncodedMemberInfo.cbData = attr->rgValue->cbData;
701 member->sEncodedMemberInfo.pbData = attr->rgValue->pbData;
703 CryptDecodeObject(cc->encoding, CAT_MEMBERINFO_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, NULL, &size);
705 if (!(mi = HeapAlloc(GetProcessHeap(), 0, size)))
707 SetLastError(ERROR_OUTOFMEMORY);
710 ret = CryptDecodeObject(cc->encoding, CAT_MEMBERINFO_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, mi, &size);
715 member->dwCertVersion = mi->dwCertVersion;
716 RtlInitUnicodeString(&guid, mi->pwszSubjGuid);
717 if (RtlGUIDFromString(&guid, &member->gSubjectType))
719 HeapFree(GetProcessHeap(), 0, mi);
723 HeapFree(GetProcessHeap(), 0, mi);
724 if (!ret) goto error;
726 else if (!strcmp(attr->pszObjId, SPC_INDIRECT_DATA_OBJID))
728 /* SPC_INDIRECT_DATA_CONTENT is equal to SIP_INDIRECT_DATA */
730 member->sEncodedIndirectData.cbData = attr->rgValue->cbData;
731 member->sEncodedIndirectData.pbData = attr->rgValue->pbData;
733 CryptDecodeObject(cc->encoding, SPC_INDIRECT_DATA_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, NULL, &size);
735 if (!(member->pIndirectData = HeapAlloc(GetProcessHeap(), 0, size)))
737 SetLastError(ERROR_OUTOFMEMORY);
740 CryptDecodeObject(cc->encoding, SPC_INDIRECT_DATA_OBJID, attr->rgValue->pbData, attr->rgValue->cbData, 0, member->pIndirectData, &size);
743 /* this object id should probably be handled in CryptCATEnumerateAttr */
744 FIXME("unhandled object id \"%s\"\n", attr->pszObjId);
747 if (!member->sEncodedMemberInfo.cbData || !member->sEncodedIndirectData.cbData)
749 ERR("Corrupted catalog entry?\n");
750 SetLastError(CRYPT_E_ATTRIBUTES_MISSING);
753 size = (2 * member->pIndirectData->Digest.cbData + 1) * sizeof(WCHAR);
754 if (member->pwszReferenceTag)
755 member->pwszReferenceTag = HeapReAlloc(GetProcessHeap(), 0, member->pwszReferenceTag, size);
757 member->pwszReferenceTag = HeapAlloc(GetProcessHeap(), 0, size);
759 if (!member->pwszReferenceTag)
761 SetLastError(ERROR_OUTOFMEMORY);
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++)
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);
774 member->pwszReferenceTag[i * 2] = 0;
778 HeapFree(GetProcessHeap(), 0, member->pIndirectData);
779 HeapFree(GetProcessHeap(), 0, member->pwszReferenceTag);
780 HeapFree(GetProcessHeap(), 0, member);
784 static CTL_INFO *decode_inner_content(HANDLE hmsg, DWORD encoding, DWORD *len)
789 CTL_INFO *inner = NULL;
791 if (!CryptMsgGetParam(hmsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, NULL, &size)) return NULL;
792 if (!(oid = HeapAlloc(GetProcessHeap(), 0, size)))
794 SetLastError(ERROR_OUTOFMEMORY);
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)))
801 SetLastError(ERROR_OUTOFMEMORY);
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)))
808 SetLastError(ERROR_OUTOFMEMORY);
811 if (!CryptDecodeObject(encoding, oid, buffer, size, 0, inner, &size)) goto out;
815 HeapFree(GetProcessHeap(), 0, oid);
816 HeapFree(GetProcessHeap(), 0, buffer);
820 /***********************************************************************
821 * CryptCATCatalogInfoFromContext (WINTRUST.@)
823 BOOL WINAPI CryptCATCatalogInfoFromContext(HCATINFO hcatinfo, CATALOG_INFO *info, DWORD flags)
825 struct catinfo *ci = hcatinfo;
827 TRACE("%p, %p, %x\n", hcatinfo, info, flags);
829 if (!hcatinfo || hcatinfo == INVALID_HANDLE_VALUE || ci->magic != CATINFO_MAGIC ||
830 flags || !info || info->cbStruct != sizeof(*info))
832 SetLastError(ERROR_INVALID_PARAMETER);
835 strcpyW(info->wszCatalogFile, ci->file);
839 /***********************************************************************
840 * CryptCATOpen (WINTRUST.@)
842 HANDLE WINAPI CryptCATOpen(LPWSTR pwszFileName, DWORD fdwOpenFlags, HCRYPTPROV hProv,
843 DWORD dwPublicVersion, DWORD dwEncodingType)
847 DWORD size, flags = OPEN_EXISTING;
850 TRACE("%s, %x, %lx, %x, %x\n", debugstr_w(pwszFileName), fdwOpenFlags,
851 hProv, dwPublicVersion, dwEncodingType);
855 SetLastError(ERROR_INVALID_PARAMETER);
856 return INVALID_HANDLE_VALUE;
859 if (!dwPublicVersion) dwPublicVersion = 0x00000100;
860 if (!dwEncodingType) dwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
862 if (fdwOpenFlags & CRYPTCAT_OPEN_ALWAYS) flags |= OPEN_ALWAYS;
863 if (fdwOpenFlags & CRYPTCAT_OPEN_CREATENEW) flags |= CREATE_NEW;
865 file = CreateFileW(pwszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, flags, 0, NULL);
866 if (file == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
868 size = GetFileSize(file, NULL);
869 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, size)))
872 SetLastError(ERROR_OUTOFMEMORY);
873 return INVALID_HANDLE_VALUE;
875 if (!(hmsg = CryptMsgOpenToDecode(dwEncodingType, 0, 0, hProv, NULL, NULL)))
878 HeapFree(GetProcessHeap(), 0, buffer);
879 return INVALID_HANDLE_VALUE;
881 if (!ReadFile(file, buffer, size, &size, NULL) || !CryptMsgUpdate(hmsg, buffer, size, TRUE))
884 HeapFree(GetProcessHeap(), 0, buffer);
886 return INVALID_HANDLE_VALUE;
888 HeapFree(GetProcessHeap(), 0, buffer);
891 size = sizeof(DWORD);
892 if (!(cc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cc))))
895 SetLastError(ERROR_OUTOFMEMORY);
896 return INVALID_HANDLE_VALUE;
900 cc->encoding = dwEncodingType;
901 if (CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_COUNT_PARAM, 0, &cc->attr_count, &size))
906 for (i = 0; i < cc->attr_count; i++)
908 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size))
911 return INVALID_HANDLE_VALUE;
915 if (!(cc->attr = HeapAlloc(GetProcessHeap(), 0, sizeof(*cc->attr) * cc->attr_count + sum)))
918 SetLastError(ERROR_OUTOFMEMORY);
919 return INVALID_HANDLE_VALUE;
921 p = (BYTE *)(cc->attr + cc->attr_count);
922 for (i = 0; i < cc->attr_count; i++)
924 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, NULL, &size))
927 HeapFree(GetProcessHeap(), 0, cc->attr);
928 return INVALID_HANDLE_VALUE;
930 if (!CryptMsgGetParam(hmsg, CMSG_ATTR_CERT_PARAM, i, p, &size))
933 HeapFree(GetProcessHeap(), 0, cc->attr);
934 return INVALID_HANDLE_VALUE;
938 cc->inner = decode_inner_content(hmsg, dwEncodingType, &cc->inner_len);
939 if (!cc->inner || !CryptSIPRetrieveSubjectGuid(pwszFileName, NULL, &cc->subject))
942 HeapFree(GetProcessHeap(), 0, cc->attr);
943 HeapFree(GetProcessHeap(), 0, cc->inner);
944 HeapFree(GetProcessHeap(), 0, cc);
945 return INVALID_HANDLE_VALUE;
947 cc->magic = CRYPTCAT_MAGIC;
950 return INVALID_HANDLE_VALUE;
953 /***********************************************************************
954 * CryptSIPCreateIndirectData (WINTRUST.@)
956 BOOL WINAPI CryptSIPCreateIndirectData(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pcbIndirectData,
957 SIP_INDIRECT_DATA* pIndirectData)
959 FIXME("(%p %p %p) stub\n", pSubjectInfo, pcbIndirectData, pIndirectData);
965 /***********************************************************************
966 * CryptCATCDFClose (WINTRUST.@)
968 BOOL WINAPI CryptCATCDFClose(CRYPTCATCDF *pCDF)
970 FIXME("(%p) stub\n", pCDF);
975 /***********************************************************************
976 * CryptCATCDFEnumCatAttributes (WINTRUST.@)
978 CRYPTCATATTRIBUTE * WINAPI CryptCATCDFEnumCatAttributes(CRYPTCATCDF *pCDF,
979 CRYPTCATATTRIBUTE *pPrevAttr,
980 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
982 FIXME("(%p %p %p) stub\n", pCDF, pPrevAttr, pfnParseError);
987 /***********************************************************************
988 * CryptCATCDFEnumMembersByCDFTagEx (WINTRUST.@)
990 LPWSTR WINAPI CryptCATCDFEnumMembersByCDFTagEx(CRYPTCATCDF *pCDF, LPWSTR pwszPrevCDFTag,
991 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError,
992 CRYPTCATMEMBER **ppMember, BOOL fContinueOnError,
995 FIXME("(%p %s %p %p %d %p) stub\n", pCDF, debugstr_w(pwszPrevCDFTag), pfnParseError,
996 ppMember, fContinueOnError, pvReserved);
1001 /***********************************************************************
1002 * CryptCATCDFOpen (WINTRUST.@)
1004 CRYPTCATCDF * WINAPI CryptCATCDFOpen(LPWSTR pwszFilePath,
1005 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
1007 FIXME("(%s %p) stub\n", debugstr_w(pwszFilePath), pfnParseError);
1012 static BOOL WINTRUST_GetSignedMsgFromPEFile(SIP_SUBJECTINFO *pSubjectInfo,
1013 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1014 BYTE *pbSignedDataMsg)
1017 WIN_CERTIFICATE *pCert = NULL;
1019 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1020 pcbSignedDataMsg, pbSignedDataMsg);
1022 if (!pbSignedDataMsg)
1024 WIN_CERTIFICATE cert;
1026 /* app hasn't passed buffer, just get the length */
1027 ret = ImageGetCertificateHeader(pSubjectInfo->hFile, dwIndex, &cert);
1029 *pcbSignedDataMsg = cert.dwLength;
1035 ret = ImageGetCertificateData(pSubjectInfo->hFile, dwIndex, NULL, &len);
1036 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1038 pCert = HeapAlloc(GetProcessHeap(), 0, len);
1044 ret = ImageGetCertificateData(pSubjectInfo->hFile, dwIndex, pCert,
1048 if (*pcbSignedDataMsg < pCert->dwLength)
1050 *pcbSignedDataMsg = pCert->dwLength;
1051 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1056 memcpy(pbSignedDataMsg, pCert->bCertificate, pCert->dwLength);
1057 switch (pCert->wCertificateType)
1059 case WIN_CERT_TYPE_X509:
1060 *pdwEncodingType = X509_ASN_ENCODING;
1062 case WIN_CERT_TYPE_PKCS_SIGNED_DATA:
1063 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1066 FIXME("don't know what to do for encoding type %d\n",
1067 pCert->wCertificateType);
1068 *pdwEncodingType = 0;
1073 HeapFree(GetProcessHeap(), 0, pCert);
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)
1091 #define cfheadRESERVE_PRESENT (0x0004)
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]))
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.
1101 typedef struct _CAB_SIGNINFO
1103 WORD unk0; /* always 0? */
1104 WORD unk1; /* always 0x0010? */
1107 } CAB_SIGNINFO, *PCAB_SIGNINFO;
1109 static BOOL WINTRUST_GetSignedMsgFromCabFile(SIP_SUBJECTINFO *pSubjectInfo,
1110 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1111 BYTE *pbSignedDataMsg)
1114 LONG base_offset, cabsize;
1117 DWORD cert_offset, cert_size, dwRead;
1119 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1120 pcbSignedDataMsg, pbSignedDataMsg);
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.
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.
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.
1152 /* get basic offset & size info */
1153 base_offset = SetFilePointer(pSubjectInfo->hFile, 0L, NULL, SEEK_CUR);
1155 if (SetFilePointer(pSubjectInfo->hFile, 0, NULL, SEEK_END) == INVALID_SET_FILE_POINTER)
1157 TRACE("seek error\n");
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))
1165 TRACE("seek error\n");
1169 /* read in the CFHEADER */
1170 if (!ReadFile(pSubjectInfo->hFile, buf, cfhead_SIZEOF, &dwRead, NULL) ||
1171 dwRead != cfhead_SIZEOF)
1173 TRACE("reading header failed\n");
1177 /* check basic MSCF signature */
1178 if (EndGetI32(buf+cfhead_Signature) != 0x4643534d)
1180 WARN("cabinet signature not present\n");
1184 /* Ignore the number of folders and files and the set and cabinet IDs */
1186 /* check the header revision */
1187 if ((buf[cfhead_MajorVersion] > 1) ||
1188 (buf[cfhead_MajorVersion] == 1 && buf[cfhead_MinorVersion] > 3))
1190 WARN("cabinet format version > 1.3\n");
1194 /* pull the flags out */
1195 flags = EndGetI16(buf+cfhead_Flags);
1197 if (!(flags & cfheadRESERVE_PRESENT))
1199 TRACE("no header present, not signed\n");
1203 if (!ReadFile(pSubjectInfo->hFile, buf, cfheadext_SIZEOF, &dwRead, NULL) ||
1204 dwRead != cfheadext_SIZEOF)
1206 ERR("bunk reserve-sizes?\n");
1210 header_resv = EndGetI16(buf+cfheadext_HeaderReserved);
1213 TRACE("no header_resv, not signed\n");
1216 else if (header_resv < cfsigninfo_SIZEOF)
1218 TRACE("header_resv too small, not signed\n");
1222 if (header_resv > 60000)
1224 WARN("WARNING; header reserved space > 60000\n");
1227 if (!ReadFile(pSubjectInfo->hFile, buf, cfsigninfo_SIZEOF, &dwRead, NULL) ||
1228 dwRead != cfsigninfo_SIZEOF)
1230 ERR("couldn't read reserve\n");
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);
1239 /* The redundant checks are to avoid wraparound */
1240 if (cert_offset > cabsize || cert_size > cabsize ||
1241 cert_offset + cert_size > cabsize)
1243 WARN("offset beyond file, not attempting to read\n");
1247 SetFilePointer(pSubjectInfo->hFile, base_offset, NULL, SEEK_SET);
1248 if (!pbSignedDataMsg)
1250 *pcbSignedDataMsg = cert_size;
1253 if (*pcbSignedDataMsg < cert_size)
1255 *pcbSignedDataMsg = cert_size;
1256 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1259 if (SetFilePointer(pSubjectInfo->hFile, cert_offset, NULL, SEEK_SET) == INVALID_SET_FILE_POINTER)
1261 ERR("couldn't seek to cert location\n");
1264 if (!ReadFile(pSubjectInfo->hFile, pbSignedDataMsg, cert_size, &dwRead,
1265 NULL) || dwRead != cert_size)
1267 ERR("couldn't read cert\n");
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
1274 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1278 static BOOL WINTRUST_GetSignedMsgFromCatFile(SIP_SUBJECTINFO *pSubjectInfo,
1279 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
1280 BYTE *pbSignedDataMsg)
1284 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1285 pcbSignedDataMsg, pbSignedDataMsg);
1287 if (!pbSignedDataMsg)
1289 *pcbSignedDataMsg = GetFileSize(pSubjectInfo->hFile, NULL);
1294 DWORD len = GetFileSize(pSubjectInfo->hFile, NULL);
1296 if (*pcbSignedDataMsg < len)
1298 *pcbSignedDataMsg = len;
1299 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1304 ret = ReadFile(pSubjectInfo->hFile, pbSignedDataMsg, len,
1305 pcbSignedDataMsg, NULL);
1307 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
1313 /***********************************************************************
1314 * CryptSIPGetSignedDataMsg (WINTRUST.@)
1316 BOOL WINAPI CryptSIPGetSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pdwEncodingType,
1317 DWORD dwIndex, DWORD* pcbSignedDataMsg, BYTE* pbSignedDataMsg)
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 }};
1327 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
1328 pcbSignedDataMsg, pbSignedDataMsg);
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);
1341 FIXME("unimplemented for subject type %s\n",
1342 debugstr_guid(pSubjectInfo->pgSubjectType));
1346 TRACE("returning %d\n", ret);
1350 /***********************************************************************
1351 * CryptSIPPutSignedDataMsg (WINTRUST.@)
1353 BOOL WINAPI CryptSIPPutSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD pdwEncodingType,
1354 DWORD* pdwIndex, DWORD cbSignedDataMsg, BYTE* pbSignedDataMsg)
1356 FIXME("(%p %d %p %d %p) stub\n", pSubjectInfo, pdwEncodingType, pdwIndex,
1357 cbSignedDataMsg, pbSignedDataMsg);
1362 /***********************************************************************
1363 * CryptSIPRemoveSignedDataMsg (WINTRUST.@)
1365 BOOL WINAPI CryptSIPRemoveSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo,
1368 FIXME("(%p %d) stub\n", pSubjectInfo, dwIndex);
1373 /***********************************************************************
1374 * CryptSIPVerifyIndirectData (WINTRUST.@)
1376 BOOL WINAPI CryptSIPVerifyIndirectData(SIP_SUBJECTINFO* pSubjectInfo,
1377 SIP_INDIRECT_DATA* pIndirectData)
1379 FIXME("(%p %p) stub\n", pSubjectInfo, pIndirectData);