Fixed buffer overflow.
[wine/multimedia.git] / dlls / imagehlp / integrity.c
blob93be8e4ea91d3995e6b22e641cc920496fbdbbb4
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "ntstatus.h"
31 #include "imagehlp.h"
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
45 * security directory.
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;
53 BOOL r;
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 )
61 return FALSE;
62 count = 0;
63 r = ReadFile( handle, &dos_hdr, sizeof dos_hdr, &count, NULL );
64 if( !r )
65 return FALSE;
66 if( count != sizeof dos_hdr )
67 return FALSE;
69 /* read the PE header */
70 count = SetFilePointer( handle, dos_hdr.e_lfanew, NULL, FILE_BEGIN );
71 if( count == INVALID_SET_FILE_POINTER )
72 return FALSE;
73 count = 0;
74 r = ReadFile( handle, &nt_hdr, sizeof nt_hdr, &count, NULL );
75 if( !r )
76 return FALSE;
77 if( count != sizeof nt_hdr )
78 return FALSE;
80 sd = &nt_hdr.OptionalHeader.
81 DataDirectory[IMAGE_FILE_SECURITY_DIRECTORY];
83 TRACE("len = %lx addr = %lx\n", sd->Size, sd->VirtualAddress);
85 offset = 0;
86 size = sd->Size;
88 /* take the n'th certificate */
89 while( 1 )
91 /* read the length of the current certificate */
92 count = SetFilePointer( handle, sd->VirtualAddress + offset,
93 NULL, FILE_BEGIN );
94 if( count == INVALID_SET_FILE_POINTER )
95 return FALSE;
96 r = ReadFile( handle, &len, sizeof len, &count, NULL );
97 if( !r )
98 return FALSE;
99 if( count != sizeof len )
100 return FALSE;
102 /* check the certificate is not too big or too small */
103 if( len < sizeof len )
104 return FALSE;
105 if( len > (size-offset) )
106 return FALSE;
107 if( !num-- )
108 break;
110 /* calculate the offset of the next certificate */
111 offset += len;
112 if( offset >= size )
113 return FALSE;
116 *pdwOfs = sd->VirtualAddress + offset;
117 *pdwSize = len;
119 TRACE("len = %lx addr = %lx\n", len, sd->VirtualAddress + offset);
121 return TRUE;
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);
136 return FALSE;
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);
150 return FALSE;
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 ) )
167 return FALSE;
169 if( !Certificate )
171 *RequiredLength = size;
172 return TRUE;
175 if( *RequiredLength < size )
177 *RequiredLength = size;
178 SetLastError( ERROR_INSUFFICIENT_BUFFER );
179 return FALSE;
182 *RequiredLength = size;
184 offset = SetFilePointer( handle, ofs, NULL, FILE_BEGIN );
185 if( offset == INVALID_SET_FILE_POINTER )
186 return FALSE;
188 r = ReadFile( handle, Certificate, size, &count, NULL );
189 if( !r )
190 return FALSE;
191 if( count != size )
192 return FALSE;
194 TRACE("OK\n");
196 return TRUE;
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);
210 return FALSE;
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);
224 return FALSE;
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);
234 return FALSE;