Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF over HTTPS and Signing the body

Tags:

c#

soap

wcf

sign

I've got a SOAP service I want to connect to. It needs to be accessed trough https and it needs to have it's body signed by a certificate.

I've tried the following code:

<basicHttpBinding>
    <binding name="P4Binding" closeTimeout="00:01:00" openTimeout="00:01:00"
        receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
        bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
        maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
        messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
        useDefaultWebProxy="true">
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
        <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="None" proxyCredentialType="None"
            realm="" />
        <message clientCredentialType="Certificate" algorithmSuite="Default" />
        </security>
    </binding>
</basicHttpBinding>

Setup up my client as follows:

P4_ServiceReference.P4PortTypeClient client = new P4_ServiceReference.P4PortTypeClient();

client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
client.ClientCredentials.ServiceCertificate.DefaultCertificate = new X509Certificate2(@"[..].cer");
client.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(@"[..]", "somepass");

Even changed my Reference.cs to include the ProtectionLevel=ProtectionLevel.Sign on the ServiceContractAttribute and the OperationContractAttribute.

What happens is that the wse:security header is created, but the body is not being signed. The service returns Element http://schemas.xmlsoap.org/soap/envelope/Body must be signed.

What am I missing so that the body gets signed properly?

like image 888
WesleyE Avatar asked Aug 07 '12 15:08

WesleyE


1 Answers

Fixed it using this customBinding instead of the basicHttpBinding.

        //Setup custom binding with HTTPS + Body Signing + Soap1.1
        CustomBinding binding = new CustomBinding();

        //HTTPS Transport
        HttpsTransportBindingElement transport = new HttpsTransportBindingElement();

        //Body signing
        AsymmetricSecurityBindingElement asec = (AsymmetricSecurityBindingElement)SecurityBindingElement.CreateMutualCertificateBindingElement(MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10);
        asec.SetKeyDerivation(false);
        asec.AllowInsecureTransport = true;
        asec.IncludeTimestamp = true;

        //Setup for SOAP 11 and UTF8 Encoding
        TextMessageEncodingBindingElement textMessageEncoding = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8);

        //Bind in order (Security layer, message layer, transport layer)
        binding.Elements.Add(asec);
        binding.Elements.Add(textMessageEncoding);
        binding.Elements.Add(transport);

It seems that TransportWithMessageCredential only uses the Transport for security and ignores everything else.

like image 60
WesleyE Avatar answered Oct 11 '22 12:10

WesleyE