Is it possible to easily attach a client certificate to a Axis2 stub generated using wsdl2java? I need to change the client certificate dynamically on a per-request basis, so simply storing it in the keystore won't work for our case.
I've found examples where this is being done for non-SOAP calls, but could not find anything related to using the Axis client stubs. Trying to hack the XML for the SOAP call is an option I guess, albiet a painful one! Groan!
If you want to change which certificate is used depending on which connection is made, you'll need to configure an SSLContext
to do so, as described in this answer: https://stackoverflow.com/a/3713147/372643
As far as I know, Axis 2 uses Apache HttpClient 3.x, so you'll need to follow its way of configuring the SSLContext
(and X509KeyManager
if needed).
The easiest way might be to configure Apache HttpClient's global https
protocol handler with your SSLContext
, set up with an X509KeyManager
configured to choose the client certificate as you require (via chooseClientAlias
).
If the issuers and the connected Socket
(probably the remote address) are not enough for deciding which certificate to choose, you may need to implement a more complex logic which will almost inevitably require careful synchronization with the rest of your application.
EDIT:
Once you've built your SSLContext
and X509KeyManager
, you need to pass them to Apache HttpClient 3.x. For this, you can build your own SecureProtocolSocketFactory, which will build the socket from this SSLContext
(via an SSLSocketFactory
, see SSLContext
methods). There are examples in the Apache HttpClient 3.x SSL guide. Avoid EasySSLProtocolSocketFactory
, since it won't check any server cert (thereby allowing for MITM attacks). You could also try this implementation.
Note that you only really need to customize your X509KeyManager
, you can initialize your SSLContext
(via init
) with null
for the other parameters to keep the default values (in particular the default trust settings).
Then, "install" this SecureProtocolSocketFactory
globally for Apache HttpClient 3.x using something like this:
Protocol.registerProtocol("https", new Protocol("https",
(ProtocolSocketFactory)secureProtocolSocketFactory, 443));