Blame SOURCES/rh1655466-global_crypto_and_fips.patch

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