-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Add serverCertificateHashes test server #50263
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,10 +8,13 @@ | |||||||||||||
import sys | ||||||||||||||
import threading | ||||||||||||||
import traceback | ||||||||||||||
from enum import IntEnum | ||||||||||||||
from enum import IntEnum, Enum | ||||||||||||||
from urllib.parse import urlparse | ||||||||||||||
from typing import Any, Dict, List, Optional, Tuple, cast | ||||||||||||||
|
||||||||||||||
from cryptography import x509 | ||||||||||||||
from cryptography.hazmat.primitives import serialization | ||||||||||||||
|
||||||||||||||
# TODO(bashi): Remove import check suppressions once aioquic dependency is resolved. | ||||||||||||||
from aioquic.buffer import Buffer # type: ignore | ||||||||||||||
from aioquic.asyncio import QuicConnectionProtocol, serve # type: ignore | ||||||||||||||
|
@@ -31,6 +34,7 @@ | |||||||||||||
from tools import localpaths # noqa: F401 | ||||||||||||||
from wptserve import stash | ||||||||||||||
from .capsule import H3Capsule, H3CapsuleDecoder, CapsuleType | ||||||||||||||
from http.server import BaseHTTPRequestHandler, HTTPServer | ||||||||||||||
|
||||||||||||||
""" | ||||||||||||||
A WebTransport over HTTP/3 server for testing. | ||||||||||||||
|
@@ -499,6 +503,16 @@ def add(self, ticket: SessionTicket) -> None: | |||||||||||||
def pop(self, label: bytes) -> Optional[SessionTicket]: | ||||||||||||||
return self.tickets.pop(label, None) | ||||||||||||||
|
||||||||||||||
class WebTransportCertificateGeneration(Enum): | ||||||||||||||
""" | ||||||||||||||
Specify, if the server should generate a certificate or use an existing certificate | ||||||||||||||
USEPREGENERATED: use existing certificate | ||||||||||||||
GENERATEDVALIDSERVERCERTIFICATEHASHCERT: generate a certificate compatible to server cert hashes | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor nit:
Suggested change
Comment on lines
+509
to
+510
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes it a little bit more readable:
Suggested change
|
||||||||||||||
""" | ||||||||||||||
USEPREGENERATED = 1, | ||||||||||||||
GENERATEDVALIDSERVERCERTIFICATEHASHCERT = 2 | ||||||||||||||
# TODO add cases for invalid certificates | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
|
||||||||||||||
class WebTransportH3Server: | ||||||||||||||
""" | ||||||||||||||
|
@@ -507,18 +521,31 @@ class WebTransportH3Server: | |||||||||||||
:param host: Host from which to serve. | ||||||||||||||
:param port: Port from which to serve. | ||||||||||||||
:param doc_root: Document root for serving handlers. | ||||||||||||||
:paran cert_mode: The used certificate mode can be | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
USEPREGENERATED or GENERATEDVALIDSERVERCERTIFICATEHASHCERT | ||||||||||||||
:param cert_path: Path to certificate file to use. | ||||||||||||||
:param key_path: Path to key file to use. | ||||||||||||||
:param logger: a Logger object for this server. | ||||||||||||||
""" | ||||||||||||||
|
||||||||||||||
def __init__(self, host: str, port: int, doc_root: str, cert_path: str, | ||||||||||||||
key_path: str, logger: Optional[logging.Logger]) -> None: | ||||||||||||||
def __init__(self, host: str, port: int, doc_root: str, cert_mode: WebTransportCertificateGeneration, | ||||||||||||||
cert_path: Optional[str], key_path: Optional[str], logger: Optional[logging.Logger], | ||||||||||||||
cert_hash_info: Optional[Dict]) -> None: | ||||||||||||||
self.host = host | ||||||||||||||
self.port = port | ||||||||||||||
self.doc_root = doc_root | ||||||||||||||
self.cert_path = cert_path | ||||||||||||||
self.key_path = key_path | ||||||||||||||
if cert_path is not None: | ||||||||||||||
self.cert_path = cert_path | ||||||||||||||
if key_path is not None: | ||||||||||||||
self.key_path = key_path | ||||||||||||||
if cert_hash_info is not None: | ||||||||||||||
self.cert_hash_info = cert_hash_info | ||||||||||||||
self.cert_mode = cert_mode | ||||||||||||||
if (cert_path is None or key_path is None) and cert_mode == WebTransportCertificateGeneration.USEPREGENERATED: | ||||||||||||||
raise ValueError("Both cert_path and key_path must be provided, if cert_mode is USEPREGENERATED") | ||||||||||||||
if (cert_hash_info is None and cert_mode == WebTransportCertificateGeneration.GENERATEDVALIDSERVERCERTIFICATEHASHCERT): | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
raise ValueError("cert_hash_info must be provided, if cert_mode is GENERATEDVALIDSERVERCERTIFICATEHASHCERT") | ||||||||||||||
|
||||||||||||||
self.started = False | ||||||||||||||
global _doc_root | ||||||||||||||
_doc_root = self.doc_root | ||||||||||||||
|
@@ -551,7 +578,16 @@ def _start_on_server_thread(self) -> None: | |||||||||||||
_logger.info("Starting WebTransport over HTTP/3 server on %s:%s", | ||||||||||||||
self.host, self.port) | ||||||||||||||
|
||||||||||||||
configuration.load_cert_chain(self.cert_path, self.key_path) | ||||||||||||||
if self.cert_mode == WebTransportCertificateGeneration.USEPREGENERATED: | ||||||||||||||
configuration.load_cert_chain(self.cert_path, self.key_path) | ||||||||||||||
else: # GENERATEDVALIDSERVERCERTIFICATEHASHCERT case | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add an assert or something there, verify that we are actually this case (and haven't missed this if/when we add another new |
||||||||||||||
configuration.private_key = serialization.load_pem_private_key(self.cert_hash_info["private_key"], | ||||||||||||||
password=None | ||||||||||||||
) | ||||||||||||||
configuration.certificate = x509.load_pem_x509_certificate(self.cert_hash_info["certificate"]) | ||||||||||||||
configuration.certificate_chain = [] | ||||||||||||||
Comment on lines
+584
to
+588
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are commented as "for internal purposes, not guaranteed to be stable". As such, we probably need to write these out to some temporary files and call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but if you look at the implementation, it is very unlikely that these will change. But maybe a PR to the module to have a setter for the two would be a good idea. (Regarding temporary files, I was just not aware of how to achieve this safely in the WPT framework.) |
||||||||||||||
|
||||||||||||||
|
||||||||||||||
|
||||||||||||||
ticket_store = SessionTicketStore() | ||||||||||||||
|
||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
aioquic==1.2.0 | ||
cryptography | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably pin its version, though I guess we're effectively already getting it as a dependency of aioquic anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, when I wrote this, I was unaware it was a dependency. Anyway, if aioquic is replaced by something else, certificates for webtransport will still be needed (e.g. for wt over http/2 sometime) |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,8 +9,14 @@ | |||||||||||||||||||||||||||||||||
import socket | ||||||||||||||||||||||||||||||||||
import sys | ||||||||||||||||||||||||||||||||||
import time | ||||||||||||||||||||||||||||||||||
import datetime | ||||||||||||||||||||||||||||||||||
from typing import Optional | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
from cryptography.hazmat.primitives.asymmetric import ec | ||||||||||||||||||||||||||||||||||
from cryptography.hazmat.primitives import hashes, serialization | ||||||||||||||||||||||||||||||||||
from cryptography.x509.oid import NameOID | ||||||||||||||||||||||||||||||||||
from cryptography import x509 | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
import mozprocess | ||||||||||||||||||||||||||||||||||
from mozlog import get_default_logger, handlers | ||||||||||||||||||||||||||||||||||
from mozlog.structuredlog import StructuredLogger | ||||||||||||||||||||||||||||||||||
|
@@ -46,6 +52,37 @@ def do_delayed_imports(logger, test_paths): | |||||||||||||||||||||||||||||||||
(", ".join(failed), serve_root)) | ||||||||||||||||||||||||||||||||||
sys.exit(1) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
def generate_hash_certificate(host: str) -> str: | ||||||||||||||||||||||||||||||||||
private_key = ec.generate_private_key(ec.SECP256R1()) | ||||||||||||||||||||||||||||||||||
subject = issuer = x509.Name([ | ||||||||||||||||||||||||||||||||||
x509.NameAttribute(NameOID.COUNTRY_NAME, "DE"), | ||||||||||||||||||||||||||||||||||
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "Berlin"), | ||||||||||||||||||||||||||||||||||
x509.NameAttribute(NameOID.LOCALITY_NAME, "Berlin"), | ||||||||||||||||||||||||||||||||||
x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Wpt tests"), | ||||||||||||||||||||||||||||||||||
x509.NameAttribute(NameOID.COMMON_NAME, host), | ||||||||||||||||||||||||||||||||||
]) | ||||||||||||||||||||||||||||||||||
Comment on lines
+57
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be closer to what we generate for our other certs: wpt/tools/wptserve/wptserve/sslutils/openssl.py Lines 176 to 191 in b862ecf
|
||||||||||||||||||||||||||||||||||
now = datetime.datetime.now(datetime.timezone.utc) | ||||||||||||||||||||||||||||||||||
certificate = ( | ||||||||||||||||||||||||||||||||||
x509.CertificateBuilder() | ||||||||||||||||||||||||||||||||||
.subject_name(subject) | ||||||||||||||||||||||||||||||||||
.issuer_name(issuer) | ||||||||||||||||||||||||||||||||||
.public_key(private_key.public_key()) | ||||||||||||||||||||||||||||||||||
.serial_number(x509.random_serial_number()) | ||||||||||||||||||||||||||||||||||
.not_valid_before(now) | ||||||||||||||||||||||||||||||||||
.not_valid_after(now + datetime.timedelta(days=13)) | ||||||||||||||||||||||||||||||||||
.sign(private_key, hashes.SHA256()) | ||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||
fingerprint = certificate.fingerprint(hashes.SHA256()) | ||||||||||||||||||||||||||||||||||
server_certificate_hash = ":".join(f"{byte:02x}" for byte in fingerprint) | ||||||||||||||||||||||||||||||||||
return { "certificate": certificate.public_bytes( | ||||||||||||||||||||||||||||||||||
encoding=serialization.Encoding.PEM | ||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||
"private_key": private_key.private_bytes( | ||||||||||||||||||||||||||||||||||
encoding=serialization.Encoding.PEM, | ||||||||||||||||||||||||||||||||||
format=serialization.PrivateFormat.TraditionalOpenSSL, | ||||||||||||||||||||||||||||||||||
encryption_algorithm=serialization.NoEncryption()), | ||||||||||||||||||||||||||||||||||
"hash": server_certificate_hash | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
def serve_path(test_paths): | ||||||||||||||||||||||||||||||||||
return test_paths["/"].tests_path | ||||||||||||||||||||||||||||||||||
|
@@ -150,7 +187,8 @@ def __enter__(self): | |||||||||||||||||||||||||||||||||
self.get_routes(), | ||||||||||||||||||||||||||||||||||
mp_context=mpcontext.get_context(), | ||||||||||||||||||||||||||||||||||
log_handlers=[server_log_handler], | ||||||||||||||||||||||||||||||||||
webtransport_h3=self.enable_webtransport) | ||||||||||||||||||||||||||||||||||
webtransport_h3=self.enable_webtransport, | ||||||||||||||||||||||||||||||||||
webtransport_h3_cert_hash=self.enable_webtransport) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
if self.options.get("supports_debugger") and self.debug_info and self.debug_info.interactive: | ||||||||||||||||||||||||||||||||||
self._stack.enter_context(self.ignore_interrupts()) | ||||||||||||||||||||||||||||||||||
|
@@ -197,6 +235,7 @@ def build_config(self): | |||||||||||||||||||||||||||||||||
"wss": [8889], | ||||||||||||||||||||||||||||||||||
"h2": [9000], | ||||||||||||||||||||||||||||||||||
"webtransport-h3": [11000], | ||||||||||||||||||||||||||||||||||
"webtransport-h3-cert-hash": [11001], | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
config.ports = ports | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
@@ -221,6 +260,8 @@ def build_config(self): | |||||||||||||||||||||||||||||||||
config.doc_root = serve_path(self.test_paths) | ||||||||||||||||||||||||||||||||||
config.inject_script = self.inject_script | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
config.cert_hash_info = generate_hash_certificate(config.server_host) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
if self.suppress_handler_traceback is not None: | ||||||||||||||||||||||||||||||||||
config.logging["suppress_handler_traceback"] = self.suppress_handler_traceback | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
@@ -323,10 +364,15 @@ def test_servers(self): | |||||||||||||||||||||||||||||||||
for port, server in self.servers.get("webtransport-h3", []): | ||||||||||||||||||||||||||||||||||
if not webtranport_h3_server_is_running(host, port, timeout=5): | ||||||||||||||||||||||||||||||||||
pending.append((host, port)) | ||||||||||||||||||||||||||||||||||
for port, server in self.servers.get("webtransport-h3-cert-hash", []): | ||||||||||||||||||||||||||||||||||
if not webtranport_h3_server_is_running(host, port, timeout=5): | ||||||||||||||||||||||||||||||||||
pending.append((host, port)) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
for scheme, servers in self.servers.items(): | ||||||||||||||||||||||||||||||||||
if scheme == "webtransport-h3": | ||||||||||||||||||||||||||||||||||
continue | ||||||||||||||||||||||||||||||||||
if scheme == "webtransport-h3-cert-hash": | ||||||||||||||||||||||||||||||||||
continue | ||||||||||||||||||||||||||||||||||
for port, server in servers: | ||||||||||||||||||||||||||||||||||
s = socket.socket() | ||||||||||||||||||||||||||||||||||
s.settimeout(0.1) | ||||||||||||||||||||||||||||||||||
|
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.
If we're assigning them integer values, we can use IntEnum and skip the additional import, right?