4aa6d7
diff --git a/common/autoconf/configure.ac b/common/autoconf/configure.ac
4aa6d7
index 151e5a109f8..a8761b500e0 100644
4aa6d7
--- a/common/autoconf/configure.ac
4aa6d7
+++ b/common/autoconf/configure.ac
4aa6d7
@@ -212,6 +212,7 @@ LIB_SETUP_FREETYPE
4aa6d7
 LIB_SETUP_ALSA
4aa6d7
 LIB_SETUP_FONTCONFIG
4aa6d7
 LIB_SETUP_MISC_LIBS
4aa6d7
+LIB_SETUP_SYSCONF_LIBS
4aa6d7
 LIB_SETUP_STATIC_LINK_LIBSTDCPP
4aa6d7
 LIB_SETUP_ON_WINDOWS
4aa6d7
 
4aa6d7
diff --git a/common/autoconf/generated-configure.sh b/common/autoconf/generated-configure.sh
927f13
index 71fabf4dbb3..17f4f50673d 100644
4aa6d7
--- a/common/autoconf/generated-configure.sh
4aa6d7
+++ b/common/autoconf/generated-configure.sh
4aa6d7
@@ -651,6 +651,9 @@ LLVM_CONFIG
4aa6d7
 LIBFFI_LIBS
4aa6d7
 LIBFFI_CFLAGS
4aa6d7
 STATIC_CXX_SETTING
4aa6d7
+USE_SYSCONF_NSS
4aa6d7
+NSS_LIBS
4aa6d7
+NSS_CFLAGS
4aa6d7
 LIBDL
4aa6d7
 LIBM
4aa6d7
 LIBZIP_CAN_USE_MMAP
4aa6d7
@@ -1111,6 +1114,7 @@ with_fontconfig
4aa6d7
 with_fontconfig_include
4aa6d7
 with_giflib
4aa6d7
 with_zlib
4aa6d7
+enable_sysconf_nss
4aa6d7
 with_stdc__lib
4aa6d7
 with_msvcr_dll
4aa6d7
 with_msvcp_dll
4aa6d7
@@ -1218,6 +1222,8 @@ FREETYPE_CFLAGS
4aa6d7
 FREETYPE_LIBS
4aa6d7
 ALSA_CFLAGS
4aa6d7
 ALSA_LIBS
4aa6d7
+NSS_CFLAGS
4aa6d7
+NSS_LIBS
4aa6d7
 LIBFFI_CFLAGS
4aa6d7
 LIBFFI_LIBS
4aa6d7
 CCACHE'
4aa6d7
@@ -1871,6 +1877,8 @@ Optional Features:
4aa6d7
                           disable bundling of the freetype library with the
4aa6d7
                           build result [enabled on Windows or when using
4aa6d7
                           --with-freetype, disabled otherwise]
4aa6d7
+  --enable-sysconf-nss    build the System Configurator (libsysconf) using the
4aa6d7
+                          system NSS library if available [disabled]
4aa6d7
   --enable-sjavac         use sjavac to do fast incremental compiles
4aa6d7
                           [disabled]
4aa6d7
   --disable-precompiled-headers
4aa6d7
@@ -2115,6 +2123,8 @@ Some influential environment variables:
4aa6d7
               linker flags for FREETYPE, overriding pkg-config
4aa6d7
   ALSA_CFLAGS C compiler flags for ALSA, overriding pkg-config
4aa6d7
   ALSA_LIBS   linker flags for ALSA, overriding pkg-config
4aa6d7
+  NSS_CFLAGS  C compiler flags for NSS, overriding pkg-config
4aa6d7
+  NSS_LIBS    linker flags for NSS, overriding pkg-config
4aa6d7
   LIBFFI_CFLAGS
4aa6d7
               C compiler flags for LIBFFI, overriding pkg-config
4aa6d7
   LIBFFI_LIBS linker flags for LIBFFI, overriding pkg-config
4aa6d7
@@ -2879,6 +2889,52 @@ $as_echo "$ac_res" >&6; }
4aa6d7
   eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
4aa6d7
 
4aa6d7
 } # ac_fn_c_check_header_compile
4aa6d7
+
4aa6d7
+# ac_fn_c_try_link LINENO
4aa6d7
+# -----------------------
4aa6d7
+# Try to link conftest.$ac_ext, and return whether this succeeded.
4aa6d7
+ac_fn_c_try_link ()
4aa6d7
+{
4aa6d7
+  as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
4aa6d7
+  rm -f conftest.$ac_objext conftest$ac_exeext
4aa6d7
+  if { { ac_try="$ac_link"
4aa6d7
+case "(($ac_try" in
4aa6d7
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
4aa6d7
+  *) ac_try_echo=$ac_try;;
4aa6d7
+esac
4aa6d7
+eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\""
4aa6d7
+$as_echo "$ac_try_echo"; } >&5
4aa6d7
+  (eval "$ac_link") 2>conftest.err
4aa6d7
+  ac_status=$?
4aa6d7
+  if test -s conftest.err; then
4aa6d7
+    grep -v '^ *+' conftest.err >conftest.er1
4aa6d7
+    cat conftest.er1 >&5
4aa6d7
+    mv -f conftest.er1 conftest.err
4aa6d7
+  fi
4aa6d7
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
4aa6d7
+  test $ac_status = 0; } && {
4aa6d7
+	 test -z "$ac_c_werror_flag" ||
4aa6d7
+	 test ! -s conftest.err
4aa6d7
+       } && test -s conftest$ac_exeext && {
4aa6d7
+	 test "$cross_compiling" = yes ||
4aa6d7
+	 test -x conftest$ac_exeext
4aa6d7
+       }; then :
4aa6d7
+  ac_retval=0
4aa6d7
+else
4aa6d7
+  $as_echo "$as_me: failed program was:" >&5
4aa6d7
+sed 's/^/| /' conftest.$ac_ext >&5
4aa6d7
+
4aa6d7
+	ac_retval=1
4aa6d7
+fi
4aa6d7
+  # Delete the IPA/IPO (Inter Procedural Analysis/Optimization) information
4aa6d7
+  # created by the PGI compiler (conftest_ipa8_conftest.oo), as it would
4aa6d7
+  # interfere with the next link command; also delete a directory that is
4aa6d7
+  # left behind by Apple's compiler.  We do this before executing the actions.
4aa6d7
+  rm -rf conftest.dSYM conftest_ipa8_conftest.oo
4aa6d7
+  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
4aa6d7
+  as_fn_set_status $ac_retval
4aa6d7
+
4aa6d7
+} # ac_fn_c_try_link
4aa6d7
 cat >config.log <<_ACEOF
4aa6d7
 This file contains any messages produced by compilers while
4aa6d7
 running configure, to aid debugging if configure makes a mistake.
4aa6d7
@@ -4049,6 +4105,11 @@ fi
4aa6d7
 
4aa6d7
 
4aa6d7
 
4aa6d7
+################################################################################
4aa6d7
+# Setup system configuration libraries
4aa6d7
+################################################################################
4aa6d7
+
4aa6d7
+
4aa6d7
 #
4aa6d7
 # Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
4aa6d7
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
927f13
@@ -49304,6 +49365,157 @@ fi
4aa6d7
   LIBS="$save_LIBS"
4aa6d7
 
4aa6d7
 
4aa6d7
+  ###############################################################################
4aa6d7
+  #
4aa6d7
+  # Check for the NSS library
4aa6d7
+  #
4aa6d7
+
4aa6d7
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to use the system NSS library with the System Configurator (libsysconf)" >&5
4aa6d7
+$as_echo_n "checking whether to use the system NSS library with the System Configurator (libsysconf)... " >&6; }
4aa6d7
+
4aa6d7
+  # default is not available
4aa6d7
+  DEFAULT_SYSCONF_NSS=no
4aa6d7
+
4aa6d7
+  # Check whether --enable-sysconf-nss was given.
4aa6d7
+if test "${enable_sysconf_nss+set}" = set; then :
4aa6d7
+  enableval=$enable_sysconf_nss;
4aa6d7
+    case "${enableval}" in
4aa6d7
+      yes)
4aa6d7
+        sysconf_nss=yes
4aa6d7
+        ;;
4aa6d7
+      *)
4aa6d7
+        sysconf_nss=no
4aa6d7
+        ;;
4aa6d7
+    esac
4aa6d7
+
4aa6d7
+else
4aa6d7
+
4aa6d7
+    sysconf_nss=${DEFAULT_SYSCONF_NSS}
4aa6d7
+
4aa6d7
+fi
4aa6d7
+
4aa6d7
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $sysconf_nss" >&5
4aa6d7
+$as_echo "$sysconf_nss" >&6; }
4aa6d7
+
4aa6d7
+  USE_SYSCONF_NSS=false
4aa6d7
+  if test "x${sysconf_nss}" = "xyes"; then
4aa6d7
+
4aa6d7
+pkg_failed=no
4aa6d7
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for NSS" >&5
4aa6d7
+$as_echo_n "checking for NSS... " >&6; }
4aa6d7
+
4aa6d7
+if test -n "$NSS_CFLAGS"; then
4aa6d7
+    pkg_cv_NSS_CFLAGS="$NSS_CFLAGS"
4aa6d7
+ elif test -n "$PKG_CONFIG"; then
4aa6d7
+    if test -n "$PKG_CONFIG" && \
4aa6d7
+    { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"nss >= 3.53\""; } >&5
4aa6d7
+  ($PKG_CONFIG --exists --print-errors "nss >= 3.53") 2>&5
4aa6d7
+  ac_status=$?
4aa6d7
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
4aa6d7
+  test $ac_status = 0; }; then
4aa6d7
+  pkg_cv_NSS_CFLAGS=`$PKG_CONFIG --cflags "nss >= 3.53" 2>/dev/null`
4aa6d7
+else
4aa6d7
+  pkg_failed=yes
4aa6d7
+fi
4aa6d7
+ else
4aa6d7
+    pkg_failed=untried
4aa6d7
+fi
4aa6d7
+if test -n "$NSS_LIBS"; then
4aa6d7
+    pkg_cv_NSS_LIBS="$NSS_LIBS"
4aa6d7
+ elif test -n "$PKG_CONFIG"; then
4aa6d7
+    if test -n "$PKG_CONFIG" && \
4aa6d7
+    { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"nss >= 3.53\""; } >&5
4aa6d7
+  ($PKG_CONFIG --exists --print-errors "nss >= 3.53") 2>&5
4aa6d7
+  ac_status=$?
4aa6d7
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
4aa6d7
+  test $ac_status = 0; }; then
4aa6d7
+  pkg_cv_NSS_LIBS=`$PKG_CONFIG --libs "nss >= 3.53" 2>/dev/null`
4aa6d7
+else
4aa6d7
+  pkg_failed=yes
4aa6d7
+fi
4aa6d7
+ else
4aa6d7
+    pkg_failed=untried
4aa6d7
+fi
4aa6d7
+
4aa6d7
+
4aa6d7
+
4aa6d7
+if test $pkg_failed = yes; then
4aa6d7
+
4aa6d7
+if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then
4aa6d7
+        _pkg_short_errors_supported=yes
4aa6d7
+else
4aa6d7
+        _pkg_short_errors_supported=no
4aa6d7
+fi
4aa6d7
+        if test $_pkg_short_errors_supported = yes; then
4aa6d7
+	        NSS_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors "nss >= 3.53" 2>&1`
4aa6d7
+        else
4aa6d7
+	        NSS_PKG_ERRORS=`$PKG_CONFIG --print-errors "nss >= 3.53" 2>&1`
4aa6d7
+        fi
4aa6d7
+	# Put the nasty error message in config.log where it belongs
4aa6d7
+	echo "$NSS_PKG_ERRORS" >&5
4aa6d7
+
4aa6d7
+	{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
4aa6d7
+$as_echo "no" >&6; }
4aa6d7
+                NSS_FOUND=no
4aa6d7
+elif test $pkg_failed = untried; then
4aa6d7
+	NSS_FOUND=no
4aa6d7
+else
4aa6d7
+	NSS_CFLAGS=$pkg_cv_NSS_CFLAGS
4aa6d7
+	NSS_LIBS=$pkg_cv_NSS_LIBS
4aa6d7
+        { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
4aa6d7
+$as_echo "yes" >&6; }
4aa6d7
+	NSS_FOUND=yes
4aa6d7
+fi
4aa6d7
+      if test "x${NSS_FOUND}" = "xyes"; then
4aa6d7
+         { $as_echo "$as_me:${as_lineno-$LINENO}: checking for system FIPS support in NSS" >&5
4aa6d7
+$as_echo_n "checking for system FIPS support in NSS... " >&6; }
4aa6d7
+         saved_libs="${LIBS}"
4aa6d7
+         saved_cflags="${CFLAGS}"
4aa6d7
+         CFLAGS="${CFLAGS} ${NSS_CFLAGS}"
4aa6d7
+         LIBS="${LIBS} ${NSS_LIBS}"
4aa6d7
+         ac_ext=c
4aa6d7
+ac_cpp='$CPP $CPPFLAGS'
4aa6d7
+ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
4aa6d7
+ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
4aa6d7
+ac_compiler_gnu=$ac_cv_c_compiler_gnu
4aa6d7
+
4aa6d7
+         cat confdefs.h - <<_ACEOF >conftest.$ac_ext
4aa6d7
+/* end confdefs.h.  */
4aa6d7
+#include <nss3/pk11pub.h>
4aa6d7
+int
4aa6d7
+main ()
4aa6d7
+{
4aa6d7
+SECMOD_GetSystemFIPSEnabled()
4aa6d7
+  ;
4aa6d7
+  return 0;
4aa6d7
+}
4aa6d7
+_ACEOF
4aa6d7
+if ac_fn_c_try_link "$LINENO"; then :
4aa6d7
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
4aa6d7
+$as_echo "yes" >&6; }
4aa6d7
+else
4aa6d7
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
4aa6d7
+$as_echo "no" >&6; }
4aa6d7
+                        as_fn_error $? "System NSS FIPS detection unavailable" "$LINENO" 5
4aa6d7
+fi
4aa6d7
+rm -f core conftest.err conftest.$ac_objext \
4aa6d7
+    conftest$ac_exeext conftest.$ac_ext
4aa6d7
+         ac_ext=cpp
4aa6d7
+ac_cpp='$CXXCPP $CPPFLAGS'
4aa6d7
+ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
4aa6d7
+ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
4aa6d7
+ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
4aa6d7
+
4aa6d7
+         CFLAGS="${saved_cflags}"
4aa6d7
+         LIBS="${saved_libs}"
4aa6d7
+         USE_SYSCONF_NSS=true
4aa6d7
+      else
4aa6d7
+                           as_fn_error $? "--enable-sysconf-nss specified, but NSS 3.53 or above not found." "$LINENO" 5
4aa6d7
+      fi
4aa6d7
+  fi
4aa6d7
+
4aa6d7
+
4aa6d7
+
4aa6d7
   ###############################################################################
4aa6d7
   #
4aa6d7
   # statically link libstdc++ before C++ ABI is stablized on Linux unless
4aa6d7
diff --git a/common/autoconf/libraries.m4 b/common/autoconf/libraries.m4
4aa6d7
index 6efae578ea9..0080846255b 100644
4aa6d7
--- a/common/autoconf/libraries.m4
4aa6d7
+++ b/common/autoconf/libraries.m4
4aa6d7
@@ -1067,3 +1067,63 @@ AC_DEFUN_ONCE([LIB_SETUP_ON_WINDOWS],
4aa6d7
     BASIC_DEPRECATED_ARG_WITH([dxsdk-include])
4aa6d7
   fi
4aa6d7
 ])
4aa6d7
+
4aa6d7
+################################################################################
4aa6d7
+# Setup system configuration libraries
4aa6d7
+################################################################################
4aa6d7
+AC_DEFUN_ONCE([LIB_SETUP_SYSCONF_LIBS],
4aa6d7
+[
4aa6d7
+  ###############################################################################
4aa6d7
+  #
4aa6d7
+  # Check for the NSS library
4aa6d7
+  #
4aa6d7
+
4aa6d7
+  AC_MSG_CHECKING([whether to use the system NSS library with the System Configurator (libsysconf)])
4aa6d7
+
4aa6d7
+  # default is not available
4aa6d7
+  DEFAULT_SYSCONF_NSS=no
4aa6d7
+
4aa6d7
+  AC_ARG_ENABLE([sysconf-nss], [AS_HELP_STRING([--enable-sysconf-nss],
4aa6d7
+     [build the System Configurator (libsysconf) using the system NSS library if available @<:@disabled@:>@])],
4aa6d7
+  [
4aa6d7
+    case "${enableval}" in
4aa6d7
+      yes)
4aa6d7
+        sysconf_nss=yes
4aa6d7
+        ;;
4aa6d7
+      *)
4aa6d7
+        sysconf_nss=no
4aa6d7
+        ;;
4aa6d7
+    esac
4aa6d7
+  ],
4aa6d7
+  [
4aa6d7
+    sysconf_nss=${DEFAULT_SYSCONF_NSS}
4aa6d7
+  ])
4aa6d7
+  AC_MSG_RESULT([$sysconf_nss])
4aa6d7
+
4aa6d7
+  USE_SYSCONF_NSS=false
4aa6d7
+  if test "x${sysconf_nss}" = "xyes"; then
4aa6d7
+      PKG_CHECK_MODULES(NSS, nss >= 3.53, [NSS_FOUND=yes], [NSS_FOUND=no])
4aa6d7
+      if test "x${NSS_FOUND}" = "xyes"; then
4aa6d7
+         AC_MSG_CHECKING([for system FIPS support in NSS])
4aa6d7
+         saved_libs="${LIBS}"
4aa6d7
+         saved_cflags="${CFLAGS}"
4aa6d7
+         CFLAGS="${CFLAGS} ${NSS_CFLAGS}"
4aa6d7
+         LIBS="${LIBS} ${NSS_LIBS}"
4aa6d7
+         AC_LANG_PUSH([C])
4aa6d7
+         AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <nss3/pk11pub.h>]],
4aa6d7
+                                         [[SECMOD_GetSystemFIPSEnabled()]])],
4aa6d7
+                        [AC_MSG_RESULT([yes])],
4aa6d7
+                        [AC_MSG_RESULT([no])
4aa6d7
+                        AC_MSG_ERROR([System NSS FIPS detection unavailable])])
4aa6d7
+         AC_LANG_POP([C])
4aa6d7
+         CFLAGS="${saved_cflags}"
4aa6d7
+         LIBS="${saved_libs}"
4aa6d7
+         USE_SYSCONF_NSS=true
4aa6d7
+      else
4aa6d7
+         dnl NSS 3.53 is the one that introduces the SECMOD_GetSystemFIPSEnabled API
4aa6d7
+         dnl in nss3/pk11pub.h.
4aa6d7
+         AC_MSG_ERROR([--enable-sysconf-nss specified, but NSS 3.53 or above not found.])
4aa6d7
+      fi
4aa6d7
+  fi
4aa6d7
+  AC_SUBST(USE_SYSCONF_NSS)
4aa6d7
+])
4aa6d7
diff --git a/common/autoconf/spec.gmk.in b/common/autoconf/spec.gmk.in
4aa6d7
index 506cf617087..7241593b1a4 100644
4aa6d7
--- a/common/autoconf/spec.gmk.in
4aa6d7
+++ b/common/autoconf/spec.gmk.in
4aa6d7
@@ -312,6 +312,10 @@ CUPS_CFLAGS:=@CUPS_CFLAGS@
4aa6d7
 ALSA_LIBS:=@ALSA_LIBS@
4aa6d7
 ALSA_CFLAGS:=@ALSA_CFLAGS@
4aa6d7
 
4aa6d7
+USE_SYSCONF_NSS:=@USE_SYSCONF_NSS@
4aa6d7
+NSS_LIBS:=@NSS_LIBS@
4aa6d7
+NSS_CFLAGS:=@NSS_CFLAGS@
4aa6d7
+
4aa6d7
 PACKAGE_PATH=@PACKAGE_PATH@
4aa6d7
 
4aa6d7
 # Source file for cacerts
4aa6d7
diff --git a/common/bin/compare_exceptions.sh.incl b/common/bin/compare_exceptions.sh.incl
4aa6d7
index 3b79a526f56..d2a0e39b206 100644
4aa6d7
--- a/common/bin/compare_exceptions.sh.incl
4aa6d7
+++ b/common/bin/compare_exceptions.sh.incl
4aa6d7
@@ -280,6 +280,7 @@ ACCEPTED_SMALL_SIZE_DIFF="
4aa6d7
 ./jre/lib/i386/libsplashscreen.so
4aa6d7
 ./jre/lib/i386/libsunec.so
4aa6d7
 ./jre/lib/i386/libsunwjdga.so
4aa6d7
+./jre/lib/i386/libsystemconf.so
4aa6d7
 ./jre/lib/i386/libt2k.so
4aa6d7
 ./jre/lib/i386/libunpack.so
4aa6d7
 ./jre/lib/i386/libverify.so
4aa6d7
@@ -433,6 +434,7 @@ ACCEPTED_SMALL_SIZE_DIFF="
4aa6d7
 ./jre/lib/amd64/libsplashscreen.so
4aa6d7
 ./jre/lib/amd64/libsunec.so
4aa6d7
 ./jre/lib/amd64/libsunwjdga.so
4aa6d7
+//jre/lib/amd64/libsystemconf.so
4aa6d7
 ./jre/lib/amd64/libt2k.so
4aa6d7
 ./jre/lib/amd64/libunpack.so
4aa6d7
 ./jre/lib/amd64/libverify.so
4aa6d7
@@ -587,6 +589,7 @@ ACCEPTED_SMALL_SIZE_DIFF="
4aa6d7
 ./jre/lib/sparc/libsplashscreen.so
4aa6d7
 ./jre/lib/sparc/libsunec.so
4aa6d7
 ./jre/lib/sparc/libsunwjdga.so
4aa6d7
+./jre/lib/sparc/libsystemconf.so
4aa6d7
 ./jre/lib/sparc/libt2k.so
4aa6d7
 ./jre/lib/sparc/libunpack.so
4aa6d7
 ./jre/lib/sparc/libverify.so
4aa6d7
@@ -741,6 +744,7 @@ ACCEPTED_SMALL_SIZE_DIFF="
4aa6d7
 ./jre/lib/sparcv9/libsplashscreen.so
4aa6d7
 ./jre/lib/sparcv9/libsunec.so
4aa6d7
 ./jre/lib/sparcv9/libsunwjdga.so
4aa6d7
+./jre/lib/sparcv9/libsystemconf.so
4aa6d7
 ./jre/lib/sparcv9/libt2k.so
4aa6d7
 ./jre/lib/sparcv9/libunpack.so
4aa6d7
 ./jre/lib/sparcv9/libverify.so
4aa6d7
diff --git a/common/nb_native/nbproject/configurations.xml b/common/nb_native/nbproject/configurations.xml
4aa6d7
index d2beed0b93a..3b6aef98d9a 100644
4aa6d7
--- a/common/nb_native/nbproject/configurations.xml
4aa6d7
+++ b/common/nb_native/nbproject/configurations.xml
4aa6d7
@@ -53,6 +53,9 @@
4aa6d7
                   <in>jvmtiEnterTrace.cpp</in>
4aa6d7
                 </df>
4aa6d7
               </df>
4aa6d7
+              <df name="libsystemconf">
4aa6d7
+                <in>systemconf.c</in>
4aa6d7
+              </df>
4aa6d7
             </df>
4aa6d7
           </df>
4aa6d7
           <df name="jdk">
4aa6d7
@@ -12772,6 +12775,11 @@
4aa6d7
             tool="0"
4aa6d7
             flavor2="0">
4aa6d7
       </item>
4aa6d7
+      
4aa6d7
+            ex="false"
4aa6d7
+            tool="0"
4aa6d7
+            flavor2="0">
4aa6d7
+      </item>
4aa6d7
       
4aa6d7
             ex="false"
4aa6d7
             tool="0"
4aa6d7
diff --git a/jdk/make/lib/SecurityLibraries.gmk b/jdk/make/lib/SecurityLibraries.gmk
4aa6d7
index b0b85d80448..47a41d7518d 100644
4aa6d7
--- a/jdk/make/lib/SecurityLibraries.gmk
4aa6d7
+++ b/jdk/make/lib/SecurityLibraries.gmk
4aa6d7
@@ -289,3 +289,34 @@ ifeq ($(OPENJDK_TARGET_OS), solaris)
4aa6d7
 
4aa6d7
   endif
4aa6d7
 endif
4aa6d7
+
4aa6d7
+################################################################################
4aa6d7
+# Create the systemconf library
4aa6d7
+
4aa6d7
+LIBSYSTEMCONF_CFLAGS :=
4aa6d7
+LIBSYSTEMCONF_CXXFLAGS :=
4aa6d7
+
4aa6d7
+ifeq ($(USE_SYSCONF_NSS), true)
4aa6d7
+  LIBSYSTEMCONF_CFLAGS += $(NSS_CFLAGS) -DSYSCONF_NSS
4aa6d7
+  LIBSYSTEMCONF_CXXFLAGS += $(NSS_CFLAGS) -DSYSCONF_NSS
4aa6d7
+endif
4aa6d7
+
4aa6d7
+ifeq ($(OPENJDK_BUILD_OS), linux)
4aa6d7
+  $(eval $(call SetupNativeCompilation,BUILD_LIBSYSTEMCONF, \
4aa6d7
+      LIBRARY := systemconf, \
4aa6d7
+      OUTPUT_DIR := $(INSTALL_LIBRARIES_HERE), \
4aa6d7
+      SRC := $(JDK_TOPDIR)/src/$(OPENJDK_TARGET_OS_API_DIR)/native/java/security, \
4aa6d7
+      LANG := C, \
4aa6d7
+      OPTIMIZATION := LOW, \
4aa6d7
+      CFLAGS := $(CFLAGS_JDKLIB) $(LIBSYSTEMCONF_CFLAGS), \
4aa6d7
+      CXXFLAGS := $(CXXFLAGS_JDKLIB) $(LIBSYSTEMCONF_CXXFLAGS), \
4aa6d7
+      MAPFILE := $(JDK_TOPDIR)/make/mapfiles/libsystemconf/mapfile-vers, \
4aa6d7
+      LDFLAGS := $(LDFLAGS_JDKLIB) \
4aa6d7
+          $(call SET_SHARED_LIBRARY_ORIGIN), \
4aa6d7
+      LDFLAGS_SUFFIX := $(LIBDL) $(NSS_LIBS), \
4aa6d7
+      OBJECT_DIR := $(JDK_OUTPUTDIR)/objs/libsystemconf, \
4aa6d7
+      DEBUG_SYMBOLS := $(DEBUG_ALL_BINARIES)))
4aa6d7
+
4aa6d7
+  BUILD_LIBRARIES += $(BUILD_LIBSYSTEMCONF)
4aa6d7
+endif
4aa6d7
+
4aa6d7
diff --git a/jdk/make/mapfiles/libsystemconf/mapfile-vers b/jdk/make/mapfiles/libsystemconf/mapfile-vers
4aa6d7
new file mode 100644
4aa6d7
index 00000000000..a65ceb3b78c
4aa6d7
--- /dev/null
4aa6d7
+++ b/jdk/make/mapfiles/libsystemconf/mapfile-vers
4aa6d7
@@ -0,0 +1,35 @@
4aa6d7
+#
4aa6d7
+# Copyright (c) 2021, Red Hat, Inc.
4aa6d7
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4aa6d7
+#
4aa6d7
+# This code is free software; you can redistribute it and/or modify it
4aa6d7
+# under the terms of the GNU General Public License version 2 only, as
4aa6d7
+# published by the Free Software Foundation.  Oracle designates this
4aa6d7
+# particular file as subject to the "Classpath" exception as provided
4aa6d7
+# by Oracle in the LICENSE file that accompanied this code.
4aa6d7
+#
4aa6d7
+# This code is distributed in the hope that it will be useful, but WITHOUT
4aa6d7
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4aa6d7
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4aa6d7
+# version 2 for more details (a copy is included in the LICENSE file that
4aa6d7
+# accompanied this code).
4aa6d7
+#
4aa6d7
+# You should have received a copy of the GNU General Public License version
4aa6d7
+# 2 along with this work; if not, write to the Free Software Foundation,
4aa6d7
+# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4aa6d7
+#
4aa6d7
+# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4aa6d7
+# or visit www.oracle.com if you need additional information or have any
4aa6d7
+# questions.
4aa6d7
+#
4aa6d7
+
4aa6d7
+# Define public interface.
4aa6d7
+
4aa6d7
+SUNWprivate_1.1 {
4aa6d7
+	global:
4aa6d7
+		DEF_JNI_OnLoad;
4aa6d7
+		DEF_JNI_OnUnLoad;
4aa6d7
+		Java_java_security_SystemConfigurator_getSystemFIPSEnabled;
4aa6d7
+	local:
4aa6d7
+		*;
4aa6d7
+};
4aa6d7
diff --git a/jdk/src/share/classes/java/security/Security.java b/jdk/src/share/classes/java/security/Security.java
4aa6d7
index 0db09da7061..813b907db3e 100644
4aa6d7
--- a/jdk/src/share/classes/java/security/Security.java
4aa6d7
+++ b/jdk/src/share/classes/java/security/Security.java
4aa6d7
@@ -30,6 +30,8 @@ import java.util.*;
4aa6d7
 import java.util.concurrent.ConcurrentHashMap;
4aa6d7
 import java.io.*;
4aa6d7
 import java.net.URL;
4aa6d7
+import sun.misc.SharedSecrets;
4aa6d7
+import sun.misc.JavaSecuritySystemConfiguratorAccess;
4aa6d7
 import sun.security.util.Debug;
4aa6d7
 import sun.security.util.PropertyExpander;
4aa6d7
 
4aa6d7
@@ -43,11 +45,19 @@ import sun.security.jca.*;
4aa6d7
  * implementation-specific location, which is typically the properties file
4aa6d7
  * {@code lib/security/java.security} in the Java installation directory.
4aa6d7
  *
4aa6d7
+ * 

Additional default values of security properties are read from a

4aa6d7
+ * system-specific location, if available.

4aa6d7
+ *
4aa6d7
  * @author Benjamin Renaud
4aa6d7
  */
4aa6d7
 
4aa6d7
 public final class Security {
4aa6d7
 
4aa6d7
+    private static final String SYS_PROP_SWITCH =
4aa6d7
+        "java.security.disableSystemPropertiesFile";
4aa6d7
+    private static final String SEC_PROP_SWITCH =
4aa6d7
+        "security.useSystemPropertiesFile";
4aa6d7
+
4aa6d7
     /* Are we debugging? -- for developers */
4aa6d7
     private static final Debug sdebug =
4aa6d7
                         Debug.getInstance("properties");
4aa6d7
@@ -62,6 +72,19 @@ public final class Security {
4aa6d7
     }
4aa6d7
 
4aa6d7
     static {
4aa6d7
+        // Initialise here as used by code with system properties disabled
4aa6d7
+        SharedSecrets.setJavaSecuritySystemConfiguratorAccess(
4aa6d7
+            new JavaSecuritySystemConfiguratorAccess() {
4aa6d7
+                @Override
4aa6d7
+                public boolean isSystemFipsEnabled() {
4aa6d7
+                    return SystemConfigurator.isSystemFipsEnabled();
4aa6d7
+                }
4aa6d7
+                @Override
4aa6d7
+                public boolean isPlainKeySupportEnabled() {
4aa6d7
+                    return SystemConfigurator.isPlainKeySupportEnabled();
4aa6d7
+                }
4aa6d7
+            });
4aa6d7
+
4aa6d7
         // doPrivileged here because there are multiple
4aa6d7
         // things in initialize that might require privs.
4aa6d7
         // (the FileInputStream call and the File.exists call,
4aa6d7
@@ -78,6 +101,7 @@ public final class Security {
4aa6d7
         props = new Properties();
4aa6d7
         boolean loadedProps = false;
4aa6d7
         boolean overrideAll = false;
4aa6d7
+        boolean systemSecPropsEnabled = false;
4aa6d7
 
4aa6d7
         // first load the system properties file
4aa6d7
         // to determine the value of security.overridePropertiesFile
4aa6d7
@@ -93,6 +117,7 @@ public final class Security {
4aa6d7
                 if (sdebug != null) {
4aa6d7
                     sdebug.println("reading security properties file: " +
4aa6d7
                                 propFile);
4aa6d7
+                    sdebug.println(props.toString());
4aa6d7
                 }
4aa6d7
             } catch (IOException e) {
4aa6d7
                 if (sdebug != null) {
4aa6d7
@@ -187,6 +212,61 @@ public final class Security {
4aa6d7
             }
4aa6d7
         }
4aa6d7
 
4aa6d7
+        boolean sysUseProps = Boolean.valueOf(System.getProperty(SYS_PROP_SWITCH, "false"));
4aa6d7
+        boolean secUseProps = Boolean.valueOf(props.getProperty(SEC_PROP_SWITCH));
4aa6d7
+        if (sdebug != null) {
4aa6d7
+            sdebug.println(SYS_PROP_SWITCH + "=" + sysUseProps);
4aa6d7
+            sdebug.println(SEC_PROP_SWITCH + "=" + secUseProps);
4aa6d7
+        }
4aa6d7
+        if (!sysUseProps && secUseProps) {
4aa6d7
+            systemSecPropsEnabled = SystemConfigurator.configureSysProps(props);
4aa6d7
+            if (!systemSecPropsEnabled) {
4aa6d7
+                if (sdebug != null) {
4aa6d7
+                    sdebug.println("WARNING: System security properties could not be loaded.");
4aa6d7
+                }
4aa6d7
+            }
4aa6d7
+        } else {
4aa6d7
+            if (sdebug != null) {
4aa6d7
+                sdebug.println("System security property support disabled by user.");
4aa6d7
+            }
4aa6d7
+        }
4aa6d7
+
4aa6d7
+        // FIPS support depends on the contents of java.security so
4aa6d7
+        // ensure it has loaded first
4aa6d7
+        if (loadedProps && systemSecPropsEnabled) {
4aa6d7
+            boolean shouldEnable;
4aa6d7
+            String sysProp = System.getProperty("com.redhat.fips");
4aa6d7
+            if (sysProp == null) {
4aa6d7
+                shouldEnable = true;
4aa6d7
+                if (sdebug != null) {
4aa6d7
+                    sdebug.println("com.redhat.fips unset, using default value of true");
4aa6d7
+                }
4aa6d7
+            } else {
4aa6d7
+                shouldEnable = Boolean.valueOf(sysProp);
4aa6d7
+                if (sdebug != null) {
4aa6d7
+                    sdebug.println("com.redhat.fips set, using its value " + shouldEnable);
4aa6d7
+                }
4aa6d7
+             }
4aa6d7
+            if (shouldEnable) {
4aa6d7
+                boolean fipsEnabled = SystemConfigurator.configureFIPS(props);
4aa6d7
+                if (sdebug != null) {
4aa6d7
+                    if (fipsEnabled) {
4aa6d7
+                        sdebug.println("FIPS mode support configured and enabled.");
4aa6d7
+                    } else {
4aa6d7
+                        sdebug.println("FIPS mode support disabled.");
4aa6d7
+                    }
4aa6d7
+                }
4aa6d7
+            } else {
4aa6d7
+                if (sdebug != null ) {
4aa6d7
+                    sdebug.println("FIPS mode support disabled by user.");
4aa6d7
+                }
4aa6d7
+            }
4aa6d7
+        } else {
4aa6d7
+            if (sdebug != null) {
4aa6d7
+                sdebug.println("WARNING: FIPS mode support can not be enabled without " +
4aa6d7
+                               "system security properties being enabled.");
4aa6d7
+            }
4aa6d7
+        }
4aa6d7
     }
4aa6d7
 
4aa6d7
     /*
4aa6d7
diff --git a/jdk/src/share/classes/java/security/SystemConfigurator.java b/jdk/src/share/classes/java/security/SystemConfigurator.java
4aa6d7
new file mode 100644
4aa6d7
index 00000000000..a24a0445db2
4aa6d7
--- /dev/null
4aa6d7
+++ b/jdk/src/share/classes/java/security/SystemConfigurator.java
4aa6d7
@@ -0,0 +1,248 @@
4aa6d7
+/*
4aa6d7
+ * Copyright (c) 2019, 2021, Red Hat, Inc.
4aa6d7
+ *
4aa6d7
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4aa6d7
+ *
4aa6d7
+ * This code is free software; you can redistribute it and/or modify it
4aa6d7
+ * under the terms of the GNU General Public License version 2 only, as
4aa6d7
+ * published by the Free Software Foundation.  Oracle designates this
4aa6d7
+ * particular file as subject to the "Classpath" exception as provided
4aa6d7
+ * by Oracle in the LICENSE file that accompanied this code.
4aa6d7
+ *
4aa6d7
+ * This code is distributed in the hope that it will be useful, but WITHOUT
4aa6d7
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4aa6d7
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4aa6d7
+ * version 2 for more details (a copy is included in the LICENSE file that
4aa6d7
+ * accompanied this code).
4aa6d7
+ *
4aa6d7
+ * You should have received a copy of the GNU General Public License version
4aa6d7
+ * 2 along with this work; if not, write to the Free Software Foundation,
4aa6d7
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4aa6d7
+ *
4aa6d7
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4aa6d7
+ * or visit www.oracle.com if you need additional information or have any
4aa6d7
+ * questions.
4aa6d7
+ */
4aa6d7
+
4aa6d7
+package java.security;
4aa6d7
+
4aa6d7
+import java.io.BufferedInputStream;
4aa6d7
+import java.io.FileInputStream;
4aa6d7
+import java.io.IOException;
4aa6d7
+
4aa6d7
+import java.util.Iterator;
4aa6d7
+import java.util.Map.Entry;
4aa6d7
+import java.util.Properties;
4aa6d7
+
4aa6d7
+import sun.security.util.Debug;
4aa6d7
+
4aa6d7
+/**
4aa6d7
+ * Internal class to align OpenJDK with global crypto-policies.
4aa6d7
+ * Called from java.security.Security class initialization,
4aa6d7
+ * during startup.
4aa6d7
+ *
4aa6d7
+ */
4aa6d7
+
4aa6d7
+final class SystemConfigurator {
4aa6d7
+
4aa6d7
+    private static final Debug sdebug =
4aa6d7
+            Debug.getInstance("properties");
4aa6d7
+
4aa6d7
+    private static final String CRYPTO_POLICIES_BASE_DIR =
4aa6d7
+            "/etc/crypto-policies";
4aa6d7
+
4aa6d7
+    private static final String CRYPTO_POLICIES_JAVA_CONFIG =
4aa6d7
+            CRYPTO_POLICIES_BASE_DIR + "/back-ends/java.config";
4aa6d7
+
4aa6d7
+    private static boolean systemFipsEnabled = false;
4aa6d7
+    private static boolean plainKeySupportEnabled = false;
4aa6d7
+
4aa6d7
+    private static final String SYSTEMCONF_NATIVE_LIB = "systemconf";
4aa6d7
+
4aa6d7
+    private static native boolean getSystemFIPSEnabled()
4aa6d7
+            throws IOException;
4aa6d7
+
4aa6d7
+    static {
4aa6d7
+        AccessController.doPrivileged(new PrivilegedAction<Void>() {
4aa6d7
+            public Void run() {
4aa6d7
+                System.loadLibrary(SYSTEMCONF_NATIVE_LIB);
4aa6d7
+                return null;
4aa6d7
+            }
4aa6d7
+        });
4aa6d7
+    }
4aa6d7
+
4aa6d7
+    /*
4aa6d7
+     * Invoked when java.security.Security class is initialized, if
4aa6d7
+     * java.security.disableSystemPropertiesFile property is not set and
4aa6d7
+     * security.useSystemPropertiesFile is true.
4aa6d7
+     */
4aa6d7
+    static boolean configureSysProps(Properties props) {
4aa6d7
+        boolean systemSecPropsLoaded = false;
4aa6d7
+
4aa6d7
+        try (BufferedInputStream bis =
4aa6d7
+                new BufferedInputStream(
4aa6d7
+                        new FileInputStream(CRYPTO_POLICIES_JAVA_CONFIG))) {
4aa6d7
+            props.load(bis);
4aa6d7
+            systemSecPropsLoaded = true;
4aa6d7
+            if (sdebug != null) {
4aa6d7
+                sdebug.println("reading system security properties file " +
4aa6d7
+                        CRYPTO_POLICIES_JAVA_CONFIG);
4aa6d7
+                sdebug.println(props.toString());
4aa6d7
+            }
4aa6d7
+        } catch (IOException e) {
4aa6d7
+            if (sdebug != null) {
4aa6d7
+                sdebug.println("unable to load security properties from " +
4aa6d7
+                        CRYPTO_POLICIES_JAVA_CONFIG);
4aa6d7
+                e.printStackTrace();
4aa6d7
+            }
4aa6d7
+        }
4aa6d7
+        return systemSecPropsLoaded;
4aa6d7
+    }
4aa6d7
+
4aa6d7
+    /*
4aa6d7
+     * Invoked at the end of java.security.Security initialisation
4aa6d7
+     * if java.security properties have been loaded
4aa6d7
+     */
4aa6d7
+    static boolean configureFIPS(Properties props) {
4aa6d7
+        boolean loadedProps = false;
4aa6d7
+
4aa6d7
+        try {
4aa6d7
+            if (enableFips()) {
4aa6d7
+                if (sdebug != null) { sdebug.println("FIPS mode detected"); }
4aa6d7
+                // Remove all security providers
4aa6d7
+                Iterator<Entry<Object, Object>> i = props.entrySet().iterator();
4aa6d7
+                while (i.hasNext()) {
4aa6d7
+                    Entry<Object, Object> e = i.next();
4aa6d7
+                    if (((String) e.getKey()).startsWith("security.provider")) {
4aa6d7
+                        if (sdebug != null) { sdebug.println("Removing provider: " + e); }
4aa6d7
+                        i.remove();
4aa6d7
+                    }
4aa6d7
+                }
4aa6d7
+                // Add FIPS security providers
4aa6d7
+                String fipsProviderValue = null;
4aa6d7
+                for (int n = 1;
4aa6d7
+                     (fipsProviderValue = (String) props.get("fips.provider." + n)) != null; n++) {
4aa6d7
+                    String fipsProviderKey = "security.provider." + n;
4aa6d7
+                    if (sdebug != null) {
4aa6d7
+                        sdebug.println("Adding provider " + n + ": " +
4aa6d7
+                                fipsProviderKey + "=" + fipsProviderValue);
4aa6d7
+                    }
4aa6d7
+                    props.put(fipsProviderKey, fipsProviderValue);
4aa6d7
+                }
4aa6d7
+                // Add other security properties
4aa6d7
+                String keystoreTypeValue = (String) props.get("fips.keystore.type");
4aa6d7
+                if (keystoreTypeValue != null) {
4aa6d7
+                    String nonFipsKeystoreType = props.getProperty("keystore.type");
4aa6d7
+                    props.put("keystore.type", keystoreTypeValue);
4aa6d7
+                    if (keystoreTypeValue.equals("PKCS11")) {
4aa6d7
+                        // If keystore.type is PKCS11, javax.net.ssl.keyStore
4aa6d7
+                        // must be "NONE". See JDK-8238264.
4aa6d7
+                        System.setProperty("javax.net.ssl.keyStore", "NONE");
4aa6d7
+                    }
4aa6d7
+                    if (System.getProperty("javax.net.ssl.trustStoreType") == null) {
4aa6d7
+                        // If no trustStoreType has been set, use the
4aa6d7
+                        // previous keystore.type under FIPS mode. In
4aa6d7
+                        // a default configuration, the Trust Store will
4aa6d7
+                        // be 'cacerts' (JKS type).
4aa6d7
+                        System.setProperty("javax.net.ssl.trustStoreType",
4aa6d7
+                                nonFipsKeystoreType);
4aa6d7
+                    }
4aa6d7
+                    if (sdebug != null) {
4aa6d7
+                        sdebug.println("FIPS mode default keystore.type = " +
4aa6d7
+                                keystoreTypeValue);
4aa6d7
+                        sdebug.println("FIPS mode javax.net.ssl.keyStore = " +
4aa6d7
+                                System.getProperty("javax.net.ssl.keyStore", ""));
4aa6d7
+                        sdebug.println("FIPS mode javax.net.ssl.trustStoreType = " +
4aa6d7
+                                System.getProperty("javax.net.ssl.trustStoreType", ""));
4aa6d7
+                    }
4aa6d7
+                }
4aa6d7
+                loadedProps = true;
4aa6d7
+                systemFipsEnabled = true;
4aa6d7
+                String plainKeySupport = System.getProperty("com.redhat.fips.plainKeySupport",
4aa6d7
+                                                            "true");
4aa6d7
+                plainKeySupportEnabled = !"false".equals(plainKeySupport);
4aa6d7
+                if (sdebug != null) {
4aa6d7
+                    if (plainKeySupportEnabled) {
4aa6d7
+                        sdebug.println("FIPS support enabled with plain key support");
4aa6d7
+                    } else {
4aa6d7
+                        sdebug.println("FIPS support enabled without plain key support");
4aa6d7
+                    }
4aa6d7
+                }
4aa6d7
+            } else {
4aa6d7
+                if (sdebug != null) { sdebug.println("FIPS mode not detected"); }
4aa6d7
+            }
4aa6d7
+        } catch (Exception e) {
4aa6d7
+            if (sdebug != null) {
4aa6d7
+                sdebug.println("unable to load FIPS configuration");
4aa6d7
+                e.printStackTrace();
4aa6d7
+            }
4aa6d7
+        }
4aa6d7
+        return loadedProps;
4aa6d7
+    }
4aa6d7
+
4aa6d7
+    /**
4aa6d7
+     * Returns whether or not global system FIPS alignment is enabled.
4aa6d7
+     *
4aa6d7
+     * Value is always 'false' before java.security.Security class is
4aa6d7
+     * initialized.
4aa6d7
+     *
4aa6d7
+     * Call from out of this package through SharedSecrets:
4aa6d7
+     *   SharedSecrets.getJavaSecuritySystemConfiguratorAccess()
4aa6d7
+     *           .isSystemFipsEnabled();
4aa6d7
+     *
4aa6d7
+     * @return  a boolean value indicating whether or not global
4aa6d7
+     *          system FIPS alignment is enabled.
4aa6d7
+     */
4aa6d7
+    static boolean isSystemFipsEnabled() {
4aa6d7
+        return systemFipsEnabled;
4aa6d7
+    }
4aa6d7
+
4aa6d7
+    /**
4aa6d7
+     * Returns {@code true} if system FIPS alignment is enabled
4aa6d7
+     * and plain key support is allowed.  Plain key support is
4aa6d7
+     * enabled by default but can be disabled with
4aa6d7
+     * {@code -Dcom.redhat.fips.plainKeySupport=false}.
4aa6d7
+     *
4aa6d7
+     * @return a boolean indicating whether plain key support
4aa6d7
+     *         should be enabled.
4aa6d7
+     */
4aa6d7
+    static boolean isPlainKeySupportEnabled() {
4aa6d7
+        return plainKeySupportEnabled;
4aa6d7
+    }
4aa6d7
+
4aa6d7
+    /**
4aa6d7
+     * Determines whether FIPS mode should be enabled.
4aa6d7
+     *
4aa6d7
+     * OpenJDK FIPS mode will be enabled only if the system is in
4aa6d7
+     * FIPS mode.
4aa6d7
+     *
4aa6d7
+     * Calls to this method only occur if the system property
4aa6d7
+     * com.redhat.fips is not set to false.
4aa6d7
+     *
4aa6d7
+     * There are 2 possible ways in which OpenJDK detects that the system
4aa6d7
+     * is in FIPS mode: 1) if the NSS SECMOD_GetSystemFIPSEnabled API is
4aa6d7
+     * available at OpenJDK's built-time, it is called; 2) otherwise, the
4aa6d7
+     * /proc/sys/crypto/fips_enabled file is read.
4aa6d7
+     *
4aa6d7
+     * @return true if the system is in FIPS mode
4aa6d7
+     */
4aa6d7
+    private static boolean enableFips() throws IOException {
4aa6d7
+        if (sdebug != null) {
4aa6d7
+            sdebug.println("Calling getSystemFIPSEnabled (libsystemconf)...");
4aa6d7
+        }
4aa6d7
+        try {
4aa6d7
+            boolean fipsEnabled = getSystemFIPSEnabled();
4aa6d7
+            if (sdebug != null) {
4aa6d7
+                sdebug.println("Call to getSystemFIPSEnabled (libsystemconf) returned: "
4aa6d7
+                               + fipsEnabled);
4aa6d7
+            }
4aa6d7
+            return fipsEnabled;
4aa6d7
+        } catch (IOException e) {
4aa6d7
+            if (sdebug != null) {
4aa6d7
+                sdebug.println("Call to getSystemFIPSEnabled (libsystemconf) failed:");
4aa6d7
+                sdebug.println(e.getMessage());
4aa6d7
+            }
4aa6d7
+            throw e;
4aa6d7
+        }
4aa6d7
+    }
4aa6d7
+}
4aa6d7
diff --git a/jdk/src/share/classes/sun/misc/JavaSecuritySystemConfiguratorAccess.java b/jdk/src/share/classes/sun/misc/JavaSecuritySystemConfiguratorAccess.java
4aa6d7
new file mode 100644
4aa6d7
index 00000000000..5c30a8b29c7
4aa6d7
--- /dev/null
4aa6d7
+++ b/jdk/src/share/classes/sun/misc/JavaSecuritySystemConfiguratorAccess.java
4aa6d7
@@ -0,0 +1,31 @@
4aa6d7
+/*
4aa6d7
+ * Copyright (c) 2020, Red Hat, Inc.
4aa6d7
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4aa6d7
+ *
4aa6d7
+ * This code is free software; you can redistribute it and/or modify it
4aa6d7
+ * under the terms of the GNU General Public License version 2 only, as
4aa6d7
+ * published by the Free Software Foundation.  Oracle designates this
4aa6d7
+ * particular file as subject to the "Classpath" exception as provided
4aa6d7
+ * by Oracle in the LICENSE file that accompanied this code.
4aa6d7
+ *
4aa6d7
+ * This code is distributed in the hope that it will be useful, but WITHOUT
4aa6d7
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4aa6d7
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4aa6d7
+ * version 2 for more details (a copy is included in the LICENSE file that
4aa6d7
+ * accompanied this code).
4aa6d7
+ *
4aa6d7
+ * You should have received a copy of the GNU General Public License version
4aa6d7
+ * 2 along with this work; if not, write to the Free Software Foundation,
4aa6d7
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4aa6d7
+ *
4aa6d7
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4aa6d7
+ * or visit www.oracle.com if you need additional information or have any
4aa6d7
+ * questions.
4aa6d7
+ */
4aa6d7
+
4aa6d7
+package sun.misc;
4aa6d7
+
4aa6d7
+public interface JavaSecuritySystemConfiguratorAccess {
4aa6d7
+    boolean isSystemFipsEnabled();
4aa6d7
+    boolean isPlainKeySupportEnabled();
4aa6d7
+}
4aa6d7
diff --git a/jdk/src/share/classes/sun/misc/SharedSecrets.java b/jdk/src/share/classes/sun/misc/SharedSecrets.java
4aa6d7
index f065a2c685d..0dafe6f59cf 100644
4aa6d7
--- a/jdk/src/share/classes/sun/misc/SharedSecrets.java
4aa6d7
+++ b/jdk/src/share/classes/sun/misc/SharedSecrets.java
4aa6d7
@@ -31,6 +31,7 @@ import java.io.Console;
4aa6d7
 import java.io.FileDescriptor;
4aa6d7
 import java.io.ObjectInputStream;
4aa6d7
 import java.security.ProtectionDomain;
4aa6d7
+import java.security.Security;
4aa6d7
 import java.security.Signature;
4aa6d7
 
4aa6d7
 import java.security.AccessController;
4aa6d7
@@ -63,6 +64,7 @@ public class SharedSecrets {
4aa6d7
     private static JavaObjectInputStreamReadString javaObjectInputStreamReadString;
4aa6d7
     private static JavaObjectInputStreamAccess javaObjectInputStreamAccess;
4aa6d7
     private static JavaSecuritySignatureAccess javaSecuritySignatureAccess;
4aa6d7
+    private static JavaSecuritySystemConfiguratorAccess javaSecuritySystemConfiguratorAccess;
4aa6d7
 
4aa6d7
     public static JavaUtilJarAccess javaUtilJarAccess() {
4aa6d7
         if (javaUtilJarAccess == null) {
4aa6d7
@@ -248,4 +250,15 @@ public class SharedSecrets {
4aa6d7
         }
4aa6d7
         return javaxCryptoSealedObjectAccess;
4aa6d7
     }
4aa6d7
+
4aa6d7
+    public static void setJavaSecuritySystemConfiguratorAccess(JavaSecuritySystemConfiguratorAccess jssca) {
4aa6d7
+        javaSecuritySystemConfiguratorAccess = jssca;
4aa6d7
+    }
4aa6d7
+
4aa6d7
+    public static JavaSecuritySystemConfiguratorAccess getJavaSecuritySystemConfiguratorAccess() {
4aa6d7
+        if (javaSecuritySystemConfiguratorAccess == null) {
4aa6d7
+            unsafe.ensureClassInitialized(Security.class);
4aa6d7
+        }
4aa6d7
+        return javaSecuritySystemConfiguratorAccess;
4aa6d7
+    }
4aa6d7
 }
4aa6d7
diff --git a/jdk/src/share/classes/sun/security/pkcs11/FIPSKeyImporter.java b/jdk/src/share/classes/sun/security/pkcs11/FIPSKeyImporter.java
4aa6d7
new file mode 100644
4aa6d7
index 00000000000..14d19450390
4aa6d7
--- /dev/null
4aa6d7
+++ b/jdk/src/share/classes/sun/security/pkcs11/FIPSKeyImporter.java
4aa6d7
@@ -0,0 +1,290 @@
4aa6d7
+/*
4aa6d7
+ * Copyright (c) 2021, Red Hat, Inc.
4aa6d7
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4aa6d7
+ *
4aa6d7
+ * This code is free software; you can redistribute it and/or modify it
4aa6d7
+ * under the terms of the GNU General Public License version 2 only, as
4aa6d7
+ * published by the Free Software Foundation.  Oracle designates this
4aa6d7
+ * particular file as subject to the "Classpath" exception as provided
4aa6d7
+ * by Oracle in the LICENSE file that accompanied this code.
4aa6d7
+ *
4aa6d7
+ * This code is distributed in the hope that it will be useful, but WITHOUT
4aa6d7
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4aa6d7
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4aa6d7
+ * version 2 for more details (a copy is included in the LICENSE file that
4aa6d7
+ * accompanied this code).
4aa6d7
+ *
4aa6d7
+ * You should have received a copy of the GNU General Public License version
4aa6d7
+ * 2 along with this work; if not, write to the Free Software Foundation,
4aa6d7
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4aa6d7
+ *
4aa6d7
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4aa6d7
+ * or visit www.oracle.com if you need additional information or have any
4aa6d7
+ * questions.
4aa6d7
+ */
4aa6d7
+
4aa6d7
+package sun.security.pkcs11;
4aa6d7
+
4aa6d7
+import java.math.BigInteger;
4aa6d7
+import java.security.KeyFactory;
4aa6d7
+import java.security.Provider;
4aa6d7
+import java.security.Security;
4aa6d7
+import java.util.HashMap;
4aa6d7
+import java.util.Map;
4aa6d7
+import java.util.concurrent.locks.ReentrantLock;
4aa6d7
+
4aa6d7
+import javax.crypto.Cipher;
4aa6d7
+import javax.crypto.spec.DHPrivateKeySpec;
4aa6d7
+import javax.crypto.spec.IvParameterSpec;
4aa6d7
+
4aa6d7
+import sun.security.jca.JCAUtil;
4aa6d7
+import sun.security.pkcs11.TemplateManager;
4aa6d7
+import sun.security.pkcs11.wrapper.CK_ATTRIBUTE;
4aa6d7
+import sun.security.pkcs11.wrapper.CK_MECHANISM;
4aa6d7
+import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
4aa6d7
+import sun.security.pkcs11.wrapper.PKCS11Exception;
4aa6d7
+import sun.security.rsa.RSAUtil.KeyType;
4aa6d7
+import sun.security.util.Debug;
4aa6d7
+import sun.security.util.ECUtil;
4aa6d7
+
4aa6d7
+final class FIPSKeyImporter {
4aa6d7
+
4aa6d7
+    private static final Debug debug =
4aa6d7
+            Debug.getInstance("sunpkcs11");
4aa6d7
+
4aa6d7
+    private static P11Key importerKey = null;
4aa6d7
+    private static final ReentrantLock importerKeyLock = new ReentrantLock();
4aa6d7
+    private static CK_MECHANISM importerKeyMechanism = null;
4aa6d7
+    private static Cipher importerCipher = null;
4aa6d7
+
4aa6d7
+    private static Provider sunECProvider = null;
4aa6d7
+    private static final ReentrantLock sunECProviderLock = new ReentrantLock();
4aa6d7
+
4aa6d7
+    private static KeyFactory DHKF = null;
4aa6d7
+    private static final ReentrantLock DHKFLock = new ReentrantLock();
4aa6d7
+
4aa6d7
+    static Long importKey(SunPKCS11 sunPKCS11, long hSession, CK_ATTRIBUTE[] attributes)
4aa6d7
+            throws PKCS11Exception {
4aa6d7
+        long keyID = -1;
4aa6d7
+        Token token = sunPKCS11.getToken();
4aa6d7
+        if (debug != null) {
4aa6d7
+            debug.println("Private or Secret key will be imported in" +
4aa6d7
+                    " system FIPS mode.");
4aa6d7
+        }
4aa6d7
+        if (importerKey == null) {
4aa6d7
+            importerKeyLock.lock();
4aa6d7
+            try {
4aa6d7
+                if (importerKey == null) {
4aa6d7
+                    if (importerKeyMechanism == null) {
4aa6d7
+                        // Importer Key creation has not been tried yet. Try it.
4aa6d7
+                        createImporterKey(token);
4aa6d7
+                    }
4aa6d7
+                    if (importerKey == null || importerCipher == null) {
4aa6d7
+                        if (debug != null) {
4aa6d7
+                            debug.println("Importer Key could not be" +
4aa6d7
+                                    " generated.");
4aa6d7
+                        }
4aa6d7
+                        throw new PKCS11Exception(CKR_GENERAL_ERROR);
4aa6d7
+                    }
4aa6d7
+                    if (debug != null) {
4aa6d7
+                        debug.println("Importer Key successfully" +
4aa6d7
+                                " generated.");
4aa6d7
+                    }
4aa6d7
+                }
4aa6d7
+            } finally {
4aa6d7
+                importerKeyLock.unlock();
4aa6d7
+            }
4aa6d7
+        }
4aa6d7
+        long importerKeyID = importerKey.getKeyID();
4aa6d7
+        try {
4aa6d7
+            byte[] keyBytes = null;
4aa6d7
+            byte[] encKeyBytes = null;
4aa6d7
+            long keyClass = 0L;
4aa6d7
+            long keyType = 0L;
4aa6d7
+            Map<Long, CK_ATTRIBUTE> attrsMap = new HashMap<>();
4aa6d7
+            for (CK_ATTRIBUTE attr : attributes) {
4aa6d7
+                if (attr.type == CKA_CLASS) {
4aa6d7
+                    keyClass = attr.getLong();
4aa6d7
+                } else if (attr.type == CKA_KEY_TYPE) {
4aa6d7
+                    keyType = attr.getLong();
4aa6d7
+                }
4aa6d7
+                attrsMap.put(attr.type, attr);
4aa6d7
+            }
4aa6d7
+            BigInteger v = null;
4aa6d7
+            if (keyClass == CKO_PRIVATE_KEY) {
4aa6d7
+                if (keyType == CKK_RSA) {
4aa6d7
+                    if (debug != null) {
4aa6d7
+                        debug.println("Importing an RSA private key...");
4aa6d7
+                    }
4aa6d7
+                    keyBytes = sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(
4aa6d7
+                            KeyType.RSA,
4aa6d7
+                            null,
4aa6d7
+                            ((v = attrsMap.get(CKA_MODULUS).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO,
4aa6d7
+                            ((v = attrsMap.get(CKA_PUBLIC_EXPONENT).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO,
4aa6d7
+                            ((v = attrsMap.get(CKA_PRIVATE_EXPONENT).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO,
4aa6d7
+                            ((v = attrsMap.get(CKA_PRIME_1).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO,
4aa6d7
+                            ((v = attrsMap.get(CKA_PRIME_2).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO,
4aa6d7
+                            ((v = attrsMap.get(CKA_EXPONENT_1).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO,
4aa6d7
+                            ((v = attrsMap.get(CKA_EXPONENT_2).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO,
4aa6d7
+                            ((v = attrsMap.get(CKA_COEFFICIENT).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO
4aa6d7
+                            ).getEncoded();
4aa6d7
+                } else if (keyType == CKK_DSA) {
4aa6d7
+                    if (debug != null) {
4aa6d7
+                        debug.println("Importing a DSA private key...");
4aa6d7
+                    }
4aa6d7
+                    keyBytes = new sun.security.provider.DSAPrivateKey(
4aa6d7
+                            ((v = attrsMap.get(CKA_VALUE).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO,
4aa6d7
+                            ((v = attrsMap.get(CKA_PRIME).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO,
4aa6d7
+                            ((v = attrsMap.get(CKA_SUBPRIME).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO,
4aa6d7
+                            ((v = attrsMap.get(CKA_BASE).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO
4aa6d7
+                            ).getEncoded();
4aa6d7
+                    if (token.config.getNssNetscapeDbWorkaround() &&
4aa6d7
+                            attrsMap.get(CKA_NETSCAPE_DB) == null) {
4aa6d7
+                        attrsMap.put(CKA_NETSCAPE_DB,
4aa6d7
+                                new CK_ATTRIBUTE(CKA_NETSCAPE_DB, BigInteger.ZERO));
4aa6d7
+                    }
4aa6d7
+                } else if (keyType == CKK_EC) {
4aa6d7
+                    if (debug != null) {
4aa6d7
+                        debug.println("Importing an EC private key...");
4aa6d7
+                    }
4aa6d7
+                    if (sunECProvider == null) {
4aa6d7
+                        sunECProviderLock.lock();
4aa6d7
+                        try {
4aa6d7
+                            if (sunECProvider == null) {
4aa6d7
+                                sunECProvider = Security.getProvider("SunEC");
4aa6d7
+                            }
4aa6d7
+                        } finally {
4aa6d7
+                            sunECProviderLock.unlock();
4aa6d7
+                        }
4aa6d7
+                    }
4aa6d7
+                    keyBytes = P11ECUtil.generateECPrivateKey(
4aa6d7
+                            ((v = attrsMap.get(CKA_VALUE).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO,
4aa6d7
+                            ECUtil.getECParameterSpec(sunECProvider,
4aa6d7
+                                    attrsMap.get(CKA_EC_PARAMS).getByteArray()))
4aa6d7
+                            .getEncoded();
4aa6d7
+                    if (token.config.getNssNetscapeDbWorkaround() &&
4aa6d7
+                            attrsMap.get(CKA_NETSCAPE_DB) == null) {
4aa6d7
+                        attrsMap.put(CKA_NETSCAPE_DB,
4aa6d7
+                                new CK_ATTRIBUTE(CKA_NETSCAPE_DB, BigInteger.ZERO));
4aa6d7
+                    }
4aa6d7
+                } else if (keyType == CKK_DH) {
4aa6d7
+                    if (debug != null) {
4aa6d7
+                        debug.println("Importing a Diffie-Hellman private key...");
4aa6d7
+                    }
4aa6d7
+                    if (DHKF == null) {
4aa6d7
+                        DHKFLock.lock();
4aa6d7
+                        try {
4aa6d7
+                            if (DHKF == null) {
4aa6d7
+                                DHKF = KeyFactory.getInstance(
4aa6d7
+                                        "DH", P11Util.getSunJceProvider());
4aa6d7
+                            }
4aa6d7
+                        } finally {
4aa6d7
+                            DHKFLock.unlock();
4aa6d7
+                        }
4aa6d7
+                    }
4aa6d7
+                    DHPrivateKeySpec spec = new DHPrivateKeySpec
4aa6d7
+                            (((v = attrsMap.get(CKA_VALUE).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO,
4aa6d7
+                            ((v = attrsMap.get(CKA_PRIME).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO,
4aa6d7
+                            ((v = attrsMap.get(CKA_BASE).getBigInteger()) != null)
4aa6d7
+                                    ? v : BigInteger.ZERO);
4aa6d7
+                    keyBytes = DHKF.generatePrivate(spec).getEncoded();
4aa6d7
+                    if (token.config.getNssNetscapeDbWorkaround() &&
4aa6d7
+                            attrsMap.get(CKA_NETSCAPE_DB) == null) {
4aa6d7
+                        attrsMap.put(CKA_NETSCAPE_DB,
4aa6d7
+                                new CK_ATTRIBUTE(CKA_NETSCAPE_DB, BigInteger.ZERO));
4aa6d7
+                    }
4aa6d7
+                } else {
4aa6d7
+                    if (debug != null) {
4aa6d7
+                        debug.println("Unrecognized private key type.");
4aa6d7
+                    }
4aa6d7
+                    throw new PKCS11Exception(CKR_GENERAL_ERROR);
4aa6d7
+                }
4aa6d7
+            } else if (keyClass == CKO_SECRET_KEY) {
4aa6d7
+                if (debug != null) {
4aa6d7
+                    debug.println("Importing a secret key...");
4aa6d7
+                }
4aa6d7
+                keyBytes = attrsMap.get(CKA_VALUE).getByteArray();
4aa6d7
+            }
4aa6d7
+            if (keyBytes == null || keyBytes.length == 0) {
4aa6d7
+                if (debug != null) {
4aa6d7
+                    debug.println("Private or secret key plain bytes could" +
4aa6d7
+                            " not be obtained. Import failed.");
4aa6d7
+                }
4aa6d7
+                throw new PKCS11Exception(CKR_GENERAL_ERROR);
4aa6d7
+            }
4aa6d7
+            importerCipher.init(Cipher.ENCRYPT_MODE, importerKey,
4aa6d7
+                    new IvParameterSpec((byte[])importerKeyMechanism.pParameter),
4aa6d7
+                    null);
4aa6d7
+            attributes = new CK_ATTRIBUTE[attrsMap.size()];
4aa6d7
+            attrsMap.values().toArray(attributes);
4aa6d7
+            encKeyBytes = importerCipher.doFinal(keyBytes);
4aa6d7
+            attributes = token.getAttributes(TemplateManager.O_IMPORT,
4aa6d7
+                    keyClass, keyType, attributes);
4aa6d7
+            keyID = token.p11.C_UnwrapKey(hSession,
4aa6d7
+                    importerKeyMechanism, importerKeyID, encKeyBytes, attributes);
4aa6d7
+            if (debug != null) {
4aa6d7
+                debug.println("Imported key ID: " + keyID);
4aa6d7
+            }
4aa6d7
+        } catch (Throwable t) {
4aa6d7
+            throw new PKCS11Exception(CKR_GENERAL_ERROR);
4aa6d7
+        } finally {
4aa6d7
+            importerKey.releaseKeyID();
4aa6d7
+        }
4aa6d7
+        return Long.valueOf(keyID);
4aa6d7
+    }
4aa6d7
+
4aa6d7
+    private static void createImporterKey(Token token) {
4aa6d7
+        if (debug != null) {
4aa6d7
+            debug.println("Generating Importer Key...");
4aa6d7
+        }
4aa6d7
+        byte[] iv = new byte[16];
4aa6d7
+        JCAUtil.getSecureRandom().nextBytes(iv);
4aa6d7
+        importerKeyMechanism = new CK_MECHANISM(CKM_AES_CBC_PAD, iv);
4aa6d7
+        try {
4aa6d7
+            CK_ATTRIBUTE[] attributes = token.getAttributes(TemplateManager.O_GENERATE,
4aa6d7
+                            CKO_SECRET_KEY, CKK_AES, new CK_ATTRIBUTE[] {
4aa6d7
+                                    new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
4aa6d7
+                                    new CK_ATTRIBUTE(CKA_VALUE_LEN, 256 >> 3)});
4aa6d7
+            Session s = null;
4aa6d7
+            try {
4aa6d7
+                s = token.getObjSession();
4aa6d7
+                long keyID = token.p11.C_GenerateKey(
4aa6d7
+                        s.id(), new CK_MECHANISM(CKM_AES_KEY_GEN),
4aa6d7
+                        attributes);
4aa6d7
+                if (debug != null) {
4aa6d7
+                    debug.println("Importer Key ID: " + keyID);
4aa6d7
+                }
4aa6d7
+                importerKey = (P11Key)P11Key.secretKey(s, keyID, "AES",
4aa6d7
+                        256 >> 3, null);
4aa6d7
+            } catch (PKCS11Exception e) {
4aa6d7
+                // best effort
4aa6d7
+            } finally {
4aa6d7
+                token.releaseSession(s);
4aa6d7
+            }
4aa6d7
+            if (importerKey != null) {
4aa6d7
+                importerCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
4aa6d7
+            }
4aa6d7
+        } catch (Throwable t) {
4aa6d7
+            // best effort
4aa6d7
+            importerKey = null;
4aa6d7
+            importerCipher = null;
4aa6d7
+            // importerKeyMechanism value is kept initialized to indicate that
4aa6d7
+            // Importer Key creation has been tried and failed.
4aa6d7
+        }
4aa6d7
+    }
4aa6d7
+}
4aa6d7
diff --git a/jdk/src/share/classes/sun/security/pkcs11/SunPKCS11.java b/jdk/src/share/classes/sun/security/pkcs11/SunPKCS11.java
4aa6d7
index fedcd7743ef..f9d70863bd1 100644
4aa6d7
--- a/jdk/src/share/classes/sun/security/pkcs11/SunPKCS11.java
4aa6d7
+++ b/jdk/src/share/classes/sun/security/pkcs11/SunPKCS11.java
4aa6d7
@@ -26,6 +26,9 @@
4aa6d7
 package sun.security.pkcs11;
4aa6d7
 
4aa6d7
 import java.io.*;
4aa6d7
+import java.lang.invoke.MethodHandle;
4aa6d7
+import java.lang.invoke.MethodHandles;
4aa6d7
+import java.lang.invoke.MethodType;
4aa6d7
 import java.util.*;
4aa6d7
 
4aa6d7
 import java.security.*;
4aa6d7
@@ -42,6 +45,8 @@ import javax.security.auth.callback.ConfirmationCallback;
4aa6d7
 import javax.security.auth.callback.PasswordCallback;
4aa6d7
 import javax.security.auth.callback.TextOutputCallback;
4aa6d7
 
4aa6d7
+import sun.misc.SharedSecrets;
4aa6d7
+
4aa6d7
 import sun.security.util.Debug;
4aa6d7
 import sun.security.util.ResourcesMgr;
4aa6d7
 
4aa6d7
@@ -58,6 +63,29 @@ import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
4aa6d7
  */
4aa6d7
 public final class SunPKCS11 extends AuthProvider {
4aa6d7
 
4aa6d7
+    private static final boolean systemFipsEnabled = SharedSecrets
4aa6d7
+            .getJavaSecuritySystemConfiguratorAccess().isSystemFipsEnabled();
4aa6d7
+
4aa6d7
+    private static final boolean plainKeySupportEnabled = SharedSecrets
4aa6d7
+            .getJavaSecuritySystemConfiguratorAccess().isPlainKeySupportEnabled();
4aa6d7
+
4aa6d7
+    private static final MethodHandle fipsImportKey;
4aa6d7
+    static {
4aa6d7
+        MethodHandle fipsImportKeyTmp = null;
4aa6d7
+        if (plainKeySupportEnabled) {
4aa6d7
+            try {
4aa6d7
+                fipsImportKeyTmp = MethodHandles.lookup().findStatic(
4aa6d7
+                        FIPSKeyImporter.class, "importKey",
4aa6d7
+                        MethodType.methodType(Long.class, SunPKCS11.class,
4aa6d7
+                        long.class, CK_ATTRIBUTE[].class));
4aa6d7
+            } catch (Throwable t) {
4aa6d7
+                throw new SecurityException("FIPS key importer initialization" +
4aa6d7
+                        " failed", t);
4aa6d7
+            }
4aa6d7
+        }
4aa6d7
+        fipsImportKey = fipsImportKeyTmp;
4aa6d7
+    }
4aa6d7
+
4aa6d7
     private static final long serialVersionUID = -1354835039035306505L;
4aa6d7
 
4aa6d7
     static final Debug debug = Debug.getInstance("sunpkcs11");
4aa6d7
@@ -309,10 +337,15 @@ public final class SunPKCS11 extends AuthProvider {
4aa6d7
             // request multithreaded access first
4aa6d7
             initArgs.flags = CKF_OS_LOCKING_OK;
4aa6d7
             PKCS11 tmpPKCS11;
4aa6d7
+            MethodHandle fipsKeyImporter = null;
4aa6d7
+            if (plainKeySupportEnabled) {
4aa6d7
+                fipsKeyImporter = MethodHandles.insertArguments(
4aa6d7
+                        fipsImportKey, 0, this);
4aa6d7
+            }
4aa6d7
             try {
4aa6d7
                 tmpPKCS11 = PKCS11.getInstance(
4aa6d7
                     library, functionList, initArgs,
4aa6d7
-                    config.getOmitInitialize());
4aa6d7
+                    config.getOmitInitialize(), fipsKeyImporter);
4aa6d7
             } catch (PKCS11Exception e) {
4aa6d7
                 if (debug != null) {
4aa6d7
                     debug.println("Multi-threaded initialization failed: " + e);
4aa6d7
@@ -328,7 +361,7 @@ public final class SunPKCS11 extends AuthProvider {
4aa6d7
                     initArgs.flags = 0;
4aa6d7
                 }
4aa6d7
                 tmpPKCS11 = PKCS11.getInstance(library,
4aa6d7
-                    functionList, initArgs, config.getOmitInitialize());
4aa6d7
+                    functionList, initArgs, config.getOmitInitialize(), fipsKeyImporter);
4aa6d7
             }
4aa6d7
             p11 = tmpPKCS11;
4aa6d7
 
4aa6d7
@@ -368,6 +401,24 @@ public final class SunPKCS11 extends AuthProvider {
4aa6d7
             if (nssModule != null) {
4aa6d7
                 nssModule.setProvider(this);
4aa6d7
             }
4aa6d7
+            if (systemFipsEnabled) {
4aa6d7
+                // The NSS Software Token in FIPS 140-2 mode requires a user
4aa6d7
+                // login for most operations. See sftk_fipsCheck. The NSS DB
4aa6d7
+                // (/etc/pki/nssdb) PIN is empty.
4aa6d7
+                Session session = null;
4aa6d7
+                try {
4aa6d7
+                    session = token.getOpSession();
4aa6d7
+                    p11.C_Login(session.id(), CKU_USER, new char[] {});
4aa6d7
+                } catch (PKCS11Exception p11e) {
4aa6d7
+                    if (debug != null) {
4aa6d7
+                        debug.println("Error during token login: " +
4aa6d7
+                                p11e.getMessage());
4aa6d7
+                    }
4aa6d7
+                    throw p11e;
4aa6d7
+                } finally {
4aa6d7
+                    token.releaseSession(session);
4aa6d7
+                }
4aa6d7
+            }
4aa6d7
         } catch (Exception e) {
4aa6d7
             if (config.getHandleStartupErrors() == Config.ERR_IGNORE_ALL) {
4aa6d7
                 throw new UnsupportedOperationException
4aa6d7
diff --git a/jdk/src/share/classes/sun/security/pkcs11/wrapper/PKCS11.java b/jdk/src/share/classes/sun/security/pkcs11/wrapper/PKCS11.java
4aa6d7
index 2e42d1d9fb0..1b7eed1c656 100644
4aa6d7
--- a/jdk/src/share/classes/sun/security/pkcs11/wrapper/PKCS11.java
4aa6d7
+++ b/jdk/src/share/classes/sun/security/pkcs11/wrapper/PKCS11.java
4aa6d7
@@ -49,6 +49,7 @@ package sun.security.pkcs11.wrapper;
4aa6d7
 
4aa6d7
 import java.io.File;
4aa6d7
 import java.io.IOException;
4aa6d7
+import java.lang.invoke.MethodHandle;
4aa6d7
 import java.util.*;
4aa6d7
 
4aa6d7
 import java.security.AccessController;
4aa6d7
@@ -145,18 +146,41 @@ public class PKCS11 {
4aa6d7
         this.pkcs11ModulePath = pkcs11ModulePath;
4aa6d7
     }
4aa6d7
 
4aa6d7
+    /*
4aa6d7
+     * Compatibility wrapper to allow this method to work as before
4aa6d7
+     * when FIPS mode support is not active.
4aa6d7
+     */
4aa6d7
+    public static synchronized PKCS11 getInstance(String pkcs11ModulePath,
4aa6d7
+           String functionList, CK_C_INITIALIZE_ARGS pInitArgs,
4aa6d7
+           boolean omitInitialize) throws IOException, PKCS11Exception {
4aa6d7
+        return getInstance(pkcs11ModulePath, functionList,
4aa6d7
+                           pInitArgs, omitInitialize, null);
4aa6d7
+    }
4aa6d7
+
4aa6d7
     public static synchronized PKCS11 getInstance(String pkcs11ModulePath,
4aa6d7
             String functionList, CK_C_INITIALIZE_ARGS pInitArgs,
4aa6d7
-            boolean omitInitialize) throws IOException, PKCS11Exception {
4aa6d7
+            boolean omitInitialize, MethodHandle fipsKeyImporter)
4aa6d7
+                    throws IOException, PKCS11Exception {
4aa6d7
         // we may only call C_Initialize once per native .so/.dll
4aa6d7
         // so keep a cache using the (non-canonicalized!) path
4aa6d7
         PKCS11 pkcs11 = moduleMap.get(pkcs11ModulePath);
4aa6d7
         if (pkcs11 == null) {
4aa6d7
+            boolean nssFipsMode = fipsKeyImporter != null;
4aa6d7
             if ((pInitArgs != null)
4aa6d7
                     && ((pInitArgs.flags & CKF_OS_LOCKING_OK) != 0)) {
4aa6d7
-                pkcs11 = new PKCS11(pkcs11ModulePath, functionList);
4aa6d7
+                if (nssFipsMode) {
4aa6d7
+                    pkcs11 = new FIPSPKCS11(pkcs11ModulePath, functionList,
4aa6d7
+                            fipsKeyImporter);
4aa6d7
+                } else {
4aa6d7
+                    pkcs11 = new PKCS11(pkcs11ModulePath, functionList);
4aa6d7
+                }
4aa6d7
             } else {
4aa6d7
-                pkcs11 = new SynchronizedPKCS11(pkcs11ModulePath, functionList);
4aa6d7
+                if (nssFipsMode) {
4aa6d7
+                    pkcs11 = new SynchronizedFIPSPKCS11(pkcs11ModulePath,
4aa6d7
+                            functionList, fipsKeyImporter);
4aa6d7
+                } else {
4aa6d7
+                    pkcs11 = new SynchronizedPKCS11(pkcs11ModulePath, functionList);
4aa6d7
+                }
4aa6d7
             }
4aa6d7
             if (omitInitialize == false) {
4aa6d7
                 try {
4aa6d7
@@ -1905,4 +1929,69 @@ static class SynchronizedPKCS11 extends PKCS11 {
4aa6d7
         super.C_GenerateRandom(hSession, randomData);
4aa6d7
     }
4aa6d7
 }
4aa6d7
+
4aa6d7
+// PKCS11 subclass that allows using plain private or secret keys in
4aa6d7
+// FIPS-configured NSS Software Tokens. Only used when System FIPS
4aa6d7
+// is enabled.
4aa6d7
+static class FIPSPKCS11 extends PKCS11 {
4aa6d7
+    private MethodHandle fipsKeyImporter;
4aa6d7
+    FIPSPKCS11(String pkcs11ModulePath, String functionListName,
4aa6d7
+            MethodHandle fipsKeyImporter) throws IOException {
4aa6d7
+        super(pkcs11ModulePath, functionListName);
4aa6d7
+        this.fipsKeyImporter = fipsKeyImporter;
4aa6d7
+    }
4aa6d7
+
4aa6d7
+    public synchronized long C_CreateObject(long hSession,
4aa6d7
+            CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {
4aa6d7
+        // Creating sensitive key objects from plain key material in a
4aa6d7
+        // FIPS-configured NSS Software Token is not allowed. We apply
4aa6d7
+        // a key-unwrapping scheme to achieve so.
4aa6d7
+        if (FIPSPKCS11Helper.isSensitiveObject(pTemplate)) {
4aa6d7
+            try {
4aa6d7
+                return ((Long)fipsKeyImporter.invoke(hSession, pTemplate))
4aa6d7
+                        .longValue();
4aa6d7
+            } catch (Throwable t) {
4aa6d7
+                throw new PKCS11Exception(CKR_GENERAL_ERROR);
4aa6d7
+            }
4aa6d7
+        }
4aa6d7
+        return super.C_CreateObject(hSession, pTemplate);
4aa6d7
+    }
4aa6d7
+}
4aa6d7
+
4aa6d7
+// FIPSPKCS11 synchronized counterpart.
4aa6d7
+static class SynchronizedFIPSPKCS11 extends SynchronizedPKCS11 {
4aa6d7
+    private MethodHandle fipsKeyImporter;
4aa6d7
+    SynchronizedFIPSPKCS11(String pkcs11ModulePath, String functionListName,
4aa6d7
+            MethodHandle fipsKeyImporter) throws IOException {
4aa6d7
+        super(pkcs11ModulePath, functionListName);
4aa6d7
+        this.fipsKeyImporter = fipsKeyImporter;
4aa6d7
+    }
4aa6d7
+
4aa6d7
+    public synchronized long C_CreateObject(long hSession,
4aa6d7
+            CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {
4aa6d7
+        // See FIPSPKCS11::C_CreateObject.
4aa6d7
+        if (FIPSPKCS11Helper.isSensitiveObject(pTemplate)) {
4aa6d7
+            try {
4aa6d7
+                return ((Long)fipsKeyImporter.invoke(hSession, pTemplate))
4aa6d7
+                        .longValue();
4aa6d7
+            } catch (Throwable t) {
4aa6d7
+                throw new PKCS11Exception(CKR_GENERAL_ERROR);
4aa6d7
+            }
4aa6d7
+        }
4aa6d7
+        return super.C_CreateObject(hSession, pTemplate);
4aa6d7
+    }
4aa6d7
+}
4aa6d7
+
4aa6d7
+private static class FIPSPKCS11Helper {
4aa6d7
+    static boolean isSensitiveObject(CK_ATTRIBUTE[] pTemplate) {
4aa6d7
+        for (CK_ATTRIBUTE attr : pTemplate) {
4aa6d7
+            if (attr.type == CKA_CLASS &&
4aa6d7
+                    (attr.getLong() == CKO_PRIVATE_KEY ||
4aa6d7
+                    attr.getLong() == CKO_SECRET_KEY)) {
4aa6d7
+                return true;
4aa6d7
+            }
4aa6d7
+        }
4aa6d7
+        return false;
4aa6d7
+    }
4aa6d7
+}
4aa6d7
 }
4aa6d7
diff --git a/jdk/src/share/classes/sun/security/ssl/KeyManagerFactoryImpl.java b/jdk/src/share/classes/sun/security/ssl/KeyManagerFactoryImpl.java
4aa6d7
index ffee2c1603b..98119479823 100644
4aa6d7
--- a/jdk/src/share/classes/sun/security/ssl/KeyManagerFactoryImpl.java
4aa6d7
+++ b/jdk/src/share/classes/sun/security/ssl/KeyManagerFactoryImpl.java
4aa6d7
@@ -33,8 +33,13 @@ import java.security.KeyStore.*;
4aa6d7
 
4aa6d7
 import javax.net.ssl.*;
4aa6d7
 
4aa6d7
+import sun.misc.SharedSecrets;
4aa6d7
+
4aa6d7
 abstract class KeyManagerFactoryImpl extends KeyManagerFactorySpi {
4aa6d7
 
4aa6d7
+    private static final boolean plainKeySupportEnabled = SharedSecrets
4aa6d7
+            .getJavaSecuritySystemConfiguratorAccess().isPlainKeySupportEnabled();
4aa6d7
+
4aa6d7
     X509ExtendedKeyManager keyManager;
4aa6d7
     boolean isInitialized;
4aa6d7
 
4aa6d7
@@ -62,7 +67,8 @@ abstract class KeyManagerFactoryImpl extends KeyManagerFactorySpi {
4aa6d7
                 KeyStoreException, NoSuchAlgorithmException,
4aa6d7
                 UnrecoverableKeyException {
4aa6d7
             if ((ks != null) && SunJSSE.isFIPS()) {
4aa6d7
-                if (ks.getProvider() != SunJSSE.cryptoProvider) {
4aa6d7
+                if (ks.getProvider() != SunJSSE.cryptoProvider &&
4aa6d7
+                        !plainKeySupportEnabled) {
4aa6d7
                     throw new KeyStoreException("FIPS mode: KeyStore must be "
4aa6d7
                         + "from provider " + SunJSSE.cryptoProvider.getName());
4aa6d7
                 }
4aa6d7
@@ -91,8 +97,8 @@ abstract class KeyManagerFactoryImpl extends KeyManagerFactorySpi {
4aa6d7
                 keyManager = new X509KeyManagerImpl(
4aa6d7
                         Collections.<Builder>emptyList());
4aa6d7
             } else {
4aa6d7
-                if (SunJSSE.isFIPS() &&
4aa6d7
-                        (ks.getProvider() != SunJSSE.cryptoProvider)) {
4aa6d7
+                if (SunJSSE.isFIPS() && (ks.getProvider() != SunJSSE.cryptoProvider)
4aa6d7
+                        && !plainKeySupportEnabled) {
4aa6d7
                     throw new KeyStoreException(
4aa6d7
                         "FIPS mode: KeyStore must be " +
4aa6d7
                         "from provider " + SunJSSE.cryptoProvider.getName());
4aa6d7
diff --git a/jdk/src/share/classes/sun/security/ssl/SSLContextImpl.java b/jdk/src/share/classes/sun/security/ssl/SSLContextImpl.java
927f13
index 820e10164fc..6fe2c29389f 100644
4aa6d7
--- a/jdk/src/share/classes/sun/security/ssl/SSLContextImpl.java
4aa6d7
+++ b/jdk/src/share/classes/sun/security/ssl/SSLContextImpl.java
4aa6d7
@@ -31,6 +31,7 @@ import java.security.*;
4aa6d7
 import java.security.cert.*;
4aa6d7
 import java.util.*;
4aa6d7
 import javax.net.ssl.*;
4aa6d7
+import sun.misc.SharedSecrets;
4aa6d7
 import sun.security.action.GetPropertyAction;
4aa6d7
 import sun.security.provider.certpath.AlgorithmChecker;
4aa6d7
 import sun.security.validator.Validator;
4aa6d7
@@ -539,20 +540,38 @@ public abstract class SSLContextImpl extends SSLContextSpi {
4aa6d7
 
4aa6d7
         static {
4aa6d7
             if (SunJSSE.isFIPS()) {
4aa6d7
-                supportedProtocols = Arrays.asList(
4aa6d7
-                    ProtocolVersion.TLS13,
4aa6d7
-                    ProtocolVersion.TLS12,
4aa6d7
-                    ProtocolVersion.TLS11,
4aa6d7
-                    ProtocolVersion.TLS10
4aa6d7
-                );
4aa6d7
+                if (SharedSecrets.getJavaSecuritySystemConfiguratorAccess()
4aa6d7
+                        .isSystemFipsEnabled()) {
4aa6d7
+                    // RH1860986: TLSv1.3 key derivation not supported with
4aa6d7
+                    // the Security Providers available in system FIPS mode.
4aa6d7
+                    supportedProtocols = Arrays.asList(
4aa6d7
+                        ProtocolVersion.TLS12,
4aa6d7
+                        ProtocolVersion.TLS11,
4aa6d7
+                        ProtocolVersion.TLS10
4aa6d7
+                    );
4aa6d7
 
4aa6d7
-                serverDefaultProtocols = getAvailableProtocols(
4aa6d7
-                        new ProtocolVersion[] {
4aa6d7
-                    ProtocolVersion.TLS13,
4aa6d7
-                    ProtocolVersion.TLS12,
4aa6d7
-                    ProtocolVersion.TLS11,
4aa6d7
-                    ProtocolVersion.TLS10
4aa6d7
-                });
4aa6d7
+                    serverDefaultProtocols = getAvailableProtocols(
4aa6d7
+                            new ProtocolVersion[] {
4aa6d7
+                        ProtocolVersion.TLS12,
4aa6d7
+                        ProtocolVersion.TLS11,
4aa6d7
+                        ProtocolVersion.TLS10
4aa6d7
+                    });
4aa6d7
+                } else {
4aa6d7
+                    supportedProtocols = Arrays.asList(
4aa6d7
+                        ProtocolVersion.TLS13,
4aa6d7
+                        ProtocolVersion.TLS12,
4aa6d7
+                        ProtocolVersion.TLS11,
4aa6d7
+                        ProtocolVersion.TLS10
4aa6d7
+                    );
4aa6d7
+
4aa6d7
+                    serverDefaultProtocols = getAvailableProtocols(
4aa6d7
+                            new ProtocolVersion[] {
4aa6d7
+                        ProtocolVersion.TLS13,
4aa6d7
+                        ProtocolVersion.TLS12,
4aa6d7
+                        ProtocolVersion.TLS11,
4aa6d7
+                        ProtocolVersion.TLS10
4aa6d7
+                    });
4aa6d7
+                }
4aa6d7
             } else {
4aa6d7
                 supportedProtocols = Arrays.asList(
4aa6d7
                     ProtocolVersion.TLS13,
4aa6d7
@@ -612,6 +631,16 @@ public abstract class SSLContextImpl extends SSLContextSpi {
4aa6d7
 
4aa6d7
         static ProtocolVersion[] getSupportedProtocols() {
4aa6d7
             if (SunJSSE.isFIPS()) {
4aa6d7
+                if (SharedSecrets.getJavaSecuritySystemConfiguratorAccess()
4aa6d7
+                        .isSystemFipsEnabled()) {
4aa6d7
+                    // RH1860986: TLSv1.3 key derivation not supported with
4aa6d7
+                    // the Security Providers available in system FIPS mode.
4aa6d7
+                    return new ProtocolVersion[] {
4aa6d7
+                            ProtocolVersion.TLS12,
4aa6d7
+                            ProtocolVersion.TLS11,
4aa6d7
+                            ProtocolVersion.TLS10
4aa6d7
+                    };
4aa6d7
+                }
4aa6d7
                 return new ProtocolVersion[] {
4aa6d7
                         ProtocolVersion.TLS13,
4aa6d7
                         ProtocolVersion.TLS12,
4aa6d7
@@ -939,6 +968,16 @@ public abstract class SSLContextImpl extends SSLContextSpi {
4aa6d7
 
4aa6d7
         static ProtocolVersion[] getProtocols() {
4aa6d7
             if (SunJSSE.isFIPS()) {
4aa6d7
+                if (SharedSecrets.getJavaSecuritySystemConfiguratorAccess()
4aa6d7
+                        .isSystemFipsEnabled()) {
4aa6d7
+                    // RH1860986: TLSv1.3 key derivation not supported with
4aa6d7
+                    // the Security Providers available in system FIPS mode.
4aa6d7
+                    return new ProtocolVersion[] {
4aa6d7
+                            ProtocolVersion.TLS12,
4aa6d7
+                            ProtocolVersion.TLS11,
4aa6d7
+                            ProtocolVersion.TLS10
4aa6d7
+                    };
4aa6d7
+                }
4aa6d7
                 return new ProtocolVersion[]{
927f13
                         ProtocolVersion.TLS13,
4aa6d7
                         ProtocolVersion.TLS12,
4aa6d7
diff --git a/jdk/src/share/classes/sun/security/ssl/SunJSSE.java b/jdk/src/share/classes/sun/security/ssl/SunJSSE.java
4aa6d7
index 2845dc37938..52337a7b6cf 100644
4aa6d7
--- a/jdk/src/share/classes/sun/security/ssl/SunJSSE.java
4aa6d7
+++ b/jdk/src/share/classes/sun/security/ssl/SunJSSE.java
4aa6d7
@@ -30,6 +30,8 @@ import static sun.security.util.SecurityConstants.PROVIDER_VER;
4aa6d7
 
4aa6d7
 import java.security.*;
4aa6d7
 
4aa6d7
+import sun.misc.SharedSecrets;
4aa6d7
+
4aa6d7
 /**
4aa6d7
  * The JSSE provider.
4aa6d7
  *
4aa6d7
@@ -215,8 +217,13 @@ public abstract class SunJSSE extends java.security.Provider {
4aa6d7
             "sun.security.ssl.SSLContextImpl$TLS11Context");
4aa6d7
         put("SSLContext.TLSv1.2",
4aa6d7
             "sun.security.ssl.SSLContextImpl$TLS12Context");
4aa6d7
-        put("SSLContext.TLSv1.3",
4aa6d7
-            "sun.security.ssl.SSLContextImpl$TLS13Context");
4aa6d7
+        if (!SharedSecrets.getJavaSecuritySystemConfiguratorAccess()
4aa6d7
+                .isSystemFipsEnabled()) {
4aa6d7
+            // RH1860986: TLSv1.3 key derivation not supported with
4aa6d7
+            // the Security Providers available in system FIPS mode.
4aa6d7
+            put("SSLContext.TLSv1.3",
4aa6d7
+                "sun.security.ssl.SSLContextImpl$TLS13Context");
4aa6d7
+        }
4aa6d7
         put("SSLContext.TLS",
4aa6d7
             "sun.security.ssl.SSLContextImpl$TLSContext");
4aa6d7
         if (isfips == false) {
4aa6d7
diff --git a/jdk/src/share/lib/security/java.security-aix b/jdk/src/share/lib/security/java.security-aix
927f13
index 7a93d4e6b59..681a24b905d 100644
4aa6d7
--- a/jdk/src/share/lib/security/java.security-aix
4aa6d7
+++ b/jdk/src/share/lib/security/java.security-aix
4aa6d7
@@ -287,6 +287,13 @@ package.definition=sun.,\
4aa6d7
 #
4aa6d7
 security.overridePropertiesFile=true
4aa6d7
 
4aa6d7
+#
4aa6d7
+# Determines whether this properties file will be appended to
4aa6d7
+# using the system properties file stored at
4aa6d7
+# /etc/crypto-policies/back-ends/java.config
4aa6d7
+#
4aa6d7
+security.useSystemPropertiesFile=false
4aa6d7
+
4aa6d7
 #
4aa6d7
 # Determines the default key and trust manager factory algorithms for
4aa6d7
 # the javax.net.ssl package.
4aa6d7
diff --git a/jdk/src/share/lib/security/java.security-linux b/jdk/src/share/lib/security/java.security-linux
927f13
index 145a84f94cf..789c19a8cba 100644
4aa6d7
--- a/jdk/src/share/lib/security/java.security-linux
4aa6d7
+++ b/jdk/src/share/lib/security/java.security-linux
4aa6d7
@@ -75,6 +75,14 @@ security.provider.7=com.sun.security.sasl.Provider
4aa6d7
 security.provider.8=org.jcp.xml.dsig.internal.dom.XMLDSigRI
4aa6d7
 security.provider.9=sun.security.smartcardio.SunPCSC
4aa6d7
 
4aa6d7
+#
4aa6d7
+# Security providers used when FIPS mode support is active
4aa6d7
+#
4aa6d7
+fips.provider.1=sun.security.pkcs11.SunPKCS11 ${java.home}/lib/security/nss.fips.cfg
4aa6d7
+fips.provider.2=sun.security.provider.Sun
4aa6d7
+fips.provider.3=sun.security.ec.SunEC
4aa6d7
+fips.provider.4=com.sun.net.ssl.internal.ssl.Provider SunPKCS11-NSS-FIPS
4aa6d7
+
4aa6d7
 #
4aa6d7
 # Sun Provider SecureRandom seed source.
4aa6d7
 #
4aa6d7
@@ -170,6 +178,11 @@ policy.ignoreIdentityScope=false
4aa6d7
 #
4aa6d7
 keystore.type=jks
4aa6d7
 
4aa6d7
+#
4aa6d7
+# Default keystore type used when global crypto-policies are set to FIPS.
4aa6d7
+#
4aa6d7
+fips.keystore.type=PKCS11
4aa6d7
+
4aa6d7
 #
4aa6d7
 # Controls compatibility mode for the JKS keystore type.
4aa6d7
 #
4aa6d7
@@ -287,6 +300,13 @@ package.definition=sun.,\
4aa6d7
 #
4aa6d7
 security.overridePropertiesFile=true
4aa6d7
 
4aa6d7
+#
4aa6d7
+# Determines whether this properties file will be appended to
4aa6d7
+# using the system properties file stored at
4aa6d7
+# /etc/crypto-policies/back-ends/java.config
4aa6d7
+#
4aa6d7
+security.useSystemPropertiesFile=false
4aa6d7
+
4aa6d7
 #
4aa6d7
 # Determines the default key and trust manager factory algorithms for
4aa6d7
 # the javax.net.ssl package.
4aa6d7
diff --git a/jdk/src/share/lib/security/java.security-macosx b/jdk/src/share/lib/security/java.security-macosx
927f13
index 35fa140d7a5..d4da666af3b 100644
4aa6d7
--- a/jdk/src/share/lib/security/java.security-macosx
4aa6d7
+++ b/jdk/src/share/lib/security/java.security-macosx
4aa6d7
@@ -290,6 +290,13 @@ package.definition=sun.,\
4aa6d7
 #
4aa6d7
 security.overridePropertiesFile=true
4aa6d7
 
4aa6d7
+#
4aa6d7
+# Determines whether this properties file will be appended to
4aa6d7
+# using the system properties file stored at
4aa6d7
+# /etc/crypto-policies/back-ends/java.config
4aa6d7
+#
4aa6d7
+security.useSystemPropertiesFile=false
4aa6d7
+
4aa6d7
 #
4aa6d7
 # Determines the default key and trust manager factory algorithms for
4aa6d7
 # the javax.net.ssl package.
4aa6d7
diff --git a/jdk/src/share/lib/security/java.security-solaris b/jdk/src/share/lib/security/java.security-solaris
927f13
index f79ba37ddb9..300132384a1 100644
4aa6d7
--- a/jdk/src/share/lib/security/java.security-solaris
4aa6d7
+++ b/jdk/src/share/lib/security/java.security-solaris
4aa6d7
@@ -288,6 +288,13 @@ package.definition=sun.,\
4aa6d7
 #
4aa6d7
 security.overridePropertiesFile=true
4aa6d7
 
4aa6d7
+#
4aa6d7
+# Determines whether this properties file will be appended to
4aa6d7
+# using the system properties file stored at
4aa6d7
+# /etc/crypto-policies/back-ends/java.config
4aa6d7
+#
4aa6d7
+security.useSystemPropertiesFile=false
4aa6d7
+
4aa6d7
 #
4aa6d7
 # Determines the default key and trust manager factory algorithms for
4aa6d7
 # the javax.net.ssl package.
4aa6d7
diff --git a/jdk/src/share/lib/security/java.security-windows b/jdk/src/share/lib/security/java.security-windows
927f13
index d70503ce95f..64db5a5cd1e 100644
4aa6d7
--- a/jdk/src/share/lib/security/java.security-windows
4aa6d7
+++ b/jdk/src/share/lib/security/java.security-windows
4aa6d7
@@ -290,6 +290,13 @@ package.definition=sun.,\
4aa6d7
 #
4aa6d7
 security.overridePropertiesFile=true
4aa6d7
 
4aa6d7
+#
4aa6d7
+# Determines whether this properties file will be appended to
4aa6d7
+# using the system properties file stored at
4aa6d7
+# /etc/crypto-policies/back-ends/java.config
4aa6d7
+#
4aa6d7
+security.useSystemPropertiesFile=false
4aa6d7
+
4aa6d7
 #
4aa6d7
 # Determines the default key and trust manager factory algorithms for
4aa6d7
 # the javax.net.ssl package.
4aa6d7
diff --git a/jdk/src/solaris/native/java/security/systemconf.c b/jdk/src/solaris/native/java/security/systemconf.c
4aa6d7
new file mode 100644
4aa6d7
index 00000000000..8dcb7d9073f
4aa6d7
--- /dev/null
4aa6d7
+++ b/jdk/src/solaris/native/java/security/systemconf.c
4aa6d7
@@ -0,0 +1,224 @@
4aa6d7
+/*
4aa6d7
+ * Copyright (c) 2021, Red Hat, Inc.
4aa6d7
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4aa6d7
+ *
4aa6d7
+ * This code is free software; you can redistribute it and/or modify it
4aa6d7
+ * under the terms of the GNU General Public License version 2 only, as
4aa6d7
+ * published by the Free Software Foundation.  Oracle designates this
4aa6d7
+ * particular file as subject to the "Classpath" exception as provided
4aa6d7
+ * by Oracle in the LICENSE file that accompanied this code.
4aa6d7
+ *
4aa6d7
+ * This code is distributed in the hope that it will be useful, but WITHOUT
4aa6d7
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4aa6d7
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4aa6d7
+ * version 2 for more details (a copy is included in the LICENSE file that
4aa6d7
+ * accompanied this code).
4aa6d7
+ *
4aa6d7
+ * You should have received a copy of the GNU General Public License version
4aa6d7
+ * 2 along with this work; if not, write to the Free Software Foundation,
4aa6d7
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4aa6d7
+ *
4aa6d7
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4aa6d7
+ * or visit www.oracle.com if you need additional information or have any
4aa6d7
+ * questions.
4aa6d7
+ */
4aa6d7
+
4aa6d7
+#include <jni.h>
4aa6d7
+#include <jni_util.h>
4aa6d7
+#include "jvm_md.h"
4aa6d7
+#include <stdio.h>
4aa6d7
+
4aa6d7
+#ifdef SYSCONF_NSS
4aa6d7
+#include <nss3/pk11pub.h>
4aa6d7
+#else
4aa6d7
+#include <dlfcn.h>
4aa6d7
+#endif //SYSCONF_NSS
4aa6d7
+
4aa6d7
+#include "java_security_SystemConfigurator.h"
4aa6d7
+
4aa6d7
+#define MSG_MAX_SIZE 256
4aa6d7
+#define FIPS_ENABLED_PATH "/proc/sys/crypto/fips_enabled"
4aa6d7
+
4aa6d7
+typedef int (SECMOD_GET_SYSTEM_FIPS_ENABLED_TYPE)(void);
4aa6d7
+
4aa6d7
+static SECMOD_GET_SYSTEM_FIPS_ENABLED_TYPE *getSystemFIPSEnabled;
4aa6d7
+static jmethodID debugPrintlnMethodID = NULL;
4aa6d7
+static jobject debugObj = NULL;
4aa6d7
+
4aa6d7
+static void dbgPrint(JNIEnv *env, const char* msg)
4aa6d7
+{
4aa6d7
+    jstring jMsg;
4aa6d7
+    if (debugObj != NULL) {
4aa6d7
+        jMsg = (*env)->NewStringUTF(env, msg);
4aa6d7
+        CHECK_NULL(jMsg);
4aa6d7
+        (*env)->CallVoidMethod(env, debugObj, debugPrintlnMethodID, jMsg);
4aa6d7
+    }
4aa6d7
+}
4aa6d7
+
4aa6d7
+static void throwIOException(JNIEnv *env, const char *msg)
4aa6d7
+{
4aa6d7
+    jclass cls = (*env)->FindClass(env, "java/io/IOException");
4aa6d7
+    if (cls != 0)
4aa6d7
+        (*env)->ThrowNew(env, cls, msg);
4aa6d7
+}
4aa6d7
+
4aa6d7
+static void handle_msg(JNIEnv *env, const char* msg, int msg_bytes)
4aa6d7
+{
4aa6d7
+  if (msg_bytes > 0 && msg_bytes < MSG_MAX_SIZE) {
4aa6d7
+    dbgPrint(env, msg);
4aa6d7
+  } else {
4aa6d7
+    dbgPrint(env, "systemconf: cannot render message");
4aa6d7
+  }
4aa6d7
+}
4aa6d7
+
4aa6d7
+// Only used when NSS is not linked at build time
4aa6d7
+#ifndef SYSCONF_NSS
4aa6d7
+
4aa6d7
+static void *nss_handle;
4aa6d7
+
4aa6d7
+static jboolean loadNSS(JNIEnv *env)
4aa6d7
+{
4aa6d7
+  char msg[MSG_MAX_SIZE];
4aa6d7
+  int msg_bytes;
4aa6d7
+  const char* errmsg;
4aa6d7
+
4aa6d7
+  nss_handle = dlopen(JNI_LIB_NAME("nss3"), RTLD_LAZY);
4aa6d7
+  if (nss_handle == NULL) {
4aa6d7
+    errmsg = dlerror();
4aa6d7
+    msg_bytes = snprintf(msg, MSG_MAX_SIZE, "loadNSS: dlopen: %s\n",
4aa6d7
+                         errmsg);
4aa6d7
+    handle_msg(env, msg, msg_bytes);
4aa6d7
+    return JNI_FALSE;
4aa6d7
+  }
4aa6d7
+  dlerror(); /* Clear errors */
4aa6d7
+  getSystemFIPSEnabled = (SECMOD_GET_SYSTEM_FIPS_ENABLED_TYPE*)dlsym(nss_handle, "SECMOD_GetSystemFIPSEnabled");
4aa6d7
+  if ((errmsg = dlerror()) != NULL) {
4aa6d7
+    msg_bytes = snprintf(msg, MSG_MAX_SIZE, "loadNSS: dlsym: %s\n",
4aa6d7
+                         errmsg);
4aa6d7
+    handle_msg(env, msg, msg_bytes);
4aa6d7
+    return JNI_FALSE;
4aa6d7
+  }
4aa6d7
+  return JNI_TRUE;
4aa6d7
+}
4aa6d7
+
4aa6d7
+static void closeNSS(JNIEnv *env)
4aa6d7
+{
4aa6d7
+  char msg[MSG_MAX_SIZE];
4aa6d7
+  int msg_bytes;
4aa6d7
+  const char* errmsg;
4aa6d7
+
4aa6d7
+  if (dlclose(nss_handle) != 0) {
4aa6d7
+    errmsg = dlerror();
4aa6d7
+    msg_bytes = snprintf(msg, MSG_MAX_SIZE, "closeNSS: dlclose: %s\n",
4aa6d7
+                         errmsg);
4aa6d7
+    handle_msg(env, msg, msg_bytes);
4aa6d7
+  }
4aa6d7
+}
4aa6d7
+
4aa6d7
+#endif
4aa6d7
+
4aa6d7
+/*
4aa6d7
+ * Class:     java_security_SystemConfigurator
4aa6d7
+ * Method:    JNI_OnLoad
4aa6d7
+ */
4aa6d7
+JNIEXPORT jint JNICALL DEF_JNI_OnLoad(JavaVM *vm, void *reserved)
4aa6d7
+{
4aa6d7
+    JNIEnv *env;
4aa6d7
+    jclass sysConfCls, debugCls;
4aa6d7
+    jfieldID sdebugFld;
4aa6d7
+
4aa6d7
+    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_2) != JNI_OK) {
4aa6d7
+        return JNI_EVERSION; /* JNI version not supported */
4aa6d7
+    }
4aa6d7
+
4aa6d7
+    sysConfCls = (*env)->FindClass(env,"java/security/SystemConfigurator");
4aa6d7
+    if (sysConfCls == NULL) {
4aa6d7
+        printf("libsystemconf: SystemConfigurator class not found\n");
4aa6d7
+        return JNI_ERR;
4aa6d7
+    }
4aa6d7
+    sdebugFld = (*env)->GetStaticFieldID(env, sysConfCls,
4aa6d7
+            "sdebug", "Lsun/security/util/Debug;");
4aa6d7
+    if (sdebugFld == NULL) {
4aa6d7
+        printf("libsystemconf: SystemConfigurator::sdebug field not found\n");
4aa6d7
+        return JNI_ERR;
4aa6d7
+    }
4aa6d7
+    debugObj = (*env)->GetStaticObjectField(env, sysConfCls, sdebugFld);
4aa6d7
+    if (debugObj != NULL) {
4aa6d7
+        debugCls = (*env)->FindClass(env,"sun/security/util/Debug");
4aa6d7
+        if (debugCls == NULL) {
4aa6d7
+            printf("libsystemconf: Debug class not found\n");
4aa6d7
+            return JNI_ERR;
4aa6d7
+        }
4aa6d7
+        debugPrintlnMethodID = (*env)->GetMethodID(env, debugCls,
4aa6d7
+                "println", "(Ljava/lang/String;)V");
4aa6d7
+        if (debugPrintlnMethodID == NULL) {
4aa6d7
+            printf("libsystemconf: Debug::println(String) method not found\n");
4aa6d7
+            return JNI_ERR;
4aa6d7
+        }
4aa6d7
+        debugObj = (*env)->NewGlobalRef(env, debugObj);
4aa6d7
+    }
4aa6d7
+
4aa6d7
+#ifdef SYSCONF_NSS
4aa6d7
+    getSystemFIPSEnabled = *SECMOD_GetSystemFIPSEnabled;
4aa6d7
+#else
4aa6d7
+    if (loadNSS(env) == JNI_FALSE) {
4aa6d7
+      dbgPrint(env, "libsystemconf: Failed to load NSS library.");
4aa6d7
+    }
4aa6d7
+#endif
4aa6d7
+
4aa6d7
+    return (*env)->GetVersion(env);
4aa6d7
+}
4aa6d7
+
4aa6d7
+/*
4aa6d7
+ * Class:     java_security_SystemConfigurator
4aa6d7
+ * Method:    JNI_OnUnload
4aa6d7
+ */
4aa6d7
+JNIEXPORT void JNICALL DEF_JNI_OnUnload(JavaVM *vm, void *reserved)
4aa6d7
+{
4aa6d7
+    JNIEnv *env;
4aa6d7
+
4aa6d7
+    if (debugObj != NULL) {
4aa6d7
+        if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_2) != JNI_OK) {
4aa6d7
+            return; /* Should not happen */
4aa6d7
+        }
4aa6d7
+#ifndef SYSCONF_NSS
4aa6d7
+        closeNSS(env);
4aa6d7
+#endif
4aa6d7
+        (*env)->DeleteGlobalRef(env, debugObj);
4aa6d7
+    }
4aa6d7
+}
4aa6d7
+
4aa6d7
+JNIEXPORT jboolean JNICALL Java_java_security_SystemConfigurator_getSystemFIPSEnabled
4aa6d7
+  (JNIEnv *env, jclass cls)
4aa6d7
+{
4aa6d7
+    int fips_enabled;
4aa6d7
+    char msg[MSG_MAX_SIZE];
4aa6d7
+    int msg_bytes;
4aa6d7
+
4aa6d7
+    if (getSystemFIPSEnabled != NULL) {
4aa6d7
+      dbgPrint(env, "getSystemFIPSEnabled: calling SECMOD_GetSystemFIPSEnabled");
4aa6d7
+      fips_enabled = (*getSystemFIPSEnabled)();
4aa6d7
+      msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:"   \
4aa6d7
+                           " SECMOD_GetSystemFIPSEnabled returned 0x%x", fips_enabled);
4aa6d7
+      handle_msg(env, msg, msg_bytes);
4aa6d7
+      return (fips_enabled == 1 ? JNI_TRUE : JNI_FALSE);
4aa6d7
+    } else {
4aa6d7
+      FILE *fe;
4aa6d7
+
4aa6d7
+      dbgPrint(env, "getSystemFIPSEnabled: reading " FIPS_ENABLED_PATH);
4aa6d7
+      if ((fe = fopen(FIPS_ENABLED_PATH, "r")) == NULL) {
4aa6d7
+        throwIOException(env, "Cannot open " FIPS_ENABLED_PATH);
4aa6d7
+        return JNI_FALSE;
4aa6d7
+      }
4aa6d7
+      fips_enabled = fgetc(fe);
4aa6d7
+      fclose(fe);
4aa6d7
+      if (fips_enabled == EOF) {
4aa6d7
+        throwIOException(env, "Cannot read " FIPS_ENABLED_PATH);
4aa6d7
+        return JNI_FALSE;
4aa6d7
+      }
4aa6d7
+      msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:"   \
4aa6d7
+                           " read character is '%c'", fips_enabled);
4aa6d7
+      handle_msg(env, msg, msg_bytes);
4aa6d7
+      return (fips_enabled == '1' ? JNI_TRUE : JNI_FALSE);
4aa6d7
+    }
4aa6d7
+}