Skip to content

Commit 381e90e

Browse files
author
Valerie Peng
committedOct 8, 2019
8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4
For CK_GCM_PARAMS, try the spec definition first before falling back to the header file definition Reviewed-by: xuelei
1 parent f3df804 commit 381e90e

File tree

12 files changed

+211
-62
lines changed

12 files changed

+211
-62
lines changed
 

‎src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11AEADCipher.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,6 @@ private void initialize() throws PKCS11Exception {
378378

379379
long p11KeyID = p11Key.getKeyID();
380380
try {
381-
if (session == null) {
382-
session = token.getOpSession();
383-
}
384381
CK_MECHANISM mechWithParams;
385382
switch (blockMode) {
386383
case MODE_GCM:
@@ -390,6 +387,9 @@ private void initialize() throws PKCS11Exception {
390387
default:
391388
throw new ProviderException("Unsupported mode: " + blockMode);
392389
}
390+
if (session == null) {
391+
session = token.getOpSession();
392+
}
393393
if (encrypt) {
394394
token.p11.C_EncryptInit(session.id(), mechWithParams,
395395
p11KeyID);
@@ -398,7 +398,6 @@ private void initialize() throws PKCS11Exception {
398398
p11KeyID);
399399
}
400400
} catch (PKCS11Exception e) {
401-
//e.printStackTrace();
402401
p11Key.releaseKeyID();
403402
session = token.releaseSession(session);
404403
throw e;
@@ -718,7 +717,9 @@ private void handleException(PKCS11Exception e)
718717
errorCode == CKR_ENCRYPTED_DATA_LEN_RANGE) {
719718
throw (IllegalBlockSizeException)
720719
(new IllegalBlockSizeException(e.toString()).initCause(e));
721-
} else if (errorCode == CKR_ENCRYPTED_DATA_INVALID) {
720+
} else if (errorCode == CKR_ENCRYPTED_DATA_INVALID ||
721+
// Solaris-specific
722+
errorCode == CKR_GENERAL_ERROR) {
722723
throw (BadPaddingException)
723724
(new BadPaddingException(e.toString()).initCause(e));
724725
}

‎src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Digest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -103,9 +103,11 @@ final class P11Digest extends MessageDigestSpi implements Cloneable,
103103
digestLength = 20;
104104
break;
105105
case (int)CKM_SHA224:
106+
case (int)CKM_SHA512_224:
106107
digestLength = 28;
107108
break;
108109
case (int)CKM_SHA256:
110+
case (int)CKM_SHA512_256:
109111
digestLength = 32;
110112
break;
111113
case (int)CKM_SHA384:

‎src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Mac.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -91,9 +91,11 @@ final class P11Mac extends MacSpi {
9191
macLength = 20;
9292
break;
9393
case (int)CKM_SHA224_HMAC:
94+
case (int)CKM_SHA512_224_HMAC:
9495
macLength = 28;
9596
break;
9697
case (int)CKM_SHA256_HMAC:
98+
case (int)CKM_SHA512_256_HMAC:
9799
macLength = 32;
98100
break;
99101
case (int)CKM_SHA384_HMAC:

‎src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_convert.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ jTlsMacParamsToCKTlsMacParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
721721
}
722722

723723
// populate using java values
724-
ckParamPtr->prfMechanism = jLongToCKULong(jPrfMechanism);
724+
ckParamPtr->prfHashMechanism = jLongToCKULong(jPrfMechanism);
725725
ckParamPtr->ulMacLength = jLongToCKULong(jUlMacLength);
726726
ckParamPtr->ulServerOrClient = jLongToCKULong(jUlServerOrClient);
727727

@@ -1014,17 +1014,18 @@ jAesCtrParamsToCKAesCtrParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
10141014
}
10151015

10161016
/*
1017-
* converts the Java CK_GCM_PARAMS object to a CK_GCM_PARAMS pointer
1017+
* converts the Java CK_GCM_PARAMS object to a CK_GCM_PARAMS_NO_IVBITS pointer
1018+
* Note: Need to try NSS definition first to avoid SIGSEGV.
10181019
*
10191020
* @param env - used to call JNI funktions to get the Java classes and objects
10201021
* @param jParam - the Java CK_GCM_PARAMS object to convert
10211022
* @param pLength - length of the allocated memory of the returned pointer
1022-
* @return pointer to the new CK_GCM_PARAMS structure
1023+
* @return pointer to the new CK_GCM_PARAMS_NO_IVBITS structure
10231024
*/
1024-
CK_GCM_PARAMS_PTR
1025+
CK_GCM_PARAMS_NO_IVBITS_PTR
10251026
jGCMParamsToCKGCMParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
10261027
{
1027-
CK_GCM_PARAMS_PTR ckParamPtr;
1028+
CK_GCM_PARAMS_NO_IVBITS_PTR ckParamPtr;
10281029
jclass jGcmParamsClass;
10291030
jfieldID fieldID;
10301031
jobject jIv, jAad;
@@ -1052,8 +1053,8 @@ jGCMParamsToCKGCMParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
10521053
if (fieldID == NULL) { return NULL; }
10531054
jTagLen = (*env)->GetLongField(env, jParam, fieldID);
10541055

1055-
// allocate memory for CK_GCM_PARAMS pointer
1056-
ckParamPtr = calloc(1, sizeof(CK_GCM_PARAMS));
1056+
// allocate memory for CK_GCM_PARAMS_NO_IVBITS pointer
1057+
ckParamPtr = calloc(1, sizeof(CK_GCM_PARAMS_NO_IVBITS));
10571058
if (ckParamPtr == NULL) {
10581059
throwOutOfMemoryError(env, 0);
10591060
return NULL;
@@ -1073,16 +1074,15 @@ jGCMParamsToCKGCMParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
10731074
ckParamPtr->ulTagBits = jLongToCKULong(jTagLen);
10741075

10751076
if (pLength != NULL) {
1076-
*pLength = sizeof(CK_GCM_PARAMS);
1077+
*pLength = sizeof(CK_GCM_PARAMS_NO_IVBITS);
10771078
}
1078-
TRACE1("Created inner GCM_PARAMS PTR %lX\n", ptr_to_jlong(ckParamPtr));
1079+
TRACE1("Created inner GCM_PARAMS PTR w/o ulIvBits %p\n", ckParamPtr);
10791080
return ckParamPtr;
10801081
cleanup:
10811082
free(ckParamPtr->pIv);
10821083
free(ckParamPtr->pAAD);
10831084
free(ckParamPtr);
10841085
return NULL;
1085-
10861086
}
10871087

10881088
/*
@@ -1179,7 +1179,7 @@ CK_MECHANISM_PTR jMechanismToCKMechanismPtr(JNIEnv *env, jobject jMech)
11791179
throwOutOfMemoryError(env, 0);
11801180
return NULL;
11811181
}
1182-
TRACE1("DEBUG jMechanismToCKMechanismPtr: allocated mech %p \n", ckpMech);
1182+
TRACE1("DEBUG jMechanismToCKMechanismPtr: allocated mech %p\n", ckpMech);
11831183

11841184
ckpMech->mechanism = jLongToCKULong(jMechType);
11851185

‎src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_crypt.c

+40-4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1EncryptInit
7272
{
7373
CK_SESSION_HANDLE ckSessionHandle;
7474
CK_MECHANISM_PTR ckpMechanism = NULL;
75+
CK_MECHANISM_PTR ckpTemp;
7576
CK_OBJECT_HANDLE ckKeyHandle;
7677
CK_RV rv;
7778

@@ -81,15 +82,32 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1EncryptInit
8182
ckSessionHandle = jLongToCKULong(jSessionHandle);
8283
ckKeyHandle = jLongToCKULong(jKeyHandle);
8384
ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);
85+
TRACE1("DEBUG C_EncryptInit: created pMech = %p\n",
86+
ckpMechanism);
87+
8488
if ((*env)->ExceptionCheck(env)) { return; }
8589

8690
rv = (*ckpFunctions->C_EncryptInit)(ckSessionHandle, ckpMechanism,
8791
ckKeyHandle);
8892

89-
// if OAEP, then cannot free here
90-
freeCKMechanismPtr(ckpMechanism);
93+
if (ckpMechanism->mechanism == CKM_AES_GCM) {
94+
if (rv == CKR_ARGUMENTS_BAD || rv == CKR_MECHANISM_PARAM_INVALID) {
95+
// retry with CKM_GCM_PARAMS structure in pkcs11t.h
96+
TRACE0("DEBUG C_EncryptInit: retry with CK_GCM_PARAMS\n");
97+
ckpTemp = updateGCMParams(env, ckpMechanism);
98+
if (ckpTemp != NULL) { // only re-call if conversion succeeds
99+
ckpMechanism = ckpTemp;
100+
rv = (*ckpFunctions->C_EncryptInit)(ckSessionHandle, ckpMechanism,
101+
ckKeyHandle);
102+
}
103+
}
104+
}
91105

106+
TRACE1("DEBUG C_EncryptInit: freed pMech = %p\n", ckpMechanism);
107+
freeCKMechanismPtr(ckpMechanism);
92108
if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
109+
110+
TRACE0("FINISHED\n");
93111
}
94112
#endif
95113

@@ -292,6 +310,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptInit
292310
{
293311
CK_SESSION_HANDLE ckSessionHandle;
294312
CK_MECHANISM_PTR ckpMechanism = NULL;
313+
CK_MECHANISM_PTR ckpTemp;
295314
CK_OBJECT_HANDLE ckKeyHandle;
296315
CK_RV rv;
297316

@@ -301,15 +320,32 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptInit
301320
ckSessionHandle = jLongToCKULong(jSessionHandle);
302321
ckKeyHandle = jLongToCKULong(jKeyHandle);
303322
ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);
323+
TRACE1("DEBUG C_DecryptInit: created pMech = %p\n",
324+
ckpMechanism);
325+
304326
if ((*env)->ExceptionCheck(env)) { return; }
305327

306328
rv = (*ckpFunctions->C_DecryptInit)(ckSessionHandle, ckpMechanism,
307329
ckKeyHandle);
308330

309-
// if OAEP, then cannot free here
310-
freeCKMechanismPtr(ckpMechanism);
331+
if (ckpMechanism->mechanism == CKM_AES_GCM) {
332+
if (rv == CKR_ARGUMENTS_BAD || rv == CKR_MECHANISM_PARAM_INVALID) {
333+
// retry with CKM_GCM_PARAMS structure in pkcs11t.h
334+
TRACE0("DEBUG C_DecryptInit: retry with CK_GCM_PARAMS\n");
335+
ckpTemp = updateGCMParams(env, ckpMechanism);
336+
if (ckpTemp != NULL) { // only re-call if conversion succeeds
337+
ckpMechanism = ckpTemp;
338+
rv = (*ckpFunctions->C_DecryptInit)(ckSessionHandle, ckpMechanism,
339+
ckKeyHandle);
340+
}
341+
}
342+
}
311343

344+
TRACE1("DEBUG C_DecryptInit: freed pMech = %p\n", ckpMechanism);
345+
freeCKMechanismPtr(ckpMechanism);
312346
if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
347+
348+
TRACE0("FINISHED\n");
313349
}
314350
#endif
315351

‎src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_util.c

+68-32
Original file line numberDiff line numberDiff line change
@@ -302,29 +302,30 @@ void freeCKMechanismPtr(CK_MECHANISM_PTR mechPtr) {
302302
CK_TLS12_KEY_MAT_PARAMS* tlsKmTmp;
303303

304304
if (mechPtr != NULL) {
305-
TRACE2("DEBUG: free mech %lX (mech id = 0x%lX)\n",
306-
ptr_to_jlong(mechPtr), mechPtr->mechanism);
305+
TRACE2("DEBUG freeCKMechanismPtr: free pMech %p (mech 0x%lX)\n",
306+
mechPtr, mechPtr->mechanism);
307307
if (mechPtr->pParameter != NULL) {
308+
tmp = mechPtr->pParameter;
308309
switch (mechPtr->mechanism) {
309310
case CKM_AES_GCM:
310-
tmp = mechPtr->pParameter;
311-
TRACE1("\t=> free GCM_PARAMS %lX\n",
312-
ptr_to_jlong(tmp));
313-
free(((CK_GCM_PARAMS*)tmp)->pIv);
314-
free(((CK_GCM_PARAMS*)tmp)->pAAD);
311+
if (mechPtr->ulParameterLen == sizeof(CK_GCM_PARAMS_NO_IVBITS)) {
312+
TRACE0("[ GCM_PARAMS w/o ulIvBits ]\n");
313+
free(((CK_GCM_PARAMS_NO_IVBITS*)tmp)->pIv);
314+
free(((CK_GCM_PARAMS_NO_IVBITS*)tmp)->pAAD);
315+
} else if (mechPtr->ulParameterLen == sizeof(CK_GCM_PARAMS)) {
316+
TRACE0("[ GCM_PARAMS ]\n");
317+
free(((CK_GCM_PARAMS*)tmp)->pIv);
318+
free(((CK_GCM_PARAMS*)tmp)->pAAD);
319+
}
315320
break;
316321
case CKM_AES_CCM:
317-
tmp = mechPtr->pParameter;
318-
TRACE1("\t=> free CK_CCM_PARAMS %lX\n",
319-
ptr_to_jlong(tmp));
322+
TRACE0("[ CK_CCM_PARAMS ]\n");
320323
free(((CK_CCM_PARAMS*)tmp)->pNonce);
321324
free(((CK_CCM_PARAMS*)tmp)->pAAD);
322325
break;
323326
case CKM_TLS_PRF:
324327
case CKM_NSS_TLS_PRF_GENERAL:
325-
tmp = mechPtr->pParameter;
326-
TRACE1("\t=> free CK_TLS_PRF_PARAMS %lX\n",
327-
ptr_to_jlong(tmp));
328+
TRACE0("[ CK_TLS_PRF_PARAMS ]\n");
328329
free(((CK_TLS_PRF_PARAMS*)tmp)->pSeed);
329330
free(((CK_TLS_PRF_PARAMS*)tmp)->pLabel);
330331
free(((CK_TLS_PRF_PARAMS*)tmp)->pulOutputLen);
@@ -334,18 +335,16 @@ void freeCKMechanismPtr(CK_MECHANISM_PTR mechPtr) {
334335
case CKM_TLS_MASTER_KEY_DERIVE:
335336
case CKM_SSL3_MASTER_KEY_DERIVE_DH:
336337
case CKM_TLS_MASTER_KEY_DERIVE_DH:
337-
sslMkdTmp = mechPtr->pParameter;
338-
TRACE1("\t=> free CK_SSL3_MASTER_KEY_DERIVE_PARAMS %lX\n",
339-
ptr_to_jlong(sslMkdTmp));
338+
sslMkdTmp = tmp;
339+
TRACE0("[ CK_SSL3_MASTER_KEY_DERIVE_PARAMS ]\n");
340340
free(sslMkdTmp->RandomInfo.pClientRandom);
341341
free(sslMkdTmp->RandomInfo.pServerRandom);
342342
free(sslMkdTmp->pVersion);
343343
break;
344344
case CKM_SSL3_KEY_AND_MAC_DERIVE:
345345
case CKM_TLS_KEY_AND_MAC_DERIVE:
346-
sslKmTmp = mechPtr->pParameter;
347-
TRACE1("\t=> free CK_SSL3_KEY_MAT_PARAMS %lX\n",
348-
ptr_to_jlong(sslKmTmp));
346+
sslKmTmp = tmp;
347+
TRACE0("[ CK_SSL3_KEY_MAT_PARAMS ]\n");
349348
free(sslKmTmp->RandomInfo.pClientRandom);
350349
free(sslKmTmp->RandomInfo.pServerRandom);
351350
if (sslKmTmp->pReturnedKeyMaterial != NULL) {
@@ -356,17 +355,15 @@ void freeCKMechanismPtr(CK_MECHANISM_PTR mechPtr) {
356355
break;
357356
case CKM_TLS12_MASTER_KEY_DERIVE:
358357
case CKM_TLS12_MASTER_KEY_DERIVE_DH:
359-
tlsMkdTmp = mechPtr->pParameter;
360-
TRACE1("\t=> CK_TLS12_MASTER_KEY_DERIVE_PARAMS %lX\n",
361-
ptr_to_jlong(tlsMkdTmp));
358+
tlsMkdTmp = tmp;
359+
TRACE0("[ CK_TLS12_MASTER_KEY_DERIVE_PARAMS ]\n");
362360
free(tlsMkdTmp->RandomInfo.pClientRandom);
363361
free(tlsMkdTmp->RandomInfo.pServerRandom);
364362
free(tlsMkdTmp->pVersion);
365363
break;
366364
case CKM_TLS12_KEY_AND_MAC_DERIVE:
367-
tlsKmTmp = mechPtr->pParameter;
368-
TRACE1("\t=> free CK_TLS12_KEY_MAT_PARAMS %lX\n",
369-
ptr_to_jlong(tlsKmTmp));
365+
tlsKmTmp = tmp;
366+
TRACE0("[ CK_TLS12_KEY_MAT_PARAMS ]\n");
370367
free(tlsKmTmp->RandomInfo.pClientRandom);
371368
free(tlsKmTmp->RandomInfo.pServerRandom);
372369
if (tlsKmTmp->pReturnedKeyMaterial != NULL) {
@@ -377,17 +374,14 @@ void freeCKMechanismPtr(CK_MECHANISM_PTR mechPtr) {
377374
break;
378375
case CKM_ECDH1_DERIVE:
379376
case CKM_ECDH1_COFACTOR_DERIVE:
380-
tmp = mechPtr->pParameter;
381-
TRACE1("\t=> free CK_ECDH1_DERIVE_PARAMS %lX\n",
382-
ptr_to_jlong(tmp));
377+
TRACE0("[ CK_ECDH1_DERIVE_PARAMS ]\n");
383378
free(((CK_ECDH1_DERIVE_PARAMS *)tmp)->pSharedData);
384379
free(((CK_ECDH1_DERIVE_PARAMS *)tmp)->pPublicData);
385380
break;
386381
case CKM_TLS_MAC:
387382
case CKM_AES_CTR:
388383
case CKM_RSA_PKCS_PSS:
389384
case CKM_CAMELLIA_CTR:
390-
TRACE0("\t=> NO OP\n");
391385
// params do not contain pointers
392386
break;
393387
default:
@@ -399,17 +393,59 @@ void freeCKMechanismPtr(CK_MECHANISM_PTR mechPtr) {
399393
// CKM_EXTRACT_KEY_FROM_KEY, CKM_OTP, CKM_KIP,
400394
// CKM_DSA_PARAMETER_GEN?, CKM_GOSTR3410_*
401395
// CK_any_CBC_ENCRYPT_DATA?
402-
TRACE0("\t=> ERROR UNSUPPORTED CK PARAMS\n");
396+
TRACE0("ERROR: UNSUPPORTED CK_MECHANISM\n");
403397
break;
404398
}
405-
free(mechPtr->pParameter);
399+
TRACE1("\t=> freed param %p\n", tmp);
400+
free(tmp);
406401
} else {
407-
TRACE0("DEBUG => Parameter NULL\n");
402+
TRACE0("\t=> param NULL\n");
408403
}
409404
free(mechPtr);
405+
TRACE0("FINISHED\n");
410406
}
411407
}
412408

409+
/* This function replaces the CK_GCM_PARAMS_NO_IVBITS structure associated
410+
* with the specified CK_MECHANISM structure with CK_GCM_PARAMS
411+
* structure.
412+
*
413+
* @param mechPtr pointer to the CK_MECHANISM structure containing
414+
* the to-be-converted CK_GCM_PARAMS_NO_IVBITS structure.
415+
* @return pointer to the CK_MECHANISM structure containing the
416+
* converted CK_GCM_PARAMS structure or NULL if no conversion took place.
417+
*/
418+
CK_MECHANISM_PTR updateGCMParams(JNIEnv *env, CK_MECHANISM_PTR mechPtr) {
419+
CK_GCM_PARAMS* pGcmParams2 = NULL;
420+
CK_GCM_PARAMS_NO_IVBITS* pParams = NULL;
421+
if ((mechPtr->mechanism == CKM_AES_GCM) &&
422+
(mechPtr->pParameter != NULL_PTR) &&
423+
(mechPtr->ulParameterLen == sizeof(CK_GCM_PARAMS_NO_IVBITS))) {
424+
pGcmParams2 = calloc(1, sizeof(CK_GCM_PARAMS));
425+
if (pGcmParams2 == NULL) {
426+
throwOutOfMemoryError(env, 0);
427+
return NULL;
428+
}
429+
pParams = (CK_GCM_PARAMS_NO_IVBITS*) mechPtr->pParameter;
430+
pGcmParams2->pIv = pParams->pIv;
431+
pGcmParams2->ulIvLen = pParams->ulIvLen;
432+
pGcmParams2->ulIvBits = (pGcmParams2->ulIvLen << 3);
433+
pGcmParams2->pAAD = pParams->pAAD;
434+
pGcmParams2->ulAADLen = pParams->ulAADLen;
435+
pGcmParams2->ulTagBits = pParams->ulTagBits;
436+
TRACE1("DEBUG updateGCMParams: pMech %p\n", mechPtr);
437+
TRACE2("\t=> GCM param w/o ulIvBits %p => GCM param %p\n", pParams,
438+
pGcmParams2);
439+
free(pParams);
440+
mechPtr->pParameter = pGcmParams2;
441+
mechPtr->ulParameterLen = sizeof(CK_GCM_PARAMS);
442+
return mechPtr;
443+
} else {
444+
TRACE0("DEBUG updateGCMParams: no conversion done\n");
445+
}
446+
return NULL;
447+
}
448+
413449
/*
414450
* the following functions convert Java arrays to PKCS#11 array pointers and
415451
* their array length and vice versa
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/* There is a known incompatibility for CK_GCM_PARAMS structure.
25+
* PKCS#11 v2.40 standard mechanisms specification specifies
26+
* CK_GCM_PARAMS as
27+
* typedef struct CK_GCM_PARAMS {
28+
* CK_BYTE_PTR pIv;
29+
* CK_ULONG ulIvLen;
30+
* CK_BYTE_PTR pAAD;
31+
* CK_ULONG ulAADLen;
32+
* CK_ULONG ulTagBits;
33+
* } CK_GCM_PARAMS;
34+
* However, the official header file of PKCS#11 v2.40 defines the
35+
* CK_GCM_PARAMS with an extra "ulIvBits" field (type CK_ULONG).
36+
* NSS uses the spec version while Solaris and SoftHSM2 use the header
37+
* version. In order to work with both sides, SunPKCS11 provider defines
38+
* the spec version of CK_GCM_PARAMS as CK_GCM_PARAMS_NO_IVBITS (as in this
39+
* file) and uses it first before failing over to the header version.
40+
*/
41+
#ifndef _PKCS11GCM2_H_
42+
#define _PKCS11GCM2_H_ 1
43+
44+
/* include the platform dependent part of the header */
45+
typedef struct CK_GCM_PARAMS_NO_IVBITS {
46+
CK_BYTE_PTR pIv;
47+
CK_ULONG ulIvLen;
48+
CK_BYTE_PTR pAAD;
49+
CK_ULONG ulAADLen;
50+
CK_ULONG ulTagBits;
51+
} CK_GCM_PARAMS_NO_IVBITS;
52+
53+
typedef CK_GCM_PARAMS_NO_IVBITS CK_PTR CK_GCM_PARAMS_NO_IVBITS_PTR;
54+
55+
#endif /* _PKCS11GCM2_H_ */

‎src/jdk.crypto.cryptoki/share/native/libj2pkcs11/pkcs11t.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,7 @@ typedef CK_AES_CTR_PARAMS CK_PTR CK_AES_CTR_PARAMS_PTR;
18331833
typedef struct CK_GCM_PARAMS {
18341834
CK_BYTE_PTR pIv;
18351835
CK_ULONG ulIvLen;
1836+
CK_ULONG ulIvBits;
18361837
CK_BYTE_PTR pAAD;
18371838
CK_ULONG ulAADLen;
18381839
CK_ULONG ulTagBits;
@@ -1962,7 +1963,7 @@ typedef struct CK_TLS_KDF_PARAMS {
19621963
typedef CK_TLS_KDF_PARAMS CK_PTR CK_TLS_KDF_PARAMS_PTR;
19631964

19641965
typedef struct CK_TLS_MAC_PARAMS {
1965-
CK_MECHANISM_TYPE prfMechanism;
1966+
CK_MECHANISM_TYPE prfHashMechanism;
19661967
CK_ULONG ulMacLength;
19671968
CK_ULONG ulServerOrClient;
19681969
} CK_TLS_MAC_PARAMS;
@@ -2000,3 +2001,4 @@ typedef CK_SEED_CBC_ENCRYPT_DATA_PARAMS CK_PTR \
20002001

20012002
#endif /* _PKCS11T_H_ */
20022003

2004+

‎src/jdk.crypto.cryptoki/share/native/libj2pkcs11/pkcs11wrapper.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@
159159
/* include the platform dependent part of the header */
160160
#include "p11_md.h"
161161

162-
#include "pkcs11.h"
163162
#include <jni.h>
164163
#include <jni_util.h>
165164
#include <stdarg.h>
@@ -296,6 +295,10 @@ void printDebug(const char *format, ...);
296295
#define CLASS_TLS_PRF_PARAMS "sun/security/pkcs11/wrapper/CK_TLS_PRF_PARAMS"
297296
#define CLASS_TLS_MAC_PARAMS "sun/security/pkcs11/wrapper/CK_TLS_MAC_PARAMS"
298297

298+
/* function to update the CK_NSS_GCM_PARAMS in mechanism pointer with
299+
* CK_GCM_PARAMS
300+
*/
301+
CK_MECHANISM_PTR updateGCMParams(JNIEnv *env, CK_MECHANISM_PTR mechPtr);
299302

300303
/* function to convert a PKCS#11 return value other than CK_OK into a Java Exception
301304
* or to throw a PKCS11RuntimeException

‎src/jdk.crypto.cryptoki/unix/native/libj2pkcs11/p11_md.h

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
*/
4+
15
/*
26
* reserved comment block
37
* DO NOT REMOVE OR ALTER!
@@ -69,6 +73,7 @@
6973
#endif
7074

7175
#include "pkcs11.h"
76+
#include "pkcs11gcm2.h"
7277

7378
#include "jni.h"
7479

‎src/jdk.crypto.cryptoki/windows/native/libj2pkcs11/p11_md.h

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
*/
4+
15
/*
26
* reserved comment block
37
* DO NOT REMOVE OR ALTER!
@@ -77,6 +81,7 @@
7781
#endif /* CreateMutex */
7882

7983
#include "pkcs11.h"
84+
#include "pkcs11gcm2.h"
8085

8186
/* statement according to PKCS11 docu */
8287
#pragma pack(pop, cryptoki)

‎test/jdk/sun/security/pkcs11/Cipher/TestGCMKeyAndIvCheck.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8080462
26+
* @bug 8080462 8229243
2727
* @library /test/lib ..
2828
* @modules jdk.crypto.cryptoki
2929
* @run main TestGCMKeyAndIvCheck
@@ -81,6 +81,7 @@ public void test(String mode, Provider p) throws Exception {
8181
", no support for " + mode);
8282
return;
8383
}
84+
System.out.println("Testing against " + p.getName());
8485
SecretKey key = new SecretKeySpec(new byte[16], "AES");
8586
// First try parameter-less init.
8687
c.init(Cipher.ENCRYPT_MODE, key);
@@ -111,12 +112,11 @@ public void test(String mode, Provider p) throws Exception {
111112
throw new Exception("Parameters contains incorrect IV value");
112113
}
113114

114-
// Should be ok to use the same key+iv for decryption
115115
c.init(Cipher.DECRYPT_MODE, key, params);
116116
c.updateAAD(AAD);
117117
byte[] recovered = c.doFinal(ctPlusTag);
118118
if (!Arrays.equals(recovered, PT)) {
119-
throw new Exception("decryption result mismatch");
119+
throw new Exception("Decryption result mismatch");
120120
}
121121

122122
// Now try to encrypt again using the same key+iv; should fail also
@@ -125,6 +125,7 @@ public void test(String mode, Provider p) throws Exception {
125125
throw new Exception("Should throw exception when same key+iv is used");
126126
} catch (InvalidAlgorithmParameterException iape) {
127127
// expected
128+
System.out.println("Expected IAPE thrown");
128129
}
129130

130131
// Now try to encrypt again using parameter-less init; should work
@@ -138,7 +139,8 @@ public void test(String mode, Provider p) throws Exception {
138139
}
139140

140141
// Now try to encrypt again using a different parameter; should work
141-
AlgorithmParameterSpec spec2 = new GCMParameterSpec(128, new byte[30]);
142+
AlgorithmParameterSpec spec2 = new GCMParameterSpec(128,
143+
"Solaris PKCS11 lib does not allow all-zero IV".getBytes());
142144
c.init(Cipher.ENCRYPT_MODE, key, spec2);
143145
c.updateAAD(AAD);
144146
c.doFinal(PT);
@@ -154,7 +156,7 @@ public void test(String mode, Provider p) throws Exception {
154156
c.updateAAD(AAD);
155157
recovered = c.doFinal(ctPlusTag);
156158
if (!Arrays.equals(recovered, PT)) {
157-
throw new Exception("decryption result mismatch");
159+
throw new Exception("Decryption result mismatch");
158160
}
159161

160162
// Now try decryption again and re-init using the same parameters

0 commit comments

Comments
 (0)
Please sign in to comment.