AL icon indicating copy to clipboard operation
AL copied to clipboard

Calling a Webservice with a Certificate

Open ftornero9 opened this issue 8 years ago • 16 comments

We are doing tests calling a Webservice that need a certificate in the call but looks like the certificate is not detetected.

We already open a issue with the sentence:

 HttpClient.AddCertificate(Certificate, Password);

That rise an error, in the June preview is not the case but either way adding the cdertificate or not the remote server give the response that the certificate is not detected.

Regards

ftornero9 avatar Jun 29 '18 11:06 ftornero9

Hello,

Could anyone take a look a this issue and say something ?

Regards.

ftornero9 avatar Sep 24 '18 20:09 ftornero9

@StanislawStempin can you look at this?

DmitryKatson avatar Sep 30 '18 10:09 DmitryKatson

Hi @ftornero9, @dkatson , Apologies for the late reply.

The HttpClient.AddCertificate is simply a wrapper for performing the following .NET code (simplified):

byte[] certificateBytes = Convert.FromBase64String(certificateContent);
X509Certificate certificate = password == null ? new X509Certificate(certificateBytes) : new X509Certificate(certificateBytes, password);
                
WebRequestHandler requestHandler = new WebRequestHandler();
requestHandler.ClientCertificates.Add(certificate);

// HttpClient client = new HttpClient(requestHandler)
// etc 

Can you make the web service call work by calling it from C# code? If not, then there is most likely a problem with the general setup of server/client.

If you can call it from C#, but not from AL, then just make sure that your are providing the base64 string of correct certificates, check that that you can perform the call from the Windows user under which the server is running, and look for other differences in the setup.

Here is a quick test I did using the latest code bits from bcsandbox-master. The server is just a console OWIN app, set up to require client certificates: image

defr0std avatar Oct 02 '18 11:10 defr0std

Hello @dennisfrostlander,

The web service call works in C/AL using this var

System.Net.HttpWebRequest.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

And this code:

Request.ClientCertificates.Add(Cert);

Where Cert is

System.Security.Cryptography.X509Certificates.X509Certificate2.'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

In AL we are using the right certificate, if in the image below we use the line in red, we get the error that password is wrong, with the right password, in the previous line, the line is executed without any message.

image

Now in AL I'm using a workaraound to make some progress with the development.

I'm calling an Azure Function passing the URL, the certificate in base64, the XML in base64 and the certificate password.

Then the Azure function (in Powershell) do the real conexion with the Web Service and return the response XML, and it works.

In my opinion the wrapper that you say is not working.

I could pass a little example to do some tests, the problem is that you will need an Spanish certificate, maybe you could contact the people in Microsoft who is doing the SII for the Spanish version an ask for one certificate, they sure need it to do his tests.

Regards.

ftornero9 avatar Oct 02 '18 12:10 ftornero9

Hmm, I played around with the password parameter in the call to AddCertificate, and I still cannot reproduce the issue.

  • Exported the certificate with the key to pfx file, protected by a password
  • Call Client.AddCertificate('MyBase64Certificate', 'MyPassword') in AL
  • Call remote server, certificate is sent
  • Changed the password to a wrong one, got an error:
The specified network password is not correct.

Tried both on a local server and in the cloud sandbox (13.0 24630), same behavior...

What is the server version you are using? Are you using a docker build, local server, or cloud sandbox? Are you able to reproduce this issue using a docker container with some sort of a local server which requires client certificate? Showing that C/AL HttpWebRequest works and AL HttpClient does not.

Looking again at the client implementation (bcsandbox-master), it does not seem like the problem is in the AddCertificate wrapper. This is pretty straightforward. There could be theoretically some subtle difference between .NET's HttpWebReqeust and HttpClient in regards to client certificates, but without a repro it's hard to say something.

defr0std avatar Oct 02 '18 14:10 defr0std

Hello @dennisfrostlander ,

I try since June 29 everything, docker (current and insider), local server (RC1 and RC3), cloud sandbox, you name it.

I don't have any local server that require a certificate, but anyway I need to solve the problem with the web service I'm doing the tests.

I don't know if we could connect and I could show you the issue.

Rebgards

ftornero9 avatar Oct 02 '18 14:10 ftornero9

Perhaps, one thing you could try is to run the c# code with an HttpClient I pasted above, and see if that works.

// string certificateContent = "your-base64-cert-string";
// string password = "your password";

byte[] certificateBytes = Convert.FromBase64String(certificateContent);
X509Certificate certificate = password == null ? new X509Certificate(certificateBytes) : new X509Certificate(certificateBytes, password);
                
WebRequestHandler requestHandler = new WebRequestHandler();
requestHandler.ClientCertificates.Add(certificate);

HttpClient client = new HttpClient(requestHandler)
await client.GetAsync("your-url");
// check that request succeeded

You could run this code under the same windows credentials as your BC server runs, including the proper elevation level (e.g. Run as administrator or not). If this code works, then there is definitely something in the AL wrapping logic. Otherwise, you could continue experimenting to understand why the code with HttpWebRequest works and the code with HttpClient doesn't.

defr0std avatar Oct 03 '18 07:10 defr0std

And one more thing I just noticed. In your working version in C/AL, you are using the X509Certificate2 class, and BC server uses X509Certificate. That could be the issue as well. With the small C# test I pasted above, you could quickly validate this as well.

defr0std avatar Oct 03 '18 07:10 defr0std

I think I have found the difference between X509Certificate and X509Certificate2 in my local setup.

When I use new X509Certificate(certificateBytes), the client certificate gets sent only if the certificate is installed in the certificate store (current user -> personal). When I use new X509Certificate2(certificateBytes), the client certificate is always sent.

I will investigate why we did not use X509Certificate2 in the first place. In the meantime, if you could confirm that this solves your issue as well, it would be very helpful.

defr0std avatar Oct 03 '18 10:10 defr0std

Hello,

Sorry, I installed the certificate (current user -> personal) but that don't change the result.

Regards.

ftornero9 avatar Oct 03 '18 11:10 ftornero9

Is the server (which runs AL code) running under the same user as the one you used to install the certificate? Would you then be able to test the C# snippet code I provided above?

defr0std avatar Oct 03 '18 12:10 defr0std

I have a meeting now, but later I will do more test and let you know the results.

Regards.

ftornero9 avatar Oct 03 '18 13:10 ftornero9

Hello,

I changed the user account that start the service with the account where I installed the certificate and finally it works (AL code).

But I think this is only a probe that the wrapper doesn't do what it suppose to do, because that is's possible in a on-prem installation but in the cloud that it's not possible.

Regards.

ftornero9 avatar Oct 03 '18 16:10 ftornero9

Great! I think that proves that the problem is that AL wrapper uses X509Certificate instead of X509Certificate2. In my local C# tests I can use X509Certificate2 without having to install the certificate into the store. Let us make this change and then you can test again.

defr0std avatar Oct 04 '18 07:10 defr0std

Hello @dennisfrostlander do you think that this issue is going to be solved anytime soon ?

Regards

ftornero9 avatar Oct 23 '18 10:10 ftornero9

This has just been fixed, it should be available on the bcsandbox-master images within a few days.

StanislawStempin avatar Oct 23 '18 10:10 StanislawStempin