Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [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 | 0307c37 | 2014-11-27 09:49:31 -1000 | [diff] [blame] | 7 | import binascii |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 8 | import datetime |
| 9 | import os |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 10 | |
| 11 | import pytest |
| 12 | |
Paul Kehrer | 53d8d49 | 2015-02-13 18:47:30 -0600 | [diff] [blame] | 13 | import six |
| 14 | |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 15 | from cryptography import x509 |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 16 | from cryptography.exceptions import UnsupportedAlgorithm |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 17 | from cryptography.hazmat.backends.interfaces import ( |
| 18 | DSABackend, EllipticCurveBackend, RSABackend, X509Backend |
| 19 | ) |
Alex Gaynor | 32c57df | 2015-02-23 21:51:27 -0800 | [diff] [blame] | 20 | from cryptography.hazmat.primitives import hashes |
| 21 | from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 22 | |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 23 | from .hazmat.primitives.test_ec import _skip_curve_unsupported |
Paul Kehrer | a9d78c1 | 2014-11-26 10:59:03 -1000 | [diff] [blame] | 24 | from .utils import load_vectors_from_file |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 25 | |
| 26 | |
Paul Kehrer | 4112032 | 2014-12-02 18:31:14 -1000 | [diff] [blame] | 27 | def _load_cert(filename, loader, backend): |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 28 | cert = load_vectors_from_file( |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 29 | filename=filename, |
| 30 | loader=lambda pemfile: loader(pemfile.read(), backend), |
| 31 | mode="rb" |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 32 | ) |
| 33 | return cert |
| 34 | |
| 35 | |
| 36 | @pytest.mark.requires_backend_interface(interface=RSABackend) |
| 37 | @pytest.mark.requires_backend_interface(interface=X509Backend) |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 38 | class TestRSACertificate(object): |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 39 | def test_load_pem_cert(self, backend): |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 40 | cert = _load_cert( |
| 41 | os.path.join("x509", "custom", "post2000utctime.pem"), |
Paul Kehrer | 4112032 | 2014-12-02 18:31:14 -1000 | [diff] [blame] | 42 | x509.load_pem_x509_certificate, |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 43 | backend |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 44 | ) |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 45 | assert isinstance(cert, x509.Certificate) |
| 46 | assert cert.serial == 11559813051657483483 |
| 47 | fingerprint = binascii.hexlify(cert.fingerprint(hashes.SHA1())) |
| 48 | assert fingerprint == b"2b619ed04bfc9c3b08eb677d272192286a0947a8" |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 49 | assert isinstance(cert.signature_hash_algorithm, hashes.SHA1) |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 50 | |
| 51 | def test_load_der_cert(self, backend): |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 52 | cert = _load_cert( |
| 53 | os.path.join("x509", "PKITS_data", "certs", "GoodCACert.crt"), |
Paul Kehrer | 4112032 | 2014-12-02 18:31:14 -1000 | [diff] [blame] | 54 | x509.load_der_x509_certificate, |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 55 | backend |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 56 | ) |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 57 | assert isinstance(cert, x509.Certificate) |
| 58 | assert cert.serial == 2 |
| 59 | fingerprint = binascii.hexlify(cert.fingerprint(hashes.SHA1())) |
| 60 | assert fingerprint == b"6f49779533d565e8b7c1062503eab41492c38e4d" |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 61 | assert isinstance(cert.signature_hash_algorithm, hashes.SHA256) |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 62 | |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 63 | def test_issuer(self, backend): |
| 64 | cert = _load_cert( |
| 65 | os.path.join( |
| 66 | "x509", "PKITS_data", "certs", |
| 67 | "Validpre2000UTCnotBeforeDateTest3EE.crt" |
| 68 | ), |
| 69 | x509.load_der_x509_certificate, |
| 70 | backend |
| 71 | ) |
| 72 | issuer = cert.issuer |
| 73 | assert isinstance(issuer, x509.Name) |
Paul Kehrer | 8b21a4a | 2015-02-14 07:56:36 -0600 | [diff] [blame] | 74 | assert list(issuer) == [ |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 75 | x509.NameAttribute(x509.OID_COUNTRY_NAME, 'US'), |
| 76 | x509.NameAttribute( |
| 77 | x509.OID_ORGANIZATION_NAME, 'Test Certificates 2011' |
| 78 | ), |
| 79 | x509.NameAttribute(x509.OID_COMMON_NAME, 'Good CA') |
| 80 | ] |
Paul Kehrer | e901d64 | 2015-02-11 18:50:58 -0600 | [diff] [blame] | 81 | assert issuer.get_attributes_for_oid(x509.OID_COMMON_NAME) == [ |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 82 | x509.NameAttribute(x509.OID_COMMON_NAME, 'Good CA') |
| 83 | ] |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 84 | |
| 85 | def test_all_issuer_name_types(self, backend): |
| 86 | cert = _load_cert( |
| 87 | os.path.join( |
| 88 | "x509", "custom", |
| 89 | "all_supported_names.pem" |
| 90 | ), |
| 91 | x509.load_pem_x509_certificate, |
| 92 | backend |
| 93 | ) |
| 94 | issuer = cert.issuer |
| 95 | |
| 96 | assert isinstance(issuer, x509.Name) |
Paul Kehrer | 8b21a4a | 2015-02-14 07:56:36 -0600 | [diff] [blame] | 97 | assert list(issuer) == [ |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 98 | x509.NameAttribute(x509.OID_COUNTRY_NAME, 'US'), |
| 99 | x509.NameAttribute(x509.OID_COUNTRY_NAME, 'CA'), |
| 100 | x509.NameAttribute(x509.OID_STATE_OR_PROVINCE_NAME, 'Texas'), |
| 101 | x509.NameAttribute(x509.OID_STATE_OR_PROVINCE_NAME, 'Illinois'), |
| 102 | x509.NameAttribute(x509.OID_LOCALITY_NAME, 'Chicago'), |
| 103 | x509.NameAttribute(x509.OID_LOCALITY_NAME, 'Austin'), |
| 104 | x509.NameAttribute(x509.OID_ORGANIZATION_NAME, 'Zero, LLC'), |
| 105 | x509.NameAttribute(x509.OID_ORGANIZATION_NAME, 'One, LLC'), |
| 106 | x509.NameAttribute(x509.OID_COMMON_NAME, 'common name 0'), |
| 107 | x509.NameAttribute(x509.OID_COMMON_NAME, 'common name 1'), |
| 108 | x509.NameAttribute(x509.OID_ORGANIZATIONAL_UNIT_NAME, 'OU 0'), |
| 109 | x509.NameAttribute(x509.OID_ORGANIZATIONAL_UNIT_NAME, 'OU 1'), |
| 110 | x509.NameAttribute(x509.OID_DN_QUALIFIER, 'dnQualifier0'), |
| 111 | x509.NameAttribute(x509.OID_DN_QUALIFIER, 'dnQualifier1'), |
| 112 | x509.NameAttribute(x509.OID_SERIAL_NUMBER, '123'), |
| 113 | x509.NameAttribute(x509.OID_SERIAL_NUMBER, '456'), |
| 114 | x509.NameAttribute(x509.OID_TITLE, 'Title 0'), |
| 115 | x509.NameAttribute(x509.OID_TITLE, 'Title 1'), |
| 116 | x509.NameAttribute(x509.OID_SURNAME, 'Surname 0'), |
| 117 | x509.NameAttribute(x509.OID_SURNAME, 'Surname 1'), |
| 118 | x509.NameAttribute(x509.OID_GIVEN_NAME, 'Given Name 0'), |
| 119 | x509.NameAttribute(x509.OID_GIVEN_NAME, 'Given Name 1'), |
| 120 | x509.NameAttribute(x509.OID_PSEUDONYM, 'Incognito 0'), |
| 121 | x509.NameAttribute(x509.OID_PSEUDONYM, 'Incognito 1'), |
| 122 | x509.NameAttribute(x509.OID_GENERATION_QUALIFIER, 'Last Gen'), |
| 123 | x509.NameAttribute(x509.OID_GENERATION_QUALIFIER, 'Next Gen'), |
| 124 | x509.NameAttribute(x509.OID_DOMAIN_COMPONENT, 'dc0'), |
| 125 | x509.NameAttribute(x509.OID_DOMAIN_COMPONENT, 'dc1'), |
| 126 | x509.NameAttribute(x509.OID_EMAIL_ADDRESS, 'test0@test.local'), |
| 127 | x509.NameAttribute(x509.OID_EMAIL_ADDRESS, 'test1@test.local'), |
| 128 | ] |
| 129 | |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 130 | def test_subject(self, backend): |
| 131 | cert = _load_cert( |
| 132 | os.path.join( |
| 133 | "x509", "PKITS_data", "certs", |
| 134 | "Validpre2000UTCnotBeforeDateTest3EE.crt" |
| 135 | ), |
| 136 | x509.load_der_x509_certificate, |
| 137 | backend |
| 138 | ) |
| 139 | subject = cert.subject |
| 140 | assert isinstance(subject, x509.Name) |
Paul Kehrer | 8b21a4a | 2015-02-14 07:56:36 -0600 | [diff] [blame] | 141 | assert list(subject) == [ |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 142 | x509.NameAttribute(x509.OID_COUNTRY_NAME, 'US'), |
| 143 | x509.NameAttribute( |
| 144 | x509.OID_ORGANIZATION_NAME, 'Test Certificates 2011' |
| 145 | ), |
| 146 | x509.NameAttribute( |
| 147 | x509.OID_COMMON_NAME, |
| 148 | 'Valid pre2000 UTC notBefore Date EE Certificate Test3' |
| 149 | ) |
| 150 | ] |
Paul Kehrer | e901d64 | 2015-02-11 18:50:58 -0600 | [diff] [blame] | 151 | assert subject.get_attributes_for_oid(x509.OID_COMMON_NAME) == [ |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 152 | x509.NameAttribute( |
| 153 | x509.OID_COMMON_NAME, |
| 154 | 'Valid pre2000 UTC notBefore Date EE Certificate Test3' |
| 155 | ) |
| 156 | ] |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 157 | |
| 158 | def test_unicode_name(self, backend): |
| 159 | cert = _load_cert( |
| 160 | os.path.join( |
| 161 | "x509", "custom", |
| 162 | "utf8_common_name.pem" |
| 163 | ), |
| 164 | x509.load_pem_x509_certificate, |
| 165 | backend |
| 166 | ) |
Paul Kehrer | e901d64 | 2015-02-11 18:50:58 -0600 | [diff] [blame] | 167 | assert cert.subject.get_attributes_for_oid(x509.OID_COMMON_NAME) == [ |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 168 | x509.NameAttribute( |
| 169 | x509.OID_COMMON_NAME, |
Paul Kehrer | 53d8d49 | 2015-02-13 18:47:30 -0600 | [diff] [blame] | 170 | six.u('We heart UTF8!\u2122') |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 171 | ) |
| 172 | ] |
Paul Kehrer | e901d64 | 2015-02-11 18:50:58 -0600 | [diff] [blame] | 173 | assert cert.issuer.get_attributes_for_oid(x509.OID_COMMON_NAME) == [ |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 174 | x509.NameAttribute( |
| 175 | x509.OID_COMMON_NAME, |
Paul Kehrer | 53d8d49 | 2015-02-13 18:47:30 -0600 | [diff] [blame] | 176 | six.u('We heart UTF8!\u2122') |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 177 | ) |
| 178 | ] |
| 179 | |
| 180 | def test_all_subject_name_types(self, backend): |
| 181 | cert = _load_cert( |
| 182 | os.path.join( |
| 183 | "x509", "custom", |
| 184 | "all_supported_names.pem" |
| 185 | ), |
| 186 | x509.load_pem_x509_certificate, |
| 187 | backend |
| 188 | ) |
| 189 | subject = cert.subject |
| 190 | assert isinstance(subject, x509.Name) |
Paul Kehrer | 8b21a4a | 2015-02-14 07:56:36 -0600 | [diff] [blame] | 191 | assert list(subject) == [ |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 192 | x509.NameAttribute(x509.OID_COUNTRY_NAME, 'AU'), |
| 193 | x509.NameAttribute(x509.OID_COUNTRY_NAME, 'DE'), |
| 194 | x509.NameAttribute(x509.OID_STATE_OR_PROVINCE_NAME, 'California'), |
| 195 | x509.NameAttribute(x509.OID_STATE_OR_PROVINCE_NAME, 'New York'), |
| 196 | x509.NameAttribute(x509.OID_LOCALITY_NAME, 'San Francisco'), |
| 197 | x509.NameAttribute(x509.OID_LOCALITY_NAME, 'Ithaca'), |
| 198 | x509.NameAttribute(x509.OID_ORGANIZATION_NAME, 'Org Zero, LLC'), |
| 199 | x509.NameAttribute(x509.OID_ORGANIZATION_NAME, 'Org One, LLC'), |
| 200 | x509.NameAttribute(x509.OID_COMMON_NAME, 'CN 0'), |
| 201 | x509.NameAttribute(x509.OID_COMMON_NAME, 'CN 1'), |
| 202 | x509.NameAttribute( |
| 203 | x509.OID_ORGANIZATIONAL_UNIT_NAME, 'Engineering 0' |
| 204 | ), |
| 205 | x509.NameAttribute( |
| 206 | x509.OID_ORGANIZATIONAL_UNIT_NAME, 'Engineering 1' |
| 207 | ), |
| 208 | x509.NameAttribute(x509.OID_DN_QUALIFIER, 'qualified0'), |
| 209 | x509.NameAttribute(x509.OID_DN_QUALIFIER, 'qualified1'), |
| 210 | x509.NameAttribute(x509.OID_SERIAL_NUMBER, '789'), |
| 211 | x509.NameAttribute(x509.OID_SERIAL_NUMBER, '012'), |
| 212 | x509.NameAttribute(x509.OID_TITLE, 'Title IX'), |
| 213 | x509.NameAttribute(x509.OID_TITLE, 'Title X'), |
| 214 | x509.NameAttribute(x509.OID_SURNAME, 'Last 0'), |
| 215 | x509.NameAttribute(x509.OID_SURNAME, 'Last 1'), |
| 216 | x509.NameAttribute(x509.OID_GIVEN_NAME, 'First 0'), |
| 217 | x509.NameAttribute(x509.OID_GIVEN_NAME, 'First 1'), |
| 218 | x509.NameAttribute(x509.OID_PSEUDONYM, 'Guy Incognito 0'), |
| 219 | x509.NameAttribute(x509.OID_PSEUDONYM, 'Guy Incognito 1'), |
| 220 | x509.NameAttribute(x509.OID_GENERATION_QUALIFIER, '32X'), |
| 221 | x509.NameAttribute(x509.OID_GENERATION_QUALIFIER, 'Dreamcast'), |
| 222 | x509.NameAttribute(x509.OID_DOMAIN_COMPONENT, 'dc2'), |
| 223 | x509.NameAttribute(x509.OID_DOMAIN_COMPONENT, 'dc3'), |
| 224 | x509.NameAttribute(x509.OID_EMAIL_ADDRESS, 'test2@test.local'), |
| 225 | x509.NameAttribute(x509.OID_EMAIL_ADDRESS, 'test3@test.local'), |
| 226 | ] |
| 227 | |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 228 | def test_load_good_ca_cert(self, backend): |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 229 | cert = _load_cert( |
| 230 | os.path.join("x509", "PKITS_data", "certs", "GoodCACert.crt"), |
Paul Kehrer | 4112032 | 2014-12-02 18:31:14 -1000 | [diff] [blame] | 231 | x509.load_der_x509_certificate, |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 232 | backend |
| 233 | ) |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 234 | |
Paul Kehrer | d9fc725 | 2014-12-11 12:25:00 -0600 | [diff] [blame] | 235 | assert cert.not_valid_before == datetime.datetime(2010, 1, 1, 8, 30) |
| 236 | assert cert.not_valid_after == datetime.datetime(2030, 12, 31, 8, 30) |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 237 | assert cert.serial == 2 |
| 238 | public_key = cert.public_key() |
Alex Gaynor | 32c57df | 2015-02-23 21:51:27 -0800 | [diff] [blame] | 239 | assert isinstance(public_key, rsa.RSAPublicKey) |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 240 | assert cert.version is x509.Version.v3 |
Paul Kehrer | 0307c37 | 2014-11-27 09:49:31 -1000 | [diff] [blame] | 241 | fingerprint = binascii.hexlify(cert.fingerprint(hashes.SHA1())) |
Paul Kehrer | 4e1db79 | 2014-11-27 10:50:55 -1000 | [diff] [blame] | 242 | assert fingerprint == b"6f49779533d565e8b7c1062503eab41492c38e4d" |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 243 | |
Paul Kehrer | 1eb5b86 | 2014-11-26 11:44:03 -1000 | [diff] [blame] | 244 | def test_utc_pre_2000_not_before_cert(self, backend): |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 245 | cert = _load_cert( |
| 246 | os.path.join( |
| 247 | "x509", "PKITS_data", "certs", |
| 248 | "Validpre2000UTCnotBeforeDateTest3EE.crt" |
| 249 | ), |
Paul Kehrer | 4112032 | 2014-12-02 18:31:14 -1000 | [diff] [blame] | 250 | x509.load_der_x509_certificate, |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 251 | backend |
| 252 | ) |
| 253 | |
Paul Kehrer | d9fc725 | 2014-12-11 12:25:00 -0600 | [diff] [blame] | 254 | assert cert.not_valid_before == datetime.datetime(1950, 1, 1, 12, 1) |
Paul Kehrer | 1eb5b86 | 2014-11-26 11:44:03 -1000 | [diff] [blame] | 255 | |
| 256 | def test_pre_2000_utc_not_after_cert(self, backend): |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 257 | cert = _load_cert( |
| 258 | os.path.join( |
| 259 | "x509", "PKITS_data", "certs", |
| 260 | "Invalidpre2000UTCEEnotAfterDateTest7EE.crt" |
| 261 | ), |
Paul Kehrer | 4112032 | 2014-12-02 18:31:14 -1000 | [diff] [blame] | 262 | x509.load_der_x509_certificate, |
Paul Kehrer | 1eb5b86 | 2014-11-26 11:44:03 -1000 | [diff] [blame] | 263 | backend |
| 264 | ) |
| 265 | |
Paul Kehrer | d9fc725 | 2014-12-11 12:25:00 -0600 | [diff] [blame] | 266 | assert cert.not_valid_after == datetime.datetime(1999, 1, 1, 12, 1) |
Paul Kehrer | 1eb5b86 | 2014-11-26 11:44:03 -1000 | [diff] [blame] | 267 | |
| 268 | def test_post_2000_utc_cert(self, backend): |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 269 | cert = _load_cert( |
Paul Kehrer | 1eb5b86 | 2014-11-26 11:44:03 -1000 | [diff] [blame] | 270 | os.path.join("x509", "custom", "post2000utctime.pem"), |
Paul Kehrer | 4112032 | 2014-12-02 18:31:14 -1000 | [diff] [blame] | 271 | x509.load_pem_x509_certificate, |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 272 | backend |
Paul Kehrer | 1eb5b86 | 2014-11-26 11:44:03 -1000 | [diff] [blame] | 273 | ) |
Paul Kehrer | d9fc725 | 2014-12-11 12:25:00 -0600 | [diff] [blame] | 274 | assert cert.not_valid_before == datetime.datetime( |
| 275 | 2014, 11, 26, 21, 41, 20 |
| 276 | ) |
| 277 | assert cert.not_valid_after == datetime.datetime( |
| 278 | 2014, 12, 26, 21, 41, 20 |
| 279 | ) |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 280 | |
| 281 | def test_generalized_time_not_before_cert(self, backend): |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 282 | cert = _load_cert( |
| 283 | os.path.join( |
| 284 | "x509", "PKITS_data", "certs", |
| 285 | "ValidGeneralizedTimenotBeforeDateTest4EE.crt" |
| 286 | ), |
Paul Kehrer | 4112032 | 2014-12-02 18:31:14 -1000 | [diff] [blame] | 287 | x509.load_der_x509_certificate, |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 288 | backend |
| 289 | ) |
Paul Kehrer | d9fc725 | 2014-12-11 12:25:00 -0600 | [diff] [blame] | 290 | assert cert.not_valid_before == datetime.datetime(2002, 1, 1, 12, 1) |
| 291 | assert cert.not_valid_after == datetime.datetime(2030, 12, 31, 8, 30) |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 292 | assert cert.version is x509.Version.v3 |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 293 | |
| 294 | def test_generalized_time_not_after_cert(self, backend): |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 295 | cert = _load_cert( |
| 296 | os.path.join( |
| 297 | "x509", "PKITS_data", "certs", |
| 298 | "ValidGeneralizedTimenotAfterDateTest8EE.crt" |
| 299 | ), |
Paul Kehrer | 4112032 | 2014-12-02 18:31:14 -1000 | [diff] [blame] | 300 | x509.load_der_x509_certificate, |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 301 | backend |
| 302 | ) |
Paul Kehrer | d9fc725 | 2014-12-11 12:25:00 -0600 | [diff] [blame] | 303 | assert cert.not_valid_before == datetime.datetime(2010, 1, 1, 8, 30) |
| 304 | assert cert.not_valid_after == datetime.datetime(2050, 1, 1, 12, 1) |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 305 | assert cert.version is x509.Version.v3 |
Paul Kehrer | a9d78c1 | 2014-11-26 10:59:03 -1000 | [diff] [blame] | 306 | |
| 307 | def test_invalid_version_cert(self, backend): |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 308 | cert = _load_cert( |
Paul Kehrer | a9d78c1 | 2014-11-26 10:59:03 -1000 | [diff] [blame] | 309 | os.path.join("x509", "custom", "invalid_version.pem"), |
Paul Kehrer | 4112032 | 2014-12-02 18:31:14 -1000 | [diff] [blame] | 310 | x509.load_pem_x509_certificate, |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 311 | backend |
Paul Kehrer | a9d78c1 | 2014-11-26 10:59:03 -1000 | [diff] [blame] | 312 | ) |
Paul Kehrer | d5cccf7 | 2014-12-15 17:20:33 -0600 | [diff] [blame] | 313 | with pytest.raises(x509.InvalidVersion) as exc: |
Paul Kehrer | a9d78c1 | 2014-11-26 10:59:03 -1000 | [diff] [blame] | 314 | cert.version |
Paul Kehrer | 30c5ccd | 2014-11-26 11:10:28 -1000 | [diff] [blame] | 315 | |
Paul Kehrer | d5cccf7 | 2014-12-15 17:20:33 -0600 | [diff] [blame] | 316 | assert exc.value.parsed_version == 7 |
| 317 | |
Paul Kehrer | 30c5ccd | 2014-11-26 11:10:28 -1000 | [diff] [blame] | 318 | def test_version_1_cert(self, backend): |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 319 | cert = _load_cert( |
Paul Kehrer | 30c5ccd | 2014-11-26 11:10:28 -1000 | [diff] [blame] | 320 | os.path.join("x509", "v1_cert.pem"), |
Paul Kehrer | 4112032 | 2014-12-02 18:31:14 -1000 | [diff] [blame] | 321 | x509.load_pem_x509_certificate, |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 322 | backend |
Paul Kehrer | 30c5ccd | 2014-11-26 11:10:28 -1000 | [diff] [blame] | 323 | ) |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 324 | assert cert.version is x509.Version.v1 |
Paul Kehrer | 7638c31 | 2014-11-26 11:13:31 -1000 | [diff] [blame] | 325 | |
| 326 | def test_invalid_pem(self, backend): |
| 327 | with pytest.raises(ValueError): |
| 328 | x509.load_pem_x509_certificate(b"notacert", backend) |
| 329 | |
| 330 | def test_invalid_der(self, backend): |
| 331 | with pytest.raises(ValueError): |
| 332 | x509.load_der_x509_certificate(b"notacert", backend) |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 333 | |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 334 | def test_unsupported_signature_hash_algorithm_cert(self, backend): |
| 335 | cert = _load_cert( |
| 336 | os.path.join("x509", "verisign_md2_root.pem"), |
| 337 | x509.load_pem_x509_certificate, |
| 338 | backend |
| 339 | ) |
| 340 | with pytest.raises(UnsupportedAlgorithm): |
| 341 | cert.signature_hash_algorithm |
| 342 | |
Paul Kehrer | 1effb6e | 2015-03-30 15:05:59 -0500 | [diff] [blame] | 343 | @pytest.mark.parametrize( |
| 344 | ("path", "loader_func"), |
| 345 | [ |
| 346 | [ |
| 347 | os.path.join("x509", "requests", "rsa_sha1.pem"), |
| 348 | x509.load_pem_x509_csr |
| 349 | ], |
| 350 | [ |
| 351 | os.path.join("x509", "requests", "rsa_sha1.der"), |
| 352 | x509.load_der_x509_csr |
| 353 | ], |
| 354 | ] |
| 355 | ) |
| 356 | def test_load_rsa_certificate_request(self, path, loader_func, backend): |
| 357 | request = _load_cert(path, loader_func, backend) |
Paul Kehrer | dc480ad | 2015-02-23 12:14:54 -0600 | [diff] [blame] | 358 | assert isinstance(request.signature_hash_algorithm, hashes.SHA1) |
| 359 | public_key = request.public_key() |
| 360 | assert isinstance(public_key, rsa.RSAPublicKey) |
| 361 | subject = request.subject |
| 362 | assert isinstance(subject, x509.Name) |
| 363 | assert list(subject) == [ |
| 364 | x509.NameAttribute(x509.OID_COUNTRY_NAME, 'US'), |
| 365 | x509.NameAttribute(x509.OID_STATE_OR_PROVINCE_NAME, 'Texas'), |
| 366 | x509.NameAttribute(x509.OID_LOCALITY_NAME, 'Austin'), |
| 367 | x509.NameAttribute(x509.OID_ORGANIZATION_NAME, 'PyCA'), |
| 368 | x509.NameAttribute(x509.OID_COMMON_NAME, 'cryptography.io'), |
| 369 | ] |
| 370 | |
Paul Kehrer | 1effb6e | 2015-03-30 15:05:59 -0500 | [diff] [blame] | 371 | @pytest.mark.parametrize( |
| 372 | "loader_func", |
| 373 | [x509.load_pem_x509_csr, x509.load_der_x509_csr] |
| 374 | ) |
| 375 | def test_invalid_certificate_request(self, loader_func, backend): |
Paul Kehrer | b759e29 | 2015-03-17 07:34:41 -0500 | [diff] [blame] | 376 | with pytest.raises(ValueError): |
Paul Kehrer | 1effb6e | 2015-03-30 15:05:59 -0500 | [diff] [blame] | 377 | loader_func(b"notacsr", backend) |
Paul Kehrer | b759e29 | 2015-03-17 07:34:41 -0500 | [diff] [blame] | 378 | |
Paul Kehrer | dc480ad | 2015-02-23 12:14:54 -0600 | [diff] [blame] | 379 | def test_unsupported_signature_hash_algorithm_request(self, backend): |
| 380 | request = _load_cert( |
| 381 | os.path.join("x509", "requests", "rsa_md4.pem"), |
Paul Kehrer | 31e3988 | 2015-03-11 11:37:04 -0500 | [diff] [blame] | 382 | x509.load_pem_x509_csr, |
Paul Kehrer | dc480ad | 2015-02-23 12:14:54 -0600 | [diff] [blame] | 383 | backend |
| 384 | ) |
| 385 | with pytest.raises(UnsupportedAlgorithm): |
| 386 | request.signature_hash_algorithm |
| 387 | |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 388 | |
| 389 | @pytest.mark.requires_backend_interface(interface=DSABackend) |
| 390 | @pytest.mark.requires_backend_interface(interface=X509Backend) |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 391 | class TestDSACertificate(object): |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 392 | def test_load_dsa_cert(self, backend): |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 393 | cert = _load_cert( |
Paul Kehrer | 4903adc | 2014-12-13 16:57:50 -0600 | [diff] [blame] | 394 | os.path.join("x509", "custom", "dsa_selfsigned_ca.pem"), |
Paul Kehrer | 4112032 | 2014-12-02 18:31:14 -1000 | [diff] [blame] | 395 | x509.load_pem_x509_certificate, |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 396 | backend |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 397 | ) |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 398 | assert isinstance(cert.signature_hash_algorithm, hashes.SHA1) |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 399 | public_key = cert.public_key() |
Alex Gaynor | 32c57df | 2015-02-23 21:51:27 -0800 | [diff] [blame] | 400 | assert isinstance(public_key, dsa.DSAPublicKey) |
Alex Gaynor | fcadda6 | 2015-03-10 10:01:18 -0400 | [diff] [blame] | 401 | if isinstance(public_key, dsa.DSAPublicKeyWithSerialization): |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 402 | num = public_key.public_numbers() |
| 403 | assert num.y == int( |
| 404 | "4c08bfe5f2d76649c80acf7d431f6ae2124b217abc8c9f6aca776ddfa94" |
| 405 | "53b6656f13e543684cd5f6431a314377d2abfa068b7080cb8ddc065afc2" |
| 406 | "dea559f0b584c97a2b235b9b69b46bc6de1aed422a6f341832618bcaae2" |
| 407 | "198aba388099dafb05ff0b5efecb3b0ae169a62e1c72022af50ae68af3b" |
| 408 | "033c18e6eec1f7df4692c456ccafb79cc7e08da0a5786e9816ceda651d6" |
| 409 | "1b4bb7b81c2783da97cea62df67af5e85991fdc13aff10fc60e06586386" |
| 410 | "b96bb78d65750f542f86951e05a6d81baadbcd35a2e5cad4119923ae6a2" |
| 411 | "002091a3d17017f93c52970113cdc119970b9074ca506eac91c3dd37632" |
| 412 | "5df4af6b3911ef267d26623a5a1c5df4a6d13f1c", 16 |
| 413 | ) |
| 414 | assert num.parameter_numbers.g == int( |
| 415 | "4b7ced71dc353965ecc10d441a9a06fc24943a32d66429dd5ef44d43e67" |
| 416 | "d789d99770aec32c0415dc92970880872da45fef8dd1e115a3e4801387b" |
| 417 | "a6d755861f062fd3b6e9ea8e2641152339b828315b1528ee6c7b79458d2" |
| 418 | "1f3db973f6fc303f9397174c2799dd2351282aa2d8842c357a73495bbaa" |
| 419 | "c4932786414c55e60d73169f5761036fba29e9eebfb049f8a3b1b7cee6f" |
| 420 | "3fbfa136205f130bee2cf5b9c38dc1095d4006f2e73335c07352c64130a" |
| 421 | "1ab2b89f13b48f628d3cc3868beece9bb7beade9f830eacc6fa241425c0" |
| 422 | "b3fcc0df416a0c89f7bf35668d765ec95cdcfbe9caff49cfc156c668c76" |
| 423 | "fa6247676a6d3ac945844a083509c6a1b436baca", 16 |
| 424 | ) |
| 425 | assert num.parameter_numbers.p == int( |
| 426 | "bfade6048e373cd4e48b677e878c8e5b08c02102ae04eb2cb5c46a523a3" |
| 427 | "af1c73d16b24f34a4964781ae7e50500e21777754a670bd19a7420d6330" |
| 428 | "84e5556e33ca2c0e7d547ea5f46a07a01bf8669ae3bdec042d9b2ae5e6e" |
| 429 | "cf49f00ba9dac99ab6eff140d2cedf722ee62c2f9736857971444c25d0a" |
| 430 | "33d2017dc36d682a1054fe2a9428dda355a851ce6e6d61e03e419fd4ca4" |
| 431 | "e703313743d86caa885930f62ed5bf342d8165627681e9cc3244ba72aa2" |
| 432 | "2148400a6bbe80154e855d042c9dc2a3405f1e517be9dea50562f56da93" |
| 433 | "f6085f844a7e705c1f043e65751c583b80d29103e590ccb26efdaa0893d" |
| 434 | "833e36468f3907cfca788a3cb790f0341c8a31bf", 16 |
| 435 | ) |
| 436 | assert num.parameter_numbers.q == int( |
| 437 | "822ff5d234e073b901cf5941f58e1f538e71d40d", 16 |
| 438 | ) |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 439 | |
Paul Kehrer | 1effb6e | 2015-03-30 15:05:59 -0500 | [diff] [blame] | 440 | @pytest.mark.parametrize( |
| 441 | ("path", "loader_func"), |
| 442 | [ |
| 443 | [ |
| 444 | os.path.join("x509", "requests", "dsa_sha1.pem"), |
| 445 | x509.load_pem_x509_csr |
| 446 | ], |
| 447 | [ |
| 448 | os.path.join("x509", "requests", "dsa_sha1.der"), |
| 449 | x509.load_der_x509_csr |
| 450 | ], |
| 451 | ] |
| 452 | ) |
| 453 | def test_load_dsa_request(self, path, loader_func, backend): |
| 454 | request = _load_cert(path, loader_func, backend) |
Paul Kehrer | dc480ad | 2015-02-23 12:14:54 -0600 | [diff] [blame] | 455 | assert isinstance(request.signature_hash_algorithm, hashes.SHA1) |
| 456 | public_key = request.public_key() |
| 457 | assert isinstance(public_key, dsa.DSAPublicKey) |
| 458 | subject = request.subject |
| 459 | assert isinstance(subject, x509.Name) |
| 460 | assert list(subject) == [ |
| 461 | x509.NameAttribute(x509.OID_COMMON_NAME, 'cryptography.io'), |
| 462 | x509.NameAttribute(x509.OID_ORGANIZATION_NAME, 'PyCA'), |
| 463 | x509.NameAttribute(x509.OID_COUNTRY_NAME, 'US'), |
| 464 | x509.NameAttribute(x509.OID_STATE_OR_PROVINCE_NAME, 'Texas'), |
| 465 | x509.NameAttribute(x509.OID_LOCALITY_NAME, 'Austin'), |
| 466 | ] |
| 467 | |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 468 | |
| 469 | @pytest.mark.requires_backend_interface(interface=EllipticCurveBackend) |
| 470 | @pytest.mark.requires_backend_interface(interface=X509Backend) |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 471 | class TestECDSACertificate(object): |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 472 | def test_load_ecdsa_cert(self, backend): |
| 473 | _skip_curve_unsupported(backend, ec.SECP384R1()) |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 474 | cert = _load_cert( |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 475 | os.path.join("x509", "ecdsa_root.pem"), |
Paul Kehrer | 4112032 | 2014-12-02 18:31:14 -1000 | [diff] [blame] | 476 | x509.load_pem_x509_certificate, |
Paul Kehrer | a693cfd | 2014-11-27 07:47:58 -1000 | [diff] [blame] | 477 | backend |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 478 | ) |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 479 | assert isinstance(cert.signature_hash_algorithm, hashes.SHA384) |
Paul Kehrer | f1ef351 | 2014-11-26 17:36:05 -1000 | [diff] [blame] | 480 | public_key = cert.public_key() |
Alex Gaynor | 32c57df | 2015-02-23 21:51:27 -0800 | [diff] [blame] | 481 | assert isinstance(public_key, ec.EllipticCurvePublicKey) |
Alex Gaynor | fcadda6 | 2015-03-10 10:01:18 -0400 | [diff] [blame] | 482 | if isinstance(public_key, ec.EllipticCurvePublicKeyWithSerialization): |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 483 | num = public_key.public_numbers() |
| 484 | assert num.x == int( |
| 485 | "dda7d9bb8ab80bfb0b7f21d2f0bebe73f3335d1abc34eadec69bbcd095f" |
| 486 | "6f0ccd00bba615b51467e9e2d9fee8e630c17", 16 |
| 487 | ) |
| 488 | assert num.y == int( |
| 489 | "ec0770f5cf842e40839ce83f416d3badd3a4145936789d0343ee10136c7" |
| 490 | "2deae88a7a16bb543ce67dc23ff031ca3e23e", 16 |
| 491 | ) |
| 492 | assert isinstance(num.curve, ec.SECP384R1) |
Paul Kehrer | 6c660a8 | 2014-12-12 11:50:44 -0600 | [diff] [blame] | 493 | |
| 494 | def test_load_ecdsa_no_named_curve(self, backend): |
| 495 | _skip_curve_unsupported(backend, ec.SECP256R1()) |
| 496 | cert = _load_cert( |
| 497 | os.path.join("x509", "custom", "ec_no_named_curve.pem"), |
| 498 | x509.load_pem_x509_certificate, |
| 499 | backend |
| 500 | ) |
| 501 | with pytest.raises(NotImplementedError): |
| 502 | cert.public_key() |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 503 | |
Paul Kehrer | 1effb6e | 2015-03-30 15:05:59 -0500 | [diff] [blame] | 504 | @pytest.mark.parametrize( |
| 505 | ("path", "loader_func"), |
| 506 | [ |
| 507 | [ |
| 508 | os.path.join("x509", "requests", "ec_sha256.pem"), |
| 509 | x509.load_pem_x509_csr |
| 510 | ], |
| 511 | [ |
| 512 | os.path.join("x509", "requests", "ec_sha256.der"), |
| 513 | x509.load_der_x509_csr |
| 514 | ], |
| 515 | ] |
| 516 | ) |
| 517 | def test_load_ecdsa_certificate_request(self, path, loader_func, backend): |
Paul Kehrer | dc480ad | 2015-02-23 12:14:54 -0600 | [diff] [blame] | 518 | _skip_curve_unsupported(backend, ec.SECP384R1()) |
Paul Kehrer | 1effb6e | 2015-03-30 15:05:59 -0500 | [diff] [blame] | 519 | request = _load_cert(path, loader_func, backend) |
Paul Kehrer | dc480ad | 2015-02-23 12:14:54 -0600 | [diff] [blame] | 520 | assert isinstance(request.signature_hash_algorithm, hashes.SHA256) |
| 521 | public_key = request.public_key() |
| 522 | assert isinstance(public_key, ec.EllipticCurvePublicKey) |
| 523 | subject = request.subject |
| 524 | assert isinstance(subject, x509.Name) |
| 525 | assert list(subject) == [ |
| 526 | x509.NameAttribute(x509.OID_COMMON_NAME, 'cryptography.io'), |
| 527 | x509.NameAttribute(x509.OID_ORGANIZATION_NAME, 'PyCA'), |
| 528 | x509.NameAttribute(x509.OID_COUNTRY_NAME, 'US'), |
| 529 | x509.NameAttribute(x509.OID_STATE_OR_PROVINCE_NAME, 'Texas'), |
| 530 | x509.NameAttribute(x509.OID_LOCALITY_NAME, 'Austin'), |
| 531 | ] |
| 532 | |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 533 | |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 534 | class TestNameAttribute(object): |
Alex Gaynor | a56ff41 | 2015-02-10 17:26:32 -0500 | [diff] [blame] | 535 | def test_init_bad_oid(self): |
| 536 | with pytest.raises(TypeError): |
| 537 | x509.NameAttribute(None, 'value') |
| 538 | |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 539 | def test_eq(self): |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 540 | assert x509.NameAttribute( |
| 541 | x509.ObjectIdentifier('oid'), 'value' |
| 542 | ) == x509.NameAttribute( |
| 543 | x509.ObjectIdentifier('oid'), 'value' |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 544 | ) |
| 545 | |
| 546 | def test_ne(self): |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 547 | assert x509.NameAttribute( |
| 548 | x509.ObjectIdentifier('2.5.4.3'), 'value' |
| 549 | ) != x509.NameAttribute( |
| 550 | x509.ObjectIdentifier('2.5.4.5'), 'value' |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 551 | ) |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 552 | assert x509.NameAttribute( |
| 553 | x509.ObjectIdentifier('oid'), 'value' |
| 554 | ) != x509.NameAttribute( |
| 555 | x509.ObjectIdentifier('oid'), 'value2' |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 556 | ) |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 557 | assert x509.NameAttribute( |
| 558 | x509.ObjectIdentifier('oid'), 'value' |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 559 | ) != object() |
| 560 | |
Paul Kehrer | a498be8 | 2015-02-12 15:00:56 -0600 | [diff] [blame] | 561 | def test_repr(self): |
| 562 | na = x509.NameAttribute(x509.ObjectIdentifier('2.5.4.3'), 'value') |
| 563 | assert repr(na) == ( |
| 564 | "<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName" |
| 565 | ")>, value='value')>" |
| 566 | ) |
| 567 | |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 568 | |
| 569 | class TestObjectIdentifier(object): |
| 570 | def test_eq(self): |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 571 | oid1 = x509.ObjectIdentifier('oid') |
| 572 | oid2 = x509.ObjectIdentifier('oid') |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 573 | assert oid1 == oid2 |
| 574 | |
| 575 | def test_ne(self): |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 576 | oid1 = x509.ObjectIdentifier('oid') |
| 577 | assert oid1 != x509.ObjectIdentifier('oid1') |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 578 | assert oid1 != object() |
| 579 | |
| 580 | def test_repr(self): |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 581 | oid = x509.ObjectIdentifier("2.5.4.3") |
| 582 | assert repr(oid) == "<ObjectIdentifier(oid=2.5.4.3, name=commonName)>" |
| 583 | oid = x509.ObjectIdentifier("oid1") |
| 584 | assert repr(oid) == "<ObjectIdentifier(oid=oid1, name=Unknown OID)>" |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 585 | |
| 586 | |
| 587 | class TestName(object): |
| 588 | def test_eq(self): |
| 589 | name1 = x509.Name([ |
| 590 | x509.NameAttribute(x509.ObjectIdentifier('oid'), 'value1'), |
| 591 | x509.NameAttribute(x509.ObjectIdentifier('oid2'), 'value2'), |
| 592 | ]) |
| 593 | name2 = x509.Name([ |
| 594 | x509.NameAttribute(x509.ObjectIdentifier('oid'), 'value1'), |
| 595 | x509.NameAttribute(x509.ObjectIdentifier('oid2'), 'value2'), |
| 596 | ]) |
| 597 | assert name1 == name2 |
| 598 | |
| 599 | def test_ne(self): |
| 600 | name1 = x509.Name([ |
| 601 | x509.NameAttribute(x509.ObjectIdentifier('oid'), 'value1'), |
| 602 | x509.NameAttribute(x509.ObjectIdentifier('oid2'), 'value2'), |
| 603 | ]) |
| 604 | name2 = x509.Name([ |
| 605 | x509.NameAttribute(x509.ObjectIdentifier('oid2'), 'value2'), |
| 606 | x509.NameAttribute(x509.ObjectIdentifier('oid'), 'value1'), |
| 607 | ]) |
| 608 | assert name1 != name2 |
| 609 | assert name1 != object() |
Paul Kehrer | 1fb35c9 | 2015-04-11 15:42:54 -0400 | [diff] [blame] | 610 | |
| 611 | def test_repr(self): |
| 612 | name = x509.Name([ |
| 613 | x509.NameAttribute(x509.OID_COMMON_NAME, 'cryptography.io'), |
| 614 | x509.NameAttribute(x509.OID_ORGANIZATION_NAME, 'PyCA'), |
| 615 | ]) |
| 616 | |
| 617 | assert repr(name) == ( |
Paul Kehrer | e613e5d | 2015-04-11 15:52:04 -0400 | [diff] [blame] | 618 | "<Name([<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=com" |
| 619 | "monName)>, value='cryptography.io')>, <NameAttribute(oid=<ObjectI" |
| 620 | "dentifier(oid=2.5.4.10, name=organizationName)>, value='PyCA')>])" |
| 621 | ">" |
Paul Kehrer | 1fb35c9 | 2015-04-11 15:42:54 -0400 | [diff] [blame] | 622 | ) |