10#include <botan/tls_messages.h>
12#include <botan/credentials_manager.h>
13#include <botan/data_src.h>
14#include <botan/ocsp.h>
15#include <botan/tls_alert.h>
16#include <botan/tls_callbacks.h>
17#include <botan/tls_exceptn.h>
18#include <botan/tls_extensions.h>
19#include <botan/x509_key.h>
20#include <botan/internal/loadstor.h>
21#include <botan/internal/stl_util.h>
22#include <botan/internal/tls_handshake_hash.h>
23#include <botan/internal/tls_handshake_io.h>
24#include <botan/internal/tls_reader.h>
33bool certificate_allows_signing(
const X509_Certificate& cert) {
34 const auto constraints = cert.constraints();
35 if(constraints.empty()) {
42std::vector<std::string> filter_signature_schemes(
const std::vector<Signature_Scheme>& peer_scheme_preference) {
43 std::vector<std::string> compatible_schemes;
44 for(
const auto& scheme : peer_scheme_preference) {
45 if(scheme.is_available() && scheme.is_compatible_with(Protocol_Version::TLS_V13)) {
46 compatible_schemes.push_back(scheme.algorithm_name());
50 if(compatible_schemes.empty()) {
51 throw TLS_Exception(Alert::HandshakeFailure,
"Failed to agree on any signature algorithm");
54 return compatible_schemes;
60 return !
empty() && m_entries.front().has_certificate();
69 std::vector<X509_Certificate> result;
70 std::transform(m_entries.cbegin(), m_entries.cend(), std::back_inserter(result), [](
const auto& cert_entry) {
71 return cert_entry.certificate();
82 for(
const auto& entry : m_entries) {
83 if(entry.extensions().contains_other_than(requested_extensions)) {
84 throw TLS_Exception(Alert::IllegalParameter,
"Certificate Entry contained an extension that was not offered");
93 return m_entries.front().public_key();
98 return m_entries.front().certificate();
104 std::string_view hostname,
105 bool use_ocsp)
const {
111 verify_certificate_chain(callbacks, policy, creds, hostname, use_ocsp, usage);
115void Certificate_13::verify_certificate_chain(
Callbacks& callbacks,
118 std::string_view hostname,
121 std::vector<X509_Certificate> certs;
122 std::vector<std::optional<OCSP::Response>> ocsp_responses;
123 for(
const auto& entry : m_entries) {
124 certs.push_back(entry.certificate());
130 ocsp_responses.emplace_back();
135 const auto& server_cert = m_entries.front().certificate();
136 if(!certificate_allows_signing(server_cert)) {
137 throw TLS_Exception(Alert::BadCertificate,
"Certificate usage constraints do not allow signing");
147void Certificate_13::setup_entries(std::vector<X509_Certificate> cert_chain,
148 const Certificate_Status_Request* csr,
149 Callbacks& callbacks) {
154 const auto ocsp_responses = (csr !=
nullptr) ? callbacks.tls_provide_cert_chain_status(
cert_chain, *csr)
155 : std::vector<std::vector<uint8_t>>(
cert_chain.size());
157 if(ocsp_responses.size() !=
cert_chain.size()) {
158 throw TLS_Exception(Alert::InternalError,
"Application didn't provide the correct number of OCSP responses");
161 for(
size_t i = 0; i <
cert_chain.size(); ++i) {
162 auto& entry = m_entries.emplace_back(
cert_chain[i]);
163 if(!ocsp_responses[i].
empty()) {
164 entry.extensions().add(
new Certificate_Status_Request(ocsp_responses[i]));
175 callbacks.tls_modify_extensions(entry.extensions(), m_side,
type());
179void Certificate_13::setup_entry(std::shared_ptr<Public_Key> raw_public_key, Callbacks& callbacks) {
181 auto& entry = m_entries.emplace_back(std::move(raw_public_key));
182 callbacks.tls_modify_extensions(entry.extensions(), m_side,
type());
189 std::string_view hostname,
194 const auto key_types = filter_signature_schemes(cert_request.
signature_schemes());
195 const auto op_type =
"tls-client";
203 std::string(hostname)),
207 auto raw_public_key = credentials_manager.
find_raw_public_key(key_types, op_type, std::string(hostname));
217 setup_entry(std::move(raw_public_key), callbacks);
235 const auto key_types = filter_signature_schemes(client_hello.
signature_schemes());
236 const auto op_type =
"tls-server";
246 throw TLS_Exception(Alert::HandshakeFailure,
"No sufficient server certificate available");
251 auto raw_public_key = credentials_manager.
find_raw_public_key(key_types, op_type, context);
258 if(!raw_public_key) {
259 throw TLS_Exception(Alert::HandshakeFailure,
"No sufficient server raw public key available");
262 setup_entry(std::move(raw_public_key), callbacks);
275 m_raw_public_key = m_certificate->subject_public_key();
285 throw TLS_Exception(Alert::InternalError,
"Unknown certificate type");
292 const auto exts_buf = reader.
get_fixed<uint8_t>(extensions_length + 2);
308 Extension_Code::CertificateStatusRequest,
311 throw TLS_Exception(Alert::IllegalParameter,
"Certificate Entry contained an extension that is not allowed");
315 Alert::IllegalParameter,
316 "Certificate Entry holding something else than a certificate contained unexpected extensions");
321 m_certificate(std::move(cert)), m_raw_public_key(m_certificate->subject_public_key()) {}
324 m_certificate(std::nullopt), m_raw_public_key(std::move(raw_public_key)) {
330 return m_certificate.value();
335 return m_raw_public_key;
339 return (has_certificate()) ? m_certificate->BER_encode() :
X509::BER_encode(*m_raw_public_key);
352 m_request_context = reader.
get_range<uint8_t>(1, 0, 255);
357 throw TLS_Exception(Alert::IllegalParameter,
"Server Certificate message must not contain a request context");
363 throw TLS_Exception(Alert::DecodeError,
"Certificate: Message malformed");
367 if(max_size > 0 && cert_entries_len > max_size) {
368 throw Decoding_Error(
"Certificate chain exceeds policy specified maximum size");
372 m_entries.emplace_back(reader, side, cert_type);
380 if(m_entries.empty()) {
385 throw TLS_Exception(Alert::DecodeError,
"No certificates sent by server");
403 throw TLS_Exception(Alert::BadCertificate,
"The leaf certificate must be v3");
410 throw TLS_Exception(Alert::IllegalParameter,
"Certificate message contained more than one RawPublicKey");
418 throw TLS_Exception(Alert::HandshakeFailure,
"Rejecting " + pubkey->algo_name() +
" signature");
426 std::vector<uint8_t> buf;
430 std::vector<uint8_t> entries;
431 for(
const auto& entry : m_entries) {
441 auto extensions = entry.extensions().serialize(m_side);
442 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)
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)
const X509_Certificate & certificate() const
std::shared_ptr< const Public_Key > public_key() const
Certificate_Entry(TLS_Data_Reader &reader, const Connection_Side side, const Certificate_Type cert_type)
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
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
bool contains_implemented_extensions_other_than(const std::set< Extension_Code > &allowed_extensions) const
void deserialize(TLS_Data_Reader &reader, Connection_Side from, Handshake_Type message_type)
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
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< uint8_t > BER_encode(const Public_Key &key)
std::unique_ptr< Public_Key > load_key(DataSource &source)