jobin
jobin

Reputation: 2798

How to inject custom SSLSocketFactory into Azure Java SDK

Is there a way I can override the SSLSocketFactory used by default by Azure HTTPConnection and use my own custom SSLSocketFactory? I need this because the system I use communicates with different servers using different SSLContexts and I can't use the default SSLSocketFactory.

I have raised an issue on the AD library for java on github regarding the same, but if there is a way I can override it, that would be better before a code fix is done.

Upvotes: 0

Views: 573

Answers (2)

Prasad Kerhalkar
Prasad Kerhalkar

Reputation: 36

I think Will's answer is correct. The SSLSocketFactory is generic class and is not specific to Azure. You can use the above method in your own code. Based on your servers you can create different SSLSocketFactory objects. I don't see any reason for Azure SDK for Java to expose the SSLSocketFactory it is using. And if it does, then it is specific to Azure http communication and I dont think you would be able to use it for other servers. Can you please clarify your question more? Also, I would recommend you to add "adal" to your tags so that Adal team can take a look at it. https://stackoverflow.com/questions/tagged/adal

Upvotes: 0

Will Shao - MSFT
Will Shao - MSFT

Reputation: 1207

As I know, Azure SDK for Java used the SSLContext and SSLSocketFactory which is in package javax.net.ssl

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;

I think you can use sslContext.getInstance(*yourparamters*) for different SSLContext if I am understanding rightly. Base on my experience, we can use this method to returns a SSLContext object that implements the specified secure socket protocol. For example, if you use SSLV3, you can code as following:

SSLContext context=SSLContext.getInstance("SSLv3");

Actually, you also use the same method to set the TLS and SSL. I suggest you can refer to samples about SSLSocketFactory


【UPDATE】

This is how I would add an SSL context for a generic SSLSocketFactory. I want to know to do this for the Azure java SDK.

If I am understanding, it seems that you want to get the different SocketFactroy for different servers. On this scenario, I suggest you can create a socketfactory management class to manage all of your socketfactory. Please see this simple code:

public class SSLScoketFactoryManage {

    public static SSLSocketFactory getSSLSocketFactory(String keyStoreName, String password) throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, IOException {
        KeyStore ks= getKeyStore(keyStoreName, password);
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
        keyManagerFactory.init(ks, password.toCharArray());

          SSLContext context = SSLContext.getInstance("TLS");
          context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());

          return context.getSocketFactory();
    }


    }

you can use this method in different sockets as following :

SSLSocketFactory sslFactory = getSSLSocketFactory(keyStore, keyStorePassword);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setSSLSocketFactory(sslFactory);

Any concerns, please let me known.

Upvotes: 1

Related Questions