Skip to content

Commit 6e16754

Browse files
authored
Merge pull request #154 from ColtonWilley/wp_sig_null_init
Fix NULL reinit handling for signatures
2 parents f37e24b + c7d8fbc commit 6e16754

File tree

8 files changed

+169
-11
lines changed

8 files changed

+169
-11
lines changed

src/wp_ecdsa_sig.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,20 @@ static int wp_ecdsa_signverify_init(wp_EcdsaSigCtx *ctx, wp_Ecc* ecc,
185185
{
186186
int ok = 1;
187187

188-
if (ctx->ecc != ecc) {
189-
wp_ecc_free(ctx->ecc);
188+
if (ctx == NULL || (ecc == NULL && ctx->ecc == NULL)) {
189+
ok = 0;
190+
}
191+
else if (ecc != NULL) {
190192
if (!wp_ecc_up_ref(ecc)) {
191193
ok = 0;
192194
}
195+
if (ok) {
196+
wp_ecc_free(ctx->ecc);
197+
ctx->ecc = ecc;
198+
}
193199
}
200+
194201
if (ok) {
195-
ctx->ecc = ecc;
196202
ctx->op = op;
197203

198204
if (!wp_ecdsa_set_ctx_params(ctx, params)) {

src/wp_ecx_sig.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,19 @@ static int wp_ecx_digest_signverify_init(wp_EcxSigCtx *ctx,
188188
ok = 0;
189189
}
190190

191-
if (ok && (ctx->ecx != ecx)) {
192-
wp_ecx_free(ctx->ecx);
191+
if (ok && (ctx == NULL || (ctx->ecx == NULL && ecx == NULL))) {
192+
ok = 0;
193+
}
194+
else if (ok && ecx != NULL) {
193195
if (!wp_ecx_up_ref(ecx)) {
194196
ok = 0;
195197
}
198+
if (ok) {
199+
wp_ecx_free(ctx->ecx);
200+
ctx->ecx = ecx;
201+
}
196202
}
197203
if (ok) {
198-
ctx->ecx = ecx;
199204
ctx->op = op;
200205
}
201206

src/wp_rsa_sig.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,16 +455,15 @@ static int wp_rsa_signverify_init(wp_RsaSigCtx* ctx, wp_Rsa* rsa,
455455
{
456456
int ok = 1;
457457

458-
if ((ctx == NULL) || (rsa == NULL)) {
458+
if ((ctx == NULL) || (ctx->rsa == NULL && rsa == NULL)) {
459459
ok = 0;
460460
}
461-
if (ok && (ctx->rsa != rsa)) {
462-
wp_rsa_free(ctx->rsa);
463-
ctx->rsa = NULL;
461+
else if (rsa != NULL) {
464462
if (!wp_rsa_up_ref(rsa)) {
465463
ok = 0;
466464
}
467-
else {
465+
if (ok) {
466+
wp_rsa_free(ctx->rsa);
468467
ctx->rsa = rsa;
469468
}
470469
}

test/test_ecc.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,5 +2005,54 @@ int test_ec_import(void* data)
20052005
return err;
20062006
}
20072007

2008+
static int test_ec_null_sign_init_ex(OSSL_LIB_CTX *libCtx)
2009+
{
2010+
#ifndef WP_HAVE_EC_P256
2011+
(void)libCtx;
2012+
PRINT_MSG("Skipping test - WP_HAVE_EC_P256 not defined");
2013+
return 0;
2014+
#else
2015+
int err = 0;
2016+
EVP_MD_CTX *ctx = NULL;
2017+
EVP_MD *md = NULL;
2018+
EVP_PKEY *pkey = NULL;
2019+
const unsigned char *p = ecc_key_der_256;
2020+
2021+
err = (ctx = EVP_MD_CTX_new()) == NULL;
2022+
if (err == 0) {
2023+
md = EVP_MD_fetch(libCtx, "SHA256", NULL);
2024+
err = md == NULL;
2025+
}
2026+
if (err == 0) {
2027+
pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, sizeof(ecc_key_der_256));
2028+
err = pkey == NULL;
2029+
}
2030+
if (err == 0) {
2031+
err = EVP_DigestSignInit_ex(ctx, NULL, "SHA256", libCtx, NULL, pkey, NULL) != 1;
2032+
}
2033+
if (err == 0) {
2034+
err = EVP_DigestSignInit_ex(ctx, NULL, "SHA256", libCtx, NULL, NULL, NULL) != 1;
2035+
}
2036+
2037+
EVP_PKEY_free(pkey);
2038+
EVP_MD_free(md);
2039+
EVP_MD_CTX_free(ctx);
2040+
2041+
return err;
2042+
#endif
2043+
}
2044+
2045+
int test_ec_null_init(void* data)
2046+
{
2047+
int err = 0;
2048+
(void)data;
2049+
2050+
err = test_ec_null_sign_init_ex(osslLibCtx);
2051+
if (err == 0) {
2052+
err = test_ec_null_sign_init_ex(wpLibCtx);
2053+
}
2054+
2055+
return err;
2056+
}
20082057

20092058
#endif /* WP_HAVE_ECC */

test/test_ecx.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,4 +626,53 @@ int test_ecx_misc(void *data)
626626
return err;
627627
}
628628

629+
static int test_ecx_null_sign_init_ex(OSSL_LIB_CTX *libCtx)
630+
{
631+
int err = 0;
632+
EVP_MD_CTX *ctx = NULL;
633+
EVP_MD *md = NULL;
634+
EVP_PKEY *pkey = NULL;
635+
#ifdef WP_HAVE_ED25519
636+
const unsigned char *p = ed25519_key_der;
637+
#endif
638+
639+
#ifndef WP_HAVE_ED25519
640+
(void)libCtx;
641+
(void)provider_name;
642+
PRINT_MSG("Skipping test - WP_HAVE_ED25519 not defined");
643+
return 0;
644+
#endif
645+
646+
err = (ctx = EVP_MD_CTX_new()) == NULL;
647+
if (err == 0) {
648+
pkey = d2i_PrivateKey(EVP_PKEY_ED25519, NULL, &p, sizeof(ed25519_key_der));
649+
err = pkey == NULL;
650+
}
651+
if (err == 0) {
652+
err = EVP_DigestSignInit_ex(ctx, NULL, NULL, libCtx, NULL, pkey, NULL) != 1;
653+
}
654+
if (err == 0) {
655+
err = EVP_DigestSignInit_ex(ctx, NULL, NULL, libCtx, NULL, NULL, NULL) != 1;
656+
}
657+
658+
EVP_PKEY_free(pkey);
659+
EVP_MD_free(md);
660+
EVP_MD_CTX_free(ctx);
661+
662+
return err;
663+
}
664+
665+
int test_ecx_null_init(void* data)
666+
{
667+
int err = 0;
668+
(void)data;
669+
670+
err = test_ecx_null_sign_init_ex(osslLibCtx);
671+
if (err == 0) {
672+
err = test_ecx_null_sign_init_ex(wpLibCtx);
673+
}
674+
675+
return err;
676+
}
677+
629678
#endif /* defined(WP_HAVE_ED25519) || defined(WP_HAVE_ECD444) */

test/test_rsa.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,4 +1615,48 @@ int test_rsa_decode(void* data)
16151615
return err;
16161616
}
16171617

1618+
static int test_rsa_null_sign_init_ex(OSSL_LIB_CTX *libCtx)
1619+
{
1620+
int err = 0;
1621+
EVP_MD_CTX *ctx = NULL;
1622+
EVP_MD *md = NULL;
1623+
EVP_PKEY *pkey = NULL;
1624+
const unsigned char *p = rsa_key_der_2048;
1625+
1626+
err = (ctx = EVP_MD_CTX_new()) == NULL;
1627+
if (err == 0) {
1628+
md = EVP_MD_fetch(libCtx, "SHA256", NULL);
1629+
err = md == NULL;
1630+
}
1631+
if (err == 0) {
1632+
pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, sizeof(rsa_key_der_2048));
1633+
err = pkey == NULL;
1634+
}
1635+
if (err == 0) {
1636+
err = EVP_DigestSignInit_ex(ctx, NULL, "SHA256", libCtx, NULL, pkey, NULL) != 1;
1637+
}
1638+
if (err == 0) {
1639+
err = EVP_DigestSignInit_ex(ctx, NULL, "SHA256", libCtx, NULL, NULL, NULL) != 1;
1640+
}
1641+
1642+
EVP_PKEY_free(pkey);
1643+
EVP_MD_free(md);
1644+
EVP_MD_CTX_free(ctx);
1645+
1646+
return err;
1647+
}
1648+
1649+
int test_rsa_null_init(void* data)
1650+
{
1651+
int err = 0;
1652+
(void)data;
1653+
1654+
err = test_rsa_null_sign_init_ex(osslLibCtx);
1655+
if (err == 0) {
1656+
err = test_rsa_null_sign_init_ex(wpLibCtx);
1657+
}
1658+
1659+
return err;
1660+
}
1661+
16181662
#endif /* WP_HAVE_RSA */

test/unit.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ TEST_CASE test_case[] = {
174174
TEST_DECL(test_rsa_load_cert, NULL),
175175
TEST_DECL(test_rsa_fromdata, NULL),
176176
TEST_DECL(test_rsa_decode, NULL),
177+
TEST_DECL(test_rsa_null_init, NULL),
177178
#endif /* WP_HAVE_RSA */
178179
#ifdef WP_HAVE_EC_P192
179180
#ifdef WP_HAVE_ECKEYGEN
@@ -221,6 +222,7 @@ TEST_CASE test_case[] = {
221222
#endif
222223
TEST_DECL(test_ec_decode, NULL),
223224
TEST_DECL(test_ec_import, NULL),
225+
TEST_DECL(test_ec_null_init, NULL),
224226
#endif
225227
#ifdef WP_HAVE_EC_P384
226228
#ifdef WP_HAVE_ECKEYGEN
@@ -290,6 +292,7 @@ TEST_CASE test_case[] = {
290292
TEST_DECL(test_ecx_sign_verify_raw_priv, NULL),
291293
TEST_DECL(test_ecx_sign_verify_raw_pub, NULL),
292294
TEST_DECL(test_ecx_misc, NULL),
295+
TEST_DECL(test_ecx_null_init, NULL),
293296
#endif
294297
};
295298
#define TEST_CASE_CNT (int)(sizeof(test_case) / sizeof(*test_case))

test/unit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ int test_rsa_load_key(void* data);
248248
int test_rsa_load_cert(void* data);
249249
int test_rsa_fromdata(void* data);
250250
int test_rsa_decode(void* data);
251+
int test_rsa_null_init(void* data);
251252
#endif /* WP_HAVE_RSA */
252253

253254
#ifdef WP_HAVE_DH
@@ -375,6 +376,7 @@ int test_ec_load_cert(void* data);
375376

376377
int test_ec_decode(void* data);
377378
int test_ec_import(void* data);
379+
int test_ec_null_init(void* data);
378380

379381
#endif /* WP_HAVE_ECC */
380382

@@ -387,6 +389,7 @@ int test_ecx_sign_verify(void *data);
387389
int test_ecx_sign_verify_raw_priv(void *data);
388390
int test_ecx_sign_verify_raw_pub(void *data);
389391
int test_ecx_misc(void *data);
392+
int test_ecx_null_init(void *data);
390393
#endif
391394

392395
#endif /* UNIT_H */

0 commit comments

Comments
 (0)