Blame SOURCES/rh1655466-global_crypto_and_fips.patch

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