Botan 3.4.0
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_reader.h>
16
17namespace Botan::TLS {
18
19namespace {
20class RFC6066_Empty_Certificate_Status_Request {
21 public:
22 RFC6066_Empty_Certificate_Status_Request() = default;
23
24 RFC6066_Empty_Certificate_Status_Request(uint16_t extension_size) {
25 if(extension_size != 0) {
26 throw Decoding_Error("Received an unexpectedly non-empty Certificate_Status_Request");
27 }
28 }
29
30 std::vector<uint8_t> serialize() const { return {}; }
31};
32
33class RFC6066_Certificate_Status_Request {
34 public:
35 RFC6066_Certificate_Status_Request(std::vector<uint8_t> names, std::vector<std::vector<uint8_t>> keys) :
36 ocsp_names(std::move(names)), ocsp_keys(std::move(keys)) {}
37
38 RFC6066_Certificate_Status_Request(TLS_Data_Reader& reader, uint16_t extension_size) {
39 if(extension_size == 0) {
40 throw Decoding_Error("Received an unexpectedly empty Certificate_Status_Request");
41 }
42
43 const uint8_t type = reader.get_byte();
44 if(type == 1 /* ocsp */) {
45 const size_t len_resp_id_list = reader.get_uint16_t();
46 ocsp_names = reader.get_fixed<uint8_t>(len_resp_id_list);
47 const size_t len_requ_ext = reader.get_uint16_t();
48 extension_bytes = reader.get_fixed<uint8_t>(len_requ_ext);
49 } else {
50 // RFC 6066 does not specify anything but 'ocsp' and we
51 // don't support anything else either.
52 reader.discard_next(extension_size - 1);
53 }
54 }
55
56 std::vector<uint8_t> serialize() const {
57 // Serialization is hard-coded as we don't support advanced features
58 // of this extension anyway.
59 return {
60 1, // status_type = ocsp
61 0,
62 0, // empty responder_id_list
63 0,
64 0, // no extensions
65 };
66 }
67
68 std::vector<uint8_t> ocsp_names; // NOLINT(*-non-private-member-variables-in-classes)
69 std::vector<std::vector<uint8_t>> ocsp_keys; // NOLINT(*-non-private-member-variables-in-classes)
70 std::vector<uint8_t> extension_bytes; // NOLINT(*-non-private-member-variables-in-classes)
71};
72
73} // namespace
74
75class Certificate_Status_Request_Internal {
76 private:
77 using Contents =
78 std::variant<RFC6066_Empty_Certificate_Status_Request, RFC6066_Certificate_Status_Request, Certificate_Status>;
79
80 public:
81 Certificate_Status_Request_Internal(Contents c) : content(std::move(c)) {}
82
83 Contents content; // NOLINT(*-non-private-member-variables-in-classes)
84};
85
87 uint16_t extension_size,
88 Handshake_Type message_type,
89 Connection_Side from) {
90 // This parser needs to take TLS 1.2 and TLS 1.3 into account. The
91 // extension's content and structure is dependent on the context it
92 // was sent in (i.e. the enclosing handshake message). Below is a list
93 // of handshake messages this can appear in.
94 //
95 // TLS 1.2
96 // * Client Hello
97 // * Server Hello
98 //
99 // TLS 1.3
100 // * Client Hello
101 // * Certificate Request
102 // * Certificate (Entry)
103
104 // RFC 6066 8.
105 // In order to indicate their desire to receive certificate status
106 // information, clients MAY include an extension of type "status_request"
107 // in the (extended) client hello.
108 if(message_type == Handshake_Type::ClientHello) {
109 m_impl = std::make_unique<Certificate_Status_Request_Internal>(
110 RFC6066_Certificate_Status_Request(reader, extension_size));
111 }
112
113 // RFC 6066 8.
114 // If a server returns a "CertificateStatus" message, then the server MUST
115 // have included an extension of type "status_request" with empty
116 // "extension_data" in the extended server hello.
117 //
118 // RFC 8446 4.4.2.1
119 // A server MAY request that a client present an OCSP response with its
120 // certificate by sending an empty "status_request" extension in its
121 // CertificateRequest message.
122 else if(message_type == Handshake_Type::ServerHello || message_type == Handshake_Type::CertificateRequest) {
123 m_impl = std::make_unique<Certificate_Status_Request_Internal>(
124 RFC6066_Empty_Certificate_Status_Request(extension_size));
125 }
126
127 // RFC 8446 4.4.2.1
128 // In TLS 1.3, the server's OCSP information is carried in an extension
129 // in the CertificateEntry [in a Certificate handshake message] [...].
130 // Specifically, the body of the "status_request" extension from the
131 // server MUST be a CertificateStatus structure as defined in [RFC6066]
132 // [...].
133 //
134 // RFC 8446 4.4.2.1
135 // If the client opts to send an OCSP response, the body of its
136 // "status_request" extension MUST be a CertificateStatus structure as
137 // defined in [RFC6066].
138 else if(message_type == Handshake_Type::Certificate) {
139 m_impl = std::make_unique<Certificate_Status_Request_Internal>(
140 Certificate_Status(reader.get_fixed<uint8_t>(extension_size), from));
141 }
142
143 // all other contexts are not allowed for this extension
144 else {
145 throw TLS_Exception(Alert::UnsupportedExtension,
146 "Server sent a Certificate_Status_Request extension in an unsupported context");
147 }
148}
149
151 m_impl(std::make_unique<Certificate_Status_Request_Internal>(RFC6066_Empty_Certificate_Status_Request())) {}
152
153Certificate_Status_Request::Certificate_Status_Request(std::vector<uint8_t> ocsp_responder_ids,
154 std::vector<std::vector<uint8_t>> ocsp_key_ids) :
155 m_impl(std::make_unique<Certificate_Status_Request_Internal>(
156 RFC6066_Certificate_Status_Request(std::move(ocsp_responder_ids), std::move(ocsp_key_ids)))) {}
157
159 m_impl(std::make_unique<Certificate_Status_Request_Internal>(Certificate_Status(std::move(response)))) {}
160
162
163const std::vector<uint8_t>& Certificate_Status_Request::get_ocsp_response() const {
164 BOTAN_ASSERT_NONNULL(m_impl);
165 BOTAN_STATE_CHECK(std::holds_alternative<Certificate_Status>(m_impl->content));
166 return std::get<Certificate_Status>(m_impl->content).response();
167}
168
170 BOTAN_ASSERT_NONNULL(m_impl);
171 return std::visit([](const auto& c) { return c.serialize(); }, m_impl->content);
172}
173
174} // namespace Botan::TLS
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:41
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:86
const std::vector< uint8_t > & get_ocsp_response() const
std::vector< uint8_t > serialize(Connection_Side whoami) const override
std::vector< T > get_fixed(size_t size)
Definition tls_reader.h:125
std::vector< std::vector< uint8_t > > ocsp_keys
std::vector< uint8_t > ocsp_names
std::vector< uint8_t > extension_bytes