10#include <botan/tls_messages_13.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/tls_policy.h>
19#include <botan/x509_key.h>
20#include <botan/internal/tls_reader.h>
29bool certificate_allows_signing(
const X509_Certificate& cert) {
30 const auto constraints = cert.constraints();
31 if(constraints.empty()) {
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());
46 if(compatible_schemes.empty()) {
47 throw TLS_Exception(Alert::HandshakeFailure,
"Failed to agree on any signature algorithm");
50 return compatible_schemes;
56 return !
empty() && m_entries.front().has_certificate();
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();
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");
89 return m_entries.front().public_key();
94 return m_entries.front().certificate();
100 std::string_view hostname,
101 bool use_ocsp)
const {
107 verify_certificate_chain(callbacks, policy, creds, hostname, use_ocsp, usage);
111void Certificate_13::verify_certificate_chain(
Callbacks& callbacks,
114 std::string_view hostname,
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());
126 ocsp_responses.emplace_back();
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");
143void Certificate_13::setup_entries(std::vector<X509_Certificate> cert_chain,
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());
153 if(ocsp_responses.size() !=
cert_chain.size()) {
154 throw TLS_Exception(Alert::InternalError,
"Application didn't provide the correct number of OCSP responses");
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]));
171 callbacks.tls_modify_extensions(entry.extensions(), m_side,
type());
175void Certificate_13::setup_entry(std::shared_ptr<Public_Key> raw_public_key,
Callbacks& callbacks) {
177 auto& entry = m_entries.emplace_back(std::move(raw_public_key));
178 callbacks.tls_modify_extensions(entry.extensions(), m_side,
type());
185 std::string_view hostname,
190 const auto key_types = filter_signature_schemes(cert_request.
signature_schemes());
191 const std::string op_type =
"tls-client";
199 std::string(hostname)),
203 auto raw_public_key = credentials_manager.
find_raw_public_key(key_types, op_type, std::string(hostname));
213 setup_entry(std::move(raw_public_key), callbacks);
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();
242 throw TLS_Exception(Alert::HandshakeFailure,
"No sufficient server certificate available");
247 auto raw_public_key = credentials_manager.
find_raw_public_key(key_types, op_type, context);
254 if(!raw_public_key) {
255 throw TLS_Exception(Alert::HandshakeFailure,
"No sufficient server raw public key available");
258 setup_entry(std::move(raw_public_key), callbacks);
271 m_raw_public_key = m_certificate->subject_public_key();
281 throw TLS_Exception(Alert::InternalError,
"Unknown certificate type");
288 const auto exts_buf = reader.
get_fixed<uint8_t>(extensions_length + 2);
303 if(m_extensions.contains_implemented_extensions_other_than({
304 Extension_Code::CertificateStatusRequest,
307 throw TLS_Exception(Alert::IllegalParameter,
"Certificate Entry contained an extension that is not allowed");
309 }
else if(m_extensions.contains_implemented_extensions_other_than({})) {
311 Alert::IllegalParameter,
312 "Certificate Entry holding something else than a certificate contained unexpected extensions");
323 m_certificate(std::make_unique<
X509_Certificate>(cert)), m_raw_public_key(m_certificate->subject_public_key()) {}
326 m_raw_public_key(std::move(raw_public_key)) {
332 return *m_certificate;
337 return m_raw_public_key;
354 m_request_context = reader.
get_range<uint8_t>(1, 0, 255);
359 throw TLS_Exception(Alert::IllegalParameter,
"Server Certificate message must not contain a request context");
365 throw TLS_Exception(Alert::DecodeError,
"Certificate: Message malformed");
369 if(max_size > 0 && cert_entries_len > max_size) {
370 throw Decoding_Error(
"Certificate chain exceeds policy specified maximum size");
374 m_entries.emplace_back(reader, side, cert_type);
382 if(m_entries.empty()) {
387 throw TLS_Exception(Alert::DecodeError,
"No certificates sent by server");
405 throw TLS_Exception(Alert::BadCertificate,
"The leaf certificate must be v3");
412 throw TLS_Exception(Alert::IllegalParameter,
"Certificate message contained more than one RawPublicKey");
420 throw TLS_Exception(Alert::HandshakeFailure,
"Rejecting " + pubkey->algo_name() +
" signature");
428 std::vector<uint8_t> buf;
432 std::vector<uint8_t> entries;
433 for(
const auto& entry : m_entries) {
443 auto extensions = entry.extensions().serialize(m_side);
444 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)
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)
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)