Skip to content

Commit

Permalink
Merge pull request wolfSSL#7646 from kojo1/i2d-ecdsa
Browse files Browse the repository at this point in the history
alloc a buffer for NULL pointer
  • Loading branch information
SparkiDev authored Jun 18, 2024
2 parents a141041 + 2f379ed commit c91d306
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/pk.c
Original file line number Diff line number Diff line change
Expand Up @@ -13659,6 +13659,7 @@ WOLFSSL_ECDSA_SIG* wolfSSL_d2i_ECDSA_SIG(WOLFSSL_ECDSA_SIG** sig,
int wolfSSL_i2d_ECDSA_SIG(const WOLFSSL_ECDSA_SIG *sig, unsigned char **pp)
{
word32 len = 0;
int update_p = 1;

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

#ifdef WOLFSSL_I2D_ECDSA_SIG_ALLOC
if ((pp != NULL) && (*pp == NULL)) {
*pp = (unsigned char *)XMALLOC(len, NULL, DYNAMIC_TYPE_OPENSSL);
if (*pp != NULL) {
WOLFSSL_MSG("malloc error");
return 0;
}
update_p = 0;
}
#endif

/* Encode only if there is a buffer to encode into. */
if ((pp != NULL) && (*pp != NULL)) {
/* Encode using the internal representations of r and s. */
Expand All @@ -13686,7 +13698,7 @@ int wolfSSL_i2d_ECDSA_SIG(const WOLFSSL_ECDSA_SIG *sig, unsigned char **pp)
/* No bytes encoded. */
len = 0;
}
else {
else if (update_p) {
/* Update pointer to after encoding. */
*pp += len;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -63464,6 +63464,16 @@ static int test_wolfSSL_ECDSA_SIG(void)
ExpectIntEQ((p == outSig + 8), 1);
ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);

p = NULL;
ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), 8);
#ifndef WOLFSSL_I2D_ECDSA_SIG_ALLOC
ExpectNull(p);
#else
ExpectNotNull(p);
ExpectIntEQ(XMEMCMP(p, outSig, 8), 0);
XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
#endif

wolfSSL_ECDSA_SIG_free(sig);
#endif
return EXPECT_RESULT();
Expand Down

0 comments on commit c91d306

Please sign in to comment.