0

I have visited almost every question on StackOverflow that was related to my problem but could not get an idea from it. That's why I have decided to ask.

I have to consume Java-based SOAP service in c#.

for authentication, I only have a p12 certificate and a private key.

SoapUI request configurations are soap ui config for my request

Security headers that I need

<soapenv:Envelope 
      xmlns:ns="name space url" 
      xmlns:ns1="name space url" 
      xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>
      <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 
            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="X509-1B2DC25F2B168EC1EE1592912049599140">
            <!--Binary Security token value-->           
         </wsse:BinarySecurityToken>
         <ds:Signature Id="SIG-1B2DC25F2B168EC1EE1592912049601144" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
            <ds:SignedInfo>
               <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
                  <ec:InclusiveNamespaces PrefixList="ns ns1 soapenv" xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" />
               </ds:CanonicalizationMethod>
               <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
               <ds:Reference URI="#id-1B2DC25F2B168EC1EE1592912049599143">
                  <ds:Transforms>
                     <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
                        <ec:InclusiveNamespaces PrefixList="ns ns1" xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" />
                     </ds:Transform>
                  </ds:Transforms>
                  <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                  <ds:DigestValue>bYJZ+gUSBWzYvN9BU85AIrG+Khc=</ds:DigestValue>
               </ds:Reference>
               <ds:Reference URI="#TS-1B2DC25F2B168EC1EE1592912049592139">
                  <ds:Transforms>
                     <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
                        <ec:InclusiveNamespaces PrefixList="wsse ns ns1 soapenv" xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" />
                     </ds:Transform>
                  </ds:Transforms>
                  <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                  <ds:DigestValue>WqrmT1JqYzIVay3Tiso9oZxEki8=</ds:DigestValue>
               </ds:Reference>
            </ds:SignedInfo>
            <ds:SignatureValue>
            <!--signature value-->
            </ds:SignatureValue>
            <ds:KeyInfo Id="KI-1B2DC25F2B168EC1EE1592912049599141">
               <wsse:SecurityTokenReference wsu:Id="STR-1B2DC25F2B168EC1EE1592912049599142">
                  <wsse:Reference URI="#X509-1B2DC25F2B168EC1EE1592912049599140" 
                  ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" />
               </wsse:SecurityTokenReference>
            </ds:KeyInfo>
         </ds:Signature>
         <wsu:Timestamp wsu:Id="TS-1B2DC25F2B168EC1EE1592912049592139">
            <wsu:Created>2020-06-23T11:34:09.592Z</wsu:Created>
            <wsu:Expires>2020-06-23T11:35:09.592Z</wsu:Expires>
         </wsu:Timestamp>
      </wsse:Security>
   </soapenv:Header>
   <soapenv:Body wsu:Id="id-1B2DC25F2B168EC1EE1592912049599143" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
   </soapenv:Body>
</soapenv:Envelope>

Security Headers that I created

<s:Envelope 
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns="name space url" 
    xmlns:ns1="name space url" >
<s:Header>
    <ActivityId CorrelationId="8c460bf2-547b-4acb-b1b3-37b8aeb3e1e9" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">00000000-0000-0000-0000-000000000000</ActivityId>
    <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
        <o:BinarySecurityToken 
            u:Id="uuid-ef870490-184f-4686-b72a-15f15915831a-1" 
            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" 
            xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <!--Binary Security token value--> 
        </o: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="#_0">
                    <Transforms>
                        <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
                    </Transforms>
                    <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
                    <DigestValue>f+P5i1Ny3wV/6BkLIPsXIhW98oY=</DigestValue>
                </Reference>
            </SignedInfo>
        <SignatureValue>
            <!--signature value-->
        </SignatureValue>
        <KeyInfo>
            <o:SecurityTokenReference>
                <o:Reference ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" URI="#uuid-ef870490-184f-4686-b72a-15f15915831a-1"/>
            </o:SecurityTokenReference>
        </KeyInfo>
        </Signature>
        <u:Timestamp u:Id="_0" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <u:Created>2021-01-03T09:37:57.203Z</u:Created>
            <u:Expires>2021-01-03T10:37:57.203Z</u:Expires>
        </u:Timestamp>
    </o:Security>
</s:Header>
<s:Body></s:Body>
</s:Envelope>

Here is my web.config file

  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="_Port">
          <!--    WsdlImporter encountered unrecognized policy assertions in ServiceDescription 'url':-->
          <!--    <wsdl:binding name='Binding'>    -->
          <!--        <wssp:Integrity xmlns:wssp="http://www.bea.com/wls90/security/policy">..</wssp:Integrity>    -->
          <!--        <wssp:MessageAge xmlns:wssp="http://www.bea.com/wls90/security/policy">..</wssp:MessageAge>    -->
          <textMessageEncoding messageVersion="Soap11" />
          <httpsTransport />
        </binding>
        <binding name="Binding">
          <textMessageEncoding messageVersion="Soap11" />
          <httpTransport />
        </binding>
      </customBinding>
    </bindings>
    <client>
      <endpoint address="service address"
        binding="customBinding" bindingConfiguration="_Port"
        contract="__ServiceType"
        name="Port" >
      </endpoint>
    </client>
  </system.serviceModel>

my custom binding is here

        private Binding GetCustomBinding3()
        {
            var element = SecurityBindingElement.CreateCertificateOverTransportBindingElement();

            element.AllowInsecureTransport = true;
            element.IncludeTimestamp = true;
            element.EnableUnsecuredResponse = true;

            //element.RequireSignatureConfirmation = false;
            //messageSecurity.RequireSignatureConfirmation = true;

            element.KeyEntropyMode = SecurityKeyEntropyMode.ClientEntropy;

            //element.EndpointSupportingTokenParameters.Signed.Add(new X509SecurityTokenParameters());
            element.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Default;

            element.LocalClientSettings.TimestampValidityDuration = TimeSpan.FromHours(1);

            //element.MessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt;
            element.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10;

            element.SecurityHeaderLayout = SecurityHeaderLayout.LaxTimestampLast;

            element.SetKeyDerivation(false);

            //element.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;

            var httpsTransport = new HttpsTransportBindingElement { RequireClientCertificate = true };
            var textMessage = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8);

            var binding = new CustomBinding();
            binding.Elements.Add(element);
            binding.Elements.Add(textMessage);
            binding.Elements.Add(httpsTransport);

            return binding;
        }

and I am calling service like that

var identity = EndpointIdentity.CreateX509CertificateIdentity(GetCertificate());
//var identity = EndpointIdentity.CreateDnsIdentity(Common.serviceEndPoint);

var address = new EndpointAddress(new Uri(Common.clientEndpointAddress), identity);
var client = new USSekundaereErhvervsmaessigeBrugereServiceTypeClient("Port", address);
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine,StoreName.TrustedPeople,X509FindType.FindByThumbprint,Common.clientCertificateFindValue);
client.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication{CertificateValidationMode = X509CertificateValidationMode.None};
client.Endpoint.Binding = GetCustomBinding3();
client.Open();
var response = client.getUSKoeretoejRegistreringSekundaerErhvervListeHent(GetRequest(vehicleInfo));

given above code is giving me an exception

System.ServiceModel.FaultException: Could not validate signature using any of the supported token types\r\n\r\n

It would be very helpful for me if someone provides some suggestion that, where I am doing wrong. Thanks in advance.

Fahad
  • 37
  • 1
  • 10
  • The certificate is rsa-sha1. So you need to use : https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsacryptoserviceprovider.signdata?view=net-5.0 and https://social.msdn.microsoft.com/Forums/vstudio/en-US/c39f2298-ffe3-48d9-ad1e-ababa122d229/sha1-with-rsa-in-c?forum=netfxbcl – jdweng Jan 03 '21 at 11:13
  • I found a similar question on SO, you can refer to this link: https://stackoverflow.com/questions/13453921/how-to-make-wcf-client-sign-securitytokenreferencereference – Ding Peng Jan 05 '21 at 05:01

0 Answers0