Sample code for 30+ languages & platforms
PureBasic

Verify XML Digital Signature having KeyName

See more XML Digital Signatures Examples

Demonstrates how to verify an XML digital signature where the KeyInfo part contains the KeyName element.
In this case, the public key is not specifically stored in the signed XML.
Rather, application specific information about the key is stored in the KeyName element.
This example uses the KeyName to find and matching public key.

This example requires Chilkat v9.5.0.69 or greater.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkXmlDSig.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkPublicKey.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ; The signed XML to be verified in this example contains the following:
    ; 
    ; <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    ; <Envelope>
    ;   <Header>
    ;     <Security>
    ;     <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/2009/xmldsig11#dsa-sha256"/><ds:Reference URI="#abc"><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>XTjDIHSEsDNTO9yn4cKtyXjRUjPFXkOQOLYI5mueZhk=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>NXOIMevGCqFl0Dwtp2KkqkT05GRV8CjmHZ9LhFpt8/d7+lkIi7mITA==</ds:SignatureValue><ds:KeyInfo><ds:KeyName>dsaKey_123</ds:KeyName></ds:KeyInfo></ds:Signature></Security>
    ;   </Header>
    ;   <Body Id="abc">
    ;     <z:FooBar xmlns:z="https://www.example-code.com"/>
    ;   </Body>
    ; </Envelope>
    ; 

    ; The above XML is available at https://www.chilkatsoft.com/exampleData/signedUsingKeyName.xml
    ; First we'll fetch the signed XML:

    url.s = "https://www.chilkatsoft.com/exampleData/signedUsingKeyName.xml"
    http.i = CkHttp::ckCreate()
    If http.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 = CkHttp::ckQuickGetSb(http,url,sbXml)
    If success <> 1
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbXml)
        ProcedureReturn
    EndIf

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

    ; Load the XML containing the signature to be verified.
    success = CkXmlDSig::ckLoadSignatureSb(verifier,sbXml)
    If success <> 1
        Debug CkXmlDSig::ckLastErrorText(verifier)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbXml)
        CkXmlDSig::ckDispose(verifier)
        ProcedureReturn
    EndIf

    ; (The Chilkat XML Digital Signature API has the capability to handle XML documents with multiple signatures.
    ; We know in advance that this signed XML has just one Signature, so we leave the dsig.Selector property at the
    ; default value of 0.)

    ; Let's get the KeyInfo XML so we can then get the KeyName.
    xmlKeyInfo.i = CkXmlDSig::ckGetKeyInfo(verifier)
    If CkXmlDSig::ckLastMethodSuccess(verifier) = 0
        Debug "There is no KeyInfo available in the Signature."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbXml)
        CkXmlDSig::ckDispose(verifier)
        ProcedureReturn
    EndIf

    ; Examine the XML:
    Debug CkXml::ckGetXml(xmlKeyInfo)

    ; The KeyInfo XML that is returned looks like this:
    ; 
    ; 	<ds:KeyInfo>
    ; 	    <ds:KeyName>dsaKey_123</ds:KeyName>
    ; 	</ds:KeyInfo>
    ; 

    ; Get the KeyName.
    sbKeyName.i = CkStringBuilder::ckCreate()
    If sbKeyName.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbKeyName,CkXml::ckGetChildContent(xmlKeyInfo,"*:KeyName"))
    CkXml::ckDispose(xmlKeyInfo)

    ; sbKeyName contains "dsaKey_123"
    Debug CkStringBuilder::ckGetAsString(sbKeyName)

    ; The application now locates and loads the public key for verification
    ; based on the key name.  This example will do the following:
    ; If the key name equals "dsaKey_123", then load the DSA key from
    ; "qa_data/dsa/dsa1024_public.pem"
    ; 

    If CkStringBuilder::ckContentsEqual(sbKeyName,"dsaKey_123",0) = 0
        Debug "I don't know this key..."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbXml)
        CkXmlDSig::ckDispose(verifier)
        CkStringBuilder::ckDispose(sbKeyName)
        ProcedureReturn
    EndIf

    ; The DSA public key used in this example is available at:
    ; https://www.chilkatsoft.com/exampleData/dsa1024_public.zip
    pubKey.i = CkPublicKey::ckCreate()
    If pubKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPublicKey::ckLoadFromFile(pubKey,"qa_data/dsa/dsa1024_public.pem")
    If success <> 1
        Debug CkPublicKey::ckLastErrorText(pubKey)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbXml)
        CkXmlDSig::ckDispose(verifier)
        CkStringBuilder::ckDispose(sbKeyName)
        CkPublicKey::ckDispose(pubKey)
        ProcedureReturn
    EndIf

    ; Provide the DSA public key to the XML DSig verifier:
    CkXmlDSig::ckSetPublicKey(verifier,pubKey)

    ; Verify the signature
    bVerified.i = CkXmlDSig::ckVerifySignature(verifier,1)
    Debug "Signature verified = " + Str(bVerified)


    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(sbXml)
    CkXmlDSig::ckDispose(verifier)
    CkStringBuilder::ckDispose(sbKeyName)
    CkPublicKey::ckDispose(pubKey)


    ProcedureReturn
EndProcedure