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

randombit / botan / 26141725099

19 May 2026 08:32PM UTC coverage: 89.343% (+0.009%) from 89.334%
26141725099

push

github

web-flow
Merge pull request #5609 from randombit/jack/improve-http

Improve the HTTP 1.0 client used for OCSP/CRL

109341 of 122383 relevant lines covered (89.34%)

11264402.07 hits per line

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

92.27
/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/dns_name.h>
16
#include <botan/ipv4_address.h>
17
#include <botan/ipv6_address.h>
18
#include <botan/tls_exceptn.h>
19
#include <botan/tls_policy.h>
20
#include <botan/internal/fmt.h>
21
#include <botan/internal/parsing.h>
22
#include <botan/internal/stl_util.h>
23
#include <botan/internal/tls_reader.h>
24
#include <algorithm>
25
#include <unordered_set>
26

27
#if defined(BOTAN_HAS_TLS_13)
28
   #include <botan/tls_extensions_13.h>
29
#endif
30

31
#if defined(BOTAN_HAS_TLS_12)
32
   #include <botan/tls_extensions_12.h>
33
#endif
34

35
namespace Botan::TLS {
36

37
namespace {
38

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

50
      case Extension_Code::SupportedGroups:
3,226✔
51
         return std::make_unique<Supported_Groups>(reader, size);
3,226✔
52

53
      case Extension_Code::CertificateStatusRequest:
3,010✔
54
         return std::make_unique<Certificate_Status_Request>(reader, size, message_type, from);
3,010✔
55

56
      case Extension_Code::SignatureAlgorithms:
3,265✔
57
         return std::make_unique<Signature_Algorithms>(reader, size);
3,265✔
58

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

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

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

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

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

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

77
      case Extension_Code::SupportedVersions:
2,261✔
78
         return std::make_unique<Supported_Versions>(reader, size, from);
2,261✔
79

80
      case Extension_Code::Padding:
81
         break;  // RFC 7685, recognized but not implemented; falls through to Unknown_Extension
82

83
#if defined(BOTAN_HAS_TLS_12)
84
      case Extension_Code::EcPointFormats:
3,525✔
85
         return std::make_unique<Supported_Point_Formats>(reader, size);
3,525✔
86

87
      case Extension_Code::SafeRenegotiation:
6,598✔
88
         return std::make_unique<Renegotiation_Extension>(reader, size);
6,598✔
89

90
      case Extension_Code::ExtendedMasterSecret:
6,402✔
91
         return std::make_unique<Extended_Master_Secret>(reader, size);
6,402✔
92

93
      case Extension_Code::EncryptThenMac:
816✔
94
         return std::make_unique<Encrypt_then_MAC>(reader, size);
816✔
95

96
      case Extension_Code::SessionTicket:
5,853✔
97
         return std::make_unique<Session_Ticket_Extension>(reader, size, from);
5,853✔
98
#else
99
      case Extension_Code::EcPointFormats:
100
      case Extension_Code::SafeRenegotiation:
101
      case Extension_Code::ExtendedMasterSecret:
102
      case Extension_Code::EncryptThenMac:
103
      case Extension_Code::SessionTicket:
104
         break;  // considered as 'unknown extension'
105
#endif
106

107
#if defined(BOTAN_HAS_TLS_13)
108
      case Extension_Code::PresharedKey:
379✔
109
         return std::make_unique<PSK>(reader, size, message_type);
379✔
110

111
      case Extension_Code::EarlyData:
5✔
112
         return std::make_unique<EarlyDataIndication>(reader, size, message_type);
5✔
113

114
      case Extension_Code::Cookie:
19✔
115
         return std::make_unique<Cookie>(reader, size);
19✔
116

117
      case Extension_Code::PskKeyExchangeModes:
1,410✔
118
         return std::make_unique<PSK_Key_Exchange_Modes>(reader, size);
1,410✔
119

120
      case Extension_Code::CertificateAuthorities:
7✔
121
         return std::make_unique<Certificate_Authorities>(reader, size);
7✔
122

123
      case Extension_Code::KeyShare:
2,038✔
124
         return std::make_unique<Key_Share>(reader, size, message_type);
2,038✔
125
#else
126
      case Extension_Code::PresharedKey:
127
      case Extension_Code::EarlyData:
128
      case Extension_Code::Cookie:
129
      case Extension_Code::PskKeyExchangeModes:
130
      case Extension_Code::CertificateAuthorities:
131
      case Extension_Code::KeyShare:
132
         break;  // considered as 'unknown extension'
133
#endif
134
   }
135

136
   return std::make_unique<Unknown_Extension>(code, reader, size);
3,060✔
137
}
138

139
}  // namespace
140

141
Extensions::~Extensions() = default;
24,935✔
142

143
bool Extensions::has(Extension_Code type) const {
137,291✔
144
   return m_extensions.contains(type);
137,291✔
145
}
146

147
Extension* Extensions::get(Extension_Code type) const {
128,014✔
148
   const auto i = m_extensions.find(type);
128,014✔
149

150
   if(i == m_extensions.end()) {
128,014✔
151
      return nullptr;
152
   } else {
153
      return i->second.get();
86,163✔
154
   }
155
}
156

157
void Extensions::add(std::unique_ptr<Extension> extn) {
91,630✔
158
   const auto type = extn->type();
91,630✔
159
   if(has(type)) {
91,630✔
160
      throw Invalid_Argument("cannot add the same extension twice: " + std::to_string(static_cast<uint16_t>(type)));
×
161
   }
162

163
   m_extension_codes.push_back(type);
91,630✔
164
   m_extensions.emplace(type, std::move(extn));
91,630✔
165
}
91,630✔
166

167
void Extensions::deserialize(TLS_Data_Reader& reader, const Connection_Side from, const Handshake_Type message_type) {
9,350✔
168
   if(reader.has_remaining()) {
9,350✔
169
      const uint16_t all_extn_size = reader.get_uint16_t();
9,344✔
170

171
      if(reader.remaining_bytes() != all_extn_size) {
9,343✔
172
         throw Decoding_Error("Bad extension size");
131✔
173
      }
174

175
      while(reader.has_remaining()) {
54,780✔
176
         const uint16_t extension_code = reader.get_uint16_t();
45,662✔
177
         const uint16_t extension_size = reader.get_uint16_t();
45,662✔
178

179
         const auto type = static_cast<Extension_Code>(extension_code);
45,661✔
180

181
         if(this->has(type)) {
45,661✔
182
            throw TLS_Exception(TLS::Alert::DecodeError, "Peer sent duplicated extensions");
19✔
183
         }
184

185
         // TODO offer a function on reader that returns a byte range as a reference
186
         // to avoid this copy of the extension data
187
         const std::vector<uint8_t> extn_data = reader.get_fixed<uint8_t>(extension_size);
45,642✔
188
         m_raw_extension_data[type] = extn_data;
45,634✔
189
         TLS_Data_Reader extn_reader("Extension", extn_data);
45,634✔
190
         this->add(make_extension(extn_reader, type, from, message_type));
45,634✔
191
         extn_reader.assert_done();
45,568✔
192
      }
45,568✔
193
   }
194
}
9,124✔
195

196
bool Extensions::contains_other_than(const std::set<Extension_Code>& allowed_extensions,
4,135✔
197
                                     const bool allow_unknown_extensions) const {
198
   const auto found = extension_types();
4,135✔
199

200
   std::vector<Extension_Code> diff;
4,135✔
201
   std::set_difference(
4,135✔
202
      found.cbegin(), found.end(), allowed_extensions.cbegin(), allowed_extensions.cend(), std::back_inserter(diff));
203

204
   if(allow_unknown_extensions) {
4,135✔
205
      // Go through the found unexpected extensions whether any of those
206
      // is known to this TLS implementation.
207
      const auto itr = std::find_if(diff.cbegin(), diff.cend(), [this](const auto ext_type) {
1,858✔
208
         const auto ext = get(ext_type);
22✔
209
         return ext && ext->is_implemented();
22✔
210
      });
211

212
      // ... if yes, `contains_other_than` is true
213
      return itr != diff.cend();
1,858✔
214
   }
215

216
   return !diff.empty();
2,277✔
217
}
4,135✔
218

219
bool Extensions::remove_extension(Extension_Code type) {
133✔
220
   auto i = m_extensions.find(type);
133✔
221

222
   if(i == m_extensions.end()) {
133✔
223
      return false;
224
   } else {
225
      m_extensions.erase(i);
133✔
226
      std::erase(m_extension_codes, type);
133✔
227
      m_raw_extension_data.erase(type);
133✔
228
      return true;
133✔
229
   }
230
}
231

232
std::vector<uint8_t> Extensions::serialize(Connection_Side whoami) const {
7,282✔
233
   std::vector<uint8_t> buf(2);  // 2 bytes for length field
7,282✔
234

235
   // Serialize in the order extensions were added, which matters for TLS 1.3
236
   for(const auto extn_type : m_extension_codes) {
61,265✔
237
      const auto& extn = m_extensions.at(extn_type);
53,983✔
238

239
      if(extn->empty()) {
53,983✔
240
         continue;
1,009✔
241
      }
242

243
      const uint16_t extn_code = static_cast<uint16_t>(extn_type);
52,974✔
244

245
      const std::vector<uint8_t> extn_val = extn->serialize(whoami);
52,974✔
246

247
      // Each extension carries a uint16 length prefix.
248
      BOTAN_ASSERT_NOMSG(extn_val.size() <= 0xFFFF);
52,974✔
249

250
      buf.push_back(get_byte<0>(extn_code));
52,974✔
251
      buf.push_back(get_byte<1>(extn_code));
52,974✔
252

253
      buf.push_back(get_byte<0>(static_cast<uint16_t>(extn_val.size())));
52,974✔
254
      buf.push_back(get_byte<1>(static_cast<uint16_t>(extn_val.size())));
52,974✔
255

256
      buf += extn_val;
52,974✔
257
   }
52,974✔
258

259
   // The outer extensions block is itself uint16-length-prefixed.
260
   BOTAN_ASSERT_NOMSG(buf.size() - 2 <= 0xFFFF);
7,282✔
261
   const uint16_t extn_size = static_cast<uint16_t>(buf.size() - 2);
7,282✔
262

263
   buf[0] = get_byte<0>(extn_size);
7,282✔
264
   buf[1] = get_byte<1>(extn_size);
7,282✔
265

266
   // avoid sending a completely empty extensions block
267
   if(buf.size() == 2) {
7,282✔
268
      return std::vector<uint8_t>();
375✔
269
   }
270

271
   return buf;
6,907✔
272
}
7,282✔
273

274
std::set<Extension_Code> Extensions::extension_types() const {
12,565✔
275
   std::set<Extension_Code> offers;
12,565✔
276
   for(const auto& [extn_type, extn] : m_extensions) {
80,482✔
277
      // Consistent with serialize(): empty extensions are not placed on
278
      // the wire so they must not appear in the "offered" set either.
279
      if(!extn->empty()) {
67,917✔
280
         offers.insert(extn_type);
67,131✔
281
      }
282
   }
283
   return offers;
12,565✔
284
}
×
285

286
void Extensions::reorder(const std::vector<Extension_Code>& order) {
50✔
287
   const std::set<Extension_Code> in_order(order.begin(), order.end());
50✔
288

289
   std::vector<Extension_Code> new_codes;
50✔
290
   new_codes.reserve(m_extension_codes.size());
50✔
291

292
   // First: extensions not mentioned in the order (preserving their relative order)
293
   for(auto code : m_extension_codes) {
426✔
294
      if(!in_order.contains(code)) {
752✔
295
         new_codes.push_back(code);
242✔
296
      }
297
   }
298

299
   // Then: extensions in the specified order. Deduplicate so a caller that
300
   // accidentally lists the same code twice doesn't cause it to be
301
   // serialized twice (which would also break peers that reject duplicate
302
   // extension codes per RFC 8446 4.2 / RFC 5246 7.4.1.4).
303
   std::unordered_set<Extension_Code> already_pushed;
50✔
304
   for(auto code : order) {
323✔
305
      if(m_extensions.contains(code) && already_pushed.insert(code).second) {
680✔
306
         new_codes.push_back(code);
134✔
307
      }
308
   }
309

310
   m_extension_codes = std::move(new_codes);
50✔
311
}
50✔
312

313
Unknown_Extension::Unknown_Extension(Extension_Code type, TLS_Data_Reader& reader, uint16_t extension_size) :
3,060✔
314
      m_type(type), m_value(reader.get_fixed<uint8_t>(extension_size)) {}
3,060✔
315

316
std::vector<uint8_t> Unknown_Extension::serialize(Connection_Side /*whoami*/) const {
2✔
317
   return m_value;
2✔
318
}
319

320
Server_Name_Indicator::Server_Name_Indicator(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
3,153✔
321
   /*
322
   RFC 6066 Section 3
323

324
      A server that receives a client hello containing the "server_name"
325
      extension MAY use the information contained in the extension to guide
326
      its selection of an appropriate certificate to return to the client,
327
      and/or other aspects of security policy.  In this event, the server
328
      SHALL include an extension of type "server_name" in the (extended)
329
      server hello.  The "extension_data" field of this extension SHALL be
330
      empty.
331
   */
332
   if(from == Connection_Side::Server) {
3,153✔
333
      if(extension_size != 0) {
39✔
334
         throw TLS_Exception(Alert::IllegalParameter, "Server sent non-empty SNI extension");
3✔
335
      }
336
   } else {
337
      // Clients are required to send at least one name in the SNI
338
      if(extension_size == 0) {
3,114✔
339
         throw TLS_Exception(Alert::IllegalParameter, "Client sent empty SNI extension");
×
340
      }
341

342
      const uint16_t name_bytes = reader.get_uint16_t();
3,114✔
343

344
      // RFC 6066 3: a ServerName carrying a host_name (the only NameType
345
      // currently defined and the only one this implementation acts on)
346
      // requires at least 1 byte name_type + 2 byte length + 1 byte HostName.
347
      if(name_bytes + 2 != extension_size || name_bytes < 4) {
3,114✔
348
         throw Decoding_Error("Bad encoding of SNI extension");
3✔
349
      }
350

351
      BOTAN_ASSERT_NOMSG(reader.remaining_bytes() == name_bytes);
3,111✔
352

353
      while(reader.has_remaining()) {
6,222✔
354
         const uint8_t name_type = reader.get_byte();
3,111✔
355

356
         if(name_type == 0) {
3,111✔
357
            /*
358
            RFC 6066 Section 3
359
               The ServerNameList MUST NOT contain more than one name of the same name_type.
360
            */
361
            if(!m_sni_host_name.empty()) {
3,111✔
362
               throw Decoding_Error("TLS ServerNameIndicator contains more than one host_name");
×
363
            }
364
            m_sni_host_name = reader.get_string(2, 1, 65535);
3,111✔
365
         } else {
366
            /*
367
            Unknown name type - skip its length-prefixed value and continue
368

369
            RFC 6066 Section 3
370
               For backward compatibility, all future data structures associated
371
               with new NameTypes MUST begin with a 16-bit length field.
372
            */
373
            const uint16_t unknown_name_len = reader.get_uint16_t();
×
374
            reader.discard_next(unknown_name_len);
×
375
         }
376
      }
377
   }
378
}
3,153✔
379

380
std::vector<uint8_t> Server_Name_Indicator::serialize(Connection_Side whoami) const {
4,929✔
381
   // RFC 6066
382
   //    [...] the server SHALL include an extension of type "server_name" in
383
   //    the (extended) server hello. The "extension_data" field of this
384
   //    extension SHALL be empty.
385
   if(whoami == Connection_Side::Server) {
4,929✔
386
      return {};
358✔
387
   }
388

389
   std::vector<uint8_t> buf;
4,571✔
390

391
   const size_t name_len = m_sni_host_name.size();
4,571✔
392

393
   // RFC 6066 3: HostName<1..2^16-1>; the outer ServerNameList wraps a
394
   // 1-byte name_type and a 2-byte length so the whole entry must fit in
395
   // a uint16_t too.
396
   BOTAN_ASSERT_NOMSG(name_len + 3 <= 0xFFFF);
4,571✔
397

398
   buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len + 3)));
4,571✔
399
   buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len + 3)));
4,571✔
400
   buf.push_back(0);  // DNS
4,571✔
401

402
   buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len)));
4,571✔
403
   buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len)));
4,571✔
404

405
   buf += as_span_of_bytes(m_sni_host_name);
4,571✔
406

407
   return buf;
4,571✔
408
}
4,571✔
409

410
bool Server_Name_Indicator::hostname_acceptable_for_sni(std::string_view hostname) {
3,853✔
411
   // Avoid sending an IPv4/IPv6 address in SNI as this is prohibited
412

413
   if(hostname.empty() || hostname.size() > 255) {
3,853✔
414
      return false;
415
   }
416

417
   if(auto ipv4 = IPv4Address::from_string(hostname)) {
3,806✔
418
      return false;
×
419
   }
420

421
   if(auto ipv6 = IPv6Address::from_string(hostname)) {
3,806✔
422
      return false;
×
423
   }
424

425
   if(auto dns = DNSName::from_string(hostname)) {
3,806✔
426
      return true;
427
   } else {
428
      return false;
×
429
   }
3,806✔
430
}
431

432
Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification(std::string_view protocol) {
144✔
433
   BOTAN_ARG_CHECK(!protocol.empty(), "ALPN protocol name must not be empty");
144✔
434
   BOTAN_ARG_CHECK(protocol.size() < 256, "ALPN protocol name too long");
144✔
435
   m_protocols.emplace_back(protocol);
144✔
436
}
144✔
437

438
Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification(
146✔
439
   const std::vector<std::string>& protocols) :
146✔
440
      m_protocols(protocols) {
146✔
441
   for(const auto& protocol : protocols) {
442✔
442
      BOTAN_ARG_CHECK(!protocol.empty(), "ALPN protocol name must not be empty");
296✔
443
      BOTAN_ARG_CHECK(protocol.size() < 256, "ALPN protocol name too long");
296✔
444
   }
445
}
146✔
446

447
Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification(TLS_Data_Reader& reader,
406✔
448
                                                                                 uint16_t extension_size,
449
                                                                                 Connection_Side from) {
406✔
450
   if(extension_size < 2) {
406✔
451
      throw Decoding_Error("ALPN extension cannot be empty");
1✔
452
   }
453

454
   const uint16_t name_bytes = reader.get_uint16_t();
405✔
455

456
   size_t bytes_remaining = extension_size - 2;
405✔
457

458
   if(name_bytes != bytes_remaining) {
405✔
459
      throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
3✔
460
   }
461

462
   // RFC 7301 3.1: ProtocolName protocol_name_list<2..2^16-1>
463
   if(name_bytes == 0) {
402✔
464
      throw Decoding_Error("Empty ALPN protocol_name_list not allowed");
1✔
465
   }
466

467
   while(bytes_remaining > 0) {
1,067✔
468
      const std::string p = reader.get_string(1, 0, 255);
674✔
469

470
      if(bytes_remaining < p.size() + 1) {
674✔
471
         throw Decoding_Error("Bad encoding of ALPN, length field too long");
×
472
      }
473

474
      if(p.empty()) {
674✔
475
         throw Decoding_Error("Empty ALPN protocol not allowed");
8✔
476
      }
477

478
      bytes_remaining -= (p.size() + 1);
666✔
479

480
      m_protocols.push_back(p);
666✔
481
   }
674✔
482

483
   // RFC 7301 3.1
484
   //    The "extension_data" field of the [...] extension is structured the
485
   //    same as described above for the client "extension_data", except that
486
   //    the "ProtocolNameList" MUST contain exactly one "ProtocolName".
487
   if(from == Connection_Side::Server && m_protocols.size() != 1) {
393✔
488
      throw TLS_Exception(
×
489
         Alert::DecodeError,
490
         "Server sent " + std::to_string(m_protocols.size()) + " protocols in ALPN extension response");
×
491
   }
492
}
406✔
493

494
std::string Application_Layer_Protocol_Notification::single_protocol() const {
285✔
495
   BOTAN_STATE_CHECK(m_protocols.size() == 1);
285✔
496
   return m_protocols.front();
285✔
497
}
498

499
std::vector<uint8_t> Application_Layer_Protocol_Notification::serialize(Connection_Side /*whoami*/) const {
357✔
500
   std::vector<uint8_t> buf(2);
357✔
501

502
   for(auto&& proto : m_protocols) {
933✔
503
      if(proto.length() >= 256) {
576✔
504
         throw TLS_Exception(Alert::InternalError, "ALPN name too long");
×
505
      }
506
      if(!proto.empty()) {
576✔
507
         append_tls_length_value(buf, proto, 1);
1,152✔
508
      }
509
   }
510

511
   // RFC 7301 3.1: ProtocolName protocol_name_list<2..2^16-1>;
512
   BOTAN_ASSERT_NOMSG(buf.size() - 2 <= 0xFFFF);
357✔
513
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
357✔
514
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
357✔
515

516
   return buf;
357✔
517
}
×
518

519
Certificate_Type_Base::Certificate_Type_Base(std::vector<Certificate_Type> supported_cert_types) :
140✔
520
      m_certificate_types(std::move(supported_cert_types)), m_from(Connection_Side::Client) {
140✔
521
   BOTAN_ARG_CHECK(!m_certificate_types.empty(), "at least one certificate type must be supported");
140✔
522
}
140✔
523

524
Client_Certificate_Type::Client_Certificate_Type(const Client_Certificate_Type& cct, const Policy& policy) :
28✔
525
      Certificate_Type_Base(cct, policy.accepted_client_certificate_types()) {}
28✔
526

527
Server_Certificate_Type::Server_Certificate_Type(const Server_Certificate_Type& sct, const Policy& policy) :
73✔
528
      Certificate_Type_Base(sct, policy.accepted_server_certificate_types()) {}
73✔
529

530
Certificate_Type_Base::Certificate_Type_Base(const Certificate_Type_Base& certificate_type_from_client,
101✔
531
                                             const std::vector<Certificate_Type>& server_preference) :
101✔
532
      m_from(Connection_Side::Server) {
101✔
533
   // RFC 7250 4.2
534
   //    The server_certificate_type extension in the client hello indicates the
535
   //    types of certificates the client is able to process when provided by
536
   //    the server in a subsequent certificate payload. [...] With the
537
   //    server_certificate_type extension in the server hello, the TLS server
538
   //    indicates the certificate type carried in the Certificate payload.
539
   for(const auto server_supported_cert_type : server_preference) {
127✔
540
      if(value_exists(certificate_type_from_client.m_certificate_types, server_supported_cert_type)) {
246✔
541
         m_certificate_types.push_back(server_supported_cert_type);
97✔
542
         return;
97✔
543
      }
544
   }
545

546
   // RFC 7250 4.2 (2.)
547
   //    The server supports the extension defined in this document, but
548
   //    it does not have any certificate type in common with the client.
549
   //    Then, the server terminates the session with a fatal alert of
550
   //    type "unsupported_certificate".
551
   throw TLS_Exception(Alert::UnsupportedCertificate, "Failed to agree on certificate_type");
4✔
552
}
4✔
553

554
Certificate_Type_Base::Certificate_Type_Base(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) :
152✔
555
      m_from(from) {
152✔
556
   if(extension_size == 0) {
152✔
557
      throw Decoding_Error("Certificate type extension cannot be empty");
×
558
   }
559

560
   if(from == Connection_Side::Client) {
152✔
561
      const auto type_bytes = reader.get_tls_length_value(1);
119✔
562
      if(static_cast<size_t>(extension_size) != type_bytes.size() + 1) {
119✔
563
         throw Decoding_Error("certificate type extension had inconsistent length");
×
564
      }
565
      // RFC 7250 4: {client,server}_certificate_types<1..2^8-1> so must be non-empty
566
      if(type_bytes.empty()) {
119✔
567
         throw Decoding_Error("Certificate type extension contains no types");
2✔
568
      }
569
      std::transform(
117✔
570
         type_bytes.begin(), type_bytes.end(), std::back_inserter(m_certificate_types), [](const auto type_byte) {
117✔
571
            return static_cast<Certificate_Type>(type_byte);
572
         });
573
   } else {
119✔
574
      // RFC 7250 4.2
575
      //    Note that only a single value is permitted in the
576
      //    server_certificate_type extension when carried in the server hello.
577
      if(extension_size != 1) {
33✔
578
         throw Decoding_Error("Server's certificate type extension must be of length 1");
×
579
      }
580
      const auto type_byte = reader.get_byte();
33✔
581
      m_certificate_types.push_back(static_cast<Certificate_Type>(type_byte));
33✔
582
   }
583
}
152✔
584

585
std::vector<uint8_t> Certificate_Type_Base::serialize(Connection_Side whoami) const {
164✔
586
   std::vector<uint8_t> result;
164✔
587
   if(whoami == Connection_Side::Client) {
164✔
588
      std::vector<uint8_t> type_bytes;
63✔
589
      std::transform(
63✔
590
         m_certificate_types.begin(), m_certificate_types.end(), std::back_inserter(type_bytes), [](const auto type) {
591
            return static_cast<uint8_t>(type);
592
         });
593
      append_tls_length_value(result, type_bytes, 1);
126✔
594
   } else {
63✔
595
      BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
101✔
596
      result.push_back(static_cast<uint8_t>(m_certificate_types.front()));
101✔
597
   }
598
   return result;
164✔
599
}
×
600

601
void Certificate_Type_Base::validate_selection(const Certificate_Type_Base& from_server) const {
26✔
602
   BOTAN_ASSERT_NOMSG(m_from == Connection_Side::Client);
26✔
603
   BOTAN_ASSERT_NOMSG(from_server.m_from == Connection_Side::Server);
26✔
604

605
   // RFC 7250 4.2
606
   //    The value conveyed in the [client_]certificate_type extension MUST be
607
   //    selected from one of the values provided in the [client_]certificate_type
608
   //    extension sent in the client hello.
609
   if(!value_exists(m_certificate_types, from_server.selected_certificate_type())) {
52✔
610
      throw TLS_Exception(Alert::IllegalParameter,
2✔
611
                          Botan::fmt("Selected certificate type was not offered: {}",
4✔
612
                                     certificate_type_to_string(from_server.selected_certificate_type())));
6✔
613
   }
614
}
24✔
615

616
Certificate_Type Certificate_Type_Base::selected_certificate_type() const {
113✔
617
   BOTAN_ASSERT_NOMSG(m_from == Connection_Side::Server);
113✔
618
   BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
113✔
619
   return m_certificate_types.front();
113✔
620
}
621

622
Supported_Groups::Supported_Groups(const std::vector<Group_Params>& groups) : m_groups(groups) {}
4,359✔
623

624
const std::vector<Group_Params>& Supported_Groups::groups() const {
1,263✔
625
   return m_groups;
1,263✔
626
}
627

628
std::vector<Group_Params> Supported_Groups::ec_groups() const {
5,953✔
629
   std::vector<Group_Params> ec;
5,953✔
630
   for(auto g : m_groups) {
63,647✔
631
      if(g.is_pure_ecc_group()) {
115,388✔
632
         ec.push_back(g);
41,344✔
633
      }
634
   }
635
   return ec;
5,953✔
636
}
×
637

638
std::vector<Group_Params> Supported_Groups::dh_groups() const {
1,585✔
639
   std::vector<Group_Params> dh;
1,585✔
640
   for(auto g : m_groups) {
12,909✔
641
      if(g.is_in_ffdhe_range()) {
15,531✔
642
         dh.push_back(g);
563✔
643
      }
644
   }
645
   return dh;
1,585✔
646
}
×
647

648
std::vector<uint8_t> Supported_Groups::serialize(Connection_Side /*whoami*/) const {
5,144✔
649
   std::vector<uint8_t> buf(2);
5,144✔
650

651
   for(auto g : m_groups) {
60,317✔
652
      const uint16_t id = g.wire_code();
55,173✔
653

654
      if(id > 0) {
55,173✔
655
         buf.push_back(get_byte<0>(id));
55,173✔
656
         buf.push_back(get_byte<1>(id));
55,173✔
657
      }
658
   }
659

660
   // RFC 8446 4.2.7: NamedGroup named_group_list<2..2^16-1>;
661
   BOTAN_ASSERT_NOMSG(buf.size() - 2 <= 0xFFFF);
5,144✔
662
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
5,144✔
663
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
5,144✔
664

665
   return buf;
5,144✔
666
}
×
667

668
Supported_Groups::Supported_Groups(TLS_Data_Reader& reader, uint16_t extension_size) {
3,232✔
669
   const uint16_t len = reader.get_uint16_t();
3,232✔
670

671
   if(len + 2 != extension_size) {
3,232✔
672
      throw Decoding_Error("Inconsistent length field in supported groups list");
6✔
673
   }
674

675
   // RFC 8446 4.2.7: NamedGroup named_group_list<2..2^16-1>;
676
   if(len == 0) {
3,226✔
677
      throw Decoding_Error("Empty supported groups list");
1✔
678
   }
679

680
   if(len % 2 == 1) {
3,225✔
681
      throw Decoding_Error("Supported groups list of strange size");
1✔
682
   }
683

684
   const size_t elems = len / 2;
3,224✔
685

686
   std::unordered_set<uint16_t> seen;
3,224✔
687
   for(size_t i = 0; i != elems; ++i) {
28,459✔
688
      const auto group = static_cast<Group_Params>(reader.get_uint16_t());
25,235✔
689
      // Note: RFC 8446 does not explicitly enforce that groups must be unique.
690
      if(seen.insert(group.wire_code()).second) {
25,235✔
691
         m_groups.push_back(group);
25,235✔
692
      }
693
   }
694
}
3,232✔
695

696
namespace {
697

698
std::vector<uint8_t> serialize_signature_algorithms(const std::vector<Signature_Scheme>& schemes) {
5,064✔
699
   BOTAN_ASSERT(schemes.size() < 256, "Too many signature schemes");
5,064✔
700

701
   std::vector<uint8_t> buf;
5,064✔
702

703
   const uint16_t len = static_cast<uint16_t>(schemes.size() * 2);
5,064✔
704

705
   buf.push_back(get_byte<0>(len));
5,064✔
706
   buf.push_back(get_byte<1>(len));
5,064✔
707

708
   for(const Signature_Scheme scheme : schemes) {
49,746✔
709
      buf.push_back(get_byte<0>(scheme.wire_code()));
44,682✔
710
      buf.push_back(get_byte<1>(scheme.wire_code()));
44,682✔
711
   }
712

713
   return buf;
5,064✔
714
}
×
715

716
std::vector<Signature_Scheme> parse_signature_algorithms(TLS_Data_Reader& reader, uint16_t extension_size) {
3,270✔
717
   uint16_t len = reader.get_uint16_t();
3,270✔
718

719
   if(len + 2 != extension_size || len % 2 == 1 || len == 0) {
3,268✔
720
      throw Decoding_Error("Bad encoding on signature algorithms extension");
4✔
721
   }
722

723
   std::vector<Signature_Scheme> schemes;
3,264✔
724
   schemes.reserve(len / 2);
3,264✔
725
   while(len > 0) {
34,544✔
726
      schemes.emplace_back(reader.get_uint16_t());
31,280✔
727
      len -= 2;
31,280✔
728
   }
729

730
   return schemes;
3,264✔
731
}
×
732

733
}  // namespace
734

735
std::vector<uint8_t> Signature_Algorithms::serialize(Connection_Side /*whoami*/) const {
5,062✔
736
   return serialize_signature_algorithms(m_schemes);
5,062✔
737
}
738

739
Signature_Algorithms::Signature_Algorithms(TLS_Data_Reader& reader, uint16_t extension_size) :
3,265✔
740
      m_schemes(parse_signature_algorithms(reader, extension_size)) {}
3,265✔
741

742
std::vector<uint8_t> Signature_Algorithms_Cert::serialize(Connection_Side /*whoami*/) const {
2✔
743
   return serialize_signature_algorithms(m_schemes);
2✔
744
}
745

746
Signature_Algorithms_Cert::Signature_Algorithms_Cert(TLS_Data_Reader& reader, uint16_t extension_size) :
5✔
747
      m_schemes(parse_signature_algorithms(reader, extension_size)) {}
5✔
748

749
SRTP_Protection_Profiles::SRTP_Protection_Profiles(TLS_Data_Reader& reader, uint16_t extension_size) {
10✔
750
   // RFC 5764 4.1.1: UseSRTPData consists of
751
   //    SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
752
   //    opaque srtp_mki<0..255>;
753
   // for a wire size of 2 (profiles len) + 2*N + 1 (mki len) + mki_bytes,
754
   // with N >= 1.
755
   if(extension_size < 5) {
10✔
756
      throw Decoding_Error("Truncated SRTP protection extension");
1✔
757
   }
758
   const size_t max_profile_pairs = (static_cast<size_t>(extension_size) - 3) / 2;
9✔
759
   m_pp = reader.get_range<uint16_t>(2, 1, max_profile_pairs);
9✔
760
   const std::vector<uint8_t> mki = reader.get_range<uint8_t>(1, 0, 255);
9✔
761

762
   if(m_pp.size() * 2 + mki.size() + 3 != extension_size) {
9✔
763
      throw Decoding_Error("Bad encoding for SRTP protection extension");
×
764
   }
765

766
   if(!mki.empty()) {
9✔
767
      throw Decoding_Error("Unhandled non-empty MKI for SRTP protection extension");
×
768
   }
769
}
10✔
770

771
std::vector<uint8_t> SRTP_Protection_Profiles::serialize(Connection_Side /*whoami*/) const {
5✔
772
   std::vector<uint8_t> buf;
5✔
773

774
   const uint16_t pp_len = static_cast<uint16_t>(m_pp.size() * 2);
5✔
775
   buf.push_back(get_byte<0>(pp_len));
5✔
776
   buf.push_back(get_byte<1>(pp_len));
5✔
777

778
   for(const uint16_t pp : m_pp) {
12✔
779
      buf.push_back(get_byte<0>(pp));
7✔
780
      buf.push_back(get_byte<1>(pp));
7✔
781
   }
782

783
   buf.push_back(0);  // srtp_mki, always empty here
5✔
784

785
   return buf;
5✔
786
}
×
787

788
std::vector<uint8_t> Supported_Versions::serialize(Connection_Side whoami) const {
4,866✔
789
   std::vector<uint8_t> buf;
4,866✔
790

791
   if(whoami == Connection_Side::Server) {
4,866✔
792
      BOTAN_ASSERT_NOMSG(m_versions.size() == 1);
581✔
793
      buf.push_back(m_versions[0].major_version());
581✔
794
      buf.push_back(m_versions[0].minor_version());
581✔
795
   } else {
796
      // RFC 8446 4.2.1: ProtocolVersion versions<2..254>; - up to 127 entries.
797
      BOTAN_ASSERT_NOMSG(!m_versions.empty());
4,285✔
798
      BOTAN_ASSERT_NOMSG(m_versions.size() <= 127);
4,285✔
799
      const uint8_t len = static_cast<uint8_t>(m_versions.size() * 2);
4,285✔
800

801
      buf.push_back(len);
4,285✔
802

803
      for(const Protocol_Version version : m_versions) {
9,795✔
804
         buf.push_back(version.major_version());
5,510✔
805
         buf.push_back(version.minor_version());
5,510✔
806
      }
807
   }
808

809
   return buf;
4,866✔
810
}
×
811

812
Supported_Versions::Supported_Versions(Protocol_Version offer, const Policy& policy) {
3,617✔
813
   // RFC 8446 4.2.1
814
   //    The extension contains a list of supported versions in preference order,
815
   //    with the most preferred version first. Implementations [...] MUST send
816
   //    this extension in the ClientHello containing all versions of TLS which
817
   //    they are prepared to negotiate.
818
   //
819
   // We simply assume that we always want the newest available TLS version.
820
#if defined(BOTAN_HAS_TLS_13)
821
   if(!offer.is_datagram_protocol()) {
3,617✔
822
      if(offer >= Protocol_Version::TLS_V13 && policy.allow_tls13()) {
5,270✔
823
         m_versions.push_back(Protocol_Version::TLS_V13);
1,098✔
824
      }
825
   }
826
#endif
827

828
#if defined(BOTAN_HAS_TLS_12)
829
   if(offer.is_datagram_protocol()) {
3,617✔
830
      if(offer >= Protocol_Version::DTLS_V12 && policy.allow_dtls12()) {
433✔
831
         m_versions.push_back(Protocol_Version::DTLS_V12);
433✔
832
      }
833
   } else {
834
      if(offer >= Protocol_Version::TLS_V12 && policy.allow_tls12()) {
4,282✔
835
         m_versions.push_back(Protocol_Version::TLS_V12);
3,114✔
836
      }
837
   }
838
#endif
839

840
   // if no versions are supported, the input variables are not used
841
   BOTAN_UNUSED(offer, policy);
3,617✔
842
}
3,617✔
843

844
Supported_Versions::Supported_Versions(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
2,263✔
845
   if(from == Connection_Side::Server) {
2,263✔
846
      if(extension_size != 2) {
643✔
847
         throw Decoding_Error("Server sent invalid supported_versions extension");
1✔
848
      }
849
      m_versions.push_back(Protocol_Version(reader.get_uint16_t()));
642✔
850
   } else {
851
      auto versions = reader.get_range<uint16_t>(1, 1, 127);
1,620✔
852

853
      for(auto v : versions) {
5,762✔
854
         m_versions.push_back(Protocol_Version(v));
4,142✔
855
      }
856

857
      if(extension_size != 1 + 2 * versions.size()) {
1,620✔
858
         throw Decoding_Error("Client sent invalid supported_versions extension");
×
859
      }
860
   }
1,620✔
861
}
2,263✔
862

863
bool Supported_Versions::supports(Protocol_Version version) const {
1,751✔
864
   for(auto v : m_versions) {
2,263✔
865
      if(version == v) {
2,244✔
866
         return true;
1,751✔
867
      }
868
   }
869
   return false;
870
}
871

872
Record_Size_Limit::Record_Size_Limit(const uint16_t limit) : m_limit(limit) {
16✔
873
   BOTAN_ARG_CHECK(limit >= 64, "RFC 8449 does not allow record size limits smaller than 64 bytes");
16✔
874
   BOTAN_ARG_CHECK(limit <= MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */,
16✔
875
                   "RFC 8449 does not allow record size limits larger than 2^14+1");
876
}
16✔
877

878
Record_Size_Limit::Record_Size_Limit(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
45✔
879
   if(extension_size != 2) {
45✔
880
      throw TLS_Exception(Alert::DecodeError, "invalid record_size_limit extension");
×
881
   }
882

883
   m_limit = reader.get_uint16_t();
45✔
884

885
   // RFC 8449 4.
886
   //    This value is the length of the plaintext of a protected record.
887
   //    The value includes the content type and padding added in TLS 1.3 (that
888
   //    is, the complete length of TLSInnerPlaintext).
889
   //
890
   //    A server MUST NOT enforce this restriction; a client might advertise
891
   //    a higher limit that is enabled by an extension or version the server
892
   //    does not understand. A client MAY abort the handshake with an
893
   //    "illegal_parameter" alert.
894
   //
895
   // Note: We are currently supporting this extension in TLS 1.3 only, hence
896
   //       we check for the TLS 1.3 limit. The TLS 1.2 limit would not include
897
   //       the "content type byte" and hence be one byte less!
898
   if(m_limit > MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */ && from == Connection_Side::Server) {
45✔
899
      throw TLS_Exception(Alert::IllegalParameter,
×
900
                          "Server requested a record size limit larger than the protocol's maximum");
×
901
   }
902

903
   // RFC 8449 4.
904
   //    Endpoints MUST NOT send a "record_size_limit" extension with a value
905
   //    smaller than 64.  An endpoint MUST treat receipt of a smaller value
906
   //    as a fatal error and generate an "illegal_parameter" alert.
907
   if(m_limit < 64) {
45✔
908
      throw TLS_Exception(Alert::IllegalParameter, "Received a record size limit smaller than 64 bytes");
×
909
   }
910
}
45✔
911

912
std::vector<uint8_t> Record_Size_Limit::serialize(Connection_Side /*whoami*/) const {
53✔
913
   std::vector<uint8_t> buf;
53✔
914

915
   buf.push_back(get_byte<0>(m_limit));
53✔
916
   buf.push_back(get_byte<1>(m_limit));
53✔
917

918
   return buf;
53✔
919
}
×
920

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