Botan 3.14.1
Crypto and TLS for C&
msg_encrypted_extensions.cpp
Go to the documentation of this file.
1/*
2* TLS Hello Request and Client Hello Messages
3* (C) 2022 Jack Lloyd
4* 2022 René Meusel, Hannes Rantzsch - neXenio GmbH
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/tls_messages_13.h>
10
11#include <botan/tls_callbacks.h>
12#include <botan/tls_exceptn.h>
13#include <botan/tls_policy.h>
14#include <botan/internal/stl_util.h>
15#include <botan/internal/tls_messages_internal.h>
16#include <botan/internal/tls_reader.h>
17
18namespace Botan::TLS {
19
21 const Policy& policy,
22 Callbacks& cb,
23 bool is_resumption,
24 bool requesting_client_auth) {
25 const auto& exts = client_hello.extensions();
26
27 // NOLINTBEGIN(*-owning-memory)
28
29 // RFC 8446 4.2.7
30 // As of TLS 1.3, servers are permitted to send the "supported_groups"
31 // extension to the client. Clients [...] MAY use the information
32 // learned from a successfully completed handshake to change what groups
33 // they use in their "key_share" extension in subsequent connections.
34 if(exts.has<Supported_Groups>()) {
35 m_extensions.add(
36 new Supported_Groups(groups_usable_in_version(policy.key_exchange_groups(), Protocol_Version::TLS_V13)));
37 }
38
39 const auto record_size_limit = policy.record_size_limit();
40 const auto max_record_size = MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */;
41 if(exts.has<Record_Size_Limit>()) {
42 // RFC 8449 4
43 // Endpoints SHOULD advertise the "record_size_limit" extension, even
44 // if they have no need to limit the size of records. [...] For
45 // servers, this allows clients to know that their limit will be
46 // respected.
47 m_extensions.add(new Record_Size_Limit(record_size_limit.value_or(max_record_size)));
48 } else if(record_size_limit.has_value() && record_size_limit.value() < max_record_size) {
49 // RFC 8449 4
50 // Endpoints SHOULD advertise the "record_size_limit" extension, even if
51 // they have no need to limit the size of records. For clients, this
52 // allows servers to advertise a limit at their discretion.
53 throw TLS_Exception(Alert::MissingExtension,
54 "Server cannot enforce record size limit without the client supporting it");
55 }
56
57 // RFC 7250 4.2
58 // If the TLS server wants to request a certificate from the client
59 // (via the certificate_request message), it MUST include the
60 // client_certificate_type extension in the server hello.
61 // [...]
62 // If the server does not send a certificate_request payload [...],
63 // then the client_certificate_type payload in the server hello MUST be
64 // omitted.
65 //
66 // Note: requesting_client_auth tracks whether the caller will actually
67 // emit a CertificateRequest. The server-side decision in
68 // Certificate_Request_13::maybe_create depends on both the policy flag
69 // *and* the credentials manager's CA list, so re-checking just the
70 // policy flag here would miss the trusted-CAs-only configuration.
71 if(const auto* ch_client_cert_types = exts.get<Client_Certificate_Type>();
72 ch_client_cert_types != nullptr && requesting_client_auth) {
73 m_extensions.add(new Client_Certificate_Type(*ch_client_cert_types, policy));
74 }
75
76 // RFC 7250 4.2
77 // The server_certificate_type extension in the client hello indicates the
78 // types of certificates the client is able to process when provided by
79 // the server in a subsequent certificate payload. [...] With the
80 // server_certificate_type extension in the server hello, the TLS server
81 // indicates the certificate type carried in the Certificate payload.
82 if(const auto* ch_server_cert_types = exts.get<Server_Certificate_Type>()) {
83 m_extensions.add(new Server_Certificate_Type(*ch_server_cert_types, policy));
84 }
85
86 // RFC 6066 3
87 // A server that receives a client hello containing the "server_name"
88 // extension [...] SHALL include an extension of type "server_name" in the
89 // (extended) server hello. The "extension_data" field of this extension
90 // SHALL be empty.
91 //
92 // When resuming a session, the server MUST NOT include a server_name
93 // extension in the server hello.
94 if(exts.has<Server_Name_Indicator>() && !is_resumption) {
95 m_extensions.add(new Server_Name_Indicator(""));
96 }
97
98 if(const auto* alpn_ext = exts.get<Application_Layer_Protocol_Notification>()) {
99 const auto& offered = alpn_ext->protocols();
100 const auto next_protocol = cb.tls_server_choose_app_protocol(offered);
101 if(!next_protocol.empty()) {
102 // RFC 7301 3.2: if a protocol is selected, the server MUST select
103 // one of the protocols advertised by the client.
104 if(!value_exists(offered, next_protocol)) {
105 throw TLS_Exception(Alert::InternalError,
106 "Application chose an ALPN protocol that the client did not offer");
107 }
108 m_extensions.add(new Application_Layer_Protocol_Notification(next_protocol));
109 }
110 }
111
112 // NOLINTEND(*-owning-memory)
113
114 // TODO: Implement handling for (at least)
115 // * SRTP
116
118
119 // After the application's tls_modify_extensions callback runs, re-check the
120 // RFC-MUST invariants we just established above. The application can add or
121 // reorder extensions, but shouldn't remove ones required direct response to
122 // ClientHello extensions would put us out of spec.
123 if(exts.has<Server_Certificate_Type>() && !m_extensions.has<Server_Certificate_Type>()) {
124 throw TLS_Exception(
125 Alert::InternalError,
126 "Application tls_modify_extensions callback removed Server_Certificate_Type from EncryptedExtensions");
127 }
128
129 if(requesting_client_auth && exts.has<Client_Certificate_Type>() && !m_extensions.has<Client_Certificate_Type>()) {
130 throw TLS_Exception(
131 Alert::InternalError,
132 "Application tls_modify_extensions callback removed Client_Certificate_Type from EncryptedExtensions");
133 }
134}
135
136Encrypted_Extensions::Encrypted_Extensions(std::span<const uint8_t> buf) {
137 TLS_Data_Reader reader("encrypted extensions reader", buf);
138
139 // Encrypted Extensions contains a list of extensions. This list may legally
140 // be empty. However, in that case we should at least see a two-byte length
141 // field that reads 0x00 0x00.
142 if(buf.size() < 2) {
143 throw TLS_Exception(Alert::DecodeError, "Server sent an empty Encrypted Extensions message");
144 }
145
146 m_extensions.deserialize(reader, Connection_Side::Server, type());
147
148 // RFC 8446 4.2
149 // If an implementation receives an extension which it recognizes and
150 // which is not specified for the message in which it appears, it MUST
151 // abort the handshake with an "illegal_parameter" alert.
152 //
153 // Note that we cannot encounter any extensions that we don't recognize here,
154 // since only extensions we previously offered are allowed in EE.
155 const auto allowed_exts = std::set<Extension_Code>{
156 // Allowed extensions listed in RFC 8446 and implemented in Botan
158 // MAX_FRAGMENT_LENGTH
161 // HEARTBEAT
163 // RFC 7250
166 // EARLY_DATA
167
168 // Allowed extensions not listed in RFC 8446 but acceptable as Botan implements them
170 };
171 if(m_extensions.contains_implemented_extensions_other_than(allowed_exts)) {
172 throw TLS_Exception(Alert::IllegalParameter, "Encrypted Extensions contained an extension that is not allowed");
173 }
174
175 reader.assert_done();
176}
177
178std::vector<uint8_t> Encrypted_Extensions::serialize() const {
179 // RFC 8446 4.3.1: EncryptedExtensions carries Extension extensions<0..2^16-1>;
180 // an empty list still requires a 2-byte length-prefix on the wire.
181 // Extensions::serialize collapses empty to {} to suit other contexts, so
182 // emit the explicit length here. Mirrors the same fallback in
183 // New_Session_Ticket_13::serialize.
184 if(m_extensions.empty()) {
185 return {0x00, 0x00};
186 }
187 return m_extensions.serialize(Connection_Side::Server);
188}
189
190} // namespace Botan::TLS
virtual void tls_modify_extensions(Extensions &extn, Connection_Side which_side, Handshake_Type which_message)
virtual std::string tls_server_choose_app_protocol(const std::vector< std::string > &client_protos)
const Extensions & extensions() const
std::vector< uint8_t > serialize() const override
Encrypted_Extensions(std::span< const uint8_t > buf)
Handshake_Type type() const override
virtual std::vector< Group_Params > key_exchange_groups() const
virtual std::optional< uint16_t > record_size_limit() const
std::vector< Group_Params > groups_usable_in_version(std::span< const Group_Params > groups, Protocol_Version version)
@ MAX_PLAINTEXT_SIZE
Definition tls_magic.h:37
bool value_exists(const std::vector< T > &vec, const V &val)
Definition stl_util.h:44