Blame SOURCES/rh1996182-login_to_nss_software_token.patch

2578b9
commit 53bda6adfacc02b8dddd8f10350c9569bca4eb1e
2578b9
Author: Martin Balao <mbalao@redhat.com>
2578b9
Date:   Fri Aug 27 19:42:07 2021 +0100
2578b9
2578b9
    RH1996182: Login to the NSS Software Token in FIPS Mode
2578b9
2578b9
diff --git openjdk.orig/src/java.base/share/classes/module-info.java openjdk/src/java.base/share/classes/module-info.java
2578b9
index 5460efcf8c..f08dc2fafc 100644
2578b9
--- openjdk.orig/src/java.base/share/classes/module-info.java
2578b9
+++ openjdk/src/java.base/share/classes/module-info.java
2578b9
@@ -182,6 +182,7 @@ module java.base {
2578b9
         java.security.jgss,
2578b9
         java.sql,
2578b9
         java.xml,
2578b9
+        jdk.crypto.cryptoki,
2578b9
         jdk.jartool,
2578b9
         jdk.attach,
2578b9
         jdk.charsets,
2578b9
diff --git openjdk.orig/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/SunPKCS11.java openjdk/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/SunPKCS11.java
2578b9
index 5e227f4531..164de8ff08 100644
2578b9
--- openjdk.orig/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/SunPKCS11.java
2578b9
+++ openjdk/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/SunPKCS11.java
2578b9
@@ -41,6 +41,8 @@ import javax.security.auth.callback.CallbackHandler;
2578b9
 import javax.security.auth.callback.PasswordCallback;
2578b9
 
2578b9
 import jdk.internal.misc.InnocuousThread;
2578b9
+import jdk.internal.misc.SharedSecrets;
2578b9
+
2578b9
 import sun.security.util.Debug;
2578b9
 import sun.security.util.ResourcesMgr;
2578b9
 import static sun.security.util.SecurityConstants.PROVIDER_VER;
2578b9
@@ -58,6 +60,9 @@ import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
2578b9
  */
2578b9
 public final class SunPKCS11 extends AuthProvider {
2578b9
 
2578b9
+    private static final boolean systemFipsEnabled = SharedSecrets
2578b9
+            .getJavaSecuritySystemConfiguratorAccess().isSystemFipsEnabled();
2578b9
+
2578b9
     private static final long serialVersionUID = -1354835039035306505L;
2578b9
 
2578b9
     static final Debug debug = Debug.getInstance("sunpkcs11");
2578b9
@@ -374,6 +379,24 @@ public final class SunPKCS11 extends AuthProvider {
2578b9
             if (nssModule != null) {
2578b9
                 nssModule.setProvider(this);
2578b9
             }
2578b9
+            if (systemFipsEnabled) {
2578b9
+                // The NSS Software Token in FIPS 140-2 mode requires a user
2578b9
+                // login for most operations. See sftk_fipsCheck. The NSS DB
2578b9
+                // (/etc/pki/nssdb) PIN is empty.
2578b9
+                Session session = null;
2578b9
+                try {
2578b9
+                    session = token.getOpSession();
2578b9
+                    p11.C_Login(session.id(), CKU_USER, new char[] {});
2578b9
+                } catch (PKCS11Exception p11e) {
2578b9
+                    if (debug != null) {
2578b9
+                        debug.println("Error during token login: " +
2578b9
+                                p11e.getMessage());
2578b9
+                    }
2578b9
+                    throw p11e;
2578b9
+                } finally {
2578b9
+                    token.releaseSession(session);
2578b9
+                }
2578b9
+            }
2578b9
         } catch (Exception e) {
2578b9
             if (config.getHandleStartupErrors() == Config.ERR_IGNORE_ALL) {
2578b9
                 throw new UnsupportedOperationException