1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
| unit CodeSignature;
interface
uses Winapi.Windows, Winapi.ImageHlp, System.Classes, System.SysUtils, System.IOUtils, System.Net.HttpClient.Win;
const
PKCS_7_ASN_ENCODING = $00010000;
X509_ASN_ENCODING = $00000001;
type
TCertificateVersion = (V1, V2, V3);
TEncodingType = (etX509=X509_ASN_ENCODING, etPKS7=PKCS_7_ASN_ENCODING);
TCertificate = record
Version :TCertificateVersion;
Company :string;
Issuer :string;
Subject :string;
FromDate :TDateTime;
ToDate :TDateTime;
Algorithm :string; // https://learn.microsoft.com/en-us/windows/win32/api/wincrypt/ns-wincrypt-crypt_algorithm_identifier
EncodingType :TEncodingType;
end;
TCodeSignature = class
private
const
CERT_NAME_SIMPLE_DISPLAY_TYPE = 4;
CERT_X500_NAME_STR = 3;
EncodingTypes = X509_ASN_ENCODING or PKCS_7_ASN_ENCODING;
var
FFileName :string;
FHFile :hFile;
procedure CloseFile;
function DecodeString(aBlob :CERT_NAME_BLOB) :string;
function FileTimeToDateTime(aFileTime: TFileTime): TDateTime;
function GetCertificates(aIndex: word): TCertificate;
function GetCount: word;
function GetSigned :boolean;
procedure OpenFile;
procedure SetFileName(const Value: string);
public
property FileName :string read FFileName write SetFileName;
property Certificates[aIndex :word] :TCertificate read GetCertificates;
property Count :word read GetCount;
property Signed :boolean read GetSigned;
constructor Create(const aFileName :string = '');
destructor Destroy; override;
class function IsSigned(const aFileName :string) :boolean;
class function GetCertificate(const aFileName :string; var aCertificate :TCertificate) :boolean;
end;
implementation
type
TCryptVerifyMessagePara = record
cbSize :dword;
dwMsgAndCertEncodingType :dword;
hCryptProv :pointer;
pfnGetSignerCertificate :pointer;
pvGetArg :Pointer;
end;
const
Crypt32 = 'Crypt32.dll';
{$Warnings off}
function CryptVerifyMessageSignature(const pVerifyPara: TCryptVerifyMessagePara; dwSignerIndex: dword; pbSignedBlob: PByte; cbSignedBlob: dword;
pbDecoded: PByte; pcbDecoded: PDWord; ppSignerCert: PCCERT_CONTEXT): BOOL; stdcall; external Crypt32 delayed;
function CertGetNameString(pCertContext: PCCERT_CONTEXT; dwType: dword; dwFlags: dword; pvTypePara: pointer; pszNameString: PChar;
cchNameString: DWORD): dword; stdcall; external Crypt32 name 'CertGetNameStringW' delayed;
function CertCreateCertificateContext(dwCertEncodingType: dword; pbCertEncoded: PByte; cbCertEncoded: dword): PCCERT_CONTEXT; stdcall; external Crypt32 delayed;
function CertFreeCertificateContext(pCertContext: PCCERT_CONTEXT): BOOL; stdcall; external Crypt32 delayed;
function CertNameToStr(dwCertEncodingType: dword; pName: PCERT_NAME_BLOB; dwStrType: dword; psz: LPTSTR; csz: dword): dword; stdcall; external Crypt32 name 'CertNameToStrW' delayed;
{$Warnings on}
{ TCodeSignature }
procedure TCodeSignature.CloseFile;
begin
if FHFile = 0 then Exit;
CloseHandle(FHFile);
FHFile := 0;
end;
constructor TCodeSignature.Create(const aFileName: string);
begin
inherited Create;
SetFileName(aFileName);
end;
function TCodeSignature.DecodeString(aBlob: CERT_NAME_BLOB): string;
begin
SetLength(Result, CertNameToStr(EncodingTypes, @aBlob, CERT_X500_NAME_STR, nil, 0) -1);
CertNameToStr(EncodingTypes, @aBlob, CERT_X500_NAME_STR, PChar(Result), Result.Length +1);
end;
destructor TCodeSignature.Destroy;
begin
CloseFile;
inherited;
end;
function TCodeSignature.FileTimeToDateTime(aFileTime: TFileTime): TDateTime;
var
SysTime :TSystemTime;
begin
FileTimeToLocalFileTime(aFileTime, aFileTime);
FileTimeToSystemTime(aFileTime, SysTime);
Result := SystemTimeToDateTime(SysTime);
end;
class function TCodeSignature.GetCertificate(const aFileName: string; var aCertificate: TCertificate): boolean;
begin
with Create(aFileName) do
try
try
aCertificate := GetCertificates(0);
Result := TRUE;
finally
Free;
end;
except
Result := FALSE;
end;
end;
function TCodeSignature.GetCertificates(aIndex: word): TCertificate;
var
VerifyParams :TCryptVerifyMessagePara;
Context :PCCERT_CONTEXT;
begin
OpenFile;
if aIndex > Count -1 then
raise ERangeError.Create('Index hors limite');
var Cert :PWinCertificate := AllocMem(SizeOf(TWinCertificate));
try
if not ImageGetCertificateHeader(FHFile, aIndex, Cert^) then
RaiseLastOSError;
ReallocMem(Cert, SizeOf(TWinCertificate) +Cert.dwLength);
if not ImageGetCertificateData(FHFile, aIndex, Cert, Cert.dwLength) then
RaiseLastOSError;
ZeroMemory(@VerifyParams, SizeOf(TCryptVerifyMessagePara));
VerifyParams.cbSize := SizeOf(TCryptVerifyMessagePara);
VerifyParams.dwMsgAndCertEncodingType := EncodingTypes;
if not CryptVerifyMessageSignature(VerifyParams, aIndex, @Cert.bCertificate, Cert.dwLength, nil, nil, @Context) then
RaiseLastOSError;
try
SetLength(Result.Company, CertGetNameString(Context, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, nil, nil, 0) -1);
CertGetNameString(Context, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, nil, PChar(Result.Company), Result.Company.Length +1);
Result.Algorithm := string(Context.pCertInfo.SignatureAlgorithm.pszObjId);
Result.Version := TCertificateVersion(Context.pCertInfo.dwVersion);
Result.Subject := DecodeString(Context.pCertInfo.Subject);
Result.Issuer := DecodeString(Context.pCertInfo.Issuer);
Result.FromDate := FileTimeToDateTime(Context.pCertInfo.NotBefore);
Result.ToDate := FileTimeToDateTime(Context.pCertInfo.NotAfter);
Result.EncodingType := TEncodingType(Context.dwCertEncodingType);
finally
CertFreeCertificateContext(Context);
end;
finally
FreeMem(Cert);
end;
end;
function TCodeSignature.GetCount: word;
begin
OpenFile;
ImageEnumerateCertificates(FHFile, CERT_SECTION_TYPE_ANY, @Result, nil, 0);
end;
function TCodeSignature.GetSigned: boolean;
var
FileInfo :TWinTrustFileInfo;
TrustData :TWinTrustData;
begin
ZeroMemory(@FileInfo, SizeOf(TWinTrustFileInfo));
FileInfo.cbStruct := SizeOf(TWinTrustFileInfo);
FileInfo.pcwszFilePath := PChar(FFilename);
ZeroMemory(@TrustData, SizeOf(TWinTrustData));
TrustData.cbStruct := SizeOf(TWinTrustData);
TrustData.dwUIChoice := WTD_UI_NONE;
TrustData.fdwRevocationChecks := WTD_REVOKE_NONE;
TrustData.dwUnionChoice := WTD_CHOICE_FILE;
TrustData.pFile := @FileInfo;
Result := WinVerifyTrust(INVALID_HANDLE_VALUE, WINTRUST_ACTION_GENERIC_VERIFY_V2, @TrustData) = 0;
end;
class function TCodeSignature.IsSigned(const aFileName: string): boolean;
begin
with Create(aFileName) do
try
try
Result := GetSigned;
finally
Free;
end;
except
Result := FALSE;
end;
end;
procedure TCodeSignature.OpenFile;
begin
if FHFile <> 0 then Exit;
if not TFile.Exists(FFileName) then
raise EInOutError.CreateFmt('Le fichier "%s" n''existe pas.', [FFileName]);
FHFile := CreateFile(PChar(FFileName), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
if FHFile = INVALID_HANDLE_VALUE then
raise EInOutError.CreateFmt('Le fichier "%s" ne peut pas être ouvert.', [FFileName]);
end;
procedure TCodeSignature.SetFileName(const Value: string);
begin
CloseFile;
FFileName := Value;
end;
end. |