Fluffy Fluffy - 1 month ago 9
Java Question

"unable to find valid certification path to requested target" in Camel Component Test

i am trying to develop and test a camel endpoint. I now want to test SSL connectivity. Whenever i run the test (which tries to do a mutual SSL authentication) i get the error "unable to find valid certification path to requested target".
My certificate chain looks like this for client and server. Both are identical besides an ID.
CA -> SubCA -> Client1 (used as "client")
-> Client2 (used as "server"

I created a PKCS12 file for both clients and imported CA, SubCA and client certificate into a keystore:

keytool -keystore store.jks -importcert -alias ca -file test_ca_certs/rootca.cert
keytool -keystore store.jsk -importcert -alias subca -file test_ca_certs/subca.cert
keytool -v -importkeystore -srckeystore source.p12 -srcstoretype PKCS12 -destkeystore store.jsk -deststoretype JKS


So lets say, i have a store for client and server only differing in the client cert. I tried to avoid having different trust/keystores, but this should be fine, right?

Pretty much directly taken from camel source examples, i have this methods used in my test class:

private static SSLContextParameters defineClientSSLContextClientParameters() {

KeyStoreParameters ksp = new KeyStoreParameters();
ksp.setResource(Thread.currentThread().getContextClassLoader().getResource("jsse/source.jks").toString());
ksp.setPassword(PWD);

KeyManagersParameters kmp = new KeyManagersParameters();
kmp.setKeyPassword(PWD);
kmp.setKeyStore(ksp);

TrustManagersParameters tmp = new TrustManagersParameters();
tmp.setKeyStore(ksp);

SSLContextServerParameters scsp = new SSLContextServerParameters();
scsp.setClientAuthentication(ClientAuthentication.REQUIRE.name());

SSLContextParameters sslContextParameters = new SSLContextParameters();
sslContextParameters.setKeyManagers(kmp);
sslContextParameters.setTrustManagers(tmp);
sslContextParameters.setServerParameters(scsp);

return sslContextParameters;
}

private static SSLContextParameters defineServerSSLContextParameters() {
KeyStoreParameters ksp = new KeyStoreParameters();
ksp.setResource(Thread.currentThread().getContextClassLoader().getResource("jsse/target.jks").toString());
ksp.setPassword(PWD);

KeyManagersParameters kmp = new KeyManagersParameters();
kmp.setKeyPassword(PWD);
kmp.setKeyStore(ksp);

TrustManagersParameters tmp = new TrustManagersParameters();
tmp.setKeyStore(ksp);

SSLContextServerParameters scsp = new SSLContextServerParameters();
scsp.setClientAuthentication(ClientAuthentication.REQUIRE.name());

SSLContextParameters sslContextParameters = new SSLContextParameters();
sslContextParameters.setKeyManagers(kmp);
sslContextParameters.setTrustManagers(tmp);
sslContextParameters.setServerParameters(scsp);


return sslContextParameters;
}

@Override
protected RouteBuilder[] createRouteBuilders() throws Exception {
RouteBuilder[] rbs = new RouteBuilder[2];

// A protocol consumer
rbs[0] = new RouteBuilder() {
public void configure() {

// Needed to configure TLS on the client side
WsComponent wsComponent = (WsComponent) context.getComponent("protocolclient");
wsComponent.setSslContextParameters(defineClientSSLContextClientParameters());

from("direct:input").routeId("foo")
.log(">>> Message from direct to WebSocket Client : ${body}")
.to("protocolclient://localhost:9292/echo")
.log(">>> Message from WebSocket Client to server: ${body}");
}
};

// A protocol provider
rbs[1] = new RouteBuilder() {
public void configure() {

// Needed to configure TLS on the server side
WebsocketComponent websocketComponent = (WebsocketComponent) context.getComponent("protocolserver");
websocketComponent.setSslContextParameters(defineServerSSLContextParameters());

// This route is set to use TLS, referring to the parameters set above
from("protocolserver:localhost:9292/echo")
.log(">>> Message from WebSocket Server to mock: ${body}")
.to("mock:result");
}
};
return rbs;
}


For completeness, here is the config file i used to generate the client certs:

HOME = .
RANDFILE = $ENV::HOME/.rnd

####################################################################
[ req ]
default_bits = 2048
#default_keyfile = client.key
distinguished_name = client_distinguished_name
req_extensions = client_req_extensions
string_mask = utf8only

####################################################################
[ client_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = DE

stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = Bayern

localityName = Locality Name (eg, city)
localityName_default = Muenchen

organizationName = Organization Name (eg, company)
organizationName_default = Company

organizationalUnitName = Organizational Unit Name (department, division)
organizationalUnitName_default = Department

commonName = Common Name (e.g. server FQDN or YOUR name)
commonName_default = Test Client X

emailAddress = Email Address
emailAddress_default = stuff@mail.de


####################################################################
[ connector_req_extensions ]

subjectKeyIdentifier = hash
basicConstraints = CA:FALSE
keyUsage = digitalSignature, keyEncipherment, nonRepudiation
extendedKeyUsage = clientAuth
subjectAltName = @alternate_names

[ alternate_names ]

DNS.1 = localhost
DNS.2 = 127.0.0.1
DNS.3 = client.companyname.de


Did i do some obvious mistake? I am quite puzzled :(

Thanks!

EDIT: I added some debug output. Is this how it should look like?

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
Validity: [From: Wed Oct 19 10:16:33 CEST 2016,
To: Fri Oct 19 10:16:33 CEST 2018]
Issuer: CN=My SubCA 2016, O=organization, C=DE
SerialNumber: [ 01]

Certificate Extensions: 8
[1]: ObjectId: 1.3.6.1.5.5.7.1.1 Criticality=false
AuthorityInfoAccess [
[
accessMethod: caIssuers
accessLocation: URIName: http://someurl
]
]

[2]: ObjectId: 2.5.29.35 Criticality=false
AuthorityKeyIdentifier [
KeyIdentifier [
0000: 58 DD 29 BF F2 31 7B 34 3F F2 7D B5 1F 2B 7D A3 X.)..1.4?....+..
0010: EB 71 EC 62 .q.b
]
]

[3]: ObjectId: 2.5.29.19 Criticality=false
BasicConstraints:[
CA:false
PathLen: undefined
]

[4]: ObjectId: 2.5.29.31 Criticality=false
CRLDistributionPoints [
[DistributionPoint:
[URIName: http://crl.someurl.crl]
]]

[5]: ObjectId: 2.5.29.37 Criticality=false
ExtendedKeyUsages [
clientAuth
]

[6]: ObjectId: 2.5.29.15 Criticality=false
KeyUsage [
DigitalSignature
Key_Encipherment
]

[7]: ObjectId: 2.5.29.17 Criticality=false
SubjectAlternativeName [
DNSName: localhost
DNSName: 127.0.0.1
]

[8]: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: F8 8E 73 ED 12 6A 61 2D A0 7F 15 F9 9D 84 BD BF ..s..ja-........
0010: 5E 96 B6 FF ^...
]
]

]
Algorithm: [SHA256withRSA]
Signature:
...

]
***
...
12:32:55.621 [main] TRACE org.apache.camel.util.jsse.TrustManagersParameters - Creating TrustManager[] from TrustManagersParameters [TrustManagerType [keyStore=KeyStoreParameters [type=null, password=********, provider=null, resource=file:/home/gbrost/GIT/karaf-policy-platform/camel-ids/target/test-classes/jsse/source-truststore.jks, getContext()=null], provider=null, algorithm=null, getContext()=null]]
---
12:32:55.644 [main] DEBUG org.apache.camel.util.jsse.JsseParameters - Opened resource [file:/home/gbrost/GIT/karaf-policy-platform/camel-ids/target/test-classes/jsse/source-truststore.jks] as a URL.
...
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

12:32:55.649 [main] DEBUG org.apache.camel.util.jsse.TrustManagersParameters - TrustManager[] [[sun.security.ssl.X509TrustManagerImpl@6b5176f2]], initialized from TrustManagerFactory [javax.net.ssl.TrustManagerFactory@209775a9].
12:32:56.099 [main] DEBUG org.apache.camel.util.jsse.SSLContextParameters -
...

Answer

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!