From 13cf0707f903609c9bda99a9bf7511f494f9feae Mon Sep 17 00:00:00 2001 From: Martin Balao Date: Fri, 22 Mar 2024 15:28:05 +0000 Subject: [PATCH] 8328556: Do not extract large CKO_SECRET_KEY keys from the NSS Software Token Reviewed-by: djelinski --- .../share/native/libj2pkcs11/p11_keymgmt.c | 11 +++ .../pkcs11/Mac/TestLargeSecretKeys.java | 72 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 test/jdk/sun/security/pkcs11/Mac/TestLargeSecretKeys.java diff --git a/src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_keymgmt.c b/src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_keymgmt.c index 08257d05b1b..928cc3d6ed8 100644 --- a/src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_keymgmt.c +++ b/src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_keymgmt.c @@ -148,6 +148,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo jbyte* nativeKeyInfoArrayRaw = NULL; jbyte* nativeKeyInfoWrappedKeyArrayRaw = NULL; unsigned int sensitiveAttributePosition = (unsigned int)-1; + unsigned int valueLenAttributePosition = (unsigned int)-1; unsigned int i = 0U; unsigned long totalDataSize = 0UL, attributesCount = 0UL; unsigned long totalCkAttributesSize = 0UL, totalNativeKeyInfoArraySize = 0UL; @@ -217,6 +218,8 @@ Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo if ((ckpAttributes+i)->type == CKA_SENSITIVE) { sensitiveAttributePosition = attributesCount; TRACE0("DEBUG: GetNativeKeyInfo key is sensitive"); + } else if ((ckpAttributes+i)->type == CKA_VALUE_LEN) { + valueLenAttributePosition = attributesCount; } attributesCount++; } @@ -296,6 +299,14 @@ Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo goto cleanup; } + if (class == CKO_SECRET_KEY && (valueLenAttributePosition != (unsigned int)-1) && + *(CK_ULONG*)(((CK_ATTRIBUTE_PTR)(((CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributes) + +valueLenAttributePosition))->pValue) > 256UL) { + // NSS' NSC_UnwrapKey does not accept CKO_SECRET_KEY keys with length greater + // than 256 (MAX_KEY_LEN - pkcs11i.h). Handle these keys as non-extractable. + goto cleanup; + } + TRACE0("DEBUG: GetNativeKeyInfo 1st C_GetAttributeValue call passed\n"); if (netscapeAttributeValueNeeded) { diff --git a/test/jdk/sun/security/pkcs11/Mac/TestLargeSecretKeys.java b/test/jdk/sun/security/pkcs11/Mac/TestLargeSecretKeys.java new file mode 100644 index 00000000000..dc263cfdb2b --- /dev/null +++ b/test/jdk/sun/security/pkcs11/Mac/TestLargeSecretKeys.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2024, Red Hat, Inc. + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import javax.crypto.*; +import java.security.Key; +import java.security.KeyPairGenerator; +import java.security.Provider; + +/* + * @test + * @bug 8328556 + * @library /test/lib .. + * @run main/othervm/timeout=30 -DCUSTOM_P11_CONFIG_NAME=p11-nss-sensitive.txt TestLargeSecretKeys + */ + +public final class TestLargeSecretKeys extends PKCS11Test { + + public void main(Provider p) throws Exception { + Key k = generateLargeSecretKey(p); + Mac m = Mac.getInstance("HmacSHA512", p); + m.init(k); + m.doFinal(new byte[10]); + // Before the fix for 8328556, the next call would require to re-build + // the key in the NSS Software Token by means of a call to + // C_UnwrapKey because the key's CKA_SENSITIVE attribute is CK_TRUE. + // This call would fail with a CKR_TEMPLATE_INCONSISTENT error due to + // secret key length checks in NSS: length of 384 bytes is greater + // than 256 (defined as MAX_KEY_LEN in pkcs11i.h). With 8328556, the + // key was never extracted after its first use so re-building for the + // second use is not necessary. + m.init(k); + m.doFinal(new byte[10]); + } + + private static Key generateLargeSecretKey(Provider p) throws Exception { + KeyPairGenerator kpg1 = KeyPairGenerator.getInstance("DH", p); + KeyPairGenerator kpg2 = KeyPairGenerator.getInstance("DH", p); + kpg1.initialize(3072); + kpg2.initialize(3072); + Key privK = kpg1.generateKeyPair().getPrivate(); + Key pubK = kpg2.generateKeyPair().getPublic(); + KeyAgreement ka = KeyAgreement.getInstance("DH", p); + ka.init(privK); + ka.doPhase(pubK, true); + return ka.generateSecret("TlsPremasterSecret"); + } + + public static void main(String[] args) throws Exception { + main(new TestLargeSecretKeys()); + } +}