gpt4 book ai didi

java - 收到致命警报 : bad_certificate

转载 作者:搜寻专家 更新时间:2023-10-30 21:08:24 26 4
gpt4 key购买 nike

我正在尝试设置 SSL 套接字连接(并在客户端上执行以下操作)

  1. 我生成证书签名请求以获取签名的客户端证书

  2. 现在我有私钥(在 CSR 期间使用)、签名的客户端证书和根证书(带外获得)。

  3. 我将私钥和签名的客户端证书添加到证书链,并将其添加到 key 管理器。和信任管理器的根证书。但是我得到了错误的证书错误。

我很确定我使用的是正确的证书。我是否也应该将已签名的客户端证书添加到信任管理器中?试过了,还是不走运。

//I add the private key and the client cert to KeyStore ks
FileInputStream certificateStream = new FileInputStream(clientCertFile);
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
java.security.cert.Certificate[] chain = {};
chain = certificateFactory.generateCertificates(certificateStream).toArray(chain);
certificateStream.close();
String privateKeyEntryPassword = "123";
ks.setEntry("abc", new KeyStore.PrivateKeyEntry(privateKey, chain),
new KeyStore.PasswordProtection(privateKeyEntryPassword.toCharArray()));

//Add the root certificate to keystore jks
FileInputStream is = new FileInputStream(new File(filename));
CertificateFactory cf = CertificateFactory.getInstance("X.509");
java.security.cert.X509Certificate cert = (X509Certificate) cf.generateCertificate(is);
System.out.println("Certificate Information: ");
System.out.println(cert.getSubjectDN().toString());
jks.setCertificateEntry(cert.getSubjectDN().toString(), cert);

//Initialize the keymanager and trustmanager and add them to the SSL context
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, "123".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(jks);

我需要在这里创建某种证书链吗?
我也有一个带有这些组件的 p12,并且在使用非常相似的代码后,将私钥添加到 key 管理器并将根证书从 p12 添加到信任管理器,我可以让它工作。但现在我需要让它在没有 p12 的情况下工作。

编辑:请求堆栈跟踪。希望这就足够了。 (注意:我屏蔽了文件名)

Caused by: javax.net.ssl.SSLHandshakeException: Received fatal alert: bad_certificate
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:136)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1720)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1138)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1165)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1149)
at client.abc2.openSocketConnection(abc2.java:33)
at client.abc1.runClient(abc1.java:63)
at screens.app.abc.validateLogin(abc.java:197)
... 32 more

最佳答案

您还需要将根证书添加到 keystore 。

关于java - 收到致命警报 : bad_certificate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11799733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com