Skip to content

Commit 09cc2de

Browse files
committed
Move allocation a little later
We need to allocate the key information after we know whether or not the object is a key.
1 parent 8c76df6 commit 09cc2de

3 files changed

Lines changed: 141 additions & 48 deletions

File tree

src/crypto.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,13 @@ static CK_RV NewObject(WP11_Session* session, CK_KEY_TYPE keyType,
702702
if (ret != 0)
703703
return CKR_FUNCTION_FAILED;
704704

705+
/* Now that object class is set, allocate type-specific data */
706+
ret = wp11_Object_AllocateTypeData(obj);
707+
if (ret == MEMORY_E)
708+
return CKR_DEVICE_MEMORY;
709+
if (ret != 0)
710+
return CKR_FUNCTION_FAILED;
711+
705712
rv = SetAttributeValue(session, obj, pTemplate, ulCount, CK_TRUE);
706713
if (rv != CKR_OK) {
707714
WP11_Object_Free(obj);
@@ -1141,6 +1148,18 @@ CK_RV C_CopyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
11411148
if (ret != 0)
11421149
return CKR_FUNCTION_FAILED;
11431150

1151+
/* Set the object class from the original object */
1152+
ret = WP11_Object_SetClass(newObj, WP11_Object_GetClass(obj));
1153+
if (ret != 0)
1154+
return CKR_FUNCTION_FAILED;
1155+
1156+
/* Now that object class is set, allocate type-specific data */
1157+
ret = wp11_Object_AllocateTypeData(newObj);
1158+
if (ret == MEMORY_E)
1159+
return CKR_DEVICE_MEMORY;
1160+
if (ret != 0)
1161+
return CKR_FUNCTION_FAILED;
1162+
11441163
/* Use get and set attribute value to fill in object. */
11451164
rv = C_GetAttributeValue(hSession, hObject, pTemplate, ulCount);
11461165
if (rv != CKR_OK) {

src/internal.c

Lines changed: 121 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,56 +1919,130 @@ static int wp11_Object_New(WP11_Slot* slot, CK_KEY_TYPE type,
19191919
obj->onToken = 0;
19201920
obj->slot = slot;
19211921
obj->keyGenMech = CK_UNAVAILABLE_INFORMATION;
1922-
#ifdef WOLFPKCS11_TPM
1923-
if (type == CKK_EC || type == CKK_RSA) {
1924-
obj->tpmKey = (WOLFTPM2_KEYBLOB*)XMALLOC(sizeof(WOLFTPM2_KEYBLOB),
1925-
NULL, DYNAMIC_TYPE_TMP_BUFFER);
1926-
if (obj->tpmKey == NULL) {
1927-
ret = MEMORY_E;
1928-
}
1929-
else {
1930-
XMEMSET(obj->tpmKey, 0, sizeof(WOLFTPM2_KEYBLOB));
1931-
obj->tpmKey->handle.hndl = TPM_RH_NULL;
1932-
}
1922+
/* TPM key allocation will be done later when object class is known */
1923+
}
1924+
1925+
/* Type-specific allocation will be done later when object class is known */
1926+
1927+
if (ret != 0) {
1928+
WP11_Object_Free(obj);
1929+
obj = NULL;
1930+
}
1931+
1932+
*object = obj;
1933+
1934+
return ret;
1935+
}
1936+
1937+
/**
1938+
* Allocate type-specific data for an object based on its class and type.
1939+
* This should be called after the object class is known.
1940+
*
1941+
* @param object [in] Object to allocate type-specific data for.
1942+
* @return MEMORY_E when dynamic memory allocation fails.
1943+
* NOT_AVAILABLE_E when key type not supported.
1944+
* 0 on success.
1945+
*/
1946+
int wp11_Object_AllocateTypeData(WP11_Object* object)
1947+
{
1948+
int ret = 0;
1949+
CK_OBJECT_CLASS objClass;
1950+
1951+
if (object == NULL) {
1952+
return BAD_FUNC_ARG;
1953+
}
1954+
1955+
objClass = object->objClass;
1956+
1957+
/* If object class is not set (-1), infer it from key type */
1958+
if (objClass == (CK_OBJECT_CLASS)-1) {
1959+
switch (object->type) {
1960+
case CKK_RSA:
1961+
case CKK_EC:
1962+
case CKK_DH:
1963+
/* These could be either public or private keys, but we can't
1964+
* tell at this point. The specific allocation will be done
1965+
* later when the actual object class is determined from
1966+
* attributes. */
1967+
return 0;
1968+
case CKK_AES:
1969+
case CKK_GENERIC_SECRET:
1970+
case CKK_HKDF:
1971+
objClass = CKO_SECRET_KEY;
1972+
break;
1973+
default:
1974+
/* Unknown type, don't allocate */
1975+
return 0;
19331976
}
1934-
#endif
19351977
}
19361978

1979+
/* Only allocate type-specific data for key objects, not certificates or
1980+
* other objects */
1981+
if (objClass != CKO_PRIVATE_KEY &&
1982+
objClass != CKO_PUBLIC_KEY &&
1983+
objClass != CKO_SECRET_KEY) {
1984+
/* For non-key objects like certificates, no type-specific allocation
1985+
* needed */
1986+
return 0;
1987+
}
1988+
1989+
#ifdef WOLFPKCS11_TPM
1990+
/* Allocate TPM key data for supported key types */
1991+
if ((object->type == CKK_EC || object->type == CKK_RSA) &&
1992+
object->tpmKey == NULL) {
1993+
object->tpmKey = (WOLFTPM2_KEYBLOB*)XMALLOC(
1994+
sizeof(WOLFTPM2_KEYBLOB), NULL, DYNAMIC_TYPE_TMP_BUFFER);
1995+
if (object->tpmKey == NULL) {
1996+
ret = MEMORY_E;
1997+
}
1998+
else {
1999+
XMEMSET(object->tpmKey, 0, sizeof(WOLFTPM2_KEYBLOB));
2000+
object->tpmKey->handle.hndl = TPM_RH_NULL;
2001+
}
2002+
}
2003+
#endif
2004+
19372005
if (ret == 0) {
1938-
switch (type) {
2006+
switch (object->type) {
19392007
#ifdef HAVE_ECC
19402008
case CKK_EC:
1941-
obj->data.ecKey = (ecc_key*)XMALLOC(sizeof(ecc_key), NULL,
1942-
DYNAMIC_TYPE_ECC);
1943-
if (obj->data.ecKey == NULL) {
1944-
ret = MEMORY_E;
1945-
}
1946-
else {
1947-
XMEMSET(obj->data.ecKey, 0, sizeof(ecc_key));
2009+
if (object->data.ecKey == NULL) {
2010+
object->data.ecKey = (ecc_key*)XMALLOC(sizeof(ecc_key),
2011+
NULL, DYNAMIC_TYPE_ECC);
2012+
if (object->data.ecKey == NULL) {
2013+
ret = MEMORY_E;
2014+
}
2015+
else {
2016+
XMEMSET(object->data.ecKey, 0, sizeof(ecc_key));
2017+
}
19482018
}
19492019
break;
19502020
#endif
19512021
#ifndef NO_RSA
19522022
case CKK_RSA:
1953-
obj->data.rsaKey = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL,
1954-
DYNAMIC_TYPE_RSA);
1955-
if (obj->data.rsaKey == NULL) {
1956-
ret = MEMORY_E;
1957-
}
1958-
else {
1959-
XMEMSET(obj->data.rsaKey, 0, sizeof(RsaKey));
2023+
if (object->data.rsaKey == NULL) {
2024+
object->data.rsaKey = (RsaKey*)XMALLOC(sizeof(RsaKey),
2025+
NULL, DYNAMIC_TYPE_RSA);
2026+
if (object->data.rsaKey == NULL) {
2027+
ret = MEMORY_E;
2028+
}
2029+
else {
2030+
XMEMSET(object->data.rsaKey, 0, sizeof(RsaKey));
2031+
}
19602032
}
19612033
break;
19622034
#endif
19632035
#ifndef NO_DH
19642036
case CKK_DH:
1965-
obj->data.dhKey = (WP11_DhKey*)XMALLOC(sizeof(WP11_DhKey), NULL,
1966-
DYNAMIC_TYPE_DH);
1967-
if (obj->data.dhKey == NULL) {
1968-
ret = MEMORY_E;
1969-
}
1970-
else {
1971-
XMEMSET(obj->data.dhKey, 0, sizeof(WP11_DhKey));
2037+
if (object->data.dhKey == NULL) {
2038+
object->data.dhKey = (WP11_DhKey*)XMALLOC(
2039+
sizeof(WP11_DhKey), NULL, DYNAMIC_TYPE_DH);
2040+
if (object->data.dhKey == NULL) {
2041+
ret = MEMORY_E;
2042+
}
2043+
else {
2044+
XMEMSET(object->data.dhKey, 0, sizeof(WP11_DhKey));
2045+
}
19722046
}
19732047
break;
19742048
#endif
@@ -1979,13 +2053,15 @@ static int wp11_Object_New(WP11_Slot* slot, CK_KEY_TYPE type,
19792053
case CKK_HKDF:
19802054
#endif
19812055
case CKK_GENERIC_SECRET:
1982-
obj->data.symmKey = (WP11_Data*)XMALLOC(sizeof(WP11_Data), NULL,
1983-
DYNAMIC_TYPE_AES);
1984-
if (obj->data.symmKey == NULL) {
1985-
ret = MEMORY_E;
1986-
}
1987-
else {
1988-
XMEMSET(obj->data.symmKey, 0, sizeof(WP11_Data));
2056+
if (object->data.symmKey == NULL) {
2057+
object->data.symmKey = (WP11_Data*)XMALLOC(
2058+
sizeof(WP11_Data), NULL, DYNAMIC_TYPE_AES);
2059+
if (object->data.symmKey == NULL) {
2060+
ret = MEMORY_E;
2061+
}
2062+
else {
2063+
XMEMSET(object->data.symmKey, 0, sizeof(WP11_Data));
2064+
}
19892065
}
19902066
break;
19912067
#ifdef WOLFPKCS11_NSS
@@ -1998,13 +2074,6 @@ static int wp11_Object_New(WP11_Slot* slot, CK_KEY_TYPE type,
19982074
}
19992075
}
20002076

2001-
if (ret != 0) {
2002-
WP11_Object_Free(obj);
2003-
obj = NULL;
2004-
}
2005-
2006-
*object = obj;
2007-
20082077
return ret;
20092078
}
20102079

@@ -3826,6 +3895,10 @@ static int wp11_Object_Load(WP11_Object* object, int tokenId, int objId)
38263895
int ret;
38273896

38283897
ret = wp11_Object_Load_Object(object, tokenId, objId);
3898+
if (ret == 0) {
3899+
/* Now that we know the object class, allocate type-specific data */
3900+
ret = wp11_Object_AllocateTypeData(object);
3901+
}
38293902
if (ret == 0) {
38303903
if (object->objClass == CKO_CERTIFICATE) {
38313904
ret = wp11_Object_Load_Cert(object, tokenId, objId);

wolfpkcs11/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ WP11_LOCAL int WP11_ConstantCompare(const byte* a, const byte* b, int length);
372372

373373
WP11_LOCAL int WP11_Object_New(WP11_Session* session, CK_KEY_TYPE type,
374374
WP11_Object** object);
375+
WP11_LOCAL int wp11_Object_AllocateTypeData(WP11_Object* object);
375376
WP11_LOCAL void WP11_Object_Free(WP11_Object* object);
376377

377378
WP11_LOCAL CK_OBJECT_HANDLE WP11_Object_GetHandle(WP11_Object* object);

0 commit comments

Comments
 (0)