Blame SOURCES/rh1655466-global_crypto_and_fips.patch

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