Sample code for 30+ languages & platforms
PowerBuilder

Create IRS MeF Login Service Request Message

See more XML Digital Signatures Examples

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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pfx
oleobject loo_SigningCert
oleobject loo_BdCert
oleobject loo_SbCert64
oleobject loo_XmlToSign
oleobject loo_Gen
oleobject loo_Cert
oleobject loo_XmlCustomKeyInfo
oleobject loo_SbXml
long li_NReplaced

li_Success = 0

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

//  --------------------------------------------------------------------------------
//  Also see Chilkat's Online WSDL Code Generator
//  to generate code and SOAP Request and Response XML for each operation in a WSDL.
//  --------------------------------------------------------------------------------

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

//  <?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">
//      <SOAP-ENV:Header>
//          <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
//              <wsse:BinarySecurityToken 
//  		xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
//  		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#X509v3"
//  		wsu:Id="CertId-1673181727">MIIHab...n71P</wsse:BinarySecurityToken>
//              <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
//                  <ds:SignedInfo>
//                      <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
//                      <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
//                      <ds:Reference URI="#id-1214941501">
//                          <ds:Transforms>
//                              <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
//                          </ds:Transforms>
//                          <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
//                          <ds:DigestValue>abcdefghijkLMNOPQRSTUVwxyz012345Em0o3VEOTck=</ds:DigestValue>
//                      </ds:Reference>
//                      <ds:Reference URI="#id-1871558655">
//                          <ds:Transforms>
//                              <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
//                          </ds:Transforms>
//                          <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
//                          <ds:DigestValue>abcdefghijkLMNOPQRSTUVwxyz012345Xm5ViA+royg=</ds:DigestValue>
//                      </ds:Reference>
//                  </ds:SignedInfo>
//                  <ds:SignatureValue>abcd...5Nbw==</ds:SignatureValue>
//                  <ds:KeyInfo Id="KeyId-256137097">
//                      <wsse:SecurityTokenReference xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="STRId-1862925355">
//                          <wsse:Reference URI="#CertId-1673181727" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/>
//                      </wsse:SecurityTokenReference>
//                  </ds:KeyInfo>
//              </ds:Signature>
//          </wsse:Security>
//          <ns1:MeFHeader xmlns:ns1="http://www.irs.gov/a2a/mef/MeFHeader.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-1214941501">
//              <ns1:MessageID>12345202018200000234</ns1:MessageID>
//              <ns1:Action>Login</ns1:Action>
//              <ns1:MessageTs>2020-06-30T15:25:42.678Z</ns1:MessageTs>
//              <ns1:ETIN>12345</ns1:ETIN>
//              <ns1:SessionKeyCd>Y</ns1:SessionKeyCd>
//              <ns1:TestCd>P</ns1:TestCd>
//              <ns1:AppSysID>65432190</ns1:AppSysID>
//              <ns1:WSDLVersionNum>10.3</ns1:WSDLVersionNum>
//              <ns1:ClientSoftwareTxt>SOATest</ns1:ClientSoftwareTxt>
//          </ns1:MeFHeader>
//      </SOAP-ENV:Header>
//      <SOAP-ENV:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-1871558655">
//          <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..)
loo_Pfx = create oleobject
li_rc = loo_Pfx.ConnectToNewObject("Chilkat.Pfx")
if li_rc < 0 then
    destroy loo_Pfx
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Pfx.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
if li_Success = 0 then
    Write-Debug loo_Pfx.LastErrorText
    destroy loo_Pfx
    return
end if

//  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.

loo_SigningCert = create oleobject
li_rc = loo_SigningCert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Pfx.CertAt(0,loo_SigningCert)
if li_Success = 0 then
    Write-Debug loo_Pfx.LastErrorText
    destroy loo_Pfx
    destroy loo_SigningCert
    return
end if

loo_BdCert = create oleobject
li_rc = loo_BdCert.ConnectToNewObject("Chilkat.BinData")

loo_SigningCert.ExportCertDerBd(loo_BdCert)
loo_SbCert64 = create oleobject
li_rc = loo_SbCert64.ConnectToNewObject("Chilkat.StringBuilder")

loo_BdCert.GetEncodedSb("base64",loo_SbCert64)

//  -------------------------------------------------------------------------------------------
//  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">
//      <SOAP-ENV:Header>
//          <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
//              <wsse:BinarySecurityToken 
//  		xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
//  		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#X509v3"
//  		wsu:Id="CertId-1673181727">MIIHab...n71P</wsse:BinarySecurityToken>
//          </wsse:Security>
//          <ns1:MeFHeader xmlns:ns1="http://www.irs.gov/a2a/mef/MeFHeader.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-1214941501">
//              <ns1:MessageID>12345202018200000234</ns1:MessageID>
//              <ns1:Action>Login</ns1:Action>
//              <ns1:MessageTs>2020-06-30T15:25:42.678Z</ns1:MessageTs>
//              <ns1:ETIN>12345</ns1:ETIN>
//              <ns1:SessionKeyCd>Y</ns1:SessionKeyCd>
//              <ns1:TestCd>P</ns1:TestCd>
//              <ns1:AppSysID>65432190</ns1:AppSysID>
//              <ns1:WSDLVersionNum>10.3</ns1:WSDLVersionNum>
//              <ns1:ClientSoftwareTxt>SOATest</ns1:ClientSoftwareTxt>
//          </ns1:MeFHeader>
//      </SOAP-ENV:Header>
//      <SOAP-ENV:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-1871558655">
//          <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:

//  Create the XML to be signed...
loo_XmlToSign = create oleobject
li_rc = loo_XmlToSign.ConnectToNewObject("Chilkat.Xml")

loo_XmlToSign.Tag = "SOAP-ENV:Envelope"
loo_XmlToSign.AddAttribute("xmlns:SOAP-ENV","http://schemas.xmlsoap.org/soap/envelope/")
loo_XmlToSign.AddAttribute("xmlns:xsd","http://www.w3.org/2001/XMLSchema")
loo_XmlToSign.AddAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
loo_XmlToSign.UpdateAttrAt("SOAP-ENV: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("SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",1,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
loo_XmlToSign.UpdateAttrAt("SOAP-ENV: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("SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",1,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3")
loo_XmlToSign.UpdateAttrAt("SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",1,"wsu:Id","CertId-1673181727")
loo_XmlToSign.UpdateChildContent("SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",loo_SbCert64.GetAsString())
loo_XmlToSign.UpdateAttrAt("SOAP-ENV:Header|ns1:MeFHeader",1,"xmlns:ns1","http://www.irs.gov/a2a/mef/MeFHeader.xsd")
loo_XmlToSign.UpdateAttrAt("SOAP-ENV:Header|ns1:MeFHeader",1,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
loo_XmlToSign.UpdateAttrAt("SOAP-ENV:Header|ns1:MeFHeader",1,"wsu:Id","id-1214941501")
loo_XmlToSign.UpdateChildContent("SOAP-ENV:Header|ns1:MeFHeader|ns1:MessageID","12345202018200000234")
loo_XmlToSign.UpdateChildContent("SOAP-ENV:Header|ns1:MeFHeader|ns1:Action","Login")
loo_XmlToSign.UpdateChildContent("SOAP-ENV:Header|ns1:MeFHeader|ns1:MessageTs","2020-06-30T15:25:42.678Z")
loo_XmlToSign.UpdateChildContentInt("SOAP-ENV:Header|ns1:MeFHeader|ns1:ETIN",12345)
loo_XmlToSign.UpdateChildContent("SOAP-ENV:Header|ns1:MeFHeader|ns1:SessionKeyCd","Y")
loo_XmlToSign.UpdateChildContent("SOAP-ENV:Header|ns1:MeFHeader|ns1:TestCd","P")
loo_XmlToSign.UpdateChildContentInt("SOAP-ENV:Header|ns1:MeFHeader|ns1:AppSysID",65432190)
loo_XmlToSign.UpdateChildContent("SOAP-ENV:Header|ns1:MeFHeader|ns1:WSDLVersionNum","10.3")
loo_XmlToSign.UpdateChildContent("SOAP-ENV:Header|ns1:MeFHeader|ns1:ClientSoftwareTxt","SOATest")
loo_XmlToSign.UpdateAttrAt("SOAP-ENV:Body",1,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
loo_XmlToSign.UpdateAttrAt("SOAP-ENV:Body",1,"wsu:Id","id-1871558655")
loo_XmlToSign.UpdateAttrAt("SOAP-ENV:Body|LoginRequest",1,"xmlns","http://www.irs.gov/a2a/mef/MeFMSIServices.xsd")

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

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

loo_Gen.SigLocation = "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security"
loo_Gen.SigLocationMod = 0
loo_Gen.SigNamespacePrefix = "ds"
loo_Gen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
loo_Gen.SignedInfoCanonAlg = "EXCL_C14N"
loo_Gen.SignedInfoDigestMethod = "sha256"

//  Set the KeyInfoId before adding references..
loo_Gen.KeyInfoId = "KeyId-256137097"

//  -------- Reference 1 --------
loo_Gen.AddSameDocRef("id-1214941501","sha256","EXCL_C14N","","")

//  -------- Reference 2 --------
loo_Gen.AddSameDocRef("id-1871558655","sha256","EXCL_C14N","","")

//  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 = 0 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Pfx
    destroy loo_SigningCert
    destroy loo_BdCert
    destroy loo_SbCert64
    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("xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
loo_XmlCustomKeyInfo.AddAttribute("wsu:Id","STRId-1862925355")
loo_XmlCustomKeyInfo.UpdateAttrAt("wsse:Reference",1,"URI","#CertId-1673181727")
loo_XmlCustomKeyInfo.UpdateAttrAt("wsse:Reference",1,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3")

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 X509 of the signing cert.
li_NReplaced = loo_SbXml.Replace("BinarySecurityToken_Base64Binary_Content",loo_Cert.GetEncoded())

loo_Gen.Behaviors = "IndentedSignature"

//  Sign the XML...
li_Success = loo_Gen.CreateXmlDSigSb(loo_SbXml)
if li_Success = 0 then
    Write-Debug loo_Gen.LastErrorText
    destroy loo_Pfx
    destroy loo_SigningCert
    destroy loo_BdCert
    destroy loo_SbCert64
    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("c:/temp/qa_output/signedXml.xml","utf-8",0)

Write-Debug loo_SbXml.GetAsString()


destroy loo_Pfx
destroy loo_SigningCert
destroy loo_BdCert
destroy loo_SbCert64
destroy loo_XmlToSign
destroy loo_Gen
destroy loo_Cert
destroy loo_XmlCustomKeyInfo
destroy loo_SbXml