Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get client certificate in a Java HttpsServer implementation for a webservice?

I wrote a Web Service server using Sun Ws implementation and I used a HttpsServer for publication (TLS mutual authentication).

        httpServer=HttpsServer.create(...);
        ssl=SSLContext.getInstance("TLS");
        ...
        ssl.init(keyFactory.getKeyManagers(),trustFactory.getTrustManagers(),new SecureRandom());
        configurator=new HttpsConfigurator(ssl) {
           public void configure (HttpsParameters params) 
           {
               SSLContext context; 
               SSLParameters sslparams;

               context=getSSLContext();
               sslparams=context.getDefaultSSLParameters();
               sslparams.setNeedClientAuth(true);
               params.setSSLParameters(sslparams);
           }
       }; 
       ((HttpsServer)httpServer).setHttpsConfigurator(configurator);
       ...
       endPoint=getSunWsProvider().createEndPoint(...);
       httpContext=httpServer.createContext(...);
       endPoint.publish(httpContext);
       httpServer.start();
       ...

Everything works fine. When the implementation of the server side of the Web Service is executed by a client, I would like to know which client is executing the code (to manage rights). Knowing that each client gets its own certificate, how can I get the client certificate used for the TLS negociation before the Web Service call ? (I would prefer to find a solution based on the client certificate analysis instead of adding an identification information to each Web Service call).

Thank you for your help.

like image 607
user1640562 Avatar asked Sep 01 '12 13:09

user1640562


People also ask

How do I get client authentication certificate?

Create a client certificate request. After receiving the certificate, export it to a password-protected PKCS12 file and send the password and the file to the user. Make sure the file is securely sent.

How do I use client certificates in a client Java application?

Client Java Implementation First, we create an SSLSocket that establishes a connection with the server. In the background, the socket will set up the TLS connection establishment handshake. As part of this handshake, the client will verify the server's certificate and check that it's in the client truststore.

How do I pass a client certificate to web API?

Using Client Certificates in Web API On the server side, you can get the client certificate by calling GetClientCertificate on the request message. The method returns null if there is no client certificate. Otherwise, it returns an X509Certificate2 instance.


1 Answers

in your handler you get not the HttpExchange instance but the instance of its subclass HttpsExchange that has the extra method:

abstract SSLSession getSSLSession();

Among many other things an SSLSession exposes peer identity

like image 133
Serge Avatar answered Oct 24 '22 04:10

Serge