10#include <botan/tls_messages_13.h>
12#include <botan/ber_dec.h>
13#include <botan/credentials_manager.h>
14#include <botan/ocsp.h>
15#include <botan/pk_algs.h>
16#include <botan/tls_alert.h>
17#include <botan/tls_callbacks.h>
18#include <botan/tls_exceptn.h>
19#include <botan/tls_extensions.h>
20#include <botan/tls_extensions_13.h>
21#include <botan/tls_policy.h>
22#include <botan/x509_key.h>
23#include <botan/internal/stl_util.h>
24#include <botan/internal/tls_reader.h>
37bool certificate_allows_signing(
const X509_Certificate& cert) {
38 const auto constraints = cert.constraints();
39 if(constraints.empty()) {
49std::unique_ptr<Public_Key> decode_raw_public_key(std::span<const uint8_t> spki) {
50 AlgorithmIdentifier alg_id;
51 std::vector<uint8_t> key_bits;
56 .decode_octet_aligned_bitstring(key_bits)
60 if(key_bits.empty()) {
61 throw Decoding_Error(
"Empty raw public key");
70 return !
empty() && m_entries.front().has_certificate();
79 std::vector<X509_Certificate> result;
80 std::transform(m_entries.cbegin(), m_entries.cend(), std::back_inserter(result), [](
const auto& cert_entry) {
81 return cert_entry.certificate();
92 for(
const auto& entry : m_entries) {
93 if(entry.extensions().contains_other_than(requested_extensions)) {
94 throw TLS_Exception(Alert::IllegalParameter,
"Certificate Entry contained an extension that was not offered");
103 return m_entries.front().public_key();
108 return m_entries.front().certificate();
114 std::string_view hostname,
115 bool use_ocsp)
const {
125 "Peer sent a certificate of a type that is not accepted by the policy");
131 verify_certificate_chain(callbacks, policy, creds, hostname, use_ocsp, usage);
135void Certificate_13::verify_certificate_chain(
Callbacks& callbacks,
138 std::string_view hostname,
141 std::vector<X509_Certificate> certs;
142 std::vector<std::optional<OCSP::Response>> ocsp_responses;
143 for(
const auto& entry : m_entries) {
144 certs.push_back(entry.certificate());
150 ocsp_responses.emplace_back();
155 const auto& server_cert = m_entries.front().certificate();
156 if(!certificate_allows_signing(server_cert)) {
157 throw TLS_Exception(Alert::BadCertificate,
"Certificate usage constraints do not allow signing");
167void Certificate_13::setup_entries(std::vector<X509_Certificate> cert_chain,
174 const auto ocsp_responses = (csr !=
nullptr) ? callbacks.tls_provide_cert_chain_status(
cert_chain, *csr)
175 : std::vector<std::vector<uint8_t>>(
cert_chain.size());
177 if(ocsp_responses.size() !=
cert_chain.size()) {
178 throw TLS_Exception(Alert::InternalError,
"Application didn't provide the correct number of OCSP responses");
181 for(
size_t i = 0; i <
cert_chain.size(); ++i) {
182 auto& entry = m_entries.emplace_back(
cert_chain[i]);
183 if(!ocsp_responses[i].
empty()) {
184 entry.extensions().add(
new Certificate_Status_Request(ocsp_responses[i]));
195 callbacks.tls_modify_extensions(entry.extensions(), m_side,
type());
199void Certificate_13::setup_entry(std::shared_ptr<Public_Key> raw_public_key,
Callbacks& callbacks) {
201 auto& entry = m_entries.emplace_back(std::move(raw_public_key));
202 callbacks.tls_modify_extensions(entry.extensions(), m_side,
type());
209 std::string_view hostname,
219 if(key_types.empty()) {
222 const std::string op_type =
"tls-client";
230 std::string(hostname)),
234 auto raw_public_key = credentials_manager.
find_raw_public_key(key_types, op_type, std::string(hostname));
244 setup_entry(std::move(raw_public_key), callbacks);
269 throw TLS_Exception(Alert::MissingExtension,
"Client Hello is missing required signature_algorithms extension");
273 if(key_types.empty()) {
274 throw TLS_Exception(Alert::HandshakeFailure,
"Failed to agree on any signature algorithm");
276 const std::string op_type =
"tls-server";
277 const std::string context = client_hello.
sni_hostname();
285 std::vector<X509_DN> acceptable_CAs;
286 if(const auto* cas = client_hello.extensions().get<Certificate_Authorities>()) {
287 acceptable_CAs = cas->distinguished_names();
300 throw TLS_Exception(Alert::HandshakeFailure,
"No sufficient server certificate available");
305 auto raw_public_key = credentials_manager.find_raw_public_key(key_types, op_type, context);
312 if(!raw_public_key) {
313 throw TLS_Exception(Alert::HandshakeFailure,
"No sufficient server raw public key available");
316 setup_entry(std::move(raw_public_key), callbacks);
330 m_certificate = std::make_unique<X509_Certificate>(cert_bytes);
334 m_raw_public_key = m_certificate->subject_public_key();
351 throw TLS_Exception(Alert::InternalError,
"Unknown certificate type");
358 const auto exts_buf = reader.
get_fixed<uint8_t>(extensions_length + 2);
373 if(m_extensions.contains_implemented_extensions_other_than({
374 Extension_Code::CertificateStatusRequest,
377 throw TLS_Exception(Alert::IllegalParameter,
"Certificate Entry contained an extension that is not allowed");
379 }
else if(m_extensions.contains_implemented_extensions_other_than({})) {
381 Alert::IllegalParameter,
382 "Certificate Entry holding something else than a certificate contained unexpected extensions");
393 m_certificate(std::make_unique<
X509_Certificate>(cert)), m_raw_public_key(m_certificate->subject_public_key()) {}
396 m_raw_public_key(std::move(raw_public_key)) {
402 return *m_certificate;
407 return m_raw_public_key;
424 m_request_context = reader.
get_range<uint8_t>(1, 0, 255);
429 throw TLS_Exception(Alert::IllegalParameter,
"Server Certificate message must not contain a request context");
435 throw TLS_Exception(Alert::DecodeError,
"Certificate: Message malformed");
439 if(max_size > 0 && cert_entries_len > max_size) {
440 throw Decoding_Error(
"Certificate chain exceeds policy specified maximum size");
444 const bool is_leaf = m_entries.empty();
450 throw TLS_Exception(Alert::IllegalParameter,
"Certificate message contained more than one RawPublicKey");
453 m_entries.emplace_back(reader, side, cert_type, is_leaf);
461 if(m_entries.empty()) {
466 throw TLS_Exception(Alert::DecodeError,
"No certificates sent by server");
484 throw TLS_Exception(Alert::BadCertificate,
"The leaf certificate must be v3");
492 throw TLS_Exception(Alert::HandshakeFailure,
"Rejecting " + pubkey->algo_name() +
" signature");
500 std::vector<uint8_t> buf;
504 std::vector<uint8_t> entries;
505 for(
const auto& entry : m_entries) {
515 const auto extensions = entry.extensions().serialize(m_side);
516 entries += (!extensions.empty()) ? extensions : std::vector<uint8_t>{0, 0};
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
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)
const char * what() const noexcept override
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
bool has_certificate() const
std::vector< uint8_t > serialize() const
Certificate_Entry(TLS_Data_Reader &reader, Connection_Side side, Certificate_Type cert_type, bool is_leaf)
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
bool is_raw_public_key() const
std::vector< X509_Certificate > cert_chain() const
bool has_certificate_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
virtual std::vector< Certificate_Type > accepted_server_certificate_types() const
virtual std::vector< Certificate_Type > accepted_client_certificate_types() const
bool allowed_signature_method(std::string_view sig_method) const
virtual size_t maximum_certificate_chain_size() const
bool has_remaining() const
std::vector< T > get_range(size_t len_bytes, size_t min_elems, size_t max_elems)
size_t remaining_bytes() const
std::vector< uint8_t > get_tls_length_value(size_t len_bytes)
std::vector< T > get_fixed(size_t size)
uint16_t peek_uint16_t() const
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)
std::vector< std::string > filter_signature_schemes(const std::vector< Signature_Scheme > &schemes, const Protocol_Version &version)
std::vector< uint8_t > BER_encode(const Public_Key &key)
bool value_exists(const std::vector< T > &vec, const V &val)
std::unique_ptr< Public_Key > load_public_key(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)