10#include <botan/tls_messages.h>
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/x509_key.h>
19#include <botan/internal/tls_reader.h>
28bool certificate_allows_signing(
const X509_Certificate& cert) {
29 const auto constraints = cert.constraints();
30 if(constraints.empty()) {
37std::vector<std::string> filter_signature_schemes(
const std::vector<Signature_Scheme>& peer_scheme_preference) {
38 std::vector<std::string> compatible_schemes;
39 for(
const auto& scheme : peer_scheme_preference) {
40 if(scheme.is_available() && scheme.is_compatible_with(Protocol_Version::TLS_V13)) {
41 compatible_schemes.push_back(scheme.algorithm_name());
45 if(compatible_schemes.empty()) {
46 throw TLS_Exception(Alert::HandshakeFailure,
"Failed to agree on any signature algorithm");
49 return compatible_schemes;
55 return !
empty() && m_entries.front().has_certificate();
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();
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");
88 return m_entries.front().public_key();
93 return m_entries.front().certificate();
99 std::string_view hostname,
100 bool use_ocsp)
const {
106 verify_certificate_chain(callbacks, policy, creds, hostname, use_ocsp, usage);
110void Certificate_13::verify_certificate_chain(
Callbacks& callbacks,
113 std::string_view hostname,
116 std::vector<X509_Certificate> certs;
117 std::vector<std::optional<OCSP::Response>> ocsp_responses;
118 for(
const auto& entry : m_entries) {
119 certs.push_back(entry.certificate());
125 ocsp_responses.emplace_back();
130 const auto& server_cert = m_entries.front().certificate();
131 if(!certificate_allows_signing(server_cert)) {
132 throw TLS_Exception(Alert::BadCertificate,
"Certificate usage constraints do not allow signing");
142void Certificate_13::setup_entries(std::vector<X509_Certificate> cert_chain,
149 const auto ocsp_responses = (csr !=
nullptr) ? callbacks.tls_provide_cert_chain_status(
cert_chain, *csr)
150 : std::vector<std::vector<uint8_t>>(
cert_chain.size());
152 if(ocsp_responses.size() !=
cert_chain.size()) {
153 throw TLS_Exception(Alert::InternalError,
"Application didn't provide the correct number of OCSP responses");
156 for(
size_t i = 0; i <
cert_chain.size(); ++i) {
157 auto& entry = m_entries.emplace_back(
cert_chain[i]);
158 if(!ocsp_responses[i].
empty()) {
159 entry.extensions().add(
new Certificate_Status_Request(ocsp_responses[i]));
170 callbacks.tls_modify_extensions(entry.extensions(), m_side,
type());
174void Certificate_13::setup_entry(std::shared_ptr<Public_Key> raw_public_key,
Callbacks& callbacks) {
176 auto& entry = m_entries.emplace_back(std::move(raw_public_key));
177 callbacks.tls_modify_extensions(entry.extensions(), m_side,
type());
184 std::string_view hostname,
189 const auto key_types = filter_signature_schemes(cert_request.
signature_schemes());
190 const std::string op_type =
"tls-client";
198 std::string(hostname)),
202 auto raw_public_key = credentials_manager.
find_raw_public_key(key_types, op_type, std::string(hostname));
212 setup_entry(std::move(raw_public_key), callbacks);
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();
241 throw TLS_Exception(Alert::HandshakeFailure,
"No sufficient server certificate available");
246 auto raw_public_key = credentials_manager.
find_raw_public_key(key_types, op_type, context);
253 if(!raw_public_key) {
254 throw TLS_Exception(Alert::HandshakeFailure,
"No sufficient server raw public key available");
257 setup_entry(std::move(raw_public_key), callbacks);
270 m_raw_public_key = m_certificate->subject_public_key();
280 throw TLS_Exception(Alert::InternalError,
"Unknown certificate type");
287 const auto exts_buf = reader.
get_fixed<uint8_t>(extensions_length + 2);
302 if(m_extensions.contains_implemented_extensions_other_than({
303 Extension_Code::CertificateStatusRequest,
306 throw TLS_Exception(Alert::IllegalParameter,
"Certificate Entry contained an extension that is not allowed");
308 }
else if(m_extensions.contains_implemented_extensions_other_than({})) {
310 Alert::IllegalParameter,
311 "Certificate Entry holding something else than a certificate contained unexpected extensions");
316 m_certificate(std::move(cert)), m_raw_public_key(m_certificate->subject_public_key()) {}
319 m_certificate(std::nullopt), m_raw_public_key(std::move(raw_public_key)) {
325 return m_certificate.value();
330 return m_raw_public_key;
347 m_request_context = reader.
get_range<uint8_t>(1, 0, 255);
352 throw TLS_Exception(Alert::IllegalParameter,
"Server Certificate message must not contain a request context");
358 throw TLS_Exception(Alert::DecodeError,
"Certificate: Message malformed");
362 if(max_size > 0 && cert_entries_len > max_size) {
363 throw Decoding_Error(
"Certificate chain exceeds policy specified maximum size");
367 m_entries.emplace_back(reader, side, cert_type);
375 if(m_entries.empty()) {
380 throw TLS_Exception(Alert::DecodeError,
"No certificates sent by server");
398 throw TLS_Exception(Alert::BadCertificate,
"The leaf certificate must be v3");
405 throw TLS_Exception(Alert::IllegalParameter,
"Certificate message contained more than one RawPublicKey");
413 throw TLS_Exception(Alert::HandshakeFailure,
"Rejecting " + pubkey->algo_name() +
" signature");
421 std::vector<uint8_t> buf;
425 std::vector<uint8_t> entries;
426 for(
const auto& entry : m_entries) {
436 auto extensions = entry.extensions().serialize(m_side);
437 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, Connection_Side side, Certificate_Type cert_type)
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
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
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)