使用Firefox浏览器做JDK11 TLS1.3连接测试
准备
JDK11开始支持TLS1.3了:JEP 332。
理解TLS连接,有时候是困难的,特别是不清楚实际上发送和接收的消息的时候。JSSE有一个内置的调试设施,设置系统属性javax.net.debug可以激活。想知道更多信息,参见Debugging Utilities。
下面看一看TLS 1.3握手时的调试输出。
例子使用默认的JSSE X509KeyManager和X509TrustManager。
ClassFileServer.java:
import java.io.*;
import java.net.*;
import java.security.KeyStore;
import javax.net.*;
import javax.net.ssl.*;
import javax.security.cert.X509Certificate;
/* ClassFileServer.java -- 一个简单的文件服务器,
* 支持HTTP GET请求,支持secure channel
*
* The ClassFileServer 实现了 a ClassServer,从文件系统读文件。
*/
public class ClassFileServer extends ClassServer {
private String docroot;
private static int DefaultServerPort = 2001;
/**
* 构造ClassFileServer.
*
* @param docroot 定位文件的路径
*/
public ClassFileServer(ServerSocket ss, String docroot) throws IOException
{
super(ss);
this.docroot = docroot;
}
/**
* 返回的byte[]是文件的内容
*
* @return the bytes for the file
* @exception FileNotFoundException,找不到文件时
*/
public byte[] getBytes(String path)
throws IOException
{
System.out.println("reading: " + path);
File f = new File(docroot + File.separator + path);
int length = (int)(f.length());
if (length == 0) {
throw new IOException("File length is zero: " + path);
} else {
FileInputStream fin = new FileInputStream(f);
DataInputStream in = new DataInputStream(fin);
byte[] bytecodes = new byte[length];
in.readFully(bytecodes);
return bytecodes;
}
}
/**
* Main方法
* 两个命令行参数
* port是服务器端口
* docroot是文件路径
*
* <code> new ClassFileServer(port, docroot);
* </code>
*/
public static void main(String args[])
{
System.out.println(
"USAGE: java ClassFileServer port docroot [TLS [true]]");
System.out.println("");
System.out.println(
"If the third argument is TLS, it will start as\n" +
"a TLS/SSL file server, otherwise, it will be\n" +
"an ordinary file server. \n" +
"If the fourth argument is true,it will require\n" +
"client authentication as well.");
int port = DefaultServerPort;
String docroot = "";
if (args.length >= 1) {
port = Integer.parseInt(args[0]);
}
if (args.length >= 2) {
docroot = args[1];
}
String type = "PlainSocket";
if (args.length >= 3) {
type = args[2];
}
try {
ServerSocketFactory ssf =
ClassFileServer.getServerSocketFactory(type);
ServerSocket ss = ssf.createServerSocket(port);
if (args.length >= 4 && args[3].equals("true")) {
((SSLServerSocket)ss).