1998-10-11 19:04:17 +02:00
|
|
|
/*
|
|
|
|
* IMAGEHLP library
|
|
|
|
*
|
|
|
|
* Copyright 1998 Patrik Stridvall
|
2003-09-16 22:26:52 +02:00
|
|
|
* Copyright 2003 Mike McCormack
|
2009-12-03 20:50:58 +01:00
|
|
|
* Copyright 2009 Owen Rudge for CodeWeavers
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
1998-10-11 19:04:17 +02:00
|
|
|
*/
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
1999-03-14 17:35:05 +01:00
|
|
|
#include "windef.h"
|
1999-02-17 14:51:06 +01:00
|
|
|
#include "winbase.h"
|
|
|
|
#include "winerror.h"
|
2003-09-16 22:26:52 +02:00
|
|
|
#include "winternl.h"
|
|
|
|
#include "winnt.h"
|
1998-10-11 19:04:17 +02:00
|
|
|
#include "imagehlp.h"
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
1998-10-11 19:04:17 +02:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
|
1999-04-19 16:56:29 +02:00
|
|
|
|
2003-09-16 22:26:52 +02:00
|
|
|
/*
|
|
|
|
* These functions are partially documented at:
|
|
|
|
* http://www.cs.auckland.ac.nz/~pgut001/pubs/authenticode.txt
|
|
|
|
*/
|
|
|
|
|
2009-12-03 20:50:58 +01:00
|
|
|
#define HDR_FAIL -1
|
|
|
|
#define HDR_NT32 0
|
|
|
|
#define HDR_NT64 1
|
|
|
|
|
2003-09-16 22:26:52 +02:00
|
|
|
/***********************************************************************
|
2009-12-03 20:50:58 +01:00
|
|
|
* IMAGEHLP_GetNTHeaders (INTERNAL)
|
2003-09-16 22:26:52 +02:00
|
|
|
*
|
2009-12-03 20:50:58 +01:00
|
|
|
* Return the IMAGE_NT_HEADERS for a PE file, after validating magic
|
|
|
|
* numbers and distinguishing between 32-bit and 64-bit files.
|
2003-09-16 22:26:52 +02:00
|
|
|
*/
|
2009-12-03 20:50:58 +01:00
|
|
|
static int IMAGEHLP_GetNTHeaders(HANDLE handle, DWORD *pe_offset, IMAGE_NT_HEADERS32 *nt32, IMAGE_NT_HEADERS64 *nt64)
|
2003-09-16 22:26:52 +02:00
|
|
|
{
|
|
|
|
IMAGE_DOS_HEADER dos_hdr;
|
2004-05-12 00:17:49 +02:00
|
|
|
DWORD count;
|
2003-09-16 22:26:52 +02:00
|
|
|
BOOL r;
|
|
|
|
|
2009-12-03 20:50:58 +01:00
|
|
|
TRACE("handle %p\n", handle);
|
|
|
|
|
|
|
|
if ((!nt32) || (!nt64))
|
|
|
|
return HDR_FAIL;
|
2003-09-16 22:26:52 +02:00
|
|
|
|
|
|
|
/* read the DOS header */
|
2009-12-03 20:50:58 +01:00
|
|
|
count = SetFilePointer(handle, 0, NULL, FILE_BEGIN);
|
|
|
|
|
|
|
|
if (count == INVALID_SET_FILE_POINTER)
|
|
|
|
return HDR_FAIL;
|
|
|
|
|
2003-09-16 22:26:52 +02:00
|
|
|
count = 0;
|
2009-12-03 20:50:58 +01:00
|
|
|
|
|
|
|
r = ReadFile(handle, &dos_hdr, sizeof dos_hdr, &count, NULL);
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
return HDR_FAIL;
|
|
|
|
|
|
|
|
if (count != sizeof dos_hdr)
|
|
|
|
return HDR_FAIL;
|
|
|
|
|
|
|
|
/* verify magic number of 'MZ' */
|
|
|
|
if (dos_hdr.e_magic != 0x5A4D)
|
|
|
|
return HDR_FAIL;
|
|
|
|
|
|
|
|
if (pe_offset != NULL)
|
|
|
|
*pe_offset = dos_hdr.e_lfanew;
|
2003-09-16 22:26:52 +02:00
|
|
|
|
|
|
|
/* read the PE header */
|
2009-12-03 20:50:58 +01:00
|
|
|
count = SetFilePointer(handle, dos_hdr.e_lfanew, NULL, FILE_BEGIN);
|
|
|
|
|
|
|
|
if (count == INVALID_SET_FILE_POINTER)
|
|
|
|
return HDR_FAIL;
|
|
|
|
|
2003-09-16 22:26:52 +02:00
|
|
|
count = 0;
|
2009-12-03 20:50:58 +01:00
|
|
|
|
|
|
|
r = ReadFile(handle, nt32, sizeof(IMAGE_NT_HEADERS32), &count, NULL);
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
return HDR_FAIL;
|
|
|
|
|
|
|
|
if (count != sizeof(IMAGE_NT_HEADERS32))
|
|
|
|
return HDR_FAIL;
|
|
|
|
|
|
|
|
/* verify NT signature */
|
|
|
|
if (nt32->Signature != IMAGE_NT_SIGNATURE)
|
|
|
|
return HDR_FAIL;
|
|
|
|
|
|
|
|
/* check if we have a 32-bit or 64-bit executable */
|
|
|
|
switch (nt32->OptionalHeader.Magic)
|
|
|
|
{
|
|
|
|
case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
|
|
|
|
return HDR_NT32;
|
|
|
|
|
|
|
|
case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
|
|
|
|
/* Re-read as 64-bit */
|
|
|
|
|
|
|
|
count = SetFilePointer(handle, dos_hdr.e_lfanew, NULL, FILE_BEGIN);
|
|
|
|
|
|
|
|
if (count == INVALID_SET_FILE_POINTER)
|
|
|
|
return HDR_FAIL;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
r = ReadFile(handle, nt64, sizeof(IMAGE_NT_HEADERS64), &count, NULL);
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
return HDR_FAIL;
|
|
|
|
|
|
|
|
if (count != sizeof(IMAGE_NT_HEADERS64))
|
|
|
|
return HDR_FAIL;
|
|
|
|
|
|
|
|
/* verify NT signature */
|
|
|
|
if (nt64->Signature != IMAGE_NT_SIGNATURE)
|
|
|
|
return HDR_FAIL;
|
|
|
|
|
|
|
|
return HDR_NT64;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HDR_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* IMAGEHLP_GetSecurityDirOffset (INTERNAL)
|
|
|
|
*
|
|
|
|
* Read a file's PE header, and return the offset and size of the
|
|
|
|
* security directory.
|
|
|
|
*/
|
|
|
|
static BOOL IMAGEHLP_GetSecurityDirOffset( HANDLE handle,
|
|
|
|
DWORD *pdwOfs, DWORD *pdwSize )
|
|
|
|
{
|
|
|
|
IMAGE_NT_HEADERS32 nt_hdr32;
|
|
|
|
IMAGE_NT_HEADERS64 nt_hdr64;
|
|
|
|
IMAGE_DATA_DIRECTORY *sd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = IMAGEHLP_GetNTHeaders(handle, NULL, &nt_hdr32, &nt_hdr64);
|
|
|
|
|
|
|
|
if (ret == HDR_NT32)
|
|
|
|
sd = &nt_hdr32.OptionalHeader.DataDirectory[IMAGE_FILE_SECURITY_DIRECTORY];
|
|
|
|
else if (ret == HDR_NT64)
|
|
|
|
sd = &nt_hdr64.OptionalHeader.DataDirectory[IMAGE_FILE_SECURITY_DIRECTORY];
|
|
|
|
else
|
2003-09-16 22:26:52 +02:00
|
|
|
return FALSE;
|
|
|
|
|
2009-12-03 20:50:58 +01:00
|
|
|
TRACE("ret = %d size = %x addr = %x\n", ret, sd->Size, sd->VirtualAddress);
|
2003-09-16 22:26:52 +02:00
|
|
|
|
2004-05-12 00:17:49 +02:00
|
|
|
*pdwSize = sd->Size;
|
|
|
|
*pdwOfs = sd->VirtualAddress;
|
2003-09-16 22:26:52 +02:00
|
|
|
|
2004-05-12 00:17:49 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
2003-09-16 22:26:52 +02:00
|
|
|
|
2009-12-03 20:51:02 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* IMAGEHLP_SetSecurityDirOffset (INTERNAL)
|
|
|
|
*
|
|
|
|
* Read a file's PE header, and update the offset and size of the
|
|
|
|
* security directory.
|
|
|
|
*/
|
|
|
|
static BOOL IMAGEHLP_SetSecurityDirOffset(HANDLE handle,
|
|
|
|
DWORD dwOfs, DWORD dwSize)
|
|
|
|
{
|
|
|
|
IMAGE_NT_HEADERS32 nt_hdr32;
|
|
|
|
IMAGE_NT_HEADERS64 nt_hdr64;
|
|
|
|
IMAGE_DATA_DIRECTORY *sd;
|
|
|
|
int ret, nt_hdr_size = 0;
|
|
|
|
DWORD pe_offset;
|
|
|
|
void *nt_hdr;
|
|
|
|
DWORD count;
|
|
|
|
BOOL r;
|
|
|
|
|
|
|
|
ret = IMAGEHLP_GetNTHeaders(handle, &pe_offset, &nt_hdr32, &nt_hdr64);
|
|
|
|
|
|
|
|
if (ret == HDR_NT32)
|
|
|
|
{
|
|
|
|
sd = &nt_hdr32.OptionalHeader.DataDirectory[IMAGE_FILE_SECURITY_DIRECTORY];
|
|
|
|
|
|
|
|
nt_hdr = &nt_hdr32;
|
|
|
|
nt_hdr_size = sizeof(IMAGE_NT_HEADERS32);
|
|
|
|
}
|
|
|
|
else if (ret == HDR_NT64)
|
|
|
|
{
|
|
|
|
sd = &nt_hdr64.OptionalHeader.DataDirectory[IMAGE_FILE_SECURITY_DIRECTORY];
|
|
|
|
|
|
|
|
nt_hdr = &nt_hdr64;
|
|
|
|
nt_hdr_size = sizeof(IMAGE_NT_HEADERS64);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
sd->Size = dwSize;
|
|
|
|
sd->VirtualAddress = dwOfs;
|
|
|
|
|
|
|
|
TRACE("size = %x addr = %x\n", sd->Size, sd->VirtualAddress);
|
|
|
|
|
|
|
|
/* write the header back again */
|
|
|
|
count = SetFilePointer(handle, pe_offset, NULL, FILE_BEGIN);
|
|
|
|
|
|
|
|
if (count == INVALID_SET_FILE_POINTER)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
r = WriteFile(handle, nt_hdr, nt_hdr_size, &count, NULL);
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (count != nt_hdr_size)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-05-12 00:17:49 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* IMAGEHLP_GetCertificateOffset (INTERNAL)
|
|
|
|
*
|
|
|
|
* Read a file's PE header, and return the offset and size of the
|
|
|
|
* security directory.
|
|
|
|
*/
|
|
|
|
static BOOL IMAGEHLP_GetCertificateOffset( HANDLE handle, DWORD num,
|
|
|
|
DWORD *pdwOfs, DWORD *pdwSize )
|
|
|
|
{
|
|
|
|
DWORD size, count, offset, len, sd_VirtualAddr;
|
|
|
|
BOOL r;
|
|
|
|
|
|
|
|
r = IMAGEHLP_GetSecurityDirOffset( handle, &sd_VirtualAddr, &size );
|
|
|
|
if( !r )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
offset = 0;
|
2003-09-16 22:26:52 +02:00
|
|
|
/* take the n'th certificate */
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
/* read the length of the current certificate */
|
2004-05-12 00:17:49 +02:00
|
|
|
count = SetFilePointer( handle, sd_VirtualAddr + offset,
|
2003-09-16 22:26:52 +02:00
|
|
|
NULL, FILE_BEGIN );
|
|
|
|
if( count == INVALID_SET_FILE_POINTER )
|
|
|
|
return FALSE;
|
|
|
|
r = ReadFile( handle, &len, sizeof len, &count, NULL );
|
|
|
|
if( !r )
|
|
|
|
return FALSE;
|
|
|
|
if( count != sizeof len )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* check the certificate is not too big or too small */
|
|
|
|
if( len < sizeof len )
|
|
|
|
return FALSE;
|
|
|
|
if( len > (size-offset) )
|
|
|
|
return FALSE;
|
|
|
|
if( !num-- )
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* calculate the offset of the next certificate */
|
|
|
|
offset += len;
|
2009-12-03 20:50:58 +01:00
|
|
|
|
|
|
|
/* padded out to the nearest 8-byte boundary */
|
|
|
|
if( len % 8 )
|
|
|
|
offset += 8 - (len % 8);
|
|
|
|
|
2003-09-16 22:26:52 +02:00
|
|
|
if( offset >= size )
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-05-12 00:17:49 +02:00
|
|
|
*pdwOfs = sd_VirtualAddr + offset;
|
2003-09-16 22:26:52 +02:00
|
|
|
*pdwSize = len;
|
|
|
|
|
2006-10-08 18:58:00 +02:00
|
|
|
TRACE("len = %x addr = %x\n", len, sd_VirtualAddr + offset);
|
2003-09-16 22:26:52 +02:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-12-04 17:12:02 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* IMAGEHLP_RecalculateChecksum (INTERNAL)
|
|
|
|
*
|
|
|
|
* Update the NT header checksum for the specified file.
|
|
|
|
*/
|
|
|
|
static BOOL IMAGEHLP_RecalculateChecksum(HANDLE handle)
|
|
|
|
{
|
|
|
|
DWORD FileLength, count, HeaderSum, pe_offset, nt_hdr_size;
|
|
|
|
IMAGE_NT_HEADERS32 nt_hdr32;
|
|
|
|
IMAGE_NT_HEADERS64 nt_hdr64;
|
|
|
|
LPVOID BaseAddress;
|
|
|
|
HANDLE hMapping;
|
|
|
|
DWORD *CheckSum;
|
|
|
|
void *nt_hdr;
|
|
|
|
int ret;
|
|
|
|
BOOL r;
|
|
|
|
|
|
|
|
TRACE("handle %p\n", handle);
|
|
|
|
|
|
|
|
ret = IMAGEHLP_GetNTHeaders(handle, &pe_offset, &nt_hdr32, &nt_hdr64);
|
|
|
|
|
|
|
|
if (ret == HDR_NT32)
|
|
|
|
{
|
|
|
|
CheckSum = &nt_hdr32.OptionalHeader.CheckSum;
|
|
|
|
|
|
|
|
nt_hdr = &nt_hdr32;
|
|
|
|
nt_hdr_size = sizeof(IMAGE_NT_HEADERS32);
|
|
|
|
}
|
|
|
|
else if (ret == HDR_NT64)
|
|
|
|
{
|
|
|
|
CheckSum = &nt_hdr64.OptionalHeader.CheckSum;
|
|
|
|
|
|
|
|
nt_hdr = &nt_hdr64;
|
|
|
|
nt_hdr_size = sizeof(IMAGE_NT_HEADERS64);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
hMapping = CreateFileMappingW(handle, NULL, PAGE_READONLY, 0, 0, NULL);
|
|
|
|
|
|
|
|
if (!hMapping)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
BaseAddress = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
|
|
|
|
|
|
|
|
if (!BaseAddress)
|
|
|
|
{
|
|
|
|
CloseHandle(hMapping);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileLength = GetFileSize(handle, NULL);
|
|
|
|
|
|
|
|
*CheckSum = 0;
|
|
|
|
CheckSumMappedFile(BaseAddress, FileLength, &HeaderSum, CheckSum);
|
|
|
|
|
|
|
|
UnmapViewOfFile(BaseAddress);
|
|
|
|
CloseHandle(hMapping);
|
|
|
|
|
|
|
|
if (*CheckSum)
|
|
|
|
{
|
|
|
|
/* write the header back again */
|
|
|
|
count = SetFilePointer(handle, pe_offset, NULL, FILE_BEGIN);
|
|
|
|
|
|
|
|
if (count == INVALID_SET_FILE_POINTER)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
r = WriteFile(handle, nt_hdr, nt_hdr_size, &count, NULL);
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (count != nt_hdr_size)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2003-09-16 22:26:52 +02:00
|
|
|
|
1998-10-11 19:04:17 +02:00
|
|
|
/***********************************************************************
|
2000-03-28 22:22:59 +02:00
|
|
|
* ImageAddCertificate (IMAGEHLP.@)
|
2009-12-03 20:51:02 +01:00
|
|
|
*
|
|
|
|
* Adds the specified certificate to the security directory of
|
|
|
|
* open PE file.
|
1998-10-11 19:04:17 +02:00
|
|
|
*/
|
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
BOOL WINAPI ImageAddCertificate(
|
2007-08-28 01:32:29 +02:00
|
|
|
HANDLE FileHandle, LPWIN_CERTIFICATE Certificate, PDWORD Index)
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
2009-12-03 20:51:02 +01:00
|
|
|
DWORD size = 0, count = 0, offset = 0, sd_VirtualAddr = 0, index = 0;
|
|
|
|
WIN_CERTIFICATE hdr;
|
|
|
|
const size_t cert_hdr_size = sizeof hdr - sizeof hdr.bCertificate;
|
|
|
|
BOOL r;
|
|
|
|
|
|
|
|
TRACE("(%p, %p, %p)\n", FileHandle, Certificate, Index);
|
|
|
|
|
|
|
|
r = IMAGEHLP_GetSecurityDirOffset(FileHandle, &sd_VirtualAddr, &size);
|
|
|
|
|
|
|
|
/* If we've already got a security directory, find the end of it */
|
|
|
|
if ((r) && (sd_VirtualAddr != 0))
|
|
|
|
{
|
|
|
|
offset = 0;
|
|
|
|
index = 0;
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
/* Check if the security directory is at the end of the file.
|
|
|
|
If not, we should probably relocate it. */
|
|
|
|
if (GetFileSize(FileHandle, NULL) != sd_VirtualAddr + size)
|
|
|
|
{
|
|
|
|
FIXME("Security directory already present but not located at EOF, not adding certificate\n");
|
|
|
|
|
|
|
|
SetLastError(ERROR_NOT_SUPPORTED);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (offset < size)
|
|
|
|
{
|
|
|
|
/* read the length of the current certificate */
|
|
|
|
count = SetFilePointer (FileHandle, sd_VirtualAddr + offset,
|
|
|
|
NULL, FILE_BEGIN);
|
|
|
|
|
|
|
|
if (count == INVALID_SET_FILE_POINTER)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
r = ReadFile(FileHandle, &hdr, cert_hdr_size, &count, NULL);
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (count != cert_hdr_size)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* check the certificate is not too big or too small */
|
|
|
|
if (hdr.dwLength < cert_hdr_size)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (hdr.dwLength > (size-offset))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* next certificate */
|
|
|
|
offset += hdr.dwLength;
|
|
|
|
|
|
|
|
/* padded out to the nearest 8-byte boundary */
|
|
|
|
if (hdr.dwLength % 8)
|
|
|
|
offset += 8 - (hdr.dwLength % 8);
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = SetFilePointer (FileHandle, sd_VirtualAddr + offset, NULL, FILE_BEGIN);
|
|
|
|
|
|
|
|
if (count == INVALID_SET_FILE_POINTER)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sd_VirtualAddr = SetFilePointer(FileHandle, 0, NULL, FILE_END);
|
|
|
|
|
|
|
|
if (sd_VirtualAddr == INVALID_SET_FILE_POINTER)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the certificate to the file */
|
|
|
|
r = WriteFile(FileHandle, Certificate, Certificate->dwLength, &count, NULL);
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Pad out if necessary */
|
|
|
|
if (Certificate->dwLength % 8)
|
|
|
|
{
|
|
|
|
char null[8];
|
|
|
|
|
|
|
|
ZeroMemory(null, 8);
|
|
|
|
WriteFile(FileHandle, null, 8 - (Certificate->dwLength % 8), NULL, NULL);
|
|
|
|
|
|
|
|
size += 8 - (Certificate->dwLength % 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
size += Certificate->dwLength;
|
|
|
|
|
|
|
|
/* Update the security directory offset and size */
|
|
|
|
if (!IMAGEHLP_SetSecurityDirOffset(FileHandle, sd_VirtualAddr, size))
|
|
|
|
return FALSE;
|
|
|
|
|
2009-12-04 17:12:02 +01:00
|
|
|
if (!IMAGEHLP_RecalculateChecksum(FileHandle))
|
|
|
|
return FALSE;
|
|
|
|
|
2009-12-03 20:51:02 +01:00
|
|
|
return TRUE;
|
1998-10-11 19:04:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2000-03-28 22:22:59 +02:00
|
|
|
* ImageEnumerateCertificates (IMAGEHLP.@)
|
1998-10-11 19:04:17 +02:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
BOOL WINAPI ImageEnumerateCertificates(
|
2004-05-12 00:17:49 +02:00
|
|
|
HANDLE handle, WORD TypeFilter, PDWORD CertificateCount,
|
|
|
|
PDWORD Indices, DWORD IndexCount)
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
2007-04-13 13:02:47 +02:00
|
|
|
DWORD size, count, offset, sd_VirtualAddr, index;
|
2004-05-12 00:17:49 +02:00
|
|
|
WIN_CERTIFICATE hdr;
|
|
|
|
const size_t cert_hdr_size = sizeof hdr - sizeof hdr.bCertificate;
|
|
|
|
BOOL r;
|
|
|
|
|
2006-10-08 18:58:00 +02:00
|
|
|
TRACE("%p %hd %p %p %d\n",
|
2004-05-12 00:17:49 +02:00
|
|
|
handle, TypeFilter, CertificateCount, Indices, IndexCount);
|
|
|
|
|
|
|
|
r = IMAGEHLP_GetSecurityDirOffset( handle, &sd_VirtualAddr, &size );
|
|
|
|
if( !r )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
offset = 0;
|
2007-04-13 13:02:47 +02:00
|
|
|
index = 0;
|
2004-05-12 00:17:49 +02:00
|
|
|
*CertificateCount = 0;
|
|
|
|
while( offset < size )
|
|
|
|
{
|
|
|
|
/* read the length of the current certificate */
|
|
|
|
count = SetFilePointer( handle, sd_VirtualAddr + offset,
|
|
|
|
NULL, FILE_BEGIN );
|
|
|
|
if( count == INVALID_SET_FILE_POINTER )
|
|
|
|
return FALSE;
|
|
|
|
r = ReadFile( handle, &hdr, cert_hdr_size, &count, NULL );
|
|
|
|
if( !r )
|
|
|
|
return FALSE;
|
|
|
|
if( count != cert_hdr_size )
|
|
|
|
return FALSE;
|
|
|
|
|
2006-10-08 18:58:00 +02:00
|
|
|
TRACE("Size = %08x id = %08hx\n",
|
2004-05-12 00:17:49 +02:00
|
|
|
hdr.dwLength, hdr.wCertificateType );
|
|
|
|
|
|
|
|
/* check the certificate is not too big or too small */
|
|
|
|
if( hdr.dwLength < cert_hdr_size )
|
|
|
|
return FALSE;
|
|
|
|
if( hdr.dwLength > (size-offset) )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if( (TypeFilter == CERT_SECTION_TYPE_ANY) ||
|
|
|
|
(TypeFilter == hdr.wCertificateType) )
|
|
|
|
{
|
|
|
|
(*CertificateCount)++;
|
2007-04-13 13:02:47 +02:00
|
|
|
if(Indices && *CertificateCount <= IndexCount)
|
|
|
|
*Indices++ = index;
|
2004-05-12 00:17:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* next certificate */
|
|
|
|
offset += hdr.dwLength;
|
2009-12-03 20:50:58 +01:00
|
|
|
|
|
|
|
/* padded out to the nearest 8-byte boundary */
|
|
|
|
if (hdr.dwLength % 8)
|
|
|
|
offset += 8 - (hdr.dwLength % 8);
|
|
|
|
|
2007-04-13 13:02:47 +02:00
|
|
|
index++;
|
2004-05-12 00:17:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
1998-10-11 19:04:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2000-03-28 22:22:59 +02:00
|
|
|
* ImageGetCertificateData (IMAGEHLP.@)
|
2003-09-16 22:26:52 +02:00
|
|
|
*
|
|
|
|
* FIXME: not sure that I'm dealing with the Index the right way
|
1998-10-11 19:04:17 +02:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
BOOL WINAPI ImageGetCertificateData(
|
2003-09-16 22:26:52 +02:00
|
|
|
HANDLE handle, DWORD Index,
|
2007-08-28 01:32:29 +02:00
|
|
|
LPWIN_CERTIFICATE Certificate, PDWORD RequiredLength)
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
2003-09-16 22:26:52 +02:00
|
|
|
DWORD r, offset, ofs, size, count;
|
|
|
|
|
2006-10-08 18:58:00 +02:00
|
|
|
TRACE("%p %d %p %p\n", handle, Index, Certificate, RequiredLength);
|
2003-09-16 22:26:52 +02:00
|
|
|
|
2007-12-12 03:11:07 +01:00
|
|
|
if( !RequiredLength)
|
|
|
|
{
|
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-05-12 00:17:49 +02:00
|
|
|
if( !IMAGEHLP_GetCertificateOffset( handle, Index, &ofs, &size ) )
|
2003-09-16 22:26:52 +02:00
|
|
|
return FALSE;
|
|
|
|
|
2007-12-12 03:11:07 +01:00
|
|
|
if( *RequiredLength < size )
|
2003-09-16 22:26:52 +02:00
|
|
|
{
|
|
|
|
*RequiredLength = size;
|
2007-12-12 03:11:07 +01:00
|
|
|
SetLastError( ERROR_INSUFFICIENT_BUFFER );
|
|
|
|
return FALSE;
|
2003-09-16 22:26:52 +02:00
|
|
|
}
|
|
|
|
|
2007-12-12 03:11:07 +01:00
|
|
|
if( !Certificate )
|
2003-09-16 22:26:52 +02:00
|
|
|
{
|
2007-12-12 03:11:07 +01:00
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
2003-09-16 22:26:52 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*RequiredLength = size;
|
|
|
|
|
|
|
|
offset = SetFilePointer( handle, ofs, NULL, FILE_BEGIN );
|
|
|
|
if( offset == INVALID_SET_FILE_POINTER )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
r = ReadFile( handle, Certificate, size, &count, NULL );
|
|
|
|
if( !r )
|
|
|
|
return FALSE;
|
|
|
|
if( count != size )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
TRACE("OK\n");
|
2007-12-12 03:11:07 +01:00
|
|
|
SetLastError( NO_ERROR );
|
2003-09-16 22:26:52 +02:00
|
|
|
|
|
|
|
return TRUE;
|
1998-10-11 19:04:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2000-03-28 22:22:59 +02:00
|
|
|
* ImageGetCertificateHeader (IMAGEHLP.@)
|
1998-10-11 19:04:17 +02:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
BOOL WINAPI ImageGetCertificateHeader(
|
2007-08-28 01:32:29 +02:00
|
|
|
HANDLE handle, DWORD index, LPWIN_CERTIFICATE pCert)
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
2004-05-12 00:17:49 +02:00
|
|
|
DWORD r, offset, ofs, size, count;
|
|
|
|
const size_t cert_hdr_size = sizeof *pCert - sizeof pCert->bCertificate;
|
|
|
|
|
2006-10-08 18:58:00 +02:00
|
|
|
TRACE("%p %d %p\n", handle, index, pCert);
|
2004-05-12 00:17:49 +02:00
|
|
|
|
|
|
|
if( !IMAGEHLP_GetCertificateOffset( handle, index, &ofs, &size ) )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if( size < cert_hdr_size )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
offset = SetFilePointer( handle, ofs, NULL, FILE_BEGIN );
|
|
|
|
if( offset == INVALID_SET_FILE_POINTER )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
r = ReadFile( handle, pCert, cert_hdr_size, &count, NULL );
|
|
|
|
if( !r )
|
|
|
|
return FALSE;
|
|
|
|
if( count != cert_hdr_size )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
TRACE("OK\n");
|
|
|
|
|
|
|
|
return TRUE;
|
1998-10-11 19:04:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2000-03-28 22:22:59 +02:00
|
|
|
* ImageGetDigestStream (IMAGEHLP.@)
|
1998-10-11 19:04:17 +02:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
BOOL WINAPI ImageGetDigestStream(
|
|
|
|
HANDLE FileHandle, DWORD DigestLevel,
|
|
|
|
DIGEST_FUNCTION DigestFunction, DIGEST_HANDLE DigestHandle)
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
2006-10-08 18:58:00 +02:00
|
|
|
FIXME("(%p, %d, %p, %p): stub\n",
|
1998-10-11 19:04:17 +02:00
|
|
|
FileHandle, DigestLevel, DigestFunction, DigestHandle
|
|
|
|
);
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2000-03-28 22:22:59 +02:00
|
|
|
* ImageRemoveCertificate (IMAGEHLP.@)
|
1998-10-11 19:04:17 +02:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
BOOL WINAPI ImageRemoveCertificate(HANDLE FileHandle, DWORD Index)
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
2009-12-04 17:11:57 +01:00
|
|
|
DWORD size = 0, count = 0, sd_VirtualAddr = 0, offset = 0;
|
|
|
|
DWORD data_size = 0, cert_size = 0, cert_size_padded = 0, ret = 0;
|
|
|
|
LPVOID cert_data;
|
|
|
|
BOOL r;
|
|
|
|
|
|
|
|
TRACE("(%p, %d)\n", FileHandle, Index);
|
|
|
|
|
|
|
|
r = ImageEnumerateCertificates(FileHandle, CERT_SECTION_TYPE_ANY, &count, NULL, 0);
|
|
|
|
|
|
|
|
if ((!r) || (count == 0))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if ((!IMAGEHLP_GetSecurityDirOffset(FileHandle, &sd_VirtualAddr, &size)) ||
|
|
|
|
(!IMAGEHLP_GetCertificateOffset(FileHandle, Index, &offset, &cert_size)))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Ignore any padding we have, too */
|
|
|
|
if (cert_size % 8)
|
|
|
|
cert_size_padded = cert_size + (8 - (cert_size % 8));
|
|
|
|
else
|
|
|
|
cert_size_padded = cert_size;
|
|
|
|
|
|
|
|
data_size = size - (offset - sd_VirtualAddr) - cert_size_padded;
|
|
|
|
|
|
|
|
if (data_size == 0)
|
|
|
|
{
|
|
|
|
ret = SetFilePointer(FileHandle, sd_VirtualAddr, NULL, FILE_BEGIN);
|
|
|
|
|
|
|
|
if (ret == INVALID_SET_FILE_POINTER)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cert_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data_size);
|
|
|
|
|
|
|
|
if (!cert_data)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
ret = SetFilePointer(FileHandle, offset + cert_size_padded, NULL, FILE_BEGIN);
|
|
|
|
|
|
|
|
if (ret == INVALID_SET_FILE_POINTER)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* Read any subsequent certificates */
|
|
|
|
r = ReadFile(FileHandle, cert_data, data_size, &count, NULL);
|
|
|
|
|
|
|
|
if ((!r) || (count != data_size))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
SetFilePointer(FileHandle, offset, NULL, FILE_BEGIN);
|
|
|
|
|
|
|
|
/* Write them one index back */
|
|
|
|
r = WriteFile(FileHandle, cert_data, data_size, &count, NULL);
|
|
|
|
|
|
|
|
if ((!r) || (count != data_size))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, cert_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If security directory is at end of file, trim the file */
|
|
|
|
if (GetFileSize(FileHandle, NULL) == sd_VirtualAddr + size)
|
|
|
|
SetEndOfFile(FileHandle);
|
|
|
|
|
|
|
|
if (count == 1)
|
|
|
|
r = IMAGEHLP_SetSecurityDirOffset(FileHandle, 0, 0);
|
|
|
|
else
|
|
|
|
r = IMAGEHLP_SetSecurityDirOffset(FileHandle, sd_VirtualAddr, size - cert_size_padded);
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
return FALSE;
|
|
|
|
|
2009-12-04 17:12:02 +01:00
|
|
|
if (!IMAGEHLP_RecalculateChecksum(FileHandle))
|
|
|
|
return FALSE;
|
|
|
|
|
2009-12-04 17:11:57 +01:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
error:
|
|
|
|
HeapFree(GetProcessHeap(), 0, cert_data);
|
|
|
|
return FALSE;
|
1998-10-11 19:04:17 +02:00
|
|
|
}
|