Blame SOURCES/rh1655466-global_crypto_and_fips.patch

e797a2
diff --git a/src/java.base/share/classes/javopenjdk.orig///security/Security.java openjdk///src/java.base/share/classes/java/security/Security.java
e797a2
--- openjdk.orig/src/java.base/share/classes/java/security/Security.java
e797a2
+++ openjdk/src/java.base/share/classes/java/security/Security.java
e797a2
@@ -196,26 +196,8 @@
e797a2
         if (disableSystemProps == null &&
e797a2
             "true".equalsIgnoreCase(props.getProperty
e797a2
                 ("security.useSystemPropertiesFile"))) {
e797a2
-
e797a2
-            // now load the system file, if it exists, so its values
e797a2
-            // will win if they conflict with the earlier values
e797a2
-            try (BufferedInputStream bis =
e797a2
-                 new BufferedInputStream(new FileInputStream(SYSTEM_PROPERTIES))) {
e797a2
-                props.load(bis);
e797a2
+            if (SystemConfigurator.configure(props)) {
e797a2
                 loadedProps = true;
e797a2
-
e797a2
-                if (sdebug != null) {
e797a2
-                    sdebug.println("reading system security properties file " +
e797a2
-                                   SYSTEM_PROPERTIES);
e797a2
-                    sdebug.println(props.toString());
e797a2
-                }
e797a2
-            } catch (IOException e) {
e797a2
-                if (sdebug != null) {
e797a2
-                    sdebug.println
e797a2
-                        ("unable to load security properties from " +
e797a2
-                         SYSTEM_PROPERTIES);
e797a2
-                    e.printStackTrace();
e797a2
-                }
e797a2
             }
e797a2
         }
e797a2
 
e797a2
diff --git a/src/java.base/share/classes/javopenjdk.orig///security/SystemConfigurator.java openjdk///src/java.base/share/classes/java/security/SystemConfigurator.java
e797a2
new file mode 100644
e797a2
--- /dev/null
e797a2
+++ openjdk/src/java.base/share/classes/java/security/SystemConfigurator.java
e797a2
@@ -0,0 +1,151 @@
e797a2
+/*
e797a2
+ * Copyright (c) 2019, Red Hat, Inc.
e797a2
+ *
e797a2
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e797a2
+ *
e797a2
+ * This code is free software; you can redistribute it and/or modify it
e797a2
+ * under the terms of the GNU General Public License version 2 only, as
e797a2
+ * published by the Free Software Foundation.
e797a2
+ *
e797a2
+ * This code is distributed in the hope that it will be useful, but WITHOUT
e797a2
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e797a2
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e797a2
+ * version 2 for more details (a copy is included in the LICENSE file that
e797a2
+ * accompanied this code).
e797a2
+ *
e797a2
+ * You should have received a copy of the GNU General Public License version
e797a2
+ * 2 along with this work; if not, write to the Free Software Foundation,
e797a2
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e797a2
+ *
e797a2
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e797a2
+ * or visit www.oracle.com if you need additional information or have any
e797a2
+ * questions.
e797a2
+ */
e797a2
+
e797a2
+package java.security;
e797a2
+
e797a2
+import java.io.BufferedInputStream;
e797a2
+import java.io.FileInputStream;
e797a2
+import java.io.IOException;
e797a2
+
e797a2
+import java.nio.file.Files;
e797a2
+import java.nio.file.Path;
e797a2
+
e797a2
+import java.util.Iterator;
e797a2
+import java.util.Map.Entry;
e797a2
+import java.util.Properties;
e797a2
+import java.util.function.Consumer;
e797a2
+import java.util.regex.Matcher;
e797a2
+import java.util.regex.Pattern;
e797a2
+
e797a2
+import sun.security.util.Debug;
e797a2
+
e797a2
+/**
e797a2
+ * Internal class to align OpenJDK with global crypto-policies.
e797a2
+ * Called from java.security.Security class initialization,
e797a2
+ * during startup.
e797a2
+ *
e797a2
+ */
e797a2
+
e797a2
+class SystemConfigurator {
e797a2
+
e797a2
+    private static final Debug sdebug =
e797a2
+            Debug.getInstance("properties");
e797a2
+
e797a2
+    private static final String CRYPTO_POLICIES_BASE_DIR =
e797a2
+            "/etc/crypto-policies";
e797a2
+
e797a2
+    private static final String CRYPTO_POLICIES_JAVA_CONFIG =
e797a2
+            CRYPTO_POLICIES_BASE_DIR + "/back-ends/java.config";
e797a2
+
e797a2
+    private static final String CRYPTO_POLICIES_CONFIG =
e797a2
+            CRYPTO_POLICIES_BASE_DIR + "/config";
e797a2
+
e797a2
+    private static final class SecurityProviderInfo {
e797a2
+        int number;
e797a2
+        String key;
e797a2
+        String value;
e797a2
+        SecurityProviderInfo(int number, String key, String value) {
e797a2
+            this.number = number;
e797a2
+            this.key = key;
e797a2
+            this.value = value;
e797a2
+        }
e797a2
+    }
e797a2
+
e797a2
+    /*
e797a2
+     * Invoked when java.security.Security class is initialized, if
e797a2
+     * java.security.disableSystemPropertiesFile property is not set and
e797a2
+     * security.useSystemPropertiesFile is true.
e797a2
+     */
e797a2
+    static boolean configure(Properties props) {
e797a2
+        boolean loadedProps = false;
e797a2
+
e797a2
+        try (BufferedInputStream bis =
e797a2
+                new BufferedInputStream(
e797a2
+                        new FileInputStream(CRYPTO_POLICIES_JAVA_CONFIG))) {
e797a2
+            props.load(bis);
e797a2
+            loadedProps = true;
e797a2
+            if (sdebug != null) {
e797a2
+                sdebug.println("reading system security properties file " +
e797a2
+                        CRYPTO_POLICIES_JAVA_CONFIG);
e797a2
+                sdebug.println(props.toString());
e797a2
+            }
e797a2
+        } catch (IOException e) {
e797a2
+            if (sdebug != null) {
e797a2
+                sdebug.println("unable to load security properties from " +
e797a2
+                        CRYPTO_POLICIES_JAVA_CONFIG);
e797a2
+                e.printStackTrace();
e797a2
+            }
e797a2
+        }
e797a2
+
e797a2
+        try {
e797a2
+            if (enableFips()) {
e797a2
+                if (sdebug != null) { sdebug.println("FIPS mode detected"); }
e797a2
+                loadedProps = false;
e797a2
+                // Remove all security providers
e797a2
+                Iterator<Entry<Object, Object>> i = props.entrySet().iterator();
e797a2
+                while (i.hasNext()) {
e797a2
+                    Entry<Object, Object> e = i.next();
e797a2
+                    if (((String) e.getKey()).startsWith("security.provider")) {
e797a2
+                        if (sdebug != null) { sdebug.println("Removing provider: " + e); }
e797a2
+                        i.remove();
e797a2
+                    }
e797a2
+                }
e797a2
+                // Add FIPS security providers
e797a2
+                String fipsProviderValue = null;
e797a2
+                for (int n = 1;
e797a2
+                     (fipsProviderValue = (String) props.get("fips.provider." + n)) != null; n++) {
e797a2
+                    String fipsProviderKey = "security.provider." + n;
e797a2
+                    if (sdebug != null) {
e797a2
+                        sdebug.println("Adding provider " + n + ": " +
e797a2
+                                fipsProviderKey + "=" + fipsProviderValue);
e797a2
+                    }
e797a2
+                    props.put(fipsProviderKey, fipsProviderValue);
e797a2
+                }
e797a2
+                loadedProps = true;
e797a2
+            }
e797a2
+        } catch (Exception e) {
e797a2
+            if (sdebug != null) {
e797a2
+                sdebug.println("unable to load FIPS configuration");
e797a2
+                e.printStackTrace();
e797a2
+            }
e797a2
+        }
e797a2
+        return loadedProps;
e797a2
+    }
e797a2
+
e797a2
+    /*
e797a2
+     * FIPS is enabled only if crypto-policies are set to "FIPS"
e797a2
+     * and the com.redhat.fips property is true.
e797a2
+     */
e797a2
+    private static boolean enableFips() throws Exception {
e797a2
+        boolean fipsEnabled = Boolean.valueOf(System.getProperty("com.redhat.fips", "true"));
e797a2
+        if (fipsEnabled) {
e797a2
+            String cryptoPoliciesConfig = new String(Files.readAllBytes(Path.of(CRYPTO_POLICIES_CONFIG)));
e797a2
+            if (sdebug != null) { sdebug.println("Crypto config:\n" + cryptoPoliciesConfig); }
e797a2
+            Pattern pattern = Pattern.compile("^FIPS$", Pattern.MULTILINE);
e797a2
+            return pattern.matcher(cryptoPoliciesConfig).find();
e797a2
+        } else {
e797a2
+            return false;
e797a2
+        }
e797a2
+    }
e797a2
+}
e797a2
diff --git openjdk.orig///src/java.base/share/conf/security/java.security openjdk///src/java.base/share/conf/security/java.security
e797a2
--- openjdk.orig/src/java.base/share/conf/security/java.security
e797a2
+++ openjdk/src/java.base/share/conf/security/java.security
e797a2
@@ -87,6 +87,14 @@
e797a2
 #security.provider.tbd=SunPKCS11 ${java.home}/lib/security/nss.cfg
e797a2
 
e797a2
 #
e797a2
+# Security providers used when global crypto-policies are set to FIPS.
e797a2
+#
e797a2
+fips.provider.1=SunPKCS11 ${java.home}/conf/security/nss.fips.cfg
e797a2
+fips.provider.2=SUN
e797a2
+fips.provider.3=SunEC
e797a2
+fips.provider.4=SunJSSE
e797a2
+
e797a2
+#
e797a2
 # A list of preferred providers for specific algorithms. These providers will
e797a2
 # be searched for matching algorithms before the list of registered providers.
e797a2
 # Entries containing errors (parsing, etc) will be ignored. Use the