blob: 0405b7986c353c366e03d62864bbea7efbcdc920 [file] [log] [blame]
Paul Kehrer8cf26422015-03-21 09:50:24 -05001# 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 Kehrer1eb82a62015-03-31 20:00:33 -05007import binascii
Paul Kehrer31bdf792015-03-25 14:11:00 -05008import ipaddress
Paul Kehrerfbb7ac82015-03-16 19:26:29 -05009import os
10
Paul Kehrer8cf26422015-03-21 09:50:24 -050011import pytest
12
Paul Kehrercbfb1012015-04-10 20:57:20 -040013import six
14
Paul Kehrer8cf26422015-03-21 09:50:24 -050015from cryptography import x509
Paul Kehrerfbb7ac82015-03-16 19:26:29 -050016from cryptography.hazmat.backends.interfaces import RSABackend, X509Backend
17
18from .test_x509 import _load_cert
Paul Kehrer8cf26422015-03-21 09:50:24 -050019
20
Paul Kehrer85894662015-03-22 13:19:31 -050021class TestExtension(object):
22 def test_not_an_oid(self):
Paul Kehrera5c6e9a2015-03-23 19:23:43 -050023 bc = x509.BasicConstraints(ca=False, path_length=None)
Paul Kehrer85894662015-03-22 13:19:31 -050024 with pytest.raises(TypeError):
25 x509.Extension("notanoid", True, bc)
26
27 def test_critical_not_a_bool(self):
Paul Kehrera5c6e9a2015-03-23 19:23:43 -050028 bc = x509.BasicConstraints(ca=False, path_length=None)
Paul Kehrer85894662015-03-22 13:19:31 -050029 with pytest.raises(TypeError):
30 x509.Extension(x509.OID_BASIC_CONSTRAINTS, "notabool", bc)
31
32 def test_repr(self):
Paul Kehrera5c6e9a2015-03-23 19:23:43 -050033 bc = x509.BasicConstraints(ca=False, path_length=None)
Paul Kehrer85894662015-03-22 13:19:31 -050034 ext = x509.Extension(x509.OID_BASIC_CONSTRAINTS, True, bc)
35 assert repr(ext) == (
36 "<Extension(oid=<ObjectIdentifier(oid=2.5.29.19, name=basicConst"
37 "raints)>, critical=True, value=<BasicConstraints(ca=False, path"
38 "_length=None)>)>"
39 )
40
Paul Kehrer58e870c2015-05-17 09:15:30 -070041 def test_eq(self):
42 ext1 = x509.Extension(
43 x509.ObjectIdentifier('1.2.3.4'), False, 'value'
44 )
45 ext2 = x509.Extension(
46 x509.ObjectIdentifier('1.2.3.4'), False, 'value'
47 )
48 assert ext1 == ext2
49
50 def test_ne(self):
51 ext1 = x509.Extension(
52 x509.ObjectIdentifier('1.2.3.4'), False, 'value'
53 )
54 ext2 = x509.Extension(
55 x509.ObjectIdentifier('1.2.3.5'), False, 'value'
56 )
57 ext3 = x509.Extension(
58 x509.ObjectIdentifier('1.2.3.4'), True, 'value'
59 )
60 ext4 = x509.Extension(
61 x509.ObjectIdentifier('1.2.3.4'), False, 'value4'
62 )
63 assert ext1 != ext2
64 assert ext1 != ext3
65 assert ext1 != ext4
66 assert ext1 != object()
67
Paul Kehrer85894662015-03-22 13:19:31 -050068
Paul Kehrer2b622582015-04-15 11:04:29 -040069class TestNoticeReference(object):
70 def test_notice_numbers_not_all_int(self):
71 with pytest.raises(TypeError):
72 x509.NoticeReference("org", [1, 2, "three"])
73
74 def test_notice_numbers_none(self):
Paul Kehrer6e198b02015-05-12 15:53:38 -050075 with pytest.raises(TypeError):
76 x509.NoticeReference("org", None)
Paul Kehrer2b622582015-04-15 11:04:29 -040077
78 def test_repr(self):
Paul Kehrer73be2ca2015-05-11 21:22:38 -050079 nr = x509.NoticeReference(u"org", [1, 3, 4])
Paul Kehrer2b622582015-04-15 11:04:29 -040080
Paul Kehrer73be2ca2015-05-11 21:22:38 -050081 if six.PY3:
82 assert repr(nr) == (
83 "<NoticeReference(organization='org', notice_numbers=[1, 3, 4"
84 "])>"
85 )
86 else:
87 assert repr(nr) == (
88 "<NoticeReference(organization=u'org', notice_numbers=[1, 3, "
89 "4])>"
90 )
Paul Kehrer2b622582015-04-15 11:04:29 -040091
Paul Kehrerc56ab622015-05-03 09:56:31 -050092 def test_eq(self):
93 nr = x509.NoticeReference("org", [1, 2])
94 nr2 = x509.NoticeReference("org", [1, 2])
95 assert nr == nr2
96
97 def test_ne(self):
98 nr = x509.NoticeReference("org", [1, 2])
99 nr2 = x509.NoticeReference("org", [1])
100 nr3 = x509.NoticeReference(None, [1, 2])
101 assert nr != nr2
102 assert nr != nr3
103 assert nr != object()
104
Paul Kehrer2b622582015-04-15 11:04:29 -0400105
106class TestUserNotice(object):
107 def test_notice_reference_invalid(self):
108 with pytest.raises(TypeError):
109 x509.UserNotice("invalid", None)
110
111 def test_notice_reference_none(self):
112 un = x509.UserNotice(None, "text")
113 assert un.notice_reference is None
114 assert un.explicit_text == "text"
115
116 def test_repr(self):
Paul Kehrer6e198b02015-05-12 15:53:38 -0500117 un = x509.UserNotice(x509.NoticeReference(u"org", [1]), u"text")
Paul Kehrer73be2ca2015-05-11 21:22:38 -0500118 if six.PY3:
119 assert repr(un) == (
120 "<UserNotice(notice_reference=<NoticeReference(organization='"
Paul Kehrer6e198b02015-05-12 15:53:38 -0500121 "org', notice_numbers=[1])>, explicit_text='text')>"
Paul Kehrer73be2ca2015-05-11 21:22:38 -0500122 )
123 else:
124 assert repr(un) == (
125 "<UserNotice(notice_reference=<NoticeReference(organization=u"
Paul Kehrer6e198b02015-05-12 15:53:38 -0500126 "'org', notice_numbers=[1])>, explicit_text=u'text')>"
Paul Kehrer73be2ca2015-05-11 21:22:38 -0500127 )
Paul Kehrer2b622582015-04-15 11:04:29 -0400128
Paul Kehrerc56ab622015-05-03 09:56:31 -0500129 def test_eq(self):
130 nr = x509.NoticeReference("org", [1, 2])
131 nr2 = x509.NoticeReference("org", [1, 2])
132 un = x509.UserNotice(nr, "text")
133 un2 = x509.UserNotice(nr2, "text")
134 assert un == un2
135
136 def test_ne(self):
137 nr = x509.NoticeReference("org", [1, 2])
138 nr2 = x509.NoticeReference("org", [1])
139 un = x509.UserNotice(nr, "text")
140 un2 = x509.UserNotice(nr2, "text")
141 un3 = x509.UserNotice(nr, "text3")
142 assert un != un2
143 assert un != un3
144 assert un != object()
145
Paul Kehrer2b622582015-04-15 11:04:29 -0400146
Paul Kehrer2b622582015-04-15 11:04:29 -0400147class TestPolicyInformation(object):
148 def test_invalid_policy_identifier(self):
149 with pytest.raises(TypeError):
150 x509.PolicyInformation("notanoid", None)
151
152 def test_none_policy_qualifiers(self):
153 pi = x509.PolicyInformation(x509.ObjectIdentifier("1.2.3"), None)
154 assert pi.policy_identifier == x509.ObjectIdentifier("1.2.3")
155 assert pi.policy_qualifiers is None
156
157 def test_policy_qualifiers(self):
Paul Kehrerba35b3b2015-05-10 13:07:59 -0500158 pq = [u"string"]
Paul Kehrer2b622582015-04-15 11:04:29 -0400159 pi = x509.PolicyInformation(x509.ObjectIdentifier("1.2.3"), pq)
160 assert pi.policy_identifier == x509.ObjectIdentifier("1.2.3")
161 assert pi.policy_qualifiers == pq
162
163 def test_invalid_policy_identifiers(self):
164 with pytest.raises(TypeError):
165 x509.PolicyInformation(x509.ObjectIdentifier("1.2.3"), [1, 2])
166
167 def test_repr(self):
Paul Kehrer73be2ca2015-05-11 21:22:38 -0500168 pq = [u"string", x509.UserNotice(None, u"hi")]
Paul Kehrer2b622582015-04-15 11:04:29 -0400169 pi = x509.PolicyInformation(x509.ObjectIdentifier("1.2.3"), pq)
Paul Kehrerba35b3b2015-05-10 13:07:59 -0500170 if six.PY3:
171 assert repr(pi) == (
172 "<PolicyInformation(policy_identifier=<ObjectIdentifier(oid=1."
173 "2.3, name=Unknown OID)>, policy_qualifiers=['string', <UserNo"
Paul Kehrer9aaef9e2015-05-11 10:49:20 -0500174 "tice(notice_reference=None, explicit_text='hi')>])>"
Paul Kehrerba35b3b2015-05-10 13:07:59 -0500175 )
176 else:
177 assert repr(pi) == (
178 "<PolicyInformation(policy_identifier=<ObjectIdentifier(oid=1."
179 "2.3, name=Unknown OID)>, policy_qualifiers=[u'string', <UserN"
Paul Kehrer73be2ca2015-05-11 21:22:38 -0500180 "otice(notice_reference=None, explicit_text=u'hi')>])>"
Paul Kehrerba35b3b2015-05-10 13:07:59 -0500181 )
Paul Kehrer2b622582015-04-15 11:04:29 -0400182
Paul Kehrerc56ab622015-05-03 09:56:31 -0500183 def test_eq(self):
184 pi = x509.PolicyInformation(
185 x509.ObjectIdentifier("1.2.3"),
186 [u"string", x509.UserNotice(None, u"hi")]
187 )
188 pi2 = x509.PolicyInformation(
189 x509.ObjectIdentifier("1.2.3"),
190 [u"string", x509.UserNotice(None, u"hi")]
191 )
192 assert pi == pi2
193
194 def test_ne(self):
195 pi = x509.PolicyInformation(
196 x509.ObjectIdentifier("1.2.3"), [u"string"]
197 )
198 pi2 = x509.PolicyInformation(
199 x509.ObjectIdentifier("1.2.3"), [u"string2"]
200 )
201 pi3 = x509.PolicyInformation(
202 x509.ObjectIdentifier("1.2.3.4"), [u"string"]
203 )
204 assert pi != pi2
205 assert pi != pi3
206 assert pi != object()
207
Paul Kehrer2b622582015-04-15 11:04:29 -0400208
209class TestCertificatePolicies(object):
210 def test_invalid_policies(self):
Paul Kehrerba35b3b2015-05-10 13:07:59 -0500211 pq = [u"string"]
Paul Kehrer2b622582015-04-15 11:04:29 -0400212 pi = x509.PolicyInformation(x509.ObjectIdentifier("1.2.3"), pq)
213 with pytest.raises(TypeError):
214 x509.CertificatePolicies([1, pi])
215
216 def test_iter_len(self):
Paul Kehrerba35b3b2015-05-10 13:07:59 -0500217 pq = [u"string"]
Paul Kehrer2b622582015-04-15 11:04:29 -0400218 pi = x509.PolicyInformation(x509.ObjectIdentifier("1.2.3"), pq)
219 cp = x509.CertificatePolicies([pi])
220 assert len(cp) == 1
221 for policyinfo in cp:
222 assert policyinfo == pi
223
224 def test_repr(self):
Paul Kehrerba35b3b2015-05-10 13:07:59 -0500225 pq = [u"string"]
Paul Kehrer2b622582015-04-15 11:04:29 -0400226 pi = x509.PolicyInformation(x509.ObjectIdentifier("1.2.3"), pq)
227 cp = x509.CertificatePolicies([pi])
Paul Kehrerba35b3b2015-05-10 13:07:59 -0500228 if six.PY3:
229 assert repr(cp) == (
230 "<CertificatePolicies([<PolicyInformation(policy_identifier=<O"
231 "bjectIdentifier(oid=1.2.3, name=Unknown OID)>, policy_qualifi"
232 "ers=['string'])>])>"
233 )
234 else:
235 assert repr(cp) == (
236 "<CertificatePolicies([<PolicyInformation(policy_identifier=<O"
237 "bjectIdentifier(oid=1.2.3, name=Unknown OID)>, policy_qualifi"
238 "ers=[u'string'])>])>"
239 )
Paul Kehrer2b622582015-04-15 11:04:29 -0400240
Paul Kehrerc56ab622015-05-03 09:56:31 -0500241 def test_eq(self):
242 pi = x509.PolicyInformation(
243 x509.ObjectIdentifier("1.2.3"), [u"string"]
244 )
245 cp = x509.CertificatePolicies([pi])
246 pi2 = x509.PolicyInformation(
247 x509.ObjectIdentifier("1.2.3"), [u"string"]
248 )
249 cp2 = x509.CertificatePolicies([pi2])
250 assert cp == cp2
251
252 def test_ne(self):
253 pi = x509.PolicyInformation(
254 x509.ObjectIdentifier("1.2.3"), [u"string"]
255 )
256 cp = x509.CertificatePolicies([pi])
257 pi2 = x509.PolicyInformation(
258 x509.ObjectIdentifier("1.2.3"), [u"string2"]
259 )
260 cp2 = x509.CertificatePolicies([pi2])
261 assert cp != cp2
262 assert cp != object()
263
Paul Kehrer2b622582015-04-15 11:04:29 -0400264
Paul Kehrer11026fe2015-05-12 11:23:56 -0500265@pytest.mark.requires_backend_interface(interface=RSABackend)
266@pytest.mark.requires_backend_interface(interface=X509Backend)
267class TestCertificatePoliciesExtension(object):
268 def test_cps_uri_policy_qualifier(self, backend):
269 cert = _load_cert(
270 os.path.join("x509", "custom", "cp_cps_uri.pem"),
271 x509.load_pem_x509_certificate,
272 backend
273 )
274
275 cp = cert.extensions.get_extension_for_oid(
276 x509.OID_CERTIFICATE_POLICIES
277 ).value
278
279 assert cp == x509.CertificatePolicies([
280 x509.PolicyInformation(
281 x509.ObjectIdentifier("2.16.840.1.12345.1.2.3.4.1"),
282 [u"http://other.com/cps"]
283 )
284 ])
285
286 def test_user_notice_with_notice_reference(self, backend):
287 cert = _load_cert(
288 os.path.join(
289 "x509", "custom", "cp_user_notice_with_notice_reference.pem"
290 ),
291 x509.load_pem_x509_certificate,
292 backend
293 )
294
295 cp = cert.extensions.get_extension_for_oid(
296 x509.OID_CERTIFICATE_POLICIES
297 ).value
298
299 assert cp == x509.CertificatePolicies([
300 x509.PolicyInformation(
301 x509.ObjectIdentifier("2.16.840.1.12345.1.2.3.4.1"),
302 [
303 u"http://example.com/cps",
304 u"http://other.com/cps",
305 x509.UserNotice(
306 x509.NoticeReference(u"my org", [1, 2, 3, 4]),
307 u"thing"
308 )
309 ]
310 )
311 ])
312
313 def test_user_notice_with_explicit_text(self, backend):
314 cert = _load_cert(
315 os.path.join(
316 "x509", "custom", "cp_user_notice_with_explicit_text.pem"
317 ),
318 x509.load_pem_x509_certificate,
319 backend
320 )
321
322 cp = cert.extensions.get_extension_for_oid(
323 x509.OID_CERTIFICATE_POLICIES
324 ).value
325
326 assert cp == x509.CertificatePolicies([
327 x509.PolicyInformation(
328 x509.ObjectIdentifier("2.16.840.1.12345.1.2.3.4.1"),
329 [x509.UserNotice(None, u"thing")]
330 )
331 ])
332
333 def test_user_notice_no_explicit_text(self, backend):
334 cert = _load_cert(
335 os.path.join(
336 "x509", "custom", "cp_user_notice_no_explicit_text.pem"
337 ),
338 x509.load_pem_x509_certificate,
339 backend
340 )
341
342 cp = cert.extensions.get_extension_for_oid(
343 x509.OID_CERTIFICATE_POLICIES
344 ).value
345
346 assert cp == x509.CertificatePolicies([
347 x509.PolicyInformation(
348 x509.ObjectIdentifier("2.16.840.1.12345.1.2.3.4.1"),
349 [
350 x509.UserNotice(
351 x509.NoticeReference(u"my org", [1, 2, 3, 4]),
352 None
353 )
354 ]
355 )
356 ])
357
358
Paul Kehrercecbbba2015-03-30 14:58:38 -0500359class TestKeyUsage(object):
360 def test_key_agreement_false_encipher_decipher_true(self):
361 with pytest.raises(ValueError):
362 x509.KeyUsage(
363 digital_signature=False,
364 content_commitment=False,
365 key_encipherment=False,
366 data_encipherment=False,
367 key_agreement=False,
368 key_cert_sign=False,
369 crl_sign=False,
370 encipher_only=True,
371 decipher_only=False
372 )
373
374 with pytest.raises(ValueError):
375 x509.KeyUsage(
376 digital_signature=False,
377 content_commitment=False,
378 key_encipherment=False,
379 data_encipherment=False,
380 key_agreement=False,
381 key_cert_sign=False,
382 crl_sign=False,
383 encipher_only=True,
384 decipher_only=True
385 )
386
387 with pytest.raises(ValueError):
388 x509.KeyUsage(
389 digital_signature=False,
390 content_commitment=False,
391 key_encipherment=False,
392 data_encipherment=False,
393 key_agreement=False,
394 key_cert_sign=False,
395 crl_sign=False,
396 encipher_only=False,
397 decipher_only=True
398 )
399
400 def test_properties_key_agreement_true(self):
401 ku = x509.KeyUsage(
402 digital_signature=True,
403 content_commitment=True,
404 key_encipherment=False,
405 data_encipherment=False,
406 key_agreement=False,
407 key_cert_sign=True,
408 crl_sign=False,
409 encipher_only=False,
410 decipher_only=False
411 )
412 assert ku.digital_signature is True
413 assert ku.content_commitment is True
414 assert ku.key_encipherment is False
415 assert ku.data_encipherment is False
416 assert ku.key_agreement is False
417 assert ku.key_cert_sign is True
418 assert ku.crl_sign is False
419
420 def test_key_agreement_true_properties(self):
421 ku = x509.KeyUsage(
422 digital_signature=False,
423 content_commitment=False,
424 key_encipherment=False,
425 data_encipherment=False,
426 key_agreement=True,
427 key_cert_sign=False,
428 crl_sign=False,
429 encipher_only=False,
430 decipher_only=True
431 )
432 assert ku.key_agreement is True
433 assert ku.encipher_only is False
434 assert ku.decipher_only is True
435
436 def test_key_agreement_false_properties(self):
437 ku = x509.KeyUsage(
438 digital_signature=False,
439 content_commitment=False,
440 key_encipherment=False,
441 data_encipherment=False,
442 key_agreement=False,
443 key_cert_sign=False,
444 crl_sign=False,
445 encipher_only=False,
446 decipher_only=False
447 )
448 assert ku.key_agreement is False
449 with pytest.raises(ValueError):
450 ku.encipher_only
451
452 with pytest.raises(ValueError):
453 ku.decipher_only
454
Paul Kehrerac3e5bb2015-04-02 23:07:10 -0500455 def test_repr_key_agreement_false(self):
456 ku = x509.KeyUsage(
457 digital_signature=True,
458 content_commitment=True,
459 key_encipherment=False,
460 data_encipherment=False,
461 key_agreement=False,
462 key_cert_sign=True,
463 crl_sign=False,
464 encipher_only=False,
465 decipher_only=False
466 )
467 assert repr(ku) == (
468 "<KeyUsage(digital_signature=True, content_commitment=True, key_en"
469 "cipherment=False, data_encipherment=False, key_agreement=False, k"
Paul Kehrerb372e672015-04-15 11:05:24 -0400470 "ey_cert_sign=True, crl_sign=False, encipher_only=None, decipher_o"
471 "nly=None)>"
Paul Kehrerac3e5bb2015-04-02 23:07:10 -0500472 )
473
474 def test_repr_key_agreement_true(self):
475 ku = x509.KeyUsage(
476 digital_signature=True,
477 content_commitment=True,
478 key_encipherment=False,
479 data_encipherment=False,
480 key_agreement=True,
481 key_cert_sign=True,
482 crl_sign=False,
483 encipher_only=False,
484 decipher_only=False
485 )
486 assert repr(ku) == (
487 "<KeyUsage(digital_signature=True, content_commitment=True, key_en"
488 "cipherment=False, data_encipherment=False, key_agreement=True, k"
489 "ey_cert_sign=True, crl_sign=False, encipher_only=False, decipher_"
490 "only=False)>"
491 )
492
Paul Kehrer8565f5e2015-05-13 09:57:09 -0500493 def test_eq(self):
494 ku = x509.KeyUsage(
495 digital_signature=False,
496 content_commitment=False,
497 key_encipherment=False,
498 data_encipherment=False,
499 key_agreement=True,
500 key_cert_sign=False,
501 crl_sign=False,
502 encipher_only=False,
503 decipher_only=True
504 )
505 ku2 = x509.KeyUsage(
506 digital_signature=False,
507 content_commitment=False,
508 key_encipherment=False,
509 data_encipherment=False,
510 key_agreement=True,
511 key_cert_sign=False,
512 crl_sign=False,
513 encipher_only=False,
514 decipher_only=True
515 )
516 assert ku == ku2
517
518 def test_ne(self):
519 ku = x509.KeyUsage(
520 digital_signature=False,
521 content_commitment=False,
522 key_encipherment=False,
523 data_encipherment=False,
524 key_agreement=True,
525 key_cert_sign=False,
526 crl_sign=False,
527 encipher_only=False,
528 decipher_only=True
529 )
530 ku2 = x509.KeyUsage(
531 digital_signature=False,
532 content_commitment=False,
533 key_encipherment=False,
534 data_encipherment=False,
535 key_agreement=False,
536 key_cert_sign=False,
537 crl_sign=False,
538 encipher_only=False,
539 decipher_only=False
540 )
541 assert ku != ku2
542 assert ku != object()
543
Paul Kehrercecbbba2015-03-30 14:58:38 -0500544
Paul Kehrer1eb82a62015-03-31 20:00:33 -0500545class TestSubjectKeyIdentifier(object):
546 def test_properties(self):
Paul Kehrercbfb1012015-04-10 20:57:20 -0400547 value = binascii.unhexlify(b"092384932230498bc980aa8098456f6ff7ff3ac9")
Paul Kehrer1eb82a62015-03-31 20:00:33 -0500548 ski = x509.SubjectKeyIdentifier(value)
549 assert ski.digest == value
Paul Kehrer1eb82a62015-03-31 20:00:33 -0500550
551 def test_repr(self):
552 ski = x509.SubjectKeyIdentifier(
Paul Kehreree997262015-04-04 12:20:28 -0500553 binascii.unhexlify(b"092384932230498bc980aa8098456f6ff7ff3ac9")
Paul Kehrer1eb82a62015-03-31 20:00:33 -0500554 )
555 ext = x509.Extension(x509.OID_SUBJECT_KEY_IDENTIFIER, False, ski)
Paul Kehrercbfb1012015-04-10 20:57:20 -0400556 if six.PY3:
557 assert repr(ext) == (
558 "<Extension(oid=<ObjectIdentifier(oid=2.5.29.14, name=subjectK"
559 "eyIdentifier)>, critical=False, value=<SubjectKeyIdentifier(d"
560 "igest=b\'\\t#\\x84\\x93\"0I\\x8b\\xc9\\x80\\xaa\\x80\\x98Eoo"
561 "\\xf7\\xff:\\xc9\')>)>"
562 )
563 else:
564 assert repr(ext) == (
565 "<Extension(oid=<ObjectIdentifier(oid=2.5.29.14, name=subjectK"
566 "eyIdentifier)>, critical=False, value=<SubjectKeyIdentifier(d"
567 "igest=\'\\t#\\x84\\x93\"0I\\x8b\\xc9\\x80\\xaa\\x80\\x98Eoo"
568 "\\xf7\\xff:\\xc9\')>)>"
569 )
Paul Kehrer1eb82a62015-03-31 20:00:33 -0500570
571 def test_eq(self):
572 ski = x509.SubjectKeyIdentifier(
Paul Kehreree997262015-04-04 12:20:28 -0500573 binascii.unhexlify(b"092384932230498bc980aa8098456f6ff7ff3ac9")
Paul Kehrer1eb82a62015-03-31 20:00:33 -0500574 )
575 ski2 = x509.SubjectKeyIdentifier(
Paul Kehreree997262015-04-04 12:20:28 -0500576 binascii.unhexlify(b"092384932230498bc980aa8098456f6ff7ff3ac9")
Paul Kehrer1eb82a62015-03-31 20:00:33 -0500577 )
578 assert ski == ski2
579
580 def test_ne(self):
581 ski = x509.SubjectKeyIdentifier(
Paul Kehreree997262015-04-04 12:20:28 -0500582 binascii.unhexlify(b"092384932230498bc980aa8098456f6ff7ff3ac9")
Paul Kehrer1eb82a62015-03-31 20:00:33 -0500583 )
584 ski2 = x509.SubjectKeyIdentifier(
Paul Kehreree997262015-04-04 12:20:28 -0500585 binascii.unhexlify(b"aa8098456f6ff7ff3ac9092384932230498bc980")
Paul Kehrer1eb82a62015-03-31 20:00:33 -0500586 )
587 assert ski != ski2
588 assert ski != object()
589
590
Paul Kehrer2eb4ed92015-04-11 15:33:45 -0400591class TestAuthorityKeyIdentifier(object):
Paul Kehrerc6e0f062015-05-03 11:04:34 -0500592 def test_authority_cert_issuer_not_generalname(self):
Paul Kehrer2eb4ed92015-04-11 15:33:45 -0400593 with pytest.raises(TypeError):
Paul Kehrerc6e0f062015-05-03 11:04:34 -0500594 x509.AuthorityKeyIdentifier(b"identifier", ["notname"], 3)
Paul Kehrer2eb4ed92015-04-11 15:33:45 -0400595
596 def test_authority_cert_serial_number_not_integer(self):
Paul Kehrerc6e0f062015-05-03 11:04:34 -0500597 dirname = x509.DirectoryName(
598 x509.Name([
599 x509.NameAttribute(x509.ObjectIdentifier('oid'), 'value1'),
600 x509.NameAttribute(x509.ObjectIdentifier('oid2'), 'value2'),
601 ])
602 )
Paul Kehrer2eb4ed92015-04-11 15:33:45 -0400603 with pytest.raises(TypeError):
Paul Kehrerc6e0f062015-05-03 11:04:34 -0500604 x509.AuthorityKeyIdentifier(b"identifier", [dirname], "notanint")
Paul Kehrer2eb4ed92015-04-11 15:33:45 -0400605
606 def test_authority_issuer_none_serial_not_none(self):
607 with pytest.raises(ValueError):
608 x509.AuthorityKeyIdentifier(b"identifier", None, 3)
609
610 def test_authority_issuer_not_none_serial_none(self):
Paul Kehrerc6e0f062015-05-03 11:04:34 -0500611 dirname = x509.DirectoryName(
612 x509.Name([
613 x509.NameAttribute(x509.ObjectIdentifier('oid'), 'value1'),
614 x509.NameAttribute(x509.ObjectIdentifier('oid2'), 'value2'),
615 ])
616 )
Paul Kehrer2eb4ed92015-04-11 15:33:45 -0400617 with pytest.raises(ValueError):
Paul Kehrerc6e0f062015-05-03 11:04:34 -0500618 x509.AuthorityKeyIdentifier(b"identifier", [dirname], None)
Paul Kehrer2eb4ed92015-04-11 15:33:45 -0400619
620 def test_authority_cert_serial_and_issuer_none(self):
621 aki = x509.AuthorityKeyIdentifier(b"id", None, None)
622 assert aki.key_identifier == b"id"
623 assert aki.authority_cert_issuer is None
624 assert aki.authority_cert_serial_number is None
625
626 def test_repr(self):
Paul Kehrerc6e0f062015-05-03 11:04:34 -0500627 dirname = x509.DirectoryName(
628 x509.Name([x509.NameAttribute(x509.OID_COMMON_NAME, 'myCN')])
629 )
630 aki = x509.AuthorityKeyIdentifier(b"digest", [dirname], 1234)
Paul Kehrer2eb4ed92015-04-11 15:33:45 -0400631
632 if six.PY3:
633 assert repr(aki) == (
634 "<AuthorityKeyIdentifier(key_identifier=b'digest', authority_"
Paul Kehrerc6e0f062015-05-03 11:04:34 -0500635 "cert_issuer=[<DirectoryName(value=<Name([<NameAttribute(oid="
636 "<ObjectIdentifier(oid=2.5.4.3, name=commonName)>, value='myC"
637 "N')>])>)>], authority_cert_serial_number=1234)>"
Paul Kehrer2eb4ed92015-04-11 15:33:45 -0400638 )
639 else:
640 assert repr(aki) == (
641 "<AuthorityKeyIdentifier(key_identifier='digest', authority_ce"
Paul Kehrerc6e0f062015-05-03 11:04:34 -0500642 "rt_issuer=[<DirectoryName(value=<Name([<NameAttribute(oid=<Ob"
643 "jectIdentifier(oid=2.5.4.3, name=commonName)>, value='myCN')>"
644 "])>)>], authority_cert_serial_number=1234)>"
Paul Kehrer2eb4ed92015-04-11 15:33:45 -0400645 )
646
Paul Kehrer2ca2eb32015-05-03 10:04:57 -0500647 def test_eq(self):
648 dirname = x509.DirectoryName(
649 x509.Name([x509.NameAttribute(x509.OID_COMMON_NAME, 'myCN')])
650 )
651 aki = x509.AuthorityKeyIdentifier(b"digest", [dirname], 1234)
652 dirname2 = x509.DirectoryName(
653 x509.Name([x509.NameAttribute(x509.OID_COMMON_NAME, 'myCN')])
654 )
655 aki2 = x509.AuthorityKeyIdentifier(b"digest", [dirname2], 1234)
656 assert aki == aki2
657
658 def test_ne(self):
659 dirname = x509.DirectoryName(
660 x509.Name([x509.NameAttribute(x509.OID_COMMON_NAME, 'myCN')])
661 )
662 dirname5 = x509.DirectoryName(
663 x509.Name([x509.NameAttribute(x509.OID_COMMON_NAME, 'aCN')])
664 )
665 aki = x509.AuthorityKeyIdentifier(b"digest", [dirname], 1234)
666 aki2 = x509.AuthorityKeyIdentifier(b"diges", [dirname], 1234)
667 aki3 = x509.AuthorityKeyIdentifier(b"digest", None, None)
668 aki4 = x509.AuthorityKeyIdentifier(b"digest", [dirname], 12345)
669 aki5 = x509.AuthorityKeyIdentifier(b"digest", [dirname5], 12345)
670 assert aki != aki2
671 assert aki != aki3
672 assert aki != aki4
673 assert aki != aki5
674 assert aki != object()
675
Paul Kehrer2eb4ed92015-04-11 15:33:45 -0400676
Paul Kehrer8cf26422015-03-21 09:50:24 -0500677class TestBasicConstraints(object):
678 def test_ca_not_boolean(self):
679 with pytest.raises(TypeError):
Paul Kehrera5c6e9a2015-03-23 19:23:43 -0500680 x509.BasicConstraints(ca="notbool", path_length=None)
Paul Kehrer8cf26422015-03-21 09:50:24 -0500681
682 def test_path_length_not_ca(self):
683 with pytest.raises(ValueError):
Paul Kehrera5c6e9a2015-03-23 19:23:43 -0500684 x509.BasicConstraints(ca=False, path_length=0)
Paul Kehrer8cf26422015-03-21 09:50:24 -0500685
686 def test_path_length_not_int(self):
687 with pytest.raises(TypeError):
Paul Kehrera5c6e9a2015-03-23 19:23:43 -0500688 x509.BasicConstraints(ca=True, path_length=1.1)
Paul Kehrer8cf26422015-03-21 09:50:24 -0500689
690 with pytest.raises(TypeError):
Paul Kehrera5c6e9a2015-03-23 19:23:43 -0500691 x509.BasicConstraints(ca=True, path_length="notint")
Paul Kehrer8cf26422015-03-21 09:50:24 -0500692
693 def test_path_length_negative(self):
694 with pytest.raises(TypeError):
Paul Kehrera5c6e9a2015-03-23 19:23:43 -0500695 x509.BasicConstraints(ca=True, path_length=-1)
Paul Kehrer8cf26422015-03-21 09:50:24 -0500696
697 def test_repr(self):
Paul Kehrera5c6e9a2015-03-23 19:23:43 -0500698 na = x509.BasicConstraints(ca=True, path_length=None)
Paul Kehrer8cf26422015-03-21 09:50:24 -0500699 assert repr(na) == (
Paul Kehrer85894662015-03-22 13:19:31 -0500700 "<BasicConstraints(ca=True, path_length=None)>"
Paul Kehrer8cf26422015-03-21 09:50:24 -0500701 )
Paul Kehrerfbb7ac82015-03-16 19:26:29 -0500702
Paul Kehrer3a69b132015-05-13 10:03:46 -0500703 def test_eq(self):
704 na = x509.BasicConstraints(ca=True, path_length=None)
705 na2 = x509.BasicConstraints(ca=True, path_length=None)
706 assert na == na2
707
708 def test_ne(self):
709 na = x509.BasicConstraints(ca=True, path_length=None)
710 na2 = x509.BasicConstraints(ca=True, path_length=1)
711 na3 = x509.BasicConstraints(ca=False, path_length=None)
712 assert na != na2
713 assert na != na3
714 assert na != object()
715
Paul Kehrerfbb7ac82015-03-16 19:26:29 -0500716
Paul Kehrerffa2a152015-03-31 08:18:25 -0500717class TestExtendedKeyUsage(object):
718 def test_not_all_oids(self):
719 with pytest.raises(TypeError):
720 x509.ExtendedKeyUsage(["notoid"])
721
722 def test_iter_len(self):
723 eku = x509.ExtendedKeyUsage([
724 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.1"),
725 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.2"),
726 ])
727 assert len(eku) == 2
728 assert list(eku) == [
729 x509.OID_SERVER_AUTH,
730 x509.OID_CLIENT_AUTH
731 ]
732
Paul Kehrer23d10c32015-04-02 23:12:32 -0500733 def test_repr(self):
734 eku = x509.ExtendedKeyUsage([
735 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.1"),
736 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.2"),
737 ])
738 assert repr(eku) == (
739 "<ExtendedKeyUsage([<ObjectIdentifier(oid=1.3.6.1.5.5.7.3.1, name="
740 "serverAuth)>, <ObjectIdentifier(oid=1.3.6.1.5.5.7.3.2, name=clien"
741 "tAuth)>])>"
742 )
743
Paul Kehrerb0476172015-05-02 19:34:51 -0500744 def test_eq(self):
745 eku = x509.ExtendedKeyUsage([
746 x509.ObjectIdentifier("1.3.6"), x509.ObjectIdentifier("1.3.7")
747 ])
748 eku2 = x509.ExtendedKeyUsage([
749 x509.ObjectIdentifier("1.3.6"), x509.ObjectIdentifier("1.3.7")
750 ])
751 assert eku == eku2
752
753 def test_ne(self):
754 eku = x509.ExtendedKeyUsage([x509.ObjectIdentifier("1.3.6")])
755 eku2 = x509.ExtendedKeyUsage([x509.ObjectIdentifier("1.3.6.1")])
756 assert eku != eku2
757 assert eku != object()
758
Paul Kehrerffa2a152015-03-31 08:18:25 -0500759
Paul Kehrerfbb7ac82015-03-16 19:26:29 -0500760@pytest.mark.requires_backend_interface(interface=RSABackend)
761@pytest.mark.requires_backend_interface(interface=X509Backend)
762class TestExtensions(object):
763 def test_no_extensions(self, backend):
764 cert = _load_cert(
765 os.path.join("x509", "verisign_md2_root.pem"),
766 x509.load_pem_x509_certificate,
767 backend
768 )
769 ext = cert.extensions
770 assert len(ext) == 0
771 assert list(ext) == []
Paul Kehrerfa56a232015-03-17 13:14:03 -0500772 with pytest.raises(x509.ExtensionNotFound) as exc:
773 ext.get_extension_for_oid(x509.OID_BASIC_CONSTRAINTS)
774
775 assert exc.value.oid == x509.OID_BASIC_CONSTRAINTS
776
777 def test_one_extension(self, backend):
778 cert = _load_cert(
779 os.path.join(
780 "x509", "custom", "basic_constraints_not_critical.pem"
781 ),
782 x509.load_pem_x509_certificate,
783 backend
784 )
785 extensions = cert.extensions
786 ext = extensions.get_extension_for_oid(x509.OID_BASIC_CONSTRAINTS)
787 assert ext is not None
788 assert ext.value.ca is False
Paul Kehrerfbb7ac82015-03-16 19:26:29 -0500789
790 def test_duplicate_extension(self, backend):
791 cert = _load_cert(
792 os.path.join(
793 "x509", "custom", "two_basic_constraints.pem"
794 ),
795 x509.load_pem_x509_certificate,
796 backend
797 )
798 with pytest.raises(x509.DuplicateExtension) as exc:
799 cert.extensions
800
801 assert exc.value.oid == x509.OID_BASIC_CONSTRAINTS
802
803 def test_unsupported_critical_extension(self, backend):
804 cert = _load_cert(
805 os.path.join(
806 "x509", "custom", "unsupported_extension_critical.pem"
807 ),
808 x509.load_pem_x509_certificate,
809 backend
810 )
811 with pytest.raises(x509.UnsupportedExtension) as exc:
812 cert.extensions
813
814 assert exc.value.oid == x509.ObjectIdentifier("1.2.3.4")
815
816 def test_unsupported_extension(self, backend):
817 # TODO: this will raise an exception when all extensions are complete
818 cert = _load_cert(
819 os.path.join(
820 "x509", "custom", "unsupported_extension.pem"
821 ),
822 x509.load_pem_x509_certificate,
823 backend
824 )
825 extensions = cert.extensions
826 assert len(extensions) == 0
Paul Kehrerfa56a232015-03-17 13:14:03 -0500827
828
829@pytest.mark.requires_backend_interface(interface=RSABackend)
830@pytest.mark.requires_backend_interface(interface=X509Backend)
Paul Kehrerde813ea2015-03-28 12:44:34 -0500831class TestBasicConstraintsExtension(object):
Paul Kehrerfa56a232015-03-17 13:14:03 -0500832 def test_ca_true_pathlen_6(self, backend):
833 cert = _load_cert(
834 os.path.join(
835 "x509", "PKITS_data", "certs", "pathLenConstraint6CACert.crt"
836 ),
837 x509.load_der_x509_certificate,
838 backend
839 )
840 ext = cert.extensions.get_extension_for_oid(
841 x509.OID_BASIC_CONSTRAINTS
842 )
843 assert ext is not None
844 assert ext.critical is True
845 assert ext.value.ca is True
846 assert ext.value.path_length == 6
847
848 def test_path_length_zero(self, backend):
849 cert = _load_cert(
850 os.path.join("x509", "custom", "bc_path_length_zero.pem"),
851 x509.load_pem_x509_certificate,
852 backend
853 )
854 ext = cert.extensions.get_extension_for_oid(
855 x509.OID_BASIC_CONSTRAINTS
856 )
857 assert ext is not None
858 assert ext.critical is True
859 assert ext.value.ca is True
860 assert ext.value.path_length == 0
861
862 def test_ca_true_no_pathlen(self, backend):
863 cert = _load_cert(
864 os.path.join("x509", "PKITS_data", "certs", "GoodCACert.crt"),
865 x509.load_der_x509_certificate,
866 backend
867 )
868 ext = cert.extensions.get_extension_for_oid(
869 x509.OID_BASIC_CONSTRAINTS
870 )
871 assert ext is not None
872 assert ext.critical is True
873 assert ext.value.ca is True
874 assert ext.value.path_length is None
875
876 def test_ca_false(self, backend):
877 cert = _load_cert(
878 os.path.join("x509", "cryptography.io.pem"),
879 x509.load_pem_x509_certificate,
880 backend
881 )
882 ext = cert.extensions.get_extension_for_oid(
883 x509.OID_BASIC_CONSTRAINTS
884 )
885 assert ext is not None
886 assert ext.critical is True
887 assert ext.value.ca is False
888 assert ext.value.path_length is None
889
890 def test_no_basic_constraints(self, backend):
891 cert = _load_cert(
892 os.path.join(
893 "x509",
894 "PKITS_data",
895 "certs",
896 "ValidCertificatePathTest1EE.crt"
897 ),
898 x509.load_der_x509_certificate,
899 backend
900 )
901 with pytest.raises(x509.ExtensionNotFound):
902 cert.extensions.get_extension_for_oid(x509.OID_BASIC_CONSTRAINTS)
903
904 def test_basic_constraint_not_critical(self, backend):
905 cert = _load_cert(
906 os.path.join(
907 "x509", "custom", "basic_constraints_not_critical.pem"
908 ),
909 x509.load_pem_x509_certificate,
910 backend
911 )
912 ext = cert.extensions.get_extension_for_oid(
913 x509.OID_BASIC_CONSTRAINTS
914 )
915 assert ext is not None
916 assert ext.critical is False
917 assert ext.value.ca is False
Paul Kehrer1eb82a62015-03-31 20:00:33 -0500918
919
920@pytest.mark.requires_backend_interface(interface=RSABackend)
921@pytest.mark.requires_backend_interface(interface=X509Backend)
922class TestSubjectKeyIdentifierExtension(object):
923 def test_subject_key_identifier(self, backend):
924 cert = _load_cert(
925 os.path.join("x509", "PKITS_data", "certs", "GoodCACert.crt"),
926 x509.load_der_x509_certificate,
927 backend
928 )
929 ext = cert.extensions.get_extension_for_oid(
930 x509.OID_SUBJECT_KEY_IDENTIFIER
931 )
932 ski = ext.value
933 assert ext is not None
934 assert ext.critical is False
Paul Kehrer1eb82a62015-03-31 20:00:33 -0500935 assert ski.digest == binascii.unhexlify(
Paul Kehreree997262015-04-04 12:20:28 -0500936 b"580184241bbc2b52944a3da510721451f5af3ac9"
Paul Kehrer1eb82a62015-03-31 20:00:33 -0500937 )
938
939 def test_no_subject_key_identifier(self, backend):
940 cert = _load_cert(
941 os.path.join("x509", "custom", "bc_path_length_zero.pem"),
942 x509.load_pem_x509_certificate,
943 backend
944 )
945 with pytest.raises(x509.ExtensionNotFound):
946 cert.extensions.get_extension_for_oid(
947 x509.OID_SUBJECT_KEY_IDENTIFIER
948 )
Paul Kehrer5508ee22015-04-02 19:31:03 -0500949
950
951@pytest.mark.requires_backend_interface(interface=RSABackend)
952@pytest.mark.requires_backend_interface(interface=X509Backend)
953class TestKeyUsageExtension(object):
954 def test_no_key_usage(self, backend):
955 cert = _load_cert(
956 os.path.join("x509", "verisign_md2_root.pem"),
957 x509.load_pem_x509_certificate,
958 backend
959 )
960 ext = cert.extensions
961 with pytest.raises(x509.ExtensionNotFound) as exc:
962 ext.get_extension_for_oid(x509.OID_KEY_USAGE)
963
964 assert exc.value.oid == x509.OID_KEY_USAGE
965
966 def test_all_purposes(self, backend):
967 cert = _load_cert(
968 os.path.join(
969 "x509", "custom", "all_key_usages.pem"
970 ),
971 x509.load_pem_x509_certificate,
972 backend
973 )
974 extensions = cert.extensions
975 ext = extensions.get_extension_for_oid(x509.OID_KEY_USAGE)
976 assert ext is not None
977
978 ku = ext.value
979 assert ku.digital_signature is True
980 assert ku.content_commitment is True
981 assert ku.key_encipherment is True
982 assert ku.data_encipherment is True
983 assert ku.key_agreement is True
984 assert ku.key_cert_sign is True
985 assert ku.crl_sign is True
986 assert ku.encipher_only is True
987 assert ku.decipher_only is True
988
989 def test_key_cert_sign_crl_sign(self, backend):
990 cert = _load_cert(
991 os.path.join(
992 "x509", "PKITS_data", "certs", "pathLenConstraint6CACert.crt"
993 ),
994 x509.load_der_x509_certificate,
995 backend
996 )
997 ext = cert.extensions.get_extension_for_oid(x509.OID_KEY_USAGE)
998 assert ext is not None
999 assert ext.critical is True
1000
1001 ku = ext.value
1002 assert ku.digital_signature is False
1003 assert ku.content_commitment is False
1004 assert ku.key_encipherment is False
1005 assert ku.data_encipherment is False
1006 assert ku.key_agreement is False
1007 assert ku.key_cert_sign is True
1008 assert ku.crl_sign is True
Paul Kehrer31bdf792015-03-25 14:11:00 -05001009
1010
1011@pytest.mark.parametrize(
1012 "name", [
1013 x509.RFC822Name,
1014 x509.DNSName,
1015 x509.UniformResourceIdentifier
1016 ]
1017)
1018class TestTextGeneralNames(object):
1019 def test_not_text(self, name):
1020 with pytest.raises(TypeError):
1021 name(b"notaunicodestring")
1022
1023 with pytest.raises(TypeError):
1024 name(1.3)
1025
1026 def test_repr(self, name):
Eeshan Gargf1234152015-04-29 18:41:00 +05301027 gn = name(u"string")
Paul Kehrer31bdf792015-03-25 14:11:00 -05001028 assert repr(gn) == "<{0}(value=string)>".format(name.__name__)
1029
1030 def test_eq(self, name):
Eeshan Gargf1234152015-04-29 18:41:00 +05301031 gn = name(u"string")
1032 gn2 = name(u"string")
Paul Kehrer31bdf792015-03-25 14:11:00 -05001033 assert gn == gn2
1034
1035 def test_ne(self, name):
Eeshan Gargf1234152015-04-29 18:41:00 +05301036 gn = name(u"string")
1037 gn2 = name(u"string2")
Paul Kehrer31bdf792015-03-25 14:11:00 -05001038 assert gn != gn2
1039 assert gn != object()
1040
1041
1042class TestDirectoryName(object):
1043 def test_not_name(self):
1044 with pytest.raises(TypeError):
1045 x509.DirectoryName(b"notaname")
1046
1047 with pytest.raises(TypeError):
1048 x509.DirectoryName(1.3)
1049
1050 def test_repr(self):
1051 name = x509.Name([x509.NameAttribute(x509.OID_COMMON_NAME, 'value1')])
1052 gn = x509.DirectoryName(x509.Name([name]))
1053 assert repr(gn) == (
1054 "<DirectoryName(value=<Name([<Name([<NameAttribute(oid=<ObjectIden"
1055 "tifier(oid=2.5.4.3, name=commonName)>, value='value1')>])>])>)>"
1056 )
1057
1058 def test_eq(self):
1059 name = x509.Name([
1060 x509.NameAttribute(x509.ObjectIdentifier('oid'), 'value1')
1061 ])
1062 name2 = x509.Name([
1063 x509.NameAttribute(x509.ObjectIdentifier('oid'), 'value1')
1064 ])
1065 gn = x509.DirectoryName(x509.Name([name]))
1066 gn2 = x509.DirectoryName(x509.Name([name2]))
1067 assert gn == gn2
1068
1069 def test_ne(self):
1070 name = x509.Name([
1071 x509.NameAttribute(x509.ObjectIdentifier('oid'), 'value1')
1072 ])
1073 name2 = x509.Name([
1074 x509.NameAttribute(x509.ObjectIdentifier('oid'), 'value2')
1075 ])
1076 gn = x509.DirectoryName(x509.Name([name]))
1077 gn2 = x509.DirectoryName(x509.Name([name2]))
1078 assert gn != gn2
1079 assert gn != object()
1080
1081
1082class TestRegisteredID(object):
1083 def test_not_oid(self):
1084 with pytest.raises(TypeError):
1085 x509.RegisteredID(b"notanoid")
1086
1087 with pytest.raises(TypeError):
1088 x509.RegisteredID(1.3)
1089
1090 def test_repr(self):
1091 gn = x509.RegisteredID(x509.OID_COMMON_NAME)
1092 assert repr(gn) == (
1093 "<RegisteredID(value=<ObjectIdentifier(oid=2.5.4.3, name=commonNam"
1094 "e)>)>"
1095 )
1096
1097 def test_eq(self):
1098 gn = x509.RegisteredID(x509.OID_COMMON_NAME)
1099 gn2 = x509.RegisteredID(x509.OID_COMMON_NAME)
1100 assert gn == gn2
1101
1102 def test_ne(self):
1103 gn = x509.RegisteredID(x509.OID_COMMON_NAME)
1104 gn2 = x509.RegisteredID(x509.OID_BASIC_CONSTRAINTS)
1105 assert gn != gn2
1106 assert gn != object()
1107
1108
1109class TestIPAddress(object):
1110 def test_not_ipaddress(self):
1111 with pytest.raises(TypeError):
1112 x509.IPAddress(b"notanipaddress")
1113
1114 with pytest.raises(TypeError):
1115 x509.IPAddress(1.3)
1116
1117 def test_repr(self):
Eeshan Gargf1234152015-04-29 18:41:00 +05301118 gn = x509.IPAddress(ipaddress.IPv4Address(u"127.0.0.1"))
Paul Kehrer31bdf792015-03-25 14:11:00 -05001119 assert repr(gn) == "<IPAddress(value=127.0.0.1)>"
1120
Eeshan Gargf1234152015-04-29 18:41:00 +05301121 gn2 = x509.IPAddress(ipaddress.IPv6Address(u"ff::"))
Paul Kehrer31bdf792015-03-25 14:11:00 -05001122 assert repr(gn2) == "<IPAddress(value=ff::)>"
1123
Paul Kehrereb177932015-05-17 18:33:33 -07001124 gn3 = x509.IPAddress(ipaddress.IPv4Network(u"192.168.0.0/24"))
1125 assert repr(gn3) == "<IPAddress(value=192.168.0.0/24)>"
1126
1127 gn4 = x509.IPAddress(ipaddress.IPv6Network(u"ff::/96"))
1128 assert repr(gn4) == "<IPAddress(value=ff::/96)>"
1129
Paul Kehrer31bdf792015-03-25 14:11:00 -05001130 def test_eq(self):
Eeshan Gargf1234152015-04-29 18:41:00 +05301131 gn = x509.IPAddress(ipaddress.IPv4Address(u"127.0.0.1"))
1132 gn2 = x509.IPAddress(ipaddress.IPv4Address(u"127.0.0.1"))
Paul Kehrer31bdf792015-03-25 14:11:00 -05001133 assert gn == gn2
1134
1135 def test_ne(self):
Eeshan Gargf1234152015-04-29 18:41:00 +05301136 gn = x509.IPAddress(ipaddress.IPv4Address(u"127.0.0.1"))
1137 gn2 = x509.IPAddress(ipaddress.IPv4Address(u"127.0.0.2"))
Paul Kehrer31bdf792015-03-25 14:11:00 -05001138 assert gn != gn2
1139 assert gn != object()
1140
1141
1142class TestSubjectAlternativeName(object):
1143 def test_get_values_for_type(self):
1144 san = x509.SubjectAlternativeName(
Eeshan Gargf1234152015-04-29 18:41:00 +05301145 [x509.DNSName(u"cryptography.io")]
Paul Kehrer31bdf792015-03-25 14:11:00 -05001146 )
1147 names = san.get_values_for_type(x509.DNSName)
Eeshan Gargf1234152015-04-29 18:41:00 +05301148 assert names == [u"cryptography.io"]
Paul Kehrer31bdf792015-03-25 14:11:00 -05001149
1150 def test_iter_names(self):
1151 san = x509.SubjectAlternativeName([
Eeshan Gargf1234152015-04-29 18:41:00 +05301152 x509.DNSName(u"cryptography.io"),
1153 x509.DNSName(u"crypto.local"),
Paul Kehrer31bdf792015-03-25 14:11:00 -05001154 ])
1155 assert len(san) == 2
1156 assert list(san) == [
Eeshan Gargf1234152015-04-29 18:41:00 +05301157 x509.DNSName(u"cryptography.io"),
1158 x509.DNSName(u"crypto.local"),
Paul Kehrer31bdf792015-03-25 14:11:00 -05001159 ]
1160
Paul Kehrerd04b39b2015-04-21 08:44:17 -05001161 def test_invalid_general_names(self):
1162 with pytest.raises(TypeError):
1163 x509.SubjectAlternativeName(
Eeshan Gargf1234152015-04-29 18:41:00 +05301164 [x509.DNSName(u"cryptography.io"), "invalid"]
Paul Kehrerd04b39b2015-04-21 08:44:17 -05001165 )
1166
Paul Kehrer31bdf792015-03-25 14:11:00 -05001167 def test_repr(self):
1168 san = x509.SubjectAlternativeName(
1169 [
Eeshan Gargf1234152015-04-29 18:41:00 +05301170 x509.DNSName(u"cryptography.io")
Paul Kehrer31bdf792015-03-25 14:11:00 -05001171 ]
1172 )
1173 assert repr(san) == (
1174 "<SubjectAlternativeName([<DNSName(value=cryptography.io)>])>"
1175 )
Paul Kehrer40f83382015-04-20 15:00:16 -05001176
Paul Kehrer58cc3972015-05-13 10:00:41 -05001177 def test_eq(self):
1178 san = x509.SubjectAlternativeName(
1179 [x509.DNSName(u"cryptography.io")]
1180 )
1181 san2 = x509.SubjectAlternativeName(
1182 [x509.DNSName(u"cryptography.io")]
1183 )
1184 assert san == san2
1185
1186 def test_ne(self):
1187 san = x509.SubjectAlternativeName(
1188 [x509.DNSName(u"cryptography.io")]
1189 )
1190 san2 = x509.SubjectAlternativeName(
1191 [x509.RFC822Name(u"admin@cryptography.io")]
1192 )
1193 assert san != san2
1194 assert san != object()
1195
Paul Kehrer40f83382015-04-20 15:00:16 -05001196
1197@pytest.mark.requires_backend_interface(interface=RSABackend)
1198@pytest.mark.requires_backend_interface(interface=X509Backend)
1199class TestRSASubjectAlternativeNameExtension(object):
1200 def test_dns_name(self, backend):
1201 cert = _load_cert(
1202 os.path.join("x509", "cryptography.io.pem"),
1203 x509.load_pem_x509_certificate,
1204 backend
1205 )
1206 ext = cert.extensions.get_extension_for_oid(
1207 x509.OID_SUBJECT_ALTERNATIVE_NAME
1208 )
1209 assert ext is not None
1210 assert ext.critical is False
1211
1212 san = ext.value
1213
1214 dns = san.get_values_for_type(x509.DNSName)
1215 assert dns == [u"www.cryptography.io", u"cryptography.io"]
Paul Kehrer9089c912015-04-20 22:15:20 -05001216
1217 def test_unsupported_other_name(self, backend):
1218 cert = _load_cert(
1219 os.path.join(
1220 "x509", "custom", "san_other_name.pem"
1221 ),
1222 x509.load_pem_x509_certificate,
1223 backend
1224 )
Paul Kehrerbed07352015-04-21 08:31:10 -05001225 with pytest.raises(x509.UnsupportedGeneralNameType) as exc:
Paul Kehrer9089c912015-04-20 22:15:20 -05001226 cert.extensions
Paul Kehrerbed07352015-04-21 08:31:10 -05001227
Paul Kehrer0a621bf2015-04-22 09:22:56 -05001228 assert exc.value.type == 0
Paul Kehrer4db96622015-04-20 22:17:39 -05001229
1230 def test_registered_id(self, backend):
1231 cert = _load_cert(
1232 os.path.join(
1233 "x509", "custom", "san_registered_id.pem"
1234 ),
1235 x509.load_pem_x509_certificate,
1236 backend
1237 )
1238 ext = cert.extensions.get_extension_for_oid(
1239 x509.OID_SUBJECT_ALTERNATIVE_NAME
1240 )
1241 assert ext is not None
1242 assert ext.critical is False
1243
1244 san = ext.value
1245 rid = san.get_values_for_type(x509.RegisteredID)
1246 assert rid == [x509.ObjectIdentifier("1.2.3.4")]
Paul Kehrerb8ef82e2015-04-22 16:04:24 -05001247
1248 def test_uri(self, backend):
1249 cert = _load_cert(
1250 os.path.join(
1251 "x509", "custom", "san_uri_with_port.pem"
1252 ),
1253 x509.load_pem_x509_certificate,
1254 backend
1255 )
1256 ext = cert.extensions.get_extension_for_oid(
1257 x509.OID_SUBJECT_ALTERNATIVE_NAME
1258 )
1259 assert ext is not None
1260 uri = ext.value.get_values_for_type(
1261 x509.UniformResourceIdentifier
1262 )
1263 assert uri == [
1264 u"gopher://\u043f\u044b\u043a\u0430.cryptography:70/path?q=s#hel"
1265 u"lo",
1266 u"http://someregulardomain.com",
1267 ]
Paul Kehrera5f030c2015-04-28 08:33:18 -05001268
1269 def test_ipaddress(self, backend):
1270 cert = _load_cert(
1271 os.path.join(
1272 "x509", "custom", "san_ipaddr.pem"
1273 ),
1274 x509.load_pem_x509_certificate,
1275 backend
1276 )
1277 ext = cert.extensions.get_extension_for_oid(
1278 x509.OID_SUBJECT_ALTERNATIVE_NAME
1279 )
1280 assert ext is not None
1281 assert ext.critical is False
1282
1283 san = ext.value
1284
1285 ip = san.get_values_for_type(x509.IPAddress)
1286 assert [
1287 ipaddress.ip_address(u"127.0.0.1"),
1288 ipaddress.ip_address(u"ff::")
1289 ] == ip
Paul Kehrer2187a052015-04-30 08:22:07 -05001290
1291 def test_dirname(self, backend):
1292 cert = _load_cert(
1293 os.path.join(
1294 "x509", "custom", "san_dirname.pem"
1295 ),
1296 x509.load_pem_x509_certificate,
1297 backend
1298 )
1299 ext = cert.extensions.get_extension_for_oid(
1300 x509.OID_SUBJECT_ALTERNATIVE_NAME
1301 )
1302 assert ext is not None
1303 assert ext.critical is False
1304
1305 san = ext.value
1306
1307 dirname = san.get_values_for_type(x509.DirectoryName)
1308 assert [
1309 x509.Name([
1310 x509.NameAttribute(x509.OID_COMMON_NAME, 'test'),
1311 x509.NameAttribute(x509.OID_ORGANIZATION_NAME, 'Org'),
1312 x509.NameAttribute(x509.OID_STATE_OR_PROVINCE_NAME, 'Texas'),
1313 ])
1314 ] == dirname
Paul Kehrere06cab42015-04-30 10:23:33 -05001315
1316 def test_rfc822name(self, backend):
1317 cert = _load_cert(
1318 os.path.join(
1319 "x509", "custom", "san_rfc822_idna.pem"
1320 ),
1321 x509.load_pem_x509_certificate,
1322 backend
1323 )
1324 ext = cert.extensions.get_extension_for_oid(
1325 x509.OID_SUBJECT_ALTERNATIVE_NAME
1326 )
1327 assert ext is not None
1328 assert ext.critical is False
1329
1330 san = ext.value
1331
1332 rfc822name = san.get_values_for_type(x509.RFC822Name)
1333 assert [u"email@em\xe5\xefl.com"] == rfc822name
1334
Paul Kehrerb8968812015-05-15 09:01:34 -07001335 def test_idna2003_invalid(self, backend):
1336 cert = _load_cert(
1337 os.path.join(
1338 "x509", "custom", "san_idna2003_dnsname.pem"
1339 ),
1340 x509.load_pem_x509_certificate,
1341 backend
1342 )
1343 with pytest.raises(UnicodeError):
1344 cert.extensions
1345
Paul Kehrere06cab42015-04-30 10:23:33 -05001346 def test_unicode_rfc822_name_dns_name_uri(self, backend):
1347 cert = _load_cert(
1348 os.path.join(
1349 "x509", "custom", "san_idna_names.pem"
1350 ),
1351 x509.load_pem_x509_certificate,
1352 backend
1353 )
1354 ext = cert.extensions.get_extension_for_oid(
1355 x509.OID_SUBJECT_ALTERNATIVE_NAME
1356 )
1357 assert ext is not None
1358 rfc822_name = ext.value.get_values_for_type(x509.RFC822Name)
1359 dns_name = ext.value.get_values_for_type(x509.DNSName)
1360 uri = ext.value.get_values_for_type(x509.UniformResourceIdentifier)
1361 assert rfc822_name == [u"email@\u043f\u044b\u043a\u0430.cryptography"]
1362 assert dns_name == [u"\u043f\u044b\u043a\u0430.cryptography"]
1363 assert uri == [u"https://www.\u043f\u044b\u043a\u0430.cryptography"]
1364
1365 def test_rfc822name_dnsname_ipaddress_directoryname_uri(self, backend):
1366 cert = _load_cert(
1367 os.path.join(
1368 "x509", "custom", "san_email_dns_ip_dirname_uri.pem"
1369 ),
1370 x509.load_pem_x509_certificate,
1371 backend
1372 )
1373 ext = cert.extensions.get_extension_for_oid(
1374 x509.OID_SUBJECT_ALTERNATIVE_NAME
1375 )
1376 assert ext is not None
1377 assert ext.critical is False
1378
1379 san = ext.value
1380
1381 rfc822_name = san.get_values_for_type(x509.RFC822Name)
1382 uri = san.get_values_for_type(x509.UniformResourceIdentifier)
1383 dns = san.get_values_for_type(x509.DNSName)
1384 ip = san.get_values_for_type(x509.IPAddress)
1385 dirname = san.get_values_for_type(x509.DirectoryName)
1386 assert [u"user@cryptography.io"] == rfc822_name
Paul Kehrere3a330c2015-05-02 16:42:52 -05001387 assert [u"https://cryptography.io"] == uri
Paul Kehrere06cab42015-04-30 10:23:33 -05001388 assert [u"cryptography.io"] == dns
1389 assert [
1390 x509.Name([
1391 x509.NameAttribute(x509.OID_COMMON_NAME, 'dirCN'),
1392 x509.NameAttribute(
1393 x509.OID_ORGANIZATION_NAME, 'Cryptographic Authority'
1394 ),
1395 ])
1396 ] == dirname
1397 assert [
1398 ipaddress.ip_address(u"127.0.0.1"),
1399 ipaddress.ip_address(u"ff::")
1400 ] == ip
1401
1402 def test_invalid_rfc822name(self, backend):
1403 cert = _load_cert(
1404 os.path.join(
1405 "x509", "custom", "san_rfc822_names.pem"
1406 ),
1407 x509.load_pem_x509_certificate,
1408 backend
1409 )
1410 with pytest.raises(ValueError) as exc:
1411 cert.extensions
1412
1413 assert 'Invalid rfc822name value' in str(exc.value)
Paul Kehrer94c69602015-05-02 19:29:40 -05001414
1415
1416@pytest.mark.requires_backend_interface(interface=RSABackend)
1417@pytest.mark.requires_backend_interface(interface=X509Backend)
1418class TestExtendedKeyUsageExtension(object):
1419 def test_eku(self, backend):
1420 cert = _load_cert(
1421 os.path.join(
1422 "x509", "custom", "extended_key_usage.pem"
1423 ),
1424 x509.load_pem_x509_certificate,
1425 backend
1426 )
1427 ext = cert.extensions.get_extension_for_oid(
1428 x509.OID_EXTENDED_KEY_USAGE
1429 )
1430 assert ext is not None
1431 assert ext.critical is False
1432
1433 assert [
1434 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.1"),
1435 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.2"),
1436 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.3"),
1437 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.4"),
1438 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.9"),
1439 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.8"),
1440 x509.ObjectIdentifier("2.5.29.37.0"),
1441 x509.ObjectIdentifier("2.16.840.1.113730.4.1"),
1442 ] == list(ext.value)
Paul Kehrer3e6d5582015-05-02 21:57:56 -05001443
1444
1445class TestAccessDescription(object):
1446 def test_invalid_access_method(self):
Paul Kehrerf506bca2015-05-02 22:31:47 -05001447 with pytest.raises(ValueError):
Paul Kehrer3e6d5582015-05-02 21:57:56 -05001448 x509.AccessDescription("notanoid", x509.DNSName(u"test"))
1449
1450 def test_invalid_access_location(self):
1451 with pytest.raises(TypeError):
1452 x509.AccessDescription(x509.OID_CA_ISSUERS, "invalid")
1453
1454 def test_repr(self):
1455 ad = x509.AccessDescription(
1456 x509.OID_OCSP,
1457 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1458 )
1459 assert repr(ad) == (
1460 "<AccessDescription(access_method=<ObjectIdentifier(oid=1.3.6.1.5."
1461 "5.7.48.1, name=OCSP)>, access_location=<UniformResourceIdentifier"
1462 "(value=http://ocsp.domain.com)>)>"
1463 )
1464
1465 def test_eq(self):
1466 ad = x509.AccessDescription(
1467 x509.OID_OCSP,
1468 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1469 )
1470 ad2 = x509.AccessDescription(
1471 x509.OID_OCSP,
1472 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1473 )
1474 assert ad == ad2
1475
1476 def test_ne(self):
1477 ad = x509.AccessDescription(
1478 x509.OID_OCSP,
1479 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1480 )
1481 ad2 = x509.AccessDescription(
1482 x509.OID_CA_ISSUERS,
1483 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1484 )
1485 ad3 = x509.AccessDescription(
1486 x509.OID_OCSP,
1487 x509.UniformResourceIdentifier(u"http://notthesame")
1488 )
1489 assert ad != ad2
1490 assert ad != ad3
1491 assert ad != object()
1492
1493
1494class TestAuthorityInformationAccess(object):
1495 def test_invalid_descriptions(self):
1496 with pytest.raises(TypeError):
1497 x509.AuthorityInformationAccess(["notanAccessDescription"])
1498
1499 def test_iter_len(self):
1500 aia = x509.AuthorityInformationAccess([
1501 x509.AccessDescription(
1502 x509.OID_OCSP,
1503 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1504 ),
1505 x509.AccessDescription(
1506 x509.OID_CA_ISSUERS,
1507 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1508 )
1509 ])
1510 assert len(aia) == 2
1511 assert list(aia) == [
1512 x509.AccessDescription(
1513 x509.OID_OCSP,
1514 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1515 ),
1516 x509.AccessDescription(
1517 x509.OID_CA_ISSUERS,
1518 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1519 )
1520 ]
1521
1522 def test_repr(self):
1523 aia = x509.AuthorityInformationAccess([
1524 x509.AccessDescription(
1525 x509.OID_OCSP,
1526 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1527 ),
1528 x509.AccessDescription(
1529 x509.OID_CA_ISSUERS,
1530 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1531 )
1532 ])
1533 assert repr(aia) == (
1534 "<AuthorityInformationAccess([<AccessDescription(access_method=<Ob"
1535 "jectIdentifier(oid=1.3.6.1.5.5.7.48.1, name=OCSP)>, access_locati"
1536 "on=<UniformResourceIdentifier(value=http://ocsp.domain.com)>)>, <"
1537 "AccessDescription(access_method=<ObjectIdentifier(oid=1.3.6.1.5.5"
1538 ".7.48.2, name=caIssuers)>, access_location=<UniformResourceIdenti"
1539 "fier(value=http://domain.com/ca.crt)>)>])>"
1540 )
1541
1542 def test_eq(self):
1543 aia = x509.AuthorityInformationAccess([
1544 x509.AccessDescription(
1545 x509.OID_OCSP,
1546 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1547 ),
1548 x509.AccessDescription(
1549 x509.OID_CA_ISSUERS,
1550 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1551 )
1552 ])
1553 aia2 = x509.AuthorityInformationAccess([
1554 x509.AccessDescription(
1555 x509.OID_OCSP,
1556 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1557 ),
1558 x509.AccessDescription(
1559 x509.OID_CA_ISSUERS,
1560 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1561 )
1562 ])
1563 assert aia == aia2
1564
1565 def test_ne(self):
1566 aia = x509.AuthorityInformationAccess([
1567 x509.AccessDescription(
1568 x509.OID_OCSP,
1569 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1570 ),
1571 x509.AccessDescription(
1572 x509.OID_CA_ISSUERS,
1573 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1574 )
1575 ])
1576 aia2 = x509.AuthorityInformationAccess([
1577 x509.AccessDescription(
1578 x509.OID_OCSP,
1579 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1580 ),
1581 ])
1582
1583 assert aia != aia2
1584 assert aia != object()
Paul Kehrerd774de92015-05-03 10:52:25 -05001585
1586
1587@pytest.mark.requires_backend_interface(interface=RSABackend)
1588@pytest.mark.requires_backend_interface(interface=X509Backend)
Paul Kehrera1476992015-05-04 17:35:47 -05001589class TestAuthorityInformationAccessExtension(object):
1590 def test_aia_ocsp_ca_issuers(self, backend):
1591 cert = _load_cert(
1592 os.path.join("x509", "cryptography.io.pem"),
1593 x509.load_pem_x509_certificate,
1594 backend
1595 )
1596 ext = cert.extensions.get_extension_for_oid(
1597 x509.OID_AUTHORITY_INFORMATION_ACCESS
1598 )
1599 assert ext is not None
1600 assert ext.critical is False
1601
1602 assert ext.value == x509.AuthorityInformationAccess([
1603 x509.AccessDescription(
1604 x509.OID_OCSP,
1605 x509.UniformResourceIdentifier(u"http://gv.symcd.com")
1606 ),
1607 x509.AccessDescription(
1608 x509.OID_CA_ISSUERS,
1609 x509.UniformResourceIdentifier(u"http://gv.symcb.com/gv.crt")
1610 ),
1611 ])
1612
1613 def test_aia_multiple_ocsp_ca_issuers(self, backend):
1614 cert = _load_cert(
1615 os.path.join("x509", "custom", "aia_ocsp_ca_issuers.pem"),
1616 x509.load_pem_x509_certificate,
1617 backend
1618 )
1619 ext = cert.extensions.get_extension_for_oid(
1620 x509.OID_AUTHORITY_INFORMATION_ACCESS
1621 )
1622 assert ext is not None
1623 assert ext.critical is False
1624
1625 assert ext.value == x509.AuthorityInformationAccess([
1626 x509.AccessDescription(
1627 x509.OID_OCSP,
1628 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1629 ),
1630 x509.AccessDescription(
1631 x509.OID_OCSP,
1632 x509.UniformResourceIdentifier(u"http://ocsp2.domain.com")
1633 ),
1634 x509.AccessDescription(
1635 x509.OID_CA_ISSUERS,
1636 x509.DirectoryName(x509.Name([
1637 x509.NameAttribute(x509.OID_COMMON_NAME, "myCN"),
1638 x509.NameAttribute(x509.OID_ORGANIZATION_NAME, "some Org"),
1639 ]))
1640 ),
1641 ])
1642
1643 def test_aia_ocsp_only(self, backend):
1644 cert = _load_cert(
1645 os.path.join("x509", "custom", "aia_ocsp.pem"),
1646 x509.load_pem_x509_certificate,
1647 backend
1648 )
1649 ext = cert.extensions.get_extension_for_oid(
1650 x509.OID_AUTHORITY_INFORMATION_ACCESS
1651 )
1652 assert ext is not None
1653 assert ext.critical is False
1654
1655 assert ext.value == x509.AuthorityInformationAccess([
1656 x509.AccessDescription(
1657 x509.OID_OCSP,
1658 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1659 ),
1660 ])
1661
1662 def test_aia_ca_issuers_only(self, backend):
1663 cert = _load_cert(
1664 os.path.join("x509", "custom", "aia_ca_issuers.pem"),
1665 x509.load_pem_x509_certificate,
1666 backend
1667 )
1668 ext = cert.extensions.get_extension_for_oid(
1669 x509.OID_AUTHORITY_INFORMATION_ACCESS
1670 )
1671 assert ext is not None
1672 assert ext.critical is False
1673
1674 assert ext.value == x509.AuthorityInformationAccess([
1675 x509.AccessDescription(
1676 x509.OID_CA_ISSUERS,
1677 x509.DirectoryName(x509.Name([
1678 x509.NameAttribute(x509.OID_COMMON_NAME, "myCN"),
1679 x509.NameAttribute(x509.OID_ORGANIZATION_NAME, "some Org"),
1680 ]))
1681 ),
1682 ])
1683
1684
1685@pytest.mark.requires_backend_interface(interface=RSABackend)
1686@pytest.mark.requires_backend_interface(interface=X509Backend)
Paul Kehrerd774de92015-05-03 10:52:25 -05001687class TestAuthorityKeyIdentifierExtension(object):
1688 def test_aki_keyid(self, backend):
1689 cert = _load_cert(
1690 os.path.join(
1691 "x509", "cryptography.io.pem"
1692 ),
1693 x509.load_pem_x509_certificate,
1694 backend
1695 )
1696 ext = cert.extensions.get_extension_for_oid(
1697 x509.OID_AUTHORITY_KEY_IDENTIFIER
1698 )
1699 assert ext is not None
1700 assert ext.critical is False
1701
1702 assert ext.value.key_identifier == (
1703 b"\xc3\x9c\xf3\xfc\xd3F\x084\xbb\xceF\x7f\xa0|[\xf3\xe2\x08\xcbY"
1704 )
1705 assert ext.value.authority_cert_issuer is None
1706 assert ext.value.authority_cert_serial_number is None
1707
1708 def test_aki_all_fields(self, backend):
1709 cert = _load_cert(
1710 os.path.join(
1711 "x509", "custom", "authority_key_identifier.pem"
1712 ),
1713 x509.load_pem_x509_certificate,
1714 backend
1715 )
1716 ext = cert.extensions.get_extension_for_oid(
1717 x509.OID_AUTHORITY_KEY_IDENTIFIER
1718 )
1719 assert ext is not None
1720 assert ext.critical is False
1721
1722 assert ext.value.key_identifier == (
1723 b"9E>\xca=b\x1d\xea\x86I\xf6Z\xab@\xb7\xa4p\x98\xf1\xec"
1724 )
1725 assert ext.value.authority_cert_issuer == [
1726 x509.DirectoryName(
1727 x509.Name([
1728 x509.NameAttribute(
1729 x509.OID_ORGANIZATION_NAME, u"PyCA"
1730 ),
1731 x509.NameAttribute(
1732 x509.OID_COMMON_NAME, u"cryptography.io"
1733 )
1734 ])
1735 )
1736 ]
1737 assert ext.value.authority_cert_serial_number == 3
1738
1739 def test_aki_no_keyid(self, backend):
1740 cert = _load_cert(
1741 os.path.join(
1742 "x509", "custom", "authority_key_identifier_no_keyid.pem"
1743 ),
1744 x509.load_pem_x509_certificate,
1745 backend
1746 )
1747 ext = cert.extensions.get_extension_for_oid(
1748 x509.OID_AUTHORITY_KEY_IDENTIFIER
1749 )
1750 assert ext is not None
1751 assert ext.critical is False
1752
1753 assert ext.value.key_identifier is None
1754 assert ext.value.authority_cert_issuer == [
1755 x509.DirectoryName(
1756 x509.Name([
1757 x509.NameAttribute(
1758 x509.OID_ORGANIZATION_NAME, u"PyCA"
1759 ),
1760 x509.NameAttribute(
1761 x509.OID_COMMON_NAME, u"cryptography.io"
1762 )
1763 ])
1764 )
1765 ]
1766 assert ext.value.authority_cert_serial_number == 3
Paul Kehrer5a485522015-05-06 00:29:12 -05001767
1768
Paul Kehrer5a485522015-05-06 00:29:12 -05001769class TestDistributionPoint(object):
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001770 def test_distribution_point_full_name_not_general_names(self):
Paul Kehrer5a485522015-05-06 00:29:12 -05001771 with pytest.raises(TypeError):
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001772 x509.DistributionPoint(["notgn"], None, None, None)
Paul Kehrer5a485522015-05-06 00:29:12 -05001773
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001774 def test_distribution_point_relative_name_not_name(self):
Paul Kehrer5a485522015-05-06 00:29:12 -05001775 with pytest.raises(TypeError):
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001776 x509.DistributionPoint(None, "notname", None, None)
1777
1778 def test_distribution_point_full_and_relative_not_none(self):
1779 with pytest.raises(ValueError):
1780 x509.DistributionPoint("data", "notname", None, None)
Paul Kehrer5a485522015-05-06 00:29:12 -05001781
1782 def test_crl_issuer_not_general_names(self):
1783 with pytest.raises(TypeError):
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001784 x509.DistributionPoint(None, None, None, ["notgn"])
Paul Kehrer5a485522015-05-06 00:29:12 -05001785
1786 def test_reason_not_reasonflags(self):
1787 with pytest.raises(TypeError):
1788 x509.DistributionPoint(
1789 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001790 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05001791 frozenset(["notreasonflags"]),
1792 None
1793 )
1794
1795 def test_reason_not_frozenset(self):
1796 with pytest.raises(TypeError):
1797 x509.DistributionPoint(
1798 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
1799 None,
1800 [x509.ReasonFlags.ca_compromise],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001801 None
1802 )
1803
1804 def test_disallowed_reasons(self):
1805 with pytest.raises(ValueError):
1806 x509.DistributionPoint(
1807 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
1808 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05001809 frozenset([x509.ReasonFlags.unspecified]),
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001810 None
1811 )
1812
1813 with pytest.raises(ValueError):
1814 x509.DistributionPoint(
1815 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
1816 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05001817 frozenset([x509.ReasonFlags.remove_from_crl]),
Paul Kehrer5a485522015-05-06 00:29:12 -05001818 None
1819 )
1820
1821 def test_reason_only(self):
1822 with pytest.raises(ValueError):
1823 x509.DistributionPoint(
1824 None,
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001825 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05001826 frozenset([x509.ReasonFlags.aa_compromise]),
Paul Kehrer5a485522015-05-06 00:29:12 -05001827 None
1828 )
1829
1830 def test_eq(self):
1831 dp = x509.DistributionPoint(
1832 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001833 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05001834 frozenset([x509.ReasonFlags.superseded]),
Paul Kehrer5a485522015-05-06 00:29:12 -05001835 [
1836 x509.DirectoryName(
1837 x509.Name([
1838 x509.NameAttribute(
1839 x509.OID_COMMON_NAME, "Important CA"
1840 )
1841 ])
1842 )
1843 ],
1844 )
1845 dp2 = x509.DistributionPoint(
1846 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001847 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05001848 frozenset([x509.ReasonFlags.superseded]),
Paul Kehrer5a485522015-05-06 00:29:12 -05001849 [
1850 x509.DirectoryName(
1851 x509.Name([
1852 x509.NameAttribute(
1853 x509.OID_COMMON_NAME, "Important CA"
1854 )
1855 ])
1856 )
1857 ],
1858 )
1859 assert dp == dp2
1860
1861 def test_ne(self):
1862 dp = x509.DistributionPoint(
1863 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001864 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05001865 frozenset([x509.ReasonFlags.superseded]),
Paul Kehrer5a485522015-05-06 00:29:12 -05001866 [
1867 x509.DirectoryName(
1868 x509.Name([
1869 x509.NameAttribute(
1870 x509.OID_COMMON_NAME, "Important CA"
1871 )
1872 ])
1873 )
1874 ],
1875 )
1876 dp2 = x509.DistributionPoint(
1877 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
1878 None,
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001879 None,
Paul Kehrer5a485522015-05-06 00:29:12 -05001880 None
1881 )
1882 assert dp != dp2
1883 assert dp != object()
1884
1885 def test_repr(self):
1886 dp = x509.DistributionPoint(
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001887 None,
Paul Kehrer5a485522015-05-06 00:29:12 -05001888 x509.Name([
1889 x509.NameAttribute(x509.OID_COMMON_NAME, "myCN")
1890 ]),
Paul Kehrer96ef63c2015-05-09 21:17:04 -05001891 frozenset([x509.ReasonFlags.ca_compromise]),
Paul Kehrer5a485522015-05-06 00:29:12 -05001892 [
1893 x509.DirectoryName(
1894 x509.Name([
1895 x509.NameAttribute(
1896 x509.OID_COMMON_NAME, "Important CA"
1897 )
1898 ])
1899 )
1900 ],
1901 )
Paul Kehrer749da3b2015-05-10 09:58:29 -05001902 if six.PY3:
1903 assert repr(dp) == (
1904 "<DistributionPoint(full_name=None, relative_name=<Name([<Name"
1905 "Attribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName)"
1906 ">, value='myCN')>])>, reasons=frozenset({<ReasonFlags.ca_comp"
1907 "romise: 'cACompromise'>}), crl_issuer=[<DirectoryName(value=<"
1908 "Name([<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name="
1909 "commonName)>, value='Important CA')>])>)>])>"
1910 )
1911 else:
1912 assert repr(dp) == (
1913 "<DistributionPoint(full_name=None, relative_name=<Name([<Name"
1914 "Attribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName)"
1915 ">, value='myCN')>])>, reasons=frozenset([<ReasonFlags.ca_comp"
1916 "romise: 'cACompromise'>]), crl_issuer=[<DirectoryName(value=<"
1917 "Name([<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name="
1918 "commonName)>, value='Important CA')>])>)>])>"
1919 )
Paul Kehrer5a485522015-05-06 00:29:12 -05001920
1921
1922class TestCRLDistributionPoints(object):
1923 def test_invalid_distribution_points(self):
1924 with pytest.raises(TypeError):
1925 x509.CRLDistributionPoints(["notadistributionpoint"])
1926
1927 def test_iter_len(self):
1928 cdp = x509.CRLDistributionPoints([
1929 x509.DistributionPoint(
1930 [x509.UniformResourceIdentifier(u"http://domain")],
1931 None,
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001932 None,
Paul Kehrer5a485522015-05-06 00:29:12 -05001933 None
1934 ),
1935 x509.DistributionPoint(
1936 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001937 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05001938 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001939 x509.ReasonFlags.key_compromise,
1940 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05001941 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05001942 None
1943 ),
1944 ])
1945 assert len(cdp) == 2
1946 assert list(cdp) == [
1947 x509.DistributionPoint(
1948 [x509.UniformResourceIdentifier(u"http://domain")],
1949 None,
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001950 None,
Paul Kehrer5a485522015-05-06 00:29:12 -05001951 None
1952 ),
1953 x509.DistributionPoint(
1954 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001955 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05001956 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001957 x509.ReasonFlags.key_compromise,
1958 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05001959 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05001960 None
1961 ),
1962 ]
1963
1964 def test_repr(self):
1965 cdp = x509.CRLDistributionPoints([
1966 x509.DistributionPoint(
1967 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001968 None,
Paul Kehrer96ef63c2015-05-09 21:17:04 -05001969 frozenset([x509.ReasonFlags.key_compromise]),
Paul Kehrer5a485522015-05-06 00:29:12 -05001970 None
1971 ),
1972 ])
Paul Kehrer749da3b2015-05-10 09:58:29 -05001973 if six.PY3:
1974 assert repr(cdp) == (
1975 "<CRLDistributionPoints([<DistributionPoint(full_name=[<Unifo"
1976 "rmResourceIdentifier(value=ftp://domain)>], relative_name=No"
1977 "ne, reasons=frozenset({<ReasonFlags.key_compromise: 'keyComp"
1978 "romise'>}), crl_issuer=None)>])>"
1979 )
1980 else:
1981 assert repr(cdp) == (
1982 "<CRLDistributionPoints([<DistributionPoint(full_name=[<Unifo"
1983 "rmResourceIdentifier(value=ftp://domain)>], relative_name=No"
1984 "ne, reasons=frozenset([<ReasonFlags.key_compromise: 'keyComp"
1985 "romise'>]), crl_issuer=None)>])>"
1986 )
Paul Kehrer5a485522015-05-06 00:29:12 -05001987
1988 def test_eq(self):
1989 cdp = x509.CRLDistributionPoints([
1990 x509.DistributionPoint(
1991 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001992 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05001993 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05001994 x509.ReasonFlags.key_compromise,
1995 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05001996 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05001997 [x509.UniformResourceIdentifier(u"uri://thing")],
1998 ),
1999 ])
2000 cdp2 = x509.CRLDistributionPoints([
2001 x509.DistributionPoint(
2002 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002003 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002004 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002005 x509.ReasonFlags.key_compromise,
2006 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002007 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002008 [x509.UniformResourceIdentifier(u"uri://thing")],
2009 ),
2010 ])
2011 assert cdp == cdp2
2012
2013 def test_ne(self):
2014 cdp = x509.CRLDistributionPoints([
2015 x509.DistributionPoint(
2016 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002017 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002018 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002019 x509.ReasonFlags.key_compromise,
2020 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002021 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002022 [x509.UniformResourceIdentifier(u"uri://thing")],
2023 ),
2024 ])
2025 cdp2 = x509.CRLDistributionPoints([
2026 x509.DistributionPoint(
2027 [x509.UniformResourceIdentifier(u"ftp://domain2")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002028 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002029 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002030 x509.ReasonFlags.key_compromise,
2031 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002032 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002033 [x509.UniformResourceIdentifier(u"uri://thing")],
2034 ),
2035 ])
2036 cdp3 = x509.CRLDistributionPoints([
2037 x509.DistributionPoint(
2038 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002039 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002040 frozenset([x509.ReasonFlags.key_compromise]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002041 [x509.UniformResourceIdentifier(u"uri://thing")],
2042 ),
2043 ])
2044 cdp4 = x509.CRLDistributionPoints([
2045 x509.DistributionPoint(
2046 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002047 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002048 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002049 x509.ReasonFlags.key_compromise,
2050 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002051 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002052 [x509.UniformResourceIdentifier(u"uri://thing2")],
2053 ),
2054 ])
2055 assert cdp != cdp2
2056 assert cdp != cdp3
2057 assert cdp != cdp4
2058 assert cdp != object()
Paul Kehrer9a10d592015-05-10 14:55:51 -05002059
2060
2061@pytest.mark.requires_backend_interface(interface=RSABackend)
2062@pytest.mark.requires_backend_interface(interface=X509Backend)
2063class TestCRLDistributionPointsExtension(object):
2064 def test_fullname_and_crl_issuer(self, backend):
2065 cert = _load_cert(
2066 os.path.join(
2067 "x509", "PKITS_data", "certs", "ValidcRLIssuerTest28EE.crt"
2068 ),
2069 x509.load_der_x509_certificate,
2070 backend
2071 )
2072
2073 cdps = cert.extensions.get_extension_for_oid(
2074 x509.OID_CRL_DISTRIBUTION_POINTS
2075 ).value
2076
2077 assert cdps == x509.CRLDistributionPoints([
2078 x509.DistributionPoint(
2079 full_name=[x509.DirectoryName(
2080 x509.Name([
2081 x509.NameAttribute(x509.OID_COUNTRY_NAME, "US"),
2082 x509.NameAttribute(
2083 x509.OID_ORGANIZATION_NAME,
2084 "Test Certificates 2011"
2085 ),
2086 x509.NameAttribute(
2087 x509.OID_ORGANIZATIONAL_UNIT_NAME,
2088 "indirectCRL CA3 cRLIssuer"
2089 ),
2090 x509.NameAttribute(
2091 x509.OID_COMMON_NAME,
2092 "indirect CRL for indirectCRL CA3"
2093 ),
2094 ])
2095 )],
2096 relative_name=None,
2097 reasons=None,
2098 crl_issuer=[x509.DirectoryName(
2099 x509.Name([
2100 x509.NameAttribute(x509.OID_COUNTRY_NAME, "US"),
2101 x509.NameAttribute(
2102 x509.OID_ORGANIZATION_NAME,
2103 "Test Certificates 2011"
2104 ),
2105 x509.NameAttribute(
2106 x509.OID_ORGANIZATIONAL_UNIT_NAME,
2107 "indirectCRL CA3 cRLIssuer"
2108 ),
2109 ])
2110 )],
2111 )
2112 ])
2113
2114 def test_relativename_and_crl_issuer(self, backend):
2115 cert = _load_cert(
2116 os.path.join(
2117 "x509", "PKITS_data", "certs", "ValidcRLIssuerTest29EE.crt"
2118 ),
2119 x509.load_der_x509_certificate,
2120 backend
2121 )
2122
2123 cdps = cert.extensions.get_extension_for_oid(
2124 x509.OID_CRL_DISTRIBUTION_POINTS
2125 ).value
2126
2127 assert cdps == x509.CRLDistributionPoints([
2128 x509.DistributionPoint(
2129 full_name=None,
2130 relative_name=x509.Name([
2131 x509.NameAttribute(
2132 x509.OID_COMMON_NAME,
2133 "indirect CRL for indirectCRL CA3"
2134 ),
2135 ]),
2136 reasons=None,
2137 crl_issuer=[x509.DirectoryName(
2138 x509.Name([
2139 x509.NameAttribute(x509.OID_COUNTRY_NAME, "US"),
2140 x509.NameAttribute(
2141 x509.OID_ORGANIZATION_NAME,
2142 "Test Certificates 2011"
2143 ),
2144 x509.NameAttribute(
2145 x509.OID_ORGANIZATIONAL_UNIT_NAME,
2146 "indirectCRL CA3 cRLIssuer"
2147 ),
2148 ])
2149 )],
2150 )
2151 ])
2152
2153 def test_fullname_crl_issuer_reasons(self, backend):
2154 cert = _load_cert(
2155 os.path.join(
2156 "x509", "custom", "cdp_fullname_reasons_crl_issuer.pem"
2157 ),
2158 x509.load_pem_x509_certificate,
2159 backend
2160 )
2161
2162 cdps = cert.extensions.get_extension_for_oid(
2163 x509.OID_CRL_DISTRIBUTION_POINTS
2164 ).value
2165
2166 assert cdps == x509.CRLDistributionPoints([
2167 x509.DistributionPoint(
2168 full_name=[x509.UniformResourceIdentifier(
2169 u"http://myhost.com/myca.crl"
2170 )],
2171 relative_name=None,
2172 reasons=frozenset([
2173 x509.ReasonFlags.key_compromise,
2174 x509.ReasonFlags.ca_compromise
2175 ]),
2176 crl_issuer=[x509.DirectoryName(
2177 x509.Name([
2178 x509.NameAttribute(x509.OID_COUNTRY_NAME, "US"),
2179 x509.NameAttribute(
2180 x509.OID_ORGANIZATION_NAME, "PyCA"
2181 ),
2182 x509.NameAttribute(
2183 x509.OID_COMMON_NAME, "cryptography CA"
2184 ),
2185 ])
2186 )],
2187 )
2188 ])
2189
Paul Kehrer594a2ed2015-05-12 23:27:32 -05002190 def test_all_reasons(self, backend):
2191 cert = _load_cert(
2192 os.path.join(
2193 "x509", "custom", "cdp_all_reasons.pem"
2194 ),
2195 x509.load_pem_x509_certificate,
2196 backend
2197 )
2198
2199 cdps = cert.extensions.get_extension_for_oid(
2200 x509.OID_CRL_DISTRIBUTION_POINTS
2201 ).value
2202
2203 assert cdps == x509.CRLDistributionPoints([
2204 x509.DistributionPoint(
2205 full_name=[x509.UniformResourceIdentifier(
2206 u"http://domain.com/some.crl"
2207 )],
2208 relative_name=None,
2209 reasons=frozenset([
2210 x509.ReasonFlags.key_compromise,
2211 x509.ReasonFlags.ca_compromise,
2212 x509.ReasonFlags.affiliation_changed,
2213 x509.ReasonFlags.superseded,
2214 x509.ReasonFlags.privilege_withdrawn,
2215 x509.ReasonFlags.cessation_of_operation,
2216 x509.ReasonFlags.aa_compromise,
2217 x509.ReasonFlags.certificate_hold,
2218 ]),
2219 crl_issuer=None
2220 )
2221 ])
2222
2223 def test_single_reason(self, backend):
2224 cert = _load_cert(
2225 os.path.join(
2226 "x509", "custom", "cdp_reason_aa_compromise.pem"
2227 ),
2228 x509.load_pem_x509_certificate,
2229 backend
2230 )
2231
2232 cdps = cert.extensions.get_extension_for_oid(
2233 x509.OID_CRL_DISTRIBUTION_POINTS
2234 ).value
2235
2236 assert cdps == x509.CRLDistributionPoints([
2237 x509.DistributionPoint(
2238 full_name=[x509.UniformResourceIdentifier(
2239 u"http://domain.com/some.crl"
2240 )],
2241 relative_name=None,
2242 reasons=frozenset([x509.ReasonFlags.aa_compromise]),
2243 crl_issuer=None
2244 )
2245 ])
2246
Paul Kehrer9a10d592015-05-10 14:55:51 -05002247 def test_crl_issuer_only(self, backend):
2248 cert = _load_cert(
2249 os.path.join(
2250 "x509", "custom", "cdp_crl_issuer.pem"
2251 ),
2252 x509.load_pem_x509_certificate,
2253 backend
2254 )
2255
2256 cdps = cert.extensions.get_extension_for_oid(
2257 x509.OID_CRL_DISTRIBUTION_POINTS
2258 ).value
2259
2260 assert cdps == x509.CRLDistributionPoints([
2261 x509.DistributionPoint(
2262 full_name=None,
2263 relative_name=None,
2264 reasons=None,
2265 crl_issuer=[x509.DirectoryName(
2266 x509.Name([
2267 x509.NameAttribute(
2268 x509.OID_COMMON_NAME, "cryptography CA"
2269 ),
2270 ])
2271 )],
2272 )
2273 ])
Paul Kehrer16fae762015-05-01 23:14:20 -05002274
2275
2276class TestInhibitAnyPolicy(object):
2277 def test_not_int(self):
2278 with pytest.raises(TypeError):
2279 x509.InhibitAnyPolicy("notint")
2280
2281 def test_negative_int(self):
2282 with pytest.raises(ValueError):
2283 x509.InhibitAnyPolicy(-1)
2284
2285 def test_repr(self):
2286 iap = x509.InhibitAnyPolicy(0)
2287 assert repr(iap) == "<InhibitAnyPolicy(skip_certs=0)>"
2288
2289 def test_eq(self):
2290 iap = x509.InhibitAnyPolicy(1)
2291 iap2 = x509.InhibitAnyPolicy(1)
2292 assert iap == iap2
2293
2294 def test_ne(self):
2295 iap = x509.InhibitAnyPolicy(1)
2296 iap2 = x509.InhibitAnyPolicy(4)
2297 assert iap != iap2
2298 assert iap != object()