Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

DataFlex Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(DataFlex) Create IRS MeF Login Service Request Message

This example demonstrates how to create a digitally signed Login Service Request Message.

This example used the documentation at https://www.irs.gov/pub/irs-utl/mef-doc-stp_ref_guide.pdf as a guide.

It creates signed XML as specified in section 4.1.1 found in Section 4: Example A2A Web Service Messages.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoPfx
    Boolean iSuccess
    Variant vSigningCert
    Handle hoSigningCert
    Variant vBdCert
    Handle hoBdCert
    Variant vSbCert64
    Handle hoSbCert64
    Handle hoXml
    Handle hoGen
    Handle hoXmlSecTokRef
    Variant vSbLoginRequest
    Handle hoSbLoginRequest
    String sTemp1
    Boolean bTemp1

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  The goal of this example is to create signed SOAP XML such as the following:

    //  IMPORTANT:  The IRS's PDF documentation at https://www.irs.gov/pub/irs-utl/mef-doc-stp_ref_guide.pdf
    //  contains a mistake in the section 4.1.1. Specifically, the Id="hdr" attribute is wrong.  It must match the Reference in the ds:Signature,
    //  which is why we changed it to "myHdr".

    //  <?xml version="1.0" encoding="UTF-8" ?>
    //  <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    //      <SOAP-ENV:Header>
    //          <ns1:MeFHeader xmlns:ns1="http://www.irs.gov/a2a/mef/MeFHeader.xsd" Id="myHdr">
    //              <ns1:MessageID>001232006026123abcde</ns1:MessageID>
    //              <ns1:Action>https://www.irs.gov/a2a/mef/Login/</ns1:Action>
    //              <ns1:MessageTs>2006-09-27T08:55:29.348Z</ns1:MessageTs>
    //              <ns1:ETIN>00123</ns1:ETIN>
    //              <ns1:SessionKeyCd>Y</ns1:SessionKeyCd>
    //              <ns1:TestCd>T</ns1:TestCd>
    //              <ns1:AppSysID>username</ns1:AppSysID>
    //              <ns1:WSDLVersionNum>10.0</ns1:WSDLVersionNum>
    //          </ns1:MeFHeader>
    //          <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">
    //              <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">/CgBr+1CEl/PtYtAaMB0GA1UdDgQWBBSrtL3lSnDOzSluMLggQUgHpkDFGTAJBgNV HRMEAjAAMBkGCSqGSIb2fQdBAAQMMAobBFY1LjADAgMoMA0GCSqGSIb3DQEB BQUAA4GBAC6SVzmX2I2kr5f0Na83ujcSwixpzHE2LqLBFsKm0GdPdarHFB2Qt85zbQ9 PRCROObm9HCIO5KMCjHp7D4g+.0QVA36IbEn8</wsse:BinarySecurityToken>
//
    //              <ds:Signature>
    //                  <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="#myHdr">
    //                          <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>qZk+NkcGgWq6PiVxeFDCbJzQ2J0=</ds:DigestValue>
    //                      </ds:Reference>
    //                      <ds:Reference URI="#myBody">
    //                          <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>hJg+RBw70m66rkqh+VEp5eVGcPE=</ds:DigestValue>
    //                      </ds:Reference>
    //                  </ds:SignedInfo>
    //                  <ds:SignatureValue>dsBeQ9qNXM1yU6BbWdBkSN676inwf+CgDORUKqOFuwWUzaOxUmHIq1Pb7uAB Yw6xkooLbQGS/M3t4Df+hd31prR9Pn9IFqG87Rt169+Il8L9GRgPhAATgne8Rh3jOaq nfrJ5BBM0VmbZdL+c7dfNcnpW13D/kltZMvlFRXR87Hs=</ds:SignatureValue>
    //                  <ds:KeyInfo>
    //                      <wsse:SecurityTokenReference>
    //                          <wsse:Reference URI="#X509Token" />
    //                      </wsse:SecurityTokenReference>
    //                  </ds:KeyInfo>
    //              </ds:Signature>
    //          </wsse:Security>
    //      </SOAP-ENV:Header>
    //      <SOAP-ENV:Body ID="myBody">
    //          <LoginRequest xmlns="http://www.irs.gov/a2a/mef/MeFMSIServices.xsd" />
    //      </SOAP-ENV:Body>
    //  </SOAP-ENV:Envelope>

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

    //  First, let's load the certificate + private key to be used for signing (from a PFX).
    //  (It is also possible to use certificates installed on a Windows system, or from other file formats..)
    Get Create (RefClass(cComChilkatPfx)) To hoPfx
    If (Not(IsComObjectCreated(hoPfx))) Begin
        Send CreateComObject of hoPfx
    End
    Get ComLoadPfxFile Of hoPfx "qa_data/pfx/cert_test123.pfx" "test123" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoPfx To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  We'll be needing the X.509 signing cert as base64 for the BinarySecurityToken, so let's get it now..
    //  The certificate having the private key should be the 1st in the PFX.
    Get ComGetCert Of hoPfx 0 To vSigningCert
    If (IsComObject(vSigningCert)) Begin
        Get Create (RefClass(cComChilkatCert)) To hoSigningCert
        Set pvComObject Of hoSigningCert To vSigningCert
    End
    Get ComLastMethodSuccess Of hoPfx To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoPfx To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatBinData)) To hoBdCert
    If (Not(IsComObjectCreated(hoBdCert))) Begin
        Send CreateComObject of hoBdCert
    End
    Get pvComObject of hoBdCert to vBdCert
    Get ComExportCertDerBd Of hoSigningCert vBdCert To iSuccess
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbCert64
    If (Not(IsComObjectCreated(hoSbCert64))) Begin
        Send CreateComObject of hoSbCert64
    End
    Get pvComObject of hoSbCert64 to vSbCert64
    Get ComGetEncodedSb Of hoBdCert "base64" vSbCert64 To iSuccess

    //  -------------------------------------------------------------------------------------------
    //  The XML before signing would look like this:

    //  <?xml version="1.0" encoding="UTF-8" ?>
    //  <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    //      <SOAP-ENV:Header>
    //          <ns1:MeFHeader xmlns:ns1="http://www.irs.gov/a2a/mef/MeFHeader.xsd" Id="myHdr">
    //              <ns1:MessageID>001232006026123abcde</ns1:MessageID>
    //              <ns1:Action>https://www.irs.gov/a2a/mef/Login/</ns1:Action>
    //              <ns1:MessageTs>2006-09-27T08:55:29.348Z</ns1:MessageTs>
    //              <ns1:ETIN>00123</ns1:ETIN>
    //              <ns1:SessionKeyCd>Y</ns1:SessionKeyCd>
    //              <ns1:TestCd>T</ns1:TestCd>
    //              <ns1:AppSysID>username</ns1:AppSysID>
    //              <ns1:WSDLVersionNum>10.0</ns1:WSDLVersionNum>
    //          </ns1:MeFHeader>
    //          <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">
    //              <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">/CgBr+1CEl/PtYtAaMB0GA1UdDgQWBBSrtL3lSnDOzSluMLggQUgHpkDFGTAJBgNV HRMEAjAAMBkGCSqGSIb2fQdBAAQMMAobBFY1LjADAgMoMA0GCSqGSIb3DQEB BQUAA4GBAC6SVzmX2I2kr5f0Na83ujcSwixpzHE2LqLBFsKm0GdPdarHFB2Qt85zbQ9 PRCROObm9HCIO5KMCjHp7D4g+.0QVA36IbEn8</wsse:BinarySecurityToken>
//
    //          </wsse:Security>
    //      </SOAP-ENV:Header>
    //      <SOAP-ENV:Body ID="myBody">
    //          <LoginRequest xmlns="http://www.irs.gov/a2a/mef/MeFMSIServices.xsd" />
    //      </SOAP-ENV:Body>
    //  </SOAP-ENV:Envelope>

    //  You can use the online XML code generation tool at http://tools.chilkat.io/xmlCreate.cshtml
    //  to generate the following XML creation source code:

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End

    Set ComTag Of hoXml To "SOAP-ENV:Envelope"
    Get ComAddAttribute Of hoXml "xmlns:SOAP-ENV" "http://schemas.xmlsoap.org/soap/envelope/" To iSuccess
    Get ComAddAttribute Of hoXml "xmlns:xsd" "http://www.w3.org/2001/XMLSchema" To iSuccess
    Get ComAddAttribute Of hoXml "xmlns:xsi" "http://www.w3.org/2001/XMLSchema-instance" To iSuccess
    Get ComAddAttribute Of hoXml "xmlns:ds" "http://www.w3.org/2000/09/xmldsig#" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Header|ns1:MeFHeader" True "xmlns:ns1" "http://www.irs.gov/a2a/mef/MeFHeader.xsd" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Header|ns1:MeFHeader" True "Id" "myHdr" To iSuccess
    Send ComUpdateChildContent To hoXml "SOAP-ENV:Header|ns1:MeFHeader|ns1:MessageID" "001232006026123abcde"
    Send ComUpdateChildContent To hoXml "SOAP-ENV:Header|ns1:MeFHeader|ns1:Action" "https://www.irs.gov/a2a/mef/Login/"
    Send ComUpdateChildContent To hoXml "SOAP-ENV:Header|ns1:MeFHeader|ns1:MessageTs" "2006-09-27T08:55:29.348Z"
    Send ComUpdateChildContent To hoXml "SOAP-ENV:Header|ns1:MeFHeader|ns1:ETIN" "00123"
    Send ComUpdateChildContent To hoXml "SOAP-ENV:Header|ns1:MeFHeader|ns1:SessionKeyCd" "Y"
    Send ComUpdateChildContent To hoXml "SOAP-ENV:Header|ns1:MeFHeader|ns1:TestCd" "T"
    Send ComUpdateChildContent To hoXml "SOAP-ENV:Header|ns1:MeFHeader|ns1:AppSysID" "username"
    Send ComUpdateChildContent To hoXml "SOAP-ENV:Header|ns1:MeFHeader|ns1:WSDLVersionNum" "10.0"
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV: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 hoXml "SOAP-ENV: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 hoXml "SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" True "ValueType" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV: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 hoXml "SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" True "wsu:Id" "X509Token" To iSuccess
    Get ComGetAsString Of hoSbCert64 To sTemp1
    Send ComUpdateChildContent To hoXml "SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" sTemp1
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Body" True "ID" "myBody" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Body|LoginRequest" True "xmlns" "http://www.irs.gov/a2a/mef/MeFMSIServices.xsd" To iSuccess

    //  -------------------------------------------------------------------------------------------
    //  Setup the XML DSig generator object to create the desired signature.

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

    //  Indicate where the signature is to be placed.
    Set ComSigLocation Of hoGen To "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security"

    //  Specify namespace prefix and algorithms.
    Set ComSigNamespacePrefix Of hoGen To "ds"
    Set ComSignedInfoCanonAlg Of hoGen To "EXCL_C14N"
    Set ComSignedInfoDigestMethod Of hoGen To "sha1"

    //  Indicate what is to be signed..
    Get ComAddSameDocRef Of hoGen "myHdr" "sha1" "EXCL_C14N" "" "" To iSuccess
    Get ComAddSameDocRef Of hoGen "myBody" "sha1" "EXCL_C14N" "" "" To iSuccess

    //  We want the ds:Signature's KeyInfo to contain the following XML:

    //      <wsse:SecurityTokenReference>
    //          <wsse:Reference URI="#X509Token" />
    //      </wsse:SecurityTokenReference>

    //  Create the above XML...
    Get Create (RefClass(cComChilkatXml)) To hoXmlSecTokRef
    If (Not(IsComObjectCreated(hoXmlSecTokRef))) Begin
        Send CreateComObject of hoXmlSecTokRef
    End
    Set ComTag Of hoXmlSecTokRef To "wsse:SecurityTokenReference"
    Get ComUpdateAttrAt Of hoXmlSecTokRef "wsse:Reference" True "URI" "#X509Token" To iSuccess

    //  Indicate that the KeyInfoType is "Custom" to allow us to provide the exact XML snippet that will go in KeyInfo.
    Set ComKeyInfoType Of hoGen To "Custom"
    Set ComEmitCompact Of hoXmlSecTokRef To True
    //  Don't emit the XML declarator for this (it's important).
    Set ComEmitXmlDecl Of hoXmlSecTokRef To False
    Get ComGetXml Of hoXmlSecTokRef To sTemp1
    Set ComCustomKeyInfoXml Of hoGen To sTemp1

    //  Provide the certificate for signing.
    Get ComSetX509Cert Of hoGen vSigningCert True To iSuccess
    Send Destroy of hoSigningCert

    //  -------------------------------------------------------------------------------------------
    //  OK.. everything is prepared to create the signed XML.
    //  XML signatures are super-complicated, and one source of errors involves whitespace in XML, which must be
    //  maintained exactly or else the signature becomes invalid.  One way to help avoid such errors,
    //  especially if the XML is intended for processing (i.e. not human eyes), is to sign compact XML
    //  where no extra whitespace exists.  Also, we can produce the XML signature on a single line.
    //  Therefore, the entire signed XML is one line (except for the XML declarator), and we avoid
    //  all potential problems relating to anything that migh muck with XML formatting or indentation.
    //  Let's do that..

    //  First, get the XML to be signed in compact form (one line, no indenting or whitespace).
    Set ComEmitCompact Of hoXml To True
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbLoginRequest
    If (Not(IsComObjectCreated(hoSbLoginRequest))) Begin
        Send CreateComObject of hoSbLoginRequest
    End
    Get pvComObject of hoSbLoginRequest to vSbLoginRequest
    Get ComGetXmlSb Of hoXml vSbLoginRequest To iSuccess

    //  Indicate we desire compact (single-line) signed XML.
    Set ComBehaviors Of hoGen To "CompactSignedXml"

    //  Sign the XML.  This adds the ds:Signature to the XML in sbLoginRequest.
    Get pvComObject of hoSbLoginRequest to vSbLoginRequest
    Get ComCreateXmlDSigSb Of hoGen vSbLoginRequest To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoGen To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Show the result
    Get ComGetAsString Of hoSbLoginRequest To sTemp1
    Showln sTemp1
    Showln "----"

    //  Alternatively, we can create a completely non-compact signature, fully indented..
    Send ComClear To hoSbLoginRequest
    Set ComEmitCompact Of hoXml To False
    Get pvComObject of hoSbLoginRequest to vSbLoginRequest
    Get ComGetXmlSb Of hoXml vSbLoginRequest To iSuccess
    Set ComBehaviors Of hoGen To "IndentedSignature"
    Get pvComObject of hoSbLoginRequest to vSbLoginRequest
    Get ComCreateXmlDSigSb Of hoGen vSbLoginRequest To iSuccess
    Get ComGetAsString Of hoSbLoginRequest To sTemp1
    Showln sTemp1


End_Procedure

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.