How I can tell alias of the wanted key-entry to SSLSocket before connecting?

前端 未结 1 721
无人共我
无人共我 2020-12-06 01:04

I have two certificate/key pairs in a java keystore. Aliases of those key-entries are \"foo\" and \"bar\".

My TLS client (java program) uses the keystore. TLS client

1条回答
  •  有刺的猬
    2020-12-06 01:23

    The default KeyManager will send the first certificate it finds that matches the conditions requested by the server, that is, it will send the first one it find for which it can build a certification chain up one of the CA names sent by the server during the request.

    If you always want a specific alias to be chosen, you'll need to implement your own X509KeyManager, possibly wrapping the default manager. Something along these lines should work (not tested this actual code, there may be a few typos):

    KeyStore keystore = ... // create and load your keystore.
    
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(keystore, password.toCharArray());
    
    final X509KeyManager origKm = (X509KeyManager)kmf.getKeyManagers()[0];
    
    X509KeyManager km = new X509KeyManager() {
        public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) {
            return "foo";
        }
    
        public X509Certificate[] getCertificateChain(String alias) {
            return origKm.getCertificateChain(alias);
        }
    
        // Delegate the rest of the methods from origKm too...
    }
    

    Then use it for your SSLContext:

    SSLContext sslContext = sslContext.getInstance("TLS");
    sslContext.init(new KeyManager[] { km }, null, null);
    SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
    

    0 讨论(0)
提交回复
热议问题