Botan 3.10.0
Crypto and TLS for C&
msg_server_kex.cpp
Go to the documentation of this file.
1/*
2* Server Key Exchange Message
3* (C) 2004-2010,2012,2015,2016 Jack Lloyd
4* 2017 Harry Reimann, Rohde & Schwarz Cybersecurity
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/tls_messages.h>
10
11#include <botan/credentials_manager.h>
12#include <botan/pubkey.h>
13#include <botan/tls_extensions.h>
14#include <botan/internal/loadstor.h>
15#include <botan/internal/target_info.h>
16#include <botan/internal/tls_handshake_io.h>
17#include <botan/internal/tls_handshake_state.h>
18#include <botan/internal/tls_reader.h>
19
20#include <botan/dh.h>
21
22namespace Botan::TLS {
23
24/**
25* Create a new Server Key Exchange message
26*/
28 Handshake_State& state,
29 const Policy& policy,
32 const Private_Key* signing_key) {
33 const std::string hostname = state.client_hello()->sni_hostname();
34 const Kex_Algo kex_algo = state.ciphersuite().kex_method();
35
36 if(kex_algo == Kex_Algo::PSK || kex_algo == Kex_Algo::ECDHE_PSK) {
37 std::string identity_hint = creds.psk_identity_hint("tls-server", hostname);
38
39 append_tls_length_value(m_params, identity_hint, 2);
40 }
41
42 if(kex_algo == Kex_Algo::DH) {
43 const std::vector<Group_Params> dh_groups = state.client_hello()->supported_dh_groups();
44
45 m_shared_group = Group_Params::NONE;
46
47 /*
48 RFC 7919 requires that if the client sends any groups in the FFDHE
49 range, that we must select one of these. If this is not possible,
50 then we are required to reject the connection.
51
52 If the client did not send any DH groups, but did offer DH ciphersuites
53 and we selected one, then consult the policy for which DH group to pick.
54 */
55
56 if(dh_groups.empty()) {
57 m_shared_group = policy.default_dh_group();
58 } else {
59 m_shared_group = policy.choose_key_exchange_group(dh_groups, {});
60 }
61
62 if(m_shared_group.value() == Group_Params::NONE) {
63 throw TLS_Exception(Alert::HandshakeFailure, "Could not agree on a DH group with the client");
64 }
65
66 // The policy had better return a group we know about:
67 BOTAN_ASSERT(m_shared_group.value().is_dh_named_group(), "DH ciphersuite is using a known finite field group");
68
69 // Note: TLS 1.2 allows defining and using arbitrary DH groups (additional
70 // to the named and standardized ones). This API doesn't allow the
71 // server to make use of that at the moment. TLS 1.3 does not
72 // provide this flexibility!
73 //
74 // A possible implementation strategy in case one would ever need that:
75 // `Policy::default_dh_group()` could return a `std::variant<Group_Params,
76 // DL_Group>`, allowing it to define arbitrary groups.
77 m_kex_key = state.callbacks().tls_generate_ephemeral_key(m_shared_group.value(), rng);
78 auto* dh = dynamic_cast<DH_PrivateKey*>(m_kex_key.get());
79 if(dh == nullptr) {
80 throw TLS_Exception(Alert::InternalError, "Application did not provide a Diffie-Hellman key");
81 }
82
83 append_tls_length_value(m_params, dh->get_int_field("p").serialize(), 2);
84 append_tls_length_value(m_params, dh->get_int_field("g").serialize(), 2);
85 append_tls_length_value(m_params, dh->public_value(), 2);
86 } else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
87 const std::vector<Group_Params> ec_groups = state.client_hello()->supported_ecc_curves();
88
89 if(ec_groups.empty()) {
90 throw Internal_Error("Client sent no ECC extension but we negotiated ECDH");
91 }
92
93 m_shared_group = policy.choose_key_exchange_group(ec_groups, {});
94
95 if(m_shared_group.value() == Group_Params::NONE) {
96 throw TLS_Exception(Alert::HandshakeFailure, "No shared ECC group with client");
97 }
98
99 m_kex_key = [&] {
100 if(m_shared_group->is_ecdh_named_curve()) {
101 const auto pubkey_point_format = state.client_hello()->prefers_compressed_ec_points()
104 return state.callbacks().tls12_generate_ephemeral_ecdh_key(*m_shared_group, rng, pubkey_point_format);
105 } else {
106 return state.callbacks().tls_generate_ephemeral_key(*m_shared_group, rng);
107 }
108 }();
109
110 if(!m_kex_key) {
111 throw TLS_Exception(Alert::InternalError, "Application did not provide an EC key");
112 }
113
114 const uint16_t named_curve_id = m_shared_group.value().wire_code();
115 m_params.push_back(3); // named curve
116 m_params.push_back(get_byte<0>(named_curve_id));
117 m_params.push_back(get_byte<1>(named_curve_id));
118
119 // Note: In contrast to public_value(), raw_public_key_bits() takes the
120 // point format (compressed vs. uncompressed) into account that was set
121 // in its construction within tls_generate_ephemeral_key().
122 append_tls_length_value(m_params, m_kex_key->raw_public_key_bits(), 1);
123 } else if(kex_algo != Kex_Algo::PSK) {
124 throw Internal_Error("Server_Key_Exchange: Unknown kex type " + kex_method_to_string(kex_algo));
125 }
126
127 if(state.ciphersuite().signature_used()) {
128 BOTAN_ASSERT(signing_key, "Signing key was set");
129
130 std::pair<std::string, Signature_Format> format = state.choose_sig_format(*signing_key, m_scheme, false, policy);
131
132 std::vector<uint8_t> buf = state.client_hello()->random();
133
134 buf += state.server_hello()->random();
135 buf += params();
136
137 m_signature = state.callbacks().tls_sign_message(*signing_key, rng, format.first, format.second, buf);
138 }
139
140 state.hash().update(io.send(*this));
141}
142
143/**
144* Deserialize a Server Key Exchange message
145*/
146Server_Key_Exchange::Server_Key_Exchange(const std::vector<uint8_t>& buf,
147 const Kex_Algo kex_algo,
148 const Auth_Method auth_method,
149 Protocol_Version version) {
150 BOTAN_UNUSED(version); // remove this
151 TLS_Data_Reader reader("ServerKeyExchange", buf);
152
153 /*
154 * Here we are deserializing enough to find out what offset the
155 * signature is at. All processing is done when the Client Key Exchange
156 * is prepared.
157 */
158
159 if(kex_algo == Kex_Algo::PSK || kex_algo == Kex_Algo::ECDHE_PSK) {
160 reader.get_string(2, 0, 65535); // identity hint
161 }
162
163 if(kex_algo == Kex_Algo::DH) {
164 // 3 bigints, DH p, g, Y
165
166 for(size_t i = 0; i != 3; ++i) {
167 reader.get_range<uint8_t>(2, 1, 65535);
168 }
169 } else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
170 reader.get_byte(); // curve type
171 reader.get_uint16_t(); // curve id
172 reader.get_range<uint8_t>(1, 1, 255); // public key
173 } else if(kex_algo != Kex_Algo::PSK) {
174 throw Decoding_Error("Server_Key_Exchange: Unsupported kex type " + kex_method_to_string(kex_algo));
175 }
176
177 m_params.assign(buf.data(), buf.data() + reader.read_so_far());
178
179 if(auth_method != Auth_Method::IMPLICIT) {
180 m_scheme = Signature_Scheme(reader.get_uint16_t());
181 m_signature = reader.get_range<uint8_t>(2, 0, 65535);
182 }
183
184 reader.assert_done();
185}
186
187/**
188* Serialize a Server Key Exchange message
189*/
190std::vector<uint8_t> Server_Key_Exchange::serialize() const {
191 std::vector<uint8_t> buf = params();
192
193 if(!m_signature.empty()) {
194 if(m_scheme.is_set()) {
195 buf.push_back(get_byte<0>(m_scheme.wire_code()));
196 buf.push_back(get_byte<1>(m_scheme.wire_code()));
197 }
198
199 append_tls_length_value(buf, m_signature, 2);
200 }
201
202 return buf;
203}
204
205/**
206* Verify a Server Key Exchange message
207*/
209 const Handshake_State& state,
210 const Policy& policy) const {
211 policy.check_peer_key_acceptable(server_key);
212
213 std::pair<std::string, Signature_Format> format =
214 state.parse_sig_format(server_key, m_scheme, state.client_hello()->signature_schemes(), false, policy);
215
216 std::vector<uint8_t> buf = state.client_hello()->random();
217
218 buf += state.server_hello()->random();
219 buf += params();
220
221 const bool signature_valid =
222 state.callbacks().tls_verify_message(server_key, format.first, format.second, buf, m_signature);
223
224#if defined(BOTAN_UNSAFE_FUZZER_MODE)
225 BOTAN_UNUSED(signature_valid);
226 return true;
227#else
228 return signature_valid;
229#endif
230}
231
233 BOTAN_ASSERT_NONNULL(m_kex_key);
234 return *m_kex_key;
235}
236
237} // namespace Botan::TLS
#define BOTAN_UNUSED
Definition assert.h:144
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:114
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:62
virtual std::string psk_identity_hint(const std::string &type, const std::string &context)
virtual std::unique_ptr< PK_Key_Agreement_Key > tls12_generate_ephemeral_ecdh_key(TLS::Group_Params group, RandomNumberGenerator &rng, EC_Point_Format tls12_ecc_pubkey_encoding_format)
virtual std::vector< uint8_t > tls_sign_message(const Private_Key &key, RandomNumberGenerator &rng, std::string_view padding, Signature_Format format, const std::vector< uint8_t > &msg)
virtual std::unique_ptr< PK_Key_Agreement_Key > tls_generate_ephemeral_key(const std::variant< TLS::Group_Params, DL_Group > &group, RandomNumberGenerator &rng)
virtual bool tls_verify_message(const Public_Key &key, std::string_view padding, Signature_Format format, const std::vector< uint8_t > &msg, const std::vector< uint8_t > &sig)
Kex_Algo kex_method() const
constexpr uint16_t wire_code() const
Definition tls_algos.h:165
void update(const uint8_t in[], size_t length)
virtual std::vector< uint8_t > send(const Handshake_Message &msg)=0
virtual std::vector< uint8_t > serialize() const =0
std::pair< std::string, Signature_Format > parse_sig_format(const Public_Key &key, Signature_Scheme scheme, const std::vector< Signature_Scheme > &offered_schemes, bool for_client_auth, const Policy &policy) const
void client_hello(std::unique_ptr< Client_Hello_12 > client_hello)
void server_hello(std::unique_ptr< Server_Hello_12 > server_hello)
const Ciphersuite & ciphersuite() const
std::pair< std::string, Signature_Format > choose_sig_format(const Private_Key &key, Signature_Scheme &scheme, bool for_client_auth, const Policy &policy) const
virtual void check_peer_key_acceptable(const Public_Key &public_key) const
virtual Group_Params default_dh_group() const
virtual Group_Params choose_key_exchange_group(const std::vector< Group_Params > &supported_by_peer, const std::vector< Group_Params > &offered_by_peer) const
bool verify(const Public_Key &server_key, const Handshake_State &state, const Policy &policy) const
const PK_Key_Agreement_Key & server_kex_key() const
const std::vector< uint8_t > & params() const
Server_Key_Exchange(Handshake_IO &io, Handshake_State &state, const Policy &policy, Credentials_Manager &creds, RandomNumberGenerator &rng, const Private_Key *signing_key=nullptr)
Signature_Scheme::Code wire_code() const noexcept
std::string get_string(size_t len_bytes, size_t min_bytes, size_t max_bytes)
Definition tls_reader.h:123
size_t read_so_far() const
Definition tls_reader.h:36
std::vector< T > get_range(size_t len_bytes, size_t min_elems, size_t max_elems)
Definition tls_reader.h:110
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:184
std::string kex_method_to_string(Kex_Algo method)
Definition tls_algos.cpp:27
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79