Botan 3.14.1
Crypto and TLS for C&
Botan::TLS::Server_Key_Exchange Class Referencefinal

#include <tls_messages_12.h>

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

Public Member Functions

Server_Key_Exchange & operator= (const Server_Key_Exchange &other)=delete
Server_Key_Exchange & operator= (Server_Key_Exchange &&other)=delete
const std::vector< uint8_t > & params () const
const PK_Key_Agreement_Key & server_kex_key () const
 Server_Key_Exchange (const Server_Key_Exchange &other)=delete
 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 (Server_Key_Exchange &&other)=delete
 Server_Key_Exchange (std::span< const uint8_t > buf, Kex_Algo kex_alg, Auth_Method sig_alg, Protocol_Version version)
const std::optional< Group_Params > & shared_group () const
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
virtual Handshake_Type wire_type () const
 ~Server_Key_Exchange () override

Detailed Description

Server Key Exchange Message

Definition at line 336 of file tls_messages_12.h.

Constructor & Destructor Documentation

◆ Server_Key_Exchange() [1/4]

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 32 of file msg_server_kex.cpp.

37 {
38 const std::string hostname = state.client_hello().sni_hostname();
39 const Kex_Algo kex_algo = state.ciphersuite().kex_method();
40
41 if(kex_algo == Kex_Algo::PSK || kex_algo == Kex_Algo::ECDHE_PSK) {
42 const std::string identity_hint = creds.psk_identity_hint("tls-server", hostname);
43
44 append_tls_length_value(m_params, identity_hint, 2);
45 }
46
47 if(kex_algo == Kex_Algo::DH) {
48 const std::vector<Group_Params> dh_groups = state.client_hello().supported_dh_groups();
49
50 m_shared_group = Group_Params::NONE;
51
52 /*
53 RFC 7919 requires that if the client sends any groups in the FFDHE
54 range, that we must select one of these. If this is not possible,
55 then we are required to reject the connection.
56
57 If the client did not send any DH groups, but did offer DH ciphersuites
58 and we selected one, then consult the policy for which DH group to pick.
59 */
60
61 if(dh_groups.empty()) {
62 m_shared_group = policy.default_dh_group();
63 } else {
64 m_shared_group = policy.choose_key_exchange_group(dh_groups, {});
65 }
66
67 if(m_shared_group.value() == Group_Params::NONE) {
68 throw TLS_Exception(Alert::HandshakeFailure, "Could not agree on a DH group with the client");
69 }
70
71 // The policy had better return a group we know about:
72 BOTAN_ASSERT(m_shared_group.value().is_dh_named_group(), "DH ciphersuite is using a known finite field group");
73
74 // Note: TLS 1.2 allows defining and using arbitrary DH groups (additional
75 // to the named and standardized ones). This API doesn't allow the
76 // server to make use of that at the moment. TLS 1.3 does not
77 // provide this flexibility!
78 //
79 // A possible implementation strategy in case one would ever need that:
80 // `Policy::default_dh_group()` could return a `std::variant<Group_Params,
81 // DL_Group>`, allowing it to define arbitrary groups.
82 m_kex_key = state.callbacks().tls_generate_ephemeral_key(m_shared_group.value(), rng);
83 const auto* dh = dynamic_cast<DH_PrivateKey*>(m_kex_key.get());
84 if(dh == nullptr) {
85 throw TLS_Exception(Alert::InternalError, "Application did not provide a Diffie-Hellman key");
86 }
87
88 append_tls_length_value(m_params, dh->get_int_field("p").serialize(), 2);
89 append_tls_length_value(m_params, dh->get_int_field("g").serialize(), 2);
90 append_tls_length_value(m_params, dh->public_value(), 2);
91 } else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
92 const std::vector<Group_Params> ec_groups =
93 groups_usable_in_version(state.client_hello().supported_ecc_curves(), Protocol_Version::TLS_V12);
94
95 if(ec_groups.empty()) {
96 throw Internal_Error("Client sent no ECC extension but we negotiated ECDH");
97 }
98
99 m_shared_group = policy.choose_key_exchange_group(ec_groups, {});
100
101 if(m_shared_group.value() == Group_Params::NONE) {
102 throw TLS_Exception(Alert::HandshakeFailure, "No shared ECC group with client");
103 }
104
105 m_kex_key = [&] {
106 if(m_shared_group->is_ecdh_named_curve()) {
107 const auto pubkey_point_format = state.client_hello().prefers_compressed_ec_points()
110 return state.callbacks().tls12_generate_ephemeral_ecdh_key(*m_shared_group, rng, pubkey_point_format);
111 } else {
112 return state.callbacks().tls_generate_ephemeral_key(*m_shared_group, rng);
113 }
114 }();
115
116 if(!m_kex_key) {
117 throw TLS_Exception(Alert::InternalError, "Application did not provide an EC key");
118 }
119
120 const uint16_t named_curve_id = m_shared_group.value().wire_code();
121 m_params.push_back(3); // named curve
122 m_params.push_back(get_byte<0>(named_curve_id));
123 m_params.push_back(get_byte<1>(named_curve_id));
124
125 // Note: In contrast to public_value(), raw_public_key_bits() takes the
126 // point format (compressed vs. uncompressed) into account that was set
127 // in its construction within tls_generate_ephemeral_key().
128 append_tls_length_value(m_params, m_kex_key->raw_public_key_bits(), 1);
129 } else if(kex_algo != Kex_Algo::PSK) {
130 throw Internal_Error("Server_Key_Exchange: Unknown kex type " + kex_method_to_string(kex_algo));
131 }
132
133 if(state.ciphersuite().signature_used()) {
134 BOTAN_ASSERT(signing_key, "Signing key was set");
135
136 const std::pair<std::string, Signature_Format> format =
137 state.choose_sig_format(*signing_key, m_scheme, false, policy);
138
139 std::vector<uint8_t> buf = state.client_hello().random();
140
141 buf += state.server_hello().random();
142 buf += params();
143
144 m_signature = state.callbacks().tls_sign_message(*signing_key, rng, format.first, format.second, buf);
145 }
146
147 state.hash().update(io.send(*this));
148}
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:69
const std::vector< uint8_t > & params() const
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:182
std::vector< Group_Params > groups_usable_in_version(std::span< const Group_Params > groups, Protocol_Version version)
std::string kex_method_to_string(Kex_Algo method)
Definition tls_algos.cpp:33
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79

References Botan::TLS::append_tls_length_value(), BOTAN_ASSERT, Botan::TLS::Handshake_State::callbacks(), 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::Compressed, Botan::TLS::Policy::default_dh_group(), Botan::TLS::DH, Botan::TLS::ECDH, Botan::TLS::ECDHE_PSK, Botan::get_byte(), Botan::TLS::groups_usable_in_version(), Botan::TLS::Handshake_State::hash(), Botan::TLS::Ciphersuite::kex_method(), Botan::TLS::kex_method_to_string(), 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::Callbacks::tls12_generate_ephemeral_ecdh_key(), Botan::TLS::Callbacks::tls_generate_ephemeral_key(), Botan::TLS::Callbacks::tls_sign_message(), Botan::Uncompressed, Botan::TLS::Handshake_Hash::update(), and Botan::TLS::Group_Params::wire_code().

Referenced by operator=(), operator=(), Server_Key_Exchange(), and Server_Key_Exchange().

◆ Server_Key_Exchange() [2/4]

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

Deserialize a Server Key Exchange message

Definition at line 153 of file msg_server_kex.cpp.

156 {
157 BOTAN_UNUSED(version); // remove this
158 TLS_Data_Reader reader("ServerKeyExchange", buf);
159
160 /*
161 * Here we are deserializing enough to find out what offset the
162 * signature is at. All processing is done when the Client Key Exchange
163 * is prepared.
164 */
165
166 if(kex_algo == Kex_Algo::PSK || kex_algo == Kex_Algo::ECDHE_PSK) {
167 reader.get_string(2, 0, 65535); // identity hint
168 }
169
170 if(kex_algo == Kex_Algo::DH) {
171 // 3 bigints, DH p, g, Y
172
173 for(size_t i = 0; i != 3; ++i) {
174 reader.get_range<uint8_t>(2, 1, 65535);
175 }
176 } else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
177 reader.get_byte(); // curve type
178 reader.get_uint16_t(); // curve id
179 reader.get_range<uint8_t>(1, 1, 255); // public key
180 } else if(kex_algo != Kex_Algo::PSK) {
181 throw Decoding_Error("Server_Key_Exchange: Unsupported kex type " + kex_method_to_string(kex_algo));
182 }
183
184 m_params.assign(buf.data(), buf.data() + reader.read_so_far());
185
186 if(auth_method != Auth_Method::IMPLICIT) {
187 m_scheme = Signature_Scheme(reader.get_uint16_t());
188 // RFC 5246 4.7: digitally-signed signatures are opaque<1..2^16-1>.
189 // Matches the parallel check in Certificate_Verify.
190 m_signature = reader.get_range<uint8_t>(2, 1, 65535);
191 }
192
193 reader.assert_done();
194}
#define BOTAN_UNUSED
Definition assert.h:151

References Botan::TLS::TLS_Data_Reader::assert_done(), BOTAN_UNUSED, Botan::TLS::DH, 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, and Botan::TLS::TLS_Data_Reader::read_so_far().

◆ ~Server_Key_Exchange()

Botan::TLS::Server_Key_Exchange::~Server_Key_Exchange ( )
overridedefault

◆ Server_Key_Exchange() [3/4]

Botan::TLS::Server_Key_Exchange::Server_Key_Exchange ( const Server_Key_Exchange & other)
delete

References Server_Key_Exchange().

◆ Server_Key_Exchange() [4/4]

Botan::TLS::Server_Key_Exchange::Server_Key_Exchange ( Server_Key_Exchange && other)
delete

References Server_Key_Exchange().

Member Function Documentation

◆ operator=() [1/2]

Server_Key_Exchange & Botan::TLS::Server_Key_Exchange::operator= ( const Server_Key_Exchange & other)
delete

References Server_Key_Exchange().

◆ operator=() [2/2]

Server_Key_Exchange & Botan::TLS::Server_Key_Exchange::operator= ( Server_Key_Exchange && other)
delete

References Server_Key_Exchange().

◆ params()

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

Definition at line 340 of file tls_messages_12.h.

340{ return m_params; }

Referenced by Server_Key_Exchange(), and verify().

◆ server_kex_key()

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

Definition at line 241 of file msg_server_kex.cpp.

241 {
242 BOTAN_ASSERT_NONNULL(m_kex_key);
243 return *m_kex_key;
244}
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:121

References BOTAN_ASSERT_NONNULL.

◆ shared_group()

const std::optional< Group_Params > & Botan::TLS::Server_Key_Exchange::shared_group ( ) const
inline
Returns
the agreed upon KEX group or std::nullopt if the KEX type does not depend on a group

Definition at line 351 of file tls_messages_12.h.

351{ return m_shared_group; }

◆ type()

Handshake_Type Botan::TLS::Server_Key_Exchange::type ( ) const
inlineoverridevirtual

Return the TLS handshake type code

Returns
the message type

Implements Botan::TLS::Handshake_Message.

Definition at line 338 of file tls_messages_12.h.

References Botan::TLS::ServerKeyExchange.

◆ type_string()

std::string Botan::TLS::Handshake_Message::type_string ( ) const
inherited

Return a free-form string describing this message type

Returns
string representation of this message type

Definition at line 22 of file tls_handshake_state.cpp.

22 {
24}
virtual Handshake_Type type() const =0
const char * handshake_type_to_string(Handshake_Type type)
Definition tls_magic.cpp:15

References Botan::TLS::handshake_type_to_string(), and 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 217 of file msg_server_kex.cpp.

219 {
220 policy.check_peer_key_acceptable(server_key);
221
222 const std::pair<std::string, Signature_Format> format =
223 state.parse_sig_format(server_key, m_scheme, state.client_hello().signature_schemes(), false, policy);
224
225 std::vector<uint8_t> buf = state.client_hello().random();
226
227 buf += state.server_hello().random();
228 buf += params();
229
230 const bool signature_valid =
231 state.callbacks().tls_verify_message(server_key, format.first, format.second, buf, m_signature);
232
233#if defined(BOTAN_UNSAFE_FUZZER_MODE)
234 BOTAN_UNUSED(signature_valid);
235 return true;
236#else
237 return signature_valid;
238#endif
239}

References BOTAN_UNUSED, 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().

◆ wire_type()

virtual Handshake_Type Botan::TLS::Handshake_Message::wire_type ( ) const
inlinevirtualinherited

Return the wire encoding of the message type code

Note
This is usually equal to type with the exception of a TLS 1.3 Helloy Retry Request.
Returns
the wire representation of the message's type

Reimplemented in Botan::TLS::Hello_Retry_Request.

Definition at line 47 of file tls_handshake_msg.h.

47{ return type(); }

References type().

Referenced by Botan::TLS::Stream_Handshake_IO::send(), and Botan::TLS::Handshake_Layer::update_transcript_for_psk_binder_calc().


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