Blame SOURCES/rh1991003-enable_fips_keys_import.patch

f78b6c
diff --git openjdk.orig/jdk/src/share/classes/java/security/Security.java openjdk/jdk/src/share/classes/java/security/Security.java
f78b6c
--- openjdk.orig/jdk/src/share/classes/java/security/Security.java
f78b6c
+++ openjdk/jdk/src/share/classes/java/security/Security.java
f78b6c
@@ -78,6 +78,10 @@
f78b6c
                 public boolean isSystemFipsEnabled() {
f78b6c
                     return SystemConfigurator.isSystemFipsEnabled();
f78b6c
                 }
f78b6c
+                @Override
f78b6c
+                public boolean isPlainKeySupportEnabled() {
f78b6c
+                    return SystemConfigurator.isPlainKeySupportEnabled();
f78b6c
+                }
f78b6c
             });
f78b6c
 
f78b6c
         // doPrivileged here because there are multiple
f78b6c
diff --git openjdk.orig/jdk/src/share/classes/java/security/SystemConfigurator.java openjdk/jdk/src/share/classes/java/security/SystemConfigurator.java
f78b6c
--- openjdk.orig/jdk/src/share/classes/java/security/SystemConfigurator.java
f78b6c
+++ openjdk/jdk/src/share/classes/java/security/SystemConfigurator.java
f78b6c
@@ -55,6 +55,7 @@
f78b6c
             CRYPTO_POLICIES_BASE_DIR + "/back-ends/java.config";
f78b6c
 
f78b6c
     private static boolean systemFipsEnabled = false;
f78b6c
+    private static boolean plainKeySupportEnabled = false;
f78b6c
 
f78b6c
     private static final String SYSTEMCONF_NATIVE_LIB = "systemconf";
f78b6c
 
f78b6c
@@ -149,6 +150,16 @@
f78b6c
                 }
f78b6c
                 loadedProps = true;
f78b6c
                 systemFipsEnabled = true;
f78b6c
+                String plainKeySupport = System.getProperty("com.redhat.fips.plainKeySupport",
f78b6c
+                                                            "true");
f78b6c
+                plainKeySupportEnabled = !"false".equals(plainKeySupport);
f78b6c
+                if (sdebug != null) {
f78b6c
+                    if (plainKeySupportEnabled) {
f78b6c
+                        sdebug.println("FIPS support enabled with plain key support");
f78b6c
+                    } else {
f78b6c
+                        sdebug.println("FIPS support enabled without plain key support");
f78b6c
+                    }
f78b6c
+                }
f78b6c
             }
f78b6c
         } catch (Exception e) {
f78b6c
             if (sdebug != null) {
f78b6c
@@ -176,6 +187,19 @@
f78b6c
         return systemFipsEnabled;
f78b6c
     }
f78b6c
 
f78b6c
+    /**
f78b6c
+     * Returns {@code true} if system FIPS alignment is enabled
f78b6c
+     * and plain key support is allowed.  Plain key support is
f78b6c
+     * enabled by default but can be disabled with
f78b6c
+     * {@code -Dcom.redhat.fips.plainKeySupport=false}.
f78b6c
+     *
f78b6c
+     * @return a boolean indicating whether plain key support
f78b6c
+     *         should be enabled.
f78b6c
+     */
f78b6c
+    static boolean isPlainKeySupportEnabled() {
f78b6c
+        return plainKeySupportEnabled;
f78b6c
+    }
f78b6c
+
f78b6c
     /*
f78b6c
      * OpenJDK FIPS mode will be enabled only if the com.redhat.fips
f78b6c
      * system property is true (default) and the system is in FIPS mode.
f78b6c
diff --git openjdk.orig/jdk/src/share/classes/sun/misc/JavaSecuritySystemConfiguratorAccess.java openjdk/jdk/src/share/classes/sun/misc/JavaSecuritySystemConfiguratorAccess.java
f78b6c
--- openjdk.orig/jdk/src/share/classes/sun/misc/JavaSecuritySystemConfiguratorAccess.java
f78b6c
+++ openjdk/jdk/src/share/classes/sun/misc/JavaSecuritySystemConfiguratorAccess.java
f78b6c
@@ -27,4 +27,5 @@
f78b6c
 
f78b6c
 public interface JavaSecuritySystemConfiguratorAccess {
f78b6c
     boolean isSystemFipsEnabled();
f78b6c
+    boolean isPlainKeySupportEnabled();
f78b6c
 }
f78b6c
diff --git openjdk.orig/jdk/src/share/classes/sun/security/pkcs11/FIPSKeyImporter.java openjdk/jdk/src/share/classes/sun/security/pkcs11/FIPSKeyImporter.java
f78b6c
new file mode 100644
f78b6c
--- /dev/null
f78b6c
+++ openjdk/jdk/src/share/classes/sun/security/pkcs11/FIPSKeyImporter.java
f78b6c
@@ -0,0 +1,290 @@
f78b6c
+/*
f78b6c
+ * Copyright (c) 2021, Red Hat, Inc.
f78b6c
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f78b6c
+ *
f78b6c
+ * This code is free software; you can redistribute it and/or modify it
f78b6c
+ * under the terms of the GNU General Public License version 2 only, as
f78b6c
+ * published by the Free Software Foundation.  Oracle designates this
f78b6c
+ * particular file as subject to the "Classpath" exception as provided
f78b6c
+ * by Oracle in the LICENSE file that accompanied this code.
f78b6c
+ *
f78b6c
+ * This code is distributed in the hope that it will be useful, but WITHOUT
f78b6c
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f78b6c
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f78b6c
+ * version 2 for more details (a copy is included in the LICENSE file that
f78b6c
+ * accompanied this code).
f78b6c
+ *
f78b6c
+ * You should have received a copy of the GNU General Public License version
f78b6c
+ * 2 along with this work; if not, write to the Free Software Foundation,
f78b6c
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f78b6c
+ *
f78b6c
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f78b6c
+ * or visit www.oracle.com if you need additional information or have any
f78b6c
+ * questions.
f78b6c
+ */
f78b6c
+
f78b6c
+package sun.security.pkcs11;
f78b6c
+
f78b6c
+import java.math.BigInteger;
f78b6c
+import java.security.KeyFactory;
f78b6c
+import java.security.Provider;
f78b6c
+import java.security.Security;
f78b6c
+import java.util.HashMap;
f78b6c
+import java.util.Map;
f78b6c
+import java.util.concurrent.locks.ReentrantLock;
f78b6c
+
f78b6c
+import javax.crypto.Cipher;
f78b6c
+import javax.crypto.spec.DHPrivateKeySpec;
f78b6c
+import javax.crypto.spec.IvParameterSpec;
f78b6c
+
f78b6c
+import sun.security.jca.JCAUtil;
f78b6c
+import sun.security.pkcs11.TemplateManager;
f78b6c
+import sun.security.pkcs11.wrapper.CK_ATTRIBUTE;
f78b6c
+import sun.security.pkcs11.wrapper.CK_MECHANISM;
f78b6c
+import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
f78b6c
+import sun.security.pkcs11.wrapper.PKCS11Exception;
f78b6c
+import sun.security.rsa.RSAUtil.KeyType;
f78b6c
+import sun.security.util.Debug;
f78b6c
+import sun.security.util.ECUtil;
f78b6c
+
f78b6c
+final class FIPSKeyImporter {
f78b6c
+
f78b6c
+    private static final Debug debug =
f78b6c
+            Debug.getInstance("sunpkcs11");
f78b6c
+
f78b6c
+    private static P11Key importerKey = null;
f78b6c
+    private static final ReentrantLock importerKeyLock = new ReentrantLock();
f78b6c
+    private static CK_MECHANISM importerKeyMechanism = null;
f78b6c
+    private static Cipher importerCipher = null;
f78b6c
+
f78b6c
+    private static Provider sunECProvider = null;
f78b6c
+    private static final ReentrantLock sunECProviderLock = new ReentrantLock();
f78b6c
+
f78b6c
+    private static KeyFactory DHKF = null;
f78b6c
+    private static final ReentrantLock DHKFLock = new ReentrantLock();
f78b6c
+
f78b6c
+    static Long importKey(SunPKCS11 sunPKCS11, long hSession, CK_ATTRIBUTE[] attributes)
f78b6c
+            throws PKCS11Exception {
f78b6c
+        long keyID = -1;
f78b6c
+        Token token = sunPKCS11.getToken();
f78b6c
+        if (debug != null) {
f78b6c
+            debug.println("Private or Secret key will be imported in" +
f78b6c
+                    " system FIPS mode.");
f78b6c
+        }
f78b6c
+        if (importerKey == null) {
f78b6c
+            importerKeyLock.lock();
f78b6c
+            try {
f78b6c
+                if (importerKey == null) {
f78b6c
+                    if (importerKeyMechanism == null) {
f78b6c
+                        // Importer Key creation has not been tried yet. Try it.
f78b6c
+                        createImporterKey(token);
f78b6c
+                    }
f78b6c
+                    if (importerKey == null || importerCipher == null) {
f78b6c
+                        if (debug != null) {
f78b6c
+                            debug.println("Importer Key could not be" +
f78b6c
+                                    " generated.");
f78b6c
+                        }
f78b6c
+                        throw new PKCS11Exception(CKR_GENERAL_ERROR);
f78b6c
+                    }
f78b6c
+                    if (debug != null) {
f78b6c
+                        debug.println("Importer Key successfully" +
f78b6c
+                                " generated.");
f78b6c
+                    }
f78b6c
+                }
f78b6c
+            } finally {
f78b6c
+                importerKeyLock.unlock();
f78b6c
+            }
f78b6c
+        }
f78b6c
+        long importerKeyID = importerKey.getKeyID();
f78b6c
+        try {
f78b6c
+            byte[] keyBytes = null;
f78b6c
+            byte[] encKeyBytes = null;
f78b6c
+            long keyClass = 0L;
f78b6c
+            long keyType = 0L;
f78b6c
+            Map<Long, CK_ATTRIBUTE> attrsMap = new HashMap<>();
f78b6c
+            for (CK_ATTRIBUTE attr : attributes) {
f78b6c
+                if (attr.type == CKA_CLASS) {
f78b6c
+                    keyClass = attr.getLong();
f78b6c
+                } else if (attr.type == CKA_KEY_TYPE) {
f78b6c
+                    keyType = attr.getLong();
f78b6c
+                }
f78b6c
+                attrsMap.put(attr.type, attr);
f78b6c
+            }
f78b6c
+            BigInteger v = null;
f78b6c
+            if (keyClass == CKO_PRIVATE_KEY) {
f78b6c
+                if (keyType == CKK_RSA) {
f78b6c
+                    if (debug != null) {
f78b6c
+                        debug.println("Importing an RSA private key...");
f78b6c
+                    }
f78b6c
+                    keyBytes = sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(
f78b6c
+                            KeyType.RSA,
f78b6c
+                            null,
f78b6c
+                            ((v = attrsMap.get(CKA_MODULUS).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO,
f78b6c
+                            ((v = attrsMap.get(CKA_PUBLIC_EXPONENT).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO,
f78b6c
+                            ((v = attrsMap.get(CKA_PRIVATE_EXPONENT).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO,
f78b6c
+                            ((v = attrsMap.get(CKA_PRIME_1).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO,
f78b6c
+                            ((v = attrsMap.get(CKA_PRIME_2).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO,
f78b6c
+                            ((v = attrsMap.get(CKA_EXPONENT_1).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO,
f78b6c
+                            ((v = attrsMap.get(CKA_EXPONENT_2).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO,
f78b6c
+                            ((v = attrsMap.get(CKA_COEFFICIENT).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO
f78b6c
+                            ).getEncoded();
f78b6c
+                } else if (keyType == CKK_DSA) {
f78b6c
+                    if (debug != null) {
f78b6c
+                        debug.println("Importing a DSA private key...");
f78b6c
+                    }
f78b6c
+                    keyBytes = new sun.security.provider.DSAPrivateKey(
f78b6c
+                            ((v = attrsMap.get(CKA_VALUE).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO,
f78b6c
+                            ((v = attrsMap.get(CKA_PRIME).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO,
f78b6c
+                            ((v = attrsMap.get(CKA_SUBPRIME).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO,
f78b6c
+                            ((v = attrsMap.get(CKA_BASE).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO
f78b6c
+                            ).getEncoded();
f78b6c
+                    if (token.config.getNssNetscapeDbWorkaround() &&
f78b6c
+                            attrsMap.get(CKA_NETSCAPE_DB) == null) {
f78b6c
+                        attrsMap.put(CKA_NETSCAPE_DB,
f78b6c
+                                new CK_ATTRIBUTE(CKA_NETSCAPE_DB, BigInteger.ZERO));
f78b6c
+                    }
f78b6c
+                } else if (keyType == CKK_EC) {
f78b6c
+                    if (debug != null) {
f78b6c
+                        debug.println("Importing an EC private key...");
f78b6c
+                    }
f78b6c
+                    if (sunECProvider == null) {
f78b6c
+                        sunECProviderLock.lock();
f78b6c
+                        try {
f78b6c
+                            if (sunECProvider == null) {
f78b6c
+                                sunECProvider = Security.getProvider("SunEC");
f78b6c
+                            }
f78b6c
+                        } finally {
f78b6c
+                            sunECProviderLock.unlock();
f78b6c
+                        }
f78b6c
+                    }
f78b6c
+                    keyBytes = P11ECUtil.generateECPrivateKey(
f78b6c
+                            ((v = attrsMap.get(CKA_VALUE).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO,
f78b6c
+                            ECUtil.getECParameterSpec(sunECProvider,
f78b6c
+                                    attrsMap.get(CKA_EC_PARAMS).getByteArray()))
f78b6c
+                            .getEncoded();
f78b6c
+                    if (token.config.getNssNetscapeDbWorkaround() &&
f78b6c
+                            attrsMap.get(CKA_NETSCAPE_DB) == null) {
f78b6c
+                        attrsMap.put(CKA_NETSCAPE_DB,
f78b6c
+                                new CK_ATTRIBUTE(CKA_NETSCAPE_DB, BigInteger.ZERO));
f78b6c
+                    }
f78b6c
+                } else if (keyType == CKK_DH) {
f78b6c
+                    if (debug != null) {
f78b6c
+                        debug.println("Importing a Diffie-Hellman private key...");
f78b6c
+                    }
f78b6c
+                    if (DHKF == null) {
f78b6c
+                        DHKFLock.lock();
f78b6c
+                        try {
f78b6c
+                            if (DHKF == null) {
f78b6c
+                                DHKF = KeyFactory.getInstance(
f78b6c
+                                        "DH", P11Util.getSunJceProvider());
f78b6c
+                            }
f78b6c
+                        } finally {
f78b6c
+                            DHKFLock.unlock();
f78b6c
+                        }
f78b6c
+                    }
f78b6c
+                    DHPrivateKeySpec spec = new DHPrivateKeySpec
f78b6c
+                            (((v = attrsMap.get(CKA_VALUE).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO,
f78b6c
+                            ((v = attrsMap.get(CKA_PRIME).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO,
f78b6c
+                            ((v = attrsMap.get(CKA_BASE).getBigInteger()) != null)
f78b6c
+                                    ? v : BigInteger.ZERO);
f78b6c
+                    keyBytes = DHKF.generatePrivate(spec).getEncoded();
f78b6c
+                    if (token.config.getNssNetscapeDbWorkaround() &&
f78b6c
+                            attrsMap.get(CKA_NETSCAPE_DB) == null) {
f78b6c
+                        attrsMap.put(CKA_NETSCAPE_DB,
f78b6c
+                                new CK_ATTRIBUTE(CKA_NETSCAPE_DB, BigInteger.ZERO));
f78b6c
+                    }
f78b6c
+                } else {
f78b6c
+                    if (debug != null) {
f78b6c
+                        debug.println("Unrecognized private key type.");
f78b6c
+                    }
f78b6c
+                    throw new PKCS11Exception(CKR_GENERAL_ERROR);
f78b6c
+                }
f78b6c
+            } else if (keyClass == CKO_SECRET_KEY) {
f78b6c
+                if (debug != null) {
f78b6c
+                    debug.println("Importing a secret key...");
f78b6c
+                }
f78b6c
+                keyBytes = attrsMap.get(CKA_VALUE).getByteArray();
f78b6c
+            }
f78b6c
+            if (keyBytes == null || keyBytes.length == 0) {
f78b6c
+                if (debug != null) {
f78b6c
+                    debug.println("Private or secret key plain bytes could" +
f78b6c
+                            " not be obtained. Import failed.");
f78b6c
+                }
f78b6c
+                throw new PKCS11Exception(CKR_GENERAL_ERROR);
f78b6c
+            }
f78b6c
+            importerCipher.init(Cipher.ENCRYPT_MODE, importerKey,
f78b6c
+                    new IvParameterSpec((byte[])importerKeyMechanism.pParameter),
f78b6c
+                    null);
f78b6c
+            attributes = new CK_ATTRIBUTE[attrsMap.size()];
f78b6c
+            attrsMap.values().toArray(attributes);
f78b6c
+            encKeyBytes = importerCipher.doFinal(keyBytes);
f78b6c
+            attributes = token.getAttributes(TemplateManager.O_IMPORT,
f78b6c
+                    keyClass, keyType, attributes);
f78b6c
+            keyID = token.p11.C_UnwrapKey(hSession,
f78b6c
+                    importerKeyMechanism, importerKeyID, encKeyBytes, attributes);
f78b6c
+            if (debug != null) {
f78b6c
+                debug.println("Imported key ID: " + keyID);
f78b6c
+            }
f78b6c
+        } catch (Throwable t) {
f78b6c
+            throw new PKCS11Exception(CKR_GENERAL_ERROR);
f78b6c
+        } finally {
f78b6c
+            importerKey.releaseKeyID();
f78b6c
+        }
f78b6c
+        return Long.valueOf(keyID);
f78b6c
+    }
f78b6c
+
f78b6c
+    private static void createImporterKey(Token token) {
f78b6c
+        if (debug != null) {
f78b6c
+            debug.println("Generating Importer Key...");
f78b6c
+        }
f78b6c
+        byte[] iv = new byte[16];
f78b6c
+        JCAUtil.getSecureRandom().nextBytes(iv);
f78b6c
+        importerKeyMechanism = new CK_MECHANISM(CKM_AES_CBC_PAD, iv);
f78b6c
+        try {
f78b6c
+            CK_ATTRIBUTE[] attributes = token.getAttributes(TemplateManager.O_GENERATE,
f78b6c
+                            CKO_SECRET_KEY, CKK_AES, new CK_ATTRIBUTE[] {
f78b6c
+                                    new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
f78b6c
+                                    new CK_ATTRIBUTE(CKA_VALUE_LEN, 256 >> 3)});
f78b6c
+            Session s = null;
f78b6c
+            try {
f78b6c
+                s = token.getObjSession();
f78b6c
+                long keyID = token.p11.C_GenerateKey(
f78b6c
+                        s.id(), new CK_MECHANISM(CKM_AES_KEY_GEN),
f78b6c
+                        attributes);
f78b6c
+                if (debug != null) {
f78b6c
+                    debug.println("Importer Key ID: " + keyID);
f78b6c
+                }
f78b6c
+                importerKey = (P11Key)P11Key.secretKey(s, keyID, "AES",
f78b6c
+                        256 >> 3, null);
f78b6c
+            } catch (PKCS11Exception e) {
f78b6c
+                // best effort
f78b6c
+            } finally {
f78b6c
+                token.releaseSession(s);
f78b6c
+            }
f78b6c
+            if (importerKey != null) {
f78b6c
+                importerCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
f78b6c
+            }
f78b6c
+        } catch (Throwable t) {
f78b6c
+            // best effort
f78b6c
+            importerKey = null;
f78b6c
+            importerCipher = null;
f78b6c
+            // importerKeyMechanism value is kept initialized to indicate that
f78b6c
+            // Importer Key creation has been tried and failed.
f78b6c
+        }
f78b6c
+    }
f78b6c
+}
f78b6c
diff --git openjdk.orig/jdk/src/share/classes/sun/security/pkcs11/SunPKCS11.java openjdk/jdk/src/share/classes/sun/security/pkcs11/SunPKCS11.java
f78b6c
--- openjdk.orig/jdk/src/share/classes/sun/security/pkcs11/SunPKCS11.java
f78b6c
+++ openjdk/jdk/src/share/classes/sun/security/pkcs11/SunPKCS11.java
f78b6c
@@ -26,6 +26,9 @@
f78b6c
 package sun.security.pkcs11;
f78b6c
 
f78b6c
 import java.io.*;
f78b6c
+import java.lang.invoke.MethodHandle;
f78b6c
+import java.lang.invoke.MethodHandles;
f78b6c
+import java.lang.invoke.MethodType;
f78b6c
 import java.util.*;
f78b6c
 
f78b6c
 import java.security.*;
f78b6c
@@ -63,6 +66,26 @@
f78b6c
     private static final boolean systemFipsEnabled = SharedSecrets
f78b6c
             .getJavaSecuritySystemConfiguratorAccess().isSystemFipsEnabled();
f78b6c
 
f78b6c
+    private static final boolean plainKeySupportEnabled = SharedSecrets
f78b6c
+            .getJavaSecuritySystemConfiguratorAccess().isPlainKeySupportEnabled();
f78b6c
+
f78b6c
+    private static final MethodHandle fipsImportKey;
f78b6c
+    static {
f78b6c
+        MethodHandle fipsImportKeyTmp = null;
f78b6c
+        if (plainKeySupportEnabled) {
f78b6c
+            try {
f78b6c
+                fipsImportKeyTmp = MethodHandles.lookup().findStatic(
f78b6c
+                        FIPSKeyImporter.class, "importKey",
f78b6c
+                        MethodType.methodType(Long.class, SunPKCS11.class,
f78b6c
+                        long.class, CK_ATTRIBUTE[].class));
f78b6c
+            } catch (Throwable t) {
f78b6c
+                throw new SecurityException("FIPS key importer initialization" +
f78b6c
+                        " failed", t);
f78b6c
+            }
f78b6c
+        }
f78b6c
+        fipsImportKey = fipsImportKeyTmp;
f78b6c
+    }
f78b6c
+
f78b6c
     private static final long serialVersionUID = -1354835039035306505L;
f78b6c
 
f78b6c
     static final Debug debug = Debug.getInstance("sunpkcs11");
f78b6c
@@ -314,10 +337,15 @@
f78b6c
             // request multithreaded access first
f78b6c
             initArgs.flags = CKF_OS_LOCKING_OK;
f78b6c
             PKCS11 tmpPKCS11;
f78b6c
+            MethodHandle fipsKeyImporter = null;
f78b6c
+            if (plainKeySupportEnabled) {
f78b6c
+                fipsKeyImporter = MethodHandles.insertArguments(
f78b6c
+                        fipsImportKey, 0, this);
f78b6c
+            }
f78b6c
             try {
f78b6c
                 tmpPKCS11 = PKCS11.getInstance(
f78b6c
                     library, functionList, initArgs,
f78b6c
-                    config.getOmitInitialize());
f78b6c
+                    config.getOmitInitialize(), fipsKeyImporter);
f78b6c
             } catch (PKCS11Exception e) {
f78b6c
                 if (debug != null) {
f78b6c
                     debug.println("Multi-threaded initialization failed: " + e);
f78b6c
@@ -333,7 +361,7 @@
f78b6c
                     initArgs.flags = 0;
f78b6c
                 }
f78b6c
                 tmpPKCS11 = PKCS11.getInstance(library,
f78b6c
-                    functionList, initArgs, config.getOmitInitialize());
f78b6c
+                    functionList, initArgs, config.getOmitInitialize(), fipsKeyImporter);
f78b6c
             }
f78b6c
             p11 = tmpPKCS11;
f78b6c
 
f78b6c
diff --git openjdk.orig/jdk/src/share/classes/sun/security/pkcs11/wrapper/PKCS11.java openjdk/jdk/src/share/classes/sun/security/pkcs11/wrapper/PKCS11.java
f78b6c
--- openjdk.orig/jdk/src/share/classes/sun/security/pkcs11/wrapper/PKCS11.java
f78b6c
+++ openjdk/jdk/src/share/classes/sun/security/pkcs11/wrapper/PKCS11.java
f78b6c
@@ -49,6 +49,7 @@
f78b6c
 
f78b6c
 import java.io.File;
f78b6c
 import java.io.IOException;
f78b6c
+import java.lang.invoke.MethodHandle;
f78b6c
 import java.util.*;
f78b6c
 
f78b6c
 import java.security.AccessController;
f78b6c
@@ -147,16 +148,28 @@
f78b6c
 
f78b6c
     public static synchronized PKCS11 getInstance(String pkcs11ModulePath,
f78b6c
             String functionList, CK_C_INITIALIZE_ARGS pInitArgs,
f78b6c
-            boolean omitInitialize) throws IOException, PKCS11Exception {
f78b6c
+            boolean omitInitialize, MethodHandle fipsKeyImporter)
f78b6c
+                    throws IOException, PKCS11Exception {
f78b6c
         // we may only call C_Initialize once per native .so/.dll
f78b6c
         // so keep a cache using the (non-canonicalized!) path
f78b6c
         PKCS11 pkcs11 = moduleMap.get(pkcs11ModulePath);
f78b6c
         if (pkcs11 == null) {
f78b6c
+            boolean nssFipsMode = fipsKeyImporter != null;
f78b6c
             if ((pInitArgs != null)
f78b6c
                     && ((pInitArgs.flags & CKF_OS_LOCKING_OK) != 0)) {
f78b6c
-                pkcs11 = new PKCS11(pkcs11ModulePath, functionList);
f78b6c
+                if (nssFipsMode) {
f78b6c
+                    pkcs11 = new FIPSPKCS11(pkcs11ModulePath, functionList,
f78b6c
+                            fipsKeyImporter);
f78b6c
+                } else {
f78b6c
+                    pkcs11 = new PKCS11(pkcs11ModulePath, functionList);
f78b6c
+                }
f78b6c
             } else {
f78b6c
-                pkcs11 = new SynchronizedPKCS11(pkcs11ModulePath, functionList);
f78b6c
+                if (nssFipsMode) {
f78b6c
+                    pkcs11 = new SynchronizedFIPSPKCS11(pkcs11ModulePath,
f78b6c
+                            functionList, fipsKeyImporter);
f78b6c
+                } else {
f78b6c
+                    pkcs11 = new SynchronizedPKCS11(pkcs11ModulePath, functionList);
f78b6c
+                }
f78b6c
             }
f78b6c
             if (omitInitialize == false) {
f78b6c
                 try {
f78b6c
@@ -1905,4 +1918,69 @@
f78b6c
         super.C_GenerateRandom(hSession, randomData);
f78b6c
     }
f78b6c
 }
f78b6c
+
f78b6c
+// PKCS11 subclass that allows using plain private or secret keys in
f78b6c
+// FIPS-configured NSS Software Tokens. Only used when System FIPS
f78b6c
+// is enabled.
f78b6c
+static class FIPSPKCS11 extends PKCS11 {
f78b6c
+    private MethodHandle fipsKeyImporter;
f78b6c
+    FIPSPKCS11(String pkcs11ModulePath, String functionListName,
f78b6c
+            MethodHandle fipsKeyImporter) throws IOException {
f78b6c
+        super(pkcs11ModulePath, functionListName);
f78b6c
+        this.fipsKeyImporter = fipsKeyImporter;
f78b6c
+    }
f78b6c
+
f78b6c
+    public synchronized long C_CreateObject(long hSession,
f78b6c
+            CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {
f78b6c
+        // Creating sensitive key objects from plain key material in a
f78b6c
+        // FIPS-configured NSS Software Token is not allowed. We apply
f78b6c
+        // a key-unwrapping scheme to achieve so.
f78b6c
+        if (FIPSPKCS11Helper.isSensitiveObject(pTemplate)) {
f78b6c
+            try {
f78b6c
+                return ((Long)fipsKeyImporter.invoke(hSession, pTemplate))
f78b6c
+                        .longValue();
f78b6c
+            } catch (Throwable t) {
f78b6c
+                throw new PKCS11Exception(CKR_GENERAL_ERROR);
f78b6c
+            }
f78b6c
+        }
f78b6c
+        return super.C_CreateObject(hSession, pTemplate);
f78b6c
+    }
f78b6c
 }
f78b6c
+
f78b6c
+// FIPSPKCS11 synchronized counterpart.
f78b6c
+static class SynchronizedFIPSPKCS11 extends SynchronizedPKCS11 {
f78b6c
+    private MethodHandle fipsKeyImporter;
f78b6c
+    SynchronizedFIPSPKCS11(String pkcs11ModulePath, String functionListName,
f78b6c
+            MethodHandle fipsKeyImporter) throws IOException {
f78b6c
+        super(pkcs11ModulePath, functionListName);
f78b6c
+        this.fipsKeyImporter = fipsKeyImporter;
f78b6c
+    }
f78b6c
+
f78b6c
+    public synchronized long C_CreateObject(long hSession,
f78b6c
+            CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {
f78b6c
+        // See FIPSPKCS11::C_CreateObject.
f78b6c
+        if (FIPSPKCS11Helper.isSensitiveObject(pTemplate)) {
f78b6c
+            try {
f78b6c
+                return ((Long)fipsKeyImporter.invoke(hSession, pTemplate))
f78b6c
+                        .longValue();
f78b6c
+            } catch (Throwable t) {
f78b6c
+                throw new PKCS11Exception(CKR_GENERAL_ERROR);
f78b6c
+            }
f78b6c
+        }
f78b6c
+        return super.C_CreateObject(hSession, pTemplate);
f78b6c
+    }
f78b6c
+}
f78b6c
+
f78b6c
+private static class FIPSPKCS11Helper {
f78b6c
+    static boolean isSensitiveObject(CK_ATTRIBUTE[] pTemplate) {
f78b6c
+        for (CK_ATTRIBUTE attr : pTemplate) {
f78b6c
+            if (attr.type == CKA_CLASS &&
f78b6c
+                    (attr.getLong() == CKO_PRIVATE_KEY ||
f78b6c
+                    attr.getLong() == CKO_SECRET_KEY)) {
f78b6c
+                return true;
f78b6c
+            }
f78b6c
+        }
f78b6c
+        return false;
f78b6c
+    }
f78b6c
+}
f78b6c
+}
f78b6c
diff --git openjdk.orig/jdk/src/share/classes/sun/security/ssl/KeyManagerFactoryImpl.java openjdk/jdk/src/share/classes/sun/security/ssl/KeyManagerFactoryImpl.java
f78b6c
--- openjdk.orig/jdk/src/share/classes/sun/security/ssl/KeyManagerFactoryImpl.java
f78b6c
+++ openjdk/jdk/src/share/classes/sun/security/ssl/KeyManagerFactoryImpl.java
f78b6c
@@ -33,8 +33,13 @@
f78b6c
 
f78b6c
 import javax.net.ssl.*;
f78b6c
 
f78b6c
+import sun.misc.SharedSecrets;
f78b6c
+
f78b6c
 abstract class KeyManagerFactoryImpl extends KeyManagerFactorySpi {
f78b6c
 
f78b6c
+    private static final boolean plainKeySupportEnabled = SharedSecrets
f78b6c
+            .getJavaSecuritySystemConfiguratorAccess().isPlainKeySupportEnabled();
f78b6c
+
f78b6c
     X509ExtendedKeyManager keyManager;
f78b6c
     boolean isInitialized;
f78b6c
 
f78b6c
@@ -62,7 +67,8 @@
f78b6c
                 KeyStoreException, NoSuchAlgorithmException,
f78b6c
                 UnrecoverableKeyException {
f78b6c
             if ((ks != null) && SunJSSE.isFIPS()) {
f78b6c
-                if (ks.getProvider() != SunJSSE.cryptoProvider) {
f78b6c
+                if (ks.getProvider() != SunJSSE.cryptoProvider &&
f78b6c
+                        !plainKeySupportEnabled) {
f78b6c
                     throw new KeyStoreException("FIPS mode: KeyStore must be "
f78b6c
                         + "from provider " + SunJSSE.cryptoProvider.getName());
f78b6c
                 }
f78b6c
@@ -91,8 +97,8 @@
f78b6c
                 keyManager = new X509KeyManagerImpl(
f78b6c
                         Collections.<Builder>emptyList());
f78b6c
             } else {
f78b6c
-                if (SunJSSE.isFIPS() &&
f78b6c
-                        (ks.getProvider() != SunJSSE.cryptoProvider)) {
f78b6c
+                if (SunJSSE.isFIPS() && (ks.getProvider() != SunJSSE.cryptoProvider)
f78b6c
+                        && !plainKeySupportEnabled) {
f78b6c
                     throw new KeyStoreException(
f78b6c
                         "FIPS mode: KeyStore must be " +
f78b6c
                         "from provider " + SunJSSE.cryptoProvider.getName());