From 484ee716d320fa16d3c703674f8a50a2fb9e37fa Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Fri, 30 Oct 2026 09:47:08 +0000 Subject: [PATCH 1/6] Leave objects unchanged when version, group and sigalgs setters fail Protocol version setters: - wolfSSL_CTX_set_min_proto_version(), wolfSSL_CTX_set_max_proto_version(), wolfSSL_set_min_proto_version() and wolfSSL_set_max_proto_version() work on a copy of the version limits and store it only on success. A failing call could leave every version disabled, or enable again versions that the application had disabled with options. - The CTX setters record the NO_* options each of them sets and drop only those, so options set by the application are kept and a later call can lower the min or raise the max. The max setter no longer re-applies the min. wolfSSL_CTX_set_options(), wolfSSL_CTX_clear_options() and wolfSSL_CTX_SetMinVersion() update the records. - A call fails when it leaves no version from the min up, counting a min held only in minDowngrade: the default one, one set with wolfSSL_CTX_SetMinVersion() or wolfSSL_SetMinVersion(), or the crypto policy's. In old TLS builds without TLS 1.0, a TLS 1.0 min is TLS 1.1. - A DTLS min on a TLS CTX fails, and so does an SSL max of the other major. - The CTX max setter rebuilds ctx->suites when it changes the method version. - wolfSSL_CTX_SetMinVersion() clears the automatic min flag. - WOLFSSL_MIN_DOWNGRADE is TLS 1.1 in old TLS builds without WOLFSSL_ALLOW_TLSV10. - With WOLFSSL_SYS_CRYPTO_POLICY at level 0, including when no policy is loaded, a new TLS CTX keeps the default min instead of an SSLv3 min. Cipher suites: - The CTX PSK callback setters and wolfSSL_CTX_allow_anon_cipher() rebuild ctx->suites when it was already derived. The max setter no longer leaves the min's options in ctx->mask, so SSLs created after a late PSK callback shared stale CTX suites without PSK. A cipher list set by the application is kept. Groups: - wolfSSL_CTX_set_groups() and wolfSSL_set_groups() build the Supported Groups extension in a local list and replace the old one only on success. They move from tls.c to ssl_api_ext.c. - set_curves_list() sets the groups once, after parsing. - wolfSSL_UseSupportedCurve() and wolfSSL_CTX_UseSupportedCurve() set userCurves only when the group was added. Signature algorithms: - SetSuitesHashSigAlgo() parses into a local buffer and copies it only when the whole list parses. - wolfSSL_CTX_set1_sigalgs_list() on a CTX without suites frees the suites it allocated when the list is rejected, and adds the default cipher suites when it is accepted. --- src/internal.c | 14 +- src/ssl.c | 636 +++++++++++++--------------- src/ssl_api_ext.c | 153 ++++++- src/tls.c | 96 ----- src/tls13.c | 12 + tests/api.c | 1004 +++++++++++++++++++++++++++++++++++++++++++- wolfssl/internal.h | 8 +- 7 files changed, 1467 insertions(+), 456 deletions(-) diff --git a/src/internal.c b/src/internal.c index 5a8af6ee8e1..da54b73833d 100644 --- a/src/internal.c +++ b/src/internal.c @@ -3032,7 +3032,7 @@ int wolfSSL_crypto_policy_init_ctx(WOLFSSL_CTX * ctx, break; case 0: default: - ctx->minDowngrade = WOLFSSL_MIN_DOWNGRADE; + minDowngrade = WOLFSSL_MIN_DOWNGRADE; break; } } @@ -3174,7 +3174,6 @@ int InitSSL_Ctx(WOLFSSL_CTX* ctx, WOLFSSL_METHOD* method, void* heap) else #endif /* WOLFSSL_DTLS */ { - /* current default: TLSv1_MINOR */ ctx->minDowngrade = WOLFSSL_MIN_DOWNGRADE; } @@ -35311,7 +35310,7 @@ static byte GetSigAlgFromName(const char* name, int len) * * suites [in,out] Cipher suites and signature algorithms. * list [in] String representing hash/signature algorithms to set. - * returns 0 on failure. + * returns 0 on failure, leaving suites unchanged. * 1 on success. */ int SetSuitesHashSigAlgo(Suites* suites, const char* list) @@ -35321,9 +35320,7 @@ int SetSuitesHashSigAlgo(Suites* suites, const char* list) const char* s = list; byte sig_alg = 0; byte mac_alg = no_mac; - - /* Setting is destructive on error. */ - suites->hashSigAlgoSz = 0; + byte hashSigAlgo[WOLFSSL_MAX_SIGALGO]; do { if (*list == '+') { @@ -35363,7 +35360,7 @@ int SetSuitesHashSigAlgo(Suites* suites, const char* list) break; } } - AddSuiteHashSigAlgo(suites->hashSigAlgo, mac_alg, sig_alg, 0, &idx); + AddSuiteHashSigAlgo(hashSigAlgo, mac_alg, sig_alg, 0, &idx); sig_alg = 0; mac_alg = no_mac; s = list + 1; @@ -35376,7 +35373,8 @@ int SetSuitesHashSigAlgo(Suites* suites, const char* list) if (s != list && (sig_alg != 0 || mac_alg != 0)) { ret = 0; } - else { + if (ret == 1) { + XMEMCPY(suites->hashSigAlgo, hashSigAlgo, idx); suites->hashSigAlgoSz = idx; } diff --git a/src/ssl.c b/src/ssl.c index 6dd0b4c1685..289e62d21cd 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -2243,8 +2243,15 @@ int wolfSSL_CTX_SetMinVersion(WOLFSSL_CTX* ctx, int version) #endif /* WOLFSSL_SYS_CRYPTO_POLICY */ ret = SetMinVersionHelper(&ctx->minDowngrade, version); - if (ret == WOLFSSL_SUCCESS) + if (ret == WOLFSSL_SUCCESS) { ctx->minVersionSet = 1; + ctx->minProto = 0; /* no longer the automatic minimum */ + #ifdef OPENSSL_EXTRA + /* replaces a min set with wolfSSL_CTX_set_min_proto_version() */ + ctx->mask &= ~ctx->minProtoMask; + ctx->minProtoMask = 0; + #endif + } return ret; } @@ -4078,6 +4085,10 @@ int wolfSSL_set_compression(WOLFSSL* ssl) ctx->havePSK = 1; ctx->client_psk_cb = cb; + if (ctx->suites != NULL) { + /* add PSK to the suites derived before */ + InitSSL_CTX_Suites(ctx, ctx->suites); + } } void wolfSSL_set_psk_client_callback(WOLFSSL* ssl,wc_psk_client_callback cb) @@ -4156,6 +4167,10 @@ int wolfSSL_set_compression(WOLFSSL* ssl) return; ctx->havePSK = 1; ctx->server_psk_cb = cb; + if (ctx->suites != NULL) { + /* add PSK to the suites derived before */ + InitSSL_CTX_Suites(ctx, ctx->suites); + } } void wolfSSL_set_psk_server_callback(WOLFSSL* ssl,wc_psk_server_callback cb) @@ -4273,6 +4288,10 @@ int wolfSSL_set_compression(WOLFSSL* ssl) return WOLFSSL_FAILURE; ctx->useAnon = 1; + if (ctx->suites != NULL) { + /* add anon to the suites derived before */ + InitSSL_CTX_Suites(ctx, ctx->suites); + } return WOLFSSL_SUCCESS; } @@ -4704,6 +4723,11 @@ int wolfSSL_set_compression(WOLFSSL* ssl) return BAD_FUNC_ARG; ctx->mask = (unsigned long)wolf_set_options((long)ctx->mask, opt); +#ifdef OPENSSL_EXTRA + /* Versions disabled here belong to the application */ + ctx->minProtoMask &= ~(unsigned long)opt; + ctx->maxProtoMask &= ~(unsigned long)opt; +#endif #if defined(HAVE_SESSION_TICKET) && (defined(OPENSSL_EXTRA) \ || defined(HAVE_WEBSERVER) || defined(WOLFSSL_WPAS_SMALL)) if ((ctx->mask & WOLFSSL_OP_NO_TICKET) == WOLFSSL_OP_NO_TICKET) { @@ -4728,6 +4752,10 @@ int wolfSSL_set_compression(WOLFSSL* ssl) if(ctx == NULL) return BAD_FUNC_ARG; ctx->mask &= (unsigned long)~opt; +#ifdef OPENSSL_EXTRA + ctx->minProtoMask &= (unsigned long)~opt; + ctx->maxProtoMask &= (unsigned long)~opt; +#endif return (long)ctx->mask; } @@ -5059,72 +5087,155 @@ static const int protoVerTbl[] = { /* number of protocol versions listed in protoVerTbl */ #define NUMBER_OF_PROTOCOLS (sizeof(protoVerTbl)/sizeof(int)) -/** - * wolfSSL_CTX_set_min_proto_version attempts to set the minimum protocol - * version to use by SSL objects created from this WOLFSSL_CTX. - * This API guarantees that a version of SSL/TLS lower than specified - * here will not be allowed. If the version specified is not compiled in - * then this API sets the lowest compiled in protocol version. - * This API also accept 0 as version, to set the minimum version automatically. - * CheckSslMethodVersion() is called to check if any remaining protocol versions - * are enabled. - * @param ctx The wolfSSL CONTEXT factory for spawning SSL/TLS objects - * @param version Any of the following - * * 0 - * * SSL3_VERSION - * * TLS1_VERSION - * * TLS1_1_VERSION - * * TLS1_2_VERSION - * * TLS1_3_VERSION - * * DTLS1_VERSION - * * DTLS1_2_VERSION - * @return WOLFSSL_SUCCESS on valid settings and WOLFSSL_FAILURE when no - * protocol versions are left enabled. - */ -static int Set_CTX_min_proto_version(WOLFSSL_CTX* ctx, int version) +static int GetMaxProtoVersion(long options); + +/* WOLFSSL_OP_NO_* option of each TLS version, indexed by minor version */ +static const unsigned long tlsVersionOptions[] = { + WOLFSSL_OP_NO_SSLv3, + WOLFSSL_OP_NO_TLSv1, + WOLFSSL_OP_NO_TLSv1_1, + WOLFSSL_OP_NO_TLSv1_2, + WOLFSSL_OP_NO_TLSv1_3 +}; + +/* NO_* options of the TLS versions below minor */ +static unsigned long tlsOptionsBelow(byte minor) { - WOLFSSL_ENTER("wolfSSL_CTX_set_min_proto_version_ex"); + unsigned long op = 0; + int i; - if (ctx == NULL) { - return WOLFSSL_FAILURE; + for (i = 0; i < minor && i < (int)XELEM_CNT(tlsVersionOptions); i++) { + op |= tlsVersionOptions[i]; } + return op; +} + +/* NO_* options of the TLS versions above minor */ +static unsigned long tlsOptionsAbove(byte minor) +{ + unsigned long op = 0; + int i; + + for (i = minor + 1; i < (int)XELEM_CNT(tlsVersionOptions); i++) { + op |= tlsVersionOptions[i]; + } + return op; +} + +/* The version limits of a CTX or an SSL. The version setters change a copy + * and store it only on success, so a failing call leaves the object + * unchanged. */ +typedef struct ProtoLimits { + unsigned long mask; /* NO_* options */ + unsigned long minProtoMask; /* NO_* options set by the min setter */ + unsigned long maxProtoMask; /* NO_* options set by the max setter */ + byte major; + byte maxMinor; + byte minDowngrade; +} ProtoLimits; + +static void getCtxProtoLimits(const WOLFSSL_CTX* ctx, ProtoLimits* lim) +{ + lim->mask = ctx->mask; + lim->minProtoMask = ctx->minProtoMask; + lim->maxProtoMask = ctx->maxProtoMask; + lim->major = ctx->method->version.major; + lim->maxMinor = ctx->method->version.minor; + lim->minDowngrade = ctx->minDowngrade; +} + +static void setCtxProtoLimits(WOLFSSL_CTX* ctx, const ProtoLimits* lim) +{ + ctx->mask = lim->mask; + ctx->minProtoMask = lim->minProtoMask; + ctx->maxProtoMask = lim->maxProtoMask; + /* the method version is the max */ + ctx->method->version.minor = lim->maxMinor; + ctx->minDowngrade = lim->minDowngrade; +} + +/* An SSL does not record the options its setters set, so they only add + * options. */ +static void getSslProtoLimits(const WOLFSSL* ssl, ProtoLimits* lim) +{ + lim->mask = ssl->options.mask; + lim->minProtoMask = 0; + lim->maxProtoMask = 0; + lim->major = ssl->version.major; + lim->maxMinor = ssl->version.minor; + lim->minDowngrade = ssl->options.minDowngrade; +} + +/* An SSL max is held in the mask only, so the SSL version is kept. */ +static void setSslProtoLimits(WOLFSSL* ssl, const ProtoLimits* lim) +{ + ssl->options.mask = lim->mask; + ssl->options.minDowngrade = lim->minDowngrade; +} + +/* Disables versions for a min or max limit. Only options not already set are + * recorded as the limit's own, so options set by the application are kept. */ +static void disableProto(ProtoLimits* lim, unsigned long* own, + unsigned long op) +{ + *own |= op & ~lim->mask; + lim->mask |= op; +} + +/* Checks that a version is left from the min up. A min held only in + * minDowngrade, such as the default one, has no options in the mask. */ +static int checkProtoLimits(const ProtoLimits* lim) +{ + unsigned long op = lim->mask; + + if (lim->major == SSLv3_MAJOR) + op |= tlsOptionsBelow(lim->minDowngrade); + return CheckSslMethodVersion(lim->major, op); +} + +/* Applies a min version to the limits: sets minDowngrade and disables the TLS + * versions below it. Fails for an unknown version or when no version is left. + */ +static int setMinProtoVersion(ProtoLimits* lim, int version) +{ + WOLFSSL_ENTER("setMinProtoVersion"); switch (version) { #ifndef NO_TLS case SSL3_VERSION: #if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS) - ctx->minDowngrade = SSLv3_MINOR; + lim->minDowngrade = SSLv3_MINOR; break; #endif case TLS1_VERSION: #ifdef WOLFSSL_ALLOW_TLSV10 - ctx->minDowngrade = TLSv1_MINOR; + lim->minDowngrade = TLSv1_MINOR; break; #endif case TLS1_1_VERSION: #ifndef NO_OLD_TLS - ctx->minDowngrade = TLSv1_1_MINOR; + lim->minDowngrade = TLSv1_1_MINOR; break; #endif case TLS1_2_VERSION: #ifndef WOLFSSL_NO_TLS12 - ctx->minDowngrade = TLSv1_2_MINOR; + lim->minDowngrade = TLSv1_2_MINOR; break; #endif case TLS1_3_VERSION: #ifdef WOLFSSL_TLS13 - ctx->minDowngrade = TLSv1_3_MINOR; + lim->minDowngrade = TLSv1_3_MINOR; break; #endif #endif #ifdef WOLFSSL_DTLS case DTLS1_VERSION: #ifndef NO_OLD_TLS - ctx->minDowngrade = DTLS_MINOR; + lim->minDowngrade = DTLS_MINOR; break; #endif case DTLS1_2_VERSION: - ctx->minDowngrade = DTLSv1_2_MINOR; + lim->minDowngrade = DTLSv1_2_MINOR; break; #endif default: @@ -5132,40 +5243,35 @@ static int Set_CTX_min_proto_version(WOLFSSL_CTX* ctx, int version) return WOLFSSL_FAILURE; } - switch (version) { -#ifndef NO_TLS - case TLS1_3_VERSION: - wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2); - FALL_THROUGH; - case TLS1_2_VERSION: - wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_1); - FALL_THROUGH; - case TLS1_1_VERSION: - wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1); - FALL_THROUGH; - case TLS1_VERSION: - wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_SSLv3); - break; - case SSL3_VERSION: - case SSL2_VERSION: - /* Nothing to do here */ - break; -#endif -#ifdef WOLFSSL_DTLS - case DTLS1_VERSION: - case DTLS1_2_VERSION: - break; -#endif - default: - WOLFSSL_MSG("Unrecognized protocol version or not compiled in"); - return WOLFSSL_FAILURE; + if ((version >> 8) == SSLv3_MAJOR) { + disableProto(lim, &lim->minProtoMask, tlsOptionsBelow((byte)version)); } - return CheckSslMethodVersion(ctx->method->version.major, ctx->mask); + return checkProtoLimits(lim); } -/* Sets the min protocol version allowed with WOLFSSL_CTX - * returns WOLFSSL_SUCCESS on success */ +/** + * wolfSSL_CTX_set_min_proto_version attempts to set the minimum protocol + * version to use by SSL objects created from this WOLFSSL_CTX. + * This API guarantees that a version of SSL/TLS lower than specified + * here will not be allowed. If the version specified is not compiled in + * then this API sets the lowest compiled in protocol version. + * This API also accept 0 as version, to set the minimum version automatically. + * CheckSslMethodVersion() is called to check if any remaining protocol versions + * are enabled. + * @param ctx The wolfSSL CONTEXT factory for spawning SSL/TLS objects + * @param version Any of the following + * * 0 + * * SSL3_VERSION + * * TLS1_VERSION + * * TLS1_1_VERSION + * * TLS1_2_VERSION + * * TLS1_3_VERSION + * * DTLS1_VERSION + * * DTLS1_2_VERSION + * @return WOLFSSL_SUCCESS on valid settings and WOLFSSL_FAILURE when no + * protocol versions are left enabled. On failure ctx is unchanged. + */ int wolfSSL_CTX_set_min_proto_version(WOLFSSL_CTX* ctx, int version) { int ret; @@ -5173,16 +5279,28 @@ int wolfSSL_CTX_set_min_proto_version(WOLFSSL_CTX* ctx, int version) int maxProto = 0; int i; int idx = 0; + ProtoLimits lim; WOLFSSL_ENTER("wolfSSL_CTX_set_min_proto_version"); if (ctx == NULL) { return WOLFSSL_FAILURE; } +#ifdef WOLFSSL_DTLS + if ((version >> 8) == DTLS_MAJOR && + ctx->method->version.major != DTLS_MAJOR) { + WOLFSSL_MSG("Mismatched protocol version"); + return WOLFSSL_FAILURE; + } +#endif + + getCtxProtoLimits(ctx, &lim); + /* drop the versions disabled for the previous min */ + lim.mask &= ~lim.minProtoMask; + lim.minProtoMask = 0; if (version != 0) { proto = version; - ctx->minProto = 0; /* turn min proto flag off */ for (i = 0; (unsigned)i < NUMBER_OF_PROTOCOLS; i++) { if (protoVerTbl[i] == version) { break; @@ -5192,32 +5310,31 @@ int wolfSSL_CTX_set_min_proto_version(WOLFSSL_CTX* ctx, int version) else { /* when 0 is specified as version, try to find out the min version */ for (i = 0; (unsigned)i < NUMBER_OF_PROTOCOLS; i++) { - ret = Set_CTX_min_proto_version(ctx, protoVerTbl[i]); + ret = setMinProtoVersion(&lim, protoVerTbl[i]); if (ret == WOLFSSL_SUCCESS) { proto = protoVerTbl[i]; - ctx->minProto = 1; /* turn min proto flag on */ break; } } } - /* check case where max > min , if so then clear the NO_* options - * i is the index into the table for proto version used, see if the max - * proto version index found is smaller */ - maxProto = wolfSSL_CTX_get_max_proto_version(ctx); + /* A min above the max drops the NO_* options set for the max. i is the + * table index of the min and idx the one of the max. */ + maxProto = ctx->maxProto ? 0 : GetMaxProtoVersion((long)lim.mask); for (idx = 0; (unsigned)idx < NUMBER_OF_PROTOCOLS; idx++) { if (protoVerTbl[idx] == maxProto) { break; } } if (idx < i) { - wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_TLSv1 | - WOLFSSL_OP_NO_TLSv1_1 | WOLFSSL_OP_NO_TLSv1_2 | - WOLFSSL_OP_NO_TLSv1_3); + lim.mask &= ~lim.maxProtoMask; + lim.maxProtoMask = 0; } - ret = Set_CTX_min_proto_version(ctx, proto); + ret = setMinProtoVersion(&lim, proto); if (ret == WOLFSSL_SUCCESS) { + setCtxProtoLimits(ctx, &lim); + ctx->minProto = (version == 0); /* Version 0 picks the lowest, so the user set no minimum */ ctx->minVersionSet = (version != 0); } @@ -5225,37 +5342,13 @@ int wolfSSL_CTX_set_min_proto_version(WOLFSSL_CTX* ctx, int version) return ret; } -/** - * wolfSSL_CTX_set_max_proto_version attempts to set the maximum protocol - * version to use by SSL objects created from this WOLFSSL_CTX. - * This API guarantees that a version of SSL/TLS higher than specified - * here will not be allowed. If the version specified is not compiled in - * then this API sets the highest compiled in protocol version. - * This API also accept 0 as version, to set the maximum version automatically. - * CheckSslMethodVersion() is called to check if any remaining protocol versions - * are enabled. - * @param ctx The wolfSSL CONTEXT factory for spawning SSL/TLS objects - * @param ver Any of the following - * * 0 - * * SSL3_VERSION - * * TLS1_VERSION - * * TLS1_1_VERSION - * * TLS1_2_VERSION - * * TLS1_3_VERSION - * * DTLS1_VERSION - * * DTLS1_2_VERSION - * @return WOLFSSL_SUCCESS on valid settings and WOLFSSL_FAILURE when no - * protocol versions are left enabled. - */ -static int Set_CTX_max_proto_version(WOLFSSL_CTX* ctx, int ver) +/* Applies a max version to the limits: disables the TLS versions above it and + * sets maxMinor. Fails for an unknown version, a version of the other major or + * when no version is left. */ +static int setMaxProtoVersion(ProtoLimits* lim, int ver) { int ret; - WOLFSSL_ENTER("Set_CTX_max_proto_version"); - - if (!ctx || !ctx->method) { - WOLFSSL_MSG("Bad parameter"); - return WOLFSSL_FAILURE; - } + WOLFSSL_ENTER("setMaxProtoVersion"); switch (ver) { #ifndef NO_TLS @@ -5265,19 +5358,10 @@ static int Set_CTX_max_proto_version(WOLFSSL_CTX* ctx, int ver) return WOLFSSL_FAILURE; #endif case SSL3_VERSION: - wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1); - FALL_THROUGH; case TLS1_VERSION: - wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_1); - FALL_THROUGH; case TLS1_1_VERSION: - wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2); - FALL_THROUGH; case TLS1_2_VERSION: - wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_3); - FALL_THROUGH; case TLS1_3_VERSION: - /* Nothing to do here */ break; #endif #ifdef WOLFSSL_DTLS @@ -5290,76 +5374,49 @@ static int Set_CTX_max_proto_version(WOLFSSL_CTX* ctx, int ver) return WOLFSSL_FAILURE; } - ret = CheckSslMethodVersion(ctx->method->version.major, ctx->mask); + if ((ver >> 8) == SSLv3_MAJOR) { + disableProto(lim, &lim->maxProtoMask, tlsOptionsAbove((byte)ver)); + } + + ret = checkProtoLimits(lim); if (ret == WOLFSSL_SUCCESS) { - /* Check the major */ - switch (ver) { - #ifndef NO_TLS - case SSL3_VERSION: - case TLS1_VERSION: - case TLS1_1_VERSION: - case TLS1_2_VERSION: - case TLS1_3_VERSION: - if (ctx->method->version.major != SSLv3_MAJOR) { - WOLFSSL_MSG("Mismatched protocol version"); - return WOLFSSL_FAILURE; - } - break; - #endif - #ifdef WOLFSSL_DTLS - case DTLS1_VERSION: - case DTLS1_2_VERSION: - if (ctx->method->version.major != DTLS_MAJOR) { - WOLFSSL_MSG("Mismatched protocol version"); - return WOLFSSL_FAILURE; - } - break; - #endif - } - /* Update the method */ - switch (ver) { - #ifndef NO_TLS - case SSL3_VERSION: - ctx->method->version.minor = SSLv3_MINOR; - break; - case TLS1_VERSION: - ctx->method->version.minor = TLSv1_MINOR; - break; - case TLS1_1_VERSION: - ctx->method->version.minor = TLSv1_1_MINOR; - break; - case TLS1_2_VERSION: - ctx->method->version.minor = TLSv1_2_MINOR; - break; - case TLS1_3_VERSION: - ctx->method->version.minor = TLSv1_3_MINOR; - break; - #endif - #ifdef WOLFSSL_DTLS - case DTLS1_VERSION: - ctx->method->version.minor = DTLS_MINOR; - break; - case DTLS1_2_VERSION: - ctx->method->version.minor = DTLSv1_2_MINOR; - break; - #endif - default: - WOLFSSL_MSG("Unrecognized protocol version or not compiled in"); + if ((ver >> 8) != lim->major) { + WOLFSSL_MSG("Mismatched protocol version"); return WOLFSSL_FAILURE; } + lim->maxMinor = (byte)ver; } return ret; } -/* Sets the max protocol version allowed with WOLFSSL_CTX - * returns WOLFSSL_SUCCESS on success */ +/** + * wolfSSL_CTX_set_max_proto_version attempts to set the maximum protocol + * version to use by SSL objects created from this WOLFSSL_CTX. + * This API guarantees that a version of SSL/TLS higher than specified + * here will not be allowed. If the version specified is not compiled in + * then this API sets the highest compiled in protocol version. + * This API also accept 0 as version, to set the maximum version automatically. + * CheckSslMethodVersion() is called to check if any remaining protocol versions + * are enabled. + * @param ctx The wolfSSL CONTEXT factory for spawning SSL/TLS objects + * @param version Any of the following + * * 0 + * * SSL3_VERSION + * * TLS1_VERSION + * * TLS1_1_VERSION + * * TLS1_2_VERSION + * * TLS1_3_VERSION + * * DTLS1_VERSION + * * DTLS1_2_VERSION + * @return WOLFSSL_SUCCESS on valid settings and WOLFSSL_FAILURE when no + * protocol versions are left enabled. On failure ctx is unchanged. + */ int wolfSSL_CTX_set_max_proto_version(WOLFSSL_CTX* ctx, int version) { int i; int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE); - int minProto; - byte minVersionSet; + ProtoLimits lim; WOLFSSL_ENTER("wolfSSL_CTX_set_max_proto_version"); @@ -5367,140 +5424,66 @@ int wolfSSL_CTX_set_max_proto_version(WOLFSSL_CTX* ctx, int version) return ret; } - /* clear out flags and reset min protocol version */ - minProto = wolfSSL_CTX_get_min_proto_version(ctx); - wolfSSL_CTX_clear_options(ctx, - WOLFSSL_OP_NO_TLSv1 | WOLFSSL_OP_NO_TLSv1_1 | - WOLFSSL_OP_NO_TLSv1_2 | WOLFSSL_OP_NO_TLSv1_3); - minVersionSet = ctx->minVersionSet; - wolfSSL_CTX_set_min_proto_version(ctx, minProto); - ctx->minVersionSet = minVersionSet; /* restoring, not setting, a minimum */ + getCtxProtoLimits(ctx, &lim); + /* drop the versions disabled for the previous max */ + lim.mask &= ~lim.maxProtoMask; + lim.maxProtoMask = 0; if (version != 0) { - ctx->maxProto = 0; /* turn max proto flag off */ - return Set_CTX_max_proto_version(ctx, version); + ret = setMaxProtoVersion(&lim, version); } - - /* when 0 is specified as version, try to find out the min version from - * the bottom to top of the protoverTbl. - */ - for (i = NUMBER_OF_PROTOCOLS -1; i >= 0; i--) { - ret = Set_CTX_max_proto_version(ctx, protoVerTbl[i]); - if (ret == WOLFSSL_SUCCESS) { - ctx->maxProto = 1; /* turn max proto flag on */ - break; + else { + /* when 0 is specified as version, try to find out the max version + * from the bottom to top of the protoverTbl. + */ + for (i = NUMBER_OF_PROTOCOLS -1; i >= 0; i--) { + ret = setMaxProtoVersion(&lim, protoVerTbl[i]); + if (ret == WOLFSSL_SUCCESS) { + break; + } } } - return ret; -} - - -static int Set_SSL_min_proto_version(WOLFSSL* ssl, int ver) -{ - WOLFSSL_ENTER("Set_SSL_min_proto_version"); - - if (ssl == NULL) { - return WOLFSSL_FAILURE; - } - - switch (ver) { -#ifndef NO_TLS - case SSL3_VERSION: -#if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS) - ssl->options.minDowngrade = SSLv3_MINOR; - break; -#endif - case TLS1_VERSION: - #ifdef WOLFSSL_ALLOW_TLSV10 - ssl->options.minDowngrade = TLSv1_MINOR; - break; - #endif - case TLS1_1_VERSION: - #ifndef NO_OLD_TLS - ssl->options.minDowngrade = TLSv1_1_MINOR; - break; - #endif - case TLS1_2_VERSION: - #ifndef WOLFSSL_NO_TLS12 - ssl->options.minDowngrade = TLSv1_2_MINOR; - break; - #endif - case TLS1_3_VERSION: - #ifdef WOLFSSL_TLS13 - ssl->options.minDowngrade = TLSv1_3_MINOR; - break; - #endif -#endif -#ifdef WOLFSSL_DTLS - case DTLS1_VERSION: - #ifndef NO_OLD_TLS - ssl->options.minDowngrade = DTLS_MINOR; - break; - #endif - case DTLS1_2_VERSION: - ssl->options.minDowngrade = DTLSv1_2_MINOR; - break; -#endif - default: - WOLFSSL_MSG("Unrecognized protocol version or not compiled in"); - return WOLFSSL_FAILURE; - } + if (ret == WOLFSSL_SUCCESS) { + byte minor = ctx->method->version.minor; - switch (ver) { -#ifndef NO_TLS - case TLS1_3_VERSION: - ssl->options.mask |= WOLFSSL_OP_NO_TLSv1_2; - FALL_THROUGH; - case TLS1_2_VERSION: - ssl->options.mask |= WOLFSSL_OP_NO_TLSv1_1; - FALL_THROUGH; - case TLS1_1_VERSION: - ssl->options.mask |= WOLFSSL_OP_NO_TLSv1; - FALL_THROUGH; - case TLS1_VERSION: - ssl->options.mask |= WOLFSSL_OP_NO_SSLv3; - break; - case SSL3_VERSION: - case SSL2_VERSION: - /* Nothing to do here */ - break; -#endif -#ifdef WOLFSSL_DTLS - case DTLS1_VERSION: - case DTLS1_2_VERSION: - break; -#endif - default: - WOLFSSL_MSG("Unrecognized protocol version or not compiled in"); - return WOLFSSL_FAILURE; + setCtxProtoLimits(ctx, &lim); + ctx->maxProto = (version == 0); + if (ctx->suites != NULL && lim.maxMinor != minor) { + /* rebuild the suites derived for the previous max */ + InitSSL_CTX_Suites(ctx, ctx->suites); + } } - return CheckSslMethodVersion(ssl->version.major, ssl->options.mask); + return ret; } int wolfSSL_set_min_proto_version(WOLFSSL* ssl, int version) { int i; - int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE);; + int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE); + ProtoLimits lim; WOLFSSL_ENTER("wolfSSL_set_min_proto_version"); if (ssl == NULL) { return WOLFSSL_FAILURE; } + + getSslProtoLimits(ssl, &lim); if (version != 0) { - ret = Set_SSL_min_proto_version(ssl, version); + ret = setMinProtoVersion(&lim, version); } else { /* when 0 is specified as version, try to find out the min version */ for (i= 0; (unsigned)i < NUMBER_OF_PROTOCOLS; i++) { - ret = Set_SSL_min_proto_version(ssl, protoVerTbl[i]); + ret = setMinProtoVersion(&lim, protoVerTbl[i]); if (ret == WOLFSSL_SUCCESS) break; } } if (ret == WOLFSSL_SUCCESS) { + setSslProtoLimits(ssl, &lim); /* Version 0 picks the lowest, so the user set no minimum */ ssl->options.minVersionSet = (version != 0); RestoreDowngrade(ssl); @@ -5509,71 +5492,35 @@ int wolfSSL_set_min_proto_version(WOLFSSL* ssl, int version) return ret; } -static int Set_SSL_max_proto_version(WOLFSSL* ssl, int ver) -{ - - WOLFSSL_ENTER("Set_SSL_max_proto_version"); - - if (!ssl) { - WOLFSSL_MSG("Bad parameter"); - return WOLFSSL_FAILURE; - } - - switch (ver) { - case SSL2_VERSION: - WOLFSSL_MSG("wolfSSL does not support SSLv2"); - return WOLFSSL_FAILURE; -#ifndef NO_TLS - case SSL3_VERSION: - ssl->options.mask |= WOLFSSL_OP_NO_TLSv1; - FALL_THROUGH; - case TLS1_VERSION: - ssl->options.mask |= WOLFSSL_OP_NO_TLSv1_1; - FALL_THROUGH; - case TLS1_1_VERSION: - ssl->options.mask |= WOLFSSL_OP_NO_TLSv1_2; - FALL_THROUGH; - case TLS1_2_VERSION: - ssl->options.mask |= WOLFSSL_OP_NO_TLSv1_3; - FALL_THROUGH; - case TLS1_3_VERSION: - /* Nothing to do here */ - break; -#endif -#ifdef WOLFSSL_DTLS - case DTLS1_VERSION: - case DTLS1_2_VERSION: - break; -#endif - default: - WOLFSSL_MSG("Unrecognized protocol version or not compiled in"); - return WOLFSSL_FAILURE; - } - - return CheckSslMethodVersion(ssl->version.major, ssl->options.mask); -} - int wolfSSL_set_max_proto_version(WOLFSSL* ssl, int version) { int i; - int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE);; + int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE); + ProtoLimits lim; WOLFSSL_ENTER("wolfSSL_set_max_proto_version"); if (ssl == NULL) { return WOLFSSL_FAILURE; } + + getSslProtoLimits(ssl, &lim); if (version != 0) { - return Set_SSL_max_proto_version(ssl, version); + ret = setMaxProtoVersion(&lim, version); + } + else { + /* when 0 is specified as version, try to find out the max version + * from the bottom to top of the protoverTbl. + */ + for (i = NUMBER_OF_PROTOCOLS -1; i >= 0; i--) { + ret = setMaxProtoVersion(&lim, protoVerTbl[i]); + if (ret == WOLFSSL_SUCCESS) + break; + } } - /* when 0 is specified as version, try to find out the min version from - * the bottom to top of the protoverTbl. - */ - for (i = NUMBER_OF_PROTOCOLS -1; i >= 0; i--) { - ret = Set_SSL_max_proto_version(ssl, protoVerTbl[i]); - if (ret == WOLFSSL_SUCCESS) - break; + if (ret == WOLFSSL_SUCCESS) { + setSslProtoLimits(ssl, &lim); } return ret; @@ -9954,20 +9901,8 @@ int set_curves_list(WOLFSSL* ssl, WOLFSSL_CTX *ctx, const char* names, else { disabled &= ~(1U << curve); } - #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_TLS) - #if !defined(WOLFSSL_OLD_SET_CURVES_LIST) - /* using the wolfSSL API to set the groups, this will populate - * (ssl|ctx)->groups and reset any TLSX_SUPPORTED_GROUPS. - * The order in (ssl|ctx)->groups will then be respected - * when TLSX_KEY_SHARE needs to be established */ - if ((ssl && wolfSSL_set_groups(ssl, groups, groups_len) - != WOLFSSL_SUCCESS) - || (ctx && wolfSSL_CTX_set_groups(ctx, groups, groups_len) - != WOLFSSL_SUCCESS)) { - WOLFSSL_MSG("Unable to set supported curve"); - goto leave; - } - #elif !defined(NO_WOLFSSL_CLIENT) + #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_TLS) && \ + defined(WOLFSSL_OLD_SET_CURVES_LIST) && !defined(NO_WOLFSSL_CLIENT) /* set the supported curve so client TLS extension contains only the * desired curves */ if ((ssl && wolfSSL_UseSupportedCurve(ssl, curve) != WOLFSSL_SUCCESS) @@ -9977,9 +9912,24 @@ int set_curves_list(WOLFSSL* ssl, WOLFSSL_CTX *ctx, const char* names, goto leave; } #endif - #endif /* HAVE_SUPPORTED_CURVES && !NO_TLS */ } +#if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_TLS) && \ + !defined(WOLFSSL_OLD_SET_CURVES_LIST) + /* using the wolfSSL API to set the groups, this will populate + * (ssl|ctx)->groups and reset any TLSX_SUPPORTED_GROUPS. + * The order in (ssl|ctx)->groups will then be respected + * when TLSX_KEY_SHARE needs to be established */ + if (groups_len > 0 && + ((ssl && wolfSSL_set_groups(ssl, groups, groups_len) + != WOLFSSL_SUCCESS) || + (ctx && wolfSSL_CTX_set_groups(ctx, groups, groups_len) + != WOLFSSL_SUCCESS))) { + WOLFSSL_MSG("Unable to set supported curve"); + goto leave; + } +#endif + if (ssl != NULL) ssl->disabledCurves = disabled; else if (ctx != NULL) diff --git a/src/ssl_api_ext.c b/src/ssl_api_ext.c index 89f442536f2..347c0b18e24 100644 --- a/src/ssl_api_ext.c +++ b/src/ssl_api_ext.c @@ -600,13 +600,15 @@ int wolfSSL_UseSupportedCurve(WOLFSSL* ssl, word16 name) ret = BAD_FUNC_ARG; } else { - ssl->options.userCurves = 1; #if defined(NO_TLS) ret = WOLFSSL_FAILURE; #else ret = TLSX_UseSupportedCurve(&ssl->extensions, name, ssl->heap, ssl->options.side); #endif /* NO_TLS */ + if (ret == WOLFSSL_SUCCESS) { + ssl->options.userCurves = 1; + } } return ret; @@ -629,13 +631,138 @@ int wolfSSL_CTX_UseSupportedCurve(WOLFSSL_CTX* ctx, word16 name) ret = BAD_FUNC_ARG; } else { - ctx->userCurves = 1; #if defined(NO_TLS) ret = WOLFSSL_FAILURE; #else ret = TLSX_UseSupportedCurve(&ctx->extensions, name, ctx->heap, ctx->method->side); #endif /* NO_TLS */ + if (ret == WOLFSSL_SUCCESS) { + ctx->userCurves = 1; + } + } + + return ret; +} + +/* Replace the Supported Groups extension in a list of extensions. + * + * The new extension is built in a list of its own, so a failure leaves the + * list unchanged. + * + * @param [in, out] extensions List of extensions. + * @param [in] groups Named groups in rank order. + * @param [in] count Number of groups. Must be positive. + * @param [in] heap Heap hint for dynamic memory allocation. + * @param [in] side Side the groups are used on. + * @return WOLFSSL_SUCCESS on success. + * @return BAD_FUNC_ARG when a group is invalid or not supported. + * @return MEMORY_E on dynamic memory allocation failure. + */ +static int wolfssl_set_groups_ext(TLSX** extensions, const int* groups, + int count, void* heap, int side) +{ + TLSX* ext = NULL; + int ret = WOLFSSL_SUCCESS; + int i; + + for (i = 0; (i < count) && (ret == WOLFSSL_SUCCESS); i++) { + if (!isValidCurveGroup((word16)groups[i])) { + ret = BAD_FUNC_ARG; + } + else { + ret = TLSX_UseSupportedCurve(&ext, (word16)groups[i], heap, side); + } + } + if (ret == WOLFSSL_SUCCESS) { + TLSX_Remove(extensions, TLSX_SUPPORTED_GROUPS, heap); + ext->next = *extensions; + *extensions = ext; + } + else { + TLSX_FreeAll(ext, heap); + } + + return ret; +} + +/* Set the key exchange groups, in rank order, on the context. A failing + * call leaves the context unchanged. + * + * @param [in, out] ctx SSL/TLS context object. + * @param [in] groups Array of named groups. + * @param [in] count Number of groups in the array. + * @return WOLFSSL_SUCCESS on success. + * @return BAD_FUNC_ARG when ctx or groups is NULL, the method is not TLS, + * count is not positive or greater than WOLFSSL_MAX_GROUP_COUNT, or + * a group is invalid or not supported. + * @return MEMORY_E on dynamic memory allocation failure. + */ +int wolfSSL_CTX_set_groups(WOLFSSL_CTX* ctx, int* groups, int count) +{ + int ret; +#ifdef WOLFSSL_TLS13 + int i; +#endif + + WOLFSSL_ENTER("wolfSSL_CTX_set_groups"); + if (ctx == NULL || groups == NULL || count <= 0 || + count > WOLFSSL_MAX_GROUP_COUNT) + return BAD_FUNC_ARG; + if (!IsTLS_ex(ctx->method->version)) + return BAD_FUNC_ARG; + + ret = wolfssl_set_groups_ext(&ctx->extensions, groups, count, ctx->heap, + ctx->method->side); + if (ret == WOLFSSL_SUCCESS) { + ctx->userCurves = 1; + #ifdef WOLFSSL_TLS13 + for (i = 0; i < count; i++) { + ctx->group[i] = (word16)groups[i]; + } + ctx->numGroups = (byte)count; + #endif + } + + return ret; +} + +/* Set the key exchange groups, in rank order, on the object. A failing + * call leaves the object unchanged. + * + * @param [in, out] ssl SSL/TLS object. + * @param [in] groups Array of named groups. + * @param [in] count Number of groups in the array. + * @return WOLFSSL_SUCCESS on success. + * @return BAD_FUNC_ARG when ssl or groups is NULL, the version is not TLS, + * count is not positive or greater than WOLFSSL_MAX_GROUP_COUNT, or + * a group is invalid or not supported. + * @return MEMORY_E on dynamic memory allocation failure. + */ +int wolfSSL_set_groups(WOLFSSL* ssl, int* groups, int count) +{ + int ret; +#ifdef WOLFSSL_TLS13 + int i; +#endif + + WOLFSSL_ENTER("wolfSSL_set_groups"); + if (ssl == NULL || groups == NULL || count <= 0 || + count > WOLFSSL_MAX_GROUP_COUNT) + return BAD_FUNC_ARG; + if (!IsTLS_ex(ssl->version)) + return BAD_FUNC_ARG; + + ret = wolfssl_set_groups_ext(&ssl->extensions, groups, count, ssl->heap, + ssl->options.side); + if (ret == WOLFSSL_SUCCESS) { + ssl->options.userCurves = 1; + #ifdef WOLFSSL_TLS13 + for (i = 0; i < count; i++) { + ssl->group[i] = (word16)groups[i]; + } + ssl->numGroups = (byte)count; + #endif } return ret; @@ -2073,11 +2200,25 @@ int wolfSSL_CTX_set1_sigalgs_list(WOLFSSL_CTX* ctx, const char* list) WOLFSSL_MSG("Bad function arguments"); ret = WOLFSSL_FAILURE; } - else if (AllocateCtxSuites(ctx) != 0) { - ret = WOLFSSL_FAILURE; - } else { - ret = SetSuitesHashSigAlgo(ctx->suites, list); + byte fresh = (ctx->suites == NULL); + + if (AllocateCtxSuites(ctx) != 0) { + ret = WOLFSSL_FAILURE; + } + else { + ret = SetSuitesHashSigAlgo(ctx->suites, list); + } + if (fresh && ctx->suites != NULL) { + if (ret == WOLFSSL_SUCCESS) { + /* Add the default cipher suites to the new sigalgs */ + InitSSL_CTX_Suites(ctx, ctx->suites); + } + else { + XFREE(ctx->suites, ctx->heap, DYNAMIC_TYPE_SUITES); + ctx->suites = NULL; + } + } } return ret; diff --git a/src/tls.c b/src/tls.c index d33c98fcc47..d76f607dc92 100644 --- a/src/tls.c +++ b/src/tls.c @@ -413,102 +413,6 @@ ProtocolVersion MakeTLSv1_3(void) } #endif -#if defined(HAVE_SUPPORTED_CURVES) -/* Sets the key exchange groups in rank order on a context. - * - * ctx SSL/TLS context object. - * groups Array of groups. - * count Number of groups in array. - * returns BAD_FUNC_ARG when ctx or groups is NULL, not using TLS v1.3, count is - * not positive or count is greater than WOLFSSL_MAX_GROUP_COUNT and - * WOLFSSL_SUCCESS on success. - */ -int wolfSSL_CTX_set_groups(WOLFSSL_CTX* ctx, int* groups, int count) -{ - int ret, i; - - WOLFSSL_ENTER("wolfSSL_CTX_set_groups"); - if (ctx == NULL || groups == NULL || count <= 0 || - count > WOLFSSL_MAX_GROUP_COUNT) - return BAD_FUNC_ARG; - if (!IsTLS_ex(ctx->method->version)) - return BAD_FUNC_ARG; - - #ifdef WOLFSSL_TLS13 - ctx->numGroups = 0; - #endif - #if !defined(NO_TLS) - TLSX_Remove(&ctx->extensions, TLSX_SUPPORTED_GROUPS, ctx->heap); - #endif /* !NO_TLS */ - for (i = 0; i < count; i++) { - /* Call to wolfSSL_CTX_UseSupportedCurve also checks if input groups - * are valid */ - if ((ret = wolfSSL_CTX_UseSupportedCurve(ctx, (word16)groups[i])) - != WOLFSSL_SUCCESS) { - #if !defined(NO_TLS) - TLSX_Remove(&ctx->extensions, TLSX_SUPPORTED_GROUPS, ctx->heap); - #endif /* !NO_TLS */ - return ret; - } - #ifdef WOLFSSL_TLS13 - ctx->group[i] = (word16)groups[i]; - #endif - } - #ifdef WOLFSSL_TLS13 - ctx->numGroups = (byte)count; - #endif - - return WOLFSSL_SUCCESS; -} - -/* Sets the key exchange groups in rank order. - * - * ssl SSL/TLS object. - * groups Array of groups. - * count Number of groups in array. - * returns BAD_FUNC_ARG when ssl or groups is NULL, not using TLS v1.3, count is - * not positive or count is greater than WOLFSSL_MAX_GROUP_COUNT and - * WOLFSSL_SUCCESS on success. - */ -int wolfSSL_set_groups(WOLFSSL* ssl, int* groups, int count) -{ - int ret, i; - - WOLFSSL_ENTER("wolfSSL_set_groups"); - if (ssl == NULL || groups == NULL || count <= 0 || - count > WOLFSSL_MAX_GROUP_COUNT) - return BAD_FUNC_ARG; - if (!IsTLS_ex(ssl->version)) - return BAD_FUNC_ARG; - - #ifdef WOLFSSL_TLS13 - ssl->numGroups = 0; - #endif - #if !defined(NO_TLS) - TLSX_Remove(&ssl->extensions, TLSX_SUPPORTED_GROUPS, ssl->heap); - #endif /* !NO_TLS */ - for (i = 0; i < count; i++) { - /* Call to wolfSSL_UseSupportedCurve also checks if input groups - * are valid */ - if ((ret = wolfSSL_UseSupportedCurve(ssl, (word16)groups[i])) - != WOLFSSL_SUCCESS) { - #if !defined(NO_TLS) - TLSX_Remove(&ssl->extensions, TLSX_SUPPORTED_GROUPS, ssl->heap); - #endif /* !NO_TLS */ - return ret; - } - #ifdef WOLFSSL_TLS13 - ssl->group[i] = (word16)groups[i]; - #endif - } - #ifdef WOLFSSL_TLS13 - ssl->numGroups = (byte)count; - #endif - - return WOLFSSL_SUCCESS; -} -#endif /* HAVE_SUPPORTED_CURVES */ - #ifndef WOLFSSL_NO_TLS12 #ifdef HAVE_EXTENDED_MASTER diff --git a/src/tls13.c b/src/tls13.c index bfd4c6441bc..31097ae09ae 100644 --- a/src/tls13.c +++ b/src/tls13.c @@ -17967,6 +17967,10 @@ void wolfSSL_CTX_set_psk_client_cs_callback(WOLFSSL_CTX* ctx, ctx->havePSK = 1; ctx->client_psk_cs_cb = cb; + if (ctx->suites != NULL) { + /* add PSK to the suites derived before */ + InitSSL_CTX_Suites(ctx, ctx->suites); + } } /* Set the PSK callback, that is passed the cipher suite, for a client to use @@ -18019,6 +18023,10 @@ void wolfSSL_CTX_set_psk_client_tls13_callback(WOLFSSL_CTX* ctx, ctx->havePSK = 1; ctx->client_psk_tls13_cb = cb; + if (ctx->suites != NULL) { + /* add PSK to the suites derived before */ + InitSSL_CTX_Suites(ctx, ctx->suites); + } } /* Set the PSK callback that returns the cipher suite for a client to use @@ -18069,6 +18077,10 @@ void wolfSSL_CTX_set_psk_server_tls13_callback(WOLFSSL_CTX* ctx, return; ctx->havePSK = 1; ctx->server_psk_tls13_cb = cb; + if (ctx->suites != NULL) { + /* add PSK to the suites derived before */ + InitSSL_CTX_Suites(ctx, ctx->suites); + } } /* Set the PSK callback that returns the cipher suite for a server to use diff --git a/tests/api.c b/tests/api.c index 709c9fbd3d3..6a1bc514123 100644 --- a/tests/api.c +++ b/tests/api.c @@ -6689,6 +6689,597 @@ static int test_wolfSSL_set_minmax_proto_version(void) return EXPECT_RESULT(); } +#if defined(OPENSSL_EXTRA) && !defined(NO_TLS) +/* Calls a version setter that must fail and checks ctx is unchanged. */ +static int test_proto_version_fail(WOLFSSL_CTX* ctx, int max, int ver) +{ + EXPECT_DECLS; + unsigned long mask; + unsigned long minProtoMask; + unsigned long maxProtoMask; + byte minDowngrade; + byte minProto; + byte maxProto; + byte minVersionSet; + ProtocolVersion pv; + int ret; + + if (ctx == NULL) { + return TEST_FAIL; + } + mask = ctx->mask; + minProtoMask = ctx->minProtoMask; + maxProtoMask = ctx->maxProtoMask; + minDowngrade = ctx->minDowngrade; + minProto = (byte)ctx->minProto; + maxProto = (byte)ctx->maxProto; + minVersionSet = (byte)ctx->minVersionSet; + pv = ctx->method->version; + + if (max) { + ret = wolfSSL_CTX_set_max_proto_version(ctx, ver); + } + else { + ret = wolfSSL_CTX_set_min_proto_version(ctx, ver); + } + ExpectIntEQ(ret, WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); + ExpectTrue(ctx->mask == mask); + ExpectTrue(ctx->minProtoMask == minProtoMask); + ExpectTrue(ctx->maxProtoMask == maxProtoMask); + ExpectIntEQ(ctx->minDowngrade, minDowngrade); + ExpectIntEQ(ctx->minProto, minProto); + ExpectIntEQ(ctx->maxProto, maxProto); + ExpectIntEQ(ctx->minVersionSet, minVersionSet); + ExpectIntEQ(ctx->method->version.major, pv.major); + ExpectIntEQ(ctx->method->version.minor, pv.minor); + + return EXPECT_RESULT(); +} + +/* Calls an SSL version setter that must fail and checks ssl is unchanged. */ +static int test_ssl_proto_version_fail(WOLFSSL* ssl, int max, int ver) +{ + EXPECT_DECLS; + unsigned long mask; + byte minDowngrade; + byte minVersionSet; + byte downgrade; + ProtocolVersion pv; + int ret; + + if (ssl == NULL) { + return TEST_FAIL; + } + mask = ssl->options.mask; + minDowngrade = ssl->options.minDowngrade; + minVersionSet = (byte)ssl->options.minVersionSet; + downgrade = (byte)ssl->options.downgrade; + pv = ssl->version; + + if (max) { + ret = wolfSSL_set_max_proto_version(ssl, ver); + } + else { + ret = wolfSSL_set_min_proto_version(ssl, ver); + } + ExpectIntEQ(ret, WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); + ExpectTrue(ssl->options.mask == mask); + ExpectIntEQ(ssl->options.minDowngrade, minDowngrade); + ExpectIntEQ(ssl->options.minVersionSet, minVersionSet); + ExpectIntEQ(ssl->options.downgrade, downgrade); + ExpectIntEQ(ssl->version.major, pv.major); + ExpectIntEQ(ssl->version.minor, pv.minor); + + return EXPECT_RESULT(); +} +#endif + +#if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \ + !defined(WOLFSSL_NO_TLS12) +/* The server makes a failing max or min call on its ssl. The client offers + * only TLS 1.2, which the server still takes. */ +static int test_ssl_proto_version_fail_hs(int max) +{ + EXPECT_DECLS; + struct test_memio_ctx test_ctx; + WOLFSSL_CTX* ctx_c = NULL; + WOLFSSL_CTX* ctx_s = NULL; + WOLFSSL* ssl_c = NULL; + WOLFSSL* ssl_s = NULL; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfTLSv1_2_client_method, wolfSSLv23_server_method), 0); + if (max) { + /* SSL 3.0 is not built */ + ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, SSL3_VERSION), + WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); + } + else { + wolfSSL_set_options(ssl_s, WOLFSSL_OP_NO_TLSv1_3); + ExpectIntEQ(wolfSSL_set_min_proto_version(ssl_s, TLS1_3_VERSION), + WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); + } + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + + return EXPECT_RESULT(); +} +#endif + +#if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \ + defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) +/* The server disables versions with opts, then makes version calls that fail + * and one that succeeds. The client offers only a version that opts disables. + */ +static int test_proto_version_fail_hs(method_provider method_c, long opts) +{ + EXPECT_DECLS; + struct test_memio_ctx test_ctx; + WOLFSSL_CTX* ctx_c = NULL; + WOLFSSL_CTX* ctx_s = NULL; + WOLFSSL* ssl_c = NULL; + WOLFSSL* ssl_s = NULL; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, NULL, NULL, + method_c, wolfSSLv23_server_method), 0); + wolfSSL_CTX_set_options(ctx_s, opts); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx_s, 0x9999), + WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); + ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx_s, 0x9999), + WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx_s, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + method_c, wolfSSLv23_server_method), 0); + ExpectIntNE(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + + return EXPECT_RESULT(); +} +#endif + +/* Failing min and max version calls must not change the ctx. */ +static int test_wolfSSL_CTX_set_minmax_proto_version_fail(void) +{ + EXPECT_DECLS; +#if defined(OPENSSL_EXTRA) && !defined(NO_TLS) + /* How the ctx limits versions before the failing calls */ + static const struct { + int minVer; /* -1: not set */ + int maxVer; /* -1: not set */ + long opts; + } states[] = { + { -1, -1, 0 }, + { -1, -1, WOLFSSL_OP_NO_TLSv1 | WOLFSSL_OP_NO_TLSv1_1 | + WOLFSSL_OP_NO_TLSv1_2 }, + { TLS1_2_VERSION, -1, WOLFSSL_OP_NO_TLSv1_3 }, + #ifndef WOLFSSL_NO_TLS12 + { TLS1_2_VERSION, TLS1_2_VERSION, 0 }, + #endif + { 0, 0, 0 }, + }; + /* Calls that fail in all of the states */ + static const struct { + int max; /* 1: max setter, 0: min setter */ + int ver; + } calls[] = { + { 0, 0x9999 }, + { 1, 0x9999 }, + { 0, SSL2_VERSION }, + { 1, SSL2_VERSION }, + { 0, DTLS1_2_VERSION }, + { 1, DTLS1_2_VERSION }, + #ifndef WOLFSSL_ALLOW_SSLV3 + { 1, SSL3_VERSION }, + #endif + }; + WOLFSSL_CTX* ctx = NULL; + size_t i; + size_t j; + + for (i = 0; i < XELEM_CNT(states); i++) { + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + if (states[i].minVer >= 0) { + ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, + states[i].minVer), WOLFSSL_SUCCESS); + } + if (states[i].maxVer >= 0) { + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, + states[i].maxVer), WOLFSSL_SUCCESS); + } + wolfSSL_CTX_set_options(ctx, states[i].opts); + for (j = 0; j < XELEM_CNT(calls); j++) { + ExpectIntEQ(test_proto_version_fail(ctx, calls[j].max, + calls[j].ver), TEST_SUCCESS); + } + wolfSSL_CTX_free(ctx); + ctx = NULL; + } + + /* No version left: the min setter fails and leaves the ctx unchanged */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_SSLv3 | WOLFSSL_OP_NO_TLSv1 | + WOLFSSL_OP_NO_TLSv1_1 | WOLFSSL_OP_NO_TLSv1_2 | WOLFSSL_OP_NO_TLSv1_3); + ExpectIntEQ(test_proto_version_fail(ctx, 0, TLS1_3_VERSION), TEST_SUCCESS); + ExpectIntEQ(test_proto_version_fail(ctx, 0, TLS1_2_VERSION), TEST_SUCCESS); + wolfSSL_CTX_free(ctx); + ctx = NULL; + + /* A min that leaves only versions disabled with options fails */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_3); + ExpectIntEQ(test_proto_version_fail(ctx, 0, TLS1_3_VERSION), TEST_SUCCESS); + wolfSSL_CTX_free(ctx); + ctx = NULL; + + /* Versions below the min are not left either */ +#if !defined(WOLFSSL_LEANPSK) && !defined(WOLFSSL_NO_TLS12) + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_TLSV1_2), + WOLFSSL_SUCCESS); + wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2); + ExpectIntEQ(test_proto_version_fail(ctx, 1, TLS1_2_VERSION), TEST_SUCCESS); + wolfSSL_CTX_free(ctx); + ctx = NULL; +#endif +#if !defined(WOLFSSL_LEANPSK) && defined(WOLFSSL_TLS13) + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_TLSV1_3), + WOLFSSL_SUCCESS); + wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_3); + ExpectIntEQ(test_proto_version_fail(ctx, 1, TLS1_3_VERSION), TEST_SUCCESS); + wolfSSL_CTX_free(ctx); + ctx = NULL; +#endif +#if !defined(NO_OLD_TLS) && !defined(WOLFSSL_ALLOW_TLSV10) + /* Without TLS 1.0 a TLS 1.0 min is TLS 1.1 */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_1 | + WOLFSSL_OP_NO_TLSv1_2 | WOLFSSL_OP_NO_TLSv1_3); + ExpectIntEQ(test_proto_version_fail(ctx, 0, TLS1_VERSION), TEST_SUCCESS); + wolfSSL_CTX_free(ctx); + ctx = NULL; +#endif + +#if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_LEANPSK) + /* A failing max keeps a min raised over an auto min */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_TLSV1_3), + WOLFSSL_SUCCESS); + ExpectIntEQ(test_proto_version_fail(ctx, 1, 0x9999), TEST_SUCCESS); +#ifndef WOLFSSL_NO_TLS12 + /* A max below that min fails */ + ExpectIntEQ(test_proto_version_fail(ctx, 1, TLS1_2_VERSION), TEST_SUCCESS); +#endif + wolfSSL_CTX_free(ctx); + ctx = NULL; +#endif +#if defined(WOLFSSL_ALLOW_SSLV3) && !defined(OPENSSL_COMPATIBLE_DEFAULTS) + /* A max below the default min fails */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + ExpectIntEQ(test_proto_version_fail(ctx, 1, SSL3_VERSION), TEST_SUCCESS); + wolfSSL_CTX_free(ctx); + ctx = NULL; +#endif + +#ifdef WOLFSSL_DTLS + /* A DTLS ctx keeps its DTLS min when a call fails */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method())); + ExpectIntEQ(test_proto_version_fail(ctx, 1, 0x9999), TEST_SUCCESS); + ExpectIntEQ(test_proto_version_fail(ctx, 0, 0x9999), TEST_SUCCESS); + ExpectIntEQ(test_proto_version_fail(ctx, 1, TLS1_2_VERSION), TEST_SUCCESS); + wolfSSL_CTX_free(ctx); +#endif +#endif +#if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \ + defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) + /* TLS 1.3 only server still rejects TLS 1.2 */ + ExpectIntEQ(test_proto_version_fail_hs(wolfTLSv1_2_client_method, + WOLFSSL_OP_NO_TLSv1 | WOLFSSL_OP_NO_TLSv1_1 | WOLFSSL_OP_NO_TLSv1_2), + TEST_SUCCESS); + /* Server without TLS 1.3 still rejects TLS 1.3 */ + ExpectIntEQ(test_proto_version_fail_hs(wolfTLSv1_3_client_method, + WOLFSSL_OP_NO_TLSv1_3), TEST_SUCCESS); +#endif + + return EXPECT_RESULT(); +} + +/* Failing SSL min and max version calls must not change the ssl. */ +static int test_wolfSSL_set_minmax_proto_version_fail(void) +{ + EXPECT_DECLS; +#if defined(OPENSSL_EXTRA) && !defined(NO_TLS) + /* Calls that fail on a TLS ssl */ + static const struct { + int max; /* 1: max setter, 0: min setter */ + int ver; + } calls[] = { + { 0, 0x9999 }, + { 1, 0x9999 }, + { 0, SSL2_VERSION }, + { 1, SSL2_VERSION }, + { 0, DTLS1_2_VERSION }, + { 1, DTLS1_2_VERSION }, + #ifndef WOLFSSL_ALLOW_SSLV3 + { 1, SSL3_VERSION }, + #endif + }; + WOLFSSL_CTX* ctx = NULL; + WOLFSSL* ssl = NULL; + size_t i; + + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + ExpectNotNull(ssl = wolfSSL_new(ctx)); + for (i = 0; i < XELEM_CNT(calls); i++) { + ExpectIntEQ(test_ssl_proto_version_fail(ssl, calls[i].max, + calls[i].ver), TEST_SUCCESS); + } + wolfSSL_free(ssl); + ssl = NULL; + + /* No version left */ + ExpectNotNull(ssl = wolfSSL_new(ctx)); + wolfSSL_set_options(ssl, WOLFSSL_OP_NO_SSLv3 | WOLFSSL_OP_NO_TLSv1 | + WOLFSSL_OP_NO_TLSv1_1 | WOLFSSL_OP_NO_TLSv1_2 | WOLFSSL_OP_NO_TLSv1_3); + ExpectIntEQ(test_ssl_proto_version_fail(ssl, 0, TLS1_3_VERSION), + TEST_SUCCESS); + ExpectIntEQ(test_ssl_proto_version_fail(ssl, 0, TLS1_2_VERSION), + TEST_SUCCESS); + ExpectIntEQ(test_ssl_proto_version_fail(ssl, 0, 0), TEST_SUCCESS); + ExpectIntEQ(test_ssl_proto_version_fail(ssl, 1, 0), TEST_SUCCESS); + wolfSSL_free(ssl); + ssl = NULL; + + /* A min that leaves only versions disabled with options fails */ + ExpectNotNull(ssl = wolfSSL_new(ctx)); + wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_3); + ExpectIntEQ(test_ssl_proto_version_fail(ssl, 0, TLS1_3_VERSION), + TEST_SUCCESS); + wolfSSL_free(ssl); + ssl = NULL; + +#if !defined(NO_OLD_TLS) && !defined(WOLFSSL_ALLOW_TLSV10) + /* Without TLS 1.0 a TLS 1.0 min is TLS 1.1 */ + ExpectNotNull(ssl = wolfSSL_new(ctx)); + wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_1 | WOLFSSL_OP_NO_TLSv1_2 | + WOLFSSL_OP_NO_TLSv1_3); + ExpectIntEQ(test_ssl_proto_version_fail(ssl, 0, TLS1_VERSION), + TEST_SUCCESS); + wolfSSL_free(ssl); + ssl = NULL; +#endif + +#if !defined(WOLFSSL_LEANPSK) && defined(WOLFSSL_TLS13) && \ + !defined(WOLFSSL_NO_TLS12) + /* A max below the min fails */ + ExpectNotNull(ssl = wolfSSL_new(ctx)); + ExpectIntEQ(wolfSSL_SetMinVersion(ssl, WOLFSSL_TLSV1_3), WOLFSSL_SUCCESS); + ExpectIntEQ(test_ssl_proto_version_fail(ssl, 1, TLS1_2_VERSION), + TEST_SUCCESS); + /* So does an automatic max with only versions below the min on */ + wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_3); + ExpectIntEQ(test_ssl_proto_version_fail(ssl, 1, 0), TEST_SUCCESS); + wolfSSL_free(ssl); + ssl = NULL; +#endif +#if defined(WOLFSSL_ALLOW_SSLV3) && !defined(OPENSSL_COMPATIBLE_DEFAULTS) + /* A max below the default min fails */ + ExpectNotNull(ssl = wolfSSL_new(ctx)); + ExpectIntEQ(test_ssl_proto_version_fail(ssl, 1, SSL3_VERSION), + TEST_SUCCESS); + wolfSSL_free(ssl); + ssl = NULL; +#endif + wolfSSL_CTX_free(ctx); + ctx = NULL; + +#ifdef WOLFSSL_DTLS + /* A DTLS ssl takes no TLS max */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method())); + ExpectNotNull(ssl = wolfSSL_new(ctx)); + ExpectIntEQ(test_ssl_proto_version_fail(ssl, 1, TLS1_2_VERSION), + TEST_SUCCESS); + ExpectIntEQ(test_ssl_proto_version_fail(ssl, 1, 0x9999), TEST_SUCCESS); + ExpectIntEQ(test_ssl_proto_version_fail(ssl, 0, 0x9999), TEST_SUCCESS); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); +#endif +#endif +#if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \ + !defined(WOLFSSL_NO_TLS12) + ExpectIntEQ(test_ssl_proto_version_fail_hs(0), TEST_SUCCESS); +#ifndef WOLFSSL_ALLOW_SSLV3 + ExpectIntEQ(test_ssl_proto_version_fail_hs(1), TEST_SUCCESS); +#endif +#endif + + return EXPECT_RESULT(); +} + +/* The version setters only replace the limits they set themselves. */ +static int test_wolfSSL_CTX_set_minmax_proto_version_opts(void) +{ + EXPECT_DECLS; +#if defined(OPENSSL_EXTRA) && !defined(NO_TLS) && defined(WOLFSSL_TLS13) && \ + !defined(WOLFSSL_NO_TLS12) + WOLFSSL_CTX* ctx = NULL; +#ifdef WOLFSSL_DTLS + byte minDowngrade = 0; +#endif +#ifndef NO_OLD_TLS + /* Setter calls that end with TLS 1.2 disabled */ + static const struct { + int max[4]; /* 1: max setter, 0: min setter */ + int ver[4]; + } seqs[] = { + { { 1, 1, 0, 1 }, + { TLS1_1_VERSION, TLS1_3_VERSION, TLS1_3_VERSION, TLS1_3_VERSION } }, + { { 0, 0, 1, 0 }, + { TLS1_3_VERSION, TLS1_1_VERSION, TLS1_1_VERSION, TLS1_1_VERSION } }, + { { 1, 0, 0, 1 }, + { TLS1_1_VERSION, TLS1_2_VERSION, TLS1_3_VERSION, TLS1_3_VERSION } }, + }; + size_t i; + size_t j; +#endif + + /* Options set by the application are kept */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), + WOLFSSL_SUCCESS); + ExpectTrue((wolfSSL_CTX_get_options(ctx) & WOLFSSL_OP_NO_TLSv1_2) != 0); + wolfSSL_CTX_free(ctx); + ctx = NULL; + + /* A new max or min replaces the previous one */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + ExpectTrue((wolfSSL_CTX_get_options(ctx) & WOLFSSL_OP_NO_TLSv1_3) == 0); + ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), + WOLFSSL_SUCCESS); + ExpectTrue((wolfSSL_CTX_get_options(ctx) & WOLFSSL_OP_NO_TLSv1_2) == 0); + wolfSSL_CTX_free(ctx); + ctx = NULL; + + /* A version also disabled with options stays disabled, whichever came + * first */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_3); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + ExpectTrue((wolfSSL_CTX_get_options(ctx) & WOLFSSL_OP_NO_TLSv1_3) != 0); + wolfSSL_CTX_free(ctx); + ctx = NULL; + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), + WOLFSSL_SUCCESS); + wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_3); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + ExpectTrue((wolfSSL_CTX_get_options(ctx) & WOLFSSL_OP_NO_TLSv1_3) != 0); + wolfSSL_CTX_free(ctx); + ctx = NULL; + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2); + ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), + WOLFSSL_SUCCESS); + ExpectTrue((wolfSSL_CTX_get_options(ctx) & WOLFSSL_OP_NO_TLSv1_2) != 0); + wolfSSL_CTX_free(ctx); + ctx = NULL; + +#ifndef NO_OLD_TLS + /* Both setters can disable TLS 1.2. Each drops only its own record. */ + for (i = 0; i < XELEM_CNT(seqs); i++) { + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + for (j = 0; j < XELEM_CNT(seqs[i].ver); j++) { + if (seqs[i].max[j]) { + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, + seqs[i].ver[j]), WOLFSSL_SUCCESS); + } + else { + ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, + seqs[i].ver[j]), WOLFSSL_SUCCESS); + } + } + ExpectTrue((wolfSSL_CTX_get_options(ctx) & + WOLFSSL_OP_NO_TLSv1_2) != 0); + wolfSSL_CTX_free(ctx); + ctx = NULL; + } + + /* Options cleared by the application are no longer the max's */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION), + WOLFSSL_SUCCESS); + wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_TLSv1_2 | + WOLFSSL_OP_NO_TLSv1_3); + ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + ExpectTrue((wolfSSL_CTX_get_options(ctx) & WOLFSSL_OP_NO_TLSv1_2) != 0); + wolfSSL_CTX_free(ctx); + ctx = NULL; +#endif + +#ifndef WOLFSSL_LEANPSK + /* wolfSSL_CTX_SetMinVersion() replaces the min of the min setter */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_TLSV1_2), + WOLFSSL_SUCCESS); + ExpectTrue((wolfSSL_CTX_get_options(ctx) & WOLFSSL_OP_NO_TLSv1_2) == 0); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), + WOLFSSL_SUCCESS); + wolfSSL_CTX_free(ctx); + ctx = NULL; + + /* The max setter does not change the min */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_TLSV1_3), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(ctx->minDowngrade, TLSv1_3_MINOR); + wolfSSL_CTX_free(ctx); + ctx = NULL; +#endif + +#if !defined(NO_OLD_TLS) && !defined(WOLFSSL_ALLOW_TLSV10) && \ + !(defined(WOLFSSL_ALLOW_SSLV3) && defined(OPENSSL_COMPATIBLE_DEFAULTS)) + /* Nor the default min, which is TLS 1.1 when TLS 1.0 is not allowed */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION); + wolfSSL_CTX_free(ctx); + ctx = NULL; +#endif + +#ifdef WOLFSSL_DTLS + /* Nor a DTLS min */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method())); + if (EXPECT_SUCCESS()) { + minDowngrade = ctx->minDowngrade; + } + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, DTLS1_2_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(ctx->minDowngrade, minDowngrade); + wolfSSL_CTX_free(ctx); +#endif +#endif + + return EXPECT_RESULT(); +} + #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \ defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl) @@ -6738,6 +7329,150 @@ static int test_wolfSSL_CTX_set_max_proto_version(void) } #endif +/* Raising the max after the suites were derived offers the new suites. */ +static int test_wolfSSL_CTX_set_max_proto_version_suites(void) +{ + EXPECT_DECLS; +#if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \ + defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) + struct test_memio_ctx test_ctx; + WOLFSSL_CTX* ctx_c = NULL; + WOLFSSL_CTX* ctx_s = NULL; + WOLFSSL* ssl_c = NULL; + WOLFSSL* ssl_s = NULL; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, NULL, NULL, + wolfSSLv23_client_method, wolfSSLv23_server_method), 0); + /* Derive the suites of both with a TLS 1.2 max */ + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx_c, TLS1_2_VERSION), + WOLFSSL_SUCCESS); + ExpectNotNull(ssl_c = wolfSSL_new(ctx_c)); + wolfSSL_free(ssl_c); + ssl_c = NULL; + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx_s, TLS1_2_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile, + WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx_c, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx_s, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, NULL, + wolfSSLv23_client_method, wolfSSLv23_server_method), 0); + /* No SetDH() on the server, which would rebuild the suites of the ssl */ + ExpectNotNull(ssl_s = wolfSSL_new(ctx_s)); + wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx); + wolfSSL_SetIOReadCtx(ssl_s, &test_ctx); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + ExpectStrEQ(wolfSSL_get_version(ssl_c), "TLSv1.3"); + + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); +#endif + return EXPECT_RESULT(); +} + +#if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \ + !defined(WOLFSSL_NO_TLS12) && \ + defined(BUILD_TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256) +static unsigned int test_suites_psk_client_cb(WOLFSSL* ssl, const char* hint, + char* identity, unsigned int id_max_len, unsigned char* key, + unsigned int key_max_len) +{ + (void)ssl; + (void)hint; + (void)key_max_len; + XSTRNCPY(identity, "Client_identity", id_max_len); + XMEMSET(key, 0x11, 32); + return 32; +} + +static unsigned int test_suites_psk_server_cb(WOLFSSL* ssl, const char* id, + unsigned char* key, unsigned int key_max_len) +{ + (void)ssl; + (void)id; + (void)key_max_len; + XMEMSET(key, 0x11, 32); + return 32; +} +#endif + +/* A PSK callback set after the suites were derived adds the PSK suites. */ +static int test_wolfSSL_CTX_set_psk_client_callback_suites(void) +{ + EXPECT_DECLS; +#if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \ + !defined(WOLFSSL_NO_TLS12) && \ + defined(BUILD_TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256) + struct test_memio_ctx test_ctx; + WOLFSSL_CTX* ctx_c = NULL; + WOLFSSL_CTX* ctx_s = NULL; + WOLFSSL* ssl_c = NULL; + WOLFSSL* ssl_s = NULL; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, NULL, NULL, + wolfSSLv23_client_method, wolfTLSv1_2_server_method), 0); +#ifdef OPENSSL_EXTRA + /* A max sets no options, so new SSLs share the suites of the ctx */ + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx_c, 0), WOLFSSL_SUCCESS); +#endif + /* The first ssl derives the suites */ + ExpectNotNull(ssl_c = wolfSSL_new(ctx_c)); + wolfSSL_free(ssl_c); + ssl_c = NULL; + wolfSSL_CTX_set_psk_client_callback(ctx_c, test_suites_psk_client_cb); + wolfSSL_CTX_set_psk_server_callback(ctx_s, test_suites_psk_server_cb); + ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx_s, + "ECDHE-PSK-AES128-GCM-SHA256"), WOLFSSL_SUCCESS); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfSSLv23_client_method, wolfTLSv1_2_server_method), 0); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); +#endif + return EXPECT_RESULT(); +} + +/* Allowing anon ciphers after the suites were derived adds the anon suites. */ +static int test_wolfSSL_CTX_allow_anon_cipher_suites(void) +{ + EXPECT_DECLS; +#if defined(BUILD_TLS_DH_anon_WITH_AES_256_GCM_SHA384) && \ + !defined(NO_WOLFSSL_CLIENT) && !defined(WOLFSSL_NO_TLS12) && \ + !defined(OPENSSL_COMPATIBLE_DEFAULTS) + WOLFSSL_CTX* ctx = NULL; + WOLFSSL* ssl = NULL; + word16 i; + int found = 0; + + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())); + /* The first ssl derives the suites */ + ExpectNotNull(ssl = wolfSSL_new(ctx)); + wolfSSL_free(ssl); + ExpectIntEQ(wolfSSL_CTX_allow_anon_cipher(ctx), WOLFSSL_SUCCESS); + ExpectNotNull(ctx->suites); + for (i = 0; (ctx != NULL) && (ctx->suites != NULL) && + (i + 1 < ctx->suites->suiteSz); i += 2) { + if ((ctx->suites->suites[i] == CIPHER_BYTE) && + (ctx->suites->suites[i + 1] == + TLS_DH_anon_WITH_AES_256_GCM_SHA384)) { + found = 1; + } + } + ExpectIntEQ(found, 1); + wolfSSL_CTX_free(ctx); +#endif + return EXPECT_RESULT(); +} + /*----------------------------------------------------------------------------* | SSL *----------------------------------------------------------------------------*/ @@ -7356,8 +8091,7 @@ static int test_wolfSSL_SetVersion_auto_min(void) ctx = NULL; #endif - /* setting a maximum reapplies the minimum internally, which must not - * disturb the flag */ + /* setting a maximum must not disturb the flag */ ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())); ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), WOLFSSL_SUCCESS); @@ -21725,6 +22459,243 @@ static int test_wolfSSL_set1_sigalgs_list(void) return EXPECT_RESULT(); } +#if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES) && \ + defined(HAVE_ECC) && (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \ + (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \ + defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) +/* The server takes only P-384 and then makes a failing call that would add + * P-256, on the ctx or on the ssl. The client offers only P-256. */ +static int test_set_groups_fail_hs(int onSsl) +{ + EXPECT_DECLS; + struct test_memio_ctx test_ctx; + WOLFSSL_CTX* ctx_c = NULL; + WOLFSSL_CTX* ctx_s = NULL; + WOLFSSL* ssl_c = NULL; + WOLFSSL* ssl_s = NULL; + TLSX* ext = NULL; + int p256 = WOLFSSL_ECC_SECP256R1; + int p384 = WOLFSSL_ECC_SECP384R1; + /* P-256 is added before the invalid group is found */ + int bad[] = { WOLFSSL_ECC_SECP256R1, 0x1234 }; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, NULL, NULL, + wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0); + if (!onSsl) { + ExpectIntEQ(wolfSSL_CTX_set_groups(ctx_s, &p384, 1), WOLFSSL_SUCCESS); + ExpectIntNE(wolfSSL_CTX_set_groups(ctx_s, bad, 2), WOLFSSL_SUCCESS); + ExpectIntEQ(ctx_s->numGroups, 1); + ExpectIntEQ(ctx_s->group[0], WOLFSSL_ECC_SECP384R1); + ExpectNotNull(ext = TLSX_Find(ctx_s->extensions, + TLSX_SUPPORTED_GROUPS)); + } + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0); + if (onSsl) { + ExpectIntEQ(wolfSSL_set_groups(ssl_s, &p384, 1), WOLFSSL_SUCCESS); + ExpectIntNE(wolfSSL_set_groups(ssl_s, bad, 2), WOLFSSL_SUCCESS); + ExpectIntEQ(ssl_s->numGroups, 1); + ExpectIntEQ(ssl_s->group[0], WOLFSSL_ECC_SECP384R1); + ExpectNotNull(ext = TLSX_Find(ssl_s->extensions, + TLSX_SUPPORTED_GROUPS)); + } + ExpectIntEQ(((SupportedCurve*)ext->data)->name, WOLFSSL_ECC_SECP384R1); + ExpectNull(((SupportedCurve*)ext->data)->next); + + ExpectIntEQ(wolfSSL_set_groups(ssl_c, &p256, 1), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1), + WOLFSSL_SUCCESS); + /* No group in common */ + ExpectIntNE(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + + return EXPECT_RESULT(); +} + +/* A valid group that is not built in */ +#if !defined(HAVE_CURVE448) + #define TEST_UNBUILT_GROUP WOLFSSL_ECC_X448 +#elif ECC_MIN_KEY_SZ > 160 || \ + (!defined(HAVE_ECC160) && !defined(HAVE_ALL_CURVES)) + #define TEST_UNBUILT_GROUP WOLFSSL_ECC_SECP160R1 +#endif + +/* Failing group calls on a new client ctx and ssl keep the default groups. */ +static int test_set_groups_fail_fresh(void) +{ + EXPECT_DECLS; + struct test_memio_ctx test_ctx; + WOLFSSL_CTX* ctx_c = NULL; + WOLFSSL_CTX* ctx_s = NULL; + WOLFSSL* ssl_c = NULL; + WOLFSSL* ssl_s = NULL; + int bad[] = { WOLFSSL_ECC_SECP256R1, 0x1234 }; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, NULL, NULL, + wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0); + ExpectIntNE(wolfSSL_CTX_set_groups(ctx_c, bad, 2), WOLFSSL_SUCCESS); +#ifdef TEST_UNBUILT_GROUP + ExpectIntNE(wolfSSL_CTX_UseSupportedCurve(ctx_c, TEST_UNBUILT_GROUP), + WOLFSSL_SUCCESS); +#endif + ExpectIntEQ(ctx_c->userCurves, 0); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0); + ExpectIntNE(wolfSSL_set_groups(ssl_c, bad, 2), WOLFSSL_SUCCESS); +#ifdef TEST_UNBUILT_GROUP + ExpectIntNE(wolfSSL_UseSupportedCurve(ssl_c, TEST_UNBUILT_GROUP), + WOLFSSL_SUCCESS); +#endif + ExpectIntEQ(ssl_c->options.userCurves, 0); + /* The client still offers the default groups */ + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + + return EXPECT_RESULT(); +} +#endif + +/* A failing group setter keeps the groups set before. */ +static int test_wolfSSL_set_groups_fail(void) +{ + EXPECT_DECLS; +#if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES) && \ + defined(HAVE_ECC) && (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \ + (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \ + defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) + ExpectIntEQ(test_set_groups_fail_hs(0), TEST_SUCCESS); + ExpectIntEQ(test_set_groups_fail_hs(1), TEST_SUCCESS); + ExpectIntEQ(test_set_groups_fail_fresh(), TEST_SUCCESS); +#endif + return EXPECT_RESULT(); +} + +#if defined(OPENSSL_EXTRA) && !defined(NO_TLS) && defined(HAVE_ECC) && \ + !defined(NO_SHA256) && defined(WOLFSSL_SHA384) +/* Makes failing calls and checks the list in suites is unchanged. */ +static int test_sigalgs_list_fail(WOLFSSL_CTX* ctx, WOLFSSL* ssl) +{ + EXPECT_DECLS; + /* An unknown name after a good entry, a bad digest, no digest */ + static const char* bad[] = { + "ECDSA+SHA384:BOGUS+SHA256", "ECDSA+BOGUS", "ECDSA" + }; + Suites* suites = NULL; + byte saved[WOLFSSL_MAX_SIGALGO]; + word16 savedSz = 0; + size_t i; + + if (ssl != NULL) { + ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"), + WOLFSSL_SUCCESS); + suites = ssl->suites; + } + else { + ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"), + WOLFSSL_SUCCESS); + suites = ctx->suites; + } + ExpectNotNull(suites); + if (EXPECT_SUCCESS()) { + savedSz = suites->hashSigAlgoSz; + XMEMCPY(saved, suites->hashSigAlgo, savedSz); + } + for (i = 0; i < XELEM_CNT(bad); i++) { + if (ssl != NULL) { + ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, bad[i]), + WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); + } + else { + ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, bad[i]), + WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); + } + ExpectIntEQ(suites->hashSigAlgoSz, savedSz); + ExpectBufEQ(suites->hashSigAlgo, saved, savedSz); + } + + return EXPECT_RESULT(); +} +#endif + +/* A failing sigalgs list call keeps the list set before. */ +static int test_wolfSSL_set1_sigalgs_list_fail(void) +{ + EXPECT_DECLS; +#if defined(OPENSSL_EXTRA) && !defined(NO_TLS) && defined(HAVE_ECC) && \ + !defined(NO_SHA256) && defined(WOLFSSL_SHA384) + WOLFSSL_CTX* ctx = NULL; + WOLFSSL* ssl = NULL; +#ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES + struct test_memio_ctx test_ctx; + WOLFSSL_CTX* ctx_c = NULL; + WOLFSSL_CTX* ctx_s = NULL; + WOLFSSL* ssl_c = NULL; + WOLFSSL* ssl_s = NULL; +#endif + + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + ExpectIntEQ(test_sigalgs_list_fail(ctx, NULL), TEST_SUCCESS); + ExpectNotNull(ssl = wolfSSL_new(ctx)); + ExpectIntEQ(test_sigalgs_list_fail(ctx, ssl), TEST_SUCCESS); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + +#ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES + /* A client that takes only ECDSA rejects the RSA server. The options make + * wolfSSL_new() fill an empty list with the defaults. */ + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, NULL, NULL, + wolfSSLv23_client_method, wolfSSLv23_server_method), 0); + wolfSSL_CTX_set_options(ctx_c, WOLFSSL_OP_NO_COMPRESSION); + ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx_c, "ECDSA+SHA256"), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx_c, "ECDSA+BOGUS"), + WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfSSLv23_client_method, wolfSSLv23_server_method), 0); + ExpectIntNE(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + +#ifdef WC_RSA_PSS + /* A new client ctx without options keeps its cipher suites */ + ctx_c = NULL; + ctx_s = NULL; + ssl_c = NULL; + ssl_s = NULL; + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, NULL, NULL, + wolfSSLv23_client_method, wolfSSLv23_server_method), 0); + ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx_c, "ECDSA+BOGUS"), + WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); + ExpectNull(ctx_c->suites); + ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx_c, + "RSA-PSS+SHA256:RSA+SHA256"), WOLFSSL_SUCCESS); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfSSLv23_client_method, wolfSSLv23_server_method), 0); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); +#endif +#endif +#endif + return EXPECT_RESULT(); +} + /* Testing wolfSSL_set_tlsext_status_type function. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined. */ @@ -37231,6 +38202,26 @@ static int test_wolfSSL_crypto_policy_ciphers(void) return EXPECT_RESULT(); } +/* Without a policy, a new TLS ctx keeps the default minimum version. */ +static int test_wolfSSL_crypto_policy_default_min(void) +{ + EXPECT_DECLS; +#if defined(WOLFSSL_SYS_CRYPTO_POLICY) && !defined(NO_TLS) && \ + !defined(OPENSSL_COMPATIBLE_DEFAULTS) + WOLFSSL_CTX* ctx = NULL; + + ExpectIntEQ(wolfSSL_crypto_policy_is_enabled(), 0); +#ifndef NO_WOLFSSL_CLIENT + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())); +#else + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())); +#endif + ExpectIntEQ(ctx->minDowngrade, WOLFSSL_MIN_DOWNGRADE); + wolfSSL_CTX_free(ctx); +#endif + return EXPECT_RESULT(); +} + static int test_wolfSSL_SSL_in_init(void) { EXPECT_DECLS; @@ -48313,6 +49304,8 @@ TEST_CASE testCases[] = { TEST_DECL(test_wolfSSL_set1_curves_list), TEST_DECL(test_wolfSSL_curves_mismatch), TEST_DECL(test_wolfSSL_set1_sigalgs_list), + TEST_DECL(test_wolfSSL_set_groups_fail), + TEST_DECL(test_wolfSSL_set1_sigalgs_list_fail), TEST_DECL(test_wolfSSL_OtherName), TEST_DECL(test_wolfSSL_FPKI), @@ -48581,6 +49574,7 @@ TEST_CASE testCases[] = { TEST_DECL(test_wolfSSL_crypto_policy_certs_and_keys), TEST_DECL(test_wolfSSL_crypto_policy_tls_methods), TEST_DECL(test_wolfSSL_crypto_policy_ciphers), + TEST_DECL(test_wolfSSL_crypto_policy_default_min), TEST_DECL(test_wolfSSL_SSL_in_init), TEST_DECL(test_wolfSSL_CTX_set_timeout), TEST_DECL(test_wolfSSL_session_cache_api_direct), @@ -48731,7 +49725,13 @@ TEST_CASE testCases[] = { TEST_DECL(test_wolfSSL_set_ecdh_auto), TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto), TEST_DECL(test_wolfSSL_set_minmax_proto_version), + TEST_DECL(test_wolfSSL_CTX_set_minmax_proto_version_fail), + TEST_DECL(test_wolfSSL_set_minmax_proto_version_fail), + TEST_DECL(test_wolfSSL_CTX_set_minmax_proto_version_opts), TEST_DECL(test_wolfSSL_CTX_set_max_proto_version), + TEST_DECL(test_wolfSSL_CTX_set_max_proto_version_suites), + TEST_DECL(test_wolfSSL_CTX_set_psk_client_callback_suites), + TEST_DECL(test_wolfSSL_CTX_allow_anon_cipher_suites), TEST_DECL(test_wolfSSL_THREADID_hash), /* TLS extensions tests */ diff --git a/wolfssl/internal.h b/wolfssl/internal.h index 22ba3e9ce3f..981f26b4949 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -2029,8 +2029,10 @@ WOLFSSL_LOCAL int NamedGroupIsPqcHybrid(int group); /* minimum Downgrade Minor version */ #ifndef WOLFSSL_MIN_DOWNGRADE - #ifndef NO_OLD_TLS + #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10) #define WOLFSSL_MIN_DOWNGRADE TLSv1_MINOR + #elif !defined(NO_OLD_TLS) + #define WOLFSSL_MIN_DOWNGRADE TLSv1_1_MINOR #else #define WOLFSSL_MIN_DOWNGRADE TLSv1_2_MINOR #endif @@ -4735,6 +4737,10 @@ struct WOLFSSL_CTX { short minMlDsaKeySz; /* minimum ML-DSA key size */ #endif unsigned long mask; /* store SSL_OP_ flags */ +#ifdef OPENSSL_EXTRA + unsigned long minProtoMask; /* SSL_OP_NO_* set by min version */ + unsigned long maxProtoMask; /* SSL_OP_NO_* set by max version */ +#endif #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL) word32 disabledCurves; /* curves disabled by user */ #endif From 9bf2e37cfebbf2865acdcc8d67ea4d07062e5b86 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Mon, 2 Nov 2026 06:40:23 +0000 Subject: [PATCH 2/6] Fix CI: cap max proto version at TLS 1.2 without TLS 1.3 In builds without TLS 1.3, wolfSSL_CTX_set_max_proto_version() with TLS1_3_VERSION or 0 set the method version to TLS 1.3. Clients then handled TLS 1.2 handshake messages as TLS 1.3 and failed with BUFFER_E (test_wolfSSL_CTX_set_psk_client_callback_suites with --enable-wpas --disable-tls13). --- src/ssl.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ssl.c b/src/ssl.c index 289e62d21cd..616b58f4277 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -5385,6 +5385,11 @@ static int setMaxProtoVersion(ProtoLimits* lim, int ver) return WOLFSSL_FAILURE; } lim->maxMinor = (byte)ver; + #ifndef WOLFSSL_TLS13 + /* a max above the compiled in versions is the highest one */ + if (lim->major == SSLv3_MAJOR && lim->maxMinor > TLSv1_2_MINOR) + lim->maxMinor = TLSv1_2_MINOR; + #endif } return ret; } From 2c02e8acd5393bc5a9918ea0d846dc10d3f93817 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Tue, 10 Nov 2026 16:59:43 +0000 Subject: [PATCH 3/6] Address review: derive the sigalgs again on a suites rebuild The max, PSK and anon CTX setters rebuild ctx->suites in place, but kept the sigalgs derived for the old max. Derive them again unless the user set them, which a new Suites flag records. --- src/internal.c | 18 ++++++++++++++++ src/ssl.c | 30 +++++++++++---------------- src/tls13.c | 18 ++++++---------- tests/api.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++ wolfssl/internal.h | 3 +++ 5 files changed, 90 insertions(+), 30 deletions(-) diff --git a/src/internal.c b/src/internal.c index da54b73833d..29d3a28e4f2 100644 --- a/src/internal.c +++ b/src/internal.c @@ -4410,6 +4410,23 @@ int InitCtxSuitesWithMutex(WOLFSSL_CTX* ctx) return ret; } +/* Drop derived sigalgs so that InitSuites derives them again. */ +void ResetSuitesHashSigAlgo(Suites* suites) +{ + if (!suites->setSuites && !suites->setHashSigAlgo) + suites->hashSigAlgoSz = 0; +} + +/* Derive ctx->suites again after a setting they depend on changed. */ +void RefreshCtxSuites(WOLFSSL_CTX* ctx) +{ + if (ctx == NULL || ctx->suites == NULL || ctx->suites->setSuites) + return; + + ResetSuitesHashSigAlgo(ctx->suites); + InitSSL_CTX_Suites(ctx, ctx->suites); +} + /* Call this when the ssl object needs to have its own ssl->suites object */ int AllocateSuites(WOLFSSL* ssl) { @@ -35376,6 +35393,7 @@ int SetSuitesHashSigAlgo(Suites* suites, const char* list) if (ret == 1) { XMEMCPY(suites->hashSigAlgo, hashSigAlgo, idx); suites->hashSigAlgoSz = idx; + suites->setHashSigAlgo = 1; } return ret; diff --git a/src/ssl.c b/src/ssl.c index 616b58f4277..2c1300ed11c 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -4085,10 +4085,8 @@ int wolfSSL_set_compression(WOLFSSL* ssl) ctx->havePSK = 1; ctx->client_psk_cb = cb; - if (ctx->suites != NULL) { - /* add PSK to the suites derived before */ - InitSSL_CTX_Suites(ctx, ctx->suites); - } + /* add PSK to the suites derived before */ + RefreshCtxSuites(ctx); } void wolfSSL_set_psk_client_callback(WOLFSSL* ssl,wc_psk_client_callback cb) @@ -4167,10 +4165,8 @@ int wolfSSL_set_compression(WOLFSSL* ssl) return; ctx->havePSK = 1; ctx->server_psk_cb = cb; - if (ctx->suites != NULL) { - /* add PSK to the suites derived before */ - InitSSL_CTX_Suites(ctx, ctx->suites); - } + /* add PSK to the suites derived before */ + RefreshCtxSuites(ctx); } void wolfSSL_set_psk_server_callback(WOLFSSL* ssl,wc_psk_server_callback cb) @@ -4288,10 +4284,8 @@ int wolfSSL_set_compression(WOLFSSL* ssl) return WOLFSSL_FAILURE; ctx->useAnon = 1; - if (ctx->suites != NULL) { - /* add anon to the suites derived before */ - InitSSL_CTX_Suites(ctx, ctx->suites); - } + /* add anon to the suites derived before */ + RefreshCtxSuites(ctx); return WOLFSSL_SUCCESS; } @@ -5146,12 +5140,18 @@ static void getCtxProtoLimits(const WOLFSSL_CTX* ctx, ProtoLimits* lim) static void setCtxProtoLimits(WOLFSSL_CTX* ctx, const ProtoLimits* lim) { + byte minor = ctx->method->version.minor; + ctx->mask = lim->mask; ctx->minProtoMask = lim->minProtoMask; ctx->maxProtoMask = lim->maxProtoMask; /* the method version is the max */ ctx->method->version.minor = lim->maxMinor; ctx->minDowngrade = lim->minDowngrade; + if (lim->maxMinor != minor) { + /* the suites depend on the max */ + RefreshCtxSuites(ctx); + } } /* An SSL does not record the options its setters set, so they only add @@ -5449,14 +5449,8 @@ int wolfSSL_CTX_set_max_proto_version(WOLFSSL_CTX* ctx, int version) } if (ret == WOLFSSL_SUCCESS) { - byte minor = ctx->method->version.minor; - setCtxProtoLimits(ctx, &lim); ctx->maxProto = (version == 0); - if (ctx->suites != NULL && lim.maxMinor != minor) { - /* rebuild the suites derived for the previous max */ - InitSSL_CTX_Suites(ctx, ctx->suites); - } } return ret; diff --git a/src/tls13.c b/src/tls13.c index 31097ae09ae..a380e79ac60 100644 --- a/src/tls13.c +++ b/src/tls13.c @@ -17967,10 +17967,8 @@ void wolfSSL_CTX_set_psk_client_cs_callback(WOLFSSL_CTX* ctx, ctx->havePSK = 1; ctx->client_psk_cs_cb = cb; - if (ctx->suites != NULL) { - /* add PSK to the suites derived before */ - InitSSL_CTX_Suites(ctx, ctx->suites); - } + /* add PSK to the suites derived before */ + RefreshCtxSuites(ctx); } /* Set the PSK callback, that is passed the cipher suite, for a client to use @@ -18023,10 +18021,8 @@ void wolfSSL_CTX_set_psk_client_tls13_callback(WOLFSSL_CTX* ctx, ctx->havePSK = 1; ctx->client_psk_tls13_cb = cb; - if (ctx->suites != NULL) { - /* add PSK to the suites derived before */ - InitSSL_CTX_Suites(ctx, ctx->suites); - } + /* add PSK to the suites derived before */ + RefreshCtxSuites(ctx); } /* Set the PSK callback that returns the cipher suite for a client to use @@ -18077,10 +18073,8 @@ void wolfSSL_CTX_set_psk_server_tls13_callback(WOLFSSL_CTX* ctx, return; ctx->havePSK = 1; ctx->server_psk_tls13_cb = cb; - if (ctx->suites != NULL) { - /* add PSK to the suites derived before */ - InitSSL_CTX_Suites(ctx, ctx->suites); - } + /* add PSK to the suites derived before */ + RefreshCtxSuites(ctx); } /* Set the PSK callback that returns the cipher suite for a server to use diff --git a/tests/api.c b/tests/api.c index 6a1bc514123..a4d9a0a8403 100644 --- a/tests/api.c +++ b/tests/api.c @@ -7375,6 +7375,56 @@ static int test_wolfSSL_CTX_set_max_proto_version_suites(void) return EXPECT_RESULT(); } +/* A new max derives the sigalgs again, unless they were set by the user. */ +static int test_wolfSSL_CTX_set_max_proto_version_sigalgs(void) +{ + EXPECT_DECLS; +#if defined(OPENSSL_EXTRA) && !defined(NO_TLS) && defined(WOLFSSL_TLS13) && \ + !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT) && \ + !defined(NO_RSA) && !defined(NO_SHA256) + WOLFSSL_CTX* ctx = NULL; + WOLFSSL_CTX* ctx_ref = NULL; + WOLFSSL* ssl = NULL; + word16 sz = 0; + + ExpectNotNull(ctx_ref = wolfSSL_CTX_new(wolfSSLv23_client_method())); + ExpectNotNull(ssl = wolfSSL_new(ctx_ref)); + wolfSSL_free(ssl); + ssl = NULL; + + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), + WOLFSSL_SUCCESS); + ExpectNotNull(ssl = wolfSSL_new(ctx)); + wolfSSL_free(ssl); + ssl = NULL; + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + ExpectNotNull(ctx->suites); + ExpectNotNull(ctx_ref->suites); + ExpectIntEQ(ctx->suites->hashSigAlgoSz, ctx_ref->suites->hashSigAlgoSz); + if (EXPECT_SUCCESS()) { + ExpectIntEQ(XMEMCMP(ctx->suites->hashSigAlgo, + ctx_ref->suites->hashSigAlgo, ctx->suites->hashSigAlgoSz), 0); + } + + /* Sigalgs set by the user are kept */ + ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"), + WOLFSSL_SUCCESS); + if (EXPECT_SUCCESS()) { + sz = ctx->suites->hashSigAlgoSz; + } + ExpectIntEQ(sz, 2); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(ctx->suites->hashSigAlgoSz, sz); + + wolfSSL_CTX_free(ctx); + wolfSSL_CTX_free(ctx_ref); +#endif + return EXPECT_RESULT(); +} + #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \ !defined(WOLFSSL_NO_TLS12) && \ defined(BUILD_TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256) @@ -49730,6 +49780,7 @@ TEST_CASE testCases[] = { TEST_DECL(test_wolfSSL_CTX_set_minmax_proto_version_opts), TEST_DECL(test_wolfSSL_CTX_set_max_proto_version), TEST_DECL(test_wolfSSL_CTX_set_max_proto_version_suites), + TEST_DECL(test_wolfSSL_CTX_set_max_proto_version_sigalgs), TEST_DECL(test_wolfSSL_CTX_set_psk_client_callback_suites), TEST_DECL(test_wolfSSL_CTX_allow_anon_cipher_suites), TEST_DECL(test_wolfSSL_THREADID_hash), diff --git a/wolfssl/internal.h b/wolfssl/internal.h index 981f26b4949..9190d7671b0 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -2678,6 +2678,7 @@ struct Suites { byte suites[WOLFSSL_MAX_SUITE_SZ]; byte hashSigAlgo[WOLFSSL_MAX_SIGALGO]; /* sig/algo to offer */ byte setSuites:1; /* user set suites from default */ + byte setHashSigAlgo:1; /* user set sigalgs */ }; typedef struct CipherSuite { @@ -2698,6 +2699,8 @@ WOLFSSL_TEST_VIS void InitSuitesHashSigAlgo(byte* hashSigAlgo, int have, word16* len); WOLFSSL_LOCAL int AllocateCtxSuites(WOLFSSL_CTX* ctx); WOLFSSL_LOCAL int InitCtxSuitesWithMutex(WOLFSSL_CTX* ctx); +WOLFSSL_LOCAL void RefreshCtxSuites(WOLFSSL_CTX* ctx); +WOLFSSL_LOCAL void ResetSuitesHashSigAlgo(Suites* suites); WOLFSSL_LOCAL int AllocateSuites(WOLFSSL* ssl); WOLFSSL_LOCAL void InitSuites(Suites* suites, ProtocolVersion pv, int keySz, word16 haveRSA, word16 havePSK, word16 haveDH, From f2926cc6211ab784042265441a85533e4a17e96c Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Tue, 10 Nov 2026 06:36:27 +0000 Subject: [PATCH 4/6] Address review: record SSL min/max options and fix min above max - SSL objects record the NO_* options their min and max setters set, copied from the CTX, so a later call can raise an SSL max again. The SSL version is raised up to the CTX max. An SSL max or min above the CTX max fails, as the handshake treats the CTX max as the highest version. - A min above the max makes the highest version left the max, so the method version no longer keeps the dropped max. - A min of the other major fails. An automatic min on DTLS now picks a DTLS version. --- src/internal.c | 5 + src/ssl.c | 302 +++++++++++++++++++++++++++++---------------- tests/api.c | 128 +++++++++++++++++++ wolfssl/internal.h | 4 + 4 files changed, 335 insertions(+), 104 deletions(-) diff --git a/src/internal.c b/src/internal.c index 29d3a28e4f2..244a9d12ff3 100644 --- a/src/internal.c +++ b/src/internal.c @@ -8756,6 +8756,11 @@ int SetSSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) WOLFSSL_MSG("wolfSSL_set_options error"); return BAD_FUNC_ARG; } +#ifdef OPENSSL_EXTRA + /* after wolfSSL_set_options(), which drops them */ + ssl->options.minProtoMask = ctx->minProtoMask; + ssl->options.maxProtoMask = ctx->maxProtoMask; +#endif #ifdef WOLFSSL_SESSION_EXPORT #ifdef WOLFSSL_DTLS diff --git a/src/ssl.c b/src/ssl.c index 2c1300ed11c..c62f029964d 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -2295,6 +2295,11 @@ int wolfSSL_SetMinVersion(WOLFSSL* ssl, int version) ret = SetMinVersionHelper(&ssl->options.minDowngrade, version); if (ret == WOLFSSL_SUCCESS) { ssl->options.minVersionSet = 1; + #ifdef OPENSSL_EXTRA + /* replaces a min set with wolfSSL_set_min_proto_version() */ + ssl->options.mask &= ~ssl->options.minProtoMask; + ssl->options.minProtoMask = 0; + #endif RestoreDowngrade(ssl); } @@ -5126,6 +5131,7 @@ typedef struct ProtoLimits { byte major; byte maxMinor; byte minDowngrade; + byte maxProto; /* max set to the highest available */ } ProtoLimits; static void getCtxProtoLimits(const WOLFSSL_CTX* ctx, ProtoLimits* lim) @@ -5136,6 +5142,7 @@ static void getCtxProtoLimits(const WOLFSSL_CTX* ctx, ProtoLimits* lim) lim->major = ctx->method->version.major; lim->maxMinor = ctx->method->version.minor; lim->minDowngrade = ctx->minDowngrade; + lim->maxProto = (byte)ctx->maxProto; } static void setCtxProtoLimits(WOLFSSL_CTX* ctx, const ProtoLimits* lim) @@ -5154,23 +5161,92 @@ static void setCtxProtoLimits(WOLFSSL_CTX* ctx, const ProtoLimits* lim) } } -/* An SSL does not record the options its setters set, so they only add - * options. */ static void getSslProtoLimits(const WOLFSSL* ssl, ProtoLimits* lim) { lim->mask = ssl->options.mask; - lim->minProtoMask = 0; - lim->maxProtoMask = 0; + lim->minProtoMask = ssl->options.minProtoMask; + lim->maxProtoMask = ssl->options.maxProtoMask; lim->major = ssl->version.major; lim->maxMinor = ssl->version.minor; lim->minDowngrade = ssl->options.minDowngrade; + lim->maxProto = (byte)ssl->options.maxProto; +} + +/* Whether the SSL TLS version is bounded by the max of the CTX. A pinned + * version is not. */ +static int sslHasCtxMax(const WOLFSSL* ssl, const ProtoLimits* lim) +{ + return lim->major == SSLv3_MAJOR && ssl->options.downgrade; +} + +/* Returns the TLS minor an SSL version is raised to for the limits, or 0 + * when it is kept. A lower max is held in the mask only. The handshake takes + * the CTX max as the highest version, so the SSL is not raised above it. */ +static byte getSslRaisedMinor(const WOLFSSL* ssl, const ProtoLimits* lim) +{ + int top; + byte minor; + + if (!sslHasCtxMax(ssl, lim)) + return 0; + top = GetMaxProtoVersion((long)lim->mask); + if (top <= 0) + return 0; + minor = (byte)top; + if (minor > lim->maxMinor) + minor = lim->maxMinor; + if (minor > ssl->ctx->method->version.minor) + minor = ssl->ctx->method->version.minor; + return (minor > ssl->version.minor) ? minor : 0; } -/* An SSL max is held in the mask only, so the SSL version is kept. */ -static void setSslProtoLimits(WOLFSSL* ssl, const ProtoLimits* lim) +/* Stores the limits in ssl. Fails, leaving ssl unchanged, for a min above the + * CTX max or when the suites for a raised version can't be allocated. */ +static int setSslProtoLimits(WOLFSSL* ssl, const ProtoLimits* lim) { + byte minor = getSslRaisedMinor(ssl, lim); + + if (sslHasCtxMax(ssl, lim) && + lim->minDowngrade > ssl->ctx->method->version.minor) { + WOLFSSL_MSG("Min above the max of the CTX"); + return WOLFSSL_FAILURE; + } + if (minor != 0 && ssl->options.side != WOLFSSL_NEITHER_END && + AllocateSuites(ssl) != 0) { + return WOLFSSL_FAILURE; + } + ssl->options.mask = lim->mask; + ssl->options.minProtoMask = lim->minProtoMask; + ssl->options.maxProtoMask = lim->maxProtoMask; ssl->options.minDowngrade = lim->minDowngrade; + if (minor != 0) { + ssl->version.minor = minor; + if (ssl->options.side != WOLFSSL_NEITHER_END) { + word16 haveRSA = 1; + word16 havePSK = 0; + int keySz = 0; + #ifdef NO_RSA + haveRSA = 0; + #endif + #ifndef NO_PSK + havePSK = ssl->options.havePSK; + #endif + #ifndef NO_CERTS + keySz = ssl->buffers.keySz; + #endif + /* derive the suites for the raised version */ + ResetSuitesHashSigAlgo(ssl->suites); + InitSuites(ssl->suites, ssl->version, keySz, haveRSA, havePSK, + ssl->options.side == WOLFSSL_CLIENT_END ? TRUE : + ssl->options.haveDH, + ssl->options.haveECDSAsig, ssl->options.haveECC, TRUE, + ssl->options.haveStaticECC, ssl->options.useAnon, + TRUE, TRUE, TRUE, TRUE, ssl->options.side); + } + } + + return WOLFSSL_SUCCESS; } /* Disables versions for a min or max limit. Only options not already set are @@ -5194,12 +5270,18 @@ static int checkProtoLimits(const ProtoLimits* lim) } /* Applies a min version to the limits: sets minDowngrade and disables the TLS - * versions below it. Fails for an unknown version or when no version is left. + * versions below it. Fails for an unknown version, a version of the other + * major or when no version is left. */ static int setMinProtoVersion(ProtoLimits* lim, int version) { WOLFSSL_ENTER("setMinProtoVersion"); + if ((version >> 8) != lim->major) { + WOLFSSL_MSG("Mismatched protocol version"); + return WOLFSSL_FAILURE; + } + switch (version) { #ifndef NO_TLS case SSL3_VERSION: @@ -5250,6 +5332,61 @@ static int setMinProtoVersion(ProtoLimits* lim, int version) return checkProtoLimits(lim); } +/* Replaces the min of the limits. Version 0 picks the lowest version left. A + * min above the max drops the max, so the highest version left is the max. + */ +static int applyMinProtoVersion(ProtoLimits* lim, int version) +{ + int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE); + int proto = 0; + int maxProto; + int i; + int idx; + + /* drop the versions disabled for the previous min */ + lim->mask &= ~lim->minProtoMask; + lim->minProtoMask = 0; + + if (version != 0) { + proto = version; + for (i = 0; (unsigned)i < NUMBER_OF_PROTOCOLS; i++) { + if (protoVerTbl[i] == version) { + break; + } + } + } + else { + /* when 0 is specified as version, try to find out the min version */ + for (i = 0; (unsigned)i < NUMBER_OF_PROTOCOLS; i++) { + ret = setMinProtoVersion(lim, protoVerTbl[i]); + if (ret == WOLFSSL_SUCCESS) { + proto = protoVerTbl[i]; + break; + } + } + } + + /* i is the table index of the min and idx the one of the max */ + maxProto = lim->maxProto ? 0 : GetMaxProtoVersion((long)lim->mask); + for (idx = 0; (unsigned)idx < NUMBER_OF_PROTOCOLS; idx++) { + if (protoVerTbl[idx] == maxProto) { + break; + } + } + if (idx < i) { + lim->mask &= ~lim->maxProtoMask; + lim->maxProtoMask = 0; + if (lim->major == SSLv3_MAJOR) { + maxProto = GetMaxProtoVersion((long)lim->mask); + if (maxProto > 0) { + lim->maxMinor = (byte)maxProto; + } + } + } + + return setMinProtoVersion(lim, proto); +} + /** * wolfSSL_CTX_set_min_proto_version attempts to set the minimum protocol * version to use by SSL objects created from this WOLFSSL_CTX. @@ -5275,10 +5412,6 @@ static int setMinProtoVersion(ProtoLimits* lim, int version) int wolfSSL_CTX_set_min_proto_version(WOLFSSL_CTX* ctx, int version) { int ret; - int proto = 0; - int maxProto = 0; - int i; - int idx = 0; ProtoLimits lim; WOLFSSL_ENTER("wolfSSL_CTX_set_min_proto_version"); @@ -5286,52 +5419,9 @@ int wolfSSL_CTX_set_min_proto_version(WOLFSSL_CTX* ctx, int version) if (ctx == NULL) { return WOLFSSL_FAILURE; } -#ifdef WOLFSSL_DTLS - if ((version >> 8) == DTLS_MAJOR && - ctx->method->version.major != DTLS_MAJOR) { - WOLFSSL_MSG("Mismatched protocol version"); - return WOLFSSL_FAILURE; - } -#endif getCtxProtoLimits(ctx, &lim); - /* drop the versions disabled for the previous min */ - lim.mask &= ~lim.minProtoMask; - lim.minProtoMask = 0; - - if (version != 0) { - proto = version; - for (i = 0; (unsigned)i < NUMBER_OF_PROTOCOLS; i++) { - if (protoVerTbl[i] == version) { - break; - } - } - } - else { - /* when 0 is specified as version, try to find out the min version */ - for (i = 0; (unsigned)i < NUMBER_OF_PROTOCOLS; i++) { - ret = setMinProtoVersion(&lim, protoVerTbl[i]); - if (ret == WOLFSSL_SUCCESS) { - proto = protoVerTbl[i]; - break; - } - } - } - - /* A min above the max drops the NO_* options set for the max. i is the - * table index of the min and idx the one of the max. */ - maxProto = ctx->maxProto ? 0 : GetMaxProtoVersion((long)lim.mask); - for (idx = 0; (unsigned)idx < NUMBER_OF_PROTOCOLS; idx++) { - if (protoVerTbl[idx] == maxProto) { - break; - } - } - if (idx < i) { - lim.mask &= ~lim.maxProtoMask; - lim.maxProtoMask = 0; - } - - ret = setMinProtoVersion(&lim, proto); + ret = applyMinProtoVersion(&lim, version); if (ret == WOLFSSL_SUCCESS) { setCtxProtoLimits(ctx, &lim); ctx->minProto = (version == 0); @@ -5395,6 +5485,36 @@ static int setMaxProtoVersion(ProtoLimits* lim, int ver) } +/* Replaces the max of the limits. Version 0 picks the highest version left. + */ +static int applyMaxProtoVersion(ProtoLimits* lim, int version) +{ + int i; + int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE); + + /* drop the versions disabled for the previous max */ + lim->mask &= ~lim->maxProtoMask; + lim->maxProtoMask = 0; + if (version != 0) { + ret = setMaxProtoVersion(lim, version); + } + else { + /* when 0 is specified as version, try to find out the max version + * from the bottom to top of the protoverTbl. + */ + for (i = NUMBER_OF_PROTOCOLS -1; i >= 0; i--) { + ret = setMaxProtoVersion(lim, protoVerTbl[i]); + if (ret == WOLFSSL_SUCCESS) { + break; + } + } + } + lim->maxProto = (version == 0); + + return ret; +} + + /** * wolfSSL_CTX_set_max_proto_version attempts to set the maximum protocol * version to use by SSL objects created from this WOLFSSL_CTX. @@ -5419,7 +5539,6 @@ static int setMaxProtoVersion(ProtoLimits* lim, int ver) */ int wolfSSL_CTX_set_max_proto_version(WOLFSSL_CTX* ctx, int version) { - int i; int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE); ProtoLimits lim; @@ -5430,27 +5549,10 @@ int wolfSSL_CTX_set_max_proto_version(WOLFSSL_CTX* ctx, int version) } getCtxProtoLimits(ctx, &lim); - /* drop the versions disabled for the previous max */ - lim.mask &= ~lim.maxProtoMask; - lim.maxProtoMask = 0; - if (version != 0) { - ret = setMaxProtoVersion(&lim, version); - } - else { - /* when 0 is specified as version, try to find out the max version - * from the bottom to top of the protoverTbl. - */ - for (i = NUMBER_OF_PROTOCOLS -1; i >= 0; i--) { - ret = setMaxProtoVersion(&lim, protoVerTbl[i]); - if (ret == WOLFSSL_SUCCESS) { - break; - } - } - } - + ret = applyMaxProtoVersion(&lim, version); if (ret == WOLFSSL_SUCCESS) { setCtxProtoLimits(ctx, &lim); - ctx->maxProto = (version == 0); + ctx->maxProto = lim.maxProto; } return ret; @@ -5458,8 +5560,7 @@ int wolfSSL_CTX_set_max_proto_version(WOLFSSL_CTX* ctx, int version) int wolfSSL_set_min_proto_version(WOLFSSL* ssl, int version) { - int i; - int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE); + int ret; ProtoLimits lim; WOLFSSL_ENTER("wolfSSL_set_min_proto_version"); @@ -5469,20 +5570,12 @@ int wolfSSL_set_min_proto_version(WOLFSSL* ssl, int version) } getSslProtoLimits(ssl, &lim); - if (version != 0) { - ret = setMinProtoVersion(&lim, version); - } - else { - /* when 0 is specified as version, try to find out the min version */ - for (i= 0; (unsigned)i < NUMBER_OF_PROTOCOLS; i++) { - ret = setMinProtoVersion(&lim, protoVerTbl[i]); - if (ret == WOLFSSL_SUCCESS) - break; - } + ret = applyMinProtoVersion(&lim, version); + if (ret == WOLFSSL_SUCCESS) { + ret = setSslProtoLimits(ssl, &lim); } - if (ret == WOLFSSL_SUCCESS) { - setSslProtoLimits(ssl, &lim); + ssl->options.minProto = (version == 0); /* Version 0 picks the lowest, so the user set no minimum */ ssl->options.minVersionSet = (version != 0); RestoreDowngrade(ssl); @@ -5493,8 +5586,7 @@ int wolfSSL_set_min_proto_version(WOLFSSL* ssl, int version) int wolfSSL_set_max_proto_version(WOLFSSL* ssl, int version) { - int i; - int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE); + int ret; ProtoLimits lim; WOLFSSL_ENTER("wolfSSL_set_max_proto_version"); @@ -5504,22 +5596,17 @@ int wolfSSL_set_max_proto_version(WOLFSSL* ssl, int version) } getSslProtoLimits(ssl, &lim); - if (version != 0) { - ret = setMaxProtoVersion(&lim, version); + ret = applyMaxProtoVersion(&lim, version); + if (ret == WOLFSSL_SUCCESS && version != 0 && sslHasCtxMax(ssl, &lim) && + lim.maxMinor > ssl->ctx->method->version.minor) { + WOLFSSL_MSG("Max above the max of the CTX"); + ret = WOLFSSL_FAILURE; } - else { - /* when 0 is specified as version, try to find out the max version - * from the bottom to top of the protoverTbl. - */ - for (i = NUMBER_OF_PROTOCOLS -1; i >= 0; i--) { - ret = setMaxProtoVersion(&lim, protoVerTbl[i]); - if (ret == WOLFSSL_SUCCESS) - break; - } + if (ret == WOLFSSL_SUCCESS) { + ret = setSslProtoLimits(ssl, &lim); } - if (ret == WOLFSSL_SUCCESS) { - setSslProtoLimits(ssl, &lim); + ssl->options.maxProto = lim.maxProto; } return ret; @@ -7557,6 +7644,11 @@ long wolfSSL_set_options(WOLFSSL* ssl, long op) } ssl->options.mask = (unsigned long)wolf_set_options((long)ssl->options.mask, op); +#ifdef OPENSSL_EXTRA + /* Versions disabled here belong to the application */ + ssl->options.minProtoMask &= ~(unsigned long)op; + ssl->options.maxProtoMask &= ~(unsigned long)op; +#endif if ((ssl->options.mask & WOLFSSL_OP_NO_TLSv1_3) == WOLFSSL_OP_NO_TLSv1_3) { WOLFSSL_MSG("Disabling TLS 1.3"); @@ -7737,6 +7829,8 @@ long wolfSSL_clear_options(WOLFSSL* ssl, long opt) if(ssl == NULL) return WOLFSSL_FAILURE; ssl->options.mask &= (unsigned long)~opt; + ssl->options.minProtoMask &= (unsigned long)~opt; + ssl->options.maxProtoMask &= (unsigned long)~opt; return (long)ssl->options.mask; } diff --git a/tests/api.c b/tests/api.c index a4d9a0a8403..945b6079c05 100644 --- a/tests/api.c +++ b/tests/api.c @@ -6741,6 +6741,8 @@ static int test_ssl_proto_version_fail(WOLFSSL* ssl, int max, int ver) { EXPECT_DECLS; unsigned long mask; + unsigned long minProtoMask; + unsigned long maxProtoMask; byte minDowngrade; byte minVersionSet; byte downgrade; @@ -6751,6 +6753,8 @@ static int test_ssl_proto_version_fail(WOLFSSL* ssl, int max, int ver) return TEST_FAIL; } mask = ssl->options.mask; + minProtoMask = ssl->options.minProtoMask; + maxProtoMask = ssl->options.maxProtoMask; minDowngrade = ssl->options.minDowngrade; minVersionSet = (byte)ssl->options.minVersionSet; downgrade = (byte)ssl->options.downgrade; @@ -6764,6 +6768,8 @@ static int test_ssl_proto_version_fail(WOLFSSL* ssl, int max, int ver) } ExpectIntEQ(ret, WC_NO_ERR_TRACE(WOLFSSL_FAILURE)); ExpectTrue(ssl->options.mask == mask); + ExpectTrue(ssl->options.minProtoMask == minProtoMask); + ExpectTrue(ssl->options.maxProtoMask == maxProtoMask); ExpectIntEQ(ssl->options.minDowngrade, minDowngrade); ExpectIntEQ(ssl->options.minVersionSet, minVersionSet); ExpectIntEQ(ssl->options.downgrade, downgrade); @@ -6979,6 +6985,10 @@ static int test_wolfSSL_CTX_set_minmax_proto_version_fail(void) ExpectIntEQ(test_proto_version_fail(ctx, 1, 0x9999), TEST_SUCCESS); ExpectIntEQ(test_proto_version_fail(ctx, 0, 0x9999), TEST_SUCCESS); ExpectIntEQ(test_proto_version_fail(ctx, 1, TLS1_2_VERSION), TEST_SUCCESS); + ExpectIntEQ(test_proto_version_fail(ctx, 0, TLS1_2_VERSION), TEST_SUCCESS); + /* An automatic min is a DTLS one */ + ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), WOLFSSL_SUCCESS); + ExpectIntGE(ctx->minDowngrade, DTLSv1_2_MINOR); wolfSSL_CTX_free(ctx); #endif #endif @@ -7093,6 +7103,11 @@ static int test_wolfSSL_set_minmax_proto_version_fail(void) TEST_SUCCESS); ExpectIntEQ(test_ssl_proto_version_fail(ssl, 1, 0x9999), TEST_SUCCESS); ExpectIntEQ(test_ssl_proto_version_fail(ssl, 0, 0x9999), TEST_SUCCESS); + ExpectIntEQ(test_ssl_proto_version_fail(ssl, 0, TLS1_2_VERSION), + TEST_SUCCESS); + /* An automatic min is a DTLS one */ + ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), WOLFSSL_SUCCESS); + ExpectIntGE(ssl->options.minDowngrade, DTLSv1_2_MINOR); wolfSSL_free(ssl); wolfSSL_CTX_free(ctx); #endif @@ -7425,6 +7440,117 @@ static int test_wolfSSL_CTX_set_max_proto_version_sigalgs(void) return EXPECT_RESULT(); } +/* A min above the max makes the highest version left the max. */ +static int test_wolfSSL_CTX_set_min_proto_version_above_max(void) +{ + EXPECT_DECLS; +#if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \ + defined(WOLFSSL_TLS13) && !defined(NO_OLD_TLS) + struct test_memio_ctx test_ctx; + WOLFSSL_CTX* ctx_c = NULL; + WOLFSSL_CTX* ctx_s = NULL; + WOLFSSL* ssl_c = NULL; + WOLFSSL* ssl_s = NULL; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, NULL, NULL, + wolfSSLv23_client_method, wolfSSLv23_server_method), 0); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx_c, TLS1_1_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx_c, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(ctx_c->method->version.minor, TLSv1_3_MINOR); + ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx_c), TLS1_3_VERSION); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfSSLv23_client_method, wolfSSLv23_server_method), 0); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + ExpectStrEQ(wolfSSL_get_version(ssl_c), "TLSv1.3"); + + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); +#endif + return EXPECT_RESULT(); +} + +#if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \ + defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) +/* Lowers the max of the client and raises it again in the way given by seq, + * then checks that TLS 1.3 is negotiated. */ +static int test_ssl_max_proto_version_raise(int seq) +{ + EXPECT_DECLS; + struct test_memio_ctx test_ctx; + WOLFSSL_CTX* ctx_c = NULL; + WOLFSSL_CTX* ctx_s = NULL; + WOLFSSL* ssl_c = NULL; + WOLFSSL* ssl_s = NULL; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfSSLv23_client_method, wolfSSLv23_server_method), 0); + ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_c, TLS1_2_VERSION), + WOLFSSL_SUCCESS); + switch (seq) { + case 0: + ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_c, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + break; + case 1: + ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_c, 0), + WOLFSSL_SUCCESS); + break; + default: + ExpectIntEQ(wolfSSL_set_min_proto_version(ssl_c, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_c, TLS1_3_VERSION), + WOLFSSL_SUCCESS); + break; + } + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + ExpectStrEQ(wolfSSL_get_version(ssl_c), "TLSv1.3"); + + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + + return EXPECT_RESULT(); +} +#endif + +/* An SSL max can be raised again. */ +static int test_wolfSSL_set_max_proto_version_raise(void) +{ + EXPECT_DECLS; +#if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \ + defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) + WOLFSSL_CTX* ctx = NULL; + WOLFSSL* ssl = NULL; + int seq; + + for (seq = 0; seq < 3; seq++) { + ExpectIntEQ(test_ssl_max_proto_version_raise(seq), TEST_SUCCESS); + } + + /* Not above the max of the ctx */ + ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method())); + ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), + WOLFSSL_SUCCESS); + ExpectNotNull(ssl = wolfSSL_new(ctx)); + ExpectIntEQ(test_ssl_proto_version_fail(ssl, 1, TLS1_3_VERSION), + TEST_SUCCESS); + ExpectIntEQ(test_ssl_proto_version_fail(ssl, 0, TLS1_3_VERSION), + TEST_SUCCESS); + ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), WOLFSSL_SUCCESS); + ExpectIntEQ(ssl->version.minor, TLSv1_2_MINOR); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); +#endif + return EXPECT_RESULT(); +} + #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \ !defined(WOLFSSL_NO_TLS12) && \ defined(BUILD_TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256) @@ -49781,6 +49907,8 @@ TEST_CASE testCases[] = { TEST_DECL(test_wolfSSL_CTX_set_max_proto_version), TEST_DECL(test_wolfSSL_CTX_set_max_proto_version_suites), TEST_DECL(test_wolfSSL_CTX_set_max_proto_version_sigalgs), + TEST_DECL(test_wolfSSL_CTX_set_min_proto_version_above_max), + TEST_DECL(test_wolfSSL_set_max_proto_version_raise), TEST_DECL(test_wolfSSL_CTX_set_psk_client_callback_suites), TEST_DECL(test_wolfSSL_CTX_allow_anon_cipher_suites), TEST_DECL(test_wolfSSL_THREADID_hash), diff --git a/wolfssl/internal.h b/wolfssl/internal.h index 9190d7671b0..158fe617f43 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -5900,6 +5900,10 @@ struct Options { void* psk_ctx; #endif /* NO_PSK */ unsigned long mask; /* store SSL_OP_ flags */ +#ifdef OPENSSL_EXTRA + unsigned long minProtoMask; /* SSL_OP_NO_* set by min version */ + unsigned long maxProtoMask; /* SSL_OP_NO_* set by max version */ +#endif #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || defined(WOLFSSL_WPAS_SMALL) word16 minProto:1; /* sets min to min available */ word16 maxProto:1; /* sets max to max available */ From c7dce4b80473f00f4dd7ec87357b19f32ab015a9 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Tue, 10 Nov 2026 06:36:27 +0000 Subject: [PATCH 5/6] Address review: document options precedence of the version setters --- src/ssl.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/ssl.c b/src/ssl.c index c62f029964d..3d868c9e558 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -5396,6 +5396,16 @@ static int applyMinProtoVersion(ProtoLimits* lim, int version) * This API also accept 0 as version, to set the minimum version automatically. * CheckSslMethodVersion() is called to check if any remaining protocol versions * are enabled. + * Options precedence: this call records the versions it disables, and the + * next min call enables them again. wolfSSL_CTX_set_options() and + * wolfSSL_CTX_clear_options() take a version out of that record, so a version + * disabled with wolfSSL_CTX_set_options() stays disabled whatever min or max + * is set later. Unlike OpenSSL, options and min/max are not kept apart. + * The SSL setters work the same way on the SSL options. + * wolfSSL_CTX_SetMinVersion() replaces the min. A min above the max drops the + * max. + * Configure the CTX before creating SSL objects from it. Later changes may + * affect them. * @param ctx The wolfSSL CONTEXT factory for spawning SSL/TLS objects * @param version Any of the following * * 0 @@ -5524,6 +5534,15 @@ static int applyMaxProtoVersion(ProtoLimits* lim, int version) * This API also accept 0 as version, to set the maximum version automatically. * CheckSslMethodVersion() is called to check if any remaining protocol versions * are enabled. + * Options precedence: this call records the versions it disables, and the + * next max call enables them again. wolfSSL_CTX_set_options() and + * wolfSSL_CTX_clear_options() take a version out of that record, so a version + * disabled with wolfSSL_CTX_set_options() stays disabled whatever min or max + * is set later. Unlike OpenSSL, options and min/max are not kept apart. + * The SSL setters work the same way on the SSL options. The CTX max bounds + * the versions of its SSLs, so an SSL max or min above it fails. + * Configure the CTX before creating SSL objects from it. Later changes may + * affect them. * @param ctx The wolfSSL CONTEXT factory for spawning SSL/TLS objects * @param version Any of the following * * 0 From 7ec58ab1ef2d2378e5c870ac1b01d2a83c65d3d7 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Fri, 13 Nov 2026 05:15:01 +0000 Subject: [PATCH 6/6] Fix CI: use the single allocation supported groups list in the test Master now holds the supported groups in one SupportedCurves array. --- tests/api.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/api.c b/tests/api.c index 945b6079c05..d728e22fd3c 100644 --- a/tests/api.c +++ b/tests/api.c @@ -22676,8 +22676,11 @@ static int test_set_groups_fail_hs(int onSsl) ExpectNotNull(ext = TLSX_Find(ssl_s->extensions, TLSX_SUPPORTED_GROUPS)); } - ExpectIntEQ(((SupportedCurve*)ext->data)->name, WOLFSSL_ECC_SECP384R1); - ExpectNull(((SupportedCurve*)ext->data)->next); + if (ext != NULL) { + ExpectIntEQ(((SupportedCurves*)ext->data)->count, 1); + ExpectIntEQ(((SupportedCurves*)ext->data)->name[0], + WOLFSSL_ECC_SECP384R1); + } ExpectIntEQ(wolfSSL_set_groups(ssl_c, &p256, 1), WOLFSSL_SUCCESS); ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),