Skip to content

Commit

Permalink
i2d_ECDSA_SIG: alloc a buffer for NULL pointer wolfSSL#7646
Browse files Browse the repository at this point in the history
  • Loading branch information
kojo1 committed Jul 9, 2024
1 parent 73a884f commit 519d141
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
26 changes: 22 additions & 4 deletions src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -37386,7 +37386,8 @@ 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;
word32 len = 0;
int update_p = 1;

if (sig == NULL)
return 0;
Expand All @@ -37403,13 +37404,30 @@ int wolfSSL_i2d_ECDSA_SIG(const WOLFSSL_ECDSA_SIG *sig, unsigned char **pp)
* and less than 256 bytes.
*/
len = 1 + ((len > 127) ? 2 : 1) + len;
if (pp != NULL && *pp != NULL) {

#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. */
if (StoreECC_DSA_Sig(*pp, &len, (mp_int*)sig->r->internal,
(mp_int*)sig->s->internal) != MP_OKAY) {
(mp_int*)sig->s->internal) != MP_OKAY) {
/* No bytes encoded. */
len = 0;
}
else
else if (update_p) {
/* Update pointer to after encoding. */
*pp += len;
}
}

return (int)len;
Expand Down
15 changes: 13 additions & 2 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -2035,8 +2035,9 @@ static void test_wolfSSL_EC(void)
0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
};

#ifdef HAVE_COMP_KEY
const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";

#ifdef HAVE_COMP_KEY
const unsigned char binCompG[] = {
0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
Expand Down Expand Up @@ -2152,9 +2153,9 @@ static void test_wolfSSL_EC(void)
AssertIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);

#ifdef HAVE_COMP_KEY
hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
AssertStrEQ(hexStr, compG);
#ifdef HAVE_COMP_KEY
AssertNotNull(get_point = EC_POINT_hex2point(group, hexStr, get_point, ctx));
AssertIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
#endif
Expand Down Expand Up @@ -2258,6 +2259,16 @@ static void test_wolfSSL_ECDSA_SIG(void)
AssertIntEQ((p == outSig + 8), 1);
AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);

p = NULL;
AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), 8);
#ifndef WOLFSSL_I2D_ECDSA_SIG_ALLOC
AssertNull(p);
#else
AssertNotNull(p);
AssertIntEQ(XMEMCMP(p, outSig, 8), 0);
XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
#endif

wolfSSL_ECDSA_SIG_free(sig);
#endif /* HAVE_ECC */
}
Expand Down

0 comments on commit 519d141

Please sign in to comment.