@@ -2088,35 +2088,46 @@ def test_digest(self):
20882088 )
20892089 )
20902090
2091- def _extcert (self , pkey , extensions ):
2092- cert = X509 ()
2093- # Certificates with extensions must be X.509v3, which is encoded with a
2094- # version of two.
2095- cert .set_version (2 )
2096- cert .set_pubkey (pkey )
2097- cert .get_subject ().commonName = "Unit Tests"
2098- cert .get_issuer ().commonName = "Unit Tests"
2099- when = datetime .now ().strftime ("%Y%m%d%H%M%SZ" ).encode ("ascii" )
2100- cert .set_notBefore (when )
2101- cert .set_notAfter (when )
2102-
2103- cert .add_extensions (extensions )
2104- cert .sign (pkey , "sha256" )
2105- return load_certificate (
2106- FILETYPE_PEM , dump_certificate (FILETYPE_PEM , cert )
2091+ def _extcert (self , key , extensions ):
2092+ subject = x509 .Name (
2093+ [x509 .NameAttribute (x509 .NameOID .COMMON_NAME , "Unit Tests" )]
21072094 )
2095+ when = datetime .now ()
2096+ builder = (
2097+ x509 .CertificateBuilder ()
2098+ .public_key (key .public_key ())
2099+ .subject_name (subject )
2100+ .issuer_name (subject )
2101+ .not_valid_before (when )
2102+ .not_valid_after (when )
2103+ .serial_number (1 )
2104+ )
2105+ for i , ext in enumerate (extensions ):
2106+ builder = builder .add_extension (ext , critical = i % 2 == 0 )
2107+
2108+ return X509 .from_cryptography (builder .sign (key , hashes .SHA256 ()))
21082109
21092110 def test_extension_count (self ):
21102111 """
21112112 `X509.get_extension_count` returns the number of extensions
21122113 that are present in the certificate.
21132114 """
2114- pkey = load_privatekey (FILETYPE_PEM , client_key_pem )
2115- ca = X509Extension (b"basicConstraints" , True , b"CA:FALSE" )
2116- key = X509Extension (b"keyUsage" , True , b"digitalSignature" )
2117- subjectAltName = X509Extension (
2118- b"subjectAltName" , True , b"DNS:example.com"
2115+ pkey = load_privatekey (
2116+ FILETYPE_PEM , client_key_pem
2117+ ).to_cryptography_key ()
2118+ ca = x509 .BasicConstraints (ca = False , path_length = None )
2119+ key = x509 .KeyUsage (
2120+ digital_signature = True ,
2121+ content_commitment = False ,
2122+ key_encipherment = False ,
2123+ data_encipherment = False ,
2124+ key_agreement = False ,
2125+ key_cert_sign = False ,
2126+ crl_sign = False ,
2127+ encipher_only = False ,
2128+ decipher_only = False ,
21192129 )
2130+ san = x509 .SubjectAlternativeName ([x509 .DNSName ("example.com" )])
21202131
21212132 # Try a certificate with no extensions at all.
21222133 c = self ._extcert (pkey , [])
@@ -2127,22 +2138,32 @@ def test_extension_count(self):
21272138 assert c .get_extension_count () == 1
21282139
21292140 # And a certificate with several
2130- c = self ._extcert (pkey , [ca , key , subjectAltName ])
2141+ c = self ._extcert (pkey , [ca , key , san ])
21312142 assert c .get_extension_count () == 3
21322143
21332144 def test_get_extension (self ):
21342145 """
21352146 `X509.get_extension` takes an integer and returns an
21362147 `X509Extension` corresponding to the extension at that index.
21372148 """
2138- pkey = load_privatekey (FILETYPE_PEM , client_key_pem )
2139- ca = X509Extension (b"basicConstraints" , True , b"CA:FALSE" )
2140- key = X509Extension (b"keyUsage" , True , b"digitalSignature" )
2141- subjectAltName = X509Extension (
2142- b"subjectAltName" , False , b"DNS:example.com"
2149+ pkey = load_privatekey (
2150+ FILETYPE_PEM , client_key_pem
2151+ ).to_cryptography_key ()
2152+ ca = x509 .BasicConstraints (ca = False , path_length = None )
2153+ key = x509 .KeyUsage (
2154+ digital_signature = True ,
2155+ content_commitment = False ,
2156+ key_encipherment = False ,
2157+ data_encipherment = False ,
2158+ key_agreement = False ,
2159+ key_cert_sign = False ,
2160+ crl_sign = False ,
2161+ encipher_only = False ,
2162+ decipher_only = False ,
21432163 )
2164+ san = x509 .SubjectAlternativeName ([x509 .DNSName ("example.com" )])
21442165
2145- cert = self ._extcert (pkey , [ca , key , subjectAltName ])
2166+ cert = self ._extcert (pkey , [ca , key , san ])
21462167
21472168 ext = cert .get_extension (0 )
21482169 assert isinstance (ext , X509Extension )
@@ -2151,12 +2172,12 @@ def test_get_extension(self):
21512172
21522173 ext = cert .get_extension (1 )
21532174 assert isinstance (ext , X509Extension )
2154- assert ext .get_critical ()
2175+ assert not ext .get_critical ()
21552176 assert ext .get_short_name () == b"keyUsage"
21562177
21572178 ext = cert .get_extension (2 )
21582179 assert isinstance (ext , X509Extension )
2159- assert not ext .get_critical ()
2180+ assert ext .get_critical ()
21602181 assert ext .get_short_name () == b"subjectAltName"
21612182
21622183 with pytest .raises (IndexError ):
0 commit comments