DataFlex
An enveloping signature is where the signed data is contained within the Signature within an Object element.
This example signs non-XML text data where the XML signature constitutes the entire output.
DataFlex
Create Enveloping XML Digital Signature
See more XML Digital Signatures Examples
This example creates an enveloping digital signature.An enveloping signature is where the signed data is contained within the Signature within an Object element.
This example signs non-XML text data where the XML signature constitutes the entire output.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
File Handle hoZipFile
String sKeyUrl
Handle hoZip
Variant vEntry
Handle hoEntry
Variant vEcKey
Handle hoEcKey
Handle hoGen
Variant vSbContent
Handle hoSbContent
Variant vSbXml
Handle hoSbXml
String sTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Let's use the ECDSA private key at https://www.chilkatsoft.com/exampleData/secp256r1-key.zip
// for signing.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Get Create (RefClass(cComChilkatBinData)) To hoZipFile
If (Not(IsComObjectCreated(hoZipFile))) Begin
Send CreateComObject of hoZipFile
End
Move "https://www.chilkatsoft.com/exampleData/secp256r1-key.zip" To sKeyUrl
Get pvComObject of hoZipFile to vZipFile
Get ComQuickGetBd Of hoHttp sKeyUrl vZipFile To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatZip)) To hoZip
If (Not(IsComObjectCreated(hoZip))) Begin
Send CreateComObject of hoZip
End
Get pvComObject of hoZipFile to vZipFile
Get ComOpenBd Of hoZip vZipFile To iSuccess
Get Create (RefClass(cComChilkatZipEntry)) To hoEntry
If (Not(IsComObjectCreated(hoEntry))) Begin
Send CreateComObject of hoEntry
End
Get pvComObject of hoEntry to vEntry
Get ComEntryMatching Of hoZip "*.pem" vEntry To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoZip To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatPrivateKey)) To hoEcKey
If (Not(IsComObjectCreated(hoEcKey))) Begin
Send CreateComObject of hoEcKey
End
Get ComUnzipToString Of hoEntry 0 "utf-8" To sTemp1
Get ComLoadPem Of hoEcKey sTemp1 To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoEcKey To sTemp1
Showln sTemp1
Procedure_Return
End
// ----------------------------------------------------------------------------
Get Create (RefClass(cComChilkatXmlDSigGen)) To hoGen
If (Not(IsComObjectCreated(hoGen))) Begin
Send CreateComObject of hoGen
End
// Provide the ECDSA key to the XML Digital Signature generator
Get pvComObject of hoEcKey to vEcKey
Get ComSetPrivateKey Of hoGen vEcKey To iSuccess
// Add an enveloped reference to the content to be signed.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbContent
If (Not(IsComObjectCreated(hoSbContent))) Begin
Send CreateComObject of hoSbContent
End
Get ComAppend Of hoSbContent "This is the content that is signed." To iSuccess
Get pvComObject of hoSbContent to vSbContent
Get ComAddEnvelopedRef Of hoGen "abc123" vSbContent "sha256" "C14N" "" To iSuccess
// Generate the XML digital signature.
// Notice that in other examples, the sbXml passed to CreateXmlDSigSb
// already contains XML, and the XML signature is inserted at the location
// specified by the SigLocation property. In this case, both SigLocation
// and sbXml are empty. The result is that sbXml will contain just 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 ComCreateXmlDSigSb Of hoGen vSbXml To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoGen To sTemp1
Showln sTemp1
Procedure_Return
End
// Examine the enveloped signature, where the data is contained within the XML Signature
Get ComGetAsString Of hoSbXml To sTemp1
Showln sTemp1
// The Signature returned is compact and in a single line, like this:
// <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#ecdsa-sha256"/><ds:Reference URI="#abc123"><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>tEVrbXXjeTXjF3tIojul4/sgeEGN49E1dxr/GMs8GNE=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>/pILUshwrzgdGc4bPgp85TDfbUiM9pn8EIPNRVWKuoVEtPsv4XRthUrv9aDDvajmyl2okLwTakANgtaxO1ULMw==</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:Object Id="abc123">This is the content that is signed.</ds:Object></ds:Signature>
// XML pretty-printed, the signature is as follows, but pretty-printing introductes whitespace that breaks the signature..
// <?xml version="1.0" encoding="utf-8" ?>
// <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#ecdsa-sha256" />
// <ds:Reference URI="#abc123">
// <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
// <ds:DigestValue>tEVrbXXjeTXjF3tIojul4/sgeEGN49E1dxr/GMs8GNE=</ds:DigestValue>
// </ds:Reference>
// </ds:SignedInfo>
// <ds:SignatureValue>/pILUshwrzgdGc4bPgp85TDfbUiM9pn8EIPNRVWKuoVEtPsv4XRthUrv9aDDvajmyl2okLwTakANgtaxO1ULMw==</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:Object Id="abc123">This is the content that is signed.</ds:Object>
// </ds:Signature>
//
End_Procedure