Skip to content

Commit 2d30cee

Browse files
committed
Use dynamic buffer for cert data
1 parent 5185609 commit 2d30cee

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

src/internal.c

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ typedef struct WP11_Data {
141141

142142
/* Certificate */
143143
typedef struct WP11_Cert {
144-
byte data[WP11_MAX_CERT_SZ]; /* Certificate data */
144+
byte *data; /* Certificate data */
145145
word32 len; /* Length of certificate data in bytes */
146146
CK_CERTIFICATE_TYPE type;
147147
} WP11_Cert;
@@ -1619,6 +1619,21 @@ static int wp11_Object_Store_Cert(WP11_Object* object, int tokenId, int objId)
16191619
return ret;
16201620
}
16211621

1622+
/**
1623+
* "Decode" the certificate.
1624+
*
1625+
* Certificates are not encrypted.
1626+
*
1627+
* @param [in, out] object Certificate object.
1628+
*/
1629+
static void wp11_Object_Decode_Cert(WP11_Object* object)
1630+
{
1631+
object->data.cert.data = object->keyData;
1632+
object->data.cert.len = object->keyDataLen;
1633+
object->keyData = NULL;
1634+
object->encoded = 0;
1635+
}
1636+
16221637
#ifndef NO_RSA
16231638
/**
16241639
* Decode the RSA key.
@@ -2808,7 +2823,7 @@ static int wp11_Object_Decode(WP11_Object* object)
28082823
int ret;
28092824

28102825
if (object->objClass == CKO_CERTIFICATE) {
2811-
object->encoded = 0;
2826+
wp11_Object_Decode_Cert(object);
28122827
ret = 0;
28132828
}
28142829
else {
@@ -4947,7 +4962,7 @@ void WP11_Object_Free(WP11_Object* object)
49474962
if (object->keyId != NULL)
49484963
XFREE(object->keyId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
49494964
if (object->objClass == CKO_CERTIFICATE) {
4950-
XMEMSET(object->data.cert.data, 0, object->data.cert.len);
4965+
XFREE(object->data.cert.data, NULL, DYNAMIC_TYPE_CERT);
49514966
}
49524967
else {
49534968
#ifndef NO_RSA
@@ -4964,12 +4979,13 @@ void WP11_Object_Free(WP11_Object* object)
49644979
#endif
49654980
if (object->type == CKK_AES || object->type == CKK_GENERIC_SECRET)
49664981
XMEMSET(object->data.symmKey.data, 0, object->data.symmKey.len);
4967-
#ifndef WOLFPKCS11_NO_STORE
4968-
if (object->keyData != NULL)
4969-
XFREE(object->keyData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
4970-
#endif
49714982
}
49724983

4984+
#ifndef WOLFPKCS11_NO_STORE
4985+
if (object->keyData != NULL)
4986+
XFREE(object->keyData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
4987+
#endif
4988+
49734989
/* Dispose of object. */
49744990
XFREE(object, NULL, DYNAMIC_TYPE_TMP_BUFFER);
49754991
}
@@ -5390,7 +5406,6 @@ int WP11_Object_SetCert(WP11_Object* object, unsigned char** data,
53905406

53915407
cert = &object->data.cert;
53925408
cert->len = 0;
5393-
XMEMSET(cert->data, 0, sizeof(cert->data));
53945409

53955410
/* First item is certificate type */
53965411
if (ret == 0 && data[0] != NULL && len[0] != (int)sizeof(CK_ULONG))
@@ -5400,13 +5415,20 @@ int WP11_Object_SetCert(WP11_Object* object, unsigned char** data,
54005415

54015416
/* Second item is certificate data (CKA_VALUE) */
54025417
if (ret == 0 && data[1] != NULL) {
5403-
if ((word32)len[1] > sizeof(cert->data))
5404-
ret = BUFFER_E;
5405-
else
5406-
cert->len = (word32)len[1];
5418+
cert->len = (word32)len[1];
54075419
}
5408-
if (ret == 0 && data[1] != NULL)
5420+
if (ret == 0 && data[1] != NULL) {
5421+
if (cert->data != NULL) {
5422+
XFREE(cert->data, NULL, DYNAMIC_TYPE_CERT);
5423+
}
5424+
cert->data = (byte *)XMALLOC(cert->len, NULL, DYNAMIC_TYPE_CERT);
5425+
if (cert->data == NULL) {
5426+
ret = MEMORY_E;
5427+
}
5428+
}
5429+
if (ret == 0 && data[1] != NULL) {
54095430
XMEMCPY(cert->data, data[1], cert->len);
5431+
}
54105432

54115433
if (object->onToken)
54125434
WP11_Lock_UnlockRW(object->lock);
@@ -6030,8 +6052,8 @@ int WP11_Object_GetAttr(WP11_Object* object, CK_ATTRIBUTE_TYPE type, byte* data,
60306052
{
60316053
if ((object->objClass == CKO_CERTIFICATE) &&
60326054
(type == CKA_VALUE)) {
6033-
ret = GetData((byte*)object->keyData,
6034-
object->keyDataLen, data, len);
6055+
ret = GetData((byte*)object->data.cert.data,
6056+
object->data.cert.len, data, len);
60356057
break;
60366058
}
60376059
else {

0 commit comments

Comments
 (0)