blob: 06adaa373dfc39e2b0cb679e8abc7d79687815d4 [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([
Ian Cordasco82fc3762015-06-16 20:59:50 -0500599 x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1'),
600 x509.NameAttribute(x509.ObjectIdentifier('oid2'), u'value2'),
Paul Kehrerc6e0f062015-05-03 11:04:34 -0500601 ])
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([
Ian Cordasco82fc3762015-06-16 20:59:50 -0500613 x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1'),
614 x509.NameAttribute(x509.ObjectIdentifier('oid2'), u'value2'),
Paul Kehrerc6e0f062015-05-03 11:04:34 -0500615 ])
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(
Ian Cordasco82fc3762015-06-16 20:59:50 -0500628 x509.Name([x509.NameAttribute(x509.OID_COMMON_NAME, u'myCN')])
Paul Kehrerc6e0f062015-05-03 11:04:34 -0500629 )
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="
Ian Cordascoa908d692015-06-16 21:35:24 -0500636 "<ObjectIdentifier(oid=2.5.4.3, name=commonName)>, value='myC"
Paul Kehrerc6e0f062015-05-03 11:04:34 -0500637 "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"
Ian Cordasco82fc3762015-06-16 20:59:50 -0500643 "jectIdentifier(oid=2.5.4.3, name=commonName)>, value=u'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(
Ian Cordasco82fc3762015-06-16 20:59:50 -0500649 x509.Name([x509.NameAttribute(x509.OID_COMMON_NAME, u'myCN')])
Paul Kehrer2ca2eb32015-05-03 10:04:57 -0500650 )
651 aki = x509.AuthorityKeyIdentifier(b"digest", [dirname], 1234)
652 dirname2 = x509.DirectoryName(
Ian Cordasco82fc3762015-06-16 20:59:50 -0500653 x509.Name([x509.NameAttribute(x509.OID_COMMON_NAME, u'myCN')])
Paul Kehrer2ca2eb32015-05-03 10:04:57 -0500654 )
655 aki2 = x509.AuthorityKeyIdentifier(b"digest", [dirname2], 1234)
656 assert aki == aki2
657
658 def test_ne(self):
659 dirname = x509.DirectoryName(
Ian Cordasco82fc3762015-06-16 20:59:50 -0500660 x509.Name([x509.NameAttribute(x509.OID_COMMON_NAME, u'myCN')])
Paul Kehrer2ca2eb32015-05-03 10:04:57 -0500661 )
662 dirname5 = x509.DirectoryName(
Ian Cordasco82fc3762015-06-16 20:59:50 -0500663 x509.Name([x509.NameAttribute(x509.OID_COMMON_NAME, u'aCN')])
Paul Kehrer2ca2eb32015-05-03 10:04:57 -0500664 )
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):
Ian Cordasco82fc3762015-06-16 20:59:50 -05001051 name = x509.Name([x509.NameAttribute(x509.OID_COMMON_NAME, u'value1')])
Paul Kehrer31bdf792015-03-25 14:11:00 -05001052 gn = x509.DirectoryName(x509.Name([name]))
Ian Cordascoa908d692015-06-16 21:35:24 -05001053 if six.PY3:
1054 assert repr(gn) == (
1055 "<DirectoryName(value=<Name([<Name([<NameAttribute(oid=<Object"
1056 "Identifier(oid=2.5.4.3, name=commonName)>, value='value1')>])"
1057 ">])>)>"
1058 )
1059 else:
1060 assert repr(gn) == (
1061 "<DirectoryName(value=<Name([<Name([<NameAttribute(oid=<Object"
1062 "Identifier(oid=2.5.4.3, name=commonName)>, value=u'value1')>]"
1063 ")>])>)>"
1064 )
Paul Kehrer31bdf792015-03-25 14:11:00 -05001065
1066 def test_eq(self):
1067 name = x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05001068 x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1')
Paul Kehrer31bdf792015-03-25 14:11:00 -05001069 ])
1070 name2 = x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05001071 x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1')
Paul Kehrer31bdf792015-03-25 14:11:00 -05001072 ])
1073 gn = x509.DirectoryName(x509.Name([name]))
1074 gn2 = x509.DirectoryName(x509.Name([name2]))
1075 assert gn == gn2
1076
1077 def test_ne(self):
1078 name = x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05001079 x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1')
Paul Kehrer31bdf792015-03-25 14:11:00 -05001080 ])
1081 name2 = x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05001082 x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value2')
Paul Kehrer31bdf792015-03-25 14:11:00 -05001083 ])
1084 gn = x509.DirectoryName(x509.Name([name]))
1085 gn2 = x509.DirectoryName(x509.Name([name2]))
1086 assert gn != gn2
1087 assert gn != object()
1088
1089
1090class TestRegisteredID(object):
1091 def test_not_oid(self):
1092 with pytest.raises(TypeError):
1093 x509.RegisteredID(b"notanoid")
1094
1095 with pytest.raises(TypeError):
1096 x509.RegisteredID(1.3)
1097
1098 def test_repr(self):
1099 gn = x509.RegisteredID(x509.OID_COMMON_NAME)
1100 assert repr(gn) == (
1101 "<RegisteredID(value=<ObjectIdentifier(oid=2.5.4.3, name=commonNam"
1102 "e)>)>"
1103 )
1104
1105 def test_eq(self):
1106 gn = x509.RegisteredID(x509.OID_COMMON_NAME)
1107 gn2 = x509.RegisteredID(x509.OID_COMMON_NAME)
1108 assert gn == gn2
1109
1110 def test_ne(self):
1111 gn = x509.RegisteredID(x509.OID_COMMON_NAME)
1112 gn2 = x509.RegisteredID(x509.OID_BASIC_CONSTRAINTS)
1113 assert gn != gn2
1114 assert gn != object()
1115
1116
1117class TestIPAddress(object):
1118 def test_not_ipaddress(self):
1119 with pytest.raises(TypeError):
1120 x509.IPAddress(b"notanipaddress")
1121
1122 with pytest.raises(TypeError):
1123 x509.IPAddress(1.3)
1124
1125 def test_repr(self):
Eeshan Gargf1234152015-04-29 18:41:00 +05301126 gn = x509.IPAddress(ipaddress.IPv4Address(u"127.0.0.1"))
Paul Kehrer31bdf792015-03-25 14:11:00 -05001127 assert repr(gn) == "<IPAddress(value=127.0.0.1)>"
1128
Eeshan Gargf1234152015-04-29 18:41:00 +05301129 gn2 = x509.IPAddress(ipaddress.IPv6Address(u"ff::"))
Paul Kehrer31bdf792015-03-25 14:11:00 -05001130 assert repr(gn2) == "<IPAddress(value=ff::)>"
1131
Paul Kehrereb177932015-05-17 18:33:33 -07001132 gn3 = x509.IPAddress(ipaddress.IPv4Network(u"192.168.0.0/24"))
1133 assert repr(gn3) == "<IPAddress(value=192.168.0.0/24)>"
1134
1135 gn4 = x509.IPAddress(ipaddress.IPv6Network(u"ff::/96"))
1136 assert repr(gn4) == "<IPAddress(value=ff::/96)>"
1137
Paul Kehrer31bdf792015-03-25 14:11:00 -05001138 def test_eq(self):
Eeshan Gargf1234152015-04-29 18:41:00 +05301139 gn = x509.IPAddress(ipaddress.IPv4Address(u"127.0.0.1"))
1140 gn2 = x509.IPAddress(ipaddress.IPv4Address(u"127.0.0.1"))
Paul Kehrer31bdf792015-03-25 14:11:00 -05001141 assert gn == gn2
1142
1143 def test_ne(self):
Eeshan Gargf1234152015-04-29 18:41:00 +05301144 gn = x509.IPAddress(ipaddress.IPv4Address(u"127.0.0.1"))
1145 gn2 = x509.IPAddress(ipaddress.IPv4Address(u"127.0.0.2"))
Paul Kehrer31bdf792015-03-25 14:11:00 -05001146 assert gn != gn2
1147 assert gn != object()
1148
1149
Erik Trauschke2dcce902015-05-14 16:12:24 -07001150class TestGeneralNames(object):
1151 def test_get_values_for_type(self):
1152 gns = x509.GeneralNames(
1153 [x509.DNSName(u"cryptography.io")]
1154 )
1155 names = gns.get_values_for_type(x509.DNSName)
1156 assert names == [u"cryptography.io"]
1157
1158 def test_iter_names(self):
1159 gns = x509.GeneralNames([
1160 x509.DNSName(u"cryptography.io"),
1161 x509.DNSName(u"crypto.local"),
1162 ])
1163 assert len(gns) == 2
1164 assert list(gns) == [
1165 x509.DNSName(u"cryptography.io"),
1166 x509.DNSName(u"crypto.local"),
1167 ]
1168
1169 def test_invalid_general_names(self):
1170 with pytest.raises(TypeError):
1171 x509.GeneralNames(
1172 [x509.DNSName(u"cryptography.io"), "invalid"]
1173 )
1174
1175 def test_repr(self):
1176 gns = x509.GeneralNames(
1177 [
1178 x509.DNSName(u"cryptography.io")
1179 ]
1180 )
1181 assert repr(gns) == (
1182 "<GeneralNames([<DNSName(value=cryptography.io)>])>"
1183 )
1184
1185 def test_eq(self):
1186 gns = x509.GeneralNames(
1187 [x509.DNSName(u"cryptography.io")]
1188 )
1189 gns2 = x509.GeneralNames(
1190 [x509.DNSName(u"cryptography.io")]
1191 )
1192 assert gns == gns2
1193
1194 def test_ne(self):
1195 gns = x509.GeneralNames(
1196 [x509.DNSName(u"cryptography.io")]
1197 )
1198 gns2 = x509.GeneralNames(
1199 [x509.RFC822Name(u"admin@cryptography.io")]
1200 )
1201 assert gns != gns2
1202 assert gns != object()
1203
1204
Paul Kehrer99125c92015-06-07 18:37:10 -05001205class TestIssuerAlternativeName(object):
1206 def test_get_values_for_type(self):
1207 san = x509.IssuerAlternativeName(
1208 [x509.DNSName(u"cryptography.io")]
1209 )
1210 names = san.get_values_for_type(x509.DNSName)
1211 assert names == [u"cryptography.io"]
1212
1213 def test_iter_names(self):
1214 san = x509.IssuerAlternativeName([
1215 x509.DNSName(u"cryptography.io"),
1216 x509.DNSName(u"crypto.local"),
1217 ])
1218 assert len(san) == 2
1219 assert list(san) == [
1220 x509.DNSName(u"cryptography.io"),
1221 x509.DNSName(u"crypto.local"),
1222 ]
1223
1224 def test_invalid_general_names(self):
1225 with pytest.raises(TypeError):
1226 x509.IssuerAlternativeName(
1227 [x509.DNSName(u"cryptography.io"), "invalid"]
1228 )
1229
1230 def test_repr(self):
1231 san = x509.IssuerAlternativeName(
1232 [
1233 x509.DNSName(u"cryptography.io")
1234 ]
1235 )
1236 assert repr(san) == (
1237 "<IssuerAlternativeName("
1238 "<GeneralNames([<DNSName(value=cryptography.io)>])>)>"
1239 )
1240
1241 def test_eq(self):
1242 san = x509.IssuerAlternativeName(
1243 [x509.DNSName(u"cryptography.io")]
1244 )
1245 san2 = x509.IssuerAlternativeName(
1246 [x509.DNSName(u"cryptography.io")]
1247 )
1248 assert san == san2
1249
1250 def test_ne(self):
1251 san = x509.IssuerAlternativeName(
1252 [x509.DNSName(u"cryptography.io")]
1253 )
1254 san2 = x509.IssuerAlternativeName(
1255 [x509.RFC822Name(u"admin@cryptography.io")]
1256 )
1257 assert san != san2
1258 assert san != object()
1259
1260
Alex Gaynorf1c17672015-06-20 14:20:20 -04001261@pytest.mark.requires_backend_interface(interface=RSABackend)
1262@pytest.mark.requires_backend_interface(interface=X509Backend)
1263class TestRSAIssuerAlternativeNameExtension(object):
1264 def test_uri(self, backend):
1265 cert = _load_cert(
1266 os.path.join("x509", "custom", "ian_uri.pem"),
1267 x509.load_pem_x509_certificate,
1268 backend,
1269 )
1270 ext = cert.extensions.get_extension_for_oid(
1271 x509.OID_ISSUER_ALTERNATIVE_NAME
1272 )
1273 assert list(ext.value) == [
1274 x509.UniformResourceIdentifier(u"http://path.to.root/root.crt"),
1275 ]
1276
1277
Paul Kehrer31bdf792015-03-25 14:11:00 -05001278class TestSubjectAlternativeName(object):
1279 def test_get_values_for_type(self):
1280 san = x509.SubjectAlternativeName(
Eeshan Gargf1234152015-04-29 18:41:00 +05301281 [x509.DNSName(u"cryptography.io")]
Paul Kehrer31bdf792015-03-25 14:11:00 -05001282 )
1283 names = san.get_values_for_type(x509.DNSName)
Eeshan Gargf1234152015-04-29 18:41:00 +05301284 assert names == [u"cryptography.io"]
Paul Kehrer31bdf792015-03-25 14:11:00 -05001285
1286 def test_iter_names(self):
1287 san = x509.SubjectAlternativeName([
Eeshan Gargf1234152015-04-29 18:41:00 +05301288 x509.DNSName(u"cryptography.io"),
1289 x509.DNSName(u"crypto.local"),
Paul Kehrer31bdf792015-03-25 14:11:00 -05001290 ])
1291 assert len(san) == 2
1292 assert list(san) == [
Eeshan Gargf1234152015-04-29 18:41:00 +05301293 x509.DNSName(u"cryptography.io"),
1294 x509.DNSName(u"crypto.local"),
Paul Kehrer31bdf792015-03-25 14:11:00 -05001295 ]
1296
Paul Kehrerd04b39b2015-04-21 08:44:17 -05001297 def test_invalid_general_names(self):
1298 with pytest.raises(TypeError):
1299 x509.SubjectAlternativeName(
Eeshan Gargf1234152015-04-29 18:41:00 +05301300 [x509.DNSName(u"cryptography.io"), "invalid"]
Paul Kehrerd04b39b2015-04-21 08:44:17 -05001301 )
1302
Paul Kehrer31bdf792015-03-25 14:11:00 -05001303 def test_repr(self):
1304 san = x509.SubjectAlternativeName(
1305 [
Eeshan Gargf1234152015-04-29 18:41:00 +05301306 x509.DNSName(u"cryptography.io")
Paul Kehrer31bdf792015-03-25 14:11:00 -05001307 ]
1308 )
1309 assert repr(san) == (
Erik Trauschke2dcce902015-05-14 16:12:24 -07001310 "<SubjectAlternativeName("
1311 "<GeneralNames([<DNSName(value=cryptography.io)>])>)>"
Paul Kehrer31bdf792015-03-25 14:11:00 -05001312 )
Paul Kehrer40f83382015-04-20 15:00:16 -05001313
Paul Kehrer58cc3972015-05-13 10:00:41 -05001314 def test_eq(self):
1315 san = x509.SubjectAlternativeName(
1316 [x509.DNSName(u"cryptography.io")]
1317 )
1318 san2 = x509.SubjectAlternativeName(
1319 [x509.DNSName(u"cryptography.io")]
1320 )
1321 assert san == san2
1322
1323 def test_ne(self):
1324 san = x509.SubjectAlternativeName(
1325 [x509.DNSName(u"cryptography.io")]
1326 )
1327 san2 = x509.SubjectAlternativeName(
1328 [x509.RFC822Name(u"admin@cryptography.io")]
1329 )
1330 assert san != san2
1331 assert san != object()
1332
Paul Kehrer40f83382015-04-20 15:00:16 -05001333
1334@pytest.mark.requires_backend_interface(interface=RSABackend)
1335@pytest.mark.requires_backend_interface(interface=X509Backend)
1336class TestRSASubjectAlternativeNameExtension(object):
1337 def test_dns_name(self, backend):
1338 cert = _load_cert(
1339 os.path.join("x509", "cryptography.io.pem"),
1340 x509.load_pem_x509_certificate,
1341 backend
1342 )
1343 ext = cert.extensions.get_extension_for_oid(
1344 x509.OID_SUBJECT_ALTERNATIVE_NAME
1345 )
1346 assert ext is not None
1347 assert ext.critical is False
1348
1349 san = ext.value
1350
1351 dns = san.get_values_for_type(x509.DNSName)
1352 assert dns == [u"www.cryptography.io", u"cryptography.io"]
Paul Kehrer9089c912015-04-20 22:15:20 -05001353
Paul Kehrer3c6bba52015-06-26 18:43:26 -05001354 def test_wildcard_dns_name(self, backend):
1355 cert = _load_cert(
1356 os.path.join("x509", "wildcard_san.pem"),
1357 x509.load_pem_x509_certificate,
1358 backend
1359 )
1360 ext = cert.extensions.get_extension_for_oid(
1361 x509.OID_SUBJECT_ALTERNATIVE_NAME
1362 )
1363
1364 dns = ext.value.get_values_for_type(x509.DNSName)
1365 assert dns == [
1366 u'*.langui.sh',
1367 u'langui.sh',
1368 u'*.saseliminator.com',
1369 u'saseliminator.com'
1370 ]
1371
1372 def test_san_wildcard_idna_dns_name(self, backend):
1373 cert = _load_cert(
1374 os.path.join("x509", "custom", "san_wildcard_idna.pem"),
1375 x509.load_pem_x509_certificate,
1376 backend
1377 )
1378 ext = cert.extensions.get_extension_for_oid(
1379 x509.OID_SUBJECT_ALTERNATIVE_NAME
1380 )
1381
1382 dns = ext.value.get_values_for_type(x509.DNSName)
1383 assert dns == [u'*.\u043f\u044b\u043a\u0430.cryptography']
1384
Paul Kehrer7bdf6572015-07-05 08:55:44 -05001385 def test_unsupported_gn(self, backend):
Paul Kehrer9089c912015-04-20 22:15:20 -05001386 cert = _load_cert(
Paul Kehrer7bdf6572015-07-05 08:55:44 -05001387 os.path.join("x509", "san_x400address.der"),
1388 x509.load_der_x509_certificate,
Paul Kehrer9089c912015-04-20 22:15:20 -05001389 backend
1390 )
Paul Kehrerbed07352015-04-21 08:31:10 -05001391 with pytest.raises(x509.UnsupportedGeneralNameType) as exc:
Paul Kehrer9089c912015-04-20 22:15:20 -05001392 cert.extensions
Paul Kehrerbed07352015-04-21 08:31:10 -05001393
Paul Kehrer7bdf6572015-07-05 08:55:44 -05001394 assert exc.value.type == 3
Paul Kehrer4db96622015-04-20 22:17:39 -05001395
1396 def test_registered_id(self, backend):
1397 cert = _load_cert(
1398 os.path.join(
1399 "x509", "custom", "san_registered_id.pem"
1400 ),
1401 x509.load_pem_x509_certificate,
1402 backend
1403 )
1404 ext = cert.extensions.get_extension_for_oid(
1405 x509.OID_SUBJECT_ALTERNATIVE_NAME
1406 )
1407 assert ext is not None
1408 assert ext.critical is False
1409
1410 san = ext.value
1411 rid = san.get_values_for_type(x509.RegisteredID)
1412 assert rid == [x509.ObjectIdentifier("1.2.3.4")]
Paul Kehrerb8ef82e2015-04-22 16:04:24 -05001413
1414 def test_uri(self, backend):
1415 cert = _load_cert(
1416 os.path.join(
1417 "x509", "custom", "san_uri_with_port.pem"
1418 ),
1419 x509.load_pem_x509_certificate,
1420 backend
1421 )
1422 ext = cert.extensions.get_extension_for_oid(
1423 x509.OID_SUBJECT_ALTERNATIVE_NAME
1424 )
1425 assert ext is not None
1426 uri = ext.value.get_values_for_type(
1427 x509.UniformResourceIdentifier
1428 )
1429 assert uri == [
1430 u"gopher://\u043f\u044b\u043a\u0430.cryptography:70/path?q=s#hel"
1431 u"lo",
1432 u"http://someregulardomain.com",
1433 ]
Paul Kehrera5f030c2015-04-28 08:33:18 -05001434
1435 def test_ipaddress(self, backend):
1436 cert = _load_cert(
1437 os.path.join(
1438 "x509", "custom", "san_ipaddr.pem"
1439 ),
1440 x509.load_pem_x509_certificate,
1441 backend
1442 )
1443 ext = cert.extensions.get_extension_for_oid(
1444 x509.OID_SUBJECT_ALTERNATIVE_NAME
1445 )
1446 assert ext is not None
1447 assert ext.critical is False
1448
1449 san = ext.value
1450
1451 ip = san.get_values_for_type(x509.IPAddress)
1452 assert [
1453 ipaddress.ip_address(u"127.0.0.1"),
1454 ipaddress.ip_address(u"ff::")
1455 ] == ip
Paul Kehrer2187a052015-04-30 08:22:07 -05001456
1457 def test_dirname(self, backend):
1458 cert = _load_cert(
1459 os.path.join(
1460 "x509", "custom", "san_dirname.pem"
1461 ),
1462 x509.load_pem_x509_certificate,
1463 backend
1464 )
1465 ext = cert.extensions.get_extension_for_oid(
1466 x509.OID_SUBJECT_ALTERNATIVE_NAME
1467 )
1468 assert ext is not None
1469 assert ext.critical is False
1470
1471 san = ext.value
1472
1473 dirname = san.get_values_for_type(x509.DirectoryName)
1474 assert [
1475 x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05001476 x509.NameAttribute(x509.OID_COMMON_NAME, u'test'),
1477 x509.NameAttribute(x509.OID_ORGANIZATION_NAME, u'Org'),
1478 x509.NameAttribute(x509.OID_STATE_OR_PROVINCE_NAME, u'Texas'),
Paul Kehrer2187a052015-04-30 08:22:07 -05001479 ])
1480 ] == dirname
Paul Kehrere06cab42015-04-30 10:23:33 -05001481
1482 def test_rfc822name(self, backend):
1483 cert = _load_cert(
1484 os.path.join(
1485 "x509", "custom", "san_rfc822_idna.pem"
1486 ),
1487 x509.load_pem_x509_certificate,
1488 backend
1489 )
1490 ext = cert.extensions.get_extension_for_oid(
1491 x509.OID_SUBJECT_ALTERNATIVE_NAME
1492 )
1493 assert ext is not None
1494 assert ext.critical is False
1495
1496 san = ext.value
1497
1498 rfc822name = san.get_values_for_type(x509.RFC822Name)
1499 assert [u"email@em\xe5\xefl.com"] == rfc822name
1500
Paul Kehrerb8968812015-05-15 09:01:34 -07001501 def test_idna2003_invalid(self, backend):
1502 cert = _load_cert(
1503 os.path.join(
1504 "x509", "custom", "san_idna2003_dnsname.pem"
1505 ),
1506 x509.load_pem_x509_certificate,
1507 backend
1508 )
1509 with pytest.raises(UnicodeError):
1510 cert.extensions
1511
Paul Kehrere06cab42015-04-30 10:23:33 -05001512 def test_unicode_rfc822_name_dns_name_uri(self, backend):
1513 cert = _load_cert(
1514 os.path.join(
1515 "x509", "custom", "san_idna_names.pem"
1516 ),
1517 x509.load_pem_x509_certificate,
1518 backend
1519 )
1520 ext = cert.extensions.get_extension_for_oid(
1521 x509.OID_SUBJECT_ALTERNATIVE_NAME
1522 )
1523 assert ext is not None
1524 rfc822_name = ext.value.get_values_for_type(x509.RFC822Name)
1525 dns_name = ext.value.get_values_for_type(x509.DNSName)
1526 uri = ext.value.get_values_for_type(x509.UniformResourceIdentifier)
1527 assert rfc822_name == [u"email@\u043f\u044b\u043a\u0430.cryptography"]
1528 assert dns_name == [u"\u043f\u044b\u043a\u0430.cryptography"]
1529 assert uri == [u"https://www.\u043f\u044b\u043a\u0430.cryptography"]
1530
1531 def test_rfc822name_dnsname_ipaddress_directoryname_uri(self, backend):
1532 cert = _load_cert(
1533 os.path.join(
1534 "x509", "custom", "san_email_dns_ip_dirname_uri.pem"
1535 ),
1536 x509.load_pem_x509_certificate,
1537 backend
1538 )
1539 ext = cert.extensions.get_extension_for_oid(
1540 x509.OID_SUBJECT_ALTERNATIVE_NAME
1541 )
1542 assert ext is not None
1543 assert ext.critical is False
1544
1545 san = ext.value
1546
1547 rfc822_name = san.get_values_for_type(x509.RFC822Name)
1548 uri = san.get_values_for_type(x509.UniformResourceIdentifier)
1549 dns = san.get_values_for_type(x509.DNSName)
1550 ip = san.get_values_for_type(x509.IPAddress)
1551 dirname = san.get_values_for_type(x509.DirectoryName)
1552 assert [u"user@cryptography.io"] == rfc822_name
Paul Kehrere3a330c2015-05-02 16:42:52 -05001553 assert [u"https://cryptography.io"] == uri
Paul Kehrere06cab42015-04-30 10:23:33 -05001554 assert [u"cryptography.io"] == dns
1555 assert [
1556 x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05001557 x509.NameAttribute(x509.OID_COMMON_NAME, u'dirCN'),
Paul Kehrere06cab42015-04-30 10:23:33 -05001558 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05001559 x509.OID_ORGANIZATION_NAME, u'Cryptographic Authority'
Paul Kehrere06cab42015-04-30 10:23:33 -05001560 ),
1561 ])
1562 ] == dirname
1563 assert [
1564 ipaddress.ip_address(u"127.0.0.1"),
1565 ipaddress.ip_address(u"ff::")
1566 ] == ip
1567
1568 def test_invalid_rfc822name(self, backend):
1569 cert = _load_cert(
1570 os.path.join(
1571 "x509", "custom", "san_rfc822_names.pem"
1572 ),
1573 x509.load_pem_x509_certificate,
1574 backend
1575 )
1576 with pytest.raises(ValueError) as exc:
1577 cert.extensions
1578
1579 assert 'Invalid rfc822name value' in str(exc.value)
Paul Kehrer94c69602015-05-02 19:29:40 -05001580
Joshua Tauberer2ee5e3c2015-07-04 20:09:46 +00001581 def test_other_name(self, backend):
1582 cert = _load_cert(
1583 os.path.join(
1584 "x509", "custom", "san_other_name.pem"
1585 ),
1586 x509.load_pem_x509_certificate,
1587 backend
1588 )
1589
1590 ext = cert.extensions.get_extension_for_oid(
1591 x509.OID_SUBJECT_ALTERNATIVE_NAME
1592 )
1593 assert ext is not None
1594 assert ext.critical is False
1595
1596 assert len(ext.value) == 1
1597 assert list(ext.value)[0] == \
1598 x509.OtherName(
1599 x509.ObjectIdentifier("1.2.3.4"),
1600 b'\x16\x0bHello World')
1601
1602 othernames = ext.value.get_values_for_type(x509.OtherName)
1603 assert othernames == [b'\x16\x0bHello World']
1604
Paul Kehrer94c69602015-05-02 19:29:40 -05001605
1606@pytest.mark.requires_backend_interface(interface=RSABackend)
1607@pytest.mark.requires_backend_interface(interface=X509Backend)
1608class TestExtendedKeyUsageExtension(object):
1609 def test_eku(self, backend):
1610 cert = _load_cert(
1611 os.path.join(
1612 "x509", "custom", "extended_key_usage.pem"
1613 ),
1614 x509.load_pem_x509_certificate,
1615 backend
1616 )
1617 ext = cert.extensions.get_extension_for_oid(
1618 x509.OID_EXTENDED_KEY_USAGE
1619 )
1620 assert ext is not None
1621 assert ext.critical is False
1622
1623 assert [
1624 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.1"),
1625 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.2"),
1626 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.3"),
1627 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.4"),
1628 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.9"),
1629 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.8"),
1630 x509.ObjectIdentifier("2.5.29.37.0"),
1631 x509.ObjectIdentifier("2.16.840.1.113730.4.1"),
1632 ] == list(ext.value)
Paul Kehrer3e6d5582015-05-02 21:57:56 -05001633
1634
1635class TestAccessDescription(object):
1636 def test_invalid_access_method(self):
Paul Kehrerf506bca2015-05-02 22:31:47 -05001637 with pytest.raises(ValueError):
Paul Kehrer3e6d5582015-05-02 21:57:56 -05001638 x509.AccessDescription("notanoid", x509.DNSName(u"test"))
1639
1640 def test_invalid_access_location(self):
1641 with pytest.raises(TypeError):
1642 x509.AccessDescription(x509.OID_CA_ISSUERS, "invalid")
1643
1644 def test_repr(self):
1645 ad = x509.AccessDescription(
1646 x509.OID_OCSP,
1647 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1648 )
1649 assert repr(ad) == (
1650 "<AccessDescription(access_method=<ObjectIdentifier(oid=1.3.6.1.5."
1651 "5.7.48.1, name=OCSP)>, access_location=<UniformResourceIdentifier"
1652 "(value=http://ocsp.domain.com)>)>"
1653 )
1654
1655 def test_eq(self):
1656 ad = x509.AccessDescription(
1657 x509.OID_OCSP,
1658 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1659 )
1660 ad2 = x509.AccessDescription(
1661 x509.OID_OCSP,
1662 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1663 )
1664 assert ad == ad2
1665
1666 def test_ne(self):
1667 ad = x509.AccessDescription(
1668 x509.OID_OCSP,
1669 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1670 )
1671 ad2 = x509.AccessDescription(
1672 x509.OID_CA_ISSUERS,
1673 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1674 )
1675 ad3 = x509.AccessDescription(
1676 x509.OID_OCSP,
1677 x509.UniformResourceIdentifier(u"http://notthesame")
1678 )
1679 assert ad != ad2
1680 assert ad != ad3
1681 assert ad != object()
1682
1683
1684class TestAuthorityInformationAccess(object):
1685 def test_invalid_descriptions(self):
1686 with pytest.raises(TypeError):
1687 x509.AuthorityInformationAccess(["notanAccessDescription"])
1688
1689 def test_iter_len(self):
1690 aia = x509.AuthorityInformationAccess([
1691 x509.AccessDescription(
1692 x509.OID_OCSP,
1693 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1694 ),
1695 x509.AccessDescription(
1696 x509.OID_CA_ISSUERS,
1697 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1698 )
1699 ])
1700 assert len(aia) == 2
1701 assert list(aia) == [
1702 x509.AccessDescription(
1703 x509.OID_OCSP,
1704 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1705 ),
1706 x509.AccessDescription(
1707 x509.OID_CA_ISSUERS,
1708 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1709 )
1710 ]
1711
1712 def test_repr(self):
1713 aia = x509.AuthorityInformationAccess([
1714 x509.AccessDescription(
1715 x509.OID_OCSP,
1716 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1717 ),
1718 x509.AccessDescription(
1719 x509.OID_CA_ISSUERS,
1720 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1721 )
1722 ])
1723 assert repr(aia) == (
1724 "<AuthorityInformationAccess([<AccessDescription(access_method=<Ob"
1725 "jectIdentifier(oid=1.3.6.1.5.5.7.48.1, name=OCSP)>, access_locati"
1726 "on=<UniformResourceIdentifier(value=http://ocsp.domain.com)>)>, <"
1727 "AccessDescription(access_method=<ObjectIdentifier(oid=1.3.6.1.5.5"
1728 ".7.48.2, name=caIssuers)>, access_location=<UniformResourceIdenti"
1729 "fier(value=http://domain.com/ca.crt)>)>])>"
1730 )
1731
1732 def test_eq(self):
1733 aia = x509.AuthorityInformationAccess([
1734 x509.AccessDescription(
1735 x509.OID_OCSP,
1736 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1737 ),
1738 x509.AccessDescription(
1739 x509.OID_CA_ISSUERS,
1740 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1741 )
1742 ])
1743 aia2 = x509.AuthorityInformationAccess([
1744 x509.AccessDescription(
1745 x509.OID_OCSP,
1746 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1747 ),
1748 x509.AccessDescription(
1749 x509.OID_CA_ISSUERS,
1750 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1751 )
1752 ])
1753 assert aia == aia2
1754
1755 def test_ne(self):
1756 aia = x509.AuthorityInformationAccess([
1757 x509.AccessDescription(
1758 x509.OID_OCSP,
1759 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1760 ),
1761 x509.AccessDescription(
1762 x509.OID_CA_ISSUERS,
1763 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1764 )
1765 ])
1766 aia2 = x509.AuthorityInformationAccess([
1767 x509.AccessDescription(
1768 x509.OID_OCSP,
1769 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1770 ),
1771 ])
1772
1773 assert aia != aia2
1774 assert aia != object()
Paul Kehrerd774de92015-05-03 10:52:25 -05001775
1776
1777@pytest.mark.requires_backend_interface(interface=RSABackend)
1778@pytest.mark.requires_backend_interface(interface=X509Backend)
Paul Kehrera1476992015-05-04 17:35:47 -05001779class TestAuthorityInformationAccessExtension(object):
1780 def test_aia_ocsp_ca_issuers(self, backend):
1781 cert = _load_cert(
1782 os.path.join("x509", "cryptography.io.pem"),
1783 x509.load_pem_x509_certificate,
1784 backend
1785 )
1786 ext = cert.extensions.get_extension_for_oid(
1787 x509.OID_AUTHORITY_INFORMATION_ACCESS
1788 )
1789 assert ext is not None
1790 assert ext.critical is False
1791
1792 assert ext.value == x509.AuthorityInformationAccess([
1793 x509.AccessDescription(
1794 x509.OID_OCSP,
1795 x509.UniformResourceIdentifier(u"http://gv.symcd.com")
1796 ),
1797 x509.AccessDescription(
1798 x509.OID_CA_ISSUERS,
1799 x509.UniformResourceIdentifier(u"http://gv.symcb.com/gv.crt")
1800 ),
1801 ])
1802
1803 def test_aia_multiple_ocsp_ca_issuers(self, backend):
1804 cert = _load_cert(
1805 os.path.join("x509", "custom", "aia_ocsp_ca_issuers.pem"),
1806 x509.load_pem_x509_certificate,
1807 backend
1808 )
1809 ext = cert.extensions.get_extension_for_oid(
1810 x509.OID_AUTHORITY_INFORMATION_ACCESS
1811 )
1812 assert ext is not None
1813 assert ext.critical is False
1814
1815 assert ext.value == x509.AuthorityInformationAccess([
1816 x509.AccessDescription(
1817 x509.OID_OCSP,
1818 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1819 ),
1820 x509.AccessDescription(
1821 x509.OID_OCSP,
1822 x509.UniformResourceIdentifier(u"http://ocsp2.domain.com")
1823 ),
1824 x509.AccessDescription(
1825 x509.OID_CA_ISSUERS,
1826 x509.DirectoryName(x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05001827 x509.NameAttribute(x509.OID_COMMON_NAME, u"myCN"),
1828 x509.NameAttribute(x509.OID_ORGANIZATION_NAME,
1829 u"some Org"),
Paul Kehrera1476992015-05-04 17:35:47 -05001830 ]))
1831 ),
1832 ])
1833
1834 def test_aia_ocsp_only(self, backend):
1835 cert = _load_cert(
1836 os.path.join("x509", "custom", "aia_ocsp.pem"),
1837 x509.load_pem_x509_certificate,
1838 backend
1839 )
1840 ext = cert.extensions.get_extension_for_oid(
1841 x509.OID_AUTHORITY_INFORMATION_ACCESS
1842 )
1843 assert ext is not None
1844 assert ext.critical is False
1845
1846 assert ext.value == x509.AuthorityInformationAccess([
1847 x509.AccessDescription(
1848 x509.OID_OCSP,
1849 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1850 ),
1851 ])
1852
1853 def test_aia_ca_issuers_only(self, backend):
1854 cert = _load_cert(
1855 os.path.join("x509", "custom", "aia_ca_issuers.pem"),
1856 x509.load_pem_x509_certificate,
1857 backend
1858 )
1859 ext = cert.extensions.get_extension_for_oid(
1860 x509.OID_AUTHORITY_INFORMATION_ACCESS
1861 )
1862 assert ext is not None
1863 assert ext.critical is False
1864
1865 assert ext.value == x509.AuthorityInformationAccess([
1866 x509.AccessDescription(
1867 x509.OID_CA_ISSUERS,
1868 x509.DirectoryName(x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05001869 x509.NameAttribute(x509.OID_COMMON_NAME, u"myCN"),
1870 x509.NameAttribute(x509.OID_ORGANIZATION_NAME,
1871 u"some Org"),
Paul Kehrera1476992015-05-04 17:35:47 -05001872 ]))
1873 ),
1874 ])
1875
1876
1877@pytest.mark.requires_backend_interface(interface=RSABackend)
1878@pytest.mark.requires_backend_interface(interface=X509Backend)
Paul Kehrerd774de92015-05-03 10:52:25 -05001879class TestAuthorityKeyIdentifierExtension(object):
1880 def test_aki_keyid(self, backend):
1881 cert = _load_cert(
1882 os.path.join(
1883 "x509", "cryptography.io.pem"
1884 ),
1885 x509.load_pem_x509_certificate,
1886 backend
1887 )
1888 ext = cert.extensions.get_extension_for_oid(
1889 x509.OID_AUTHORITY_KEY_IDENTIFIER
1890 )
1891 assert ext is not None
1892 assert ext.critical is False
1893
1894 assert ext.value.key_identifier == (
1895 b"\xc3\x9c\xf3\xfc\xd3F\x084\xbb\xceF\x7f\xa0|[\xf3\xe2\x08\xcbY"
1896 )
1897 assert ext.value.authority_cert_issuer is None
1898 assert ext.value.authority_cert_serial_number is None
1899
1900 def test_aki_all_fields(self, backend):
1901 cert = _load_cert(
1902 os.path.join(
1903 "x509", "custom", "authority_key_identifier.pem"
1904 ),
1905 x509.load_pem_x509_certificate,
1906 backend
1907 )
1908 ext = cert.extensions.get_extension_for_oid(
1909 x509.OID_AUTHORITY_KEY_IDENTIFIER
1910 )
1911 assert ext is not None
1912 assert ext.critical is False
1913
1914 assert ext.value.key_identifier == (
1915 b"9E>\xca=b\x1d\xea\x86I\xf6Z\xab@\xb7\xa4p\x98\xf1\xec"
1916 )
1917 assert ext.value.authority_cert_issuer == [
1918 x509.DirectoryName(
1919 x509.Name([
1920 x509.NameAttribute(
1921 x509.OID_ORGANIZATION_NAME, u"PyCA"
1922 ),
1923 x509.NameAttribute(
1924 x509.OID_COMMON_NAME, u"cryptography.io"
1925 )
1926 ])
1927 )
1928 ]
1929 assert ext.value.authority_cert_serial_number == 3
1930
1931 def test_aki_no_keyid(self, backend):
1932 cert = _load_cert(
1933 os.path.join(
1934 "x509", "custom", "authority_key_identifier_no_keyid.pem"
1935 ),
1936 x509.load_pem_x509_certificate,
1937 backend
1938 )
1939 ext = cert.extensions.get_extension_for_oid(
1940 x509.OID_AUTHORITY_KEY_IDENTIFIER
1941 )
1942 assert ext is not None
1943 assert ext.critical is False
1944
1945 assert ext.value.key_identifier is None
1946 assert ext.value.authority_cert_issuer == [
1947 x509.DirectoryName(
1948 x509.Name([
1949 x509.NameAttribute(
1950 x509.OID_ORGANIZATION_NAME, u"PyCA"
1951 ),
1952 x509.NameAttribute(
1953 x509.OID_COMMON_NAME, u"cryptography.io"
1954 )
1955 ])
1956 )
1957 ]
1958 assert ext.value.authority_cert_serial_number == 3
Paul Kehrer5a485522015-05-06 00:29:12 -05001959
1960
Paul Kehrere0017be2015-05-17 20:39:40 -06001961class TestNameConstraints(object):
1962 def test_ipaddress_wrong_type(self):
1963 with pytest.raises(TypeError):
1964 x509.NameConstraints(
1965 permitted_subtrees=[
1966 x509.IPAddress(ipaddress.IPv4Address(u"127.0.0.1"))
1967 ],
1968 excluded_subtrees=None
1969 )
1970
1971 with pytest.raises(TypeError):
1972 x509.NameConstraints(
1973 permitted_subtrees=None,
1974 excluded_subtrees=[
1975 x509.IPAddress(ipaddress.IPv4Address(u"127.0.0.1"))
1976 ]
1977 )
1978
1979 def test_ipaddress_allowed_type(self):
1980 permitted = [x509.IPAddress(ipaddress.IPv4Network(u"192.168.0.0/29"))]
1981 excluded = [x509.IPAddress(ipaddress.IPv4Network(u"10.10.0.0/24"))]
1982 nc = x509.NameConstraints(
1983 permitted_subtrees=permitted,
1984 excluded_subtrees=excluded
1985 )
1986 assert nc.permitted_subtrees == permitted
1987 assert nc.excluded_subtrees == excluded
1988
1989 def test_invalid_permitted_subtrees(self):
1990 with pytest.raises(TypeError):
1991 x509.NameConstraints("badpermitted", None)
1992
1993 def test_invalid_excluded_subtrees(self):
1994 with pytest.raises(TypeError):
1995 x509.NameConstraints(None, "badexcluded")
1996
1997 def test_no_subtrees(self):
1998 with pytest.raises(ValueError):
1999 x509.NameConstraints(None, None)
2000
2001 def test_permitted_none(self):
2002 excluded = [x509.DNSName(u"name.local")]
2003 nc = x509.NameConstraints(
2004 permitted_subtrees=None, excluded_subtrees=excluded
2005 )
2006 assert nc.permitted_subtrees is None
2007 assert nc.excluded_subtrees is not None
2008
2009 def test_excluded_none(self):
2010 permitted = [x509.DNSName(u"name.local")]
2011 nc = x509.NameConstraints(
2012 permitted_subtrees=permitted, excluded_subtrees=None
2013 )
2014 assert nc.permitted_subtrees is not None
2015 assert nc.excluded_subtrees is None
2016
2017 def test_repr(self):
2018 permitted = [x509.DNSName(u"name.local"), x509.DNSName(u"name2.local")]
2019 nc = x509.NameConstraints(
2020 permitted_subtrees=permitted,
2021 excluded_subtrees=None
2022 )
2023 assert repr(nc) == (
2024 "<NameConstraints(permitted_subtrees=[<DNSName(value=name.local)>"
2025 ", <DNSName(value=name2.local)>], excluded_subtrees=None)>"
2026 )
2027
Paul Kehrer31894282015-06-21 21:46:41 -05002028 def test_eq(self):
2029 nc = x509.NameConstraints(
2030 permitted_subtrees=[x509.DNSName(u"name.local")],
2031 excluded_subtrees=[x509.DNSName(u"name2.local")]
2032 )
2033 nc2 = x509.NameConstraints(
2034 permitted_subtrees=[x509.DNSName(u"name.local")],
2035 excluded_subtrees=[x509.DNSName(u"name2.local")]
2036 )
2037 assert nc == nc2
2038
2039 def test_ne(self):
2040 nc = x509.NameConstraints(
2041 permitted_subtrees=[x509.DNSName(u"name.local")],
2042 excluded_subtrees=[x509.DNSName(u"name2.local")]
2043 )
2044 nc2 = x509.NameConstraints(
2045 permitted_subtrees=[x509.DNSName(u"name.local")],
2046 excluded_subtrees=None
2047 )
2048 nc3 = x509.NameConstraints(
2049 permitted_subtrees=None,
2050 excluded_subtrees=[x509.DNSName(u"name2.local")]
2051 )
2052
2053 assert nc != nc2
2054 assert nc != nc3
2055 assert nc != object()
2056
Paul Kehrere0017be2015-05-17 20:39:40 -06002057
Paul Kehrer870d7e82015-06-21 22:20:44 -05002058@pytest.mark.requires_backend_interface(interface=RSABackend)
2059@pytest.mark.requires_backend_interface(interface=X509Backend)
2060class TestNameConstraintsExtension(object):
2061 def test_permitted_excluded(self, backend):
2062 cert = _load_cert(
2063 os.path.join(
2064 "x509", "custom", "nc_permitted_excluded_2.pem"
2065 ),
2066 x509.load_pem_x509_certificate,
2067 backend
2068 )
2069 nc = cert.extensions.get_extension_for_oid(
2070 x509.OID_NAME_CONSTRAINTS
2071 ).value
2072 assert nc == x509.NameConstraints(
2073 permitted_subtrees=[
2074 x509.DNSName(u"zombo.local"),
2075 ],
2076 excluded_subtrees=[
2077 x509.DirectoryName(x509.Name([
2078 x509.NameAttribute(x509.OID_COMMON_NAME, u"zombo")
2079 ]))
2080 ]
2081 )
2082
2083 def test_permitted(self, backend):
2084 cert = _load_cert(
2085 os.path.join(
2086 "x509", "custom", "nc_permitted_2.pem"
2087 ),
2088 x509.load_pem_x509_certificate,
2089 backend
2090 )
2091 nc = cert.extensions.get_extension_for_oid(
2092 x509.OID_NAME_CONSTRAINTS
2093 ).value
2094 assert nc == x509.NameConstraints(
2095 permitted_subtrees=[
2096 x509.DNSName(u"zombo.local"),
2097 ],
2098 excluded_subtrees=None
2099 )
2100
Paul Kehrer42376832015-07-01 18:10:32 -05002101 def test_permitted_with_leading_period(self, backend):
2102 cert = _load_cert(
2103 os.path.join(
2104 "x509", "custom", "nc_permitted.pem"
2105 ),
2106 x509.load_pem_x509_certificate,
2107 backend
2108 )
2109 nc = cert.extensions.get_extension_for_oid(
2110 x509.OID_NAME_CONSTRAINTS
2111 ).value
2112 assert nc == x509.NameConstraints(
2113 permitted_subtrees=[
2114 x509.DNSName(u".cryptography.io"),
2115 x509.UniformResourceIdentifier(u"ftp://cryptography.test")
2116 ],
2117 excluded_subtrees=None
2118 )
2119
2120 def test_excluded_with_leading_period(self, backend):
2121 cert = _load_cert(
2122 os.path.join(
2123 "x509", "custom", "nc_excluded.pem"
2124 ),
2125 x509.load_pem_x509_certificate,
2126 backend
2127 )
2128 nc = cert.extensions.get_extension_for_oid(
2129 x509.OID_NAME_CONSTRAINTS
2130 ).value
2131 assert nc == x509.NameConstraints(
2132 permitted_subtrees=None,
2133 excluded_subtrees=[
2134 x509.DNSName(u".cryptography.io"),
2135 x509.UniformResourceIdentifier(u"gopher://cryptography.test")
2136 ]
2137 )
2138
Paul Kehrer870d7e82015-06-21 22:20:44 -05002139
Paul Kehrer5a485522015-05-06 00:29:12 -05002140class TestDistributionPoint(object):
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002141 def test_distribution_point_full_name_not_general_names(self):
Paul Kehrer5a485522015-05-06 00:29:12 -05002142 with pytest.raises(TypeError):
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002143 x509.DistributionPoint(["notgn"], None, None, None)
Paul Kehrer5a485522015-05-06 00:29:12 -05002144
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002145 def test_distribution_point_relative_name_not_name(self):
Paul Kehrer5a485522015-05-06 00:29:12 -05002146 with pytest.raises(TypeError):
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002147 x509.DistributionPoint(None, "notname", None, None)
2148
2149 def test_distribution_point_full_and_relative_not_none(self):
2150 with pytest.raises(ValueError):
2151 x509.DistributionPoint("data", "notname", None, None)
Paul Kehrer5a485522015-05-06 00:29:12 -05002152
2153 def test_crl_issuer_not_general_names(self):
2154 with pytest.raises(TypeError):
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002155 x509.DistributionPoint(None, None, None, ["notgn"])
Paul Kehrer5a485522015-05-06 00:29:12 -05002156
2157 def test_reason_not_reasonflags(self):
2158 with pytest.raises(TypeError):
2159 x509.DistributionPoint(
2160 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002161 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002162 frozenset(["notreasonflags"]),
2163 None
2164 )
2165
2166 def test_reason_not_frozenset(self):
2167 with pytest.raises(TypeError):
2168 x509.DistributionPoint(
2169 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
2170 None,
2171 [x509.ReasonFlags.ca_compromise],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002172 None
2173 )
2174
2175 def test_disallowed_reasons(self):
2176 with pytest.raises(ValueError):
2177 x509.DistributionPoint(
2178 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
2179 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002180 frozenset([x509.ReasonFlags.unspecified]),
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002181 None
2182 )
2183
2184 with pytest.raises(ValueError):
2185 x509.DistributionPoint(
2186 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
2187 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002188 frozenset([x509.ReasonFlags.remove_from_crl]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002189 None
2190 )
2191
2192 def test_reason_only(self):
2193 with pytest.raises(ValueError):
2194 x509.DistributionPoint(
2195 None,
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002196 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002197 frozenset([x509.ReasonFlags.aa_compromise]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002198 None
2199 )
2200
2201 def test_eq(self):
2202 dp = x509.DistributionPoint(
2203 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002204 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002205 frozenset([x509.ReasonFlags.superseded]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002206 [
2207 x509.DirectoryName(
2208 x509.Name([
2209 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05002210 x509.OID_COMMON_NAME, u"Important CA"
Paul Kehrer5a485522015-05-06 00:29:12 -05002211 )
2212 ])
2213 )
2214 ],
2215 )
2216 dp2 = x509.DistributionPoint(
2217 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002218 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002219 frozenset([x509.ReasonFlags.superseded]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002220 [
2221 x509.DirectoryName(
2222 x509.Name([
2223 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05002224 x509.OID_COMMON_NAME, u"Important CA"
Paul Kehrer5a485522015-05-06 00:29:12 -05002225 )
2226 ])
2227 )
2228 ],
2229 )
2230 assert dp == dp2
2231
2232 def test_ne(self):
2233 dp = x509.DistributionPoint(
2234 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002235 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002236 frozenset([x509.ReasonFlags.superseded]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002237 [
2238 x509.DirectoryName(
2239 x509.Name([
2240 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05002241 x509.OID_COMMON_NAME, u"Important CA"
Paul Kehrer5a485522015-05-06 00:29:12 -05002242 )
2243 ])
2244 )
2245 ],
2246 )
2247 dp2 = x509.DistributionPoint(
2248 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
2249 None,
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002250 None,
Paul Kehrer5a485522015-05-06 00:29:12 -05002251 None
2252 )
2253 assert dp != dp2
2254 assert dp != object()
2255
2256 def test_repr(self):
2257 dp = x509.DistributionPoint(
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002258 None,
Paul Kehrer5a485522015-05-06 00:29:12 -05002259 x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05002260 x509.NameAttribute(x509.OID_COMMON_NAME, u"myCN")
Paul Kehrer5a485522015-05-06 00:29:12 -05002261 ]),
Paul Kehrer96ef63c2015-05-09 21:17:04 -05002262 frozenset([x509.ReasonFlags.ca_compromise]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002263 [
2264 x509.DirectoryName(
2265 x509.Name([
2266 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05002267 x509.OID_COMMON_NAME, u"Important CA"
Paul Kehrer5a485522015-05-06 00:29:12 -05002268 )
2269 ])
2270 )
2271 ],
2272 )
Paul Kehrer749da3b2015-05-10 09:58:29 -05002273 if six.PY3:
2274 assert repr(dp) == (
2275 "<DistributionPoint(full_name=None, relative_name=<Name([<Name"
2276 "Attribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName)"
Ian Cordascoa908d692015-06-16 21:35:24 -05002277 ">, value='myCN')>])>, reasons=frozenset({<ReasonFlags.ca_comp"
2278 "romise: 'cACompromise'>}), crl_issuer=[<DirectoryName(value=<"
2279 "Name([<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name="
2280 "commonName)>, value='Important CA')>])>)>])>"
Paul Kehrer749da3b2015-05-10 09:58:29 -05002281 )
2282 else:
2283 assert repr(dp) == (
2284 "<DistributionPoint(full_name=None, relative_name=<Name([<Name"
2285 "Attribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName)"
Ian Cordasco82fc3762015-06-16 20:59:50 -05002286 ">, value=u'myCN')>])>, reasons=frozenset([<ReasonFlags.ca_com"
2287 "promise: 'cACompromise'>]), crl_issuer=[<DirectoryName(value="
2288 "<Name([<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name"
2289 "=commonName)>, value=u'Important CA')>])>)>])>"
Paul Kehrer749da3b2015-05-10 09:58:29 -05002290 )
Paul Kehrer5a485522015-05-06 00:29:12 -05002291
2292
2293class TestCRLDistributionPoints(object):
2294 def test_invalid_distribution_points(self):
2295 with pytest.raises(TypeError):
2296 x509.CRLDistributionPoints(["notadistributionpoint"])
2297
2298 def test_iter_len(self):
2299 cdp = x509.CRLDistributionPoints([
2300 x509.DistributionPoint(
2301 [x509.UniformResourceIdentifier(u"http://domain")],
2302 None,
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002303 None,
Paul Kehrer5a485522015-05-06 00:29:12 -05002304 None
2305 ),
2306 x509.DistributionPoint(
2307 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002308 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002309 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002310 x509.ReasonFlags.key_compromise,
2311 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002312 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002313 None
2314 ),
2315 ])
2316 assert len(cdp) == 2
2317 assert list(cdp) == [
2318 x509.DistributionPoint(
2319 [x509.UniformResourceIdentifier(u"http://domain")],
2320 None,
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002321 None,
Paul Kehrer5a485522015-05-06 00:29:12 -05002322 None
2323 ),
2324 x509.DistributionPoint(
2325 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002326 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002327 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002328 x509.ReasonFlags.key_compromise,
2329 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002330 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002331 None
2332 ),
2333 ]
2334
2335 def test_repr(self):
2336 cdp = x509.CRLDistributionPoints([
2337 x509.DistributionPoint(
2338 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002339 None,
Paul Kehrer96ef63c2015-05-09 21:17:04 -05002340 frozenset([x509.ReasonFlags.key_compromise]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002341 None
2342 ),
2343 ])
Paul Kehrer749da3b2015-05-10 09:58:29 -05002344 if six.PY3:
2345 assert repr(cdp) == (
2346 "<CRLDistributionPoints([<DistributionPoint(full_name=[<Unifo"
2347 "rmResourceIdentifier(value=ftp://domain)>], relative_name=No"
2348 "ne, reasons=frozenset({<ReasonFlags.key_compromise: 'keyComp"
2349 "romise'>}), crl_issuer=None)>])>"
2350 )
2351 else:
2352 assert repr(cdp) == (
2353 "<CRLDistributionPoints([<DistributionPoint(full_name=[<Unifo"
2354 "rmResourceIdentifier(value=ftp://domain)>], relative_name=No"
2355 "ne, reasons=frozenset([<ReasonFlags.key_compromise: 'keyComp"
2356 "romise'>]), crl_issuer=None)>])>"
2357 )
Paul Kehrer5a485522015-05-06 00:29:12 -05002358
2359 def test_eq(self):
2360 cdp = x509.CRLDistributionPoints([
2361 x509.DistributionPoint(
2362 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002363 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002364 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002365 x509.ReasonFlags.key_compromise,
2366 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002367 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002368 [x509.UniformResourceIdentifier(u"uri://thing")],
2369 ),
2370 ])
2371 cdp2 = x509.CRLDistributionPoints([
2372 x509.DistributionPoint(
2373 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002374 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002375 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002376 x509.ReasonFlags.key_compromise,
2377 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002378 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002379 [x509.UniformResourceIdentifier(u"uri://thing")],
2380 ),
2381 ])
2382 assert cdp == cdp2
2383
2384 def test_ne(self):
2385 cdp = x509.CRLDistributionPoints([
2386 x509.DistributionPoint(
2387 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002388 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002389 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002390 x509.ReasonFlags.key_compromise,
2391 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002392 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002393 [x509.UniformResourceIdentifier(u"uri://thing")],
2394 ),
2395 ])
2396 cdp2 = x509.CRLDistributionPoints([
2397 x509.DistributionPoint(
2398 [x509.UniformResourceIdentifier(u"ftp://domain2")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002399 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002400 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002401 x509.ReasonFlags.key_compromise,
2402 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002403 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002404 [x509.UniformResourceIdentifier(u"uri://thing")],
2405 ),
2406 ])
2407 cdp3 = x509.CRLDistributionPoints([
2408 x509.DistributionPoint(
2409 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002410 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002411 frozenset([x509.ReasonFlags.key_compromise]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002412 [x509.UniformResourceIdentifier(u"uri://thing")],
2413 ),
2414 ])
2415 cdp4 = x509.CRLDistributionPoints([
2416 x509.DistributionPoint(
2417 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002418 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002419 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002420 x509.ReasonFlags.key_compromise,
2421 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002422 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002423 [x509.UniformResourceIdentifier(u"uri://thing2")],
2424 ),
2425 ])
2426 assert cdp != cdp2
2427 assert cdp != cdp3
2428 assert cdp != cdp4
2429 assert cdp != object()
Paul Kehrer9a10d592015-05-10 14:55:51 -05002430
2431
2432@pytest.mark.requires_backend_interface(interface=RSABackend)
2433@pytest.mark.requires_backend_interface(interface=X509Backend)
2434class TestCRLDistributionPointsExtension(object):
2435 def test_fullname_and_crl_issuer(self, backend):
2436 cert = _load_cert(
2437 os.path.join(
2438 "x509", "PKITS_data", "certs", "ValidcRLIssuerTest28EE.crt"
2439 ),
2440 x509.load_der_x509_certificate,
2441 backend
2442 )
2443
2444 cdps = cert.extensions.get_extension_for_oid(
2445 x509.OID_CRL_DISTRIBUTION_POINTS
2446 ).value
2447
2448 assert cdps == x509.CRLDistributionPoints([
2449 x509.DistributionPoint(
2450 full_name=[x509.DirectoryName(
2451 x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05002452 x509.NameAttribute(x509.OID_COUNTRY_NAME, u"US"),
Paul Kehrer9a10d592015-05-10 14:55:51 -05002453 x509.NameAttribute(
2454 x509.OID_ORGANIZATION_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002455 u"Test Certificates 2011"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002456 ),
2457 x509.NameAttribute(
2458 x509.OID_ORGANIZATIONAL_UNIT_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002459 u"indirectCRL CA3 cRLIssuer"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002460 ),
2461 x509.NameAttribute(
2462 x509.OID_COMMON_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002463 u"indirect CRL for indirectCRL CA3"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002464 ),
2465 ])
2466 )],
2467 relative_name=None,
2468 reasons=None,
2469 crl_issuer=[x509.DirectoryName(
2470 x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05002471 x509.NameAttribute(x509.OID_COUNTRY_NAME, u"US"),
Paul Kehrer9a10d592015-05-10 14:55:51 -05002472 x509.NameAttribute(
2473 x509.OID_ORGANIZATION_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002474 u"Test Certificates 2011"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002475 ),
2476 x509.NameAttribute(
2477 x509.OID_ORGANIZATIONAL_UNIT_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002478 u"indirectCRL CA3 cRLIssuer"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002479 ),
2480 ])
2481 )],
2482 )
2483 ])
2484
2485 def test_relativename_and_crl_issuer(self, backend):
2486 cert = _load_cert(
2487 os.path.join(
2488 "x509", "PKITS_data", "certs", "ValidcRLIssuerTest29EE.crt"
2489 ),
2490 x509.load_der_x509_certificate,
2491 backend
2492 )
2493
2494 cdps = cert.extensions.get_extension_for_oid(
2495 x509.OID_CRL_DISTRIBUTION_POINTS
2496 ).value
2497
2498 assert cdps == x509.CRLDistributionPoints([
2499 x509.DistributionPoint(
2500 full_name=None,
2501 relative_name=x509.Name([
2502 x509.NameAttribute(
2503 x509.OID_COMMON_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002504 u"indirect CRL for indirectCRL CA3"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002505 ),
2506 ]),
2507 reasons=None,
2508 crl_issuer=[x509.DirectoryName(
2509 x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05002510 x509.NameAttribute(x509.OID_COUNTRY_NAME, u"US"),
Paul Kehrer9a10d592015-05-10 14:55:51 -05002511 x509.NameAttribute(
2512 x509.OID_ORGANIZATION_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002513 u"Test Certificates 2011"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002514 ),
2515 x509.NameAttribute(
2516 x509.OID_ORGANIZATIONAL_UNIT_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002517 u"indirectCRL CA3 cRLIssuer"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002518 ),
2519 ])
2520 )],
2521 )
2522 ])
2523
2524 def test_fullname_crl_issuer_reasons(self, backend):
2525 cert = _load_cert(
2526 os.path.join(
2527 "x509", "custom", "cdp_fullname_reasons_crl_issuer.pem"
2528 ),
2529 x509.load_pem_x509_certificate,
2530 backend
2531 )
2532
2533 cdps = cert.extensions.get_extension_for_oid(
2534 x509.OID_CRL_DISTRIBUTION_POINTS
2535 ).value
2536
2537 assert cdps == x509.CRLDistributionPoints([
2538 x509.DistributionPoint(
2539 full_name=[x509.UniformResourceIdentifier(
2540 u"http://myhost.com/myca.crl"
2541 )],
2542 relative_name=None,
2543 reasons=frozenset([
2544 x509.ReasonFlags.key_compromise,
2545 x509.ReasonFlags.ca_compromise
2546 ]),
2547 crl_issuer=[x509.DirectoryName(
2548 x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05002549 x509.NameAttribute(x509.OID_COUNTRY_NAME, u"US"),
Paul Kehrer9a10d592015-05-10 14:55:51 -05002550 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05002551 x509.OID_ORGANIZATION_NAME, u"PyCA"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002552 ),
2553 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05002554 x509.OID_COMMON_NAME, u"cryptography CA"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002555 ),
2556 ])
2557 )],
2558 )
2559 ])
2560
Paul Kehrer594a2ed2015-05-12 23:27:32 -05002561 def test_all_reasons(self, backend):
2562 cert = _load_cert(
2563 os.path.join(
2564 "x509", "custom", "cdp_all_reasons.pem"
2565 ),
2566 x509.load_pem_x509_certificate,
2567 backend
2568 )
2569
2570 cdps = cert.extensions.get_extension_for_oid(
2571 x509.OID_CRL_DISTRIBUTION_POINTS
2572 ).value
2573
2574 assert cdps == x509.CRLDistributionPoints([
2575 x509.DistributionPoint(
2576 full_name=[x509.UniformResourceIdentifier(
2577 u"http://domain.com/some.crl"
2578 )],
2579 relative_name=None,
2580 reasons=frozenset([
2581 x509.ReasonFlags.key_compromise,
2582 x509.ReasonFlags.ca_compromise,
2583 x509.ReasonFlags.affiliation_changed,
2584 x509.ReasonFlags.superseded,
2585 x509.ReasonFlags.privilege_withdrawn,
2586 x509.ReasonFlags.cessation_of_operation,
2587 x509.ReasonFlags.aa_compromise,
2588 x509.ReasonFlags.certificate_hold,
2589 ]),
2590 crl_issuer=None
2591 )
2592 ])
2593
2594 def test_single_reason(self, backend):
2595 cert = _load_cert(
2596 os.path.join(
2597 "x509", "custom", "cdp_reason_aa_compromise.pem"
2598 ),
2599 x509.load_pem_x509_certificate,
2600 backend
2601 )
2602
2603 cdps = cert.extensions.get_extension_for_oid(
2604 x509.OID_CRL_DISTRIBUTION_POINTS
2605 ).value
2606
2607 assert cdps == x509.CRLDistributionPoints([
2608 x509.DistributionPoint(
2609 full_name=[x509.UniformResourceIdentifier(
2610 u"http://domain.com/some.crl"
2611 )],
2612 relative_name=None,
2613 reasons=frozenset([x509.ReasonFlags.aa_compromise]),
2614 crl_issuer=None
2615 )
2616 ])
2617
Paul Kehrer9a10d592015-05-10 14:55:51 -05002618 def test_crl_issuer_only(self, backend):
2619 cert = _load_cert(
2620 os.path.join(
2621 "x509", "custom", "cdp_crl_issuer.pem"
2622 ),
2623 x509.load_pem_x509_certificate,
2624 backend
2625 )
2626
2627 cdps = cert.extensions.get_extension_for_oid(
2628 x509.OID_CRL_DISTRIBUTION_POINTS
2629 ).value
2630
2631 assert cdps == x509.CRLDistributionPoints([
2632 x509.DistributionPoint(
2633 full_name=None,
2634 relative_name=None,
2635 reasons=None,
2636 crl_issuer=[x509.DirectoryName(
2637 x509.Name([
2638 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05002639 x509.OID_COMMON_NAME, u"cryptography CA"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002640 ),
2641 ])
2642 )],
2643 )
2644 ])
Paul Kehrer16fae762015-05-01 23:14:20 -05002645
2646
Paul Kehrerf54a50b2015-06-17 18:31:26 -06002647@pytest.mark.requires_backend_interface(interface=RSABackend)
2648@pytest.mark.requires_backend_interface(interface=X509Backend)
2649class TestOCSPNoCheckExtension(object):
2650 def test_nocheck(self, backend):
2651 cert = _load_cert(
2652 os.path.join(
2653 "x509", "custom", "ocsp_nocheck.pem"
2654 ),
2655 x509.load_pem_x509_certificate,
2656 backend
2657 )
2658 ext = cert.extensions.get_extension_for_oid(
2659 x509.OID_OCSP_NO_CHECK
2660 )
2661 assert isinstance(ext.value, x509.OCSPNoCheck)
2662
2663
Paul Kehrer16fae762015-05-01 23:14:20 -05002664class TestInhibitAnyPolicy(object):
2665 def test_not_int(self):
2666 with pytest.raises(TypeError):
2667 x509.InhibitAnyPolicy("notint")
2668
2669 def test_negative_int(self):
2670 with pytest.raises(ValueError):
2671 x509.InhibitAnyPolicy(-1)
2672
2673 def test_repr(self):
2674 iap = x509.InhibitAnyPolicy(0)
2675 assert repr(iap) == "<InhibitAnyPolicy(skip_certs=0)>"
2676
2677 def test_eq(self):
2678 iap = x509.InhibitAnyPolicy(1)
2679 iap2 = x509.InhibitAnyPolicy(1)
2680 assert iap == iap2
2681
2682 def test_ne(self):
2683 iap = x509.InhibitAnyPolicy(1)
2684 iap2 = x509.InhibitAnyPolicy(4)
2685 assert iap != iap2
2686 assert iap != object()
Paul Kehrerca6ce992015-06-17 22:13:15 -06002687
2688
2689@pytest.mark.requires_backend_interface(interface=RSABackend)
2690@pytest.mark.requires_backend_interface(interface=X509Backend)
2691class TestInhibitAnyPolicyExtension(object):
2692 def test_nocheck(self, backend):
2693 cert = _load_cert(
2694 os.path.join(
2695 "x509", "custom", "inhibit_any_policy_5.pem"
2696 ),
2697 x509.load_pem_x509_certificate,
2698 backend
2699 )
2700 iap = cert.extensions.get_extension_for_oid(
2701 x509.OID_INHIBIT_ANY_POLICY
2702 ).value
2703 assert iap.skip_certs == 5