2016-11-28 81 views
1

我正在嘗試使用TLS調用服務器,只接受具有SNI支持的客戶端。無法在Java 7上啓用SNI

我正在使用Java 7的客戶:

$ java -version 
java version "1.7.0_80" 
Java(TM) SE Runtime Environment (build 1.7.0_80-b15) 
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode) 

應該是默認啓用,但是:

  • 「它不工作」
  • 我不能把它看成日誌中的擴展名

例如從我的日誌中:

Compression Methods: { 0 } 
Extension elliptic_curves, curve names: {secp256r1, sect163k1, sect163r2, secp192r1, secp224r1, sect233k1, sect233r1, sect283k1, sect283r1, secp384r1, sect409k1, sect409r1, secp521r1, sect571k1, sect571r1, secp160k1, secp160r1, secp160r2, sect163r1, secp192k1, sect193r1, sect193r2, secp224k1, sect239k1, secp256k1} 
Extension ec_point_formats, formats: [uncompressed] 
Extension signature_algorithms, signature_algorithms: SHA512withECDSA, SHA512withRSA, SHA384withECDSA, SHA384withRSA, SHA256withECDSA, SHA256withRSA, SHA256withDSA, SHA224withECDSA, SHA224withRSA, SHA224withDSA, SHA1withECDSA, SHA1withRSA, SHA1withDSA 
Extension renegotiation_info, renegotiated_connection: <empty> 
*** 

雖然我希望找到

Extension server_name, server_name: [host_name: my.servername.com] 

我嘗試添加該標誌-Djsse.enableSNIExtension=true可以肯定的,但沒有成功。

我正在使用的Netty 4.0.37測試,並得到此異常:

io.netty.handler.codec.DecoderException: javax.net.ssl.SSLException: Received fatal alert: handshake_failure 
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:442) 
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:248) 

什麼把戲上的Java 7 JVM啓用SNI?

回答

5

問題出在Netty/SSL引擎配置上。

它用來進行配置很喜歡這樣的:

public void initChannel(SocketChannel ch) throws Exception { 
    SSLEngine sslEngine = SSLContext.getDefault().createSSLEngine(); 
    sslEngine.setUseClientMode(true); 
    ch.pipeline() 
     .addLast("ssl", new SslHandler(sslEngine)); 
} 

實際上,它的工作可以這樣來配置時,明確設定目標服務器名稱:

public void initChannel(SocketChannel ch) throws Exception { 
    SslContext sslContext = SslContextBuilder.forClient().build(); 
    ch.pipeline() 
     .addLast(
      sslContext.newHandler(
       ch.alloc(), 
       host, 
       port 
     ) 
    ); 
} 

利用這種結構,應該有在日誌中(與-Djavax.net.debug=all標誌集):

Extension server_name, server_name: [type=host_name (0), value=target.hostname.com]