C
C
Signed SOAP for www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/ using BinarySecurityToken
See more XML Digital Signatures Examples
Creates a signed SOAP request for www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/ using a BinarySecurityToken.Chilkat C Downloads
#include <C_CkXml.h>
#include <C_CkDateTime.h>
#include <C_CkXmlDSigGen.h>
#include <C_CkCert.h>
#include <C_CkStringBuilder.h>
#include <C_CkBinData.h>
#include <C_CkXmlDSig.h>
void ChilkatSample(void)
{
BOOL success;
HCkXml xmlToSign;
HCkDateTime dtNow;
HCkXmlDSigGen gen;
HCkCert cert;
HCkXml xmlCustomKeyInfo;
HCkStringBuilder sbXml;
HCkBinData bdCert;
HCkStringBuilder sbCert64;
int nReplaced;
HCkXmlDSig verifier;
int numSigs;
int verifyIdx;
BOOL verified;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Create the following pre-signed SOAP XML:
// Note: We'll be constructing a placeholder for the wseBinarySecurityToken in the soapenv:Header.
// Chilkat will automatically replace the "BinarySecurityToken_Base64Binary_Content" with actual data when signing the XML.
// However: Your application should insert the actual desired timestamp values for wsu:Created and wsuExpires
// Typically this is the current system date/time and a few minutes from the current system date/time.
// The example below shows a 5 minute window. We'll write code below to insert the current date/time and the current date/time + 5 minutes..
// <?xml version="1.0" encoding="utf-8"?>
// <soapenv:Envelope
// xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
// xmlns:scat="www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/"
// xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
// xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
// xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
// xmlns:exc14n="http://www.w3.org/2001/10/xml-exc-c14n#">
// <soapenv:Header xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
// <wsse:Security soapenv:mustUnderstand="1">
// <wsse:BinarySecurityToken
// ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"
// EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"
// wsu:Id="X509Token">BinarySecurityToken_Base64Binary_Content</wsse:BinarySecurityToken>
// <wsu:Timestamp wsu:Id="_1">
// <wsu:Created>2020-03-20T18:08:19Z</wsu:Created>
// <wsu:Expires>2020-03-20T18:13:19Z</wsu:Expires>
// </wsu:Timestamp>
// </wsse:Security>
// <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"/>
// </soapenv:Header>
// <soapenv:Body>
// <scat:getRuoliStruttureOperatore>
// <scat:codFiscaleOperatore>CLDxxxxxxxxxxxxL</scat:codFiscaleOperatore>
// </scat:getRuoliStruttureOperatore>
// </soapenv:Body>
// </soapenv:Envelope>
success = TRUE;
// Create the XML to be signed...
xmlToSign = CkXml_Create();
CkXml_putTag(xmlToSign,"soapenv:Envelope");
CkXml_AddAttribute(xmlToSign,"xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/");
CkXml_AddAttribute(xmlToSign,"xmlns:scat","www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/");
CkXml_AddAttribute(xmlToSign,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
CkXml_AddAttribute(xmlToSign,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
CkXml_AddAttribute(xmlToSign,"xmlns:ds","http://www.w3.org/2000/09/xmldsig#");
CkXml_AddAttribute(xmlToSign,"xmlns:exc14n","http://www.w3.org/2001/10/xml-exc-c14n#");
CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header",TRUE,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security",TRUE,"soapenv:mustUnderstand","1");
CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,"EncodingType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,"wsu:Id","X509Token");
CkXml_UpdateChildContent(xmlToSign,"soapenv:Header|wsse:Security|wsse:BinarySecurityToken","BinarySecurityToken_Base64Binary_Content");
CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security|wsu:Timestamp",TRUE,"wsu:Id","_1");
// Insert a 5-minute timestampe window.
dtNow = CkDateTime_Create();
CkDateTime_SetFromCurrentSystemTime(dtNow);
CkXml_UpdateChildContent(xmlToSign,"soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created",CkDateTime_getAsTimestamp(dtNow,FALSE));
// Add 5 minutes to the time.
CkDateTime_AddSeconds(dtNow,300);
CkXml_UpdateChildContent(xmlToSign,"soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires",CkDateTime_getAsTimestamp(dtNow,FALSE));
CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security[1]",TRUE,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
CkXml_UpdateChildContent(xmlToSign,"soapenv:Body|scat:getRuoliStruttureOperatore|scat:codFiscaleOperatore","CLDxxxxxxxxxxxxL");
printf("XML to sign:\n");
printf("%s\n",CkXml_getXml(xmlToSign));
gen = CkXmlDSigGen_Create();
CkXmlDSigGen_putSigLocation(gen,"soapenv:Envelope|soapenv:Header|wsse:Security");
CkXmlDSigGen_putSigLocationMod(gen,0);
CkXmlDSigGen_putSigNamespacePrefix(gen,"ds");
CkXmlDSigGen_putSigNamespaceUri(gen,"http://www.w3.org/2000/09/xmldsig#");
CkXmlDSigGen_putSignedInfoCanonAlg(gen,"EXCL_C14N");
CkXmlDSigGen_putSignedInfoDigestMethod(gen,"sha1");
// Set the KeyInfoId before adding references..
CkXmlDSigGen_putKeyInfoId(gen,"X509KeyId");
// -------- Reference 1 --------
CkXmlDSigGen_AddSameDocRef(gen,"_1","sha1","EXCL_C14N","","");
// Provide a certificate + private key. (PFX password is test123)
cert = CkCert_Create();
success = CkCert_LoadPfxFile(cert,"qa_data/pfx/cert_test123.pfx","test123");
if (success != TRUE) {
printf("%s\n",CkCert_lastErrorText(cert));
CkXml_Dispose(xmlToSign);
CkDateTime_Dispose(dtNow);
CkXmlDSigGen_Dispose(gen);
CkCert_Dispose(cert);
return;
}
CkXmlDSigGen_SetX509Cert(gen,cert,TRUE);
CkXmlDSigGen_putKeyInfoType(gen,"Custom");
// Create the custom KeyInfo XML..
xmlCustomKeyInfo = CkXml_Create();
CkXml_putTag(xmlCustomKeyInfo,"wsse:SecurityTokenReference");
CkXml_AddAttribute(xmlCustomKeyInfo,"wsu:Id","X509TokenReference");
CkXml_UpdateAttrAt(xmlCustomKeyInfo,"wsse:Reference",TRUE,"URI","#X509Token");
CkXml_UpdateAttrAt(xmlCustomKeyInfo,"wsse:Reference",TRUE,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
CkXml_putEmitXmlDecl(xmlCustomKeyInfo,FALSE);
CkXmlDSigGen_putCustomKeyInfoXml(gen,CkXml_getXml(xmlCustomKeyInfo));
// Load XML to be signed...
sbXml = CkStringBuilder_Create();
CkXml_GetXmlSb(xmlToSign,sbXml);
// Update BinarySecurityToken_Base64Binary_Content with the actual X509 of the signing cert.
bdCert = CkBinData_Create();
CkCert_ExportCertDerBd(cert,bdCert);
sbCert64 = CkStringBuilder_Create();
CkBinData_GetEncodedSb(bdCert,"base64",sbCert64);
nReplaced = CkStringBuilder_Replace(sbXml,"BinarySecurityToken_Base64Binary_Content",CkStringBuilder_getAsString(sbCert64));
CkXmlDSigGen_putBehaviors(gen,"IndentedSignature");
// Sign the XML...
success = CkXmlDSigGen_CreateXmlDSigSb(gen,sbXml);
if (success != TRUE) {
printf("%s\n",CkXmlDSigGen_lastErrorText(gen));
CkXml_Dispose(xmlToSign);
CkDateTime_Dispose(dtNow);
CkXmlDSigGen_Dispose(gen);
CkCert_Dispose(cert);
CkXml_Dispose(xmlCustomKeyInfo);
CkStringBuilder_Dispose(sbXml);
CkBinData_Dispose(bdCert);
CkStringBuilder_Dispose(sbCert64);
return;
}
// -----------------------------------------------
// Save the signed XML to a file.
success = CkStringBuilder_WriteFile(sbXml,"qa_output/signedXml.xml","utf-8",FALSE);
printf("%s\n",CkStringBuilder_getAsString(sbXml));
// Produces the following signed XML:
// Note: The following has been edited for readability. Therefore, what you see here will not actually validate because it's been modified.
// <?xml version="1.0" encoding="utf-8"?>
// <soapenv:Envelope
// xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
// xmlns:scat="www.sist.puglia.it/Schemas/PDD_SIST/SCATEL/"
// xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
// xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
// xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:exc14n="http://www.w3.org/2001/10/xml-exc-c14n#">
// <soapenv:Header xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
// <wsse:Security soapenv:mustUnderstand="1">
// <wsse:BinarySecurityToken ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" wsu:Id="X509Token">MIIFNTCCBB2gAwIBAgIQHozVnBl1lTsusAh26u6WZTANBgkqhkiG9w0BAQsFADCB
// lzELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4G
// A1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENBIExpbWl0ZWQxPTA7BgNV
// BAMTNENPTU9ETyBSU0EgQ2xpZW50IEF1dGhlbnRpY2F0aW9uIGFuZCBTZWN1cmUg
// RW1haWwgQ0EwHhcNMTcxMjE0MDAwMDAwWhcNMTgxMjE0MjM1OTU5WjAmMSQwIgYJ
// KoZIhvcNAQkBFhVhc2Rhc2Rhc2Rhc2RkQGJ5b20uZGUwggEiMA0GCSqGSIb3DQEB
// AQUAA4IBDwAwggEKAoIBAQC96oQe50EDoiuJVITeKJzy6GzVq74cEa14eFypjMNb
// YVyedfCI6cn9pVClLqL7dlwxFGCRA62bbNE9woKLBT3SO1IvgoFDVIrbJm+84GEo
// qQReZe8HpZnF06d3DWwhUjjcuO1z2yliGdSymSee8/1OaztxEAEOWaZR+4MkfSNc
// AzzjGtKcKjVMSdiiO0JGAG/IXEwzlulfkF8zVdqDGkQTQesvT4WBys4BYwTDI64d
// sM7rcC9vwuK6gvpkUi9i1Wzu0W4v9T3iHAbl3rLihPcjQ95c4q7+NjwpRqQW1VXR
// enR/0iyLEFOek0D4JvOoscUwJ0LYd8f9SxFEWIzc4iAfAgMBAAGjggHrMIIB5zAf
// BgNVHSMEGDAWgBSCr2yM+MX+lmF86B89K3FIXsSLwDAdBgNVHQ4EFgQUrC0DOyBB
// AgOWgo2EfVZbppe3xfEwDgYDVR0PAQH/BAQDAgWgMAwGA1UdEwEB/wQCMAAwIAYD
// VR0lBBkwFwYIKwYBBQUHAwQGCysGAQQBsjEBAwUCMBEGCWCGSAGG+EIBAQQEAwIF
// IDBGBgNVHSAEPzA9MDsGDCsGAQQBsjEBAgEBATArMCkGCCsGAQUFBwIBFh1odHRw
// czovL3NlY3VyZS5jb21vZG8ubmV0L0NQUzBaBgNVHR8EUzBRME+gTaBLhklodHRw
// Oi8vY3JsLmNvbW9kb2NhLmNvbS9DT01PRE9SU0FDbGllbnRBdXRoZW50aWNhdGlv
// bmFuZFNlY3VyZUVtYWlsQ0EuY3JsMIGLBggrBgEFBQcBAQR/MH0wVQYIKwYBBQUH
// MAKGSWh0dHA6Ly9jcnQuY29tb2RvY2EuY29tL0NPTU9ET1JTQUNsaWVudEF1dGhl
// bnRpY2F0aW9uYW5kU2VjdXJlRW1haWxDQS5jcnQwJAYIKwYBBQUHMAGGGGh0dHA6
// Ly9vY3NwLmNvbW9kb2NhLmNvbTAgBgNVHREEGTAXgRVhc2Rhc2Rhc2Rhc2RkQGJ5
// b20uZGUwDQYJKoZIhvcNAQELBQADggEBAHEQTr0WFcwHVk0xozn26P3s6i3RWEco
// kNr8AdOtEvU0UYf1AfyVxUs04rS3Fs0lu2TD0840S3R687xF4HXhLYxSdD0QoZyU
// S2mgxxyVxCqhwptmLn7ZQjUKEuK6Kv6wZ3/XugsBoNrMYWlYX8g2jWVDBCJ+Z0eT
// QkaYkeSxzBSMQP3DxJS/bWh5LfwSyWYk4a3SVRJV4QVyBDXKt2uwjj1wWfxfGtiw
// 6uAd2F3YIymZKsRVdrU6+h0gXMnmtpX8T+SDV6M72PP2/ZzF6gVVItyyrIScK1J8
// mcEaR5GGkLBk1s9qQM9esp3FRlACVeb1Qlytr4vgc5FlCqn0rMtjlF4=
// </wsse:BinarySecurityToken>
// <wsu:Timestamp wsu:Id="_1">
// <wsu:Created>2020-03-26T17:19:50Z</wsu:Created>
// <wsu:Expires>2020-03-26T17:24:50Z</wsu:Expires>
// </wsu:Timestamp>
// </wsse:Security>
// <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><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/2000/09/xmldsig#rsa-sha1"/>
// <ds:Reference URI="#_1">
// <ds:Transforms>
// <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
// </ds:Transforms>
// <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
// <ds:DigestValue>cwrTw7wFZqNc50NxE//UT11qZCY=</ds:DigestValue>
// </ds:Reference>
// </ds:SignedInfo>
// <ds:SignatureValue>s2UmYNVDz9dm3eU ... 619qSZqw==</ds:SignatureValue>
// <ds:KeyInfo Id="X509KeyId"><wsse:SecurityTokenReference wsu:Id="X509TokenReference">
// <wsse:Reference URI="#X509Token" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/>
// </wsse:SecurityTokenReference>
// </ds:KeyInfo>
// </ds:Signature></wsse:Security>
// </soapenv:Header>
// <soapenv:Body>
// <scat:getRuoliStruttureOperatore>
// <scat:codFiscaleOperatore>CLDxxxxxxxxxxxxL</scat:codFiscaleOperatore>
// </scat:getRuoliStruttureOperatore>
// </soapenv:Body>
// </soapenv:Envelope>
// ----------------------------------------
// Verify the signatures we just produced...
verifier = CkXmlDSig_Create();
success = CkXmlDSig_LoadSignatureSb(verifier,sbXml);
if (success != TRUE) {
printf("%s\n",CkXmlDSig_lastErrorText(verifier));
CkXml_Dispose(xmlToSign);
CkDateTime_Dispose(dtNow);
CkXmlDSigGen_Dispose(gen);
CkCert_Dispose(cert);
CkXml_Dispose(xmlCustomKeyInfo);
CkStringBuilder_Dispose(sbXml);
CkBinData_Dispose(bdCert);
CkStringBuilder_Dispose(sbCert64);
CkXmlDSig_Dispose(verifier);
return;
}
numSigs = CkXmlDSig_getNumSignatures(verifier);
verifyIdx = 0;
while (verifyIdx < numSigs) {
CkXmlDSig_putSelector(verifier,verifyIdx);
verified = CkXmlDSig_VerifySignature(verifier,TRUE);
if (verified != TRUE) {
printf("%s\n",CkXmlDSig_lastErrorText(verifier));
CkXml_Dispose(xmlToSign);
CkDateTime_Dispose(dtNow);
CkXmlDSigGen_Dispose(gen);
CkCert_Dispose(cert);
CkXml_Dispose(xmlCustomKeyInfo);
CkStringBuilder_Dispose(sbXml);
CkBinData_Dispose(bdCert);
CkStringBuilder_Dispose(sbCert64);
CkXmlDSig_Dispose(verifier);
return;
}
verifyIdx = verifyIdx + 1;
}
printf("All signatures were successfully verified.\n");
CkXml_Dispose(xmlToSign);
CkDateTime_Dispose(dtNow);
CkXmlDSigGen_Dispose(gen);
CkCert_Dispose(cert);
CkXml_Dispose(xmlCustomKeyInfo);
CkStringBuilder_Dispose(sbXml);
CkBinData_Dispose(bdCert);
CkStringBuilder_Dispose(sbCert64);
CkXmlDSig_Dispose(verifier);
}