Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoXmlToSign
    Handle hoGen
    Variant vCert
    Handle hoCert
    Handle hoXmlCustomKeyInfo
    Variant vSbXml
    Handle hoSbXml
    Integer iNReplaced
    Handle hoVerifier
    Integer iNumSigs
    Integer iVerifyIdx
    Boolean iVerified
    String sTemp1

    Move False To iSuccess

    // 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.
    // ----------------------------------------------------------------------
    Move True To iSuccess
    // Create the above XML to be signed...
    Get Create (RefClass(cComChilkatXml)) To hoXmlToSign
    If (Not(IsComObjectCreated(hoXmlToSign))) Begin
        Send CreateComObject of hoXmlToSign
    End
    Set ComTag Of hoXmlToSign To "soapenv:Envelope"
    Get ComAddAttribute Of hoXmlToSign "xmlns:obs" "http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/" To iSuccess
    Get ComAddAttribute Of hoXmlToSign "xmlns:soapenv" "http://schemas.xmlsoap.org/soap/envelope/" To iSuccess
    Get ComUpdateAttrAt Of hoXmlToSign "soapenv:Header|wsse:Security" True "xmlns:wsse" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" To iSuccess
    Get ComUpdateAttrAt Of hoXmlToSign "soapenv:Header|wsse:Security" True "xmlns:wsu" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" To iSuccess
    Get ComUpdateAttrAt Of hoXmlToSign "soapenv:Header|wsse:Security" True "soapenv:mustUnderstand" "1" To iSuccess
    Get ComUpdateAttrAt Of hoXmlToSign "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" To iSuccess
    Get ComUpdateAttrAt Of hoXmlToSign "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" To iSuccess
    Get ComUpdateAttrAt Of hoXmlToSign "soapenv:Header|wsse:Security|wsse:BinarySecurityToken" True "wsu:Id" "X509-02BF0107214FC61449FD0013DF68F0359" To iSuccess
    // Note: The content of this XML node is a placeholder that will be updated below with the X509PKIPathv1 for the signing certificate.
    Send ComUpdateChildContent To hoXmlToSign "soapenv:Header|wsse:Security|wsse:BinarySecurityToken" "BinarySecurityToken_Base64Binary_Content"
    Get ComUpdateAttrAt Of hoXmlToSign "soapenv:Body" True "xmlns:wsu" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" To iSuccess
    Get ComUpdateAttrAt Of hoXmlToSign "soapenv:Body" True "wsu:Id" "id-396BB6026342EB5C0E1EA73593B3CC098" To iSuccess
    Send ComUpdateChildContent To hoXmlToSign "soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|idBiznesowy" "000000011986"
    Send ComUpdateChildContent To hoXmlToSign "soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|rodzajPodmiotuRaportujacego" "PA"

    Get Create (RefClass(cComChilkatXmlDSigGen)) To hoGen
    If (Not(IsComObjectCreated(hoGen))) Begin
        Send CreateComObject of hoGen
    End

    Set ComSigLocation Of hoGen To "soapenv:Envelope|soapenv:Header|wsse:Security"
    Set ComSigLocationMod Of hoGen To 0
    Set ComSigId Of hoGen To "SIG-BB965DFC3C8AAF87903C0ED898B8D2A8D"
    Set ComSigNamespacePrefix Of hoGen To "ds"
    Set ComSigNamespaceUri Of hoGen To "http://www.w3.org/2000/09/xmldsig#"
    Set ComSignedInfoCanonAlg Of hoGen To "EXCL_C14N"
    Set ComSignedInfoDigestMethod Of hoGen To "sha1"

    // Set the KeyInfoId before adding references..
    Set ComKeyInfoId Of hoGen To "KI-9D95C38916099AD2EE87DDAC1A76E97E4"

    // -------- Reference 1 --------
    Get ComAddSameDocRef Of hoGen "id-396BB6026342EB5C0E1EA73593B3CC098" "sha1" "EXCL_C14N" "obs" "" To iSuccess

    // 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)
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadPfxFile Of hoCert "qa_data/pfx/cert_test123.pfx" "test123" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoCert to vCert
    Get ComSetX509Cert Of hoGen vCert True To iSuccess

    Set ComKeyInfoType Of hoGen To "Custom"

    // Create the custom KeyInfo XML..
    Get Create (RefClass(cComChilkatXml)) To hoXmlCustomKeyInfo
    If (Not(IsComObjectCreated(hoXmlCustomKeyInfo))) Begin
        Send CreateComObject of hoXmlCustomKeyInfo
    End
    Set ComTag Of hoXmlCustomKeyInfo To "wsse:SecurityTokenReference"
    Get ComAddAttribute Of hoXmlCustomKeyInfo "wsse11:TokenType" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1" To iSuccess
    Get ComAddAttribute Of hoXmlCustomKeyInfo "xmlns:wsse11" "http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd" To iSuccess
    Get ComAddAttribute Of hoXmlCustomKeyInfo "wsu:Id" "STR-FF238E7C061332C5B19752C2FBC8CDEF2" To iSuccess
    Get ComUpdateAttrAt Of hoXmlCustomKeyInfo "wsse:Reference" True "URI" "#X509-02BF0107214FC61449FD0013DF68F0359" To iSuccess
    Get ComUpdateAttrAt Of hoXmlCustomKeyInfo "wsse:Reference" True "ValueType" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1" To iSuccess

    Set ComEmitXmlDecl Of hoXmlCustomKeyInfo To False
    Get ComGetXml Of hoXmlCustomKeyInfo To sTemp1
    Set ComCustomKeyInfoXml Of hoGen To sTemp1

    // Load XML to be signed...
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml
    If (Not(IsComObjectCreated(hoSbXml))) Begin
        Send CreateComObject of hoSbXml
    End
    Get pvComObject of hoSbXml to vSbXml
    Get ComGetXmlSb Of hoXmlToSign vSbXml To iSuccess

    // Update BinarySecurityToken_Base64Binary_Content with the actual X509PKIPathv1 of the signing cert.
    Get ComX509PKIPathv1 Of hoCert To sTemp1
    Get ComReplace Of hoSbXml "BinarySecurityToken_Base64Binary_Content" sTemp1 To iNReplaced

    Set ComBehaviors Of hoGen To "IndentedSignature"

    // Sign the XML...
    Get pvComObject of hoSbXml to vSbXml
    Get ComCreateXmlDSigSb Of hoGen vSbXml To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoGen To sTemp1
        Showln sTemp1
        Procedure_Return
    End

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

    // Save the signed XML to a file.
    Get ComWriteFile Of hoSbXml "qa_output/signedXml.xml" "utf-8" False To iSuccess

    Get ComGetAsString Of hoSbXml To sTemp1
    Showln sTemp1

    // ----------------------------------------
    // Verify the signatures we just produced...
    Get Create (RefClass(cComChilkatXmlDSig)) To hoVerifier
    If (Not(IsComObjectCreated(hoVerifier))) Begin
        Send CreateComObject of hoVerifier
    End
    Get pvComObject of hoSbXml to vSbXml
    Get ComLoadSignatureSb Of hoVerifier vSbXml To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoVerifier To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumSignatures Of hoVerifier To iNumSigs
    Move 0 To iVerifyIdx
    While (iVerifyIdx < iNumSigs)
        Set ComSelector Of hoVerifier To iVerifyIdx
        Get ComVerifySignature Of hoVerifier True To iVerified
        If (iVerified <> True) Begin
            Get ComLastErrorText Of hoVerifier To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Move (iVerifyIdx + 1) To iVerifyIdx
    Loop

    Showln "All signatures were successfully verified."


End_Procedure