Sample code for 30+ languages & platforms
AutoIt

Get Certificates within XML Signature

See more XML Digital Signatures Examples

Demonstrates how to get the certificates contained within an XML signature.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.

$oSbXml = ObjCreate("Chilkat.StringBuilder")

; Load XML containing one or more signatures.
$bSuccess = $oSbXml.LoadFile("qa_data/xml_dsig_valid_samples/multipleSigners/sp.pdf.XAdES.xml","utf-8")
If ($bSuccess = False) Then
    ConsoleWrite("Failed to load the XML file.." & @CRLF)
    Exit
EndIf

$oDsig = ObjCreate("Chilkat.XmlDSig")

; First load the XML containing the signatures to be verified.
; Note that this particular Signature already contains the RSA public key that will be used
; for verification.
$bSuccess = $oDsig.LoadSignatureSb($oSbXml)
If ($bSuccess <> True) Then
    ConsoleWrite($oDsig.LastErrorText & @CRLF)
    Exit
EndIf

; For each signature, verify and also get the certificate(s) contained within each Signature.
Local $i = 0
$oSaCerts = ObjCreate("Chilkat.StringArray")
$oCert = ObjCreate("Chilkat.Cert")

ConsoleWrite("numSignatures = " & $oDsig.NumSignatures & @CRLF)

While $i < $oDsig.NumSignatures
    ; Select the Nth signature by setting the Selector property.
    $oDsig.Selector = $i

Local $bVerifyReferenceDigests = True
Local $bVerified = $oDsig.VerifySignature($bVerifyReferenceDigests)
    ConsoleWrite("Signature " & ($i + 1) & " verified = " & $bVerified & @CRLF)

    ; Get the certificates embedded in this signature.
    $oSaCerts.Clear 
    $bSuccess = $oDsig.GetCerts($oSaCerts)
    If ($bSuccess = True) Then
Local $iJ = 0
        While $iJ < $oSaCerts.Count
            $bSuccess = $oCert.LoadFromBase64($oSaCerts.GetString($iJ))
            If ($bSuccess = True) Then
                ConsoleWrite("    " & $oCert.SubjectDN & @CRLF)
            EndIf

            $iJ = $iJ + 1
        Wend
    EndIf

    $i = $i + 1
Wend