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

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 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,
605✔
33
                                         Handshake_State& state,
34
                                         const Policy& policy,
35
                                         Credentials_Manager& creds,
36
                                         RandomNumberGenerator& rng,
37
                                         const Private_Key* signing_key) {
605✔
38
   const std::string hostname = state.client_hello()->sni_hostname();
605✔
39
   const Kex_Algo kex_algo = state.ciphersuite().kex_method();
605✔
40

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

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

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

50
      m_shared_group = Group_Params::NONE;
4✔
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()) {
4✔
58
         m_shared_group = policy.default_dh_group();
×
59
      } else {
60
         m_shared_group = policy.choose_key_exchange_group(dh_groups, {});
4✔
61
      }
62

63
      if(m_shared_group.value() == Group_Params::NONE)
4✔
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");
4✔
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);
4✔
77
      auto dh = dynamic_cast<DH_PrivateKey*>(m_kex_key.get());
4✔
78
      if(!dh) {
4✔
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);
8✔
83
      append_tls_length_value(m_params, BigInt::encode(dh->get_int_field("g")), 2);
8✔
84
      append_tls_length_value(m_params, dh->public_value(), 2);
12✔
85
   } else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
605✔
86
      const std::vector<Group_Params> ec_groups = state.client_hello()->supported_ecc_curves();
568✔
87

88
      if(ec_groups.empty())
568✔
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, {});
568✔
92

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

96
      std::vector<uint8_t> ecdh_public_val;
568✔
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);
499✔
100
         if(!m_kex_key) {
499✔
101
            throw TLS_Exception(Alert::InternalError, "Application did not provide a X25519 key");
×
102
         }
103
         ecdh_public_val = m_kex_key->public_value();
998✔
104
      } else {
105
         m_kex_key = state.callbacks().tls_generate_ephemeral_key(m_shared_group.value(), rng);
69✔
106
         auto ecdh = dynamic_cast<ECDH_PrivateKey*>(m_kex_key.get());
69✔
107
         if(!ecdh) {
69✔
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 =
69✔
113
            ecdh->public_value(state.client_hello()->prefers_compressed_ec_points() ? EC_Point_Format::Compressed
207✔
114
                                                                                    : EC_Point_Format::Uncompressed);
69✔
115
      }
116

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

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

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

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

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

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

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

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

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

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

166
      for(size_t i = 0; i != 3; ++i) {
16✔
167
         reader.get_range<uint8_t>(2, 1, 65535);
24✔
168
      }
169
   } else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
742✔
170
      reader.get_byte();                     // curve type
716✔
171
      reader.get_uint16_t();                 // curve id
716✔
172
      reader.get_range<uint8_t>(1, 1, 255);  // public key
1,432✔
173
   } else if(kex_algo != Kex_Algo::PSK)
26✔
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());
746✔
177

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

183
   reader.assert_done();
746✔
184
}
750✔
185

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

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

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

201
   return buf;
605✔
202
}
×
203

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

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

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

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

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

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

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

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