From d8c53534662367e6264cf92998fc8a724c40d18d Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Thu, 16 Jan 2020 13:21:14 -0700 Subject: [PATCH 1/5] adjust set1 curves list function for TLS extension sent --- src/ssl.c | 30 ++++++++++++++++++++++++++---- wolfssl/test.h | 4 ++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/ssl.c b/src/ssl.c index a04a10bbf..74efe13ea 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -44376,7 +44376,7 @@ void wolfSSL_get0_next_proto_negotiated(const WOLFSSL *s, const unsigned char ** int wolfSSL_CTX_set1_curves_list(WOLFSSL_CTX* ctx, const char* names) { int idx, start = 0, len; - int curve; + word16 curve; char name[MAX_CURVE_NAME_SZ]; /* Disable all curves so that only the ones the user wants are enabled. */ @@ -44405,13 +44405,35 @@ int wolfSSL_CTX_set1_curves_list(WOLFSSL_CTX* ctx, const char* names) (XSTRNCMP(name, "P-521", len) == 0)) { curve = WOLFSSL_ECC_SECP521R1; } - else if (XSTRNCMP(name, "X25519", len) == 0) + else if (XSTRNCMP(name, "X25519", len) == 0) { curve = WOLFSSL_ECC_X25519; - else if ((curve = wc_ecc_get_curve_id_from_name(name)) < 0) + } + else { + int ret = wc_ecc_get_curve_id_from_name(name); + if (ret < 0) { + return WOLFSSL_FAILURE; + } + curve = (word16)ret; + } + + if (curve > (sizeof(word32) * WOLFSSL_BIT_SIZE)) { + /* shift left more than size of ctx->disabledCurves causes static + * analysis report */ + WOLFSSL_MSG("curve value is too large for upcoming shift"); return WOLFSSL_FAILURE; + } + + #ifndef NO_WOLFSSL_CLIENT + /* set the supported curve so client TLS extension contains only the + * desired curves */ + if (wolfSSL_CTX_UseSupportedCurve(ctx, curve) != WOLFSSL_SUCCESS) { + WOLFSSL_MSG("Unable to set supported curve"); + return WOLFSSL_FAILURE; + } + #endif /* Switch the bit to off and therefore is enabled. */ - ctx->disabledCurves &= ~(1 << curve); + ctx->disabledCurves &= ~(1U << curve); start = idx + 1; } diff --git a/wolfssl/test.h b/wolfssl/test.h index 221899a41..d48e30327 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -489,8 +489,8 @@ static WC_INLINE int mygetopt(int argc, char** argv, const char* optstring) if (myoptind == 0) myoptind++; - if (myoptind >= argc || argv[myoptind][0] != '-' || - argv[myoptind][1] == '\0') { + if (myoptind >= argc || argv[myoptind] == NULL || + argv[myoptind][0] != '-' || argv[myoptind][1] == '\0') { myoptarg = NULL; if (myoptind < argc) myoptarg = argv[myoptind]; From bd4a9c69ddcb6cc7a29acf7b31d4a232ab314022 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Fri, 17 Jan 2020 11:56:46 -0700 Subject: [PATCH 2/5] convert name to oidsum to curve type for setting supported curves --- src/internal.c | 22 +++++++++++++--------- src/ssl.c | 15 +++++++++++++-- wolfssl/internal.h | 1 + 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/internal.c b/src/internal.c index b31524025..02e61f4ec 100644 --- a/src/internal.c +++ b/src/internal.c @@ -23272,15 +23272,9 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx, #if defined(HAVE_ECC) - - static byte SetCurveId(ecc_key* key) - { - if (key == NULL || key->dp == NULL) { - WOLFSSL_MSG("SetCurveId: Invalid key!"); - return 0; - } - - switch(key->dp->oidSum) { + /* returns the WOLFSSL_* version of the curve from the OID sum */ + unsigned char GetCurveByOID(int oidSum) { + switch(oidSum) { #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES) #ifndef NO_ECC_SECP case ECC_SECP160R1_OID: @@ -23356,6 +23350,16 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx, } } + static byte SetCurveId(ecc_key* key) + { + if (key == NULL || key->dp == NULL) { + WOLFSSL_MSG("SetCurveId: Invalid key!"); + return 0; + } + + return (byte)GetCurveByOID(key->dp->oidSum); + } + #endif /* HAVE_ECC || HAVE_CURVE25519 */ typedef struct SskeArgs { diff --git a/src/ssl.c b/src/ssl.c index 74efe13ea..5b16fdb84 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -44409,11 +44409,22 @@ int wolfSSL_CTX_set1_curves_list(WOLFSSL_CTX* ctx, const char* names) curve = WOLFSSL_ECC_X25519; } else { - int ret = wc_ecc_get_curve_id_from_name(name); + int ret; + const ecc_set_type *eccSet; + + ret = wc_ecc_get_curve_idx_from_name(name); if (ret < 0) { + WOLFSSL_MSG("Could not find name in set"); return WOLFSSL_FAILURE; } - curve = (word16)ret; + + eccSet = wc_ecc_get_curve_params(ret); + if (eccSet == NULL) { + WOLFSSL_MSG("NULL set returned"); + return WOLFSSL_FAILURE; + } + + curve = GetCurveByOID(eccSet->oidSum); } if (curve > (sizeof(word32) * WOLFSSL_BIT_SIZE)) { diff --git a/wolfssl/internal.h b/wolfssl/internal.h index 2b00a3749..d50fa52ba 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -4465,6 +4465,7 @@ WOLFSSL_LOCAL int SetKeysSide(WOLFSSL*, enum encrypt_side); #ifdef HAVE_ECC WOLFSSL_LOCAL int EccMakeKey(WOLFSSL* ssl, ecc_key* key, ecc_key* peer); + WOLFSSL_LOCAL unsigned char GetCurveByOID(int oidSum); #endif WOLFSSL_LOCAL int InitHandshakeHashes(WOLFSSL* ssl); From c5932a9874729a71ba1b39a5743d25c555b128a7 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Fri, 17 Jan 2020 13:32:59 -0700 Subject: [PATCH 3/5] account for leantls and selftest builds --- src/internal.c | 158 +++++++++++++++++++++++++------------------------ src/ssl.c | 5 ++ 2 files changed, 86 insertions(+), 77 deletions(-) diff --git a/src/internal.c b/src/internal.c index 02e61f4ec..293f41405 100644 --- a/src/internal.c +++ b/src/internal.c @@ -23036,6 +23036,87 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx, #endif /* NO_WOLFSSL_CLIENT */ +#ifdef HAVE_ECC + /* returns the WOLFSSL_* version of the curve from the OID sum */ + unsigned char GetCurveByOID(int oidSum) { + switch(oidSum) { + #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES) + #ifndef NO_ECC_SECP + case ECC_SECP160R1_OID: + return WOLFSSL_ECC_SECP160R1; + #endif /* !NO_ECC_SECP */ + #ifdef HAVE_ECC_SECPR2 + case ECC_SECP160R2_OID: + return WOLFSSL_ECC_SECP160R2; + #endif /* HAVE_ECC_SECPR2 */ + #ifdef HAVE_ECC_KOBLITZ + case ECC_SECP160K1_OID: + return WOLFSSL_ECC_SECP160K1; + #endif /* HAVE_ECC_KOBLITZ */ + #endif + #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES) + #ifndef NO_ECC_SECP + case ECC_SECP192R1_OID: + return WOLFSSL_ECC_SECP192R1; + #endif /* !NO_ECC_SECP */ + #ifdef HAVE_ECC_KOBLITZ + case ECC_SECP192K1_OID: + return WOLFSSL_ECC_SECP192K1; + #endif /* HAVE_ECC_KOBLITZ */ + #endif + #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES) + #ifndef NO_ECC_SECP + case ECC_SECP224R1_OID: + return WOLFSSL_ECC_SECP224R1; + #endif /* !NO_ECC_SECP */ + #ifdef HAVE_ECC_KOBLITZ + case ECC_SECP224K1_OID: + return WOLFSSL_ECC_SECP224K1; + #endif /* HAVE_ECC_KOBLITZ */ + #endif + #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES) + #ifndef NO_ECC_SECP + case ECC_SECP256R1_OID: + return WOLFSSL_ECC_SECP256R1; + #endif /* !NO_ECC_SECP */ + #ifdef HAVE_ECC_KOBLITZ + case ECC_SECP256K1_OID: + return WOLFSSL_ECC_SECP256K1; + #endif /* HAVE_ECC_KOBLITZ */ + #ifdef HAVE_ECC_BRAINPOOL + case ECC_BRAINPOOLP256R1_OID: + return WOLFSSL_ECC_BRAINPOOLP256R1; + #endif /* HAVE_ECC_BRAINPOOL */ + #endif + #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES) + #ifndef NO_ECC_SECP + case ECC_SECP384R1_OID: + return WOLFSSL_ECC_SECP384R1; + #endif /* !NO_ECC_SECP */ + #ifdef HAVE_ECC_BRAINPOOL + case ECC_BRAINPOOLP384R1_OID: + return WOLFSSL_ECC_BRAINPOOLP384R1; + #endif /* HAVE_ECC_BRAINPOOL */ + #endif + #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES) + #ifdef HAVE_ECC_BRAINPOOL + case ECC_BRAINPOOLP512R1_OID: + return WOLFSSL_ECC_BRAINPOOLP512R1; + #endif /* HAVE_ECC_BRAINPOOL */ + #endif + #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES) + #ifndef NO_ECC_SECP + case ECC_SECP521R1_OID: + return WOLFSSL_ECC_SECP521R1; + #endif /* !NO_ECC_SECP */ + #endif + default: + return 0; + } + } +#endif /* HAVE_ECC */ + + #ifndef NO_WOLFSSL_SERVER #ifndef WOLFSSL_NO_TLS12 @@ -23272,83 +23353,6 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx, #if defined(HAVE_ECC) - /* returns the WOLFSSL_* version of the curve from the OID sum */ - unsigned char GetCurveByOID(int oidSum) { - switch(oidSum) { - #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES) - #ifndef NO_ECC_SECP - case ECC_SECP160R1_OID: - return WOLFSSL_ECC_SECP160R1; - #endif /* !NO_ECC_SECP */ - #ifdef HAVE_ECC_SECPR2 - case ECC_SECP160R2_OID: - return WOLFSSL_ECC_SECP160R2; - #endif /* HAVE_ECC_SECPR2 */ - #ifdef HAVE_ECC_KOBLITZ - case ECC_SECP160K1_OID: - return WOLFSSL_ECC_SECP160K1; - #endif /* HAVE_ECC_KOBLITZ */ - #endif - #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES) - #ifndef NO_ECC_SECP - case ECC_SECP192R1_OID: - return WOLFSSL_ECC_SECP192R1; - #endif /* !NO_ECC_SECP */ - #ifdef HAVE_ECC_KOBLITZ - case ECC_SECP192K1_OID: - return WOLFSSL_ECC_SECP192K1; - #endif /* HAVE_ECC_KOBLITZ */ - #endif - #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES) - #ifndef NO_ECC_SECP - case ECC_SECP224R1_OID: - return WOLFSSL_ECC_SECP224R1; - #endif /* !NO_ECC_SECP */ - #ifdef HAVE_ECC_KOBLITZ - case ECC_SECP224K1_OID: - return WOLFSSL_ECC_SECP224K1; - #endif /* HAVE_ECC_KOBLITZ */ - #endif - #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES) - #ifndef NO_ECC_SECP - case ECC_SECP256R1_OID: - return WOLFSSL_ECC_SECP256R1; - #endif /* !NO_ECC_SECP */ - #ifdef HAVE_ECC_KOBLITZ - case ECC_SECP256K1_OID: - return WOLFSSL_ECC_SECP256K1; - #endif /* HAVE_ECC_KOBLITZ */ - #ifdef HAVE_ECC_BRAINPOOL - case ECC_BRAINPOOLP256R1_OID: - return WOLFSSL_ECC_BRAINPOOLP256R1; - #endif /* HAVE_ECC_BRAINPOOL */ - #endif - #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES) - #ifndef NO_ECC_SECP - case ECC_SECP384R1_OID: - return WOLFSSL_ECC_SECP384R1; - #endif /* !NO_ECC_SECP */ - #ifdef HAVE_ECC_BRAINPOOL - case ECC_BRAINPOOLP384R1_OID: - return WOLFSSL_ECC_BRAINPOOLP384R1; - #endif /* HAVE_ECC_BRAINPOOL */ - #endif - #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES) - #ifdef HAVE_ECC_BRAINPOOL - case ECC_BRAINPOOLP512R1_OID: - return WOLFSSL_ECC_BRAINPOOLP512R1; - #endif /* HAVE_ECC_BRAINPOOL */ - #endif - #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES) - #ifndef NO_ECC_SECP - case ECC_SECP521R1_OID: - return WOLFSSL_ECC_SECP521R1; - #endif /* !NO_ECC_SECP */ - #endif - default: - return 0; - } - } static byte SetCurveId(ecc_key* key) { diff --git a/src/ssl.c b/src/ssl.c index 5b16fdb84..9a6557b11 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -44409,6 +44409,7 @@ int wolfSSL_CTX_set1_curves_list(WOLFSSL_CTX* ctx, const char* names) curve = WOLFSSL_ECC_X25519; } else { + #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) int ret; const ecc_set_type *eccSet; @@ -44425,6 +44426,10 @@ int wolfSSL_CTX_set1_curves_list(WOLFSSL_CTX* ctx, const char* names) } curve = GetCurveByOID(eccSet->oidSum); + #else + WOLFSSL_MSG("API not present to search farther using name"); + return WOLFSSL_FAILURE + #endif } if (curve > (sizeof(word32) * WOLFSSL_BIT_SIZE)) { From 356636e88df2aa663b0d1f8b981df12b7c716764 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Fri, 17 Jan 2020 15:13:52 -0700 Subject: [PATCH 4/5] fix typo --- src/ssl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ssl.c b/src/ssl.c index 9a6557b11..9786acdd4 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -44428,7 +44428,7 @@ int wolfSSL_CTX_set1_curves_list(WOLFSSL_CTX* ctx, const char* names) curve = GetCurveByOID(eccSet->oidSum); #else WOLFSSL_MSG("API not present to search farther using name"); - return WOLFSSL_FAILURE + return WOLFSSL_FAILURE; #endif } From c581c56999071c3fb971327878b6f2e2ac96de7e Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Mon, 20 Jan 2020 10:40:56 -0700 Subject: [PATCH 5/5] update return value of local GetCurveByOID --- src/internal.c | 3 ++- wolfssl/internal.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/internal.c b/src/internal.c index 293f41405..624b4c765 100644 --- a/src/internal.c +++ b/src/internal.c @@ -23038,7 +23038,7 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx, #ifdef HAVE_ECC /* returns the WOLFSSL_* version of the curve from the OID sum */ - unsigned char GetCurveByOID(int oidSum) { + word16 GetCurveByOID(int oidSum) { switch(oidSum) { #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES) #ifndef NO_ECC_SECP @@ -23111,6 +23111,7 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx, #endif /* !NO_ECC_SECP */ #endif default: + WOLFSSL_MSG("Curve OID not compiled in or implemented"); return 0; } } diff --git a/wolfssl/internal.h b/wolfssl/internal.h index d50fa52ba..34a59badb 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -4465,7 +4465,7 @@ WOLFSSL_LOCAL int SetKeysSide(WOLFSSL*, enum encrypt_side); #ifdef HAVE_ECC WOLFSSL_LOCAL int EccMakeKey(WOLFSSL* ssl, ecc_key* key, ecc_key* peer); - WOLFSSL_LOCAL unsigned char GetCurveByOID(int oidSum); + WOLFSSL_LOCAL word16 GetCurveByOID(int oidSum); #endif WOLFSSL_LOCAL int InitHandshakeHashes(WOLFSSL* ssl);