Botan  2.7.0
Crypto and TLS for C++11
Public Member Functions | List of all members
Botan::TLS::Server_Key_Exchange Class Referencefinal

#include <tls_messages.h>

Inheritance diagram for Botan::TLS::Server_Key_Exchange:
Botan::TLS::Handshake_Message

Public Member Functions

const std::vector< uint8_t > & params () const
 
const Private_Keyserver_kex_key () const
 
 Server_Key_Exchange (Handshake_IO &io, Handshake_State &state, const Policy &policy, Credentials_Manager &creds, RandomNumberGenerator &rng, const Private_Key *signing_key=nullptr)
 
 Server_Key_Exchange (const std::vector< uint8_t > &buf, Kex_Algo kex_alg, Auth_Method sig_alg, Protocol_Version version)
 
Handshake_Type type () const override
 
std::string type_string () const
 
bool verify (const Public_Key &server_key, const Handshake_State &state, const Policy &policy) const
 
 ~Server_Key_Exchange ()=default
 

Detailed Description

Server Key Exchange Message

Definition at line 513 of file tls_messages.h.

Constructor & Destructor Documentation

◆ Server_Key_Exchange() [1/2]

Botan::TLS::Server_Key_Exchange::Server_Key_Exchange ( Handshake_IO io,
Handshake_State state,
const Policy policy,
Credentials_Manager creds,
RandomNumberGenerator rng,
const Private_Key signing_key = nullptr 
)

Create a new Server Key Exchange message

Definition at line 40 of file msg_server_kex.cpp.

References Botan::TLS::append_tls_length_value(), BOTAN_ASSERT, Botan::TLS::Handshake_State::callbacks(), Botan::TLS::CECPQ1, Botan::CECPQ1_offer(), Botan::CECPQ1_OFFER_BYTES, Botan::TLS::Policy::choose_key_exchange_group(), Botan::TLS::Handshake_State::choose_sig_format(), Botan::TLS::Handshake_State::ciphersuite(), Botan::TLS::Handshake_State::client_hello(), Botan::PointGFp::COMPRESSED, Botan::TLS::Policy::default_dh_group(), Botan::TLS::DH, Botan::TLS::DHE_PSK, Botan::TLS::ECDH, Botan::TLS::ECDHE_PSK, Botan::BigInt::encode(), Botan::get_byte(), Botan::DL_Group::get_g(), Botan::DL_Group::get_p(), Botan::TLS::group_param_is_dh(), Botan::TLS::Alert::HANDSHAKE_FAILURE, Botan::TLS::Handshake_State::hash(), Botan::TLS::Policy::hide_unknown_users(), Botan::TLS::Ciphersuite::kex_method(), Botan::TLS::kex_method_to_string(), Botan::TLS::NONE, params(), Botan::TLS::PSK, Botan::Credentials_Manager::psk_identity_hint(), Botan::TLS::Handshake_IO::send(), Botan::TLS::Handshake_State::server_hello(), Botan::TLS::Ciphersuite::signature_used(), Botan::TLS::SRP_SHA, Botan::Credentials_Manager::srp_verifier(), Botan::TLS::Callbacks::tls_decode_group_param(), Botan::TLS::Callbacks::tls_sign_message(), Botan::PointGFp::UNCOMPRESSED, Botan::TLS::Alert::UNKNOWN_PSK_IDENTITY, Botan::TLS::Handshake_Hash::update(), and Botan::TLS::X25519.

46  {
47  const std::string hostname = state.client_hello()->sni_hostname();
48  const Kex_Algo kex_algo = state.ciphersuite().kex_method();
49 
50  if(kex_algo == Kex_Algo::PSK || kex_algo == Kex_Algo::DHE_PSK || kex_algo == Kex_Algo::ECDHE_PSK)
51  {
52  std::string identity_hint =
53  creds.psk_identity_hint("tls-server", hostname);
54 
55  append_tls_length_value(m_params, identity_hint, 2);
56  }
57 
58  if(kex_algo == Kex_Algo::DH || kex_algo == Kex_Algo::DHE_PSK)
59  {
60  const std::vector<Group_Params> dh_groups = state.client_hello()->supported_dh_groups();
61 
62  Group_Params shared_group = Group_Params::NONE;
63 
64  /*
65  If the client does not send any DH groups in the supported groups
66  extension, but does offer DH ciphersuites, we select a group arbitrarily
67  */
68 
69  if(dh_groups.empty())
70  {
71  shared_group = policy.default_dh_group();
72  }
73  else
74  {
75  shared_group = policy.choose_key_exchange_group(dh_groups);
76  }
77 
78  if(shared_group == Group_Params::NONE)
79  throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
80  "Could not agree on a DH group with the client");
81 
82  BOTAN_ASSERT(group_param_is_dh(shared_group), "DH groups for the DH ciphersuites god");
83 
84  const std::string group_name = state.callbacks().tls_decode_group_param(shared_group);
85  std::unique_ptr<DH_PrivateKey> dh(new DH_PrivateKey(rng, DL_Group(group_name)));
86 
87  append_tls_length_value(m_params, BigInt::encode(dh->get_domain().get_p()), 2);
88  append_tls_length_value(m_params, BigInt::encode(dh->get_domain().get_g()), 2);
89  append_tls_length_value(m_params, dh->public_value(), 2);
90  m_kex_key.reset(dh.release());
91  }
92  else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK)
93  {
94  const std::vector<Group_Params> ec_groups = state.client_hello()->supported_ecc_curves();
95 
96  if(ec_groups.empty())
97  throw Internal_Error("Client sent no ECC extension but we negotiated ECDH");
98 
99  Group_Params shared_group = policy.choose_key_exchange_group(ec_groups);
100 
101  if(shared_group == Group_Params::NONE)
102  throw TLS_Exception(Alert::HANDSHAKE_FAILURE, "No shared ECC group with client");
103 
104  std::vector<uint8_t> ecdh_public_val;
105 
106  if(shared_group == Group_Params::X25519)
107  {
108 #if defined(BOTAN_HAS_CURVE_25519)
109  std::unique_ptr<Curve25519_PrivateKey> x25519(new Curve25519_PrivateKey(rng));
110  ecdh_public_val = x25519->public_value();
111  m_kex_key.reset(x25519.release());
112 #else
113  throw Internal_Error("Negotiated X25519 somehow, but it is disabled");
114 #endif
115  }
116  else
117  {
118  Group_Params curve = policy.choose_key_exchange_group(ec_groups);
119 
120  const std::string curve_name = state.callbacks().tls_decode_group_param(curve);
121 
122  EC_Group ec_group(curve_name);
123  std::unique_ptr<ECDH_PrivateKey> ecdh(new ECDH_PrivateKey(rng, ec_group));
124 
125  // follow client's preference for point compression
126  ecdh_public_val = ecdh->public_value(
127  state.client_hello()->prefers_compressed_ec_points() ?
129 
130  m_kex_key.reset(ecdh.release());
131  }
132 
133  const uint16_t named_curve_id = static_cast<uint16_t>(shared_group);
134  m_params.push_back(3); // named curve
135  m_params.push_back(get_byte(0, named_curve_id));
136  m_params.push_back(get_byte(1, named_curve_id));
137 
138  append_tls_length_value(m_params, ecdh_public_val, 1);
139  }
140 #if defined(BOTAN_HAS_SRP6)
141  else if(kex_algo == Kex_Algo::SRP_SHA)
142  {
143  const std::string srp_identifier = state.client_hello()->srp_identifier();
144 
145  std::string group_id;
146  BigInt v;
147  std::vector<uint8_t> salt;
148 
149  const bool found = creds.srp_verifier("tls-server", hostname,
150  srp_identifier,
151  group_id, v, salt,
152  policy.hide_unknown_users());
153 
154  if(!found)
155  throw TLS_Exception(Alert::UNKNOWN_PSK_IDENTITY,
156  "Unknown SRP user " + srp_identifier);
157 
158  m_srp_params.reset(new SRP6_Server_Session);
159 
160  BigInt B = m_srp_params->step1(v, group_id,
161  "SHA-1", rng);
162 
163  DL_Group group(group_id);
164 
165  append_tls_length_value(m_params, BigInt::encode(group.get_p()), 2);
166  append_tls_length_value(m_params, BigInt::encode(group.get_g()), 2);
167  append_tls_length_value(m_params, salt, 1);
168  append_tls_length_value(m_params, BigInt::encode(B), 2);
169  }
170 #endif
171 #if defined(BOTAN_HAS_CECPQ1)
172  else if(kex_algo == Kex_Algo::CECPQ1)
173  {
174  std::vector<uint8_t> cecpq1_offer(CECPQ1_OFFER_BYTES);
175  m_cecpq1_key.reset(new CECPQ1_key);
176  CECPQ1_offer(cecpq1_offer.data(), m_cecpq1_key.get(), rng);
177  append_tls_length_value(m_params, cecpq1_offer, 2);
178  }
179 #endif
180  else if(kex_algo != Kex_Algo::PSK)
181  {
182  throw Internal_Error("Server_Key_Exchange: Unknown kex type " +
183  kex_method_to_string(kex_algo));
184  }
185 
186  if(state.ciphersuite().signature_used())
187  {
188  BOTAN_ASSERT(signing_key, "Signing key was set");
189 
190  std::pair<std::string, Signature_Format> format =
191  state.choose_sig_format(*signing_key, m_scheme, false, policy);
192 
193  std::vector<uint8_t> buf = state.client_hello()->random();
194 
195  buf += state.server_hello()->random();
196  buf += params();
197 
198  m_signature =
199  state.callbacks().tls_sign_message(*signing_key, rng,
200  format.first, format.second, buf);
201  }
202 
203  state.hash().update(io.send(*this));
204  }
const std::vector< uint8_t > & params() const
Definition: tls_messages.h:518
bool group_param_is_dh(Group_Params group)
Definition: tls_algos.cpp:118
std::string kex_method_to_string(Kex_Algo method)
Definition: tls_algos.cpp:29
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:43
void CECPQ1_offer(uint8_t send[CECPQ1_OFFER_BYTES], CECPQ1_key *offer_key_output, RandomNumberGenerator &rng)
Definition: cecpq1.cpp:15
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:39
static std::vector< uint8_t > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:54
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:185

◆ Server_Key_Exchange() [2/2]

Botan::TLS::Server_Key_Exchange::Server_Key_Exchange ( const std::vector< uint8_t > &  buf,
Kex_Algo  kex_alg,
Auth_Method  sig_alg,
Protocol_Version  version 
)

Deserialize a Server Key Exchange message

Definition at line 209 of file msg_server_kex.cpp.

References Botan::TLS::ANONYMOUS, Botan::TLS::TLS_Data_Reader::assert_done(), Botan::TLS::CECPQ1, Botan::TLS::DH, Botan::TLS::DHE_PSK, Botan::TLS::ECDH, Botan::TLS::ECDHE_PSK, Botan::TLS::TLS_Data_Reader::get_byte(), Botan::TLS::TLS_Data_Reader::get_range(), Botan::TLS::TLS_Data_Reader::get_string(), Botan::TLS::TLS_Data_Reader::get_uint16_t(), Botan::TLS::IMPLICIT, Botan::TLS::kex_method_to_string(), Botan::TLS::PSK, Botan::TLS::TLS_Data_Reader::read_so_far(), Botan::TLS::SRP_SHA, and Botan::TLS::Protocol_Version::supports_negotiable_signature_algorithms().

213  {
214  TLS_Data_Reader reader("ServerKeyExchange", buf);
215 
216  /*
217  * Here we are deserializing enough to find out what offset the
218  * signature is at. All processing is done when the Client Key Exchange
219  * is prepared.
220  */
221 
222  if(kex_algo == Kex_Algo::PSK || kex_algo == Kex_Algo::DHE_PSK || kex_algo == Kex_Algo::ECDHE_PSK)
223  {
224  reader.get_string(2, 0, 65535); // identity hint
225  }
226 
227  if(kex_algo == Kex_Algo::DH || kex_algo == Kex_Algo::DHE_PSK)
228  {
229  // 3 bigints, DH p, g, Y
230 
231  for(size_t i = 0; i != 3; ++i)
232  {
233  reader.get_range<uint8_t>(2, 1, 65535);
234  }
235  }
236  else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK)
237  {
238  reader.get_byte(); // curve type
239  reader.get_uint16_t(); // curve id
240  reader.get_range<uint8_t>(1, 1, 255); // public key
241  }
242  else if(kex_algo == Kex_Algo::SRP_SHA)
243  {
244  // 2 bigints (N,g) then salt, then server B
245 
246  reader.get_range<uint8_t>(2, 1, 65535);
247  reader.get_range<uint8_t>(2, 1, 65535);
248  reader.get_range<uint8_t>(1, 1, 255);
249  reader.get_range<uint8_t>(2, 1, 65535);
250  }
251  else if(kex_algo == Kex_Algo::CECPQ1)
252  {
253  // u16 blob
254  reader.get_range<uint8_t>(2, 1, 65535);
255  }
256  else if(kex_algo != Kex_Algo::PSK)
257  throw Decoding_Error("Server_Key_Exchange: Unsupported kex type " +
258  kex_method_to_string(kex_algo));
259 
260  m_params.assign(buf.data(), buf.data() + reader.read_so_far());
261 
262  if(auth_method != Auth_Method::ANONYMOUS && auth_method != Auth_Method::IMPLICIT)
263  {
264  if(version.supports_negotiable_signature_algorithms())
265  {
266  m_scheme = static_cast<Signature_Scheme>(reader.get_uint16_t());
267  }
268 
269  m_signature = reader.get_range<uint8_t>(2, 0, 65535);
270  }
271 
272  reader.assert_done();
273  }
Signature_Scheme
Definition: tls_algos.h:84
std::string kex_method_to_string(Kex_Algo method)
Definition: tls_algos.cpp:29

◆ ~Server_Key_Exchange()

Botan::TLS::Server_Key_Exchange::~Server_Key_Exchange ( )
default

Member Function Documentation

◆ params()

const std::vector<uint8_t>& Botan::TLS::Server_Key_Exchange::params ( ) const
inline

Definition at line 518 of file tls_messages.h.

Referenced by Server_Key_Exchange(), and verify().

518 { return m_params; }

◆ server_kex_key()

const Private_Key & Botan::TLS::Server_Key_Exchange::server_kex_key ( ) const

Definition at line 325 of file msg_server_kex.cpp.

References BOTAN_ASSERT_NONNULL.

326  {
327  BOTAN_ASSERT_NONNULL(m_kex_key);
328  return *m_kex_key;
329  }
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:95

◆ type()

Handshake_Type Botan::TLS::Server_Key_Exchange::type ( ) const
inlineoverridevirtual
Returns
the message type

Implements Botan::TLS::Handshake_Message.

Definition at line 516 of file tls_messages.h.

References Botan::TLS::SERVER_KEX.

516 { return SERVER_KEX; }

◆ type_string()

std::string Botan::TLS::Handshake_Message::type_string ( ) const
inherited
Returns
string representation of this message type

Definition at line 19 of file tls_handshake_state.cpp.

References Botan::TLS::handshake_type_to_string(), and Botan::TLS::Handshake_Message::type().

20  {
22  }
virtual Handshake_Type type() const =0
const char * handshake_type_to_string(Handshake_Type type)

◆ verify()

bool Botan::TLS::Server_Key_Exchange::verify ( const Public_Key server_key,
const Handshake_State state,
const Policy policy 
) const

Verify a Server Key Exchange message

Definition at line 300 of file msg_server_kex.cpp.

References Botan::TLS::Handshake_State::callbacks(), Botan::TLS::Policy::check_peer_key_acceptable(), Botan::TLS::Handshake_State::client_hello(), params(), Botan::TLS::Handshake_State::parse_sig_format(), Botan::TLS::Handshake_State::server_hello(), and Botan::TLS::Callbacks::tls_verify_message().

303  {
304  policy.check_peer_key_acceptable(server_key);
305 
306  std::pair<std::string, Signature_Format> format =
307  state.parse_sig_format(server_key, m_scheme, false, policy);
308 
309  std::vector<uint8_t> buf = state.client_hello()->random();
310 
311  buf += state.server_hello()->random();
312  buf += params();
313 
314  const bool signature_valid =
315  state.callbacks().tls_verify_message(server_key, format.first, format.second,
316  buf, m_signature);
317 
318 #if defined(BOTAN_UNSAFE_FUZZER_MODE)
319  return true;
320 #else
321  return signature_valid;
322 #endif
323  }
const std::vector< uint8_t > & params() const
Definition: tls_messages.h:518

The documentation for this class was generated from the following files: