Blame SOURCES/rh1655466-global_crypto_and_fips.patch

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