Skip to content

Commit 9e01d67

Browse files
authored
Merge pull request #39 from ColtonWilley/decode_cert_reuse_fix
Fix decode cert when store is enabled for login/logout reuse scenario
2 parents e516c6f + 6a2a80d commit 9e01d67

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

src/internal.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,9 +1628,10 @@ static int wp11_Object_Store_Cert(WP11_Object* object, int tokenId, int objId)
16281628
*/
16291629
static void wp11_Object_Decode_Cert(WP11_Object* object)
16301630
{
1631-
object->data.cert.data = object->keyData;
1632-
object->data.cert.len = object->keyDataLen;
1633-
object->keyData = NULL;
1631+
if (object->data.cert.data == NULL) {
1632+
object->data.cert.data = object->keyData;
1633+
object->data.cert.len = object->keyDataLen;
1634+
}
16341635
object->encoded = 0;
16351636
}
16361637

@@ -4952,6 +4953,7 @@ void WP11_Session_FindFinal(WP11_Session* session)
49524953
*/
49534954
void WP11_Object_Free(WP11_Object* object)
49544955
{
4956+
int certFreed = 0;
49554957
#ifdef WOLFPKCS11_TPM
49564958
wolfTPM2_UnloadHandle(&object->slot->tpmDev, &object->tpmKey.handle);
49574959
#endif
@@ -4963,6 +4965,7 @@ void WP11_Object_Free(WP11_Object* object)
49634965
XFREE(object->keyId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
49644966
if (object->objClass == CKO_CERTIFICATE) {
49654967
XFREE(object->data.cert.data, NULL, DYNAMIC_TYPE_CERT);
4968+
certFreed = 1;
49664969
}
49674970
else {
49684971
#ifndef NO_RSA
@@ -4982,8 +4985,10 @@ void WP11_Object_Free(WP11_Object* object)
49824985
}
49834986

49844987
#ifndef WOLFPKCS11_NO_STORE
4985-
if (object->keyData != NULL)
4988+
if (object->keyData != NULL && certFreed == 0)
49864989
XFREE(object->keyData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
4990+
#else
4991+
(void)certFreed;
49874992
#endif
49884993

49894994
/* Dispose of object. */

tests/pkcs11test.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7581,6 +7581,7 @@ static CK_RV test_hmac_sha512_fail(void* args)
75817581
static CK_RV test_x509(void* args)
75827582
{
75837583
CK_RV ret = CKR_OK;
7584+
int sessFlags = CKF_SERIAL_SESSION | CKF_RW_SESSION;
75847585
CK_SESSION_HANDLE session = *(CK_SESSION_HANDLE*)args;
75857586
CK_CERTIFICATE_TYPE certType = CKC_X_509;
75867587
CK_UTF8CHAR label[] = "A certificate object";
@@ -7733,6 +7734,11 @@ static CK_RV test_x509(void* args)
77337734
{ CKA_VALUE, NULL, 0 }
77347735
};
77357736
CK_ULONG getTmplCnt = sizeof(getTmpl) / sizeof(*getTmpl);
7737+
CK_ATTRIBUTE findTmpl[] = {
7738+
{ CKA_ID, id, sizeof(id)}
7739+
};
7740+
CK_ULONG findTmplCnt = sizeof(findTmpl) / sizeof(*findTmpl);
7741+
CK_ULONG count = 1;
77367742

77377743
ret = funcList->C_CreateObject(session, tmpl, tmplCnt, &obj);
77387744
CHECK_CKR(ret, "Create certificate object");
@@ -7762,8 +7768,61 @@ static CK_RV test_x509(void* args)
77627768
XFREE(getTmpl[0].pValue, NULL, DYNAMIC_TYPE_TMP_BUFFER);
77637769
}
77647770
}
7771+
}
7772+
7773+
/* Do a login/logout cycle and check that the value still matches */
7774+
if (userPinLen != 0)
7775+
funcList->C_Logout(session);
7776+
funcList->C_CloseSession(session);
7777+
7778+
ret = funcList->C_OpenSession(slot, sessFlags, NULL, NULL, &session);
7779+
CHECK_CKR(ret, "Open Session");
7780+
if (ret == CKR_OK && userPinLen != 0) {
7781+
ret = funcList->C_Login(session, CKU_USER, userPin, userPinLen);
7782+
CHECK_CKR(ret, "Login");
7783+
}
7784+
7785+
ret = funcList->C_FindObjectsInit(session, findTmpl, findTmplCnt);
7786+
CHECK_CKR(ret, "C_FindObjectsInit");
7787+
if (ret == CKR_OK) {
7788+
ret = funcList->C_FindObjects(session, &obj, 1, &count);
7789+
CHECK_CKR(ret, "C_FindObjects");
7790+
if (ret == CKR_OK) {
7791+
ret = funcList->C_FindObjectsFinal(session);
7792+
CHECK_CKR(ret, "C_FindObjectsFinal");
7793+
}
7794+
}
7795+
7796+
if (ret == CKR_OK) {
7797+
getTmpl[0].pValue = NULL;
7798+
getTmpl[0].ulValueLen = 0;
7799+
ret = funcList->C_GetAttributeValue(session, obj, getTmpl, getTmplCnt);
7800+
CHECK_CKR(ret, "C_GetAttributeValue");
7801+
if (ret == CKR_OK) {
7802+
getTmpl[0].pValue = XMALLOC(getTmpl[0].ulValueLen * sizeof(byte),
7803+
NULL, DYNAMIC_TYPE_TMP_BUFFER);
7804+
if (getTmpl[0].pValue == NULL)
7805+
ret = CKR_DEVICE_MEMORY;
7806+
CHECK_CKR(ret, "Allocate get attribute memory");
7807+
7808+
if (ret == CKR_OK) {
7809+
ret = funcList->C_GetAttributeValue(session, obj, getTmpl, getTmplCnt);
7810+
CHECK_CKR(ret, "C_GetAttributeValue");
7811+
7812+
if (sizeof(certificate) != getTmpl[0].ulValueLen) {
7813+
ret = CKR_GENERAL_ERROR;
7814+
}
7815+
if (XMEMCMP(certificate, getTmpl[0].pValue, sizeof(certificate)) != 0) {
7816+
ret = CKR_GENERAL_ERROR;
7817+
}
7818+
CHECK_CKR(ret, "Verify that stored cert matches original");
7819+
7820+
XFREE(getTmpl[0].pValue, NULL, DYNAMIC_TYPE_TMP_BUFFER);
7821+
}
7822+
}
77657823
ret = funcList->C_DestroyObject(session, obj);
77667824
}
7825+
77677826
return ret;
77687827
}
77697828

0 commit comments

Comments
 (0)