Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Examine KeyInfo Certificate in XML Signature
See more XML Digital Signatures Examples
This example loads signed XML and gets the signing certificate, assuming the certificate is contained in X509Certificate within the KeyInfo.Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.StringBuilder,
Chilkat.Xml,
Chilkat.XmlDSig,
Chilkat.Cert;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
dsig: TXmlDSig;
sbXml: TStringBuilder;
xmlKeyInfo: TXml;
certBase64: string;
cert: TCert;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
dsig := TXmlDSig.Create;
sbXml := TStringBuilder.Create;
success := sbXml.LoadFile('c:/aaworkarea/elias/3/face_f09006808443a699d1b.xml','utf-8');
if (success <> True) then
begin
WriteLn('Failed to load XML file.');
Exit;
end;
success := dsig.LoadSignatureSb(sbXml);
if (success <> True) then
begin
WriteLn(dsig.LastErrorText);
Exit;
end;
// Get the KeyInfo XML.
xmlKeyInfo := dsig.GetKeyInfo();
if (dsig.LastMethodSuccess <> True) then
begin
WriteLn(dsig.LastErrorText);
Exit;
end;
WriteLn(xmlKeyInfo.GetXml());
WriteLn('----');
// Assuming the X509Certificate is in the KeyInfo, it will look like this:
// <ds:KeyInfo Id="...">
// <ds:KeyValue>
// ...
// <ds:X509Data>
// <ds:X509Certificate>MIIHAz...</ds:X509Certificate>
// </ds:X509Data>
// </ds:KeyInfo>
certBase64 := xmlKeyInfo.GetChildContent('*:X509Data|*:X509Certificate');
if (xmlKeyInfo.LastMethodSuccess <> True) then
begin
WriteLn('No X509Certificate found in the KeyInfo.');
Exit;
end;
// Load a certificate object w/ the base64.
cert := TCert.Create;
success := cert.LoadFromBase64(certBase64);
if (success <> True) then
begin
WriteLn(cert.LastErrorText);
Exit;
end;
// Examine the cert..
WriteLn('SubjectDN: ' + cert.SubjectDN);
WriteLn('IssuerDN: ' + cert.IssuerDN);
WriteLn('SerialNumber as Decimal: ' + cert.SerialDecimal);
xmlKeyInfo.Free;
dsig.Free;
sbXml.Free;
cert.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.