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

randombit / botan / 25139258422

29 Apr 2026 08:02PM UTC coverage: 89.37% (-0.02%) from 89.385%
25139258422

push

github

web-flow
Merge pull request #5550 from randombit/jack/tls-misc

TLS conformance, hardening, and performance fixes

107055 of 119789 relevant lines covered (89.37%)

11415549.66 hits per line

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

91.35
/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/bigint.h>
12
#include <botan/credentials_manager.h>
13
#include <botan/dl_group.h>
14
#include <botan/tls_callbacks.h>
15
#include <botan/tls_policy.h>
16
#include <botan/internal/loadstor.h>
17
#include <botan/internal/target_info.h>
18
#include <botan/internal/tls_handshake_io.h>
19
#include <botan/internal/tls_handshake_state.h>
20
#include <botan/internal/tls_reader.h>
21

22
#include <botan/dh.h>
23

24
namespace Botan::TLS {
25

26
Server_Key_Exchange::~Server_Key_Exchange() = default;
6,679✔
27

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

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

43
      append_tls_length_value(m_params, identity_hint, 2);
98✔
44
   }
49✔
45

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

49
      m_shared_group = Group_Params::NONE;
7✔
50

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

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

60
      if(dh_groups.empty()) {
7✔
61
         m_shared_group = policy.default_dh_group();
×
62
      } else {
63
         m_shared_group = policy.choose_key_exchange_group(dh_groups, {});
14✔
64
      }
65

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

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

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

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

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

97
      m_shared_group = policy.choose_key_exchange_group(ec_groups, {});
1,302✔
98

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

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

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

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

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

131
   if(state.ciphersuite().signature_used()) {
695✔
132
      BOTAN_ASSERT(signing_key, "Signing key was set");
646✔
133

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

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

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

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

145
   state.hash().update(io.send(*this));
2,083✔
146
}
697✔
147

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

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

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

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

171
      for(size_t i = 0; i != 3; ++i) {
87✔
172
         reader.get_range<uint8_t>(2, 1, 65535);
131✔
173
      }
174
   } else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
1,005✔
175
      reader.get_byte();                     // curve type
975✔
176
      reader.get_uint16_t();                 // curve id
974✔
177
      reader.get_range<uint8_t>(1, 1, 255);  // public key
1,945✔
178
   } else if(kex_algo != Kex_Algo::PSK) {
30✔
179
      throw Decoding_Error("Server_Key_Exchange: Unsupported kex type " + kex_method_to_string(kex_algo));
×
180
   }
181

182
   m_params.assign(buf.data(), buf.data() + reader.read_so_far());
1,020✔
183

184
   if(auth_method != Auth_Method::IMPLICIT) {
1,020✔
185
      m_scheme = Signature_Scheme(reader.get_uint16_t());
964✔
186
      // RFC 5246 4.7: digitally-signed signatures are opaque<1..2^16-1>.
187
      // Matches the parallel check in Certificate_Verify.
188
      m_signature = reader.get_range<uint8_t>(2, 1, 65535);
961✔
189
   }
190

191
   reader.assert_done();
1,012✔
192
}
1,046✔
193

194
/**
195
* Serialize a Server Key Exchange message
196
*/
197
std::vector<uint8_t> Server_Key_Exchange::serialize() const {
695✔
198
   std::vector<uint8_t> buf = params();
695✔
199

200
   if(!m_signature.empty()) {
695✔
201
      if(m_scheme.is_set()) {
646✔
202
         buf.push_back(get_byte<0>(m_scheme.wire_code()));
646✔
203
         buf.push_back(get_byte<1>(m_scheme.wire_code()));
646✔
204
      }
205

206
      append_tls_length_value(buf, m_signature, 2);
646✔
207
   }
208

209
   return buf;
695✔
210
}
×
211

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

220
   const std::pair<std::string, Signature_Format> format =
908✔
221
      state.parse_sig_format(server_key, m_scheme, state.client_hello()->signature_schemes(), false, policy);
908✔
222

223
   std::vector<uint8_t> buf = state.client_hello()->random();
899✔
224

225
   buf += state.server_hello()->random();
899✔
226
   buf += params();
899✔
227

228
   const bool signature_valid =
899✔
229
      state.callbacks().tls_verify_message(server_key, format.first, format.second, buf, m_signature);
899✔
230

231
#if defined(BOTAN_UNSAFE_FUZZER_MODE)
232
   BOTAN_UNUSED(signature_valid);
233
   return true;
234
#else
235
   return signature_valid;
899✔
236
#endif
237
}
899✔
238

239
const PK_Key_Agreement_Key& Server_Key_Exchange::server_kex_key() const {
643✔
240
   BOTAN_ASSERT_NONNULL(m_kex_key);
643✔
241
   return *m_kex_key;
643✔
242
}
243

244
}  // 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