简体   繁体   English

如何在 Java8 上自定义 TCP/IP(非 HTTP)TLS 的主机名检查?

[英]How to customize hostname checking for TCP/IP (not HTTP) TLS on Java8?

I'm using Java SE 8.我正在使用 Java SE 8。

When I connect TLS (TCP/IP) server, I'd like to control (customize) hostname checking logic.当我连接 TLS (TCP/IP) 服务器时,我想控制(自定义)主机名检查逻辑。 For example, accepts only if hostname is matched, or always matched without any checking.例如,仅当主机名匹配时才接受,或始终匹配而不进行任何检查。 For HTTP, I can use HttpsURLConnection 's setHostnameVerifier() method.对于 HTTP,我可以使用HttpsURLConnectionsetHostnameVerifier()方法。

https://docs.oracle.com/javase/8/docs/api/javax/net/ssl/HttpsURLConnection.html#setHostnameVerifier-javax.net.ssl.HostnameVerifier- https://docs.oracle.com/javase/8/docs/api/javax/net/ssl/HttpsURLConnection.html#setHostnameVerifier-javax.net.ssl.HostnameVerifier-

I'd like to do the similar thing on TCP/IP not HTTP.我想在 TCP/IP 而不是 HTTP 上做类似的事情。 For example MQTT(S) on TCP/IP TLS.例如 TCP/IP TLS 上的 MQTT(S)。

I've read the following sites, but I couldn't get useful information.我已阅读以下网站,但无法获得有用的信息。

https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html#SettingHostnameVerifier https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse /JSSERefGuide.html#SettingHostnameVerifier

Is there any convenient way to do that?有没有什么方便的方法来做到这一点?

edit 2017/10/24编辑 2017/10/24

Some comments indicate that TCP/IP doesn't treat hostname.一些评论表明 TCP/IP 不处理主机名。 True.真的。 I should rephrase my question.我应该改写我的问题。

Updated question is "how to write checking hostname function between the hostname of connect target and the hostname from server certificate?"更新的问题是“如何在连接目标的主机名和服务器证书的主机名之间编写检查主机名功能?”

Connection target IP address is gotten using getByName() .使用getByName()连接目标 IP 地址。 https://docs.oracle.com/javase/8/docs/api/java/net/InetAddress.html#getByName-java.lang.String- https://docs.oracle.com/javase/8/docs/api/java/net/InetAddress.html#getByName-java.lang.String-

Get the SSLSession , either via a HandshakeCompletedListener or inline from the SSLSocket after you connect it, get the peer certificate from the session, get the subjectDN from the certificate, and check it against the hostname you thought you were connecting to.在连接后通过HandshakeCompletedListener或从SSLSocket内联获取SSLSession ,从会话中获取对等证书,从证书中获取subjectDN ,然后根据您认为要连接的主机名进行检查。 If any of that fails, close the connection.如果其中任何一个失败,请关闭连接。

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

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