Assorted spelling fixes.
[wine/wine-gecko.git] / dlls / imagehlp / integrity.c
blobb69e4f0862690e996bf3fc2596fc3b04a91c1b31
1 /*
2 * IMAGEHLP library
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
22 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "winreg.h"
28 #include "winternl.h"
29 #include "winnt.h"
30 #include "imagehlp.h"
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
44 * security directory.
46 static BOOL IMAGEHLP_GetSecurityDirOffset( HANDLE handle,
47 DWORD *pdwOfs, DWORD *pdwSize )
49 IMAGE_DOS_HEADER dos_hdr;
50 IMAGE_NT_HEADERS nt_hdr;
51 DWORD count;
52 BOOL r;
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 )
60 return FALSE;
61 count = 0;
62 r = ReadFile( handle, &dos_hdr, sizeof dos_hdr, &count, NULL );
63 if( !r )
64 return FALSE;
65 if( count != sizeof dos_hdr )
66 return FALSE;
68 /* read the PE header */
69 count = SetFilePointer( handle, dos_hdr.e_lfanew, NULL, FILE_BEGIN );
70 if( count == INVALID_SET_FILE_POINTER )
71 return FALSE;
72 count = 0;
73 r = ReadFile( handle, &nt_hdr, sizeof nt_hdr, &count, NULL );
74 if( !r )
75 return FALSE;
76 if( count != sizeof nt_hdr )
77 return FALSE;
79 sd = &nt_hdr.OptionalHeader.
80 DataDirectory[IMAGE_FILE_SECURITY_DIRECTORY];
82 TRACE("size = %lx addr = %lx\n", sd->Size, sd->VirtualAddress);
83 *pdwSize = sd->Size;
84 *pdwOfs = sd->VirtualAddress;
86 return TRUE;
89 /***********************************************************************
90 * IMAGEHLP_GetCertificateOffset (INTERNAL)
92 * Read a file's PE header, and return the offset and size of the
93 * security directory.
95 static BOOL IMAGEHLP_GetCertificateOffset( HANDLE handle, DWORD num,
96 DWORD *pdwOfs, DWORD *pdwSize )
98 DWORD size, count, offset, len, sd_VirtualAddr;
99 BOOL r;
101 r = IMAGEHLP_GetSecurityDirOffset( handle, &sd_VirtualAddr, &size );
102 if( !r )
103 return FALSE;
105 offset = 0;
106 /* take the n'th certificate */
107 while( 1 )
109 /* read the length of the current certificate */
110 count = SetFilePointer( handle, sd_VirtualAddr + offset,
111 NULL, FILE_BEGIN );
112 if( count == INVALID_SET_FILE_POINTER )
113 return FALSE;
114 r = ReadFile( handle, &len, sizeof len, &count, NULL );
115 if( !r )
116 return FALSE;
117 if( count != sizeof len )
118 return FALSE;
120 /* check the certificate is not too big or too small */
121 if( len < sizeof len )
122 return FALSE;
123 if( len > (size-offset) )
124 return FALSE;
125 if( !num-- )
126 break;
128 /* calculate the offset of the next certificate */
129 offset += len;
130 if( offset >= size )
131 return FALSE;
134 *pdwOfs = sd_VirtualAddr + offset;
135 *pdwSize = len;
137 TRACE("len = %lx addr = %lx\n", len, sd_VirtualAddr + offset);
139 return TRUE;
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);
154 return FALSE;
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;
165 WIN_CERTIFICATE hdr;
166 const size_t cert_hdr_size = sizeof hdr - sizeof hdr.bCertificate;
167 BOOL r;
169 TRACE("%p %hd %p %p %ld\n",
170 handle, TypeFilter, CertificateCount, Indices, IndexCount);
172 if( Indices )
174 FIXME("Indicies not handled!\n");
175 return FALSE;
178 r = IMAGEHLP_GetSecurityDirOffset( handle, &sd_VirtualAddr, &size );
179 if( !r )
180 return FALSE;
182 offset = 0;
183 *CertificateCount = 0;
184 while( offset < size )
186 /* read the length of the current certificate */
187 count = SetFilePointer( handle, sd_VirtualAddr + offset,
188 NULL, FILE_BEGIN );
189 if( count == INVALID_SET_FILE_POINTER )
190 return FALSE;
191 r = ReadFile( handle, &hdr, cert_hdr_size, &count, NULL );
192 if( !r )
193 return FALSE;
194 if( count != cert_hdr_size )
195 return FALSE;
197 TRACE("Size = %08lx 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 )
202 return FALSE;
203 if( hdr.dwLength > (size-offset) )
204 return FALSE;
206 if( (TypeFilter == CERT_SECTION_TYPE_ANY) ||
207 (TypeFilter == hdr.wCertificateType) )
209 (*CertificateCount)++;
212 /* next certificate */
213 offset += hdr.dwLength;
216 return TRUE;
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 %ld %p %p\n", handle, Index, Certificate, RequiredLength);
232 if( !IMAGEHLP_GetCertificateOffset( handle, Index, &ofs, &size ) )
233 return FALSE;
235 if( !Certificate )
237 *RequiredLength = size;
238 return TRUE;
241 if( *RequiredLength < size )
243 *RequiredLength = size;
244 SetLastError( ERROR_INSUFFICIENT_BUFFER );
245 return FALSE;
248 *RequiredLength = size;
250 offset = SetFilePointer( handle, ofs, NULL, FILE_BEGIN );
251 if( offset == INVALID_SET_FILE_POINTER )
252 return FALSE;
254 r = ReadFile( handle, Certificate, size, &count, NULL );
255 if( !r )
256 return FALSE;
257 if( count != size )
258 return FALSE;
260 TRACE("OK\n");
262 return TRUE;
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 %ld %p\n", handle, index, pCert);
276 if( !IMAGEHLP_GetCertificateOffset( handle, index, &ofs, &size ) )
277 return FALSE;
279 if( size < cert_hdr_size )
280 return FALSE;
282 offset = SetFilePointer( handle, ofs, NULL, FILE_BEGIN );
283 if( offset == INVALID_SET_FILE_POINTER )
284 return FALSE;
286 r = ReadFile( handle, pCert, cert_hdr_size, &count, NULL );
287 if( !r )
288 return FALSE;
289 if( count != cert_hdr_size )
290 return FALSE;
292 TRACE("OK\n");
294 return TRUE;
297 /***********************************************************************
298 * ImageGetDigestStream (IMAGEHLP.@)
300 BOOL WINAPI ImageGetDigestStream(
301 HANDLE FileHandle, DWORD DigestLevel,
302 DIGEST_FUNCTION DigestFunction, DIGEST_HANDLE DigestHandle)
304 FIXME("(%p, %ld, %p, %p): stub\n",
305 FileHandle, DigestLevel, DigestFunction, DigestHandle
307 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
308 return FALSE;
311 /***********************************************************************
312 * ImageRemoveCertificate (IMAGEHLP.@)
314 BOOL WINAPI ImageRemoveCertificate(HANDLE FileHandle, DWORD Index)
316 FIXME("(%p, %ld): stub\n", FileHandle, Index);
317 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
318 return FALSE;