Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

certificate mechanism between webservice provider and consumer

What are the exact steps done by server and client to place a ssl certificate mechanism in a webservice call? Who(client/server/both) will generate .keystore,.p7b/.cer files? I have googled a lot but couldn't find the answer to it. In my case, i am the client running a java application which consumes a soap webservice call. I have a .p7b file given by WebService provider. I know where to place the files(.keystore, .cer) and how to use it in the application.

But, my questions are

  1. Do i need to generate keystore file or should i get it from webservice provider? If i need to generate, how? Do i need private key/passphrase?
  2. I need a .cer file, so how can i use keytool to convert .p7b to .cer file?

Thank you for your help in advance.

like image 639
Sumanth Avatar asked Jan 06 '23 21:01

Sumanth


1 Answers

It looks like you're calling a web service where the HTTP connection is protected by TLS/SSL using X509 certificates. That means the server has set up a keystore with those certificates as well as the corresponding private keys. When you call the web service, the server will retrieve from its keystore the certificate used for the trust establishment (that is, to protect the TLS connection to the web service) and sends it to the client. When the client receives the response from the server it will check the trust of that certificate. Now we have two scenarios:

  1. If the server uses a self-signed certificate (can be used for developments and testing, but not in production), then the client won't recognize it as trusted because it's not stored in the client's truststore. By default, in a Java environment, the truststore is searched (by order) in the following two locations: $JAVA_HOME/lib/security/jssecacerts and $JAVA_HOME/lib/security/cacerts. A custom truststore can also be used by running the client with -Djavax.net.ssl.trustStore and -Djavax.net.ssl.trustStorePassword or by using a custom TrustManager. As such, if the server self-signed certificate is not stored in one of these locations, the secure connection will fail. So the client will have to import the certificate into its truststore. To circumvent the import of self-signed certificates into the client's truststore, you can create a custom X509TrustManager as stated here.

  2. If the server uses a certificate signed by one of the recognized root CA authorities, then it'll be validated automagically because those CA's certificates are already installed in Java's default truststore. As such, the trusted TLS connection will be successful.

In the case where the server does not require client authentication the process is over (this is what happens when you connect to most HTTPS websites via browser).

If the server requires client authentication, then the client will need to provide its own certificate from its keystore to the server, and the server will need to have it installed in its truststore. The web service provider must provide to the client the specification for the certificate profile that the client should use.

Here you can find a good clarification to the keystore vs truststore terminology.

By default in Java environments, keystores and truststores are JKS files.

So you're saying you have a .p7b file provided by the web service provider. Quoting from this page:

PKCS#7/P7B Format

The PKCS#7 or P7B format is usually stored in Base64 ASCII format and has a file extention of .p7b or .p7c. P7B certificates contain "-----BEGIN PKCS7-----" and "-----END PKCS7-----" statements. A P7B file only contains certificates and chain certificates, not the private key. Several platforms support P7B files including Microsoft Windows and Java Tomcat.

So that P7B file contains the server certificate or certificate chain (more on this here).

I believe you're in a no-client-auth scenario. Therefore, you won't need your own keystore. You'll only need to import the server's certificate (P7B file) into the truststore you're using. You can directly import a P7B file without converting it to CER format:

keytool -import -trustcacerts -alias web_service -keystore my_truststore.jks -file web_service.p7b

In the case you still want a CER formatted certificate, you can convert from P7B to CER like this (to answer to your 2nd question):

openssl pkcs7 -print_certs -in certificate.p7b -out certificate.cer

If in fact client authentication is needed, then you'll need to create your keystore with your private key and public certificate and provide it to the connection by either the -Djavax.net.ssl.keyStore and -Djavax.net.ssl.keyStorePassword parameters or through a KeyManager. The same workflow previously explained applies now in the opposite direction.

like image 76
Baderous Avatar answered Jan 30 '23 20:01

Baderous