Java 在SSL套接字工厂连接中使用多个密钥对
我正在使用一个密钥对,我考虑使用多个私钥来创建ans SSL套接字工厂的可能性 因此,我将能够共享不同的公钥并进行握手Java 在SSL套接字工厂连接中使用多个密钥对,java,security,ssl,ssl-certificate,Java,Security,Ssl,Ssl Certificate,我正在使用一个密钥对,我考虑使用多个私钥来创建ans SSL套接字工厂的可能性 因此,我将能够共享不同的公钥并进行握手 在公钥存储中动态地为客户端提供 下面是解释如何创建此SSL连接的源代码 ... ...log("Activating an SSL connection"); System.setProperty("javax.net.ssl.keyStore", "myPrivateKey"); System.setProperty("javax.net.ssl.keyStore
在公钥存储中动态地为客户端提供 下面是解释如何创建此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
并使用其方法选择密钥库别名(或选择服务器别名
,具体取决于侧)
按照这些思路应该可以做到:
// 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();
(有一种方法可以帮助您开始。)
实际上,您不必委托给原始的KeyManager(我只是觉得它更方便)。您可以很好地实现它的所有方法,使用加载的密钥库返回密钥和证书
请注意,这对于选择客户端证书非常有用。Java不支持服务器端的服务器名称指示(SNI)(据我所知,即使是在Java 7中),因此在选择别名之前(从服务器的角度),您将无法知道客户端请求的主机名
按照这些思路应该可以做到:
// 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();
(有一种方法可以帮助您开始。)
实际上,您不必委托给原始的KeyManager(我只是觉得它更方便)。您可以很好地实现它的所有方法,使用加载的密钥库返回密钥和证书
请注意,这对于选择客户端证书非常有用。Java不支持服务器端的服务器名称指示(SNI)(据我所知,即使在Java 7中也是如此),因此在选择别名之前(从服务器的角度),您将无法知道客户端请求的主机名。无法在使用Java 7的服务器端实现这一点。默认密钥管理器可以工作,但使用自定义实现时,服务器无法找到匹配的密码套件。无法使用Java 7在服务器端实现这一点。默认密钥管理器可以工作,但使用自定义实现时,服务器无法找到匹配的密码套件。