blob: a646f4b7eef7497a44b3729533ba0f1d927d6ea1 [file] [log] [blame]
Paul Kehrer732cf642018-08-15 18:04:28 -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 Kehrer002fa752018-08-30 10:41:32 -04007import base64
Paul Kehrer732cf642018-08-15 18:04:28 -05008import os
9
10import pytest
11
Paul Kehrer002fa752018-08-30 10:41:32 -040012from cryptography import x509
Paul Kehrer732cf642018-08-15 18:04:28 -050013from cryptography.exceptions import UnsupportedAlgorithm
14from cryptography.hazmat.primitives import hashes, serialization
15from cryptography.x509 import ocsp
16
Paul Kehrer002fa752018-08-30 10:41:32 -040017from .test_x509 import _load_cert
Paul Kehrer732cf642018-08-15 18:04:28 -050018from ..utils import load_vectors_from_file
19
20
21def _load_data(filename, loader):
22 return load_vectors_from_file(
23 filename=filename,
24 loader=lambda data: loader(data.read()),
25 mode="rb"
26 )
27
28
Paul Kehrer002fa752018-08-30 10:41:32 -040029def _cert_and_issuer():
30 from cryptography.hazmat.backends.openssl.backend import backend
31 cert = _load_cert(
32 os.path.join("x509", "cryptography.io.pem"),
33 x509.load_pem_x509_certificate,
34 backend
35 )
36 issuer = _load_cert(
37 os.path.join("x509", "rapidssl_sha256_ca_g3.pem"),
38 x509.load_pem_x509_certificate,
39 backend
40 )
41 return cert, issuer
42
43
Paul Kehrer732cf642018-08-15 18:04:28 -050044class TestOCSPRequest(object):
45 def test_bad_request(self):
46 with pytest.raises(ValueError):
47 ocsp.load_der_ocsp_request(b"invalid")
48
Paul Kehrer0f629bb2018-08-31 10:47:56 -040049 def test_load_request(self):
Paul Kehrer732cf642018-08-15 18:04:28 -050050 req = _load_data(
51 os.path.join("x509", "ocsp", "req-sha1.der"),
52 ocsp.load_der_ocsp_request,
53 )
Paul Kehrer0f629bb2018-08-31 10:47:56 -040054 assert req.issuer_name_hash == (b"8\xcaF\x8c\x07D\x8d\xf4\x81\x96"
55 b"\xc7mmLpQ\x9e`\xa7\xbd")
56 assert req.issuer_key_hash == (b"yu\xbb\x84:\xcb,\xdez\t\xbe1"
57 b"\x1bC\xbc\x1c*MSX")
58 assert isinstance(req.hash_algorithm, hashes.SHA1)
59 assert req.serial_number == int(
Paul Kehrer732cf642018-08-15 18:04:28 -050060 "98D9E5C0B4C373552DF77C5D0F1EB5128E4945F9", 16
61 )
Paul Kehrer09403102018-09-09 21:57:21 -050062 assert len(req.extensions) == 0
63
64 def test_load_request_with_extensions(self):
65 req = _load_data(
66 os.path.join("x509", "ocsp", "req-ext-nonce.der"),
67 ocsp.load_der_ocsp_request,
68 )
69 assert len(req.extensions) == 1
70 ext = req.extensions[0]
71 assert ext.critical is False
72 assert ext.value == x509.OCSPNonce(
73 b"\x04\x10{\x80Z\x1d7&\xb8\xb8OH\xd2\xf8\xbf\xd7-\xfd"
74 )
Paul Kehrer732cf642018-08-15 18:04:28 -050075
Paul Kehrer0f629bb2018-08-31 10:47:56 -040076 def test_load_request_two_requests(self):
77 with pytest.raises(NotImplementedError):
78 _load_data(
79 os.path.join("x509", "ocsp", "req-multi-sha1.der"),
80 ocsp.load_der_ocsp_request,
81 )
Paul Kehrer732cf642018-08-15 18:04:28 -050082
83 def test_invalid_hash_algorithm(self):
84 req = _load_data(
85 os.path.join("x509", "ocsp", "req-invalid-hash-alg.der"),
86 ocsp.load_der_ocsp_request,
87 )
88 with pytest.raises(UnsupportedAlgorithm):
Paul Kehrer0f629bb2018-08-31 10:47:56 -040089 req.hash_algorithm
Paul Kehrer732cf642018-08-15 18:04:28 -050090
91 def test_serialize_request(self):
92 req_bytes = load_vectors_from_file(
93 filename=os.path.join("x509", "ocsp", "req-sha1.der"),
94 loader=lambda data: data.read(),
95 mode="rb"
96 )
97 req = ocsp.load_der_ocsp_request(req_bytes)
98 assert req.public_bytes(serialization.Encoding.DER) == req_bytes
99
100 def test_invalid_serialize_encoding(self):
101 req = _load_data(
102 os.path.join("x509", "ocsp", "req-sha1.der"),
103 ocsp.load_der_ocsp_request,
104 )
105 with pytest.raises(ValueError):
106 req.public_bytes("invalid")
107 with pytest.raises(ValueError):
108 req.public_bytes(serialization.Encoding.PEM)
Paul Kehrer002fa752018-08-30 10:41:32 -0400109
110
111class TestOCSPRequestBuilder(object):
Paul Kehrer0f629bb2018-08-31 10:47:56 -0400112 def test_add_two_certs(self):
113 cert, issuer = _cert_and_issuer()
114 builder = ocsp.OCSPRequestBuilder()
115 builder = builder.add_certificate(cert, issuer, hashes.SHA1())
116 with pytest.raises(ValueError):
117 builder.add_certificate(cert, issuer, hashes.SHA1())
118
Paul Kehrer002fa752018-08-30 10:41:32 -0400119 def test_create_ocsp_request_no_req(self):
120 builder = ocsp.OCSPRequestBuilder()
121 with pytest.raises(ValueError):
122 builder.build()
123
124 def test_create_ocsp_request_invalid_alg(self):
125 cert, issuer = _cert_and_issuer()
126 builder = ocsp.OCSPRequestBuilder()
127 with pytest.raises(ValueError):
Paul Kehrer0f629bb2018-08-31 10:47:56 -0400128 builder.add_certificate(cert, issuer, hashes.MD5())
Paul Kehrer002fa752018-08-30 10:41:32 -0400129
130 def test_create_ocsp_request_invalid_cert(self):
131 cert, issuer = _cert_and_issuer()
132 builder = ocsp.OCSPRequestBuilder()
133 with pytest.raises(TypeError):
Paul Kehrer0f629bb2018-08-31 10:47:56 -0400134 builder.add_certificate(b"notacert", issuer, hashes.SHA1())
Paul Kehrer002fa752018-08-30 10:41:32 -0400135
136 with pytest.raises(TypeError):
Paul Kehrer0f629bb2018-08-31 10:47:56 -0400137 builder.add_certificate(cert, b"notacert", hashes.SHA1())
Paul Kehrer002fa752018-08-30 10:41:32 -0400138
139 def test_create_ocsp_request(self):
140 cert, issuer = _cert_and_issuer()
141 builder = ocsp.OCSPRequestBuilder()
Paul Kehrer0f629bb2018-08-31 10:47:56 -0400142 builder = builder.add_certificate(cert, issuer, hashes.SHA1())
Paul Kehrer002fa752018-08-30 10:41:32 -0400143 req = builder.build()
144 serialized = req.public_bytes(serialization.Encoding.DER)
145 assert serialized == base64.b64decode(
146 b"MEMwQTA/MD0wOzAJBgUrDgMCGgUABBRAC0Z68eay0wmDug1gfn5ZN0gkxAQUw5zz"
147 b"/NNGCDS7zkZ/oHxb8+IIy1kCAj8g"
148 )