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

randombit / botan / 15656488073

14 Jun 2025 04:56PM UTC coverage: 90.561% (-0.02%) from 90.585%
15656488073

push

github

web-flow
Merge pull request #4912 from randombit/jack/clang-tidy-headers-part-2

Further clang-tidy fixes in header files

98779 of 109074 relevant lines covered (90.56%)

12362398.38 hits per line

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

89.22
/src/lib/tls/tls_extensions.cpp
1
/*
2
* TLS Extensions
3
* (C) 2011,2012,2015,2016 Jack Lloyd
4
*     2016 Juraj Somorovsky
5
*     2021 Elektrobit Automotive GmbH
6
*     2022 René Meusel, Hannes Rantzsch - neXenio GmbH
7
*     2023 Mateusz Berezecki
8
*     2023 Fabian Albert, René Meusel - Rohde & Schwarz Cybersecurity
9
*
10
* Botan is released under the Simplified BSD License (see license.txt)
11
*/
12

13
#include <botan/tls_extensions.h>
14

15
#include <botan/ber_dec.h>
16
#include <botan/der_enc.h>
17
#include <botan/tls_exceptn.h>
18
#include <botan/tls_policy.h>
19
#include <botan/internal/stl_util.h>
20
#include <botan/internal/tls_reader.h>
21

22
#include <algorithm>
23
#include <iterator>
24

25
namespace Botan::TLS {
26

27
namespace {
28

29
std::unique_ptr<Extension> make_extension(TLS_Data_Reader& reader,
39,412✔
30
                                          Extension_Code code,
31
                                          const Connection_Side from,
32
                                          const Handshake_Type message_type) {
33
   // This cast is safe because we read exactly a 16 bit length field for
34
   // the extension in Extensions::deserialize
35
   const uint16_t size = static_cast<uint16_t>(reader.remaining_bytes());
39,412✔
36
   switch(code) {
39,412✔
37
      case Extension_Code::ServerNameIndication:
2,825✔
38
         return std::make_unique<Server_Name_Indicator>(reader, size);
2,825✔
39

40
      case Extension_Code::SupportedGroups:
2,905✔
41
         return std::make_unique<Supported_Groups>(reader, size);
2,905✔
42

43
      case Extension_Code::CertificateStatusRequest:
2,666✔
44
         return std::make_unique<Certificate_Status_Request>(reader, size, message_type, from);
2,666✔
45

46
      case Extension_Code::EcPointFormats:
3,193✔
47
         return std::make_unique<Supported_Point_Formats>(reader, size);
3,193✔
48

49
      case Extension_Code::SafeRenegotiation:
5,684✔
50
         return std::make_unique<Renegotiation_Extension>(reader, size);
5,684✔
51

52
      case Extension_Code::SignatureAlgorithms:
2,930✔
53
         return std::make_unique<Signature_Algorithms>(reader, size);
2,930✔
54

55
      case Extension_Code::CertSignatureAlgorithms:
×
56
         return std::make_unique<Signature_Algorithms_Cert>(reader, size);
×
57

58
      case Extension_Code::UseSrtp:
10✔
59
         return std::make_unique<SRTP_Protection_Profiles>(reader, size);
10✔
60

61
      case Extension_Code::ApplicationLayerProtocolNegotiation:
401✔
62
         return std::make_unique<Application_Layer_Protocol_Notification>(reader, size, from);
401✔
63

64
      case Extension_Code::ClientCertificateType:
2✔
65
         return std::make_unique<Client_Certificate_Type>(reader, size, from);
2✔
66

67
      case Extension_Code::ServerCertificateType:
2✔
68
         return std::make_unique<Server_Certificate_Type>(reader, size, from);
2✔
69

70
      case Extension_Code::ExtendedMasterSecret:
5,508✔
71
         return std::make_unique<Extended_Master_Secret>(reader, size);
5,508✔
72

73
      case Extension_Code::RecordSizeLimit:
45✔
74
         return std::make_unique<Record_Size_Limit>(reader, size, from);
45✔
75

76
      case Extension_Code::EncryptThenMac:
810✔
77
         return std::make_unique<Encrypt_then_MAC>(reader, size);
810✔
78

79
      case Extension_Code::SessionTicket:
4,976✔
80
         return std::make_unique<Session_Ticket_Extension>(reader, size);
4,976✔
81

82
      case Extension_Code::SupportedVersions:
1,822✔
83
         return std::make_unique<Supported_Versions>(reader, size, from);
1,822✔
84

85
#if defined(BOTAN_HAS_TLS_13)
86
      case Extension_Code::PresharedKey:
232✔
87
         return std::make_unique<PSK>(reader, size, message_type);
232✔
88

89
      case Extension_Code::EarlyData:
5✔
90
         return std::make_unique<EarlyDataIndication>(reader, size, message_type);
5✔
91

92
      case Extension_Code::Cookie:
10✔
93
         return std::make_unique<Cookie>(reader, size);
10✔
94

95
      case Extension_Code::PskKeyExchangeModes:
1,072✔
96
         return std::make_unique<PSK_Key_Exchange_Modes>(reader, size);
1,072✔
97

98
      case Extension_Code::CertificateAuthorities:
3✔
99
         return std::make_unique<Certificate_Authorities>(reader, size);
3✔
100

101
      case Extension_Code::KeyShare:
1,609✔
102
         return std::make_unique<Key_Share>(reader, size, message_type);
1,609✔
103
#endif
104
   }
105

106
   return std::make_unique<Unknown_Extension>(code, reader, size);
2,702✔
107
}
108

109
}  // namespace
110

111
Extensions::~Extensions() = default;
22,751✔
112

113
Extension* Extensions::get(Extension_Code type) const {
219,833✔
114
   const auto i =
219,833✔
115
      std::find_if(m_extensions.cbegin(), m_extensions.cend(), [type](const auto& ext) { return ext->type() == type; });
1,128,926✔
116

117
   return (i != m_extensions.end()) ? i->get() : nullptr;
219,833✔
118
}
119

120
void Extensions::add(std::unique_ptr<Extension> extn) {
84,821✔
121
   if(has(extn->type())) {
84,821✔
122
      throw Invalid_Argument("cannot add the same extension twice: " +
×
123
                             std::to_string(static_cast<uint16_t>(extn->type())));
×
124
   }
125

126
   m_extensions.emplace_back(extn.release());
84,821✔
127
}
84,821✔
128

129
void Extensions::deserialize(TLS_Data_Reader& reader, const Connection_Side from, const Handshake_Type message_type) {
7,966✔
130
   if(reader.has_remaining()) {
7,966✔
131
      const uint16_t all_extn_size = reader.get_uint16_t();
7,961✔
132

133
      if(reader.remaining_bytes() != all_extn_size) {
7,961✔
134
         throw Decoding_Error("Bad extension size");
118✔
135
      }
136

137
      while(reader.has_remaining()) {
47,222✔
138
         const uint16_t extension_code = reader.get_uint16_t();
39,434✔
139
         const uint16_t extension_size = reader.get_uint16_t();
39,433✔
140

141
         const auto type = static_cast<Extension_Code>(extension_code);
39,433✔
142

143
         if(this->has(type)) {
39,433✔
144
            throw TLS_Exception(TLS::Alert::DecodeError, "Peer sent duplicated extensions");
13✔
145
         }
146

147
         // TODO offer a function on reader that returns a byte range as a reference
148
         // to avoid this copy of the extension data
149
         const std::vector<uint8_t> extn_data = reader.get_fixed<uint8_t>(extension_size);
39,420✔
150
         TLS_Data_Reader extn_reader("Extension", extn_data);
39,412✔
151
         this->add(make_extension(extn_reader, type, from, message_type));
39,412✔
152
         extn_reader.assert_done();
39,379✔
153
      }
39,379✔
154
   }
155
}
7,793✔
156

157
bool Extensions::contains_other_than(const std::set<Extension_Code>& allowed_extensions,
3,437✔
158
                                     const bool allow_unknown_extensions) const {
159
   const auto found = extension_types();
3,437✔
160

161
   std::vector<Extension_Code> diff;
3,437✔
162
   std::set_difference(
3,437✔
163
      found.cbegin(), found.end(), allowed_extensions.cbegin(), allowed_extensions.cend(), std::back_inserter(diff));
164

165
   if(allow_unknown_extensions) {
3,437✔
166
      // Go through the found unexpected extensions whether any of those
167
      // is known to this TLS implementation.
168
      const auto itr = std::find_if(diff.cbegin(), diff.cend(), [this](const auto ext_type) {
1,523✔
169
         const auto ext = get(ext_type);
10✔
170
         return ext && ext->is_implemented();
10✔
171
      });
172

173
      // ... if yes, `contains_other_than` is true
174
      return itr != diff.cend();
1,523✔
175
   }
176

177
   return !diff.empty();
1,914✔
178
}
3,437✔
179

180
std::unique_ptr<Extension> Extensions::take(Extension_Code type) {
514✔
181
   const auto i =
514✔
182
      std::find_if(m_extensions.begin(), m_extensions.end(), [type](const auto& ext) { return ext->type() == type; });
3,137✔
183

184
   std::unique_ptr<Extension> result;
514✔
185
   if(i != m_extensions.end()) {
514✔
186
      std::swap(result, *i);
248✔
187
      m_extensions.erase(i);
248✔
188
   }
189

190
   return result;
514✔
191
}
192

193
std::vector<uint8_t> Extensions::serialize(Connection_Side whoami) const {
6,534✔
194
   std::vector<uint8_t> buf(2);  // 2 bytes for length field
6,534✔
195

196
   for(const auto& extn : m_extensions) {
58,975✔
197
      if(extn->empty()) {
52,441✔
198
         continue;
3,120✔
199
      }
200

201
      const uint16_t extn_code = static_cast<uint16_t>(extn->type());
49,321✔
202

203
      const std::vector<uint8_t> extn_val = extn->serialize(whoami);
49,321✔
204

205
      buf.push_back(get_byte<0>(extn_code));
49,321✔
206
      buf.push_back(get_byte<1>(extn_code));
49,321✔
207

208
      buf.push_back(get_byte<0>(static_cast<uint16_t>(extn_val.size())));
49,321✔
209
      buf.push_back(get_byte<1>(static_cast<uint16_t>(extn_val.size())));
49,321✔
210

211
      buf += extn_val;
49,321✔
212
   }
49,321✔
213

214
   const uint16_t extn_size = static_cast<uint16_t>(buf.size() - 2);
6,534✔
215

216
   buf[0] = get_byte<0>(extn_size);
6,534✔
217
   buf[1] = get_byte<1>(extn_size);
6,534✔
218

219
   // avoid sending a completely empty extensions block
220
   if(buf.size() == 2) {
6,534✔
221
      return std::vector<uint8_t>();
304✔
222
   }
223

224
   return buf;
6,230✔
225
}
6,534✔
226

227
std::set<Extension_Code> Extensions::extension_types() const {
10,348✔
228
   std::set<Extension_Code> offers;
10,348✔
229
   std::transform(
10,348✔
230
      m_extensions.cbegin(), m_extensions.cend(), std::inserter(offers, offers.begin()), [](const auto& ext) {
58,546✔
231
         return ext->type();
58,546✔
232
      });
233
   return offers;
10,348✔
234
}
×
235

236
Unknown_Extension::Unknown_Extension(Extension_Code type, TLS_Data_Reader& reader, uint16_t extension_size) :
2,702✔
237
      m_type(type), m_value(reader.get_fixed<uint8_t>(extension_size)) {}
2,702✔
238

239
std::vector<uint8_t> Unknown_Extension::serialize(Connection_Side /*whoami*/) const {
2✔
240
   return m_value;
2✔
241
}
242

243
Server_Name_Indicator::Server_Name_Indicator(TLS_Data_Reader& reader, uint16_t extension_size) {
2,825✔
244
   /*
245
   * This is used by the server to confirm that it knew the name
246
   */
247
   if(extension_size == 0) {
2,825✔
248
      return;
249
   }
250

251
   uint16_t name_bytes = reader.get_uint16_t();
2,790✔
252

253
   if(name_bytes + 2 != extension_size) {
2,790✔
254
      throw Decoding_Error("Bad encoding of SNI extension");
×
255
   }
256

257
   while(name_bytes) {
5,580✔
258
      uint8_t name_type = reader.get_byte();
2,790✔
259
      name_bytes--;
2,790✔
260

261
      if(name_type == 0) {
2,790✔
262
         // DNS
263
         m_sni_host_name = reader.get_string(2, 1, 65535);
2,790✔
264
         name_bytes -= static_cast<uint16_t>(2 + m_sni_host_name.size());
2,790✔
265
      } else {
266
         // some other unknown name type, which we will ignore
267
         reader.discard_next(name_bytes);
×
268
         name_bytes = 0;
×
269
      }
270
   }
271
}
×
272

273
std::vector<uint8_t> Server_Name_Indicator::serialize(Connection_Side whoami) const {
4,653✔
274
   // RFC 6066
275
   //    [...] the server SHALL include an extension of type "server_name" in
276
   //    the (extended) server hello. The "extension_data" field of this
277
   //    extension SHALL be empty.
278
   if(whoami == Connection_Side::Server) {
4,653✔
279
      return {};
366✔
280
   }
281

282
   std::vector<uint8_t> buf;
4,287✔
283

284
   size_t name_len = m_sni_host_name.size();
4,287✔
285

286
   buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len + 3)));
4,287✔
287
   buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len + 3)));
4,287✔
288
   buf.push_back(0);  // DNS
4,287✔
289

290
   buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len)));
4,287✔
291
   buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len)));
4,287✔
292

293
   buf += std::make_pair(cast_char_ptr_to_uint8(m_sni_host_name.data()), m_sni_host_name.size());
4,287✔
294

295
   return buf;
4,287✔
296
}
4,287✔
297

298
Renegotiation_Extension::Renegotiation_Extension(TLS_Data_Reader& reader, uint16_t extension_size) :
5,684✔
299
      m_reneg_data(reader.get_range<uint8_t>(1, 0, 255)) {
5,684✔
300
   if(m_reneg_data.size() + 1 != extension_size) {
5,682✔
301
      throw Decoding_Error("Bad encoding for secure renegotiation extn");
1✔
302
   }
303
}
5,682✔
304

305
std::vector<uint8_t> Renegotiation_Extension::serialize(Connection_Side /*whoami*/) const {
5,262✔
306
   std::vector<uint8_t> buf;
5,262✔
307
   append_tls_length_value(buf, m_reneg_data, 1);
5,262✔
308
   return buf;
5,262✔
309
}
×
310

311
Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification(TLS_Data_Reader& reader,
401✔
312
                                                                                 uint16_t extension_size,
313
                                                                                 Connection_Side from) {
401✔
314
   if(extension_size == 0) {
401✔
315
      return;  // empty extension
316
   }
317

318
   const uint16_t name_bytes = reader.get_uint16_t();
401✔
319

320
   size_t bytes_remaining = extension_size - 2;
401✔
321

322
   if(name_bytes != bytes_remaining) {
401✔
323
      throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
×
324
   }
325

326
   while(bytes_remaining) {
1,070✔
327
      const std::string p = reader.get_string(1, 0, 255);
676✔
328

329
      if(bytes_remaining < p.size() + 1) {
676✔
330
         throw Decoding_Error("Bad encoding of ALPN, length field too long");
×
331
      }
332

333
      if(p.empty()) {
676✔
334
         throw Decoding_Error("Empty ALPN protocol not allowed");
7✔
335
      }
336

337
      bytes_remaining -= (p.size() + 1);
669✔
338

339
      m_protocols.push_back(p);
669✔
340
   }
676✔
341

342
   // RFC 7301 3.1
343
   //    The "extension_data" field of the [...] extension is structured the
344
   //    same as described above for the client "extension_data", except that
345
   //    the "ProtocolNameList" MUST contain exactly one "ProtocolName".
346
   if(from == Connection_Side::Server && m_protocols.size() != 1) {
394✔
347
      throw TLS_Exception(
×
348
         Alert::DecodeError,
349
         "Server sent " + std::to_string(m_protocols.size()) + " protocols in ALPN extension response");
×
350
   }
351
}
7✔
352

353
std::string Application_Layer_Protocol_Notification::single_protocol() const {
145✔
354
   BOTAN_STATE_CHECK(m_protocols.size() == 1);
145✔
355
   return m_protocols.front();
145✔
356
}
357

358
std::vector<uint8_t> Application_Layer_Protocol_Notification::serialize(Connection_Side /*whoami*/) const {
359✔
359
   std::vector<uint8_t> buf(2);
359✔
360

361
   for(auto&& p : m_protocols) {
943✔
362
      if(p.length() >= 256) {
584✔
363
         throw TLS_Exception(Alert::InternalError, "ALPN name too long");
×
364
      }
365
      if(!p.empty()) {
584✔
366
         append_tls_length_value(buf, cast_char_ptr_to_uint8(p.data()), p.size(), 1);
584✔
367
      }
368
   }
369

370
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
359✔
371
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
359✔
372

373
   return buf;
359✔
374
}
×
375

376
std::string certificate_type_to_string(Certificate_Type type) {
24✔
377
   switch(type) {
24✔
378
      case Certificate_Type::X509:
24✔
379
         return "X509";
24✔
380
      case Certificate_Type::RawPublicKey:
×
381
         return "RawPublicKey";
×
382
   }
383

384
   return "Unknown";
×
385
}
386

387
Certificate_Type certificate_type_from_string(const std::string& type_str) {
10✔
388
   if(type_str == "X509") {
10✔
389
      return Certificate_Type::X509;
390
   } else if(type_str == "RawPublicKey") {
4✔
391
      return Certificate_Type::RawPublicKey;
392
   } else {
393
      throw Decoding_Error("Unknown certificate type: " + type_str);
×
394
   }
395
}
396

397
Certificate_Type_Base::Certificate_Type_Base(std::vector<Certificate_Type> supported_cert_types) :
1,976✔
398
      m_certificate_types(std::move(supported_cert_types)), m_from(Connection_Side::Client) {
1,976✔
399
   BOTAN_ARG_CHECK(!m_certificate_types.empty(), "at least one certificate type must be supported");
1,976✔
400
}
1,976✔
401

402
Client_Certificate_Type::Client_Certificate_Type(const Client_Certificate_Type& cct, const Policy& policy) :
1✔
403
      Certificate_Type_Base(cct, policy.accepted_client_certificate_types()) {}
1✔
404

405
Server_Certificate_Type::Server_Certificate_Type(const Server_Certificate_Type& sct, const Policy& policy) :
1✔
406
      Certificate_Type_Base(sct, policy.accepted_server_certificate_types()) {}
1✔
407

408
Certificate_Type_Base::Certificate_Type_Base(const Certificate_Type_Base& certificate_type_from_client,
2✔
409
                                             const std::vector<Certificate_Type>& server_preference) :
2✔
410
      m_from(Connection_Side::Server) {
2✔
411
   // RFC 7250 4.2
412
   //    The server_certificate_type extension in the client hello indicates the
413
   //    types of certificates the client is able to process when provided by
414
   //    the server in a subsequent certificate payload. [...] With the
415
   //    server_certificate_type extension in the server hello, the TLS server
416
   //    indicates the certificate type carried in the Certificate payload.
417
   for(const auto server_supported_cert_type : server_preference) {
2✔
418
      if(value_exists(certificate_type_from_client.m_certificate_types, server_supported_cert_type)) {
4✔
419
         m_certificate_types.push_back(server_supported_cert_type);
2✔
420
         return;
2✔
421
      }
422
   }
423

424
   // RFC 7250 4.2 (2.)
425
   //    The server supports the extension defined in this document, but
426
   //    it does not have any certificate type in common with the client.
427
   //    Then, the server terminates the session with a fatal alert of
428
   //    type "unsupported_certificate".
429
   throw TLS_Exception(Alert::UnsupportedCertificate, "Failed to agree on certificate_type");
×
430
}
×
431

432
Certificate_Type_Base::Certificate_Type_Base(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) :
4✔
433
      m_from(from) {
4✔
434
   if(extension_size == 0) {
4✔
435
      throw Decoding_Error("Certificate type extension cannot be empty");
×
436
   }
437

438
   if(from == Connection_Side::Client) {
4✔
439
      const auto type_bytes = reader.get_tls_length_value(1);
2✔
440
      if(static_cast<size_t>(extension_size) != type_bytes.size() + 1) {
2✔
441
         throw Decoding_Error("certificate type extension had inconsistent length");
×
442
      }
443
      std::transform(
2✔
444
         type_bytes.begin(), type_bytes.end(), std::back_inserter(m_certificate_types), [](const auto type_byte) {
2✔
445
            return static_cast<Certificate_Type>(type_byte);
446
         });
447
   } else {
2✔
448
      // RFC 7250 4.2
449
      //    Note that only a single value is permitted in the
450
      //    server_certificate_type extension when carried in the server hello.
451
      if(extension_size != 1) {
2✔
452
         throw Decoding_Error("Server's certificate type extension must be of length 1");
×
453
      }
454
      const auto type_byte = reader.get_byte();
2✔
455
      m_certificate_types.push_back(static_cast<Certificate_Type>(type_byte));
2✔
456
   }
457
}
4✔
458

459
std::vector<uint8_t> Certificate_Type_Base::serialize(Connection_Side whoami) const {
12✔
460
   std::vector<uint8_t> result;
12✔
461
   if(whoami == Connection_Side::Client) {
12✔
462
      std::vector<uint8_t> type_bytes;
6✔
463
      std::transform(
6✔
464
         m_certificate_types.begin(), m_certificate_types.end(), std::back_inserter(type_bytes), [](const auto type) {
465
            return static_cast<uint8_t>(type);
466
         });
467
      append_tls_length_value(result, type_bytes, 1);
12✔
468
   } else {
6✔
469
      BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
6✔
470
      result.push_back(static_cast<uint8_t>(m_certificate_types.front()));
6✔
471
   }
472
   return result;
12✔
473
}
×
474

475
void Certificate_Type_Base::validate_selection(const Certificate_Type_Base& from_server) const {
2✔
476
   BOTAN_ASSERT_NOMSG(m_from == Connection_Side::Client);
2✔
477
   BOTAN_ASSERT_NOMSG(from_server.m_from == Connection_Side::Server);
2✔
478

479
   // RFC 7250 4.2
480
   //    The value conveyed in the [client_]certificate_type extension MUST be
481
   //    selected from one of the values provided in the [client_]certificate_type
482
   //    extension sent in the client hello.
483
   if(!value_exists(m_certificate_types, from_server.selected_certificate_type())) {
4✔
484
      throw TLS_Exception(Alert::IllegalParameter,
×
485
                          Botan::fmt("Selected certificate type was not offered: {}",
×
486
                                     certificate_type_to_string(from_server.selected_certificate_type())));
×
487
   }
488
}
2✔
489

490
Certificate_Type Certificate_Type_Base::selected_certificate_type() const {
6✔
491
   BOTAN_ASSERT_NOMSG(m_from == Connection_Side::Server);
6✔
492
   BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
6✔
493
   return m_certificate_types.front();
6✔
494
}
495

496
Supported_Groups::Supported_Groups(const std::vector<Group_Params>& groups) : m_groups(groups) {}
4,044✔
497

498
const std::vector<Group_Params>& Supported_Groups::groups() const {
948✔
499
   return m_groups;
948✔
500
}
501

502
std::vector<Group_Params> Supported_Groups::ec_groups() const {
5,015✔
503
   std::vector<Group_Params> ec;
5,015✔
504
   for(auto g : m_groups) {
52,082✔
505
      if(g.is_pure_ecc_group()) {
94,134✔
506
         ec.push_back(g);
34,680✔
507
      }
508
   }
509
   return ec;
5,015✔
510
}
×
511

512
std::vector<Group_Params> Supported_Groups::dh_groups() const {
1,505✔
513
   std::vector<Group_Params> dh;
1,505✔
514
   for(auto g : m_groups) {
11,104✔
515
      if(g.is_in_ffdhe_range()) {
12,441✔
516
         dh.push_back(g);
550✔
517
      }
518
   }
519
   return dh;
1,505✔
520
}
×
521

522
std::vector<uint8_t> Supported_Groups::serialize(Connection_Side /*whoami*/) const {
4,721✔
523
   std::vector<uint8_t> buf(2);
4,721✔
524

525
   for(auto g : m_groups) {
54,917✔
526
      const uint16_t id = g.wire_code();
50,196✔
527

528
      if(id > 0) {
50,196✔
529
         buf.push_back(get_byte<0>(id));
50,196✔
530
         buf.push_back(get_byte<1>(id));
50,196✔
531
      }
532
   }
533

534
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
4,721✔
535
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
4,721✔
536

537
   return buf;
4,721✔
538
}
×
539

540
Supported_Groups::Supported_Groups(TLS_Data_Reader& reader, uint16_t extension_size) {
2,907✔
541
   const uint16_t len = reader.get_uint16_t();
2,907✔
542

543
   if(len + 2 != extension_size) {
2,907✔
544
      throw Decoding_Error("Inconsistent length field in supported groups list");
4✔
545
   }
546

547
   if(len % 2 == 1) {
2,903✔
548
      throw Decoding_Error("Supported groups list of strange size");
×
549
   }
550

551
   const size_t elems = len / 2;
2,903✔
552

553
   for(size_t i = 0; i != elems; ++i) {
23,894✔
554
      const auto group = static_cast<Group_Params>(reader.get_uint16_t());
20,991✔
555
      // Note: RFC 8446 does not explicitly enforce that groups must be unique.
556
      if(!value_exists(m_groups, group)) {
41,982✔
557
         m_groups.push_back(group);
20,991✔
558
      }
559
   }
560
}
2,907✔
561

562
std::vector<uint8_t> Supported_Point_Formats::serialize(Connection_Side /*whoami*/) const {
5,106✔
563
   // if this extension is sent, it MUST include uncompressed (RFC 4492, section 5.1)
564
   if(m_prefers_compressed) {
5,106✔
565
      return std::vector<uint8_t>{2, ANSIX962_COMPRESSED_PRIME, UNCOMPRESSED};
10✔
566
   } else {
567
      return std::vector<uint8_t>{1, UNCOMPRESSED};
5,096✔
568
   }
569
}
570

571
Supported_Point_Formats::Supported_Point_Formats(TLS_Data_Reader& reader, uint16_t extension_size) {
3,193✔
572
   uint8_t len = reader.get_byte();
3,193✔
573

574
   if(len + 1 != extension_size) {
3,193✔
575
      throw Decoding_Error("Inconsistent length field in supported point formats list");
2✔
576
   }
577

578
   bool includes_uncompressed = false;
3,196✔
579
   for(size_t i = 0; i != len; ++i) {
3,196✔
580
      uint8_t format = reader.get_byte();
3,195✔
581

582
      if(static_cast<ECPointFormat>(format) == UNCOMPRESSED) {
3,195✔
583
         m_prefers_compressed = false;
3,180✔
584
         reader.discard_next(len - i - 1);
3,180✔
585
         return;
3,180✔
586
      } else if(static_cast<ECPointFormat>(format) == ANSIX962_COMPRESSED_PRIME) {
15✔
587
         m_prefers_compressed = true;
10✔
588
         std::vector<uint8_t> remaining_formats = reader.get_fixed<uint8_t>(len - i - 1);
10✔
589
         includes_uncompressed =
10✔
590
            std::any_of(std::begin(remaining_formats), std::end(remaining_formats), [](uint8_t remaining_format) {
10✔
591
               return static_cast<ECPointFormat>(remaining_format) == UNCOMPRESSED;
592
            });
593
         break;
10✔
594
      }
10✔
595

596
      // ignore ANSIX962_COMPRESSED_CHAR2, we don't support these curves
597
   }
598

599
   // RFC 4492 5.1.:
600
   //   If the Supported Point Formats Extension is indeed sent, it MUST contain the value 0 (uncompressed)
601
   //   as one of the items in the list of point formats.
602
   // Note:
603
   //   RFC 8422 5.1.2. explicitly requires this check,
604
   //   but only if the Supported Groups extension was sent.
605
   if(!includes_uncompressed) {
11✔
606
      throw TLS_Exception(Alert::IllegalParameter,
1✔
607
                          "Supported Point Formats Extension must contain the uncompressed point format");
1✔
608
   }
609
}
610

611
namespace {
612

613
std::vector<uint8_t> serialize_signature_algorithms(const std::vector<Signature_Scheme>& schemes) {
4,723✔
614
   BOTAN_ASSERT(schemes.size() < 256, "Too many signature schemes");
4,723✔
615

616
   std::vector<uint8_t> buf;
4,723✔
617

618
   const uint16_t len = static_cast<uint16_t>(schemes.size() * 2);
4,723✔
619

620
   buf.push_back(get_byte<0>(len));
4,723✔
621
   buf.push_back(get_byte<1>(len));
4,723✔
622

623
   for(Signature_Scheme scheme : schemes) {
46,267✔
624
      buf.push_back(get_byte<0>(scheme.wire_code()));
41,544✔
625
      buf.push_back(get_byte<1>(scheme.wire_code()));
41,544✔
626
   }
627

628
   return buf;
4,723✔
629
}
×
630

631
std::vector<Signature_Scheme> parse_signature_algorithms(TLS_Data_Reader& reader, uint16_t extension_size) {
2,932✔
632
   uint16_t len = reader.get_uint16_t();
2,932✔
633

634
   if(len + 2 != extension_size || len % 2 == 1 || len == 0) {
2,931✔
635
      throw Decoding_Error("Bad encoding on signature algorithms extension");
1✔
636
   }
637

638
   std::vector<Signature_Scheme> schemes;
2,930✔
639
   schemes.reserve(len / 2);
2,930✔
640
   while(len) {
30,738✔
641
      schemes.emplace_back(reader.get_uint16_t());
27,808✔
642
      len -= 2;
27,808✔
643
   }
644

645
   return schemes;
2,930✔
646
}
×
647

648
}  // namespace
649

650
std::vector<uint8_t> Signature_Algorithms::serialize(Connection_Side /*whoami*/) const {
4,721✔
651
   return serialize_signature_algorithms(m_schemes);
4,721✔
652
}
653

654
Signature_Algorithms::Signature_Algorithms(TLS_Data_Reader& reader, uint16_t extension_size) :
2,930✔
655
      m_schemes(parse_signature_algorithms(reader, extension_size)) {}
2,930✔
656

657
std::vector<uint8_t> Signature_Algorithms_Cert::serialize(Connection_Side /*whoami*/) const {
2✔
658
   return serialize_signature_algorithms(m_schemes);
2✔
659
}
660

661
Signature_Algorithms_Cert::Signature_Algorithms_Cert(TLS_Data_Reader& reader, uint16_t extension_size) :
2✔
662
      m_schemes(parse_signature_algorithms(reader, extension_size)) {}
2✔
663

664
Session_Ticket_Extension::Session_Ticket_Extension(TLS_Data_Reader& reader, uint16_t extension_size) :
4,976✔
665
      m_ticket(Session_Ticket(reader.get_elem<uint8_t, std::vector<uint8_t>>(extension_size))) {}
4,976✔
666

667
SRTP_Protection_Profiles::SRTP_Protection_Profiles(TLS_Data_Reader& reader, uint16_t extension_size) :
10✔
668
      m_pp(reader.get_range<uint16_t>(2, 0, 65535)) {
10✔
669
   const std::vector<uint8_t> mki = reader.get_range<uint8_t>(1, 0, 255);
9✔
670

671
   if(m_pp.size() * 2 + mki.size() + 3 != extension_size) {
9✔
672
      throw Decoding_Error("Bad encoding for SRTP protection extension");
×
673
   }
674

675
   if(!mki.empty()) {
9✔
676
      throw Decoding_Error("Unhandled non-empty MKI for SRTP protection extension");
×
677
   }
678
}
9✔
679

680
std::vector<uint8_t> SRTP_Protection_Profiles::serialize(Connection_Side /*whoami*/) const {
5✔
681
   std::vector<uint8_t> buf;
5✔
682

683
   const uint16_t pp_len = static_cast<uint16_t>(m_pp.size() * 2);
5✔
684
   buf.push_back(get_byte<0>(pp_len));
5✔
685
   buf.push_back(get_byte<1>(pp_len));
5✔
686

687
   for(uint16_t pp : m_pp) {
12✔
688
      buf.push_back(get_byte<0>(pp));
7✔
689
      buf.push_back(get_byte<1>(pp));
7✔
690
   }
691

692
   buf.push_back(0);  // srtp_mki, always empty here
5✔
693

694
   return buf;
5✔
695
}
×
696

697
Extended_Master_Secret::Extended_Master_Secret(TLS_Data_Reader& /*unused*/, uint16_t extension_size) {
5,508✔
698
   if(extension_size != 0) {
5,508✔
699
      throw Decoding_Error("Invalid extended_master_secret extension");
2✔
700
   }
701
}
5,506✔
702

703
std::vector<uint8_t> Extended_Master_Secret::serialize(Connection_Side /*whoami*/) const {
5,236✔
704
   return std::vector<uint8_t>();
5,236✔
705
}
706

707
Encrypt_then_MAC::Encrypt_then_MAC(TLS_Data_Reader& /*unused*/, uint16_t extension_size) {
810✔
708
   if(extension_size != 0) {
810✔
709
      throw Decoding_Error("Invalid encrypt_then_mac extension");
×
710
   }
711
}
810✔
712

713
std::vector<uint8_t> Encrypt_then_MAC::serialize(Connection_Side /*whoami*/) const {
3,979✔
714
   return std::vector<uint8_t>();
3,979✔
715
}
716

717
std::vector<uint8_t> Supported_Versions::serialize(Connection_Side whoami) const {
4,479✔
718
   std::vector<uint8_t> buf;
4,479✔
719

720
   if(whoami == Connection_Side::Server) {
4,479✔
721
      BOTAN_ASSERT_NOMSG(m_versions.size() == 1);
429✔
722
      buf.push_back(m_versions[0].major_version());
429✔
723
      buf.push_back(m_versions[0].minor_version());
429✔
724
   } else {
725
      BOTAN_ASSERT_NOMSG(!m_versions.empty());
4,050✔
726
      const uint8_t len = static_cast<uint8_t>(m_versions.size() * 2);
4,050✔
727

728
      buf.push_back(len);
4,050✔
729

730
      for(Protocol_Version version : m_versions) {
9,218✔
731
         buf.push_back(version.major_version());
5,168✔
732
         buf.push_back(version.minor_version());
5,168✔
733
      }
734
   }
735

736
   return buf;
4,479✔
737
}
×
738

739
Supported_Versions::Supported_Versions(Protocol_Version offer, const Policy& policy) {
3,469✔
740
   if(offer.is_datagram_protocol()) {
3,469✔
741
#if defined(BOTAN_HAS_TLS_12)
742
      if(offer >= Protocol_Version::DTLS_V12 && policy.allow_dtls12()) {
400✔
743
         m_versions.push_back(Protocol_Version::DTLS_V12);
400✔
744
      }
745
#endif
746
   } else {
747
#if defined(BOTAN_HAS_TLS_13)
748
      if(offer >= Protocol_Version::TLS_V13 && policy.allow_tls13()) {
5,150✔
749
         m_versions.push_back(Protocol_Version::TLS_V13);
988✔
750
      }
751
#endif
752
#if defined(BOTAN_HAS_TLS_12)
753
      if(offer >= Protocol_Version::TLS_V12 && policy.allow_tls12()) {
4,057✔
754
         m_versions.push_back(Protocol_Version::TLS_V12);
3,040✔
755
      }
756
#endif
757
   }
758
}
3,469✔
759

760
Supported_Versions::Supported_Versions(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
1,824✔
761
   if(from == Connection_Side::Server) {
1,824✔
762
      if(extension_size != 2) {
541✔
763
         throw Decoding_Error("Server sent invalid supported_versions extension");
×
764
      }
765
      m_versions.push_back(Protocol_Version(reader.get_uint16_t()));
541✔
766
   } else {
767
      auto versions = reader.get_range<uint16_t>(1, 1, 127);
1,283✔
768

769
      for(auto v : versions) {
4,555✔
770
         m_versions.push_back(Protocol_Version(v));
3,272✔
771
      }
772

773
      if(extension_size != 1 + 2 * versions.size()) {
1,283✔
774
         throw Decoding_Error("Client sent invalid supported_versions extension");
×
775
      }
776
   }
1,283✔
777
}
1,824✔
778

779
bool Supported_Versions::supports(Protocol_Version version) const {
1,485✔
780
   for(auto v : m_versions) {
1,996✔
781
      if(version == v) {
1,977✔
782
         return true;
1,485✔
783
      }
784
   }
785
   return false;
786
}
787

788
Record_Size_Limit::Record_Size_Limit(const uint16_t limit) : m_limit(limit) {
16✔
789
   BOTAN_ASSERT(limit >= 64, "RFC 8449 does not allow record size limits smaller than 64 bytes");
16✔
790
   BOTAN_ASSERT(limit <= MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */,
16✔
791
                "RFC 8449 does not allow record size limits larger than 2^14+1");
792
}
16✔
793

794
Record_Size_Limit::Record_Size_Limit(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
45✔
795
   if(extension_size != 2) {
45✔
796
      throw TLS_Exception(Alert::DecodeError, "invalid record_size_limit extension");
×
797
   }
798

799
   m_limit = reader.get_uint16_t();
45✔
800

801
   // RFC 8449 4.
802
   //    This value is the length of the plaintext of a protected record.
803
   //    The value includes the content type and padding added in TLS 1.3 (that
804
   //    is, the complete length of TLSInnerPlaintext).
805
   //
806
   //    A server MUST NOT enforce this restriction; a client might advertise
807
   //    a higher limit that is enabled by an extension or version the server
808
   //    does not understand. A client MAY abort the handshake with an
809
   //    "illegal_parameter" alert.
810
   //
811
   // Note: We are currently supporting this extension in TLS 1.3 only, hence
812
   //       we check for the TLS 1.3 limit. The TLS 1.2 limit would not include
813
   //       the "content type byte" and hence be one byte less!
814
   if(m_limit > MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */ && from == Connection_Side::Server) {
45✔
815
      throw TLS_Exception(Alert::IllegalParameter,
×
816
                          "Server requested a record size limit larger than the protocol's maximum");
×
817
   }
818

819
   // RFC 8449 4.
820
   //    Endpoints MUST NOT send a "record_size_limit" extension with a value
821
   //    smaller than 64.  An endpoint MUST treat receipt of a smaller value
822
   //    as a fatal error and generate an "illegal_parameter" alert.
823
   if(m_limit < 64) {
45✔
824
      throw TLS_Exception(Alert::IllegalParameter, "Received a record size limit smaller than 64 bytes");
×
825
   }
826
}
45✔
827

828
std::vector<uint8_t> Record_Size_Limit::serialize(Connection_Side) const {
53✔
829
   std::vector<uint8_t> buf;
53✔
830

831
   buf.push_back(get_byte<0>(m_limit));
53✔
832
   buf.push_back(get_byte<1>(m_limit));
53✔
833

834
   return buf;
53✔
835
}
×
836

837
#if defined(BOTAN_HAS_TLS_13)
838
Cookie::Cookie(const std::vector<uint8_t>& cookie) : m_cookie(cookie) {}
4✔
839

840
Cookie::Cookie(TLS_Data_Reader& reader, uint16_t extension_size) {
12✔
841
   if(extension_size == 0) {
12✔
842
      return;
843
   }
844

845
   const uint16_t len = reader.get_uint16_t();
12✔
846

847
   if(len == 0) {
12✔
848
      // Based on RFC 8446 4.2.2, len of the Cookie buffer must be at least 1
849
      throw Decoding_Error("Cookie length must be at least 1 byte");
1✔
850
   }
851

852
   if(len > reader.remaining_bytes()) {
11✔
853
      throw Decoding_Error("Not enough bytes in the buffer to decode Cookie");
×
854
   }
855

856
   for(size_t i = 0; i < len; ++i) {
719✔
857
      m_cookie.push_back(reader.get_byte());
708✔
858
   }
859
}
1✔
860

861
std::vector<uint8_t> Cookie::serialize(Connection_Side /*whoami*/) const {
10✔
862
   std::vector<uint8_t> buf;
10✔
863

864
   const uint16_t len = static_cast<uint16_t>(m_cookie.size());
10✔
865

866
   buf.push_back(get_byte<0>(len));
10✔
867
   buf.push_back(get_byte<1>(len));
10✔
868

869
   for(const auto& cookie_byte : m_cookie) {
712✔
870
      buf.push_back(cookie_byte);
702✔
871
   }
872

873
   return buf;
10✔
874
}
×
875

876
std::vector<uint8_t> PSK_Key_Exchange_Modes::serialize(Connection_Side) const {
1,184✔
877
   std::vector<uint8_t> buf;
1,184✔
878

879
   BOTAN_ASSERT_NOMSG(m_modes.size() < 256);
1,184✔
880
   buf.push_back(static_cast<uint8_t>(m_modes.size()));
1,184✔
881
   for(const auto& mode : m_modes) {
2,368✔
882
      buf.push_back(static_cast<uint8_t>(mode));
1,184✔
883
   }
884

885
   return buf;
1,184✔
886
}
×
887

888
PSK_Key_Exchange_Modes::PSK_Key_Exchange_Modes(TLS_Data_Reader& reader, uint16_t extension_size) {
1,072✔
889
   if(extension_size < 2) {
1,072✔
890
      throw Decoding_Error("Empty psk_key_exchange_modes extension is illegal");
×
891
   }
892

893
   const auto mode_count = reader.get_byte();
1,072✔
894
   for(uint16_t i = 0; i < mode_count; ++i) {
2,148✔
895
      const auto mode = static_cast<PSK_Key_Exchange_Mode>(reader.get_byte());
1,076✔
896
      if(mode == PSK_Key_Exchange_Mode::PSK_KE || mode == PSK_Key_Exchange_Mode::PSK_DHE_KE) {
1,076✔
897
         m_modes.push_back(mode);
1,070✔
898
      }
899
   }
900
}
1,072✔
901

902
std::vector<uint8_t> Certificate_Authorities::serialize(Connection_Side) const {
81✔
903
   std::vector<uint8_t> out;
81✔
904
   std::vector<uint8_t> dn_list;
81✔
905

906
   for(const auto& dn : m_distinguished_names) {
162✔
907
      std::vector<uint8_t> encoded_dn;
81✔
908
      auto encoder = DER_Encoder(encoded_dn);
81✔
909
      dn.encode_into(encoder);
81✔
910
      append_tls_length_value(dn_list, encoded_dn, 2);
81✔
911
   }
162✔
912

913
   append_tls_length_value(out, dn_list, 2);
81✔
914

915
   return out;
81✔
916
}
81✔
917

918
Certificate_Authorities::Certificate_Authorities(TLS_Data_Reader& reader, uint16_t extension_size) {
3✔
919
   if(extension_size < 2) {
3✔
920
      throw Decoding_Error("Empty certificate_authorities extension is illegal");
×
921
   }
922

923
   const uint16_t purported_size = reader.get_uint16_t();
3✔
924

925
   if(reader.remaining_bytes() != purported_size) {
3✔
926
      throw Decoding_Error("Inconsistent length in certificate_authorities extension");
×
927
   }
928

929
   while(reader.has_remaining()) {
9✔
930
      std::vector<uint8_t> name_bits = reader.get_tls_length_value(2);
6✔
931

932
      BER_Decoder decoder(name_bits.data(), name_bits.size());
6✔
933
      m_distinguished_names.emplace_back();
6✔
934
      decoder.decode(m_distinguished_names.back());
6✔
935
   }
12✔
936
}
3✔
937

938
Certificate_Authorities::Certificate_Authorities(std::vector<X509_DN> acceptable_DNs) :
82✔
939
      m_distinguished_names(std::move(acceptable_DNs)) {}
82✔
940

941
std::vector<uint8_t> EarlyDataIndication::serialize(Connection_Side) const {
8✔
942
   std::vector<uint8_t> result;
8✔
943
   if(m_max_early_data_size.has_value()) {
8✔
944
      const auto max_data = m_max_early_data_size.value();
2✔
945
      result.push_back(get_byte<0>(max_data));
2✔
946
      result.push_back(get_byte<1>(max_data));
2✔
947
      result.push_back(get_byte<2>(max_data));
2✔
948
      result.push_back(get_byte<3>(max_data));
2✔
949
   }
950
   return result;
8✔
951
}
×
952

953
EarlyDataIndication::EarlyDataIndication(TLS_Data_Reader& reader,
5✔
954
                                         uint16_t extension_size,
955
                                         Handshake_Type message_type) {
5✔
956
   if(message_type == Handshake_Type::NewSessionTicket) {
5✔
957
      if(extension_size != 4) {
1✔
958
         throw TLS_Exception(Alert::DecodeError,
×
959
                             "Received an early_data extension in a NewSessionTicket message "
960
                             "without maximum early data size indication");
×
961
      }
962

963
      m_max_early_data_size = reader.get_uint32_t();
1✔
964
   } else if(extension_size != 0) {
4✔
965
      throw TLS_Exception(Alert::DecodeError,
×
966
                          "Received an early_data extension containing an unexpected data "
967
                          "size indication");
×
968
   }
969
}
5✔
970

971
bool EarlyDataIndication::empty() const {
8✔
972
   // This extension may be empty by definition but still carry information
973
   return false;
8✔
974
}
975

976
#endif
977
}  // 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