Paul Kehrer | 732cf64 | 2018-08-15 18:04:28 -0500 | [diff] [blame] | 1 | # 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 | |
| 5 | from __future__ import absolute_import, division, print_function |
| 6 | |
Paul Kehrer | 002fa75 | 2018-08-30 10:41:32 -0400 | [diff] [blame] | 7 | import base64 |
Paul Kehrer | 732cf64 | 2018-08-15 18:04:28 -0500 | [diff] [blame] | 8 | import os |
| 9 | |
| 10 | import pytest |
| 11 | |
Paul Kehrer | 002fa75 | 2018-08-30 10:41:32 -0400 | [diff] [blame] | 12 | from cryptography import x509 |
Paul Kehrer | 732cf64 | 2018-08-15 18:04:28 -0500 | [diff] [blame] | 13 | from cryptography.exceptions import UnsupportedAlgorithm |
| 14 | from cryptography.hazmat.primitives import hashes, serialization |
| 15 | from cryptography.x509 import ocsp |
| 16 | |
Paul Kehrer | 002fa75 | 2018-08-30 10:41:32 -0400 | [diff] [blame] | 17 | from .test_x509 import _load_cert |
Paul Kehrer | 732cf64 | 2018-08-15 18:04:28 -0500 | [diff] [blame] | 18 | from ..utils import load_vectors_from_file |
| 19 | |
| 20 | |
| 21 | def _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 Kehrer | 002fa75 | 2018-08-30 10:41:32 -0400 | [diff] [blame] | 29 | def _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 Kehrer | 732cf64 | 2018-08-15 18:04:28 -0500 | [diff] [blame] | 44 | class TestOCSPRequest(object): |
| 45 | def test_bad_request(self): |
| 46 | with pytest.raises(ValueError): |
| 47 | ocsp.load_der_ocsp_request(b"invalid") |
| 48 | |
Paul Kehrer | 0f629bb | 2018-08-31 10:47:56 -0400 | [diff] [blame] | 49 | def test_load_request(self): |
Paul Kehrer | 732cf64 | 2018-08-15 18:04:28 -0500 | [diff] [blame] | 50 | req = _load_data( |
| 51 | os.path.join("x509", "ocsp", "req-sha1.der"), |
| 52 | ocsp.load_der_ocsp_request, |
| 53 | ) |
Paul Kehrer | 0f629bb | 2018-08-31 10:47:56 -0400 | [diff] [blame] | 54 | 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 Kehrer | 732cf64 | 2018-08-15 18:04:28 -0500 | [diff] [blame] | 60 | "98D9E5C0B4C373552DF77C5D0F1EB5128E4945F9", 16 |
| 61 | ) |
Paul Kehrer | 0940310 | 2018-09-09 21:57:21 -0500 | [diff] [blame^] | 62 | 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 Kehrer | 732cf64 | 2018-08-15 18:04:28 -0500 | [diff] [blame] | 75 | |
Paul Kehrer | 0f629bb | 2018-08-31 10:47:56 -0400 | [diff] [blame] | 76 | 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 Kehrer | 732cf64 | 2018-08-15 18:04:28 -0500 | [diff] [blame] | 82 | |
| 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 Kehrer | 0f629bb | 2018-08-31 10:47:56 -0400 | [diff] [blame] | 89 | req.hash_algorithm |
Paul Kehrer | 732cf64 | 2018-08-15 18:04:28 -0500 | [diff] [blame] | 90 | |
| 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 Kehrer | 002fa75 | 2018-08-30 10:41:32 -0400 | [diff] [blame] | 109 | |
| 110 | |
| 111 | class TestOCSPRequestBuilder(object): |
Paul Kehrer | 0f629bb | 2018-08-31 10:47:56 -0400 | [diff] [blame] | 112 | 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 Kehrer | 002fa75 | 2018-08-30 10:41:32 -0400 | [diff] [blame] | 119 | 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 Kehrer | 0f629bb | 2018-08-31 10:47:56 -0400 | [diff] [blame] | 128 | builder.add_certificate(cert, issuer, hashes.MD5()) |
Paul Kehrer | 002fa75 | 2018-08-30 10:41:32 -0400 | [diff] [blame] | 129 | |
| 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 Kehrer | 0f629bb | 2018-08-31 10:47:56 -0400 | [diff] [blame] | 134 | builder.add_certificate(b"notacert", issuer, hashes.SHA1()) |
Paul Kehrer | 002fa75 | 2018-08-30 10:41:32 -0400 | [diff] [blame] | 135 | |
| 136 | with pytest.raises(TypeError): |
Paul Kehrer | 0f629bb | 2018-08-31 10:47:56 -0400 | [diff] [blame] | 137 | builder.add_certificate(cert, b"notacert", hashes.SHA1()) |
Paul Kehrer | 002fa75 | 2018-08-30 10:41:32 -0400 | [diff] [blame] | 138 | |
| 139 | def test_create_ocsp_request(self): |
| 140 | cert, issuer = _cert_and_issuer() |
| 141 | builder = ocsp.OCSPRequestBuilder() |
Paul Kehrer | 0f629bb | 2018-08-31 10:47:56 -0400 | [diff] [blame] | 142 | builder = builder.add_certificate(cert, issuer, hashes.SHA1()) |
Paul Kehrer | 002fa75 | 2018-08-30 10:41:32 -0400 | [diff] [blame] | 143 | 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 | ) |