Skip to content

Commit 0b95179

Browse files
max-qlabDominik Blain
andcommitted
crypto-policies: address granular allowlist review feedback
Six fixes for the granular allowlist back-end, in response to the GitHub Copilot pull-request review and the os-check.yml CI matrix failure on the initial PR (wolfSSL#10541). 1. src/crypto_policy_granular.c top includes. The file was using the standalone `#include <config.h>` + `#include <wolfssl/wolfcrypt/settings.h>` pattern, which trips the include-order assertion in settings.h whenever the build defines TEST_LIBWOLFSSL_SOURCES_INCLUSION_SEQUENCE — that is, on the upstream `make check linux` matrix (75 jobs failed, first observed on CPPFLAGS=-DNO_VERIFY_OID). When the module's content is gated behind WOLFSSL_SYS_CRYPTO_POLICY and the macro is undefined, the same configuration also hit ISO C's empty-translation-unit rule with -Werror=pedantic. Switch to `#include <wolfssl/wolfcrypt/libwolfssl_sources.h>` (the canonical pattern used by src/ssl.c, src/internal.c, src/tls.c) which both establishes the correct include order and pulls in types.h / error-crypt.h / logging.h so the TU is never empty. The downstream includes of logging.h and types.h become redundant and are removed. 2. DTLS-only allowlist no longer derives an empty cipher list (Copilot review, src/crypto_policy_granular.c:432). The previous derivation matched a suite's `version` field (e.g. "TLS1.2") byte-for-byte against the policy's enabled protocol tokens. A DTLS-only file (enabled-version = DTLS1.2) therefore produced zero cipher rows even though the IANA suite identifiers used by TLS 1.x and DTLS 1.x at the same minor version are identical. The new helper `wcp_protocol_family_enabled()` treats "TLS1.x" and "DTLS1.x" as suite-equivalent at the same minor version, in both directions. 3. min_version is now scoped to the CTX's protocol family (Copilot review, src/crypto_policy_granular.c:545). `wolfSSL_crypto_policy_min_version()` used to pick the numerically smallest enabled version across both families. On a DTLS CTX, that would feed a TLS constant (e.g. WOLFSSL_TLSV1_2 = 3) into `wolfSSL_CTX_SetMinVersion()`, which rejects it for the DTLS major version and the floor was silently dropped. The function now takes an `is_dtls` argument and only considers tokens of that family; apply_granular() derives `is_dtls` from `ctx->method->version.major == DTLS_MAJOR`. 4. Empty derived cipher list is now a hard failure (Copilot review, src/crypto_policy_granular.c:559). apply_granular() used to skip `wolfSSL_CTX_set_cipher_list()` when the derived list was empty and still return success, leaving the CTX's default cipher list in place — i.e. an allowlist that didn't constrain anything. It now returns WOLFSSL_FAILURE in that case, which tears the CTX down in the WOLFSSL_CTX_new_ex granular-apply branch of src/ssl.c. 5. wolfSSL_crypto_policy_get_ciphers() reflects the granular derivation (Copilot review, src/ssl.c:5473 and :5535). Both enable paths mirror the derived cipher list into `crypto_policy.str` after a successful granular parse, so the public getter no longer returns an empty string while `crypto_policy.enabled == 1`. The cipher set is identical for the TLS and DTLS families at a given minor version, so one derivation feeds both CTX families. Truncation is safe: the string is informational; the authoritative apply happens per CTX. 6. Cipher-count test helper uses the public stack accessor (Copilot review, tests/api.c:30068). `crypto_policy_cipher_count()` is replaced by a single call to `wolfSSL_sk_SSL_CIPHER_num()`. The manual loop relied on out-of-range behavior of `wolfSSL_sk_SSL_CIPHER_value()`. The DTLS section of test_wolfSSL_crypto_policy_granular() is extended to assert (a) the global policy cipher list is non-empty for a DTLS-only fixture (regression guard for fix wolfSSL#2), (b) the DTLS CTX survives apply_granular (regression guard for fixes wolfSSL#3 and wolfSSL#4), (c) the derived list round-trips through `wolfSSL_CTX_set_cipher_list()` on a fresh DTLS CTX. The test explicitly does not assert on `wolfSSL_get_ciphers_compat()` for a DTLS SSL: its `sslCipherMinMaxCheck()` compares TLS minor numbers with DTLS minor numbers, a pre-existing TLS-vs-DTLS quirk unrelated to the granular back-end. Verified against five local build configurations on Ubuntu 24.04 + gcc 13: (1) `CPPFLAGS=-DNO_VERIFY_OID` (the original failing CI config) — clean; (2) `--enable-all --enable-dtls --enable-dtls13 --with-sys-crypto-policy=...` — make check 17/17 pass; (3) default `./configure` — make check 5/5 pass; (4) `--enable-all` + `-pedantic -Werror -fsanitize=undefined -O1` — build clean (a pre-existing UBSan finding in src/ssl_crypto.c DES code is unrelated); (5) `--enable-dtls --enable-dtls13` + DTLS-only policy — our test passes. Co-Authored-By: Dominik Blain <[email protected]>
1 parent 0e8ed3c commit 0b95179

4 files changed

Lines changed: 168 additions & 39 deletions

File tree

‎src/crypto_policy_granular.c‎

Lines changed: 82 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,12 @@
3333
* none.
3434
*/
3535

36-
#ifdef HAVE_CONFIG_H
37-
#include <config.h>
38-
#endif
39-
40-
#include <wolfssl/wolfcrypt/settings.h>
36+
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
4137

4238
#if defined(WOLFSSL_SYS_CRYPTO_POLICY)
4339

4440
#include <wolfssl/ssl.h>
4541
#include <wolfssl/internal.h>
46-
#include <wolfssl/wolfcrypt/logging.h>
47-
#include <wolfssl/wolfcrypt/types.h>
4842

4943
#include <ctype.h>
5044
#include <stdlib.h>
@@ -409,6 +403,39 @@ static const char *wcp_lookup_str(const struct wcp_kv_str *m, const char *cp)
409403
/* derive cipher list */
410404
/* -------------------------------------------------------------------- */
411405

406+
/* The IANA cipher suites used by TLS 1.x and DTLS 1.x at the same minor
407+
* version are identical (the DTLS variant is encoded as an alias of the
408+
* TLS code-point). The suite table tags each row with its TLS label, so
409+
* a DTLS-only allowlist (e.g. enabled-version = DTLS1.2) must still
410+
* enable every TLS 1.2 row that survives the other constraints — and
411+
* vice versa. Treat the protocol token as "TLS 1.x family" rather than
412+
* exact string match. */
413+
static int wcp_protocol_family_enabled(const WolfCPList *protocols,
414+
const char *suite_version)
415+
{
416+
static const struct { const char *tls; const char *dtls; } pair[] = {
417+
{ "TLS1.2", "DTLS1.2" },
418+
{ "TLS1.3", "DTLS1.3" },
419+
{ NULL, NULL }
420+
};
421+
int i;
422+
423+
if (wcp_has(protocols, suite_version)) {
424+
return 1;
425+
}
426+
for (i = 0; pair[i].tls != NULL; i++) {
427+
if (XSTRCMP(suite_version, pair[i].tls) == 0
428+
&& wcp_has(protocols, pair[i].dtls)) {
429+
return 1;
430+
}
431+
if (XSTRCMP(suite_version, pair[i].dtls) == 0
432+
&& wcp_has(protocols, pair[i].tls)) {
433+
return 1;
434+
}
435+
}
436+
return 0;
437+
}
438+
412439
int wolfSSL_crypto_policy_derive_cipher_list(const WolfGranularPolicy *p,
413440
char *out, size_t outlen)
414441
{
@@ -427,7 +454,7 @@ int wolfSSL_crypto_policy_derive_cipher_list(const WolfGranularPolicy *p,
427454
size_t need;
428455

429456
if (!wcp_has(&p->ciphers, s->cipher)) continue;
430-
if (!wcp_has(&p->protocols, s->version)) continue;
457+
if (!wcp_protocol_family_enabled(&p->protocols, s->version)) continue;
431458
if (s->kx[0] != '\0' && !wcp_has(&p->kx, s->kx)) continue;
432459
if (!wcp_has(&p->macs, s->mac)) continue;
433460

@@ -490,7 +517,14 @@ int wolfSSL_crypto_policy_derive_sigalgs_list(const WolfGranularPolicy *p,
490517
/* lowest enabled TLS/DTLS version */
491518
/* -------------------------------------------------------------------- */
492519

493-
int wolfSSL_crypto_policy_min_version(const WolfGranularPolicy *p)
520+
/* `wolfSSL_CTX_SetMinVersion` rejects a TLS constant on a DTLS CTX (and
521+
* vice versa). The min-version computation must therefore be scoped to
522+
* the CTX's protocol family, otherwise a policy that enables both TLS
523+
* and DTLS would pass a TLS constant into a DTLS CTX and the floor
524+
* would be silently dropped. is_dtls != 0 restricts the search to DTLS
525+
* tokens, is_dtls == 0 restricts it to TLS tokens. */
526+
int wolfSSL_crypto_policy_min_version(const WolfGranularPolicy *p,
527+
int is_dtls)
494528
{
495529
int i;
496530
int best = -1;
@@ -499,13 +533,22 @@ int wolfSSL_crypto_policy_min_version(const WolfGranularPolicy *p)
499533
if (p == NULL) return -1;
500534

501535
for (i = 0; i < p->protocols.count; i++) {
502-
int v = wcp_lookup_int(wcp_version_map, p->protocols.tok[i]);
503-
int pri;
536+
const char *tok = p->protocols.tok[i];
537+
int v;
538+
int tok_is_dtls = (XSTRNCMP(tok, "DTLS", 4) == 0);
539+
if (tok_is_dtls != (is_dtls != 0)) {
540+
continue;
541+
}
542+
v = wcp_lookup_int(wcp_version_map, tok);
504543
if (v < 0) continue;
505-
/* Pin "min" to numerically lowest (TLSV1=1, TLSV1_3=4). */
506-
pri = v;
507-
if (pri < best_pri) {
508-
best_pri = pri;
544+
/* Pin "min" to numerically lowest within the family. The
545+
* wolfSSL public enum is monotonic per family: TLSV1=1 <
546+
* TLSV1_1=2 < TLSV1_2=3 < TLSV1_3=4, and DTLSV1=5 <
547+
* DTLSV1_2=6 < DTLSV1_3=7. We've already filtered by family
548+
* above, so the smallest value is the oldest version in that
549+
* family — the right floor. */
550+
if (v < best_pri) {
551+
best_pri = v;
509552
best = v;
510553
}
511554
}
@@ -523,19 +566,27 @@ int wolfSSL_crypto_policy_apply_granular(WOLFSSL_CTX *ctx,
523566
int i;
524567
char buf[2048];
525568
int min_ver;
569+
int is_dtls;
526570

527571
if (ctx == NULL || p == NULL) {
528572
return BAD_FUNC_ARG;
529573
}
530574

531575
WOLFSSL_ENTER("wolfSSL_crypto_policy_apply_granular");
532576

577+
is_dtls = (ctx->method != NULL
578+
&& ctx->method->version.major == DTLS_MAJOR);
579+
533580
/* 1. Protocol min version. Best-effort: a TLS 1.0 floor against a
534581
* build that lacks WOLFSSL_ALLOW_TLSV10 must not tear down the CTX
535582
* — we keep the wolfSSL-default downgrade floor and let the cipher
536583
* list + key-size floors carry the policy. The caller will still
537-
* negotiate within the build's supported version range. */
538-
min_ver = wolfSSL_crypto_policy_min_version(p);
584+
* negotiate within the build's supported version range.
585+
*
586+
* We resolve the floor inside the CTX's protocol family. Passing a
587+
* TLS constant to a DTLS CTX (or vice versa) makes SetMinVersion
588+
* fail and the floor would be silently dropped. */
589+
min_ver = wolfSSL_crypto_policy_min_version(p, is_dtls);
539590
if (min_ver >= 0) {
540591
rc = wolfSSL_CTX_SetMinVersion(ctx, min_ver);
541592
if (rc != WOLFSSL_SUCCESS) {
@@ -544,18 +595,25 @@ int wolfSSL_crypto_policy_apply_granular(WOLFSSL_CTX *ctx,
544595
}
545596
}
546597

547-
/* 2. Cipher list. */
598+
/* 2. Cipher list. An allowlist is authoritative: if the
599+
* intersection of policy-enabled cipher suites and the suite table
600+
* is empty, the CTX would silently keep its default cipher list
601+
* and the policy would not actually constrain anything. Refuse
602+
* outright in that case. */
548603
rc = wolfSSL_crypto_policy_derive_cipher_list(p, buf, sizeof(buf));
549604
if (rc != WOLF_CP_OK) {
550605
WOLFSSL_MSG("granular policy: cipher list derivation failed");
551606
return WOLFSSL_FAILURE;
552607
}
553-
if (buf[0] != '\0') {
554-
rc = wolfSSL_CTX_set_cipher_list(ctx, buf);
555-
if (rc != WOLFSSL_SUCCESS) {
556-
WOLFSSL_MSG_EX("granular policy: set_cipher_list failed: %d", rc);
557-
return rc;
558-
}
608+
if (buf[0] == '\0') {
609+
WOLFSSL_MSG("granular policy: derived cipher list is empty — "
610+
"policy enables no suites this build can serve");
611+
return WOLFSSL_FAILURE;
612+
}
613+
rc = wolfSSL_CTX_set_cipher_list(ctx, buf);
614+
if (rc != WOLFSSL_SUCCESS) {
615+
WOLFSSL_MSG_EX("granular policy: set_cipher_list failed: %d", rc);
616+
return rc;
559617
}
560618

561619
/* 3. Supported groups (TLS named groups). */

‎src/crypto_policy_granular.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ WOLFSSL_LOCAL int wolfSSL_crypto_policy_derive_cipher_list(
6464
WOLFSSL_LOCAL int wolfSSL_crypto_policy_derive_sigalgs_list(
6565
const WolfGranularPolicy *p, char *out, size_t outlen);
6666

67-
/* Lowest TLS/DTLS version enabled. Returns -1 if none. */
67+
/* Lowest enabled version inside the requested protocol family.
68+
* is_dtls != 0 considers only DTLS tokens, is_dtls == 0 considers
69+
* only TLS tokens. Returns -1 if no token of that family is enabled. */
6870
WOLFSSL_LOCAL int wolfSSL_crypto_policy_min_version(
69-
const WolfGranularPolicy *p);
71+
const WolfGranularPolicy *p, int is_dtls);
7072

7173
/* Apply the parsed policy to a CTX: drive SetMinVersion,
7274
* set_cipher_list, UseSupportedCurve, set1_sigalgs_list and

‎src/ssl.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5468,6 +5468,21 @@ int wolfSSL_crypto_policy_enable(const char * policy_file)
54685468
crypto_policy.secLevel = crypto_policy_gran.security_level > 0
54695469
? crypto_policy_gran.security_level
54705470
: 0;
5471+
/* Mirror the derived cipher list into the legacy str buffer so
5472+
* wolfSSL_crypto_policy_get_ciphers() returns the actual list
5473+
* the policy enables (and not the empty string while
5474+
* `enabled == 1`). The cipher set is the same for the TLS and
5475+
* DTLS protocol families at a given minor version, so a single
5476+
* derivation feeds both CTX families. Truncation is safe: the
5477+
* string is informational, the authoritative apply happens per
5478+
* CTX in wolfSSL_crypto_policy_apply_granular(). */
5479+
rc = wolfSSL_crypto_policy_derive_cipher_list(
5480+
&crypto_policy_gran,
5481+
crypto_policy.str,
5482+
sizeof(crypto_policy.str));
5483+
if (rc != WOLF_CP_OK) {
5484+
crypto_policy.str[0] = '\0';
5485+
}
54715486
crypto_policy.enabled = 1;
54725487
crypto_policy_gran_enabled = 1;
54735488
return WOLFSSL_SUCCESS;
@@ -5529,6 +5544,15 @@ int wolfSSL_crypto_policy_enable_buffer(const char * buf)
55295544
crypto_policy.secLevel = crypto_policy_gran.security_level > 0
55305545
? crypto_policy_gran.security_level
55315546
: 0;
5547+
/* Mirror the derived cipher list — see comment in
5548+
* wolfSSL_crypto_policy_enable() above. */
5549+
rc = wolfSSL_crypto_policy_derive_cipher_list(
5550+
&crypto_policy_gran,
5551+
crypto_policy.str,
5552+
sizeof(crypto_policy.str));
5553+
if (rc != WOLF_CP_OK) {
5554+
crypto_policy.str[0] = '\0';
5555+
}
55325556
crypto_policy.enabled = 1;
55335557
crypto_policy_gran_enabled = 1;
55345558
return WOLFSSL_SUCCESS;

‎tests/api.c‎

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30045,8 +30045,6 @@ static int test_wolfSSL_crypto_policy_ciphers(void)
3004530045
static int crypto_policy_cipher_count(const WOLFSSL * ssl)
3004630046
{
3004730047
WOLF_STACK_OF(WOLFSSL_CIPHER) * sk = NULL;
30048-
WOLFSSL_CIPHER * current = NULL;
30049-
int i = 0;
3005030048

3005130049
if (ssl == NULL) {
3005230050
return -1;
@@ -30058,14 +30056,7 @@ static int crypto_policy_cipher_count(const WOLFSSL * ssl)
3005830056
return 0;
3005930057
}
3006030058

30061-
do {
30062-
current = wolfSSL_sk_SSL_CIPHER_value(sk, i);
30063-
if (current) {
30064-
i++;
30065-
}
30066-
} while (current);
30067-
30068-
return i;
30059+
return wolfSSL_sk_SSL_CIPHER_num(sk);
3006930060
}
3007030061
#endif
3007130062

@@ -30160,10 +30151,36 @@ static int test_wolfSSL_crypto_policy_granular(void)
3016030151
ExpectIntGT(legacy_suites, future_suites);
3016130152

3016230153
/* DTLS-only fixture: the parser/applier must accept a policy whose
30163-
* sole protocol is DTLS 1.2 and produce a usable CTX. */
30154+
* sole protocol is DTLS 1.2 and produce a usable CTX whose cipher
30155+
* list reflects the policy. A previous version of the granular
30156+
* back-end only matched the exact "TLS1.x" token against suites,
30157+
* so a DTLS-only policy derived an empty cipher list and the
30158+
* applier silently kept the CTX's default suites — i.e. the
30159+
* allowlist did not constrain anything.
30160+
*
30161+
* We assert:
30162+
* (a) get_ciphers() returns a non-empty derived list, proving
30163+
* DTLS1.2 protocol tokens enable their TLS1.2 cipher peers
30164+
* in the derivation (fix for the DTLS suite-match gap);
30165+
* (b) the DTLS CTX is created, proving apply_granular ran to
30166+
* success — empty derive now returns WOLFSSL_FAILURE and
30167+
* the CTX would be torn down before reaching this point;
30168+
* (c) the derived list round-trips through set_cipher_list on
30169+
* a fresh DTLS CTX (the CTX-level cipher store accepts the
30170+
* suite names the granular derivation produced).
30171+
*
30172+
* We deliberately do NOT assert on wolfSSL_get_ciphers_compat()
30173+
* for a DTLS SSL here: its sslCipherMinMaxCheck() compares the
30174+
* TLS minor of each suite (e.g. TLSv1_2_MINOR = 3) against the
30175+
* SSL's options.minDowngrade, which on a DTLS CTX holds a DTLS
30176+
* minor (e.g. DTLSv1_2_MINOR = 0xfd). The comparison is a
30177+
* pre-existing wolfSSL TLS-vs-DTLS minor quirk, unrelated to
30178+
* the granular back-end, so we leave it alone. */
3016430179
#ifdef WOLFSSL_DTLS
3016530180
{
30166-
WOLFSSL_CTX * dtls_ctx = NULL;
30181+
WOLFSSL_CTX * dtls_ctx = NULL;
30182+
WOLFSSL_CTX * dtls_ctx_rt = NULL;
30183+
const char * gran_str = NULL;
3016730184

3016830185
rc = wolfSSL_crypto_policy_enable(
3016930186
"examples/crypto_policies/default/wolfssl-allowlist-dtls.txt");
@@ -30172,12 +30189,40 @@ static int test_wolfSSL_crypto_policy_granular(void)
3017230189
rc = wolfSSL_crypto_policy_is_enabled();
3017330190
ExpectIntEQ(rc, 1);
3017430191

30192+
/* (a) Derived cipher list mirrored to the legacy str buffer. */
30193+
gran_str = wolfSSL_crypto_policy_get_ciphers();
30194+
ExpectNotNull(gran_str);
30195+
if (gran_str != NULL) {
30196+
ExpectIntGT((int)XSTRLEN(gran_str), 0);
30197+
}
30198+
30199+
/* (b) DTLS CTX survives apply_granular. */
3017530200
dtls_ctx = wolfSSL_CTX_new(wolfDTLS_method());
3017630201
ExpectNotNull(dtls_ctx);
30177-
3017830202
if (dtls_ctx != NULL) {
3017930203
wolfSSL_CTX_free(dtls_ctx);
3018030204
}
30205+
30206+
/* (c) Round-trip: the derived list is a string set_cipher_list
30207+
* accepts on a DTLS CTX. We disable the policy first so the
30208+
* CTX is built without the apply step, then drive the API
30209+
* directly with the previously-captured string. */
30210+
if (gran_str != NULL && gran_str[0] != '\0') {
30211+
char gran_copy[MAX_WOLFSSL_CRYPTO_POLICY_SIZE + 1];
30212+
XSTRNCPY(gran_copy, gran_str, sizeof(gran_copy) - 1);
30213+
gran_copy[sizeof(gran_copy) - 1] = '\0';
30214+
30215+
wolfSSL_crypto_policy_disable();
30216+
30217+
dtls_ctx_rt = wolfSSL_CTX_new(wolfDTLS_method());
30218+
ExpectNotNull(dtls_ctx_rt);
30219+
if (dtls_ctx_rt != NULL) {
30220+
ExpectIntEQ(wolfSSL_CTX_set_cipher_list(dtls_ctx_rt,
30221+
gran_copy),
30222+
WOLFSSL_SUCCESS);
30223+
wolfSSL_CTX_free(dtls_ctx_rt);
30224+
}
30225+
}
3018130226
wolfSSL_crypto_policy_disable();
3018230227
}
3018330228
#endif /* WOLFSSL_DTLS */

0 commit comments

Comments
 (0)