Botan 3.11.0
Crypto and TLS for C&
msg_certificate_13.cpp
Go to the documentation of this file.
1/*
2* Certificate Message
3* (C) 2022 Jack Lloyd
4* 2022 Hannes Rantzsch, René Meusel - neXenio GmbH
5* 2023 René Meusel, Fabian Albert - Rohde & Schwarz Cybersecurity
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#include <botan/tls_messages_13.h>
11
12#include <botan/credentials_manager.h>
13#include <botan/ocsp.h>
14#include <botan/tls_alert.h>
15#include <botan/tls_callbacks.h>
16#include <botan/tls_exceptn.h>
17#include <botan/tls_extensions.h>
18#include <botan/tls_policy.h>
19#include <botan/x509_key.h>
20#include <botan/internal/tls_reader.h>
21#include <algorithm>
22#include <iterator>
23#include <memory>
24
25namespace Botan::TLS {
26
27namespace {
28
29bool certificate_allows_signing(const X509_Certificate& cert) {
30 const auto constraints = cert.constraints();
31 if(constraints.empty()) {
32 return true;
33 }
34
36}
37
38std::vector<std::string> filter_signature_schemes(const std::vector<Signature_Scheme>& peer_scheme_preference) {
39 std::vector<std::string> compatible_schemes;
40 for(const auto& scheme : peer_scheme_preference) {
41 if(scheme.is_available() && scheme.is_compatible_with(Protocol_Version::TLS_V13)) {
42 compatible_schemes.push_back(scheme.algorithm_name());
43 }
44 }
45
46 if(compatible_schemes.empty()) {
47 throw TLS_Exception(Alert::HandshakeFailure, "Failed to agree on any signature algorithm");
48 }
49
50 return compatible_schemes;
51}
52
53} // namespace
54
56 return !empty() && m_entries.front().has_certificate();
57}
58
60 return !empty() && !has_certificate_chain();
61}
62
63std::vector<X509_Certificate> Certificate_13::cert_chain() const {
65 std::vector<X509_Certificate> result;
66 std::transform(m_entries.cbegin(), m_entries.cend(), std::back_inserter(result), [](const auto& cert_entry) {
67 return cert_entry.certificate();
68 });
69 return result;
70}
71
72void Certificate_13::validate_extensions(const std::set<Extension_Code>& requested_extensions, Callbacks& cb) const {
73 // RFC 8446 4.4.2
74 // Extensions in the Certificate message from the server MUST
75 // correspond to ones from the ClientHello message. Extensions in
76 // the Certificate message from the client MUST correspond to
77 // extensions in the CertificateRequest message from the server.
78 for(const auto& entry : m_entries) {
79 if(entry.extensions().contains_other_than(requested_extensions)) {
80 throw TLS_Exception(Alert::IllegalParameter, "Certificate Entry contained an extension that was not offered");
81 }
82
83 cb.tls_examine_extensions(entry.extensions(), m_side, type());
84 }
85}
86
87std::shared_ptr<const Public_Key> Certificate_13::public_key() const {
89 return m_entries.front().public_key();
90}
91
94 return m_entries.front().certificate();
95}
96
98 const Policy& policy,
100 std::string_view hostname,
101 bool use_ocsp) const {
103
104 if(is_raw_public_key()) {
105 callbacks.tls_verify_raw_public_key(*public_key(), usage, hostname, policy);
106 } else {
107 verify_certificate_chain(callbacks, policy, creds, hostname, use_ocsp, usage);
108 }
109}
110
111void Certificate_13::verify_certificate_chain(Callbacks& callbacks,
112 const Policy& policy,
113 Credentials_Manager& creds,
114 std::string_view hostname,
115 bool use_ocsp,
116 Usage_Type usage_type) const {
117 std::vector<X509_Certificate> certs;
118 std::vector<std::optional<OCSP::Response>> ocsp_responses;
119 for(const auto& entry : m_entries) {
120 certs.push_back(entry.certificate());
121 if(use_ocsp) {
122 if(entry.extensions().has<Certificate_Status_Request>()) {
123 ocsp_responses.push_back(callbacks.tls_parse_ocsp_response(
124 entry.extensions().get<Certificate_Status_Request>()->get_ocsp_response()));
125 } else {
126 ocsp_responses.emplace_back();
127 }
128 }
129 }
130
131 const auto& server_cert = m_entries.front().certificate();
132 if(!certificate_allows_signing(server_cert)) {
133 throw TLS_Exception(Alert::BadCertificate, "Certificate usage constraints do not allow signing");
134 }
135
136 // Note that m_side represents the sender, so the usages here are swapped
137 const auto trusted_CAs = creds.trusted_certificate_authorities(
138 m_side == Connection_Side::Client ? "tls-server" : "tls-client", std::string(hostname));
139
140 callbacks.tls_verify_cert_chain(certs, ocsp_responses, trusted_CAs, usage_type, hostname, policy);
141}
142
143void Certificate_13::setup_entries(std::vector<X509_Certificate> cert_chain,
145 Callbacks& callbacks) {
146 // RFC 8446 4.4.2.1
147 // A server MAY request that a client present an OCSP response with its
148 // certificate by sending an empty "status_request" extension in its
149 // CertificateRequest message.
150 const auto ocsp_responses = (csr != nullptr) ? callbacks.tls_provide_cert_chain_status(cert_chain, *csr)
151 : std::vector<std::vector<uint8_t>>(cert_chain.size());
152
153 if(ocsp_responses.size() != cert_chain.size()) {
154 throw TLS_Exception(Alert::InternalError, "Application didn't provide the correct number of OCSP responses");
155 }
156
157 for(size_t i = 0; i < cert_chain.size(); ++i) {
158 auto& entry = m_entries.emplace_back(cert_chain[i]);
159 if(!ocsp_responses[i].empty()) {
160 entry.extensions().add(new Certificate_Status_Request(ocsp_responses[i])); // NOLINT(*-owning-memory)
161 }
162
163 // This will call the modification callback multiple times. Once for
164 // each certificate in the `cert_chain`. Users that want to add an
165 // extension to a specific Certificate Entry might have a hard time
166 // to distinguish them.
167 //
168 // TODO: Callbacks::tls_modify_extensions() might need even more
169 // context depending on the message whose extensions should be
170 // manipulatable.
171 callbacks.tls_modify_extensions(entry.extensions(), m_side, type());
172 }
173}
174
175void Certificate_13::setup_entry(std::shared_ptr<Public_Key> raw_public_key, Callbacks& callbacks) {
176 BOTAN_ASSERT_NONNULL(raw_public_key);
177 auto& entry = m_entries.emplace_back(std::move(raw_public_key));
178 callbacks.tls_modify_extensions(entry.extensions(), m_side, type());
179}
180
181/**
182 * Create a Client Certificate message
183 */
185 std::string_view hostname,
186 Credentials_Manager& credentials_manager,
187 Callbacks& callbacks,
188 Certificate_Type cert_type) :
189 m_request_context(cert_request.context()), m_side(Connection_Side::Client) {
190 const auto key_types = filter_signature_schemes(cert_request.signature_schemes());
191 const std::string op_type = "tls-client";
192
193 if(cert_type == Certificate_Type::X509) {
194 setup_entries(
195 credentials_manager.find_cert_chain(key_types,
197 cert_request.acceptable_CAs(),
198 op_type,
199 std::string(hostname)),
200 cert_request.extensions().get<Certificate_Status_Request>(),
201 callbacks);
202 } else if(cert_type == Certificate_Type::RawPublicKey) {
203 auto raw_public_key = credentials_manager.find_raw_public_key(key_types, op_type, std::string(hostname));
204
205 // RFC 8446 4.4.2
206 // If the RawPublicKey certificate type was negotiated, then the
207 // certificate_list MUST contain no more than one CertificateEntry
208 // [...].
209 // A client will send an empty certificate_list if it does not have
210 // an appropriate certificate to send in response to the server's
211 // authentication request.
212 if(raw_public_key) {
213 setup_entry(std::move(raw_public_key), callbacks);
214 }
215 }
216}
217
218/**
219 * Create a Server Certificate message
220 */
222 Credentials_Manager& credentials_manager,
223 Callbacks& callbacks,
224 Certificate_Type cert_type) :
225 // RFC 8446 4.4.2:
226 // [In the case of server authentication], the request context
227 // SHALL be zero length
228 m_request_context(/* NOLINT(*-redundant-member-init) */), m_side(Connection_Side::Server) {
230
231 const auto key_types = filter_signature_schemes(client_hello.signature_schemes());
232 const std::string op_type = "tls-server";
233 const std::string context = client_hello.sni_hostname();
234
235 if(cert_type == Certificate_Type::X509) {
236 auto cert_chain = credentials_manager.find_cert_chain(
237 key_types, to_algorithm_identifiers(client_hello.certificate_signature_schemes()), {}, op_type, context);
238
239 // RFC 8446 4.4.2
240 // The server's certificate_list MUST always be non-empty.
241 if(cert_chain.empty()) {
242 throw TLS_Exception(Alert::HandshakeFailure, "No sufficient server certificate available");
243 }
244
245 setup_entries(std::move(cert_chain), client_hello.extensions().get<Certificate_Status_Request>(), callbacks);
246 } else if(cert_type == Certificate_Type::RawPublicKey) {
247 auto raw_public_key = credentials_manager.find_raw_public_key(key_types, op_type, context);
248
249 // RFC 8446 4.4.2
250 // If the RawPublicKey certificate type was negotiated, then the
251 // certificate_list MUST contain no more than one CertificateEntry
252 // [...].
253 // The server's certificate_list MUST always be non-empty
254 if(!raw_public_key) {
255 throw TLS_Exception(Alert::HandshakeFailure, "No sufficient server raw public key available");
256 }
257
258 setup_entry(std::move(raw_public_key), callbacks);
259 }
260}
261
263 Connection_Side side,
264 Certificate_Type cert_type) {
265 switch(cert_type) {
267 // RFC 8446 4.2.2
268 // [...] each CertificateEntry contains a DER-encoded X.509
269 // certificate.
270 m_certificate = std::make_unique<X509_Certificate>(reader.get_tls_length_value(3));
271 m_raw_public_key = m_certificate->subject_public_key();
272 break;
274 // RFC 7250 3.
275 // This specification uses raw public keys whereby the already
276 // available encoding used in a PKIX certificate in the form of a
277 // SubjectPublicKeyInfo structure is reused.
278 m_raw_public_key = X509::load_key(reader.get_tls_length_value(3));
279 break;
280 default:
281 throw TLS_Exception(Alert::InternalError, "Unknown certificate type");
282 }
283
284 // Extensions are simply tacked at the end of the certificate entry. This
285 // is a departure from the typical "tag-length-value" in a sense that the
286 // Extensions deserializer needs the length value of the extensions.
287 const auto extensions_length = reader.peek_uint16_t();
288 const auto exts_buf = reader.get_fixed<uint8_t>(extensions_length + 2);
289 TLS_Data_Reader exts_reader("extensions reader", exts_buf);
290 m_extensions.deserialize(exts_reader, side, Handshake_Type::Certificate);
291
292 if(cert_type == Certificate_Type::X509) {
293 // RFC 8446 4.4.2
294 // Valid extensions for server certificates at present include the
295 // OCSP Status extension [RFC6066] and the SignedCertificateTimestamp
296 // extension [RFC6962]; future extensions may be defined for this
297 // message as well.
298 //
299 // RFC 8446 4.4.2.1
300 // A server MAY request that a client present an OCSP response with its
301 // certificate by sending an empty "status_request" extension in its
302 // CertificateRequest message.
303 if(m_extensions.contains_implemented_extensions_other_than({
304 Extension_Code::CertificateStatusRequest,
305 // Extension_Code::SignedCertificateTimestamp
306 })) {
307 throw TLS_Exception(Alert::IllegalParameter, "Certificate Entry contained an extension that is not allowed");
308 }
309 } else if(m_extensions.contains_implemented_extensions_other_than({})) {
310 throw TLS_Exception(
311 Alert::IllegalParameter,
312 "Certificate Entry holding something else than a certificate contained unexpected extensions");
313 }
314}
315
317
320 Certificate_13::Certificate_Entry&& other) noexcept = default;
321
323 m_certificate(std::make_unique<X509_Certificate>(cert)), m_raw_public_key(m_certificate->subject_public_key()) {}
324
325Certificate_13::Certificate_Entry::Certificate_Entry(std::shared_ptr<Public_Key> raw_public_key) :
326 m_raw_public_key(std::move(raw_public_key)) {
327 BOTAN_ASSERT_NONNULL(m_raw_public_key);
328}
329
334
335std::shared_ptr<const Public_Key> Certificate_13::Certificate_Entry::public_key() const {
336 BOTAN_ASSERT_NONNULL(m_raw_public_key);
337 return m_raw_public_key;
338}
339
341 return (has_certificate()) ? m_certificate->BER_encode() : X509::BER_encode(*m_raw_public_key);
342}
343
344/**
345* Deserialize a Certificate message
346*/
347Certificate_13::Certificate_13(const std::vector<uint8_t>& buf,
348 const Policy& policy,
349 Connection_Side side,
350 Certificate_Type cert_type) :
351 m_side(side) {
352 TLS_Data_Reader reader("cert message reader", buf);
353
354 m_request_context = reader.get_range<uint8_t>(1, 0, 255);
355
356 // RFC 8446 4.4.2
357 // [...] in the case of server authentication, this field SHALL be zero length.
358 if(m_side == Connection_Side::Server && !m_request_context.empty()) {
359 throw TLS_Exception(Alert::IllegalParameter, "Server Certificate message must not contain a request context");
360 }
361
362 const auto cert_entries_len = reader.get_uint24_t();
363
364 if(reader.remaining_bytes() != cert_entries_len) {
365 throw TLS_Exception(Alert::DecodeError, "Certificate: Message malformed");
366 }
367
368 const size_t max_size = policy.maximum_certificate_chain_size();
369 if(max_size > 0 && cert_entries_len > max_size) {
370 throw Decoding_Error("Certificate chain exceeds policy specified maximum size");
371 }
372
373 while(reader.has_remaining()) {
374 m_entries.emplace_back(reader, side, cert_type);
375 }
376
377 // RFC 8446 4.4.2
378 // The server's certificate_list MUST always be non-empty. A client
379 // will send an empty certificate_list if it does not have an
380 // appropriate certificate to send in response to the server's
381 // authentication request.
382 if(m_entries.empty()) {
383 // RFC 8446 4.4.2.4
384 // If the server supplies an empty Certificate message, the client MUST
385 // abort the handshake with a "decode_error" alert.
386 if(m_side == Connection_Side::Server) {
387 throw TLS_Exception(Alert::DecodeError, "No certificates sent by server");
388 }
389
390 return;
391 }
392
393 BOTAN_ASSERT_NOMSG(!m_entries.empty());
394
395 // RFC 8446 4.4.2.2
396 // The certificate type MUST be X.509v3 [RFC5280], unless explicitly
397 // negotiated otherwise (e.g., [RFC7250]).
398 //
399 // TLS 1.0 through 1.3 all seem to require that the certificate be
400 // precisely a v3 certificate. In fact the strict wording would seem
401 // to require that every certificate in the chain be v3. But often
402 // the intermediates are outside of the control of the server.
403 // But, require that the leaf certificate be v3.
404 if(cert_type == Certificate_Type::X509 && m_entries.front().certificate().x509_version() != 3) {
405 throw TLS_Exception(Alert::BadCertificate, "The leaf certificate must be v3");
406 }
407
408 // RFC 8446 4.4.2
409 // If the RawPublicKey certificate type was negotiated, then the
410 // certificate_list MUST contain no more than one CertificateEntry.
411 if(cert_type == Certificate_Type::RawPublicKey && m_entries.size() != 1) {
412 throw TLS_Exception(Alert::IllegalParameter, "Certificate message contained more than one RawPublicKey");
413 }
414
415 // Validate the provided (certificate) public key against our policy
416 auto pubkey = public_key();
417 policy.check_peer_key_acceptable(*pubkey);
418
419 if(!policy.allowed_signature_method(pubkey->algo_name())) {
420 throw TLS_Exception(Alert::HandshakeFailure, "Rejecting " + pubkey->algo_name() + " signature");
421 }
422}
423
424/**
425* Serialize a Certificate message
426*/
427std::vector<uint8_t> Certificate_13::serialize() const {
428 std::vector<uint8_t> buf;
429
430 append_tls_length_value(buf, m_request_context, 1);
431
432 std::vector<uint8_t> entries;
433 for(const auto& entry : m_entries) {
434 append_tls_length_value(entries, entry.serialize(), 3);
435
436 // Extensions are tacked at the end of certificate entries. Note that
437 // Extensions::serialize() usually emits the required length field,
438 // except when no extensions are added at all, then it returns an
439 // empty buffer.
440 //
441 // TODO: look into this issue more generally when overhauling the
442 // message marshalling.
443 auto extensions = entry.extensions().serialize(m_side);
444 entries += (!extensions.empty()) ? extensions : std::vector<uint8_t>{0, 0};
445 }
446
447 append_tls_length_value(buf, entries, 3);
448
449 return buf;
450}
451
452} // namespace Botan::TLS
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:114
virtual std::vector< Certificate_Store * > trusted_certificate_authorities(const std::string &type, const std::string &context)
virtual std::vector< X509_Certificate > find_cert_chain(const std::vector< std::string > &cert_key_types, const std::vector< AlgorithmIdentifier > &cert_signature_schemes, const std::vector< X509_DN > &acceptable_CAs, const std::string &type, const std::string &context)
virtual std::shared_ptr< Public_Key > find_raw_public_key(const std::vector< std::string > &key_types, const std::string &type, const std::string &context)
virtual std::optional< OCSP::Response > tls_parse_ocsp_response(const std::vector< uint8_t > &raw_response)
virtual void tls_examine_extensions(const Extensions &extn, Connection_Side which_side, Handshake_Type which_message)
virtual void tls_verify_raw_public_key(const Public_Key &raw_public_key, Usage_Type usage, std::string_view hostname, const TLS::Policy &policy)
virtual void tls_verify_cert_chain(const std::vector< X509_Certificate > &cert_chain, const std::vector< std::optional< OCSP::Response > > &ocsp_responses, const std::vector< Certificate_Store * > &trusted_roots, Usage_Type usage, std::string_view hostname, const TLS::Policy &policy)
Certificate_Entry & operator=(const Certificate_Entry &other)=delete
const X509_Certificate & certificate() const
std::shared_ptr< const Public_Key > public_key() const
Certificate_Entry(TLS_Data_Reader &reader, Connection_Side side, Certificate_Type cert_type)
~Certificate_Entry()
bool has_certificate() const
std::vector< uint8_t > serialize() const
const X509_Certificate & leaf() const
void validate_extensions(const std::set< Extension_Code > &requested_extensions, Callbacks &cb) const
std::shared_ptr< const Public_Key > public_key() const
Handshake_Type type() const override
void verify(Callbacks &callbacks, const Policy &policy, Credentials_Manager &creds, std::string_view hostname, bool use_ocsp) const
std::vector< uint8_t > serialize() const override
std::vector< X509_Certificate > cert_chain() const
Certificate_13(const Certificate_Request_13 &cert_request, std::string_view hostname, Credentials_Manager &credentials_manager, Callbacks &callbacks, Certificate_Type cert_type)
const std::vector< Signature_Scheme > & signature_schemes() const
const Extensions & extensions() const
const std::vector< Signature_Scheme > & certificate_signature_schemes() const
std::vector< X509_DN > acceptable_CAs() const
const std::vector< uint8_t > & get_ocsp_response() const
std::string sni_hostname() const
std::vector< Signature_Scheme > signature_schemes() const
const Extensions & extensions() const
std::vector< Signature_Scheme > certificate_signature_schemes() const
virtual void check_peer_key_acceptable(const Public_Key &public_key) const
bool allowed_signature_method(std::string_view sig_method) const
virtual size_t maximum_certificate_chain_size() const
std::vector< T > get_range(size_t len_bytes, size_t min_elems, size_t max_elems)
Definition tls_reader.h:110
size_t remaining_bytes() const
Definition tls_reader.h:37
std::vector< uint8_t > get_tls_length_value(size_t len_bytes)
Definition tls_reader.h:105
std::vector< T > get_fixed(size_t size)
Definition tls_reader.h:129
uint16_t peek_uint16_t() const
Definition tls_reader.h:78
std::vector< AlgorithmIdentifier > to_algorithm_identifiers(const std::vector< Signature_Scheme > &schemes)
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:177
std::vector< uint8_t > BER_encode(const Public_Key &key)
Definition x509_key.h:24
std::unique_ptr< Public_Key > load_key(DataSource &source)
Definition x509_key.cpp:28