• 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

91.18
/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.h>
10

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

20
#include <botan/dh.h>
21

22
namespace Botan::TLS {
23

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

36
   if(kex_algo == Kex_Algo::PSK || kex_algo == Kex_Algo::ECDHE_PSK) {
653✔
37
      std::string identity_hint = creds.psk_identity_hint("tls-server", hostname);
47✔
38

39
      append_tls_length_value(m_params, identity_hint, 2);
94✔
40
   }
47✔
41

42
   if(kex_algo == Kex_Algo::DH) {
653✔
43
      const std::vector<Group_Params> dh_groups = state.client_hello()->supported_dh_groups();
5✔
44

45
      m_shared_group = Group_Params::NONE;
5✔
46

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

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

56
      if(dh_groups.empty()) {
5✔
57
         m_shared_group = policy.default_dh_group();
×
58
      } else {
59
         m_shared_group = policy.choose_key_exchange_group(dh_groups, {});
10✔
60
      }
61

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

66
      // The policy had better return a group we know about:
67
      BOTAN_ASSERT(m_shared_group.value().is_dh_named_group(), "DH ciphersuite is using a known finite field group");
5✔
68

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

83
      append_tls_length_value(m_params, dh->get_int_field("p").serialize(), 2);
15✔
84
      append_tls_length_value(m_params, dh->get_int_field("g").serialize(), 2);
15✔
85
      append_tls_length_value(m_params, dh->public_value(), 2);
15✔
86
   } else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
653✔
87
      const std::vector<Group_Params> ec_groups = state.client_hello()->supported_ecc_curves();
611✔
88

89
      if(ec_groups.empty()) {
611✔
90
         throw Internal_Error("Client sent no ECC extension but we negotiated ECDH");
×
91
      }
92

93
      m_shared_group = policy.choose_key_exchange_group(ec_groups, {});
1,222✔
94

95
      if(m_shared_group.value() == Group_Params::NONE) {
611✔
96
         throw TLS_Exception(Alert::HandshakeFailure, "No shared ECC group with client");
×
97
      }
98

99
      m_kex_key = [&] {
1,833✔
100
         if(m_shared_group->is_ecdh_named_curve()) {
611✔
101
            const auto pubkey_point_format = state.client_hello()->prefers_compressed_ec_points()
28✔
102
                                                ? EC_Point_Format::Compressed
28✔
103
                                                : EC_Point_Format::Uncompressed;
28✔
104
            return state.callbacks().tls12_generate_ephemeral_ecdh_key(*m_shared_group, rng, pubkey_point_format);
28✔
105
         } else {
106
            return state.callbacks().tls_generate_ephemeral_key(*m_shared_group, rng);
1,166✔
107
         }
108
      }();
611✔
109

110
      if(!m_kex_key) {
611✔
111
         throw TLS_Exception(Alert::InternalError, "Application did not provide an EC key");
×
112
      }
113

114
      const uint16_t named_curve_id = m_shared_group.value().wire_code();
611✔
115
      m_params.push_back(3);  // named curve
611✔
116
      m_params.push_back(get_byte<0>(named_curve_id));
611✔
117
      m_params.push_back(get_byte<1>(named_curve_id));
611✔
118

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

127
   if(state.ciphersuite().signature_used()) {
653✔
128
      BOTAN_ASSERT(signing_key, "Signing key was set");
606✔
129

130
      std::pair<std::string, Signature_Format> format = state.choose_sig_format(*signing_key, m_scheme, false, policy);
606✔
131

132
      std::vector<uint8_t> buf = state.client_hello()->random();
606✔
133

134
      buf += state.server_hello()->random();
606✔
135
      buf += params();
606✔
136

137
      m_signature = state.callbacks().tls_sign_message(*signing_key, rng, format.first, format.second, buf);
606✔
138
   }
606✔
139

140
   state.hash().update(io.send(*this));
1,957✔
141
}
655✔
142

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

153
   /*
154
   * Here we are deserializing enough to find out what offset the
155
   * signature is at. All processing is done when the Client Key Exchange
156
   * is prepared.
157
   */
158

159
   if(kex_algo == Kex_Algo::PSK || kex_algo == Kex_Algo::ECDHE_PSK) {
1,000✔
160
      reader.get_string(2, 0, 65535);  // identity hint
108✔
161
   }
162

163
   if(kex_algo == Kex_Algo::DH) {
1,000✔
164
      // 3 bigints, DH p, g, Y
165

166
      for(size_t i = 0; i != 3; ++i) {
91✔
167
         reader.get_range<uint8_t>(2, 1, 65535);
137✔
168
      }
169
   } else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
974✔
170
      reader.get_byte();                     // curve type
944✔
171
      reader.get_uint16_t();                 // curve id
943✔
172
      reader.get_range<uint8_t>(1, 1, 255);  // public key
1,883✔
173
   } else if(kex_algo != Kex_Algo::PSK) {
30✔
174
      throw Decoding_Error("Server_Key_Exchange: Unsupported kex type " + kex_method_to_string(kex_algo));
×
175
   }
176

177
   m_params.assign(buf.data(), buf.data() + reader.read_so_far());
990✔
178

179
   if(auth_method != Auth_Method::IMPLICIT) {
990✔
180
      m_scheme = Signature_Scheme(reader.get_uint16_t());
936✔
181
      m_signature = reader.get_range<uint8_t>(2, 0, 65535);
933✔
182
   }
183

184
   reader.assert_done();
982✔
185
}
1,016✔
186

187
/**
188
* Serialize a Server Key Exchange message
189
*/
190
std::vector<uint8_t> Server_Key_Exchange::serialize() const {
653✔
191
   std::vector<uint8_t> buf = params();
653✔
192

193
   if(!m_signature.empty()) {
653✔
194
      if(m_scheme.is_set()) {
606✔
195
         buf.push_back(get_byte<0>(m_scheme.wire_code()));
606✔
196
         buf.push_back(get_byte<1>(m_scheme.wire_code()));
606✔
197
      }
198

199
      append_tls_length_value(buf, m_signature, 2);
606✔
200
   }
201

202
   return buf;
653✔
203
}
×
204

205
/**
206
* Verify a Server Key Exchange message
207
*/
208
bool Server_Key_Exchange::verify(const Public_Key& server_key,
924✔
209
                                 const Handshake_State& state,
210
                                 const Policy& policy) const {
211
   policy.check_peer_key_acceptable(server_key);
924✔
212

213
   std::pair<std::string, Signature_Format> format =
875✔
214
      state.parse_sig_format(server_key, m_scheme, state.client_hello()->signature_schemes(), false, policy);
875✔
215

216
   std::vector<uint8_t> buf = state.client_hello()->random();
866✔
217

218
   buf += state.server_hello()->random();
866✔
219
   buf += params();
866✔
220

221
   const bool signature_valid =
866✔
222
      state.callbacks().tls_verify_message(server_key, format.first, format.second, buf, m_signature);
866✔
223

224
#if defined(BOTAN_UNSAFE_FUZZER_MODE)
225
   BOTAN_UNUSED(signature_valid);
226
   return true;
227
#else
228
   return signature_valid;
866✔
229
#endif
230
}
866✔
231

232
const PK_Key_Agreement_Key& Server_Key_Exchange::server_kex_key() const {
601✔
233
   BOTAN_ASSERT_NONNULL(m_kex_key);
601✔
234
   return *m_kex_key;
601✔
235
}
236

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