Custom Certificate chain Validation in .net 6 webapi to connect to another Api - ssl

In our asp.net core web api-1 we are accessing another api-2
by bypassing ssl validation by using the code below
ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
ClientCertificateOptions = ClientCertificateOption.Manual,
ServerCertificateCustomValidationCallback =
(httpRequestMessage, cert, cetChain, policyErrors) =>
{
return true;
}
For security reason are asked to get rid off the above bypass.
But we need to connect to api-2 by cert chain validation , we cant install certificate because of some organization reason and because we are on Azureappservice.
we need to keep the certificates in key vault,
While making a request to api-2 we need to get the cert form key vault and do cert chain validation
I read some articles ,But I am not sure about the below
1, what certificate we need to ask from the api-2 team to keep it in our keyvault
2, what is this cert chain validation doing are we are extracting the certificate of api-2 from http response and comparing it with the certificate in our keyvault , if its all the same , then we allow to connect .
can anybody explain this in simple terms or give any pointers
thanks

Related

How do Azure Function Apps handle Client Certificate Auth?

Hopefully I can make this clear enough.
Goal:
Client Certificate-Authenticated Azure Function
Scenario:
Azure Function App with:
HTTPS Only: set to Yes
Client certificate mode: set to Require
HTTP-triggered Azure Function (Python) which:
Loads client certificate from X-ARR-ClientCert header
Pulls a pre-shared client cert from a database and compares:
Issuer
CommonName
Not Valid Before/After
Hits the listed OCSP endpoint to see if cert is revoked
If properties from each cert match and the certificate has not been revoked, the Function will generate a SAS token for the requestor and send it in the response.
Question:
How is the cryptographic part of client cert auth handled in this scenario?
According to this (great) blog post, there is a CertificateVerify step where...
"The client is authenticated by using its private key to sign a
hash of all the messages up to this point. The recipient verifies
the signature using the public key of the signer, thus ensuring it
was signed with the client’s private key."
I don't see a way to access ...all the messages up to this point. to validate this has occured using the Function (Python) code.
Is this something Microsoft handles automagically (similar to how they forward client certs via the X-ARR-ClientCert header)? Or is this not possible?
From what I implemented in a similar case:
Your app received the certificate via the header and must:
load the certificate (using the library cryptography in python for example)
verify the signature of the certificate with you certificate authority
verify the date of validity
verify that it has not been revoked
Using web app (but the same would apply to functions), the Azure frontend seems to just launch authentication protocol to verify that the client that send the certificate has the private key associated (and launch the mutual auth protocol as described in the blog post). But it does not verify the validity or signature of the certificate.
The CertificateVerify step you're mentionning seems to be handled by the Azure Frontend, I don't think your need to worry about this process.
Hopes this helps !

Could not establish trust relationship for the SSL/TLS secure channel - SOAP Service

I have a SOAP web service inside that I am calling a third party secured web service (it was HTTP earlier now they secured it). they have valid SSL certificate. while calling the third party service I am getting below error,
Server was unable to process request. ---> The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel
at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest request)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
There are few articles which says TSL 1.2 should be enabled, to check about TSL further, I have below component on my server,
Here is third party service call,
Can someone guide me, where should I setup trust relationship ?
Do I need to modify my code or just some configurations changes needed
In addition to Abraham post above, make sure that that IIS has enough access to the certificates. We faced the issue and at first, it seemed like the app could access the certificates but that wasn't the case. We fixed it by going to the Manage Certificates -> Personal -> Certificates -> Right Click the certificate -> All tasks -> Manage Private Keys -> Add -> Grant Access to "Everyone" (testing only, you should only grant access to IIS). The connection worked after doing this.
There is a process of exchanging the public key of the service certificate during the secure communication. Therefore, we should establish the trust relationship between the client-side and the server-side. As for mutual certification authentication, we should establish the trust relationship each other.
Trust relationship represents the certificate is valid, the server is real and secure. Namely, it represents this is a validation of the server’s identity. This also could be accomplished by the below code segments.
//adding below code segments to ignore the service certificate validation.
ServicePointManager.ServerCertificateValidationCallback += delegate
{
return true;
};
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Ssl3;
More commonly, this should be finished by installing the service Root certificate in the local Trusted Certification Authorities.
To get the certificate you can either,
1, Ask the service vendor for it, you can ask for the Root CA
certificate, you can authorize all the servers you need at once;
Use a web browser to get the certificate. Access the service creation page with HTTPS(https://localhost:xxxx/xxx.svc). Then use the web browser options to export the certificate to a .cer file.
Install the certificate.
Double-click the .cer file to install the certificate. Choose Local Computer, then choose Trusted Root Certification Authorities.
Here is a detailed step.
https://success.outsystems.com/Support/Enterprise_Customers/Installation/Install_a_trusted_root_CA__or_self-signed_certificate
Feel free to let me know if there is anything I can help with.

what is ServicePointManager.ServerCertificateValidationCallback

I'm using a web service in my website. the provider provide me a sample code, in the code there is a line like this:
// For Ignore SSL Error
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
The web service link users to its own page and then return to my website. it uses https.
What is this code application? Is it for ignoring certification error in users' web explorers?
I think the certification with the web server's site may not be valid certification, or it's just a local certification. Am I right?
Thanks.
It is common mistake to think that it is used only to bypass invalid SSL certificates. However, many uses this callback only to ignore invalid certificates.
This callback provides an ability to attach additional checks for SSL certificate and access information cannot be accessed otherwise. For example, only within this callback you can get specific certificate errors which cannot be handled externally: name match/mismatch (they are stored in the sslPolicyErrors parameter. In addition, chain parameter contains exact certificate chain sent by the server. This object can be used to construct the certificate chain when system store has missing intermediate CA certificates.
For example, when you purchase certificates from Thawte (or other providers), their certificate do not have 'Certification Authority Issuer' access method and intermediate certificates are not installed in the system store. In this case, you will be unable to build the chain for that certificate. However, web browsers will not complain because missing (on client) certificates are shipped along the SSL handshake from web server. And this callback is the only way to access them.
And I wouldn't bypass SSL errors in a production environments, because it will open security holes.

Client Certificate for WCF NetTCP Transport binding

We have selfhosted WCF services running using NetTCP:Transport:WindowsClientCredentialType
// Set Binding Security.
netTcpBinding.Security.Mode = SecurityMode.Transport;
netTcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
netTcpBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
We now need to expose these services to domains outside our own but want to continue selfhosting and not use IIS. Thus I am trying to switch us to a ClientCredentialType of Certificate.
// Set Binding Security.
netTcpBinding.Security.Mode = SecurityMode.Transport;
netTcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate;
netTcpBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
I have previously setup a development environment (long ago) where we used MakeCert to generate a "dummy" certificate for development purposes with WCF. But we have since purchased a certificate from Verisign. I am a bit fuzzy on what needs to happen now. I can see us using this certificate to validate our Services to the Client, but how do we validate our .NET client with certificate? Do we use the same certificate? Do we have to install this certificate during install of our client? Bit wrapped around the axle here and could use a could explaining if anyone can help out.
No you will not install your certificate with private key on your clients. You mustn't give your private key to anybody - once it is compromised your security has gone! The problem is that client certificate must have a private key as well but that private key must be owned only by that single client. That means another certificate per client.
How is it usually implemented? By local certificate authority issuing certificates to your clients. Your service will trust that authority and so all clients holding certificates issued by your authority. That is the only scenario to get this under control otherwise you need to find another mechanism to authenticate your clients.

WCF client certificate signing : how to?

So I have a WCF service where I have various parts of the service contract with a ProtectionLevel of Sign. I set the credentials on the client side by choosing a certificate from the certificate store. This is fine.
However ....
Does this client certificate need to be in the 3rd party certificate store on the server for this to work?
If this is the case how can I configure a service which accepts any client certificate?
And finally how do I access the signing certificate within the implementation of the operations which require signed messages? Just the signing certificate subject name would do fine!
Thanks
Ah the joy of self answering.
So
"It depends". If you have
ChainOrPeer validation then both the
chain or the presence of the
certificate in the trusted people
store results in success. Only
validating on chain obviously checks
the chain, setting Peer validation
uses the store, and None lets
everything through.
Set the validation mode to none
The SecurityContext for the request
contains an X509CertificateClaimSet
which in turn exposes the
certificate itself.