-
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): | ||||||||||||||
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 | ||||||||||||||
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 = [] | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
|
||||||||||||||
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 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?