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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
37 * These functions are partially documented at:
38 * http://www.cs.auckland.ac.nz/~pgut001/pubs/authenticode.txt
41 /***********************************************************************
42 * IMAGEHLP_GetSecurityDirOffset (INTERNAL)
44 * Read a file's PE header, and return the offset and size of the
47 static BOOL IMAGEHLP_GetSecurityDirOffset( HANDLE handle, DWORD num,
48 DWORD *pdwOfs, DWORD *pdwSize )
50 IMAGE_DOS_HEADER dos_hdr;
51 IMAGE_NT_HEADERS nt_hdr;
52 DWORD size, count, offset, len;
54 IMAGE_DATA_DIRECTORY *sd;
56 TRACE("handle %p\n", handle );
58 /* read the DOS header */
59 count = SetFilePointer( handle, 0, NULL, FILE_BEGIN );
60 if( count == INVALID_SET_FILE_POINTER )
63 r = ReadFile( handle, &dos_hdr, sizeof dos_hdr, &count, NULL );
66 if( count != sizeof dos_hdr )
69 /* read the PE header */
70 count = SetFilePointer( handle, dos_hdr.e_lfanew, NULL, FILE_BEGIN );
71 if( count == INVALID_SET_FILE_POINTER )
74 r = ReadFile( handle, &nt_hdr, sizeof nt_hdr, &count, NULL );
77 if( count != sizeof nt_hdr )
80 sd = &nt_hdr.OptionalHeader.
81 DataDirectory[IMAGE_FILE_SECURITY_DIRECTORY];
83 TRACE("len = %lx addr = %lx\n", sd->Size, sd->VirtualAddress);
88 /* take the n'th certificate */
91 /* read the length of the current certificate */
92 count = SetFilePointer( handle, sd->VirtualAddress + offset,
94 if( count == INVALID_SET_FILE_POINTER )
96 r = ReadFile( handle, &len, sizeof len, &count, NULL );
99 if( count != sizeof len )
102 /* check the certificate is not too big or too small */
103 if( len < sizeof len )
105 if( len > (size-offset) )
110 /* calculate the offset of the next certificate */
116 *pdwOfs = sd->VirtualAddress + offset;
119 TRACE("len = %lx addr = %lx\n", len, sd->VirtualAddress + offset);
125 /***********************************************************************
126 * ImageAddCertificate (IMAGEHLP.@)
129 BOOL WINAPI ImageAddCertificate(
130 HANDLE FileHandle, PWIN_CERTIFICATE Certificate, PDWORD Index)
132 FIXME("(%p, %p, %p): stub\n",
133 FileHandle, Certificate, Index
135 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
139 /***********************************************************************
140 * ImageEnumerateCertificates (IMAGEHLP.@)
142 BOOL WINAPI ImageEnumerateCertificates(
143 HANDLE FileHandle, WORD TypeFilter, PDWORD CertificateCount,
144 PDWORD Indices, DWORD IndexCount)
146 FIXME("(%p, %hd, %p, %p, %ld): stub\n",
147 FileHandle, TypeFilter, CertificateCount, Indices, IndexCount
149 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
153 /***********************************************************************
154 * ImageGetCertificateData (IMAGEHLP.@)
156 * FIXME: not sure that I'm dealing with the Index the right way
158 BOOL WINAPI ImageGetCertificateData(
159 HANDLE handle, DWORD Index,
160 PWIN_CERTIFICATE Certificate, PDWORD RequiredLength)
162 DWORD r, offset, ofs, size, count;
164 TRACE("%p %ld %p %p\n", handle, Index, Certificate, RequiredLength);
166 if( !IMAGEHLP_GetSecurityDirOffset( handle, Index, &ofs, &size ) )
171 *RequiredLength = size;
175 if( *RequiredLength < size )
177 *RequiredLength = size;
178 SetLastError( ERROR_INSUFFICIENT_BUFFER );
182 *RequiredLength = size;
184 offset = SetFilePointer( handle, ofs, NULL, FILE_BEGIN );
185 if( offset == INVALID_SET_FILE_POINTER )
188 r = ReadFile( handle, Certificate, size, &count, NULL );
199 /***********************************************************************
200 * ImageGetCertificateHeader (IMAGEHLP.@)
202 BOOL WINAPI ImageGetCertificateHeader(
203 HANDLE FileHandle, DWORD CertificateIndex,
204 PWIN_CERTIFICATE Certificateheader)
206 FIXME("(%p, %ld, %p): stub\n",
207 FileHandle, CertificateIndex, Certificateheader
209 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
213 /***********************************************************************
214 * ImageGetDigestStream (IMAGEHLP.@)
216 BOOL WINAPI ImageGetDigestStream(
217 HANDLE FileHandle, DWORD DigestLevel,
218 DIGEST_FUNCTION DigestFunction, DIGEST_HANDLE DigestHandle)
220 FIXME("(%p, %ld, %p, %p): stub\n",
221 FileHandle, DigestLevel, DigestFunction, DigestHandle
223 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
227 /***********************************************************************
228 * ImageRemoveCertificate (IMAGEHLP.@)
230 BOOL WINAPI ImageRemoveCertificate(HANDLE FileHandle, DWORD Index)
232 FIXME("(%p, %ld): stub\n", FileHandle, Index);
233 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);