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

randombit / botan / 19012754211

02 Nov 2025 01:10PM UTC coverage: 90.677% (+0.006%) from 90.671%
19012754211

push

github

web-flow
Merge pull request #5137 from randombit/jack/clang-tidy-includes

Remove various unused includes flagged by clang-tidy misc-include-cleaner

100457 of 110786 relevant lines covered (90.68%)

12189873.8 hits per line

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

87.84
/src/lib/tls/tls12/msg_client_kex.cpp
1
/*
2
* Client Key Exchange Message
3
* (C) 2004-2010,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.h>
10

11
#include <botan/credentials_manager.h>
12
#include <botan/rng.h>
13
#include <botan/rsa.h>
14
#include <botan/tls_extensions.h>
15
#include <botan/internal/ct_utils.h>
16
#include <botan/internal/tls_handshake_hash.h>
17
#include <botan/internal/tls_handshake_io.h>
18
#include <botan/internal/tls_handshake_state.h>
19
#include <botan/internal/tls_reader.h>
20

21
namespace Botan::TLS {
22

23
/*
24
* Create a new Client Key Exchange message
25
*/
26
Client_Key_Exchange::Client_Key_Exchange(Handshake_IO& io,
827✔
27
                                         Handshake_State& state,
28
                                         const Policy& policy,
29
                                         Credentials_Manager& creds,
30
                                         const Public_Key* server_public_key,
31
                                         std::string_view hostname,
32
                                         RandomNumberGenerator& rng) {
827✔
33
   const Kex_Algo kex_algo = state.ciphersuite().kex_method();
827✔
34

35
   if(kex_algo == Kex_Algo::PSK) {
827✔
36
      std::string identity_hint;
47✔
37

38
      if(state.server_kex() != nullptr) {
47✔
39
         TLS_Data_Reader reader("ClientKeyExchange", state.server_kex()->params());
30✔
40
         identity_hint = reader.get_string(2, 0, 65535);
30✔
41
      }
42

43
      m_psk_identity = creds.psk_identity("tls-client", std::string(hostname), identity_hint);
94✔
44

45
      append_tls_length_value(m_key_material, as_span_of_bytes(m_psk_identity.value()), 2);
47✔
46

47
      SymmetricKey psk = creds.psk("tls-client", std::string(hostname), m_psk_identity.value());
141✔
48

49
      std::vector<uint8_t> zeros(psk.length());
47✔
50

51
      append_tls_length_value(m_pre_master, zeros, 2);
47✔
52
      append_tls_length_value(m_pre_master, psk.bits_of(), 2);
141✔
53
   } else if(state.server_kex() != nullptr) {
874✔
54
      TLS_Data_Reader reader("ClientKeyExchange", state.server_kex()->params());
700✔
55

56
      SymmetricKey psk;
700✔
57

58
      if(kex_algo == Kex_Algo::ECDHE_PSK) {
700✔
59
         std::string identity_hint = reader.get_string(2, 0, 65535);
22✔
60

61
         m_psk_identity = creds.psk_identity("tls-client", std::string(hostname), identity_hint);
44✔
62

63
         append_tls_length_value(m_key_material, as_span_of_bytes(m_psk_identity.value()), 2);
22✔
64

65
         psk = creds.psk("tls-client", std::string(hostname), m_psk_identity.value());
66✔
66
      }
22✔
67

68
      if(kex_algo == Kex_Algo::DH) {
700✔
69
         const auto modulus = BigInt::from_bytes(reader.get_range<uint8_t>(2, 1, 65535));
21✔
70
         const auto generator = BigInt::from_bytes(reader.get_range<uint8_t>(2, 1, 65535));
5✔
71
         const std::vector<uint8_t> peer_public_value = reader.get_range<uint8_t>(2, 1, 65535);
5✔
72

73
         if(reader.remaining_bytes() > 0) {
5✔
74
            throw Decoding_Error("Bad params size for DH key exchange");
×
75
         }
76

77
         DL_Group group(modulus, generator);
5✔
78

79
         if(!group.verify_group(rng, false)) {
5✔
80
            throw TLS_Exception(Alert::InsufficientSecurity, "DH group validation failed");
×
81
         }
82

83
         const auto private_key = state.callbacks().tls_generate_ephemeral_key(group, rng);
5✔
84
         m_pre_master = CT::strip_leading_zeros(
15✔
85
            state.callbacks().tls_ephemeral_key_agreement(group, *private_key, peer_public_value, rng, policy));
15✔
86
         append_tls_length_value(m_key_material, private_key->public_value(), 2);
15✔
87
      } else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
710✔
88
         const uint8_t curve_type = reader.get_byte();
695✔
89
         if(curve_type != 3) {
695✔
90
            throw Decoding_Error("Server sent non-named ECC curve");
×
91
         }
92

93
         const Group_Params curve_id = static_cast<Group_Params>(reader.get_uint16_t());
695✔
94
         const std::vector<uint8_t> peer_public_value = reader.get_range<uint8_t>(1, 1, 255);
695✔
95

96
         if(!curve_id.is_ecdh_named_curve() && !curve_id.is_x25519() && !curve_id.is_x448()) {
695✔
97
            throw TLS_Exception(Alert::IllegalParameter,
3✔
98
                                "Server selected a group that is not compatible with the negotiated ciphersuite");
3✔
99
         }
100

101
         if(policy.choose_key_exchange_group({curve_id}, {}) != curve_id) {
1,384✔
102
            throw TLS_Exception(Alert::HandshakeFailure, "Server sent ECC curve prohibited by policy");
1✔
103
         }
104

105
         const auto private_key = [&] {
16✔
106
            if(curve_id.is_ecdh_named_curve()) {
691✔
107
               const auto pubkey_point_format = state.server_hello()->prefers_compressed_ec_points()
25✔
108
                                                   ? EC_Point_Format::Compressed
25✔
109
                                                   : EC_Point_Format::Uncompressed;
25✔
110
               return state.callbacks().tls12_generate_ephemeral_ecdh_key(curve_id, rng, pubkey_point_format);
25✔
111
            } else {
112
               return state.callbacks().tls_generate_ephemeral_key(curve_id, rng);
1,332✔
113
            }
114
         }();
691✔
115

116
         if(!private_key) {
691✔
117
            throw TLS_Exception(Alert::InternalError, "Application did not provide an EC key");
×
118
         }
119

120
         auto shared_secret =
691✔
121
            state.callbacks().tls_ephemeral_key_agreement(curve_id, *private_key, peer_public_value, rng, policy);
703✔
122

123
         if(kex_algo == Kex_Algo::ECDH) {
679✔
124
            m_pre_master = std::move(shared_secret);
657✔
125
         } else {
126
            append_tls_length_value(m_pre_master, shared_secret, 2);
22✔
127
            append_tls_length_value(m_pre_master, psk.bits_of(), 2);
66✔
128
         }
129

130
         // Note: In contrast to public_value(), raw_public_key_bits() takes the
131
         // point format (compressed vs. uncompressed) into account that was set
132
         // in its construction within tls_generate_ephemeral_key().
133
         append_tls_length_value(m_key_material, private_key->raw_public_key_bits(), 1);
2,037✔
134
      } else {
2,065✔
135
         throw Internal_Error("Client_Key_Exchange: Unknown key exchange method was negotiated");
×
136
      }
137

138
      reader.assert_done();
684✔
139
   } else {
700✔
140
      // No server key exchange msg better mean RSA kex + RSA key in cert
141

142
      if(kex_algo != Kex_Algo::STATIC_RSA) {
80✔
143
         throw Unexpected_Message("No server kex message, but negotiated a key exchange that required it");
×
144
      }
145

146
      if(server_public_key == nullptr) {
80✔
147
         throw Internal_Error("No server public key for RSA exchange");
×
148
      }
149

150
      if(const auto* rsa_pub = dynamic_cast<const RSA_PublicKey*>(server_public_key)) {
80✔
151
         const Protocol_Version offered_version = state.client_hello()->legacy_version();
80✔
152

153
         rng.random_vec(m_pre_master, 48);
80✔
154
         m_pre_master[0] = offered_version.major_version();
80✔
155
         m_pre_master[1] = offered_version.minor_version();
80✔
156

157
         PK_Encryptor_EME encryptor(*rsa_pub, rng, "PKCS1v15");
80✔
158

159
         const std::vector<uint8_t> encrypted_key = encryptor.encrypt(m_pre_master, rng);
80✔
160

161
         append_tls_length_value(m_key_material, encrypted_key, 2);
160✔
162
      } else {
80✔
163
         throw TLS_Exception(Alert::HandshakeFailure,
×
164
                             "Expected a RSA key in server cert but got " + server_public_key->algo_name());
×
165
      }
166
   }
167

168
   state.hash().update(io.send(*this));
1,622✔
169
}
827✔
170

171
/*
172
* Read a Client Key Exchange message
173
*/
174
Client_Key_Exchange::Client_Key_Exchange(const std::vector<uint8_t>& contents,
696✔
175
                                         const Handshake_State& state,
176
                                         const Private_Key* server_rsa_kex_key,
177
                                         Credentials_Manager& creds,
178
                                         const Policy& policy,
179
                                         RandomNumberGenerator& rng) {
696✔
180
   const Kex_Algo kex_algo = state.ciphersuite().kex_method();
696✔
181

182
   if(kex_algo == Kex_Algo::STATIC_RSA) {
696✔
183
      BOTAN_ASSERT(state.server_certs() && !state.server_certs()->cert_chain().empty(),
58✔
184
                   "RSA key exchange negotiated so server sent a certificate");
185

186
      if(server_rsa_kex_key == nullptr) {
58✔
187
         throw Internal_Error("Expected RSA kex but no server kex key set");
×
188
      }
189

190
      if(server_rsa_kex_key->algo_name() != "RSA") {
58✔
191
         throw Internal_Error("Expected RSA key but got " + server_rsa_kex_key->algo_name());
×
192
      }
193

194
      TLS_Data_Reader reader("ClientKeyExchange", contents);
58✔
195
      const std::vector<uint8_t> encrypted_pre_master = reader.get_range<uint8_t>(2, 0, 65535);
58✔
196
      reader.assert_done();
58✔
197

198
      PK_Decryptor_EME decryptor(*server_rsa_kex_key, rng, "PKCS1v15");
58✔
199

200
      const uint8_t client_major = state.client_hello()->legacy_version().major_version();
58✔
201
      const uint8_t client_minor = state.client_hello()->legacy_version().minor_version();
58✔
202

203
      /*
204
      * PK_Decryptor::decrypt_or_random will return a random value if
205
      * either the length does not match the expected value or if the
206
      * version number embedded in the PMS does not match the one sent
207
      * in the client hello.
208
      */
209
      const size_t expected_plaintext_size = 48;
58✔
210
      const size_t expected_content_size = 2;
58✔
211
      const uint8_t expected_content_bytes[expected_content_size] = {client_major, client_minor};
58✔
212
      const uint8_t expected_content_pos[expected_content_size] = {0, 1};
58✔
213

214
      m_pre_master = decryptor.decrypt_or_random(encrypted_pre_master.data(),
58✔
215
                                                 encrypted_pre_master.size(),
216
                                                 expected_plaintext_size,
217
                                                 rng,
218
                                                 expected_content_bytes,
219
                                                 expected_content_pos,
220
                                                 expected_content_size);
58✔
221
   } else {
116✔
222
      TLS_Data_Reader reader("ClientKeyExchange", contents);
638✔
223

224
      SymmetricKey psk;
638✔
225

226
      if(key_exchange_is_psk(kex_algo)) {
638✔
227
         m_psk_identity = reader.get_string(2, 0, 65535);
47✔
228

229
         psk = creds.psk("tls-server", state.client_hello()->sni_hostname(), m_psk_identity.value());
94✔
230

231
         if(psk.empty()) {
47✔
232
            if(policy.hide_unknown_users()) {
×
233
               psk = SymmetricKey(rng, 16);
×
234
            } else {
235
               throw TLS_Exception(Alert::UnknownPSKIdentity, "No PSK for identifier " + m_psk_identity.value());
×
236
            }
237
         }
238
      }
239

240
      if(kex_algo == Kex_Algo::PSK) {
47✔
241
         std::vector<uint8_t> zeros(psk.length());
37✔
242
         append_tls_length_value(m_pre_master, zeros, 2);
37✔
243
         append_tls_length_value(m_pre_master, psk.bits_of(), 2);
111✔
244
      } else if(kex_algo == Kex_Algo::DH || kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
638✔
245
         const PK_Key_Agreement_Key& ka_key = state.server_kex()->server_kex_key();
601✔
246

247
         const std::vector<uint8_t> client_pubkey = (ka_key.algo_name() == "DH")
615✔
248
                                                       ? reader.get_range<uint8_t>(2, 0, 65535)
601✔
249
                                                       : reader.get_range<uint8_t>(1, 1, 255);
601✔
250

251
         const auto shared_group = state.server_kex()->shared_group();
601✔
252
         BOTAN_STATE_CHECK(shared_group && shared_group.value() != Group_Params::NONE);
601✔
253

254
         try {
601✔
255
            auto shared_secret =
601✔
256
               state.callbacks().tls_ephemeral_key_agreement(shared_group.value(), ka_key, client_pubkey, rng, policy);
613✔
257

258
            if(ka_key.algo_name() == "DH") {
589✔
259
               shared_secret = CT::strip_leading_zeros(shared_secret);
10✔
260
            }
261

262
            if(kex_algo == Kex_Algo::ECDHE_PSK) {
589✔
263
               append_tls_length_value(m_pre_master, shared_secret, 2);
10✔
264
               append_tls_length_value(m_pre_master, psk.bits_of(), 2);
30✔
265
            } else {
266
               m_pre_master = shared_secret;
579✔
267
            }
268
         } catch(Invalid_Argument& e) {
601✔
269
            throw TLS_Exception(Alert::IllegalParameter, e.what());
×
270
         } catch(TLS_Exception& e) {
12✔
271
            // NOLINTNEXTLINE(cert-err60-cpp)
272
            throw e;
12✔
273
         } catch(std::exception&) {
12✔
274
            /*
275
            * Something failed in the DH/ECDH computation. To avoid possible
276
            * attacks which are based on triggering and detecting some edge
277
            * failure condition, randomize the pre-master output and carry on,
278
            * allowing the protocol to fail later in the finished checks.
279
            */
280
            rng.random_vec(m_pre_master, ka_key.public_value().size());
×
281
         }
×
282

283
         reader.assert_done();
589✔
284
      } else {
601✔
285
         throw Internal_Error("Client_Key_Exchange: Unknown key exchange negotiated");
×
286
      }
287
   }
638✔
288
}
698✔
289

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