Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_XmlToSign
oleobject loo_Gen
oleobject loo_Cert
oleobject loo_XmlCustomKeyInfo
oleobject loo_SbXml
long li_NReplaced
oleobject loo_Verifier
long li_NumSigs
long li_VerifyIdx
long li_Verified

li_Success = 0

//  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.
//  ----------------------------------------------------------------------
li_Success = 1
//  Create the above XML to be signed...
loo_XmlToSign = create oleobject
li_rc = loo_XmlToSign.ConnectToNewObject("Chilkat.Xml")
if li_rc < 0 then
    destroy loo_XmlToSign
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_XmlToSign.Tag = "soapenv:Envelope"
loo_XmlToSign.AddAttribute("xmlns:obs","http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/")
loo_XmlToSign.AddAttribute("xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/")
loo_XmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",1,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")
loo_XmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",1,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
loo_XmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",1,"soapenv:mustUnderstand","1")
loo_XmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security|wsse:BinarySecurityToken",1,"EncodingType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary")
loo_XmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security|wsse:BinarySecurityToken",1,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1")
loo_XmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security|wsse:BinarySecurityToken",1,"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.
loo_XmlToSign.UpdateChildContent("soapenv:Header|wsse:Security|wsse:BinarySecurityToken","BinarySecurityToken_Base64Binary_Content")
loo_XmlToSign.UpdateAttrAt("soapenv:Body",1,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
loo_XmlToSign.UpdateAttrAt("soapenv:Body",1,"wsu:Id","id-396BB6026342EB5C0E1EA73593B3CC098")
loo_XmlToSign.UpdateChildContent("soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|idBiznesowy","000000011986")
loo_XmlToSign.UpdateChildContent("soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|rodzajPodmiotuRaportujacego","PA")

loo_Gen = create oleobject
li_rc = loo_Gen.ConnectToNewObject("Chilkat.XmlDSigGen")

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

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

//  -------- Reference 1 --------
loo_Gen.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)
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Cert
    return
end if

loo_Gen.SetX509Cert(loo_Cert,1)

loo_Gen.KeyInfoType = "Custom"

//  Create the custom KeyInfo XML..
loo_XmlCustomKeyInfo = create oleobject
li_rc = loo_XmlCustomKeyInfo.ConnectToNewObject("Chilkat.Xml")

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

loo_XmlCustomKeyInfo.EmitXmlDecl = 0
loo_Gen.CustomKeyInfoXml = loo_XmlCustomKeyInfo.GetXml()

//  Load XML to be signed...
loo_SbXml = create oleobject
li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder")

loo_XmlToSign.GetXmlSb(loo_SbXml)

//  Update BinarySecurityToken_Base64Binary_Content with the actual X509PKIPathv1 of the signing cert.
li_NReplaced = loo_SbXml.Replace("BinarySecurityToken_Base64Binary_Content",loo_Cert.X509PKIPathv1())

loo_Gen.Behaviors = "IndentedSignature"

//  Sign the XML...
li_Success = loo_Gen.CreateXmlDSigSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug loo_Gen.LastErrorText
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Cert
    destroy loo_XmlCustomKeyInfo
    destroy loo_SbXml
    return
end if

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

//  Save the signed XML to a file.
li_Success = loo_SbXml.WriteFile("qa_output/signedXml.xml","utf-8",0)

Write-Debug loo_SbXml.GetAsString()

//  ----------------------------------------
//  Verify the signatures we just produced...
loo_Verifier = create oleobject
li_rc = loo_Verifier.ConnectToNewObject("Chilkat.XmlDSig")

li_Success = loo_Verifier.LoadSignatureSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug loo_Verifier.LastErrorText
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Cert
    destroy loo_XmlCustomKeyInfo
    destroy loo_SbXml
    destroy loo_Verifier
    return
end if

li_NumSigs = loo_Verifier.NumSignatures
li_VerifyIdx = 0
do while li_VerifyIdx < li_NumSigs
    loo_Verifier.Selector = li_VerifyIdx
    li_Verified = loo_Verifier.VerifySignature(1)
    if li_Verified <> 1 then
        Write-Debug loo_Verifier.LastErrorText
        destroy loo_XmlToSign
        destroy loo_Gen
        destroy loo_Cert
        destroy loo_XmlCustomKeyInfo
        destroy loo_SbXml
        destroy loo_Verifier
        return
    end if

    li_VerifyIdx = li_VerifyIdx + 1
loop
Write-Debug "All signatures were successfully verified."


destroy loo_XmlToSign
destroy loo_Gen
destroy loo_Cert
destroy loo_XmlCustomKeyInfo
destroy loo_SbXml
destroy loo_Verifier