Delphi DLL
Delphi DLL
Create XAdES with ec:InclusiveNamespaces within the Transforms
See more XML Digital Signatures Examples
Demonstrates how to generate XAdES that has a Reference that has Transforms that include an ec:InclusiveNamespaces.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, StringBuilder, XmlDSigGen, Xml, XmlDSig, Cert;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
xmlToSign: HCkXml;
gen: HCkXmlDSigGen;
cert: HCkCert;
xmlCustomKeyInfo: HCkXml;
sbXml: HCkStringBuilder;
nReplaced: Integer;
verifier: HCkXmlDSig;
numSigs: Integer;
verifyIdx: Integer;
verified: Boolean;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This is the XML we'll be signing:
// <soapenv:Envelope xmlns:obs="http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
// <soapenv:Header>
// <wsse:Security
// 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" soapenv:mustUnderstand="1">
// <wsse:BinarySecurityToken
// EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"
// ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1"
// wsu:Id="X509-02BF0107214FC61449FD0013DF68F0359">MII...</wsse:BinarySecurityToken>
// </wsse:Security>
// </soapenv:Header>
// <soapenv:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
// wsu:Id="id-396BB6026342EB5C0E1EA73593B3CC098">
// <obs:zapiszKomunikatOS>
// <komunikatOS>
// <idPodmiotuRaportujacego>
// <idBiznesowy>000000011986</idBiznesowy>
// <rodzajPodmiotuRaportujacego>PA</rodzajPodmiotuRaportujacego>
// </idPodmiotuRaportujacego>
// </komunikatOS>
// </obs:zapiszKomunikatOS>
// </soapenv:Body>
// </soapenv:Envelope>
//
// ----------------------------------------------------------------------
// IMPORTANT: This generated example requires Chilkat v9.5.0.77 or later.
// ----------------------------------------------------------------------
success := True;
// Create the above XML to be signed...
xmlToSign := CkXml_Create();
CkXml_putTag(xmlToSign,'soapenv:Envelope');
CkXml_AddAttribute(xmlToSign,'xmlns:obs','http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/');
CkXml_AddAttribute(xmlToSign,'xmlns:soapenv','http://schemas.xmlsoap.org/soap/envelope/');
CkXml_UpdateAttrAt(xmlToSign,'soapenv:Header|wsse:Security',True,'xmlns:wsse','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
CkXml_UpdateAttrAt(xmlToSign,'soapenv:Header|wsse:Security',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,'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,'ValueType','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1');
CkXml_UpdateAttrAt(xmlToSign,'soapenv:Header|wsse:Security|wsse:BinarySecurityToken',True,'wsu:Id','X509-02BF0107214FC61449FD0013DF68F0359');
// Note: The content of this XML node is a placeholder that will be updated below with the X509PKIPathv1 for the signing certificate.
CkXml_UpdateChildContent(xmlToSign,'soapenv:Header|wsse:Security|wsse:BinarySecurityToken','BinarySecurityToken_Base64Binary_Content');
CkXml_UpdateAttrAt(xmlToSign,'soapenv:Body',True,'xmlns:wsu','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd');
CkXml_UpdateAttrAt(xmlToSign,'soapenv:Body',True,'wsu:Id','id-396BB6026342EB5C0E1EA73593B3CC098');
CkXml_UpdateChildContent(xmlToSign,'soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|idBiznesowy','000000011986');
CkXml_UpdateChildContent(xmlToSign,'soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|rodzajPodmiotuRaportujacego','PA');
gen := CkXmlDSigGen_Create();
CkXmlDSigGen_putSigLocation(gen,'soapenv:Envelope|soapenv:Header|wsse:Security');
CkXmlDSigGen_putSigLocationMod(gen,0);
CkXmlDSigGen_putSigId(gen,'SIG-BB965DFC3C8AAF87903C0ED898B8D2A8D');
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,'KI-9D95C38916099AD2EE87DDAC1A76E97E4');
// -------- Reference 1 --------
CkXmlDSigGen_AddSameDocRef(gen,'id-396BB6026342EB5C0E1EA73593B3CC098','sha1','EXCL_C14N','obs','');
// The reference to be produced in the Signature should look like this:
// <ds:Reference URI="#id-396BB6026342EB5C0E1EA73593B3CC098">
// <ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
// <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="obs"></ec:InclusiveNamespaces>
// </ds:Transform></ds:Transforms>
// <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></ds:DigestMethod>
// <ds:DigestValue>2e9hZYj/CN2nPsgQqUraU43k3ds=</ds:DigestValue>
// </ds:Reference>
//
// 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) then
begin
Memo1.Lines.Add(CkCert__lastErrorText(cert));
Exit;
end;
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,'wsse11:TokenType','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1');
CkXml_AddAttribute(xmlCustomKeyInfo,'xmlns:wsse11','http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd');
CkXml_AddAttribute(xmlCustomKeyInfo,'wsu:Id','STR-FF238E7C061332C5B19752C2FBC8CDEF2');
CkXml_UpdateAttrAt(xmlCustomKeyInfo,'wsse:Reference',True,'URI','#X509-02BF0107214FC61449FD0013DF68F0359');
CkXml_UpdateAttrAt(xmlCustomKeyInfo,'wsse:Reference',True,'ValueType','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1');
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 X509PKIPathv1 of the signing cert.
nReplaced := CkStringBuilder_Replace(sbXml,'BinarySecurityToken_Base64Binary_Content',CkCert__x509PKIPathv1(cert));
CkXmlDSigGen_putBehaviors(gen,'IndentedSignature');
// Sign the XML...
success := CkXmlDSigGen_CreateXmlDSigSb(gen,sbXml);
if (success <> True) then
begin
Memo1.Lines.Add(CkXmlDSigGen__lastErrorText(gen));
Exit;
end;
// -----------------------------------------------
// Save the signed XML to a file.
success := CkStringBuilder_WriteFile(sbXml,'qa_output/signedXml.xml','utf-8',False);
Memo1.Lines.Add(CkStringBuilder__getAsString(sbXml));
// ----------------------------------------
// Verify the signatures we just produced...
verifier := CkXmlDSig_Create();
success := CkXmlDSig_LoadSignatureSb(verifier,sbXml);
if (success <> True) then
begin
Memo1.Lines.Add(CkXmlDSig__lastErrorText(verifier));
Exit;
end;
numSigs := CkXmlDSig_getNumSignatures(verifier);
verifyIdx := 0;
while verifyIdx < numSigs do
begin
CkXmlDSig_putSelector(verifier,verifyIdx);
verified := CkXmlDSig_VerifySignature(verifier,True);
if (verified <> True) then
begin
Memo1.Lines.Add(CkXmlDSig__lastErrorText(verifier));
Exit;
end;
verifyIdx := verifyIdx + 1;
end;
Memo1.Lines.Add('All signatures were successfully verified.');
CkXml_Dispose(xmlToSign);
CkXmlDSigGen_Dispose(gen);
CkCert_Dispose(cert);
CkXml_Dispose(xmlCustomKeyInfo);
CkStringBuilder_Dispose(sbXml);
CkXmlDSig_Dispose(verifier);
end;