Blame SOURCES/rh1655466-global_crypto_and_fips.patch

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