Blame SOURCES/rh1655466-global_crypto_and_fips.patch

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