Blame SOURCES/rh1655466-global_crypto_and_fips.patch

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