Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) Create XML Signature using Java KeyStore (.jks)

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

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

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

    // 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 <> True) 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 ComGetCertChain Of hoJks 0 To vChain
    If (IsComObject(vChain)) Begin
        Get Create (RefClass(cComChilkatCertChain)) To hoChain
        Set pvComObject Of hoChain To vChain
    End
    Get ComLastMethodSuccess Of hoJks To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoJks To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetCert Of hoChain 0 To vCert
    If (IsComObject(vCert)) Begin
        Get Create (RefClass(cComChilkatCert)) To hoCert
        Set pvComObject Of hoCert To vCert
    End
    Get ComLastMethodSuccess Of hoChain To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoChain To sTemp1
        Showln sTemp1
        Send Destroy of hoChain
        Procedure_Return
    End

    Send Destroy of hoChain

    // 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."
        Send Destroy of hoCert
        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 ComSetX509Cert Of hoGen vCert iBUsePrivateKey To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoGen To sTemp1
        Showln sTemp1
        Send Destroy of hoCert
        Procedure_Return
    End

    Send Destroy of hoCert

    // 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 <> True) 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

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.