Skip to content

Commit 758c669

Browse files
committed
F-2382 - https://fenrir.wolfssl.com/finding/2382 - Add test for CKA_WRAP/CKA_UNWRAP attribute enforcement in C_WrapKey/C_UnwrapKey
1 parent 7372b16 commit 758c669

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

tests/pkcs11test.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6329,6 +6329,106 @@ static CK_RV test_wrap_key_unextractable(void* args)
63296329
return ret;
63306330
}
63316331

6332+
static CK_RV test_wrap_unwrap_op_not_supported(void* args)
6333+
{
6334+
CK_SESSION_HANDLE session = *(CK_SESSION_HANDLE*)args;
6335+
CK_RV ret;
6336+
CK_MECHANISM mech = { CKM_AES_KEY_WRAP, NULL, 0 };
6337+
CK_OBJECT_HANDLE noWrapKey = CK_INVALID_HANDLE;
6338+
CK_OBJECT_HANDLE key = CK_INVALID_HANDLE;
6339+
byte wrappedKey[40], keyData[16];
6340+
CK_ULONG wrappedKeyLen = sizeof(wrappedKey);
6341+
CK_ATTRIBUTE noWrapTmpl[] = {
6342+
{ CKA_CLASS, &secretKeyClass, sizeof(secretKeyClass) },
6343+
{ CKA_KEY_TYPE, &aesKeyType, sizeof(aesKeyType) },
6344+
{ CKA_VALUE, aes_128_key, sizeof(aes_128_key) },
6345+
{ CKA_WRAP, &ckFalse, sizeof(ckFalse) },
6346+
{ CKA_UNWRAP, &ckTrue, sizeof(ckTrue) },
6347+
};
6348+
CK_ULONG noWrapTmplCnt = sizeof(noWrapTmpl) / sizeof(*noWrapTmpl);
6349+
CK_ATTRIBUTE noUnwrapTmpl[] = {
6350+
{ CKA_CLASS, &secretKeyClass, sizeof(secretKeyClass) },
6351+
{ CKA_KEY_TYPE, &aesKeyType, sizeof(aesKeyType) },
6352+
{ CKA_VALUE, aes_128_key, sizeof(aes_128_key) },
6353+
{ CKA_WRAP, &ckTrue, sizeof(ckTrue) },
6354+
{ CKA_UNWRAP, &ckFalse, sizeof(ckFalse) },
6355+
};
6356+
CK_ULONG noUnwrapTmplCnt = sizeof(noUnwrapTmpl) / sizeof(*noUnwrapTmpl);
6357+
CK_ATTRIBUTE unwrapResultTmpl[] = {
6358+
{ CKA_CLASS, &secretKeyClass, sizeof(secretKeyClass) },
6359+
{ CKA_KEY_TYPE, &genericKeyType, sizeof(genericKeyType) },
6360+
};
6361+
CK_ULONG unwrapResultCnt = sizeof(unwrapResultTmpl) /
6362+
sizeof(*unwrapResultTmpl);
6363+
CK_OBJECT_HANDLE unwrapped = CK_INVALID_HANDLE;
6364+
6365+
memset(keyData, 0x42, sizeof(keyData));
6366+
6367+
/* Create key with CKA_WRAP=FALSE */
6368+
ret = funcList->C_CreateObject(session, noWrapTmpl, noWrapTmplCnt,
6369+
&noWrapKey);
6370+
CHECK_CKR(ret, "Create AES key with CKA_WRAP=FALSE");
6371+
if (ret == CKR_OK) {
6372+
ret = get_generic_key(session, keyData, sizeof(keyData), CK_TRUE, &key);
6373+
}
6374+
if (ret == CKR_OK) {
6375+
ret = funcList->C_WrapKey(session, &mech, noWrapKey, key,
6376+
wrappedKey, &wrappedKeyLen);
6377+
CHECK_CKR_FAIL(ret, CKR_KEY_TYPE_INCONSISTENT,
6378+
"WrapKey should fail with CKA_WRAP=FALSE");
6379+
}
6380+
funcList->C_DestroyObject(session, noWrapKey);
6381+
funcList->C_DestroyObject(session, key);
6382+
6383+
/* Now test CKA_UNWRAP=FALSE: first wrap with a valid key, then unwrap
6384+
* with a key that has CKA_UNWRAP=FALSE */
6385+
if (ret == CKR_OK) {
6386+
CK_OBJECT_HANDLE wrapKey = CK_INVALID_HANDLE;
6387+
ret = funcList->C_CreateObject(session, noUnwrapTmpl, noUnwrapTmplCnt,
6388+
&noWrapKey);
6389+
CHECK_CKR(ret, "Create AES key with CKA_UNWRAP=FALSE");
6390+
if (ret == CKR_OK) {
6391+
/* Use same key template but with wrap=true to actually wrap */
6392+
CK_ATTRIBUTE wrapOkTmpl[] = {
6393+
{ CKA_CLASS, &secretKeyClass, sizeof(secretKeyClass) },
6394+
{ CKA_KEY_TYPE, &aesKeyType, sizeof(aesKeyType) },
6395+
{ CKA_VALUE, aes_128_key, sizeof(aes_128_key) },
6396+
{ CKA_WRAP, &ckTrue, sizeof(ckTrue) },
6397+
{ CKA_UNWRAP, &ckTrue, sizeof(ckTrue) },
6398+
};
6399+
ret = funcList->C_CreateObject(session, wrapOkTmpl,
6400+
sizeof(wrapOkTmpl)/sizeof(*wrapOkTmpl),
6401+
&wrapKey);
6402+
CHECK_CKR(ret, "Create valid wrapping key");
6403+
}
6404+
if (ret == CKR_OK) {
6405+
ret = get_generic_key(session, keyData, sizeof(keyData), CK_TRUE,
6406+
&key);
6407+
}
6408+
if (ret == CKR_OK) {
6409+
wrappedKeyLen = sizeof(wrappedKey);
6410+
ret = funcList->C_WrapKey(session, &mech, wrapKey, key,
6411+
wrappedKey, &wrappedKeyLen);
6412+
CHECK_CKR(ret, "Wrap key for unwrap test");
6413+
}
6414+
funcList->C_DestroyObject(session, wrapKey);
6415+
funcList->C_DestroyObject(session, key);
6416+
6417+
if (ret == CKR_OK) {
6418+
ret = funcList->C_UnwrapKey(session, &mech, noWrapKey, wrappedKey,
6419+
wrappedKeyLen, unwrapResultTmpl,
6420+
unwrapResultCnt, &unwrapped);
6421+
CHECK_CKR_FAIL(ret, CKR_KEY_TYPE_INCONSISTENT,
6422+
"UnwrapKey should fail with CKA_UNWRAP=FALSE");
6423+
}
6424+
funcList->C_DestroyObject(session, noWrapKey);
6425+
if (unwrapped != CK_INVALID_HANDLE)
6426+
funcList->C_DestroyObject(session, unwrapped);
6427+
}
6428+
6429+
return ret;
6430+
}
6431+
63326432
/* Regression test: C_WrapKey on a key with CKA_WRAP_WITH_TRUSTED=CK_TRUE must
63336433
* return CKR_KEY_NOT_WRAPPABLE when the wrapping key lacks CKA_TRUSTED, and
63346434
* succeed when the wrapping key has CKA_TRUSTED=CK_TRUE. */
@@ -17156,6 +17256,7 @@ static TEST_FUNC testFunc[] = {
1715617256
PKCS11TEST_FUNC_SESS_DECL(test_aes_wrap_unwrap_pad_key),
1715717257
PKCS11TEST_FUNC_SESS_DECL(test_wrap_unwrap_key),
1715817258
PKCS11TEST_FUNC_SESS_DECL(test_wrap_key_unextractable),
17259+
PKCS11TEST_FUNC_SESS_DECL(test_wrap_unwrap_op_not_supported),
1715917260
PKCS11TEST_FUNC_SESS_DECL(test_wrap_key_wrap_with_trusted),
1716017261
#if !defined(WOLFPKCS11_NSS)
1716117262
PKCS11TEST_FUNC_SESS_DECL(test_wrap_key_ro_session),

0 commit comments

Comments
 (0)