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 | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 38 | class TestBasicConstraints(object): |
| 39 | def test_ca_not_boolean(self): |
| 40 | with pytest.raises(TypeError): |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 41 | x509.BasicConstraints(ca="notbool", path_length=None) |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 42 | |
| 43 | def test_path_length_not_ca(self): |
| 44 | with pytest.raises(ValueError): |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 45 | x509.BasicConstraints(ca=False, path_length=0) |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 46 | |
| 47 | def test_path_length_not_int(self): |
| 48 | with pytest.raises(TypeError): |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 49 | x509.BasicConstraints(ca=True, path_length=1.1) |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 50 | |
| 51 | with pytest.raises(TypeError): |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 52 | x509.BasicConstraints(ca=True, path_length="notint") |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 53 | |
| 54 | def test_path_length_negative(self): |
| 55 | with pytest.raises(TypeError): |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 56 | x509.BasicConstraints(ca=True, path_length=-1) |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 57 | |
| 58 | def test_repr(self): |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 59 | na = x509.BasicConstraints(ca=True, path_length=None) |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 60 | assert repr(na) == ( |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 61 | "<BasicConstraints(ca=True, path_length=None)>" |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 62 | ) |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 63 | |
| 64 | |
| 65 | @pytest.mark.requires_backend_interface(interface=RSABackend) |
| 66 | @pytest.mark.requires_backend_interface(interface=X509Backend) |
| 67 | class TestExtensions(object): |
| 68 | def test_no_extensions(self, backend): |
| 69 | cert = _load_cert( |
| 70 | os.path.join("x509", "verisign_md2_root.pem"), |
| 71 | x509.load_pem_x509_certificate, |
| 72 | backend |
| 73 | ) |
| 74 | ext = cert.extensions |
| 75 | assert len(ext) == 0 |
| 76 | assert list(ext) == [] |
| 77 | |
| 78 | def test_duplicate_extension(self, backend): |
| 79 | cert = _load_cert( |
| 80 | os.path.join( |
| 81 | "x509", "custom", "two_basic_constraints.pem" |
| 82 | ), |
| 83 | x509.load_pem_x509_certificate, |
| 84 | backend |
| 85 | ) |
| 86 | with pytest.raises(x509.DuplicateExtension) as exc: |
| 87 | cert.extensions |
| 88 | |
| 89 | assert exc.value.oid == x509.OID_BASIC_CONSTRAINTS |
| 90 | |
| 91 | def test_unsupported_critical_extension(self, backend): |
| 92 | cert = _load_cert( |
| 93 | os.path.join( |
| 94 | "x509", "custom", "unsupported_extension_critical.pem" |
| 95 | ), |
| 96 | x509.load_pem_x509_certificate, |
| 97 | backend |
| 98 | ) |
| 99 | with pytest.raises(x509.UnsupportedExtension) as exc: |
| 100 | cert.extensions |
| 101 | |
| 102 | assert exc.value.oid == x509.ObjectIdentifier("1.2.3.4") |
| 103 | |
| 104 | def test_unsupported_extension(self, backend): |
| 105 | # TODO: this will raise an exception when all extensions are complete |
| 106 | cert = _load_cert( |
| 107 | os.path.join( |
| 108 | "x509", "custom", "unsupported_extension.pem" |
| 109 | ), |
| 110 | x509.load_pem_x509_certificate, |
| 111 | backend |
| 112 | ) |
| 113 | extensions = cert.extensions |
| 114 | assert len(extensions) == 0 |