Discuss this help topic in SecureBlackbox Forum
Validate CryptoAPI signature
To validate a signature with CryptoAPI signature handler you need to
C#:
bool ValidateBinaryCryptoAPISignature(string sourceFilename)
{
using (TElOfficeDocument Document = new TElOfficeDocument())
{
Document.Open(sourceFilename);
if ((Document.DocumentFormat != TSBOfficeDocumentFormat.Binary) || !Document.IsSigned)
throw new Exception("Cannot validate Binary CryptoAPI signature");
bool Result = true;
for (int i = 0; i < Document.SignatureHandlerCount; i++)
{
if (!(Document.get_SignatureHandlers(i) is TElOfficeBinaryCryptoAPISignatureHandler))
throw new Exception("Unexpected signature handler");
TElOfficeBinaryCryptoAPISignatureHandler BinCryptoAPISigHandler = (TElOfficeBinaryCryptoAPISignatureHandler)(Document.get_SignatureHandlers(i));
TSBOfficeBinarySignatureValidationStatus BinValidationStatus = BinCryptoAPISigHandler.Validate();
Result = Result && (BinValidationStatus == TSBOfficeBinarySignatureValidationStatus.Valid);
TElX509Certificate SignerCertificate = BinCryptoAPISigHandler.Certificate;
TElCustomCertStorage AdditionalCertificates = BinCryptoAPISigHandler.IntermediateCertificatesStorage;
DateTime SigningTime = BinCryptoAPISigHandler.SignTime;
// then use TElX509CertificateValidator object to validate the signer certificate
}
return Result;
}
}
Delphi:
function ValidateBinaryCryptoAPISignature(const SourceFilename : string) : Boolean;
var
Document : TElOfficeDocument;
BinCryptoAPISigHandler : TElOfficeBinaryCryptoAPISignatureHandler;
BinValidationStatus : TSBOfficeBinarySignatureValidationStatus;
SignerCertificate : TElX509Certificate;
AdditionalCertificates : TElCustomCertStorage;
SigningTime : TDateTime;
i : Integer;
begin
Document := TElOfficeDocument.Create(nil);
try
Document.Open(SourceFilename);
if (Document.DocumentFormat <> dfBinary) or not Document.IsSigned then
raise Exception.Create('Cannot validate Binary CryptoAPI signature');
Result := true;
for i := 0 to Document.SignatureHandlerCount - 1 do
begin
if Document.SignatureHandlers[i] is TElOfficeBinaryCryptoAPISignatureHandler then
BinCryptoAPISigHandler := TElOfficeBinaryCryptoAPISignatureHandler(Document.SignatureHandlers[i])
else
raise Exception.Create('Unexpected signature handler');
BinValidationStatus := BinCryptoAPISigHandler.Validate();
Result := Result and (BinValidationStatus = bsvsValid);
SignerCertificate := BinCryptoAPISigHandler.Certificate;
AdditionalCertificates := BinCryptoAPISigHandler.IntermediateCertificatesStorage;
SigningTime := BinCryptoAPISigHandler.SignTime;
// then use TElX509CertificateValidator object to validate the signer certificate
end;
finally
FreeAndNil(Document);
end;
end;