Skip to content

Commit

Permalink
8328556: Do not extract large CKO_SECRET_KEY keys from the NSS Softwa…
Browse files Browse the repository at this point in the history
…re Token

Reviewed-by: djelinski
  • Loading branch information
martinuy committed Mar 22, 2024
1 parent 709410d commit 13cf070
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_keymgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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++;
}
Expand Down Expand Up @@ -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) {
Expand Down
72 changes: 72 additions & 0 deletions test/jdk/sun/security/pkcs11/Mac/TestLargeSecretKeys.java
Original file line number Diff line number Diff line change
@@ -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());
}
}

1 comment on commit 13cf070

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.