【问题标题】:Using more than one key-pair in SSL Socket Factory Connection在 SSL 套接字工厂连接中使用多个密钥对
【发布时间】:2012-02-29 02:02:45
【问题描述】:

我正在使用一个密钥对,并且我正在考虑使用多个私钥来创建 ans SSL 套接字工厂的可能性。

这样我就可以共享不同的公钥并握手
在公钥存储中动态地为客户端提供

下面是解释我如何创建 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");
...

这是可能的还是梦想?

谢谢

【问题讨论】:

    标签: java security ssl ssl-certificate


    【解决方案1】:

    您可以通过使用自己的X509KeyManager 构造自己的SSLContext 并使用其chooseClientAlias 方法(或chooseServerAlias,取决于一方)选择密钥库alias 来做到这一点。

    按照这些思路应该可以工作:

    // 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 中也是如此),因此在选择别名之前您将无法知道客户端请求的主机名(来自服务器的观点)。

    【讨论】:

    • 无法使用 Java 7 在服务器端实现该功能。默认密钥管理器可以工作,但使用自定义实现时,服务器无法找到匹配的密码套件。 stackoverflow.com/q/39996178/2878556
    猜你喜欢
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 2013-09-18
    相关资源
    最近更新 更多