Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 1 | # This file is dual licensed under the terms of the Apache License, Version |
| 2 | # 2.0, and the BSD License. See the LICENSE file in the root of this repository |
| 3 | # for complete details. |
| 4 | |
| 5 | from __future__ import absolute_import, division, print_function |
| 6 | |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 7 | import os |
| 8 | |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 9 | import pytest |
| 10 | |
| 11 | from cryptography import x509 |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 12 | from cryptography.hazmat.backends.interfaces import RSABackend, X509Backend |
| 13 | |
| 14 | from .test_x509 import _load_cert |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 15 | |
| 16 | |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 17 | class TestExtension(object): |
| 18 | def test_not_an_oid(self): |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 19 | bc = x509.BasicConstraints(ca=False, path_length=None) |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 20 | with pytest.raises(TypeError): |
| 21 | x509.Extension("notanoid", True, bc) |
| 22 | |
| 23 | def test_critical_not_a_bool(self): |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 24 | bc = x509.BasicConstraints(ca=False, path_length=None) |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 25 | with pytest.raises(TypeError): |
| 26 | x509.Extension(x509.OID_BASIC_CONSTRAINTS, "notabool", bc) |
| 27 | |
| 28 | def test_repr(self): |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 29 | bc = x509.BasicConstraints(ca=False, path_length=None) |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 30 | ext = x509.Extension(x509.OID_BASIC_CONSTRAINTS, True, bc) |
| 31 | assert repr(ext) == ( |
| 32 | "<Extension(oid=<ObjectIdentifier(oid=2.5.29.19, name=basicConst" |
| 33 | "raints)>, critical=True, value=<BasicConstraints(ca=False, path" |
| 34 | "_length=None)>)>" |
| 35 | ) |
| 36 | |
| 37 | |
Paul Kehrer | cecbbba | 2015-03-30 14:58:38 -0500 | [diff] [blame^] | 38 | class TestKeyUsage(object): |
| 39 | def test_key_agreement_false_encipher_decipher_true(self): |
| 40 | with pytest.raises(ValueError): |
| 41 | x509.KeyUsage( |
| 42 | digital_signature=False, |
| 43 | content_commitment=False, |
| 44 | key_encipherment=False, |
| 45 | data_encipherment=False, |
| 46 | key_agreement=False, |
| 47 | key_cert_sign=False, |
| 48 | crl_sign=False, |
| 49 | encipher_only=True, |
| 50 | decipher_only=False |
| 51 | ) |
| 52 | |
| 53 | with pytest.raises(ValueError): |
| 54 | x509.KeyUsage( |
| 55 | digital_signature=False, |
| 56 | content_commitment=False, |
| 57 | key_encipherment=False, |
| 58 | data_encipherment=False, |
| 59 | key_agreement=False, |
| 60 | key_cert_sign=False, |
| 61 | crl_sign=False, |
| 62 | encipher_only=True, |
| 63 | decipher_only=True |
| 64 | ) |
| 65 | |
| 66 | with pytest.raises(ValueError): |
| 67 | x509.KeyUsage( |
| 68 | digital_signature=False, |
| 69 | content_commitment=False, |
| 70 | key_encipherment=False, |
| 71 | data_encipherment=False, |
| 72 | key_agreement=False, |
| 73 | key_cert_sign=False, |
| 74 | crl_sign=False, |
| 75 | encipher_only=False, |
| 76 | decipher_only=True |
| 77 | ) |
| 78 | |
| 79 | def test_properties_key_agreement_true(self): |
| 80 | ku = x509.KeyUsage( |
| 81 | digital_signature=True, |
| 82 | content_commitment=True, |
| 83 | key_encipherment=False, |
| 84 | data_encipherment=False, |
| 85 | key_agreement=False, |
| 86 | key_cert_sign=True, |
| 87 | crl_sign=False, |
| 88 | encipher_only=False, |
| 89 | decipher_only=False |
| 90 | ) |
| 91 | assert ku.digital_signature is True |
| 92 | assert ku.content_commitment is True |
| 93 | assert ku.key_encipherment is False |
| 94 | assert ku.data_encipherment is False |
| 95 | assert ku.key_agreement is False |
| 96 | assert ku.key_cert_sign is True |
| 97 | assert ku.crl_sign is False |
| 98 | |
| 99 | def test_key_agreement_true_properties(self): |
| 100 | ku = x509.KeyUsage( |
| 101 | digital_signature=False, |
| 102 | content_commitment=False, |
| 103 | key_encipherment=False, |
| 104 | data_encipherment=False, |
| 105 | key_agreement=True, |
| 106 | key_cert_sign=False, |
| 107 | crl_sign=False, |
| 108 | encipher_only=False, |
| 109 | decipher_only=True |
| 110 | ) |
| 111 | assert ku.key_agreement is True |
| 112 | assert ku.encipher_only is False |
| 113 | assert ku.decipher_only is True |
| 114 | |
| 115 | def test_key_agreement_false_properties(self): |
| 116 | ku = x509.KeyUsage( |
| 117 | digital_signature=False, |
| 118 | content_commitment=False, |
| 119 | key_encipherment=False, |
| 120 | data_encipherment=False, |
| 121 | key_agreement=False, |
| 122 | key_cert_sign=False, |
| 123 | crl_sign=False, |
| 124 | encipher_only=False, |
| 125 | decipher_only=False |
| 126 | ) |
| 127 | assert ku.key_agreement is False |
| 128 | with pytest.raises(ValueError): |
| 129 | ku.encipher_only |
| 130 | |
| 131 | with pytest.raises(ValueError): |
| 132 | ku.decipher_only |
| 133 | |
| 134 | |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 135 | class TestBasicConstraints(object): |
| 136 | def test_ca_not_boolean(self): |
| 137 | with pytest.raises(TypeError): |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 138 | x509.BasicConstraints(ca="notbool", path_length=None) |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 139 | |
| 140 | def test_path_length_not_ca(self): |
| 141 | with pytest.raises(ValueError): |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 142 | x509.BasicConstraints(ca=False, path_length=0) |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 143 | |
| 144 | def test_path_length_not_int(self): |
| 145 | with pytest.raises(TypeError): |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 146 | x509.BasicConstraints(ca=True, path_length=1.1) |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 147 | |
| 148 | with pytest.raises(TypeError): |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 149 | x509.BasicConstraints(ca=True, path_length="notint") |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 150 | |
| 151 | def test_path_length_negative(self): |
| 152 | with pytest.raises(TypeError): |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 153 | x509.BasicConstraints(ca=True, path_length=-1) |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 154 | |
| 155 | def test_repr(self): |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 156 | na = x509.BasicConstraints(ca=True, path_length=None) |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 157 | assert repr(na) == ( |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 158 | "<BasicConstraints(ca=True, path_length=None)>" |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 159 | ) |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 160 | |
| 161 | |
Paul Kehrer | ffa2a15 | 2015-03-31 08:18:25 -0500 | [diff] [blame] | 162 | class TestExtendedKeyUsage(object): |
| 163 | def test_not_all_oids(self): |
| 164 | with pytest.raises(TypeError): |
| 165 | x509.ExtendedKeyUsage(["notoid"]) |
| 166 | |
| 167 | def test_iter_len(self): |
| 168 | eku = x509.ExtendedKeyUsage([ |
| 169 | x509.ObjectIdentifier("1.3.6.1.5.5.7.3.1"), |
| 170 | x509.ObjectIdentifier("1.3.6.1.5.5.7.3.2"), |
| 171 | ]) |
| 172 | assert len(eku) == 2 |
| 173 | assert list(eku) == [ |
| 174 | x509.OID_SERVER_AUTH, |
| 175 | x509.OID_CLIENT_AUTH |
| 176 | ] |
| 177 | |
| 178 | |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 179 | @pytest.mark.requires_backend_interface(interface=RSABackend) |
| 180 | @pytest.mark.requires_backend_interface(interface=X509Backend) |
| 181 | class TestExtensions(object): |
| 182 | def test_no_extensions(self, backend): |
| 183 | cert = _load_cert( |
| 184 | os.path.join("x509", "verisign_md2_root.pem"), |
| 185 | x509.load_pem_x509_certificate, |
| 186 | backend |
| 187 | ) |
| 188 | ext = cert.extensions |
| 189 | assert len(ext) == 0 |
| 190 | assert list(ext) == [] |
Paul Kehrer | fa56a23 | 2015-03-17 13:14:03 -0500 | [diff] [blame] | 191 | with pytest.raises(x509.ExtensionNotFound) as exc: |
| 192 | ext.get_extension_for_oid(x509.OID_BASIC_CONSTRAINTS) |
| 193 | |
| 194 | assert exc.value.oid == x509.OID_BASIC_CONSTRAINTS |
| 195 | |
| 196 | def test_one_extension(self, backend): |
| 197 | cert = _load_cert( |
| 198 | os.path.join( |
| 199 | "x509", "custom", "basic_constraints_not_critical.pem" |
| 200 | ), |
| 201 | x509.load_pem_x509_certificate, |
| 202 | backend |
| 203 | ) |
| 204 | extensions = cert.extensions |
| 205 | ext = extensions.get_extension_for_oid(x509.OID_BASIC_CONSTRAINTS) |
| 206 | assert ext is not None |
| 207 | assert ext.value.ca is False |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 208 | |
| 209 | def test_duplicate_extension(self, backend): |
| 210 | cert = _load_cert( |
| 211 | os.path.join( |
| 212 | "x509", "custom", "two_basic_constraints.pem" |
| 213 | ), |
| 214 | x509.load_pem_x509_certificate, |
| 215 | backend |
| 216 | ) |
| 217 | with pytest.raises(x509.DuplicateExtension) as exc: |
| 218 | cert.extensions |
| 219 | |
| 220 | assert exc.value.oid == x509.OID_BASIC_CONSTRAINTS |
| 221 | |
| 222 | def test_unsupported_critical_extension(self, backend): |
| 223 | cert = _load_cert( |
| 224 | os.path.join( |
| 225 | "x509", "custom", "unsupported_extension_critical.pem" |
| 226 | ), |
| 227 | x509.load_pem_x509_certificate, |
| 228 | backend |
| 229 | ) |
| 230 | with pytest.raises(x509.UnsupportedExtension) as exc: |
| 231 | cert.extensions |
| 232 | |
| 233 | assert exc.value.oid == x509.ObjectIdentifier("1.2.3.4") |
| 234 | |
| 235 | def test_unsupported_extension(self, backend): |
| 236 | # TODO: this will raise an exception when all extensions are complete |
| 237 | cert = _load_cert( |
| 238 | os.path.join( |
| 239 | "x509", "custom", "unsupported_extension.pem" |
| 240 | ), |
| 241 | x509.load_pem_x509_certificate, |
| 242 | backend |
| 243 | ) |
| 244 | extensions = cert.extensions |
| 245 | assert len(extensions) == 0 |
Paul Kehrer | fa56a23 | 2015-03-17 13:14:03 -0500 | [diff] [blame] | 246 | |
| 247 | |
| 248 | @pytest.mark.requires_backend_interface(interface=RSABackend) |
| 249 | @pytest.mark.requires_backend_interface(interface=X509Backend) |
Paul Kehrer | de813ea | 2015-03-28 12:44:34 -0500 | [diff] [blame] | 250 | class TestBasicConstraintsExtension(object): |
Paul Kehrer | fa56a23 | 2015-03-17 13:14:03 -0500 | [diff] [blame] | 251 | def test_ca_true_pathlen_6(self, backend): |
| 252 | cert = _load_cert( |
| 253 | os.path.join( |
| 254 | "x509", "PKITS_data", "certs", "pathLenConstraint6CACert.crt" |
| 255 | ), |
| 256 | x509.load_der_x509_certificate, |
| 257 | backend |
| 258 | ) |
| 259 | ext = cert.extensions.get_extension_for_oid( |
| 260 | x509.OID_BASIC_CONSTRAINTS |
| 261 | ) |
| 262 | assert ext is not None |
| 263 | assert ext.critical is True |
| 264 | assert ext.value.ca is True |
| 265 | assert ext.value.path_length == 6 |
| 266 | |
| 267 | def test_path_length_zero(self, backend): |
| 268 | cert = _load_cert( |
| 269 | os.path.join("x509", "custom", "bc_path_length_zero.pem"), |
| 270 | x509.load_pem_x509_certificate, |
| 271 | backend |
| 272 | ) |
| 273 | ext = cert.extensions.get_extension_for_oid( |
| 274 | x509.OID_BASIC_CONSTRAINTS |
| 275 | ) |
| 276 | assert ext is not None |
| 277 | assert ext.critical is True |
| 278 | assert ext.value.ca is True |
| 279 | assert ext.value.path_length == 0 |
| 280 | |
| 281 | def test_ca_true_no_pathlen(self, backend): |
| 282 | cert = _load_cert( |
| 283 | os.path.join("x509", "PKITS_data", "certs", "GoodCACert.crt"), |
| 284 | x509.load_der_x509_certificate, |
| 285 | backend |
| 286 | ) |
| 287 | ext = cert.extensions.get_extension_for_oid( |
| 288 | x509.OID_BASIC_CONSTRAINTS |
| 289 | ) |
| 290 | assert ext is not None |
| 291 | assert ext.critical is True |
| 292 | assert ext.value.ca is True |
| 293 | assert ext.value.path_length is None |
| 294 | |
| 295 | def test_ca_false(self, backend): |
| 296 | cert = _load_cert( |
| 297 | os.path.join("x509", "cryptography.io.pem"), |
| 298 | x509.load_pem_x509_certificate, |
| 299 | backend |
| 300 | ) |
| 301 | ext = cert.extensions.get_extension_for_oid( |
| 302 | x509.OID_BASIC_CONSTRAINTS |
| 303 | ) |
| 304 | assert ext is not None |
| 305 | assert ext.critical is True |
| 306 | assert ext.value.ca is False |
| 307 | assert ext.value.path_length is None |
| 308 | |
| 309 | def test_no_basic_constraints(self, backend): |
| 310 | cert = _load_cert( |
| 311 | os.path.join( |
| 312 | "x509", |
| 313 | "PKITS_data", |
| 314 | "certs", |
| 315 | "ValidCertificatePathTest1EE.crt" |
| 316 | ), |
| 317 | x509.load_der_x509_certificate, |
| 318 | backend |
| 319 | ) |
| 320 | with pytest.raises(x509.ExtensionNotFound): |
| 321 | cert.extensions.get_extension_for_oid(x509.OID_BASIC_CONSTRAINTS) |
| 322 | |
| 323 | def test_basic_constraint_not_critical(self, backend): |
| 324 | cert = _load_cert( |
| 325 | os.path.join( |
| 326 | "x509", "custom", "basic_constraints_not_critical.pem" |
| 327 | ), |
| 328 | x509.load_pem_x509_certificate, |
| 329 | backend |
| 330 | ) |
| 331 | ext = cert.extensions.get_extension_for_oid( |
| 332 | x509.OID_BASIC_CONSTRAINTS |
| 333 | ) |
| 334 | assert ext is not None |
| 335 | assert ext.critical is False |
| 336 | assert ext.value.ca is False |