在SSL Socket Factory连接中使用多个密钥对

问题描述:

我正在使用密钥对,我想可能会使用多个私钥来创建SSL套接字工厂。在SSL Socket Factory连接中使用多个密钥对

因此,我将能够分享不同的公共密钥并进行手抖动
动态基于公共密钥存储为客户提供

贝娄源代码解释我是如何创造这个连接SSL

... 
    ...log("Activating an SSL connection"); 
    System.setProperty("javax.net.ssl.keyStore", "myPrivateKey"); 
    System.setProperty("javax.net.ssl.keyStorePassword", "myPass"); 

    // SSL Server Socket Factory 
    SSLServerSocketFactory sslSrvFact = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); 
    objServerSocket = sslSrvFact.createServerSocket(iPort); 
    log("SSL connection actived"); 
... 

这是可能的还是梦想?

THX

您可以使用自己的X509KeyManager构建自己的SSLContext使用其chooseClientAlias方法做到这一点,并选择密钥库alias(或chooseServerAlias,根据侧)。

东西沿着这些路线应该工作:

// Load the key store: change store type if needed 
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); 
FileInputStream fis = new FileInputStream("/path/to/keystore"); 
try { 
    ks.load(fis, keystorePassword); 
} finally { 
    if (fis != null) { fis.close(); } 
} 

// Get the default Key Manager 
KeyManagerFactory kmf = KeyManagerFactory.getInstance(
    KeyManagerFactory.getDefaultAlgorithm()); 
kmf.init(ks, keyPassword); 

final X509KeyManager origKm = (X509KeyManager)kmf.getKeyManagers()[0]; 
X509KeyManager km = new X509KeyManager() { 
    public String chooseClientAlias(String[] keyType, 
            Principal[] issuers, Socket socket) { 
     // Implement your alias selection, possibly based on the socket 
     // and the remote IP address, for example. 
    } 

    // Delegate the other methods to origKm. 
} 

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

SSLSocketFactory sslSocketFactory = sslContext.getSSLSocketFactory(); 

(有一个short example here,可以帮助你开始)

你实际上并没有委托给原来的KeyManager(我只是觉得更方便)。您可以很好地实施其所有方法,以使用您已加载的KeyStore返回密钥和证书。

请注意,这对于选择客户端证书非常有用。 Java在服务器端不支持服务器名称指示(SNI)(就我所知即使在Java 7中也是如此),所以在选择别名之前,您将无法知道客户端请求的主机名服务器的角度)。

+0

这个建议的解决方案不起作用 – Bera 2012-02-23 12:59:17

+0

无法在服务器端使用Java 7工作。缺省密钥管理器可以工作,但通过自定义实现,服务器无法找到匹配的密码套件。 http://*.com/q/39996178/2878556 – 2016-10-19 11:53:28