Blame SOURCES/rh1655466-global_crypto_and_fips.patch

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