Java HTTPS客户端在curl成功时失败SSL握手

时间:2018-12-06 17:09:52

标签: java ssl apache-httpclient-4.x mutual-authentication

道歉,对于SSL编码我还是很陌生。过去几天,我一直在寻找答案,尽管发现了很多建议,但到目前为止没有任何帮助。

我所拥有的是在Dropwizard之上实现的服务器,该服务器需要接受传入的HTTPS连接并使用附带的证书来唯一标识客户端。在开发过程中,我目前正在使用所有自签名证书。服务器证书对是使用链-根对->中间对->服务器对创建的。服务器P12是使用中间证书和服务器证书以及服务器私钥的串联创建的。然后将其添加到一个空的JKS中,并成为服务器的KeyStore。

我分别创建了两个客户端证书,一个使用与基础证书相同的中间对,另一个作为纯独立证书对。这两个证书对的x509公钥部分已添加到JKS文件中,并成为服务器的TrustStore。 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>
***

我还尝试过使用以sslContext初始化的SSLConnectionSocketFactoryRegistry<ConnectionSocketFactory>为“ https”注册socketFactory的方法。没事。对于curl为何接受证书颁发机构并发送客户端证书但Java httpClient不接受的原因,我完全感到茫然。

编辑:

我尝试将服务器的公共证书添加到客户端请求中,这没有什么不同-我仍然看到相同的行为。代码更新如下:

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();

1 个答案:

答案 0 :(得分:2)

因此,我最终彻底修改了证书的生成方式,并能够使工作正常进行,但需要警告:这取决于证书的生成方式。

与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

不知道为什么“快速”证书会引起很多问题,但至少现在可以使用了。感谢Patrick和Dave的帮助!