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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

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

89.09
/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
*
8
* Botan is released under the Simplified BSD License (see license.txt)
9
*/
10

11
#include <botan/tls_extensions.h>
12

13
#include <botan/ber_dec.h>
14
#include <botan/tls_exceptn.h>
15
#include <botan/tls_policy.h>
16
#include <botan/internal/stl_util.h>
17
#include <botan/internal/tls_reader.h>
18

19
#include <iterator>
20

21
namespace Botan::TLS {
22

23
namespace {
24

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

36
      case Extension_Code::SupportedGroups:
2,716✔
37
         return std::make_unique<Supported_Groups>(reader, size);
2,716✔
38

39
      case Extension_Code::CertificateStatusRequest:
2,496✔
40
         return std::make_unique<Certificate_Status_Request>(reader, size, message_type, from);
2,496✔
41

42
      case Extension_Code::EcPointFormats:
3,039✔
43
         return std::make_unique<Supported_Point_Formats>(reader, size);
3,039✔
44

45
      case Extension_Code::SafeRenegotiation:
5,412✔
46
         return std::make_unique<Renegotiation_Extension>(reader, size);
5,412✔
47

48
      case Extension_Code::SignatureAlgorithms:
2,738✔
49
         return std::make_unique<Signature_Algorithms>(reader, size);
2,738✔
50

51
      case Extension_Code::CertSignatureAlgorithms:
×
52
         return std::make_unique<Signature_Algorithms_Cert>(reader, size);
×
53

54
      case Extension_Code::UseSrtp:
10✔
55
         return std::make_unique<SRTP_Protection_Profiles>(reader, size);
10✔
56

57
      case Extension_Code::ApplicationLayerProtocolNegotiation:
395✔
58
         return std::make_unique<Application_Layer_Protocol_Notification>(reader, size, from);
395✔
59

60
      case Extension_Code::ExtendedMasterSecret:
5,236✔
61
         return std::make_unique<Extended_Master_Secret>(reader, size);
5,236✔
62

63
      case Extension_Code::RecordSizeLimit:
40✔
64
         return std::make_unique<Record_Size_Limit>(reader, size, from);
40✔
65

66
      case Extension_Code::EncryptThenMac:
763✔
67
         return std::make_unique<Encrypt_then_MAC>(reader, size);
763✔
68

69
      case Extension_Code::SessionTicket:
4,701✔
70
         return std::make_unique<Session_Ticket_Extension>(reader, size);
4,701✔
71

72
      case Extension_Code::SupportedVersions:
1,640✔
73
         return std::make_unique<Supported_Versions>(reader, size, from);
1,640✔
74

75
#if defined(BOTAN_HAS_TLS_13)
76
      case Extension_Code::PresharedKey:
206✔
77
         return std::make_unique<PSK>(reader, size, message_type);
206✔
78

79
      case Extension_Code::EarlyData:
5✔
80
         return std::make_unique<EarlyDataIndication>(reader, size, message_type);
5✔
81

82
      case Extension_Code::Cookie:
10✔
83
         return std::make_unique<Cookie>(reader, size);
10✔
84

85
      case Extension_Code::PskKeyExchangeModes:
931✔
86
         return std::make_unique<PSK_Key_Exchange_Modes>(reader, size);
931✔
87

88
      case Extension_Code::CertificateAuthorities:
3✔
89
         return std::make_unique<Certificate_Authorities>(reader, size);
3✔
90

91
      case Extension_Code::KeyShare:
1,439✔
92
         return std::make_unique<Key_Share>(reader, size, message_type);
1,439✔
93
#endif
94
   }
95

96
   return std::make_unique<Unknown_Extension>(static_cast<Extension_Code>(code), reader, size);
4,516✔
97
}
98

99
}  // namespace
100

101
void Extensions::add(std::unique_ptr<Extension> extn) {
80,965✔
102
   if(has(extn->type())) {
80,965✔
103
      throw Invalid_Argument("cannot add the same extension twice: " +
×
104
                             std::to_string(static_cast<uint16_t>(extn->type())));
×
105
   }
106

107
   m_extensions.emplace_back(extn.release());
80,965✔
108
}
80,965✔
109

110
void Extensions::deserialize(TLS_Data_Reader& reader, const Connection_Side from, const Handshake_Type message_type) {
7,610✔
111
   if(reader.has_remaining()) {
7,610✔
112
      const uint16_t all_extn_size = reader.get_uint16_t();
7,605✔
113

114
      if(reader.remaining_bytes() != all_extn_size) {
7,605✔
115
         throw Decoding_Error("Bad extension size");
119✔
116
      }
117

118
      while(reader.has_remaining()) {
46,402✔
119
         const uint16_t extension_code = reader.get_uint16_t();
38,971✔
120
         const uint16_t extension_size = reader.get_uint16_t();
38,970✔
121

122
         const auto type = static_cast<Extension_Code>(extension_code);
38,970✔
123

124
         if(this->has(type)) {
38,970✔
125
            throw TLS_Exception(TLS::Alert::DecodeError, "Peer sent duplicated extensions");
13✔
126
         }
127

128
         // TODO offer a function on reader that returns a byte range as a reference
129
         // to avoid this copy of the extension data
130
         const std::vector<uint8_t> extn_data = reader.get_fixed<uint8_t>(extension_size);
38,957✔
131
         TLS_Data_Reader extn_reader("Extension", extn_data);
38,949✔
132
         this->add(make_extension(extn_reader, type, from, message_type));
38,949✔
133
         extn_reader.assert_done();
38,916✔
134
      }
38,916✔
135
   }
136
}
7,436✔
137

138
bool Extensions::contains_other_than(const std::set<Extension_Code>& allowed_extensions,
3,333✔
139
                                     const bool allow_unknown_extensions) const {
140
   const auto found = extension_types();
3,333✔
141

142
   std::vector<Extension_Code> diff;
3,333✔
143
   std::set_difference(
3,333✔
144
      found.cbegin(), found.end(), allowed_extensions.cbegin(), allowed_extensions.cend(), std::back_inserter(diff));
145

146
   if(allow_unknown_extensions) {
3,333✔
147
      // Go through the found unexpected extensions whether any of those
148
      // is known to this TLS implementation.
149
      const auto itr = std::find_if(diff.cbegin(), diff.cend(), [this](const auto ext_type) {
1,489✔
150
         const auto ext = get(ext_type);
10✔
151
         return ext && ext->is_implemented();
10✔
152
      });
153

154
      // ... if yes, `contains_other_than` is true
155
      return itr != diff.cend();
1,489✔
156
   }
157

158
   return !diff.empty();
1,844✔
159
}
3,333✔
160

161
std::unique_ptr<Extension> Extensions::take(Extension_Code type) {
462✔
162
   const auto i =
462✔
163
      std::find_if(m_extensions.begin(), m_extensions.end(), [type](const auto& ext) { return ext->type() == type; });
2,912✔
164

165
   std::unique_ptr<Extension> result;
462✔
166
   if(i != m_extensions.end()) {
462✔
167
      std::swap(result, *i);
212✔
168
      m_extensions.erase(i);
212✔
169
   }
170

171
   return result;
462✔
172
}
173

174
std::vector<uint8_t> Extensions::serialize(Connection_Side whoami) const {
6,218✔
175
   std::vector<uint8_t> buf(2);  // 2 bytes for length field
6,218✔
176

177
   for(const auto& extn : m_extensions) {
53,851✔
178
      if(extn->empty()) {
47,633✔
179
         continue;
684✔
180
      }
181

182
      const uint16_t extn_code = static_cast<uint16_t>(extn->type());
46,949✔
183

184
      const std::vector<uint8_t> extn_val = extn->serialize(whoami);
46,949✔
185

186
      buf.push_back(get_byte<0>(extn_code));
46,949✔
187
      buf.push_back(get_byte<1>(extn_code));
46,949✔
188

189
      buf.push_back(get_byte<0>(static_cast<uint16_t>(extn_val.size())));
46,949✔
190
      buf.push_back(get_byte<1>(static_cast<uint16_t>(extn_val.size())));
46,949✔
191

192
      buf += extn_val;
46,949✔
193
   }
46,949✔
194

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

197
   buf[0] = get_byte<0>(extn_size);
6,218✔
198
   buf[1] = get_byte<1>(extn_size);
6,218✔
199

200
   // avoid sending a completely empty extensions block
201
   if(buf.size() == 2) {
6,218✔
202
      return std::vector<uint8_t>();
283✔
203
   }
204

205
   return buf;
6,218✔
206
}
6,218✔
207

208
std::set<Extension_Code> Extensions::extension_types() const {
7,702✔
209
   std::set<Extension_Code> offers;
7,702✔
210
   std::transform(
7,702✔
211
      m_extensions.cbegin(), m_extensions.cend(), std::inserter(offers, offers.begin()), [](const auto& ext) {
39,331✔
212
         return ext->type();
39,331✔
213
      });
214
   return offers;
7,702✔
215
}
×
216

217
Unknown_Extension::Unknown_Extension(Extension_Code type, TLS_Data_Reader& reader, uint16_t extension_size) :
4,516✔
218
      m_type(type), m_value(reader.get_fixed<uint8_t>(extension_size)) {}
4,516✔
219

220
std::vector<uint8_t> Unknown_Extension::serialize(Connection_Side /*whoami*/) const { return m_value; }
2✔
221

222
Server_Name_Indicator::Server_Name_Indicator(TLS_Data_Reader& reader, uint16_t extension_size) {
2,653✔
223
   /*
224
   * This is used by the server to confirm that it knew the name
225
   */
226
   if(extension_size == 0) {
2,653✔
227
      return;
228
   }
229

230
   uint16_t name_bytes = reader.get_uint16_t();
2,624✔
231

232
   if(name_bytes + 2 != extension_size) {
2,624✔
233
      throw Decoding_Error("Bad encoding of SNI extension");
×
234
   }
235

236
   while(name_bytes) {
5,248✔
237
      uint8_t name_type = reader.get_byte();
2,624✔
238
      name_bytes--;
2,624✔
239

240
      if(name_type == 0)  // DNS
2,624✔
241
      {
242
         m_sni_host_name = reader.get_string(2, 1, 65535);
2,624✔
243
         name_bytes -= static_cast<uint16_t>(2 + m_sni_host_name.size());
2,624✔
244
      } else  // some other unknown name type
245
      {
246
         reader.discard_next(name_bytes);
×
247
         name_bytes = 0;
×
248
      }
249
   }
250
}
×
251

252
std::vector<uint8_t> Server_Name_Indicator::serialize(Connection_Side whoami) const {
4,459✔
253
   // RFC 6066
254
   //    [...] the server SHALL include an extension of type "server_name" in
255
   //    the (extended) server hello. The "extension_data" field of this
256
   //    extension SHALL be empty.
257
   if(whoami == Connection_Side::Server) {
4,459✔
258
      return {};
351✔
259
   }
260

261
   std::vector<uint8_t> buf;
4,108✔
262

263
   size_t name_len = m_sni_host_name.size();
4,108✔
264

265
   buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len + 3)));
4,108✔
266
   buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len + 3)));
4,108✔
267
   buf.push_back(0);  // DNS
4,108✔
268

269
   buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len)));
4,108✔
270
   buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len)));
4,108✔
271

272
   buf += std::make_pair(cast_char_ptr_to_uint8(m_sni_host_name.data()), m_sni_host_name.size());
4,108✔
273

274
   return buf;
4,108✔
275
}
4,459✔
276

277
Renegotiation_Extension::Renegotiation_Extension(TLS_Data_Reader& reader, uint16_t extension_size) :
5,412✔
278
      m_reneg_data(reader.get_range<uint8_t>(1, 0, 255)) {
5,412✔
279
   if(m_reneg_data.size() + 1 != extension_size) {
5,410✔
280
      throw Decoding_Error("Bad encoding for secure renegotiation extn");
1✔
281
   }
282
}
5,410✔
283

284
std::vector<uint8_t> Renegotiation_Extension::serialize(Connection_Side /*whoami*/) const {
5,038✔
285
   std::vector<uint8_t> buf;
5,038✔
286
   append_tls_length_value(buf, m_reneg_data, 1);
5,038✔
287
   return buf;
5,038✔
288
}
×
289

290
Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification(TLS_Data_Reader& reader,
395✔
291
                                                                                 uint16_t extension_size,
292
                                                                                 Connection_Side from) {
395✔
293
   if(extension_size == 0) {
395✔
294
      return;  // empty extension
295
   }
296

297
   const uint16_t name_bytes = reader.get_uint16_t();
395✔
298

299
   size_t bytes_remaining = extension_size - 2;
395✔
300

301
   if(name_bytes != bytes_remaining) {
395✔
302
      throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
×
303
   }
304

305
   while(bytes_remaining) {
1,052✔
306
      const std::string p = reader.get_string(1, 0, 255);
664✔
307

308
      if(bytes_remaining < p.size() + 1) {
664✔
309
         throw Decoding_Error("Bad encoding of ALPN, length field too long");
×
310
      }
311

312
      if(p.empty()) {
664✔
313
         throw Decoding_Error("Empty ALPN protocol not allowed");
7✔
314
      }
315

316
      bytes_remaining -= (p.size() + 1);
657✔
317

318
      m_protocols.push_back(p);
657✔
319
   }
664✔
320

321
   // RFC 7301 3.1
322
   //    The "extension_data" field of the [...] extension is structured the
323
   //    same as described above for the client "extension_data", except that
324
   //    the "ProtocolNameList" MUST contain exactly one "ProtocolName".
325
   if(from == Connection_Side::Server && m_protocols.size() != 1) {
388✔
326
      throw TLS_Exception(
×
327
         Alert::DecodeError,
328
         "Server sent " + std::to_string(m_protocols.size()) + " protocols in ALPN extension response");
×
329
   }
330
}
7✔
331

332
std::string Application_Layer_Protocol_Notification::single_protocol() const {
145✔
333
   BOTAN_STATE_CHECK(m_protocols.size() == 1);
145✔
334
   return m_protocols.front();
145✔
335
}
336

337
std::vector<uint8_t> Application_Layer_Protocol_Notification::serialize(Connection_Side /*whoami*/) const {
353✔
338
   std::vector<uint8_t> buf(2);
353✔
339

340
   for(auto&& p : m_protocols) {
925✔
341
      if(p.length() >= 256) {
572✔
342
         throw TLS_Exception(Alert::InternalError, "ALPN name too long");
×
343
      }
344
      if(!p.empty()) {
572✔
345
         append_tls_length_value(buf, cast_char_ptr_to_uint8(p.data()), p.size(), 1);
572✔
346
      }
347
   }
348

349
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
353✔
350
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
353✔
351

352
   return buf;
353✔
353
}
×
354

355
Supported_Groups::Supported_Groups(const std::vector<Group_Params>& groups) : m_groups(groups) {}
3,940✔
356

357
const std::vector<Group_Params>& Supported_Groups::groups() const { return m_groups; }
876✔
358

359
std::vector<Group_Params> Supported_Groups::ec_groups() const {
4,820✔
360
   std::vector<Group_Params> ec;
4,820✔
361
   for(auto g : m_groups) {
53,815✔
362
      if(group_param_is_dh(g) == false) {
48,995✔
363
         ec.push_back(g);
30,879✔
364
      }
365
   }
366
   return ec;
4,820✔
367
}
×
368

369
std::vector<Group_Params> Supported_Groups::dh_groups() const {
6✔
370
   std::vector<Group_Params> dh;
6✔
371
   for(auto g : m_groups) {
42✔
372
      if(group_param_is_dh(g) == true) {
36✔
373
         dh.push_back(g);
17✔
374
      }
375
   }
376
   return dh;
6✔
377
}
×
378

379
std::vector<uint8_t> Supported_Groups::serialize(Connection_Side /*whoami*/) const {
4,510✔
380
   std::vector<uint8_t> buf(2);
4,510✔
381

382
   for(auto g : m_groups) {
56,313✔
383
      const uint16_t id = static_cast<uint16_t>(g);
51,803✔
384

385
      if(id > 0) {
51,803✔
386
         buf.push_back(get_byte<0>(id));
51,803✔
387
         buf.push_back(get_byte<1>(id));
51,803✔
388
      }
389
   }
390

391
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
4,510✔
392
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
4,510✔
393

394
   return buf;
4,510✔
395
}
×
396

397
Supported_Groups::Supported_Groups(TLS_Data_Reader& reader, uint16_t extension_size) {
2,718✔
398
   const uint16_t len = reader.get_uint16_t();
2,718✔
399

400
   if(len + 2 != extension_size) {
2,718✔
401
      throw Decoding_Error("Inconsistent length field in supported groups list");
4✔
402
   }
403

404
   if(len % 2 == 1) {
2,714✔
405
      throw Decoding_Error("Supported groups list of strange size");
×
406
   }
407

408
   const size_t elems = len / 2;
2,714✔
409

410
   for(size_t i = 0; i != elems; ++i) {
20,543✔
411
      const auto group = static_cast<Group_Params>(reader.get_uint16_t());
17,829✔
412
      // Note: RFC 8446 does not explicitly enforce that groups must be unique.
413
      if(!value_exists(m_groups, group)) {
35,658✔
414
         m_groups.push_back(group);
17,829✔
415
      }
416
   }
417
}
2,718✔
418

419
std::vector<uint8_t> Supported_Point_Formats::serialize(Connection_Side /*whoami*/) const {
4,892✔
420
   // if this extension is sent, it MUST include uncompressed (RFC 4492, section 5.1)
421
   if(m_prefers_compressed) {
4,892✔
422
      return std::vector<uint8_t>{2, ANSIX962_COMPRESSED_PRIME, UNCOMPRESSED};
10✔
423
   } else {
424
      return std::vector<uint8_t>{1, UNCOMPRESSED};
4,882✔
425
   }
426
}
427

428
Supported_Point_Formats::Supported_Point_Formats(TLS_Data_Reader& reader, uint16_t extension_size) {
3,039✔
429
   uint8_t len = reader.get_byte();
3,039✔
430

431
   if(len + 1 != extension_size) {
3,039✔
432
      throw Decoding_Error("Inconsistent length field in supported point formats list");
2✔
433
   }
434

435
   bool includes_uncompressed = false;
3,042✔
436
   for(size_t i = 0; i != len; ++i) {
3,042✔
437
      uint8_t format = reader.get_byte();
3,041✔
438

439
      if(static_cast<ECPointFormat>(format) == UNCOMPRESSED) {
3,041✔
440
         m_prefers_compressed = false;
3,026✔
441
         reader.discard_next(len - i - 1);
3,026✔
442
         return;
3,026✔
443
      } else if(static_cast<ECPointFormat>(format) == ANSIX962_COMPRESSED_PRIME) {
15✔
444
         m_prefers_compressed = true;
10✔
445
         std::vector<uint8_t> remaining_formats = reader.get_fixed<uint8_t>(len - i - 1);
10✔
446
         includes_uncompressed =
10✔
447
            std::any_of(std::begin(remaining_formats), std::end(remaining_formats), [](uint8_t remaining_format) {
10✔
448
               return static_cast<ECPointFormat>(remaining_format) == UNCOMPRESSED;
449
            });
450
         break;
10✔
451
      }
10✔
452

453
      // ignore ANSIX962_COMPRESSED_CHAR2, we don't support these curves
454
   }
455

456
   // RFC 4492 5.1.:
457
   //   If the Supported Point Formats Extension is indeed sent, it MUST contain the value 0 (uncompressed)
458
   //   as one of the items in the list of point formats.
459
   // Note:
460
   //   RFC 8422 5.1.2. explicitly requires this check,
461
   //   but only if the Supported Groups extension was sent.
462
   if(!includes_uncompressed) {
11✔
463
      throw TLS_Exception(Alert::IllegalParameter,
1✔
464
                          "Supported Point Formats Extension must contain the uncompressed point format");
2✔
465
   }
466
}
467

468
namespace {
469

470
std::vector<uint8_t> serialize_signature_algorithms(const std::vector<Signature_Scheme>& schemes) {
4,487✔
471
   BOTAN_ASSERT(schemes.size() < 256, "Too many signature schemes");
4,487✔
472

473
   std::vector<uint8_t> buf;
4,487✔
474

475
   const uint16_t len = static_cast<uint16_t>(schemes.size() * 2);
4,487✔
476

477
   buf.push_back(get_byte<0>(len));
4,487✔
478
   buf.push_back(get_byte<1>(len));
4,487✔
479

480
   for(Signature_Scheme scheme : schemes) {
43,960✔
481
      buf.push_back(get_byte<0>(scheme.wire_code()));
39,473✔
482
      buf.push_back(get_byte<1>(scheme.wire_code()));
39,473✔
483
   }
484

485
   return buf;
4,487✔
486
}
×
487

488
std::vector<Signature_Scheme> parse_signature_algorithms(TLS_Data_Reader& reader, uint16_t extension_size) {
2,740✔
489
   uint16_t len = reader.get_uint16_t();
2,740✔
490

491
   if(len + 2 != extension_size || len % 2 == 1 || len == 0) {
2,739✔
492
      throw Decoding_Error("Bad encoding on signature algorithms extension");
1✔
493
   }
494

495
   std::vector<Signature_Scheme> schemes;
2,738✔
496
   schemes.reserve(len / 2);
2,738✔
497
   while(len) {
21,391✔
498
      schemes.emplace_back(reader.get_uint16_t());
18,653✔
499
      len -= 2;
18,653✔
500
   }
501

502
   return schemes;
2,738✔
503
}
×
504

505
}  // namespace
506

507
std::vector<uint8_t> Signature_Algorithms::serialize(Connection_Side /*whoami*/) const {
4,485✔
508
   return serialize_signature_algorithms(m_schemes);
4,485✔
509
}
510

511
Signature_Algorithms::Signature_Algorithms(TLS_Data_Reader& reader, uint16_t extension_size) :
2,738✔
512
      m_schemes(parse_signature_algorithms(reader, extension_size)) {}
2,738✔
513

514
std::vector<uint8_t> Signature_Algorithms_Cert::serialize(Connection_Side /*whoami*/) const {
2✔
515
   return serialize_signature_algorithms(m_schemes);
2✔
516
}
517

518
Signature_Algorithms_Cert::Signature_Algorithms_Cert(TLS_Data_Reader& reader, uint16_t extension_size) :
2✔
519
      m_schemes(parse_signature_algorithms(reader, extension_size)) {}
2✔
520

521
Session_Ticket_Extension::Session_Ticket_Extension(TLS_Data_Reader& reader, uint16_t extension_size) :
4,701✔
522
      m_ticket(Session_Ticket(reader.get_elem<uint8_t, std::vector<uint8_t>>(extension_size))) {}
4,701✔
523

524
SRTP_Protection_Profiles::SRTP_Protection_Profiles(TLS_Data_Reader& reader, uint16_t extension_size) :
10✔
525
      m_pp(reader.get_range<uint16_t>(2, 0, 65535)) {
10✔
526
   const std::vector<uint8_t> mki = reader.get_range<uint8_t>(1, 0, 255);
9✔
527

528
   if(m_pp.size() * 2 + mki.size() + 3 != extension_size) {
9✔
529
      throw Decoding_Error("Bad encoding for SRTP protection extension");
×
530
   }
531

532
   if(!mki.empty()) {
9✔
533
      throw Decoding_Error("Unhandled non-empty MKI for SRTP protection extension");
×
534
   }
535
}
9✔
536

537
std::vector<uint8_t> SRTP_Protection_Profiles::serialize(Connection_Side /*whoami*/) const {
5✔
538
   std::vector<uint8_t> buf;
5✔
539

540
   const uint16_t pp_len = static_cast<uint16_t>(m_pp.size() * 2);
5✔
541
   buf.push_back(get_byte<0>(pp_len));
5✔
542
   buf.push_back(get_byte<1>(pp_len));
5✔
543

544
   for(uint16_t pp : m_pp) {
12✔
545
      buf.push_back(get_byte<0>(pp));
7✔
546
      buf.push_back(get_byte<1>(pp));
7✔
547
   }
548

549
   buf.push_back(0);  // srtp_mki, always empty here
5✔
550

551
   return buf;
5✔
552
}
×
553

554
Extended_Master_Secret::Extended_Master_Secret(TLS_Data_Reader& /*unused*/, uint16_t extension_size) {
5,236✔
555
   if(extension_size != 0) {
5,236✔
556
      throw Decoding_Error("Invalid extended_master_secret extension");
2✔
557
   }
558
}
5,234✔
559

560
std::vector<uint8_t> Extended_Master_Secret::serialize(Connection_Side /*whoami*/) const {
5,012✔
561
   return std::vector<uint8_t>();
5,012✔
562
}
563

564
Encrypt_then_MAC::Encrypt_then_MAC(TLS_Data_Reader& /*unused*/, uint16_t extension_size) {
763✔
565
   if(extension_size != 0) {
763✔
566
      throw Decoding_Error("Invalid encrypt_then_mac extension");
×
567
   }
568
}
763✔
569

570
std::vector<uint8_t> Encrypt_then_MAC::serialize(Connection_Side /*whoami*/) const { return std::vector<uint8_t>(); }
3,815✔
571

572
std::vector<uint8_t> Supported_Versions::serialize(Connection_Side whoami) const {
4,252✔
573
   std::vector<uint8_t> buf;
4,252✔
574

575
   if(whoami == Connection_Side::Server) {
4,252✔
576
      BOTAN_ASSERT_NOMSG(m_versions.size() == 1);
401✔
577
      buf.push_back(m_versions[0].major_version());
401✔
578
      buf.push_back(m_versions[0].minor_version());
401✔
579
   } else {
580
      BOTAN_ASSERT_NOMSG(!m_versions.empty());
3,851✔
581
      const uint8_t len = static_cast<uint8_t>(m_versions.size() * 2);
3,851✔
582

583
      buf.push_back(len);
3,851✔
584

585
      for(Protocol_Version version : m_versions) {
8,753✔
586
         buf.push_back(version.major_version());
4,902✔
587
         buf.push_back(version.minor_version());
4,902✔
588
      }
589
   }
590

591
   return buf;
4,252✔
592
}
×
593

594
Supported_Versions::Supported_Versions(Protocol_Version offer, const Policy& policy) {
3,376✔
595
   if(offer.is_datagram_protocol()) {
3,376✔
596
#if defined(BOTAN_HAS_TLS_12)
597
      if(offer >= Protocol_Version::DTLS_V12 && policy.allow_dtls12()) {
352✔
598
         m_versions.push_back(Protocol_Version::DTLS_V12);
352✔
599
      }
600
#endif
601
   } else {
602
#if defined(BOTAN_HAS_TLS_13)
603
      if(offer >= Protocol_Version::TLS_V13 && policy.allow_tls13()) {
5,098✔
604
         m_versions.push_back(Protocol_Version::TLS_V13);
950✔
605
      }
606
#endif
607
#if defined(BOTAN_HAS_TLS_12)
608
      if(offer >= Protocol_Version::TLS_V12 && policy.allow_tls12()) {
3,974✔
609
         m_versions.push_back(Protocol_Version::TLS_V12);
3,004✔
610
      }
611
#endif
612
   }
613
}
3,376✔
614

615
Supported_Versions::Supported_Versions(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
1,642✔
616
   if(from == Connection_Side::Server) {
1,642✔
617
      if(extension_size != 2) {
512✔
618
         throw Decoding_Error("Server sent invalid supported_versions extension");
×
619
      }
620
      m_versions.push_back(Protocol_Version(reader.get_uint16_t()));
512✔
621
   } else {
622
      auto versions = reader.get_range<uint16_t>(1, 1, 127);
1,130✔
623

624
      for(auto v : versions) {
3,962✔
625
         m_versions.push_back(Protocol_Version(v));
2,832✔
626
      }
627

628
      if(extension_size != 1 + 2 * versions.size()) {
1,130✔
629
         throw Decoding_Error("Client sent invalid supported_versions extension");
×
630
      }
631
   }
1,130✔
632
}
1,642✔
633

634
bool Supported_Versions::supports(Protocol_Version version) const {
1,398✔
635
   for(auto v : m_versions) {
1,874✔
636
      if(version == v) {
1,858✔
637
         return true;
1,398✔
638
      }
639
   }
640
   return false;
641
}
642

643
Record_Size_Limit::Record_Size_Limit(const uint16_t limit) : m_limit(limit) {
12✔
644
   BOTAN_ASSERT(limit >= 64, "RFC 8449 does not allow record size limits smaller than 64 bytes");
12✔
645
   BOTAN_ASSERT(limit <= MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */,
12✔
646
                "RFC 8449 does not allow record size limits larger than 2^14+1");
647
}
12✔
648

649
Record_Size_Limit::Record_Size_Limit(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
40✔
650
   if(extension_size != 2) {
40✔
651
      throw TLS_Exception(Alert::DecodeError, "invalid record_size_limit extension");
×
652
   }
653

654
   m_limit = reader.get_uint16_t();
40✔
655

656
   // RFC 8449 4.
657
   //    This value is the length of the plaintext of a protected record.
658
   //    The value includes the content type and padding added in TLS 1.3 (that
659
   //    is, the complete length of TLSInnerPlaintext).
660
   //
661
   //    A server MUST NOT enforce this restriction; a client might advertise
662
   //    a higher limit that is enabled by an extension or version the server
663
   //    does not understand. A client MAY abort the handshake with an
664
   //    "illegal_parameter" alert.
665
   //
666
   // Note: We are currently supporting this extension in TLS 1.3 only, hence
667
   //       we check for the TLS 1.3 limit. The TLS 1.2 limit would not include
668
   //       the "content type byte" and hence be one byte less!
669
   if(m_limit > MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */ && from == Connection_Side::Server) {
40✔
670
      throw TLS_Exception(Alert::IllegalParameter,
×
671
                          "Server requested a record size limit larger than the protocol's maximum");
×
672
   }
673

674
   // RFC 8449 4.
675
   //    Endpoints MUST NOT send a "record_size_limit" extension with a value
676
   //    smaller than 64.  An endpoint MUST treat receipt of a smaller value
677
   //    as a fatal error and generate an "illegal_parameter" alert.
678
   if(m_limit < 64) {
40✔
679
      throw TLS_Exception(Alert::IllegalParameter, "Received a record size limit smaller than 64 bytes");
×
680
   }
681
}
40✔
682

683
std::vector<uint8_t> Record_Size_Limit::serialize(Connection_Side) const {
39✔
684
   std::vector<uint8_t> buf;
39✔
685

686
   buf.push_back(get_byte<0>(m_limit));
39✔
687
   buf.push_back(get_byte<1>(m_limit));
39✔
688

689
   return buf;
39✔
690
}
×
691

692
#if defined(BOTAN_HAS_TLS_13)
693
Cookie::Cookie(const std::vector<uint8_t>& cookie) : m_cookie(cookie) {}
4✔
694

695
Cookie::Cookie(TLS_Data_Reader& reader, uint16_t extension_size) {
12✔
696
   if(extension_size == 0) {
12✔
697
      return;
698
   }
699

700
   const uint16_t len = reader.get_uint16_t();
12✔
701

702
   if(len == 0) {
12✔
703
      // Based on RFC 8446 4.2.2, len of the Cookie buffer must be at least 1
704
      throw Decoding_Error("Cookie length must be at least 1 byte");
1✔
705
   }
706

707
   if(len > reader.remaining_bytes()) {
11✔
708
      throw Decoding_Error("Not enough bytes in the buffer to decode Cookie");
×
709
   }
710

711
   for(size_t i = 0; i < len; ++i) {
719✔
712
      m_cookie.push_back(reader.get_byte());
708✔
713
   }
714
}
1✔
715

716
std::vector<uint8_t> Cookie::serialize(Connection_Side /*whoami*/) const {
10✔
717
   std::vector<uint8_t> buf;
10✔
718

719
   const uint16_t len = static_cast<uint16_t>(m_cookie.size());
10✔
720

721
   buf.push_back(get_byte<0>(len));
10✔
722
   buf.push_back(get_byte<1>(len));
10✔
723

724
   for(const auto& cookie_byte : m_cookie) {
712✔
725
      buf.push_back(cookie_byte);
702✔
726
   }
727

728
   return buf;
10✔
729
}
×
730

731
std::vector<uint8_t> PSK_Key_Exchange_Modes::serialize(Connection_Side) const {
1,088✔
732
   std::vector<uint8_t> buf;
1,088✔
733

734
   BOTAN_ASSERT_NOMSG(m_modes.size() < 256);
1,088✔
735
   buf.push_back(static_cast<uint8_t>(m_modes.size()));
1,088✔
736
   for(const auto& mode : m_modes) {
2,176✔
737
      buf.push_back(static_cast<uint8_t>(mode));
1,088✔
738
   }
739

740
   return buf;
1,088✔
741
}
×
742

743
PSK_Key_Exchange_Modes::PSK_Key_Exchange_Modes(TLS_Data_Reader& reader, uint16_t extension_size) {
931✔
744
   if(extension_size < 2) {
931✔
745
      throw Decoding_Error("Empty psk_key_exchange_modes extension is illegal");
×
746
   }
747

748
   const auto mode_count = reader.get_byte();
931✔
749
   for(uint16_t i = 0; i < mode_count; ++i) {
1,866✔
750
      const auto mode = static_cast<PSK_Key_Exchange_Mode>(reader.get_byte());
935✔
751
      if(mode == PSK_Key_Exchange_Mode::PSK_KE || mode == PSK_Key_Exchange_Mode::PSK_DHE_KE) {
935✔
752
         m_modes.push_back(mode);
929✔
753
      }
754
   }
755
}
931✔
756

757
std::vector<uint8_t> Certificate_Authorities::serialize(Connection_Side) const {
×
758
   throw Not_Implemented("serializing Certificate_Authorities is NYI");
×
759
}
760

761
Certificate_Authorities::Certificate_Authorities(TLS_Data_Reader& reader, uint16_t extension_size) {
3✔
762
   if(extension_size < 2) {
3✔
763
      throw Decoding_Error("Empty certificate_authorities extension is illegal");
×
764
   }
765

766
   const uint16_t purported_size = reader.get_uint16_t();
3✔
767

768
   if(reader.remaining_bytes() != purported_size) {
3✔
769
      throw Decoding_Error("Inconsistent length in certificate_authorities extension");
×
770
   }
771

772
   while(reader.has_remaining()) {
9✔
773
      std::vector<uint8_t> name_bits = reader.get_tls_length_value(2);
6✔
774

775
      BER_Decoder decoder(name_bits.data(), name_bits.size());
6✔
776
      m_distinguished_names.emplace_back();
6✔
777
      decoder.decode(m_distinguished_names.back());
6✔
778
   }
12✔
779
}
3✔
780

781
Certificate_Authorities::Certificate_Authorities(std::vector<X509_DN> acceptable_DNs) :
×
782
      m_distinguished_names(std::move(acceptable_DNs)) {}
×
783

784
std::vector<uint8_t> EarlyDataIndication::serialize(Connection_Side) const {
8✔
785
   std::vector<uint8_t> result;
8✔
786
   if(m_max_early_data_size.has_value()) {
8✔
787
      const auto max_data = m_max_early_data_size.value();
2✔
788
      result.push_back(get_byte<0>(max_data));
2✔
789
      result.push_back(get_byte<1>(max_data));
2✔
790
      result.push_back(get_byte<2>(max_data));
2✔
791
      result.push_back(get_byte<3>(max_data));
2✔
792
   }
793
   return result;
8✔
794
}
×
795

796
EarlyDataIndication::EarlyDataIndication(TLS_Data_Reader& reader,
5✔
797
                                         uint16_t extension_size,
798
                                         Handshake_Type message_type) {
5✔
799
   if(message_type == Handshake_Type::NewSessionTicket) {
5✔
800
      if(extension_size != 4) {
1✔
801
         throw TLS_Exception(Alert::DecodeError,
×
802
                             "Received an early_data extension in a NewSessionTicket message "
803
                             "without maximum early data size indication");
×
804
      }
805

806
      m_max_early_data_size = reader.get_uint32_t();
1✔
807
   } else if(extension_size != 0) {
4✔
808
      throw TLS_Exception(Alert::DecodeError,
×
809
                          "Received an early_data extension containing an unexpected data "
810
                          "size indication");
×
811
   }
812
}
5✔
813

814
bool EarlyDataIndication::empty() const {
8✔
815
   // This extension may be empty by definition but still carry information
816
   return false;
8✔
817
}
818

819
#endif
820
}  // 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