Botan 3.10.0
Crypto and TLS for C&
Botan::TLS::Certificate_13 Class Referencefinal

#include <tls_messages.h>

Inheritance diagram for Botan::TLS::Certificate_13:
Botan::TLS::Handshake_Message

Classes

class  Certificate_Entry

Public Member Functions

std::vector< X509_Certificatecert_chain () const
 Certificate_13 (const Certificate_Request_13 &cert_request, std::string_view hostname, Credentials_Manager &credentials_manager, Callbacks &callbacks, Certificate_Type cert_type)
 Certificate_13 (const Client_Hello_13 &client_hello, Credentials_Manager &credentials_manager, Callbacks &callbacks, Certificate_Type cert_type)
 Certificate_13 (const std::vector< uint8_t > &buf, const Policy &policy, Connection_Side side, Certificate_Type cert_type)
size_t count () const
bool empty () const
bool has_certificate_chain () const
bool is_raw_public_key () const
const X509_Certificateleaf () const
std::shared_ptr< const Public_Keypublic_key () const
const std::vector< uint8_t > & request_context () const
std::vector< uint8_t > serialize () const override
Handshake_Type type () const override
std::string type_string () const
void validate_extensions (const std::set< Extension_Code > &requested_extensions, Callbacks &cb) const
void verify (Callbacks &callbacks, const Policy &policy, Credentials_Manager &creds, std::string_view hostname, bool use_ocsp) const
virtual Handshake_Type wire_type () const

Detailed Description

Certificate Message of TLS 1.3

Definition at line 549 of file tls_messages.h.

Constructor & Destructor Documentation

◆ Certificate_13() [1/3]

Botan::TLS::Certificate_13::Certificate_13 ( const Certificate_Request_13 & cert_request,
std::string_view hostname,
Credentials_Manager & credentials_manager,
Callbacks & callbacks,
Certificate_Type cert_type )

Create a Client Certificate message ... in response to a Certificate Request message.

Create a Client Certificate message

Definition at line 183 of file msg_certificate_13.cpp.

187 :
188 m_request_context(cert_request.context()), m_side(Connection_Side::Client) {
189 const auto key_types = filter_signature_schemes(cert_request.signature_schemes());
190 const std::string op_type = "tls-client";
191
192 if(cert_type == Certificate_Type::X509) {
193 setup_entries(
194 credentials_manager.find_cert_chain(key_types,
195 to_algorithm_identifiers(cert_request.certificate_signature_schemes()),
196 cert_request.acceptable_CAs(),
197 op_type,
198 std::string(hostname)),
199 cert_request.extensions().get<Certificate_Status_Request>(),
200 callbacks);
201 } else if(cert_type == Certificate_Type::RawPublicKey) {
202 auto raw_public_key = credentials_manager.find_raw_public_key(key_types, op_type, std::string(hostname));
203
204 // RFC 8446 4.4.2
205 // If the RawPublicKey certificate type was negotiated, then the
206 // certificate_list MUST contain no more than one CertificateEntry
207 // [...].
208 // A client will send an empty certificate_list if it does not have
209 // an appropriate certificate to send in response to the server's
210 // authentication request.
211 if(raw_public_key) {
212 setup_entry(std::move(raw_public_key), callbacks);
213 }
214 }
215}
std::vector< AlgorithmIdentifier > to_algorithm_identifiers(const std::vector< Signature_Scheme > &schemes)

References Botan::TLS::Certificate_Request_13::acceptable_CAs(), Botan::TLS::Certificate_Request_13::certificate_signature_schemes(), Botan::TLS::Certificate_Request_13::extensions(), Botan::Credentials_Manager::find_cert_chain(), Botan::Credentials_Manager::find_raw_public_key(), Botan::TLS::Extensions::get(), Botan::TLS::RawPublicKey, Botan::TLS::Certificate_Request_13::signature_schemes(), Botan::TLS::to_algorithm_identifiers(), and Botan::TLS::X509.

◆ Certificate_13() [2/3]

Botan::TLS::Certificate_13::Certificate_13 ( const Client_Hello_13 & client_hello,
Credentials_Manager & credentials_manager,
Callbacks & callbacks,
Certificate_Type cert_type )

Create a Server Certificate message ... in response to a Client Hello indicating the need to authenticate with a server certificate.

Create a Server Certificate message

Definition at line 220 of file msg_certificate_13.cpp.

223 :
224 // RFC 8446 4.4.2:
225 // [In the case of server authentication], the request context
226 // SHALL be zero length
227 m_request_context(/* NOLINT(*-redundant-member-init) */), m_side(Connection_Side::Server) {
228 BOTAN_ASSERT_NOMSG(client_hello.extensions().has<Signature_Algorithms>());
229
230 const auto key_types = filter_signature_schemes(client_hello.signature_schemes());
231 const std::string op_type = "tls-server";
232 const std::string context = client_hello.sni_hostname();
233
234 if(cert_type == Certificate_Type::X509) {
235 auto cert_chain = credentials_manager.find_cert_chain(
236 key_types, to_algorithm_identifiers(client_hello.certificate_signature_schemes()), {}, op_type, context);
237
238 // RFC 8446 4.4.2
239 // The server's certificate_list MUST always be non-empty.
240 if(cert_chain.empty()) {
241 throw TLS_Exception(Alert::HandshakeFailure, "No sufficient server certificate available");
242 }
243
244 setup_entries(std::move(cert_chain), client_hello.extensions().get<Certificate_Status_Request>(), callbacks);
245 } else if(cert_type == Certificate_Type::RawPublicKey) {
246 auto raw_public_key = credentials_manager.find_raw_public_key(key_types, op_type, context);
247
248 // RFC 8446 4.4.2
249 // If the RawPublicKey certificate type was negotiated, then the
250 // certificate_list MUST contain no more than one CertificateEntry
251 // [...].
252 // The server's certificate_list MUST always be non-empty
253 if(!raw_public_key) {
254 throw TLS_Exception(Alert::HandshakeFailure, "No sufficient server raw public key available");
255 }
256
257 setup_entry(std::move(raw_public_key), callbacks);
258 }
259}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
std::vector< X509_Certificate > cert_chain() const

References BOTAN_ASSERT_NOMSG, cert_chain(), Botan::TLS::Client_Hello::certificate_signature_schemes(), Botan::TLS::Client_Hello::extensions(), Botan::Credentials_Manager::find_cert_chain(), Botan::Credentials_Manager::find_raw_public_key(), Botan::TLS::Extensions::get(), Botan::TLS::Extensions::has(), Botan::TLS::RawPublicKey, Botan::TLS::Client_Hello::signature_schemes(), Botan::TLS::Client_Hello::sni_hostname(), Botan::TLS::to_algorithm_identifiers(), and Botan::TLS::X509.

◆ Certificate_13() [3/3]

Botan::TLS::Certificate_13::Certificate_13 ( const std::vector< uint8_t > & buf,
const Policy & policy,
Connection_Side side,
Certificate_Type cert_type )

Deserialize a Certificate message

Parameters
bufthe serialized message
policythe TLS policy
sideis this a Connection_Side::Server or Connection_Side::Client certificate message
cert_typeis the certificate type that was negotiated during the handshake

Deserialize a Certificate message

Definition at line 340 of file msg_certificate_13.cpp.

343 :
344 m_side(side) {
345 TLS_Data_Reader reader("cert message reader", buf);
346
347 m_request_context = reader.get_range<uint8_t>(1, 0, 255);
348
349 // RFC 8446 4.4.2
350 // [...] in the case of server authentication, this field SHALL be zero length.
351 if(m_side == Connection_Side::Server && !m_request_context.empty()) {
352 throw TLS_Exception(Alert::IllegalParameter, "Server Certificate message must not contain a request context");
353 }
354
355 const auto cert_entries_len = reader.get_uint24_t();
356
357 if(reader.remaining_bytes() != cert_entries_len) {
358 throw TLS_Exception(Alert::DecodeError, "Certificate: Message malformed");
359 }
360
361 const size_t max_size = policy.maximum_certificate_chain_size();
362 if(max_size > 0 && cert_entries_len > max_size) {
363 throw Decoding_Error("Certificate chain exceeds policy specified maximum size");
364 }
365
366 while(reader.has_remaining()) {
367 m_entries.emplace_back(reader, side, cert_type);
368 }
369
370 // RFC 8446 4.4.2
371 // The server's certificate_list MUST always be non-empty. A client
372 // will send an empty certificate_list if it does not have an
373 // appropriate certificate to send in response to the server's
374 // authentication request.
375 if(m_entries.empty()) {
376 // RFC 8446 4.4.2.4
377 // If the server supplies an empty Certificate message, the client MUST
378 // abort the handshake with a "decode_error" alert.
379 if(m_side == Connection_Side::Server) {
380 throw TLS_Exception(Alert::DecodeError, "No certificates sent by server");
381 }
382
383 return;
384 }
385
386 BOTAN_ASSERT_NOMSG(!m_entries.empty());
387
388 // RFC 8446 4.4.2.2
389 // The certificate type MUST be X.509v3 [RFC5280], unless explicitly
390 // negotiated otherwise (e.g., [RFC7250]).
391 //
392 // TLS 1.0 through 1.3 all seem to require that the certificate be
393 // precisely a v3 certificate. In fact the strict wording would seem
394 // to require that every certificate in the chain be v3. But often
395 // the intermediates are outside of the control of the server.
396 // But, require that the leaf certificate be v3.
397 if(cert_type == Certificate_Type::X509 && m_entries.front().certificate().x509_version() != 3) {
398 throw TLS_Exception(Alert::BadCertificate, "The leaf certificate must be v3");
399 }
400
401 // RFC 8446 4.4.2
402 // If the RawPublicKey certificate type was negotiated, then the
403 // certificate_list MUST contain no more than one CertificateEntry.
404 if(cert_type == Certificate_Type::RawPublicKey && m_entries.size() != 1) {
405 throw TLS_Exception(Alert::IllegalParameter, "Certificate message contained more than one RawPublicKey");
406 }
407
408 // Validate the provided (certificate) public key against our policy
409 auto pubkey = public_key();
410 policy.check_peer_key_acceptable(*pubkey);
411
412 if(!policy.allowed_signature_method(pubkey->algo_name())) {
413 throw TLS_Exception(Alert::HandshakeFailure, "Rejecting " + pubkey->algo_name() + " signature");
414 }
415}
std::shared_ptr< const Public_Key > public_key() const

References Botan::TLS::Policy::allowed_signature_method(), BOTAN_ASSERT_NOMSG, Botan::TLS::Policy::check_peer_key_acceptable(), Botan::TLS::TLS_Data_Reader::get_range(), Botan::TLS::TLS_Data_Reader::get_uint24_t(), Botan::TLS::TLS_Data_Reader::has_remaining(), Botan::TLS::Policy::maximum_certificate_chain_size(), public_key(), Botan::TLS::RawPublicKey, Botan::TLS::TLS_Data_Reader::remaining_bytes(), Botan::TLS::Server, and Botan::TLS::X509.

Member Function Documentation

◆ cert_chain()

std::vector< X509_Certificate > Botan::TLS::Certificate_13::cert_chain ( ) const

Definition at line 62 of file msg_certificate_13.cpp.

62 {
64 std::vector<X509_Certificate> result;
65 std::transform(m_entries.cbegin(), m_entries.cend(), std::back_inserter(result), [](const auto& cert_entry) {
66 return cert_entry.certificate();
67 });
68 return result;
69}
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49

References BOTAN_STATE_CHECK, and has_certificate_chain().

Referenced by Certificate_13().

◆ count()

size_t Botan::TLS::Certificate_13::count ( ) const
inline

Definition at line 582 of file tls_messages.h.

582{ return m_entries.size(); }

◆ empty()

bool Botan::TLS::Certificate_13::empty ( ) const
inline

Definition at line 584 of file tls_messages.h.

584{ return m_entries.empty(); }

Referenced by Botan::TLS::Certificate_Verify_13::Certificate_Verify_13(), has_certificate_chain(), is_raw_public_key(), leaf(), and public_key().

◆ has_certificate_chain()

bool Botan::TLS::Certificate_13::has_certificate_chain ( ) const

Definition at line 54 of file msg_certificate_13.cpp.

54 {
55 return !empty() && m_entries.front().has_certificate();
56}

References empty().

Referenced by cert_chain(), Botan::TLS::Certificate_Verify_13::Certificate_Verify_13(), and is_raw_public_key().

◆ is_raw_public_key()

bool Botan::TLS::Certificate_13::is_raw_public_key ( ) const

Definition at line 58 of file msg_certificate_13.cpp.

58 {
59 return !empty() && !has_certificate_chain();
60}

References empty(), and has_certificate_chain().

Referenced by verify().

◆ leaf()

const X509_Certificate & Botan::TLS::Certificate_13::leaf ( ) const

Definition at line 91 of file msg_certificate_13.cpp.

91 {
93 return m_entries.front().certificate();
94}

References BOTAN_STATE_CHECK, and empty().

Referenced by Botan::TLS::Certificate_Verify_13::Certificate_Verify_13().

◆ public_key()

std::shared_ptr< const Public_Key > Botan::TLS::Certificate_13::public_key ( ) const

Definition at line 86 of file msg_certificate_13.cpp.

86 {
88 return m_entries.front().public_key();
89}

References BOTAN_STATE_CHECK, and empty().

Referenced by Certificate_13(), Botan::TLS::Certificate_Verify_13::Certificate_Verify_13(), and verify().

◆ request_context()

const std::vector< uint8_t > & Botan::TLS::Certificate_13::request_context ( ) const
inline

Definition at line 589 of file tls_messages.h.

589{ return m_request_context; }

◆ serialize()

std::vector< uint8_t > Botan::TLS::Certificate_13::serialize ( ) const
overridevirtual

Serialize a Certificate message

Implements Botan::TLS::Handshake_Message.

Definition at line 420 of file msg_certificate_13.cpp.

420 {
421 std::vector<uint8_t> buf;
422
423 append_tls_length_value(buf, m_request_context, 1);
424
425 std::vector<uint8_t> entries;
426 for(const auto& entry : m_entries) {
427 append_tls_length_value(entries, entry.serialize(), 3);
428
429 // Extensions are tacked at the end of certificate entries. Note that
430 // Extensions::serialize() usually emits the required length field,
431 // except when no extensions are added at all, then it returns an
432 // empty buffer.
433 //
434 // TODO: look into this issue more generally when overhauling the
435 // message marshalling.
436 auto extensions = entry.extensions().serialize(m_side);
437 entries += (!extensions.empty()) ? extensions : std::vector<uint8_t>{0, 0};
438 }
439
440 append_tls_length_value(buf, entries, 3);
441
442 return buf;
443}
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:184

References Botan::TLS::append_tls_length_value().

◆ type()

Handshake_Type Botan::TLS::Certificate_13::type ( ) const
inlineoverridevirtual
Returns
the message type

Implements Botan::TLS::Handshake_Message.

Definition at line 575 of file tls_messages.h.

References Botan::TLS::Certificate.

Referenced by validate_extensions().

◆ type_string()

std::string Botan::TLS::Handshake_Message::type_string ( ) const
inherited
Returns
string representation of this message type

Definition at line 18 of file tls_handshake_state.cpp.

18 {
20}
virtual Handshake_Type type() const =0
const char * handshake_type_to_string(Handshake_Type type)

References Botan::TLS::handshake_type_to_string(), and type().

◆ validate_extensions()

void Botan::TLS::Certificate_13::validate_extensions ( const std::set< Extension_Code > & requested_extensions,
Callbacks & cb ) const

Validate a Certificate message regarding what extensions are expected based on previous handshake messages. Also call the tls_examine_extensions() callback for each entry.

Parameters
requested_extensionsExtensions of Client_Hello or Certificate_Request messages
cbCallback that will be called for each extension.

Definition at line 71 of file msg_certificate_13.cpp.

71 {
72 // RFC 8446 4.4.2
73 // Extensions in the Certificate message from the server MUST
74 // correspond to ones from the ClientHello message. Extensions in
75 // the Certificate message from the client MUST correspond to
76 // extensions in the CertificateRequest message from the server.
77 for(const auto& entry : m_entries) {
78 if(entry.extensions().contains_other_than(requested_extensions)) {
79 throw TLS_Exception(Alert::IllegalParameter, "Certificate Entry contained an extension that was not offered");
80 }
81
82 cb.tls_examine_extensions(entry.extensions(), m_side, type());
83 }
84}
Handshake_Type type() const override

References Botan::TLS::Callbacks::tls_examine_extensions(), and type().

◆ verify()

void Botan::TLS::Certificate_13::verify ( Callbacks & callbacks,
const Policy & policy,
Credentials_Manager & creds,
std::string_view hostname,
bool use_ocsp ) const

Verify the certificate chain

Exceptions
ifverification fails.

Definition at line 96 of file msg_certificate_13.cpp.

100 {
102
103 if(is_raw_public_key()) {
104 callbacks.tls_verify_raw_public_key(*public_key(), usage, hostname, policy);
105 } else {
106 verify_certificate_chain(callbacks, policy, creds, hostname, use_ocsp, usage);
107 }
108}
Usage_Type
Definition x509cert.h:22

References Botan::TLS::Client, is_raw_public_key(), public_key(), Botan::TLS_CLIENT_AUTH, Botan::TLS_SERVER_AUTH, and Botan::TLS::Callbacks::tls_verify_raw_public_key().

◆ wire_type()

virtual Handshake_Type Botan::TLS::Handshake_Message::wire_type ( ) const
inlinevirtualinherited
Returns
the wire representation of the message's type

Reimplemented in Botan::TLS::Hello_Retry_Request.

Definition at line 39 of file tls_handshake_msg.h.

39 {
40 // Usually equal to the Handshake_Type enum value,
41 // with the exception of TLS 1.3 Hello Retry Request.
42 return type();
43 }

References type().

Referenced by Botan::TLS::Stream_Handshake_IO::send().


The documentation for this class was generated from the following files: