是否可以在JDK 1.7上使用BCM和BC?

时间:2018-04-05 12:22:23

标签: ssl encryption java-7 bouncycastle aes-gcm

我正在尝试使用任何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 repo上复制该问题。我找不到只接受这些密码套件的服务器,这就是我的repo因另一个错误而失败的原因。

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;
        }

    }

谢谢

1 个答案:

答案 0 :(得分:2)

一位工作同事发现了问题:) here is the fix

Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
Security.removeProvider(BouncyCastleJsseProvider.PROVIDER_NAME);
Security.insertProviderAt(new BouncyCastleProvider(), 0);
Security.insertProviderAt(new BouncyCastleJsseProvider(), 1);

似乎JDK7支持TLS 1.2,但不支持AES GCM密码。由于加密提供程序是一个列表以某种方式提取JDK提供程序,因为它支持TLS 1.2,即使它不支持所需的密码。只需将Bouncy Castle放在列表中稍高一点即可解决问题。

我不知道为什么我的问题被投票:(我不知道我打破了什么规则,所以它投了票,我甚至提供了源代码。

无论如何....超级高兴我发现修复即使它不在Stack Overflow之外。留在这里为后代。