@@ -2088,35 +2088,46 @@ def test_digest(self):
2088
2088
)
2089
2089
)
2090
2090
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" )]
2107
2094
)
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 ()))
2108
2109
2109
2110
def test_extension_count (self ):
2110
2111
"""
2111
2112
`X509.get_extension_count` returns the number of extensions
2112
2113
that are present in the certificate.
2113
2114
"""
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 ,
2119
2129
)
2130
+ san = x509 .SubjectAlternativeName ([x509 .DNSName ("example.com" )])
2120
2131
2121
2132
# Try a certificate with no extensions at all.
2122
2133
c = self ._extcert (pkey , [])
@@ -2127,22 +2138,32 @@ def test_extension_count(self):
2127
2138
assert c .get_extension_count () == 1
2128
2139
2129
2140
# And a certificate with several
2130
- c = self ._extcert (pkey , [ca , key , subjectAltName ])
2141
+ c = self ._extcert (pkey , [ca , key , san ])
2131
2142
assert c .get_extension_count () == 3
2132
2143
2133
2144
def test_get_extension (self ):
2134
2145
"""
2135
2146
`X509.get_extension` takes an integer and returns an
2136
2147
`X509Extension` corresponding to the extension at that index.
2137
2148
"""
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 ,
2143
2163
)
2164
+ san = x509 .SubjectAlternativeName ([x509 .DNSName ("example.com" )])
2144
2165
2145
- cert = self ._extcert (pkey , [ca , key , subjectAltName ])
2166
+ cert = self ._extcert (pkey , [ca , key , san ])
2146
2167
2147
2168
ext = cert .get_extension (0 )
2148
2169
assert isinstance (ext , X509Extension )
@@ -2151,12 +2172,12 @@ def test_get_extension(self):
2151
2172
2152
2173
ext = cert .get_extension (1 )
2153
2174
assert isinstance (ext , X509Extension )
2154
- assert ext .get_critical ()
2175
+ assert not ext .get_critical ()
2155
2176
assert ext .get_short_name () == b"keyUsage"
2156
2177
2157
2178
ext = cert .get_extension (2 )
2158
2179
assert isinstance (ext , X509Extension )
2159
- assert not ext .get_critical ()
2180
+ assert ext .get_critical ()
2160
2181
assert ext .get_short_name () == b"subjectAltName"
2161
2182
2162
2183
with pytest .raises (IndexError ):
0 commit comments