Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

// 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.
// ----------------------------------------------------------------------
llSuccess = .T.
// Create the above XML to be signed...
loXmlToSign = createobject("CkXml")
loXmlToSign.Tag = "soapenv:Envelope"
loXmlToSign.AddAttribute("xmlns:obs","http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/")
loXmlToSign.AddAttribute("xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/")
loXmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",.T.,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")
loXmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",.T.,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
loXmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",.T.,"soapenv:mustUnderstand","1")
loXmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security|wsse:BinarySecurityToken",.T.,"EncodingType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary")
loXmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security|wsse:BinarySecurityToken",.T.,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1")
loXmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security|wsse:BinarySecurityToken",.T.,"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.
loXmlToSign.UpdateChildContent("soapenv:Header|wsse:Security|wsse:BinarySecurityToken","BinarySecurityToken_Base64Binary_Content")
loXmlToSign.UpdateAttrAt("soapenv:Body",.T.,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
loXmlToSign.UpdateAttrAt("soapenv:Body",.T.,"wsu:Id","id-396BB6026342EB5C0E1EA73593B3CC098")
loXmlToSign.UpdateChildContent("soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|idBiznesowy","000000011986")
loXmlToSign.UpdateChildContent("soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|rodzajPodmiotuRaportujacego","PA")

loGen = createobject("CkXmlDSigGen")

loGen.SigLocation = "soapenv:Envelope|soapenv:Header|wsse:Security"
loGen.SigLocationMod = 0
loGen.SigId = "SIG-BB965DFC3C8AAF87903C0ED898B8D2A8D"
loGen.SigNamespacePrefix = "ds"
loGen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
loGen.SignedInfoCanonAlg = "EXCL_C14N"
loGen.SignedInfoDigestMethod = "sha1"

// Set the KeyInfoId before adding references..
loGen.KeyInfoId = "KI-9D95C38916099AD2EE87DDAC1A76E97E4"

// -------- Reference 1 --------
loGen.AddSameDocRef("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)
loCert = createobject("CkCert")
llSuccess = loCert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
if (llSuccess <> .T.) then
    ? loCert.LastErrorText
    release loXmlToSign
    release loGen
    release loCert
    return
endif

loGen.SetX509Cert(loCert,.T.)

loGen.KeyInfoType = "Custom"

// Create the custom KeyInfo XML..
loXmlCustomKeyInfo = createobject("CkXml")
loXmlCustomKeyInfo.Tag = "wsse:SecurityTokenReference"
loXmlCustomKeyInfo.AddAttribute("wsse11:TokenType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1")
loXmlCustomKeyInfo.AddAttribute("xmlns:wsse11","http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd")
loXmlCustomKeyInfo.AddAttribute("wsu:Id","STR-FF238E7C061332C5B19752C2FBC8CDEF2")
loXmlCustomKeyInfo.UpdateAttrAt("wsse:Reference",.T.,"URI","#X509-02BF0107214FC61449FD0013DF68F0359")
loXmlCustomKeyInfo.UpdateAttrAt("wsse:Reference",.T.,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1")

loXmlCustomKeyInfo.EmitXmlDecl = .F.
loGen.CustomKeyInfoXml = loXmlCustomKeyInfo.GetXml()

// Load XML to be signed...
loSbXml = createobject("CkStringBuilder")
loXmlToSign.GetXmlSb(loSbXml)

// Update BinarySecurityToken_Base64Binary_Content with the actual X509PKIPathv1 of the signing cert.
lnNReplaced = loSbXml.Replace("BinarySecurityToken_Base64Binary_Content",loCert.X509PKIPathv1())

loGen.Behaviors = "IndentedSignature"

// Sign the XML...
llSuccess = loGen.CreateXmlDSigSb(loSbXml)
if (llSuccess <> .T.) then
    ? loGen.LastErrorText
    release loXmlToSign
    release loGen
    release loCert
    release loXmlCustomKeyInfo
    release loSbXml
    return
endif

// -----------------------------------------------

// Save the signed XML to a file.
llSuccess = loSbXml.WriteFile("qa_output/signedXml.xml","utf-8",.F.)

? loSbXml.GetAsString()

// ----------------------------------------
// Verify the signatures we just produced...
loVerifier = createobject("CkXmlDSig")
llSuccess = loVerifier.LoadSignatureSb(loSbXml)
if (llSuccess <> .T.) then
    ? loVerifier.LastErrorText
    release loXmlToSign
    release loGen
    release loCert
    release loXmlCustomKeyInfo
    release loSbXml
    release loVerifier
    return
endif

lnNumSigs = loVerifier.NumSignatures
lnVerifyIdx = 0
do while lnVerifyIdx < lnNumSigs
    loVerifier.Selector = lnVerifyIdx
    llVerified = loVerifier.VerifySignature(.T.)
    if (llVerified <> .T.) then
        ? loVerifier.LastErrorText
        release loXmlToSign
        release loGen
        release loCert
        release loXmlCustomKeyInfo
        release loSbXml
        release loVerifier
        return
    endif

    lnVerifyIdx = lnVerifyIdx + 1
enddo
? "All signatures were successfully verified."


release loXmlToSign
release loGen
release loCert
release loXmlCustomKeyInfo
release loSbXml
release loVerifier