• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 21753596263

06 Feb 2026 02:13PM UTC coverage: 90.063% (-0.01%) from 90.073%
21753596263

Pull #5289

github

web-flow
Merge 587099284 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102237 of 113517 relevant lines covered (90.06%)

11402137.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.26
/src/lib/tls/tls12/msg_server_kex.cpp
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_12.h>
10

11
#include <botan/credentials_manager.h>
12
#include <botan/dl_group.h>
13
#include <botan/pubkey.h>
14
#include <botan/tls_callbacks.h>
15
#include <botan/tls_extensions.h>
16
#include <botan/tls_policy.h>
17
#include <botan/internal/loadstor.h>
18
#include <botan/internal/target_info.h>
19
#include <botan/internal/tls_handshake_io.h>
20
#include <botan/internal/tls_handshake_state.h>
21
#include <botan/internal/tls_reader.h>
22

23
#include <botan/dh.h>
24

25
namespace Botan::TLS {
26

27
/**
28
* Create a new Server Key Exchange message
29
*/
30
Server_Key_Exchange::Server_Key_Exchange(Handshake_IO& io,
659✔
31
                                         Handshake_State& state,
32
                                         const Policy& policy,
33
                                         Credentials_Manager& creds,
34
                                         RandomNumberGenerator& rng,
35
                                         const Private_Key* signing_key) {
659✔
36
   const std::string hostname = state.client_hello()->sni_hostname();
659✔
37
   const Kex_Algo kex_algo = state.ciphersuite().kex_method();
659✔
38

39
   if(kex_algo == Kex_Algo::PSK || kex_algo == Kex_Algo::ECDHE_PSK) {
659✔
40
      const std::string identity_hint = creds.psk_identity_hint("tls-server", hostname);
50✔
41

42
      append_tls_length_value(m_params, identity_hint, 2);
100✔
43
   }
50✔
44

45
   if(kex_algo == Kex_Algo::DH) {
659✔
46
      const std::vector<Group_Params> dh_groups = state.client_hello()->supported_dh_groups();
6✔
47

48
      m_shared_group = Group_Params::NONE;
6✔
49

50
      /*
51
      RFC 7919 requires that if the client sends any groups in the FFDHE
52
      range, that we must select one of these. If this is not possible,
53
      then we are required to reject the connection.
54

55
      If the client did not send any DH groups, but did offer DH ciphersuites
56
      and we selected one, then consult the policy for which DH group to pick.
57
      */
58

59
      if(dh_groups.empty()) {
6✔
60
         m_shared_group = policy.default_dh_group();
×
61
      } else {
62
         m_shared_group = policy.choose_key_exchange_group(dh_groups, {});
12✔
63
      }
64

65
      if(m_shared_group.value() == Group_Params::NONE) {
6✔
66
         throw TLS_Exception(Alert::HandshakeFailure, "Could not agree on a DH group with the client");
×
67
      }
68

69
      // The policy had better return a group we know about:
70
      BOTAN_ASSERT(m_shared_group.value().is_dh_named_group(), "DH ciphersuite is using a known finite field group");
6✔
71

72
      // Note: TLS 1.2 allows defining and using arbitrary DH groups (additional
73
      //       to the named and standardized ones). This API doesn't allow the
74
      //       server to make use of that at the moment. TLS 1.3 does not
75
      //       provide this flexibility!
76
      //
77
      // A possible implementation strategy in case one would ever need that:
78
      // `Policy::default_dh_group()` could return a `std::variant<Group_Params,
79
      // DL_Group>`, allowing it to define arbitrary groups.
80
      m_kex_key = state.callbacks().tls_generate_ephemeral_key(m_shared_group.value(), rng);
6✔
81
      auto* dh = dynamic_cast<DH_PrivateKey*>(m_kex_key.get());
6✔
82
      if(dh == nullptr) {
6✔
83
         throw TLS_Exception(Alert::InternalError, "Application did not provide a Diffie-Hellman key");
×
84
      }
85

86
      append_tls_length_value(m_params, dh->get_int_field("p").serialize(), 2);
18✔
87
      append_tls_length_value(m_params, dh->get_int_field("g").serialize(), 2);
18✔
88
      append_tls_length_value(m_params, dh->public_value(), 2);
18✔
89
   } else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
659✔
90
      const std::vector<Group_Params> ec_groups = state.client_hello()->supported_ecc_curves();
615✔
91

92
      if(ec_groups.empty()) {
615✔
93
         throw Internal_Error("Client sent no ECC extension but we negotiated ECDH");
×
94
      }
95

96
      m_shared_group = policy.choose_key_exchange_group(ec_groups, {});
1,230✔
97

98
      if(m_shared_group.value() == Group_Params::NONE) {
615✔
99
         throw TLS_Exception(Alert::HandshakeFailure, "No shared ECC group with client");
×
100
      }
101

102
      m_kex_key = [&] {
1,845✔
103
         if(m_shared_group->is_ecdh_named_curve()) {
615✔
104
            const auto pubkey_point_format = state.client_hello()->prefers_compressed_ec_points()
28✔
105
                                                ? EC_Point_Format::Compressed
28✔
106
                                                : EC_Point_Format::Uncompressed;
28✔
107
            return state.callbacks().tls12_generate_ephemeral_ecdh_key(*m_shared_group, rng, pubkey_point_format);
28✔
108
         } else {
109
            return state.callbacks().tls_generate_ephemeral_key(*m_shared_group, rng);
1,174✔
110
         }
111
      }();
615✔
112

113
      if(!m_kex_key) {
615✔
114
         throw TLS_Exception(Alert::InternalError, "Application did not provide an EC key");
×
115
      }
116

117
      const uint16_t named_curve_id = m_shared_group.value().wire_code();
615✔
118
      m_params.push_back(3);  // named curve
615✔
119
      m_params.push_back(get_byte<0>(named_curve_id));
615✔
120
      m_params.push_back(get_byte<1>(named_curve_id));
615✔
121

122
      // Note: In contrast to public_value(), raw_public_key_bits() takes the
123
      // point format (compressed vs. uncompressed) into account that was set
124
      // in its construction within tls_generate_ephemeral_key().
125
      append_tls_length_value(m_params, m_kex_key->raw_public_key_bits(), 1);
1,845✔
126
   } else if(kex_algo != Kex_Algo::PSK) {
653✔
127
      throw Internal_Error("Server_Key_Exchange: Unknown kex type " + kex_method_to_string(kex_algo));
×
128
   }
129

130
   if(state.ciphersuite().signature_used()) {
659✔
131
      BOTAN_ASSERT(signing_key, "Signing key was set");
609✔
132

133
      const std::pair<std::string, Signature_Format> format =
609✔
134
         state.choose_sig_format(*signing_key, m_scheme, false, policy);
609✔
135

136
      std::vector<uint8_t> buf = state.client_hello()->random();
609✔
137

138
      buf += state.server_hello()->random();
609✔
139
      buf += params();
609✔
140

141
      m_signature = state.callbacks().tls_sign_message(*signing_key, rng, format.first, format.second, buf);
609✔
142
   }
609✔
143

144
   state.hash().update(io.send(*this));
1,975✔
145
}
661✔
146

147
/**
148
* Deserialize a Server Key Exchange message
149
*/
150
Server_Key_Exchange::Server_Key_Exchange(const std::vector<uint8_t>& buf,
1,006✔
151
                                         const Kex_Algo kex_algo,
152
                                         const Auth_Method auth_method,
153
                                         Protocol_Version version) {
1,006✔
154
   BOTAN_UNUSED(version);  // remove this
1,006✔
155
   TLS_Data_Reader reader("ServerKeyExchange", buf);
1,006✔
156

157
   /*
158
   * Here we are deserializing enough to find out what offset the
159
   * signature is at. All processing is done when the Client Key Exchange
160
   * is prepared.
161
   */
162

163
   if(kex_algo == Kex_Algo::PSK || kex_algo == Kex_Algo::ECDHE_PSK) {
1,006✔
164
      reader.get_string(2, 0, 65535);  // identity hint
114✔
165
   }
166

167
   if(kex_algo == Kex_Algo::DH) {
1,006✔
168
      // 3 bigints, DH p, g, Y
169

170
      for(size_t i = 0; i != 3; ++i) {
95✔
171
         reader.get_range<uint8_t>(2, 1, 65535);
143✔
172
      }
173
   } else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
979✔
174
      reader.get_byte();                     // curve type
948✔
175
      reader.get_uint16_t();                 // curve id
947✔
176
      reader.get_range<uint8_t>(1, 1, 255);  // public key
1,891✔
177
   } else if(kex_algo != Kex_Algo::PSK) {
31✔
178
      throw Decoding_Error("Server_Key_Exchange: Unsupported kex type " + kex_method_to_string(kex_algo));
×
179
   }
180

181
   m_params.assign(buf.data(), buf.data() + reader.read_so_far());
996✔
182

183
   if(auth_method != Auth_Method::IMPLICIT) {
996✔
184
      m_scheme = Signature_Scheme(reader.get_uint16_t());
939✔
185
      m_signature = reader.get_range<uint8_t>(2, 0, 65535);
936✔
186
   }
187

188
   reader.assert_done();
988✔
189
}
1,022✔
190

191
/**
192
* Serialize a Server Key Exchange message
193
*/
194
std::vector<uint8_t> Server_Key_Exchange::serialize() const {
659✔
195
   std::vector<uint8_t> buf = params();
659✔
196

197
   if(!m_signature.empty()) {
659✔
198
      if(m_scheme.is_set()) {
609✔
199
         buf.push_back(get_byte<0>(m_scheme.wire_code()));
609✔
200
         buf.push_back(get_byte<1>(m_scheme.wire_code()));
609✔
201
      }
202

203
      append_tls_length_value(buf, m_signature, 2);
609✔
204
   }
205

206
   return buf;
659✔
207
}
×
208

209
/**
210
* Verify a Server Key Exchange message
211
*/
212
bool Server_Key_Exchange::verify(const Public_Key& server_key,
927✔
213
                                 const Handshake_State& state,
214
                                 const Policy& policy) const {
215
   policy.check_peer_key_acceptable(server_key);
927✔
216

217
   const std::pair<std::string, Signature_Format> format =
878✔
218
      state.parse_sig_format(server_key, m_scheme, state.client_hello()->signature_schemes(), false, policy);
878✔
219

220
   std::vector<uint8_t> buf = state.client_hello()->random();
869✔
221

222
   buf += state.server_hello()->random();
869✔
223
   buf += params();
869✔
224

225
   const bool signature_valid =
869✔
226
      state.callbacks().tls_verify_message(server_key, format.first, format.second, buf, m_signature);
869✔
227

228
#if defined(BOTAN_UNSAFE_FUZZER_MODE)
229
   BOTAN_UNUSED(signature_valid);
230
   return true;
231
#else
232
   return signature_valid;
869✔
233
#endif
234
}
869✔
235

236
const PK_Key_Agreement_Key& Server_Key_Exchange::server_kex_key() const {
606✔
237
   BOTAN_ASSERT_NONNULL(m_kex_key);
606✔
238
   return *m_kex_key;
606✔
239
}
240

241
}  // namespace Botan::TLS
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc