Blame SOURCES/fips-17u-f8142a23d0a.patch

461e1c
diff --git a/make/autoconf/lib-sysconf.m4 b/make/autoconf/lib-sysconf.m4
461e1c
new file mode 100644
461e1c
index 00000000000..b2b1c1787da
461e1c
--- /dev/null
461e1c
+++ b/make/autoconf/lib-sysconf.m4
461e1c
@@ -0,0 +1,84 @@
461e1c
+#
461e1c
+# Copyright (c) 2021, Red Hat, Inc.
461e1c
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
461e1c
+#
461e1c
+# This code is free software; you can redistribute it and/or modify it
461e1c
+# under the terms of the GNU General Public License version 2 only, as
461e1c
+# published by the Free Software Foundation.  Oracle designates this
461e1c
+# particular file as subject to the "Classpath" exception as provided
461e1c
+# by Oracle in the LICENSE file that accompanied this code.
461e1c
+#
461e1c
+# This code is distributed in the hope that it will be useful, but WITHOUT
461e1c
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
461e1c
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
461e1c
+# version 2 for more details (a copy is included in the LICENSE file that
461e1c
+# accompanied this code).
461e1c
+#
461e1c
+# You should have received a copy of the GNU General Public License version
461e1c
+# 2 along with this work; if not, write to the Free Software Foundation,
461e1c
+# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
461e1c
+#
461e1c
+# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
461e1c
+# or visit www.oracle.com if you need additional information or have any
461e1c
+# questions.
461e1c
+#
461e1c
+
461e1c
+################################################################################
461e1c
+# Setup system configuration libraries
461e1c
+################################################################################
461e1c
+AC_DEFUN_ONCE([LIB_SETUP_SYSCONF_LIBS],
461e1c
+[
461e1c
+  ###############################################################################
461e1c
+  #
461e1c
+  # Check for the NSS library
461e1c
+  #
461e1c
+
461e1c
+  AC_MSG_CHECKING([whether to use the system NSS library with the System Configurator (libsysconf)])
461e1c
+
461e1c
+  # default is not available
461e1c
+  DEFAULT_SYSCONF_NSS=no
461e1c
+
461e1c
+  AC_ARG_ENABLE([sysconf-nss], [AS_HELP_STRING([--enable-sysconf-nss],
461e1c
+     [build the System Configurator (libsysconf) using the system NSS library if available @<:@disabled@:>@])],
461e1c
+  [
461e1c
+    case "${enableval}" in
461e1c
+      yes)
461e1c
+        sysconf_nss=yes
461e1c
+        ;;
461e1c
+      *)
461e1c
+        sysconf_nss=no
461e1c
+        ;;
461e1c
+    esac
461e1c
+  ],
461e1c
+  [
461e1c
+    sysconf_nss=${DEFAULT_SYSCONF_NSS}
461e1c
+  ])
461e1c
+  AC_MSG_RESULT([$sysconf_nss])
461e1c
+
461e1c
+  USE_SYSCONF_NSS=false
461e1c
+  if test "x${sysconf_nss}" = "xyes"; then
461e1c
+      PKG_CHECK_MODULES(NSS, nss >= 3.53, [NSS_FOUND=yes], [NSS_FOUND=no])
461e1c
+      if test "x${NSS_FOUND}" = "xyes"; then
461e1c
+         AC_MSG_CHECKING([for system FIPS support in NSS])
461e1c
+         saved_libs="${LIBS}"
461e1c
+         saved_cflags="${CFLAGS}"
461e1c
+         CFLAGS="${CFLAGS} ${NSS_CFLAGS}"
461e1c
+         LIBS="${LIBS} ${NSS_LIBS}"
461e1c
+         AC_LANG_PUSH([C])
461e1c
+         AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <nss3/pk11pub.h>]],
461e1c
+                                         [[SECMOD_GetSystemFIPSEnabled()]])],
461e1c
+                        [AC_MSG_RESULT([yes])],
461e1c
+                        [AC_MSG_RESULT([no])
461e1c
+                        AC_MSG_ERROR([System NSS FIPS detection unavailable])])
461e1c
+         AC_LANG_POP([C])
461e1c
+         CFLAGS="${saved_cflags}"
461e1c
+         LIBS="${saved_libs}"
461e1c
+         USE_SYSCONF_NSS=true
461e1c
+      else
461e1c
+         dnl NSS 3.53 is the one that introduces the SECMOD_GetSystemFIPSEnabled API
461e1c
+         dnl in nss3/pk11pub.h.
461e1c
+         AC_MSG_ERROR([--enable-sysconf-nss specified, but NSS 3.53 or above not found.])
461e1c
+      fi
461e1c
+  fi
461e1c
+  AC_SUBST(USE_SYSCONF_NSS)
461e1c
+])
461e1c
diff --git a/make/autoconf/libraries.m4 b/make/autoconf/libraries.m4
461e1c
index a65d91ee974..a8f054c1397 100644
461e1c
--- a/make/autoconf/libraries.m4
461e1c
+++ b/make/autoconf/libraries.m4
461e1c
@@ -33,6 +33,7 @@ m4_include([lib-std.m4])
461e1c
 m4_include([lib-x11.m4])
461e1c
 m4_include([lib-fontconfig.m4])
461e1c
 m4_include([lib-tests.m4])
461e1c
+m4_include([lib-sysconf.m4])
461e1c
 
461e1c
 ################################################################################
461e1c
 # Determine which libraries are needed for this configuration
461e1c
@@ -104,6 +105,7 @@ AC_DEFUN_ONCE([LIB_SETUP_LIBRARIES],
461e1c
   LIB_SETUP_BUNDLED_LIBS
461e1c
   LIB_SETUP_MISC_LIBS
461e1c
   LIB_TESTS_SETUP_GTEST
461e1c
+  LIB_SETUP_SYSCONF_LIBS
461e1c
 
461e1c
   BASIC_JDKLIB_LIBS=""
461e1c
   if test "x$TOOLCHAIN_TYPE" != xmicrosoft; then
461e1c
diff --git a/make/autoconf/spec.gmk.in b/make/autoconf/spec.gmk.in
461e1c
index c2c9c4adf3a..9d105b37acf 100644
461e1c
--- a/make/autoconf/spec.gmk.in
461e1c
+++ b/make/autoconf/spec.gmk.in
461e1c
@@ -836,6 +836,10 @@ INSTALL_SYSCONFDIR=@sysconfdir@
461e1c
 # Libraries
461e1c
 #
461e1c
 
461e1c
+USE_SYSCONF_NSS:=@USE_SYSCONF_NSS@
461e1c
+NSS_LIBS:=@NSS_LIBS@
461e1c
+NSS_CFLAGS:=@NSS_CFLAGS@
461e1c
+
461e1c
 USE_EXTERNAL_LCMS:=@USE_EXTERNAL_LCMS@
461e1c
 LCMS_CFLAGS:=@LCMS_CFLAGS@
461e1c
 LCMS_LIBS:=@LCMS_LIBS@
461e1c
diff --git a/make/modules/java.base/Lib.gmk b/make/modules/java.base/Lib.gmk
461e1c
index 5658ff342e5..cb7a56852f7 100644
461e1c
--- a/make/modules/java.base/Lib.gmk
461e1c
+++ b/make/modules/java.base/Lib.gmk
461e1c
@@ -167,6 +167,31 @@ ifeq ($(call isTargetOsType, unix), true)
461e1c
   endif
461e1c
 endif
461e1c
 
461e1c
+################################################################################
461e1c
+# Create the systemconf library
461e1c
+
461e1c
+LIBSYSTEMCONF_CFLAGS :=
461e1c
+LIBSYSTEMCONF_CXXFLAGS :=
461e1c
+
461e1c
+ifeq ($(USE_SYSCONF_NSS), true)
461e1c
+  LIBSYSTEMCONF_CFLAGS += $(NSS_CFLAGS) -DSYSCONF_NSS
461e1c
+  LIBSYSTEMCONF_CXXFLAGS += $(NSS_CFLAGS) -DSYSCONF_NSS
461e1c
+endif
461e1c
+
461e1c
+ifeq ($(OPENJDK_BUILD_OS), linux)
461e1c
+  $(eval $(call SetupJdkLibrary, BUILD_LIBSYSTEMCONF, \
461e1c
+      NAME := systemconf, \
461e1c
+      OPTIMIZATION := LOW, \
461e1c
+      CFLAGS := $(CFLAGS_JDKLIB) $(LIBSYSTEMCONF_CFLAGS), \
461e1c
+      CXXFLAGS := $(CXXFLAGS_JDKLIB) $(LIBSYSTEMCONF_CXXFLAGS), \
461e1c
+      LDFLAGS := $(LDFLAGS_JDKLIB) \
461e1c
+          $(call SET_SHARED_LIBRARY_ORIGIN), \
461e1c
+      LIBS_unix := $(LIBDL) $(NSS_LIBS), \
461e1c
+  ))
461e1c
+
461e1c
+  TARGETS += $(BUILD_LIBSYSTEMCONF)
461e1c
+endif
461e1c
+
461e1c
 ################################################################################
461e1c
 # Create the symbols file for static builds.
461e1c
 
461e1c
diff --git a/src/java.base/linux/native/libsystemconf/systemconf.c b/src/java.base/linux/native/libsystemconf/systemconf.c
461e1c
new file mode 100644
461e1c
index 00000000000..8dcb7d9073f
461e1c
--- /dev/null
461e1c
+++ b/src/java.base/linux/native/libsystemconf/systemconf.c
461e1c
@@ -0,0 +1,224 @@
461e1c
+/*
461e1c
+ * Copyright (c) 2021, Red Hat, Inc.
461e1c
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
461e1c
+ *
461e1c
+ * This code is free software; you can redistribute it and/or modify it
461e1c
+ * under the terms of the GNU General Public License version 2 only, as
461e1c
+ * published by the Free Software Foundation.  Oracle designates this
461e1c
+ * particular file as subject to the "Classpath" exception as provided
461e1c
+ * by Oracle in the LICENSE file that accompanied this code.
461e1c
+ *
461e1c
+ * This code is distributed in the hope that it will be useful, but WITHOUT
461e1c
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
461e1c
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
461e1c
+ * version 2 for more details (a copy is included in the LICENSE file that
461e1c
+ * accompanied this code).
461e1c
+ *
461e1c
+ * You should have received a copy of the GNU General Public License version
461e1c
+ * 2 along with this work; if not, write to the Free Software Foundation,
461e1c
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
461e1c
+ *
461e1c
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
461e1c
+ * or visit www.oracle.com if you need additional information or have any
461e1c
+ * questions.
461e1c
+ */
461e1c
+
461e1c
+#include <jni.h>
461e1c
+#include <jni_util.h>
461e1c
+#include "jvm_md.h"
461e1c
+#include <stdio.h>
461e1c
+
461e1c
+#ifdef SYSCONF_NSS
461e1c
+#include <nss3/pk11pub.h>
461e1c
+#else
461e1c
+#include <dlfcn.h>
461e1c
+#endif //SYSCONF_NSS
461e1c
+
461e1c
+#include "java_security_SystemConfigurator.h"
461e1c
+
461e1c
+#define MSG_MAX_SIZE 256
461e1c
+#define FIPS_ENABLED_PATH "/proc/sys/crypto/fips_enabled"
461e1c
+
461e1c
+typedef int (SECMOD_GET_SYSTEM_FIPS_ENABLED_TYPE)(void);
461e1c
+
461e1c
+static SECMOD_GET_SYSTEM_FIPS_ENABLED_TYPE *getSystemFIPSEnabled;
461e1c
+static jmethodID debugPrintlnMethodID = NULL;
461e1c
+static jobject debugObj = NULL;
461e1c
+
461e1c
+static void dbgPrint(JNIEnv *env, const char* msg)
461e1c
+{
461e1c
+    jstring jMsg;
461e1c
+    if (debugObj != NULL) {
461e1c
+        jMsg = (*env)->NewStringUTF(env, msg);
461e1c
+        CHECK_NULL(jMsg);
461e1c
+        (*env)->CallVoidMethod(env, debugObj, debugPrintlnMethodID, jMsg);
461e1c
+    }
461e1c
+}
461e1c
+
461e1c
+static void throwIOException(JNIEnv *env, const char *msg)
461e1c
+{
461e1c
+    jclass cls = (*env)->FindClass(env, "java/io/IOException");
461e1c
+    if (cls != 0)
461e1c
+        (*env)->ThrowNew(env, cls, msg);
461e1c
+}
461e1c
+
461e1c
+static void handle_msg(JNIEnv *env, const char* msg, int msg_bytes)
461e1c
+{
461e1c
+  if (msg_bytes > 0 && msg_bytes < MSG_MAX_SIZE) {
461e1c
+    dbgPrint(env, msg);
461e1c
+  } else {
461e1c
+    dbgPrint(env, "systemconf: cannot render message");
461e1c
+  }
461e1c
+}
461e1c
+
461e1c
+// Only used when NSS is not linked at build time
461e1c
+#ifndef SYSCONF_NSS
461e1c
+
461e1c
+static void *nss_handle;
461e1c
+
461e1c
+static jboolean loadNSS(JNIEnv *env)
461e1c
+{
461e1c
+  char msg[MSG_MAX_SIZE];
461e1c
+  int msg_bytes;
461e1c
+  const char* errmsg;
461e1c
+
461e1c
+  nss_handle = dlopen(JNI_LIB_NAME("nss3"), RTLD_LAZY);
461e1c
+  if (nss_handle == NULL) {
461e1c
+    errmsg = dlerror();
461e1c
+    msg_bytes = snprintf(msg, MSG_MAX_SIZE, "loadNSS: dlopen: %s\n",
461e1c
+                         errmsg);
461e1c
+    handle_msg(env, msg, msg_bytes);
461e1c
+    return JNI_FALSE;
461e1c
+  }
461e1c
+  dlerror(); /* Clear errors */
461e1c
+  getSystemFIPSEnabled = (SECMOD_GET_SYSTEM_FIPS_ENABLED_TYPE*)dlsym(nss_handle, "SECMOD_GetSystemFIPSEnabled");
461e1c
+  if ((errmsg = dlerror()) != NULL) {
461e1c
+    msg_bytes = snprintf(msg, MSG_MAX_SIZE, "loadNSS: dlsym: %s\n",
461e1c
+                         errmsg);
461e1c
+    handle_msg(env, msg, msg_bytes);
461e1c
+    return JNI_FALSE;
461e1c
+  }
461e1c
+  return JNI_TRUE;
461e1c
+}
461e1c
+
461e1c
+static void closeNSS(JNIEnv *env)
461e1c
+{
461e1c
+  char msg[MSG_MAX_SIZE];
461e1c
+  int msg_bytes;
461e1c
+  const char* errmsg;
461e1c
+
461e1c
+  if (dlclose(nss_handle) != 0) {
461e1c
+    errmsg = dlerror();
461e1c
+    msg_bytes = snprintf(msg, MSG_MAX_SIZE, "closeNSS: dlclose: %s\n",
461e1c
+                         errmsg);
461e1c
+    handle_msg(env, msg, msg_bytes);
461e1c
+  }
461e1c
+}
461e1c
+
461e1c
+#endif
461e1c
+
461e1c
+/*
461e1c
+ * Class:     java_security_SystemConfigurator
461e1c
+ * Method:    JNI_OnLoad
461e1c
+ */
461e1c
+JNIEXPORT jint JNICALL DEF_JNI_OnLoad(JavaVM *vm, void *reserved)
461e1c
+{
461e1c
+    JNIEnv *env;
461e1c
+    jclass sysConfCls, debugCls;
461e1c
+    jfieldID sdebugFld;
461e1c
+
461e1c
+    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_2) != JNI_OK) {
461e1c
+        return JNI_EVERSION; /* JNI version not supported */
461e1c
+    }
461e1c
+
461e1c
+    sysConfCls = (*env)->FindClass(env,"java/security/SystemConfigurator");
461e1c
+    if (sysConfCls == NULL) {
461e1c
+        printf("libsystemconf: SystemConfigurator class not found\n");
461e1c
+        return JNI_ERR;
461e1c
+    }
461e1c
+    sdebugFld = (*env)->GetStaticFieldID(env, sysConfCls,
461e1c
+            "sdebug", "Lsun/security/util/Debug;");
461e1c
+    if (sdebugFld == NULL) {
461e1c
+        printf("libsystemconf: SystemConfigurator::sdebug field not found\n");
461e1c
+        return JNI_ERR;
461e1c
+    }
461e1c
+    debugObj = (*env)->GetStaticObjectField(env, sysConfCls, sdebugFld);
461e1c
+    if (debugObj != NULL) {
461e1c
+        debugCls = (*env)->FindClass(env,"sun/security/util/Debug");
461e1c
+        if (debugCls == NULL) {
461e1c
+            printf("libsystemconf: Debug class not found\n");
461e1c
+            return JNI_ERR;
461e1c
+        }
461e1c
+        debugPrintlnMethodID = (*env)->GetMethodID(env, debugCls,
461e1c
+                "println", "(Ljava/lang/String;)V");
461e1c
+        if (debugPrintlnMethodID == NULL) {
461e1c
+            printf("libsystemconf: Debug::println(String) method not found\n");
461e1c
+            return JNI_ERR;
461e1c
+        }
461e1c
+        debugObj = (*env)->NewGlobalRef(env, debugObj);
461e1c
+    }
461e1c
+
461e1c
+#ifdef SYSCONF_NSS
461e1c
+    getSystemFIPSEnabled = *SECMOD_GetSystemFIPSEnabled;
461e1c
+#else
461e1c
+    if (loadNSS(env) == JNI_FALSE) {
461e1c
+      dbgPrint(env, "libsystemconf: Failed to load NSS library.");
461e1c
+    }
461e1c
+#endif
461e1c
+
461e1c
+    return (*env)->GetVersion(env);
461e1c
+}
461e1c
+
461e1c
+/*
461e1c
+ * Class:     java_security_SystemConfigurator
461e1c
+ * Method:    JNI_OnUnload
461e1c
+ */
461e1c
+JNIEXPORT void JNICALL DEF_JNI_OnUnload(JavaVM *vm, void *reserved)
461e1c
+{
461e1c
+    JNIEnv *env;
461e1c
+
461e1c
+    if (debugObj != NULL) {
461e1c
+        if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_2) != JNI_OK) {
461e1c
+            return; /* Should not happen */
461e1c
+        }
461e1c
+#ifndef SYSCONF_NSS
461e1c
+        closeNSS(env);
461e1c
+#endif
461e1c
+        (*env)->DeleteGlobalRef(env, debugObj);
461e1c
+    }
461e1c
+}
461e1c
+
461e1c
+JNIEXPORT jboolean JNICALL Java_java_security_SystemConfigurator_getSystemFIPSEnabled
461e1c
+  (JNIEnv *env, jclass cls)
461e1c
+{
461e1c
+    int fips_enabled;
461e1c
+    char msg[MSG_MAX_SIZE];
461e1c
+    int msg_bytes;
461e1c
+
461e1c
+    if (getSystemFIPSEnabled != NULL) {
461e1c
+      dbgPrint(env, "getSystemFIPSEnabled: calling SECMOD_GetSystemFIPSEnabled");
461e1c
+      fips_enabled = (*getSystemFIPSEnabled)();
461e1c
+      msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:"   \
461e1c
+                           " SECMOD_GetSystemFIPSEnabled returned 0x%x", fips_enabled);
461e1c
+      handle_msg(env, msg, msg_bytes);
461e1c
+      return (fips_enabled == 1 ? JNI_TRUE : JNI_FALSE);
461e1c
+    } else {
461e1c
+      FILE *fe;
461e1c
+
461e1c
+      dbgPrint(env, "getSystemFIPSEnabled: reading " FIPS_ENABLED_PATH);
461e1c
+      if ((fe = fopen(FIPS_ENABLED_PATH, "r")) == NULL) {
461e1c
+        throwIOException(env, "Cannot open " FIPS_ENABLED_PATH);
461e1c
+        return JNI_FALSE;
461e1c
+      }
461e1c
+      fips_enabled = fgetc(fe);
461e1c
+      fclose(fe);
461e1c
+      if (fips_enabled == EOF) {
461e1c
+        throwIOException(env, "Cannot read " FIPS_ENABLED_PATH);
461e1c
+        return JNI_FALSE;
461e1c
+      }
461e1c
+      msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:"   \
461e1c
+                           " read character is '%c'", fips_enabled);
461e1c
+      handle_msg(env, msg, msg_bytes);
461e1c
+      return (fips_enabled == '1' ? JNI_TRUE : JNI_FALSE);
461e1c
+    }
461e1c
+}
461e1c
diff --git a/src/java.base/share/classes/com/sun/crypto/provider/SunJCE.java b/src/java.base/share/classes/com/sun/crypto/provider/SunJCE.java
461e1c
index a020e1c15d8..6d459fdec01 100644
461e1c
--- a/src/java.base/share/classes/com/sun/crypto/provider/SunJCE.java
461e1c
+++ b/src/java.base/share/classes/com/sun/crypto/provider/SunJCE.java
461e1c
@@ -31,6 +31,7 @@ import java.security.SecureRandom;
461e1c
 import java.security.PrivilegedAction;
461e1c
 import java.util.HashMap;
461e1c
 import java.util.List;
461e1c
+import jdk.internal.access.SharedSecrets;
461e1c
 import static sun.security.util.SecurityConstants.PROVIDER_VER;
461e1c
 import static sun.security.util.SecurityProviderConstants.*;
461e1c
 
461e1c
@@ -78,6 +79,10 @@ import static sun.security.util.SecurityProviderConstants.*;
461e1c
 
461e1c
 public final class SunJCE extends Provider {
461e1c
 
461e1c
+    private static final boolean systemFipsEnabled =
461e1c
+            SharedSecrets.getJavaSecuritySystemConfiguratorAccess()
461e1c
+            .isSystemFipsEnabled();
461e1c
+
461e1c
     @java.io.Serial
461e1c
     private static final long serialVersionUID = 6812507587804302833L;
461e1c
 
461e1c
@@ -143,285 +148,287 @@ public final class SunJCE extends Provider {
461e1c
     void putEntries() {
461e1c
         // reuse attribute map and reset before each reuse
461e1c
         HashMap<String, String> attrs = new HashMap<>(3);
461e1c
-        attrs.put("SupportedModes", "ECB");
461e1c
-        attrs.put("SupportedPaddings", "NOPADDING|PKCS1PADDING|OAEPPADDING"
461e1c
-                + "|OAEPWITHMD5ANDMGF1PADDING"
461e1c
-                + "|OAEPWITHSHA1ANDMGF1PADDING"
461e1c
-                + "|OAEPWITHSHA-1ANDMGF1PADDING"
461e1c
-                + "|OAEPWITHSHA-224ANDMGF1PADDING"
461e1c
-                + "|OAEPWITHSHA-256ANDMGF1PADDING"
461e1c
-                + "|OAEPWITHSHA-384ANDMGF1PADDING"
461e1c
-                + "|OAEPWITHSHA-512ANDMGF1PADDING"
461e1c
-                + "|OAEPWITHSHA-512/224ANDMGF1PADDING"
461e1c
-                + "|OAEPWITHSHA-512/256ANDMGF1PADDING");
461e1c
-        attrs.put("SupportedKeyClasses",
461e1c
-                "java.security.interfaces.RSAPublicKey" +
461e1c
-                "|java.security.interfaces.RSAPrivateKey");
461e1c
-        ps("Cipher", "RSA",
461e1c
-                "com.sun.crypto.provider.RSACipher", null, attrs);
461e1c
-
461e1c
-        // common block cipher modes, pads
461e1c
-        final String BLOCK_MODES = "ECB|CBC|PCBC|CTR|CTS|CFB|OFB" +
461e1c
-            "|CFB8|CFB16|CFB24|CFB32|CFB40|CFB48|CFB56|CFB64" +
461e1c
-            "|OFB8|OFB16|OFB24|OFB32|OFB40|OFB48|OFB56|OFB64";
461e1c
-        final String BLOCK_MODES128 = BLOCK_MODES +
461e1c
-            "|CFB72|CFB80|CFB88|CFB96|CFB104|CFB112|CFB120|CFB128" +
461e1c
-            "|OFB72|OFB80|OFB88|OFB96|OFB104|OFB112|OFB120|OFB128";
461e1c
-        final String BLOCK_PADS = "NOPADDING|PKCS5PADDING|ISO10126PADDING";
461e1c
-
461e1c
-        attrs.clear();
461e1c
-        attrs.put("SupportedModes", BLOCK_MODES);
461e1c
-        attrs.put("SupportedPaddings", BLOCK_PADS);
461e1c
-        attrs.put("SupportedKeyFormats", "RAW");
461e1c
-        ps("Cipher", "DES",
461e1c
-                "com.sun.crypto.provider.DESCipher", null, attrs);
461e1c
-        psA("Cipher", "DESede", "com.sun.crypto.provider.DESedeCipher",
461e1c
-                attrs);
461e1c
-        ps("Cipher", "Blowfish",
461e1c
-                "com.sun.crypto.provider.BlowfishCipher", null, attrs);
461e1c
-
461e1c
-        ps("Cipher", "RC2",
461e1c
-                "com.sun.crypto.provider.RC2Cipher", null, attrs);
461e1c
-
461e1c
-        attrs.clear();
461e1c
-        attrs.put("SupportedModes", BLOCK_MODES128);
461e1c
-        attrs.put("SupportedPaddings", BLOCK_PADS);
461e1c
-        attrs.put("SupportedKeyFormats", "RAW");
461e1c
-        psA("Cipher", "AES",
461e1c
-                "com.sun.crypto.provider.AESCipher$General", attrs);
461e1c
-
461e1c
-        attrs.clear();
461e1c
-        attrs.put("SupportedKeyFormats", "RAW");
461e1c
-        psA("Cipher", "AES/KW/NoPadding",
461e1c
-                "com.sun.crypto.provider.KeyWrapCipher$AES_KW_NoPadding",
461e1c
-                attrs);
461e1c
-        ps("Cipher", "AES/KW/PKCS5Padding",
461e1c
-                "com.sun.crypto.provider.KeyWrapCipher$AES_KW_PKCS5Padding",
461e1c
-                null, attrs);
461e1c
-        psA("Cipher", "AES/KWP/NoPadding",
461e1c
-                "com.sun.crypto.provider.KeyWrapCipher$AES_KWP_NoPadding",
461e1c
-                attrs);
461e1c
-
461e1c
-        psA("Cipher", "AES_128/ECB/NoPadding",
461e1c
-                "com.sun.crypto.provider.AESCipher$AES128_ECB_NoPadding",
461e1c
-                attrs);
461e1c
-        psA("Cipher", "AES_128/CBC/NoPadding",
461e1c
-                "com.sun.crypto.provider.AESCipher$AES128_CBC_NoPadding",
461e1c
-                attrs);
461e1c
-        psA("Cipher", "AES_128/OFB/NoPadding",
461e1c
-                "com.sun.crypto.provider.AESCipher$AES128_OFB_NoPadding",
461e1c
-                attrs);
461e1c
-        psA("Cipher", "AES_128/CFB/NoPadding",
461e1c
-                "com.sun.crypto.provider.AESCipher$AES128_CFB_NoPadding",
461e1c
-                attrs);
461e1c
-        psA("Cipher", "AES_128/KW/NoPadding",
461e1c
-                "com.sun.crypto.provider.KeyWrapCipher$AES128_KW_NoPadding",
461e1c
-                attrs);
461e1c
-        ps("Cipher", "AES_128/KW/PKCS5Padding",
461e1c
-                "com.sun.crypto.provider.KeyWrapCipher$AES128_KW_PKCS5Padding",
461e1c
-                null, attrs);
461e1c
-        psA("Cipher", "AES_128/KWP/NoPadding",
461e1c
-                "com.sun.crypto.provider.KeyWrapCipher$AES128_KWP_NoPadding",
461e1c
-                attrs);
461e1c
-
461e1c
-        psA("Cipher", "AES_192/ECB/NoPadding",
461e1c
-                "com.sun.crypto.provider.AESCipher$AES192_ECB_NoPadding",
461e1c
-                attrs);
461e1c
-        psA("Cipher", "AES_192/CBC/NoPadding",
461e1c
-                "com.sun.crypto.provider.AESCipher$AES192_CBC_NoPadding",
461e1c
-                attrs);
461e1c
-        psA("Cipher", "AES_192/OFB/NoPadding",
461e1c
-                "com.sun.crypto.provider.AESCipher$AES192_OFB_NoPadding",
461e1c
-                attrs);
461e1c
-        psA("Cipher", "AES_192/CFB/NoPadding",
461e1c
-                "com.sun.crypto.provider.AESCipher$AES192_CFB_NoPadding",
461e1c
-                attrs);
461e1c
-        psA("Cipher", "AES_192/KW/NoPadding",
461e1c
-                "com.sun.crypto.provider.KeyWrapCipher$AES192_KW_NoPadding",
461e1c
-                attrs);
461e1c
-        ps("Cipher", "AES_192/KW/PKCS5Padding",
461e1c
-                "com.sun.crypto.provider.KeyWrapCipher$AES192_KW_PKCS5Padding",
461e1c
-                null, attrs);
461e1c
-        psA("Cipher", "AES_192/KWP/NoPadding",
461e1c
-                "com.sun.crypto.provider.KeyWrapCipher$AES192_KWP_NoPadding",
461e1c
-                attrs);
461e1c
-
461e1c
-        psA("Cipher", "AES_256/ECB/NoPadding",
461e1c
-                "com.sun.crypto.provider.AESCipher$AES256_ECB_NoPadding",
461e1c
-                attrs);
461e1c
-        psA("Cipher", "AES_256/CBC/NoPadding",
461e1c
-                "com.sun.crypto.provider.AESCipher$AES256_CBC_NoPadding",
461e1c
-                attrs);
461e1c
-        psA("Cipher", "AES_256/OFB/NoPadding",
461e1c
-                "com.sun.crypto.provider.AESCipher$AES256_OFB_NoPadding",
461e1c
-                attrs);
461e1c
-        psA("Cipher", "AES_256/CFB/NoPadding",
461e1c
-                "com.sun.crypto.provider.AESCipher$AES256_CFB_NoPadding",
461e1c
-                attrs);
461e1c
-        psA("Cipher", "AES_256/KW/NoPadding",
461e1c
-                "com.sun.crypto.provider.KeyWrapCipher$AES256_KW_NoPadding",
461e1c
-                attrs);
461e1c
-        ps("Cipher", "AES_256/KW/PKCS5Padding",
461e1c
-                "com.sun.crypto.provider.KeyWrapCipher$AES256_KW_PKCS5Padding",
461e1c
-                null, attrs);
461e1c
-        psA("Cipher", "AES_256/KWP/NoPadding",
461e1c
-                "com.sun.crypto.provider.KeyWrapCipher$AES256_KWP_NoPadding",
461e1c
-                attrs);
461e1c
-
461e1c
-        attrs.clear();
461e1c
-        attrs.put("SupportedModes", "GCM");
461e1c
-        attrs.put("SupportedKeyFormats", "RAW");
461e1c
-
461e1c
-        ps("Cipher", "AES/GCM/NoPadding",
461e1c
-                "com.sun.crypto.provider.GaloisCounterMode$AESGCM", null,
461e1c
-                attrs);
461e1c
-        psA("Cipher", "AES_128/GCM/NoPadding",
461e1c
-                "com.sun.crypto.provider.GaloisCounterMode$AES128",
461e1c
-                attrs);
461e1c
-        psA("Cipher", "AES_192/GCM/NoPadding",
461e1c
-                "com.sun.crypto.provider.GaloisCounterMode$AES192",
461e1c
-                attrs);
461e1c
-        psA("Cipher", "AES_256/GCM/NoPadding",
461e1c
-                "com.sun.crypto.provider.GaloisCounterMode$AES256",
461e1c
-                attrs);
461e1c
-
461e1c
-        attrs.clear();
461e1c
-        attrs.put("SupportedModes", "CBC");
461e1c
-        attrs.put("SupportedPaddings", "NOPADDING");
461e1c
-        attrs.put("SupportedKeyFormats", "RAW");
461e1c
-        ps("Cipher", "DESedeWrap",
461e1c
-                "com.sun.crypto.provider.DESedeWrapCipher", null, attrs);
461e1c
-
461e1c
-        attrs.clear();
461e1c
-        attrs.put("SupportedModes", "ECB");
461e1c
-        attrs.put("SupportedPaddings", "NOPADDING");
461e1c
-        attrs.put("SupportedKeyFormats", "RAW");
461e1c
-        psA("Cipher", "ARCFOUR",
461e1c
-                "com.sun.crypto.provider.ARCFOURCipher", attrs);
461e1c
-
461e1c
-        attrs.clear();
461e1c
-        attrs.put("SupportedKeyFormats", "RAW");
461e1c
-        ps("Cipher",  "ChaCha20",
461e1c
-                "com.sun.crypto.provider.ChaCha20Cipher$ChaCha20Only",
461e1c
-                null, attrs);
461e1c
-        psA("Cipher",  "ChaCha20-Poly1305",
461e1c
-                "com.sun.crypto.provider.ChaCha20Cipher$ChaCha20Poly1305",
461e1c
-                attrs);
461e1c
-
461e1c
-        // PBES1
461e1c
-        psA("Cipher", "PBEWithMD5AndDES",
461e1c
-                "com.sun.crypto.provider.PBEWithMD5AndDESCipher",
461e1c
-                null);
461e1c
-        ps("Cipher", "PBEWithMD5AndTripleDES",
461e1c
-                "com.sun.crypto.provider.PBEWithMD5AndTripleDESCipher");
461e1c
-        psA("Cipher", "PBEWithSHA1AndDESede",
461e1c
-                "com.sun.crypto.provider.PKCS12PBECipherCore$PBEWithSHA1AndDESede",
461e1c
-                null);
461e1c
-        psA("Cipher", "PBEWithSHA1AndRC2_40",
461e1c
-                "com.sun.crypto.provider.PKCS12PBECipherCore$PBEWithSHA1AndRC2_40",
461e1c
-                null);
461e1c
-        psA("Cipher", "PBEWithSHA1AndRC2_128",
461e1c
-                "com.sun.crypto.provider.PKCS12PBECipherCore$PBEWithSHA1AndRC2_128",
461e1c
-                null);
461e1c
-        psA("Cipher", "PBEWithSHA1AndRC4_40",
461e1c
-                "com.sun.crypto.provider.PKCS12PBECipherCore$PBEWithSHA1AndRC4_40",
461e1c
-                null);
461e1c
-
461e1c
-        psA("Cipher", "PBEWithSHA1AndRC4_128",
461e1c
-                "com.sun.crypto.provider.PKCS12PBECipherCore$PBEWithSHA1AndRC4_128",
461e1c
-                null);
461e1c
-
461e1c
-        // PBES2
461e1c
-        ps("Cipher", "PBEWithHmacSHA1AndAES_128",
461e1c
-                "com.sun.crypto.provider.PBES2Core$HmacSHA1AndAES_128");
461e1c
-
461e1c
-        ps("Cipher", "PBEWithHmacSHA224AndAES_128",
461e1c
-                "com.sun.crypto.provider.PBES2Core$HmacSHA224AndAES_128");
461e1c
-
461e1c
-        ps("Cipher", "PBEWithHmacSHA256AndAES_128",
461e1c
-                "com.sun.crypto.provider.PBES2Core$HmacSHA256AndAES_128");
461e1c
-
461e1c
-        ps("Cipher", "PBEWithHmacSHA384AndAES_128",
461e1c
-                "com.sun.crypto.provider.PBES2Core$HmacSHA384AndAES_128");
461e1c
-
461e1c
-        ps("Cipher", "PBEWithHmacSHA512AndAES_128",
461e1c
-                "com.sun.crypto.provider.PBES2Core$HmacSHA512AndAES_128");
461e1c
-
461e1c
-        ps("Cipher", "PBEWithHmacSHA1AndAES_256",
461e1c
-                "com.sun.crypto.provider.PBES2Core$HmacSHA1AndAES_256");
461e1c
-
461e1c
-        ps("Cipher", "PBEWithHmacSHA224AndAES_256",
461e1c
-                "com.sun.crypto.provider.PBES2Core$HmacSHA224AndAES_256");
461e1c
-
461e1c
-        ps("Cipher", "PBEWithHmacSHA256AndAES_256",
461e1c
-                "com.sun.crypto.provider.PBES2Core$HmacSHA256AndAES_256");
461e1c
-
461e1c
-        ps("Cipher", "PBEWithHmacSHA384AndAES_256",
461e1c
-                "com.sun.crypto.provider.PBES2Core$HmacSHA384AndAES_256");
461e1c
-
461e1c
-        ps("Cipher", "PBEWithHmacSHA512AndAES_256",
461e1c
-                "com.sun.crypto.provider.PBES2Core$HmacSHA512AndAES_256");
461e1c
-
461e1c
-        /*
461e1c
-         * Key(pair) Generator engines
461e1c
-         */
461e1c
-        ps("KeyGenerator", "DES",
461e1c
-                "com.sun.crypto.provider.DESKeyGenerator");
461e1c
-        psA("KeyGenerator", "DESede",
461e1c
-                "com.sun.crypto.provider.DESedeKeyGenerator",
461e1c
-                null);
461e1c
-        ps("KeyGenerator", "Blowfish",
461e1c
-                "com.sun.crypto.provider.BlowfishKeyGenerator");
461e1c
-        psA("KeyGenerator", "AES",
461e1c
-                "com.sun.crypto.provider.AESKeyGenerator",
461e1c
-                null);
461e1c
-        ps("KeyGenerator", "RC2",
461e1c
-                "com.sun.crypto.provider.KeyGeneratorCore$RC2KeyGenerator");
461e1c
-        psA("KeyGenerator", "ARCFOUR",
461e1c
-                "com.sun.crypto.provider.KeyGeneratorCore$ARCFOURKeyGenerator",
461e1c
-                null);
461e1c
-        ps("KeyGenerator", "ChaCha20",
461e1c
-                "com.sun.crypto.provider.KeyGeneratorCore$ChaCha20KeyGenerator");
461e1c
-        ps("KeyGenerator", "HmacMD5",
461e1c
-                "com.sun.crypto.provider.HmacMD5KeyGenerator");
461e1c
-
461e1c
-        psA("KeyGenerator", "HmacSHA1",
461e1c
-                "com.sun.crypto.provider.HmacSHA1KeyGenerator", null);
461e1c
-        psA("KeyGenerator", "HmacSHA224",
461e1c
-                "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA224",
461e1c
-                null);
461e1c
-        psA("KeyGenerator", "HmacSHA256",
461e1c
-                "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA256",
461e1c
-                null);
461e1c
-        psA("KeyGenerator", "HmacSHA384",
461e1c
-                "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA384",
461e1c
-                null);
461e1c
-        psA("KeyGenerator", "HmacSHA512",
461e1c
-                "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA512",
461e1c
-                null);
461e1c
-        psA("KeyGenerator", "HmacSHA512/224",
461e1c
-                "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA512_224",
461e1c
-                null);
461e1c
-        psA("KeyGenerator", "HmacSHA512/256",
461e1c
-                "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA512_256",
461e1c
-                null);
461e1c
-
461e1c
-        psA("KeyGenerator", "HmacSHA3-224",
461e1c
-                "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA3_224",
461e1c
-                null);
461e1c
-        psA("KeyGenerator", "HmacSHA3-256",
461e1c
-                "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA3_256",
461e1c
-                null);
461e1c
-        psA("KeyGenerator", "HmacSHA3-384",
461e1c
-                "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA3_384",
461e1c
-                null);
461e1c
-        psA("KeyGenerator", "HmacSHA3-512",
461e1c
-                "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA3_512",
461e1c
-                null);
461e1c
-
461e1c
-        psA("KeyPairGenerator", "DiffieHellman",
461e1c
-                "com.sun.crypto.provider.DHKeyPairGenerator",
461e1c
-                null);
461e1c
+        if (!systemFipsEnabled) {
461e1c
+            attrs.put("SupportedModes", "ECB");
461e1c
+            attrs.put("SupportedPaddings", "NOPADDING|PKCS1PADDING|OAEPPADDING"
461e1c
+                    + "|OAEPWITHMD5ANDMGF1PADDING"
461e1c
+                    + "|OAEPWITHSHA1ANDMGF1PADDING"
461e1c
+                    + "|OAEPWITHSHA-1ANDMGF1PADDING"
461e1c
+                    + "|OAEPWITHSHA-224ANDMGF1PADDING"
461e1c
+                    + "|OAEPWITHSHA-256ANDMGF1PADDING"
461e1c
+                    + "|OAEPWITHSHA-384ANDMGF1PADDING"
461e1c
+                    + "|OAEPWITHSHA-512ANDMGF1PADDING"
461e1c
+                    + "|OAEPWITHSHA-512/224ANDMGF1PADDING"
461e1c
+                    + "|OAEPWITHSHA-512/256ANDMGF1PADDING");
461e1c
+            attrs.put("SupportedKeyClasses",
461e1c
+                    "java.security.interfaces.RSAPublicKey" +
461e1c
+                    "|java.security.interfaces.RSAPrivateKey");
461e1c
+            ps("Cipher", "RSA",
461e1c
+                    "com.sun.crypto.provider.RSACipher", null, attrs);
461e1c
+
461e1c
+            // common block cipher modes, pads
461e1c
+            final String BLOCK_MODES = "ECB|CBC|PCBC|CTR|CTS|CFB|OFB" +
461e1c
+                "|CFB8|CFB16|CFB24|CFB32|CFB40|CFB48|CFB56|CFB64" +
461e1c
+                "|OFB8|OFB16|OFB24|OFB32|OFB40|OFB48|OFB56|OFB64";
461e1c
+            final String BLOCK_MODES128 = BLOCK_MODES +
461e1c
+                "|CFB72|CFB80|CFB88|CFB96|CFB104|CFB112|CFB120|CFB128" +
461e1c
+                "|OFB72|OFB80|OFB88|OFB96|OFB104|OFB112|OFB120|OFB128";
461e1c
+            final String BLOCK_PADS = "NOPADDING|PKCS5PADDING|ISO10126PADDING";
461e1c
+
461e1c
+            attrs.clear();
461e1c
+            attrs.put("SupportedModes", BLOCK_MODES);
461e1c
+            attrs.put("SupportedPaddings", BLOCK_PADS);
461e1c
+            attrs.put("SupportedKeyFormats", "RAW");
461e1c
+            ps("Cipher", "DES",
461e1c
+                    "com.sun.crypto.provider.DESCipher", null, attrs);
461e1c
+            psA("Cipher", "DESede", "com.sun.crypto.provider.DESedeCipher",
461e1c
+                    attrs);
461e1c
+            ps("Cipher", "Blowfish",
461e1c
+                    "com.sun.crypto.provider.BlowfishCipher", null, attrs);
461e1c
+
461e1c
+            ps("Cipher", "RC2",
461e1c
+                    "com.sun.crypto.provider.RC2Cipher", null, attrs);
461e1c
+
461e1c
+            attrs.clear();
461e1c
+            attrs.put("SupportedModes", BLOCK_MODES128);
461e1c
+            attrs.put("SupportedPaddings", BLOCK_PADS);
461e1c
+            attrs.put("SupportedKeyFormats", "RAW");
461e1c
+            psA("Cipher", "AES",
461e1c
+                    "com.sun.crypto.provider.AESCipher$General", attrs);
461e1c
+
461e1c
+            attrs.clear();
461e1c
+            attrs.put("SupportedKeyFormats", "RAW");
461e1c
+            psA("Cipher", "AES/KW/NoPadding",
461e1c
+                    "com.sun.crypto.provider.KeyWrapCipher$AES_KW_NoPadding",
461e1c
+                    attrs);
461e1c
+            ps("Cipher", "AES/KW/PKCS5Padding",
461e1c
+                    "com.sun.crypto.provider.KeyWrapCipher$AES_KW_PKCS5Padding",
461e1c
+                    null, attrs);
461e1c
+            psA("Cipher", "AES/KWP/NoPadding",
461e1c
+                    "com.sun.crypto.provider.KeyWrapCipher$AES_KWP_NoPadding",
461e1c
+                    attrs);
461e1c
+
461e1c
+            psA("Cipher", "AES_128/ECB/NoPadding",
461e1c
+                    "com.sun.crypto.provider.AESCipher$AES128_ECB_NoPadding",
461e1c
+                    attrs);
461e1c
+            psA("Cipher", "AES_128/CBC/NoPadding",
461e1c
+                    "com.sun.crypto.provider.AESCipher$AES128_CBC_NoPadding",
461e1c
+                    attrs);
461e1c
+            psA("Cipher", "AES_128/OFB/NoPadding",
461e1c
+                    "com.sun.crypto.provider.AESCipher$AES128_OFB_NoPadding",
461e1c
+                    attrs);
461e1c
+            psA("Cipher", "AES_128/CFB/NoPadding",
461e1c
+                    "com.sun.crypto.provider.AESCipher$AES128_CFB_NoPadding",
461e1c
+                    attrs);
461e1c
+            psA("Cipher", "AES_128/KW/NoPadding",
461e1c
+                    "com.sun.crypto.provider.KeyWrapCipher$AES128_KW_NoPadding",
461e1c
+                    attrs);
461e1c
+            ps("Cipher", "AES_128/KW/PKCS5Padding",
461e1c
+                    "com.sun.crypto.provider.KeyWrapCipher$AES128_KW_PKCS5Padding",
461e1c
+                    null, attrs);
461e1c
+            psA("Cipher", "AES_128/KWP/NoPadding",
461e1c
+                    "com.sun.crypto.provider.KeyWrapCipher$AES128_KWP_NoPadding",
461e1c
+                    attrs);
461e1c
+
461e1c
+            psA("Cipher", "AES_192/ECB/NoPadding",
461e1c
+                    "com.sun.crypto.provider.AESCipher$AES192_ECB_NoPadding",
461e1c
+                    attrs);
461e1c
+            psA("Cipher", "AES_192/CBC/NoPadding",
461e1c
+                    "com.sun.crypto.provider.AESCipher$AES192_CBC_NoPadding",
461e1c
+                    attrs);
461e1c
+            psA("Cipher", "AES_192/OFB/NoPadding",
461e1c
+                    "com.sun.crypto.provider.AESCipher$AES192_OFB_NoPadding",
461e1c
+                    attrs);
461e1c
+            psA("Cipher", "AES_192/CFB/NoPadding",
461e1c
+                    "com.sun.crypto.provider.AESCipher$AES192_CFB_NoPadding",
461e1c
+                    attrs);
461e1c
+            psA("Cipher", "AES_192/KW/NoPadding",
461e1c
+                    "com.sun.crypto.provider.KeyWrapCipher$AES192_KW_NoPadding",
461e1c
+                    attrs);
461e1c
+            ps("Cipher", "AES_192/KW/PKCS5Padding",
461e1c
+                    "com.sun.crypto.provider.KeyWrapCipher$AES192_KW_PKCS5Padding",
461e1c
+                    null, attrs);
461e1c
+            psA("Cipher", "AES_192/KWP/NoPadding",
461e1c
+                    "com.sun.crypto.provider.KeyWrapCipher$AES192_KWP_NoPadding",
461e1c
+                    attrs);
461e1c
+
461e1c
+            psA("Cipher", "AES_256/ECB/NoPadding",
461e1c
+                    "com.sun.crypto.provider.AESCipher$AES256_ECB_NoPadding",
461e1c
+                    attrs);
461e1c
+            psA("Cipher", "AES_256/CBC/NoPadding",
461e1c
+                    "com.sun.crypto.provider.AESCipher$AES256_CBC_NoPadding",
461e1c
+                    attrs);
461e1c
+            psA("Cipher", "AES_256/OFB/NoPadding",
461e1c
+                    "com.sun.crypto.provider.AESCipher$AES256_OFB_NoPadding",
461e1c
+                    attrs);
461e1c
+            psA("Cipher", "AES_256/CFB/NoPadding",
461e1c
+                    "com.sun.crypto.provider.AESCipher$AES256_CFB_NoPadding",
461e1c
+                    attrs);
461e1c
+            psA("Cipher", "AES_256/KW/NoPadding",
461e1c
+                    "com.sun.crypto.provider.KeyWrapCipher$AES256_KW_NoPadding",
461e1c
+                    attrs);
461e1c
+            ps("Cipher", "AES_256/KW/PKCS5Padding",
461e1c
+                    "com.sun.crypto.provider.KeyWrapCipher$AES256_KW_PKCS5Padding",
461e1c
+                    null, attrs);
461e1c
+            psA("Cipher", "AES_256/KWP/NoPadding",
461e1c
+                    "com.sun.crypto.provider.KeyWrapCipher$AES256_KWP_NoPadding",
461e1c
+                    attrs);
461e1c
+
461e1c
+            attrs.clear();
461e1c
+            attrs.put("SupportedModes", "GCM");
461e1c
+            attrs.put("SupportedKeyFormats", "RAW");
461e1c
+
461e1c
+            ps("Cipher", "AES/GCM/NoPadding",
461e1c
+                    "com.sun.crypto.provider.GaloisCounterMode$AESGCM", null,
461e1c
+                    attrs);
461e1c
+            psA("Cipher", "AES_128/GCM/NoPadding",
461e1c
+                    "com.sun.crypto.provider.GaloisCounterMode$AES128",
461e1c
+                    attrs);
461e1c
+            psA("Cipher", "AES_192/GCM/NoPadding",
461e1c
+                    "com.sun.crypto.provider.GaloisCounterMode$AES192",
461e1c
+                    attrs);
461e1c
+            psA("Cipher", "AES_256/GCM/NoPadding",
461e1c
+                    "com.sun.crypto.provider.GaloisCounterMode$AES256",
461e1c
+                    attrs);
461e1c
+
461e1c
+            attrs.clear();
461e1c
+            attrs.put("SupportedModes", "CBC");
461e1c
+            attrs.put("SupportedPaddings", "NOPADDING");
461e1c
+            attrs.put("SupportedKeyFormats", "RAW");
461e1c
+            ps("Cipher", "DESedeWrap",
461e1c
+                    "com.sun.crypto.provider.DESedeWrapCipher", null, attrs);
461e1c
+
461e1c
+            attrs.clear();
461e1c
+            attrs.put("SupportedModes", "ECB");
461e1c
+            attrs.put("SupportedPaddings", "NOPADDING");
461e1c
+            attrs.put("SupportedKeyFormats", "RAW");
461e1c
+            psA("Cipher", "ARCFOUR",
461e1c
+                    "com.sun.crypto.provider.ARCFOURCipher", attrs);
461e1c
+
461e1c
+            attrs.clear();
461e1c
+            attrs.put("SupportedKeyFormats", "RAW");
461e1c
+            ps("Cipher",  "ChaCha20",
461e1c
+                    "com.sun.crypto.provider.ChaCha20Cipher$ChaCha20Only",
461e1c
+                    null, attrs);
461e1c
+            psA("Cipher",  "ChaCha20-Poly1305",
461e1c
+                    "com.sun.crypto.provider.ChaCha20Cipher$ChaCha20Poly1305",
461e1c
+                    attrs);
461e1c
+
461e1c
+            // PBES1
461e1c
+            psA("Cipher", "PBEWithMD5AndDES",
461e1c
+                    "com.sun.crypto.provider.PBEWithMD5AndDESCipher",
461e1c
+                    null);
461e1c
+            ps("Cipher", "PBEWithMD5AndTripleDES",
461e1c
+                    "com.sun.crypto.provider.PBEWithMD5AndTripleDESCipher");
461e1c
+            psA("Cipher", "PBEWithSHA1AndDESede",
461e1c
+                    "com.sun.crypto.provider.PKCS12PBECipherCore$PBEWithSHA1AndDESede",
461e1c
+                    null);
461e1c
+            psA("Cipher", "PBEWithSHA1AndRC2_40",
461e1c
+                    "com.sun.crypto.provider.PKCS12PBECipherCore$PBEWithSHA1AndRC2_40",
461e1c
+                    null);
461e1c
+            psA("Cipher", "PBEWithSHA1AndRC2_128",
461e1c
+                    "com.sun.crypto.provider.PKCS12PBECipherCore$PBEWithSHA1AndRC2_128",
461e1c
+                    null);
461e1c
+            psA("Cipher", "PBEWithSHA1AndRC4_40",
461e1c
+                    "com.sun.crypto.provider.PKCS12PBECipherCore$PBEWithSHA1AndRC4_40",
461e1c
+                    null);
461e1c
+
461e1c
+            psA("Cipher", "PBEWithSHA1AndRC4_128",
461e1c
+                    "com.sun.crypto.provider.PKCS12PBECipherCore$PBEWithSHA1AndRC4_128",
461e1c
+                    null);
461e1c
+
461e1c
+            // PBES2
461e1c
+            ps("Cipher", "PBEWithHmacSHA1AndAES_128",
461e1c
+                    "com.sun.crypto.provider.PBES2Core$HmacSHA1AndAES_128");
461e1c
+
461e1c
+            ps("Cipher", "PBEWithHmacSHA224AndAES_128",
461e1c
+                    "com.sun.crypto.provider.PBES2Core$HmacSHA224AndAES_128");
461e1c
+
461e1c
+            ps("Cipher", "PBEWithHmacSHA256AndAES_128",
461e1c
+                    "com.sun.crypto.provider.PBES2Core$HmacSHA256AndAES_128");
461e1c
+
461e1c
+            ps("Cipher", "PBEWithHmacSHA384AndAES_128",
461e1c
+                    "com.sun.crypto.provider.PBES2Core$HmacSHA384AndAES_128");
461e1c
+
461e1c
+            ps("Cipher", "PBEWithHmacSHA512AndAES_128",
461e1c
+                    "com.sun.crypto.provider.PBES2Core$HmacSHA512AndAES_128");
461e1c
+
461e1c
+            ps("Cipher", "PBEWithHmacSHA1AndAES_256",
461e1c
+                    "com.sun.crypto.provider.PBES2Core$HmacSHA1AndAES_256");
461e1c
+
461e1c
+            ps("Cipher", "PBEWithHmacSHA224AndAES_256",
461e1c
+                    "com.sun.crypto.provider.PBES2Core$HmacSHA224AndAES_256");
461e1c
+
461e1c
+            ps("Cipher", "PBEWithHmacSHA256AndAES_256",
461e1c
+                    "com.sun.crypto.provider.PBES2Core$HmacSHA256AndAES_256");
461e1c
+
461e1c
+            ps("Cipher", "PBEWithHmacSHA384AndAES_256",
461e1c
+                    "com.sun.crypto.provider.PBES2Core$HmacSHA384AndAES_256");
461e1c
+
461e1c
+            ps("Cipher", "PBEWithHmacSHA512AndAES_256",
461e1c
+                    "com.sun.crypto.provider.PBES2Core$HmacSHA512AndAES_256");
461e1c
+
461e1c
+            /*
461e1c
+             * Key(pair) Generator engines
461e1c
+             */
461e1c
+            ps("KeyGenerator", "DES",
461e1c
+                    "com.sun.crypto.provider.DESKeyGenerator");
461e1c
+            psA("KeyGenerator", "DESede",
461e1c
+                    "com.sun.crypto.provider.DESedeKeyGenerator",
461e1c
+                    null);
461e1c
+            ps("KeyGenerator", "Blowfish",
461e1c
+                    "com.sun.crypto.provider.BlowfishKeyGenerator");
461e1c
+            psA("KeyGenerator", "AES",
461e1c
+                    "com.sun.crypto.provider.AESKeyGenerator",
461e1c
+                    null);
461e1c
+            ps("KeyGenerator", "RC2",
461e1c
+                    "com.sun.crypto.provider.KeyGeneratorCore$RC2KeyGenerator");
461e1c
+            psA("KeyGenerator", "ARCFOUR",
461e1c
+                    "com.sun.crypto.provider.KeyGeneratorCore$ARCFOURKeyGenerator",
461e1c
+                    null);
461e1c
+            ps("KeyGenerator", "ChaCha20",
461e1c
+                    "com.sun.crypto.provider.KeyGeneratorCore$ChaCha20KeyGenerator");
461e1c
+            ps("KeyGenerator", "HmacMD5",
461e1c
+                    "com.sun.crypto.provider.HmacMD5KeyGenerator");
461e1c
+
461e1c
+            psA("KeyGenerator", "HmacSHA1",
461e1c
+                    "com.sun.crypto.provider.HmacSHA1KeyGenerator", null);
461e1c
+            psA("KeyGenerator", "HmacSHA224",
461e1c
+                    "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA224",
461e1c
+                    null);
461e1c
+            psA("KeyGenerator", "HmacSHA256",
461e1c
+                    "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA256",
461e1c
+                    null);
461e1c
+            psA("KeyGenerator", "HmacSHA384",
461e1c
+                    "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA384",
461e1c
+                    null);
461e1c
+            psA("KeyGenerator", "HmacSHA512",
461e1c
+                    "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA512",
461e1c
+                    null);
461e1c
+            psA("KeyGenerator", "HmacSHA512/224",
461e1c
+                    "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA512_224",
461e1c
+                    null);
461e1c
+            psA("KeyGenerator", "HmacSHA512/256",
461e1c
+                    "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA512_256",
461e1c
+                    null);
461e1c
+
461e1c
+            psA("KeyGenerator", "HmacSHA3-224",
461e1c
+                    "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA3_224",
461e1c
+                    null);
461e1c
+            psA("KeyGenerator", "HmacSHA3-256",
461e1c
+                    "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA3_256",
461e1c
+                    null);
461e1c
+            psA("KeyGenerator", "HmacSHA3-384",
461e1c
+                    "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA3_384",
461e1c
+                    null);
461e1c
+            psA("KeyGenerator", "HmacSHA3-512",
461e1c
+                    "com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA3_512",
461e1c
+                    null);
461e1c
+
461e1c
+            psA("KeyPairGenerator", "DiffieHellman",
461e1c
+                    "com.sun.crypto.provider.DHKeyPairGenerator",
461e1c
+                    null);
461e1c
+        }
461e1c
 
461e1c
         /*
461e1c
          * Algorithm parameter generation engines
461e1c
@@ -430,15 +437,17 @@ public final class SunJCE extends Provider {
461e1c
                 "DiffieHellman", "com.sun.crypto.provider.DHParameterGenerator",
461e1c
                 null);
461e1c
 
461e1c
-        /*
461e1c
-         * Key Agreement engines
461e1c
-         */
461e1c
-        attrs.clear();
461e1c
-        attrs.put("SupportedKeyClasses", "javax.crypto.interfaces.DHPublicKey" +
461e1c
-                        "|javax.crypto.interfaces.DHPrivateKey");
461e1c
-        psA("KeyAgreement", "DiffieHellman",
461e1c
-                "com.sun.crypto.provider.DHKeyAgreement",
461e1c
-                attrs);
461e1c
+        if (!systemFipsEnabled) {
461e1c
+            /*
461e1c
+             * Key Agreement engines
461e1c
+             */
461e1c
+            attrs.clear();
461e1c
+            attrs.put("SupportedKeyClasses", "javax.crypto.interfaces.DHPublicKey" +
461e1c
+                            "|javax.crypto.interfaces.DHPrivateKey");
461e1c
+            psA("KeyAgreement", "DiffieHellman",
461e1c
+                    "com.sun.crypto.provider.DHKeyAgreement",
461e1c
+                    attrs);
461e1c
+        }
461e1c
 
461e1c
         /*
461e1c
          * Algorithm Parameter engines
461e1c
@@ -531,197 +540,199 @@ public final class SunJCE extends Provider {
461e1c
         psA("AlgorithmParameters", "ChaCha20-Poly1305",
461e1c
                 "com.sun.crypto.provider.ChaCha20Poly1305Parameters", null);
461e1c
 
461e1c
-        /*
461e1c
-         * Key factories
461e1c
-         */
461e1c
-        psA("KeyFactory", "DiffieHellman",
461e1c
-                "com.sun.crypto.provider.DHKeyFactory",
461e1c
-                null);
461e1c
-
461e1c
-        /*
461e1c
-         * Secret-key factories
461e1c
-         */
461e1c
-        ps("SecretKeyFactory", "DES",
461e1c
-                "com.sun.crypto.provider.DESKeyFactory");
461e1c
-
461e1c
-        psA("SecretKeyFactory", "DESede",
461e1c
-                "com.sun.crypto.provider.DESedeKeyFactory", null);
461e1c
-
461e1c
-        psA("SecretKeyFactory", "PBEWithMD5AndDES",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithMD5AndDES",
461e1c
-                null);
461e1c
-
461e1c
-        /*
461e1c
-         * Internal in-house crypto algorithm used for
461e1c
-         * the JCEKS keystore type.  Since this was developed
461e1c
-         * internally, there isn't an OID corresponding to this
461e1c
-         * algorithm.
461e1c
-         */
461e1c
-        ps("SecretKeyFactory", "PBEWithMD5AndTripleDES",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithMD5AndTripleDES");
461e1c
-
461e1c
-        psA("SecretKeyFactory", "PBEWithSHA1AndDESede",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithSHA1AndDESede",
461e1c
-                null);
461e1c
-
461e1c
-        psA("SecretKeyFactory", "PBEWithSHA1AndRC2_40",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithSHA1AndRC2_40",
461e1c
-                null);
461e1c
-
461e1c
-        psA("SecretKeyFactory", "PBEWithSHA1AndRC2_128",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithSHA1AndRC2_128",
461e1c
-                null);
461e1c
-
461e1c
-        psA("SecretKeyFactory", "PBEWithSHA1AndRC4_40",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithSHA1AndRC4_40",
461e1c
-                null);
461e1c
-
461e1c
-        psA("SecretKeyFactory", "PBEWithSHA1AndRC4_128",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithSHA1AndRC4_128",
461e1c
-                null);
461e1c
-
461e1c
-        ps("SecretKeyFactory", "PBEWithHmacSHA1AndAES_128",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA1AndAES_128");
461e1c
-
461e1c
-        ps("SecretKeyFactory", "PBEWithHmacSHA224AndAES_128",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA224AndAES_128");
461e1c
-
461e1c
-        ps("SecretKeyFactory", "PBEWithHmacSHA256AndAES_128",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA256AndAES_128");
461e1c
-
461e1c
-        ps("SecretKeyFactory", "PBEWithHmacSHA384AndAES_128",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA384AndAES_128");
461e1c
-
461e1c
-        ps("SecretKeyFactory", "PBEWithHmacSHA512AndAES_128",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA512AndAES_128");
461e1c
-
461e1c
-        ps("SecretKeyFactory", "PBEWithHmacSHA1AndAES_256",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA1AndAES_256");
461e1c
-
461e1c
-        ps("SecretKeyFactory", "PBEWithHmacSHA224AndAES_256",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA224AndAES_256");
461e1c
-
461e1c
-        ps("SecretKeyFactory", "PBEWithHmacSHA256AndAES_256",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA256AndAES_256");
461e1c
-
461e1c
-        ps("SecretKeyFactory", "PBEWithHmacSHA384AndAES_256",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA384AndAES_256");
461e1c
-
461e1c
-        ps("SecretKeyFactory", "PBEWithHmacSHA512AndAES_256",
461e1c
-                "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA512AndAES_256");
461e1c
-
461e1c
-        // PBKDF2
461e1c
-        psA("SecretKeyFactory", "PBKDF2WithHmacSHA1",
461e1c
-                "com.sun.crypto.provider.PBKDF2Core$HmacSHA1",
461e1c
-                null);
461e1c
-        ps("SecretKeyFactory", "PBKDF2WithHmacSHA224",
461e1c
-                "com.sun.crypto.provider.PBKDF2Core$HmacSHA224");
461e1c
-        ps("SecretKeyFactory", "PBKDF2WithHmacSHA256",
461e1c
-                "com.sun.crypto.provider.PBKDF2Core$HmacSHA256");
461e1c
-        ps("SecretKeyFactory", "PBKDF2WithHmacSHA384",
461e1c
-                "com.sun.crypto.provider.PBKDF2Core$HmacSHA384");
461e1c
-        ps("SecretKeyFactory", "PBKDF2WithHmacSHA512",
461e1c
-                "com.sun.crypto.provider.PBKDF2Core$HmacSHA512");
461e1c
-
461e1c
-        /*
461e1c
-         * MAC
461e1c
-         */
461e1c
-        attrs.clear();
461e1c
-        attrs.put("SupportedKeyFormats", "RAW");
461e1c
-        ps("Mac", "HmacMD5", "com.sun.crypto.provider.HmacMD5", null, attrs);
461e1c
-        psA("Mac", "HmacSHA1", "com.sun.crypto.provider.HmacSHA1",
461e1c
-                attrs);
461e1c
-        psA("Mac", "HmacSHA224",
461e1c
-                "com.sun.crypto.provider.HmacCore$HmacSHA224", attrs);
461e1c
-        psA("Mac", "HmacSHA256",
461e1c
-                "com.sun.crypto.provider.HmacCore$HmacSHA256", attrs);
461e1c
-        psA("Mac", "HmacSHA384",
461e1c
-                "com.sun.crypto.provider.HmacCore$HmacSHA384", attrs);
461e1c
-        psA("Mac", "HmacSHA512",
461e1c
-                "com.sun.crypto.provider.HmacCore$HmacSHA512", attrs);
461e1c
-        psA("Mac", "HmacSHA512/224",
461e1c
-                "com.sun.crypto.provider.HmacCore$HmacSHA512_224", attrs);
461e1c
-        psA("Mac", "HmacSHA512/256",
461e1c
-                "com.sun.crypto.provider.HmacCore$HmacSHA512_256", attrs);
461e1c
-        psA("Mac", "HmacSHA3-224",
461e1c
-                "com.sun.crypto.provider.HmacCore$HmacSHA3_224", attrs);
461e1c
-        psA("Mac", "HmacSHA3-256",
461e1c
-                "com.sun.crypto.provider.HmacCore$HmacSHA3_256", attrs);
461e1c
-        psA("Mac", "HmacSHA3-384",
461e1c
-                "com.sun.crypto.provider.HmacCore$HmacSHA3_384", attrs);
461e1c
-        psA("Mac", "HmacSHA3-512",
461e1c
-                "com.sun.crypto.provider.HmacCore$HmacSHA3_512", attrs);
461e1c
-
461e1c
-        ps("Mac", "HmacPBESHA1",
461e1c
-                "com.sun.crypto.provider.HmacPKCS12PBECore$HmacPKCS12PBE_SHA1",
461e1c
-                null, attrs);
461e1c
-        ps("Mac", "HmacPBESHA224",
461e1c
-                "com.sun.crypto.provider.HmacPKCS12PBECore$HmacPKCS12PBE_SHA224",
461e1c
-                null, attrs);
461e1c
-        ps("Mac", "HmacPBESHA256",
461e1c
-                "com.sun.crypto.provider.HmacPKCS12PBECore$HmacPKCS12PBE_SHA256",
461e1c
-                null, attrs);
461e1c
-        ps("Mac", "HmacPBESHA384",
461e1c
-                "com.sun.crypto.provider.HmacPKCS12PBECore$HmacPKCS12PBE_SHA384",
461e1c
-                null, attrs);
461e1c
-        ps("Mac", "HmacPBESHA512",
461e1c
-                "com.sun.crypto.provider.HmacPKCS12PBECore$HmacPKCS12PBE_SHA512",
461e1c
-                null, attrs);
461e1c
-        ps("Mac", "HmacPBESHA512/224",
461e1c
-                "com.sun.crypto.provider.HmacPKCS12PBECore$HmacPKCS12PBE_SHA512_224",
461e1c
-                null, attrs);
461e1c
-        ps("Mac", "HmacPBESHA512/256",
461e1c
-                "com.sun.crypto.provider.HmacPKCS12PBECore$HmacPKCS12PBE_SHA512_256",
461e1c
-                null, attrs);
461e1c
-
461e1c
-
461e1c
-        // PBMAC1
461e1c
-        ps("Mac", "PBEWithHmacSHA1",
461e1c
-                "com.sun.crypto.provider.PBMAC1Core$HmacSHA1", null, attrs);
461e1c
-        ps("Mac", "PBEWithHmacSHA224",
461e1c
-                "com.sun.crypto.provider.PBMAC1Core$HmacSHA224", null, attrs);
461e1c
-        ps("Mac", "PBEWithHmacSHA256",
461e1c
-                "com.sun.crypto.provider.PBMAC1Core$HmacSHA256", null, attrs);
461e1c
-        ps("Mac", "PBEWithHmacSHA384",
461e1c
-                "com.sun.crypto.provider.PBMAC1Core$HmacSHA384", null, attrs);
461e1c
-        ps("Mac", "PBEWithHmacSHA512",
461e1c
-                "com.sun.crypto.provider.PBMAC1Core$HmacSHA512", null, attrs);
461e1c
-        ps("Mac", "SslMacMD5",
461e1c
-                "com.sun.crypto.provider.SslMacCore$SslMacMD5", null, attrs);
461e1c
-        ps("Mac", "SslMacSHA1",
461e1c
-                "com.sun.crypto.provider.SslMacCore$SslMacSHA1", null, attrs);
461e1c
-
461e1c
-        /*
461e1c
-         * KeyStore
461e1c
-         */
461e1c
-        ps("KeyStore", "JCEKS",
461e1c
-                "com.sun.crypto.provider.JceKeyStore");
461e1c
-
461e1c
-        /*
461e1c
-         * SSL/TLS mechanisms
461e1c
-         *
461e1c
-         * These are strictly internal implementations and may
461e1c
-         * be changed at any time.  These names were chosen
461e1c
-         * because PKCS11/SunPKCS11 does not yet have TLS1.2
461e1c
-         * mechanisms, and it will cause calls to come here.
461e1c
-         */
461e1c
-        ps("KeyGenerator", "SunTlsPrf",
461e1c
-                "com.sun.crypto.provider.TlsPrfGenerator$V10");
461e1c
-        ps("KeyGenerator", "SunTls12Prf",
461e1c
-                "com.sun.crypto.provider.TlsPrfGenerator$V12");
461e1c
-
461e1c
-        ps("KeyGenerator", "SunTlsMasterSecret",
461e1c
-                "com.sun.crypto.provider.TlsMasterSecretGenerator",
461e1c
-                List.of("SunTls12MasterSecret", "SunTlsExtendedMasterSecret"),
461e1c
-                null);
461e1c
-
461e1c
-        ps("KeyGenerator", "SunTlsKeyMaterial",
461e1c
-                "com.sun.crypto.provider.TlsKeyMaterialGenerator",
461e1c
-                List.of("SunTls12KeyMaterial"), null);
461e1c
-
461e1c
-        ps("KeyGenerator", "SunTlsRsaPremasterSecret",
461e1c
-                "com.sun.crypto.provider.TlsRsaPremasterSecretGenerator",
461e1c
-                List.of("SunTls12RsaPremasterSecret"), null);
461e1c
+        if (!systemFipsEnabled) {
461e1c
+            /*
461e1c
+             * Key factories
461e1c
+             */
461e1c
+            psA("KeyFactory", "DiffieHellman",
461e1c
+                    "com.sun.crypto.provider.DHKeyFactory",
461e1c
+                    null);
461e1c
+
461e1c
+            /*
461e1c
+             * Secret-key factories
461e1c
+             */
461e1c
+            ps("SecretKeyFactory", "DES",
461e1c
+                    "com.sun.crypto.provider.DESKeyFactory");
461e1c
+
461e1c
+            psA("SecretKeyFactory", "DESede",
461e1c
+                    "com.sun.crypto.provider.DESedeKeyFactory", null);
461e1c
+
461e1c
+            psA("SecretKeyFactory", "PBEWithMD5AndDES",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithMD5AndDES",
461e1c
+                    null);
461e1c
+
461e1c
+            /*
461e1c
+             * Internal in-house crypto algorithm used for
461e1c
+             * the JCEKS keystore type.  Since this was developed
461e1c
+             * internally, there isn't an OID corresponding to this
461e1c
+             * algorithm.
461e1c
+             */
461e1c
+            ps("SecretKeyFactory", "PBEWithMD5AndTripleDES",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithMD5AndTripleDES");
461e1c
+
461e1c
+            psA("SecretKeyFactory", "PBEWithSHA1AndDESede",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithSHA1AndDESede",
461e1c
+                    null);
461e1c
+
461e1c
+            psA("SecretKeyFactory", "PBEWithSHA1AndRC2_40",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithSHA1AndRC2_40",
461e1c
+                    null);
461e1c
+
461e1c
+            psA("SecretKeyFactory", "PBEWithSHA1AndRC2_128",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithSHA1AndRC2_128",
461e1c
+                    null);
461e1c
+
461e1c
+            psA("SecretKeyFactory", "PBEWithSHA1AndRC4_40",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithSHA1AndRC4_40",
461e1c
+                    null);
461e1c
+
461e1c
+            psA("SecretKeyFactory", "PBEWithSHA1AndRC4_128",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithSHA1AndRC4_128",
461e1c
+                    null);
461e1c
+
461e1c
+            ps("SecretKeyFactory", "PBEWithHmacSHA1AndAES_128",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA1AndAES_128");
461e1c
+
461e1c
+            ps("SecretKeyFactory", "PBEWithHmacSHA224AndAES_128",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA224AndAES_128");
461e1c
+
461e1c
+            ps("SecretKeyFactory", "PBEWithHmacSHA256AndAES_128",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA256AndAES_128");
461e1c
+
461e1c
+            ps("SecretKeyFactory", "PBEWithHmacSHA384AndAES_128",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA384AndAES_128");
461e1c
+
461e1c
+            ps("SecretKeyFactory", "PBEWithHmacSHA512AndAES_128",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA512AndAES_128");
461e1c
+
461e1c
+            ps("SecretKeyFactory", "PBEWithHmacSHA1AndAES_256",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA1AndAES_256");
461e1c
+
461e1c
+            ps("SecretKeyFactory", "PBEWithHmacSHA224AndAES_256",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA224AndAES_256");
461e1c
+
461e1c
+            ps("SecretKeyFactory", "PBEWithHmacSHA256AndAES_256",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA256AndAES_256");
461e1c
+
461e1c
+            ps("SecretKeyFactory", "PBEWithHmacSHA384AndAES_256",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA384AndAES_256");
461e1c
+
461e1c
+            ps("SecretKeyFactory", "PBEWithHmacSHA512AndAES_256",
461e1c
+                    "com.sun.crypto.provider.PBEKeyFactory$PBEWithHmacSHA512AndAES_256");
461e1c
+
461e1c
+            // PBKDF2
461e1c
+            psA("SecretKeyFactory", "PBKDF2WithHmacSHA1",
461e1c
+                    "com.sun.crypto.provider.PBKDF2Core$HmacSHA1",
461e1c
+                    null);
461e1c
+            ps("SecretKeyFactory", "PBKDF2WithHmacSHA224",
461e1c
+                    "com.sun.crypto.provider.PBKDF2Core$HmacSHA224");
461e1c
+            ps("SecretKeyFactory", "PBKDF2WithHmacSHA256",
461e1c
+                    "com.sun.crypto.provider.PBKDF2Core$HmacSHA256");
461e1c
+            ps("SecretKeyFactory", "PBKDF2WithHmacSHA384",
461e1c
+                    "com.sun.crypto.provider.PBKDF2Core$HmacSHA384");
461e1c
+            ps("SecretKeyFactory", "PBKDF2WithHmacSHA512",
461e1c
+                    "com.sun.crypto.provider.PBKDF2Core$HmacSHA512");
461e1c
+
461e1c
+            /*
461e1c
+             * MAC
461e1c
+             */
461e1c
+            attrs.clear();
461e1c
+            attrs.put("SupportedKeyFormats", "RAW");
461e1c
+            ps("Mac", "HmacMD5", "com.sun.crypto.provider.HmacMD5", null, attrs);
461e1c
+            psA("Mac", "HmacSHA1", "com.sun.crypto.provider.HmacSHA1",
461e1c
+                    attrs);
461e1c
+            psA("Mac", "HmacSHA224",
461e1c
+                    "com.sun.crypto.provider.HmacCore$HmacSHA224", attrs);
461e1c
+            psA("Mac", "HmacSHA256",
461e1c
+                    "com.sun.crypto.provider.HmacCore$HmacSHA256", attrs);
461e1c
+            psA("Mac", "HmacSHA384",
461e1c
+                    "com.sun.crypto.provider.HmacCore$HmacSHA384", attrs);
461e1c
+            psA("Mac", "HmacSHA512",
461e1c
+                    "com.sun.crypto.provider.HmacCore$HmacSHA512", attrs);
461e1c
+            psA("Mac", "HmacSHA512/224",
461e1c
+                    "com.sun.crypto.provider.HmacCore$HmacSHA512_224", attrs);
461e1c
+            psA("Mac", "HmacSHA512/256",
461e1c
+                    "com.sun.crypto.provider.HmacCore$HmacSHA512_256", attrs);
461e1c
+            psA("Mac", "HmacSHA3-224",
461e1c
+                    "com.sun.crypto.provider.HmacCore$HmacSHA3_224", attrs);
461e1c
+            psA("Mac", "HmacSHA3-256",
461e1c
+                    "com.sun.crypto.provider.HmacCore$HmacSHA3_256", attrs);
461e1c
+            psA("Mac", "HmacSHA3-384",
461e1c
+                    "com.sun.crypto.provider.HmacCore$HmacSHA3_384", attrs);
461e1c
+            psA("Mac", "HmacSHA3-512",
461e1c
+                    "com.sun.crypto.provider.HmacCore$HmacSHA3_512", attrs);
461e1c
+
461e1c
+            ps("Mac", "HmacPBESHA1",
461e1c
+                    "com.sun.crypto.provider.HmacPKCS12PBECore$HmacPKCS12PBE_SHA1",
461e1c
+                    null, attrs);
461e1c
+            ps("Mac", "HmacPBESHA224",
461e1c
+                    "com.sun.crypto.provider.HmacPKCS12PBECore$HmacPKCS12PBE_SHA224",
461e1c
+                    null, attrs);
461e1c
+            ps("Mac", "HmacPBESHA256",
461e1c
+                    "com.sun.crypto.provider.HmacPKCS12PBECore$HmacPKCS12PBE_SHA256",
461e1c
+                    null, attrs);
461e1c
+            ps("Mac", "HmacPBESHA384",
461e1c
+                    "com.sun.crypto.provider.HmacPKCS12PBECore$HmacPKCS12PBE_SHA384",
461e1c
+                    null, attrs);
461e1c
+            ps("Mac", "HmacPBESHA512",
461e1c
+                    "com.sun.crypto.provider.HmacPKCS12PBECore$HmacPKCS12PBE_SHA512",
461e1c
+                    null, attrs);
461e1c
+            ps("Mac", "HmacPBESHA512/224",
461e1c
+                    "com.sun.crypto.provider.HmacPKCS12PBECore$HmacPKCS12PBE_SHA512_224",
461e1c
+                    null, attrs);
461e1c
+            ps("Mac", "HmacPBESHA512/256",
461e1c
+                    "com.sun.crypto.provider.HmacPKCS12PBECore$HmacPKCS12PBE_SHA512_256",
461e1c
+                    null, attrs);
461e1c
+
461e1c
+
461e1c
+            // PBMAC1
461e1c
+            ps("Mac", "PBEWithHmacSHA1",
461e1c
+                    "com.sun.crypto.provider.PBMAC1Core$HmacSHA1", null, attrs);
461e1c
+            ps("Mac", "PBEWithHmacSHA224",
461e1c
+                    "com.sun.crypto.provider.PBMAC1Core$HmacSHA224", null, attrs);
461e1c
+            ps("Mac", "PBEWithHmacSHA256",
461e1c
+                    "com.sun.crypto.provider.PBMAC1Core$HmacSHA256", null, attrs);
461e1c
+            ps("Mac", "PBEWithHmacSHA384",
461e1c
+                    "com.sun.crypto.provider.PBMAC1Core$HmacSHA384", null, attrs);
461e1c
+            ps("Mac", "PBEWithHmacSHA512",
461e1c
+                    "com.sun.crypto.provider.PBMAC1Core$HmacSHA512", null, attrs);
461e1c
+            ps("Mac", "SslMacMD5",
461e1c
+                    "com.sun.crypto.provider.SslMacCore$SslMacMD5", null, attrs);
461e1c
+            ps("Mac", "SslMacSHA1",
461e1c
+                    "com.sun.crypto.provider.SslMacCore$SslMacSHA1", null, attrs);
461e1c
+
461e1c
+            /*
461e1c
+             * KeyStore
461e1c
+             */
461e1c
+            ps("KeyStore", "JCEKS",
461e1c
+                    "com.sun.crypto.provider.JceKeyStore");
461e1c
+
461e1c
+            /*
461e1c
+             * SSL/TLS mechanisms
461e1c
+             *
461e1c
+             * These are strictly internal implementations and may
461e1c
+             * be changed at any time.  These names were chosen
461e1c
+             * because PKCS11/SunPKCS11 does not yet have TLS1.2
461e1c
+             * mechanisms, and it will cause calls to come here.
461e1c
+             */
461e1c
+            ps("KeyGenerator", "SunTlsPrf",
461e1c
+                    "com.sun.crypto.provider.TlsPrfGenerator$V10");
461e1c
+            ps("KeyGenerator", "SunTls12Prf",
461e1c
+                    "com.sun.crypto.provider.TlsPrfGenerator$V12");
461e1c
+
461e1c
+            ps("KeyGenerator", "SunTlsMasterSecret",
461e1c
+                    "com.sun.crypto.provider.TlsMasterSecretGenerator",
461e1c
+                    List.of("SunTls12MasterSecret", "SunTlsExtendedMasterSecret"),
461e1c
+                    null);
461e1c
+
461e1c
+            ps("KeyGenerator", "SunTlsKeyMaterial",
461e1c
+                    "com.sun.crypto.provider.TlsKeyMaterialGenerator",
461e1c
+                    List.of("SunTls12KeyMaterial"), null);
461e1c
+
461e1c
+            ps("KeyGenerator", "SunTlsRsaPremasterSecret",
461e1c
+                    "com.sun.crypto.provider.TlsRsaPremasterSecretGenerator",
461e1c
+                    List.of("SunTls12RsaPremasterSecret"), null);
461e1c
+        }
461e1c
     }
461e1c
 
461e1c
     // Return the instance of this class or create one if needed.
461e1c
diff --git a/src/java.base/share/classes/java/security/Security.java b/src/java.base/share/classes/java/security/Security.java
461e1c
index ff2bc942c03..96a3ba4040c 100644
461e1c
--- a/src/java.base/share/classes/java/security/Security.java
461e1c
+++ b/src/java.base/share/classes/java/security/Security.java
461e1c
@@ -32,6 +32,7 @@ import java.net.URL;
461e1c
 
461e1c
 import jdk.internal.event.EventHelper;
461e1c
 import jdk.internal.event.SecurityPropertyModificationEvent;
461e1c
+import jdk.internal.access.JavaSecuritySystemConfiguratorAccess;
461e1c
 import jdk.internal.access.SharedSecrets;
461e1c
 import jdk.internal.util.StaticProperty;
461e1c
 import sun.security.util.Debug;
461e1c
@@ -47,12 +48,20 @@ import sun.security.jca.*;
461e1c
  * implementation-specific location, which is typically the properties file
461e1c
  * {@code conf/security/java.security} in the Java installation directory.
461e1c
  *
461e1c
+ * 

Additional default values of security properties are read from a

461e1c
+ * system-specific location, if available.

461e1c
+ *
461e1c
  * @author Benjamin Renaud
461e1c
  * @since 1.1
461e1c
  */
461e1c
 
461e1c
 public final class Security {
461e1c
 
461e1c
+    private static final String SYS_PROP_SWITCH =
461e1c
+        "java.security.disableSystemPropertiesFile";
461e1c
+    private static final String SEC_PROP_SWITCH =
461e1c
+        "security.useSystemPropertiesFile";
461e1c
+
461e1c
     /* Are we debugging? -- for developers */
461e1c
     private static final Debug sdebug =
461e1c
                         Debug.getInstance("properties");
461e1c
@@ -67,6 +76,19 @@ public final class Security {
461e1c
     }
461e1c
 
461e1c
     static {
461e1c
+        // Initialise here as used by code with system properties disabled
461e1c
+        SharedSecrets.setJavaSecuritySystemConfiguratorAccess(
461e1c
+            new JavaSecuritySystemConfiguratorAccess() {
461e1c
+                @Override
461e1c
+                public boolean isSystemFipsEnabled() {
461e1c
+                    return SystemConfigurator.isSystemFipsEnabled();
461e1c
+                }
461e1c
+                @Override
461e1c
+                public boolean isPlainKeySupportEnabled() {
461e1c
+                    return SystemConfigurator.isPlainKeySupportEnabled();
461e1c
+                }
461e1c
+            });
461e1c
+
461e1c
         // doPrivileged here because there are multiple
461e1c
         // things in initialize that might require privs.
461e1c
         // (the FileInputStream call and the File.exists call,
461e1c
@@ -84,6 +106,7 @@ public final class Security {
461e1c
         props = new Properties();
461e1c
         boolean loadedProps = false;
461e1c
         boolean overrideAll = false;
461e1c
+        boolean systemSecPropsEnabled = false;
461e1c
 
461e1c
         // first load the system properties file
461e1c
         // to determine the value of security.overridePropertiesFile
461e1c
@@ -99,6 +122,7 @@ public final class Security {
461e1c
                 if (sdebug != null) {
461e1c
                     sdebug.println("reading security properties file: " +
461e1c
                                 propFile);
461e1c
+                    sdebug.println(props.toString());
461e1c
                 }
461e1c
             } catch (IOException e) {
461e1c
                 if (sdebug != null) {
461e1c
@@ -193,6 +217,61 @@ public final class Security {
461e1c
             }
461e1c
         }
461e1c
 
461e1c
+        boolean sysUseProps = Boolean.valueOf(System.getProperty(SYS_PROP_SWITCH, "false"));
461e1c
+        boolean secUseProps = Boolean.valueOf(props.getProperty(SEC_PROP_SWITCH));
461e1c
+        if (sdebug != null) {
461e1c
+            sdebug.println(SYS_PROP_SWITCH + "=" + sysUseProps);
461e1c
+            sdebug.println(SEC_PROP_SWITCH + "=" + secUseProps);
461e1c
+        }
461e1c
+        if (!sysUseProps && secUseProps) {
461e1c
+            systemSecPropsEnabled = SystemConfigurator.configureSysProps(props);
461e1c
+            if (!systemSecPropsEnabled) {
461e1c
+                if (sdebug != null) {
461e1c
+                    sdebug.println("WARNING: System security properties could not be loaded.");
461e1c
+                }
461e1c
+            }
461e1c
+        } else {
461e1c
+            if (sdebug != null) {
461e1c
+                sdebug.println("System security property support disabled by user.");
461e1c
+            }
461e1c
+        }
461e1c
+
461e1c
+        // FIPS support depends on the contents of java.security so
461e1c
+        // ensure it has loaded first
461e1c
+        if (loadedProps && systemSecPropsEnabled) {
461e1c
+            boolean shouldEnable;
461e1c
+            String sysProp = System.getProperty("com.redhat.fips");
461e1c
+            if (sysProp == null) {
461e1c
+                shouldEnable = true;
461e1c
+                if (sdebug != null) {
461e1c
+                    sdebug.println("com.redhat.fips unset, using default value of true");
461e1c
+                }
461e1c
+            } else {
461e1c
+                shouldEnable = Boolean.valueOf(sysProp);
461e1c
+                if (sdebug != null) {
461e1c
+                    sdebug.println("com.redhat.fips set, using its value " + shouldEnable);
461e1c
+                }
461e1c
+            }
461e1c
+            if (shouldEnable) {
461e1c
+                boolean fipsEnabled = SystemConfigurator.configureFIPS(props);
461e1c
+                if (sdebug != null) {
461e1c
+                    if (fipsEnabled) {
461e1c
+                        sdebug.println("FIPS mode support configured and enabled.");
461e1c
+                    } else {
461e1c
+                        sdebug.println("FIPS mode support disabled.");
461e1c
+                    }
461e1c
+                }
461e1c
+            } else {
461e1c
+                if (sdebug != null ) {
461e1c
+                    sdebug.println("FIPS mode support disabled by user.");
461e1c
+                }
461e1c
+            }
461e1c
+        } else {
461e1c
+            if (sdebug != null) {
461e1c
+                sdebug.println("WARNING: FIPS mode support can not be enabled without " +
461e1c
+                               "system security properties being enabled.");
461e1c
+            }
461e1c
+        }
461e1c
     }
461e1c
 
461e1c
     /*
461e1c
diff --git a/src/java.base/share/classes/java/security/SystemConfigurator.java b/src/java.base/share/classes/java/security/SystemConfigurator.java
461e1c
new file mode 100644
461e1c
index 00000000000..98ffced455b
461e1c
--- /dev/null
461e1c
+++ b/src/java.base/share/classes/java/security/SystemConfigurator.java
461e1c
@@ -0,0 +1,249 @@
461e1c
+/*
461e1c
+ * Copyright (c) 2019, 2021, Red Hat, Inc.
461e1c
+ *
461e1c
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
461e1c
+ *
461e1c
+ * This code is free software; you can redistribute it and/or modify it
461e1c
+ * under the terms of the GNU General Public License version 2 only, as
461e1c
+ * published by the Free Software Foundation.  Oracle designates this
461e1c
+ * particular file as subject to the "Classpath" exception as provided
461e1c
+ * by Oracle in the LICENSE file that accompanied this code.
461e1c
+ *
461e1c
+ * This code is distributed in the hope that it will be useful, but WITHOUT
461e1c
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
461e1c
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
461e1c
+ * version 2 for more details (a copy is included in the LICENSE file that
461e1c
+ * accompanied this code).
461e1c
+ *
461e1c
+ * You should have received a copy of the GNU General Public License version
461e1c
+ * 2 along with this work; if not, write to the Free Software Foundation,
461e1c
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
461e1c
+ *
461e1c
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
461e1c
+ * or visit www.oracle.com if you need additional information or have any
461e1c
+ * questions.
461e1c
+ */
461e1c
+
461e1c
+package java.security;
461e1c
+
461e1c
+import java.io.BufferedInputStream;
461e1c
+import java.io.FileInputStream;
461e1c
+import java.io.IOException;
461e1c
+
461e1c
+import java.util.Iterator;
461e1c
+import java.util.Map.Entry;
461e1c
+import java.util.Properties;
461e1c
+
461e1c
+import sun.security.util.Debug;
461e1c
+
461e1c
+/**
461e1c
+ * Internal class to align OpenJDK with global crypto-policies.
461e1c
+ * Called from java.security.Security class initialization,
461e1c
+ * during startup.
461e1c
+ *
461e1c
+ */
461e1c
+
461e1c
+final class SystemConfigurator {
461e1c
+
461e1c
+    private static final Debug sdebug =
461e1c
+            Debug.getInstance("properties");
461e1c
+
461e1c
+    private static final String CRYPTO_POLICIES_BASE_DIR =
461e1c
+            "/etc/crypto-policies";
461e1c
+
461e1c
+    private static final String CRYPTO_POLICIES_JAVA_CONFIG =
461e1c
+            CRYPTO_POLICIES_BASE_DIR + "/back-ends/java.config";
461e1c
+
461e1c
+    private static boolean systemFipsEnabled = false;
461e1c
+    private static boolean plainKeySupportEnabled = false;
461e1c
+
461e1c
+    private static final String SYSTEMCONF_NATIVE_LIB = "systemconf";
461e1c
+
461e1c
+    private static native boolean getSystemFIPSEnabled()
461e1c
+            throws IOException;
461e1c
+
461e1c
+    static {
461e1c
+        @SuppressWarnings("removal")
461e1c
+        var dummy = AccessController.doPrivileged(new PrivilegedAction<Void>() {
461e1c
+            public Void run() {
461e1c
+                System.loadLibrary(SYSTEMCONF_NATIVE_LIB);
461e1c
+                return null;
461e1c
+            }
461e1c
+        });
461e1c
+    }
461e1c
+
461e1c
+    /*
461e1c
+     * Invoked when java.security.Security class is initialized, if
461e1c
+     * java.security.disableSystemPropertiesFile property is not set and
461e1c
+     * security.useSystemPropertiesFile is true.
461e1c
+     */
461e1c
+    static boolean configureSysProps(Properties props) {
461e1c
+        boolean systemSecPropsLoaded = false;
461e1c
+
461e1c
+        try (BufferedInputStream bis =
461e1c
+                new BufferedInputStream(
461e1c
+                        new FileInputStream(CRYPTO_POLICIES_JAVA_CONFIG))) {
461e1c
+            props.load(bis);
461e1c
+            systemSecPropsLoaded = true;
461e1c
+            if (sdebug != null) {
461e1c
+                sdebug.println("reading system security properties file " +
461e1c
+                        CRYPTO_POLICIES_JAVA_CONFIG);
461e1c
+                sdebug.println(props.toString());
461e1c
+            }
461e1c
+        } catch (IOException e) {
461e1c
+            if (sdebug != null) {
461e1c
+                sdebug.println("unable to load security properties from " +
461e1c
+                        CRYPTO_POLICIES_JAVA_CONFIG);
461e1c
+                e.printStackTrace();
461e1c
+            }
461e1c
+        }
461e1c
+        return systemSecPropsLoaded;
461e1c
+    }
461e1c
+
461e1c
+    /*
461e1c
+     * Invoked at the end of java.security.Security initialisation
461e1c
+     * if java.security properties have been loaded
461e1c
+     */
461e1c
+    static boolean configureFIPS(Properties props) {
461e1c
+        boolean loadedProps = false;
461e1c
+
461e1c
+        try {
461e1c
+            if (enableFips()) {
461e1c
+                if (sdebug != null) { sdebug.println("FIPS mode detected"); }
461e1c
+                // Remove all security providers
461e1c
+                Iterator<Entry<Object, Object>> i = props.entrySet().iterator();
461e1c
+                while (i.hasNext()) {
461e1c
+                    Entry<Object, Object> e = i.next();
461e1c
+                    if (((String) e.getKey()).startsWith("security.provider")) {
461e1c
+                        if (sdebug != null) { sdebug.println("Removing provider: " + e); }
461e1c
+                        i.remove();
461e1c
+                    }
461e1c
+                }
461e1c
+                // Add FIPS security providers
461e1c
+                String fipsProviderValue = null;
461e1c
+                for (int n = 1;
461e1c
+                     (fipsProviderValue = (String) props.get("fips.provider." + n)) != null; n++) {
461e1c
+                    String fipsProviderKey = "security.provider." + n;
461e1c
+                    if (sdebug != null) {
461e1c
+                        sdebug.println("Adding provider " + n + ": " +
461e1c
+                                fipsProviderKey + "=" + fipsProviderValue);
461e1c
+                    }
461e1c
+                    props.put(fipsProviderKey, fipsProviderValue);
461e1c
+                }
461e1c
+                // Add other security properties
461e1c
+                String keystoreTypeValue = (String) props.get("fips.keystore.type");
461e1c
+                if (keystoreTypeValue != null) {
461e1c
+                    String nonFipsKeystoreType = props.getProperty("keystore.type");
461e1c
+                    props.put("keystore.type", keystoreTypeValue);
461e1c
+                    if (keystoreTypeValue.equals("PKCS11")) {
461e1c
+                        // If keystore.type is PKCS11, javax.net.ssl.keyStore
461e1c
+                        // must be "NONE". See JDK-8238264.
461e1c
+                        System.setProperty("javax.net.ssl.keyStore", "NONE");
461e1c
+                    }
461e1c
+                    if (System.getProperty("javax.net.ssl.trustStoreType") == null) {
461e1c
+                        // If no trustStoreType has been set, use the
461e1c
+                        // previous keystore.type under FIPS mode. In
461e1c
+                        // a default configuration, the Trust Store will
461e1c
+                        // be 'cacerts' (JKS type).
461e1c
+                        System.setProperty("javax.net.ssl.trustStoreType",
461e1c
+                                nonFipsKeystoreType);
461e1c
+                    }
461e1c
+                    if (sdebug != null) {
461e1c
+                        sdebug.println("FIPS mode default keystore.type = " +
461e1c
+                                keystoreTypeValue);
461e1c
+                        sdebug.println("FIPS mode javax.net.ssl.keyStore = " +
461e1c
+                                System.getProperty("javax.net.ssl.keyStore", ""));
461e1c
+                        sdebug.println("FIPS mode javax.net.ssl.trustStoreType = " +
461e1c
+                                System.getProperty("javax.net.ssl.trustStoreType", ""));
461e1c
+                    }
461e1c
+                }
461e1c
+                loadedProps = true;
461e1c
+                systemFipsEnabled = true;
461e1c
+                String plainKeySupport = System.getProperty("com.redhat.fips.plainKeySupport",
461e1c
+                                                            "true");
461e1c
+                plainKeySupportEnabled = !"false".equals(plainKeySupport);
461e1c
+                if (sdebug != null) {
461e1c
+                    if (plainKeySupportEnabled) {
461e1c
+                        sdebug.println("FIPS support enabled with plain key support");
461e1c
+                    } else {
461e1c
+                        sdebug.println("FIPS support enabled without plain key support");
461e1c
+                    }
461e1c
+                }
461e1c
+            } else {
461e1c
+                if (sdebug != null) { sdebug.println("FIPS mode not detected"); }
461e1c
+            }
461e1c
+        } catch (Exception e) {
461e1c
+            if (sdebug != null) {
461e1c
+                sdebug.println("unable to load FIPS configuration");
461e1c
+                e.printStackTrace();
461e1c
+            }
461e1c
+        }
461e1c
+        return loadedProps;
461e1c
+    }
461e1c
+
461e1c
+    /**
461e1c
+     * Returns whether or not global system FIPS alignment is enabled.
461e1c
+     *
461e1c
+     * Value is always 'false' before java.security.Security class is
461e1c
+     * initialized.
461e1c
+     *
461e1c
+     * Call from out of this package through SharedSecrets:
461e1c
+     *   SharedSecrets.getJavaSecuritySystemConfiguratorAccess()
461e1c
+     *           .isSystemFipsEnabled();
461e1c
+     *
461e1c
+     * @return  a boolean value indicating whether or not global
461e1c
+     *          system FIPS alignment is enabled.
461e1c
+     */
461e1c
+    static boolean isSystemFipsEnabled() {
461e1c
+        return systemFipsEnabled;
461e1c
+    }
461e1c
+
461e1c
+    /**
461e1c
+     * Returns {@code true} if system FIPS alignment is enabled
461e1c
+     * and plain key support is allowed.  Plain key support is
461e1c
+     * enabled by default but can be disabled with
461e1c
+     * {@code -Dcom.redhat.fips.plainKeySupport=false}.
461e1c
+     *
461e1c
+     * @return a boolean indicating whether plain key support
461e1c
+     *         should be enabled.
461e1c
+     */
461e1c
+    static boolean isPlainKeySupportEnabled() {
461e1c
+        return plainKeySupportEnabled;
461e1c
+    }
461e1c
+
461e1c
+    /**
461e1c
+     * Determines whether FIPS mode should be enabled.
461e1c
+     *
461e1c
+     * OpenJDK FIPS mode will be enabled only if the system is in
461e1c
+     * FIPS mode.
461e1c
+     *
461e1c
+     * Calls to this method only occur if the system property
461e1c
+     * com.redhat.fips is not set to false.
461e1c
+     *
461e1c
+     * There are 2 possible ways in which OpenJDK detects that the system
461e1c
+     * is in FIPS mode: 1) if the NSS SECMOD_GetSystemFIPSEnabled API is
461e1c
+     * available at OpenJDK's built-time, it is called; 2) otherwise, the
461e1c
+     * /proc/sys/crypto/fips_enabled file is read.
461e1c
+     *
461e1c
+     * @return true if the system is in FIPS mode
461e1c
+     */
461e1c
+    private static boolean enableFips() throws Exception {
461e1c
+        if (sdebug != null) {
461e1c
+            sdebug.println("Calling getSystemFIPSEnabled (libsystemconf)...");
461e1c
+        }
461e1c
+        try {
461e1c
+            boolean fipsEnabled = getSystemFIPSEnabled();
461e1c
+            if (sdebug != null) {
461e1c
+                sdebug.println("Call to getSystemFIPSEnabled (libsystemconf) returned: "
461e1c
+                               + fipsEnabled);
461e1c
+            }
461e1c
+            return fipsEnabled;
461e1c
+        } catch (IOException e) {
461e1c
+            if (sdebug != null) {
461e1c
+                sdebug.println("Call to getSystemFIPSEnabled (libsystemconf) failed:");
461e1c
+                sdebug.println(e.getMessage());
461e1c
+            }
461e1c
+            throw e;
461e1c
+        }
461e1c
+    }
461e1c
+}
461e1c
diff --git a/src/java.base/share/classes/jdk/internal/access/JavaSecuritySystemConfiguratorAccess.java b/src/java.base/share/classes/jdk/internal/access/JavaSecuritySystemConfiguratorAccess.java
461e1c
new file mode 100644
461e1c
index 00000000000..3f3caac64dc
461e1c
--- /dev/null
461e1c
+++ b/src/java.base/share/classes/jdk/internal/access/JavaSecuritySystemConfiguratorAccess.java
461e1c
@@ -0,0 +1,31 @@
461e1c
+/*
461e1c
+ * Copyright (c) 2020, Red Hat, Inc.
461e1c
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
461e1c
+ *
461e1c
+ * This code is free software; you can redistribute it and/or modify it
461e1c
+ * under the terms of the GNU General Public License version 2 only, as
461e1c
+ * published by the Free Software Foundation.  Oracle designates this
461e1c
+ * particular file as subject to the "Classpath" exception as provided
461e1c
+ * by Oracle in the LICENSE file that accompanied this code.
461e1c
+ *
461e1c
+ * This code is distributed in the hope that it will be useful, but WITHOUT
461e1c
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
461e1c
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
461e1c
+ * version 2 for more details (a copy is included in the LICENSE file that
461e1c
+ * accompanied this code).
461e1c
+ *
461e1c
+ * You should have received a copy of the GNU General Public License version
461e1c
+ * 2 along with this work; if not, write to the Free Software Foundation,
461e1c
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
461e1c
+ *
461e1c
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
461e1c
+ * or visit www.oracle.com if you need additional information or have any
461e1c
+ * questions.
461e1c
+ */
461e1c
+
461e1c
+package jdk.internal.access;
461e1c
+
461e1c
+public interface JavaSecuritySystemConfiguratorAccess {
461e1c
+    boolean isSystemFipsEnabled();
461e1c
+    boolean isPlainKeySupportEnabled();
461e1c
+}
461e1c
diff --git a/src/java.base/share/classes/jdk/internal/access/SharedSecrets.java b/src/java.base/share/classes/jdk/internal/access/SharedSecrets.java
461e1c
index f6d3638c3dd..a1ee182d913 100644
461e1c
--- a/src/java.base/share/classes/jdk/internal/access/SharedSecrets.java
461e1c
+++ b/src/java.base/share/classes/jdk/internal/access/SharedSecrets.java
461e1c
@@ -39,6 +39,7 @@ import java.io.FilePermission;
461e1c
 import java.io.ObjectInputStream;
461e1c
 import java.io.RandomAccessFile;
461e1c
 import java.security.ProtectionDomain;
461e1c
+import java.security.Security;
461e1c
 import java.security.Signature;
461e1c
 
461e1c
 /** A repository of "shared secrets", which are a mechanism for
461e1c
@@ -81,6 +82,7 @@ public class SharedSecrets {
461e1c
     private static JavaSecuritySpecAccess javaSecuritySpecAccess;
461e1c
     private static JavaxCryptoSealedObjectAccess javaxCryptoSealedObjectAccess;
461e1c
     private static JavaxCryptoSpecAccess javaxCryptoSpecAccess;
461e1c
+    private static JavaSecuritySystemConfiguratorAccess javaSecuritySystemConfiguratorAccess;
461e1c
 
461e1c
     public static void setJavaUtilCollectionAccess(JavaUtilCollectionAccess juca) {
461e1c
         javaUtilCollectionAccess = juca;
461e1c
@@ -442,4 +444,15 @@ public class SharedSecrets {
461e1c
             MethodHandles.lookup().ensureInitialized(c);
461e1c
         } catch (IllegalAccessException e) {}
461e1c
     }
461e1c
+
461e1c
+    public static void setJavaSecuritySystemConfiguratorAccess(JavaSecuritySystemConfiguratorAccess jssca) {
461e1c
+        javaSecuritySystemConfiguratorAccess = jssca;
461e1c
+    }
461e1c
+
461e1c
+    public static JavaSecuritySystemConfiguratorAccess getJavaSecuritySystemConfiguratorAccess() {
461e1c
+        if (javaSecuritySystemConfiguratorAccess == null) {
461e1c
+            ensureClassInitialized(Security.class);
461e1c
+        }
461e1c
+        return javaSecuritySystemConfiguratorAccess;
461e1c
+    }
461e1c
 }
461e1c
diff --git a/src/java.base/share/classes/module-info.java b/src/java.base/share/classes/module-info.java
461e1c
index 63bb580eb3a..dbbf11bbb22 100644
461e1c
--- a/src/java.base/share/classes/module-info.java
461e1c
+++ b/src/java.base/share/classes/module-info.java
461e1c
@@ -152,6 +152,8 @@ module java.base {
461e1c
         java.naming,
461e1c
         java.rmi,
461e1c
         jdk.charsets,
461e1c
+        jdk.crypto.cryptoki,
461e1c
+        jdk.crypto.ec,
461e1c
         jdk.jartool,
461e1c
         jdk.jlink,
461e1c
         jdk.net,
461e1c
diff --git a/src/java.base/share/classes/sun/security/provider/SunEntries.java b/src/java.base/share/classes/sun/security/provider/SunEntries.java
461e1c
index 912cad59714..709d32912ca 100644
461e1c
--- a/src/java.base/share/classes/sun/security/provider/SunEntries.java
461e1c
+++ b/src/java.base/share/classes/sun/security/provider/SunEntries.java
461e1c
@@ -30,6 +30,7 @@ import java.net.*;
461e1c
 import java.util.*;
461e1c
 import java.security.*;
461e1c
 
461e1c
+import jdk.internal.access.SharedSecrets;
461e1c
 import jdk.internal.util.StaticProperty;
461e1c
 import sun.security.action.GetPropertyAction;
461e1c
 import sun.security.util.SecurityProviderConstants;
461e1c
@@ -83,6 +84,10 @@ import static sun.security.util.SecurityProviderConstants.getAliases;
461e1c
 
461e1c
 public final class SunEntries {
461e1c
 
461e1c
+    private static final boolean systemFipsEnabled =
461e1c
+            SharedSecrets.getJavaSecuritySystemConfiguratorAccess()
461e1c
+            .isSystemFipsEnabled();
461e1c
+
461e1c
     // the default algo used by SecureRandom class for new SecureRandom() calls
461e1c
     public static final String DEF_SECURE_RANDOM_ALGO;
461e1c
 
461e1c
@@ -94,99 +99,101 @@ public final class SunEntries {
461e1c
         // common attribute map
461e1c
         HashMap<String, String> attrs = new HashMap<>(3);
461e1c
 
461e1c
-        /*
461e1c
-         * SecureRandom engines
461e1c
-         */
461e1c
-        attrs.put("ThreadSafe", "true");
461e1c
-        if (NativePRNG.isAvailable()) {
461e1c
-            add(p, "SecureRandom", "NativePRNG",
461e1c
-                    "sun.security.provider.NativePRNG", attrs);
461e1c
-        }
461e1c
-        if (NativePRNG.Blocking.isAvailable()) {
461e1c
-            add(p, "SecureRandom", "NativePRNGBlocking",
461e1c
-                    "sun.security.provider.NativePRNG$Blocking", attrs);
461e1c
-        }
461e1c
-        if (NativePRNG.NonBlocking.isAvailable()) {
461e1c
-            add(p, "SecureRandom", "NativePRNGNonBlocking",
461e1c
-                    "sun.security.provider.NativePRNG$NonBlocking", attrs);
461e1c
-        }
461e1c
-        attrs.put("ImplementedIn", "Software");
461e1c
-        add(p, "SecureRandom", "DRBG", "sun.security.provider.DRBG", attrs);
461e1c
-        add(p, "SecureRandom", "SHA1PRNG",
461e1c
-                "sun.security.provider.SecureRandom", attrs);
461e1c
-
461e1c
-        /*
461e1c
-         * Signature engines
461e1c
-         */
461e1c
-        attrs.clear();
461e1c
-        String dsaKeyClasses = "java.security.interfaces.DSAPublicKey" +
461e1c
-                "|java.security.interfaces.DSAPrivateKey";
461e1c
-        attrs.put("SupportedKeyClasses", dsaKeyClasses);
461e1c
-        attrs.put("ImplementedIn", "Software");
461e1c
-
461e1c
-        attrs.put("KeySize", "1024"); // for NONE and SHA1 DSA signatures
461e1c
-
461e1c
-        addWithAlias(p, "Signature", "SHA1withDSA",
461e1c
-                "sun.security.provider.DSA$SHA1withDSA", attrs);
461e1c
-        addWithAlias(p, "Signature", "NONEwithDSA",
461e1c
-                "sun.security.provider.DSA$RawDSA", attrs);
461e1c
-
461e1c
-        // for DSA signatures with 224/256-bit digests
461e1c
-        attrs.put("KeySize", "2048");
461e1c
-
461e1c
-        addWithAlias(p, "Signature", "SHA224withDSA",
461e1c
-                "sun.security.provider.DSA$SHA224withDSA", attrs);
461e1c
-        addWithAlias(p, "Signature", "SHA256withDSA",
461e1c
-                "sun.security.provider.DSA$SHA256withDSA", attrs);
461e1c
-
461e1c
-        addWithAlias(p, "Signature", "SHA3-224withDSA",
461e1c
-                "sun.security.provider.DSA$SHA3_224withDSA", attrs);
461e1c
-        addWithAlias(p, "Signature", "SHA3-256withDSA",
461e1c
-                "sun.security.provider.DSA$SHA3_256withDSA", attrs);
461e1c
-
461e1c
-        attrs.put("KeySize", "3072"); // for DSA sig using 384/512-bit digests
461e1c
-
461e1c
-        addWithAlias(p, "Signature", "SHA384withDSA",
461e1c
-                "sun.security.provider.DSA$SHA384withDSA", attrs);
461e1c
-        addWithAlias(p, "Signature", "SHA512withDSA",
461e1c
-                "sun.security.provider.DSA$SHA512withDSA", attrs);
461e1c
-        addWithAlias(p, "Signature", "SHA3-384withDSA",
461e1c
-                "sun.security.provider.DSA$SHA3_384withDSA", attrs);
461e1c
-        addWithAlias(p, "Signature", "SHA3-512withDSA",
461e1c
-                "sun.security.provider.DSA$SHA3_512withDSA", attrs);
461e1c
-
461e1c
-        attrs.remove("KeySize");
461e1c
+        if (!systemFipsEnabled) {
461e1c
+            /*
461e1c
+             * SecureRandom engines
461e1c
+             */
461e1c
+            attrs.put("ThreadSafe", "true");
461e1c
+            if (NativePRNG.isAvailable()) {
461e1c
+                add(p, "SecureRandom", "NativePRNG",
461e1c
+                        "sun.security.provider.NativePRNG", attrs);
461e1c
+            }
461e1c
+            if (NativePRNG.Blocking.isAvailable()) {
461e1c
+                add(p, "SecureRandom", "NativePRNGBlocking",
461e1c
+                        "sun.security.provider.NativePRNG$Blocking", attrs);
461e1c
+            }
461e1c
+            if (NativePRNG.NonBlocking.isAvailable()) {
461e1c
+                add(p, "SecureRandom", "NativePRNGNonBlocking",
461e1c
+                        "sun.security.provider.NativePRNG$NonBlocking", attrs);
461e1c
+            }
461e1c
+            attrs.put("ImplementedIn", "Software");
461e1c
+            add(p, "SecureRandom", "DRBG", "sun.security.provider.DRBG", attrs);
461e1c
+            add(p, "SecureRandom", "SHA1PRNG",
461e1c
+                    "sun.security.provider.SecureRandom", attrs);
461e1c
 
461e1c
-        add(p, "Signature", "SHA1withDSAinP1363Format",
461e1c
-                "sun.security.provider.DSA$SHA1withDSAinP1363Format");
461e1c
-        add(p, "Signature", "NONEwithDSAinP1363Format",
461e1c
-                "sun.security.provider.DSA$RawDSAinP1363Format");
461e1c
-        add(p, "Signature", "SHA224withDSAinP1363Format",
461e1c
-                "sun.security.provider.DSA$SHA224withDSAinP1363Format");
461e1c
-        add(p, "Signature", "SHA256withDSAinP1363Format",
461e1c
-                "sun.security.provider.DSA$SHA256withDSAinP1363Format");
461e1c
-        add(p, "Signature", "SHA384withDSAinP1363Format",
461e1c
-                "sun.security.provider.DSA$SHA384withDSAinP1363Format");
461e1c
-        add(p, "Signature", "SHA512withDSAinP1363Format",
461e1c
-                "sun.security.provider.DSA$SHA512withDSAinP1363Format");
461e1c
-        add(p, "Signature", "SHA3-224withDSAinP1363Format",
461e1c
-                "sun.security.provider.DSA$SHA3_224withDSAinP1363Format");
461e1c
-        add(p, "Signature", "SHA3-256withDSAinP1363Format",
461e1c
-                "sun.security.provider.DSA$SHA3_256withDSAinP1363Format");
461e1c
-        add(p, "Signature", "SHA3-384withDSAinP1363Format",
461e1c
-                "sun.security.provider.DSA$SHA3_384withDSAinP1363Format");
461e1c
-        add(p, "Signature", "SHA3-512withDSAinP1363Format",
461e1c
-                "sun.security.provider.DSA$SHA3_512withDSAinP1363Format");
461e1c
-        /*
461e1c
-         *  Key Pair Generator engines
461e1c
-         */
461e1c
-        attrs.clear();
461e1c
-        attrs.put("ImplementedIn", "Software");
461e1c
-        attrs.put("KeySize", "2048"); // for DSA KPG and APG only
461e1c
+            /*
461e1c
+             * Signature engines
461e1c
+             */
461e1c
+            attrs.clear();
461e1c
+            String dsaKeyClasses = "java.security.interfaces.DSAPublicKey" +
461e1c
+                    "|java.security.interfaces.DSAPrivateKey";
461e1c
+            attrs.put("SupportedKeyClasses", dsaKeyClasses);
461e1c
+            attrs.put("ImplementedIn", "Software");
461e1c
+
461e1c
+            attrs.put("KeySize", "1024"); // for NONE and SHA1 DSA signatures
461e1c
+
461e1c
+            addWithAlias(p, "Signature", "SHA1withDSA",
461e1c
+                    "sun.security.provider.DSA$SHA1withDSA", attrs);
461e1c
+            addWithAlias(p, "Signature", "NONEwithDSA",
461e1c
+                    "sun.security.provider.DSA$RawDSA", attrs);
461e1c
+
461e1c
+            // for DSA signatures with 224/256-bit digests
461e1c
+            attrs.put("KeySize", "2048");
461e1c
+
461e1c
+            addWithAlias(p, "Signature", "SHA224withDSA",
461e1c
+                    "sun.security.provider.DSA$SHA224withDSA", attrs);
461e1c
+            addWithAlias(p, "Signature", "SHA256withDSA",
461e1c
+                    "sun.security.provider.DSA$SHA256withDSA", attrs);
461e1c
+
461e1c
+            addWithAlias(p, "Signature", "SHA3-224withDSA",
461e1c
+                    "sun.security.provider.DSA$SHA3_224withDSA", attrs);
461e1c
+            addWithAlias(p, "Signature", "SHA3-256withDSA",
461e1c
+                    "sun.security.provider.DSA$SHA3_256withDSA", attrs);
461e1c
+
461e1c
+            attrs.put("KeySize", "3072"); // for DSA sig using 384/512-bit digests
461e1c
+
461e1c
+            addWithAlias(p, "Signature", "SHA384withDSA",
461e1c
+                    "sun.security.provider.DSA$SHA384withDSA", attrs);
461e1c
+            addWithAlias(p, "Signature", "SHA512withDSA",
461e1c
+                    "sun.security.provider.DSA$SHA512withDSA", attrs);
461e1c
+            addWithAlias(p, "Signature", "SHA3-384withDSA",
461e1c
+                    "sun.security.provider.DSA$SHA3_384withDSA", attrs);
461e1c
+            addWithAlias(p, "Signature", "SHA3-512withDSA",
461e1c
+                    "sun.security.provider.DSA$SHA3_512withDSA", attrs);
461e1c
+
461e1c
+            attrs.remove("KeySize");
461e1c
+
461e1c
+            add(p, "Signature", "SHA1withDSAinP1363Format",
461e1c
+                    "sun.security.provider.DSA$SHA1withDSAinP1363Format");
461e1c
+            add(p, "Signature", "NONEwithDSAinP1363Format",
461e1c
+                    "sun.security.provider.DSA$RawDSAinP1363Format");
461e1c
+            add(p, "Signature", "SHA224withDSAinP1363Format",
461e1c
+                    "sun.security.provider.DSA$SHA224withDSAinP1363Format");
461e1c
+            add(p, "Signature", "SHA256withDSAinP1363Format",
461e1c
+                    "sun.security.provider.DSA$SHA256withDSAinP1363Format");
461e1c
+            add(p, "Signature", "SHA384withDSAinP1363Format",
461e1c
+                    "sun.security.provider.DSA$SHA384withDSAinP1363Format");
461e1c
+            add(p, "Signature", "SHA512withDSAinP1363Format",
461e1c
+                    "sun.security.provider.DSA$SHA512withDSAinP1363Format");
461e1c
+            add(p, "Signature", "SHA3-224withDSAinP1363Format",
461e1c
+                    "sun.security.provider.DSA$SHA3_224withDSAinP1363Format");
461e1c
+            add(p, "Signature", "SHA3-256withDSAinP1363Format",
461e1c
+                    "sun.security.provider.DSA$SHA3_256withDSAinP1363Format");
461e1c
+            add(p, "Signature", "SHA3-384withDSAinP1363Format",
461e1c
+                    "sun.security.provider.DSA$SHA3_384withDSAinP1363Format");
461e1c
+            add(p, "Signature", "SHA3-512withDSAinP1363Format",
461e1c
+                    "sun.security.provider.DSA$SHA3_512withDSAinP1363Format");
461e1c
+            /*
461e1c
+             *  Key Pair Generator engines
461e1c
+             */
461e1c
+            attrs.clear();
461e1c
+            attrs.put("ImplementedIn", "Software");
461e1c
+            attrs.put("KeySize", "2048"); // for DSA KPG and APG only
461e1c
 
461e1c
-        String dsaKPGImplClass = "sun.security.provider.DSAKeyPairGenerator$";
461e1c
-        dsaKPGImplClass += (useLegacyDSA? "Legacy" : "Current");
461e1c
-        addWithAlias(p, "KeyPairGenerator", "DSA", dsaKPGImplClass, attrs);
461e1c
+            String dsaKPGImplClass = "sun.security.provider.DSAKeyPairGenerator$";
461e1c
+            dsaKPGImplClass += (useLegacyDSA? "Legacy" : "Current");
461e1c
+            addWithAlias(p, "KeyPairGenerator", "DSA", dsaKPGImplClass, attrs);
461e1c
+        }
461e1c
 
461e1c
         /*
461e1c
          * Algorithm Parameter Generator engines
461e1c
@@ -201,40 +208,42 @@ public final class SunEntries {
461e1c
         addWithAlias(p, "AlgorithmParameters", "DSA",
461e1c
                 "sun.security.provider.DSAParameters", attrs);
461e1c
 
461e1c
-        /*
461e1c
-         * Key factories
461e1c
-         */
461e1c
-        addWithAlias(p, "KeyFactory", "DSA",
461e1c
-                "sun.security.provider.DSAKeyFactory", attrs);
461e1c
-
461e1c
-        /*
461e1c
-         * Digest engines
461e1c
-         */
461e1c
-        add(p, "MessageDigest", "MD2", "sun.security.provider.MD2", attrs);
461e1c
-        add(p, "MessageDigest", "MD5", "sun.security.provider.MD5", attrs);
461e1c
-        addWithAlias(p, "MessageDigest", "SHA-1", "sun.security.provider.SHA",
461e1c
-                attrs);
461e1c
+        if (!systemFipsEnabled) {
461e1c
+            /*
461e1c
+             * Key factories
461e1c
+             */
461e1c
+            addWithAlias(p, "KeyFactory", "DSA",
461e1c
+                    "sun.security.provider.DSAKeyFactory", attrs);
461e1c
 
461e1c
-        addWithAlias(p, "MessageDigest", "SHA-224",
461e1c
-                "sun.security.provider.SHA2$SHA224", attrs);
461e1c
-        addWithAlias(p, "MessageDigest", "SHA-256",
461e1c
-                "sun.security.provider.SHA2$SHA256", attrs);
461e1c
-        addWithAlias(p, "MessageDigest", "SHA-384",
461e1c
-                "sun.security.provider.SHA5$SHA384", attrs);
461e1c
-        addWithAlias(p, "MessageDigest", "SHA-512",
461e1c
-                "sun.security.provider.SHA5$SHA512", attrs);
461e1c
-        addWithAlias(p, "MessageDigest", "SHA-512/224",
461e1c
-                "sun.security.provider.SHA5$SHA512_224", attrs);
461e1c
-        addWithAlias(p, "MessageDigest", "SHA-512/256",
461e1c
-                "sun.security.provider.SHA5$SHA512_256", attrs);
461e1c
-        addWithAlias(p, "MessageDigest", "SHA3-224",
461e1c
-                "sun.security.provider.SHA3$SHA224", attrs);
461e1c
-        addWithAlias(p, "MessageDigest", "SHA3-256",
461e1c
-                "sun.security.provider.SHA3$SHA256", attrs);
461e1c
-        addWithAlias(p, "MessageDigest", "SHA3-384",
461e1c
-                "sun.security.provider.SHA3$SHA384", attrs);
461e1c
-        addWithAlias(p, "MessageDigest", "SHA3-512",
461e1c
-                "sun.security.provider.SHA3$SHA512", attrs);
461e1c
+            /*
461e1c
+             * Digest engines
461e1c
+             */
461e1c
+            add(p, "MessageDigest", "MD2", "sun.security.provider.MD2", attrs);
461e1c
+            add(p, "MessageDigest", "MD5", "sun.security.provider.MD5", attrs);
461e1c
+            addWithAlias(p, "MessageDigest", "SHA-1", "sun.security.provider.SHA",
461e1c
+                    attrs);
461e1c
+
461e1c
+            addWithAlias(p, "MessageDigest", "SHA-224",
461e1c
+                    "sun.security.provider.SHA2$SHA224", attrs);
461e1c
+            addWithAlias(p, "MessageDigest", "SHA-256",
461e1c
+                    "sun.security.provider.SHA2$SHA256", attrs);
461e1c
+            addWithAlias(p, "MessageDigest", "SHA-384",
461e1c
+                    "sun.security.provider.SHA5$SHA384", attrs);
461e1c
+            addWithAlias(p, "MessageDigest", "SHA-512",
461e1c
+                    "sun.security.provider.SHA5$SHA512", attrs);
461e1c
+            addWithAlias(p, "MessageDigest", "SHA-512/224",
461e1c
+                    "sun.security.provider.SHA5$SHA512_224", attrs);
461e1c
+            addWithAlias(p, "MessageDigest", "SHA-512/256",
461e1c
+                    "sun.security.provider.SHA5$SHA512_256", attrs);
461e1c
+            addWithAlias(p, "MessageDigest", "SHA3-224",
461e1c
+                    "sun.security.provider.SHA3$SHA224", attrs);
461e1c
+            addWithAlias(p, "MessageDigest", "SHA3-256",
461e1c
+                    "sun.security.provider.SHA3$SHA256", attrs);
461e1c
+            addWithAlias(p, "MessageDigest", "SHA3-384",
461e1c
+                    "sun.security.provider.SHA3$SHA384", attrs);
461e1c
+            addWithAlias(p, "MessageDigest", "SHA3-512",
461e1c
+                    "sun.security.provider.SHA3$SHA512", attrs);
461e1c
+        }
461e1c
 
461e1c
         /*
461e1c
          * Certificates
461e1c
diff --git a/src/java.base/share/classes/sun/security/rsa/SunRsaSignEntries.java b/src/java.base/share/classes/sun/security/rsa/SunRsaSignEntries.java
461e1c
index ca79f25cc44..225517ac69b 100644
461e1c
--- a/src/java.base/share/classes/sun/security/rsa/SunRsaSignEntries.java
461e1c
+++ b/src/java.base/share/classes/sun/security/rsa/SunRsaSignEntries.java
461e1c
@@ -27,6 +27,7 @@ package sun.security.rsa;
461e1c
 
461e1c
 import java.util.*;
461e1c
 import java.security.Provider;
461e1c
+import jdk.internal.access.SharedSecrets;
461e1c
 import static sun.security.util.SecurityProviderConstants.getAliases;
461e1c
 
461e1c
 /**
461e1c
@@ -36,6 +37,10 @@ import static sun.security.util.SecurityProviderConstants.getAliases;
461e1c
  */
461e1c
 public final class SunRsaSignEntries {
461e1c
 
461e1c
+    private static final boolean systemFipsEnabled =
461e1c
+            SharedSecrets.getJavaSecuritySystemConfiguratorAccess()
461e1c
+            .isSystemFipsEnabled();
461e1c
+
461e1c
     private void add(Provider p, String type, String algo, String cn,
461e1c
              List<String> aliases, HashMap<String, String> attrs) {
461e1c
          services.add(new Provider.Service(p, type, algo, cn,
461e1c
@@ -56,49 +61,58 @@ public final class SunRsaSignEntries {
461e1c
         // start populating content using the specified provider
461e1c
         // common attribute map
461e1c
         HashMap<String, String> attrs = new HashMap<>(3);
461e1c
-        attrs.put("SupportedKeyClasses",
461e1c
-                "java.security.interfaces.RSAPublicKey" +
461e1c
-                "|java.security.interfaces.RSAPrivateKey");
461e1c
+        if (!systemFipsEnabled) {
461e1c
+            attrs.put("SupportedKeyClasses",
461e1c
+                    "java.security.interfaces.RSAPublicKey" +
461e1c
+                    "|java.security.interfaces.RSAPrivateKey");
461e1c
+        }
461e1c
 
461e1c
         add(p, "KeyFactory", "RSA",
461e1c
                 "sun.security.rsa.RSAKeyFactory$Legacy",
461e1c
                 getAliases("PKCS1"), null);
461e1c
-        add(p, "KeyPairGenerator", "RSA",
461e1c
-                "sun.security.rsa.RSAKeyPairGenerator$Legacy",
461e1c
-                getAliases("PKCS1"), null);
461e1c
-        addA(p, "Signature", "MD2withRSA",
461e1c
-                "sun.security.rsa.RSASignature$MD2withRSA", attrs);
461e1c
-        addA(p, "Signature", "MD5withRSA",
461e1c
-                "sun.security.rsa.RSASignature$MD5withRSA", attrs);
461e1c
-        addA(p, "Signature", "SHA1withRSA",
461e1c
-                "sun.security.rsa.RSASignature$SHA1withRSA", attrs);
461e1c
-        addA(p, "Signature", "SHA224withRSA",
461e1c
-                "sun.security.rsa.RSASignature$SHA224withRSA", attrs);
461e1c
-        addA(p, "Signature", "SHA256withRSA",
461e1c
-                "sun.security.rsa.RSASignature$SHA256withRSA", attrs);
461e1c
-        addA(p, "Signature", "SHA384withRSA",
461e1c
-                "sun.security.rsa.RSASignature$SHA384withRSA", attrs);
461e1c
-        addA(p, "Signature", "SHA512withRSA",
461e1c
-                "sun.security.rsa.RSASignature$SHA512withRSA", attrs);
461e1c
-        addA(p, "Signature", "SHA512/224withRSA",
461e1c
-                "sun.security.rsa.RSASignature$SHA512_224withRSA", attrs);
461e1c
-        addA(p, "Signature", "SHA512/256withRSA",
461e1c
-                "sun.security.rsa.RSASignature$SHA512_256withRSA", attrs);
461e1c
-        addA(p, "Signature", "SHA3-224withRSA",
461e1c
-                "sun.security.rsa.RSASignature$SHA3_224withRSA", attrs);
461e1c
-        addA(p, "Signature", "SHA3-256withRSA",
461e1c
-                "sun.security.rsa.RSASignature$SHA3_256withRSA", attrs);
461e1c
-        addA(p, "Signature", "SHA3-384withRSA",
461e1c
-               "sun.security.rsa.RSASignature$SHA3_384withRSA", attrs);
461e1c
-        addA(p, "Signature", "SHA3-512withRSA",
461e1c
-                "sun.security.rsa.RSASignature$SHA3_512withRSA", attrs);
461e1c
+
461e1c
+        if (!systemFipsEnabled) {
461e1c
+            add(p, "KeyPairGenerator", "RSA",
461e1c
+                    "sun.security.rsa.RSAKeyPairGenerator$Legacy",
461e1c
+                    getAliases("PKCS1"), null);
461e1c
+            addA(p, "Signature", "MD2withRSA",
461e1c
+                    "sun.security.rsa.RSASignature$MD2withRSA", attrs);
461e1c
+            addA(p, "Signature", "MD5withRSA",
461e1c
+                    "sun.security.rsa.RSASignature$MD5withRSA", attrs);
461e1c
+            addA(p, "Signature", "SHA1withRSA",
461e1c
+                    "sun.security.rsa.RSASignature$SHA1withRSA", attrs);
461e1c
+            addA(p, "Signature", "SHA224withRSA",
461e1c
+                    "sun.security.rsa.RSASignature$SHA224withRSA", attrs);
461e1c
+            addA(p, "Signature", "SHA256withRSA",
461e1c
+                    "sun.security.rsa.RSASignature$SHA256withRSA", attrs);
461e1c
+            addA(p, "Signature", "SHA384withRSA",
461e1c
+                    "sun.security.rsa.RSASignature$SHA384withRSA", attrs);
461e1c
+            addA(p, "Signature", "SHA512withRSA",
461e1c
+                    "sun.security.rsa.RSASignature$SHA512withRSA", attrs);
461e1c
+            addA(p, "Signature", "SHA512/224withRSA",
461e1c
+                    "sun.security.rsa.RSASignature$SHA512_224withRSA", attrs);
461e1c
+            addA(p, "Signature", "SHA512/256withRSA",
461e1c
+                    "sun.security.rsa.RSASignature$SHA512_256withRSA", attrs);
461e1c
+            addA(p, "Signature", "SHA3-224withRSA",
461e1c
+                    "sun.security.rsa.RSASignature$SHA3_224withRSA", attrs);
461e1c
+            addA(p, "Signature", "SHA3-256withRSA",
461e1c
+                    "sun.security.rsa.RSASignature$SHA3_256withRSA", attrs);
461e1c
+            addA(p, "Signature", "SHA3-384withRSA",
461e1c
+                   "sun.security.rsa.RSASignature$SHA3_384withRSA", attrs);
461e1c
+            addA(p, "Signature", "SHA3-512withRSA",
461e1c
+                    "sun.security.rsa.RSASignature$SHA3_512withRSA", attrs);
461e1c
+        }
461e1c
 
461e1c
         addA(p, "KeyFactory", "RSASSA-PSS",
461e1c
                 "sun.security.rsa.RSAKeyFactory$PSS", attrs);
461e1c
-        addA(p, "KeyPairGenerator", "RSASSA-PSS",
461e1c
-                "sun.security.rsa.RSAKeyPairGenerator$PSS", attrs);
461e1c
-        addA(p, "Signature", "RSASSA-PSS",
461e1c
-                "sun.security.rsa.RSAPSSSignature", attrs);
461e1c
+
461e1c
+        if (!systemFipsEnabled) {
461e1c
+            addA(p, "KeyPairGenerator", "RSASSA-PSS",
461e1c
+                    "sun.security.rsa.RSAKeyPairGenerator$PSS", attrs);
461e1c
+            addA(p, "Signature", "RSASSA-PSS",
461e1c
+                    "sun.security.rsa.RSAPSSSignature", attrs);
461e1c
+        }
461e1c
+
461e1c
         addA(p, "AlgorithmParameters", "RSASSA-PSS",
461e1c
                 "sun.security.rsa.PSSParameters", null);
461e1c
     }
461e1c
diff --git a/src/java.base/share/classes/sun/security/ssl/SSLContextImpl.java b/src/java.base/share/classes/sun/security/ssl/SSLContextImpl.java
461e1c
index 6ffdfeda18d..775b185fb06 100644
461e1c
--- a/src/java.base/share/classes/sun/security/ssl/SSLContextImpl.java
461e1c
+++ b/src/java.base/share/classes/sun/security/ssl/SSLContextImpl.java
461e1c
@@ -32,6 +32,7 @@ import java.security.cert.*;
461e1c
 import java.util.*;
461e1c
 import java.util.concurrent.locks.ReentrantLock;
461e1c
 import javax.net.ssl.*;
461e1c
+import jdk.internal.access.SharedSecrets;
461e1c
 import sun.security.action.GetPropertyAction;
461e1c
 import sun.security.provider.certpath.AlgorithmChecker;
461e1c
 import sun.security.validator.Validator;
461e1c
@@ -536,22 +537,40 @@ public abstract class SSLContextImpl extends SSLContextSpi {
461e1c
         private static final List<CipherSuite> serverDefaultCipherSuites;
461e1c
 
461e1c
         static {
461e1c
-            supportedProtocols = Arrays.asList(
461e1c
-                ProtocolVersion.TLS13,
461e1c
-                ProtocolVersion.TLS12,
461e1c
-                ProtocolVersion.TLS11,
461e1c
-                ProtocolVersion.TLS10,
461e1c
-                ProtocolVersion.SSL30,
461e1c
-                ProtocolVersion.SSL20Hello
461e1c
-            );
461e1c
-
461e1c
-            serverDefaultProtocols = getAvailableProtocols(
461e1c
-                    new ProtocolVersion[] {
461e1c
-                ProtocolVersion.TLS13,
461e1c
-                ProtocolVersion.TLS12,
461e1c
-                ProtocolVersion.TLS11,
461e1c
-                ProtocolVersion.TLS10
461e1c
-            });
461e1c
+            if (SharedSecrets.getJavaSecuritySystemConfiguratorAccess()
461e1c
+                    .isSystemFipsEnabled()) {
461e1c
+                // RH1860986: TLSv1.3 key derivation not supported with
461e1c
+                // the Security Providers available in system FIPS mode.
461e1c
+                supportedProtocols = Arrays.asList(
461e1c
+                    ProtocolVersion.TLS12,
461e1c
+                    ProtocolVersion.TLS11,
461e1c
+                    ProtocolVersion.TLS10
461e1c
+                );
461e1c
+
461e1c
+                serverDefaultProtocols = getAvailableProtocols(
461e1c
+                        new ProtocolVersion[] {
461e1c
+                    ProtocolVersion.TLS12,
461e1c
+                    ProtocolVersion.TLS11,
461e1c
+                    ProtocolVersion.TLS10
461e1c
+                });
461e1c
+            } else {
461e1c
+                supportedProtocols = Arrays.asList(
461e1c
+                    ProtocolVersion.TLS13,
461e1c
+                    ProtocolVersion.TLS12,
461e1c
+                    ProtocolVersion.TLS11,
461e1c
+                    ProtocolVersion.TLS10,
461e1c
+                    ProtocolVersion.SSL30,
461e1c
+                    ProtocolVersion.SSL20Hello
461e1c
+                );
461e1c
+
461e1c
+                serverDefaultProtocols = getAvailableProtocols(
461e1c
+                        new ProtocolVersion[] {
461e1c
+                    ProtocolVersion.TLS13,
461e1c
+                    ProtocolVersion.TLS12,
461e1c
+                    ProtocolVersion.TLS11,
461e1c
+                    ProtocolVersion.TLS10
461e1c
+                });
461e1c
+            }
461e1c
 
461e1c
             supportedCipherSuites = getApplicableSupportedCipherSuites(
461e1c
                     supportedProtocols);
461e1c
@@ -842,12 +861,23 @@ public abstract class SSLContextImpl extends SSLContextSpi {
461e1c
             ProtocolVersion[] candidates;
461e1c
             if (refactored.isEmpty()) {
461e1c
                 // Client and server use the same default protocols.
461e1c
-                candidates = new ProtocolVersion[] {
461e1c
-                        ProtocolVersion.TLS13,
461e1c
-                        ProtocolVersion.TLS12,
461e1c
-                        ProtocolVersion.TLS11,
461e1c
-                        ProtocolVersion.TLS10
461e1c
-                    };
461e1c
+                if (SharedSecrets.getJavaSecuritySystemConfiguratorAccess()
461e1c
+                        .isSystemFipsEnabled()) {
461e1c
+                    // RH1860986: TLSv1.3 key derivation not supported with
461e1c
+                    // the Security Providers available in system FIPS mode.
461e1c
+                    candidates = new ProtocolVersion[] {
461e1c
+                            ProtocolVersion.TLS12,
461e1c
+                            ProtocolVersion.TLS11,
461e1c
+                            ProtocolVersion.TLS10
461e1c
+                        };
461e1c
+                } else {
461e1c
+                    candidates = new ProtocolVersion[] {
461e1c
+                            ProtocolVersion.TLS13,
461e1c
+                            ProtocolVersion.TLS12,
461e1c
+                            ProtocolVersion.TLS11,
461e1c
+                            ProtocolVersion.TLS10
461e1c
+                        };
461e1c
+                }
461e1c
             } else {
461e1c
                 // Use the customized TLS protocols.
461e1c
                 candidates =
461e1c
diff --git a/src/java.base/share/classes/sun/security/ssl/SunJSSE.java b/src/java.base/share/classes/sun/security/ssl/SunJSSE.java
461e1c
index 894e26dfad8..8b16378b96b 100644
461e1c
--- a/src/java.base/share/classes/sun/security/ssl/SunJSSE.java
461e1c
+++ b/src/java.base/share/classes/sun/security/ssl/SunJSSE.java
461e1c
@@ -27,6 +27,8 @@ package sun.security.ssl;
461e1c
 
461e1c
 import java.security.*;
461e1c
 import java.util.*;
461e1c
+
461e1c
+import jdk.internal.access.SharedSecrets;
461e1c
 import static sun.security.util.SecurityConstants.PROVIDER_VER;
461e1c
 
461e1c
 /**
461e1c
@@ -102,8 +104,13 @@ public class SunJSSE extends java.security.Provider {
461e1c
             "sun.security.ssl.SSLContextImpl$TLS11Context", null, null);
461e1c
         ps("SSLContext", "TLSv1.2",
461e1c
             "sun.security.ssl.SSLContextImpl$TLS12Context", null, null);
461e1c
-        ps("SSLContext", "TLSv1.3",
461e1c
-            "sun.security.ssl.SSLContextImpl$TLS13Context", null, null);
461e1c
+        if (!SharedSecrets.getJavaSecuritySystemConfiguratorAccess()
461e1c
+                .isSystemFipsEnabled()) {
461e1c
+            // RH1860986: TLSv1.3 key derivation not supported with
461e1c
+            // the Security Providers available in system FIPS mode.
461e1c
+            ps("SSLContext", "TLSv1.3",
461e1c
+                "sun.security.ssl.SSLContextImpl$TLS13Context", null, null);
461e1c
+        }
461e1c
         ps("SSLContext", "TLS",
461e1c
             "sun.security.ssl.SSLContextImpl$TLSContext",
461e1c
             List.of("SSL"), null);
461e1c
diff --git a/src/java.base/share/conf/security/java.security b/src/java.base/share/conf/security/java.security
461e1c
index 6d91e3f8e4e..adfaf57d29e 100644
461e1c
--- a/src/java.base/share/conf/security/java.security
461e1c
+++ b/src/java.base/share/conf/security/java.security
461e1c
@@ -79,6 +79,16 @@ security.provider.tbd=Apple
461e1c
 #endif
461e1c
 security.provider.tbd=SunPKCS11
461e1c
 
461e1c
+#
461e1c
+# Security providers used when FIPS mode support is active
461e1c
+#
461e1c
+fips.provider.1=SunPKCS11 ${java.home}/conf/security/nss.fips.cfg
461e1c
+fips.provider.2=SUN
461e1c
+fips.provider.3=SunEC
461e1c
+fips.provider.4=SunJSSE
461e1c
+fips.provider.5=SunJCE
461e1c
+fips.provider.6=SunRsaSign
461e1c
+
461e1c
 #
461e1c
 # A list of preferred providers for specific algorithms. These providers will
461e1c
 # be searched for matching algorithms before the list of registered providers.
461e1c
@@ -289,6 +299,11 @@ policy.ignoreIdentityScope=false
461e1c
 #
461e1c
 keystore.type=pkcs12
461e1c
 
461e1c
+#
461e1c
+# Default keystore type used when global crypto-policies are set to FIPS.
461e1c
+#
461e1c
+fips.keystore.type=PKCS11
461e1c
+
461e1c
 #
461e1c
 # Controls compatibility mode for JKS and PKCS12 keystore types.
461e1c
 #
461e1c
@@ -326,6 +341,13 @@ package.definition=sun.misc.,\
461e1c
 #
461e1c
 security.overridePropertiesFile=true
461e1c
 
461e1c
+#
461e1c
+# Determines whether this properties file will be appended to
461e1c
+# using the system properties file stored at
461e1c
+# /etc/crypto-policies/back-ends/java.config
461e1c
+#
461e1c
+security.useSystemPropertiesFile=false
461e1c
+
461e1c
 #
461e1c
 # Determines the default key and trust manager factory algorithms for
461e1c
 # the javax.net.ssl package.
461e1c
diff --git a/src/java.base/share/lib/security/default.policy b/src/java.base/share/lib/security/default.policy
461e1c
index b22f26947af..3ee2ce6ea88 100644
461e1c
--- a/src/java.base/share/lib/security/default.policy
461e1c
+++ b/src/java.base/share/lib/security/default.policy
461e1c
@@ -121,6 +121,7 @@ grant codeBase "jrt:/jdk.charsets" {
461e1c
 grant codeBase "jrt:/jdk.crypto.ec" {
461e1c
     permission java.lang.RuntimePermission
461e1c
                    "accessClassInPackage.sun.security.*";
461e1c
+    permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.access";
461e1c
     permission java.lang.RuntimePermission "loadLibrary.sunec";
461e1c
     permission java.security.SecurityPermission "putProviderProperty.SunEC";
461e1c
     permission java.security.SecurityPermission "clearProviderProperties.SunEC";
461e1c
@@ -130,6 +131,7 @@ grant codeBase "jrt:/jdk.crypto.ec" {
461e1c
 grant codeBase "jrt:/jdk.crypto.cryptoki" {
461e1c
     permission java.lang.RuntimePermission
461e1c
                    "accessClassInPackage.com.sun.crypto.provider";
461e1c
+    permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.access";
461e1c
     permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.misc";
461e1c
     permission java.lang.RuntimePermission
461e1c
                    "accessClassInPackage.sun.security.*";
461e1c
diff --git a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/FIPSKeyImporter.java b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/FIPSKeyImporter.java
461e1c
new file mode 100644
461e1c
index 00000000000..9bb31555f48
461e1c
--- /dev/null
461e1c
+++ b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/FIPSKeyImporter.java
461e1c
@@ -0,0 +1,490 @@
461e1c
+/*
461e1c
+ * Copyright (c) 2021, Red Hat, Inc.
461e1c
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
461e1c
+ *
461e1c
+ * This code is free software; you can redistribute it and/or modify it
461e1c
+ * under the terms of the GNU General Public License version 2 only, as
461e1c
+ * published by the Free Software Foundation.  Oracle designates this
461e1c
+ * particular file as subject to the "Classpath" exception as provided
461e1c
+ * by Oracle in the LICENSE file that accompanied this code.
461e1c
+ *
461e1c
+ * This code is distributed in the hope that it will be useful, but WITHOUT
461e1c
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
461e1c
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
461e1c
+ * version 2 for more details (a copy is included in the LICENSE file that
461e1c
+ * accompanied this code).
461e1c
+ *
461e1c
+ * You should have received a copy of the GNU General Public License version
461e1c
+ * 2 along with this work; if not, write to the Free Software Foundation,
461e1c
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
461e1c
+ *
461e1c
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
461e1c
+ * or visit www.oracle.com if you need additional information or have any
461e1c
+ * questions.
461e1c
+ */
461e1c
+
461e1c
+package sun.security.pkcs11;
461e1c
+
461e1c
+import java.math.BigInteger;
461e1c
+import java.security.KeyFactory;
461e1c
+import java.security.Provider;
461e1c
+import java.security.Security;
461e1c
+import java.security.interfaces.RSAPrivateCrtKey;
461e1c
+import java.security.interfaces.RSAPrivateKey;
461e1c
+import java.util.HashMap;
461e1c
+import java.util.Map;
461e1c
+import java.util.concurrent.locks.ReentrantLock;
461e1c
+
461e1c
+import javax.crypto.Cipher;
461e1c
+import javax.crypto.SecretKeyFactory;
461e1c
+import javax.crypto.spec.SecretKeySpec;
461e1c
+import javax.crypto.spec.DHPrivateKeySpec;
461e1c
+import javax.crypto.spec.IvParameterSpec;
461e1c
+
461e1c
+import sun.security.jca.JCAUtil;
461e1c
+import sun.security.pkcs11.TemplateManager;
461e1c
+import sun.security.pkcs11.wrapper.CK_ATTRIBUTE;
461e1c
+import sun.security.pkcs11.wrapper.CK_MECHANISM;
461e1c
+import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
461e1c
+import static sun.security.pkcs11.wrapper.PKCS11Exception.*;
461e1c
+import sun.security.pkcs11.wrapper.PKCS11Exception;
461e1c
+import sun.security.rsa.RSAPrivateCrtKeyImpl;
461e1c
+import sun.security.rsa.RSAUtil;
461e1c
+import sun.security.rsa.RSAUtil.KeyType;
461e1c
+import sun.security.util.Debug;
461e1c
+import sun.security.util.ECUtil;
461e1c
+
461e1c
+final class FIPSKeyImporter {
461e1c
+
461e1c
+    private static final Debug debug =
461e1c
+            Debug.getInstance("sunpkcs11");
461e1c
+
461e1c
+    private static volatile P11Key importerKey = null;
461e1c
+    private static SecretKeySpec exporterKey = null;
461e1c
+    private static volatile P11Key exporterKeyP11 = null;
461e1c
+    private static final ReentrantLock importerKeyLock = new ReentrantLock();
461e1c
+    // Do not take the exporterKeyLock with the importerKeyLock held.
461e1c
+    private static final ReentrantLock exporterKeyLock = new ReentrantLock();
461e1c
+    private static volatile CK_MECHANISM importerKeyMechanism = null;
461e1c
+    private static volatile CK_MECHANISM exporterKeyMechanism = null;
461e1c
+    private static Cipher importerCipher = null;
461e1c
+    private static Cipher exporterCipher = null;
461e1c
+
461e1c
+    private static volatile Provider sunECProvider = null;
461e1c
+    private static final ReentrantLock sunECProviderLock = new ReentrantLock();
461e1c
+
461e1c
+    private static volatile KeyFactory DHKF = null;
461e1c
+    private static final ReentrantLock DHKFLock = new ReentrantLock();
461e1c
+
461e1c
+    static Long importKey(SunPKCS11 sunPKCS11, long hSession, CK_ATTRIBUTE[] attributes)
461e1c
+            throws PKCS11Exception {
461e1c
+        long keyID = -1;
461e1c
+        Token token = sunPKCS11.getToken();
461e1c
+        if (debug != null) {
461e1c
+            debug.println("Private or Secret key will be imported in" +
461e1c
+                    " system FIPS mode.");
461e1c
+        }
461e1c
+        if (importerKey == null) {
461e1c
+            importerKeyLock.lock();
461e1c
+            try {
461e1c
+                if (importerKey == null) {
461e1c
+                    if (importerKeyMechanism == null) {
461e1c
+                        // Importer Key creation has not been tried yet. Try it.
461e1c
+                        createImporterKey(token);
461e1c
+                    }
461e1c
+                    if (importerKey == null || importerCipher == null) {
461e1c
+                        if (debug != null) {
461e1c
+                            debug.println("Importer Key could not be" +
461e1c
+                                    " generated.");
461e1c
+                        }
461e1c
+                        throw new PKCS11Exception(CKR_GENERAL_ERROR,
461e1c
+                                " fips key importer");
461e1c
+                    }
461e1c
+                    if (debug != null) {
461e1c
+                        debug.println("Importer Key successfully" +
461e1c
+                                " generated.");
461e1c
+                    }
461e1c
+                }
461e1c
+            } finally {
461e1c
+                importerKeyLock.unlock();
461e1c
+            }
461e1c
+        }
461e1c
+        long importerKeyID = importerKey.getKeyID();
461e1c
+        try {
461e1c
+            byte[] keyBytes = null;
461e1c
+            byte[] encKeyBytes = null;
461e1c
+            long keyClass = 0L;
461e1c
+            long keyType = 0L;
461e1c
+            Map<Long, CK_ATTRIBUTE> attrsMap = new HashMap<>();
461e1c
+            for (CK_ATTRIBUTE attr : attributes) {
461e1c
+                if (attr.type == CKA_CLASS) {
461e1c
+                    keyClass = attr.getLong();
461e1c
+                } else if (attr.type == CKA_KEY_TYPE) {
461e1c
+                    keyType = attr.getLong();
461e1c
+                }
461e1c
+                attrsMap.put(attr.type, attr);
461e1c
+            }
461e1c
+            BigInteger v = null;
461e1c
+            if (keyClass == CKO_PRIVATE_KEY) {
461e1c
+                if (keyType == CKK_RSA) {
461e1c
+                    if (debug != null) {
461e1c
+                        debug.println("Importing an RSA private key...");
461e1c
+                    }
461e1c
+                    keyBytes = sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(
461e1c
+                            KeyType.RSA,
461e1c
+                            null,
461e1c
+                            ((v = attrsMap.get(CKA_MODULUS).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO,
461e1c
+                            ((v = attrsMap.get(CKA_PUBLIC_EXPONENT).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO,
461e1c
+                            ((v = attrsMap.get(CKA_PRIVATE_EXPONENT).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO,
461e1c
+                            ((v = attrsMap.get(CKA_PRIME_1).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO,
461e1c
+                            ((v = attrsMap.get(CKA_PRIME_2).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO,
461e1c
+                            ((v = attrsMap.get(CKA_EXPONENT_1).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO,
461e1c
+                            ((v = attrsMap.get(CKA_EXPONENT_2).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO,
461e1c
+                            ((v = attrsMap.get(CKA_COEFFICIENT).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO
461e1c
+                            ).getEncoded();
461e1c
+                } else if (keyType == CKK_DSA) {
461e1c
+                    if (debug != null) {
461e1c
+                        debug.println("Importing a DSA private key...");
461e1c
+                    }
461e1c
+                    keyBytes = new sun.security.provider.DSAPrivateKey(
461e1c
+                            ((v = attrsMap.get(CKA_VALUE).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO,
461e1c
+                            ((v = attrsMap.get(CKA_PRIME).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO,
461e1c
+                            ((v = attrsMap.get(CKA_SUBPRIME).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO,
461e1c
+                            ((v = attrsMap.get(CKA_BASE).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO
461e1c
+                            ).getEncoded();
461e1c
+                    if (token.config.getNssNetscapeDbWorkaround() &&
461e1c
+                            attrsMap.get(CKA_NETSCAPE_DB) == null) {
461e1c
+                        attrsMap.put(CKA_NETSCAPE_DB,
461e1c
+                                new CK_ATTRIBUTE(CKA_NETSCAPE_DB, BigInteger.ZERO));
461e1c
+                    }
461e1c
+                } else if (keyType == CKK_EC) {
461e1c
+                    if (debug != null) {
461e1c
+                        debug.println("Importing an EC private key...");
461e1c
+                    }
461e1c
+                    if (sunECProvider == null) {
461e1c
+                        sunECProviderLock.lock();
461e1c
+                        try {
461e1c
+                            if (sunECProvider == null) {
461e1c
+                                sunECProvider = Security.getProvider("SunEC");
461e1c
+                            }
461e1c
+                        } finally {
461e1c
+                            sunECProviderLock.unlock();
461e1c
+                        }
461e1c
+                    }
461e1c
+                    keyBytes = ECUtil.generateECPrivateKey(
461e1c
+                            ((v = attrsMap.get(CKA_VALUE).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO,
461e1c
+                            ECUtil.getECParameterSpec(sunECProvider,
461e1c
+                                    attrsMap.get(CKA_EC_PARAMS).getByteArray()))
461e1c
+                            .getEncoded();
461e1c
+                    if (token.config.getNssNetscapeDbWorkaround() &&
461e1c
+                            attrsMap.get(CKA_NETSCAPE_DB) == null) {
461e1c
+                        attrsMap.put(CKA_NETSCAPE_DB,
461e1c
+                                new CK_ATTRIBUTE(CKA_NETSCAPE_DB, BigInteger.ZERO));
461e1c
+                    }
461e1c
+                } else if (keyType == CKK_DH) {
461e1c
+                    if (debug != null) {
461e1c
+                        debug.println("Importing a Diffie-Hellman private key...");
461e1c
+                    }
461e1c
+                    if (DHKF == null) {
461e1c
+                        DHKFLock.lock();
461e1c
+                        try {
461e1c
+                            if (DHKF == null) {
461e1c
+                                DHKF = KeyFactory.getInstance(
461e1c
+                                        "DH", P11Util.getSunJceProvider());
461e1c
+                            }
461e1c
+                        } finally {
461e1c
+                            DHKFLock.unlock();
461e1c
+                        }
461e1c
+                    }
461e1c
+                    DHPrivateKeySpec spec = new DHPrivateKeySpec
461e1c
+                            (((v = attrsMap.get(CKA_VALUE).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO,
461e1c
+                            ((v = attrsMap.get(CKA_PRIME).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO,
461e1c
+                            ((v = attrsMap.get(CKA_BASE).getBigInteger()) != null)
461e1c
+                                    ? v : BigInteger.ZERO);
461e1c
+                    keyBytes = DHKF.generatePrivate(spec).getEncoded();
461e1c
+                    if (token.config.getNssNetscapeDbWorkaround() &&
461e1c
+                            attrsMap.get(CKA_NETSCAPE_DB) == null) {
461e1c
+                        attrsMap.put(CKA_NETSCAPE_DB,
461e1c
+                                new CK_ATTRIBUTE(CKA_NETSCAPE_DB, BigInteger.ZERO));
461e1c
+                    }
461e1c
+                } else {
461e1c
+                    if (debug != null) {
461e1c
+                        debug.println("Unrecognized private key type.");
461e1c
+                    }
461e1c
+                    throw new PKCS11Exception(CKR_GENERAL_ERROR,
461e1c
+                            " fips key importer");
461e1c
+                }
461e1c
+            } else if (keyClass == CKO_SECRET_KEY) {
461e1c
+                if (debug != null) {
461e1c
+                    debug.println("Importing a secret key...");
461e1c
+                }
461e1c
+                keyBytes = attrsMap.get(CKA_VALUE).getByteArray();
461e1c
+            }
461e1c
+            if (keyBytes == null || keyBytes.length == 0) {
461e1c
+                if (debug != null) {
461e1c
+                    debug.println("Private or secret key plain bytes could" +
461e1c
+                            " not be obtained. Import failed.");
461e1c
+                }
461e1c
+                throw new PKCS11Exception(CKR_GENERAL_ERROR,
461e1c
+                        " fips key importer");
461e1c
+            }
461e1c
+            attributes = new CK_ATTRIBUTE[attrsMap.size()];
461e1c
+            attrsMap.values().toArray(attributes);
461e1c
+            importerKeyLock.lock();
461e1c
+            try {
461e1c
+                // No need to reset the cipher object because no multi-part
461e1c
+                // operations are performed.
461e1c
+                encKeyBytes = importerCipher.doFinal(keyBytes);
461e1c
+            } finally {
461e1c
+                importerKeyLock.unlock();
461e1c
+            }
461e1c
+            attributes = token.getAttributes(TemplateManager.O_IMPORT,
461e1c
+                    keyClass, keyType, attributes);
461e1c
+            keyID = token.p11.C_UnwrapKey(hSession,
461e1c
+                    importerKeyMechanism, importerKeyID, encKeyBytes, attributes);
461e1c
+            if (debug != null) {
461e1c
+                debug.println("Imported key ID: " + keyID);
461e1c
+            }
461e1c
+        } catch (Throwable t) {
461e1c
+            if (t instanceof PKCS11Exception) {
461e1c
+                throw (PKCS11Exception)t;
461e1c
+            }
461e1c
+            throw new PKCS11Exception(CKR_GENERAL_ERROR,
461e1c
+                    t.getMessage());
461e1c
+        } finally {
461e1c
+            importerKey.releaseKeyID();
461e1c
+        }
461e1c
+        return Long.valueOf(keyID);
461e1c
+    }
461e1c
+
461e1c
+    static void exportKey(SunPKCS11 sunPKCS11, long hSession, long hObject,
461e1c
+            long keyClass, long keyType, Map<Long, CK_ATTRIBUTE> sensitiveAttrs)
461e1c
+            throws PKCS11Exception {
461e1c
+        Token token = sunPKCS11.getToken();
461e1c
+        if (debug != null) {
461e1c
+            debug.println("Private or Secret key will be exported in" +
461e1c
+                    " system FIPS mode.");
461e1c
+        }
461e1c
+        if (exporterKeyP11 == null) {
461e1c
+            try {
461e1c
+                exporterKeyLock.lock();
461e1c
+                if (exporterKeyP11 == null) {
461e1c
+                    if (exporterKeyMechanism == null) {
461e1c
+                        // Exporter Key creation has not been tried yet. Try it.
461e1c
+                        createExporterKey(token);
461e1c
+                    }
461e1c
+                    if (exporterKeyP11 == null || exporterCipher == null) {
461e1c
+                        if (debug != null) {
461e1c
+                            debug.println("Exporter Key could not be" +
461e1c
+                                    " generated.");
461e1c
+                        }
461e1c
+                        throw new PKCS11Exception(CKR_GENERAL_ERROR,
461e1c
+                                " fips key exporter");
461e1c
+                    }
461e1c
+                    if (debug != null) {
461e1c
+                        debug.println("Exporter Key successfully" +
461e1c
+                                " generated.");
461e1c
+                    }
461e1c
+                }
461e1c
+            } finally {
461e1c
+                exporterKeyLock.unlock();
461e1c
+            }
461e1c
+        }
461e1c
+        long exporterKeyID = exporterKeyP11.getKeyID();
461e1c
+        try {
461e1c
+            byte[] wrappedKeyBytes = token.p11.C_WrapKey(hSession,
461e1c
+                    exporterKeyMechanism, exporterKeyID, hObject);
461e1c
+            byte[] plainExportedKey = null;
461e1c
+            exporterKeyLock.lock();
461e1c
+            try {
461e1c
+                // No need to reset the cipher object because no multi-part
461e1c
+                // operations are performed.
461e1c
+                plainExportedKey = exporterCipher.doFinal(wrappedKeyBytes);
461e1c
+            } finally {
461e1c
+                exporterKeyLock.unlock();
461e1c
+            }
461e1c
+            if (keyClass == CKO_PRIVATE_KEY) {
461e1c
+                exportPrivateKey(sensitiveAttrs, keyType, plainExportedKey);
461e1c
+            } else if (keyClass == CKO_SECRET_KEY) {
461e1c
+                checkAttrs(sensitiveAttrs, "CKO_SECRET_KEY", CKA_VALUE);
461e1c
+                // CKA_VALUE is guaranteed to be present, since sensitiveAttrs'
461e1c
+                // size is greater than 0 and no invalid attributes exist
461e1c
+                sensitiveAttrs.get(CKA_VALUE).pValue = plainExportedKey;
461e1c
+            } else {
461e1c
+                throw new PKCS11Exception(CKR_GENERAL_ERROR,
461e1c
+                        " fips key exporter");
461e1c
+            }
461e1c
+        } catch (Throwable t) {
461e1c
+            if (t instanceof PKCS11Exception) {
461e1c
+                throw (PKCS11Exception)t;
461e1c
+            }
461e1c
+            throw new PKCS11Exception(CKR_GENERAL_ERROR,
461e1c
+                    t.getMessage());
461e1c
+        } finally {
461e1c
+            exporterKeyP11.releaseKeyID();
461e1c
+        }
461e1c
+    }
461e1c
+
461e1c
+    private static void exportPrivateKey(
461e1c
+            Map<Long, CK_ATTRIBUTE> sensitiveAttrs, long keyType,
461e1c
+            byte[] plainExportedKey) throws Throwable {
461e1c
+        if (keyType == CKK_RSA) {
461e1c
+            checkAttrs(sensitiveAttrs, "CKO_PRIVATE_KEY CKK_RSA",
461e1c
+                    CKA_PRIVATE_EXPONENT, CKA_PRIME_1, CKA_PRIME_2,
461e1c
+                    CKA_EXPONENT_1, CKA_EXPONENT_2, CKA_COEFFICIENT);
461e1c
+            RSAPrivateKey rsaPKey = RSAPrivateCrtKeyImpl.newKey(
461e1c
+                    RSAUtil.KeyType.RSA, "PKCS#8", plainExportedKey
461e1c
+            );
461e1c
+            CK_ATTRIBUTE attr;
461e1c
+            if ((attr = sensitiveAttrs.get(CKA_PRIVATE_EXPONENT)) != null) {
461e1c
+                attr.pValue = rsaPKey.getPrivateExponent().toByteArray();
461e1c
+            }
461e1c
+            if (rsaPKey instanceof RSAPrivateCrtKey) {
461e1c
+                RSAPrivateCrtKey rsaPCrtKey = (RSAPrivateCrtKey) rsaPKey;
461e1c
+                if ((attr = sensitiveAttrs.get(CKA_PRIME_1)) != null) {
461e1c
+                    attr.pValue = rsaPCrtKey.getPrimeP().toByteArray();
461e1c
+                }
461e1c
+                if ((attr = sensitiveAttrs.get(CKA_PRIME_2)) != null) {
461e1c
+                    attr.pValue = rsaPCrtKey.getPrimeQ().toByteArray();
461e1c
+                }
461e1c
+                if ((attr = sensitiveAttrs.get(CKA_EXPONENT_1)) != null) {
461e1c
+                    attr.pValue = rsaPCrtKey.getPrimeExponentP().toByteArray();
461e1c
+                }
461e1c
+                if ((attr = sensitiveAttrs.get(CKA_EXPONENT_2)) != null) {
461e1c
+                    attr.pValue = rsaPCrtKey.getPrimeExponentQ().toByteArray();
461e1c
+                }
461e1c
+                if ((attr = sensitiveAttrs.get(CKA_COEFFICIENT)) != null) {
461e1c
+                    attr.pValue = rsaPCrtKey.getCrtCoefficient().toByteArray();
461e1c
+                }
461e1c
+            } else {
461e1c
+                checkAttrs(sensitiveAttrs, "CKO_PRIVATE_KEY CKK_RSA",
461e1c
+                        CKA_PRIVATE_EXPONENT);
461e1c
+            }
461e1c
+        } else if (keyType == CKK_DSA) {
461e1c
+            checkAttrs(sensitiveAttrs, "CKO_PRIVATE_KEY CKK_DSA", CKA_VALUE);
461e1c
+            // CKA_VALUE is guaranteed to be present, since sensitiveAttrs'
461e1c
+            // size is greater than 0 and no invalid attributes exist
461e1c
+            sensitiveAttrs.get(CKA_VALUE).pValue =
461e1c
+                    new sun.security.provider.DSAPrivateKey(plainExportedKey)
461e1c
+                            .getX().toByteArray();
461e1c
+        } else if (keyType == CKK_EC) {
461e1c
+            checkAttrs(sensitiveAttrs, "CKO_PRIVATE_KEY CKK_EC", CKA_VALUE);
461e1c
+            // CKA_VALUE is guaranteed to be present, since sensitiveAttrs'
461e1c
+            // size is greater than 0 and no invalid attributes exist
461e1c
+            sensitiveAttrs.get(CKA_VALUE).pValue =
461e1c
+                    ECUtil.decodePKCS8ECPrivateKey(plainExportedKey)
461e1c
+                            .getS().toByteArray();
461e1c
+        } else {
461e1c
+            throw new PKCS11Exception(CKR_GENERAL_ERROR,
461e1c
+                    " unsupported CKO_PRIVATE_KEY key type: " + keyType);
461e1c
+        }
461e1c
+    }
461e1c
+
461e1c
+    private static void checkAttrs(Map<Long, CK_ATTRIBUTE> sensitiveAttrs,
461e1c
+                                     String keyName, long... validAttrs)
461e1c
+            throws PKCS11Exception {
461e1c
+        int sensitiveAttrsCount = sensitiveAttrs.size();
461e1c
+        if (sensitiveAttrsCount <= validAttrs.length) {
461e1c
+            int validAttrsCount = 0;
461e1c
+            for (long validAttr : validAttrs) {
461e1c
+                if (sensitiveAttrs.containsKey(validAttr)) validAttrsCount++;
461e1c
+            }
461e1c
+            if (validAttrsCount == sensitiveAttrsCount) return;
461e1c
+        }
461e1c
+        throw new PKCS11Exception(CKR_GENERAL_ERROR,
461e1c
+                " invalid attribute types for a " + keyName + " key object");
461e1c
+    }
461e1c
+
461e1c
+    private static void createImporterKey(Token token) {
461e1c
+        if (debug != null) {
461e1c
+            debug.println("Generating Importer Key...");
461e1c
+        }
461e1c
+        byte[] iv = new byte[16];
461e1c
+        JCAUtil.getSecureRandom().nextBytes(iv);
461e1c
+        importerKeyMechanism = new CK_MECHANISM(CKM_AES_CBC_PAD, iv);
461e1c
+        try {
461e1c
+            CK_ATTRIBUTE[] attributes = token.getAttributes(TemplateManager.O_GENERATE,
461e1c
+                            CKO_SECRET_KEY, CKK_AES, new CK_ATTRIBUTE[] {
461e1c
+                                    new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
461e1c
+                                    new CK_ATTRIBUTE(CKA_VALUE_LEN, 256 >> 3)});
461e1c
+            Session s = null;
461e1c
+            try {
461e1c
+                s = token.getObjSession();
461e1c
+                long keyID = token.p11.C_GenerateKey(
461e1c
+                        s.id(), new CK_MECHANISM(CKM_AES_KEY_GEN),
461e1c
+                        attributes);
461e1c
+                if (debug != null) {
461e1c
+                    debug.println("Importer Key ID: " + keyID);
461e1c
+                }
461e1c
+                importerKey = (P11Key)P11Key.secretKey(s, keyID, "AES",
461e1c
+                        256 >> 3, null);
461e1c
+            } catch (PKCS11Exception e) {
461e1c
+                // best effort
461e1c
+            } finally {
461e1c
+                token.releaseSession(s);
461e1c
+            }
461e1c
+            if (importerKey != null) {
461e1c
+                importerCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
461e1c
+                importerCipher.init(Cipher.ENCRYPT_MODE, importerKey,
461e1c
+                        new IvParameterSpec(
461e1c
+                                (byte[])importerKeyMechanism.pParameter), null);
461e1c
+            }
461e1c
+        } catch (Throwable t) {
461e1c
+            // best effort
461e1c
+            importerKey = null;
461e1c
+            importerCipher = null;
461e1c
+            // importerKeyMechanism value is kept initialized to indicate that
461e1c
+            // Importer Key creation has been tried and failed.
461e1c
+            if (debug != null) {
461e1c
+                debug.println("Error generating the Importer Key");
461e1c
+            }
461e1c
+        }
461e1c
+    }
461e1c
+
461e1c
+    private static void createExporterKey(Token token) {
461e1c
+        if (debug != null) {
461e1c
+            debug.println("Generating Exporter Key...");
461e1c
+        }
461e1c
+        byte[] iv = new byte[16];
461e1c
+        JCAUtil.getSecureRandom().nextBytes(iv);
461e1c
+        exporterKeyMechanism = new CK_MECHANISM(CKM_AES_CBC_PAD, iv);
461e1c
+        byte[] exporterKeyRaw = new byte[32];
461e1c
+        JCAUtil.getSecureRandom().nextBytes(exporterKeyRaw);
461e1c
+        exporterKey = new SecretKeySpec(exporterKeyRaw, "AES");
461e1c
+        try {
461e1c
+            SecretKeyFactory skf = SecretKeyFactory.getInstance("AES");
461e1c
+            exporterKeyP11 = (P11Key)(skf.translateKey(exporterKey));
461e1c
+            if (exporterKeyP11 != null) {
461e1c
+                exporterCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
461e1c
+                exporterCipher.init(Cipher.DECRYPT_MODE, exporterKey,
461e1c
+                        new IvParameterSpec(
461e1c
+                                (byte[])exporterKeyMechanism.pParameter), null);
461e1c
+            }
461e1c
+        } catch (Throwable t) {
461e1c
+            // best effort
461e1c
+            exporterKey = null;
461e1c
+            exporterKeyP11 = null;
461e1c
+            exporterCipher = null;
461e1c
+            // exporterKeyMechanism value is kept initialized to indicate that
461e1c
+            // Exporter Key creation has been tried and failed.
461e1c
+            if (debug != null) {
461e1c
+                debug.println("Error generating the Exporter Key");
461e1c
+            }
461e1c
+        }
461e1c
+    }
461e1c
+}
461e1c
diff --git a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Key.java b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Key.java
461e1c
index 9b69072280e..b403e6d3c6d 100644
461e1c
--- a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Key.java
461e1c
+++ b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Key.java
461e1c
@@ -37,6 +37,8 @@ import javax.crypto.*;
461e1c
 import javax.crypto.interfaces.*;
461e1c
 import javax.crypto.spec.*;
461e1c
 
461e1c
+import jdk.internal.access.SharedSecrets;
461e1c
+
461e1c
 import sun.security.rsa.RSAUtil.KeyType;
461e1c
 import sun.security.rsa.RSAPublicKeyImpl;
461e1c
 import sun.security.rsa.RSAPrivateCrtKeyImpl;
461e1c
@@ -69,6 +71,9 @@ import sun.security.jca.JCAUtil;
461e1c
  */
461e1c
 abstract class P11Key implements Key, Length {
461e1c
 
461e1c
+    private static final boolean plainKeySupportEnabled = SharedSecrets
461e1c
+            .getJavaSecuritySystemConfiguratorAccess().isPlainKeySupportEnabled();
461e1c
+
461e1c
     private static final long serialVersionUID = -2575874101938349339L;
461e1c
 
461e1c
     private static final String PUBLIC = "public";
461e1c
@@ -379,7 +384,8 @@ abstract class P11Key implements Key, Length {
461e1c
             new CK_ATTRIBUTE(CKA_SENSITIVE),
461e1c
             new CK_ATTRIBUTE(CKA_EXTRACTABLE),
461e1c
         });
461e1c
-        if (attributes[1].getBoolean() || (attributes[2].getBoolean() == false)) {
461e1c
+        if (!plainKeySupportEnabled && (attributes[1].getBoolean() ||
461e1c
+                (attributes[2].getBoolean() == false))) {
461e1c
             return new P11PrivateKey
461e1c
                 (session, keyID, algorithm, keyLength, attributes);
461e1c
         } else {
461e1c
@@ -461,7 +467,8 @@ abstract class P11Key implements Key, Length {
461e1c
         }
461e1c
         public String getFormat() {
461e1c
             token.ensureValid();
461e1c
-            if (sensitive || (extractable == false)) {
461e1c
+            if (!plainKeySupportEnabled &&
461e1c
+                    (sensitive || (extractable == false))) {
461e1c
                 return null;
461e1c
             } else {
461e1c
                 return "RAW";
461e1c
diff --git a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/SunPKCS11.java b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/SunPKCS11.java
461e1c
index 112b639aa96..5549cd9ed4e 100644
461e1c
--- a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/SunPKCS11.java
461e1c
+++ b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/SunPKCS11.java
461e1c
@@ -26,6 +26,9 @@
461e1c
 package sun.security.pkcs11;
461e1c
 
461e1c
 import java.io.*;
461e1c
+import java.lang.invoke.MethodHandle;
461e1c
+import java.lang.invoke.MethodHandles;
461e1c
+import java.lang.invoke.MethodType;
461e1c
 import java.util.*;
461e1c
 
461e1c
 import java.security.*;
461e1c
@@ -42,6 +45,7 @@ import javax.security.auth.callback.PasswordCallback;
461e1c
 
461e1c
 import com.sun.crypto.provider.ChaCha20Poly1305Parameters;
461e1c
 
461e1c
+import jdk.internal.access.SharedSecrets;
461e1c
 import jdk.internal.misc.InnocuousThread;
461e1c
 import sun.security.util.Debug;
461e1c
 import sun.security.util.ResourcesMgr;
461e1c
@@ -62,6 +66,37 @@ import static sun.security.pkcs11.wrapper.PKCS11Exception.*;
461e1c
  */
461e1c
 public final class SunPKCS11 extends AuthProvider {
461e1c
 
461e1c
+    private static final boolean systemFipsEnabled = SharedSecrets
461e1c
+            .getJavaSecuritySystemConfiguratorAccess().isSystemFipsEnabled();
461e1c
+
461e1c
+    private static final boolean plainKeySupportEnabled = SharedSecrets
461e1c
+            .getJavaSecuritySystemConfiguratorAccess().isPlainKeySupportEnabled();
461e1c
+
461e1c
+    private static final MethodHandle fipsImportKey;
461e1c
+    private static final MethodHandle fipsExportKey;
461e1c
+    static {
461e1c
+        MethodHandle fipsImportKeyTmp = null;
461e1c
+        MethodHandle fipsExportKeyTmp = null;
461e1c
+        if (plainKeySupportEnabled) {
461e1c
+            try {
461e1c
+                fipsImportKeyTmp = MethodHandles.lookup().findStatic(
461e1c
+                        FIPSKeyImporter.class, "importKey",
461e1c
+                        MethodType.methodType(Long.class, SunPKCS11.class,
461e1c
+                        long.class, CK_ATTRIBUTE[].class));
461e1c
+                fipsExportKeyTmp = MethodHandles.lookup().findStatic(
461e1c
+                        FIPSKeyImporter.class, "exportKey",
461e1c
+                        MethodType.methodType(void.class, SunPKCS11.class,
461e1c
+                        long.class, long.class,
461e1c
+                        long.class, long.class, Map.class));
461e1c
+            } catch (Throwable t) {
461e1c
+                throw new SecurityException("FIPS key importer-exporter" +
461e1c
+                        " initialization failed", t);
461e1c
+            }
461e1c
+        }
461e1c
+        fipsImportKey = fipsImportKeyTmp;
461e1c
+        fipsExportKey = fipsExportKeyTmp;
461e1c
+    }
461e1c
+
461e1c
     private static final long serialVersionUID = -1354835039035306505L;
461e1c
 
461e1c
     static final Debug debug = Debug.getInstance("sunpkcs11");
461e1c
@@ -320,10 +355,19 @@ public final class SunPKCS11 extends AuthProvider {
461e1c
             // request multithreaded access first
461e1c
             initArgs.flags = CKF_OS_LOCKING_OK;
461e1c
             PKCS11 tmpPKCS11;
461e1c
+            MethodHandle fipsKeyImporter = null;
461e1c
+            MethodHandle fipsKeyExporter = null;
461e1c
+            if (plainKeySupportEnabled) {
461e1c
+                fipsKeyImporter = MethodHandles.insertArguments(
461e1c
+                        fipsImportKey, 0, this);
461e1c
+                fipsKeyExporter = MethodHandles.insertArguments(
461e1c
+                        fipsExportKey, 0, this);
461e1c
+            }
461e1c
             try {
461e1c
                 tmpPKCS11 = PKCS11.getInstance(
461e1c
                     library, functionList, initArgs,
461e1c
-                    config.getOmitInitialize());
461e1c
+                    config.getOmitInitialize(), fipsKeyImporter,
461e1c
+                    fipsKeyExporter);
461e1c
             } catch (PKCS11Exception e) {
461e1c
                 if (debug != null) {
461e1c
                     debug.println("Multi-threaded initialization failed: " + e);
461e1c
@@ -339,7 +383,8 @@ public final class SunPKCS11 extends AuthProvider {
461e1c
                     initArgs.flags = 0;
461e1c
                 }
461e1c
                 tmpPKCS11 = PKCS11.getInstance(library,
461e1c
-                    functionList, initArgs, config.getOmitInitialize());
461e1c
+                    functionList, initArgs, config.getOmitInitialize(), fipsKeyImporter,
461e1c
+                    fipsKeyExporter);
461e1c
             }
461e1c
             p11 = tmpPKCS11;
461e1c
 
461e1c
@@ -379,6 +424,24 @@ public final class SunPKCS11 extends AuthProvider {
461e1c
             if (nssModule != null) {
461e1c
                 nssModule.setProvider(this);
461e1c
             }
461e1c
+            if (systemFipsEnabled) {
461e1c
+                // The NSS Software Token in FIPS 140-2 mode requires a user
461e1c
+                // login for most operations. See sftk_fipsCheck. The NSS DB
461e1c
+                // (/etc/pki/nssdb) PIN is empty.
461e1c
+                Session session = null;
461e1c
+                try {
461e1c
+                    session = token.getOpSession();
461e1c
+                    p11.C_Login(session.id(), CKU_USER, new char[] {});
461e1c
+                } catch (PKCS11Exception p11e) {
461e1c
+                    if (debug != null) {
461e1c
+                        debug.println("Error during token login: " +
461e1c
+                                p11e.getMessage());
461e1c
+                    }
461e1c
+                    throw p11e;
461e1c
+                } finally {
461e1c
+                    token.releaseSession(session);
461e1c
+                }
461e1c
+            }
461e1c
         } catch (Exception e) {
461e1c
             if (config.getHandleStartupErrors() == Config.ERR_IGNORE_ALL) {
461e1c
                 throw new UnsupportedOperationException
461e1c
diff --git a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/wrapper/PKCS11.java b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/wrapper/PKCS11.java
461e1c
index 5c0aacd1a67..1e98ce2e280 100644
461e1c
--- a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/wrapper/PKCS11.java
461e1c
+++ b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/wrapper/PKCS11.java
461e1c
@@ -49,6 +49,9 @@ package sun.security.pkcs11.wrapper;
461e1c
 
461e1c
 import java.io.File;
461e1c
 import java.io.IOException;
461e1c
+import java.lang.invoke.MethodHandle;
461e1c
+import java.lang.invoke.MethodHandles;
461e1c
+import java.lang.invoke.MethodType;
461e1c
 import java.util.*;
461e1c
 
461e1c
 import java.security.AccessController;
461e1c
@@ -150,18 +153,43 @@ public class PKCS11 {
461e1c
         this.pkcs11ModulePath = pkcs11ModulePath;
461e1c
     }
461e1c
 
461e1c
+    /*
461e1c
+     * Compatibility wrapper to allow this method to work as before
461e1c
+     * when FIPS mode support is not active.
461e1c
+     */
461e1c
+    public static synchronized PKCS11 getInstance(String pkcs11ModulePath,
461e1c
+           String functionList, CK_C_INITIALIZE_ARGS pInitArgs,
461e1c
+           boolean omitInitialize) throws IOException, PKCS11Exception {
461e1c
+        return getInstance(pkcs11ModulePath, functionList,
461e1c
+                           pInitArgs, omitInitialize, null, null);
461e1c
+    }
461e1c
+
461e1c
     public static synchronized PKCS11 getInstance(String pkcs11ModulePath,
461e1c
             String functionList, CK_C_INITIALIZE_ARGS pInitArgs,
461e1c
-            boolean omitInitialize) throws IOException, PKCS11Exception {
461e1c
+            boolean omitInitialize, MethodHandle fipsKeyImporter,
461e1c
+            MethodHandle fipsKeyExporter)
461e1c
+                    throws IOException, PKCS11Exception {
461e1c
         // we may only call C_Initialize once per native .so/.dll
461e1c
         // so keep a cache using the (non-canonicalized!) path
461e1c
         PKCS11 pkcs11 = moduleMap.get(pkcs11ModulePath);
461e1c
         if (pkcs11 == null) {
461e1c
+            boolean nssFipsMode = fipsKeyImporter != null &&
461e1c
+                    fipsKeyExporter != null;
461e1c
             if ((pInitArgs != null)
461e1c
                     && ((pInitArgs.flags & CKF_OS_LOCKING_OK) != 0)) {
461e1c
-                pkcs11 = new PKCS11(pkcs11ModulePath, functionList);
461e1c
+                if (nssFipsMode) {
461e1c
+                    pkcs11 = new FIPSPKCS11(pkcs11ModulePath, functionList,
461e1c
+                            fipsKeyImporter, fipsKeyExporter);
461e1c
+                } else {
461e1c
+                    pkcs11 = new PKCS11(pkcs11ModulePath, functionList);
461e1c
+                }
461e1c
             } else {
461e1c
-                pkcs11 = new SynchronizedPKCS11(pkcs11ModulePath, functionList);
461e1c
+                if (nssFipsMode) {
461e1c
+                    pkcs11 = new SynchronizedFIPSPKCS11(pkcs11ModulePath,
461e1c
+                            functionList, fipsKeyImporter, fipsKeyExporter);
461e1c
+                } else {
461e1c
+                    pkcs11 = new SynchronizedPKCS11(pkcs11ModulePath, functionList);
461e1c
+                }
461e1c
             }
461e1c
             if (omitInitialize == false) {
461e1c
                 try {
461e1c
@@ -1911,4 +1939,194 @@ static class SynchronizedPKCS11 extends PKCS11 {
461e1c
         super.C_GenerateRandom(hSession, randomData);
461e1c
     }
461e1c
 }
461e1c
+
461e1c
+// PKCS11 subclass that allows using plain private or secret keys in
461e1c
+// FIPS-configured NSS Software Tokens. Only used when System FIPS
461e1c
+// is enabled.
461e1c
+static class FIPSPKCS11 extends PKCS11 {
461e1c
+    private MethodHandle fipsKeyImporter;
461e1c
+    private MethodHandle fipsKeyExporter;
461e1c
+    private MethodHandle hC_GetAttributeValue;
461e1c
+    FIPSPKCS11(String pkcs11ModulePath, String functionListName,
461e1c
+            MethodHandle fipsKeyImporter, MethodHandle fipsKeyExporter)
461e1c
+                    throws IOException {
461e1c
+        super(pkcs11ModulePath, functionListName);
461e1c
+        this.fipsKeyImporter = fipsKeyImporter;
461e1c
+        this.fipsKeyExporter = fipsKeyExporter;
461e1c
+        try {
461e1c
+            hC_GetAttributeValue = MethodHandles.insertArguments(
461e1c
+                    MethodHandles.lookup().findSpecial(PKCS11.class,
461e1c
+                            "C_GetAttributeValue", MethodType.methodType(
461e1c
+                                    void.class, long.class, long.class,
461e1c
+                                    CK_ATTRIBUTE[].class),
461e1c
+                            FIPSPKCS11.class), 0, this);
461e1c
+        } catch (Throwable t) {
461e1c
+            throw new RuntimeException(
461e1c
+                    "sun.security.pkcs11.wrapper.PKCS11" +
461e1c
+                    "::C_GetAttributeValue method not found.", t);
461e1c
+        }
461e1c
+    }
461e1c
+
461e1c
+    public long C_CreateObject(long hSession,
461e1c
+            CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {
461e1c
+        // Creating sensitive key objects from plain key material in a
461e1c
+        // FIPS-configured NSS Software Token is not allowed. We apply
461e1c
+        // a key-unwrapping scheme to achieve so.
461e1c
+        if (FIPSPKCS11Helper.isSensitiveObject(pTemplate)) {
461e1c
+            try {
461e1c
+                return ((Long)fipsKeyImporter.invoke(hSession, pTemplate))
461e1c
+                        .longValue();
461e1c
+            } catch (Throwable t) {
461e1c
+                if (t instanceof PKCS11Exception) {
461e1c
+                    throw (PKCS11Exception)t;
461e1c
+                }
461e1c
+                throw new PKCS11Exception(CKR_GENERAL_ERROR,
461e1c
+                        t.getMessage());
461e1c
+            }
461e1c
+        }
461e1c
+        return super.C_CreateObject(hSession, pTemplate);
461e1c
+    }
461e1c
+
461e1c
+    public void C_GetAttributeValue(long hSession, long hObject,
461e1c
+            CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {
461e1c
+        FIPSPKCS11Helper.C_GetAttributeValue(hC_GetAttributeValue,
461e1c
+                fipsKeyExporter, hSession, hObject, pTemplate);
461e1c
+    }
461e1c
+}
461e1c
+
461e1c
+// FIPSPKCS11 synchronized counterpart.
461e1c
+static class SynchronizedFIPSPKCS11 extends SynchronizedPKCS11 {
461e1c
+    private MethodHandle fipsKeyImporter;
461e1c
+    private MethodHandle fipsKeyExporter;
461e1c
+    private MethodHandle hC_GetAttributeValue;
461e1c
+    SynchronizedFIPSPKCS11(String pkcs11ModulePath, String functionListName,
461e1c
+            MethodHandle fipsKeyImporter, MethodHandle fipsKeyExporter)
461e1c
+                    throws IOException {
461e1c
+        super(pkcs11ModulePath, functionListName);
461e1c
+        this.fipsKeyImporter = fipsKeyImporter;
461e1c
+        this.fipsKeyExporter = fipsKeyExporter;
461e1c
+        try {
461e1c
+            hC_GetAttributeValue = MethodHandles.insertArguments(
461e1c
+                    MethodHandles.lookup().findSpecial(SynchronizedPKCS11.class,
461e1c
+                            "C_GetAttributeValue", MethodType.methodType(
461e1c
+                                    void.class, long.class, long.class,
461e1c
+                                    CK_ATTRIBUTE[].class),
461e1c
+                            SynchronizedFIPSPKCS11.class), 0, this);
461e1c
+        } catch (Throwable t) {
461e1c
+            throw new RuntimeException(
461e1c
+                    "sun.security.pkcs11.wrapper.SynchronizedPKCS11" +
461e1c
+                    "::C_GetAttributeValue method not found.", t);
461e1c
+        }
461e1c
+    }
461e1c
+
461e1c
+    public synchronized long C_CreateObject(long hSession,
461e1c
+            CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {
461e1c
+        // See FIPSPKCS11::C_CreateObject.
461e1c
+        if (FIPSPKCS11Helper.isSensitiveObject(pTemplate)) {
461e1c
+            try {
461e1c
+                return ((Long)fipsKeyImporter.invoke(hSession, pTemplate))
461e1c
+                        .longValue();
461e1c
+            } catch (Throwable t) {
461e1c
+                if (t instanceof PKCS11Exception) {
461e1c
+                    throw (PKCS11Exception)t;
461e1c
+                }
461e1c
+                throw new PKCS11Exception(CKR_GENERAL_ERROR,
461e1c
+                        t.getMessage());
461e1c
+            }
461e1c
+        }
461e1c
+        return super.C_CreateObject(hSession, pTemplate);
461e1c
+    }
461e1c
+
461e1c
+    public synchronized void C_GetAttributeValue(long hSession, long hObject,
461e1c
+            CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {
461e1c
+        FIPSPKCS11Helper.C_GetAttributeValue(hC_GetAttributeValue,
461e1c
+                fipsKeyExporter, hSession, hObject, pTemplate);
461e1c
+    }
461e1c
+}
461e1c
+
461e1c
+private static class FIPSPKCS11Helper {
461e1c
+    static boolean isSensitiveObject(CK_ATTRIBUTE[] pTemplate) {
461e1c
+        for (CK_ATTRIBUTE attr : pTemplate) {
461e1c
+            if (attr.type == CKA_CLASS &&
461e1c
+                    (attr.getLong() == CKO_PRIVATE_KEY ||
461e1c
+                    attr.getLong() == CKO_SECRET_KEY)) {
461e1c
+                return true;
461e1c
+            }
461e1c
+        }
461e1c
+        return false;
461e1c
+    }
461e1c
+    static void C_GetAttributeValue(MethodHandle hC_GetAttributeValue,
461e1c
+            MethodHandle fipsKeyExporter, long hSession, long hObject,
461e1c
+            CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {
461e1c
+        Map<Long, CK_ATTRIBUTE> sensitiveAttrs = new HashMap<>();
461e1c
+        List<CK_ATTRIBUTE> nonSensitiveAttrs = new LinkedList<>();
461e1c
+        FIPSPKCS11Helper.getAttributesBySensitivity(pTemplate,
461e1c
+                sensitiveAttrs, nonSensitiveAttrs);
461e1c
+        try {
461e1c
+            if (sensitiveAttrs.size() > 0) {
461e1c
+                long keyClass = -1L;
461e1c
+                long keyType = -1L;
461e1c
+                try {
461e1c
+                    // Secret and private keys have both class and type
461e1c
+                    // attributes, so we can query them at once.
461e1c
+                    CK_ATTRIBUTE[] queryAttrs = new CK_ATTRIBUTE[]{
461e1c
+                            new CK_ATTRIBUTE(CKA_CLASS),
461e1c
+                            new CK_ATTRIBUTE(CKA_KEY_TYPE),
461e1c
+                    };
461e1c
+                    hC_GetAttributeValue.invoke(hSession, hObject, queryAttrs);
461e1c
+                    keyClass = queryAttrs[0].getLong();
461e1c
+                    keyType = queryAttrs[1].getLong();
461e1c
+                } catch (PKCS11Exception e) {
461e1c
+                    // If the query fails, the object is neither a secret nor a
461e1c
+                    // private key. As this case won't be handled with the FIPS
461e1c
+                    // Key Exporter, we keep keyClass initialized to -1L.
461e1c
+                }
461e1c
+                if (keyClass == CKO_SECRET_KEY || keyClass == CKO_PRIVATE_KEY) {
461e1c
+                    fipsKeyExporter.invoke(hSession, hObject, keyClass, keyType,
461e1c
+                            sensitiveAttrs);
461e1c
+                    if (nonSensitiveAttrs.size() > 0) {
461e1c
+                        CK_ATTRIBUTE[] pNonSensitiveAttrs =
461e1c
+                                new CK_ATTRIBUTE[nonSensitiveAttrs.size()];
461e1c
+                        int i = 0;
461e1c
+                        for (CK_ATTRIBUTE nonSensAttr : nonSensitiveAttrs) {
461e1c
+                            pNonSensitiveAttrs[i++] = nonSensAttr;
461e1c
+                        }
461e1c
+                        hC_GetAttributeValue.invoke(hSession, hObject,
461e1c
+                                pNonSensitiveAttrs);
461e1c
+                        // libj2pkcs11 allocates new CK_ATTRIBUTE objects, so we
461e1c
+                        // update the reference on the previous CK_ATTRIBUTEs
461e1c
+                        i = 0;
461e1c
+                        for (CK_ATTRIBUTE nonSensAttr : nonSensitiveAttrs) {
461e1c
+                            nonSensAttr.pValue = pNonSensitiveAttrs[i++].pValue;
461e1c
+                        }
461e1c
+                    }
461e1c
+                    return;
461e1c
+                }
461e1c
+            }
461e1c
+            hC_GetAttributeValue.invoke(hSession, hObject, pTemplate);
461e1c
+        } catch (Throwable t) {
461e1c
+            if (t instanceof PKCS11Exception) {
461e1c
+                throw (PKCS11Exception)t;
461e1c
+            }
461e1c
+            throw new PKCS11Exception(CKR_GENERAL_ERROR,
461e1c
+                    t.getMessage());
461e1c
+        }
461e1c
+    }
461e1c
+    private static void getAttributesBySensitivity(CK_ATTRIBUTE[] pTemplate,
461e1c
+            Map<Long, CK_ATTRIBUTE> sensitiveAttrs,
461e1c
+            List<CK_ATTRIBUTE> nonSensitiveAttrs) {
461e1c
+        for (CK_ATTRIBUTE attr : pTemplate) {
461e1c
+            long type = attr.type;
461e1c
+            // Aligned with NSS' sftk_isSensitive in lib/softoken/pkcs11u.c
461e1c
+            if (type == CKA_VALUE || type == CKA_PRIVATE_EXPONENT ||
461e1c
+                    type == CKA_PRIME_1 || type == CKA_PRIME_2 ||
461e1c
+                    type == CKA_EXPONENT_1 || type == CKA_EXPONENT_2 ||
461e1c
+                    type == CKA_COEFFICIENT) {
461e1c
+                sensitiveAttrs.put(type, attr);
461e1c
+            } else {
461e1c
+                nonSensitiveAttrs.add(attr);
461e1c
+            }
461e1c
+        }
461e1c
+    }
461e1c
+}
461e1c
 }
461e1c
diff --git a/src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java b/src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java
461e1c
index 8c9e4f9dbe6..883dc04758e 100644
461e1c
--- a/src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java
461e1c
+++ b/src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java
461e1c
@@ -38,6 +38,7 @@ import java.util.HashMap;
461e1c
 import java.util.Iterator;
461e1c
 import java.util.List;
461e1c
 
461e1c
+import jdk.internal.access.SharedSecrets;
461e1c
 import sun.security.ec.ed.EdDSAAlgorithmParameters;
461e1c
 import sun.security.ec.ed.EdDSAKeyFactory;
461e1c
 import sun.security.ec.ed.EdDSAKeyPairGenerator;
461e1c
@@ -56,6 +57,10 @@ public final class SunEC extends Provider {
461e1c
 
461e1c
     private static final long serialVersionUID = -2279741672933606418L;
461e1c
 
461e1c
+    private static final boolean systemFipsEnabled =
461e1c
+            SharedSecrets.getJavaSecuritySystemConfiguratorAccess()
461e1c
+            .isSystemFipsEnabled();
461e1c
+
461e1c
     private static class ProviderServiceA extends ProviderService {
461e1c
         ProviderServiceA(Provider p, String type, String algo, String cn,
461e1c
             HashMap<String, String> attrs) {
461e1c
@@ -249,85 +254,86 @@ public final class SunEC extends Provider {
461e1c
 
461e1c
         putXDHEntries();
461e1c
         putEdDSAEntries();
461e1c
-
461e1c
-        /*
461e1c
-         * Signature engines
461e1c
-         */
461e1c
-        putService(new ProviderService(this, "Signature",
461e1c
-            "NONEwithECDSA", "sun.security.ec.ECDSASignature$Raw",
461e1c
-            null, ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "Signature",
461e1c
-            "SHA1withECDSA", "sun.security.ec.ECDSASignature$SHA1",
461e1c
-            ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "Signature",
461e1c
-            "SHA224withECDSA", "sun.security.ec.ECDSASignature$SHA224",
461e1c
-            ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "Signature",
461e1c
-            "SHA256withECDSA", "sun.security.ec.ECDSASignature$SHA256",
461e1c
-            ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "Signature",
461e1c
-            "SHA384withECDSA", "sun.security.ec.ECDSASignature$SHA384",
461e1c
-            ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "Signature",
461e1c
-            "SHA512withECDSA", "sun.security.ec.ECDSASignature$SHA512",
461e1c
-            ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "Signature",
461e1c
-            "SHA3-224withECDSA", "sun.security.ec.ECDSASignature$SHA3_224",
461e1c
-            ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "Signature",
461e1c
-            "SHA3-256withECDSA", "sun.security.ec.ECDSASignature$SHA3_256",
461e1c
-            ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "Signature",
461e1c
-            "SHA3-384withECDSA", "sun.security.ec.ECDSASignature$SHA3_384",
461e1c
-            ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "Signature",
461e1c
-            "SHA3-512withECDSA", "sun.security.ec.ECDSASignature$SHA3_512",
461e1c
-            ATTRS));
461e1c
-
461e1c
-        putService(new ProviderService(this, "Signature",
461e1c
-             "NONEwithECDSAinP1363Format",
461e1c
-             "sun.security.ec.ECDSASignature$RawinP1363Format"));
461e1c
-        putService(new ProviderService(this, "Signature",
461e1c
-             "SHA1withECDSAinP1363Format",
461e1c
-             "sun.security.ec.ECDSASignature$SHA1inP1363Format"));
461e1c
-        putService(new ProviderService(this, "Signature",
461e1c
-             "SHA224withECDSAinP1363Format",
461e1c
-             "sun.security.ec.ECDSASignature$SHA224inP1363Format"));
461e1c
-        putService(new ProviderService(this, "Signature",
461e1c
-             "SHA256withECDSAinP1363Format",
461e1c
-             "sun.security.ec.ECDSASignature$SHA256inP1363Format"));
461e1c
-        putService(new ProviderService(this, "Signature",
461e1c
-            "SHA384withECDSAinP1363Format",
461e1c
-            "sun.security.ec.ECDSASignature$SHA384inP1363Format"));
461e1c
-        putService(new ProviderService(this, "Signature",
461e1c
-            "SHA512withECDSAinP1363Format",
461e1c
-            "sun.security.ec.ECDSASignature$SHA512inP1363Format"));
461e1c
-
461e1c
-        putService(new ProviderService(this, "Signature",
461e1c
-             "SHA3-224withECDSAinP1363Format",
461e1c
-             "sun.security.ec.ECDSASignature$SHA3_224inP1363Format"));
461e1c
-        putService(new ProviderService(this, "Signature",
461e1c
-             "SHA3-256withECDSAinP1363Format",
461e1c
-             "sun.security.ec.ECDSASignature$SHA3_256inP1363Format"));
461e1c
-        putService(new ProviderService(this, "Signature",
461e1c
-            "SHA3-384withECDSAinP1363Format",
461e1c
-            "sun.security.ec.ECDSASignature$SHA3_384inP1363Format"));
461e1c
-        putService(new ProviderService(this, "Signature",
461e1c
-            "SHA3-512withECDSAinP1363Format",
461e1c
-            "sun.security.ec.ECDSASignature$SHA3_512inP1363Format"));
461e1c
-
461e1c
-        /*
461e1c
-         *  Key Pair Generator engine
461e1c
-         */
461e1c
-        putService(new ProviderService(this, "KeyPairGenerator",
461e1c
-            "EC", "sun.security.ec.ECKeyPairGenerator",
461e1c
-            List.of("EllipticCurve"), ATTRS));
461e1c
-
461e1c
-        /*
461e1c
-         * Key Agreement engine
461e1c
-         */
461e1c
-        putService(new ProviderService(this, "KeyAgreement",
461e1c
-            "ECDH", "sun.security.ec.ECDHKeyAgreement", null, ATTRS));
461e1c
+        if (!systemFipsEnabled) {
461e1c
+            /*
461e1c
+             * Signature engines
461e1c
+             */
461e1c
+            putService(new ProviderService(this, "Signature",
461e1c
+                "NONEwithECDSA", "sun.security.ec.ECDSASignature$Raw",
461e1c
+                null, ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "Signature",
461e1c
+                "SHA1withECDSA", "sun.security.ec.ECDSASignature$SHA1",
461e1c
+                ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "Signature",
461e1c
+                "SHA224withECDSA", "sun.security.ec.ECDSASignature$SHA224",
461e1c
+                ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "Signature",
461e1c
+                "SHA256withECDSA", "sun.security.ec.ECDSASignature$SHA256",
461e1c
+                ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "Signature",
461e1c
+                "SHA384withECDSA", "sun.security.ec.ECDSASignature$SHA384",
461e1c
+                ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "Signature",
461e1c
+                "SHA512withECDSA", "sun.security.ec.ECDSASignature$SHA512",
461e1c
+                ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "Signature",
461e1c
+                "SHA3-224withECDSA", "sun.security.ec.ECDSASignature$SHA3_224",
461e1c
+                ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "Signature",
461e1c
+                "SHA3-256withECDSA", "sun.security.ec.ECDSASignature$SHA3_256",
461e1c
+                ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "Signature",
461e1c
+                "SHA3-384withECDSA", "sun.security.ec.ECDSASignature$SHA3_384",
461e1c
+                ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "Signature",
461e1c
+                "SHA3-512withECDSA", "sun.security.ec.ECDSASignature$SHA3_512",
461e1c
+                ATTRS));
461e1c
+
461e1c
+            putService(new ProviderService(this, "Signature",
461e1c
+                 "NONEwithECDSAinP1363Format",
461e1c
+                 "sun.security.ec.ECDSASignature$RawinP1363Format"));
461e1c
+            putService(new ProviderService(this, "Signature",
461e1c
+                 "SHA1withECDSAinP1363Format",
461e1c
+                 "sun.security.ec.ECDSASignature$SHA1inP1363Format"));
461e1c
+            putService(new ProviderService(this, "Signature",
461e1c
+                 "SHA224withECDSAinP1363Format",
461e1c
+                 "sun.security.ec.ECDSASignature$SHA224inP1363Format"));
461e1c
+            putService(new ProviderService(this, "Signature",
461e1c
+                 "SHA256withECDSAinP1363Format",
461e1c
+                 "sun.security.ec.ECDSASignature$SHA256inP1363Format"));
461e1c
+            putService(new ProviderService(this, "Signature",
461e1c
+                "SHA384withECDSAinP1363Format",
461e1c
+                "sun.security.ec.ECDSASignature$SHA384inP1363Format"));
461e1c
+            putService(new ProviderService(this, "Signature",
461e1c
+                "SHA512withECDSAinP1363Format",
461e1c
+                "sun.security.ec.ECDSASignature$SHA512inP1363Format"));
461e1c
+
461e1c
+            putService(new ProviderService(this, "Signature",
461e1c
+                 "SHA3-224withECDSAinP1363Format",
461e1c
+                 "sun.security.ec.ECDSASignature$SHA3_224inP1363Format"));
461e1c
+            putService(new ProviderService(this, "Signature",
461e1c
+                 "SHA3-256withECDSAinP1363Format",
461e1c
+                 "sun.security.ec.ECDSASignature$SHA3_256inP1363Format"));
461e1c
+            putService(new ProviderService(this, "Signature",
461e1c
+                "SHA3-384withECDSAinP1363Format",
461e1c
+                "sun.security.ec.ECDSASignature$SHA3_384inP1363Format"));
461e1c
+            putService(new ProviderService(this, "Signature",
461e1c
+                "SHA3-512withECDSAinP1363Format",
461e1c
+                "sun.security.ec.ECDSASignature$SHA3_512inP1363Format"));
461e1c
+
461e1c
+            /*
461e1c
+             *  Key Pair Generator engine
461e1c
+             */
461e1c
+            putService(new ProviderService(this, "KeyPairGenerator",
461e1c
+                "EC", "sun.security.ec.ECKeyPairGenerator",
461e1c
+                List.of("EllipticCurve"), ATTRS));
461e1c
+
461e1c
+            /*
461e1c
+             * Key Agreement engine
461e1c
+             */
461e1c
+            putService(new ProviderService(this, "KeyAgreement",
461e1c
+                "ECDH", "sun.security.ec.ECDHKeyAgreement", null, ATTRS));
461e1c
+        }
461e1c
     }
461e1c
 
461e1c
     private void putXDHEntries() {
461e1c
@@ -344,23 +350,25 @@ public final class SunEC extends Provider {
461e1c
             "X448", "sun.security.ec.XDHKeyFactory.X448",
461e1c
             ATTRS));
461e1c
 
461e1c
-        putService(new ProviderService(this, "KeyPairGenerator",
461e1c
-            "XDH", "sun.security.ec.XDHKeyPairGenerator", null, ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "KeyPairGenerator",
461e1c
-            "X25519", "sun.security.ec.XDHKeyPairGenerator.X25519",
461e1c
-            ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "KeyPairGenerator",
461e1c
-            "X448", "sun.security.ec.XDHKeyPairGenerator.X448",
461e1c
-            ATTRS));
461e1c
-
461e1c
-        putService(new ProviderService(this, "KeyAgreement",
461e1c
-            "XDH", "sun.security.ec.XDHKeyAgreement", null, ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "KeyAgreement",
461e1c
-            "X25519", "sun.security.ec.XDHKeyAgreement.X25519",
461e1c
-            ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "KeyAgreement",
461e1c
-            "X448", "sun.security.ec.XDHKeyAgreement.X448",
461e1c
-            ATTRS));
461e1c
+        if (!systemFipsEnabled) {
461e1c
+            putService(new ProviderService(this, "KeyPairGenerator",
461e1c
+                "XDH", "sun.security.ec.XDHKeyPairGenerator", null, ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "KeyPairGenerator",
461e1c
+                "X25519", "sun.security.ec.XDHKeyPairGenerator.X25519",
461e1c
+                ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "KeyPairGenerator",
461e1c
+                "X448", "sun.security.ec.XDHKeyPairGenerator.X448",
461e1c
+                ATTRS));
461e1c
+
461e1c
+            putService(new ProviderService(this, "KeyAgreement",
461e1c
+                "XDH", "sun.security.ec.XDHKeyAgreement", null, ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "KeyAgreement",
461e1c
+                "X25519", "sun.security.ec.XDHKeyAgreement.X25519",
461e1c
+                ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "KeyAgreement",
461e1c
+                "X448", "sun.security.ec.XDHKeyAgreement.X448",
461e1c
+                ATTRS));
461e1c
+        }
461e1c
     }
461e1c
 
461e1c
     private void putEdDSAEntries() {
461e1c
@@ -375,21 +383,23 @@ public final class SunEC extends Provider {
461e1c
         putService(new ProviderServiceA(this, "KeyFactory",
461e1c
             "Ed448", "sun.security.ec.ed.EdDSAKeyFactory.Ed448", ATTRS));
461e1c
 
461e1c
-        putService(new ProviderService(this, "KeyPairGenerator",
461e1c
-            "EdDSA", "sun.security.ec.ed.EdDSAKeyPairGenerator", null, ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "KeyPairGenerator",
461e1c
-            "Ed25519", "sun.security.ec.ed.EdDSAKeyPairGenerator.Ed25519",
461e1c
-            ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "KeyPairGenerator",
461e1c
-            "Ed448", "sun.security.ec.ed.EdDSAKeyPairGenerator.Ed448",
461e1c
-            ATTRS));
461e1c
-
461e1c
-        putService(new ProviderService(this, "Signature",
461e1c
-            "EdDSA", "sun.security.ec.ed.EdDSASignature", null, ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "Signature",
461e1c
-            "Ed25519", "sun.security.ec.ed.EdDSASignature.Ed25519", ATTRS));
461e1c
-        putService(new ProviderServiceA(this, "Signature",
461e1c
-            "Ed448", "sun.security.ec.ed.EdDSASignature.Ed448", ATTRS));
461e1c
+        if (!systemFipsEnabled) {
461e1c
+            putService(new ProviderService(this, "KeyPairGenerator",
461e1c
+                "EdDSA", "sun.security.ec.ed.EdDSAKeyPairGenerator", null, ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "KeyPairGenerator",
461e1c
+                "Ed25519", "sun.security.ec.ed.EdDSAKeyPairGenerator.Ed25519",
461e1c
+                ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "KeyPairGenerator",
461e1c
+                "Ed448", "sun.security.ec.ed.EdDSAKeyPairGenerator.Ed448",
461e1c
+                ATTRS));
461e1c
+
461e1c
+            putService(new ProviderService(this, "Signature",
461e1c
+                "EdDSA", "sun.security.ec.ed.EdDSASignature", null, ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "Signature",
461e1c
+                "Ed25519", "sun.security.ec.ed.EdDSASignature.Ed25519", ATTRS));
461e1c
+            putService(new ProviderServiceA(this, "Signature",
461e1c
+                "Ed448", "sun.security.ec.ed.EdDSASignature.Ed448", ATTRS));
461e1c
+        }
461e1c
 
461e1c
     }
461e1c
 }