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
,
48 DWORD
*pdwOfs
, DWORD
*pdwSize
)
50 IMAGE_DOS_HEADER dos_hdr
;
51 IMAGE_NT_HEADERS nt_hdr
;
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("size = %lx addr = %lx\n", sd
->Size
, sd
->VirtualAddress
);
85 *pdwOfs
= sd
->VirtualAddress
;
90 /***********************************************************************
91 * IMAGEHLP_GetCertificateOffset (INTERNAL)
93 * Read a file's PE header, and return the offset and size of the
96 static BOOL
IMAGEHLP_GetCertificateOffset( HANDLE handle
, DWORD num
,
97 DWORD
*pdwOfs
, DWORD
*pdwSize
)
99 DWORD size
, count
, offset
, len
, sd_VirtualAddr
;
102 r
= IMAGEHLP_GetSecurityDirOffset( handle
, &sd_VirtualAddr
, &size
);
107 /* take the n'th certificate */
110 /* read the length of the current certificate */
111 count
= SetFilePointer( handle
, sd_VirtualAddr
+ offset
,
113 if( count
== INVALID_SET_FILE_POINTER
)
115 r
= ReadFile( handle
, &len
, sizeof len
, &count
, NULL
);
118 if( count
!= sizeof len
)
121 /* check the certificate is not too big or too small */
122 if( len
< sizeof len
)
124 if( len
> (size
-offset
) )
129 /* calculate the offset of the next certificate */
135 *pdwOfs
= sd_VirtualAddr
+ offset
;
138 TRACE("len = %lx addr = %lx\n", len
, sd_VirtualAddr
+ offset
);
144 /***********************************************************************
145 * ImageAddCertificate (IMAGEHLP.@)
148 BOOL WINAPI
ImageAddCertificate(
149 HANDLE FileHandle
, PWIN_CERTIFICATE Certificate
, PDWORD Index
)
151 FIXME("(%p, %p, %p): stub\n",
152 FileHandle
, Certificate
, Index
154 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
158 /***********************************************************************
159 * ImageEnumerateCertificates (IMAGEHLP.@)
161 BOOL WINAPI
ImageEnumerateCertificates(
162 HANDLE handle
, WORD TypeFilter
, PDWORD CertificateCount
,
163 PDWORD Indices
, DWORD IndexCount
)
165 DWORD size
, count
, offset
, sd_VirtualAddr
;
167 const size_t cert_hdr_size
= sizeof hdr
- sizeof hdr
.bCertificate
;
170 TRACE("%p %hd %p %p %ld\n",
171 handle
, TypeFilter
, CertificateCount
, Indices
, IndexCount
);
175 FIXME("Indicies not handled!\n");
179 r
= IMAGEHLP_GetSecurityDirOffset( handle
, &sd_VirtualAddr
, &size
);
184 *CertificateCount
= 0;
185 while( offset
< size
)
187 /* read the length of the current certificate */
188 count
= SetFilePointer( handle
, sd_VirtualAddr
+ offset
,
190 if( count
== INVALID_SET_FILE_POINTER
)
192 r
= ReadFile( handle
, &hdr
, cert_hdr_size
, &count
, NULL
);
195 if( count
!= cert_hdr_size
)
198 TRACE("Size = %08lx id = %08hx\n",
199 hdr
.dwLength
, hdr
.wCertificateType
);
201 /* check the certificate is not too big or too small */
202 if( hdr
.dwLength
< cert_hdr_size
)
204 if( hdr
.dwLength
> (size
-offset
) )
207 if( (TypeFilter
== CERT_SECTION_TYPE_ANY
) ||
208 (TypeFilter
== hdr
.wCertificateType
) )
210 (*CertificateCount
)++;
213 /* next certificate */
214 offset
+= hdr
.dwLength
;
220 /***********************************************************************
221 * ImageGetCertificateData (IMAGEHLP.@)
223 * FIXME: not sure that I'm dealing with the Index the right way
225 BOOL WINAPI
ImageGetCertificateData(
226 HANDLE handle
, DWORD Index
,
227 PWIN_CERTIFICATE Certificate
, PDWORD RequiredLength
)
229 DWORD r
, offset
, ofs
, size
, count
;
231 TRACE("%p %ld %p %p\n", handle
, Index
, Certificate
, RequiredLength
);
233 if( !IMAGEHLP_GetCertificateOffset( handle
, Index
, &ofs
, &size
) )
238 *RequiredLength
= size
;
242 if( *RequiredLength
< size
)
244 *RequiredLength
= size
;
245 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
249 *RequiredLength
= size
;
251 offset
= SetFilePointer( handle
, ofs
, NULL
, FILE_BEGIN
);
252 if( offset
== INVALID_SET_FILE_POINTER
)
255 r
= ReadFile( handle
, Certificate
, size
, &count
, NULL
);
266 /***********************************************************************
267 * ImageGetCertificateHeader (IMAGEHLP.@)
269 BOOL WINAPI
ImageGetCertificateHeader(
270 HANDLE handle
, DWORD index
, PWIN_CERTIFICATE pCert
)
272 DWORD r
, offset
, ofs
, size
, count
;
273 const size_t cert_hdr_size
= sizeof *pCert
- sizeof pCert
->bCertificate
;
275 TRACE("%p %ld %p\n", handle
, index
, pCert
);
277 if( !IMAGEHLP_GetCertificateOffset( handle
, index
, &ofs
, &size
) )
280 if( size
< cert_hdr_size
)
283 offset
= SetFilePointer( handle
, ofs
, NULL
, FILE_BEGIN
);
284 if( offset
== INVALID_SET_FILE_POINTER
)
287 r
= ReadFile( handle
, pCert
, cert_hdr_size
, &count
, NULL
);
290 if( count
!= cert_hdr_size
)
298 /***********************************************************************
299 * ImageGetDigestStream (IMAGEHLP.@)
301 BOOL WINAPI
ImageGetDigestStream(
302 HANDLE FileHandle
, DWORD DigestLevel
,
303 DIGEST_FUNCTION DigestFunction
, DIGEST_HANDLE DigestHandle
)
305 FIXME("(%p, %ld, %p, %p): stub\n",
306 FileHandle
, DigestLevel
, DigestFunction
, DigestHandle
308 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
312 /***********************************************************************
313 * ImageRemoveCertificate (IMAGEHLP.@)
315 BOOL WINAPI
ImageRemoveCertificate(HANDLE FileHandle
, DWORD Index
)
317 FIXME("(%p, %ld): stub\n", FileHandle
, Index
);
318 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);