Skip to content

Commit cc78b33

Browse files
Copy root CA information to the generated certificate
1 parent ec6c432 commit cc78b33

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/asyncio_https_proxy/tls_store.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,16 @@ def _generate_cert(
113113
self, domain
114114
) -> tuple[ec.EllipticCurvePrivateKey, x509.Certificate]:
115115
ee_key = ec.generate_private_key(ec.SECP256R1())
116+
117+
ca_subject = self._ca[1].subject
118+
ca_attrs = {attr.oid: attr.value for attr in ca_subject}
119+
116120
subject = x509.Name(
117121
[
118-
x509.NameAttribute(NameOID.COUNTRY_NAME, "FR"),
119-
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "Ile-de-France"),
120-
x509.NameAttribute(NameOID.LOCALITY_NAME, "Paris"),
121-
x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Asyncio HTTPS Proxy"),
122+
x509.NameAttribute(NameOID.COUNTRY_NAME, ca_attrs.get(NameOID.COUNTRY_NAME, "US")),
123+
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, ca_attrs.get(NameOID.STATE_OR_PROVINCE_NAME, "Unknown")),
124+
x509.NameAttribute(NameOID.LOCALITY_NAME, ca_attrs.get(NameOID.LOCALITY_NAME, "Unknown")),
125+
x509.NameAttribute(NameOID.ORGANIZATION_NAME, ca_attrs.get(NameOID.ORGANIZATION_NAME, "Unknown")),
122126
]
123127
)
124128
ee_cert = (

tests/test_tls_store.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,27 @@ def test_generate_cert_creates_valid_certificate(tls_store):
124124
assert cert.issuer == tls_store._ca[1].subject
125125

126126

127+
def test_generate_cert_copies_ca_certificate_information(tls_store):
128+
"""Test that _generate_cert copies certificate information from the root CA"""
129+
domain = "example.com"
130+
_, cert = tls_store._generate_cert(domain)
131+
132+
# Get CA certificate attributes
133+
ca_cert = tls_store._ca[1]
134+
ca_subject_attrs = {attr.oid: attr.value for attr in ca_cert.subject}
135+
136+
# Get generated certificate attributes
137+
cert_subject_attrs = {attr.oid: attr.value for attr in cert.subject}
138+
139+
# Verify that the generated certificate copies information from the CA
140+
assert cert_subject_attrs[x509.NameOID.COUNTRY_NAME] == ca_subject_attrs[x509.NameOID.COUNTRY_NAME]
141+
assert cert_subject_attrs[x509.NameOID.STATE_OR_PROVINCE_NAME] == ca_subject_attrs[x509.NameOID.STATE_OR_PROVINCE_NAME]
142+
assert cert_subject_attrs[x509.NameOID.LOCALITY_NAME] == ca_subject_attrs[x509.NameOID.LOCALITY_NAME]
143+
assert cert_subject_attrs[x509.NameOID.ORGANIZATION_NAME] == ca_subject_attrs[x509.NameOID.ORGANIZATION_NAME]
144+
145+
assert cert_subject_attrs[x509.NameOID.ORGANIZATION_NAME] == "Asyncio HTTPS Proxy"
146+
147+
127148
def test_get_ssl_context_returns_valid_context(tls_store):
128149
"""Test that get_ssl_context returns a valid SSL context"""
129150
domain = "test.example.com"

0 commit comments

Comments
 (0)