问题描述:近期遇到访问外部一个Https服务出现Received fatal alert: handshake_failure问题,看起来应该是握手环节有问题。
解决步骤
1. 查询握手需要协商的信息
下图是TLSv1.2的连接过程
可以看到ClientHello和ServerHello阶段协商的信息主要包括TLS版本,随机数和Cipher Suite(即密码套件)。其中随机数一般不考虑这个因素,主要考虑协议版本和密码套件不匹配两点,接下来分析问题我们都将从这两点入手。
2. 分析不同版本JDK支持的协议
不同的JDK版本支持的protocol version也是有所差异的,这一般是造成protocol_version的主要原因,下图是几个JDK版本支持协议的对照表:
| JDK 8 (March 2014 to 8u261) | JDK 7 (July 2011 to present) | JDK 6 |
---|
protocol version | TLSv1.2 (default) TLSv1.1 TLSv1 SSLv3 TLSv1.3(8u 261 and above) | TLSv1.2 TLSv1.1 TLSv1 (default) SSLv3 | TLSv1.2(JDK 6u121 and above) TLS v1.1 (JDK 6 update 111 and above) TLSv1 (default) SSLv3 |
发请求的应用使用的JDK为6u131, 服务端为JDK8,发现可能是JDK支持的默认协议版本不一致导致的version_protocol错误,因此在启动工程时加上-Djavax.net.debug=all 参数,调用查看DEBUG日志,可以看到HTTPS连接过程。错误日志如下:
main, READ: TLSv1 Alert, length = 2
main, RECV TLSv1 ALERT: fatal, protocol_version
可以看出我们使用了TLSv1协议,对方没有支持。为了确定服务支持的协议,在本地改用JDK8来对HTTPS服务进行请求尝试,用如下代码指定协议访问,测试发现对方仅支持TLSv1.2。
SSLContext sslContext = SSLContext.getInstance("TLSv1.1");
sslContext.init(null, tm, null, new java.security.SecureRandom());
SocketFactory ssf = sslContext.getSocketFactory();
httpsconn.setSSLSocketFactory(ssf);
3. 使JDK6支持TLSv1.2
BouncyCastle是一个提供了很多加密算法和哈希算法的第三方库,同时增加一个继承SSLSocketFactory的类,就可以成功以TLSv1.2访问。
注意两个点:
* BC包版本保持在jdk15on-1.54版本以后
* 以如下方式使用新增的类TLSSCF
HttpsURLConnection httpsconn = null;
httpsconn.setSSLSocketFactory(new TLSSCF())
新建类的代码网上有许多可以参考,如:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyStore;
import java.security.Principal;
import java.security.SecureRandom;
import java.security.Security;
import java.security.cert.CertificateExpiredException;
import java.security.cert.CertificateFactory;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import javax.net.ssl.HandshakeCompletedListener;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSessionContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.security.cert.X509Certificate;
import org.bouncycastle.crypto.tls.*;
import org.bouncycastle.crypto.tls.Certificate;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class TLSSocketConnectionFactory extends SSLSocketFactory {
static {
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
Security.addProvider(new BouncyCastleProvider());
}
}
@Override
public Socket createSocket(Socket socket, final String host, int port,
boolean arg3) throws IOException {
if (socket == null) {
socket = new Socket();
}
if (!socket.isConnected()) {
socket.connect(new InetSocketAddress(host, port));
}
final TlsClientProtocol tlsClientProtocol = new TlsClientProtocol(socket.getInputStream(), socket.getOutputStream(), new SecureRandom());
return _createSSLSocket(host, tlsClientProtocol);
}
@Override
public String[] getDefaultCipherSuites() {
return null;
}
@Override
public String[] getSupportedCipherSuites() {
return null;
}
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
throw new UnsupportedOperationException();
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return null;
}
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
throw new UnsupportedOperationException();
}
private SSLSocket _createSSLSocket(final String host, final TlsClientProtocol tlsClientProtocol) {
return new SSLSocket() {
private java.security.cert.Certificate[] peertCerts;
@Override
public InputStream getInputStream() throws IOException {
return tlsClientProtocol.getInputStream();
}
@Override
public OutputStream getOutputStream() throws IOException {
return tlsClientProtocol.getOutputStream();
}
@Override
public synchronized void close() throws IOException {
tlsClientProtocol.close();
}
@Override
public void addHandshakeCompletedListener(HandshakeCompletedListener arg0) {
}
@Override
public boolean getEnableSessionCreation() {
return false;
}
@Override
public String[] getEnabledCipherSuites() {
return null;
}
@Override
public String[] getEnabledProtocols() {
return null;
}
@Override
public boolean getNeedClientAuth() {
return false;
}
@Override
public SSLSession getSession() {
return new SSLSession() {
@Override
public int getApplicationBufferSize() {
return 0;
}
@Override
public String getCipherSuite() {
throw new UnsupportedOperationException();
}
@Override
public long getCreationTime() {
throw new UnsupportedOperationException();
}
@Override
public byte[] getId() {
throw new UnsupportedOperationException();
}
@Override
public long getLastAccessedTime() {
throw new UnsupportedOperationException();
}
@Override
public java.security.cert.Certificate[] getLocalCertificates() {
throw new UnsupportedOperationException();
}
@Override
public Principal getLocalPrincipal() {
throw new UnsupportedOperationException();
}
@Override
public int getPacketBufferSize() {
throw new UnsupportedOperationException();
}
@Override
public X509Certificate[] getPeerCertificateChain() throws SSLPeerUnverifiedException {
return null;
}
@Override
public java.security.cert.Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException {
return peertCerts;
}
@Override
public String getPeerHost() {
throw new UnsupportedOperationException();
}
@Override
public int getPeerPort() {
return 0;
}
@Override
public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
return null;
}
@Override
public String getProtocol() {
throw new UnsupportedOperationException();
}
@Override
public SSLSessionContext getSessionContext() {
throw new UnsupportedOperationException();
}
@Override
public Object getValue(String arg0) {
throw new UnsupportedOperationException();
}
@Override
public String[] getValueNames() {
throw new UnsupportedOperationException();
}
@Override
public void invalidate() {
throw new UnsupportedOperationException();
}
@Override
public boolean isValid() {
throw new UnsupportedOperationException();
}
@Override
public void putValue(String arg0, Object arg1) {
throw new UnsupportedOperationException();
}
@Override
public void removeValue(String arg0) {
throw new UnsupportedOperationException();
}
};
}
@Override
public String[] getSupportedProtocols() {
return null;
}
@Override
public boolean getUseClientMode() {
return false;
}
@Override
public boolean getWantClientAuth() {
return false;
}
@Override
public void removeHandshakeCompletedListener(HandshakeCompletedListener arg0) {
}
@Override
public void setEnableSessionCreation(boolean arg0) {
}
@Override
public void setEnabledCipherSuites(String[] arg0) {
}
@Override
public void setEnabledProtocols(String[] arg0) {
}
@Override
public void setNeedClientAuth(boolean arg0) {
}
@Override
public void setUseClientMode(boolean arg0) {
}
@Override
public void setWantClientAuth(boolean arg0) {
}
@Override
public String[] getSupportedCipherSuites() {
return null;
}
@Override
public void startHandshake() throws IOException {
tlsClientProtocol.connect(new DefaultTlsClient() {
@SuppressWarnings("unchecked")
@Override
public Hashtable<Integer, byte[]> getClientExtensions() throws IOException {
Hashtable<Integer, byte[]> clientExtensions = super.getClientExtensions();
if (clientExtensions == null) {
clientExtensions = new Hashtable<Integer, byte[]>();
}
byte[] host_name = host.getBytes();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final DataOutputStream dos = new DataOutputStream(baos);
dos.writeShort(host_name.length + 3);
dos.writeByte(0);
dos.writeShort(host_name.length);
dos.write(host_name);
dos.close();
clientExtensions.put(ExtensionType.server_name, baos.toByteArray());
return clientExtensions;
}
@Override
public TlsAuthentication getAuthentication() throws IOException {
return new TlsAuthentication() {
@Override
public void notifyServerCertificate(Certificate serverCertificate) throws IOException {
try {
KeyStore ks = _loadKeyStore();
CertificateFactory cf = CertificateFactory.getInstance("X.509");
List<java.security.cert.Certificate> certs = new LinkedList<java.security.cert.Certificate>();
boolean trustedCertificate = false;
for (org.bouncycastle.asn1.x509.Certificate c : ((org.bouncycastle.crypto.tls.Certificate) serverCertificate).getCertificateList()) {
java.security.cert.Certificate cert = cf.generateCertificate(new ByteArrayInputStream(c.getEncoded()));
certs.add(cert);
String alias = ks.getCertificateAlias(cert);
if (alias != null) {
if (cert instanceof java.security.cert.X509Certificate) {
try {
((java.security.cert.X509Certificate) cert).checkValidity();
trustedCertificate = true;
} catch (CertificateExpiredException cee) {
}
}
} else {
}
}
if (!trustedCertificate) {
}
peertCerts = certs.toArray(new java.security.cert.Certificate[0]);
} catch (Exception ex) {
ex.printStackTrace();
throw new IOException(ex);
}
}
@Override
public TlsCredentials getClientCredentials(CertificateRequest certificateRequest) throws IOException {
return null;
}
private KeyStore _loadKeyStore() throws Exception {
FileInputStream trustStoreFis = null;
try {
KeyStore localKeyStore = null;
String trustStoreType = System.getProperty("javax.net.ssl.trustStoreType") != null ? System.getProperty("javax.net.ssl.trustStoreType") : KeyStore.getDefaultType();
String trustStoreProvider = System.getProperty("javax.net.ssl.trustStoreProvider") != null ? System.getProperty("javax.net.ssl.trustStoreProvider") : "";
if (trustStoreType.length() != 0) {
if (trustStoreProvider.length() == 0) {
localKeyStore = KeyStore.getInstance(trustStoreType);
} else {
localKeyStore = KeyStore.getInstance(trustStoreType, trustStoreProvider);
}
char[] keyStorePass = null;
String str5 = System.getProperty("javax.net.ssl.trustStorePassword") != null ? System.getProperty("javax.net.ssl.trustStorePassword") : "";
if (str5.length() != 0) {
keyStorePass = str5.toCharArray();
}
localKeyStore.load(trustStoreFis, keyStorePass);
if (keyStorePass != null) {
for (int i = 0; i < keyStorePass.length; i++) {
keyStorePass[i] = 0;
}
}
}
return localKeyStore;
} finally {
if (trustStoreFis != null) {
trustStoreFis.close();
}
}
}
};
}
});
}
};
}
}
- 升级JDK6至121以后版本
根据ORACLE官网的信息,JDK 6u121以后TLS v1.2已经加入了可选项,在没有指定的情况下,TLSv1.0仍然是默认选项。
注意两个点:
* JDK6的许多后续版只提供给商用用户
* 需去[官网](http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html)下载JCE扩展包进行替换
升级后指定TLSv1.2进行请求,一般这时候就成功了,但是我的问题并没有解决,根据猜测应该是第一部分中提到的密码套件不匹配的问题。
4. 查询密码套件
排除协议版本的问题后,考虑密码套件不匹配的因素。
指定密码套件的测试方法:
HttpClientBuilder builder = HttpClientBuilder.create().
setSSLSocketFactory(new SSLConnectionSocketFactory(SSLContext.getDefault(),
new String[]{"TLSv1.2"}, new String[]{"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256"},
new DefaultHostnameVerifier()));
CloseableHttpClient httpClient = builder.build();
HttpPost httpPost = new HttpPost("https://example.com");
CloseableHttpResponse response = httpClient.execute(httpPost);
response.close();
httpClient.close();
利用之前设置的启动参数,可以在日志中看到客户端支持的密码套件:
服务端支持的密码套件,如果可以互联网访问的话,可以使用https://myssl.com/查询,内网系统的话可以使用SSLyz查询,具体的方法可参见解决密码套件不一致导致的 Received fatal alert: handshake_failure – 天涯旅店 (sekiro.top) 。
上图是客户端在JDK6_211版本加JCE包和不加JCE包的情况下分别有的密码套件,但是密码套件并没有增加,和天涯老哥 这篇中分析的JCE包是通过增加客户端支持的密码套件列表来达到解决不匹配问题的观点有些不一样。
但是我发现仍然不行,当然至此也就发现我使用JDK 6u211是不能解决我遇到的问题,我猜测原因可能是服务端指定了密码套件,我这个版本没有,所以不行,毕竟访问其他的TSLv1.2是正常。
5. 探索探索
走完整个摸索过程后,我找到了唯一的解决方法,就是BC包。但是我还有三个疑惑,一下是探索过程:
1. 用JDK8是可以成功访问的,我看一下他使用的密码套件是否在6u211里面不存在呢?是否和我猜测的原因吻合?
经测试,jdk8使用的套件在6u211的套件列表中确实不存在,和我的猜测没有冲突 。
2. 替换JCE包不会对密码套件的列表产生变化吗?明明天涯旅店老哥在JDK7实践了的啊?
如图,对jdk 6u113版本替换JCE包后,套件列表是有变化的,之所以JDK 6u211没有变化,我觉得是因为作为2018年后续发布的版本,之前JCE包包含的扩展套件已经被正式引入了。 随后,我在官网无意中看到这一行注释,从6u181后JCE便已作为默认策略了。
3. 既然加JDK6+BC包可以成功,那么他使用的哪个密码套件呢?套件列表有变化吗?
这个部分,在实验时发现,不会有cipher suites相关日志,因此我也无从猜测。
|