Skip to content

Commit 3b74a64

Browse files
authored
Merge pull request #7791 from aidangarske/privkeytoder_fix2
`api.c` and `asn.c` changes to allow 0 to be passed in and expanded coverage on test cases.
2 parents b1765ca + 55540d0 commit 3b74a64

2 files changed

Lines changed: 33 additions & 18 deletions

File tree

tests/api.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23718,7 +23718,11 @@ static int test_wc_Ed25519PublicKeyToDer(void)
2371823718
ExpectIntEQ(wc_ed25519_init(&key), 0);
2371923719
ExpectIntEQ(wc_InitRng(&rng), 0);
2372023720
ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
23721-
ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
23721+
/* length only */
23722+
ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, NULL, 0, 0), 0);
23723+
ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, NULL, 0, 1), 0);
23724+
ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf,
23725+
(word32)sizeof(derBuf), 1), 0);
2372223726

2372323727
DoExpectIntEQ(wc_FreeRng(&rng), 0);
2372423728
wc_ed25519_free(&key);
@@ -24611,8 +24615,11 @@ static int test_wc_Ed448PublicKeyToDer(void)
2461124615
ExpectIntEQ(wc_ed448_init(&key), 0);
2461224616
ExpectIntEQ(wc_InitRng(&rng), 0);
2461324617
ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
24614-
24615-
ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
24618+
/* length only */
24619+
ExpectIntGT(wc_Ed448PublicKeyToDer(&key, NULL, 0, 0), 0);
24620+
ExpectIntGT(wc_Ed448PublicKeyToDer(&key, NULL, 0, 1), 0);
24621+
ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf,
24622+
(word32)sizeof(derBuf), 1), 0);
2461624623

2461724624
DoExpectIntEQ(wc_FreeRng(&rng), 0);
2461824625
wc_ed448_free(&key);
@@ -27238,9 +27245,10 @@ static int test_wc_Ed25519KeyToDer(void)
2723827245
/* Bad Cases */
2723927246
ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
2724027247
ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
27241-
ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
27248+
ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BUFFER_E);
2724227249
/* Good Cases */
2724327250
/* length only */
27251+
ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, 0), 0);
2724427252
ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
2724527253
ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
2724627254

@@ -27276,10 +27284,10 @@ static int test_wc_Ed25519PrivateKeyToDer(void)
2727627284
ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
2727727285
ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
2727827286
ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
27279-
BAD_FUNC_ARG);
27287+
BUFFER_E);
2728027288
/* Good Cases */
2728127289
/* length only */
27282-
ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
27290+
ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, 0), 0);
2728327291
ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
2728427292

2728527293
DoExpectIntEQ(wc_FreeRng(&rng), 0);
@@ -27312,10 +27320,10 @@ static int test_wc_Ed448KeyToDer(void)
2731227320
/* Bad Cases */
2731327321
ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
2731427322
ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
27315-
ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
27323+
ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BUFFER_E);
2731627324
/* Good Cases */
2731727325
/* length only */
27318-
ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
27326+
ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, 0), 0);
2731927327
ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
2732027328

2732127329
DoExpectIntEQ(wc_FreeRng(&rng), 0);
@@ -27350,10 +27358,10 @@ static int test_wc_Ed448PrivateKeyToDer(void)
2735027358
ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
2735127359
ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
2735227360
ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
27353-
BAD_FUNC_ARG);
27361+
BUFFER_E);
2735427362
/* Good cases */
2735527363
/* length only */
27356-
ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
27364+
ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, 0), 0);
2735727365
ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
2735827366

2735927367
DoExpectIntEQ(wc_FreeRng(&rng), 0);
@@ -27388,10 +27396,10 @@ static int test_wc_Curve448PrivateKeyToDer(void)
2738827396
ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
2738927397
ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
2739027398
ExpectIntEQ(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, 0),
27391-
BAD_FUNC_ARG);
27399+
BUFFER_E);
2739227400
/* Good cases */
2739327401
/* length only */
27394-
ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, NULL, inLen), 0);
27402+
ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, NULL, 0), 0);
2739527403
ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, inLen), 0);
2739627404

2739727405
/* Bad Cases */
@@ -27403,8 +27411,8 @@ static int test_wc_Curve448PrivateKeyToDer(void)
2740327411
BUFFER_E);
2740427412
/* Good cases */
2740527413
/* length only */
27406-
ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 0), 0);
27407-
ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 1), 0);
27414+
ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, 0, 0), 0);
27415+
ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, 0, 1), 0);
2740827416
ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 0), 0);
2740927417
ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 1), 0);
2741027418

wolfcrypt/src/asn.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12012,9 +12012,13 @@ int SetAsymKeyDerPublic(const byte* pubKey, word32 pubKeyLen,
1201212012
DECL_ASNSETDATA(dataASN, edPubKeyASN_Length);
1201312013
#endif
1201412014

12015-
if (pubKey == NULL) {
12015+
/* validate parameters */
12016+
if (pubKey == NULL){
1201612017
return BAD_FUNC_ARG;
1201712018
}
12019+
if (output != NULL && outLen == 0) {
12020+
return BUFFER_E;
12021+
}
1201812022

1201912023
#ifndef WOLFSSL_ASN_TEMPLATE
1202012024
/* calculate size */
@@ -35370,10 +35374,13 @@ int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
3537035374
int sz;
3537135375
#endif
3537235376

35373-
/* Validate parameters. */
35374-
if (privKey == NULL || outLen == 0) {
35377+
/* validate parameters */
35378+
if (privKey == NULL) {
3537535379
return BAD_FUNC_ARG;
3537635380
}
35381+
if (output != NULL && outLen == 0) {
35382+
return BUFFER_E;
35383+
}
3537735384

3537835385
#ifndef WOLFSSL_ASN_TEMPLATE
3537935386
/* calculate size */
@@ -35531,7 +35538,7 @@ int wc_Curve25519PublicKeyToDer(curve25519_key* key, byte* output, word32 inLen,
3553135538
byte pubKey[CURVE25519_PUB_KEY_SIZE];
3553235539
word32 pubKeyLen = (word32)sizeof(pubKey);
3553335540

35534-
if (key == NULL || output == NULL) {
35541+
if (key == NULL) {
3553535542
return BAD_FUNC_ARG;
3553635543
}
3553735544

0 commit comments

Comments
 (0)