Blame SOURCES/0003-Add-optional-test-case-against-badssl.com.patch

1beea6
From 7b4c0fa04f5e4469fc8bc442c9f12f975c5e1610 Mon Sep 17 00:00:00 2001
1beea6
From: Alexander Scheel <ascheel@redhat.com>
1beea6
Date: Wed, 28 Aug 2019 09:23:41 -0400
1beea6
Subject: [PATCH 3/3] Add optional test case against badssl.com
1beea6
1beea6
badssl.com maintains a number of subdomains with valid and invalid TLS
1beea6
configurations. A number of these test certificates which fail in
1beea6
certain scenarios (revoked, expired, etc). Add a test runner which
1beea6
validates SSLSocket's implementation against badssl.com.
1beea6
1beea6
Signed-off-by: Alexander Scheel <ascheel@redhat.com>
1beea6
---
1beea6
 org/mozilla/jss/tests/BadSSL.java | 208 ++++++++++++++++++++++++++++++
1beea6
 1 file changed, 208 insertions(+)
1beea6
 create mode 100644 org/mozilla/jss/tests/BadSSL.java
1beea6
1beea6
diff --git a/org/mozilla/jss/tests/BadSSL.java b/org/mozilla/jss/tests/BadSSL.java
1beea6
new file mode 100644
1beea6
index 00000000..60bfe820
1beea6
--- /dev/null
1beea6
+++ b/org/mozilla/jss/tests/BadSSL.java
1beea6
@@ -0,0 +1,208 @@
1beea6
+package org.mozilla.jss.tests;
1beea6
+
1beea6
+import org.mozilla.jss.CryptoManager;
1beea6
+
1beea6
+import org.mozilla.jss.ssl.SSLSocket;
1beea6
+import org.mozilla.jss.ssl.SSLSocketException;
1beea6
+
1beea6
+import org.mozilla.jss.util.NativeErrcodes;
1beea6
+
1beea6
+/**
1beea6
+ * The BadSSL test case maintains an internal mapping from badssl.com
1beea6
+ * subdomains to expected exceptions and validates they occur.
1beea6
+ *
1beea6
+ * Since badssl.com offers no guaranteed SLA or availability, we likely
1beea6
+ * shouldn't add this site to automated tests.
1beea6
+ */
1beea6
+
1beea6
+public class BadSSL {
1beea6
+    public static void main(String[] args) throws Exception {
1beea6
+        boolean ocsp = false;
1beea6
+
1beea6
+        if (args.length < 1) {
1beea6
+            System.out.println("Usage: BadSSL nssdb [LEAF_AND_CHAIN]");
1beea6
+            return;
1beea6
+        }
1beea6
+
1beea6
+        if (args.length >= 2 && args[1].equals("LEAF_AND_CHAIN")) {
1beea6
+            System.out.println("Enabling leaf and chain policy...");
1beea6
+            ocsp = true;
1beea6
+        }
1beea6
+
1beea6
+        CryptoManager.initialize(args[0]);
1beea6
+        CryptoManager cm = CryptoManager.getInstance();
1beea6
+
1beea6
+        if (ocsp) {
1beea6
+            cm.setOCSPPolicy(CryptoManager.OCSPPolicy.LEAF_AND_CHAIN);
1beea6
+        }
1beea6
+
1beea6
+
1beea6
+        // Test cases which should fail due to various certificate errors.
1beea6
+        testExpired();
1beea6
+        testWrongHost();
1beea6
+        testSelfSigned();
1beea6
+        testUntrustedRoot();
1beea6
+
1beea6
+        // The following test cases depend on crypto-policies or local NSS
1beea6
+        // configuration.
1beea6
+        testSHA1();
1beea6
+        testRC4MD5();
1beea6
+        testRC4();
1beea6
+        test3DES();
1beea6
+        testNULL();
1beea6
+
1beea6
+        // The following test cases depend on OCSP being enabled.
1beea6
+        if (ocsp) {
1beea6
+            testRevoked();
1beea6
+        }
1beea6
+
1beea6
+        // Test cases which should pass given the correct root certs.
1beea6
+        testSHA256();
1beea6
+        testSHA384();
1beea6
+        testSHA512();
1beea6
+
1beea6
+        testECC256();
1beea6
+        testECC384();
1beea6
+
1beea6
+        testRSA2048();
1beea6
+        testRSA4096();
1beea6
+        testRSA8192();
1beea6
+
1beea6
+        testExtendedValidation();
1beea6
+    }
1beea6
+
1beea6
+    /* Test cases whose handshakes should fail below. */
1beea6
+
1beea6
+    public static void testExpired() throws Exception {
1beea6
+        testHelper("expired.badssl.com", 443, new String[]{ "(-8181)", "has expired" });
1beea6
+    }
1beea6
+
1beea6
+    public static void testWrongHost() throws Exception {
1beea6
+        testHelper("wrong.host.badssl.com", 443, new String[]{ "(-12276)", "domain name does not match" });
1beea6
+    }
1beea6
+
1beea6
+    public static void testSelfSigned() throws Exception {
1beea6
+        testHelper("self-signed.badssl.com", 443, new String[]{ "(-8101)", "(-8156)", "type not approved", "issuer certificate is invalid" });
1beea6
+    }
1beea6
+
1beea6
+    public static void testUntrustedRoot() throws Exception {
1beea6
+        testHelper("untrusted-root.badssl.com", 443, new String[]{ "(-8172)", "certificate issuer has been marked as not trusted" });
1beea6
+    }
1beea6
+
1beea6
+    public static void testRevoked() throws Exception {
1beea6
+        testHelper("revoked.badssl.com", 443, new String[]{ "(-8180)", "has been revoked" });
1beea6
+    }
1beea6
+
1beea6
+    public static void testSHA1() throws Exception {
1beea6
+        testHelper("sha1-intermediate.badssl.com", 443, new String[] { "(-12286)", "Cannot communicate securely" });
1beea6
+    }
1beea6
+
1beea6
+    public static void testRC4MD5() throws Exception {
1beea6
+        testHelper("rc4-md5.badssl.com", 443, new String[] { "(-12286)", "Cannot communicate securely" });
1beea6
+    }
1beea6
+
1beea6
+    public static void testRC4() throws Exception {
1beea6
+        testHelper("rc4.badssl.com", 443, new String[] { "(-12286)", "Cannot communicate securely" });
1beea6
+    }
1beea6
+
1beea6
+    public static void test3DES() throws Exception {
1beea6
+        testHelper("3des.badssl.com", 443, new String[] { "(-12286)", "Cannot communicate securely" });
1beea6
+    }
1beea6
+
1beea6
+    public static void testNULL() throws Exception {
1beea6
+        testHelper("null.badssl.com", 443, new String[] { "(-12286)", "Cannot communicate securely" });
1beea6
+    }
1beea6
+
1beea6
+    /* Test cases which should handshake successfully below. */
1beea6
+
1beea6
+    public static void testSHA256() throws Exception {
1beea6
+        testHelper("sha256.badssl.com", 443);
1beea6
+    }
1beea6
+
1beea6
+    public static void testSHA384() throws Exception {
1beea6
+        testHelper("sha384.badssl.com", 443);
1beea6
+    }
1beea6
+
1beea6
+    public static void testSHA512() throws Exception {
1beea6
+        testHelper("sha512.badssl.com", 443);
1beea6
+    }
1beea6
+
1beea6
+    public static void testECC256() throws Exception {
1beea6
+        testHelper("ecc256.badssl.com", 443);
1beea6
+    }
1beea6
+
1beea6
+    public static void testECC384() throws Exception {
1beea6
+        testHelper("ecc384.badssl.com", 443);
1beea6
+    }
1beea6
+
1beea6
+    public static void testRSA2048() throws Exception {
1beea6
+        testHelper("rsa2048.badssl.com", 443);
1beea6
+    }
1beea6
+
1beea6
+    public static void testRSA4096() throws Exception {
1beea6
+        testHelper("rsa4096.badssl.com", 443);
1beea6
+    }
1beea6
+
1beea6
+    public static void testRSA8192() throws Exception {
1beea6
+        testHelper("rsa8192.badssl.com", 443);
1beea6
+    }
1beea6
+
1beea6
+    public static void testExtendedValidation() throws Exception {
1beea6
+        testHelper("extended-validation.badssl.com", 443);
1beea6
+    }
1beea6
+
1beea6
+    /* Test case helpers. */
1beea6
+
1beea6
+    public static void testHelper(String host, int port) throws Exception {
1beea6
+        testSite(host, port);
1beea6
+        System.out.println("\t...ok");
1beea6
+    }
1beea6
+
1beea6
+    public static void testHelper(String host, int port, String[] substrs) throws Exception {
1beea6
+        try {
1beea6
+            testSite(host, port);
1beea6
+        } catch (SSLSocketException sse) {
1beea6
+            String actual = sse.getMessage().toLowerCase();
1beea6
+
1beea6
+            for (String expected : substrs) {
1beea6
+                if (actual.contains(expected.toLowerCase())) {
1beea6
+                    System.out.println("\t...got expected error message.");
1beea6
+                    return;
1beea6
+                }
1beea6
+            }
1beea6
+
1beea6
+            System.err.println("\tUnexpected error message: " + actual);
1beea6
+            throw sse;
1beea6
+        }
1beea6
+
1beea6
+        throw new RuntimeException("Expected to get an exception, but didn't!");
1beea6
+    }
1beea6
+
1beea6
+    public static void testHelper(String host, int port, int[] codes) throws Exception {
1beea6
+        try {
1beea6
+            testSite(host, port);
1beea6
+        } catch (SSLSocketException sse) {
1beea6
+            int actual = sse.getErrcode();
1beea6
+            for (int expected : codes) {
1beea6
+                if (actual == expected) {
1beea6
+                    System.out.println("\t...got expected error code.");
1beea6
+                    return;
1beea6
+                }
1beea6
+            }
1beea6
+
1beea6
+            System.err.println("\tUnexpected error code: " + actual);
1beea6
+            throw sse;
1beea6
+        }
1beea6
+
1beea6
+        throw new RuntimeException("Expected to get an exception, but didn't!");
1beea6
+    }
1beea6
+
1beea6
+    public static void testSite(String host, int port) throws Exception {
1beea6
+        System.out.println("Testing connection to " + host + ":" + port);
1beea6
+        SSLSocket sock = new SSLSocket(host, 443);
1beea6
+        sock.forceHandshake();
1beea6
+        sock.shutdownOutput();
1beea6
+        sock.shutdownInput();
1beea6
+        sock.close();
1beea6
+    }
1beea6
+}
1beea6
-- 
1beea6
2.21.0
1beea6