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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

90.38
/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/tls_handshake_io.h>
16
#include <botan/internal/tls_handshake_state.h>
17
#include <botan/internal/tls_reader.h>
18

19
#include <botan/dh.h>
20
#include <botan/dl_group.h>
21
#include <botan/ecdh.h>
22

23
#if defined(BOTAN_HAS_CURVE_25519)
24
   #include <botan/curve25519.h>
25
#endif
26

27
namespace Botan::TLS {
28

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

41
   if(kex_algo == Kex_Algo::PSK || kex_algo == Kex_Algo::ECDHE_PSK) {
602✔
42
      std::string identity_hint = creds.psk_identity_hint("tls-server", hostname);
45✔
43

44
      append_tls_length_value(m_params, identity_hint, 2);
90✔
45
   }
45✔
46

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

50
      m_shared_group = Group_Params::NONE;
5✔
51

52
      /*
53
      If the client does not send any DH groups in the supported groups
54
      extension, but does offer DH ciphersuites, we select a group arbitrarily
55
      */
56

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

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

66
      BOTAN_ASSERT(group_param_is_dh(m_shared_group.value()), "DH ciphersuite is using a finite field group");
5✔
67

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

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

88
      if(ec_groups.empty())
563✔
89
         throw Internal_Error("Client sent no ECC extension but we negotiated ECDH");
×
90

91
      m_shared_group = policy.choose_key_exchange_group(ec_groups, {});
563✔
92

93
      if(m_shared_group.value() == Group_Params::NONE)
563✔
94
         throw TLS_Exception(Alert::HandshakeFailure, "No shared ECC group with client");
×
95

96
      std::vector<uint8_t> ecdh_public_val;
563✔
97

98
      if(m_shared_group.value() == Group_Params::X25519) {
99
         m_kex_key = state.callbacks().tls_generate_ephemeral_key(m_shared_group.value(), rng);
495✔
100
         if(!m_kex_key) {
495✔
101
            throw TLS_Exception(Alert::InternalError, "Application did not provide a X25519 key");
×
102
         }
103
         ecdh_public_val = m_kex_key->public_value();
990✔
104
      } else {
105
         m_kex_key = state.callbacks().tls_generate_ephemeral_key(m_shared_group.value(), rng);
68✔
106
         auto ecdh = dynamic_cast<ECDH_PrivateKey*>(m_kex_key.get());
68✔
107
         if(!ecdh) {
68✔
108
            throw TLS_Exception(Alert::InternalError, "Application did not provide a EC-Diffie-Hellman key");
×
109
         }
110

111
         // follow client's preference for point compression
112
         ecdh_public_val =
68✔
113
            ecdh->public_value(state.client_hello()->prefers_compressed_ec_points() ? EC_Point_Format::Compressed
204✔
114
                                                                                    : EC_Point_Format::Uncompressed);
68✔
115
      }
116

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

122
      append_tls_length_value(m_params, ecdh_public_val, 1);
1,126✔
123
   } else if(kex_algo != Kex_Algo::PSK) {
1,160✔
124
      throw Internal_Error("Server_Key_Exchange: Unknown kex type " + kex_method_to_string(kex_algo));
×
125
   }
126

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

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

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

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

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

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

143
/**
144
* Deserialize a Server Key Exchange message
145
*/
146
Server_Key_Exchange::Server_Key_Exchange(const std::vector<uint8_t>& buf,
743✔
147
                                         const Kex_Algo kex_algo,
148
                                         const Auth_Method auth_method,
149
                                         Protocol_Version version) {
743✔
150
   BOTAN_UNUSED(version);  // remove this
743✔
151
   TLS_Data_Reader reader("ServerKeyExchange", buf);
743✔
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) {
743✔
160
      reader.get_string(2, 0, 65535);  // identity hint
104✔
161
   }
162

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

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

176
   m_params.assign(buf.data(), buf.data() + reader.read_so_far());
743✔
177

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

183
   reader.assert_done();
743✔
184
}
747✔
185

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

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

198
      append_tls_length_value(buf, m_signature, 2);
557✔
199
   }
200

201
   return buf;
602✔
202
}
×
203

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

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

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

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

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

223
#if defined(BOTAN_UNSAFE_FUZZER_MODE)
224
   BOTAN_UNUSED(signature_valid);
225
   return true;
226
#else
227
   return signature_valid;
687✔
228
#endif
229
}
1,245✔
230

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

236
}
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