简体   繁体   中英

Java.nio Channels and TLS

How do I secure a Java SocketChannel , ServerSocketChannel or, perhaps even, a DatagramChannel with TLS?

I know that there are some frameworks ( #1 #2 ) that advertise to be able, but I want to know if it is possible to achieve this with the pure Java standard library alone.

You need to use the SSLEngine , as documented in Non-blocking I/O with SSLEngine . The libraries you mention use it or use libraries that use it.

(Note that this is notoriously difficult to use.)

You may find these links interesting:


For Datagrams, you should look into using DTLS instead of TLS. I'm not sure of its implementation status in Java, but you could dig through the archives of the java.openjdk.security.devel mailing list.

You need to use SSLEngine and do the handshake manually using that state machine. SSL/TLS is implemented on top of TCP so you can not use it directly on top of a DatagramChannel .

The article Non-blocking I/O with SSLEngine may be helpful.

As Bruno correctly mentions, the standard way of doing that is using SSLEngine. But that class is seriously hard to use.

I came across the same problem some time ago and ended up writing my own library. There are some examples out there and of course there is also the code inside projects like Netty, etc. But neither option is robust or easily reusable.

TLS Channel wraps an SSLEngine in a ByteBuffer and allows to use it just like normal SocketChannels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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