From 463b0838290078a2443a8f795554f17d8c7d42cd Mon Sep 17 00:00:00 2001 From: rue19 Date: Wed, 25 Nov 2026 15:43:34 +0530 Subject: [PATCH 1/3] Don't ignore SSLOptions when GNU TLS has no SYSTEM priority The priority string started with "@SYSTEM,", which GNU TLS only accepts when its configuration file defines a SYSTEM priority. Debian and Ubuntu do not, so the whole string was rejected, the error was ignored, and the session kept the default priorities - every SSLOptions setting (MinTLS, MaxTLS, DenyCBC, ...) was silently dropped. Mirror the fix from CUPS (OpenPrinting/cups#1677, #1701): probe "@SYSTEM" before using it, and fail the connection if the priorities cannot be set. --- cups/tls-gnutls.c | 50 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/cups/tls-gnutls.c b/cups/tls-gnutls.c index 5a622863e..2d5f58eff 100644 --- a/cups/tls-gnutls.c +++ b/cups/tls-gnutls.c @@ -1875,10 +1875,26 @@ _httpTLSStart(http_t *http) // I - Connection to server return (false); } - if (tls_options & _HTTP_TLS_NO_SYSTEM) - priority_string[0] = '\0'; - else - cupsCopyString(priority_string, "@SYSTEM,", sizeof(priority_string)); + priority_string[0] = '\0'; + + if (!(tls_options & _HTTP_TLS_NO_SYSTEM)) + { + // Named system priorities are not configured on every system; only use + // them when available so the options below are not discarded... +#ifdef HAVE_GNUTLS_PRIORITY_SET_DIRECT + if (!gnutls_priority_set_direct(http->tls, "@SYSTEM", NULL)) + cupsCopyString(priority_string, "@SYSTEM,", sizeof(priority_string)); + +#else + gnutls_priority_t system_priority; // System priority + + if (!gnutls_priority_init(&system_priority, "@SYSTEM", NULL)) + { + gnutls_priority_deinit(system_priority); + cupsCopyString(priority_string, "@SYSTEM,", sizeof(priority_string)); + } +#endif // HAVE_GNUTLS_PRIORITY_SET_DIRECT + } cupsConcatString(priority_string, "NORMAL", sizeof(priority_string)); @@ -1919,16 +1935,34 @@ _httpTLSStart(http_t *http) // I - Connection to server cupsConcatString(priority_string, ":!AES-128-CBC:!AES-256-CBC:!CAMELLIA-128-CBC:!CAMELLIA-256-CBC:!3DES-CBC", sizeof(priority_string)); #ifdef HAVE_GNUTLS_PRIORITY_SET_DIRECT - gnutls_priority_set_direct(http->tls, priority_string, NULL); + status = gnutls_priority_set_direct(http->tls, priority_string, NULL); #else gnutls_priority_t priority; // Priority - gnutls_priority_init(&priority, priority_string, NULL); - gnutls_priority_set(http->tls, priority); - gnutls_priority_deinit(priority); + status = gnutls_priority_init(&priority, priority_string, NULL); + if (!status) + { + status = gnutls_priority_set(http->tls, priority); + gnutls_priority_deinit(priority); + } #endif // HAVE_GNUTLS_PRIORITY_SET_DIRECT + if (status) + { + http->error = EIO; + http->status = HTTP_STATUS_ERROR; + + DEBUG_printf("4_httpTLSStart: Unable to set TLS priorities: %s", gnutls_strerror(status)); + _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, gnutls_strerror(status), 0); + + gnutls_deinit(http->tls); + _httpFreeCredentials(credentials); + http->tls = NULL; + + return (false); + } + gnutls_transport_set_ptr(http->tls, (gnutls_transport_ptr_t)http); gnutls_transport_set_pull_function(http->tls, gnutls_http_read); #ifdef HAVE_GNUTLS_TRANSPORT_SET_PULL_TIMEOUT_FUNCTION From 4e968f3c09908c68a2e1e3c850e2a2074b62e672 Mon Sep 17 00:00:00 2001 From: rue19 Date: Wed, 25 Nov 2026 15:43:34 +0530 Subject: [PATCH 2/3] Add GNU TLS SSLOptions test to the GNU TLS CI job Port test/testssloptions.py from CUPS (OpenPrinting/cups#1701) to the libcups 3 API. It builds a small client with the flags from Makedefs, runs it against local Python TLS servers with and without a SYSTEM priority, and checks that the SSLOptions in client.conf are honored. Like in CUPS it is only run by the GNU TLS job in build.yml, so the build system does not depend on Python. --- .github/workflows/build.yml | 6 +- scripts/testssloptions.py | 257 ++++++++++++++++++++++++++++++++++++ 2 files changed, 262 insertions(+), 1 deletion(-) create mode 100755 scripts/testssloptions.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0bda0254d..ba1b6a569 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -56,7 +56,7 @@ jobs: - name: Update Build Environment run: sudo apt-get update --fix-missing -y - name: Install Prerequisites - run: sudo apt-get install -y avahi-daemon ca-certificates libavahi-client-dev libgnutls28-dev libpam-dev libpng-dev libusb-1.0-0-dev zlib1g-dev + run: sudo apt-get install -y avahi-daemon ca-certificates libavahi-client-dev libgnutls28-dev libpam-dev libpng-dev libusb-1.0-0-dev openssl python3 zlib1g-dev - name: Start Avahi run: sudo systemctl start avahi-daemon - name: Configure libcups @@ -71,6 +71,10 @@ jobs: CUPS_DEBUG_LOG: test-debug.log CUPS_DEBUG_LEVEL: 4 run: make test + - name: Test GNU TLS SSLOptions + env: + ASAN_OPTIONS: leak_check_at_exit=false + run: python3 scripts/testssloptions.py - name: Upload Test Results uses: actions/upload-artifact@v7 if: ${{ !cancelled() }} diff --git a/scripts/testssloptions.py b/scripts/testssloptions.py new file mode 100755 index 000000000..8342bf42d --- /dev/null +++ b/scripts/testssloptions.py @@ -0,0 +1,257 @@ +#!/usr/bin/env python3 +# +# GNU TLS SSLOptions integration tests for libcups. +# +# Copyright © 2026 by OpenPrinting. +# +# Licensed under Apache License v2.0. See the file "LICENSE" for more +# information. +# +# Usage (after building with --with-tls=gnutls): +# +# python3 scripts/testssloptions.py +# +# Requires Python 3 with TLS 1.3 support and the openssl command. The test +# client is built with the compiler and flags from Makedefs and linked against +# the static libcups. All certificates, configuration, and connections are +# local to this test. +# +# Must be run as a normal user - libcups ignores CUPS_SYSCONFIG and +# CUPS_USERCONFIG when running as root. +# + +import os +import re +import shlex +import socket +import ssl +import subprocess +import tempfile +import threading +from pathlib import Path + +root = Path(__file__).resolve().parent.parent + +if "#define HAVE_GNUTLS 1" not in (root / "config.h").read_text(): + raise SystemExit("Configure libcups with --with-tls=gnutls before running this test.") + +if os.getuid() == 0: + raise SystemExit("Run this test as a normal user, not root.") + +# Get the compiler and flags from Makedefs... +makedefs = {} +for line in (root / "Makedefs").read_text().splitlines(): + if m := re.match(r"^([A-Z_]+)\s*=\s*(.*)$", line): + makedefs[m.group(1)] = m.group(2) + +failures = 0 + +with tempfile.TemporaryDirectory(prefix="libcups-ssloptions-") as d: + p = Path(d) + (p / "client.c").write_text(r""" +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + char security[1024]; // Negotiated TLS settings + http_t *http; // Connection to the test server + + if (argc != 2) + return (2); + + http = httpConnect("localhost", atoi(argv[1]), NULL, AF_INET, + HTTP_ENCRYPTION_ALWAYS, true, 2000, NULL); + if (!http) + { + fprintf(stderr, "%s\n", cupsGetErrorString()); + return (1); + } + + puts(httpGetSecurity(http, security, sizeof(security))); + httpClose(http); + return (0); +} +""") + subprocess.run( + shlex.split(os.environ.get("CC", makedefs.get("CC", "cc"))) + + shlex.split(makedefs.get("OPTIM", "")) + + shlex.split(makedefs.get("LDFLAGS", "")) + + [ + "-I" + str(root), + "-o", + str(p / "client"), + str(p / "client.c"), + str(root / "cups" / "libcups3.a"), + ] + + shlex.split(makedefs.get("LIBS", "")), + check=True, + ) + subprocess.run( + [ + os.environ.get("OPENSSL", "openssl"), + "req", + "-x509", + "-newkey", + "rsa:2048", + "-nodes", + "-keyout", + str(p / "key.pem"), + "-out", + str(p / "cert.pem"), + "-days", + "1", + "-subj", + "/CN=localhost", + ], + check=True, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + + # Each case is (name, GNU TLS system config, SSLOptions, server minimum + # TLS version, server maximum TLS version, client should connect, + # expected text in output or cipher suite to force on the server) + cases = [ + ( + "missing SYSTEM honors MaxTLS1.2", + "", + "MinTLS1.2 MaxTLS1.2", + ssl.TLSVersion.TLSv1_2, + ssl.TLSVersion.TLSv1_3, + True, + "TLS/1.2", + ), + ( + "missing SYSTEM rejects TLS1.3-only server", + "", + "MinTLS1.2 MaxTLS1.2", + ssl.TLSVersion.TLSv1_3, + ssl.TLSVersion.TLSv1_3, + False, + None, + ), + ( + "missing SYSTEM honors MinTLS1.3", + "", + "MinTLS1.3", + ssl.TLSVersion.TLSv1_2, + ssl.TLSVersion.TLSv1_2, + False, + None, + ), + ( + "NoSystem remains functional", + "", + "NoSystem MinTLS1.2 MaxTLS1.2", + ssl.TLSVersion.TLSv1_2, + ssl.TLSVersion.TLSv1_3, + True, + "TLS/1.2", + ), + ( + "configured SYSTEM remains functional", + "[priorities]\nSYSTEM = NORMAL\n", + "MinTLS1.2 MaxTLS1.2", + ssl.TLSVersion.TLSv1_2, + ssl.TLSVersion.TLSv1_3, + True, + "TLS/1.2", + ), + ( + "configured SYSTEM keeps cipher restriction", + "[priorities]\nSYSTEM = NORMAL:-AES-128-GCM\n", + "MinTLS1.2 MaxTLS1.2", + ssl.TLSVersion.TLSv1_2, + ssl.TLSVersion.TLSv1_2, + False, + "GCM", + ), + ( + "NoSystem bypasses named cipher restriction", + "[priorities]\nSYSTEM = NORMAL:-AES-128-GCM\n", + "NoSystem MinTLS1.2 MaxTLS1.2", + ssl.TLSVersion.TLSv1_2, + ssl.TLSVersion.TLSv1_2, + True, + "GCM", + ), + ( + "missing SYSTEM honors DenyCBC", + "", + "MinTLS1.2 MaxTLS1.2 DenyCBC", + ssl.TLSVersion.TLSv1_2, + ssl.TLSVersion.TLSv1_2, + False, + "CBC", + ), + ] + + for name, policy, options, minimum, maximum, success, expected in cases: + (p / "gnutls.conf").write_text(policy) + (p / "client.conf").write_text("SSLOptions " + options + "\n") + + ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + ctx.minimum_version = minimum + ctx.maximum_version = maximum + ctx.load_cert_chain(p / "cert.pem", p / "key.pem") + if expected == "CBC": + ctx.set_ciphers("ECDHE-RSA-AES128-SHA256") + if expected == "GCM": + ctx.set_ciphers("ECDHE-RSA-AES128-GCM-SHA256") + + listener = socket.socket() + listener.bind(("127.0.0.1", 0)) + listener.listen() + listener.settimeout(5) + port = listener.getsockname()[1] + + def serve(listener, ctx): + try: + conn, _ = listener.accept() + with conn: + conn.settimeout(4) + with ctx.wrap_socket(conn, server_side=True) as tls: + tls.recv(1) + except (ssl.SSLError, OSError): + pass + finally: + listener.close() + + thread = threading.Thread(target=serve, args=(listener, ctx)) + thread.start() + + env = dict( + os.environ, + GNUTLS_SYSTEM_PRIORITY_FILE=str(p / "gnutls.conf"), + CUPS_SYSCONFIG=d, + CUPS_USERCONFIG=d, + ) + r = subprocess.run( + [str(p / "client"), str(port)], + env=env, + check=False, + capture_output=True, + text=True, + timeout=8, + ) + thread.join(6) + + if success: + passed = r.returncode == 0 and expected in r.stdout + else: + passed = r.returncode == 1 and "TLS" in r.stderr + failures += not passed + + print( + ("PASS" if passed else "FAIL"), + name, + "=>", + (r.stdout + r.stderr).strip(), + flush=True, + ) + +print("Failures:", failures) +raise SystemExit(bool(failures)) From 18ce91caee64b1305540f150d3941a4823858db2 Mon Sep 17 00:00:00 2001 From: rue19 Date: Wed, 25 Nov 2026 19:43:48 +0530 Subject: [PATCH 3/3] Remove the Python GNU TLS SSLOptions test Python isn't portable enough for the libcups test suite. --- .github/workflows/build.yml | 6 +- scripts/testssloptions.py | 257 ------------------------------------ 2 files changed, 1 insertion(+), 262 deletions(-) delete mode 100755 scripts/testssloptions.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ba1b6a569..0bda0254d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -56,7 +56,7 @@ jobs: - name: Update Build Environment run: sudo apt-get update --fix-missing -y - name: Install Prerequisites - run: sudo apt-get install -y avahi-daemon ca-certificates libavahi-client-dev libgnutls28-dev libpam-dev libpng-dev libusb-1.0-0-dev openssl python3 zlib1g-dev + run: sudo apt-get install -y avahi-daemon ca-certificates libavahi-client-dev libgnutls28-dev libpam-dev libpng-dev libusb-1.0-0-dev zlib1g-dev - name: Start Avahi run: sudo systemctl start avahi-daemon - name: Configure libcups @@ -71,10 +71,6 @@ jobs: CUPS_DEBUG_LOG: test-debug.log CUPS_DEBUG_LEVEL: 4 run: make test - - name: Test GNU TLS SSLOptions - env: - ASAN_OPTIONS: leak_check_at_exit=false - run: python3 scripts/testssloptions.py - name: Upload Test Results uses: actions/upload-artifact@v7 if: ${{ !cancelled() }} diff --git a/scripts/testssloptions.py b/scripts/testssloptions.py deleted file mode 100755 index 8342bf42d..000000000 --- a/scripts/testssloptions.py +++ /dev/null @@ -1,257 +0,0 @@ -#!/usr/bin/env python3 -# -# GNU TLS SSLOptions integration tests for libcups. -# -# Copyright © 2026 by OpenPrinting. -# -# Licensed under Apache License v2.0. See the file "LICENSE" for more -# information. -# -# Usage (after building with --with-tls=gnutls): -# -# python3 scripts/testssloptions.py -# -# Requires Python 3 with TLS 1.3 support and the openssl command. The test -# client is built with the compiler and flags from Makedefs and linked against -# the static libcups. All certificates, configuration, and connections are -# local to this test. -# -# Must be run as a normal user - libcups ignores CUPS_SYSCONFIG and -# CUPS_USERCONFIG when running as root. -# - -import os -import re -import shlex -import socket -import ssl -import subprocess -import tempfile -import threading -from pathlib import Path - -root = Path(__file__).resolve().parent.parent - -if "#define HAVE_GNUTLS 1" not in (root / "config.h").read_text(): - raise SystemExit("Configure libcups with --with-tls=gnutls before running this test.") - -if os.getuid() == 0: - raise SystemExit("Run this test as a normal user, not root.") - -# Get the compiler and flags from Makedefs... -makedefs = {} -for line in (root / "Makedefs").read_text().splitlines(): - if m := re.match(r"^([A-Z_]+)\s*=\s*(.*)$", line): - makedefs[m.group(1)] = m.group(2) - -failures = 0 - -with tempfile.TemporaryDirectory(prefix="libcups-ssloptions-") as d: - p = Path(d) - (p / "client.c").write_text(r""" -#include -#include -#include - -int -main(int argc, char *argv[]) -{ - char security[1024]; // Negotiated TLS settings - http_t *http; // Connection to the test server - - if (argc != 2) - return (2); - - http = httpConnect("localhost", atoi(argv[1]), NULL, AF_INET, - HTTP_ENCRYPTION_ALWAYS, true, 2000, NULL); - if (!http) - { - fprintf(stderr, "%s\n", cupsGetErrorString()); - return (1); - } - - puts(httpGetSecurity(http, security, sizeof(security))); - httpClose(http); - return (0); -} -""") - subprocess.run( - shlex.split(os.environ.get("CC", makedefs.get("CC", "cc"))) - + shlex.split(makedefs.get("OPTIM", "")) - + shlex.split(makedefs.get("LDFLAGS", "")) - + [ - "-I" + str(root), - "-o", - str(p / "client"), - str(p / "client.c"), - str(root / "cups" / "libcups3.a"), - ] - + shlex.split(makedefs.get("LIBS", "")), - check=True, - ) - subprocess.run( - [ - os.environ.get("OPENSSL", "openssl"), - "req", - "-x509", - "-newkey", - "rsa:2048", - "-nodes", - "-keyout", - str(p / "key.pem"), - "-out", - str(p / "cert.pem"), - "-days", - "1", - "-subj", - "/CN=localhost", - ], - check=True, - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, - ) - - # Each case is (name, GNU TLS system config, SSLOptions, server minimum - # TLS version, server maximum TLS version, client should connect, - # expected text in output or cipher suite to force on the server) - cases = [ - ( - "missing SYSTEM honors MaxTLS1.2", - "", - "MinTLS1.2 MaxTLS1.2", - ssl.TLSVersion.TLSv1_2, - ssl.TLSVersion.TLSv1_3, - True, - "TLS/1.2", - ), - ( - "missing SYSTEM rejects TLS1.3-only server", - "", - "MinTLS1.2 MaxTLS1.2", - ssl.TLSVersion.TLSv1_3, - ssl.TLSVersion.TLSv1_3, - False, - None, - ), - ( - "missing SYSTEM honors MinTLS1.3", - "", - "MinTLS1.3", - ssl.TLSVersion.TLSv1_2, - ssl.TLSVersion.TLSv1_2, - False, - None, - ), - ( - "NoSystem remains functional", - "", - "NoSystem MinTLS1.2 MaxTLS1.2", - ssl.TLSVersion.TLSv1_2, - ssl.TLSVersion.TLSv1_3, - True, - "TLS/1.2", - ), - ( - "configured SYSTEM remains functional", - "[priorities]\nSYSTEM = NORMAL\n", - "MinTLS1.2 MaxTLS1.2", - ssl.TLSVersion.TLSv1_2, - ssl.TLSVersion.TLSv1_3, - True, - "TLS/1.2", - ), - ( - "configured SYSTEM keeps cipher restriction", - "[priorities]\nSYSTEM = NORMAL:-AES-128-GCM\n", - "MinTLS1.2 MaxTLS1.2", - ssl.TLSVersion.TLSv1_2, - ssl.TLSVersion.TLSv1_2, - False, - "GCM", - ), - ( - "NoSystem bypasses named cipher restriction", - "[priorities]\nSYSTEM = NORMAL:-AES-128-GCM\n", - "NoSystem MinTLS1.2 MaxTLS1.2", - ssl.TLSVersion.TLSv1_2, - ssl.TLSVersion.TLSv1_2, - True, - "GCM", - ), - ( - "missing SYSTEM honors DenyCBC", - "", - "MinTLS1.2 MaxTLS1.2 DenyCBC", - ssl.TLSVersion.TLSv1_2, - ssl.TLSVersion.TLSv1_2, - False, - "CBC", - ), - ] - - for name, policy, options, minimum, maximum, success, expected in cases: - (p / "gnutls.conf").write_text(policy) - (p / "client.conf").write_text("SSLOptions " + options + "\n") - - ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) - ctx.minimum_version = minimum - ctx.maximum_version = maximum - ctx.load_cert_chain(p / "cert.pem", p / "key.pem") - if expected == "CBC": - ctx.set_ciphers("ECDHE-RSA-AES128-SHA256") - if expected == "GCM": - ctx.set_ciphers("ECDHE-RSA-AES128-GCM-SHA256") - - listener = socket.socket() - listener.bind(("127.0.0.1", 0)) - listener.listen() - listener.settimeout(5) - port = listener.getsockname()[1] - - def serve(listener, ctx): - try: - conn, _ = listener.accept() - with conn: - conn.settimeout(4) - with ctx.wrap_socket(conn, server_side=True) as tls: - tls.recv(1) - except (ssl.SSLError, OSError): - pass - finally: - listener.close() - - thread = threading.Thread(target=serve, args=(listener, ctx)) - thread.start() - - env = dict( - os.environ, - GNUTLS_SYSTEM_PRIORITY_FILE=str(p / "gnutls.conf"), - CUPS_SYSCONFIG=d, - CUPS_USERCONFIG=d, - ) - r = subprocess.run( - [str(p / "client"), str(port)], - env=env, - check=False, - capture_output=True, - text=True, - timeout=8, - ) - thread.join(6) - - if success: - passed = r.returncode == 0 and expected in r.stdout - else: - passed = r.returncode == 1 and "TLS" in r.stderr - failures += not passed - - print( - ("PASS" if passed else "FAIL"), - name, - "=>", - (r.stdout + r.stderr).strip(), - flush=True, - ) - -print("Failures:", failures) -raise SystemExit(bool(failures))