Sample code for 30+ languages & platforms
DataFlex

Create XML Signature using Java KeyStore (.jks)

See more XML Digital Signatures Examples

Demonstrates how to create an XML digital signature using a certificate and private key from a Java KeyStore (.jks)

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoXml
    Handle hoJks
    String sPassword
    Variant vChain
    Handle hoChain
    Variant vCert
    Handle hoCert
    Handle hoGen
    Boolean iBUsePrivateKey
    Variant vSbXml
    Handle hoSbXml
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    // The SOAP XML to be signed in this example contains the following:

    // <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    // <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    //     <SOAP-ENV:Header>
    //         <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" SOAP-ENV:mustUnderstand="1"></wsse:Security>
    //     </SOAP-ENV:Header>
    //     <SOAP-ENV:Body xmlns:SOAP-SEC="http://schemas.xmlsoap.org/soap/security/2000-12" SOAP-SEC:id="Body">
    //         <z:FooBar xmlns:z="http://example.com" />
    //     </SOAP-ENV:Body>
    // </SOAP-ENV:Envelope>
    // 

    // Build the XML to sign.
    // Use this online tool to generate the code from sample XML: 
    // Generate Code to Create XML

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Set ComTag Of hoXml To "SOAP-ENV:Envelope"
    Get ComAddAttribute Of hoXml "xmlns:SOAP-ENV" "http://schemas.xmlsoap.org/soap/envelope/" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Header|wsse:Security" True "xmlns:wsse" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Header|wsse:Security" True "SOAP-ENV:mustUnderstand" "1" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Body" True "xmlns:SOAP-SEC" "http://schemas.xmlsoap.org/soap/security/2000-12" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Body" True "SOAP-SEC:id" "Body" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Body|z:FooBar" True "xmlns:z" "http://example.com" To iSuccess

    // Load a JavaKeyStore file containing the certificate + private key.
    Get Create (RefClass(cComChilkatJavaKeyStore)) To hoJks
    If (Not(IsComObjectCreated(hoJks))) Begin
        Send CreateComObject of hoJks
    End
    Move "secret" To sPassword
    Get ComLoadFile Of hoJks sPassword "qa_data/jks/test_secret.jks" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJks To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Make sure we have a private key.
    Get ComNumPrivateKeys Of hoJks To iTemp1
    If (iTemp1 < 1) Begin
        Showln "No private key available."
        Procedure_Return
    End

    // -------------------------------------------------------------------------
    // Get the certificate chain associated with the 1st (and probably only) private key in the JKS.

    Get Create (RefClass(cComChilkatCertChain)) To hoChain
    If (Not(IsComObjectCreated(hoChain))) Begin
        Send CreateComObject of hoChain
    End
    Get pvComObject of hoChain to vChain
    Get ComCertChainAt Of hoJks 0 vChain To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJks To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get pvComObject of hoCert to vCert
    Get ComCertAt Of hoChain 0 vCert To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoChain To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Verify again that this cert has a private key.
    Get ComHasPrivateKey Of hoCert To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Certificate has no associated private key."
        Procedure_Return
    End

    // Prepare for signing...
    // Use this online tool to generate the following code from an already-signed XML sample: 
    // Generate Code to Create an XML Signature
    Get Create (RefClass(cComChilkatXmlDSigGen)) To hoGen
    If (Not(IsComObjectCreated(hoGen))) Begin
        Send CreateComObject of hoGen
    End

    // Indicate where the Signature will be inserted.
    Set ComSigLocation Of hoGen To "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security"

    // Add a reference to the fragment of the XML to be signed.
    Get ComAddSameDocRef Of hoGen "Body" "sha1" "EXCL_C14N" "" "" To iSuccess

    // (You can read about the SignedInfoPrefixList in the online reference documentation.  It's optional..)
    Set ComSignedInfoPrefixList Of hoGen To "wsse SOAP-ENV"

    // Provide the private key for signing via the certificate, and indicate that
    // we want the base64 of the certificate embedded in the KeyInfo.
    Set ComKeyInfoType Of hoGen To "X509Data"
    Set ComX509Type Of hoGen To "Certificate"

    // Note: Because our certificate was loaded from a JKS which also contained the private key,
    // Chilkat automatically knows and has the private key associated with the certificate.
    // We set bUsePrivateKey to tell the SetX509Cert method to automatically use the private key
    // associated with the certificate for signing.
    Move True To iBUsePrivateKey
    Get pvComObject of hoCert to vCert
    Get ComSetX509Cert Of hoGen vCert iBUsePrivateKey To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoGen To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Everything's specified.  Now create and insert the Signature
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml
    If (Not(IsComObjectCreated(hoSbXml))) Begin
        Send CreateComObject of hoSbXml
    End
    Get pvComObject of hoSbXml to vSbXml
    Get ComGetXmlSb Of hoXml vSbXml To iSuccess
    Get pvComObject of hoSbXml to vSbXml
    Get ComCreateXmlDSigSb Of hoGen vSbXml To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoGen To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Examine the XML with the digital signature inserted
    Get ComGetAsString Of hoSbXml To sTemp1
    Showln sTemp1


End_Procedure