blob: e6ee7d66933924e71ed9e61a4b7e37513dfce544 [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
Joshua Tauberer18b6fc82015-07-05 21:44:51 +00001150class TestOtherName(object):
1151 def test_invalid_args(self):
1152 with pytest.raises(TypeError):
1153 x509.OtherName(b"notanobjectidentifier", b"derdata")
1154
1155 with pytest.raises(TypeError):
1156 x509.OtherName(x509.ObjectIdentifier("1.2.3.4"), u"notderdata")
1157
1158 def test_repr(self):
1159 gn = x509.OtherName(x509.ObjectIdentifier("1.2.3.4"), b"derdata")
1160 if six.PY3:
1161 assert repr(gn) == (
1162 "<OtherName(type_id=<ObjectIdentifier(oid=1.2.3.4, "
1163 "name=Unknown OID)>, value=b'derdata')>"
1164 )
1165 else:
1166 assert repr(gn) == (
1167 "<OtherName(type_id=<ObjectIdentifier(oid=1.2.3.4, "
1168 "name=Unknown OID)>, value='derdata')>"
1169 )
1170
1171 gn = x509.OtherName(x509.ObjectIdentifier("2.5.4.65"), b"derdata")
1172 if six.PY3:
1173 assert repr(gn) == (
1174 "<OtherName(type_id=<ObjectIdentifier(oid=2.5.4.65, "
1175 "name=pseudonym)>, value=b'derdata')>"
1176 )
1177 else:
1178 assert repr(gn) == (
1179 "<OtherName(type_id=<ObjectIdentifier(oid=2.5.4.65, "
1180 "name=pseudonym)>, value='derdata')>"
1181 )
1182
1183 def test_eq(self):
1184 gn = x509.OtherName(x509.ObjectIdentifier("1.2.3.4"), b"derdata")
1185 gn2 = x509.OtherName(x509.ObjectIdentifier("1.2.3.4"), b"derdata")
1186 assert gn == gn2
1187
1188 def test_ne(self):
1189 gn = x509.OtherName(x509.ObjectIdentifier("1.2.3.4"), b"derdata")
1190 assert gn != object()
1191
1192 gn2 = x509.OtherName(x509.ObjectIdentifier("1.2.3.4"), b"derdata2")
1193 assert gn != gn2
1194
1195 gn2 = x509.OtherName(x509.ObjectIdentifier("1.2.3.5"), b"derdata")
1196 assert gn != gn2
1197
1198
Erik Trauschke2dcce902015-05-14 16:12:24 -07001199class TestGeneralNames(object):
1200 def test_get_values_for_type(self):
1201 gns = x509.GeneralNames(
1202 [x509.DNSName(u"cryptography.io")]
1203 )
1204 names = gns.get_values_for_type(x509.DNSName)
1205 assert names == [u"cryptography.io"]
1206
1207 def test_iter_names(self):
1208 gns = x509.GeneralNames([
1209 x509.DNSName(u"cryptography.io"),
1210 x509.DNSName(u"crypto.local"),
1211 ])
1212 assert len(gns) == 2
1213 assert list(gns) == [
1214 x509.DNSName(u"cryptography.io"),
1215 x509.DNSName(u"crypto.local"),
1216 ]
1217
1218 def test_invalid_general_names(self):
1219 with pytest.raises(TypeError):
1220 x509.GeneralNames(
1221 [x509.DNSName(u"cryptography.io"), "invalid"]
1222 )
1223
1224 def test_repr(self):
1225 gns = x509.GeneralNames(
1226 [
1227 x509.DNSName(u"cryptography.io")
1228 ]
1229 )
1230 assert repr(gns) == (
1231 "<GeneralNames([<DNSName(value=cryptography.io)>])>"
1232 )
1233
1234 def test_eq(self):
1235 gns = x509.GeneralNames(
1236 [x509.DNSName(u"cryptography.io")]
1237 )
1238 gns2 = x509.GeneralNames(
1239 [x509.DNSName(u"cryptography.io")]
1240 )
1241 assert gns == gns2
1242
1243 def test_ne(self):
1244 gns = x509.GeneralNames(
1245 [x509.DNSName(u"cryptography.io")]
1246 )
1247 gns2 = x509.GeneralNames(
1248 [x509.RFC822Name(u"admin@cryptography.io")]
1249 )
1250 assert gns != gns2
1251 assert gns != object()
1252
1253
Paul Kehrer99125c92015-06-07 18:37:10 -05001254class TestIssuerAlternativeName(object):
1255 def test_get_values_for_type(self):
1256 san = x509.IssuerAlternativeName(
1257 [x509.DNSName(u"cryptography.io")]
1258 )
1259 names = san.get_values_for_type(x509.DNSName)
1260 assert names == [u"cryptography.io"]
1261
1262 def test_iter_names(self):
1263 san = x509.IssuerAlternativeName([
1264 x509.DNSName(u"cryptography.io"),
1265 x509.DNSName(u"crypto.local"),
1266 ])
1267 assert len(san) == 2
1268 assert list(san) == [
1269 x509.DNSName(u"cryptography.io"),
1270 x509.DNSName(u"crypto.local"),
1271 ]
1272
1273 def test_invalid_general_names(self):
1274 with pytest.raises(TypeError):
1275 x509.IssuerAlternativeName(
1276 [x509.DNSName(u"cryptography.io"), "invalid"]
1277 )
1278
1279 def test_repr(self):
1280 san = x509.IssuerAlternativeName(
1281 [
1282 x509.DNSName(u"cryptography.io")
1283 ]
1284 )
1285 assert repr(san) == (
1286 "<IssuerAlternativeName("
1287 "<GeneralNames([<DNSName(value=cryptography.io)>])>)>"
1288 )
1289
1290 def test_eq(self):
1291 san = x509.IssuerAlternativeName(
1292 [x509.DNSName(u"cryptography.io")]
1293 )
1294 san2 = x509.IssuerAlternativeName(
1295 [x509.DNSName(u"cryptography.io")]
1296 )
1297 assert san == san2
1298
1299 def test_ne(self):
1300 san = x509.IssuerAlternativeName(
1301 [x509.DNSName(u"cryptography.io")]
1302 )
1303 san2 = x509.IssuerAlternativeName(
1304 [x509.RFC822Name(u"admin@cryptography.io")]
1305 )
1306 assert san != san2
1307 assert san != object()
1308
1309
Alex Gaynorf1c17672015-06-20 14:20:20 -04001310@pytest.mark.requires_backend_interface(interface=RSABackend)
1311@pytest.mark.requires_backend_interface(interface=X509Backend)
1312class TestRSAIssuerAlternativeNameExtension(object):
1313 def test_uri(self, backend):
1314 cert = _load_cert(
1315 os.path.join("x509", "custom", "ian_uri.pem"),
1316 x509.load_pem_x509_certificate,
1317 backend,
1318 )
1319 ext = cert.extensions.get_extension_for_oid(
1320 x509.OID_ISSUER_ALTERNATIVE_NAME
1321 )
1322 assert list(ext.value) == [
1323 x509.UniformResourceIdentifier(u"http://path.to.root/root.crt"),
1324 ]
1325
1326
Paul Kehrer31bdf792015-03-25 14:11:00 -05001327class TestSubjectAlternativeName(object):
1328 def test_get_values_for_type(self):
1329 san = x509.SubjectAlternativeName(
Eeshan Gargf1234152015-04-29 18:41:00 +05301330 [x509.DNSName(u"cryptography.io")]
Paul Kehrer31bdf792015-03-25 14:11:00 -05001331 )
1332 names = san.get_values_for_type(x509.DNSName)
Eeshan Gargf1234152015-04-29 18:41:00 +05301333 assert names == [u"cryptography.io"]
Paul Kehrer31bdf792015-03-25 14:11:00 -05001334
1335 def test_iter_names(self):
1336 san = x509.SubjectAlternativeName([
Eeshan Gargf1234152015-04-29 18:41:00 +05301337 x509.DNSName(u"cryptography.io"),
1338 x509.DNSName(u"crypto.local"),
Paul Kehrer31bdf792015-03-25 14:11:00 -05001339 ])
1340 assert len(san) == 2
1341 assert list(san) == [
Eeshan Gargf1234152015-04-29 18:41:00 +05301342 x509.DNSName(u"cryptography.io"),
1343 x509.DNSName(u"crypto.local"),
Paul Kehrer31bdf792015-03-25 14:11:00 -05001344 ]
1345
Paul Kehrerd04b39b2015-04-21 08:44:17 -05001346 def test_invalid_general_names(self):
1347 with pytest.raises(TypeError):
1348 x509.SubjectAlternativeName(
Eeshan Gargf1234152015-04-29 18:41:00 +05301349 [x509.DNSName(u"cryptography.io"), "invalid"]
Paul Kehrerd04b39b2015-04-21 08:44:17 -05001350 )
1351
Paul Kehrer31bdf792015-03-25 14:11:00 -05001352 def test_repr(self):
1353 san = x509.SubjectAlternativeName(
1354 [
Eeshan Gargf1234152015-04-29 18:41:00 +05301355 x509.DNSName(u"cryptography.io")
Paul Kehrer31bdf792015-03-25 14:11:00 -05001356 ]
1357 )
1358 assert repr(san) == (
Erik Trauschke2dcce902015-05-14 16:12:24 -07001359 "<SubjectAlternativeName("
1360 "<GeneralNames([<DNSName(value=cryptography.io)>])>)>"
Paul Kehrer31bdf792015-03-25 14:11:00 -05001361 )
Paul Kehrer40f83382015-04-20 15:00:16 -05001362
Paul Kehrer58cc3972015-05-13 10:00:41 -05001363 def test_eq(self):
1364 san = x509.SubjectAlternativeName(
1365 [x509.DNSName(u"cryptography.io")]
1366 )
1367 san2 = x509.SubjectAlternativeName(
1368 [x509.DNSName(u"cryptography.io")]
1369 )
1370 assert san == san2
1371
1372 def test_ne(self):
1373 san = x509.SubjectAlternativeName(
1374 [x509.DNSName(u"cryptography.io")]
1375 )
1376 san2 = x509.SubjectAlternativeName(
1377 [x509.RFC822Name(u"admin@cryptography.io")]
1378 )
1379 assert san != san2
1380 assert san != object()
1381
Paul Kehrer40f83382015-04-20 15:00:16 -05001382
1383@pytest.mark.requires_backend_interface(interface=RSABackend)
1384@pytest.mark.requires_backend_interface(interface=X509Backend)
1385class TestRSASubjectAlternativeNameExtension(object):
1386 def test_dns_name(self, backend):
1387 cert = _load_cert(
1388 os.path.join("x509", "cryptography.io.pem"),
1389 x509.load_pem_x509_certificate,
1390 backend
1391 )
1392 ext = cert.extensions.get_extension_for_oid(
1393 x509.OID_SUBJECT_ALTERNATIVE_NAME
1394 )
1395 assert ext is not None
1396 assert ext.critical is False
1397
1398 san = ext.value
1399
1400 dns = san.get_values_for_type(x509.DNSName)
1401 assert dns == [u"www.cryptography.io", u"cryptography.io"]
Paul Kehrer9089c912015-04-20 22:15:20 -05001402
Paul Kehrer3c6bba52015-06-26 18:43:26 -05001403 def test_wildcard_dns_name(self, backend):
1404 cert = _load_cert(
1405 os.path.join("x509", "wildcard_san.pem"),
1406 x509.load_pem_x509_certificate,
1407 backend
1408 )
1409 ext = cert.extensions.get_extension_for_oid(
1410 x509.OID_SUBJECT_ALTERNATIVE_NAME
1411 )
1412
1413 dns = ext.value.get_values_for_type(x509.DNSName)
1414 assert dns == [
1415 u'*.langui.sh',
1416 u'langui.sh',
1417 u'*.saseliminator.com',
1418 u'saseliminator.com'
1419 ]
1420
1421 def test_san_wildcard_idna_dns_name(self, backend):
1422 cert = _load_cert(
1423 os.path.join("x509", "custom", "san_wildcard_idna.pem"),
1424 x509.load_pem_x509_certificate,
1425 backend
1426 )
1427 ext = cert.extensions.get_extension_for_oid(
1428 x509.OID_SUBJECT_ALTERNATIVE_NAME
1429 )
1430
1431 dns = ext.value.get_values_for_type(x509.DNSName)
1432 assert dns == [u'*.\u043f\u044b\u043a\u0430.cryptography']
1433
Paul Kehrer7bdf6572015-07-05 08:55:44 -05001434 def test_unsupported_gn(self, backend):
Paul Kehrer9089c912015-04-20 22:15:20 -05001435 cert = _load_cert(
Paul Kehrer7bdf6572015-07-05 08:55:44 -05001436 os.path.join("x509", "san_x400address.der"),
1437 x509.load_der_x509_certificate,
Paul Kehrer9089c912015-04-20 22:15:20 -05001438 backend
1439 )
Paul Kehrerbed07352015-04-21 08:31:10 -05001440 with pytest.raises(x509.UnsupportedGeneralNameType) as exc:
Paul Kehrer9089c912015-04-20 22:15:20 -05001441 cert.extensions
Paul Kehrerbed07352015-04-21 08:31:10 -05001442
Paul Kehrer7bdf6572015-07-05 08:55:44 -05001443 assert exc.value.type == 3
Paul Kehrer4db96622015-04-20 22:17:39 -05001444
1445 def test_registered_id(self, backend):
1446 cert = _load_cert(
1447 os.path.join(
1448 "x509", "custom", "san_registered_id.pem"
1449 ),
1450 x509.load_pem_x509_certificate,
1451 backend
1452 )
1453 ext = cert.extensions.get_extension_for_oid(
1454 x509.OID_SUBJECT_ALTERNATIVE_NAME
1455 )
1456 assert ext is not None
1457 assert ext.critical is False
1458
1459 san = ext.value
1460 rid = san.get_values_for_type(x509.RegisteredID)
1461 assert rid == [x509.ObjectIdentifier("1.2.3.4")]
Paul Kehrerb8ef82e2015-04-22 16:04:24 -05001462
1463 def test_uri(self, backend):
1464 cert = _load_cert(
1465 os.path.join(
1466 "x509", "custom", "san_uri_with_port.pem"
1467 ),
1468 x509.load_pem_x509_certificate,
1469 backend
1470 )
1471 ext = cert.extensions.get_extension_for_oid(
1472 x509.OID_SUBJECT_ALTERNATIVE_NAME
1473 )
1474 assert ext is not None
1475 uri = ext.value.get_values_for_type(
1476 x509.UniformResourceIdentifier
1477 )
1478 assert uri == [
1479 u"gopher://\u043f\u044b\u043a\u0430.cryptography:70/path?q=s#hel"
1480 u"lo",
1481 u"http://someregulardomain.com",
1482 ]
Paul Kehrera5f030c2015-04-28 08:33:18 -05001483
1484 def test_ipaddress(self, backend):
1485 cert = _load_cert(
1486 os.path.join(
1487 "x509", "custom", "san_ipaddr.pem"
1488 ),
1489 x509.load_pem_x509_certificate,
1490 backend
1491 )
1492 ext = cert.extensions.get_extension_for_oid(
1493 x509.OID_SUBJECT_ALTERNATIVE_NAME
1494 )
1495 assert ext is not None
1496 assert ext.critical is False
1497
1498 san = ext.value
1499
1500 ip = san.get_values_for_type(x509.IPAddress)
1501 assert [
1502 ipaddress.ip_address(u"127.0.0.1"),
1503 ipaddress.ip_address(u"ff::")
1504 ] == ip
Paul Kehrer2187a052015-04-30 08:22:07 -05001505
1506 def test_dirname(self, backend):
1507 cert = _load_cert(
1508 os.path.join(
1509 "x509", "custom", "san_dirname.pem"
1510 ),
1511 x509.load_pem_x509_certificate,
1512 backend
1513 )
1514 ext = cert.extensions.get_extension_for_oid(
1515 x509.OID_SUBJECT_ALTERNATIVE_NAME
1516 )
1517 assert ext is not None
1518 assert ext.critical is False
1519
1520 san = ext.value
1521
1522 dirname = san.get_values_for_type(x509.DirectoryName)
1523 assert [
1524 x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05001525 x509.NameAttribute(x509.OID_COMMON_NAME, u'test'),
1526 x509.NameAttribute(x509.OID_ORGANIZATION_NAME, u'Org'),
1527 x509.NameAttribute(x509.OID_STATE_OR_PROVINCE_NAME, u'Texas'),
Paul Kehrer2187a052015-04-30 08:22:07 -05001528 ])
1529 ] == dirname
Paul Kehrere06cab42015-04-30 10:23:33 -05001530
1531 def test_rfc822name(self, backend):
1532 cert = _load_cert(
1533 os.path.join(
1534 "x509", "custom", "san_rfc822_idna.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 rfc822name = san.get_values_for_type(x509.RFC822Name)
1548 assert [u"email@em\xe5\xefl.com"] == rfc822name
1549
Paul Kehrerb8968812015-05-15 09:01:34 -07001550 def test_idna2003_invalid(self, backend):
1551 cert = _load_cert(
1552 os.path.join(
1553 "x509", "custom", "san_idna2003_dnsname.pem"
1554 ),
1555 x509.load_pem_x509_certificate,
1556 backend
1557 )
1558 with pytest.raises(UnicodeError):
1559 cert.extensions
1560
Paul Kehrere06cab42015-04-30 10:23:33 -05001561 def test_unicode_rfc822_name_dns_name_uri(self, backend):
1562 cert = _load_cert(
1563 os.path.join(
1564 "x509", "custom", "san_idna_names.pem"
1565 ),
1566 x509.load_pem_x509_certificate,
1567 backend
1568 )
1569 ext = cert.extensions.get_extension_for_oid(
1570 x509.OID_SUBJECT_ALTERNATIVE_NAME
1571 )
1572 assert ext is not None
1573 rfc822_name = ext.value.get_values_for_type(x509.RFC822Name)
1574 dns_name = ext.value.get_values_for_type(x509.DNSName)
1575 uri = ext.value.get_values_for_type(x509.UniformResourceIdentifier)
1576 assert rfc822_name == [u"email@\u043f\u044b\u043a\u0430.cryptography"]
1577 assert dns_name == [u"\u043f\u044b\u043a\u0430.cryptography"]
1578 assert uri == [u"https://www.\u043f\u044b\u043a\u0430.cryptography"]
1579
1580 def test_rfc822name_dnsname_ipaddress_directoryname_uri(self, backend):
1581 cert = _load_cert(
1582 os.path.join(
1583 "x509", "custom", "san_email_dns_ip_dirname_uri.pem"
1584 ),
1585 x509.load_pem_x509_certificate,
1586 backend
1587 )
1588 ext = cert.extensions.get_extension_for_oid(
1589 x509.OID_SUBJECT_ALTERNATIVE_NAME
1590 )
1591 assert ext is not None
1592 assert ext.critical is False
1593
1594 san = ext.value
1595
1596 rfc822_name = san.get_values_for_type(x509.RFC822Name)
1597 uri = san.get_values_for_type(x509.UniformResourceIdentifier)
1598 dns = san.get_values_for_type(x509.DNSName)
1599 ip = san.get_values_for_type(x509.IPAddress)
1600 dirname = san.get_values_for_type(x509.DirectoryName)
1601 assert [u"user@cryptography.io"] == rfc822_name
Paul Kehrere3a330c2015-05-02 16:42:52 -05001602 assert [u"https://cryptography.io"] == uri
Paul Kehrere06cab42015-04-30 10:23:33 -05001603 assert [u"cryptography.io"] == dns
1604 assert [
1605 x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05001606 x509.NameAttribute(x509.OID_COMMON_NAME, u'dirCN'),
Paul Kehrere06cab42015-04-30 10:23:33 -05001607 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05001608 x509.OID_ORGANIZATION_NAME, u'Cryptographic Authority'
Paul Kehrere06cab42015-04-30 10:23:33 -05001609 ),
1610 ])
1611 ] == dirname
1612 assert [
1613 ipaddress.ip_address(u"127.0.0.1"),
1614 ipaddress.ip_address(u"ff::")
1615 ] == ip
1616
1617 def test_invalid_rfc822name(self, backend):
1618 cert = _load_cert(
1619 os.path.join(
1620 "x509", "custom", "san_rfc822_names.pem"
1621 ),
1622 x509.load_pem_x509_certificate,
1623 backend
1624 )
1625 with pytest.raises(ValueError) as exc:
1626 cert.extensions
1627
1628 assert 'Invalid rfc822name value' in str(exc.value)
Paul Kehrer94c69602015-05-02 19:29:40 -05001629
Joshua Tauberer2ee5e3c2015-07-04 20:09:46 +00001630 def test_other_name(self, backend):
1631 cert = _load_cert(
1632 os.path.join(
1633 "x509", "custom", "san_other_name.pem"
1634 ),
1635 x509.load_pem_x509_certificate,
1636 backend
1637 )
1638
1639 ext = cert.extensions.get_extension_for_oid(
1640 x509.OID_SUBJECT_ALTERNATIVE_NAME
1641 )
1642 assert ext is not None
1643 assert ext.critical is False
1644
1645 assert len(ext.value) == 1
1646 assert list(ext.value)[0] == \
1647 x509.OtherName(
1648 x509.ObjectIdentifier("1.2.3.4"),
1649 b'\x16\x0bHello World')
1650
1651 othernames = ext.value.get_values_for_type(x509.OtherName)
1652 assert othernames == [b'\x16\x0bHello World']
1653
Paul Kehrer94c69602015-05-02 19:29:40 -05001654
1655@pytest.mark.requires_backend_interface(interface=RSABackend)
1656@pytest.mark.requires_backend_interface(interface=X509Backend)
1657class TestExtendedKeyUsageExtension(object):
1658 def test_eku(self, backend):
1659 cert = _load_cert(
1660 os.path.join(
1661 "x509", "custom", "extended_key_usage.pem"
1662 ),
1663 x509.load_pem_x509_certificate,
1664 backend
1665 )
1666 ext = cert.extensions.get_extension_for_oid(
1667 x509.OID_EXTENDED_KEY_USAGE
1668 )
1669 assert ext is not None
1670 assert ext.critical is False
1671
1672 assert [
1673 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.1"),
1674 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.2"),
1675 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.3"),
1676 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.4"),
1677 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.9"),
1678 x509.ObjectIdentifier("1.3.6.1.5.5.7.3.8"),
1679 x509.ObjectIdentifier("2.5.29.37.0"),
1680 x509.ObjectIdentifier("2.16.840.1.113730.4.1"),
1681 ] == list(ext.value)
Paul Kehrer3e6d5582015-05-02 21:57:56 -05001682
1683
1684class TestAccessDescription(object):
1685 def test_invalid_access_method(self):
Paul Kehrerf506bca2015-05-02 22:31:47 -05001686 with pytest.raises(ValueError):
Paul Kehrer3e6d5582015-05-02 21:57:56 -05001687 x509.AccessDescription("notanoid", x509.DNSName(u"test"))
1688
1689 def test_invalid_access_location(self):
1690 with pytest.raises(TypeError):
1691 x509.AccessDescription(x509.OID_CA_ISSUERS, "invalid")
1692
1693 def test_repr(self):
1694 ad = x509.AccessDescription(
1695 x509.OID_OCSP,
1696 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1697 )
1698 assert repr(ad) == (
1699 "<AccessDescription(access_method=<ObjectIdentifier(oid=1.3.6.1.5."
1700 "5.7.48.1, name=OCSP)>, access_location=<UniformResourceIdentifier"
1701 "(value=http://ocsp.domain.com)>)>"
1702 )
1703
1704 def test_eq(self):
1705 ad = x509.AccessDescription(
1706 x509.OID_OCSP,
1707 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1708 )
1709 ad2 = x509.AccessDescription(
1710 x509.OID_OCSP,
1711 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1712 )
1713 assert ad == ad2
1714
1715 def test_ne(self):
1716 ad = x509.AccessDescription(
1717 x509.OID_OCSP,
1718 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1719 )
1720 ad2 = x509.AccessDescription(
1721 x509.OID_CA_ISSUERS,
1722 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1723 )
1724 ad3 = x509.AccessDescription(
1725 x509.OID_OCSP,
1726 x509.UniformResourceIdentifier(u"http://notthesame")
1727 )
1728 assert ad != ad2
1729 assert ad != ad3
1730 assert ad != object()
1731
1732
1733class TestAuthorityInformationAccess(object):
1734 def test_invalid_descriptions(self):
1735 with pytest.raises(TypeError):
1736 x509.AuthorityInformationAccess(["notanAccessDescription"])
1737
1738 def test_iter_len(self):
1739 aia = x509.AuthorityInformationAccess([
1740 x509.AccessDescription(
1741 x509.OID_OCSP,
1742 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1743 ),
1744 x509.AccessDescription(
1745 x509.OID_CA_ISSUERS,
1746 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1747 )
1748 ])
1749 assert len(aia) == 2
1750 assert list(aia) == [
1751 x509.AccessDescription(
1752 x509.OID_OCSP,
1753 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1754 ),
1755 x509.AccessDescription(
1756 x509.OID_CA_ISSUERS,
1757 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1758 )
1759 ]
1760
1761 def test_repr(self):
1762 aia = x509.AuthorityInformationAccess([
1763 x509.AccessDescription(
1764 x509.OID_OCSP,
1765 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1766 ),
1767 x509.AccessDescription(
1768 x509.OID_CA_ISSUERS,
1769 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1770 )
1771 ])
1772 assert repr(aia) == (
1773 "<AuthorityInformationAccess([<AccessDescription(access_method=<Ob"
1774 "jectIdentifier(oid=1.3.6.1.5.5.7.48.1, name=OCSP)>, access_locati"
1775 "on=<UniformResourceIdentifier(value=http://ocsp.domain.com)>)>, <"
1776 "AccessDescription(access_method=<ObjectIdentifier(oid=1.3.6.1.5.5"
1777 ".7.48.2, name=caIssuers)>, access_location=<UniformResourceIdenti"
1778 "fier(value=http://domain.com/ca.crt)>)>])>"
1779 )
1780
1781 def test_eq(self):
1782 aia = x509.AuthorityInformationAccess([
1783 x509.AccessDescription(
1784 x509.OID_OCSP,
1785 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1786 ),
1787 x509.AccessDescription(
1788 x509.OID_CA_ISSUERS,
1789 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1790 )
1791 ])
1792 aia2 = x509.AuthorityInformationAccess([
1793 x509.AccessDescription(
1794 x509.OID_OCSP,
1795 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1796 ),
1797 x509.AccessDescription(
1798 x509.OID_CA_ISSUERS,
1799 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1800 )
1801 ])
1802 assert aia == aia2
1803
1804 def test_ne(self):
1805 aia = x509.AuthorityInformationAccess([
1806 x509.AccessDescription(
1807 x509.OID_OCSP,
1808 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1809 ),
1810 x509.AccessDescription(
1811 x509.OID_CA_ISSUERS,
1812 x509.UniformResourceIdentifier(u"http://domain.com/ca.crt")
1813 )
1814 ])
1815 aia2 = x509.AuthorityInformationAccess([
1816 x509.AccessDescription(
1817 x509.OID_OCSP,
1818 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1819 ),
1820 ])
1821
1822 assert aia != aia2
1823 assert aia != object()
Paul Kehrerd774de92015-05-03 10:52:25 -05001824
1825
1826@pytest.mark.requires_backend_interface(interface=RSABackend)
1827@pytest.mark.requires_backend_interface(interface=X509Backend)
Paul Kehrera1476992015-05-04 17:35:47 -05001828class TestAuthorityInformationAccessExtension(object):
1829 def test_aia_ocsp_ca_issuers(self, backend):
1830 cert = _load_cert(
1831 os.path.join("x509", "cryptography.io.pem"),
1832 x509.load_pem_x509_certificate,
1833 backend
1834 )
1835 ext = cert.extensions.get_extension_for_oid(
1836 x509.OID_AUTHORITY_INFORMATION_ACCESS
1837 )
1838 assert ext is not None
1839 assert ext.critical is False
1840
1841 assert ext.value == x509.AuthorityInformationAccess([
1842 x509.AccessDescription(
1843 x509.OID_OCSP,
1844 x509.UniformResourceIdentifier(u"http://gv.symcd.com")
1845 ),
1846 x509.AccessDescription(
1847 x509.OID_CA_ISSUERS,
1848 x509.UniformResourceIdentifier(u"http://gv.symcb.com/gv.crt")
1849 ),
1850 ])
1851
1852 def test_aia_multiple_ocsp_ca_issuers(self, backend):
1853 cert = _load_cert(
1854 os.path.join("x509", "custom", "aia_ocsp_ca_issuers.pem"),
1855 x509.load_pem_x509_certificate,
1856 backend
1857 )
1858 ext = cert.extensions.get_extension_for_oid(
1859 x509.OID_AUTHORITY_INFORMATION_ACCESS
1860 )
1861 assert ext is not None
1862 assert ext.critical is False
1863
1864 assert ext.value == x509.AuthorityInformationAccess([
1865 x509.AccessDescription(
1866 x509.OID_OCSP,
1867 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1868 ),
1869 x509.AccessDescription(
1870 x509.OID_OCSP,
1871 x509.UniformResourceIdentifier(u"http://ocsp2.domain.com")
1872 ),
1873 x509.AccessDescription(
1874 x509.OID_CA_ISSUERS,
1875 x509.DirectoryName(x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05001876 x509.NameAttribute(x509.OID_COMMON_NAME, u"myCN"),
1877 x509.NameAttribute(x509.OID_ORGANIZATION_NAME,
1878 u"some Org"),
Paul Kehrera1476992015-05-04 17:35:47 -05001879 ]))
1880 ),
1881 ])
1882
1883 def test_aia_ocsp_only(self, backend):
1884 cert = _load_cert(
1885 os.path.join("x509", "custom", "aia_ocsp.pem"),
1886 x509.load_pem_x509_certificate,
1887 backend
1888 )
1889 ext = cert.extensions.get_extension_for_oid(
1890 x509.OID_AUTHORITY_INFORMATION_ACCESS
1891 )
1892 assert ext is not None
1893 assert ext.critical is False
1894
1895 assert ext.value == x509.AuthorityInformationAccess([
1896 x509.AccessDescription(
1897 x509.OID_OCSP,
1898 x509.UniformResourceIdentifier(u"http://ocsp.domain.com")
1899 ),
1900 ])
1901
1902 def test_aia_ca_issuers_only(self, backend):
1903 cert = _load_cert(
1904 os.path.join("x509", "custom", "aia_ca_issuers.pem"),
1905 x509.load_pem_x509_certificate,
1906 backend
1907 )
1908 ext = cert.extensions.get_extension_for_oid(
1909 x509.OID_AUTHORITY_INFORMATION_ACCESS
1910 )
1911 assert ext is not None
1912 assert ext.critical is False
1913
1914 assert ext.value == x509.AuthorityInformationAccess([
1915 x509.AccessDescription(
1916 x509.OID_CA_ISSUERS,
1917 x509.DirectoryName(x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05001918 x509.NameAttribute(x509.OID_COMMON_NAME, u"myCN"),
1919 x509.NameAttribute(x509.OID_ORGANIZATION_NAME,
1920 u"some Org"),
Paul Kehrera1476992015-05-04 17:35:47 -05001921 ]))
1922 ),
1923 ])
1924
1925
1926@pytest.mark.requires_backend_interface(interface=RSABackend)
1927@pytest.mark.requires_backend_interface(interface=X509Backend)
Paul Kehrerd774de92015-05-03 10:52:25 -05001928class TestAuthorityKeyIdentifierExtension(object):
1929 def test_aki_keyid(self, backend):
1930 cert = _load_cert(
1931 os.path.join(
1932 "x509", "cryptography.io.pem"
1933 ),
1934 x509.load_pem_x509_certificate,
1935 backend
1936 )
1937 ext = cert.extensions.get_extension_for_oid(
1938 x509.OID_AUTHORITY_KEY_IDENTIFIER
1939 )
1940 assert ext is not None
1941 assert ext.critical is False
1942
1943 assert ext.value.key_identifier == (
1944 b"\xc3\x9c\xf3\xfc\xd3F\x084\xbb\xceF\x7f\xa0|[\xf3\xe2\x08\xcbY"
1945 )
1946 assert ext.value.authority_cert_issuer is None
1947 assert ext.value.authority_cert_serial_number is None
1948
1949 def test_aki_all_fields(self, backend):
1950 cert = _load_cert(
1951 os.path.join(
1952 "x509", "custom", "authority_key_identifier.pem"
1953 ),
1954 x509.load_pem_x509_certificate,
1955 backend
1956 )
1957 ext = cert.extensions.get_extension_for_oid(
1958 x509.OID_AUTHORITY_KEY_IDENTIFIER
1959 )
1960 assert ext is not None
1961 assert ext.critical is False
1962
1963 assert ext.value.key_identifier == (
1964 b"9E>\xca=b\x1d\xea\x86I\xf6Z\xab@\xb7\xa4p\x98\xf1\xec"
1965 )
1966 assert ext.value.authority_cert_issuer == [
1967 x509.DirectoryName(
1968 x509.Name([
1969 x509.NameAttribute(
1970 x509.OID_ORGANIZATION_NAME, u"PyCA"
1971 ),
1972 x509.NameAttribute(
1973 x509.OID_COMMON_NAME, u"cryptography.io"
1974 )
1975 ])
1976 )
1977 ]
1978 assert ext.value.authority_cert_serial_number == 3
1979
1980 def test_aki_no_keyid(self, backend):
1981 cert = _load_cert(
1982 os.path.join(
1983 "x509", "custom", "authority_key_identifier_no_keyid.pem"
1984 ),
1985 x509.load_pem_x509_certificate,
1986 backend
1987 )
1988 ext = cert.extensions.get_extension_for_oid(
1989 x509.OID_AUTHORITY_KEY_IDENTIFIER
1990 )
1991 assert ext is not None
1992 assert ext.critical is False
1993
1994 assert ext.value.key_identifier is None
1995 assert ext.value.authority_cert_issuer == [
1996 x509.DirectoryName(
1997 x509.Name([
1998 x509.NameAttribute(
1999 x509.OID_ORGANIZATION_NAME, u"PyCA"
2000 ),
2001 x509.NameAttribute(
2002 x509.OID_COMMON_NAME, u"cryptography.io"
2003 )
2004 ])
2005 )
2006 ]
2007 assert ext.value.authority_cert_serial_number == 3
Paul Kehrer5a485522015-05-06 00:29:12 -05002008
2009
Paul Kehrere0017be2015-05-17 20:39:40 -06002010class TestNameConstraints(object):
2011 def test_ipaddress_wrong_type(self):
2012 with pytest.raises(TypeError):
2013 x509.NameConstraints(
2014 permitted_subtrees=[
2015 x509.IPAddress(ipaddress.IPv4Address(u"127.0.0.1"))
2016 ],
2017 excluded_subtrees=None
2018 )
2019
2020 with pytest.raises(TypeError):
2021 x509.NameConstraints(
2022 permitted_subtrees=None,
2023 excluded_subtrees=[
2024 x509.IPAddress(ipaddress.IPv4Address(u"127.0.0.1"))
2025 ]
2026 )
2027
2028 def test_ipaddress_allowed_type(self):
2029 permitted = [x509.IPAddress(ipaddress.IPv4Network(u"192.168.0.0/29"))]
2030 excluded = [x509.IPAddress(ipaddress.IPv4Network(u"10.10.0.0/24"))]
2031 nc = x509.NameConstraints(
2032 permitted_subtrees=permitted,
2033 excluded_subtrees=excluded
2034 )
2035 assert nc.permitted_subtrees == permitted
2036 assert nc.excluded_subtrees == excluded
2037
2038 def test_invalid_permitted_subtrees(self):
2039 with pytest.raises(TypeError):
2040 x509.NameConstraints("badpermitted", None)
2041
2042 def test_invalid_excluded_subtrees(self):
2043 with pytest.raises(TypeError):
2044 x509.NameConstraints(None, "badexcluded")
2045
2046 def test_no_subtrees(self):
2047 with pytest.raises(ValueError):
2048 x509.NameConstraints(None, None)
2049
2050 def test_permitted_none(self):
2051 excluded = [x509.DNSName(u"name.local")]
2052 nc = x509.NameConstraints(
2053 permitted_subtrees=None, excluded_subtrees=excluded
2054 )
2055 assert nc.permitted_subtrees is None
2056 assert nc.excluded_subtrees is not None
2057
2058 def test_excluded_none(self):
2059 permitted = [x509.DNSName(u"name.local")]
2060 nc = x509.NameConstraints(
2061 permitted_subtrees=permitted, excluded_subtrees=None
2062 )
2063 assert nc.permitted_subtrees is not None
2064 assert nc.excluded_subtrees is None
2065
2066 def test_repr(self):
2067 permitted = [x509.DNSName(u"name.local"), x509.DNSName(u"name2.local")]
2068 nc = x509.NameConstraints(
2069 permitted_subtrees=permitted,
2070 excluded_subtrees=None
2071 )
2072 assert repr(nc) == (
2073 "<NameConstraints(permitted_subtrees=[<DNSName(value=name.local)>"
2074 ", <DNSName(value=name2.local)>], excluded_subtrees=None)>"
2075 )
2076
Paul Kehrer31894282015-06-21 21:46:41 -05002077 def test_eq(self):
2078 nc = x509.NameConstraints(
2079 permitted_subtrees=[x509.DNSName(u"name.local")],
2080 excluded_subtrees=[x509.DNSName(u"name2.local")]
2081 )
2082 nc2 = x509.NameConstraints(
2083 permitted_subtrees=[x509.DNSName(u"name.local")],
2084 excluded_subtrees=[x509.DNSName(u"name2.local")]
2085 )
2086 assert nc == nc2
2087
2088 def test_ne(self):
2089 nc = x509.NameConstraints(
2090 permitted_subtrees=[x509.DNSName(u"name.local")],
2091 excluded_subtrees=[x509.DNSName(u"name2.local")]
2092 )
2093 nc2 = x509.NameConstraints(
2094 permitted_subtrees=[x509.DNSName(u"name.local")],
2095 excluded_subtrees=None
2096 )
2097 nc3 = x509.NameConstraints(
2098 permitted_subtrees=None,
2099 excluded_subtrees=[x509.DNSName(u"name2.local")]
2100 )
2101
2102 assert nc != nc2
2103 assert nc != nc3
2104 assert nc != object()
2105
Paul Kehrere0017be2015-05-17 20:39:40 -06002106
Paul Kehrer870d7e82015-06-21 22:20:44 -05002107@pytest.mark.requires_backend_interface(interface=RSABackend)
2108@pytest.mark.requires_backend_interface(interface=X509Backend)
2109class TestNameConstraintsExtension(object):
2110 def test_permitted_excluded(self, backend):
2111 cert = _load_cert(
2112 os.path.join(
2113 "x509", "custom", "nc_permitted_excluded_2.pem"
2114 ),
2115 x509.load_pem_x509_certificate,
2116 backend
2117 )
2118 nc = cert.extensions.get_extension_for_oid(
2119 x509.OID_NAME_CONSTRAINTS
2120 ).value
2121 assert nc == x509.NameConstraints(
2122 permitted_subtrees=[
2123 x509.DNSName(u"zombo.local"),
2124 ],
2125 excluded_subtrees=[
2126 x509.DirectoryName(x509.Name([
2127 x509.NameAttribute(x509.OID_COMMON_NAME, u"zombo")
2128 ]))
2129 ]
2130 )
2131
2132 def test_permitted(self, backend):
2133 cert = _load_cert(
2134 os.path.join(
2135 "x509", "custom", "nc_permitted_2.pem"
2136 ),
2137 x509.load_pem_x509_certificate,
2138 backend
2139 )
2140 nc = cert.extensions.get_extension_for_oid(
2141 x509.OID_NAME_CONSTRAINTS
2142 ).value
2143 assert nc == x509.NameConstraints(
2144 permitted_subtrees=[
2145 x509.DNSName(u"zombo.local"),
2146 ],
2147 excluded_subtrees=None
2148 )
2149
Paul Kehrer42376832015-07-01 18:10:32 -05002150 def test_permitted_with_leading_period(self, backend):
2151 cert = _load_cert(
2152 os.path.join(
2153 "x509", "custom", "nc_permitted.pem"
2154 ),
2155 x509.load_pem_x509_certificate,
2156 backend
2157 )
2158 nc = cert.extensions.get_extension_for_oid(
2159 x509.OID_NAME_CONSTRAINTS
2160 ).value
2161 assert nc == x509.NameConstraints(
2162 permitted_subtrees=[
2163 x509.DNSName(u".cryptography.io"),
2164 x509.UniformResourceIdentifier(u"ftp://cryptography.test")
2165 ],
2166 excluded_subtrees=None
2167 )
2168
2169 def test_excluded_with_leading_period(self, backend):
2170 cert = _load_cert(
2171 os.path.join(
2172 "x509", "custom", "nc_excluded.pem"
2173 ),
2174 x509.load_pem_x509_certificate,
2175 backend
2176 )
2177 nc = cert.extensions.get_extension_for_oid(
2178 x509.OID_NAME_CONSTRAINTS
2179 ).value
2180 assert nc == x509.NameConstraints(
2181 permitted_subtrees=None,
2182 excluded_subtrees=[
2183 x509.DNSName(u".cryptography.io"),
2184 x509.UniformResourceIdentifier(u"gopher://cryptography.test")
2185 ]
2186 )
2187
Paul Kehrer870d7e82015-06-21 22:20:44 -05002188
Paul Kehrer5a485522015-05-06 00:29:12 -05002189class TestDistributionPoint(object):
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002190 def test_distribution_point_full_name_not_general_names(self):
Paul Kehrer5a485522015-05-06 00:29:12 -05002191 with pytest.raises(TypeError):
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002192 x509.DistributionPoint(["notgn"], None, None, None)
Paul Kehrer5a485522015-05-06 00:29:12 -05002193
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002194 def test_distribution_point_relative_name_not_name(self):
Paul Kehrer5a485522015-05-06 00:29:12 -05002195 with pytest.raises(TypeError):
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002196 x509.DistributionPoint(None, "notname", None, None)
2197
2198 def test_distribution_point_full_and_relative_not_none(self):
2199 with pytest.raises(ValueError):
2200 x509.DistributionPoint("data", "notname", None, None)
Paul Kehrer5a485522015-05-06 00:29:12 -05002201
2202 def test_crl_issuer_not_general_names(self):
2203 with pytest.raises(TypeError):
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002204 x509.DistributionPoint(None, None, None, ["notgn"])
Paul Kehrer5a485522015-05-06 00:29:12 -05002205
2206 def test_reason_not_reasonflags(self):
2207 with pytest.raises(TypeError):
2208 x509.DistributionPoint(
2209 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002210 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002211 frozenset(["notreasonflags"]),
2212 None
2213 )
2214
2215 def test_reason_not_frozenset(self):
2216 with pytest.raises(TypeError):
2217 x509.DistributionPoint(
2218 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
2219 None,
2220 [x509.ReasonFlags.ca_compromise],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002221 None
2222 )
2223
2224 def test_disallowed_reasons(self):
2225 with pytest.raises(ValueError):
2226 x509.DistributionPoint(
2227 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
2228 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002229 frozenset([x509.ReasonFlags.unspecified]),
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002230 None
2231 )
2232
2233 with pytest.raises(ValueError):
2234 x509.DistributionPoint(
2235 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
2236 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002237 frozenset([x509.ReasonFlags.remove_from_crl]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002238 None
2239 )
2240
2241 def test_reason_only(self):
2242 with pytest.raises(ValueError):
2243 x509.DistributionPoint(
2244 None,
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002245 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002246 frozenset([x509.ReasonFlags.aa_compromise]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002247 None
2248 )
2249
2250 def test_eq(self):
2251 dp = x509.DistributionPoint(
2252 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002253 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002254 frozenset([x509.ReasonFlags.superseded]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002255 [
2256 x509.DirectoryName(
2257 x509.Name([
2258 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05002259 x509.OID_COMMON_NAME, u"Important CA"
Paul Kehrer5a485522015-05-06 00:29:12 -05002260 )
2261 ])
2262 )
2263 ],
2264 )
2265 dp2 = x509.DistributionPoint(
2266 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002267 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002268 frozenset([x509.ReasonFlags.superseded]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002269 [
2270 x509.DirectoryName(
2271 x509.Name([
2272 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05002273 x509.OID_COMMON_NAME, u"Important CA"
Paul Kehrer5a485522015-05-06 00:29:12 -05002274 )
2275 ])
2276 )
2277 ],
2278 )
2279 assert dp == dp2
2280
2281 def test_ne(self):
2282 dp = x509.DistributionPoint(
2283 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002284 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002285 frozenset([x509.ReasonFlags.superseded]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002286 [
2287 x509.DirectoryName(
2288 x509.Name([
2289 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05002290 x509.OID_COMMON_NAME, u"Important CA"
Paul Kehrer5a485522015-05-06 00:29:12 -05002291 )
2292 ])
2293 )
2294 ],
2295 )
2296 dp2 = x509.DistributionPoint(
2297 [x509.UniformResourceIdentifier(u"http://crypt.og/crl")],
2298 None,
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002299 None,
Paul Kehrer5a485522015-05-06 00:29:12 -05002300 None
2301 )
2302 assert dp != dp2
2303 assert dp != object()
2304
2305 def test_repr(self):
2306 dp = x509.DistributionPoint(
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002307 None,
Paul Kehrer5a485522015-05-06 00:29:12 -05002308 x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05002309 x509.NameAttribute(x509.OID_COMMON_NAME, u"myCN")
Paul Kehrer5a485522015-05-06 00:29:12 -05002310 ]),
Paul Kehrer96ef63c2015-05-09 21:17:04 -05002311 frozenset([x509.ReasonFlags.ca_compromise]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002312 [
2313 x509.DirectoryName(
2314 x509.Name([
2315 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05002316 x509.OID_COMMON_NAME, u"Important CA"
Paul Kehrer5a485522015-05-06 00:29:12 -05002317 )
2318 ])
2319 )
2320 ],
2321 )
Paul Kehrer749da3b2015-05-10 09:58:29 -05002322 if six.PY3:
2323 assert repr(dp) == (
2324 "<DistributionPoint(full_name=None, relative_name=<Name([<Name"
2325 "Attribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName)"
Ian Cordascoa908d692015-06-16 21:35:24 -05002326 ">, value='myCN')>])>, reasons=frozenset({<ReasonFlags.ca_comp"
2327 "romise: 'cACompromise'>}), crl_issuer=[<DirectoryName(value=<"
2328 "Name([<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name="
2329 "commonName)>, value='Important CA')>])>)>])>"
Paul Kehrer749da3b2015-05-10 09:58:29 -05002330 )
2331 else:
2332 assert repr(dp) == (
2333 "<DistributionPoint(full_name=None, relative_name=<Name([<Name"
2334 "Attribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName)"
Ian Cordasco82fc3762015-06-16 20:59:50 -05002335 ">, value=u'myCN')>])>, reasons=frozenset([<ReasonFlags.ca_com"
2336 "promise: 'cACompromise'>]), crl_issuer=[<DirectoryName(value="
2337 "<Name([<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name"
2338 "=commonName)>, value=u'Important CA')>])>)>])>"
Paul Kehrer749da3b2015-05-10 09:58:29 -05002339 )
Paul Kehrer5a485522015-05-06 00:29:12 -05002340
2341
2342class TestCRLDistributionPoints(object):
2343 def test_invalid_distribution_points(self):
2344 with pytest.raises(TypeError):
2345 x509.CRLDistributionPoints(["notadistributionpoint"])
2346
2347 def test_iter_len(self):
2348 cdp = x509.CRLDistributionPoints([
2349 x509.DistributionPoint(
2350 [x509.UniformResourceIdentifier(u"http://domain")],
2351 None,
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002352 None,
Paul Kehrer5a485522015-05-06 00:29:12 -05002353 None
2354 ),
2355 x509.DistributionPoint(
2356 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002357 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002358 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002359 x509.ReasonFlags.key_compromise,
2360 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002361 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002362 None
2363 ),
2364 ])
2365 assert len(cdp) == 2
2366 assert list(cdp) == [
2367 x509.DistributionPoint(
2368 [x509.UniformResourceIdentifier(u"http://domain")],
2369 None,
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002370 None,
Paul Kehrer5a485522015-05-06 00:29:12 -05002371 None
2372 ),
2373 x509.DistributionPoint(
2374 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002375 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002376 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002377 x509.ReasonFlags.key_compromise,
2378 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002379 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002380 None
2381 ),
2382 ]
2383
2384 def test_repr(self):
2385 cdp = x509.CRLDistributionPoints([
2386 x509.DistributionPoint(
2387 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002388 None,
Paul Kehrer96ef63c2015-05-09 21:17:04 -05002389 frozenset([x509.ReasonFlags.key_compromise]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002390 None
2391 ),
2392 ])
Paul Kehrer749da3b2015-05-10 09:58:29 -05002393 if six.PY3:
2394 assert repr(cdp) == (
2395 "<CRLDistributionPoints([<DistributionPoint(full_name=[<Unifo"
2396 "rmResourceIdentifier(value=ftp://domain)>], relative_name=No"
2397 "ne, reasons=frozenset({<ReasonFlags.key_compromise: 'keyComp"
2398 "romise'>}), crl_issuer=None)>])>"
2399 )
2400 else:
2401 assert repr(cdp) == (
2402 "<CRLDistributionPoints([<DistributionPoint(full_name=[<Unifo"
2403 "rmResourceIdentifier(value=ftp://domain)>], relative_name=No"
2404 "ne, reasons=frozenset([<ReasonFlags.key_compromise: 'keyComp"
2405 "romise'>]), crl_issuer=None)>])>"
2406 )
Paul Kehrer5a485522015-05-06 00:29:12 -05002407
2408 def test_eq(self):
2409 cdp = x509.CRLDistributionPoints([
2410 x509.DistributionPoint(
2411 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002412 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002413 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002414 x509.ReasonFlags.key_compromise,
2415 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002416 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002417 [x509.UniformResourceIdentifier(u"uri://thing")],
2418 ),
2419 ])
2420 cdp2 = x509.CRLDistributionPoints([
2421 x509.DistributionPoint(
2422 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002423 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002424 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002425 x509.ReasonFlags.key_compromise,
2426 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002427 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002428 [x509.UniformResourceIdentifier(u"uri://thing")],
2429 ),
2430 ])
2431 assert cdp == cdp2
2432
2433 def test_ne(self):
2434 cdp = x509.CRLDistributionPoints([
2435 x509.DistributionPoint(
2436 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002437 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002438 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002439 x509.ReasonFlags.key_compromise,
2440 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002441 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002442 [x509.UniformResourceIdentifier(u"uri://thing")],
2443 ),
2444 ])
2445 cdp2 = x509.CRLDistributionPoints([
2446 x509.DistributionPoint(
2447 [x509.UniformResourceIdentifier(u"ftp://domain2")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002448 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002449 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002450 x509.ReasonFlags.key_compromise,
2451 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002452 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002453 [x509.UniformResourceIdentifier(u"uri://thing")],
2454 ),
2455 ])
2456 cdp3 = x509.CRLDistributionPoints([
2457 x509.DistributionPoint(
2458 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002459 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002460 frozenset([x509.ReasonFlags.key_compromise]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002461 [x509.UniformResourceIdentifier(u"uri://thing")],
2462 ),
2463 ])
2464 cdp4 = x509.CRLDistributionPoints([
2465 x509.DistributionPoint(
2466 [x509.UniformResourceIdentifier(u"ftp://domain")],
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002467 None,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002468 frozenset([
Paul Kehrer4e8dacd2015-05-09 10:38:23 -05002469 x509.ReasonFlags.key_compromise,
2470 x509.ReasonFlags.ca_compromise,
Paul Kehrer3fd02602015-05-09 19:46:13 -05002471 ]),
Paul Kehrer5a485522015-05-06 00:29:12 -05002472 [x509.UniformResourceIdentifier(u"uri://thing2")],
2473 ),
2474 ])
2475 assert cdp != cdp2
2476 assert cdp != cdp3
2477 assert cdp != cdp4
2478 assert cdp != object()
Paul Kehrer9a10d592015-05-10 14:55:51 -05002479
2480
2481@pytest.mark.requires_backend_interface(interface=RSABackend)
2482@pytest.mark.requires_backend_interface(interface=X509Backend)
2483class TestCRLDistributionPointsExtension(object):
2484 def test_fullname_and_crl_issuer(self, backend):
2485 cert = _load_cert(
2486 os.path.join(
2487 "x509", "PKITS_data", "certs", "ValidcRLIssuerTest28EE.crt"
2488 ),
2489 x509.load_der_x509_certificate,
2490 backend
2491 )
2492
2493 cdps = cert.extensions.get_extension_for_oid(
2494 x509.OID_CRL_DISTRIBUTION_POINTS
2495 ).value
2496
2497 assert cdps == x509.CRLDistributionPoints([
2498 x509.DistributionPoint(
2499 full_name=[x509.DirectoryName(
2500 x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05002501 x509.NameAttribute(x509.OID_COUNTRY_NAME, u"US"),
Paul Kehrer9a10d592015-05-10 14:55:51 -05002502 x509.NameAttribute(
2503 x509.OID_ORGANIZATION_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002504 u"Test Certificates 2011"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002505 ),
2506 x509.NameAttribute(
2507 x509.OID_ORGANIZATIONAL_UNIT_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002508 u"indirectCRL CA3 cRLIssuer"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002509 ),
2510 x509.NameAttribute(
2511 x509.OID_COMMON_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002512 u"indirect CRL for indirectCRL CA3"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002513 ),
2514 ])
2515 )],
2516 relative_name=None,
2517 reasons=None,
2518 crl_issuer=[x509.DirectoryName(
2519 x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05002520 x509.NameAttribute(x509.OID_COUNTRY_NAME, u"US"),
Paul Kehrer9a10d592015-05-10 14:55:51 -05002521 x509.NameAttribute(
2522 x509.OID_ORGANIZATION_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002523 u"Test Certificates 2011"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002524 ),
2525 x509.NameAttribute(
2526 x509.OID_ORGANIZATIONAL_UNIT_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002527 u"indirectCRL CA3 cRLIssuer"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002528 ),
2529 ])
2530 )],
2531 )
2532 ])
2533
2534 def test_relativename_and_crl_issuer(self, backend):
2535 cert = _load_cert(
2536 os.path.join(
2537 "x509", "PKITS_data", "certs", "ValidcRLIssuerTest29EE.crt"
2538 ),
2539 x509.load_der_x509_certificate,
2540 backend
2541 )
2542
2543 cdps = cert.extensions.get_extension_for_oid(
2544 x509.OID_CRL_DISTRIBUTION_POINTS
2545 ).value
2546
2547 assert cdps == x509.CRLDistributionPoints([
2548 x509.DistributionPoint(
2549 full_name=None,
2550 relative_name=x509.Name([
2551 x509.NameAttribute(
2552 x509.OID_COMMON_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002553 u"indirect CRL for indirectCRL CA3"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002554 ),
2555 ]),
2556 reasons=None,
2557 crl_issuer=[x509.DirectoryName(
2558 x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05002559 x509.NameAttribute(x509.OID_COUNTRY_NAME, u"US"),
Paul Kehrer9a10d592015-05-10 14:55:51 -05002560 x509.NameAttribute(
2561 x509.OID_ORGANIZATION_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002562 u"Test Certificates 2011"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002563 ),
2564 x509.NameAttribute(
2565 x509.OID_ORGANIZATIONAL_UNIT_NAME,
Ian Cordasco82fc3762015-06-16 20:59:50 -05002566 u"indirectCRL CA3 cRLIssuer"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002567 ),
2568 ])
2569 )],
2570 )
2571 ])
2572
2573 def test_fullname_crl_issuer_reasons(self, backend):
2574 cert = _load_cert(
2575 os.path.join(
2576 "x509", "custom", "cdp_fullname_reasons_crl_issuer.pem"
2577 ),
2578 x509.load_pem_x509_certificate,
2579 backend
2580 )
2581
2582 cdps = cert.extensions.get_extension_for_oid(
2583 x509.OID_CRL_DISTRIBUTION_POINTS
2584 ).value
2585
2586 assert cdps == x509.CRLDistributionPoints([
2587 x509.DistributionPoint(
2588 full_name=[x509.UniformResourceIdentifier(
2589 u"http://myhost.com/myca.crl"
2590 )],
2591 relative_name=None,
2592 reasons=frozenset([
2593 x509.ReasonFlags.key_compromise,
2594 x509.ReasonFlags.ca_compromise
2595 ]),
2596 crl_issuer=[x509.DirectoryName(
2597 x509.Name([
Ian Cordasco82fc3762015-06-16 20:59:50 -05002598 x509.NameAttribute(x509.OID_COUNTRY_NAME, u"US"),
Paul Kehrer9a10d592015-05-10 14:55:51 -05002599 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05002600 x509.OID_ORGANIZATION_NAME, u"PyCA"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002601 ),
2602 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05002603 x509.OID_COMMON_NAME, u"cryptography CA"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002604 ),
2605 ])
2606 )],
2607 )
2608 ])
2609
Paul Kehrer594a2ed2015-05-12 23:27:32 -05002610 def test_all_reasons(self, backend):
2611 cert = _load_cert(
2612 os.path.join(
2613 "x509", "custom", "cdp_all_reasons.pem"
2614 ),
2615 x509.load_pem_x509_certificate,
2616 backend
2617 )
2618
2619 cdps = cert.extensions.get_extension_for_oid(
2620 x509.OID_CRL_DISTRIBUTION_POINTS
2621 ).value
2622
2623 assert cdps == x509.CRLDistributionPoints([
2624 x509.DistributionPoint(
2625 full_name=[x509.UniformResourceIdentifier(
2626 u"http://domain.com/some.crl"
2627 )],
2628 relative_name=None,
2629 reasons=frozenset([
2630 x509.ReasonFlags.key_compromise,
2631 x509.ReasonFlags.ca_compromise,
2632 x509.ReasonFlags.affiliation_changed,
2633 x509.ReasonFlags.superseded,
2634 x509.ReasonFlags.privilege_withdrawn,
2635 x509.ReasonFlags.cessation_of_operation,
2636 x509.ReasonFlags.aa_compromise,
2637 x509.ReasonFlags.certificate_hold,
2638 ]),
2639 crl_issuer=None
2640 )
2641 ])
2642
2643 def test_single_reason(self, backend):
2644 cert = _load_cert(
2645 os.path.join(
2646 "x509", "custom", "cdp_reason_aa_compromise.pem"
2647 ),
2648 x509.load_pem_x509_certificate,
2649 backend
2650 )
2651
2652 cdps = cert.extensions.get_extension_for_oid(
2653 x509.OID_CRL_DISTRIBUTION_POINTS
2654 ).value
2655
2656 assert cdps == x509.CRLDistributionPoints([
2657 x509.DistributionPoint(
2658 full_name=[x509.UniformResourceIdentifier(
2659 u"http://domain.com/some.crl"
2660 )],
2661 relative_name=None,
2662 reasons=frozenset([x509.ReasonFlags.aa_compromise]),
2663 crl_issuer=None
2664 )
2665 ])
2666
Paul Kehrer9a10d592015-05-10 14:55:51 -05002667 def test_crl_issuer_only(self, backend):
2668 cert = _load_cert(
2669 os.path.join(
2670 "x509", "custom", "cdp_crl_issuer.pem"
2671 ),
2672 x509.load_pem_x509_certificate,
2673 backend
2674 )
2675
2676 cdps = cert.extensions.get_extension_for_oid(
2677 x509.OID_CRL_DISTRIBUTION_POINTS
2678 ).value
2679
2680 assert cdps == x509.CRLDistributionPoints([
2681 x509.DistributionPoint(
2682 full_name=None,
2683 relative_name=None,
2684 reasons=None,
2685 crl_issuer=[x509.DirectoryName(
2686 x509.Name([
2687 x509.NameAttribute(
Ian Cordasco82fc3762015-06-16 20:59:50 -05002688 x509.OID_COMMON_NAME, u"cryptography CA"
Paul Kehrer9a10d592015-05-10 14:55:51 -05002689 ),
2690 ])
2691 )],
2692 )
2693 ])
Paul Kehrer16fae762015-05-01 23:14:20 -05002694
2695
Paul Kehrerf54a50b2015-06-17 18:31:26 -06002696@pytest.mark.requires_backend_interface(interface=RSABackend)
2697@pytest.mark.requires_backend_interface(interface=X509Backend)
2698class TestOCSPNoCheckExtension(object):
2699 def test_nocheck(self, backend):
2700 cert = _load_cert(
2701 os.path.join(
2702 "x509", "custom", "ocsp_nocheck.pem"
2703 ),
2704 x509.load_pem_x509_certificate,
2705 backend
2706 )
2707 ext = cert.extensions.get_extension_for_oid(
2708 x509.OID_OCSP_NO_CHECK
2709 )
2710 assert isinstance(ext.value, x509.OCSPNoCheck)
2711
2712
Paul Kehrer16fae762015-05-01 23:14:20 -05002713class TestInhibitAnyPolicy(object):
2714 def test_not_int(self):
2715 with pytest.raises(TypeError):
2716 x509.InhibitAnyPolicy("notint")
2717
2718 def test_negative_int(self):
2719 with pytest.raises(ValueError):
2720 x509.InhibitAnyPolicy(-1)
2721
2722 def test_repr(self):
2723 iap = x509.InhibitAnyPolicy(0)
2724 assert repr(iap) == "<InhibitAnyPolicy(skip_certs=0)>"
2725
2726 def test_eq(self):
2727 iap = x509.InhibitAnyPolicy(1)
2728 iap2 = x509.InhibitAnyPolicy(1)
2729 assert iap == iap2
2730
2731 def test_ne(self):
2732 iap = x509.InhibitAnyPolicy(1)
2733 iap2 = x509.InhibitAnyPolicy(4)
2734 assert iap != iap2
2735 assert iap != object()
Paul Kehrerca6ce992015-06-17 22:13:15 -06002736
2737
2738@pytest.mark.requires_backend_interface(interface=RSABackend)
2739@pytest.mark.requires_backend_interface(interface=X509Backend)
2740class TestInhibitAnyPolicyExtension(object):
2741 def test_nocheck(self, backend):
2742 cert = _load_cert(
2743 os.path.join(
2744 "x509", "custom", "inhibit_any_policy_5.pem"
2745 ),
2746 x509.load_pem_x509_certificate,
2747 backend
2748 )
2749 iap = cert.extensions.get_extension_for_oid(
2750 x509.OID_INHIBIT_ANY_POLICY
2751 ).value
2752 assert iap.skip_certs == 5