Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkCert.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkXmlDSig.pb"

Procedure ChilkatExample()

    success.i = 0

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

    dsig.i = CkXmlDSig::ckCreate()
    If dsig.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    sbXml.i = CkStringBuilder::ckCreate()
    If sbXml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkStringBuilder::ckLoadFile(sbXml,"c:/aaworkarea/elias/3/face_f09006808443a699d1b.xml","utf-8")
    If success <> 1
        Debug "Failed to load XML file."
        CkXmlDSig::ckDispose(dsig)
        CkStringBuilder::ckDispose(sbXml)
        ProcedureReturn
    EndIf

    success = CkXmlDSig::ckLoadSignatureSb(dsig,sbXml)
    If success <> 1
        Debug CkXmlDSig::ckLastErrorText(dsig)
        CkXmlDSig::ckDispose(dsig)
        CkStringBuilder::ckDispose(sbXml)
        ProcedureReturn
    EndIf

    ; Get the KeyInfo XML.
    xmlKeyInfo.i = CkXmlDSig::ckGetKeyInfo(dsig)
    If CkXmlDSig::ckLastMethodSuccess(dsig) <> 1
        Debug CkXmlDSig::ckLastErrorText(dsig)
        CkXmlDSig::ckDispose(dsig)
        CkStringBuilder::ckDispose(sbXml)
        ProcedureReturn
    EndIf

    Debug CkXml::ckGetXml(xmlKeyInfo)
    Debug "----"

    ; 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.s = CkXml::ckGetChildContent(xmlKeyInfo,"*:X509Data|*:X509Certificate")
    If CkXml::ckLastMethodSuccess(xmlKeyInfo) <> 1
        Debug "No X509Certificate found in the KeyInfo."
        CkXmlDSig::ckDispose(dsig)
        CkStringBuilder::ckDispose(sbXml)
        ProcedureReturn
    EndIf

    ; Load a certificate object w/ the base64.
    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckLoadFromBase64(cert,certBase64)
    If success <> 1
        Debug CkCert::ckLastErrorText(cert)
        CkXmlDSig::ckDispose(dsig)
        CkStringBuilder::ckDispose(sbXml)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ; Examine the cert..
    Debug "SubjectDN: " + CkCert::ckSubjectDN(cert)
    Debug "IssuerDN: " + CkCert::ckIssuerDN(cert)
    Debug "SerialNumber as Decimal: " + CkCert::ckSerialDecimal(cert)

    CkXml::ckDispose(xmlKeyInfo)



    CkXmlDSig::ckDispose(dsig)
    CkStringBuilder::ckDispose(sbXml)
    CkCert::ckDispose(cert)


    ProcedureReturn
EndProcedure