Blame SOURCES/rh1655466-global_crypto_and_fips.patch

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