@@ -233,6 +233,72 @@ def rsa_keypair(rsa_keypair_factory):
233233 return rsa_keypair_factory ()
234234
235235
236+ @copy_fixture (copies = 3 )
237+ @pytest .fixture
238+ def rsa_keypair_with_signed_cert (rsa_keypair_factory ):
239+ root_keypair = rsa_keypair_factory ()
240+ root_private_key = serialization .load_pem_private_key (root_keypair .private .encode ("utf-8" ), password = None )
241+ root_public_key = serialization .load_pem_public_key (root_keypair .public .encode ("utf-8" ))
242+ rsa_keypair = rsa_keypair_factory ()
243+ public_key = serialization .load_pem_public_key (rsa_keypair .public .encode ("utf-8" ))
244+ issuer = x509 .Name (
245+ [
246+ x509 .NameAttribute (NameOID .COUNTRY_NAME , u"US" ),
247+ x509 .NameAttribute (NameOID .STATE_OR_PROVINCE_NAME , u"California" ),
248+ x509 .NameAttribute (NameOID .LOCALITY_NAME , u"San Francisco" ),
249+ x509 .NameAttribute (NameOID .ORGANIZATION_NAME , u"My Company" ),
250+ x509 .NameAttribute (NameOID .COMMON_NAME , u"mycompany.com" ),
251+ ]
252+ )
253+ subject = x509 .Name (
254+ [
255+ x509 .NameAttribute (NameOID .COUNTRY_NAME , u"US" ),
256+ x509 .NameAttribute (NameOID .STATE_OR_PROVINCE_NAME , u"California" ),
257+ x509 .NameAttribute (NameOID .LOCALITY_NAME , u"San Francisco" ),
258+ x509 .NameAttribute (NameOID .ORGANIZATION_NAME , u"My Company" ),
259+ x509 .NameAttribute (NameOID .COMMON_NAME , u"qa.mycompany.com" ),
260+ ]
261+ )
262+ root_certificate = (
263+ x509 .CertificateBuilder ()
264+ .subject_name (issuer )
265+ .issuer_name (issuer )
266+ .public_key (root_public_key )
267+ .serial_number (x509 .random_serial_number ())
268+ .not_valid_before (datetime .utcnow ())
269+ .not_valid_after (datetime .utcnow () + timedelta (days = 365 ))
270+ .add_extension (
271+ x509 .SubjectAlternativeName ([x509 .DNSName (u"mycompany.com" )]),
272+ critical = False ,
273+ )
274+ .sign (root_private_key , hashes .SHA256 ())
275+ )
276+ certificate = (
277+ x509 .CertificateBuilder ()
278+ .subject_name (subject )
279+ .issuer_name (issuer )
280+ .public_key (public_key )
281+ .serial_number (x509 .random_serial_number ())
282+ .not_valid_before (datetime .utcnow ())
283+ .not_valid_after (datetime .utcnow () + timedelta (days = 365 ))
284+ .add_extension (
285+ x509 .SubjectAlternativeName ([x509 .DNSName (u"qa.mycompany.com" )]),
286+ critical = False ,
287+ )
288+ .sign (root_private_key , hashes .SHA256 ())
289+ )
290+
291+ root_certificate_bytes = root_certificate .public_bytes (serialization .Encoding .PEM ).decode ("utf-8" )
292+ certificate_bytes = certificate .public_bytes (serialization .Encoding .PEM ).decode ("utf-8" )
293+
294+ RSAKeyPairWithCert = namedtuple ("RSAKeyPairWithCert" , ["private" , "public" , "certificate" ])
295+ CertificateChain = namedtuple ("CertificateChain" , ["root" , "subordinate" ])
296+
297+ root_keypair_with_cert = RSAKeyPairWithCert (private = root_keypair .private , public = root_keypair .public , certificate = root_certificate_bytes )
298+ subordinate_keypair_with_cert = RSAKeyPairWithCert (private = rsa_keypair .private , public = rsa_keypair .public , certificate = certificate_bytes )
299+ return CertificateChain (root = root_keypair_with_cert , subordinate = subordinate_keypair_with_cert )
300+
301+
236302@copy_fixture (copies = 3 )
237303@pytest .fixture
238304def rsa_keypair_with_cert (rsa_keypair_factory ):
0 commit comments