Sample code for 30+ languages & platforms
PureBasic

Create XML Signature with External XML Reference

See more XML Digital Signatures Examples

Demonstrates how to create an XML digital signature where the referenced data is external. In this case, the data is an XML file located at the following URL: https://www.chilkatsoft.com/data/helloWorld.xml

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

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

    ; This example inserts an XML signature in the following XML:

    ; <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    ; <abc>
    ;   <xyz>
    ; 	<name>helloWorld.xml</name>
    ; 	<url>https://www.chilkatsoft.com/data/helloWorld.xml</url>
    ;   </xyz>
    ; </abc>

    ; Build the above XML to be signed.
    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(xml, "abc")
    CkXml::ckUpdateChildContent(xml,"xyz|name","helloWorld.xml")
    CkXml::ckUpdateChildContent(xml,"xyz|url","https://www.chilkatsoft.com/data/helloWorld.xml")

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

    success = CkCert::ckLoadPfxFile(cert,"qa_data/pfx/test_secret.pfx","secret")
    If success = 0
        Debug CkCert::ckLastErrorText(cert)
        CkXml::ckDispose(xml)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

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

    ; Indicate the location within the XML the Signature will be inserted.
    CkXmlDSigGen::setCkSigLocation(gen, "abc|xyz")

    ; Get the content of the XML data to be referenced.
    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    success = CkHttp::ckQuickGetSb(http,"https://www.chilkatsoft.com/data/helloWorld.xml",sbExternalXml)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkXml::ckDispose(xml)
        CkCert::ckDispose(cert)
        CkXmlDSigGen::ckDispose(gen)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbExternalXml)
        ProcedureReturn
    EndIf

    success = CkXmlDSigGen::ckAddExternalXmlRef(gen,"https://www.chilkatsoft.com/data/helloWorld.xml",sbExternalXml,"sha256","EXCL_C14N","")
    If success = 0
        Debug CkXmlDSigGen::ckLastErrorText(gen)
        CkXml::ckDispose(xml)
        CkCert::ckDispose(cert)
        CkXmlDSigGen::ckDispose(gen)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbExternalXml)
        ProcedureReturn
    EndIf

    ; Provide the private key for signing via the certificate, and indicate that
    ; we want the base64 of the certificate embedded in the KeyInfo.
    CkXmlDSigGen::setCkKeyInfoType(gen, "X509Data")
    CkXmlDSigGen::setCkX509Type(gen, "Certificate")

    bUsePrivateKey.i = 1
    success = CkXmlDSigGen::ckSetX509Cert(gen,cert,bUsePrivateKey)
    If success <> 1
        Debug CkXmlDSigGen::ckLastErrorText(gen)
        CkXml::ckDispose(xml)
        CkCert::ckDispose(cert)
        CkXmlDSigGen::ckDispose(gen)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbExternalXml)
        ProcedureReturn
    EndIf

    ; Indicate we want an indented signature for readability.
    ; This can be removed after debugging is finished..
    CkXmlDSigGen::setCkBehaviors(gen, "IndentedSignature")

    ; Now create and insert the Signature
    sbXml.i = CkStringBuilder::ckCreate()
    If sbXml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::ckGetXmlSb(xml,sbXml)
    success = CkXmlDSigGen::ckCreateXmlDSigSb(gen,sbXml)
    If success <> 1
        Debug CkXmlDSigGen::ckLastErrorText(gen)
        CkXml::ckDispose(xml)
        CkCert::ckDispose(cert)
        CkXmlDSigGen::ckDispose(gen)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbExternalXml)
        CkStringBuilder::ckDispose(sbXml)
        ProcedureReturn
    EndIf

    ; Examine the XML with the digital signature inserted
    Debug CkStringBuilder::ckGetAsString(sbXml)

    ; Here is the output:

    ; <?xml version="1.0" encoding="utf-8"?>
    ; <abc>
    ;     <xyz>
    ;         <name>helloWorld.xml</name>
    ;         <url>https://www.chilkatsoft.com/data/helloWorld.xml</url>
    ;     <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    ;   <ds:SignedInfo>
    ;     <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    ;     <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
    ;     <ds:Reference URI="https://www.chilkatsoft.com/data/helloWorld.xml">
    ;       <ds:Transforms>
    ;         <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    ;       </ds:Transforms>
    ;       <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    ;       <ds:DigestValue>H/s775OZhel6wdBhe02l4LK6m0lkplOisKxAbVwWxp0=</ds:DigestValue>
    ;     </ds:Reference>
    ;   </ds:SignedInfo>
    ;   <ds:SignatureValue>UruwnBqh..Ll4IzUA==</ds:SignatureValue>
    ;   <ds:KeyInfo>
    ;     <ds:X509Data>
    ;       <ds:X509Certificate>MIIHAz...nUZWCaDE=</ds:X509Certificate>
    ;     </ds:X509Data>
    ;   </ds:KeyInfo>
    ; </ds:Signature></xyz>
    ; </abc>

    ; Let's verify the signature...
    verifier.i = CkXmlDSig::ckCreate()
    If verifier.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkXmlDSig::ckLoadSignatureSb(verifier,sbXml)
    If success <> 1
        Debug CkXmlDSig::ckLastErrorText(verifier)
        CkXml::ckDispose(xml)
        CkCert::ckDispose(cert)
        CkXmlDSigGen::ckDispose(gen)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbExternalXml)
        CkStringBuilder::ckDispose(sbXml)
        CkXmlDSig::ckDispose(verifier)
        ProcedureReturn
    EndIf

    ; Let's examine the external references, and fetch the data for each..
    sbExternalData.i = CkStringBuilder::ckCreate()
    If sbExternalData.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    numRefs.i = CkXmlDSig::ckNumReferences(verifier)
    i.i = 0
    While i < numRefs
        bExternal.i = CkXmlDSig::ckIsReferenceExternal(verifier,i)
        If bExternal = 1
            uri.s = CkXmlDSig::ckReferenceUri(verifier,i)

            ; We're assuming the URI is an https:// or http:// URL...
            ; Let's also assume we know that the referenced data is text and we want the utf-8 byte representation.
            CkStringBuilder::ckClear(sbExternalData)
            success = CkHttp::ckQuickGetSb(http,uri,sbExternalData)
            If success = 0
                Debug CkHttp::ckLastErrorText(http)
                CkXml::ckDispose(xml)
                CkCert::ckDispose(cert)
                CkXmlDSigGen::ckDispose(gen)
                CkHttp::ckDispose(http)
                CkStringBuilder::ckDispose(sbExternalXml)
                CkStringBuilder::ckDispose(sbXml)
                CkXmlDSig::ckDispose(verifier)
                CkStringBuilder::ckDispose(sbExternalData)
                ProcedureReturn
            EndIf

            ; Because the referenced external data was explicitly XML, it was the canonicalized XML that was digested.
            ; We need to canonicalize the XML in the same way as when signed.
            canonXml.s = CkXmlDSig::ckCanonicalizeXml(verifier,CkStringBuilder::ckGetAsString(sbExternalData),"EXCL_C14N",0)
            CkStringBuilder::ckSetString(sbExternalData,canonXml)

            success = CkXmlDSig::ckSetRefDataSb(verifier,i,sbExternalData,"utf-8")
            If success = 0
                Debug CkXmlDSig::ckLastErrorText(verifier)
                CkXml::ckDispose(xml)
                CkCert::ckDispose(cert)
                CkXmlDSigGen::ckDispose(gen)
                CkHttp::ckDispose(http)
                CkStringBuilder::ckDispose(sbExternalXml)
                CkStringBuilder::ckDispose(sbXml)
                CkXmlDSig::ckDispose(verifier)
                CkStringBuilder::ckDispose(sbExternalData)
                ProcedureReturn
            EndIf

        EndIf

        i = i + 1
    Wend

    ; Now that we have the external data available, we can verify the reference digest(s) and the signature.
    bVerified.i = CkXmlDSig::ckVerifySignature(verifier,1)
    If bVerified <> 1
        Debug CkXmlDSig::ckLastErrorText(verifier)
        CkXml::ckDispose(xml)
        CkCert::ckDispose(cert)
        CkXmlDSigGen::ckDispose(gen)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbExternalXml)
        CkStringBuilder::ckDispose(sbXml)
        CkXmlDSig::ckDispose(verifier)
        CkStringBuilder::ckDispose(sbExternalData)
        ProcedureReturn
    EndIf

    Debug "Signature verified!"


    CkXml::ckDispose(xml)
    CkCert::ckDispose(cert)
    CkXmlDSigGen::ckDispose(gen)
    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(sbExternalXml)
    CkStringBuilder::ckDispose(sbXml)
    CkXmlDSig::ckDispose(verifier)
    CkStringBuilder::ckDispose(sbExternalData)


    ProcedureReturn
EndProcedure