简体   繁体   English

在Java中打开SSLSocket时对证书链的验证

[英]Validation of a certificate chain when opening a SSLSocket in Java

My Setup 我的设定

My goal is to set up a SSL/TLS secured connection (explicit) with an FTP-Server. 我的目标是与FTP服务器建立SSL / TLS安全连接(显式)。 The appropriate Root CA Certificate is stored in a Truststore called truststore.jks . 适当的根CA证书存储在名为truststore.jks的Truststore中。 After the AUTH TLS command I'm using the following code to build up the SSLSocket. AUTH TLS命令之后,我使用以下代码来构建SSLSocket。

public SSLSocket enterSecureMode(Socket s) throws Exception {
        KeyStore truststore = KeyStore.getInstance("JKS");
        truststore.load(Files.newInputStream(Paths.get("truststore.jks")), "mypass".toCharArray());

        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(truststore);

        SSLContext sCon = SSLContext.getInstance("TLS");
        sCon.init(null, tmf.getTrustManagers(), null);

        SSLSocketFactory sslSocketFactory = sCon.getSocketFactory();
        return (SSLSocket) sslSocketFactory.createSocket(s, "<HOSTNAME>", 21, true);
}

The code itself is running fine and I received a secured Socket-Connection, but I wonder whether this can stand attacks like eg MITM or not. 代码本身运行良好,并且我收到了安全的Socket-Connection,但是我不知道这是否可以承受像MITM这样的攻击。 I mean would that program discover an attempt of somebody trying to 'give me a Fake-Certificate'. 我的意思是说该程序会发现有人试图“给我一张假证书”的尝试。 Therefore I'd be very happy if some more experienced SSL-Network-Programmers could enlight me :D 因此,如果一些经验丰富的SSL网络程序员可以启发我,我将非常高兴:D

This is sufficient. 这样就足够了。 The attacker would have to provide a certificate signed by the root CA. 攻击者必须提供由根CA签名的证书。 However you don't need all this code: you only need 但是,您不需要所有这些代码:您只需要

System.setProperty("javax.net.ssl.trustStore", "truststore.jks");
SSLContext sCon = SSLContext.getDefault();
SSLSocketFactory sslSocketFactory = sCon.getSocketFactory();
return (SSLSocket) sslSocketFactory.createSocket(s, "<HOSTNAME>", 21, true)

If you want to be totally paranoid, after creating the SSLSocket you can get the SSLSession and then the peer certificate chain and make sure that the zeroth entry exactly matches the exact server's certificate, but this step is mostly omitted. 如果您想完全偏执,则在创建SSLSocket之后,您可以获取SSLSession ,然后获取对等证书链,并确保第零个条目与确切的服务器证书完全匹配,但是此步骤通常被省略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM