Skip to content

Commit c91d306

Browse files
authored
Merge pull request #7646 from kojo1/i2d-ecdsa
alloc a buffer for NULL pointer
2 parents a141041 + 2f379ed commit c91d306

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/pk.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13659,6 +13659,7 @@ WOLFSSL_ECDSA_SIG* wolfSSL_d2i_ECDSA_SIG(WOLFSSL_ECDSA_SIG** sig,
1365913659
int wolfSSL_i2d_ECDSA_SIG(const WOLFSSL_ECDSA_SIG *sig, unsigned char **pp)
1366013660
{
1366113661
word32 len = 0;
13662+
int update_p = 1;
1366213663

1366313664
/* Validate parameter. */
1366413665
if (sig != NULL) {
@@ -13678,6 +13679,17 @@ int wolfSSL_i2d_ECDSA_SIG(const WOLFSSL_ECDSA_SIG *sig, unsigned char **pp)
1367813679
/* Add in the length of the SEQUENCE. */
1367913680
len += (word32)1 + ASN_LEN_SIZE(len);
1368013681

13682+
#ifdef WOLFSSL_I2D_ECDSA_SIG_ALLOC
13683+
if ((pp != NULL) && (*pp == NULL)) {
13684+
*pp = (unsigned char *)XMALLOC(len, NULL, DYNAMIC_TYPE_OPENSSL);
13685+
if (*pp != NULL) {
13686+
WOLFSSL_MSG("malloc error");
13687+
return 0;
13688+
}
13689+
update_p = 0;
13690+
}
13691+
#endif
13692+
1368113693
/* Encode only if there is a buffer to encode into. */
1368213694
if ((pp != NULL) && (*pp != NULL)) {
1368313695
/* Encode using the internal representations of r and s. */
@@ -13686,7 +13698,7 @@ int wolfSSL_i2d_ECDSA_SIG(const WOLFSSL_ECDSA_SIG *sig, unsigned char **pp)
1368613698
/* No bytes encoded. */
1368713699
len = 0;
1368813700
}
13689-
else {
13701+
else if (update_p) {
1369013702
/* Update pointer to after encoding. */
1369113703
*pp += len;
1369213704
}

tests/api.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63464,6 +63464,16 @@ static int test_wolfSSL_ECDSA_SIG(void)
6346463464
ExpectIntEQ((p == outSig + 8), 1);
6346563465
ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
6346663466

63467+
p = NULL;
63468+
ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), 8);
63469+
#ifndef WOLFSSL_I2D_ECDSA_SIG_ALLOC
63470+
ExpectNull(p);
63471+
#else
63472+
ExpectNotNull(p);
63473+
ExpectIntEQ(XMEMCMP(p, outSig, 8), 0);
63474+
XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
63475+
#endif
63476+
6346763477
wolfSSL_ECDSA_SIG_free(sig);
6346863478
#endif
6346963479
return EXPECT_RESULT();

0 commit comments

Comments
 (0)