Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alloc a buffer for NULL pointer #7646

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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