@@ -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 );
0 commit comments