You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2019/10/12 07:39:28 UTC

[httpcomponents-core] 01/01: Use PKCS12 keystore type instead of deprecated JKS for TLS and integration tests

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch ssl-tests
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit e4357dadfc4cc1dcd504ad29ebdb2ecb104a274a
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sat Oct 12 09:39:07 2019 +0200

    Use PKCS12 keystore type instead of deprecated JKS for TLS and integration tests
---
 .../apache/hc/core5/testing/SSLTestContexts.java   |   6 +-
 httpcore5-testing/src/test/resources/test.keystore | Bin 2241 -> 0 bytes
 httpcore5-testing/src/test/resources/test.p12      | Bin 0 -> 2623 bytes
 .../apache/hc/core5/ssl/TestSSLContextBuilder.java |  69 +++++++++------------
 httpcore5/src/test/resources/ca.keystore           | Bin 2332 -> 0 bytes
 httpcore5/src/test/resources/ca.p12                | Bin 0 -> 2663 bytes
 httpcore5/src/test/resources/test-client.keystore  | Bin 7654 -> 0 bytes
 httpcore5/src/test/resources/test-client.p12       | Bin 0 -> 8845 bytes
 .../src/test/resources/test-keypasswd.keystore     | Bin 2242 -> 0 bytes
 httpcore5/src/test/resources/test-keypasswd.p12    | Bin 0 -> 2623 bytes
 httpcore5/src/test/resources/test-server.keystore  | Bin 4345 -> 0 bytes
 httpcore5/src/test/resources/test-server.p12       | Bin 0 -> 5071 bytes
 httpcore5/src/test/resources/test-ssl.txt          |  40 ++++++------
 httpcore5/src/test/resources/test.keystore         | Bin 2242 -> 0 bytes
 httpcore5/src/test/resources/test.p12              | Bin 0 -> 2623 bytes
 15 files changed, 55 insertions(+), 60 deletions(-)

diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/SSLTestContexts.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/SSLTestContexts.java
index 6668f7a..52e1649 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/SSLTestContexts.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/SSLTestContexts.java
@@ -36,18 +36,20 @@ import org.apache.hc.core5.ssl.SSLContextBuilder;
 public final class SSLTestContexts {
 
     public static SSLContext createServerSSLContext() throws Exception {
-        final URL keyStoreURL = SSLTestContexts.class.getResource("/test.keystore");
+        final URL keyStoreURL = SSLTestContexts.class.getResource("/test.p12");
         final String storePassword = "nopassword";
         return SSLContextBuilder.create()
+                .setKeyStoreType("pkcs12")
                 .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
                 .loadKeyMaterial(keyStoreURL, storePassword.toCharArray(), storePassword.toCharArray())
                 .build();
     }
 
     public static SSLContext createClientSSLContext() throws Exception {
-        final URL keyStoreURL = SSLTestContexts.class.getResource("/test.keystore");
+        final URL keyStoreURL = SSLTestContexts.class.getResource("/test.p12");
         final String storePassword = "nopassword";
         return SSLContextBuilder.create()
+                .setKeyStoreType("pkcs12")
                 .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
                 .build();
     }
diff --git a/httpcore5-testing/src/test/resources/test.keystore b/httpcore5-testing/src/test/resources/test.keystore
deleted file mode 100644
index 4fba6a6..0000000
Binary files a/httpcore5-testing/src/test/resources/test.keystore and /dev/null differ
diff --git a/httpcore5-testing/src/test/resources/test.p12 b/httpcore5-testing/src/test/resources/test.p12
new file mode 100644
index 0000000..17f49ce
Binary files /dev/null and b/httpcore5-testing/src/test/resources/test.p12 differ
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/ssl/TestSSLContextBuilder.java b/httpcore5/src/test/java/org/apache/hc/core5/ssl/TestSSLContextBuilder.java
index e3274f2..dd563a5 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/ssl/TestSSLContextBuilder.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/ssl/TestSSLContextBuilder.java
@@ -62,12 +62,14 @@ import javax.net.ssl.SSLPeerUnverifiedException;
 import javax.net.ssl.SSLServerSocket;
 import javax.net.ssl.SSLSession;
 import javax.net.ssl.SSLSocket;
-import javax.net.ssl.SSLSocketFactory;
 import javax.net.ssl.TrustManagerFactory;
 
+import org.apache.hc.core5.util.ReflectionUtils;
 import org.apache.hc.core5.util.Timeout;
 import org.junit.After;
 import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.BeforeClass;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -83,6 +85,11 @@ public class TestSSLContextBuilder {
         return System.getProperty("os.name").contains("Windows");
     }
 
+    @BeforeClass
+    public static void determineJavaVersion() {
+        Assume.assumeTrue("Java version must be 8 or greater", ReflectionUtils.determineJRELevel() >= 8);
+    }
+
     @Rule
     public ExpectedException thrown = ExpectedException.none();
 
@@ -154,7 +161,7 @@ public class TestSSLContextBuilder {
 
     @Test(expected=NoSuchAlgorithmException.class)
     public void testBuildNoSuchKeyManagerFactoryAlgorithm() throws Exception {
-        final URL resource1 = getResource("/test-keypasswd.keystore");
+        final URL resource1 = getResource("/test-keypasswd.p12");
         final String storePassword = "nopassword";
         final String keyPassword = "password";
         SSLContextBuilder.create()
@@ -163,9 +170,9 @@ public class TestSSLContextBuilder {
                 .build();
     }
 
-    @Test(expected=KeyStoreException.class)
+    @Test(expected= KeyStoreException.class)
     public void testBuildNoSuchKeyStoreType() throws Exception {
-        final URL resource1 = getResource("/test-keypasswd.keystore");
+        final URL resource1 = getResource("/test-keypasswd.p12");
         final String storePassword = "nopassword";
         final String keyPassword = "password";
         SSLContextBuilder.create()
@@ -176,7 +183,7 @@ public class TestSSLContextBuilder {
 
     @Test(expected=NoSuchAlgorithmException.class)
     public void testBuildNoSuchTrustManagerFactoryAlgorithm() throws Exception {
-        final URL resource1 = getResource("/test-keypasswd.keystore");
+        final URL resource1 = getResource("/test-keypasswd.p12");
         final String storePassword = "nopassword";
         SSLContextBuilder.create()
                 .setTrustManagerFactoryAlgorithm(" BAD ")
@@ -186,7 +193,7 @@ public class TestSSLContextBuilder {
 
     @Test
     public void testBuildWithProvider() throws Exception {
-        final URL resource1 = getResource("/test-server.keystore");
+        final URL resource1 = getResource("/test-server.p12");
         final String storePassword = "nopassword";
         final String keyPassword = "nopassword";
         final SSLContext sslContext=SSLContextBuilder.create()
@@ -198,7 +205,7 @@ public class TestSSLContextBuilder {
 
     @Test
     public void testBuildWithProviderName() throws Exception {
-        final URL resource1 = getResource("/test-server.keystore");
+        final URL resource1 = getResource("/test-server.p12");
         final String storePassword = "nopassword";
         final String keyPassword = "nopassword";
         final SSLContext sslContext=SSLContextBuilder.create()
@@ -209,25 +216,11 @@ public class TestSSLContextBuilder {
     }
 
     @Test
-    public void testKeyWithAlternatePassword() throws Exception {
-        final URL resource1 = getResource("/test-keypasswd.keystore");
-        final String storePassword = "nopassword";
-        final String keyPassword = "password";
-        final SSLContext sslContext = SSLContextBuilder.create()
-                .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray())
-                .loadTrustMaterial(resource1, storePassword.toCharArray())
-                .build();
-        Assert.assertNotNull(sslContext);
-        final SSLSocketFactory socketFactory = sslContext.getSocketFactory();
-        Assert.assertNotNull(socketFactory);
-    }
-
-    @Test
     public void testKeyWithAlternatePasswordInvalid() throws Exception {
 
         thrown.expect(UnrecoverableKeyException.class);
 
-        final URL resource1 = getResource("/test-keypasswd.keystore");
+        final URL resource1 = getResource("/test-keypasswd.p12");
         final String storePassword = "nopassword";
         final String keyPassword = "!password";
         SSLContextBuilder.create()
@@ -238,7 +231,7 @@ public class TestSSLContextBuilder {
 
     @Test
     public void testSSLHandshakeServerTrusted() throws Exception {
-        final URL resource1 = getResource("/test.keystore");
+        final URL resource1 = getResource("/test.p12");
         final String storePassword = "nopassword";
         final String keyPassword = "nopassword";
         final SSLContext serverSslContext = SSLContextBuilder.create()
@@ -286,14 +279,14 @@ public class TestSSLContextBuilder {
     public void testSSLHandshakeServerNotTrusted() throws Exception {
         thrown.expect(IOException.class);
 
-        final URL resource1 = getResource("/test-server.keystore");
+        final URL resource1 = getResource("/test-server.p12");
         final String storePassword = "nopassword";
         final String keyPassword = "nopassword";
         final SSLContext serverSslContext = SSLContextBuilder.create()
                 .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray())
                 .build();
         Assert.assertNotNull(serverSslContext);
-        final URL resource2 = getResource("/test.keystore");
+        final URL resource2 = getResource("/test.p12");
         final SSLContext clientSslContext = SSLContextBuilder.create()
                 .loadTrustMaterial(resource2, storePassword.toCharArray())
                 .build();
@@ -324,7 +317,7 @@ public class TestSSLContextBuilder {
 
     @Test
     public void testSSLHandshakeServerCustomTrustStrategy() throws Exception {
-        final URL resource1 = getResource("/test-server.keystore");
+        final URL resource1 = getResource("/test-server.p12");
         final String storePassword = "nopassword";
         final String keyPassword = "nopassword";
         final SSLContext serverSslContext = SSLContextBuilder.create()
@@ -403,14 +396,14 @@ public class TestSSLContextBuilder {
 
     @Test
     public void testSSLHandshakeClientUnauthenticated() throws Exception {
-        final URL resource1 = getResource("/test-server.keystore");
+        final URL resource1 = getResource("/test-server.p12");
         final String storePassword = "nopassword";
         final String keyPassword = "nopassword";
         final SSLContext serverSslContext = SSLContextBuilder.create()
                 .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray())
                 .build();
         Assert.assertNotNull(serverSslContext);
-        final URL resource2 = getResource("/test-client.keystore");
+        final URL resource2 = getResource("/test-client.p12");
         final SSLContext clientSslContext = SSLContextBuilder.create()
                 .loadTrustMaterial(resource2, storePassword.toCharArray())
                 .build();
@@ -460,14 +453,14 @@ public class TestSSLContextBuilder {
     public void testSSLHandshakeClientUnauthenticatedError() throws Exception {
         thrown.expect(IOException.class);
 
-        final URL resource1 = getResource("/test-server.keystore");
+        final URL resource1 = getResource("/test-server.p12");
         final String storePassword = "nopassword";
         final String keyPassword = "nopassword";
         final SSLContext serverSslContext = SSLContextBuilder.create()
                 .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray())
                 .build();
         Assert.assertNotNull(serverSslContext);
-        final URL resource2 = getResource("/test-client.keystore");
+        final URL resource2 = getResource("/test-client.p12");
         final SSLContext clientSslContext = SSLContextBuilder.create()
                 .loadTrustMaterial(resource2, storePassword.toCharArray())
                 .build();
@@ -502,7 +495,7 @@ public class TestSSLContextBuilder {
 
     @Test
     public void testSSLHandshakeClientAuthenticated() throws Exception {
-        final URL resource1 = getResource("/test-server.keystore");
+        final URL resource1 = getResource("/test-server.p12");
         final String storePassword = "nopassword";
         final String keyPassword = "nopassword";
         final SSLContext serverSslContext = SSLContextBuilder.create()
@@ -510,7 +503,7 @@ public class TestSSLContextBuilder {
                 .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray())
                 .build();
         Assert.assertNotNull(serverSslContext);
-        final URL resource2 = getResource("/test-client.keystore");
+        final URL resource2 = getResource("/test-client.p12");
         final SSLContext clientSslContext = SSLContextBuilder.create()
                 .loadTrustMaterial(resource2, storePassword.toCharArray())
                 .loadKeyMaterial(resource2, storePassword.toCharArray(), storePassword.toCharArray())
@@ -554,7 +547,7 @@ public class TestSSLContextBuilder {
 
     @Test
     public void testSSLHandshakeClientAuthenticatedPrivateKeyStrategy() throws Exception {
-        final URL resource1 = getResource("/test-server.keystore");
+        final URL resource1 = getResource("/test-server.p12");
         final String storePassword = "nopassword";
         final String keyPassword = "nopassword";
         final SSLContext serverSslContext = SSLContextBuilder.create()
@@ -571,7 +564,7 @@ public class TestSSLContextBuilder {
             }
         };
 
-        final URL resource2 = getResource("/test-client.keystore");
+        final URL resource2 = getResource("/test-client.p12");
         final SSLContext clientSslContext = SSLContextBuilder.create()
                 .loadTrustMaterial(resource2, storePassword.toCharArray())
                 .loadKeyMaterial(resource2, storePassword.toCharArray(), storePassword.toCharArray(), privateKeyStrategy)
@@ -623,14 +616,14 @@ public class TestSSLContextBuilder {
             thrown.expect(SSLHandshakeException.class);
         }
 
-        final URL resource1 = getResource("/test-server.keystore");
+        final URL resource1 = getResource("/test-server.p12");
         final String storePassword = "nopassword";
         final String keyPassword = "nopassword";
         final SSLContext serverSslContext = SSLContextBuilder.create()
                 .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray())
                 .build();
         Assert.assertNotNull(serverSslContext);
-        final URL resource2 = getResource("/test-client.keystore");
+        final URL resource2 = getResource("/test-client.p12");
         final SSLContext clientSslContext = SSLContextBuilder.create()
                 .loadTrustMaterial(resource2, storePassword.toCharArray())
                 .build();
@@ -674,14 +667,14 @@ public class TestSSLContextBuilder {
             thrown.expect(SSLException.class);
         }
 
-        final URL resource1 = getResource("/test-server.keystore");
+        final URL resource1 = getResource("/test-server.p12");
         final String storePassword = "nopassword";
         final String keyPassword = "nopassword";
         final SSLContext serverSslContext = SSLContextBuilder.create()
                 .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray())
                 .build();
         Assert.assertNotNull(serverSslContext);
-        final URL resource2 = getResource("/test-client.keystore");
+        final URL resource2 = getResource("/test-client.p12");
         final SSLContext clientSslContext = SSLContextBuilder.create()
                 .loadTrustMaterial(resource2, storePassword.toCharArray())
                 .build();
diff --git a/httpcore5/src/test/resources/ca.keystore b/httpcore5/src/test/resources/ca.keystore
deleted file mode 100644
index 2c93a60..0000000
Binary files a/httpcore5/src/test/resources/ca.keystore and /dev/null differ
diff --git a/httpcore5/src/test/resources/ca.p12 b/httpcore5/src/test/resources/ca.p12
new file mode 100644
index 0000000..ab6f6aa
Binary files /dev/null and b/httpcore5/src/test/resources/ca.p12 differ
diff --git a/httpcore5/src/test/resources/test-client.keystore b/httpcore5/src/test/resources/test-client.keystore
deleted file mode 100644
index 261ebdb..0000000
Binary files a/httpcore5/src/test/resources/test-client.keystore and /dev/null differ
diff --git a/httpcore5/src/test/resources/test-client.p12 b/httpcore5/src/test/resources/test-client.p12
new file mode 100644
index 0000000..f1170f5
Binary files /dev/null and b/httpcore5/src/test/resources/test-client.p12 differ
diff --git a/httpcore5/src/test/resources/test-keypasswd.keystore b/httpcore5/src/test/resources/test-keypasswd.keystore
deleted file mode 100644
index b063cb3..0000000
Binary files a/httpcore5/src/test/resources/test-keypasswd.keystore and /dev/null differ
diff --git a/httpcore5/src/test/resources/test-keypasswd.p12 b/httpcore5/src/test/resources/test-keypasswd.p12
new file mode 100644
index 0000000..1c7cb1f
Binary files /dev/null and b/httpcore5/src/test/resources/test-keypasswd.p12 differ
diff --git a/httpcore5/src/test/resources/test-server.keystore b/httpcore5/src/test/resources/test-server.keystore
deleted file mode 100644
index f9acf64..0000000
Binary files a/httpcore5/src/test/resources/test-server.keystore and /dev/null differ
diff --git a/httpcore5/src/test/resources/test-server.p12 b/httpcore5/src/test/resources/test-server.p12
new file mode 100644
index 0000000..c4cca0e
Binary files /dev/null and b/httpcore5/src/test/resources/test-server.p12 differ
diff --git a/httpcore5/src/test/resources/test-ssl.txt b/httpcore5/src/test/resources/test-ssl.txt
index 1130ea3..ae90463 100644
--- a/httpcore5/src/test/resources/test-ssl.txt
+++ b/httpcore5/src/test/resources/test-ssl.txt
@@ -26,7 +26,7 @@
 
 ---
 keytool -genkey \
-  -keystore test.keystore -storepass nopassword \
+  -keystore test.12 -storepass nopassword \
   -keyalg RSA -keysize 2048 \
   -alias simple-http-server \
   -validity 100000 \
@@ -38,8 +38,8 @@ keytool -genkey \
 
 ---
 keytool -genkey \
-  -keystore test-keypasswd.keystore -storepass nopassword \
-  -keyalg RSA -keysize 2048 -keypass password \
+  -keystore test-keypasswd.p12 -storepass nopassword \
+  -keyalg RSA -keysize 2048 \
   -alias simple-http-server \
   -validity 100000 \
   -dname "CN=localhost, OU=Apache HttpComponents, O=Apache Software Foundation" \
@@ -50,8 +50,8 @@ keytool -genkey \
 
 ---
 keytool -genkeypair \
-  -keystore ca.keystore -storepass nopassword \
-  -keyalg RSA -keysize 2048 -keypass password \
+  -keystore ca.p12 -storepass nopassword \
+  -keyalg RSA -keysize 2048 \
   -alias ca \
   -validity 100000 \
   -dname "EMAILADDRESS=dev@hc.apache.org, CN=Test CA, OU=HttpComponents Project, O=Apache Software Foundation" \
@@ -64,7 +64,7 @@ keytool -genkeypair \
 
 ---
 keytool -export \
-  -keystore ca.keystore -storepass nopassword -keypass password \
+  -keystore ca.p12 -storepass nopassword \
   -alias ca \
   -file test-ca.crt \
   -rfc
@@ -74,7 +74,7 @@ keytool -export \
 
 ---
 keytool -genkeypair \
-  -keystore test-server.keystore -storepass nopassword \
+  -keystore test-server.p12 -storepass nopassword \
   -keyalg RSA -keysize 2048 \
   -alias server \
   -validity 100000 \
@@ -85,7 +85,7 @@ keytool -genkeypair \
 
 ---
 keytool -certreq \
-  -keystore test-server.keystore -storepass nopassword \
+  -keystore test-server.p12 -storepass nopassword \
   -alias server \
   -file server.csr
 ---
@@ -94,7 +94,7 @@ keytool -certreq \
 
 ---
 keytool -gencert \
-  -keystore ca.keystore -storepass nopassword -keypass password \
+  -keystore ca.p12 -storepass nopassword \
   -alias ca \
   -validity 100000 \
   -infile server.csr \
@@ -109,12 +109,12 @@ keytool -gencert \
 
 ---
 keytool -importcert \
-  -keystore test-server.keystore -storepass nopassword \
+  -keystore test-server.p12 -storepass nopassword \
   -file test-ca.crt \
   -alias caroot
 ---
 keytool -importcert \
-  -keystore test-server.keystore -storepass nopassword \
+  -keystore test-server.p12 -storepass nopassword \
   -file server.crt \
   -alias server
 ---
@@ -123,14 +123,14 @@ keytool -importcert \
 
 ---
 keytool -genkeypair \
-  -keystore test-client.keystore -storepass nopassword \
+  -keystore test-client.p12 -storepass nopassword \
   -keyalg RSA -keysize 2048 \
   -alias client1 \
   -validity 100000 \
   -dname "CN=Test Client 1, OU=HttpComponents Project, O=Apache Software Foundation"
 ---
 keytool -genkeypair \
-  -keystore test-client.keystore -storepass nopassword \
+  -keystore test-client.p12 -storepass nopassword \
   -keyalg RSA -keysize 2048 \
   -alias client2 \
   -validity 100000 \
@@ -141,12 +141,12 @@ keytool -genkeypair \
 
 ---
 keytool -certreq \
-  -keystore test-client.keystore -storepass nopassword \
+  -keystore test-client.p12 -storepass nopassword \
   -alias client1 \
   -file client1.csr
 ---
 keytool -certreq \
-  -keystore test-client.keystore -storepass nopassword \
+  -keystore test-client.p12 -storepass nopassword \
   -alias client2 \
   -file client2.csr
 ---
@@ -155,7 +155,7 @@ keytool -certreq \
 
 ---
 keytool -gencert \
-  -keystore ca.keystore -storepass nopassword -keypass password \
+  -keystore ca.p12 -storepass nopassword \
   -alias ca \
   -validity 100000 \
   -infile client1.csr \
@@ -165,7 +165,7 @@ keytool -gencert \
   -rfc
 ---
 keytool -gencert \
-  -keystore ca.keystore -storepass nopassword -keypass password \
+  -keystore ca.p12 -storepass nopassword \
   -alias ca \
   -validity 100000 \
   -infile client2.csr \
@@ -179,17 +179,17 @@ keytool -gencert \
 
 ---
 keytool -importcert \
-  -keystore test-client.keystore -storepass nopassword \
+  -keystore test-client.p12 -storepass nopassword \
   -file test-ca.crt \
   -alias caroot
 ---
 keytool -importcert \
-  -keystore test-client.keystore -storepass nopassword \
+  -keystore test-client.p12 -storepass nopassword \
   -file client1.crt \
   -alias client1
 ---
 keytool -importcert \
-  -keystore test-client.keystore -storepass nopassword \
+  -keystore test-client.p12 -storepass nopassword \
   -file client2.crt \
   -alias client2
 ---
diff --git a/httpcore5/src/test/resources/test.keystore b/httpcore5/src/test/resources/test.keystore
deleted file mode 100644
index 5010669..0000000
Binary files a/httpcore5/src/test/resources/test.keystore and /dev/null differ
diff --git a/httpcore5/src/test/resources/test.p12 b/httpcore5/src/test/resources/test.p12
new file mode 100644
index 0000000..17f49ce
Binary files /dev/null and b/httpcore5/src/test/resources/test.p12 differ