Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

DataFlex Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
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
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(DataFlex) Create XML Digital Signature using a ECDSA Key

Demonstrates how to create an XML digital signature using a ECDSA key.

This example requires Chilkat v9.5.0.69 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    String sUrl
    Handle hoHttp
    Variant vSbSoapXml
    Handle hoSbSoapXml
    Boolean iSuccess
    Variant vEcKey
    Handle hoEcKey
    Handle hoXmlSigGen
    Handle hoVerifier
    Boolean iBVerified
    String sTemp1

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

    //  The above XML is available at https://www.chilkatsoft.com/exampleData/soapToSign.xml
    //  Fetch the XML and then sign it..

    Move "https://www.chilkatsoft.com/exampleData/soapToSign.xml" To sUrl
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbSoapXml
    If (Not(IsComObjectCreated(hoSbSoapXml))) Begin
        Send CreateComObject of hoSbSoapXml
    End
    Get pvComObject of hoSbSoapXml to vSbSoapXml
    Get ComQuickGetSb Of hoHttp sUrl vSbSoapXml To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  This example uses an ECDSA private key for signing.
    //  There are many ways to load an ECDSA private key using Chilkat.  This example
    //  will load from an unencrypted PEM file.

    //  For your convenience, Chilkat put this sample ECDSA PEM here:  https://www.chilkatsoft.com/exampleData/secp256r1-key.zip
    Get Create (RefClass(cComChilkatPrivateKey)) To hoEcKey
    If (Not(IsComObjectCreated(hoEcKey))) Begin
        Send CreateComObject of hoEcKey
    End
    Get ComLoadPemFile Of hoEcKey "qa_data/ecc/secp256r1-key.pem" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoEcKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  To create the XML digital signature (i.e. embed the signature within
    //  the SOAP XML), we specify what is desired, and then call the method to
    //  create the XML signature.
    // 
    //  For example, the application must provide the following:
    //      - Where to put the signature.
    //      - What to sign.
    //      - The algorithms to be used.
    //      - The key to be used for signing.
    // 

    Get Create (RefClass(cComChilkatXmlDSigGen)) To hoXmlSigGen
    If (Not(IsComObjectCreated(hoXmlSigGen))) Begin
        Send CreateComObject of hoXmlSigGen
    End

    //  In this example, the Signature will be placed within the wsse:Security element.
    //  To specify the location, set the SigLocation property to the XML path to this element,
    //  using vertical bar characters to separate tags.
    Set ComSigLocation Of hoXmlSigGen To "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security"

    //  An XML digital signature contains one or more references.  A given reference
    //  can be a same-document reference (to an XML fragment), or an external reference.
    //  This example will add a single same-document reference.  We'll add a reference to the XML fragment
    //  at SOAP-ENV:Body, which is indicated by providing the value of the "Id" attribute (where "Id" is case
    //  insensitive).  For each same-document reference, we must also indicate the hash algorithm and XML canonicalization
    //  algorithm to be used.  For this example we'll choose SHA-256 and Exclusive XML Canonicalization.
    Get ComAddSameDocRef Of hoXmlSigGen "Body" "sha256" "EXCL_C14N" "" "" To iSuccess

    //  Let's provide the ECDSA key to be used for signing:
    Get pvComObject of hoEcKey to vEcKey
    Get ComSetPrivateKey Of hoXmlSigGen vEcKey To iSuccess

    //  We're leaving the following properties at their default values:
    // 
    //     - SigNamespacePrefix (default is "ds")
    //     - SignedInfoCanonAlg  (default is EXCL_C14N)
    //     - SignedInfoDigestMethod (default is sha256)
    //     - KeyInfoType (default is "KeyValue", where the ECDSA public key is included in the Signature)

    //  Note: Each Reference specifies its own algorithms for XML canonicalization and hashing.
    //  The SignedInfo part also has specified hash and canonicalization algorithms, which may be different
    //  than the algorithms used in the References.
    //  In this example, we use Exclusive XML Canonicalization and SHA-256 throughout.

    //  Finally, we're going to set one property that's optional, but commonly used.
    //  It's the SignedInfoPrefixList.  In this case, we're using Exclusive Canonicalization, and the signature
    //  will be placed in a location within the XML document where namespace prefixes are used in the ancestors.
    //  Specifically, the "wsse" and "SOAP-ENV" namespace prefixes are used.
    Set ComSignedInfoPrefixList Of hoXmlSigGen To "wsse SOAP-ENV"

    //  OK, everything's specified, so let's create the XML digital signature:
    //  This in-place signs the XML.  If successful, sbSoapXml will contain the
    //  XML with the digital signature at the specified location.
    Get pvComObject of hoSbSoapXml to vSbSoapXml
    Get ComCreateXmlDSigSb Of hoXmlSigGen vSbSoapXml To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoXmlSigGen To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Examine the signed SOAP XML:
    Get ComGetAsString Of hoSbSoapXml To sTemp1
    Showln sTemp1

    //  This is the signed SOAP XML.
    //  Chilkat emits the Signature in compact form on a single line.  Whitespace in XML signatures
    //  matters.  Chilkat's opinion is that writing the Signature without whitespace minimizes the chance
    //  for problems with whatever software might be verifying the signature.

    //  <?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"><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#"><InclusiveNamespaces xmlns="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="wsse SOAP-ENV"/></ds:CanonicalizationMethod><ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256"/><ds:Reference URI="#Body"><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>OwgHPZNfDkXnZsjpfzXqAcT3RV3HzmTsEy2bP44FJ0M=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>jmPSGgtirHSjjcs/o0NIDwwvy4aFBCEosSN+XP5NLVuGpRYnS2EM7mItiw6TKmESAoJYV23ZN4pZ9BFRKSvZqw==</ds:SignatureValue><ds:KeyInfo><ds:KeyValue><ds:ECKeyValue xmlns="http://www.w3.org/2009/xmldsig11#"><ds:NamedCurve URI="urn:oid:1.2.840.10045.3.1.7" /><ds:PublicKey>BOVKaiLPKEDChhkA64UEBOXTv/VFHnhrUPN+bXqCvEl7rroAYpH5tKzbiGTtMSlp4JO9Pxg44zeX7EoWDvOrpD0=</ds:PublicKey></ds:ECKeyValue></ds:KeyValue></ds:KeyInfo></ds:Signature></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>

    //  The above signed XML contains the ECDSA public key in the ECKeyValue fragment (within the ds:KeyInfo).
    //  The signature can easily be verified like this:

    Get Create (RefClass(cComChilkatXmlDSig)) To hoVerifier
    If (Not(IsComObjectCreated(hoVerifier))) Begin
        Send CreateComObject of hoVerifier
    End

    //  First load the XML containing the signature(s) to be verified.
    Get pvComObject of hoSbSoapXml to vSbSoapXml
    Get ComLoadSignatureSb Of hoVerifier vSbSoapXml To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoVerifier To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComVerifySignature Of hoVerifier True To iBVerified
    If (iBVerified <> True) Begin
        Get ComLastErrorText Of hoVerifier To sTemp1
        Showln sTemp1
    End

    Showln "Signature verified = " iBVerified


End_Procedure

 

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