WINTRUST_DATA = packed record
cbStruct: DWORD; // = sizeof(WINTRUST_DATA)
pPolicyCallbackData: pointer; // optional - auf 0 setzen
pSIPClientData: pointer; // optional - auf 0 setzen
dwUIChoice: DWORD; // benötigt, UI auswahl
fdwRevocationChecks: DWORD; // benötigt, auf zurückgezogene Zertifikate prüfen (online ben.)
dwUnionChoice: DWORD; // benötigt, welche Datenstruktur soll verwendet werden
pWTDINFO: pointer; // Pointer zu einer der Wintrust_X_Info Strukturen
pFake: pointer; //Fake Pointer - nötig damit der Speicer wieder freigegeben wird
pFake1: pointer; //Fake Pointer - nötig damit der Speicer wieder freigegeben wird
pFake2: pointer; //Fake Pointer - nötig damit der Speicer wieder freigegeben wird
pFake3: pointer; //Fake Pointer - nötig damit der Speicer wieder freigegeben wird
dwStateAction: DWORD;
hWVTStateData: THANDLE;
pwszURLReference: PWChar;
dwProvFlags: DWORD;
dwUIContext: DWORD;
end;
PWINTRUST_DATA = ^WINTRUST_DATA;
//Handle und Pointer auf KatalogAdminKontext
HCatAdmin = THANDLE;
PHCatAdmin = ^HCatAdmin;
//Catalog Admin Kontext öffnen und falls nicht möglich Prozedur verlassen
if CryptCATAdminAcquireContext(@hCatAdminContext, nil, 0) = False then Exit;
//Filehandle auf die zu prüfende Datei holen
hFile := CreateFile(PChar(string(sFilename)), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
//Wenn das Handle nicht erhalten wurde Prozedur verlassen
if hFile = INVALID_HANDLE_VALUE then Exit;
//iaBytescount nach größe des Arrays setzen
iByteCount := SizeOf(aByteHash);
//ByteArray mit Hash füllen lassen und die Größe in iByteCount bekommen
CryptCATAdminCalcHashFromFileHandle(hFile, @iByteCount, @aByteHash, 0);
// MemberTag brechnen (vom ByteArray auf HEX)
for x := 0 to iByteCount - 1 do
begin
swMemberTag := swMemberTag + IntToHex(aByteHash[x], 2);
end;
//FileHandle schließen - wird nicht mehr gebraucht
CloseHandle(hFile);
//Erste Prüfung erfolgt mit WINTRUST_DATA.dwUnionChoice := WTD_CHOICE_CATALOG;
//also muss WINTRUST_CATALOG_INFO gefüllt werden
//
//Handle auf den Katalog Kontext holen
hCatalogContext := CryptCATAdminEnumCatalogFromHash(hCatAdminContext, @aByteHash, iByteCount, 0, nil);
//Wenn das Handle 0 ist muss die Prüfung mit der
//WINTRUST_DATA.dwUnionChoice := WTD_CHOICE_FILE; Struktur durchgeführt werden
if hCatalogContext = 0 then
begin
//CatalogContext = 0 also
//
//WINTRUST_FILE_INFO Struktur initialisieren und füllen
WTDFileInfo.cbStruct := SizeOf(WTDFileInfo);
WTDFileInfo.pcwszFilePath := PWideChar(swFilename);
WTDFileInfo.pgKnownSubject := nil;
WTDFileInfo.hFile := 0;
//WINTRUST_DATA Struktur initialisieren und füllen
WTrustData.cbStruct := SizeOf(WTrustData);
WTrustData.dwUnionChoice := WTD_CHOICE_FILE; //WINTRUST_FILE_INFO Struktur wählen
WTrustData.pWTDINFO := @WTDFileInfo; //Pointer zu WINTRUST_FILE_INFO
WTrustData.dwUIChoice := WTD_UI_NONE;
WTrustData.fdwRevocationChecks := WTD_REVOKE_NONE;
WTrustData.dwStateAction := WTD_STATEACTION_IGNORE;
WTrustData.dwProvFlags := WTD_SAFER_FLAG; //UI bei XP SP2 unterbinden
WTrustData.hWVTStateData := 0;
WTrustData.pwszURLReference := nil;
end
else
begin
//CatalogContext <> 0 also CATALOG_INFO benutzen
//
//CATALOG_INFO Struktur füllen
CryptCATCatalogInfoFromContext(hCatalogContext, @CatalogInfo, 0);
//WINTRUST_CATALOG_INFO Struktur initialisieren und füllen
WTDCatalogInfo.cbStruct := SizeOf(WTDCatalogInfo);
WTDCatalogInfo.pcwszCatalogFilePath := CatalogInfo.sCatalogFile;
WTDCatalogInfo.pcwszMemberFilePath := PWideChar(swFilename);
WTDCatalogInfo.pcwszMemberTag := PWideChar(swMemberTag);
//WINTRUST_DATA Struktur initialisieren und füllen
WTrustData.cbStruct := SizeOf(WTrustData);
WTrustData.dwUnionChoice := WTD_CHOICE_CATALOG; //WINTRUST_CATALOG_INFO Struktur wählen
WTrustData.pWTDINFO := @WTDCatalogInfo; //Pointer zu WINTRUST_CATALOG_INFO
WTrustData.dwUIChoice := WTD_UI_NONE;
WTrustData.fdwRevocationChecks := WTD_REVOKE_NONE;
WTrustData.pPolicyCallbackData := nil;
WTrustData.pSIPClientData := nil;
WTrustData.dwStateAction := WTD_STATEACTION_VERIFY;
WTrustData.dwProvFlags := 0; //WTD_SAFER_FLAG; //UI bei XP SP2 unterbinden
WTrustData.hWVTStateData := 0;
WTrustData.pwszURLReference := nil;
end;
//WinVerifyTrust aufrufen um die Prüfung durchzuführen
ilRet := WinVerifyTrust(INVALID_HANDLE_VALUE, @WINTRUST_ACTION_GENERIC_VERIFY_V2, @WTrustData);
//Wenn Erg. 0 ist dann ist das File Trusted - alle anderen Werte sind Fehlercodes
if ilRet = 0 then
begin
Result := True
end
else
Result := False;
//Handle zum Catalogfile schließen
CryptCATAdminReleaseCatalogContext(hCatAdminContext, hCatalogContext, 0);
//Catalog Admin Kontext schließen
CryptCATAdminReleaseContext(hCatAdminContext, 0);
end;
initialization
//Dynamisches laden der Dll und deren Funktionen
hLibWintrust := LoadLibrary('wintrust.dll');
if hLibWintrust >= 32 then { success }
begin
CryptCATAdminAcquireContext := GetProcAddress(hLibWintrust, 'CryptCATAdminAcquireContext');
CryptCATAdminReleaseContext := GetProcAddress(hLibWintrust, 'CryptCATAdminReleaseContext');
CryptCATAdminCalcHashFromFileHandle := GetProcAddress(hLibWintrust, 'CryptCATAdminCalcHashFromFileHandle');
CryptCATAdminEnumCatalogFromHash := GetProcAddress(hLibWintrust, 'CryptCATAdminEnumCatalogFromHash');
CryptCATCatalogInfoFromContext := GetProcAddress(hLibWintrust, 'CryptCATCatalogInfoFromContext');
CryptCATAdminReleaseCatalogContext := GetProcAddress(hLibWintrust, 'CryptCATAdminReleaseCatalogContext');
WinVerifyTrust := GetProcAddress(hLibWintrust, 'WinVerifyTrust');
end;
finalization
FreeLibrary(hLibWintrust);
end.