Fluffy Fluffy - 1 year ago 46
Java Question

Setup to avoid PKIX path building failed error

Sorry for opening another question with the same topic, but i think this sub-question would bloat the other one into oblivion.

I run into the mentioned error message, which is quite unspecific (at least for me). The debug output shows the certificates are loaded and then only the mentioned error. I generated the test certificate with its own CA chain:
CA -> SubCA -> ClientCert

I try to connect a client and a server on the same machine (to test a two way protocol) with SSL.

I generate my ca certificates using these commands:

openssl req -batch -x509 -config ${ROOTCA_CONFIG} -newkey rsa:2048 -sha1 -nodes -out ${ROOTCA_CERT} -outform PEM -days 7300
openssl req -batch -config ${SUBCA_CONFIG} -newkey rsa:2048 -sha1 -nodes -out ${SUBCA_CSR} -outform PEM
openssl ca -batch -config ${ROOTCA_CONFIG} -policy signing_policy -extensions signing_req_CA -out ${SUBCA_CERT} -infiles ${SUBCA_CSR}

They seem to be fine. The only thing that puzzles me is: If concatenate both certificates into a single file and verify them with that chain, it is fine. If it try to verify with subCA or the root CA only, verification fails.

Then i create my client/server cert:

openssl req -batch -config ${CLIENT_CONFIG} -newkey rsa:2048 -sha256 -nodes -out ${CLIENT_CSR} -outform PEM -keyout $1.key
openssl ca -batch -config ${SUBCA_CONFIG} -policy signing_policy -extensions signing_req -out ${CLIENT_CERT} -infiles ${CLIENT_CSR}

With this i create a PKCS12 file to use in my keystore:

openssl pkcs12 -export -inkey ${CONNECTOR_KEY} -in ${CONNECTOR_CERT} -out ${CONNECTOR_P12}

I do this by calling my script twice, once for the server and once for the client. Let's call them client.cert and server.cert, even if client/server is confusing since they both are local protocol endpoints.

I then use these commands to generate the truststore and keystore for client and server:

keytool -keystore $2-truststore.jks -importcert -alias ca -file test_ca_certs/rootca.cert
keytool -keystore $2-truststore.jks -importcert -alias subca -file test_ca_certs/subca.cert
keytool -v -importkeystore -srckeystore $1 -srcstoretype PKCS12 -destkeystore $2-keystore.jks -deststoretype JKS

Let $2 be client and server each (server-truststore etc.) and $1 be the same as ${CONNECTOR_P12} before (somefile.p12)

So now i have a truststore with CA and SubCA and a keystore with the PKCS12 Token. Truststore is the same on client and server side, Token is pretty much the same, but has different keypairs, since they are generated each time.

The ssl debug output indicates the certs are loaded:

keystore (...) has type [jks], and contains aliases [1].
found key for : 1
chain [0] = [
Version: V3
Subject: CN=cnname, OU=ouname, O=oname, L=location, ST=bavaria, C=DE
Signature Algorithm: SHA256withRSA, OID = 1.2.840.113549.1.1.11

Key: Sun RSA public key, 2048 bits
modulus: 2999...
public exponent: 65537
keystore has type [jks], and contains aliases [ca, subca].
adding as trusted cert:
Subject: CN=my Root CA 2016, O=organization, C=DE
Issuer: CN=my Root CA 2016, O=organization, C=DE
Algorithm: RSA; Serial number: 0xfc8239c0355555c1
Valid from Wed Oct 19 10:14:36 CEST 2016 until Tue Oct 14 10:14:36 CEST 2036

adding as trusted cert:
Subject: CN=my SubCA 2016, O=Fraunhofer, C=DE
Issuer: CN=my Root CA 2016, O=Fraunhofer, C=DE
Algorithm: RSA; Serial number: 0x1
Valid from Wed Oct 19 10:14:38 CEST 2016 until Thu Oct 17 10:14:38 CEST 2024

Is there some general flaw in my understanding? Again, sorry for posting two questions but i start to believe i do something wrong in a more fundamental fashion. Thanks!

Answer Source

I finally found the solution. I only set debugging to SSL. This was my mistake. I would have needed to set the debug output to "all". Then i can see this error message:

Caused by: sun.security.validator.ValidatorException: Extended key usage does not permit use for TLS server authentication

This is much more specific. To fix that, indeed i needed to change my extended key usage to this:

keyUsage            = digitalSignature, keyEncipherment, nonRepudiation
extendedKeyUsage    = clientAuth, serverAuth

Thank you very much!