Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Create Enveloping XML Digital SignatureThis example creates an enveloping digital signature.
#include <C_CkHttpW.h> #include <C_CkBinDataW.h> #include <C_CkZipW.h> #include <C_CkZipEntryW.h> #include <C_CkPrivateKeyW.h> #include <C_CkXmlDSigGenW.h> #include <C_CkStringBuilderW.h> void ChilkatSample(void) { HCkHttpW http; HCkBinDataW zipFile; const wchar_t *keyUrl; BOOL success; HCkZipW zip; HCkZipEntryW zipEntry; HCkPrivateKeyW ecKey; HCkXmlDSigGenW gen; HCkStringBuilderW sbContent; HCkStringBuilderW sbXml; // 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. http = CkHttpW_Create(); zipFile = CkBinDataW_Create(); keyUrl = L"https://www.chilkatsoft.com/exampleData/secp256r1-key.zip"; success = CkHttpW_QuickGetBd(http,keyUrl,zipFile); if (success != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkBinDataW_Dispose(zipFile); return; } zip = CkZipW_Create(); success = CkZipW_OpenBd(zip,zipFile); zipEntry = CkZipW_FirstMatchingEntry(zip,L"*.pem"); ecKey = CkPrivateKeyW_Create(); success = CkPrivateKeyW_LoadPem(ecKey,CkZipEntryW_unzipToString(zipEntry,0,L"utf-8")); if (success != TRUE) { wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(ecKey)); CkZipEntryW_Dispose(zipEntry); CkHttpW_Dispose(http); CkBinDataW_Dispose(zipFile); CkZipW_Dispose(zip); CkPrivateKeyW_Dispose(ecKey); return; } CkZipEntryW_Dispose(zipEntry); // ---------------------------------------------------------------------------- gen = CkXmlDSigGenW_Create(); // Provide the ECDSA key to the XML Digital Signature generator CkXmlDSigGenW_SetPrivateKey(gen,ecKey); // Add an enveloped reference to the content to be signed. sbContent = CkStringBuilderW_Create(); CkStringBuilderW_Append(sbContent,L"This is the content that is signed."); CkXmlDSigGenW_AddEnvelopedRef(gen,L"abc123",sbContent,L"sha256",L"C14N",L""); // 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. sbXml = CkStringBuilderW_Create(); success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml); if (success != TRUE) { wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen)); CkHttpW_Dispose(http); CkBinDataW_Dispose(zipFile); CkZipW_Dispose(zip); CkPrivateKeyW_Dispose(ecKey); CkXmlDSigGenW_Dispose(gen); CkStringBuilderW_Dispose(sbContent); CkStringBuilderW_Dispose(sbXml); return; } // Examine the enveloped signature, where the data is contained within the XML Signature wprintf(L"%s\n",CkStringBuilderW_getAsString(sbXml)); // 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> // CkHttpW_Dispose(http); CkBinDataW_Dispose(zipFile); CkZipW_Dispose(zip); CkPrivateKeyW_Dispose(ecKey); CkXmlDSigGenW_Dispose(gen); CkStringBuilderW_Dispose(sbContent); CkStringBuilderW_Dispose(sbXml); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.