Julian Alwandy

Julian Alwandy

  • NA
  • 5
  • 5.6k

Signing XML document for SOAP

Jul 4 2017 10:20 AM

I've made a small program for myself to see how the API works for the third party. Currently I am outputting correct XML information but the signatureValue or the binary security token is causing auth to fail.

XML Output:

 <soapenv: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#" xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header> <ns1:Operation xmlns:ns1="http://www.ros.ie/schemas/service/" Id="MsgOperation" soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" type="xsd:string">FormRct/File</ns1:Operation> <wsse:Security soapenv:actor="" soapenv:mustUnderstand="0" xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext/"> <wsse:BinarySecurityToken EncodingType="wsse:Base64Binary" Id="X509Token" ValueType="wsse:X509v3">MIIELzCCAxegAwIBAgIQMialyOJHTtqFvKw2kJIA7jANBgkqhkiG9w0BAQsFADBpMQswCQYDVQQGEwJJRTEeMBwGA1UEChMVUmV2ZW51ZSBDb21taXNzaW9uZXJzMSAwHgYDVQQLExdSZXZlbnVlIE9uLUxpbmUgU2VydmljZTEYMBYGA1UEAxMPREVWIFJPUyBDQSAyMDIxMB4XDTE3MDQyNjE0NTAxM1oXDTIyMDYwNzE3MzExMVowZzELMAkGA1UEBhMCSUUxLDAqBgNVBAoTI0lWWURaSUIgR1NGWkcgWVZMVkYgWEdXVkdYWElWR1cgTFREMRIwEAYDVQQLEwkyNjY1Njk3NDUxFjAUBgNVBAMTDUFETUlOSVNUUkFUT1IwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAITBFfz4jZ31ogJlwheAH+cG9sdrK6Gj+jaixbm6J+rAocZTT5MQYo6cmc7wJXbHOxD5OXEvLoIFP2r/mzGjDYCr/RK8GtiraNK6c1ioi8bSVEqFkxqvkbtwBMH1bFY84xADJ7jqzsdmroGZ4mGfWo+fw502B3EpFolm/xniWobRAgMBAAGjggFXMIIBUzAfBgNVHSMEGDAWgBShLecYtPbHqwQaTAxXr4oPzPT/8DCCAQ8GA1UdIASCAQYwggECMIH/BgsqgnS76CMBAQEBATCB7zAaBggrBgEFBQcCARYOd3d3LnJldmVudWUuaWUwgdAGCCsGAQUFBwICMIHDGoHAQ2VydGlmaWNhdGVzIGlzc3VlZCB1bmRlciB0aGlzIENQIGFyZSBxdWFsaWZpZWQgY2VydGlmaWNhdGVzIHVuZGVyIHRoZSBFbGVjdHJvbmljIENvbW1lcmNlIEFjdCAyMDAwIGZvciB1c2UgYnkgQXBwcm92ZWQgb3IgQXV0aG9yaXNlZCBwZXJzb25zIG9ubHkgdG8gY29tbXVuaWNhdGUgd2l0aCB0aGUgUmV2ZW51ZSBDb21taXNzaW9uZXJzMB0GA1UdDgQWBBTmn5zlp58q4e4IAaT6yWV7TJn4CzANBgkqhkiG9w0BAQsFAAOCAQEAEIGnh5pj2IQYwfWG6i/YR4SOnC00j/K3pDUK82y3GZV/98+Orb+T8gnTy2KM7qHfwfDuIKqnV9BPbvdlZXJ6wet+jWfZ6bz+X31qQJM7L5Tsw5mXflzE8fQibm+pF1GmDroW9f7D2UgP0SkKUGoKPWVRyUHbB1+o3B12sRkhpYpqogqYX035v5wpKzmB0xEt0QScT5Z2GrkSQ1GHOr9z/xZ7J//AaT7dDXV2UGstYD16wre6p5TSX7nve0hsfSUfIN9ybfsmwFGQRP49nQJTPNK1yYULFCV2eytPOHdIG2e2UgrZ5IoPI+0wGxyT156BuIcigWJ2ko56CszEg/V19w==</wsse:BinarySecurityToken> <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> <SignedInfo> <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" /> <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /> <Reference URI="#MsgBody"> <Transforms> <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" /> </Transforms> <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /> <DigestValue>RjQbhsvCFFRerpuXbtk/aDMlbCI=</DigestValue> </Reference> <Reference URI="#MsgOperation"> <Transforms> <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" /> </Transforms> <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /> <DigestValue>jqLzbQY6xUddOi6pOS7954lqf2A=</DigestValue> </Reference> </SignedInfo> <SignatureValue>Zu2Ld1zeSnWGTgNeQc20IjqjMPE4aUHtYbI1RDDRof/4lXHAuiPDjGFJZOaj3I5DseZQjELgeu4rjgZxKervL2H0xvcPisTJ6wSS4aePMbi3VRQe6jSTWFqLn4BctFMphe1YZ7BGNVESUM9RYpDoZKpyEeB3lPiw7D5YuvRFwEE=</SignatureValue> <KeyInfo> <wsse:SecurityTokenReference> <wsse:Reference URI="#X509Token" /> </wsse:SecurityTokenReference> </KeyInfo> </Signature> </wsse:Security> </soapenv:Header> <soapenv:Body Id="MsgBody"> <rct:SubmitRctRequest xmlns:rct="http://www.revenue.ie/schemas/rct/" xmlns:cus="http://www.revenue.ie/schemas/common/customer/customer-v1" xmlns:rev="http://www.revenue.ie/schemas/common/revenue-types-v1"> <rct:PaymentNotification> <rct:Principal> <cus:Customer> <cus:ContactDetails> <cus:NameDetails type="OFFICIAL"> <cus:FirstName>INSERT OWN FIRSTNAME HERE</cus:FirstName> <cus:MainName>INSERT OWN MAINNAME HERE</cus:MainName> <cus:Title>Mr.</cus:Title> <cus:Suffix>Snr</cus:Suffix> </cus:NameDetails> </cus:ContactDetails> <cus:AccountDetails> <cus:Account> <rev:Registration> <rev:Type>RCT</rev:Type> <rev:Number>INSERT OWN PPSN HERE</rev:Number> </rev:Registration> </cus:Account> </cus:AccountDetails> </cus:Customer> </rct:Principal> <rct:Subcontractor> <cus:Customer> <cus:ContactDetails> <cus:NameDetails> <cus:FirstName>PIT</cus:FirstName> <cus:MainName>WLFFGII    </cus:MainName> <cus:Title>Mr.</cus:Title> </cus:NameDetails> </cus:ContactDetails> <cus:AccountDetails> <cus:Account> <rev:Registration> <rev:Type>RCT</rev:Type> <rev:Number>3135939V</rev:Number> </rev:Registration> </cus:Account> </cus:AccountDetails> </cus:Customer> </rct:Subcontractor> <rct:NotificationUpdateType>NEW</rct:NotificationUpdateType> <rct:ContractId>4753</rct:ContractId> <rct:GrossPay>1000</rct:GrossPay> <rct:AppliedRate>20</rct:AppliedRate> <rct:PostPnRateApplied>1</rct:PostPnRateApplied> <rct:PostPnReason>Test Reason</rct:PostPnReason> <rct:IsCarriedForward>0</rct:IsCarriedForward> </rct:PaymentNotification> <rct:ExternalId>037c1936-0f0d-41fc-9ae3-0c5aee839181</rct:ExternalId> </rct:SubmitRctRequest> </soapenv:Body> </soapenv:Envelope>

Respone I get :

<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <soapenv:Fault> <faultcode>soapenv:Server</faultcode> <faultstring>An error occured during authentication</faultstring> <detail> <ns1:ROSFaultDetail xmlns:ns1="http://www.ros.ie/schemas/service/"></ns1:ROSFaultDetail> </detail> </soapenv:Fault> </soapenv:Body> </soapenv:Envelope>

My code (Pretty basic, just want it working first) :

 XmlDocument _xmlRequest = new XmlDocument(); XmlDocument document = new XmlDocument();     document.Load(@"C:\Users\v-jualwa\Downloads\SampleXMLFiles\Sample Files\PaymentNotification\Submit\formRct_submitPNRequest.xml"); // If document has Xml declaration then remove it' if ((document.FirstChild.NodeType == XmlNodeType.XmlDeclaration)) {         document.RemoveChild(document.FirstChild); } XmlElement soapEnvelope = _xmlRequest.CreateElement("soapenv", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/"); // Creates <soapenv:Envelope>'     soapEnvelope.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");     soapEnvelope.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");     soapEnvelope.SetAttribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#");     soapEnvelope.SetAttribute("xmlns:wsse", "http://schemas.xmlsoap.org/ws/2002/07/secext"); XmlElement soapHeader = _xmlRequest.CreateElement("soapenv", "Header", "http://schemas.xmlsoap.org/soap/envelope/"); XmlElement soapOperation = _xmlRequest.CreateElement("ns1", "Operation", "http://schemas.xmlsoap.org/soap/envelope/"); // Creates <ns1:Operation> child'     soapOperation.SetAttribute("xmlns:ns1", "http://www.ros.ie/schemas/service/");     soapOperation.SetAttribute("Id", "MsgOperation");     soapOperation.SetAttribute("actor", "http://schemas.xmlsoap.org/soap/envelope/", "http://schemas.xmlsoap.org/soap/actor/next");     soapOperation.SetAttribute("mustUnderstand", "http://schemas.xmlsoap.org/soap/envelope/", "0");     soapOperation.SetAttribute("xsi:type", "xsd:string");     soapOperation.InnerXml = "FormRct/File"; XmlElement soapBody = _xmlRequest.CreateElement("soapenv", "Body", "http://schemas.xmlsoap.org/soap/envelope/"); // Creates <soapenv:Body>'     soapBody.SetAttribute("Id", "MsgBody");     soapBody.InnerXml = document.OuterXml; // soapBody.InnerXml = document.OuterXml; XmlElement wsseSecurity = _xmlRequest.CreateElement("wsse", "Security", "http://schemas.xmlsoap.org/ws/2002/07/secext/"); // Creates <wsse:Security>'     wsseSecurity.SetAttribute("actor", "http://schemas.xmlsoap.org/soap/envelope/", "");     wsseSecurity.SetAttribute("mustUnderstand", "http://schemas.xmlsoap.org/soap/envelope/", "0"); XmlElement wsseBinarySecurityToken = _xmlRequest.CreateElement("wsse", "BinarySecurityToken", "http://schemas.xmlsoap.org/ws/2002/07/secext/"); // Creates <wsse:BinarySecurityToken>'     wsseBinarySecurityToken.SetAttribute("EncodingType", "wsse:Base64Binary");     wsseBinarySecurityToken.SetAttribute("Id", "X509Token");     wsseBinarySecurityToken.SetAttribute("ValueType", "wsse:X509v3");     wsseBinarySecurityToken.InnerXml = getCertificate("password");      _xmlRequest.AppendChild(soapEnvelope);     soapEnvelope.AppendChild(soapHeader);     soapHeader.AppendChild(soapOperation);     soapHeader.AppendChild(wsseSecurity);     wsseSecurity.AppendChild(wsseBinarySecurityToken);     soapEnvelope.AppendChild(soapBody); // Get the key pair from the cert. var csp = new RSACryptoServiceProvider( new CspParameters(24 /* PROV_RSA_AES */));     csp.PersistKeyInCsp = false;     csp.FromXmlString(getSignedKey("password")); // Creating the XML signing object. SignedXml sxml = new SignedXml(_xmlRequest);     sxml.SigningKey = csp; // Set the canonicalization method for the document.     sxml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl; // No comments. //Set the SignatureMethod     sxml.SignedInfo.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#rsa-sha1"; // SHA //Add Keyinfo KeyInfo keyInfo = new KeyInfo(); XmlElement SecurityTokenReference = _xmlRequest.CreateElement("wsse", "SecurityTokenReference", "http://schemas.xmlsoap.org/ws/2002/07/secext/"); XmlElement wsseReference = _xmlRequest.CreateElement("wsse", "Reference", "http://schemas.xmlsoap.org/ws/2002/07/secext/");     wsseReference.SetAttribute("URI", "#X509Token"); SecurityTokenReference.AppendChild(wsseReference); var keyInfoData = new KeyInfoNode(SecurityTokenReference);     keyInfo.AddClause(keyInfoData);     sxml.KeyInfo = keyInfo; // Create an empty reference (not enveloped) for the XPath // transformation. Reference r = new Reference("#MsgBody"); // Create reference for MsgBody Reference xr = new Reference("#MsgOperation"); // Create the XPath transform and add it to the reference list. XmlDsigExcC14NTransform env = new XmlDsigExcC14NTransform();     r.AddTransform(env);     xr.AddTransform(env); // Add the reference to the SignedXml object.     sxml.AddReference(r);     sxml.AddReference(xr); // Compute the signature.     sxml.ComputeSignature(); // Get the signature XML and add it to the document element. XmlElement sig = sxml.GetXml();     wsseSecurity.AppendChild(sig);      _xmlRequest.Save((System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\formRct_submitPNRequest.soap"));   }  public static string getSignedKey(String input) {     MD5 md5 = new MD5CryptoServiceProvider();     md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)); byte[] result = md5.Hash; String Base64Result = Convert.ToBase64String(result);     X509Certificate2 cert = new X509Certificate2(@"C:\Users\v-jualwa\Downloads\RosCerts\RichardNolan_PIT1.p12", Base64Result, X509KeyStorageFlags.Exportable); return cert.PrivateKey.ToXmlString(true); }  public static string getCertificate(String input) {     MD5 md5 = new MD5CryptoServiceProvider();     md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)); byte[] result = md5.Hash; String Base64Result = Convert.ToBase64String(result);     X509Certificate cert = new X509Certificate(@"C:\Users\v-jualwa\Downloads\RosCerts\RichardNolan_PIT1.p12", Base64Result, X509KeyStorageFlags.Exportable); return Convert.ToBase64String(cert.Export(X509ContentType.Cert, Base64Result));  }

Answers (1)