Creating Signed SOAP Message as a String with C# RRS feed

  • Question

  • I need to call web service that I have to send such soap request below by using C#. SoapBody and TimeStamp must be signed.    

    <soap:Envelope xmlns:soap="" xmlns:web="">
              <wsse:Security xmlns:wsse="" xmlns:wsu="">
                 <wsse:BinarySecurityToken EncodingType="" ValueType="" wsu:Id="X509-F4AF9673207AC5E0B614180667985061">MIIFsDCCBawwggSUoAMCAQICBgCaWhnEajANBgkqhkiG9w0BAQsFADBcMQswCQYDVQQGEwJUUjFNMEsGA1UEAwxETWFsaSBNw7xow7xyIEVsZWt0cm9uaWsgU2VydGlmaWthIEhpem1ldCBTYcSfbGF5xLFjxLFzxLEgLSBTw7xyw7xtIDEwHhcNMT</wsse:BinarySecurityToken>
                 <ds:Signature Id="SIG-3" xmlns:ds="">
                       <ds:CanonicalizationMethod Algorithm="">
                          <ec:InclusiveNamespaces PrefixList="soap web" xmlns:ec=""/>
                       <ds:SignatureMethod Algorithm=""/>
                       <ds:Reference URI="#id-2">
                             <ds:Transform Algorithm="">
                                <ec:InclusiveNamespaces PrefixList="web" xmlns:ec=""/>
                          <ds:DigestMethod Algorithm=""/>
                       <ds:Reference URI="#TS-1">
                             <ds:Transform Algorithm="">
                                <ec:InclusiveNamespaces PrefixList="wsse soap web" xmlns:ec=""/>
                          <ds:DigestMethod Algorithm=""/>
                    <ds:KeyInfo Id="KI-F4AF9673207AC5E0B614180667986422">
                       <wsse:SecurityTokenReference wsse11:TokenType="" wsu:Id="STR-F4AF9673207AC5E0B614180667986643" xmlns:wsse11="">
                          <wsse:Reference URI="#X509-F4AF9673207AC5E0B614180667985061" ValueType=""/>
                 <wsu:Timestamp wsu:Id="TS-1">
           <soap:Body wsu:Id="id-2" xmlns:wsu="">

    I created this soap request and get good response by using WCF Client CustomBinding and .pfx file that has certificate with private key. 

    Most of examples about signing soap messages use certificate from certificate store or pfx file. But in my scenario user's certificate (with private key) stored on smart card that certificate private key can't be exported. So in this case I can not use WCF CustomBinding or I can not use SignedXml class to sign SOAP message because when i try to get certificate programatically then private key is missing. (Also I got private key by using NCryptoki - PKCS wrapper but this private key type is different then RSA that i can't set for WCF client or SignedXmlClass private key.)

    So, I try to create this SOAP message as a string, and manually create DigestValues, BinarySecurityToken and SignatureValue by using smart card.

    I can calculate BinarySecurityToken value as :

     var certificate = GetX5092Certificate(); // X5092 certificate on smart card without private key
     string binarySecToken= Convert.ToBase64String(certificate.RawData);

    Also i have some code to calculate digest value as:

     byte[] dataToHashTS = Encoding.UTF8.GetBytes(TimeStampReference.OuterXml);
     XmlDsigExcC14NTransform transformDataTS = new XmlDsigExcC14NTransform("wsse soap web");
     transformDataTS.LoadInput(new MemoryStream(dataToHashTS));
     byte[] bDigestDataTS = transformDataTS.GetDigestedOutput(SHA1Managed.Create());
     string sDigestDataTS = Convert.ToBase64String(bDigestDataTS); //timestamp digest

    - I am not sure that If I calculate digest values right or not?

    - To calculate SignatureValue, I think I need to get hash of SignedInfo part. I have method that sign content (byte array) by using smart card. So how I can send SignedInfo content to this method? I mean is it enough to get hash of SignedInfo block as a string? OR I have get SignedInfo as XmlElement then transform + hash  as i did to calculate digest values?

    Any help would be much appreciated. Thanks.

    Monday, December 8, 2014 9:24 PM


All replies