当curl成功时,javahttps客户端的ssl握手失败

tzcvj98z  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(558)

恕我直言,我对ssl的编码还是相当陌生的。在过去的几天里,我一直在寻找答案,虽然我找到了很多建议,但到目前为止,一切都不管用。
我拥有的是一个在dropwizard之上实现的服务器,它需要接受传入的https连接,并使用附加的证书来唯一地标识客户机。我目前正在使用所有自签名证书,而我在发展。服务器证书对是使用链-根对->中间对->服务器对创建的。服务器的p12是使用中间证书和服务器证书加上服务器私钥的连接创建的。然后它被添加到一个空的jks中,成为服务器的密钥库。
我分别创建了两个客户机证书,一个使用相同的中间对作为基础,另一个作为纯独立的证书对。这两个证书对的x509公钥部分被添加到jks文件中,并成为服务器的信任库。dropwizard配置如下:

type: "https"
port: "9843"
keyStorePath: "keystore.jks"
keyStorePassword: "changeme"
keyStoreType: "JKS"
trustStorePath: "truststore.jks"
trustStorePassword: "changeme"
trustStoreType: "JKS"
allowRenegotiation: false
validateCerts: false
validatePeers: false
needClientAuth: true
wantClientAuth: true

我可以使用 curl 以及任一客户端证书对:

curl -v --cert client.pem --key client.key -k https://localhost:9843/v1/ld

启用ssl调试后,服务器将记录以下内容:


***CertificateRequest

Cert Types: RSA, DSS, ECDSA
Supported Signature Algorithms: SHA512withECDSA, SHA512withRSA, SHA384withECDSA, SHA384withRSA, SHA256withECDSA, SHA256withRSA, SHA256withDSA, SHA224withECDSA, SHA224withRSA, SHA224withDSA, SHA1withECDSA, SHA1withRSA, SHA1withDSA
Cert Authorities:
<CN=*.me.com, O=Me, ST=Massachusetts, C=US>
<O=Internet Widgits Pty Ltd, ST=Massachusetts, C=US>

***ServerHelloDone

dw-51, WRITE: TLSv1.2 Handshake, length = 3536
dw-44, READ: TLSv1.2 Handshake, length = 1047

***Certificate chain

chain [0] = [
[
  Version: V3
  Subject: O=Internet Widgits Pty Ltd, L=North Reading, ST=Massachusetts, C=US
  Signature Algorithm: SHA1withRSA, OID = 1.2.840.113549.1.1.5

  Key:  Sun RSA public key, 2048 bits
  modulus: 23250299629324311533731283912176366463399376328149948822580485256237233115567136794461732268017120297060017586981907979910958857247642884566364833267711927344361604478514119965230314679194017013023991389216461419030751049820266939279047536006291610734616600760688907006770883510297954698233112783686968024400749969025850008781641616624298935923926427096257861170476293580684942956111432790304698635393966967864288730561678135798437678912431564767611000006312358137647455886578135011989168265295083928014176435879778838966450081419161406209555593636745048857672445188811541416453143809594265089422302064600885289819601
  public exponent: 65537
  Validity: [From: Wed Dec 05 13:52:49 EST 2018,
               To: Thu Dec 05 13:52:49 EST 2019]
  Issuer: O=Internet Widgits Pty Ltd, ST=Massachusetts, C=US
  SerialNumber: [    8174655c c8387da4]

Certificate Extensions: 3
...

到现在为止,一直都还不错。接下来,我尝试使用一个java客户机连接到我的服务器,该客户机使用相同的证书对,组合在一个p12文件中。java代码如下:

char[] password = "changeme".toCharArray();
KeyStore keystore = KeyStore.getInstance("PKCS12");
try (FileInputStream fileInputStream = new FileInputStream("client.p12")) {
    keystore.load(fileInputStream, password);
}

SSLContext sslContext = 
        SSLContexts.custom()
                   .loadKeyMaterial(keystore, password)
                   .loadTrustMaterial(null, (chain, authType) -> true)
                   .build();

return HttpClients.custom()
                  .setSSLContext(sslContext)
                  .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                  .build();

但当此客户端尝试连接服务器时,会记录以下内容:


***CertificateRequest

Cert Types: RSA, DSS, ECDSA
Supported Signature Algorithms: SHA512withECDSA, SHA512withRSA, SHA384withECDSA, SHA384withRSA, SHA256withECDSA, SHA256withRSA, SHA256withDSA, SHA224withECDSA, SHA224withRSA, SHA224withDSA, SHA1withECDSA, SHA1withRSA, SHA1withDSA
Cert Authorities:
<CN=*.me.com, O=Me, ST=Massachusetts, C=US>
<O=Internet Widgits Pty Ltd, ST=Massachusetts, C=US>

***ServerHelloDone

Warning: no suitable certificate found - continuing without client authentication

***Certificate chain

<Empty>

***

我也试过使用 SSLConnectionSocketFactory 用sslcontext初始化,并且 Registry<ConnectionSocketFactory> 为“https”注册socketfactory。什么都没用。我完全不知道为什么 curl 接受证书颁发机构并发送客户端证书,但JavaHttpClient不接受。
编辑:
我尝试将服务器的公共证书添加到客户机请求中,但没有产生任何影响-我仍然看到相同的行为。代码更新如下:

KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
try (FileInputStream fileInputStream = new FileInputStream("server.cert.pem")) {
    try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(IOUtils.toByteArray(fileInputStream))) {
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
        Certificate certificate = certificateFactory.generateCertificate(byteArrayInputStream);
        trustStore.setCertificateEntry("server", certificate);
    }
}

SSLContext sslContext =
        SSLContexts.custom()
                   .loadKeyMaterial(keystore, password)
                   .loadTrustMaterial(trustStore, (chain, authType) -> true)
                   .build();
kokeuurv

kokeuurv1#

因此,我最终完全重新设计了我的证书的生成方式,并且我能够让事情正常工作,但有一个警告:这取决于证书的生成方式。
使用java客户端、curl和postman:

openssl genrsa -aes256 -out private/${SVRNAME}.key.pem 2048
openssl req -config ${CONFIGDIR}/openssl.cnf \
      -new -x509 -days 7300 -sha256 -extensions v3_ca \
      -key private/${SVRNAME}.key.pem \
      -out certs/${SVRNAME}.cert.pem

适用于curl和postman,但不适用于java客户端:

openssl req -newkey rsa:2048 -nodes \
      -keyout private/${CLINAME}.key.pem \
      -x509 -days 365 -out certs/${CLINAME}.cert.pem

不知道为什么“快速”证书会引起这么多问题,但至少它现在起作用了。感谢帕特里克和戴夫的帮助!

相关问题