wcf icon indicating copy to clipboard operation
wcf copied to clipboard

CreateCertificateOverTransportBindingElement

Open jason-russell opened this issue 5 years ago • 19 comments

Describe the bug CreateCertificateOverTransportBindingElement is not available for SecurityBindingElement in CustomBinding

.NET Framework code is using this to create a Custom Binding, and I'm trying to port to .NET Core 3.1 I'm trying to connect to a third-party service that requires a certificate binding

To Reproduce

This is the .NET Framework code. I'm trying to recreate the custom binding

private CustomBinding CreateCustomBinding()
{
	var dvsBinding = new CustomBinding();

	var securityElement = SecurityBindingElement.CreateCertificateOverTransportBindingElement();
	securityElement.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10;
	securityElement.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
	securityElement.SetKeyDerivation(true);                     // require derived keys
	securityElement.LocalClientSettings.DetectReplays = false;
	securityElement.LocalServiceSettings.DetectReplays = false;
	securityElement.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Default;

	var textElement = new TextMessageEncodingBindingElement(MessageVersion.Soap12, Encoding.UTF8); // TODO: Verify Message Version
	var httpsElement = new HttpsTransportBindingElement();

	dvsBinding.Elements.AddRange(securityElement, textElement, httpsElement); // Transport Binding Element must occur last (in order)

	return dvsBinding;
}

This custom binding was derived from the following app.config

    <binding name="Https2">
      <security defaultAlgorithmSuite="Basic256Sha256Rsa15" authenticationMode="CertificateOverTransport"
            requireDerivedKeys="true" securityHeaderLayout="Lax" includeTimestamp="true"
            messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10">
        <localClientSettings detectReplays="false" />
        <localServiceSettings detectReplays="false" />
      </security>
      <textMessageEncoding />
      <httpsTransport />
    </binding>

Expected behavior To be able to create a Certificate Over Transport binding

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Add any other context about the problem here.

jason-russell avatar Apr 15 '21 03:04 jason-russell

Thanks for reporting this, @jason-russell !

@mconnew can you please weigh in here?

@imcarolwang could you please looking into adding the API here?

HongGit avatar Apr 29 '21 20:04 HongGit

I'm a little confused as your binding config doesn't match your code, and I don't believe the two different bindings would be compatible, so I'm not sure which you are needing. Depending on which one you are wanting, you may or may not have a problem with the SecurityAlgorithmSuite. Let's start with the code version of the binding.

// WSHttpBinding uses MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10
// which when coupled with Certificate credentials uses the thumbprint to reference the certificate in the sent token.
var binding = new WSHttpBinding(SecurityMode.TransportWithMessageCredential);
binding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
binding.Security.Message.EstablishSecurityContext = false;
var customBinding = new CustomBinding(binding);
var sbe = customBinding.Elements.Find<TransportSecurityBindingElement>();
sbe.SecurityHeaderLayout = SecurityHeaderLayout.Lax; // Default for WSHttpBinding is strict
// Not needed as X509SecurityTokenParameters.HasAsymmetricKey == true which never uses derived keys making it a noop
// sbe.SetKeyDerivation(true);
// Not needed as default is false
// sbe.LocalClientSettings.DetectReplays = false;
// Not needed as Default is Basic256 which is the default value
// sbe.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Default;
// If matching your original config, you can use the parameterless constructor to have it match
var textElement = new TextMessageEncodingBindingElement(MessageVersion.Soap12, Encoding.UTF8); 
var httpsElement = new HttpsTransportBindingElement();
var dvsBinding = new CustomBinding(sbe, textElement, httpsElement);

To better match your config, we need to start with a different base binding. The config specifies Basic256Sha256Rsa15 which we haven't added yet (nobody has asked for it). You don't actually need that, you can just use Basic256Sha256, the Rsa15 suffix is to specify what's used to encrypt when using full Message security. But that's not available, only Basic256. We can add Basic256Sha256 in a future preview build if you do need it.

var binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;
var customBinding = new CustomBinding(binding);
var sbe = customBinding.Elements.Find<TransportSecurityBindingElement>();
// Not currently available
// sbe.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic256Sha256;
// Parameterless constructor uses Soap12WSAddressing10 and UTF8
var textElement = new TextMessageEncodingBindingElement(); 
var httpsElement = new HttpsTransportBindingElement();
var dvsBinding = new CustomBinding(sbe, textElement, httpsElement);

The SecurityAlgorithmSuite.Basic256 uses SHA1 for the signature hash so the two aren't compatible. I tried creating a type derived from the abstract class SecurityAlgorithmSuite with the correct values populated but it didn't work as there's 2 missing abstract methods not exposed on the contract so you end up with a TypeLoadException.

@imcarolwang, can you the following two instance methods and static method (with an internal type backing it which is currently not in the code base) to the contract for SecurityAlgorithmSuite:

        public abstract bool IsSymmetricKeyLengthSupported(int length);
        public abstract bool IsAsymmetricKeyLengthSupported(int length);
        public static SecurityAlgorithmSuite Basic256Sha256 { get; }

@jason-russell, let me know how you get on with this. I suspect you're blocked by the missing security algorithm, but the rest should produce the correct output for you.

mconnew avatar Apr 30 '21 00:04 mconnew

Any Idea when this can be implemented? We're currently porting our .NET Framework application to .NET Standard and for an integration with a third party service we need the Basic256 Security Algortithm. We tried implementing it ourselves by overriding from the SecuritiyAlgorithmSuite class, but - as mconnew already pointed out - there are two missing instance methods.

Lootvoet avatar May 03 '21 11:05 Lootvoet

@Lootvoet, Basic256 is available, it's the default. It's Basic256Sha256 which is missing. If that's what you need, we'll release this in the next possible preview build (we have one in progress so it won't be our next preview release, but the one after that).

mconnew avatar May 03 '21 18:05 mconnew

The only DefaultAlgorithmSuite I can pick is Triple Des (using .NET Standard 2.0, with System.ServiceModel.Security version 4.8). Am I missing something?

Lootvoet avatar May 03 '21 18:05 Lootvoet

It looks like we didn't add two more properties to the contract for SecurityAlgorithmSuite. There should be a .Default and .Basic256. But the good news is if you don't set it to anything, the default value is Basic256.

@imcarolwang, can you add the Default and Basic256 static properties to SecurityAlgorithmSuite in the contract too. They already exist in the implementation.

mconnew avatar May 03 '21 19:05 mconnew

Alright perfect! Thanks for the clarification.

Lootvoet avatar May 03 '21 19:05 Lootvoet

For the Colombian electronic invoice is necessary use SecurityAlgorithmSuite.Basic256Sha256Rsa15 to validate message security.

¿There is any possibility to include this algorithm in future versions? or ¿There is any way to re-write this .Net Framework code with the current version on .NET 5?

var binding = new WSHttpBinding(SecurityMode.TransportWithMessageCredential); binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; binding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate; binding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Basic256Sha256Rsa15; binding.Security.Message.EstablishSecurityContext = false; var endpoint = new EndpointAddress("https://vpfe-hab.dian.gov.co/WcfDianCustomerServices.svc"); var seriviceClient = new WcfDianCustomerServicesClient(binding, endpoint); seriviceClient.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(@"file path", "password");

weimaryn avatar May 06 '21 13:05 weimaryn

@weimaryn, it will be added to an upcoming preview build. WCF doesn't depend on the latest .NET build and for the foreseeable future will continue to work all the way back to .NET Core 2.1 so there's no worry about staying on .NET 5.

mconnew avatar May 07 '21 20:05 mconnew

@imcarolwang, can you also add the two overloads of SecurityBindingElement.CreateCertificateOverTransportBindingElement to the contract, the implementation is already there.

mconnew avatar May 07 '21 20:05 mconnew

@imcarolwang, can you also add the two overloads of SecurityBindingElement.CreateCertificateOverTransportBindingElement to the contract, the implementation is already there.

Added in PR #4617 .

imcarolwang avatar May 08 '21 01:05 imcarolwang

@mconnew Hello, do you know if SecurityAlgorithmSuite.Basic256Sha256Rsa15 will be available for the 6 preview?

waguirre avatar Jun 09 '21 21:06 waguirre

@waguirre, the suite SecurityAlgorithmSuite.Basic256Sha256Rsa15 is functionally identical to SecurityAlgorithmSuite.Basic256Sha256 when using TransportWithMessageCredential. The tail Rsa15 only changes things when using SecurityMode.Message where the message is encrypted. So if you are using TransportWithMessageCredential, then you are covered. If you are using Message security, then that feature isn't supported yet so it's a non-issue.

mconnew avatar Jun 10 '21 19:06 mconnew

I am getting this error when calling SOAP services from .net core client. Any help? when i am calling i get this error. An unsecured or incorrectly secured fault was received from the other party. See the inner fault exception for the fault code and detail. Dynamic back end host not specified.

sekar79 avatar Jul 08 '21 03:07 sekar79

@sekar79 can you please log a separate issue for the issue above?

Closing this issue as this is already implemented.

HongGit avatar Oct 13 '21 23:10 HongGit

I see this Issue is closed, but SecurityAlgorithmSuite.Basic256Sha256Rsa15 is not included yet. Anybody knows a workaround?

BillVelasquez avatar Jul 20 '22 18:07 BillVelasquez

@BillVelasquez, did you read my comment here: https://github.com/dotnet/wcf/issues/4589#issuecomment-858939698

mconnew avatar Jul 21 '22 21:07 mconnew

@mconnew yes I red the comment, but Basic256Sha256 is not implemented in Net6 neither.

This is the code for binding to the service that works in Net Framework 4.5 :

        private static System.ServiceModel.WSHttpBinding CreateBinding()
        {
            var binding = new System.ServiceModel.WSHttpBinding();
            binding.Security.Mode = System.ServiceModel.SecurityMode.TransportWithMessageCredential;
            binding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.None;
            binding.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Certificate;
            binding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Basic256Sha256Rsa15;
            binding.Security.Message.EstablishSecurityContext = false;
            return binding;
        }

I think is the same problem that @weimaryn and @waguirre are facing for colombian e-invoicing platform, that is built with .Net Framework also.

BillVelasquez avatar Jul 22 '22 17:07 BillVelasquez

Hi @BillVelasquez, I have tested my code again in .NET 6 and it worked fine for SecurityMode.TransportWithMessageCredential with SecurityAlgorithmSuite.Basic256Sha256. It´s important update all NUGET packages to latest version, in my case I´m using the version 4.9.0 for System.ServiceModel.Security, System.ServiceModel.NetTcp, System.ServiceModel.Http, System.ServiceModel.Federation and System.ServiceModel.Duplex.

This is my code (only change SecurityAlgorithmSuite.Basic256Sha256 compared with .NET Framework).

using ServiceReference1;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Security;

var binding = new WSHttpBinding(SecurityMode.TransportWithMessageCredential);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
binding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Basic256Sha256;
binding.Security.Message.EstablishSecurityContext = false;
var endpoint = new EndpointAddress("https://vpfe-hab.dian.gov.co/WcfDianCustomerServices.svc");
var seriviceClient = new WcfDianCustomerServicesClient(binding, endpoint);
seriviceClient.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(@"Path .pfx file", "password");

I hope this helps.

weimaryn avatar Jul 25 '22 19:07 weimaryn