blob: d8a4cc022da13301640a1642753a83668a9079ee [file] [log] [blame]
Paul Kehrer016e08a2014-11-26 09:41:18 -10001# 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
5from __future__ import absolute_import, division, print_function
6
Paul Kehrer0307c372014-11-27 09:49:31 -10007import binascii
Paul Kehrer016e08a2014-11-26 09:41:18 -10008import datetime
9import os
Paul Kehrer016e08a2014-11-26 09:41:18 -100010
11import pytest
12
Paul Kehrer53d8d492015-02-13 18:47:30 -060013import six
14
Paul Kehrer016e08a2014-11-26 09:41:18 -100015from cryptography import x509
Paul Kehrer8802a5b2015-02-13 12:06:57 -060016from cryptography.exceptions import UnsupportedAlgorithm
Paul Kehrerf1ef3512014-11-26 17:36:05 -100017from cryptography.hazmat.backends.interfaces import (
18 DSABackend, EllipticCurveBackend, RSABackend, X509Backend
19)
Alex Gaynor32c57df2015-02-23 21:51:27 -080020from cryptography.hazmat.primitives import hashes
21from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa
Paul Kehrer016e08a2014-11-26 09:41:18 -100022
Paul Kehrerf1ef3512014-11-26 17:36:05 -100023from .hazmat.primitives.test_ec import _skip_curve_unsupported
Paul Kehrera9d78c12014-11-26 10:59:03 -100024from .utils import load_vectors_from_file
Paul Kehrer016e08a2014-11-26 09:41:18 -100025
26
Paul Kehrer41120322014-12-02 18:31:14 -100027def _load_cert(filename, loader, backend):
Paul Kehrer016e08a2014-11-26 09:41:18 -100028 cert = load_vectors_from_file(
Paul Kehrera693cfd2014-11-27 07:47:58 -100029 filename=filename,
30 loader=lambda pemfile: loader(pemfile.read(), backend),
31 mode="rb"
Paul Kehrer016e08a2014-11-26 09:41:18 -100032 )
33 return cert
34
35
36@pytest.mark.requires_backend_interface(interface=RSABackend)
37@pytest.mark.requires_backend_interface(interface=X509Backend)
Paul Kehrere76cd272014-12-14 19:00:51 -060038class TestRSACertificate(object):
Paul Kehrerf1ef3512014-11-26 17:36:05 -100039 def test_load_pem_cert(self, backend):
Paul Kehrera693cfd2014-11-27 07:47:58 -100040 cert = _load_cert(
41 os.path.join("x509", "custom", "post2000utctime.pem"),
Paul Kehrer41120322014-12-02 18:31:14 -100042 x509.load_pem_x509_certificate,
Paul Kehrera693cfd2014-11-27 07:47:58 -100043 backend
Paul Kehrerf1ef3512014-11-26 17:36:05 -100044 )
Paul Kehrere76cd272014-12-14 19:00:51 -060045 assert isinstance(cert, x509.Certificate)
46 assert cert.serial == 11559813051657483483
47 fingerprint = binascii.hexlify(cert.fingerprint(hashes.SHA1()))
48 assert fingerprint == b"2b619ed04bfc9c3b08eb677d272192286a0947a8"
Paul Kehrer8802a5b2015-02-13 12:06:57 -060049 assert isinstance(cert.signature_hash_algorithm, hashes.SHA1)
Paul Kehrerf1ef3512014-11-26 17:36:05 -100050
51 def test_load_der_cert(self, backend):
Paul Kehrera693cfd2014-11-27 07:47:58 -100052 cert = _load_cert(
53 os.path.join("x509", "PKITS_data", "certs", "GoodCACert.crt"),
Paul Kehrer41120322014-12-02 18:31:14 -100054 x509.load_der_x509_certificate,
Paul Kehrera693cfd2014-11-27 07:47:58 -100055 backend
Paul Kehrerf1ef3512014-11-26 17:36:05 -100056 )
Paul Kehrere76cd272014-12-14 19:00:51 -060057 assert isinstance(cert, x509.Certificate)
58 assert cert.serial == 2
59 fingerprint = binascii.hexlify(cert.fingerprint(hashes.SHA1()))
60 assert fingerprint == b"6f49779533d565e8b7c1062503eab41492c38e4d"
Paul Kehrer8802a5b2015-02-13 12:06:57 -060061 assert isinstance(cert.signature_hash_algorithm, hashes.SHA256)
Paul Kehrerf1ef3512014-11-26 17:36:05 -100062
Paul Kehrer719d5362015-01-01 20:03:52 -060063 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 Kehrer8b21a4a2015-02-14 07:56:36 -060074 assert list(issuer) == [
Paul Kehrer719d5362015-01-01 20:03:52 -060075 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 Kehrere901d642015-02-11 18:50:58 -060081 assert issuer.get_attributes_for_oid(x509.OID_COMMON_NAME) == [
Paul Kehrer719d5362015-01-01 20:03:52 -060082 x509.NameAttribute(x509.OID_COMMON_NAME, 'Good CA')
83 ]
Paul Kehrer719d5362015-01-01 20:03:52 -060084
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 Kehrer8b21a4a2015-02-14 07:56:36 -060097 assert list(issuer) == [
Paul Kehrer719d5362015-01-01 20:03:52 -060098 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 Kehrer719d5362015-01-01 20:03:52 -0600130 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 Kehrer8b21a4a2015-02-14 07:56:36 -0600141 assert list(subject) == [
Paul Kehrer719d5362015-01-01 20:03:52 -0600142 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 Kehrere901d642015-02-11 18:50:58 -0600151 assert subject.get_attributes_for_oid(x509.OID_COMMON_NAME) == [
Paul Kehrer719d5362015-01-01 20:03:52 -0600152 x509.NameAttribute(
153 x509.OID_COMMON_NAME,
154 'Valid pre2000 UTC notBefore Date EE Certificate Test3'
155 )
156 ]
Paul Kehrer719d5362015-01-01 20:03:52 -0600157
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 Kehrere901d642015-02-11 18:50:58 -0600167 assert cert.subject.get_attributes_for_oid(x509.OID_COMMON_NAME) == [
Paul Kehrer719d5362015-01-01 20:03:52 -0600168 x509.NameAttribute(
169 x509.OID_COMMON_NAME,
Paul Kehrer53d8d492015-02-13 18:47:30 -0600170 six.u('We heart UTF8!\u2122')
Paul Kehrer719d5362015-01-01 20:03:52 -0600171 )
172 ]
Paul Kehrere901d642015-02-11 18:50:58 -0600173 assert cert.issuer.get_attributes_for_oid(x509.OID_COMMON_NAME) == [
Paul Kehrer719d5362015-01-01 20:03:52 -0600174 x509.NameAttribute(
175 x509.OID_COMMON_NAME,
Paul Kehrer53d8d492015-02-13 18:47:30 -0600176 six.u('We heart UTF8!\u2122')
Paul Kehrer719d5362015-01-01 20:03:52 -0600177 )
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 Kehrer8b21a4a2015-02-14 07:56:36 -0600191 assert list(subject) == [
Paul Kehrer719d5362015-01-01 20:03:52 -0600192 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 Kehrer016e08a2014-11-26 09:41:18 -1000228 def test_load_good_ca_cert(self, backend):
Paul Kehrera693cfd2014-11-27 07:47:58 -1000229 cert = _load_cert(
230 os.path.join("x509", "PKITS_data", "certs", "GoodCACert.crt"),
Paul Kehrer41120322014-12-02 18:31:14 -1000231 x509.load_der_x509_certificate,
Paul Kehrera693cfd2014-11-27 07:47:58 -1000232 backend
233 )
Paul Kehrer016e08a2014-11-26 09:41:18 -1000234
Paul Kehrerd9fc7252014-12-11 12:25:00 -0600235 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 Kehrer016e08a2014-11-26 09:41:18 -1000237 assert cert.serial == 2
238 public_key = cert.public_key()
Alex Gaynor32c57df2015-02-23 21:51:27 -0800239 assert isinstance(public_key, rsa.RSAPublicKey)
Paul Kehrere76cd272014-12-14 19:00:51 -0600240 assert cert.version is x509.Version.v3
Paul Kehrer0307c372014-11-27 09:49:31 -1000241 fingerprint = binascii.hexlify(cert.fingerprint(hashes.SHA1()))
Paul Kehrer4e1db792014-11-27 10:50:55 -1000242 assert fingerprint == b"6f49779533d565e8b7c1062503eab41492c38e4d"
Paul Kehrer016e08a2014-11-26 09:41:18 -1000243
Paul Kehrer1eb5b862014-11-26 11:44:03 -1000244 def test_utc_pre_2000_not_before_cert(self, backend):
Paul Kehrera693cfd2014-11-27 07:47:58 -1000245 cert = _load_cert(
246 os.path.join(
247 "x509", "PKITS_data", "certs",
248 "Validpre2000UTCnotBeforeDateTest3EE.crt"
249 ),
Paul Kehrer41120322014-12-02 18:31:14 -1000250 x509.load_der_x509_certificate,
Paul Kehrer016e08a2014-11-26 09:41:18 -1000251 backend
252 )
253
Paul Kehrerd9fc7252014-12-11 12:25:00 -0600254 assert cert.not_valid_before == datetime.datetime(1950, 1, 1, 12, 1)
Paul Kehrer1eb5b862014-11-26 11:44:03 -1000255
256 def test_pre_2000_utc_not_after_cert(self, backend):
Paul Kehrera693cfd2014-11-27 07:47:58 -1000257 cert = _load_cert(
258 os.path.join(
259 "x509", "PKITS_data", "certs",
260 "Invalidpre2000UTCEEnotAfterDateTest7EE.crt"
261 ),
Paul Kehrer41120322014-12-02 18:31:14 -1000262 x509.load_der_x509_certificate,
Paul Kehrer1eb5b862014-11-26 11:44:03 -1000263 backend
264 )
265
Paul Kehrerd9fc7252014-12-11 12:25:00 -0600266 assert cert.not_valid_after == datetime.datetime(1999, 1, 1, 12, 1)
Paul Kehrer1eb5b862014-11-26 11:44:03 -1000267
268 def test_post_2000_utc_cert(self, backend):
Paul Kehrera693cfd2014-11-27 07:47:58 -1000269 cert = _load_cert(
Paul Kehrer1eb5b862014-11-26 11:44:03 -1000270 os.path.join("x509", "custom", "post2000utctime.pem"),
Paul Kehrer41120322014-12-02 18:31:14 -1000271 x509.load_pem_x509_certificate,
Paul Kehrera693cfd2014-11-27 07:47:58 -1000272 backend
Paul Kehrer1eb5b862014-11-26 11:44:03 -1000273 )
Paul Kehrerd9fc7252014-12-11 12:25:00 -0600274 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 Kehrer016e08a2014-11-26 09:41:18 -1000280
281 def test_generalized_time_not_before_cert(self, backend):
Paul Kehrera693cfd2014-11-27 07:47:58 -1000282 cert = _load_cert(
283 os.path.join(
284 "x509", "PKITS_data", "certs",
285 "ValidGeneralizedTimenotBeforeDateTest4EE.crt"
286 ),
Paul Kehrer41120322014-12-02 18:31:14 -1000287 x509.load_der_x509_certificate,
Paul Kehrer016e08a2014-11-26 09:41:18 -1000288 backend
289 )
Paul Kehrerd9fc7252014-12-11 12:25:00 -0600290 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 Kehrere76cd272014-12-14 19:00:51 -0600292 assert cert.version is x509.Version.v3
Paul Kehrer016e08a2014-11-26 09:41:18 -1000293
294 def test_generalized_time_not_after_cert(self, backend):
Paul Kehrera693cfd2014-11-27 07:47:58 -1000295 cert = _load_cert(
296 os.path.join(
297 "x509", "PKITS_data", "certs",
298 "ValidGeneralizedTimenotAfterDateTest8EE.crt"
299 ),
Paul Kehrer41120322014-12-02 18:31:14 -1000300 x509.load_der_x509_certificate,
Paul Kehrer016e08a2014-11-26 09:41:18 -1000301 backend
302 )
Paul Kehrerd9fc7252014-12-11 12:25:00 -0600303 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 Kehrere76cd272014-12-14 19:00:51 -0600305 assert cert.version is x509.Version.v3
Paul Kehrera9d78c12014-11-26 10:59:03 -1000306
307 def test_invalid_version_cert(self, backend):
Paul Kehrera693cfd2014-11-27 07:47:58 -1000308 cert = _load_cert(
Paul Kehrera9d78c12014-11-26 10:59:03 -1000309 os.path.join("x509", "custom", "invalid_version.pem"),
Paul Kehrer41120322014-12-02 18:31:14 -1000310 x509.load_pem_x509_certificate,
Paul Kehrera693cfd2014-11-27 07:47:58 -1000311 backend
Paul Kehrera9d78c12014-11-26 10:59:03 -1000312 )
Paul Kehrerd5cccf72014-12-15 17:20:33 -0600313 with pytest.raises(x509.InvalidVersion) as exc:
Paul Kehrera9d78c12014-11-26 10:59:03 -1000314 cert.version
Paul Kehrer30c5ccd2014-11-26 11:10:28 -1000315
Paul Kehrerd5cccf72014-12-15 17:20:33 -0600316 assert exc.value.parsed_version == 7
317
Paul Kehrer30c5ccd2014-11-26 11:10:28 -1000318 def test_version_1_cert(self, backend):
Paul Kehrera693cfd2014-11-27 07:47:58 -1000319 cert = _load_cert(
Paul Kehrer30c5ccd2014-11-26 11:10:28 -1000320 os.path.join("x509", "v1_cert.pem"),
Paul Kehrer41120322014-12-02 18:31:14 -1000321 x509.load_pem_x509_certificate,
Paul Kehrera693cfd2014-11-27 07:47:58 -1000322 backend
Paul Kehrer30c5ccd2014-11-26 11:10:28 -1000323 )
Paul Kehrere76cd272014-12-14 19:00:51 -0600324 assert cert.version is x509.Version.v1
Paul Kehrer7638c312014-11-26 11:13:31 -1000325
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 Kehrerf1ef3512014-11-26 17:36:05 -1000333
Paul Kehrer8802a5b2015-02-13 12:06:57 -0600334 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 Kehrer1effb6e2015-03-30 15:05:59 -0500343 @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 Kehrerdc480ad2015-02-23 12:14:54 -0600358 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 Kehrer1effb6e2015-03-30 15:05:59 -0500371 @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 Kehrerb759e292015-03-17 07:34:41 -0500376 with pytest.raises(ValueError):
Paul Kehrer1effb6e2015-03-30 15:05:59 -0500377 loader_func(b"notacsr", backend)
Paul Kehrerb759e292015-03-17 07:34:41 -0500378
Paul Kehrerdc480ad2015-02-23 12:14:54 -0600379 def test_unsupported_signature_hash_algorithm_request(self, backend):
380 request = _load_cert(
381 os.path.join("x509", "requests", "rsa_md4.pem"),
Paul Kehrer31e39882015-03-11 11:37:04 -0500382 x509.load_pem_x509_csr,
Paul Kehrerdc480ad2015-02-23 12:14:54 -0600383 backend
384 )
385 with pytest.raises(UnsupportedAlgorithm):
386 request.signature_hash_algorithm
387
Paul Kehrerf1ef3512014-11-26 17:36:05 -1000388
389@pytest.mark.requires_backend_interface(interface=DSABackend)
390@pytest.mark.requires_backend_interface(interface=X509Backend)
Paul Kehrere76cd272014-12-14 19:00:51 -0600391class TestDSACertificate(object):
Paul Kehrerf1ef3512014-11-26 17:36:05 -1000392 def test_load_dsa_cert(self, backend):
Paul Kehrera693cfd2014-11-27 07:47:58 -1000393 cert = _load_cert(
Paul Kehrer4903adc2014-12-13 16:57:50 -0600394 os.path.join("x509", "custom", "dsa_selfsigned_ca.pem"),
Paul Kehrer41120322014-12-02 18:31:14 -1000395 x509.load_pem_x509_certificate,
Paul Kehrera693cfd2014-11-27 07:47:58 -1000396 backend
Paul Kehrerf1ef3512014-11-26 17:36:05 -1000397 )
Paul Kehrer8802a5b2015-02-13 12:06:57 -0600398 assert isinstance(cert.signature_hash_algorithm, hashes.SHA1)
Paul Kehrerf1ef3512014-11-26 17:36:05 -1000399 public_key = cert.public_key()
Alex Gaynor32c57df2015-02-23 21:51:27 -0800400 assert isinstance(public_key, dsa.DSAPublicKey)
Alex Gaynorfcadda62015-03-10 10:01:18 -0400401 if isinstance(public_key, dsa.DSAPublicKeyWithSerialization):
Paul Kehrere76cd272014-12-14 19:00:51 -0600402 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 Kehrerf1ef3512014-11-26 17:36:05 -1000439
Paul Kehrer1effb6e2015-03-30 15:05:59 -0500440 @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 Kehrerdc480ad2015-02-23 12:14:54 -0600455 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 Kehrerf1ef3512014-11-26 17:36:05 -1000468
469@pytest.mark.requires_backend_interface(interface=EllipticCurveBackend)
470@pytest.mark.requires_backend_interface(interface=X509Backend)
Paul Kehrere76cd272014-12-14 19:00:51 -0600471class TestECDSACertificate(object):
Paul Kehrerf1ef3512014-11-26 17:36:05 -1000472 def test_load_ecdsa_cert(self, backend):
473 _skip_curve_unsupported(backend, ec.SECP384R1())
Paul Kehrera693cfd2014-11-27 07:47:58 -1000474 cert = _load_cert(
Paul Kehrerf1ef3512014-11-26 17:36:05 -1000475 os.path.join("x509", "ecdsa_root.pem"),
Paul Kehrer41120322014-12-02 18:31:14 -1000476 x509.load_pem_x509_certificate,
Paul Kehrera693cfd2014-11-27 07:47:58 -1000477 backend
Paul Kehrerf1ef3512014-11-26 17:36:05 -1000478 )
Paul Kehrer8802a5b2015-02-13 12:06:57 -0600479 assert isinstance(cert.signature_hash_algorithm, hashes.SHA384)
Paul Kehrerf1ef3512014-11-26 17:36:05 -1000480 public_key = cert.public_key()
Alex Gaynor32c57df2015-02-23 21:51:27 -0800481 assert isinstance(public_key, ec.EllipticCurvePublicKey)
Alex Gaynorfcadda62015-03-10 10:01:18 -0400482 if isinstance(public_key, ec.EllipticCurvePublicKeyWithSerialization):
Paul Kehrere76cd272014-12-14 19:00:51 -0600483 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 Kehrer6c660a82014-12-12 11:50:44 -0600493
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 Kehrer912d3fb2015-01-29 11:19:22 -0600503
Paul Kehrer1effb6e2015-03-30 15:05:59 -0500504 @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 Kehrerdc480ad2015-02-23 12:14:54 -0600518 _skip_curve_unsupported(backend, ec.SECP384R1())
Paul Kehrer1effb6e2015-03-30 15:05:59 -0500519 request = _load_cert(path, loader_func, backend)
Paul Kehrerdc480ad2015-02-23 12:14:54 -0600520 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 Kehrer912d3fb2015-01-29 11:19:22 -0600533
Paul Kehrer806bfb22015-02-02 17:05:24 -0600534class TestNameAttribute(object):
Alex Gaynora56ff412015-02-10 17:26:32 -0500535 def test_init_bad_oid(self):
536 with pytest.raises(TypeError):
537 x509.NameAttribute(None, 'value')
538
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600539 def test_eq(self):
Paul Kehrer806bfb22015-02-02 17:05:24 -0600540 assert x509.NameAttribute(
541 x509.ObjectIdentifier('oid'), 'value'
542 ) == x509.NameAttribute(
543 x509.ObjectIdentifier('oid'), 'value'
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600544 )
545
546 def test_ne(self):
Paul Kehrer806bfb22015-02-02 17:05:24 -0600547 assert x509.NameAttribute(
548 x509.ObjectIdentifier('2.5.4.3'), 'value'
549 ) != x509.NameAttribute(
550 x509.ObjectIdentifier('2.5.4.5'), 'value'
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600551 )
Paul Kehrer806bfb22015-02-02 17:05:24 -0600552 assert x509.NameAttribute(
553 x509.ObjectIdentifier('oid'), 'value'
554 ) != x509.NameAttribute(
555 x509.ObjectIdentifier('oid'), 'value2'
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600556 )
Paul Kehrer806bfb22015-02-02 17:05:24 -0600557 assert x509.NameAttribute(
558 x509.ObjectIdentifier('oid'), 'value'
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600559 ) != object()
560
Paul Kehrera498be82015-02-12 15:00:56 -0600561 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 Kehrer912d3fb2015-01-29 11:19:22 -0600568
569class TestObjectIdentifier(object):
570 def test_eq(self):
Paul Kehrer806bfb22015-02-02 17:05:24 -0600571 oid1 = x509.ObjectIdentifier('oid')
572 oid2 = x509.ObjectIdentifier('oid')
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600573 assert oid1 == oid2
574
575 def test_ne(self):
Paul Kehrer806bfb22015-02-02 17:05:24 -0600576 oid1 = x509.ObjectIdentifier('oid')
577 assert oid1 != x509.ObjectIdentifier('oid1')
Paul Kehrer912d3fb2015-01-29 11:19:22 -0600578 assert oid1 != object()
579
580 def test_repr(self):
Paul Kehrer806bfb22015-02-02 17:05:24 -0600581 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 Kehrer719d5362015-01-01 20:03:52 -0600585
586
587class 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 Kehrer1fb35c92015-04-11 15:42:54 -0400610
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 Kehrere613e5d2015-04-11 15:52:04 -0400618 "<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 Kehrer1fb35c92015-04-11 15:42:54 -0400622 )