-
Notifications
You must be signed in to change notification settings - Fork 335
Fix ignored GnuTLS options when SYSTEM priorities are unavailable #1701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Shubham-Padkonde
wants to merge
2
commits into
OpenPrinting:master
Choose a base branch
from
Shubham-Padkonde:fix/gnutls-missing-system-priority
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+266
−8
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| #!/usr/bin/env python3 | ||
| # | ||
| # GnuTLS SSLOptions integration tests for CUPS. | ||
| # Copyright (c) 2026 by OpenPrinting. | ||
| # Licensed under Apache License v2.0. See LICENSE for details. | ||
| # | ||
| # Run after building with --with-tls=gnutls: python3 test/testssloptions.py | ||
| # Requires a C compiler, Python 3 with TLS 1.3 support, and the openssl command. | ||
| # All certificates, configuration, and connections are local to this test. | ||
|
|
||
| import os | ||
| 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 CUPS with --with-tls=gnutls before running this test.") | ||
| failures = 0 | ||
| with tempfile.TemporaryDirectory(prefix="cups-ssloptions-") as d: | ||
| p = Path(d) | ||
| (p / "client.c").write_text(r""" | ||
| #include "cups/cups.h" | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| 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 = httpConnect2("localhost", atoi(argv[1]), NULL, AF_INET, | ||
| HTTP_ENCRYPTION_ALWAYS, 1, 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", "cc")) | ||
| + [ | ||
| "-I" + str(root), | ||
| str(p / "client.c"), | ||
| "-L" + str(root / "cups"), | ||
| "-Wl,-rpath," + str(root / "cups"), | ||
| "-lcups", | ||
| "-o", | ||
| str(p / "client"), | ||
| ], | ||
| check=True, | ||
| ) | ||
| subprocess.run( | ||
| [ | ||
| "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, | ||
| ) | ||
| 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)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO it would be better to have the check for @System before we assign it into priority string, instead of handling it here, although it adds new set of HAVE_GNUTLS_PRIORITY_SET_DIRECT ifdef.
Just do not forget to initialize the string with NULL terminator in case of error, as I did :( .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in db6e871:
@SYSTEMis now probed before the priority string is built (withset_directorpriority_init/deinitdepending onHAVE_GNUTLS_PRIORITY_SET_DIRECT),priority_stringis initialized to an empty string first, and the retry after failure is removed.test/testssloptions.pypasses all 8 cases with GnuTLS 3.7.3 on both theset_directand thepriority_initcode paths.