Botan  2.4.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, const std::string &kex_alg, const std::string &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 510 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::CECPQ1_offer(), Botan::CECPQ1_OFFER_BYTES, Botan::TLS::Policy::choose_curve(), Botan::TLS::Policy::choose_dh_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::dh_group(), Botan::BigInt::encode(), Botan::get_byte(), Botan::DL_Group::get_g(), Botan::DL_Group::get_p(), Botan::TLS::Alert::HANDSHAKE_FAILURE, Botan::TLS::Handshake_State::hash(), Botan::TLS::Policy::hide_unknown_users(), Botan::TLS::Ciphersuite::kex_algo(), Botan::TLS::Supported_Groups::name_to_curve_id(), params(), Botan::Credentials_Manager::psk_identity_hint(), Botan::TLS::Handshake_IO::send(), Botan::TLS::Handshake_State::server_hello(), Botan::TLS::Ciphersuite::sig_algo(), Botan::Credentials_Manager::srp_verifier(), Botan::TLS::Callbacks::tls_sign_message(), Botan::PointGFp::UNCOMPRESSED, Botan::TLS::Alert::UNKNOWN_PSK_IDENTITY, and Botan::TLS::Handshake_Hash::update().

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

Deserialize a Server Key Exchange message

Definition at line 205 of file msg_server_kex.cpp.

References Botan::TLS::append_tls_length_value(), Botan::TLS::TLS_Data_Reader::assert_done(), 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::Signature_Algorithms::hash_algo_code(), Botan::TLS::Signature_Algorithms::hash_algo_name(), params(), Botan::TLS::TLS_Data_Reader::read_so_far(), Botan::TLS::Signature_Algorithms::sig_algo_code(), Botan::TLS::Signature_Algorithms::sig_algo_name(), and Botan::TLS::Protocol_Version::supports_negotiable_signature_algorithms().

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

◆ ~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 515 of file tls_messages.h.

References BOTAN_ASSERT_NONNULL.

Referenced by Server_Key_Exchange(), and verify().

515 { return m_params; }

◆ server_kex_key()

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

Definition at line 322 of file msg_server_kex.cpp.

References BOTAN_ASSERT_NONNULL.

323  {
324  BOTAN_ASSERT_NONNULL(m_kex_key);
325  return *m_kex_key;
326  }
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:81

◆ type()

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

Implements Botan::TLS::Handshake_Message.

Definition at line 513 of file tls_messages.h.

References Botan::TLS::SERVER_KEX.

513 { 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 296 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().

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

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