Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oDsig
LOCAL oSbXml
LOCAL oXmlKeyInfo
LOCAL cCertBase64
LOCAL oCert

nSuccess := 0

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

oDsig := CreateObject("Chilkat.XmlDSig")
oSbXml := CreateObject("Chilkat.StringBuilder")

nSuccess := oSbXml:LoadFile("c:/aaworkarea/elias/3/face_f09006808443a699d1b.xml", "utf-8")
IF (nSuccess != 1)
    ? "Failed to load XML file."
    oDsig:destroy()
    oSbXml:destroy()
    RETURN
ENDIF

nSuccess := oDsig:LoadSignatureSb(oSbXml)
IF (nSuccess != 1)
    ? oDsig:LastErrorText
    oDsig:destroy()
    oSbXml:destroy()
    RETURN
ENDIF

//  Get the KeyInfo XML.
oXmlKeyInfo := oDsig:GetKeyInfo()
IF (oDsig:LastMethodSuccess != 1)
    ? oDsig:LastErrorText
    oDsig:destroy()
    oSbXml:destroy()
    RETURN
ENDIF

? oXmlKeyInfo:GetXml()
? "----"

//  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>
cCertBase64 := oXmlKeyInfo:GetChildContent("*:X509Data|*:X509Certificate")
IF (oXmlKeyInfo:LastMethodSuccess != 1)
    ? "No X509Certificate found in the KeyInfo."
    oDsig:destroy()
    oSbXml:destroy()
    RETURN
ENDIF

//  Load a certificate object w/ the base64.
oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadFromBase64(cCertBase64)
IF (nSuccess != 1)
    ? oCert:LastErrorText
    oDsig:destroy()
    oSbXml:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  Examine the cert..
? "SubjectDN: " + oCert:SubjectDN
? "IssuerDN: " + oCert:IssuerDN
? "SerialNumber as Decimal: " + oCert:SerialDecimal

oXmlKeyInfo:destroy()

oDsig:destroy()
oSbXml:destroy()
oCert:destroy()