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

Additional default values of security properties are read from a

adf9a5
+ * system-specific location, if available.

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