0

I have a simple JAX-WS standalone server which is using TLS:

SSLContext ssl = SSLContext.getInstance("TLS");

KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 
KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
store.load(new FileInputStream(keystoreFile), keyPass.toCharArray()); 

kmf.init(store, keyPass.toCharArray());
KeyManager[] keyManagers = new KeyManager[1];
keyManagers = kmf.getKeyManagers();

TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(store);
TrustManager[] trustManagers = tmf.getTrustManagers();
ssl.init(keyManagers, trustManagers, new SecureRandom());

HttpsConfigurator configurator = new HttpsConfigurator(ssl);
HttpsServer httpsServer = HttpsServer.create(new InetSocketAddress("localhost", 8443), 8443);
httpsServer.setHttpsConfigurator(configurator);   

HttpContext context = httpsServer.createContext("/test");
httpsServer.start();

endpoint.publish(context);

I would like to use client certificates to create mutual authentication before using web services. Also I would like to see what certificate was used by the client to read DN and other certificate attributes.

How can I do it?

pedrofb
  • 37,271
  • 5
  • 94
  • 142
user1563721
  • 1,373
  • 3
  • 28
  • 46

1 Answers1

0

Finally I got it working.

Mutual authentication can be configured through setNeedClientAuth in SSLParameters like the following:

HttpsConfigurator configurator=new HttpsConfigurator(ssl) {
    public void configure (HttpsParameters params) 
            {
                SSLContext context; 
                SSLParameters sslparams;

                context=getSSLContext();
                sslparams=context.getDefaultSSLParameters();
                sslparams.setNeedClientAuth(true);
                params.setSSLParameters(sslparams);
            }
        };

And the client certificate can be checked and parsed according needs from SSLSession. This class can be loaded as resource in web service class:

@Resource
private WebServiceContext context;

HttpsExchange exchange = (HttpsExchange) context.getMessageContext().get(JAXWSProperties.HTTP_EXCHANGE);
SSLSession sslsession = exchange.getSSLSession();

sslsession.getPeerCertificates();
user1563721
  • 1,373
  • 3
  • 28
  • 46