【发布时间】:2018-09-15 06:31:57
【问题描述】:
我正在尝试使用任何 AES GCM 变体进行 TLS 连接,根据我在文档中的理解,这应该是可能的,但我收到此错误:
Caused by: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1989)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1096)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1342)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1369)
问题是我尝试连接的服务器只接受这些密码:
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
TLS_RSA_WITH_AES_256_GCM_SHA384
TLS_RSA_WITH_AES_128_GCM_SHA256
我无法在此处发布我尝试连接的服务器,但我尝试在 github 存储库上复制该问题。我找不到只接受这些密码套件的服务器,这就是为什么我的回购失败并出现另一个错误的原因。
git clone https://github.com/andreicristianpetcu/gcm_with_bc_onjdk17
cd gcm_with_bc_onjdk17
JAVA_HOME="/usr/lib/jvm/java-7-openjdk-amd64/jre" mvn clean install
基本上这是来自 GitHub 的代码
package com.github.gcm_with_bc_onjdk17;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
public class GcmWithBouncyCasteleOnJDK17 {
public SSLConnectionSocketFactory getSslConnectionSocketFactory() throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, KeyManagementException, IOException {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
System.out.println(cipher);
SSLContext sslContext = SSLContexts.custom()
.build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(sslConnectionSocketFactory)
.build();
HttpGet out = new HttpGet("https://cloudflare.com/");
CloseableHttpResponse execute = httpClient.execute(out);
return sslConnectionSocketFactory;
}
}
谢谢
【问题讨论】:
-
错误信息是什么?
-
感谢 Eugène 对此进行调查。出于后代原因,我将错误消息添加为问题的一部分。幸运的是,一位同事找到了解决方案。你能解释一下为什么我被选为-1吗?我是否违反了任何堆栈溢出规则?再次感谢您调查我的问题。
-
我不知道,也没有人知道谁和为什么。当有评论时,我们可以认为评论的人就是投票的人。 SO 并不完美,你无法想象有多少人问,得到他们的答案,甚至没有将其标记为正确。我会为你所做的出色工作投赞成票。
标签: ssl encryption java-7 bouncycastle aes-gcm