Botan 3.14.1
Crypto and TLS for C&
tls_extensions_cert_status_req.cpp
Go to the documentation of this file.
1/*
2* TLS Extension Certificate_Status_Request
3* (C) 2011,2012,2015,2016,2022 Jack Lloyd
4* 2016 Juraj Somorovsky
5* 2021 Elektrobit Automotive GmbH
6* 2022 Hannes Rantzsch, René Meusel, neXenio GmbH
7*
8* Botan is released under the Simplified BSD License (see license.txt)
9*/
10
11#include <botan/tls_extensions.h>
12
13#include <botan/tls_exceptn.h>
14#include <botan/tls_messages.h>
15#include <botan/internal/tls_messages_internal.h>
16#include <botan/internal/tls_reader.h>
17
18namespace Botan::TLS {
19
20namespace {
21class RFC6066_Empty_Certificate_Status_Request {
22 public:
23 RFC6066_Empty_Certificate_Status_Request() = default;
24
25 explicit RFC6066_Empty_Certificate_Status_Request(uint16_t extension_size) {
26 if(extension_size != 0) {
27 throw Decoding_Error("Received an unexpectedly non-empty Certificate_Status_Request");
28 }
29 }
30
31 std::vector<uint8_t> serialize() const { return {}; }
32};
33
34class RFC6066_Certificate_Status_Request {
35 public:
36 explicit RFC6066_Certificate_Status_Request(std::vector<uint8_t> names) : ocsp_names(std::move(names)) {
37 BOTAN_ARG_CHECK(ocsp_names.size() <= 0xFFFF, "OCSP responder_id_list is too long");
38 }
39
40 RFC6066_Certificate_Status_Request(TLS_Data_Reader& reader, uint16_t extension_size) {
41 if(extension_size == 0) {
42 throw Decoding_Error("Received an unexpectedly empty Certificate_Status_Request");
43 }
44
45 const uint8_t type = reader.get_byte();
46 if(type == 1 /* ocsp */) {
47 // RFC 6066 Section 8: OCSP CertificateStatusRequest is
48 // ResponderID responder_id_list<0..2^16-1>;
49 // Extensions request_extensions;
50 //
51 // for a total wire size of 1 (status_type) + 2 (resp_id_list len)
52 // + len_resp_id_list + 2 (request_ext len) + len_requ_ext.
53 if(extension_size < 5) {
54 throw Decoding_Error("Truncated OCSP CertificateStatusRequest");
55 }
56 const size_t len_resp_id_list = reader.get_uint16_t();
57 if(len_resp_id_list > static_cast<size_t>(extension_size) - 5) {
58 throw Decoding_Error("Inconsistent length in OCSP CertificateStatusRequest");
59 }
60 ocsp_names = reader.get_fixed<uint8_t>(len_resp_id_list);
61 const size_t len_requ_ext = reader.get_uint16_t();
62 if(len_resp_id_list + len_requ_ext + 5 != extension_size) {
63 throw Decoding_Error("Inconsistent length in OCSP CertificateStatusRequest");
64 }
65 extension_bytes = reader.get_fixed<uint8_t>(len_requ_ext);
66 } else {
67 // Other status types are filtered out by make_certificate_status_request()
68 throw Decoding_Error("Unexpected status_type in Certificate_Status_Request");
69 }
70 }
71
72 std::vector<uint8_t> serialize() const {
73 std::vector<uint8_t> buf;
74 buf.push_back(1); // status_type = ocsp
75 append_tls_length_value(buf, ocsp_names, 2);
76 append_tls_length_value(buf, extension_bytes, 2);
77 return buf;
78 }
79
80 std::vector<uint8_t> ocsp_names; // NOLINT(*-non-private-member-variable*)
81 std::vector<uint8_t> extension_bytes; // NOLINT(*-non-private-member-variable*)
82};
83
84} // namespace
85
86class Certificate_Status_Request_Internal {
87 private:
88 using Contents =
89 std::variant<RFC6066_Empty_Certificate_Status_Request, RFC6066_Certificate_Status_Request, Certificate_Status>;
90
91 public:
92 explicit Certificate_Status_Request_Internal(Contents c) : content(std::move(c)) {}
93
94 Contents content; // NOLINT(*-non-private-member-variable*)
95};
96
98 uint16_t extension_size,
99 Handshake_Type message_type,
100 Connection_Side from) {
101 // This parser needs to take TLS 1.2 and TLS 1.3 into account. The
102 // extension's content and structure is dependent on the context it
103 // was sent in (i.e. the enclosing handshake message). Below is a list
104 // of handshake messages this can appear in.
105 //
106 // TLS 1.2
107 // * Client Hello
108 // * Server Hello
109 //
110 // TLS 1.3
111 // * Client Hello
112 // * Certificate Request
113 // * Certificate (Entry)
114
115 // RFC 6066 8.
116 // In order to indicate their desire to receive certificate status
117 // information, clients MAY include an extension of type "status_request"
118 // in the (extended) client hello.
119 if(message_type == Handshake_Type::ClientHello) {
120 m_impl = std::make_unique<Certificate_Status_Request_Internal>(
121 RFC6066_Certificate_Status_Request(reader, extension_size));
122 }
123
124 // RFC 6066 8.
125 // If a server returns a "CertificateStatus" message, then the server MUST
126 // have included an extension of type "status_request" with empty
127 // "extension_data" in the extended server hello.
128 //
129 // RFC 8446 4.4.2.1
130 // A server MAY request that a client present an OCSP response with its
131 // certificate by sending an empty "status_request" extension in its
132 // CertificateRequest message.
133 else if(message_type == Handshake_Type::ServerHello || message_type == Handshake_Type::CertificateRequest) {
134 m_impl = std::make_unique<Certificate_Status_Request_Internal>(
135 RFC6066_Empty_Certificate_Status_Request(extension_size));
136 }
137
138 // RFC 8446 4.4.2.1
139 // In TLS 1.3, the server's OCSP information is carried in an extension
140 // in the CertificateEntry [in a Certificate handshake message] [...].
141 // Specifically, the body of the "status_request" extension from the
142 // server MUST be a CertificateStatus structure as defined in [RFC6066]
143 // [...].
144 //
145 // RFC 8446 4.4.2.1
146 // If the client opts to send an OCSP response, the body of its
147 // "status_request" extension MUST be a CertificateStatus structure as
148 // defined in [RFC6066].
149 else if(message_type == Handshake_Type::Certificate) {
150 m_impl = std::make_unique<Certificate_Status_Request_Internal>(
151 Certificate_Status(reader.get_fixed<uint8_t>(extension_size), from));
152 }
153
154 // all other contexts are not allowed for this extension
155 else {
156 throw TLS_Exception(Alert::UnsupportedExtension,
157 "Server sent a Certificate_Status_Request extension in an unsupported context");
158 }
159}
160
161std::unique_ptr<Extension> make_certificate_status_request(TLS_Data_Reader& reader,
162 uint16_t extension_size,
163 Handshake_Type message_type,
164 Connection_Side from) {
165 // Only the "ocsp" status_type is supported, a request for any other type is ignored
166 if(message_type == Handshake_Type::ClientHello && extension_size > 0 && reader.peek_byte() != 1 /* ocsp */) {
167 return std::make_unique<Unknown_Extension>(Certificate_Status_Request::static_type(), reader, extension_size);
168 }
169
170 return std::make_unique<Certificate_Status_Request>(reader, extension_size, message_type, from);
171}
172
174 m_impl(std::make_unique<Certificate_Status_Request_Internal>(RFC6066_Empty_Certificate_Status_Request())) {}
175
176// The public signature takes ocsp_key_ids by value
177// NOLINTBEGIN(performance-unnecessary-value-param)
178Certificate_Status_Request::Certificate_Status_Request(std::vector<uint8_t> ocsp_responder_ids,
179 std::vector<std::vector<uint8_t>> ocsp_key_ids) :
180 m_impl(std::make_unique<Certificate_Status_Request_Internal>(
181 RFC6066_Certificate_Status_Request(std::move(ocsp_responder_ids)))) {
182 // The OCSPStatusRequest structure has no place to carry these
183 BOTAN_ARG_CHECK(ocsp_key_ids.empty(), "Certificate_Status_Request does not support OCSP key IDs");
184}
185
186// NOLINTEND(performance-unnecessary-value-param)
187
189 m_impl(std::make_unique<Certificate_Status_Request_Internal>(Certificate_Status(std::move(response)))) {}
190
192
193const std::vector<uint8_t>& Certificate_Status_Request::get_responder_id_list() const {
194 BOTAN_ASSERT_NONNULL(m_impl);
195 BOTAN_STATE_CHECK(std::holds_alternative<RFC6066_Certificate_Status_Request>(m_impl->content));
196 return std::get<RFC6066_Certificate_Status_Request>(m_impl->content).ocsp_names;
197}
198
199const std::vector<uint8_t>& Certificate_Status_Request::get_request_extensions() const {
200 BOTAN_ASSERT_NONNULL(m_impl);
201 BOTAN_STATE_CHECK(std::holds_alternative<RFC6066_Certificate_Status_Request>(m_impl->content));
202 return std::get<RFC6066_Certificate_Status_Request>(m_impl->content).extension_bytes;
203}
204
205const std::vector<uint8_t>& Certificate_Status_Request::get_ocsp_response() const {
206 BOTAN_ASSERT_NONNULL(m_impl);
207 BOTAN_STATE_CHECK(std::holds_alternative<Certificate_Status>(m_impl->content));
208 return std::get<Certificate_Status>(m_impl->content).response();
209}
210
211std::vector<uint8_t> Certificate_Status_Request::serialize(Connection_Side /*side*/) const {
212 BOTAN_ASSERT_NONNULL(m_impl);
213 return std::visit([](const auto& c) { return c.serialize(); }, m_impl->content);
214}
215
216} // namespace Botan::TLS
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:121
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
const std::vector< uint8_t > & get_request_extensions() const
const std::vector< uint8_t > & get_responder_id_list() const
const std::vector< uint8_t > & get_ocsp_response() const
std::vector< uint8_t > serialize(Connection_Side whoami) const override
uint8_t peek_byte() const
Definition tls_reader.h:83
std::vector< T > get_fixed(size_t size)
Definition tls_reader.h:134
void append_tls_length_value(std::vector< uint8_t, Alloc > &buf, const T *vals, size_t vals_size, size_t tag_size)
Definition tls_reader.h:182
std::unique_ptr< Extension > make_certificate_status_request(TLS_Data_Reader &reader, uint16_t extension_size, Handshake_Type message_type, Connection_Side from)