Blame SOURCES/rh1655466-global_crypto_and_fips.patch

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