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

randombit / botan / 23989899232

04 Apr 2026 11:31PM UTC coverage: 89.44% (-0.01%) from 89.454%
23989899232

Pull #5521

github

web-flow
Merge fdd9c9e10 into 417709dd7
Pull Request #5521: Rollup of small fixes

105816 of 118310 relevant lines covered (89.44%)

11574334.41 hits per line

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

90.38
/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/tls_exceptn.h>
16
#include <botan/tls_policy.h>
17
#include <botan/internal/fmt.h>
18
#include <botan/internal/parsing.h>
19
#include <botan/internal/stl_util.h>
20
#include <botan/internal/tls_reader.h>
21
#include <algorithm>
22
#include <unordered_set>
23

24
#if defined(BOTAN_HAS_TLS_13)
25
   #include <botan/tls_extensions_13.h>
26
#endif
27

28
#if defined(BOTAN_HAS_TLS_12)
29
   #include <botan/tls_extensions_12.h>
30
#endif
31

32
namespace Botan::TLS {
33

34
namespace {
35

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

47
      case Extension_Code::SupportedGroups:
2,910✔
48
         return std::make_unique<Supported_Groups>(reader, size);
2,910✔
49

50
      case Extension_Code::CertificateStatusRequest:
2,677✔
51
         return std::make_unique<Certificate_Status_Request>(reader, size, message_type, from);
2,677✔
52

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

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

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

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

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

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

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

74
      case Extension_Code::SupportedVersions:
1,827✔
75
         return std::make_unique<Supported_Versions>(reader, size, from);
1,827✔
76

77
#if defined(BOTAN_HAS_TLS_12)
78
      case Extension_Code::EcPointFormats:
3,203✔
79
         return std::make_unique<Supported_Point_Formats>(reader, size);
3,203✔
80

81
      case Extension_Code::SafeRenegotiation:
6,220✔
82
         return std::make_unique<Renegotiation_Extension>(reader, size);
6,220✔
83

84
      case Extension_Code::ExtendedMasterSecret:
6,024✔
85
         return std::make_unique<Extended_Master_Secret>(reader, size);
6,024✔
86

87
      case Extension_Code::EncryptThenMac:
822✔
88
         return std::make_unique<Encrypt_then_MAC>(reader, size);
822✔
89

90
      case Extension_Code::SessionTicket:
5,499✔
91
         return std::make_unique<Session_Ticket_Extension>(reader, size);
5,499✔
92
#else
93
      case Extension_Code::EcPointFormats:
94
      case Extension_Code::SafeRenegotiation:
95
      case Extension_Code::ExtendedMasterSecret:
96
      case Extension_Code::EncryptThenMac:
97
      case Extension_Code::SessionTicket:
98
         break;  // considered as 'unknown extension'
99
#endif
100

101
#if defined(BOTAN_HAS_TLS_13)
102
      case Extension_Code::PresharedKey:
232✔
103
         return std::make_unique<PSK>(reader, size, message_type);
232✔
104

105
      case Extension_Code::EarlyData:
5✔
106
         return std::make_unique<EarlyDataIndication>(reader, size, message_type);
5✔
107

108
      case Extension_Code::Cookie:
10✔
109
         return std::make_unique<Cookie>(reader, size);
10✔
110

111
      case Extension_Code::PskKeyExchangeModes:
1,072✔
112
         return std::make_unique<PSK_Key_Exchange_Modes>(reader, size);
1,072✔
113

114
      case Extension_Code::CertificateAuthorities:
3✔
115
         return std::make_unique<Certificate_Authorities>(reader, size);
3✔
116

117
      case Extension_Code::KeyShare:
1,609✔
118
         return std::make_unique<Key_Share>(reader, size, message_type);
1,609✔
119
#else
120
      case Extension_Code::PresharedKey:
121
      case Extension_Code::EarlyData:
122
      case Extension_Code::Cookie:
123
      case Extension_Code::PskKeyExchangeModes:
124
      case Extension_Code::CertificateAuthorities:
125
      case Extension_Code::KeyShare:
126
         break;  // considered as 'unknown extension'
127
#endif
128
   }
129

130
   return std::make_unique<Unknown_Extension>(code, reader, size);
2,731✔
131
}
132

133
}  // namespace
134

135
Extensions::~Extensions() = default;
23,327✔
136

137
bool Extensions::has(Extension_Code type) const {
127,545✔
138
   return m_extensions.contains(type);
127,545✔
139
}
140

141
Extension* Extensions::get(Extension_Code type) const {
100,007✔
142
   const auto i = m_extensions.find(type);
100,007✔
143

144
   if(i == m_extensions.end()) {
100,007✔
145
      return nullptr;
146
   } else {
147
      return i->second.get();
64,937✔
148
   }
149
}
150

151
void Extensions::add(std::unique_ptr<Extension> extn) {
86,437✔
152
   const auto type = extn->type();
86,437✔
153
   if(has(type)) {
86,437✔
154
      throw Invalid_Argument("cannot add the same extension twice: " + std::to_string(static_cast<uint16_t>(type)));
×
155
   }
156

157
   m_extension_codes.push_back(type);
86,437✔
158
   m_extensions.emplace(type, std::move(extn));
86,437✔
159
}
86,437✔
160

161
void Extensions::deserialize(TLS_Data_Reader& reader, const Connection_Side from, const Handshake_Type message_type) {
8,524✔
162
   if(reader.has_remaining()) {
8,524✔
163
      const uint16_t all_extn_size = reader.get_uint16_t();
8,518✔
164

165
      if(reader.remaining_bytes() != all_extn_size) {
8,517✔
166
         throw Decoding_Error("Bad extension size");
129✔
167
      }
168

169
      while(reader.has_remaining()) {
49,428✔
170
         const uint16_t extension_code = reader.get_uint16_t();
41,110✔
171
         const uint16_t extension_size = reader.get_uint16_t();
41,109✔
172

173
         const auto type = static_cast<Extension_Code>(extension_code);
41,108✔
174

175
         if(this->has(type)) {
41,108✔
176
            throw TLS_Exception(TLS::Alert::DecodeError, "Peer sent duplicated extensions");
19✔
177
         }
178

179
         // TODO offer a function on reader that returns a byte range as a reference
180
         // to avoid this copy of the extension data
181
         const std::vector<uint8_t> extn_data = reader.get_fixed<uint8_t>(extension_size);
41,089✔
182
         TLS_Data_Reader extn_reader("Extension", extn_data);
41,081✔
183
         this->add(make_extension(extn_reader, type, from, message_type));
41,081✔
184
         extn_reader.assert_done();
41,040✔
185
      }
41,040✔
186
   }
187
}
8,324✔
188

189
bool Extensions::contains_other_than(const std::set<Extension_Code>& allowed_extensions,
3,438✔
190
                                     const bool allow_unknown_extensions) const {
191
   const auto found = extension_types();
3,438✔
192

193
   std::vector<Extension_Code> diff;
3,438✔
194
   std::set_difference(
3,438✔
195
      found.cbegin(), found.end(), allowed_extensions.cbegin(), allowed_extensions.cend(), std::back_inserter(diff));
196

197
   if(allow_unknown_extensions) {
3,438✔
198
      // Go through the found unexpected extensions whether any of those
199
      // is known to this TLS implementation.
200
      const auto itr = std::find_if(diff.cbegin(), diff.cend(), [this](const auto ext_type) {
1,523✔
201
         const auto ext = get(ext_type);
10✔
202
         return ext && ext->is_implemented();
10✔
203
      });
204

205
      // ... if yes, `contains_other_than` is true
206
      return itr != diff.cend();
1,523✔
207
   }
208

209
   return !diff.empty();
1,915✔
210
}
3,438✔
211

212
bool Extensions::remove_extension(Extension_Code type) {
137✔
213
   auto i = m_extensions.find(type);
137✔
214

215
   if(i == m_extensions.end()) {
137✔
216
      return false;
217
   } else {
218
      m_extensions.erase(i);
137✔
219
      std::erase(m_extension_codes, type);
137✔
220
      return true;
137✔
221
   }
222
}
223

224
std::vector<uint8_t> Extensions::serialize(Connection_Side whoami) const {
6,542✔
225
   std::vector<uint8_t> buf(2);  // 2 bytes for length field
6,542✔
226

227
   // Serialize in the order extensions were added, which matters for TLS 1.3
228
   for(const auto extn_type : m_extension_codes) {
59,055✔
229
      const auto& extn = m_extensions.at(extn_type);
52,513✔
230

231
      if(extn->empty()) {
52,513✔
232
         continue;
3,120✔
233
      }
234

235
      const uint16_t extn_code = static_cast<uint16_t>(extn_type);
49,393✔
236

237
      const std::vector<uint8_t> extn_val = extn->serialize(whoami);
49,393✔
238

239
      buf.push_back(get_byte<0>(extn_code));
49,393✔
240
      buf.push_back(get_byte<1>(extn_code));
49,393✔
241

242
      buf.push_back(get_byte<0>(static_cast<uint16_t>(extn_val.size())));
49,393✔
243
      buf.push_back(get_byte<1>(static_cast<uint16_t>(extn_val.size())));
49,393✔
244

245
      buf += extn_val;
49,393✔
246
   }
49,393✔
247

248
   const uint16_t extn_size = static_cast<uint16_t>(buf.size() - 2);
6,542✔
249

250
   buf[0] = get_byte<0>(extn_size);
6,542✔
251
   buf[1] = get_byte<1>(extn_size);
6,542✔
252

253
   // avoid sending a completely empty extensions block
254
   if(buf.size() == 2) {
6,542✔
255
      return std::vector<uint8_t>();
304✔
256
   }
257

258
   return buf;
6,238✔
259
}
6,542✔
260

261
std::set<Extension_Code> Extensions::extension_types() const {
11,406✔
262
   std::set<Extension_Code> offers;
11,406✔
263
   for(const auto& [extn_type, extn] : m_extensions) {
76,870✔
264
      offers.insert(extn_type);
65,464✔
265
   }
266
   return offers;
11,406✔
267
}
×
268

269
void Extensions::reorder(const std::vector<Extension_Code>& order) {
41✔
270
   std::set<Extension_Code> in_order(order.begin(), order.end());
41✔
271

272
   std::vector<Extension_Code> new_codes;
41✔
273
   new_codes.reserve(m_extension_codes.size());
41✔
274

275
   // First: extensions not mentioned in the order (preserving their relative order)
276
   for(auto code : m_extension_codes) {
321✔
277
      if(!in_order.contains(code)) {
560✔
278
         new_codes.push_back(code);
154✔
279
      }
280
   }
281

282
   // Then: extensions in the specified order
283
   for(auto code : order) {
305✔
284
      if(m_extensions.contains(code)) {
528✔
285
         new_codes.push_back(code);
126✔
286
      }
287
   }
288

289
   m_extension_codes = std::move(new_codes);
41✔
290
}
41✔
291

292
Unknown_Extension::Unknown_Extension(Extension_Code type, TLS_Data_Reader& reader, uint16_t extension_size) :
2,731✔
293
      m_type(type), m_value(reader.get_fixed<uint8_t>(extension_size)) {}
2,731✔
294

295
std::vector<uint8_t> Unknown_Extension::serialize(Connection_Side /*whoami*/) const {
2✔
296
   return m_value;
2✔
297
}
298

299
Server_Name_Indicator::Server_Name_Indicator(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
2,831✔
300
   /*
301
   RFC 6066 Section 3
302

303
      A server that receives a client hello containing the "server_name"
304
      extension MAY use the information contained in the extension to guide
305
      its selection of an appropriate certificate to return to the client,
306
      and/or other aspects of security policy.  In this event, the server
307
      SHALL include an extension of type "server_name" in the (extended)
308
      server hello.  The "extension_data" field of this extension SHALL be
309
      empty.
310
   */
311
   if(from == Connection_Side::Server) {
2,831✔
312
      if(extension_size != 0) {
36✔
313
         throw TLS_Exception(Alert::IllegalParameter, "Server sent non-empty SNI extension");
×
314
      }
315
   } else {
316
      // Clients are required to send at least one name in the SNI
317
      if(extension_size == 0) {
2,795✔
318
         throw TLS_Exception(Alert::IllegalParameter, "Client sent empty SNI extension");
×
319
      }
320

321
      const uint16_t name_bytes = reader.get_uint16_t();
2,795✔
322

323
      if(name_bytes + 2 != extension_size || name_bytes < 3) {
2,795✔
324
         throw Decoding_Error("Bad encoding of SNI extension");
×
325
      }
326

327
      BOTAN_ASSERT_NOMSG(reader.remaining_bytes() == name_bytes);
2,795✔
328

329
      while(reader.has_remaining()) {
5,590✔
330
         const uint8_t name_type = reader.get_byte();
2,795✔
331

332
         if(name_type == 0) {
2,795✔
333
            /*
334
            RFC 6066 Section 3
335
               The ServerNameList MUST NOT contain more than one name of the same name_type.
336
            */
337
            if(!m_sni_host_name.empty()) {
2,795✔
338
               throw Decoding_Error("TLS ServerNameIndicator contains more than one host_name");
×
339
            }
340
            m_sni_host_name = reader.get_string(2, 1, 65535);
2,795✔
341
         } else {
342
            /*
343
            Unknown name type - skip its length-prefixed value and continue
344

345
            RFC 6066 Section 3
346
               For backward compatibility, all future data structures associated
347
               with new NameTypes MUST begin with a 16-bit length field.
348
            */
349
            const uint16_t unknown_name_len = reader.get_uint16_t();
×
350
            reader.discard_next(unknown_name_len);
×
351
         }
352
      }
353
   }
354
}
2,831✔
355

356
std::vector<uint8_t> Server_Name_Indicator::serialize(Connection_Side whoami) const {
4,657✔
357
   // RFC 6066
358
   //    [...] the server SHALL include an extension of type "server_name" in
359
   //    the (extended) server hello. The "extension_data" field of this
360
   //    extension SHALL be empty.
361
   if(whoami == Connection_Side::Server) {
4,657✔
362
      return {};
366✔
363
   }
364

365
   std::vector<uint8_t> buf;
4,291✔
366

367
   const size_t name_len = m_sni_host_name.size();
4,291✔
368

369
   buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len + 3)));
4,291✔
370
   buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len + 3)));
4,291✔
371
   buf.push_back(0);  // DNS
4,291✔
372

373
   buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len)));
4,291✔
374
   buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len)));
4,291✔
375

376
   buf += as_span_of_bytes(m_sni_host_name);
4,291✔
377

378
   return buf;
4,291✔
379
}
4,291✔
380

381
bool Server_Name_Indicator::hostname_acceptable_for_sni(std::string_view hostname) {
3,685✔
382
   // Avoid sending an IPv4/IPv6 address in SNI as this is prohibited
383

384
   if(hostname.empty()) {
3,685✔
385
      return false;
386
   }
387

388
   if(string_to_ipv4(hostname).has_value()) {
3,638✔
389
      return false;
390
   }
391

392
   // IPv6? Anyway ':' is not valid in DNS
393
   if(hostname.find(':') != std::string_view::npos) {
3,638✔
394
      return false;
395
   }
396

397
   return true;
398
}
399

400
Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification(TLS_Data_Reader& reader,
411✔
401
                                                                                 uint16_t extension_size,
402
                                                                                 Connection_Side from) {
411✔
403
   if(extension_size == 0) {
411✔
404
      return;  // empty extension
405
   }
406

407
   const uint16_t name_bytes = reader.get_uint16_t();
411✔
408

409
   size_t bytes_remaining = extension_size - 2;
411✔
410

411
   if(name_bytes != bytes_remaining) {
411✔
412
      throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
2✔
413
   }
414

415
   while(bytes_remaining > 0) {
1,090✔
416
      const std::string p = reader.get_string(1, 0, 255);
688✔
417

418
      if(bytes_remaining < p.size() + 1) {
688✔
419
         throw Decoding_Error("Bad encoding of ALPN, length field too long");
×
420
      }
421

422
      if(p.empty()) {
688✔
423
         throw Decoding_Error("Empty ALPN protocol not allowed");
7✔
424
      }
425

426
      bytes_remaining -= (p.size() + 1);
681✔
427

428
      m_protocols.push_back(p);
681✔
429
   }
688✔
430

431
   // RFC 7301 3.1
432
   //    The "extension_data" field of the [...] extension is structured the
433
   //    same as described above for the client "extension_data", except that
434
   //    the "ProtocolNameList" MUST contain exactly one "ProtocolName".
435
   if(from == Connection_Side::Server && m_protocols.size() != 1) {
402✔
436
      throw TLS_Exception(
×
437
         Alert::DecodeError,
438
         "Server sent " + std::to_string(m_protocols.size()) + " protocols in ALPN extension response");
×
439
   }
440
}
9✔
441

442
std::string Application_Layer_Protocol_Notification::single_protocol() const {
149✔
443
   BOTAN_STATE_CHECK(m_protocols.size() == 1);
149✔
444
   return m_protocols.front();
149✔
445
}
446

447
std::vector<uint8_t> Application_Layer_Protocol_Notification::serialize(Connection_Side /*whoami*/) const {
367✔
448
   std::vector<uint8_t> buf(2);
367✔
449

450
   for(auto&& proto : m_protocols) {
963✔
451
      if(proto.length() >= 256) {
596✔
452
         throw TLS_Exception(Alert::InternalError, "ALPN name too long");
×
453
      }
454
      if(!proto.empty()) {
596✔
455
         append_tls_length_value(buf, proto, 1);
1,192✔
456
      }
457
   }
458

459
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
367✔
460
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
367✔
461

462
   return buf;
367✔
463
}
×
464

465
Certificate_Type_Base::Certificate_Type_Base(std::vector<Certificate_Type> supported_cert_types) :
1,976✔
466
      m_certificate_types(std::move(supported_cert_types)), m_from(Connection_Side::Client) {
1,976✔
467
   BOTAN_ARG_CHECK(!m_certificate_types.empty(), "at least one certificate type must be supported");
1,976✔
468
}
1,976✔
469

470
Client_Certificate_Type::Client_Certificate_Type(const Client_Certificate_Type& cct, const Policy& policy) :
1✔
471
      Certificate_Type_Base(cct, policy.accepted_client_certificate_types()) {}
1✔
472

473
Server_Certificate_Type::Server_Certificate_Type(const Server_Certificate_Type& sct, const Policy& policy) :
1✔
474
      Certificate_Type_Base(sct, policy.accepted_server_certificate_types()) {}
1✔
475

476
Certificate_Type_Base::Certificate_Type_Base(const Certificate_Type_Base& certificate_type_from_client,
2✔
477
                                             const std::vector<Certificate_Type>& server_preference) :
2✔
478
      m_from(Connection_Side::Server) {
2✔
479
   // RFC 7250 4.2
480
   //    The server_certificate_type extension in the client hello indicates the
481
   //    types of certificates the client is able to process when provided by
482
   //    the server in a subsequent certificate payload. [...] With the
483
   //    server_certificate_type extension in the server hello, the TLS server
484
   //    indicates the certificate type carried in the Certificate payload.
485
   for(const auto server_supported_cert_type : server_preference) {
2✔
486
      if(value_exists(certificate_type_from_client.m_certificate_types, server_supported_cert_type)) {
4✔
487
         m_certificate_types.push_back(server_supported_cert_type);
2✔
488
         return;
2✔
489
      }
490
   }
491

492
   // RFC 7250 4.2 (2.)
493
   //    The server supports the extension defined in this document, but
494
   //    it does not have any certificate type in common with the client.
495
   //    Then, the server terminates the session with a fatal alert of
496
   //    type "unsupported_certificate".
497
   throw TLS_Exception(Alert::UnsupportedCertificate, "Failed to agree on certificate_type");
×
498
}
×
499

500
Certificate_Type_Base::Certificate_Type_Base(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) :
4✔
501
      m_from(from) {
4✔
502
   if(extension_size == 0) {
4✔
503
      throw Decoding_Error("Certificate type extension cannot be empty");
×
504
   }
505

506
   if(from == Connection_Side::Client) {
4✔
507
      const auto type_bytes = reader.get_tls_length_value(1);
2✔
508
      if(static_cast<size_t>(extension_size) != type_bytes.size() + 1) {
2✔
509
         throw Decoding_Error("certificate type extension had inconsistent length");
×
510
      }
511
      std::transform(
2✔
512
         type_bytes.begin(), type_bytes.end(), std::back_inserter(m_certificate_types), [](const auto type_byte) {
2✔
513
            return static_cast<Certificate_Type>(type_byte);
514
         });
515
   } else {
2✔
516
      // RFC 7250 4.2
517
      //    Note that only a single value is permitted in the
518
      //    server_certificate_type extension when carried in the server hello.
519
      if(extension_size != 1) {
2✔
520
         throw Decoding_Error("Server's certificate type extension must be of length 1");
×
521
      }
522
      const auto type_byte = reader.get_byte();
2✔
523
      m_certificate_types.push_back(static_cast<Certificate_Type>(type_byte));
2✔
524
   }
525
}
4✔
526

527
std::vector<uint8_t> Certificate_Type_Base::serialize(Connection_Side whoami) const {
12✔
528
   std::vector<uint8_t> result;
12✔
529
   if(whoami == Connection_Side::Client) {
12✔
530
      std::vector<uint8_t> type_bytes;
6✔
531
      std::transform(
6✔
532
         m_certificate_types.begin(), m_certificate_types.end(), std::back_inserter(type_bytes), [](const auto type) {
533
            return static_cast<uint8_t>(type);
534
         });
535
      append_tls_length_value(result, type_bytes, 1);
12✔
536
   } else {
6✔
537
      BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
6✔
538
      result.push_back(static_cast<uint8_t>(m_certificate_types.front()));
6✔
539
   }
540
   return result;
12✔
541
}
×
542

543
void Certificate_Type_Base::validate_selection(const Certificate_Type_Base& from_server) const {
2✔
544
   BOTAN_ASSERT_NOMSG(m_from == Connection_Side::Client);
2✔
545
   BOTAN_ASSERT_NOMSG(from_server.m_from == Connection_Side::Server);
2✔
546

547
   // RFC 7250 4.2
548
   //    The value conveyed in the [client_]certificate_type extension MUST be
549
   //    selected from one of the values provided in the [client_]certificate_type
550
   //    extension sent in the client hello.
551
   if(!value_exists(m_certificate_types, from_server.selected_certificate_type())) {
4✔
552
      throw TLS_Exception(Alert::IllegalParameter,
×
553
                          Botan::fmt("Selected certificate type was not offered: {}",
×
554
                                     certificate_type_to_string(from_server.selected_certificate_type())));
×
555
   }
556
}
2✔
557

558
Certificate_Type Certificate_Type_Base::selected_certificate_type() const {
6✔
559
   BOTAN_ASSERT_NOMSG(m_from == Connection_Side::Server);
6✔
560
   BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
6✔
561
   return m_certificate_types.front();
6✔
562
}
563

564
Supported_Groups::Supported_Groups(const std::vector<Group_Params>& groups) : m_groups(groups) {}
4,048✔
565

566
const std::vector<Group_Params>& Supported_Groups::groups() const {
948✔
567
   return m_groups;
948✔
568
}
569

570
std::vector<Group_Params> Supported_Groups::ec_groups() const {
5,028✔
571
   std::vector<Group_Params> ec;
5,028✔
572
   for(auto g : m_groups) {
52,153✔
573
      if(g.is_pure_ecc_group()) {
94,250✔
574
         ec.push_back(g);
34,729✔
575
      }
576
   }
577
   return ec;
5,028✔
578
}
×
579

580
std::vector<Group_Params> Supported_Groups::dh_groups() const {
1,512✔
581
   std::vector<Group_Params> dh;
1,512✔
582
   for(auto g : m_groups) {
11,154✔
583
      if(g.is_in_ffdhe_range()) {
12,489✔
584
         dh.push_back(g);
555✔
585
      }
586
   }
587
   return dh;
1,512✔
588
}
×
589

590
std::vector<uint8_t> Supported_Groups::serialize(Connection_Side /*whoami*/) const {
4,725✔
591
   std::vector<uint8_t> buf(2);
4,725✔
592

593
   for(auto g : m_groups) {
54,934✔
594
      const uint16_t id = g.wire_code();
50,209✔
595

596
      if(id > 0) {
50,209✔
597
         buf.push_back(get_byte<0>(id));
50,209✔
598
         buf.push_back(get_byte<1>(id));
50,209✔
599
      }
600
   }
601

602
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
4,725✔
603
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
4,725✔
604

605
   return buf;
4,725✔
606
}
×
607

608
Supported_Groups::Supported_Groups(TLS_Data_Reader& reader, uint16_t extension_size) {
2,912✔
609
   const uint16_t len = reader.get_uint16_t();
2,912✔
610

611
   if(len + 2 != extension_size) {
2,912✔
612
      throw Decoding_Error("Inconsistent length field in supported groups list");
4✔
613
   }
614

615
   if(len % 2 == 1) {
2,908✔
616
      throw Decoding_Error("Supported groups list of strange size");
×
617
   }
618

619
   const size_t elems = len / 2;
2,908✔
620

621
   std::unordered_set<uint16_t> seen;
2,908✔
622
   for(size_t i = 0; i != elems; ++i) {
23,924✔
623
      const auto group = static_cast<Group_Params>(reader.get_uint16_t());
21,016✔
624
      // Note: RFC 8446 does not explicitly enforce that groups must be unique.
625
      if(seen.insert(group.wire_code()).second) {
21,016✔
626
         m_groups.push_back(group);
21,016✔
627
      }
628
   }
629
}
2,912✔
630

631
namespace {
632

633
std::vector<uint8_t> serialize_signature_algorithms(const std::vector<Signature_Scheme>& schemes) {
4,730✔
634
   BOTAN_ASSERT(schemes.size() < 256, "Too many signature schemes");
4,730✔
635

636
   std::vector<uint8_t> buf;
4,730✔
637

638
   const uint16_t len = static_cast<uint16_t>(schemes.size() * 2);
4,730✔
639

640
   buf.push_back(get_byte<0>(len));
4,730✔
641
   buf.push_back(get_byte<1>(len));
4,730✔
642

643
   for(const Signature_Scheme scheme : schemes) {
46,341✔
644
      buf.push_back(get_byte<0>(scheme.wire_code()));
41,611✔
645
      buf.push_back(get_byte<1>(scheme.wire_code()));
41,611✔
646
   }
647

648
   return buf;
4,730✔
649
}
×
650

651
std::vector<Signature_Scheme> parse_signature_algorithms(TLS_Data_Reader& reader, uint16_t extension_size) {
2,938✔
652
   uint16_t len = reader.get_uint16_t();
2,938✔
653

654
   if(len + 2 != extension_size || len % 2 == 1 || len == 0) {
2,936✔
655
      throw Decoding_Error("Bad encoding on signature algorithms extension");
1✔
656
   }
657

658
   std::vector<Signature_Scheme> schemes;
2,935✔
659
   schemes.reserve(len / 2);
2,935✔
660
   while(len > 0) {
30,792✔
661
      schemes.emplace_back(reader.get_uint16_t());
27,857✔
662
      len -= 2;
27,857✔
663
   }
664

665
   return schemes;
2,935✔
666
}
×
667

668
}  // namespace
669

670
std::vector<uint8_t> Signature_Algorithms::serialize(Connection_Side /*whoami*/) const {
4,728✔
671
   return serialize_signature_algorithms(m_schemes);
4,728✔
672
}
673

674
Signature_Algorithms::Signature_Algorithms(TLS_Data_Reader& reader, uint16_t extension_size) :
2,936✔
675
      m_schemes(parse_signature_algorithms(reader, extension_size)) {}
2,936✔
676

677
std::vector<uint8_t> Signature_Algorithms_Cert::serialize(Connection_Side /*whoami*/) const {
2✔
678
   return serialize_signature_algorithms(m_schemes);
2✔
679
}
680

681
Signature_Algorithms_Cert::Signature_Algorithms_Cert(TLS_Data_Reader& reader, uint16_t extension_size) :
2✔
682
      m_schemes(parse_signature_algorithms(reader, extension_size)) {}
2✔
683

684
SRTP_Protection_Profiles::SRTP_Protection_Profiles(TLS_Data_Reader& reader, uint16_t extension_size) :
10✔
685
      m_pp(reader.get_range<uint16_t>(2, 0, 65535)) {
10✔
686
   const std::vector<uint8_t> mki = reader.get_range<uint8_t>(1, 0, 255);
9✔
687

688
   if(m_pp.size() * 2 + mki.size() + 3 != extension_size) {
9✔
689
      throw Decoding_Error("Bad encoding for SRTP protection extension");
×
690
   }
691

692
   if(!mki.empty()) {
9✔
693
      throw Decoding_Error("Unhandled non-empty MKI for SRTP protection extension");
×
694
   }
695
}
9✔
696

697
std::vector<uint8_t> SRTP_Protection_Profiles::serialize(Connection_Side /*whoami*/) const {
5✔
698
   std::vector<uint8_t> buf;
5✔
699

700
   const uint16_t pp_len = static_cast<uint16_t>(m_pp.size() * 2);
5✔
701
   buf.push_back(get_byte<0>(pp_len));
5✔
702
   buf.push_back(get_byte<1>(pp_len));
5✔
703

704
   for(const uint16_t pp : m_pp) {
12✔
705
      buf.push_back(get_byte<0>(pp));
7✔
706
      buf.push_back(get_byte<1>(pp));
7✔
707
   }
708

709
   buf.push_back(0);  // srtp_mki, always empty here
5✔
710

711
   return buf;
5✔
712
}
×
713

714
std::vector<uint8_t> Supported_Versions::serialize(Connection_Side whoami) const {
4,483✔
715
   std::vector<uint8_t> buf;
4,483✔
716

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

725
      buf.push_back(len);
4,054✔
726

727
      for(const Protocol_Version version : m_versions) {
9,226✔
728
         buf.push_back(version.major_version());
5,172✔
729
         buf.push_back(version.minor_version());
5,172✔
730
      }
731
   }
732

733
   return buf;
4,483✔
734
}
×
735

736
Supported_Versions::Supported_Versions(Protocol_Version offer, const Policy& policy) {
3,473✔
737
   // RFC 8446 4.2.1
738
   //    The extension contains a list of supported versions in preference order,
739
   //    with the most preferred version first. Implementations [...] MUST send
740
   //    this extension in the ClientHello containing all versions of TLS which
741
   //    they are prepared to negotiate.
742
   //
743
   // We simply assume that we always want the newest available TLS version.
744
#if defined(BOTAN_HAS_TLS_13)
745
   if(!offer.is_datagram_protocol()) {
3,473✔
746
      if(offer >= Protocol_Version::TLS_V13 && policy.allow_tls13()) {
5,158✔
747
         m_versions.push_back(Protocol_Version::TLS_V13);
988✔
748
      }
749
   }
750
#endif
751

752
#if defined(BOTAN_HAS_TLS_12)
753
   if(offer.is_datagram_protocol()) {
3,473✔
754
      if(offer >= Protocol_Version::DTLS_V12 && policy.allow_dtls12()) {
400✔
755
         m_versions.push_back(Protocol_Version::DTLS_V12);
400✔
756
      }
757
   } else {
758
      if(offer >= Protocol_Version::TLS_V12 && policy.allow_tls12()) {
4,061✔
759
         m_versions.push_back(Protocol_Version::TLS_V12);
3,044✔
760
      }
761
   }
762
#endif
763

764
   // if no versions are supported, the input variables are not used
765
   BOTAN_UNUSED(offer, policy);
3,473✔
766
}
3,473✔
767

768
Supported_Versions::Supported_Versions(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
1,829✔
769
   if(from == Connection_Side::Server) {
1,829✔
770
      if(extension_size != 2) {
542✔
771
         throw Decoding_Error("Server sent invalid supported_versions extension");
1✔
772
      }
773
      m_versions.push_back(Protocol_Version(reader.get_uint16_t()));
541✔
774
   } else {
775
      auto versions = reader.get_range<uint16_t>(1, 1, 127);
1,287✔
776

777
      for(auto v : versions) {
4,563✔
778
         m_versions.push_back(Protocol_Version(v));
3,276✔
779
      }
780

781
      if(extension_size != 1 + 2 * versions.size()) {
1,287✔
782
         throw Decoding_Error("Client sent invalid supported_versions extension");
×
783
      }
784
   }
1,287✔
785
}
1,829✔
786

787
bool Supported_Versions::supports(Protocol_Version version) const {
1,486✔
788
   for(auto v : m_versions) {
1,997✔
789
      if(version == v) {
1,978✔
790
         return true;
1,486✔
791
      }
792
   }
793
   return false;
794
}
795

796
Record_Size_Limit::Record_Size_Limit(const uint16_t limit) : m_limit(limit) {
16✔
797
   BOTAN_ASSERT(limit >= 64, "RFC 8449 does not allow record size limits smaller than 64 bytes");
16✔
798
   BOTAN_ASSERT(limit <= MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */,
16✔
799
                "RFC 8449 does not allow record size limits larger than 2^14+1");
800
}
16✔
801

802
Record_Size_Limit::Record_Size_Limit(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
45✔
803
   if(extension_size != 2) {
45✔
804
      throw TLS_Exception(Alert::DecodeError, "invalid record_size_limit extension");
×
805
   }
806

807
   m_limit = reader.get_uint16_t();
45✔
808

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

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

836
std::vector<uint8_t> Record_Size_Limit::serialize(Connection_Side /*whoami*/) const {
53✔
837
   std::vector<uint8_t> buf;
53✔
838

839
   buf.push_back(get_byte<0>(m_limit));
53✔
840
   buf.push_back(get_byte<1>(m_limit));
53✔
841

842
   return buf;
53✔
843
}
×
844

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