Skip to content

Commit 462bab4

Browse files
authored
Merge pull request #221 from ColtonWilley/wp_ecc_pubkey_validate
Fix ECC public key validation
2 parents 9b20510 + ce2e041 commit 462bab4

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/wp_ecc_kmgmt.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -980,18 +980,18 @@ static int wp_ecc_validate_public_key_quick(const wp_Ecc* ecc)
980980
static int wp_ecc_validate(const wp_Ecc* ecc, int selection, int checkType)
981981
{
982982
int ok = 1;
983-
int privDone = 0;
983+
int origType;
984984
int rc;
985985

986986
/* Only named curves supported. */
987987
if (((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) &&
988988
(ecc->curveId == 0)) {
989989
ok = 0;
990990
}
991-
if (((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) && (!ecc->hasPub)) {
991+
if (ok && ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) && (!ecc->hasPub)) {
992992
ok = 0;
993993
}
994-
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
994+
if (ok && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
995995
#if LIBWOLFSSL_VERSION_HEX >= 0x05000000
996996
/* TODO: Quick check for older versions? */
997997
if (checkType == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK) {
@@ -1004,18 +1004,23 @@ static int wp_ecc_validate(const wp_Ecc* ecc, int selection, int checkType)
10041004
(void)checkType;
10051005
#endif
10061006
{
1007-
privDone = 1;
1007+
/* We may have a private key inside that does not match the public
1008+
* key that has been set, which is OK. Override the internal type
1009+
* to force a public key only check */
1010+
origType = ecc->key.type;
1011+
((wp_Ecc*)ecc)->key.type = ECC_PUBLICKEY;
10081012
rc = wc_ecc_check_key((ecc_key*)&ecc->key);
1013+
((wp_Ecc*)ecc)->key.type = origType;
10091014
if (rc != 0) {
10101015
ok = 0;
10111016
}
10121017
}
10131018
}
1014-
if (((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) &&
1019+
if (ok && ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) &&
10151020
(!ecc->hasPriv)) {
10161021
ok = 0;
10171022
}
1018-
if (((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) && (!privDone)) {
1023+
if ((ok && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)) {
10191024
rc = wc_ecc_check_key((ecc_key*)&ecc->key);
10201025
if (rc != 0) {
10211026
ok = 0;

0 commit comments

Comments
 (0)