Botan 3.4.0
Crypto and TLS for C&
msg_certificate_req_13.cpp
Go to the documentation of this file.
1/*
2* (C) 2022 Jack Lloyd
3* (C) 2022 Hannes Rantzsch, René Meusel - neXenio GmbH
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/tls_messages.h>
9
10#include <botan/credentials_manager.h>
11#include <botan/tls_callbacks.h>
12#include <botan/tls_exceptn.h>
13#include <botan/internal/tls_reader.h>
14
15namespace Botan::TLS {
16
20
21Certificate_Request_13::Certificate_Request_13(const std::vector<uint8_t>& buf, const Connection_Side side) {
22 TLS_Data_Reader reader("Certificate_Request_13", buf);
23
24 // RFC 8446 4.3.2
25 // A server which is authenticating with a certificate MAY optionally
26 // request a certificate from the client.
27 if(side != Connection_Side::Server) {
28 throw TLS_Exception(Alert::UnexpectedMessage, "Received a Certificate_Request message from a client");
29 }
30
31 m_context = reader.get_tls_length_value(1);
32 m_extensions.deserialize(reader, side, type());
33
34 // RFC 8446 4.3.2
35 // The "signature_algorithms" extension MUST be specified, and other
36 // extensions may optionally be included if defined for this message.
37 // Clients MUST ignore unrecognized extensions.
38
39 if(!m_extensions.has<Signature_Algorithms>()) {
40 throw TLS_Exception(Alert::MissingExtension,
41 "Certificate_Request message did not provide a signature_algorithms extension");
42 }
43
44 // RFC 8446 4.2.
45 // The table below indicates the messages where a given extension may
46 // appear [...]. If an implementation receives an extension which it
47 // recognizes and which is not specified for the message in which it
48 // appears, it MUST abort the handshake with an "illegal_parameter" alert.
49 //
50 // For Certificate Request said table states:
51 // "status_request", "signature_algorithms", "signed_certificate_timestamp",
52 // "certificate_authorities", "oid_filters", "signature_algorithms_cert",
53 std::set<Extension_Code> allowed_extensions = {
56 // Extension_Code::SignedCertificateTimestamp, // NYI
58 // Extension_Code::OidFilters, // NYI
60 };
61
62 if(m_extensions.contains_implemented_extensions_other_than(allowed_extensions)) {
63 throw TLS_Exception(Alert::IllegalParameter, "Certificate Request contained an extension that is not allowed");
64 }
65}
66
67Certificate_Request_13::Certificate_Request_13(std::vector<X509_DN> acceptable_CAs, const Policy& policy, Callbacks&) {
68 // RFC 8446 4.3.2
69 // The certificate_request_context [here: m_context] MUST be unique within
70 // the scope of this connection (thus preventing replay of client
71 // CertificateVerify messages). This field SHALL be zero length unless
72 // used for the post-handshake authentication exchanges described in
73 // Section 4.6.2.
74 //
75 // TODO: Post-Handshake auth must fill m_context in an unpredictable way
76
77 // RFC 8446 4.3.2
78 // [Supported signature algorithms are] expressed by sending the
79 // "signature_algorithms" and optionally "signature_algorithms_cert"
80 // extensions. [A list of certificate authorities which the server would
81 // accept] is expressed by sending the "certificate_authorities" extension.
82 //
83 // The "signature_algorithms" extension MUST be specified, and other
84 // extensions may optionally be included if defined for this message.
85 m_extensions.add(std::make_unique<Signature_Algorithms>(policy.acceptable_signature_schemes()));
86 if(auto cert_signing_prefs = policy.acceptable_certificate_signature_schemes()) {
87 // RFC 8446 4.2.3
88 // Implementations which have the same policy in both cases MAY omit
89 // the "signature_algorithms_cert" extension.
90 m_extensions.add(std::make_unique<Signature_Algorithms_Cert>(std::move(cert_signing_prefs.value())));
91 }
92
93 if(!acceptable_CAs.empty()) {
94 m_extensions.add(std::make_unique<Certificate_Authorities>(std::move(acceptable_CAs)));
95 }
96
97 // TODO: Support cert_status_request for OCSP stapling
98
99 // TODO: give the application a chance to modifying extensions
100 // (after GH #2988 is merged)
101 // callbacks.tls_modify_extensions(m_extensions, Connection_Side::Server);
102}
103
104std::optional<Certificate_Request_13> Certificate_Request_13::maybe_create(const Client_Hello_13& client_hello,
105 Credentials_Manager& cred_mgr,
106 Callbacks& callbacks,
107 const Policy& policy) {
108 const auto trusted_CAs = cred_mgr.trusted_certificate_authorities("tls-server", client_hello.sni_hostname());
109
110 std::vector<X509_DN> client_auth_CAs;
111 for(const auto store : trusted_CAs) {
112 const auto subjects = store->all_subjects();
113 client_auth_CAs.insert(client_auth_CAs.end(), subjects.begin(), subjects.end());
114 }
115
116 if(client_auth_CAs.empty() && !policy.request_client_certificate_authentication()) {
117 return std::nullopt;
118 }
119
120 return Certificate_Request_13(std::move(client_auth_CAs), policy, callbacks);
121}
122
123std::vector<X509_DN> Certificate_Request_13::acceptable_CAs() const {
124 if(m_extensions.has<Certificate_Authorities>()) {
125 return m_extensions.get<Certificate_Authorities>()->distinguished_names();
126 }
127 return {};
128}
129
130const std::vector<Signature_Scheme>& Certificate_Request_13::signature_schemes() const {
131 // RFC 8446 4.3.2
132 // The "signature_algorithms" extension MUST be specified
134
135 return m_extensions.get<Signature_Algorithms>()->supported_schemes();
136}
137
138const std::vector<Signature_Scheme>& Certificate_Request_13::certificate_signature_schemes() const {
139 // RFC 8446 4.2.3
140 // If no "signature_algorithms_cert" extension is present, then the
141 // "signature_algorithms" extension also applies to signatures appearing
142 // in certificates.
143 if(auto sig_schemes_cert = m_extensions.get<Signature_Algorithms_Cert>()) {
144 return sig_schemes_cert->supported_schemes();
145 } else {
146 return signature_schemes();
147 }
148}
149
150std::vector<uint8_t> Certificate_Request_13::serialize() const {
151 std::vector<uint8_t> buf;
152 append_tls_length_value(buf, m_context, 1);
153 buf += m_extensions.serialize(Connection_Side::Server);
154 return buf;
155}
156
157} // namespace Botan::TLS
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
virtual std::vector< Certificate_Store * > trusted_certificate_authorities(const std::string &type, const std::string &context)
const std::vector< Signature_Scheme > & signature_schemes() const
std::vector< uint8_t > serialize() const override
Handshake_Type type() const override
const std::vector< Signature_Scheme > & certificate_signature_schemes() const
static std::optional< Certificate_Request_13 > maybe_create(const Client_Hello_13 &sni_hostname, Credentials_Manager &cred_mgr, Callbacks &callbacks, const Policy &policy)
std::vector< X509_DN > acceptable_CAs() const
Certificate_Request_13(const std::vector< uint8_t > &buf, Connection_Side side)
std::string sni_hostname() const
bool contains_implemented_extensions_other_than(const std::set< Extension_Code > &allowed_extensions) const
std::vector< uint8_t > serialize(Connection_Side whoami) const
void deserialize(TLS_Data_Reader &reader, Connection_Side from, Handshake_Type message_type)
void add(std::unique_ptr< Extension > extn)
virtual bool request_client_certificate_authentication() const
virtual std::optional< std::vector< Signature_Scheme > > acceptable_certificate_signature_schemes() const
virtual std::vector< Signature_Scheme > acceptable_signature_schemes() const
std::vector< uint8_t > get_tls_length_value(size_t len_bytes)
Definition tls_reader.h:100
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:180