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