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
36 #include "wine/debug.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
= malloc(sizeof(*ci
))))
75 SetLastError(ERROR_OUTOFMEMORY
);
76 return INVALID_HANDLE_VALUE
;
78 lstrcpyW(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 %lx\n", catAdmin
, debugstr_guid(sys
), dwFlags
);
116 if (!catAdmin
|| dwFlags
)
118 SetLastError(ERROR_INVALID_PARAMETER
);
121 if (!(ca
= malloc(sizeof(*ca
))))
123 SetLastError(ERROR_OUTOFMEMORY
);
127 GetSystemDirectoryW(catroot_dir
, MAX_PATH
);
128 lstrcatW(catroot_dir
, catroot
);
130 /* create the directory if it doesn't exist */
131 CreateDirectoryW(catroot_dir
, NULL
);
133 if (!sys
) sys
= &defsys
;
134 swprintf(ca
->path
, ARRAY_SIZE(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 * CryptCATAdminAcquireContext2 (WINTRUST.@)
152 BOOL WINAPI
CryptCATAdminAcquireContext2(HCATADMIN
*catAdmin
, const GUID
*sys
, const WCHAR
*algorithm
,
153 const CERT_STRONG_SIGN_PARA
*policy
, DWORD flags
)
155 FIXME("%p %s %s %p %lx stub\n", catAdmin
, debugstr_guid(sys
), debugstr_w(algorithm
), policy
, flags
);
156 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
160 /***********************************************************************
161 * CryptCATAdminAddCatalog (WINTRUST.@)
163 HCATINFO WINAPI
CryptCATAdminAddCatalog(HCATADMIN catAdmin
, PWSTR catalogFile
,
164 PWSTR selectBaseName
, DWORD flags
)
166 static const WCHAR slashW
[] = {'\\',0};
167 struct catadmin
*ca
= catAdmin
;
172 TRACE("%p %s %s %ld\n", catAdmin
, debugstr_w(catalogFile
),
173 debugstr_w(selectBaseName
), flags
);
177 FIXME("NULL basename not handled\n");
178 SetLastError(ERROR_INVALID_PARAMETER
);
181 if (!ca
|| ca
->magic
!= CATADMIN_MAGIC
|| !catalogFile
|| flags
)
183 SetLastError(ERROR_INVALID_PARAMETER
);
187 len
= lstrlenW(ca
->path
) + lstrlenW(selectBaseName
) + 2;
188 if (!(target
= malloc(len
* sizeof(WCHAR
))))
190 SetLastError(ERROR_OUTOFMEMORY
);
193 lstrcpyW(target
, ca
->path
);
194 lstrcatW(target
, slashW
);
195 lstrcatW(target
, selectBaseName
);
197 if (!CopyFileW(catalogFile
, target
, FALSE
))
202 SetFileAttributesW(target
, FILE_ATTRIBUTE_SYSTEM
);
204 if (!(ci
= malloc(sizeof(*ci
))))
207 SetLastError(ERROR_OUTOFMEMORY
);
210 ci
->magic
= CATINFO_MAGIC
;
211 lstrcpyW(ci
->file
, target
);
217 static BOOL
pe_image_hash( HANDLE file
, HCRYPTHASH hash
)
219 UINT32 size
, offset
, file_size
, sig_pos
;
222 IMAGE_NT_HEADERS
*nt
;
225 if ((file_size
= GetFileSize( file
, NULL
)) == INVALID_FILE_SIZE
) return FALSE
;
227 if ((mapping
= CreateFileMappingW( file
, NULL
, PAGE_READONLY
, 0, 0, NULL
)) == INVALID_HANDLE_VALUE
)
230 if (!(view
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 )) || !(nt
= ImageNtHeader( view
))) goto done
;
232 if (nt
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
234 const IMAGE_NT_HEADERS64
*nt64
= (const IMAGE_NT_HEADERS64
*)nt
;
236 /* offset from start of file to checksum */
237 offset
= (BYTE
*)&nt64
->OptionalHeader
.CheckSum
- view
;
239 /* area between checksum and security directory entry */
240 size
= FIELD_OFFSET( IMAGE_OPTIONAL_HEADER64
, DataDirectory
[IMAGE_DIRECTORY_ENTRY_SECURITY
] ) -
241 FIELD_OFFSET( IMAGE_OPTIONAL_HEADER64
, Subsystem
);
243 if (nt64
->OptionalHeader
.NumberOfRvaAndSizes
< IMAGE_FILE_SECURITY_DIRECTORY
+ 1) goto done
;
244 sig_pos
= nt64
->OptionalHeader
.DataDirectory
[IMAGE_FILE_SECURITY_DIRECTORY
].VirtualAddress
;
246 else if (nt
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR32_MAGIC
)
248 const IMAGE_NT_HEADERS32
*nt32
= (const IMAGE_NT_HEADERS32
*)nt
;
250 /* offset from start of file to checksum */
251 offset
= (BYTE
*)&nt32
->OptionalHeader
.CheckSum
- view
;
253 /* area between checksum and security directory entry */
254 size
= FIELD_OFFSET( IMAGE_OPTIONAL_HEADER32
, DataDirectory
[IMAGE_DIRECTORY_ENTRY_SECURITY
] ) -
255 FIELD_OFFSET( IMAGE_OPTIONAL_HEADER32
, Subsystem
);
257 if (nt32
->OptionalHeader
.NumberOfRvaAndSizes
< IMAGE_FILE_SECURITY_DIRECTORY
+ 1) goto done
;
258 sig_pos
= nt32
->OptionalHeader
.DataDirectory
[IMAGE_FILE_SECURITY_DIRECTORY
].VirtualAddress
;
262 if (!CryptHashData( hash
, view
, offset
, 0 )) goto done
;
263 offset
+= sizeof(DWORD
); /* skip checksum */
264 if (!CryptHashData( hash
, view
+ offset
, size
, 0 )) goto done
;
266 offset
+= size
+ sizeof(IMAGE_DATA_DIRECTORY
); /* skip security entry */
267 if (offset
> file_size
) goto done
;
270 if (sig_pos
< offset
) goto done
;
271 if (sig_pos
> file_size
) goto done
;
272 size
= sig_pos
- offset
; /* exclude signature */
274 else size
= file_size
- offset
;
276 if (!CryptHashData( hash
, view
+ offset
, size
, 0 )) goto done
;
279 if (!sig_pos
&& (size
= file_size
% 8))
281 static const BYTE pad
[7];
282 ret
= CryptHashData( hash
, pad
, 8 - size
, 0 );
286 UnmapViewOfFile( view
);
287 CloseHandle( mapping
);
291 /***********************************************************************
292 * CryptCATAdminCalcHashFromFileHandle (WINTRUST.@)
294 BOOL WINAPI
CryptCATAdminCalcHashFromFileHandle(HANDLE hFile
, DWORD
*pcbHash
, BYTE
*pbHash
, DWORD dwFlags
)
298 TRACE("%p %p %p %lx\n", hFile
, pcbHash
, pbHash
, dwFlags
);
300 if (!hFile
|| !pcbHash
|| dwFlags
)
302 SetLastError(ERROR_INVALID_PARAMETER
);
308 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
320 if (!(buffer
= malloc(4096)))
322 SetLastError(ERROR_OUTOFMEMORY
);
325 ret
= CryptAcquireContextW(&prov
, NULL
, MS_DEF_PROV_W
, PROV_RSA_FULL
, CRYPT_VERIFYCONTEXT
);
331 ret
= CryptCreateHash(prov
, CALG_SHA1
, 0, 0, &hash
);
335 CryptReleaseContext(prov
, 0);
339 if (!(ret
= pe_image_hash(hFile
, hash
)))
341 while ((ret
= ReadFile(hFile
, buffer
, 4096, &bytes_read
, NULL
)) && bytes_read
)
343 CryptHashData(hash
, buffer
, bytes_read
, 0);
346 if (ret
) ret
= CryptGetHashParam(hash
, HP_HASHVAL
, pbHash
, pcbHash
, 0);
349 CryptDestroyHash(hash
);
350 CryptReleaseContext(prov
, 0);
355 /***********************************************************************
356 * CryptCATAdminEnumCatalogFromHash (WINTRUST.@)
358 HCATINFO WINAPI
CryptCATAdminEnumCatalogFromHash(HCATADMIN hCatAdmin
, BYTE
* pbHash
,
359 DWORD cbHash
, DWORD dwFlags
,
360 HCATINFO
* phPrevCatInfo
)
362 static const WCHAR slashW
[] = {'\\',0};
363 static const WCHAR globW
[] = {'\\','*','.','c','a','t',0};
365 struct catadmin
*ca
= hCatAdmin
;
366 WIN32_FIND_DATAW data
;
367 HCATINFO prev
= NULL
;
372 TRACE("%p %p %ld %lx %p\n", hCatAdmin
, pbHash
, cbHash
, dwFlags
, phPrevCatInfo
);
374 if (!ca
|| ca
->magic
!= CATADMIN_MAGIC
|| !pbHash
|| cbHash
!= 20 || dwFlags
)
376 SetLastError(ERROR_INVALID_PARAMETER
);
379 if (phPrevCatInfo
) prev
= *phPrevCatInfo
;
381 ret
= CryptAcquireContextW(&prov
, NULL
, MS_DEF_PROV_W
, PROV_RSA_FULL
, CRYPT_VERIFYCONTEXT
);
382 if (!ret
) return NULL
;
388 size
= lstrlenW(ca
->path
) * sizeof(WCHAR
) + sizeof(globW
);
389 if (!(path
= malloc(size
)))
391 CryptReleaseContext(prov
, 0);
392 SetLastError(ERROR_OUTOFMEMORY
);
395 lstrcpyW(path
, ca
->path
);
396 lstrcatW(path
, globW
);
399 ca
->find
= FindFirstFileW(path
, &data
);
402 if (ca
->find
== INVALID_HANDLE_VALUE
)
404 CryptReleaseContext(prov
, 0);
408 else if (!FindNextFileW(ca
->find
, &data
))
410 CryptCATAdminReleaseCatalogContext(hCatAdmin
, prev
, 0);
411 CryptReleaseContext(prov
, 0);
418 CRYPTCATMEMBER
*member
= NULL
;
422 size
= (lstrlenW(ca
->path
) + lstrlenW(data
.cFileName
) + 2) * sizeof(WCHAR
);
423 if (!(filename
= malloc(size
)))
425 SetLastError(ERROR_OUTOFMEMORY
);
428 lstrcpyW(filename
, ca
->path
);
429 lstrcatW(filename
, slashW
);
430 lstrcatW(filename
, data
.cFileName
);
432 hcat
= CryptCATOpen(filename
, CRYPTCAT_OPEN_EXISTING
, prov
, 0, 0);
433 if (hcat
== INVALID_HANDLE_VALUE
)
435 WARN("couldn't open %s (%lu)\n", debugstr_w(filename
), GetLastError());
438 while ((member
= CryptCATEnumerateMember(hcat
, member
)))
440 if (member
->pIndirectData
->Digest
.cbData
!= cbHash
)
442 WARN("amount of hash bytes differs: %lu/%lu\n", member
->pIndirectData
->Digest
.cbData
, cbHash
);
445 if (!memcmp(member
->pIndirectData
->Digest
.pbData
, pbHash
, cbHash
))
447 TRACE("file %s matches\n", debugstr_w(data
.cFileName
));
450 CryptReleaseContext(prov
, 0);
454 ca
->find
= INVALID_HANDLE_VALUE
;
456 ci
= create_catinfo(filename
);
464 if (!FindNextFileW(ca
->find
, &data
))
467 ca
->find
= INVALID_HANDLE_VALUE
;
468 CryptReleaseContext(prov
, 0);
475 /***********************************************************************
476 * CryptCATAdminReleaseCatalogContext (WINTRUST.@)
478 * Release a catalog context handle.
481 * hCatAdmin [I] Context handle.
482 * hCatInfo [I] Catalog handle.
483 * dwFlags [I] Reserved.
490 BOOL WINAPI
CryptCATAdminReleaseCatalogContext(HCATADMIN hCatAdmin
,
494 struct catinfo
*ci
= hCatInfo
;
495 struct catadmin
*ca
= hCatAdmin
;
497 TRACE("%p %p %lx\n", hCatAdmin
, hCatInfo
, dwFlags
);
499 if (!ca
|| ca
->magic
!= CATADMIN_MAGIC
|| !ci
|| ci
->magic
!= CATINFO_MAGIC
)
501 SetLastError(ERROR_INVALID_PARAMETER
);
504 /* Ensure compiler doesn't optimize out the assignment with 0. */
505 SecureZeroMemory(&ci
->magic
, sizeof(ci
->magic
));
510 /***********************************************************************
511 * CryptCATAdminReleaseContext (WINTRUST.@)
513 * Release a catalog administrator context handle.
516 * catAdmin [I] Context handle.
517 * dwFlags [I] Reserved.
524 BOOL WINAPI
CryptCATAdminReleaseContext(HCATADMIN hCatAdmin
, DWORD dwFlags
)
526 struct catadmin
*ca
= hCatAdmin
;
528 TRACE("%p %lx\n", hCatAdmin
, dwFlags
);
530 if (!ca
|| ca
->magic
!= CATADMIN_MAGIC
)
532 SetLastError(ERROR_INVALID_PARAMETER
);
535 if (ca
->find
!= INVALID_HANDLE_VALUE
) FindClose(ca
->find
);
536 /* Ensure compiler doesn't optimize out the assignment with 0. */
537 SecureZeroMemory(&ca
->magic
, sizeof(ca
->magic
));
542 /***********************************************************************
543 * CryptCATAdminRemoveCatalog (WINTRUST.@)
545 * Remove a catalog file.
548 * catAdmin [I] Context handle.
549 * pwszCatalogFile [I] Catalog file.
550 * dwFlags [I] Reserved.
557 BOOL WINAPI
CryptCATAdminRemoveCatalog(HCATADMIN hCatAdmin
, LPCWSTR pwszCatalogFile
, DWORD dwFlags
)
559 struct catadmin
*ca
= hCatAdmin
;
561 TRACE("%p %s %lx\n", hCatAdmin
, debugstr_w(pwszCatalogFile
), dwFlags
);
563 if (!ca
|| ca
->magic
!= CATADMIN_MAGIC
)
565 SetLastError(ERROR_INVALID_PARAMETER
);
569 /* Only delete when there is a filename and no path */
570 if (pwszCatalogFile
&& pwszCatalogFile
[0] != 0 &&
571 !wcschr(pwszCatalogFile
, '\\') && !wcschr(pwszCatalogFile
, '/') &&
572 !wcschr(pwszCatalogFile
, ':'))
574 static const WCHAR slashW
[] = {'\\',0};
578 len
= lstrlenW(ca
->path
) + lstrlenW(pwszCatalogFile
) + 2;
579 if (!(target
= malloc(len
* sizeof(WCHAR
))))
581 SetLastError(ERROR_OUTOFMEMORY
);
584 lstrcpyW(target
, ca
->path
);
585 lstrcatW(target
, slashW
);
586 lstrcatW(target
, pwszCatalogFile
);
596 /***********************************************************************
597 * CryptCATAdminResolveCatalogPath (WINTRUST.@)
599 BOOL WINAPI
CryptCATAdminResolveCatalogPath(HCATADMIN hcatadmin
, WCHAR
*catalog_file
,
600 CATALOG_INFO
*info
, DWORD flags
)
602 static const WCHAR slashW
[] = {'\\',0};
603 struct catadmin
*ca
= hcatadmin
;
605 TRACE("%p %s %p %lx\n", hcatadmin
, debugstr_w(catalog_file
), info
, flags
);
607 if (!ca
|| ca
->magic
!= CATADMIN_MAGIC
|| !info
|| info
->cbStruct
!= sizeof(*info
) || flags
)
609 SetLastError(ERROR_INVALID_PARAMETER
);
612 lstrcpyW(info
->wszCatalogFile
, ca
->path
);
613 lstrcatW(info
->wszCatalogFile
, slashW
);
614 lstrcatW(info
->wszCatalogFile
, catalog_file
);
619 /***********************************************************************
620 * CryptCATClose (WINTRUST.@)
622 BOOL WINAPI
CryptCATClose(HANDLE hCatalog
)
624 struct cryptcat
*cc
= hCatalog
;
626 TRACE("(%p)\n", hCatalog
);
628 if (!hCatalog
|| hCatalog
== INVALID_HANDLE_VALUE
|| cc
->magic
!= CRYPTCAT_MAGIC
)
630 SetLastError(ERROR_INVALID_PARAMETER
);
635 CryptMsgClose(cc
->msg
);
637 /* Ensure compiler doesn't optimize out the assignment with 0. */
638 SecureZeroMemory(&cc
->magic
, sizeof(cc
->magic
));
643 /***********************************************************************
644 * CryptCATGetAttrInfo (WINTRUST.@)
646 CRYPTCATATTRIBUTE
* WINAPI
CryptCATGetAttrInfo(HANDLE hCatalog
, CRYPTCATMEMBER
*member
, LPWSTR tag
)
648 struct cryptcat
*cc
= hCatalog
;
650 FIXME("%p, %p, %s\n", hCatalog
, member
, debugstr_w(tag
));
652 if (!hCatalog
|| hCatalog
== INVALID_HANDLE_VALUE
|| cc
->magic
!= CRYPTCAT_MAGIC
)
654 SetLastError(ERROR_INVALID_PARAMETER
);
657 SetLastError(CRYPT_E_NOT_FOUND
);
661 /***********************************************************************
662 * CryptCATGetCatAttrInfo (WINTRUST.@)
664 CRYPTCATATTRIBUTE
* WINAPI
CryptCATGetCatAttrInfo(HANDLE hCatalog
, LPWSTR tag
)
666 struct cryptcat
*cc
= hCatalog
;
668 FIXME("%p, %s\n", hCatalog
, debugstr_w(tag
));
670 if (!hCatalog
|| hCatalog
== INVALID_HANDLE_VALUE
|| cc
->magic
!= CRYPTCAT_MAGIC
)
672 SetLastError(ERROR_INVALID_PARAMETER
);
675 SetLastError(CRYPT_E_NOT_FOUND
);
679 CRYPTCATMEMBER
* WINAPI
CryptCATGetMemberInfo(HANDLE hCatalog
, LPWSTR tag
)
681 struct cryptcat
*cc
= hCatalog
;
683 FIXME("%p, %s\n", hCatalog
, debugstr_w(tag
));
685 if (!hCatalog
|| hCatalog
== INVALID_HANDLE_VALUE
|| cc
->magic
!= CRYPTCAT_MAGIC
)
687 SetLastError(ERROR_INVALID_PARAMETER
);
690 SetLastError(CRYPT_E_NOT_FOUND
);
694 /***********************************************************************
695 * CryptCATEnumerateAttr (WINTRUST.@)
697 CRYPTCATATTRIBUTE
* WINAPI
CryptCATEnumerateAttr(HANDLE hCatalog
, CRYPTCATMEMBER
*member
, CRYPTCATATTRIBUTE
*prev
)
699 struct cryptcat
*cc
= hCatalog
;
701 FIXME("%p, %p, %p\n", hCatalog
, member
, prev
);
703 if (!hCatalog
|| hCatalog
== INVALID_HANDLE_VALUE
|| cc
->magic
!= CRYPTCAT_MAGIC
)
705 SetLastError(ERROR_INVALID_PARAMETER
);
708 SetLastError(CRYPT_E_NOT_FOUND
);
712 /***********************************************************************
713 * CryptCATEnumerateCatAttr (WINTRUST.@)
715 CRYPTCATATTRIBUTE
* WINAPI
CryptCATEnumerateCatAttr(HANDLE hCatalog
, CRYPTCATATTRIBUTE
*prev
)
717 struct cryptcat
*cc
= hCatalog
;
719 FIXME("%p, %p\n", hCatalog
, prev
);
721 if (!hCatalog
|| hCatalog
== INVALID_HANDLE_VALUE
|| cc
->magic
!= CRYPTCAT_MAGIC
)
723 SetLastError(ERROR_INVALID_PARAMETER
);
726 SetLastError(CRYPT_E_NOT_FOUND
);
730 /***********************************************************************
731 * CryptCATEnumerateMember (WINTRUST.@)
733 CRYPTCATMEMBER
* WINAPI
CryptCATEnumerateMember(HANDLE hCatalog
, CRYPTCATMEMBER
*prev
)
735 struct cryptcat
*cc
= hCatalog
;
736 CRYPTCATMEMBER
*member
= prev
;
740 TRACE("%p, %p\n", hCatalog
, prev
);
742 if (!hCatalog
|| hCatalog
== INVALID_HANDLE_VALUE
|| cc
->magic
!= CRYPTCAT_MAGIC
)
744 SetLastError(ERROR_INVALID_PARAMETER
);
748 /* dumping the contents makes me think that dwReserved is the iteration number */
751 if (!(member
= malloc(sizeof(*member
))))
753 SetLastError(ERROR_OUTOFMEMORY
);
756 member
->cbStruct
= sizeof(*member
);
757 member
->pwszFileName
= member
->pwszReferenceTag
= NULL
;
758 member
->dwReserved
= 0;
759 member
->hReserved
= NULL
;
760 member
->gSubjectType
= cc
->subject
;
761 member
->fdwMemberFlags
= 0;
762 member
->pIndirectData
= NULL
;
763 member
->dwCertVersion
= cc
->inner
->dwVersion
;
765 else member
->dwReserved
++;
767 if (member
->dwReserved
>= cc
->inner
->cCTLEntry
)
769 SetLastError(ERROR_INVALID_PARAMETER
);
773 /* list them backwards, like native */
774 entry
= &cc
->inner
->rgCTLEntry
[cc
->inner
->cCTLEntry
- member
->dwReserved
- 1];
776 member
->sEncodedIndirectData
.cbData
= member
->sEncodedMemberInfo
.cbData
= 0;
777 member
->sEncodedIndirectData
.pbData
= member
->sEncodedMemberInfo
.pbData
= NULL
;
778 free(member
->pIndirectData
);
779 member
->pIndirectData
= NULL
;
781 for (i
= 0; i
< entry
->cAttribute
; i
++)
783 CRYPT_ATTRIBUTE
*attr
= entry
->rgAttribute
+ i
;
785 if (attr
->cValue
!= 1)
787 ERR("Can't handle attr->cValue of %lu\n", attr
->cValue
);
790 if (!strcmp(attr
->pszObjId
, CAT_MEMBERINFO_OBJID
))
795 member
->sEncodedMemberInfo
.cbData
= attr
->rgValue
->cbData
;
796 member
->sEncodedMemberInfo
.pbData
= attr
->rgValue
->pbData
;
798 CryptDecodeObject(cc
->encoding
, CAT_MEMBERINFO_OBJID
, attr
->rgValue
->pbData
, attr
->rgValue
->cbData
, 0, NULL
, &size
);
800 if (!(mi
= malloc(size
)))
802 SetLastError(ERROR_OUTOFMEMORY
);
805 ret
= CryptDecodeObject(cc
->encoding
, CAT_MEMBERINFO_OBJID
, attr
->rgValue
->pbData
, attr
->rgValue
->cbData
, 0, mi
, &size
);
810 member
->dwCertVersion
= mi
->dwCertVersion
;
811 RtlInitUnicodeString(&guid
, mi
->pwszSubjGuid
);
812 if (RtlGUIDFromString(&guid
, &member
->gSubjectType
))
819 if (!ret
) goto error
;
821 else if (!strcmp(attr
->pszObjId
, SPC_INDIRECT_DATA_OBJID
))
823 /* SPC_INDIRECT_DATA_CONTENT is equal to SIP_INDIRECT_DATA */
825 member
->sEncodedIndirectData
.cbData
= attr
->rgValue
->cbData
;
826 member
->sEncodedIndirectData
.pbData
= attr
->rgValue
->pbData
;
828 CryptDecodeObject(cc
->encoding
, SPC_INDIRECT_DATA_OBJID
, attr
->rgValue
->pbData
, attr
->rgValue
->cbData
, 0, NULL
, &size
);
830 if (!(member
->pIndirectData
= malloc(size
)))
832 SetLastError(ERROR_OUTOFMEMORY
);
835 CryptDecodeObject(cc
->encoding
, SPC_INDIRECT_DATA_OBJID
, attr
->rgValue
->pbData
, attr
->rgValue
->cbData
, 0, member
->pIndirectData
, &size
);
838 /* this object id should probably be handled in CryptCATEnumerateAttr */
839 FIXME("unhandled object id \"%s\"\n", attr
->pszObjId
);
842 if (!member
->sEncodedMemberInfo
.cbData
|| !member
->sEncodedIndirectData
.cbData
)
844 ERR("Corrupted catalog entry?\n");
845 SetLastError(CRYPT_E_ATTRIBUTES_MISSING
);
848 size
= (2 * member
->pIndirectData
->Digest
.cbData
+ 1) * sizeof(WCHAR
);
849 member
->pwszReferenceTag
= realloc(member
->pwszReferenceTag
, size
);
851 if (!member
->pwszReferenceTag
)
853 SetLastError(ERROR_OUTOFMEMORY
);
856 /* FIXME: reference tag is usually the file hash but doesn't have to be */
857 for (i
= 0; i
< member
->pIndirectData
->Digest
.cbData
; i
++)
861 sub
= member
->pIndirectData
->Digest
.pbData
[i
] >> 4;
862 member
->pwszReferenceTag
[i
* 2] = (sub
< 10 ? '0' + sub
: 'A' + sub
- 10);
863 sub
= member
->pIndirectData
->Digest
.pbData
[i
] & 0xf;
864 member
->pwszReferenceTag
[i
* 2 + 1] = (sub
< 10 ? '0' + sub
: 'A' + sub
- 10);
866 member
->pwszReferenceTag
[i
* 2] = 0;
870 free(member
->pIndirectData
);
871 free(member
->pwszReferenceTag
);
876 static CTL_INFO
*decode_inner_content(HANDLE hmsg
, DWORD encoding
, DWORD
*len
)
881 CTL_INFO
*inner
= NULL
;
883 if (!CryptMsgGetParam(hmsg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0, NULL
, &size
)) return NULL
;
884 if (!(oid
= malloc(size
)))
886 SetLastError(ERROR_OUTOFMEMORY
);
889 if (!CryptMsgGetParam(hmsg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0, oid
, &size
)) goto out
;
890 if (!CryptMsgGetParam(hmsg
, CMSG_CONTENT_PARAM
, 0, NULL
, &size
)) goto out
;
891 if (!(buffer
= malloc(size
)))
893 SetLastError(ERROR_OUTOFMEMORY
);
896 if (!CryptMsgGetParam(hmsg
, CMSG_CONTENT_PARAM
, 0, buffer
, &size
)) goto out
;
897 if (!CryptDecodeObject(encoding
, oid
, buffer
, size
, 0, NULL
, &size
)) goto out
;
898 if (!(inner
= malloc(size
)))
900 SetLastError(ERROR_OUTOFMEMORY
);
903 if (!CryptDecodeObject(encoding
, oid
, buffer
, size
, 0, inner
, &size
)) goto out
;
912 /***********************************************************************
913 * CryptCATCatalogInfoFromContext (WINTRUST.@)
915 BOOL WINAPI
CryptCATCatalogInfoFromContext(HCATINFO hcatinfo
, CATALOG_INFO
*info
, DWORD flags
)
917 struct catinfo
*ci
= hcatinfo
;
919 TRACE("%p, %p, %lx\n", hcatinfo
, info
, flags
);
921 if (!hcatinfo
|| hcatinfo
== INVALID_HANDLE_VALUE
|| ci
->magic
!= CATINFO_MAGIC
||
922 flags
|| !info
|| info
->cbStruct
!= sizeof(*info
))
924 SetLastError(ERROR_INVALID_PARAMETER
);
927 lstrcpyW(info
->wszCatalogFile
, ci
->file
);
931 /***********************************************************************
932 * CryptCATPutAttrInfo (WINTRUST.@)
934 CRYPTCATATTRIBUTE
* WINAPI
CryptCATPutAttrInfo(HANDLE catalog
, CRYPTCATMEMBER
*member
,
935 WCHAR
*name
, DWORD flags
, DWORD size
, BYTE
*data
)
937 FIXME("catalog %p, member %p, name %s, flags %#lx, size %lu, data %p, stub!\n",
938 catalog
, member
, debugstr_w(name
), flags
, size
, data
);
940 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
944 /***********************************************************************
945 * CryptCATPutCatAttrInfo (WINTRUST.@)
947 CRYPTCATATTRIBUTE
* WINAPI
CryptCATPutCatAttrInfo(HANDLE catalog
,
948 WCHAR
*name
, DWORD flags
, DWORD size
, BYTE
*data
)
950 FIXME("catalog %p, name %s, flags %#lx, size %lu, data %p, stub!\n",
951 catalog
, debugstr_w(name
), flags
, size
, data
);
953 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
957 /***********************************************************************
958 * CryptCATPutMemberInfo (WINTRUST.@)
960 CRYPTCATMEMBER
* WINAPI
CryptCATPutMemberInfo(HANDLE catalog
, WCHAR
*filename
,
961 WCHAR
*member
, GUID
*subject
, DWORD version
, DWORD size
, BYTE
*data
)
963 FIXME("catalog %p, filename %s, member %s, subject %s, version %lu, size %lu, data %p, stub!\n",
964 catalog
, debugstr_w(filename
), debugstr_w(member
), debugstr_guid(subject
), version
, size
, data
);
966 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
970 /***********************************************************************
971 * CryptCATPersistStore (WINTRUST.@)
973 BOOL WINAPI
CryptCATPersistStore(HANDLE catalog
)
975 FIXME("catalog %p, stub!\n", catalog
);
977 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
981 /***********************************************************************
982 * CryptCATOpen (WINTRUST.@)
984 HANDLE WINAPI
CryptCATOpen(WCHAR
*filename
, DWORD flags
, HCRYPTPROV hProv
,
985 DWORD dwPublicVersion
, DWORD dwEncodingType
)
989 DWORD size
, open_mode
= OPEN_ALWAYS
;
993 TRACE("filename %s, flags %#lx, provider %#Ix, version %#lx, type %#lx\n",
994 debugstr_w(filename
), flags
, hProv
, dwPublicVersion
, dwEncodingType
);
998 SetLastError(ERROR_INVALID_PARAMETER
);
999 return INVALID_HANDLE_VALUE
;
1002 if (!dwEncodingType
) dwEncodingType
= X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
;
1004 if (flags
== CRYPTCAT_OPEN_EXISTING
)
1005 open_mode
= OPEN_EXISTING
;
1006 if (flags
& CRYPTCAT_OPEN_CREATENEW
)
1007 open_mode
= CREATE_ALWAYS
;
1009 file
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, open_mode
, 0, NULL
);
1010 if (file
== INVALID_HANDLE_VALUE
) return INVALID_HANDLE_VALUE
;
1012 size
= GetFileSize(file
, NULL
);
1013 if (!(buffer
= malloc(size
)))
1016 SetLastError(ERROR_OUTOFMEMORY
);
1017 return INVALID_HANDLE_VALUE
;
1019 if (!(hmsg
= CryptMsgOpenToDecode(dwEncodingType
, 0, 0, hProv
, NULL
, NULL
)))
1023 return INVALID_HANDLE_VALUE
;
1025 if (!size
) valid
= FALSE
;
1026 else if (!ReadFile(file
, buffer
, size
, &size
, NULL
))
1030 CryptMsgClose(hmsg
);
1031 return INVALID_HANDLE_VALUE
;
1033 else valid
= CryptMsgUpdate(hmsg
, buffer
, size
, TRUE
);
1037 size
= sizeof(DWORD
);
1038 if (!(cc
= calloc(1, sizeof(*cc
))))
1040 CryptMsgClose(hmsg
);
1041 SetLastError(ERROR_OUTOFMEMORY
);
1042 return INVALID_HANDLE_VALUE
;
1046 cc
->encoding
= dwEncodingType
;
1049 cc
->magic
= CRYPTCAT_MAGIC
;
1050 SetLastError(ERROR_SUCCESS
);
1053 else if (CryptMsgGetParam(hmsg
, CMSG_ATTR_CERT_COUNT_PARAM
, 0, &cc
->attr_count
, &size
))
1058 for (i
= 0; i
< cc
->attr_count
; i
++)
1060 if (!CryptMsgGetParam(hmsg
, CMSG_ATTR_CERT_PARAM
, i
, NULL
, &size
))
1062 CryptMsgClose(hmsg
);
1064 return INVALID_HANDLE_VALUE
;
1068 if (!(cc
->attr
= malloc(sizeof(*cc
->attr
) * cc
->attr_count
+ sum
)))
1070 CryptMsgClose(hmsg
);
1072 SetLastError(ERROR_OUTOFMEMORY
);
1073 return INVALID_HANDLE_VALUE
;
1075 p
= (BYTE
*)(cc
->attr
+ cc
->attr_count
);
1076 for (i
= 0; i
< cc
->attr_count
; i
++)
1078 if (!CryptMsgGetParam(hmsg
, CMSG_ATTR_CERT_PARAM
, i
, NULL
, &size
))
1080 CryptMsgClose(hmsg
);
1083 return INVALID_HANDLE_VALUE
;
1085 if (!CryptMsgGetParam(hmsg
, CMSG_ATTR_CERT_PARAM
, i
, p
, &size
))
1087 CryptMsgClose(hmsg
);
1090 return INVALID_HANDLE_VALUE
;
1094 cc
->inner
= decode_inner_content(hmsg
, dwEncodingType
, &cc
->inner_len
);
1095 if (!cc
->inner
|| !CryptSIPRetrieveSubjectGuid(filename
, NULL
, &cc
->subject
))
1097 CryptMsgClose(hmsg
);
1101 return INVALID_HANDLE_VALUE
;
1103 cc
->magic
= CRYPTCAT_MAGIC
;
1104 SetLastError(ERROR_SUCCESS
);
1108 return INVALID_HANDLE_VALUE
;
1111 /***********************************************************************
1112 * CryptSIPCreateIndirectData (WINTRUST.@)
1114 BOOL WINAPI
CryptSIPCreateIndirectData(SIP_SUBJECTINFO
* pSubjectInfo
, DWORD
* pcbIndirectData
,
1115 SIP_INDIRECT_DATA
* pIndirectData
)
1117 FIXME("(%p %p %p) stub\n", pSubjectInfo
, pcbIndirectData
, pIndirectData
);
1123 /***********************************************************************
1124 * CryptCATCDFClose (WINTRUST.@)
1126 BOOL WINAPI
CryptCATCDFClose(CRYPTCATCDF
*pCDF
)
1128 FIXME("(%p) stub\n", pCDF
);
1133 /***********************************************************************
1134 * CryptCATCDFEnumCatAttributes (WINTRUST.@)
1136 CRYPTCATATTRIBUTE
* WINAPI
CryptCATCDFEnumCatAttributes(CRYPTCATCDF
*pCDF
,
1137 CRYPTCATATTRIBUTE
*pPrevAttr
,
1138 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError
)
1140 FIXME("(%p %p %p) stub\n", pCDF
, pPrevAttr
, pfnParseError
);
1145 /***********************************************************************
1146 * CryptCATCDFEnumMembersByCDFTagEx (WINTRUST.@)
1148 LPWSTR WINAPI
CryptCATCDFEnumMembersByCDFTagEx(CRYPTCATCDF
*pCDF
, LPWSTR pwszPrevCDFTag
,
1149 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError
,
1150 CRYPTCATMEMBER
**ppMember
, BOOL fContinueOnError
,
1153 FIXME("(%p %s %p %p %d %p) stub\n", pCDF
, debugstr_w(pwszPrevCDFTag
), pfnParseError
,
1154 ppMember
, fContinueOnError
, pvReserved
);
1159 /***********************************************************************
1160 * CryptCATCDFOpen (WINTRUST.@)
1162 CRYPTCATCDF
* WINAPI
CryptCATCDFOpen(LPWSTR pwszFilePath
,
1163 PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError
)
1165 FIXME("(%s %p) stub\n", debugstr_w(pwszFilePath
), pfnParseError
);
1170 static BOOL
WINTRUST_GetSignedMsgFromPEFile(SIP_SUBJECTINFO
*pSubjectInfo
,
1171 DWORD
*pdwEncodingType
, DWORD dwIndex
, DWORD
*pcbSignedDataMsg
,
1172 BYTE
*pbSignedDataMsg
)
1175 WIN_CERTIFICATE
*pCert
= NULL
;
1178 TRACE("(%p %p %ld %p %p)\n", pSubjectInfo
, pdwEncodingType
, dwIndex
,
1179 pcbSignedDataMsg
, pbSignedDataMsg
);
1181 if(pSubjectInfo
->hFile
&& pSubjectInfo
->hFile
!=INVALID_HANDLE_VALUE
)
1182 file
= pSubjectInfo
->hFile
;
1185 file
= CreateFileW(pSubjectInfo
->pwsFileName
, GENERIC_READ
,
1186 FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, NULL
);
1187 if(file
== INVALID_HANDLE_VALUE
)
1191 if (!pbSignedDataMsg
)
1193 WIN_CERTIFICATE cert
;
1195 /* app hasn't passed buffer, just get the length */
1196 ret
= ImageGetCertificateHeader(file
, dwIndex
, &cert
);
1199 switch (cert
.wCertificateType
)
1201 case WIN_CERT_TYPE_X509
:
1202 case WIN_CERT_TYPE_PKCS_SIGNED_DATA
:
1203 *pcbSignedDataMsg
= cert
.dwLength
;
1206 WARN("unknown certificate type %d\n", cert
.wCertificateType
);
1215 ret
= ImageGetCertificateData(file
, dwIndex
, NULL
, &len
);
1216 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
1218 pCert
= malloc(len
);
1224 ret
= ImageGetCertificateData(file
, dwIndex
, pCert
, &len
);
1227 pCert
->dwLength
-= FIELD_OFFSET(WIN_CERTIFICATE
, bCertificate
);
1228 if (*pcbSignedDataMsg
< pCert
->dwLength
)
1230 *pcbSignedDataMsg
= pCert
->dwLength
;
1231 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1236 memcpy(pbSignedDataMsg
, pCert
->bCertificate
, pCert
->dwLength
);
1237 *pcbSignedDataMsg
= pCert
->dwLength
;
1238 switch (pCert
->wCertificateType
)
1240 case WIN_CERT_TYPE_X509
:
1241 *pdwEncodingType
= X509_ASN_ENCODING
;
1243 case WIN_CERT_TYPE_PKCS_SIGNED_DATA
:
1244 *pdwEncodingType
= X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
;
1247 WARN("don't know what to do for encoding type %d\n",
1248 pCert
->wCertificateType
);
1249 *pdwEncodingType
= 0;
1255 if(pSubjectInfo
->hFile
!= file
)
1261 static BOOL
WINTRUST_PutSignedMsgToPEFile(SIP_SUBJECTINFO
* pSubjectInfo
, DWORD pdwEncodingType
,
1262 DWORD
* pdwIndex
, DWORD cbSignedDataMsg
, BYTE
* pbSignedDataMsg
)
1264 WIN_CERTIFICATE
*cert
;
1269 if(pSubjectInfo
->hFile
&& pSubjectInfo
->hFile
!=INVALID_HANDLE_VALUE
)
1270 file
= pSubjectInfo
->hFile
;
1273 file
= CreateFileW(pSubjectInfo
->pwsFileName
, GENERIC_READ
|GENERIC_WRITE
,
1274 FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, NULL
);
1275 if(file
== INVALID_HANDLE_VALUE
)
1279 /* int aligned WIN_CERTIFICATE structure with cbSignedDataMsg+1 bytes of data */
1280 size
= FIELD_OFFSET(WIN_CERTIFICATE
, bCertificate
[cbSignedDataMsg
+4]) & (~3);
1281 cert
= calloc(1, size
);
1285 cert
->dwLength
= size
;
1286 cert
->wRevision
= WIN_CERT_REVISION_2_0
;
1287 cert
->wCertificateType
= WIN_CERT_TYPE_PKCS_SIGNED_DATA
;
1288 memcpy(cert
->bCertificate
, pbSignedDataMsg
, cbSignedDataMsg
);
1289 ret
= ImageAddCertificate(file
, cert
, pdwIndex
);
1292 if(file
!= pSubjectInfo
->hFile
)
1297 /* structure offsets */
1298 #define cfhead_Signature (0x00)
1299 #define cfhead_CabinetSize (0x08)
1300 #define cfhead_MinorVersion (0x18)
1301 #define cfhead_MajorVersion (0x19)
1302 #define cfhead_Flags (0x1E)
1303 #define cfhead_SIZEOF (0x24)
1304 #define cfheadext_HeaderReserved (0x00)
1305 #define cfheadext_SIZEOF (0x04)
1306 #define cfsigninfo_CertOffset (0x04)
1307 #define cfsigninfo_CertSize (0x08)
1308 #define cfsigninfo_SIZEOF (0x0C)
1311 #define cfheadRESERVE_PRESENT (0x0004)
1313 /* endian-neutral reading of little-endian data */
1314 #define EndGetI32(a) ((((a)[3])<<24)|(((a)[2])<<16)|(((a)[1])<<8)|((a)[0]))
1315 #define EndGetI16(a) ((((a)[1])<<8)|((a)[0]))
1317 /* For documentation purposes only: this is the structure in the reserved
1318 * area of a signed cabinet file. The cert offset indicates where in the
1319 * cabinet file the signature resides, and the count indicates its size.
1321 typedef struct _CAB_SIGNINFO
1323 WORD unk0
; /* always 0? */
1324 WORD unk1
; /* always 0x0010? */
1327 } CAB_SIGNINFO
, *PCAB_SIGNINFO
;
1329 static BOOL
WINTRUST_GetSignedMsgFromCabFile(SIP_SUBJECTINFO
*pSubjectInfo
,
1330 DWORD
*pdwEncodingType
, DWORD dwIndex
, DWORD
*pcbSignedDataMsg
,
1331 BYTE
*pbSignedDataMsg
)
1334 LONG base_offset
, cabsize
;
1337 DWORD cert_offset
, cert_size
, dwRead
;
1339 TRACE("(%p %p %ld %p %p)\n", pSubjectInfo
, pdwEncodingType
, dwIndex
,
1340 pcbSignedDataMsg
, pbSignedDataMsg
);
1342 /* get basic offset & size info */
1343 base_offset
= SetFilePointer(pSubjectInfo
->hFile
, 0L, NULL
, SEEK_CUR
);
1345 if (SetFilePointer(pSubjectInfo
->hFile
, 0, NULL
, SEEK_END
) == INVALID_SET_FILE_POINTER
)
1347 TRACE("seek error\n");
1351 cabsize
= SetFilePointer(pSubjectInfo
->hFile
, 0L, NULL
, SEEK_CUR
);
1352 if ((cabsize
== -1) || (base_offset
== -1) ||
1353 (SetFilePointer(pSubjectInfo
->hFile
, 0, NULL
, SEEK_SET
) == INVALID_SET_FILE_POINTER
))
1355 TRACE("seek error\n");
1359 /* read in the CFHEADER */
1360 if (!ReadFile(pSubjectInfo
->hFile
, buf
, cfhead_SIZEOF
, &dwRead
, NULL
) ||
1361 dwRead
!= cfhead_SIZEOF
)
1363 TRACE("reading header failed\n");
1367 /* check basic MSCF signature */
1368 if (EndGetI32(buf
+cfhead_Signature
) != 0x4643534d)
1370 WARN("cabinet signature not present\n");
1374 /* Ignore the number of folders and files and the set and cabinet IDs */
1376 /* check the header revision */
1377 if ((buf
[cfhead_MajorVersion
] > 1) ||
1378 (buf
[cfhead_MajorVersion
] == 1 && buf
[cfhead_MinorVersion
] > 3))
1380 WARN("cabinet format version > 1.3\n");
1384 /* pull the flags out */
1385 flags
= EndGetI16(buf
+cfhead_Flags
);
1387 if (!(flags
& cfheadRESERVE_PRESENT
))
1389 TRACE("no header present, not signed\n");
1393 if (!ReadFile(pSubjectInfo
->hFile
, buf
, cfheadext_SIZEOF
, &dwRead
, NULL
) ||
1394 dwRead
!= cfheadext_SIZEOF
)
1396 ERR("bunk reserve-sizes?\n");
1400 header_resv
= EndGetI16(buf
+cfheadext_HeaderReserved
);
1403 TRACE("no header_resv, not signed\n");
1406 else if (header_resv
< cfsigninfo_SIZEOF
)
1408 TRACE("header_resv too small, not signed\n");
1412 if (header_resv
> 60000)
1414 WARN("WARNING; header reserved space > 60000\n");
1417 if (!ReadFile(pSubjectInfo
->hFile
, buf
, cfsigninfo_SIZEOF
, &dwRead
, NULL
) ||
1418 dwRead
!= cfsigninfo_SIZEOF
)
1420 ERR("couldn't read reserve\n");
1424 cert_offset
= EndGetI32(buf
+cfsigninfo_CertOffset
);
1425 TRACE("cert_offset: %ld\n", cert_offset
);
1426 cert_size
= EndGetI32(buf
+cfsigninfo_CertSize
);
1427 TRACE("cert_size: %ld\n", cert_size
);
1429 /* The redundant checks are to avoid wraparound */
1430 if (cert_offset
> cabsize
|| cert_size
> cabsize
||
1431 cert_offset
+ cert_size
> cabsize
)
1433 WARN("offset beyond file, not attempting to read\n");
1437 SetFilePointer(pSubjectInfo
->hFile
, base_offset
, NULL
, SEEK_SET
);
1438 if (!pbSignedDataMsg
)
1440 *pcbSignedDataMsg
= cert_size
;
1443 if (*pcbSignedDataMsg
< cert_size
)
1445 *pcbSignedDataMsg
= cert_size
;
1446 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1449 if (SetFilePointer(pSubjectInfo
->hFile
, cert_offset
, NULL
, SEEK_SET
) == INVALID_SET_FILE_POINTER
)
1451 ERR("couldn't seek to cert location\n");
1454 if (!ReadFile(pSubjectInfo
->hFile
, pbSignedDataMsg
, cert_size
, &dwRead
,
1455 NULL
) || dwRead
!= cert_size
)
1457 ERR("couldn't read cert\n");
1458 SetFilePointer(pSubjectInfo
->hFile
, base_offset
, NULL
, SEEK_SET
);
1461 /* The encoding of the files I've seen appears to be in ASN.1
1462 * format, and there isn't a field indicating the type, so assume it
1465 *pdwEncodingType
= X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
;
1466 /* Restore base offset */
1467 SetFilePointer(pSubjectInfo
->hFile
, base_offset
, NULL
, SEEK_SET
);
1471 static BOOL
WINTRUST_GetSignedMsgFromCatFile(SIP_SUBJECTINFO
*pSubjectInfo
,
1472 DWORD
*pdwEncodingType
, DWORD dwIndex
, DWORD
*pcbSignedDataMsg
,
1473 BYTE
*pbSignedDataMsg
)
1477 TRACE("(%p %p %ld %p %p)\n", pSubjectInfo
, pdwEncodingType
, dwIndex
,
1478 pcbSignedDataMsg
, pbSignedDataMsg
);
1480 if (!pbSignedDataMsg
)
1482 *pcbSignedDataMsg
= GetFileSize(pSubjectInfo
->hFile
, NULL
);
1487 DWORD len
= GetFileSize(pSubjectInfo
->hFile
, NULL
);
1489 if (*pcbSignedDataMsg
< len
)
1491 *pcbSignedDataMsg
= len
;
1492 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1497 ret
= ReadFile(pSubjectInfo
->hFile
, pbSignedDataMsg
, len
,
1498 pcbSignedDataMsg
, NULL
);
1500 *pdwEncodingType
= X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
;
1506 /* GUIDs used by CryptSIPGetSignedDataMsg and CryptSIPPutSignedDataMsg */
1507 static const GUID unknown
= { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,
1508 0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
1509 static const GUID cabGUID
= { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,
1510 0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
1511 static const GUID catGUID
= { 0xDE351A43, 0x8E59, 0x11D0, { 0x8C,0x47,
1512 0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
1514 /***********************************************************************
1515 * CryptSIPGetSignedDataMsg (WINTRUST.@)
1517 BOOL WINAPI
CryptSIPGetSignedDataMsg(SIP_SUBJECTINFO
* pSubjectInfo
, DWORD
* pdwEncodingType
,
1518 DWORD dwIndex
, DWORD
* pcbSignedDataMsg
, BYTE
* pbSignedDataMsg
)
1522 TRACE("(%p %p %ld %p %p)\n", pSubjectInfo
, pdwEncodingType
, dwIndex
,
1523 pcbSignedDataMsg
, pbSignedDataMsg
);
1527 SetLastError(ERROR_INVALID_PARAMETER
);
1531 if (!memcmp(pSubjectInfo
->pgSubjectType
, &unknown
, sizeof(unknown
)))
1532 ret
= WINTRUST_GetSignedMsgFromPEFile(pSubjectInfo
, pdwEncodingType
,
1533 dwIndex
, pcbSignedDataMsg
, pbSignedDataMsg
);
1534 else if (!memcmp(pSubjectInfo
->pgSubjectType
, &cabGUID
, sizeof(cabGUID
)))
1535 ret
= WINTRUST_GetSignedMsgFromCabFile(pSubjectInfo
, pdwEncodingType
,
1536 dwIndex
, pcbSignedDataMsg
, pbSignedDataMsg
);
1537 else if (!memcmp(pSubjectInfo
->pgSubjectType
, &catGUID
, sizeof(catGUID
)))
1538 ret
= WINTRUST_GetSignedMsgFromCatFile(pSubjectInfo
, pdwEncodingType
,
1539 dwIndex
, pcbSignedDataMsg
, pbSignedDataMsg
);
1542 FIXME("unimplemented for subject type %s\n",
1543 debugstr_guid(pSubjectInfo
->pgSubjectType
));
1547 TRACE("returning %d\n", ret
);
1551 /***********************************************************************
1552 * CryptSIPPutSignedDataMsg (WINTRUST.@)
1554 BOOL WINAPI
CryptSIPPutSignedDataMsg(SIP_SUBJECTINFO
* pSubjectInfo
, DWORD pdwEncodingType
,
1555 DWORD
* pdwIndex
, DWORD cbSignedDataMsg
, BYTE
* pbSignedDataMsg
)
1557 TRACE("(%p %ld %p %ld %p)\n", pSubjectInfo
, pdwEncodingType
, pdwIndex
,
1558 cbSignedDataMsg
, pbSignedDataMsg
);
1561 SetLastError(ERROR_INVALID_PARAMETER
);
1565 if(!memcmp(pSubjectInfo
->pgSubjectType
, &unknown
, sizeof(unknown
)))
1566 return WINTRUST_PutSignedMsgToPEFile(pSubjectInfo
, pdwEncodingType
,
1567 pdwIndex
, cbSignedDataMsg
, pbSignedDataMsg
);
1569 FIXME("unimplemented for subject type %s\n",
1570 debugstr_guid(pSubjectInfo
->pgSubjectType
));
1575 /***********************************************************************
1576 * CryptSIPRemoveSignedDataMsg (WINTRUST.@)
1578 BOOL WINAPI
CryptSIPRemoveSignedDataMsg(SIP_SUBJECTINFO
* pSubjectInfo
,
1581 FIXME("(%p %ld) stub\n", pSubjectInfo
, dwIndex
);
1586 /***********************************************************************
1587 * CryptSIPVerifyIndirectData (WINTRUST.@)
1589 BOOL WINAPI
CryptSIPVerifyIndirectData(SIP_SUBJECTINFO
* pSubjectInfo
,
1590 SIP_INDIRECT_DATA
* pIndirectData
)
1592 FIXME("(%p %p) stub\n", pSubjectInfo
, pIndirectData
);