In fact I'm not using Apache4SslUtils. I just copy pasted the 5 lines to my code here as example to make it simple :) This brings me to the question why you had separated Apache4SslUtils to is own jar .
You could have used optional scope dependencies in sslcontext-kickstart
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<optional>true</optional>
</dependency>
Is this about DIY zero-dependency ?
I was very eager to try it out yesterday night, so I ended up coding:
It looks similar to your solution, but I was not quite sure if it would work without the keystone/truststore in the ssl engine factory. I was thinking that they would not be needed if the engine has the pre initialized key manager and trust manager and if shouldBeRebuilt returns false. But I need to try this with a poc with ssl enabled
Hi @mic331
Thank you for switching over from mail to Gitter! The ssl configuration is simple, you should have the following dependency in your project:
<dependency>
<groupId>io.github.hakky54</groupId>
<artifactId>sslcontext-kickstart-for-pem</artifactId>
<version>7.3.0</version>
</dependency>
What you need after this is use the private key and certificate chain as identity for the client and use the other pem file as trusted certificates so the client can compare that when t receives the server certificate.
The example sslconfiguration would look like:
var keyManager = PemUtils.loadIdentityMaterial("certificate-chain.pem", "private-key.pem", "my-password".toCharArray());
var trustManager = PemUtils.loadTrustMaterial("some-trusted-certificate.pem");
var sslFactory = SSLFactory.builder()
.withIdentityMaterial(keyManager)
.withTrustMaterial(trustManager)
.build();
var httpClient = new OkHttpClient.Builder()
.sslSocketFactory(sslFactory.getSslSocketFactory(), sslFactory.getTrustManager().orElseThrow())
.hostnameVerifier(sslFactory.getHostnameVerifier())
.build();
I hope this answers all off your questions, please let me know if you have additional questions
{
"result": {
"store00293": "-----BEGIN RSA PRIVATE KEY-----\nMIIEogIBAAKCAQEAzydv5mr730A2S0rjp/zs3EsuZnJuegS+d4+0plTjlS+x5kol\nsE5NaEGYhBUogQtWGysbXXfeyUK91rhQuBoEUW1dVT5ni75F4fRiNUwTr3KVFI+X\nqOuLw7F2hCBG+CfnvmlOe5bAEPlcRm9a3ov2+uOO+p1dzc8c5a+A9L1/ItWDUawf\nhe4c2jCjESls3NmKwOEdWiPWlX7GBiqCT82IWjtfarP9IeslIdxM/A+VeB3AHnEeqqmzY2XNyuMJ4+xi7IC/\nxIejxUOQYToqPz+zmoFhYwBWJPKwROadOl44KXn1a+ky1gpiNAjlhsL0xSU4Wnu5\n0TBXFl/6kbMNCwAl4VghLKAa9yv7SQsNL3SdNXLv=\n-----END RSA PRIVATE KEY-----\n",
"store00293.pem": "Bag Attributes\n localKeyID: FC 0F 42 B6 45 20 F3 F3 71 0C 10 61 C7 94 6D 00 70 B5 9C 98 \nsubject=/C=US/L=New Jersey/O=IDT/OU=ABC Solutions/CN=store 00293\nissuer=/C=US/L=New Jersey/O=ABC Store/OU=ABC Solutions/CN=POS API\n-----BEGIN CERTIFICATE-----\nMIIDfjCCAmagAwIBAgIIPRx0pM+8kyowDQYJKoZIhvcNAQELBQAwaTELMAkGA1UE\nBhMCVVMxEzARBgNVBAcTCk5ldyBKZXJzZXkxGDAWBgNVBAoTD0lEVCBMTC3N0b3JlIDAwMjkzMIIBIjANBgkqhkiG\n9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3KBpzdGm3TRrmPbf7Wublvi8bZ3p4gtT+FAe\nI8b2MaYI4y2Wv=\n-----END CERTIFICATE-----\nBag Attributes\n localKeyID: FC 0F 42 B6 45 20 F3 F3 71 0C 10 61 C7 94 6D 00 70 B5 9C 98 \nKey Attributes: <No Attributes>\n-----BEGIN ENCRYPTED PRIVATE KEY-----\nMIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQImMLPuq28tAoCAggA\nMBQGCCqGSIb3DQMHBAhGAWJKXxF0HwSCBMg3mUMH2W+AlqoXXyBs5TAvnWday3XF\neN/qX7M/stO4AQgqkB5ZH193NPRfsrvK8fqKRrNlBk0SG3ZKm3Jnb6/XbmkvLGfo\nDxXLU90RaSflG/4/1wE6Hmugfsj8XSs3WZDLa0GUEcCzVWvhoho2T2GSV5/rh0h8\nuXlNzZElBfzsUEFdS5zKDJL0DcJX6g5R6Ke+F52gNCkf9/sj7w8/PCbXlJSDBTnA\n52ponOkwBIbxRIsGHaS4Smn0AZOs93BQfrfGl+o9b7q+qsqlMjtDOpT0mWpxbVvZ\nBl9DX/tp7ESQBaoh2eJf+3z9m78kokCC/RkpdpV9lOZogE4NE3tesiF6WWVbmE1m\nEsM8LnBWVk1CN=\n-----END ENCRYPTED PRIVATE KEY-----\n",
"store00293.pub": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPJ2/mavvfQDZLSuOn/OzcSy5mcm56BL53j7SmVOOVL7HmSiWwTk1oQZiEFSiBC1YbKxtdd97JQr3WuFC4GgRRbV1VPmeLvkXh9GI1TBOvcpUUj5eo64vDsXaEIEb4J+e+aU57lsAQ+VxGb1rei/b64476nV3Nzxzlr4D0vX8i1YNRrB+F7hzaMKMRKWzc2YrA4R1aI9aVfsYGKoJPzYhaO19qs/0h6yUh3Ez8D5V44KQJqkT for ABC Store\n",
"store00293-cert.pub": "ssh-rsa-cert-v01@openssh.com AAAAHHNzaC1yc2EtY2VydC12MDFAb3BlbnNzaC5jb20AAAAgevB86dIf2NZYTl8SMeiHawnpcAGAMNWMe208kqWzvT8AAAADAQABAAABAQDPJ2/mavvfQDZLSuuCe4ZZNhlCo44KQJqkTDkoC6Hc6iAhvmEm3Wbc0AMILyT4mXvidbCn2y+o55gZzHmYBY1wCF5AAAAAAAAAAAAAAABAAAAEy90bXAvc3RvcmUwMDI5M19uZXcAAAANAAAACXR1bm5lbGVycwAAAABf+uyPAAAAAHLG8LsAAAAAAAAAggAAABVwZXJtaXQtWDExLWZvcndhcmRpbmcAAAAAAAAAF3Blcm1pdC1hZ2VudC1mb3J3YXJkaW5nAAAAAAAAABZw for ABC Store\n",
}
}
<dependency>
<groupId>io.github.hakky54</groupId>
<artifactId>sslcontext-kickstart-for-pem</artifactId>
<version>7.4.1</version>
</dependency>
InputStream keyStream = new ByteArrayInputStream(kesStr.getBytes());
X509ExtendedKeyManager keyManager = PemUtils.loadIdentityMaterial(keyStream);
SSLFactory sslFactory = SSLFactory.builder().withIdentityMaterial(keyManager).build();
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-ri</artifactId>
<version>2.3.2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>16.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-csv -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.opencsv/opencsv -->
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.5.2</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>mvc-auth-commons</artifactId>
<version>1.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.6.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>smtp</artifactId>
<version>1.6.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.paho/org.eclipse.paho.client.mqttv3 -->
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>2019-08-01T04-03-44-e9b53f6</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.smartbear.soapui</groupId>
<artifactId>soapui</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>io.github.hakky54</groupId>
<artifactId>sslcontext-kickstart-for-pem</artifactId>
<version>7.4.1</version>
</dependency>
<dependency>
<groupId>eviware</groupId>
<artifactId>soap-xmlbeans</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>eviware</groupId>
<artifactId>policy-xmlbeans</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-ssl</groupId>
<artifactId>not-yet-commons-ssl</artifactId>
<version>0.3.11</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j -->
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-javamail_1.4_spec</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.fife.ui/rsyntaxtextarea -->
<dependency>
<groupId>org.fife.ui</groupId>
<artifactId>rsyntaxtextarea</artifactId>
<version>2.0.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.8.0</version>
<exclusions>
<exclusion>
<artifactId>xml-apis</artifactId>
<groupId>xml-apis</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>com.sun.xsom</groupId>
<artifactId>xsom</artifactId>
<version>20110809</version>
</dependency>
<!-- End of SoapUI -->
<!-- sftp lib -->
<dependency>
<groupId>com.hierynomus</groupId>
<artifactId>sshj</artifactId>
<version>0.32.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 -->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.xlate/staedi -->
<dependency>
<groupId>io.xlate</groupId>
<artifactId>staedi</artifactId>
<version>1.19.0</version>
</dependency>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1.4</version>
</dependency>
not-yet-commons-ssl
which relies on an older version of BouncyCastle.
Hi @Hakky54! First of all, love the library. It's really solved a lot of my SSL woes.
I have a downstream service that recently moved behind HTTPS, but hasn't quite nailed down their certificate situation (mis-configured SANs or some such). In the meantime I'm trying to trust the IP directly, but the validator I've configured never seems to get triggered as no messages are logged.I'm sure I've just misunderstood how HTTPS works - should I not be expecting HTTPS requests to go through the checkTrustedServer
trust manager methods during the SSL handshake?
Here's what I'm doing to configure our SSL connections:
private SSLFactory getSslFactory(KeyStore keystore) throws KeyStoreException {
SSLFactory.Builder sslBuilder = SSLFactory.builder()
.withDefaultTrustMaterial()
.withTrustEnhancer((ChainAndAuthTypeWithSocketValidator) (chainCerts, authType, socket) -> {
logger.info("socket remote address = {}", socket.getRemoteSocketAddress());
return socket.getRemoteSocketAddress().toString().contains("1.1.1.1"); // <-- trying to trust IP explicitly
});
keystore.aliases().asIterator().forEachRemaining(alias -> {
Certificate cert = keystore.getCertificate(alias); // <-- loading their cert, still get PKIX errors referencing no matching SAN for IP
sslBuilder.withTrustMaterial(cert);
logger.info("Loaded certificate '{}'", alias);
});
return sslBuilder.build();
}
Or that's my misunderstanding - would HTTPS only ever hit the SSLEngine validator?
The JDK SSL Engine implementation has an algorithm to decide which method should be called from the Trustmanager. That is a black box for me, but i think it is worth to investigate that part so I will be able to answer your question 😁 So I don't know which of the three methods will be called... But I am pretty sure that 99% of the cases it would be ChainAndAuthTypeWithSSLEngineValidator
As the Socket, which is being used by ChainAndAuthTypeWithSocketValidator
, is the older and SSLEngine is the newer one.