4 * Copyright 1998 Patrik Stridvall
5 * Copyright 2003 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
36 * These functions are partially documented at:
37 * http://www.cs.auckland.ac.nz/~pgut001/pubs/authenticode.txt
40 /***********************************************************************
41 * IMAGEHLP_GetSecurityDirOffset (INTERNAL)
43 * Read a file's PE header, and return the offset and size of the
46 static BOOL IMAGEHLP_GetSecurityDirOffset( HANDLE handle,
47 DWORD *pdwOfs, DWORD *pdwSize )
49 IMAGE_DOS_HEADER dos_hdr;
50 IMAGE_NT_HEADERS nt_hdr;
53 IMAGE_DATA_DIRECTORY *sd;
55 TRACE("handle %p\n", handle );
57 /* read the DOS header */
58 count = SetFilePointer( handle, 0, NULL, FILE_BEGIN );
59 if( count == INVALID_SET_FILE_POINTER )
62 r = ReadFile( handle, &dos_hdr, sizeof dos_hdr, &count, NULL );
65 if( count != sizeof dos_hdr )
68 /* read the PE header */
69 count = SetFilePointer( handle, dos_hdr.e_lfanew, NULL, FILE_BEGIN );
70 if( count == INVALID_SET_FILE_POINTER )
73 r = ReadFile( handle, &nt_hdr, sizeof nt_hdr, &count, NULL );
76 if( count != sizeof nt_hdr )
79 sd = &nt_hdr.OptionalHeader.
80 DataDirectory[IMAGE_FILE_SECURITY_DIRECTORY];
82 TRACE("size = %x addr = %x\n", sd->Size, sd->VirtualAddress);
84 *pdwOfs = sd->VirtualAddress;
89 /***********************************************************************
90 * IMAGEHLP_GetCertificateOffset (INTERNAL)
92 * Read a file's PE header, and return the offset and size of the
95 static BOOL IMAGEHLP_GetCertificateOffset( HANDLE handle, DWORD num,
96 DWORD *pdwOfs, DWORD *pdwSize )
98 DWORD size, count, offset, len, sd_VirtualAddr;
101 r = IMAGEHLP_GetSecurityDirOffset( handle, &sd_VirtualAddr, &size );
106 /* take the n'th certificate */
109 /* read the length of the current certificate */
110 count = SetFilePointer( handle, sd_VirtualAddr + offset,
112 if( count == INVALID_SET_FILE_POINTER )
114 r = ReadFile( handle, &len, sizeof len, &count, NULL );
117 if( count != sizeof len )
120 /* check the certificate is not too big or too small */
121 if( len < sizeof len )
123 if( len > (size-offset) )
128 /* calculate the offset of the next certificate */
134 *pdwOfs = sd_VirtualAddr + offset;
137 TRACE("len = %x addr = %x\n", len, sd_VirtualAddr + offset);
143 /***********************************************************************
144 * ImageAddCertificate (IMAGEHLP.@)
147 BOOL WINAPI ImageAddCertificate(
148 HANDLE FileHandle, PWIN_CERTIFICATE Certificate, PDWORD Index)
150 FIXME("(%p, %p, %p): stub\n",
151 FileHandle, Certificate, Index
153 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
157 /***********************************************************************
158 * ImageEnumerateCertificates (IMAGEHLP.@)
160 BOOL WINAPI ImageEnumerateCertificates(
161 HANDLE handle, WORD TypeFilter, PDWORD CertificateCount,
162 PDWORD Indices, DWORD IndexCount)
164 DWORD size, count, offset, sd_VirtualAddr;
166 const size_t cert_hdr_size = sizeof hdr - sizeof hdr.bCertificate;
169 TRACE("%p %hd %p %p %d\n",
170 handle, TypeFilter, CertificateCount, Indices, IndexCount);
174 FIXME("Indices not handled!\n");
178 r = IMAGEHLP_GetSecurityDirOffset( handle, &sd_VirtualAddr, &size );
183 *CertificateCount = 0;
184 while( offset < size )
186 /* read the length of the current certificate */
187 count = SetFilePointer( handle, sd_VirtualAddr + offset,
189 if( count == INVALID_SET_FILE_POINTER )
191 r = ReadFile( handle, &hdr, cert_hdr_size, &count, NULL );
194 if( count != cert_hdr_size )
197 TRACE("Size = %08x id = %08hx\n",
198 hdr.dwLength, hdr.wCertificateType );
200 /* check the certificate is not too big or too small */
201 if( hdr.dwLength < cert_hdr_size )
203 if( hdr.dwLength > (size-offset) )
206 if( (TypeFilter == CERT_SECTION_TYPE_ANY) ||
207 (TypeFilter == hdr.wCertificateType) )
209 (*CertificateCount)++;
212 /* next certificate */
213 offset += hdr.dwLength;
219 /***********************************************************************
220 * ImageGetCertificateData (IMAGEHLP.@)
222 * FIXME: not sure that I'm dealing with the Index the right way
224 BOOL WINAPI ImageGetCertificateData(
225 HANDLE handle, DWORD Index,
226 PWIN_CERTIFICATE Certificate, PDWORD RequiredLength)
228 DWORD r, offset, ofs, size, count;
230 TRACE("%p %d %p %p\n", handle, Index, Certificate, RequiredLength);
232 if( !IMAGEHLP_GetCertificateOffset( handle, Index, &ofs, &size ) )
237 *RequiredLength = size;
241 if( *RequiredLength < size )
243 *RequiredLength = size;
244 SetLastError( ERROR_INSUFFICIENT_BUFFER );
248 *RequiredLength = size;
250 offset = SetFilePointer( handle, ofs, NULL, FILE_BEGIN );
251 if( offset == INVALID_SET_FILE_POINTER )
254 r = ReadFile( handle, Certificate, size, &count, NULL );
265 /***********************************************************************
266 * ImageGetCertificateHeader (IMAGEHLP.@)
268 BOOL WINAPI ImageGetCertificateHeader(
269 HANDLE handle, DWORD index, PWIN_CERTIFICATE pCert)
271 DWORD r, offset, ofs, size, count;
272 const size_t cert_hdr_size = sizeof *pCert - sizeof pCert->bCertificate;
274 TRACE("%p %d %p\n", handle, index, pCert);
276 if( !IMAGEHLP_GetCertificateOffset( handle, index, &ofs, &size ) )
279 if( size < cert_hdr_size )
282 offset = SetFilePointer( handle, ofs, NULL, FILE_BEGIN );
283 if( offset == INVALID_SET_FILE_POINTER )
286 r = ReadFile( handle, pCert, cert_hdr_size, &count, NULL );
289 if( count != cert_hdr_size )
297 /***********************************************************************
298 * ImageGetDigestStream (IMAGEHLP.@)
300 BOOL WINAPI ImageGetDigestStream(
301 HANDLE FileHandle, DWORD DigestLevel,
302 DIGEST_FUNCTION DigestFunction, DIGEST_HANDLE DigestHandle)
304 FIXME("(%p, %d, %p, %p): stub\n",
305 FileHandle, DigestLevel, DigestFunction, DigestHandle
307 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
311 /***********************************************************************
312 * ImageRemoveCertificate (IMAGEHLP.@)
314 BOOL WINAPI ImageRemoveCertificate(HANDLE FileHandle, DWORD Index)
316 FIXME("(%p, %d): stub\n", FileHandle, Index);
317 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);