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