Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Create IRS MeF Login Service Request MessageThis 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.
// Important: See this note about string length limitations for strings returned by sp_OAMethod calls. // CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int DECLARE @sTmp0 nvarchar(4000) -- 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..) DECLARE @pfx int EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Pfx', @pfx OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int EXEC sp_OAMethod @pfx, 'LoadPfxFile', @success OUT, 'qa_data/pfx/cert_test123.pfx', 'test123' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @pfx, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @pfx 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. DECLARE @signingCert int EXEC sp_OAMethod @pfx, 'GetCert', @signingCert OUT, 0 EXEC sp_OAGetProperty @pfx, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @pfx, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @pfx RETURN END DECLARE @bdCert int EXEC @hr = sp_OACreate 'Chilkat_9_5_0.BinData', @bdCert OUT EXEC sp_OAMethod @signingCert, 'ExportCertDerBd', @success OUT, @bdCert DECLARE @sbCert64 int EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbCert64 OUT EXEC sp_OAMethod @bdCert, 'GetEncodedSb', @success OUT, 'base64', @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" 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: DECLARE @xml int EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Xml', @xml OUT EXEC sp_OASetProperty @xml, 'Tag', 'SOAP-ENV:Envelope' EXEC sp_OAMethod @xml, 'AddAttribute', @success OUT, 'xmlns:SOAP-ENV', 'http://schemas.xmlsoap.org/soap/envelope/' EXEC sp_OAMethod @xml, 'AddAttribute', @success OUT, 'xmlns:xsd', 'http://www.w3.org/2001/XMLSchema' EXEC sp_OAMethod @xml, 'AddAttribute', @success OUT, 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance' EXEC sp_OAMethod @xml, 'AddAttribute', @success OUT, 'xmlns:ds', 'http://www.w3.org/2000/09/xmldsig#' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'SOAP-ENV:Header|ns1:MeFHeader', 1, 'xmlns:ns1', 'http://www.irs.gov/a2a/mef/MeFHeader.xsd' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'SOAP-ENV:Header|ns1:MeFHeader', 1, 'Id', 'myHdr' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'SOAP-ENV:Header|ns1:MeFHeader|ns1:MessageID', '001232006026123abcde' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'SOAP-ENV:Header|ns1:MeFHeader|ns1:Action', 'https://www.irs.gov/a2a/mef/Login/' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'SOAP-ENV:Header|ns1:MeFHeader|ns1:MessageTs', '2006-09-27T08:55:29.348Z' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'SOAP-ENV:Header|ns1:MeFHeader|ns1:ETIN', '00123' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'SOAP-ENV:Header|ns1:MeFHeader|ns1:SessionKeyCd', 'Y' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'SOAP-ENV:Header|ns1:MeFHeader|ns1:TestCd', 'T' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'SOAP-ENV:Header|ns1:MeFHeader|ns1:AppSysID', 'username' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'SOAP-ENV:Header|ns1:MeFHeader|ns1:WSDLVersionNum', '10.0' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'SOAP-ENV:Header|wsse:Security', 1, 'xmlns:wsse', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'SOAP-ENV:Header|wsse:Security', 1, 'xmlns:wsu', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, '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' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, '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' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken', 1, 'wsu:Id', 'X509Token' EXEC sp_OAMethod @sbCert64, 'GetAsString', @sTmp0 OUT EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken', @sTmp0 EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'SOAP-ENV:Body', 1, 'ID', 'myBody' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, '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. DECLARE @gen int EXEC @hr = sp_OACreate 'Chilkat_9_5_0.XmlDSigGen', @gen OUT -- Indicate where the signature is to be placed. EXEC sp_OASetProperty @gen, 'SigLocation', 'SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security' -- Specify namespace prefix and algorithms. EXEC sp_OASetProperty @gen, 'SigNamespacePrefix', 'ds' EXEC sp_OASetProperty @gen, 'SignedInfoCanonAlg', 'EXCL_C14N' EXEC sp_OASetProperty @gen, 'SignedInfoDigestMethod', 'sha1' -- Indicate what is to be signed.. EXEC sp_OAMethod @gen, 'AddSameDocRef', @success OUT, 'myHdr', 'sha1', 'EXCL_C14N', '', '' EXEC sp_OAMethod @gen, 'AddSameDocRef', @success OUT, 'myBody', 'sha1', 'EXCL_C14N', '', '' -- We want the ds:Signature's KeyInfo to contain the following XML: -- <wsse:SecurityTokenReference> -- <wsse:Reference URI="#X509Token" /> -- </wsse:SecurityTokenReference> -- Create the above XML... DECLARE @xmlSecTokRef int EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Xml', @xmlSecTokRef OUT EXEC sp_OASetProperty @xmlSecTokRef, 'Tag', 'wsse:SecurityTokenReference' EXEC sp_OAMethod @xmlSecTokRef, 'UpdateAttrAt', @success OUT, 'wsse:Reference', 1, 'URI', '#X509Token' -- Indicate that the KeyInfoType is "Custom" to allow us to provide the exact XML snippet that will go in KeyInfo. EXEC sp_OASetProperty @gen, 'KeyInfoType', 'Custom' EXEC sp_OASetProperty @xmlSecTokRef, 'EmitCompact', 1 -- Don't emit the XML declarator for this (it's important). EXEC sp_OASetProperty @xmlSecTokRef, 'EmitXmlDecl', 0 EXEC sp_OAMethod @xmlSecTokRef, 'GetXml', @sTmp0 OUT EXEC sp_OASetProperty @gen, 'CustomKeyInfoXml', @sTmp0 -- Provide the certificate for signing. EXEC sp_OAMethod @gen, 'SetX509Cert', @success OUT, @signingCert, 1 EXEC @hr = sp_OADestroy @signingCert -- ------------------------------------------------------------------------------------------- -- 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). EXEC sp_OASetProperty @xml, 'EmitCompact', 1 DECLARE @sbLoginRequest int EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbLoginRequest OUT EXEC sp_OAMethod @xml, 'GetXmlSb', @success OUT, @sbLoginRequest -- Indicate we desire compact (single-line) signed XML. EXEC sp_OASetProperty @gen, 'Behaviors', 'CompactSignedXml' -- Sign the XML. This adds the ds:Signature to the XML in sbLoginRequest. EXEC sp_OAMethod @gen, 'CreateXmlDSigSb', @success OUT, @sbLoginRequest IF @success <> 1 BEGIN EXEC sp_OAGetProperty @gen, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @pfx EXEC @hr = sp_OADestroy @bdCert EXEC @hr = sp_OADestroy @sbCert64 EXEC @hr = sp_OADestroy @xml EXEC @hr = sp_OADestroy @gen EXEC @hr = sp_OADestroy @xmlSecTokRef EXEC @hr = sp_OADestroy @sbLoginRequest RETURN END -- Show the result EXEC sp_OAMethod @sbLoginRequest, 'GetAsString', @sTmp0 OUT PRINT @sTmp0 PRINT '----' -- Alternatively, we can create a completely non-compact signature, fully indented.. EXEC sp_OAMethod @sbLoginRequest, 'Clear', NULL EXEC sp_OASetProperty @xml, 'EmitCompact', 0 EXEC sp_OAMethod @xml, 'GetXmlSb', @success OUT, @sbLoginRequest EXEC sp_OASetProperty @gen, 'Behaviors', 'IndentedSignature' EXEC sp_OAMethod @gen, 'CreateXmlDSigSb', @success OUT, @sbLoginRequest EXEC sp_OAMethod @sbLoginRequest, 'GetAsString', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @pfx EXEC @hr = sp_OADestroy @bdCert EXEC @hr = sp_OADestroy @sbCert64 EXEC @hr = sp_OADestroy @xml EXEC @hr = sp_OADestroy @gen EXEC @hr = sp_OADestroy @xmlSecTokRef EXEC @hr = sp_OADestroy @sbLoginRequest END GO |
© 2000-2023 Chilkat Software, Inc. All Rights Reserved.