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

randombit / botan / 21851334357

10 Feb 2026 01:47AM UTC coverage: 90.073% (+0.004%) from 90.069%
21851334357

push

github

web-flow
Merge pull request #5296 from randombit/jack/tls-header-patrol

Various changes to reduce header dependencies in TLS

102230 of 113497 relevant lines covered (90.07%)

11359047.13 hits per line

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

89.77
/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/pkix_types.h>
18
#include <botan/tls_exceptn.h>
19
#include <botan/tls_policy.h>
20
#include <botan/internal/fmt.h>
21
#include <botan/internal/mem_utils.h>
22
#include <botan/internal/stl_util.h>
23
#include <botan/internal/tls_reader.h>
24

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

28
namespace Botan::TLS {
29

30
namespace {
31

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

112
}  // namespace
113

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

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

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

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

129
   m_extensions.emplace_back(extn.release());
84,932✔
130
}
84,932✔
131

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

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

140
      while(reader.has_remaining()) {
47,282✔
141
         const uint16_t extension_code = reader.get_uint16_t();
39,488✔
142
         const uint16_t extension_size = reader.get_uint16_t();
39,487✔
143

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

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

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

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

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

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

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

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

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

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

193
   return result;
520✔
194
}
195

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

199
   for(const auto& extn : m_extensions) {
59,035✔
200
      if(extn->empty()) {
52,495✔
201
         continue;
3,120✔
202
      }
203

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

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

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

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

214
      buf += extn_val;
49,375✔
215
   }
49,375✔
216

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

323
   size_t bytes_remaining = extension_size - 2;
407✔
324

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

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

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

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

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

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

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

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

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

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

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

376
   return buf;
365✔
377
}
×
378

379
Certificate_Type_Base::Certificate_Type_Base(std::vector<Certificate_Type> supported_cert_types) :
1,976✔
380
      m_certificate_types(std::move(supported_cert_types)), m_from(Connection_Side::Client) {
1,976✔
381
   BOTAN_ARG_CHECK(!m_certificate_types.empty(), "at least one certificate type must be supported");
1,976✔
382
}
1,976✔
383

384
Client_Certificate_Type::Client_Certificate_Type(const Client_Certificate_Type& cct, const Policy& policy) :
1✔
385
      Certificate_Type_Base(cct, policy.accepted_client_certificate_types()) {}
1✔
386

387
Server_Certificate_Type::Server_Certificate_Type(const Server_Certificate_Type& sct, const Policy& policy) :
1✔
388
      Certificate_Type_Base(sct, policy.accepted_server_certificate_types()) {}
1✔
389

390
Certificate_Type_Base::Certificate_Type_Base(const Certificate_Type_Base& certificate_type_from_client,
2✔
391
                                             const std::vector<Certificate_Type>& server_preference) :
2✔
392
      m_from(Connection_Side::Server) {
2✔
393
   // RFC 7250 4.2
394
   //    The server_certificate_type extension in the client hello indicates the
395
   //    types of certificates the client is able to process when provided by
396
   //    the server in a subsequent certificate payload. [...] With the
397
   //    server_certificate_type extension in the server hello, the TLS server
398
   //    indicates the certificate type carried in the Certificate payload.
399
   for(const auto server_supported_cert_type : server_preference) {
2✔
400
      if(value_exists(certificate_type_from_client.m_certificate_types, server_supported_cert_type)) {
4✔
401
         m_certificate_types.push_back(server_supported_cert_type);
2✔
402
         return;
2✔
403
      }
404
   }
405

406
   // RFC 7250 4.2 (2.)
407
   //    The server supports the extension defined in this document, but
408
   //    it does not have any certificate type in common with the client.
409
   //    Then, the server terminates the session with a fatal alert of
410
   //    type "unsupported_certificate".
411
   throw TLS_Exception(Alert::UnsupportedCertificate, "Failed to agree on certificate_type");
×
412
}
×
413

414
Certificate_Type_Base::Certificate_Type_Base(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) :
4✔
415
      m_from(from) {
4✔
416
   if(extension_size == 0) {
4✔
417
      throw Decoding_Error("Certificate type extension cannot be empty");
×
418
   }
419

420
   if(from == Connection_Side::Client) {
4✔
421
      const auto type_bytes = reader.get_tls_length_value(1);
2✔
422
      if(static_cast<size_t>(extension_size) != type_bytes.size() + 1) {
2✔
423
         throw Decoding_Error("certificate type extension had inconsistent length");
×
424
      }
425
      std::transform(
2✔
426
         type_bytes.begin(), type_bytes.end(), std::back_inserter(m_certificate_types), [](const auto type_byte) {
2✔
427
            return static_cast<Certificate_Type>(type_byte);
428
         });
429
   } else {
2✔
430
      // RFC 7250 4.2
431
      //    Note that only a single value is permitted in the
432
      //    server_certificate_type extension when carried in the server hello.
433
      if(extension_size != 1) {
2✔
434
         throw Decoding_Error("Server's certificate type extension must be of length 1");
×
435
      }
436
      const auto type_byte = reader.get_byte();
2✔
437
      m_certificate_types.push_back(static_cast<Certificate_Type>(type_byte));
2✔
438
   }
439
}
4✔
440

441
std::vector<uint8_t> Certificate_Type_Base::serialize(Connection_Side whoami) const {
12✔
442
   std::vector<uint8_t> result;
12✔
443
   if(whoami == Connection_Side::Client) {
12✔
444
      std::vector<uint8_t> type_bytes;
6✔
445
      std::transform(
6✔
446
         m_certificate_types.begin(), m_certificate_types.end(), std::back_inserter(type_bytes), [](const auto type) {
447
            return static_cast<uint8_t>(type);
448
         });
449
      append_tls_length_value(result, type_bytes, 1);
12✔
450
   } else {
6✔
451
      BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
6✔
452
      result.push_back(static_cast<uint8_t>(m_certificate_types.front()));
6✔
453
   }
454
   return result;
12✔
455
}
×
456

457
void Certificate_Type_Base::validate_selection(const Certificate_Type_Base& from_server) const {
2✔
458
   BOTAN_ASSERT_NOMSG(m_from == Connection_Side::Client);
2✔
459
   BOTAN_ASSERT_NOMSG(from_server.m_from == Connection_Side::Server);
2✔
460

461
   // RFC 7250 4.2
462
   //    The value conveyed in the [client_]certificate_type extension MUST be
463
   //    selected from one of the values provided in the [client_]certificate_type
464
   //    extension sent in the client hello.
465
   if(!value_exists(m_certificate_types, from_server.selected_certificate_type())) {
4✔
466
      throw TLS_Exception(Alert::IllegalParameter,
×
467
                          Botan::fmt("Selected certificate type was not offered: {}",
×
468
                                     certificate_type_to_string(from_server.selected_certificate_type())));
×
469
   }
470
}
2✔
471

472
Certificate_Type Certificate_Type_Base::selected_certificate_type() const {
6✔
473
   BOTAN_ASSERT_NOMSG(m_from == Connection_Side::Server);
6✔
474
   BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
6✔
475
   return m_certificate_types.front();
6✔
476
}
477

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

480
const std::vector<Group_Params>& Supported_Groups::groups() const {
948✔
481
   return m_groups;
948✔
482
}
483

484
std::vector<Group_Params> Supported_Groups::ec_groups() const {
5,024✔
485
   std::vector<Group_Params> ec;
5,024✔
486
   for(auto g : m_groups) {
52,100✔
487
      if(g.is_pure_ecc_group()) {
94,152✔
488
         ec.push_back(g);
34,689✔
489
      }
490
   }
491
   return ec;
5,024✔
492
}
×
493

494
std::vector<Group_Params> Supported_Groups::dh_groups() const {
1,511✔
495
   std::vector<Group_Params> dh;
1,511✔
496
   for(auto g : m_groups) {
11,116✔
497
      if(g.is_in_ffdhe_range()) {
12,447✔
498
         dh.push_back(g);
550✔
499
      }
500
   }
501
   return dh;
1,511✔
502
}
×
503

504
std::vector<uint8_t> Supported_Groups::serialize(Connection_Side /*whoami*/) const {
4,724✔
505
   std::vector<uint8_t> buf(2);
4,724✔
506

507
   for(auto g : m_groups) {
54,923✔
508
      const uint16_t id = g.wire_code();
50,199✔
509

510
      if(id > 0) {
50,199✔
511
         buf.push_back(get_byte<0>(id));
50,199✔
512
         buf.push_back(get_byte<1>(id));
50,199✔
513
      }
514
   }
515

516
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
4,724✔
517
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
4,724✔
518

519
   return buf;
4,724✔
520
}
×
521

522
Supported_Groups::Supported_Groups(TLS_Data_Reader& reader, uint16_t extension_size) {
2,910✔
523
   const uint16_t len = reader.get_uint16_t();
2,910✔
524

525
   if(len + 2 != extension_size) {
2,910✔
526
      throw Decoding_Error("Inconsistent length field in supported groups list");
4✔
527
   }
528

529
   if(len % 2 == 1) {
2,906✔
530
      throw Decoding_Error("Supported groups list of strange size");
×
531
   }
532

533
   const size_t elems = len / 2;
2,906✔
534

535
   for(size_t i = 0; i != elems; ++i) {
23,900✔
536
      const auto group = static_cast<Group_Params>(reader.get_uint16_t());
20,994✔
537
      // Note: RFC 8446 does not explicitly enforce that groups must be unique.
538
      if(!value_exists(m_groups, group)) {
41,988✔
539
         m_groups.push_back(group);
20,994✔
540
      }
541
   }
542
}
2,910✔
543

544
std::vector<uint8_t> Supported_Point_Formats::serialize(Connection_Side /*whoami*/) const {
5,112✔
545
   // if this extension is sent, it MUST include uncompressed (RFC 4492, section 5.1)
546
   if(m_prefers_compressed) {
5,112✔
547
      return std::vector<uint8_t>{2, ANSIX962_COMPRESSED_PRIME, UNCOMPRESSED};
10✔
548
   } else {
549
      return std::vector<uint8_t>{1, UNCOMPRESSED};
5,102✔
550
   }
551
}
552

553
Supported_Point_Formats::Supported_Point_Formats(TLS_Data_Reader& reader, uint16_t extension_size) {
3,199✔
554
   const uint8_t len = reader.get_byte();
3,199✔
555

556
   if(len + 1 != extension_size) {
3,199✔
557
      throw Decoding_Error("Inconsistent length field in supported point formats list");
2✔
558
   }
559

560
   bool includes_uncompressed = false;
3,202✔
561
   for(size_t i = 0; i != len; ++i) {
3,202✔
562
      const uint8_t format = reader.get_byte();
3,201✔
563

564
      if(static_cast<ECPointFormat>(format) == UNCOMPRESSED) {
3,201✔
565
         m_prefers_compressed = false;
3,186✔
566
         reader.discard_next(len - i - 1);
3,186✔
567
         return;
3,186✔
568
      } else if(static_cast<ECPointFormat>(format) == ANSIX962_COMPRESSED_PRIME) {
15✔
569
         m_prefers_compressed = true;
10✔
570
         std::vector<uint8_t> remaining_formats = reader.get_fixed<uint8_t>(len - i - 1);
10✔
571
         includes_uncompressed =
10✔
572
            std::any_of(std::begin(remaining_formats), std::end(remaining_formats), [](uint8_t remaining_format) {
10✔
573
               return static_cast<ECPointFormat>(remaining_format) == UNCOMPRESSED;
574
            });
575
         break;
10✔
576
      }
10✔
577

578
      // ignore ANSIX962_COMPRESSED_CHAR2, we don't support these curves
579
   }
580

581
   // RFC 4492 5.1.:
582
   //   If the Supported Point Formats Extension is indeed sent, it MUST contain the value 0 (uncompressed)
583
   //   as one of the items in the list of point formats.
584
   // Note:
585
   //   RFC 8422 5.1.2. explicitly requires this check,
586
   //   but only if the Supported Groups extension was sent.
587
   if(!includes_uncompressed) {
11✔
588
      throw TLS_Exception(Alert::IllegalParameter,
1✔
589
                          "Supported Point Formats Extension must contain the uncompressed point format");
1✔
590
   }
591
}
592

593
namespace {
594

595
std::vector<uint8_t> serialize_signature_algorithms(const std::vector<Signature_Scheme>& schemes) {
4,728✔
596
   BOTAN_ASSERT(schemes.size() < 256, "Too many signature schemes");
4,728✔
597

598
   std::vector<uint8_t> buf;
4,728✔
599

600
   const uint16_t len = static_cast<uint16_t>(schemes.size() * 2);
4,728✔
601

602
   buf.push_back(get_byte<0>(len));
4,728✔
603
   buf.push_back(get_byte<1>(len));
4,728✔
604

605
   for(const Signature_Scheme scheme : schemes) {
46,320✔
606
      buf.push_back(get_byte<0>(scheme.wire_code()));
41,592✔
607
      buf.push_back(get_byte<1>(scheme.wire_code()));
41,592✔
608
   }
609

610
   return buf;
4,728✔
611
}
×
612

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

616
   if(len + 2 != extension_size || len % 2 == 1 || len == 0) {
2,934✔
617
      throw Decoding_Error("Bad encoding on signature algorithms extension");
1✔
618
   }
619

620
   std::vector<Signature_Scheme> schemes;
2,933✔
621
   schemes.reserve(len / 2);
2,933✔
622
   while(len > 0) {
30,771✔
623
      schemes.emplace_back(reader.get_uint16_t());
27,838✔
624
      len -= 2;
27,838✔
625
   }
626

627
   return schemes;
2,933✔
628
}
×
629

630
}  // namespace
631

632
std::vector<uint8_t> Signature_Algorithms::serialize(Connection_Side /*whoami*/) const {
4,726✔
633
   return serialize_signature_algorithms(m_schemes);
4,726✔
634
}
635

636
Signature_Algorithms::Signature_Algorithms(TLS_Data_Reader& reader, uint16_t extension_size) :
2,933✔
637
      m_schemes(parse_signature_algorithms(reader, extension_size)) {}
2,933✔
638

639
std::vector<uint8_t> Signature_Algorithms_Cert::serialize(Connection_Side /*whoami*/) const {
2✔
640
   return serialize_signature_algorithms(m_schemes);
2✔
641
}
642

643
Signature_Algorithms_Cert::Signature_Algorithms_Cert(TLS_Data_Reader& reader, uint16_t extension_size) :
2✔
644
      m_schemes(parse_signature_algorithms(reader, extension_size)) {}
2✔
645

646
Session_Ticket_Extension::Session_Ticket_Extension(TLS_Data_Reader& reader, uint16_t extension_size) :
4,979✔
647
      m_ticket(Session_Ticket(reader.get_elem<uint8_t, std::vector<uint8_t>>(extension_size))) {}
4,979✔
648

649
SRTP_Protection_Profiles::SRTP_Protection_Profiles(TLS_Data_Reader& reader, uint16_t extension_size) :
10✔
650
      m_pp(reader.get_range<uint16_t>(2, 0, 65535)) {
10✔
651
   const std::vector<uint8_t> mki = reader.get_range<uint8_t>(1, 0, 255);
9✔
652

653
   if(m_pp.size() * 2 + mki.size() + 3 != extension_size) {
9✔
654
      throw Decoding_Error("Bad encoding for SRTP protection extension");
×
655
   }
656

657
   if(!mki.empty()) {
9✔
658
      throw Decoding_Error("Unhandled non-empty MKI for SRTP protection extension");
×
659
   }
660
}
9✔
661

662
std::vector<uint8_t> SRTP_Protection_Profiles::serialize(Connection_Side /*whoami*/) const {
5✔
663
   std::vector<uint8_t> buf;
5✔
664

665
   const uint16_t pp_len = static_cast<uint16_t>(m_pp.size() * 2);
5✔
666
   buf.push_back(get_byte<0>(pp_len));
5✔
667
   buf.push_back(get_byte<1>(pp_len));
5✔
668

669
   for(const uint16_t pp : m_pp) {
12✔
670
      buf.push_back(get_byte<0>(pp));
7✔
671
      buf.push_back(get_byte<1>(pp));
7✔
672
   }
673

674
   buf.push_back(0);  // srtp_mki, always empty here
5✔
675

676
   return buf;
5✔
677
}
×
678

679
Extended_Master_Secret::Extended_Master_Secret(TLS_Data_Reader& /*unused*/, uint16_t extension_size) {
5,514✔
680
   if(extension_size != 0) {
5,514✔
681
      throw Decoding_Error("Invalid extended_master_secret extension");
2✔
682
   }
683
}
5,512✔
684

685
std::vector<uint8_t> Extended_Master_Secret::serialize(Connection_Side /*whoami*/) const {
5,242✔
686
   return std::vector<uint8_t>();
5,242✔
687
}
688

689
Encrypt_then_MAC::Encrypt_then_MAC(TLS_Data_Reader& /*unused*/, uint16_t extension_size) {
813✔
690
   if(extension_size != 0) {
813✔
691
      throw Decoding_Error("Invalid encrypt_then_mac extension");
×
692
   }
693
}
813✔
694

695
std::vector<uint8_t> Encrypt_then_MAC::serialize(Connection_Side /*whoami*/) const {
3,982✔
696
   return std::vector<uint8_t>();
3,982✔
697
}
698

699
std::vector<uint8_t> Supported_Versions::serialize(Connection_Side whoami) const {
4,482✔
700
   std::vector<uint8_t> buf;
4,482✔
701

702
   if(whoami == Connection_Side::Server) {
4,482✔
703
      BOTAN_ASSERT_NOMSG(m_versions.size() == 1);
429✔
704
      buf.push_back(m_versions[0].major_version());
429✔
705
      buf.push_back(m_versions[0].minor_version());
429✔
706
   } else {
707
      BOTAN_ASSERT_NOMSG(!m_versions.empty());
4,053✔
708
      const uint8_t len = static_cast<uint8_t>(m_versions.size() * 2);
4,053✔
709

710
      buf.push_back(len);
4,053✔
711

712
      for(const Protocol_Version version : m_versions) {
9,224✔
713
         buf.push_back(version.major_version());
5,171✔
714
         buf.push_back(version.minor_version());
5,171✔
715
      }
716
   }
717

718
   return buf;
4,482✔
719
}
×
720

721
Supported_Versions::Supported_Versions(Protocol_Version offer, const Policy& policy) {
3,472✔
722
   if(offer.is_datagram_protocol()) {
3,472✔
723
#if defined(BOTAN_HAS_TLS_12)
724
      if(offer >= Protocol_Version::DTLS_V12 && policy.allow_dtls12()) {
400✔
725
         m_versions.push_back(Protocol_Version::DTLS_V12);
400✔
726
      }
727
#endif
728
   } else {
729
#if defined(BOTAN_HAS_TLS_13)
730
      if(offer >= Protocol_Version::TLS_V13 && policy.allow_tls13()) {
5,156✔
731
         m_versions.push_back(Protocol_Version::TLS_V13);
988✔
732
      }
733
#endif
734
#if defined(BOTAN_HAS_TLS_12)
735
      if(offer >= Protocol_Version::TLS_V12 && policy.allow_tls12()) {
4,060✔
736
         m_versions.push_back(Protocol_Version::TLS_V12);
3,043✔
737
      }
738
#endif
739
   }
740
}
3,472✔
741

742
Supported_Versions::Supported_Versions(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
1,827✔
743
   if(from == Connection_Side::Server) {
1,827✔
744
      if(extension_size != 2) {
541✔
745
         throw Decoding_Error("Server sent invalid supported_versions extension");
×
746
      }
747
      m_versions.push_back(Protocol_Version(reader.get_uint16_t()));
541✔
748
   } else {
749
      auto versions = reader.get_range<uint16_t>(1, 1, 127);
1,286✔
750

751
      for(auto v : versions) {
4,561✔
752
         m_versions.push_back(Protocol_Version(v));
3,275✔
753
      }
754

755
      if(extension_size != 1 + 2 * versions.size()) {
1,286✔
756
         throw Decoding_Error("Client sent invalid supported_versions extension");
×
757
      }
758
   }
1,286✔
759
}
1,827✔
760

761
bool Supported_Versions::supports(Protocol_Version version) const {
1,485✔
762
   for(auto v : m_versions) {
1,996✔
763
      if(version == v) {
1,977✔
764
         return true;
1,485✔
765
      }
766
   }
767
   return false;
768
}
769

770
Record_Size_Limit::Record_Size_Limit(const uint16_t limit) : m_limit(limit) {
16✔
771
   BOTAN_ASSERT(limit >= 64, "RFC 8449 does not allow record size limits smaller than 64 bytes");
16✔
772
   BOTAN_ASSERT(limit <= MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */,
16✔
773
                "RFC 8449 does not allow record size limits larger than 2^14+1");
774
}
16✔
775

776
Record_Size_Limit::Record_Size_Limit(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
45✔
777
   if(extension_size != 2) {
45✔
778
      throw TLS_Exception(Alert::DecodeError, "invalid record_size_limit extension");
×
779
   }
780

781
   m_limit = reader.get_uint16_t();
45✔
782

783
   // RFC 8449 4.
784
   //    This value is the length of the plaintext of a protected record.
785
   //    The value includes the content type and padding added in TLS 1.3 (that
786
   //    is, the complete length of TLSInnerPlaintext).
787
   //
788
   //    A server MUST NOT enforce this restriction; a client might advertise
789
   //    a higher limit that is enabled by an extension or version the server
790
   //    does not understand. A client MAY abort the handshake with an
791
   //    "illegal_parameter" alert.
792
   //
793
   // Note: We are currently supporting this extension in TLS 1.3 only, hence
794
   //       we check for the TLS 1.3 limit. The TLS 1.2 limit would not include
795
   //       the "content type byte" and hence be one byte less!
796
   if(m_limit > MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */ && from == Connection_Side::Server) {
45✔
797
      throw TLS_Exception(Alert::IllegalParameter,
×
798
                          "Server requested a record size limit larger than the protocol's maximum");
×
799
   }
800

801
   // RFC 8449 4.
802
   //    Endpoints MUST NOT send a "record_size_limit" extension with a value
803
   //    smaller than 64.  An endpoint MUST treat receipt of a smaller value
804
   //    as a fatal error and generate an "illegal_parameter" alert.
805
   if(m_limit < 64) {
45✔
806
      throw TLS_Exception(Alert::IllegalParameter, "Received a record size limit smaller than 64 bytes");
×
807
   }
808
}
45✔
809

810
std::vector<uint8_t> Record_Size_Limit::serialize(Connection_Side /*whoami*/) const {
53✔
811
   std::vector<uint8_t> buf;
53✔
812

813
   buf.push_back(get_byte<0>(m_limit));
53✔
814
   buf.push_back(get_byte<1>(m_limit));
53✔
815

816
   return buf;
53✔
817
}
×
818

819
#if defined(BOTAN_HAS_TLS_13)
820
Cookie::Cookie(const std::vector<uint8_t>& cookie) : m_cookie(cookie) {}
4✔
821

822
Cookie::Cookie(TLS_Data_Reader& reader, uint16_t extension_size) {
12✔
823
   if(extension_size == 0) {
12✔
824
      return;
825
   }
826

827
   const uint16_t len = reader.get_uint16_t();
12✔
828

829
   if(len == 0) {
12✔
830
      // Based on RFC 8446 4.2.2, len of the Cookie buffer must be at least 1
831
      throw Decoding_Error("Cookie length must be at least 1 byte");
1✔
832
   }
833

834
   if(len > reader.remaining_bytes()) {
11✔
835
      throw Decoding_Error("Not enough bytes in the buffer to decode Cookie");
×
836
   }
837

838
   for(size_t i = 0; i < len; ++i) {
719✔
839
      m_cookie.push_back(reader.get_byte());
708✔
840
   }
841
}
1✔
842

843
std::vector<uint8_t> Cookie::serialize(Connection_Side /*whoami*/) const {
10✔
844
   std::vector<uint8_t> buf;
10✔
845

846
   const uint16_t len = static_cast<uint16_t>(m_cookie.size());
10✔
847

848
   buf.push_back(get_byte<0>(len));
10✔
849
   buf.push_back(get_byte<1>(len));
10✔
850

851
   for(const auto& cookie_byte : m_cookie) {
712✔
852
      buf.push_back(cookie_byte);
702✔
853
   }
854

855
   return buf;
10✔
856
}
×
857

858
std::vector<uint8_t> PSK_Key_Exchange_Modes::serialize(Connection_Side /*whoami*/) const {
1,184✔
859
   std::vector<uint8_t> buf;
1,184✔
860

861
   BOTAN_ASSERT_NOMSG(m_modes.size() < 256);
1,184✔
862
   buf.push_back(static_cast<uint8_t>(m_modes.size()));
1,184✔
863
   for(const auto& mode : m_modes) {
2,368✔
864
      buf.push_back(static_cast<uint8_t>(mode));
1,184✔
865
   }
866

867
   return buf;
1,184✔
868
}
×
869

870
PSK_Key_Exchange_Modes::PSK_Key_Exchange_Modes(TLS_Data_Reader& reader, uint16_t extension_size) {
1,072✔
871
   if(extension_size < 2) {
1,072✔
872
      throw Decoding_Error("Empty psk_key_exchange_modes extension is illegal");
×
873
   }
874

875
   const auto mode_count = reader.get_byte();
1,072✔
876
   for(uint16_t i = 0; i < mode_count; ++i) {
2,148✔
877
      const auto mode = static_cast<PSK_Key_Exchange_Mode>(reader.get_byte());
1,076✔
878
      if(mode == PSK_Key_Exchange_Mode::PSK_KE || mode == PSK_Key_Exchange_Mode::PSK_DHE_KE) {
1,076✔
879
         m_modes.push_back(mode);
1,070✔
880
      }
881
   }
882
}
1,072✔
883

884
std::vector<uint8_t> Certificate_Authorities::serialize(Connection_Side /*whoami*/) const {
81✔
885
   std::vector<uint8_t> out;
81✔
886
   std::vector<uint8_t> dn_list;
81✔
887

888
   for(const auto& dn : m_distinguished_names) {
162✔
889
      std::vector<uint8_t> encoded_dn;
81✔
890
      auto encoder = DER_Encoder(encoded_dn);
81✔
891
      dn.encode_into(encoder);
81✔
892
      append_tls_length_value(dn_list, encoded_dn, 2);
81✔
893
   }
162✔
894

895
   append_tls_length_value(out, dn_list, 2);
81✔
896

897
   return out;
81✔
898
}
81✔
899

900
Certificate_Authorities::Certificate_Authorities(TLS_Data_Reader& reader, uint16_t extension_size) {
3✔
901
   if(extension_size < 2) {
3✔
902
      throw Decoding_Error("Empty certificate_authorities extension is illegal");
×
903
   }
904

905
   const uint16_t purported_size = reader.get_uint16_t();
3✔
906

907
   if(reader.remaining_bytes() != purported_size) {
3✔
908
      throw Decoding_Error("Inconsistent length in certificate_authorities extension");
×
909
   }
910

911
   while(reader.has_remaining()) {
9✔
912
      std::vector<uint8_t> name_bits = reader.get_tls_length_value(2);
6✔
913

914
      BER_Decoder decoder(name_bits.data(), name_bits.size());
6✔
915
      m_distinguished_names.emplace_back();
6✔
916
      decoder.decode(m_distinguished_names.back());
6✔
917
   }
12✔
918
}
3✔
919

920
Certificate_Authorities::~Certificate_Authorities() = default;
170✔
921

922
Certificate_Authorities::Certificate_Authorities(const std::vector<X509_DN>& acceptable_DNs) :
82✔
923
      m_distinguished_names(acceptable_DNs) {}
82✔
924

925
std::vector<uint8_t> EarlyDataIndication::serialize(Connection_Side /*whoami*/) const {
8✔
926
   std::vector<uint8_t> result;
8✔
927
   if(m_max_early_data_size.has_value()) {
8✔
928
      const auto max_data = m_max_early_data_size.value();
2✔
929
      result.push_back(get_byte<0>(max_data));
2✔
930
      result.push_back(get_byte<1>(max_data));
2✔
931
      result.push_back(get_byte<2>(max_data));
2✔
932
      result.push_back(get_byte<3>(max_data));
2✔
933
   }
934
   return result;
8✔
935
}
×
936

937
EarlyDataIndication::EarlyDataIndication(TLS_Data_Reader& reader,
5✔
938
                                         uint16_t extension_size,
939
                                         Handshake_Type message_type) {
5✔
940
   if(message_type == Handshake_Type::NewSessionTicket) {
5✔
941
      if(extension_size != 4) {
1✔
942
         throw TLS_Exception(Alert::DecodeError,
×
943
                             "Received an early_data extension in a NewSessionTicket message "
944
                             "without maximum early data size indication");
×
945
      }
946

947
      m_max_early_data_size = reader.get_uint32_t();
1✔
948
   } else if(extension_size != 0) {
4✔
949
      throw TLS_Exception(Alert::DecodeError,
×
950
                          "Received an early_data extension containing an unexpected data "
951
                          "size indication");
×
952
   }
953
}
5✔
954

955
bool EarlyDataIndication::empty() const {
8✔
956
   // This extension may be empty by definition but still carry information
957
   return false;
8✔
958
}
959

960
#endif
961
}  // 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