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