密钥库加载

时间:2013-01-25 13:31:24

标签: java ssl keystore

我想知道以下java中的等价物:

-Djavax.net.ssl.trustStore=keystore.jks -Djavax.net.ssl.keyStore=keystore.jks
-Djavax.net.debug=ssl -Djavax.net.ssl.keyStorePassword=test JavaFile

我想加载密钥库,而不是从命令行发送它作为参数。我一直在努力:

private TcpLink createSSL() {
        KeyStore keyStore = null;
        TrustManagerFactory tmf = null;
        SSLContext ctx = null;
        SSLSocket socket = null;
        TcpLink smscLink = null;

        try {
            keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
            LOGGER.info("Got keystore");
            keyStore.load(new FileInputStream("/root/keystore.jks"), "test".toCharArray());
            LOGGER.info("Loaded keystore");
            tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            tmf.init(keyStore);
            LOGGER.info("Inited keystore");
            ctx = SSLContext.getInstance("TLSv1");
            ctx.init(null, tmf.getTrustManagers(), null);
            SSLSocketFactory factory = ctx.getSocketFactory();
            socket = (SSLSocket)factory.createSocket("100.100.201.189", 8807);
            LOGGER.info("Got socket");
            smscLink = new TcpLink(socket);

            return smscLink;

        } catch (KeyStoreException e) {
            LOGGER.error("Key store exception : " + e);
        } catch (NoSuchAlgorithmException e) {
            LOGGER.error("NoSuchAlgorithmException : " + e);
        } catch (CertificateException e) {
            LOGGER.error("CertificateException : " + e);
        } catch (FileNotFoundException e) {
            LOGGER.error("FileNotFoundException : " + e);
        } catch (IOException e) {
            LOGGER.error("FileNotFoundException : " + e);
        } catch (KeyManagementException e) {
            LOGGER.error("KeyManagementException : " + e);
        } catch (Exception e) {
            LOGGER.error("Exception : " + e);
        }
        return null;
    }

但我明白了:

javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.checkEOF(SSLSocketImpl.java:1293)
        at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:65)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)

欢迎任何想法!

THX

3 个答案:

答案 0 :(得分:1)

您可以这样设置系统属性:

System.setProperty("javax.net.ssl.keyStore", '/path/to/keystore.jks');
System.setProperty("javax.net.ssl.keyStorePassword", 'your-password-here');

它们将在系统范围内使用(在此JVM实例中),因此您可能希望在初始化时执行此操作。

答案 1 :(得分:1)

它可以使用这段代码:

KeyManagerFactory kmf =  
    KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(this.getCertificateContent(), "test".toCharArray());
kmf.init(keyStore, "test".toCharArray());

TrustManagerFactory tmf =
    TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
SSLContext ctx = SSLContext.getInstance("TLSv1");
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
SSLSocketFactory factory = ctx.getSocketFactory();
socket = (SSLSocket)factory.createSocket("100.125.100.1", 8775);

答案 2 :(得分:-2)

如果您愿意,这里有一个API可以轻松创建SSLSocket和SSLServerSocket:

https://github.com/gpotter2/SSLKeystoreFactories

它不需要任何其他罐....只需获取文件并使用它们:

SSLSocket s = SSLSocketKeystoreFactory.getSocketWithCert(ip, port, Main.class.getResourceAsStream("/mykey.jks"), "password")

或者:

SSLServerSocket s = SSLServerSocketKeystoreFactory.getSocketWithCert(port, Main.class.getResourceAsStream("/mykey.jks"), "password")

这更容易使用:)