forked from aiortc/aioquic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
91 lines (73 loc) · 2.71 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import asyncio
import datetime
import functools
import logging
import os
from cryptography import x509
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import ec, ed448, ed25519
def asynctest(coro):
@functools.wraps(coro)
def wrap(*args, **kwargs):
asyncio.run(coro(*args, **kwargs))
return wrap
def generate_certificate(*, alternative_names, common_name, hash_algorithm, key):
subject = issuer = x509.Name(
[x509.NameAttribute(x509.NameOID.COMMON_NAME, common_name)]
)
builder = (
x509.CertificateBuilder()
.subject_name(subject)
.issuer_name(issuer)
.public_key(key.public_key())
.serial_number(x509.random_serial_number())
.not_valid_before(datetime.datetime.utcnow())
.not_valid_after(datetime.datetime.utcnow() + datetime.timedelta(days=10))
)
if alternative_names:
builder = builder.add_extension(
x509.SubjectAlternativeName(
[x509.DNSName(name) for name in alternative_names]
),
critical=False,
)
cert = builder.sign(key, hash_algorithm)
return cert, key
def generate_ec_certificate(common_name, alternative_names=[], curve=ec.SECP256R1):
key = ec.generate_private_key(curve=curve)
return generate_certificate(
alternative_names=alternative_names,
common_name=common_name,
hash_algorithm=hashes.SHA256(),
key=key,
)
def generate_ed25519_certificate(common_name, alternative_names=[]):
key = ed25519.Ed25519PrivateKey.generate()
return generate_certificate(
alternative_names=alternative_names,
common_name=common_name,
hash_algorithm=None,
key=key,
)
def generate_ed448_certificate(common_name, alternative_names=[]):
key = ed448.Ed448PrivateKey.generate()
return generate_certificate(
alternative_names=alternative_names,
common_name=common_name,
hash_algorithm=None,
key=key,
)
def load(name: str) -> bytes:
path = os.path.join(os.path.dirname(__file__), name)
with open(path, "rb") as fp:
return fp.read()
SERVER_CACERTFILE = os.path.join(os.path.dirname(__file__), "pycacert.pem")
SERVER_CERTFILE = os.path.join(os.path.dirname(__file__), "ssl_cert.pem")
SERVER_CERTFILE_WITH_CHAIN = os.path.join(
os.path.dirname(__file__), "ssl_cert_with_chain.pem"
)
SERVER_KEYFILE = os.path.join(os.path.dirname(__file__), "ssl_key.pem")
SERVER_COMBINEDFILE = os.path.join(os.path.dirname(__file__), "ssl_combined.pem")
SKIP_TESTS = frozenset(os.environ.get("AIOQUIC_SKIP_TESTS", "").split(","))
if os.environ.get("AIOQUIC_DEBUG"):
logging.basicConfig(level=logging.DEBUG)