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

randombit / botan / 21753596263

06 Feb 2026 02:13PM UTC coverage: 90.063% (-0.01%) from 90.073%
21753596263

Pull #5289

github

web-flow
Merge 587099284 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102237 of 113517 relevant lines covered (90.06%)

11402137.11 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/fmt.h>
20
#include <botan/internal/mem_utils.h>
21
#include <botan/internal/stl_util.h>
22
#include <botan/internal/tls_reader.h>
23

24
#include <algorithm>
25
#include <iterator>
26

27
namespace Botan::TLS {
28

29
namespace {
30

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

42
      case Extension_Code::SupportedGroups:
2,908✔
43
         return std::make_unique<Supported_Groups>(reader, size);
2,908✔
44

45
      case Extension_Code::CertificateStatusRequest:
2,673✔
46
         return std::make_unique<Certificate_Status_Request>(reader, size, message_type, from);
2,673✔
47

48
      case Extension_Code::EcPointFormats:
3,199✔
49
         return std::make_unique<Supported_Point_Formats>(reader, size);
3,199✔
50

51
      case Extension_Code::SafeRenegotiation:
5,690✔
52
         return std::make_unique<Renegotiation_Extension>(reader, size);
5,690✔
53

54
      case Extension_Code::SignatureAlgorithms:
2,933✔
55
         return std::make_unique<Signature_Algorithms>(reader, size);
2,933✔
56

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

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

63
      case Extension_Code::ApplicationLayerProtocolNegotiation:
407✔
64
         return std::make_unique<Application_Layer_Protocol_Notification>(reader, size, from);
407✔
65

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

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

72
      case Extension_Code::ExtendedMasterSecret:
5,514✔
73
         return std::make_unique<Extended_Master_Secret>(reader, size);
5,514✔
74

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

78
      case Extension_Code::EncryptThenMac:
813✔
79
         return std::make_unique<Encrypt_then_MAC>(reader, size);
813✔
80

81
      case Extension_Code::SessionTicket:
4,981✔
82
         return std::make_unique<Session_Ticket_Extension>(reader, size);
4,981✔
83

84
      case Extension_Code::SupportedVersions:
1,827✔
85
         return std::make_unique<Supported_Versions>(reader, size, from);
1,827✔
86

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

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

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

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

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

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

108
   return std::make_unique<Unknown_Extension>(code, reader, size);
2,708✔
109
}
110

111
}  // namespace
112

113
Extensions::~Extensions() = default;
22,765✔
114

115
Extension* Extensions::get(Extension_Code type) const {
220,204✔
116
   const auto i =
220,204✔
117
      std::find_if(m_extensions.cbegin(), m_extensions.cend(), [type](const auto& ext) { return ext->type() == type; });
1,131,362✔
118

119
   return (i != m_extensions.end()) ? i->get() : nullptr;
220,204✔
120
}
121

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

128
   m_extensions.emplace_back(extn.release());
84,941✔
129
}
84,941✔
130

131
void Extensions::deserialize(TLS_Data_Reader& reader, const Connection_Side from, const Handshake_Type message_type) {
7,973✔
132
   if(reader.has_remaining()) {
7,973✔
133
      const uint16_t all_extn_size = reader.get_uint16_t();
7,968✔
134

135
      if(reader.remaining_bytes() != all_extn_size) {
7,968✔
136
         throw Decoding_Error("Bad extension size");
119✔
137
      }
138

139
      while(reader.has_remaining()) {
47,287✔
140
         const uint16_t extension_code = reader.get_uint16_t();
39,493✔
141
         const uint16_t extension_size = reader.get_uint16_t();
39,492✔
142

143
         const auto type = static_cast<Extension_Code>(extension_code);
39,492✔
144

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

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

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

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

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

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

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

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

186
   std::unique_ptr<Extension> result;
520✔
187
   if(i != m_extensions.end()) {
520✔
188
      std::swap(result, *i);
251✔
189
      m_extensions.erase(i);
251✔
190
   }
191

192
   return result;
520✔
193
}
194

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

198
   for(const auto& extn : m_extensions) {
59,042✔
199
      if(extn->empty()) {
52,502✔
200
         continue;
3,122✔
201
      }
202

203
      const uint16_t extn_code = static_cast<uint16_t>(extn->type());
49,380✔
204

205
      const std::vector<uint8_t> extn_val = extn->serialize(whoami);
49,380✔
206

207
      buf.push_back(get_byte<0>(extn_code));
49,380✔
208
      buf.push_back(get_byte<1>(extn_code));
49,380✔
209

210
      buf.push_back(get_byte<0>(static_cast<uint16_t>(extn_val.size())));
49,380✔
211
      buf.push_back(get_byte<1>(static_cast<uint16_t>(extn_val.size())));
49,380✔
212

213
      buf += extn_val;
49,380✔
214
   }
49,380✔
215

216
   const uint16_t extn_size = static_cast<uint16_t>(buf.size() - 2);
6,540✔
217

218
   buf[0] = get_byte<0>(extn_size);
6,540✔
219
   buf[1] = get_byte<1>(extn_size);
6,540✔
220

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

226
   return buf;
6,236✔
227
}
6,540✔
228

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

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

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

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

253
   uint16_t name_bytes = reader.get_uint16_t();
2,793✔
254

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

259
   while(name_bytes > 0) {
5,586✔
260
      const uint8_t name_type = reader.get_byte();
2,793✔
261
      name_bytes--;
2,793✔
262

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

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

284
   std::vector<uint8_t> buf;
4,290✔
285

286
   const size_t name_len = m_sni_host_name.size();
4,290✔
287

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

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

295
   buf += as_span_of_bytes(m_sni_host_name);
4,290✔
296

297
   return buf;
4,290✔
298
}
4,290✔
299

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

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

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

320
   const uint16_t name_bytes = reader.get_uint16_t();
407✔
321

322
   size_t bytes_remaining = extension_size - 2;
407✔
323

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

328
   while(bytes_remaining > 0) {
1,085✔
329
      const std::string p = reader.get_string(1, 0, 255);
685✔
330

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

335
      if(p.empty()) {
685✔
336
         throw Decoding_Error("Empty ALPN protocol not allowed");
7✔
337
      }
338

339
      bytes_remaining -= (p.size() + 1);
678✔
340

341
      m_protocols.push_back(p);
678✔
342
   }
685✔
343

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

355
std::string Application_Layer_Protocol_Notification::single_protocol() const {
148✔
356
   BOTAN_STATE_CHECK(m_protocols.size() == 1);
148✔
357
   return m_protocols.front();
148✔
358
}
359

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

363
   for(auto&& proto : m_protocols) {
958✔
364
      if(proto.length() >= 256) {
593✔
365
         throw TLS_Exception(Alert::InternalError, "ALPN name too long");
×
366
      }
367
      if(!proto.empty()) {
593✔
368
         append_tls_length_value(buf, proto, 1);
1,186✔
369
      }
370
   }
371

372
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
365✔
373
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
365✔
374

375
   return buf;
365✔
376
}
×
377

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

386
   return "Unknown";
×
387
}
388

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

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

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

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

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

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

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

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

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

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

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

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

498
Supported_Groups::Supported_Groups(const std::vector<Group_Params>& groups) : m_groups(groups) {}
4,047✔
499

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

504
std::vector<Group_Params> Supported_Groups::ec_groups() const {
5,025✔
505
   std::vector<Group_Params> ec;
5,025✔
506
   for(auto g : m_groups) {
52,111✔
507
      if(g.is_pure_ecc_group()) {
94,172✔
508
         ec.push_back(g);
34,697✔
509
      }
510
   }
511
   return ec;
5,025✔
512
}
×
513

514
std::vector<Group_Params> Supported_Groups::dh_groups() const {
1,514✔
515
   std::vector<Group_Params> dh;
1,514✔
516
   for(auto g : m_groups) {
11,149✔
517
      if(g.is_in_ffdhe_range()) {
12,483✔
518
         dh.push_back(g);
556✔
519
      }
520
   }
521
   return dh;
1,514✔
522
}
×
523

524
std::vector<uint8_t> Supported_Groups::serialize(Connection_Side /*whoami*/) const {
4,724✔
525
   std::vector<uint8_t> buf(2);
4,724✔
526

527
   for(auto g : m_groups) {
54,923✔
528
      const uint16_t id = g.wire_code();
50,199✔
529

530
      if(id > 0) {
50,199✔
531
         buf.push_back(get_byte<0>(id));
50,199✔
532
         buf.push_back(get_byte<1>(id));
50,199✔
533
      }
534
   }
535

536
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
4,724✔
537
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
4,724✔
538

539
   return buf;
4,724✔
540
}
×
541

542
Supported_Groups::Supported_Groups(TLS_Data_Reader& reader, uint16_t extension_size) {
2,910✔
543
   const uint16_t len = reader.get_uint16_t();
2,910✔
544

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

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

553
   const size_t elems = len / 2;
2,906✔
554

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

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

573
Supported_Point_Formats::Supported_Point_Formats(TLS_Data_Reader& reader, uint16_t extension_size) {
3,199✔
574
   const uint8_t len = reader.get_byte();
3,199✔
575

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

580
   bool includes_uncompressed = false;
3,202✔
581
   for(size_t i = 0; i != len; ++i) {
3,202✔
582
      const uint8_t format = reader.get_byte();
3,201✔
583

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

598
      // ignore ANSIX962_COMPRESSED_CHAR2, we don't support these curves
599
   }
600

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

613
namespace {
614

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

618
   std::vector<uint8_t> buf;
4,729✔
619

620
   const uint16_t len = static_cast<uint16_t>(schemes.size() * 2);
4,729✔
621

622
   buf.push_back(get_byte<0>(len));
4,729✔
623
   buf.push_back(get_byte<1>(len));
4,729✔
624

625
   for(const Signature_Scheme scheme : schemes) {
46,330✔
626
      buf.push_back(get_byte<0>(scheme.wire_code()));
41,601✔
627
      buf.push_back(get_byte<1>(scheme.wire_code()));
41,601✔
628
   }
629

630
   return buf;
4,729✔
631
}
×
632

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

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

640
   std::vector<Signature_Scheme> schemes;
2,933✔
641
   schemes.reserve(len / 2);
2,933✔
642
   while(len > 0) {
30,771✔
643
      schemes.emplace_back(reader.get_uint16_t());
27,838✔
644
      len -= 2;
27,838✔
645
   }
646

647
   return schemes;
2,933✔
648
}
×
649

650
}  // namespace
651

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

656
Signature_Algorithms::Signature_Algorithms(TLS_Data_Reader& reader, uint16_t extension_size) :
2,933✔
657
      m_schemes(parse_signature_algorithms(reader, extension_size)) {}
2,933✔
658

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

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

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

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

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

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

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

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

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

694
   buf.push_back(0);  // srtp_mki, always empty here
5✔
695

696
   return buf;
5✔
697
}
×
698

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

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

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

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

719
std::vector<uint8_t> Supported_Versions::serialize(Connection_Side whoami) const {
4,484✔
720
   std::vector<uint8_t> buf;
4,484✔
721

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

730
      buf.push_back(len);
4,055✔
731

732
      for(const Protocol_Version version : m_versions) {
9,228✔
733
         buf.push_back(version.major_version());
5,173✔
734
         buf.push_back(version.minor_version());
5,173✔
735
      }
736
   }
737

738
   return buf;
4,484✔
739
}
×
740

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

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

771
      for(auto v : versions) {
4,565✔
772
         m_versions.push_back(Protocol_Version(v));
3,277✔
773
      }
774

775
      if(extension_size != 1 + 2 * versions.size()) {
1,288✔
776
         throw Decoding_Error("Client sent invalid supported_versions extension");
×
777
      }
778
   }
1,288✔
779
}
1,829✔
780

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

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

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

801
   m_limit = reader.get_uint16_t();
45✔
802

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

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

830
std::vector<uint8_t> Record_Size_Limit::serialize(Connection_Side /*whoami*/) const {
53✔
831
   std::vector<uint8_t> buf;
53✔
832

833
   buf.push_back(get_byte<0>(m_limit));
53✔
834
   buf.push_back(get_byte<1>(m_limit));
53✔
835

836
   return buf;
53✔
837
}
×
838

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

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

847
   const uint16_t len = reader.get_uint16_t();
12✔
848

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

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

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

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

866
   const uint16_t len = static_cast<uint16_t>(m_cookie.size());
10✔
867

868
   buf.push_back(get_byte<0>(len));
10✔
869
   buf.push_back(get_byte<1>(len));
10✔
870

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

875
   return buf;
10✔
876
}
×
877

878
std::vector<uint8_t> PSK_Key_Exchange_Modes::serialize(Connection_Side /*whoami*/) const {
1,184✔
879
   std::vector<uint8_t> buf;
1,184✔
880

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

887
   return buf;
1,184✔
888
}
×
889

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

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

904
std::vector<uint8_t> Certificate_Authorities::serialize(Connection_Side /*whoami*/) const {
81✔
905
   std::vector<uint8_t> out;
81✔
906
   std::vector<uint8_t> dn_list;
81✔
907

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

915
   append_tls_length_value(out, dn_list, 2);
81✔
916

917
   return out;
81✔
918
}
81✔
919

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

925
   const uint16_t purported_size = reader.get_uint16_t();
3✔
926

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

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

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

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

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

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

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

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

978
#endif
979
}  // 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