I have rarely worked with certificate based authentication, so when I got this neat little SOAP service with certificate authentication in my hands I gladly accepted the challenge. All I got was the URL to the service with a certificate and the password. I also got an example written in PHP:
This of course looks very easy, but we all know that it's not really that easy if I want to use .NET and WCF. First of all the PEM-certificate cannot natively be used in .NET. Second of all I don't have the WSDL for the service. Here are the steps I had to go through to be able to successfully connect to the SOAP service.
Convert the certificate
The certificate I received was a PEM certificate which included both the private and public key. .NET does not natively support the PEM format, so I had to convert the certificate to a type that is usable in my application. For this purpose I used Win32OpenSSL and the command is really straight forward:
I was prompted for the current password and then prompted to enter a password for the new certificate. This results in a PFX certificate ready to use in .NET and that is compatible with the SOAP service.
Connecting to the service
By installing the PFX-certificate in the certificate store on my computer I am able to connect to the service URL. This also means that I can use the Add service reference Wizard in Visual Studio to generate the service client classes. However, I don't want to make the users install the certificate on their computers, but instead include the certificate in the application that will consume the service.
Another issue is that the SSL-certificate is not issued by a trusted source. This means that a secure connection cannot be established. An ugly but surely effective hack is to override the ServerCertificateValidationCallback and simply returning true for all certificates on validation. Note that this override is effective for the entire application!
When that little obstacle is overcome, it is actually a very straight path to connect to the service. First we will have to add the certificate to the solution and set it as Embedded Resource. The next step is to read the resource and add the certificate to the service binding.
When connecting all we need to do is create a BasicHttpBinding and setting the ClientCredentialType to Certificate. We also have to add the certificate to the ClientCertificate property on the service.
That is about everything that needs to be done. With the certificate added to the project and to the binding the authentication is handled with the certificate and the supplied password. The service works great on any computer without having to install the certificate or entering any credentials. Of course this is not the most secure solution out there, but it sure gets the job done!
$client = new SoapClient("https://url.to.service/", array( 'local_cert' => 'client_certificate.pem', 'passphrase' => 'PASSPHRASE', 'cache_wsdl' => WSDL_CACHE_NONE ));
This of course looks very easy, but we all know that it's not really that easy if I want to use .NET and WCF. First of all the PEM-certificate cannot natively be used in .NET. Second of all I don't have the WSDL for the service. Here are the steps I had to go through to be able to successfully connect to the SOAP service.
Convert the certificate
The certificate I received was a PEM certificate which included both the private and public key. .NET does not natively support the PEM format, so I had to convert the certificate to a type that is usable in my application. For this purpose I used Win32OpenSSL and the command is really straight forward:
openssl pkcs12 -export -in client_certificate.pem -out client_certificate.pfx
I was prompted for the current password and then prompted to enter a password for the new certificate. This results in a PFX certificate ready to use in .NET and that is compatible with the SOAP service.
Connecting to the service
By installing the PFX-certificate in the certificate store on my computer I am able to connect to the service URL. This also means that I can use the Add service reference Wizard in Visual Studio to generate the service client classes. However, I don't want to make the users install the certificate on their computers, but instead include the certificate in the application that will consume the service.
Another issue is that the SSL-certificate is not issued by a trusted source. This means that a secure connection cannot be established. An ugly but surely effective hack is to override the ServerCertificateValidationCallback and simply returning true for all certificates on validation. Note that this override is effective for the entire application!
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
When that little obstacle is overcome, it is actually a very straight path to connect to the service. First we will have to add the certificate to the solution and set it as Embedded Resource. The next step is to read the resource and add the certificate to the service binding.
var resource = typeof(MyService).Assembly.GetManifestResourceStream("client_certificate.pfx"); using(var stream = new MemoryStream()) { resource.CopyTo(stream); _certificate = new X509Certificate2(stream.ToArray(), "PASSWORD"); }
When connecting all we need to do is create a BasicHttpBinding and setting the ClientCredentialType to Certificate. We also have to add the certificate to the ClientCertificate property on the service.
public void Connect() { var binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport); binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate; var endpointAddress = new EndpointAddress("https://url.to.service/"); _service = new ServiceClient(binding, endpointAddress); _service.ClientCredentials.ClientCertificate.Certificate = _certificate; _service.Open(); }
That is about everything that needs to be done. With the certificate added to the project and to the binding the authentication is handled with the certificate and the supplied password. The service works great on any computer without having to install the certificate or entering any credentials. Of course this is not the most secure solution out there, but it sure gets the job done!
Comments
Post a Comment