![]() |
Anzahl von Zertifikaten/Signaturen in Dateien - ImageEnumerateCertificates?
Hallo Leute :).
Da die Informationslage zum Thema Zertifikate/Signaturen in Dateien und wie die Informationen daraus extrahiert werden können, nicht gerade prächtig ist, und ich auf keinen grünen Zweig komme, möchte ich eure Hilfe an Anspruch nehmen. Gleich zu Beginn mal ein kleines Testprogramm, das sich jeder kompilieren kann, wenn nötig:
Delphi-Quellcode:
program Project1;
{$APPTYPE CONSOLE} uses SysUtils, Signatures ; var i: integer; begin try if (ParamCount > 0) then for i:= 1 to ParamCount do WriteLn(Format('%s: %u', [ParamStr(i), Signatures.GetCertificateCount(ParamStr(i))])); except on E: Exception do begin WriteLn(Format('Exception %s: %s', [E.ClassName, E.Message])); Halt(1); end; end; end.
Delphi-Quellcode:
Die Funktion gibt eine 1 zurück für Dateien, die ein Zertifikat enthalten, und eine 0 für Dateien, die keines enthalten. Soweit so gut.
unit Signatures;
uses Windows; interface const CERT_SECTION_TYPE_ANY = $FF; // Any Certificate type const IMAGEHLPDLL = 'imagehlp.dll'; function ImageEnumerateCertificates(FileHandle: THandle; TypeFilter: WORD; CertificateCount, Indices: PDWORD; IndexCount: DWORD): BOOL; stdcall; external IMAGEHLPDLL; function GetCertificateCount(const AFileName: string): DWORD; implementation function GetCertificateCount(const AFileName: string): DWORD; var hExe: THandle; CertCount: DWORD; begin Result:= 0; hExe := CreateFile(PChar(AFilename), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_RANDOM_ACCESS, 0); if (hExe <> INVALID_HANDLE_VALUE) then try if ImageEnumerateCertificates(hExe, CERT_SECTION_TYPE_ANY, @CertCount, nil, 0) then Result:= CertCount; finally CloseHandle(hExe); end; end; end. Problemstellung: Selbst bei dual signierten Dateien, also solchen, die laut Eigenschaften > Register Digitale Signaturen einerseits eine Signatur mit SHA1 und eine mit SHA256 besitzen, liefert die Funktion immer nur eine 1 als Ergebnis. Die Doku zu ![]() Zitat:
Grüße Dalai |
AW: Anzahl von Zertifikaten/Signaturen in Dateien - ImageEnumerateCertificates?
Hallo,
vielleicht zählt ja die Seriennummer mit? 2 Zertifikate mit gleicher Seriennummer = 1Zertifikat? |
AW: Anzahl von Zertifikaten/Signaturen in Dateien - ImageEnumerateCertificates?
Wenn beide Signierungen mit dem gleichen Zertifikat durchgeführt werden hast du ja auch nur ein Zertifikat angehängt.
Aber Probier mal das hier: ![]() |
AW: Anzahl von Zertifikaten/Signaturen in Dateien - ImageEnumerateCertificates?
Zitat:
Code:
Kann das dann vom selben Zertifikat stammen?
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorsvw.exe (von .NET 4.6.2)
SHA1: 33 00 00 01 2f e1 e3 b9 bc 80 bd 8e e6 00 01 00 00 01 2f SHA256: 33 00 00 00 ed 9a 4a 53 36 52 2e 14 aa 00 00 00 00 00 ed Zitat:
Grüße Dalai |
AW: Anzahl von Zertifikaten/Signaturen in Dateien - ImageEnumerateCertificates?
Vorweg die gute Nachricht: es funktioniert :-D, auch unter Windows 7, sogar wenn selbiges keine weiteren Updates installiert hat (die mit SHA-2 zu tun haben). Vielleicht stimmt die Doku von MS bzgl. der WINTRUST_SIGNATURE_SETTINGS nicht (mehr)? Wer weiß.
Nach einer Übersetzung der nötigen Datenstrukturen nach Delphi:
Delphi-Quellcode:
komme ich mit diesem Code an die Anzahl der sekundären Signaturen
type
_CERT_STRONG_SIGN_SERIALIZED_INFO = record dwFlags: DWORD; pwszCNGSignHashAlgids: LPWSTR; pwszCNGPubKeyMinBitLengths: LPWSTR; end; CERT_STRONG_SIGN_SERIALIZED_INFO = _CERT_STRONG_SIGN_SERIALIZED_INFO; PCERT_STRONG_SIGN_SERIALIZED_INFO = ^CERT_STRONG_SIGN_SERIALIZED_INFO; _CERT_STRONG_SIGN_PARA = record cbSize: DWORD; dwInfoChoice: DWORD; union : record // Not sure if the definition of this union is correct... case DWORD of 0: (pvInfo: Pointer); 1: (pSerializedInfo: PCERT_STRONG_SIGN_SERIALIZED_INFO); 2: (pszOID: LPSTR); end; end; CERT_STRONG_SIGN_PARA = _CERT_STRONG_SIGN_PARA; PCERT_STRONG_SIGN_PARA = ^CERT_STRONG_SIGN_PARA; WINTRUST_SIGNATURE_SETTINGS_ = record cbStruct: DWORD; dwIndex: DWORD; dwFlags: DWORD; cSecondarySigs: DWORD; dwVerifiedSigIndex: DWORD; pCryptoPolicy: PCERT_STRONG_SIGN_PARA; end; WINTRUST_SIGNATURE_SETTINGS = WINTRUST_SIGNATURE_SETTINGS_; PWINTRUST_SIGNATURE_SETTINGS = ^WINTRUST_SIGNATURE_SETTINGS; const WSS_VERIFY_SPECIFIC = $00000001; // Set this value if you set the dwIndex parameter. WSS_GET_SECONDARY_SIG_COUNT = $00000002; // Set this value to return the number of secondary // signatures found in the cSecondarySigs member. type TWinTrustFileInfo = record cbStruct: DWORD; pcwszFilePath: LPCWSTR; hFile: THandle; pgKnownSubject: PGUID; end; type _WINTRUST_DATA = record cbStruct: DWORD; pPolicyCallbackData: Pointer; pSIPClientData: Pointer; dwUIChoice: DWORD; fdwRevocationChecks: DWORD; dwUnionChoice: DWORD; InfoUnion: record case DWORD of {WTD_CHOICE_FILE} 0: (pFile: PWinTrustFileInfo); // {WTD_CHOICE_CATALOG} 1: (pCatalog: PWinTrustCatalogInfo); // {WTD_CHOICE_BLOB} 2: (pBlob: PWinTrustBlobInfo); // {WTD_CHOICE_SIGNER} 3: (pSgnr: PWinTrustSgnrInfo); {WTD_CHOICE_CERT} 4: (pCert: PWinTrustCertInfo); end; dwStateAction: DWORD; hWVTStateData: THandle; pwszURLReference: LPCWSTR; dwProvFlags: DWORD; dwUIContext: DWORD; pSignatureSettings: PWINTRUST_SIGNATURE_SETTINGS; // Windows 8 and Windows Server 2012: // Support for this member begins. end; TWinTrustData = WINTRUST_DATA;
Delphi-Quellcode:
und kann diese mit der Zahl der primären Signaturen/Zertifikate addieren. Bei der genannten Datei aus dem .NET Framework kommt also tatsächlich eine 2 als Summe.
function GetSecondarySignatureCount(const AFileName: string): DWORD;
var Lwtd: TWinTrustData; Lfileinfo: TWinTrustFileInfo; Lsigsettings: WINTRUST_SIGNATURE_SETTINGS; begin Result:= 0; ZeroMemory(@Lfileinfo, SizeOf(Lfileinfo)); Lfileinfo.cbStruct := SizeOf(Lfileinfo); Lfileinfo.pcwszFilePath := PWideChar(WideString(AFilename)); ZeroMemory(@Lsigsettings, SizeOf(WINTRUST_SIGNATURE_SETTINGS)); Lsigsettings.cbStruct:= SizeOf(WINTRUST_SIGNATURE_SETTINGS); Lsigsettings.dwFlags:= WSS_GET_SECONDARY_SIG_COUNT; ZeroMemory(@Lwtd, SizeOf(TWinTrustData)); with Lwtd do begin cbStruct := SizeOf(TWinTrustData); dwUIChoice := WTD_UI_NONE; fdwRevocationChecks := WTD_REVOKE_NONE; dwUnionChoice := WTD_CHOICE_FILE; dwStateAction := WTD_STATEACTION_IGNORE; InfoUnion.pFile := @Lfileinfo; pSignatureSettings:= @Lsigsettings; end; if Windows.WinVerifyTrust(INVALID_HANDLE_VALUE, WINTRUST_ACTION_GENERIC_VERIFY_V2, @Lwtd) = ERROR_SUCCESS then Result:= Lwtd.pSignatureSettings.cSecondarySigs; end; Danke für den Stupser in die richtige Richtung :dp: Grüße Dalai |
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:19 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz