Blame SOURCES/rh1655466-global_crypto_and_fips.patch

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