Botan 3.11.1
Crypto and TLS for C&
tls_extensions.cpp
Go to the documentation of this file.
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
23#if defined(BOTAN_HAS_TLS_13)
24 #include <botan/tls_extensions_13.h>
25#endif
26
27#if defined(BOTAN_HAS_TLS_12)
28 #include <botan/tls_extensions_12.h>
29#endif
30
31namespace Botan::TLS {
32
33namespace {
34
35std::unique_ptr<Extension> make_extension(TLS_Data_Reader& reader,
36 Extension_Code code,
37 const Connection_Side from,
38 const Handshake_Type message_type) {
39 // This cast is safe because we read exactly a 16 bit length field for
40 // the extension in Extensions::deserialize
41 const uint16_t size = static_cast<uint16_t>(reader.remaining_bytes());
42 switch(code) {
44 return std::make_unique<Server_Name_Indicator>(reader, size, from);
45
47 return std::make_unique<Supported_Groups>(reader, size);
48
50 return std::make_unique<Certificate_Status_Request>(reader, size, message_type, from);
51
53 return std::make_unique<Signature_Algorithms>(reader, size);
54
56 return std::make_unique<Signature_Algorithms_Cert>(reader, size);
57
59 return std::make_unique<SRTP_Protection_Profiles>(reader, size);
60
62 return std::make_unique<Application_Layer_Protocol_Notification>(reader, size, from);
63
65 return std::make_unique<Client_Certificate_Type>(reader, size, from);
66
68 return std::make_unique<Server_Certificate_Type>(reader, size, from);
69
71 return std::make_unique<Record_Size_Limit>(reader, size, from);
72
74 return std::make_unique<Supported_Versions>(reader, size, from);
75
76#if defined(BOTAN_HAS_TLS_12)
78 return std::make_unique<Supported_Point_Formats>(reader, size);
79
81 return std::make_unique<Renegotiation_Extension>(reader, size);
82
84 return std::make_unique<Extended_Master_Secret>(reader, size);
85
87 return std::make_unique<Encrypt_then_MAC>(reader, size);
88
90 return std::make_unique<Session_Ticket_Extension>(reader, size);
91#else
97 break; // considered as 'unknown extension'
98#endif
99
100#if defined(BOTAN_HAS_TLS_13)
102 return std::make_unique<PSK>(reader, size, message_type);
103
105 return std::make_unique<EarlyDataIndication>(reader, size, message_type);
106
108 return std::make_unique<Cookie>(reader, size);
109
111 return std::make_unique<PSK_Key_Exchange_Modes>(reader, size);
112
114 return std::make_unique<Certificate_Authorities>(reader, size);
115
117 return std::make_unique<Key_Share>(reader, size, message_type);
118#else
125 break; // considered as 'unknown extension'
126#endif
127 }
128
129 return std::make_unique<Unknown_Extension>(code, reader, size);
130}
131
132} // namespace
133
134Extensions::~Extensions() = default;
135
137 const auto i =
138 std::find_if(m_extensions.cbegin(), m_extensions.cend(), [type](const auto& ext) { return ext->type() == type; });
139
140 return (i != m_extensions.end()) ? i->get() : nullptr;
141}
142
143void Extensions::add(std::unique_ptr<Extension> extn) {
144 if(has(extn->type())) {
145 throw Invalid_Argument("cannot add the same extension twice: " +
146 std::to_string(static_cast<uint16_t>(extn->type())));
147 }
148
149 m_extensions.emplace_back(extn.release());
150}
151
152void Extensions::deserialize(TLS_Data_Reader& reader, const Connection_Side from, const Handshake_Type message_type) {
153 if(reader.has_remaining()) {
154 const uint16_t all_extn_size = reader.get_uint16_t();
155
156 if(reader.remaining_bytes() != all_extn_size) {
157 throw Decoding_Error("Bad extension size");
158 }
159
160 while(reader.has_remaining()) {
161 const uint16_t extension_code = reader.get_uint16_t();
162 const uint16_t extension_size = reader.get_uint16_t();
163
164 const auto type = static_cast<Extension_Code>(extension_code);
165
166 if(this->has(type)) {
167 throw TLS_Exception(TLS::Alert::DecodeError, "Peer sent duplicated extensions");
168 }
170 // TODO offer a function on reader that returns a byte range as a reference
171 // to avoid this copy of the extension data
172 const std::vector<uint8_t> extn_data = reader.get_fixed<uint8_t>(extension_size);
173 TLS_Data_Reader extn_reader("Extension", extn_data);
174 this->add(make_extension(extn_reader, type, from, message_type));
175 extn_reader.assert_done();
176 }
177 }
178}
180bool Extensions::contains_other_than(const std::set<Extension_Code>& allowed_extensions,
181 const bool allow_unknown_extensions) const {
182 const auto found = extension_types();
183
184 std::vector<Extension_Code> diff;
185 std::set_difference(
186 found.cbegin(), found.end(), allowed_extensions.cbegin(), allowed_extensions.cend(), std::back_inserter(diff));
187
188 if(allow_unknown_extensions) {
189 // Go through the found unexpected extensions whether any of those
190 // is known to this TLS implementation.
191 const auto itr = std::find_if(diff.cbegin(), diff.cend(), [this](const auto ext_type) {
192 const auto ext = get(ext_type);
193 return ext && ext->is_implemented();
194 });
195
196 // ... if yes, `contains_other_than` is true
197 return itr != diff.cend();
198 }
199
200 return !diff.empty();
201}
202
203std::unique_ptr<Extension> Extensions::take(Extension_Code type) {
204 const auto i =
205 std::find_if(m_extensions.begin(), m_extensions.end(), [type](const auto& ext) { return ext->type() == type; });
206
207 std::unique_ptr<Extension> result;
208 if(i != m_extensions.end()) {
209 std::swap(result, *i);
210 m_extensions.erase(i);
211 }
212
213 return result;
214}
215
216std::vector<uint8_t> Extensions::serialize(Connection_Side whoami) const {
217 std::vector<uint8_t> buf(2); // 2 bytes for length field
218
219 for(const auto& extn : m_extensions) {
220 if(extn->empty()) {
221 continue;
222 }
223
224 const uint16_t extn_code = static_cast<uint16_t>(extn->type());
225
226 const std::vector<uint8_t> extn_val = extn->serialize(whoami);
227
228 buf.push_back(get_byte<0>(extn_code));
229 buf.push_back(get_byte<1>(extn_code));
230
231 buf.push_back(get_byte<0>(static_cast<uint16_t>(extn_val.size())));
232 buf.push_back(get_byte<1>(static_cast<uint16_t>(extn_val.size())));
233
234 buf += extn_val;
235 }
236
237 const uint16_t extn_size = static_cast<uint16_t>(buf.size() - 2);
238
239 buf[0] = get_byte<0>(extn_size);
240 buf[1] = get_byte<1>(extn_size);
241
242 // avoid sending a completely empty extensions block
243 if(buf.size() == 2) {
244 return std::vector<uint8_t>();
245 }
246
247 return buf;
248}
249
250std::set<Extension_Code> Extensions::extension_types() const {
251 std::set<Extension_Code> offers;
252 std::transform(
253 m_extensions.cbegin(), m_extensions.cend(), std::inserter(offers, offers.begin()), [](const auto& ext) {
254 return ext->type();
255 });
256 return offers;
257}
258
260 m_type(type), m_value(reader.get_fixed<uint8_t>(extension_size)) {}
261
262std::vector<uint8_t> Unknown_Extension::serialize(Connection_Side /*whoami*/) const {
263 return m_value;
264}
265
267 /*
268 RFC 6066 Section 3
269
270 A server that receives a client hello containing the "server_name"
271 extension MAY use the information contained in the extension to guide
272 its selection of an appropriate certificate to return to the client,
273 and/or other aspects of security policy. In this event, the server
274 SHALL include an extension of type "server_name" in the (extended)
275 server hello. The "extension_data" field of this extension SHALL be
276 empty.
277 */
278 if(from == Connection_Side::Server) {
279 if(extension_size != 0) {
280 throw TLS_Exception(Alert::IllegalParameter, "Server sent non-empty SNI extension");
281 }
282 } else {
283 // Clients are required to send at least one name in the SNI
284 if(extension_size == 0) {
285 throw TLS_Exception(Alert::IllegalParameter, "Client sent empty SNI extension");
286 }
287
288 const uint16_t name_bytes = reader.get_uint16_t();
289
290 if(name_bytes + 2 != extension_size || name_bytes < 3) {
291 throw Decoding_Error("Bad encoding of SNI extension");
292 }
293
294 BOTAN_ASSERT_NOMSG(reader.remaining_bytes() == name_bytes);
295
296 while(reader.has_remaining()) {
297 const uint8_t name_type = reader.get_byte();
298
299 if(name_type == 0) {
300 /*
301 RFC 6066 Section 3
302 The ServerNameList MUST NOT contain more than one name of the same name_type.
303 */
304 if(!m_sni_host_name.empty()) {
305 throw Decoding_Error("TLS ServerNameIndicator contains more than one host_name");
306 }
307 m_sni_host_name = reader.get_string(2, 1, 65535);
308 } else {
309 /*
310 Unknown name type - skip its length-prefixed value and continue
311
312 RFC 6066 Section 3
313 For backward compatibility, all future data structures associated
314 with new NameTypes MUST begin with a 16-bit length field.
315 */
316 const uint16_t unknown_name_len = reader.get_uint16_t();
317 reader.discard_next(unknown_name_len);
318 }
319 }
320 }
321}
322
323std::vector<uint8_t> Server_Name_Indicator::serialize(Connection_Side whoami) const {
324 // RFC 6066
325 // [...] the server SHALL include an extension of type "server_name" in
326 // the (extended) server hello. The "extension_data" field of this
327 // extension SHALL be empty.
328 if(whoami == Connection_Side::Server) {
329 return {};
330 }
331
332 std::vector<uint8_t> buf;
333
334 const size_t name_len = m_sni_host_name.size();
335
336 buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len + 3)));
337 buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len + 3)));
338 buf.push_back(0); // DNS
339
340 buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len)));
341 buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len)));
342
343 buf += as_span_of_bytes(m_sni_host_name);
344
345 return buf;
346}
347
349 // Avoid sending an IPv4/IPv6 address in SNI as this is prohibited
350
351 if(hostname.empty()) {
352 return false;
353 }
354
355 if(string_to_ipv4(hostname).has_value()) {
356 return false;
357 }
358
359 // IPv6? Anyway ':' is not valid in DNS
360 if(hostname.find(':') != std::string_view::npos) {
361 return false;
362 }
363
364 return true;
365}
366
368 uint16_t extension_size,
369 Connection_Side from) {
370 if(extension_size == 0) {
371 return; // empty extension
372 }
373
374 const uint16_t name_bytes = reader.get_uint16_t();
375
376 size_t bytes_remaining = extension_size - 2;
377
378 if(name_bytes != bytes_remaining) {
379 throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
380 }
381
382 while(bytes_remaining > 0) {
383 const std::string p = reader.get_string(1, 0, 255);
384
385 if(bytes_remaining < p.size() + 1) {
386 throw Decoding_Error("Bad encoding of ALPN, length field too long");
387 }
388
389 if(p.empty()) {
390 throw Decoding_Error("Empty ALPN protocol not allowed");
391 }
392
393 bytes_remaining -= (p.size() + 1);
394
395 m_protocols.push_back(p);
396 }
397
398 // RFC 7301 3.1
399 // The "extension_data" field of the [...] extension is structured the
400 // same as described above for the client "extension_data", except that
401 // the "ProtocolNameList" MUST contain exactly one "ProtocolName".
402 if(from == Connection_Side::Server && m_protocols.size() != 1) {
403 throw TLS_Exception(
404 Alert::DecodeError,
405 "Server sent " + std::to_string(m_protocols.size()) + " protocols in ALPN extension response");
406 }
407}
408
410 BOTAN_STATE_CHECK(m_protocols.size() == 1);
411 return m_protocols.front();
412}
413
415 std::vector<uint8_t> buf(2);
416
417 for(auto&& proto : m_protocols) {
418 if(proto.length() >= 256) {
419 throw TLS_Exception(Alert::InternalError, "ALPN name too long");
420 }
421 if(!proto.empty()) {
422 append_tls_length_value(buf, proto, 1);
423 }
424 }
425
426 buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
427 buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
428
429 return buf;
430}
431
432Certificate_Type_Base::Certificate_Type_Base(std::vector<Certificate_Type> supported_cert_types) :
433 m_certificate_types(std::move(supported_cert_types)), m_from(Connection_Side::Client) {
434 BOTAN_ARG_CHECK(!m_certificate_types.empty(), "at least one certificate type must be supported");
435}
436
438 Certificate_Type_Base(cct, policy.accepted_client_certificate_types()) {}
439
441 Certificate_Type_Base(sct, policy.accepted_server_certificate_types()) {}
442
444 const std::vector<Certificate_Type>& server_preference) :
445 m_from(Connection_Side::Server) {
446 // RFC 7250 4.2
447 // The server_certificate_type extension in the client hello indicates the
448 // types of certificates the client is able to process when provided by
449 // the server in a subsequent certificate payload. [...] With the
450 // server_certificate_type extension in the server hello, the TLS server
451 // indicates the certificate type carried in the Certificate payload.
452 for(const auto server_supported_cert_type : server_preference) {
453 if(value_exists(certificate_type_from_client.m_certificate_types, server_supported_cert_type)) {
454 m_certificate_types.push_back(server_supported_cert_type);
455 return;
456 }
457 }
458
459 // RFC 7250 4.2 (2.)
460 // The server supports the extension defined in this document, but
461 // it does not have any certificate type in common with the client.
462 // Then, the server terminates the session with a fatal alert of
463 // type "unsupported_certificate".
464 throw TLS_Exception(Alert::UnsupportedCertificate, "Failed to agree on certificate_type");
465}
466
468 m_from(from) {
469 if(extension_size == 0) {
470 throw Decoding_Error("Certificate type extension cannot be empty");
471 }
472
473 if(from == Connection_Side::Client) {
474 const auto type_bytes = reader.get_tls_length_value(1);
475 if(static_cast<size_t>(extension_size) != type_bytes.size() + 1) {
476 throw Decoding_Error("certificate type extension had inconsistent length");
477 }
478 std::transform(
479 type_bytes.begin(), type_bytes.end(), std::back_inserter(m_certificate_types), [](const auto type_byte) {
480 return static_cast<Certificate_Type>(type_byte);
481 });
482 } else {
483 // RFC 7250 4.2
484 // Note that only a single value is permitted in the
485 // server_certificate_type extension when carried in the server hello.
486 if(extension_size != 1) {
487 throw Decoding_Error("Server's certificate type extension must be of length 1");
488 }
489 const auto type_byte = reader.get_byte();
490 m_certificate_types.push_back(static_cast<Certificate_Type>(type_byte));
491 }
492}
493
494std::vector<uint8_t> Certificate_Type_Base::serialize(Connection_Side whoami) const {
495 std::vector<uint8_t> result;
496 if(whoami == Connection_Side::Client) {
497 std::vector<uint8_t> type_bytes;
498 std::transform(
499 m_certificate_types.begin(), m_certificate_types.end(), std::back_inserter(type_bytes), [](const auto type) {
500 return static_cast<uint8_t>(type);
501 });
502 append_tls_length_value(result, type_bytes, 1);
503 } else {
504 BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
505 result.push_back(static_cast<uint8_t>(m_certificate_types.front()));
506 }
507 return result;
508}
509
512 BOTAN_ASSERT_NOMSG(from_server.m_from == Connection_Side::Server);
513
514 // RFC 7250 4.2
515 // The value conveyed in the [client_]certificate_type extension MUST be
516 // selected from one of the values provided in the [client_]certificate_type
517 // extension sent in the client hello.
518 if(!value_exists(m_certificate_types, from_server.selected_certificate_type())) {
519 throw TLS_Exception(Alert::IllegalParameter,
520 Botan::fmt("Selected certificate type was not offered: {}",
522 }
523}
524
527 BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
528 return m_certificate_types.front();
529}
530
531Supported_Groups::Supported_Groups(const std::vector<Group_Params>& groups) : m_groups(groups) {}
532
533const std::vector<Group_Params>& Supported_Groups::groups() const {
534 return m_groups;
535}
536
537std::vector<Group_Params> Supported_Groups::ec_groups() const {
538 std::vector<Group_Params> ec;
539 for(auto g : m_groups) {
540 if(g.is_pure_ecc_group()) {
541 ec.push_back(g);
542 }
543 }
544 return ec;
545}
546
547std::vector<Group_Params> Supported_Groups::dh_groups() const {
548 std::vector<Group_Params> dh;
549 for(auto g : m_groups) {
550 if(g.is_in_ffdhe_range()) {
551 dh.push_back(g);
552 }
553 }
554 return dh;
555}
556
557std::vector<uint8_t> Supported_Groups::serialize(Connection_Side /*whoami*/) const {
558 std::vector<uint8_t> buf(2);
559
560 for(auto g : m_groups) {
561 const uint16_t id = g.wire_code();
562
563 if(id > 0) {
564 buf.push_back(get_byte<0>(id));
565 buf.push_back(get_byte<1>(id));
566 }
567 }
568
569 buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
570 buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
571
572 return buf;
573}
574
575Supported_Groups::Supported_Groups(TLS_Data_Reader& reader, uint16_t extension_size) {
576 const uint16_t len = reader.get_uint16_t();
577
578 if(len + 2 != extension_size) {
579 throw Decoding_Error("Inconsistent length field in supported groups list");
580 }
581
582 if(len % 2 == 1) {
583 throw Decoding_Error("Supported groups list of strange size");
584 }
585
586 const size_t elems = len / 2;
587
588 for(size_t i = 0; i != elems; ++i) {
589 const auto group = static_cast<Group_Params>(reader.get_uint16_t());
590 // Note: RFC 8446 does not explicitly enforce that groups must be unique.
591 if(!value_exists(m_groups, group)) {
592 m_groups.push_back(group);
593 }
594 }
595}
596
597namespace {
598
599std::vector<uint8_t> serialize_signature_algorithms(const std::vector<Signature_Scheme>& schemes) {
600 BOTAN_ASSERT(schemes.size() < 256, "Too many signature schemes");
601
602 std::vector<uint8_t> buf;
603
604 const uint16_t len = static_cast<uint16_t>(schemes.size() * 2);
605
606 buf.push_back(get_byte<0>(len));
607 buf.push_back(get_byte<1>(len));
608
609 for(const Signature_Scheme scheme : schemes) {
610 buf.push_back(get_byte<0>(scheme.wire_code()));
611 buf.push_back(get_byte<1>(scheme.wire_code()));
612 }
613
614 return buf;
615}
616
617std::vector<Signature_Scheme> parse_signature_algorithms(TLS_Data_Reader& reader, uint16_t extension_size) {
618 uint16_t len = reader.get_uint16_t();
619
620 if(len + 2 != extension_size || len % 2 == 1 || len == 0) {
621 throw Decoding_Error("Bad encoding on signature algorithms extension");
622 }
623
624 std::vector<Signature_Scheme> schemes;
625 schemes.reserve(len / 2);
626 while(len > 0) {
627 schemes.emplace_back(reader.get_uint16_t());
628 len -= 2;
629 }
630
631 return schemes;
632}
633
634} // namespace
635
636std::vector<uint8_t> Signature_Algorithms::serialize(Connection_Side /*whoami*/) const {
637 return serialize_signature_algorithms(m_schemes);
638}
639
641 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
642
643std::vector<uint8_t> Signature_Algorithms_Cert::serialize(Connection_Side /*whoami*/) const {
644 return serialize_signature_algorithms(m_schemes);
645}
646
648 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
649
651 m_pp(reader.get_range<uint16_t>(2, 0, 65535)) {
652 const std::vector<uint8_t> mki = reader.get_range<uint8_t>(1, 0, 255);
653
654 if(m_pp.size() * 2 + mki.size() + 3 != extension_size) {
655 throw Decoding_Error("Bad encoding for SRTP protection extension");
656 }
657
658 if(!mki.empty()) {
659 throw Decoding_Error("Unhandled non-empty MKI for SRTP protection extension");
660 }
661}
662
663std::vector<uint8_t> SRTP_Protection_Profiles::serialize(Connection_Side /*whoami*/) const {
664 std::vector<uint8_t> buf;
665
666 const uint16_t pp_len = static_cast<uint16_t>(m_pp.size() * 2);
667 buf.push_back(get_byte<0>(pp_len));
668 buf.push_back(get_byte<1>(pp_len));
669
670 for(const uint16_t pp : m_pp) {
671 buf.push_back(get_byte<0>(pp));
672 buf.push_back(get_byte<1>(pp));
673 }
674
675 buf.push_back(0); // srtp_mki, always empty here
676
677 return buf;
678}
679
680std::vector<uint8_t> Supported_Versions::serialize(Connection_Side whoami) const {
681 std::vector<uint8_t> buf;
682
683 if(whoami == Connection_Side::Server) {
684 BOTAN_ASSERT_NOMSG(m_versions.size() == 1);
685 buf.push_back(m_versions[0].major_version());
686 buf.push_back(m_versions[0].minor_version());
687 } else {
688 BOTAN_ASSERT_NOMSG(!m_versions.empty());
689 const uint8_t len = static_cast<uint8_t>(m_versions.size() * 2);
690
691 buf.push_back(len);
692
693 for(const Protocol_Version version : m_versions) {
694 buf.push_back(version.major_version());
695 buf.push_back(version.minor_version());
696 }
697 }
698
699 return buf;
700}
701
703 // RFC 8446 4.2.1
704 // The extension contains a list of supported versions in preference order,
705 // with the most preferred version first. Implementations [...] MUST send
706 // this extension in the ClientHello containing all versions of TLS which
707 // they are prepared to negotiate.
708 //
709 // We simply assume that we always want the newest available TLS version.
710#if defined(BOTAN_HAS_TLS_13)
711 if(!offer.is_datagram_protocol()) {
712 if(offer >= Protocol_Version::TLS_V13 && policy.allow_tls13()) {
713 m_versions.push_back(Protocol_Version::TLS_V13);
714 }
715 }
716#endif
717
718#if defined(BOTAN_HAS_TLS_12)
719 if(offer.is_datagram_protocol()) {
720 if(offer >= Protocol_Version::DTLS_V12 && policy.allow_dtls12()) {
721 m_versions.push_back(Protocol_Version::DTLS_V12);
722 }
723 } else {
724 if(offer >= Protocol_Version::TLS_V12 && policy.allow_tls12()) {
725 m_versions.push_back(Protocol_Version::TLS_V12);
726 }
727 }
728#endif
729
730 // if no versions are supported, the input variables are not used
731 BOTAN_UNUSED(offer, policy);
732}
733
735 if(from == Connection_Side::Server) {
736 if(extension_size != 2) {
737 throw Decoding_Error("Server sent invalid supported_versions extension");
738 }
739 m_versions.push_back(Protocol_Version(reader.get_uint16_t()));
740 } else {
741 auto versions = reader.get_range<uint16_t>(1, 1, 127);
742
743 for(auto v : versions) {
744 m_versions.push_back(Protocol_Version(v));
745 }
746
747 if(extension_size != 1 + 2 * versions.size()) {
748 throw Decoding_Error("Client sent invalid supported_versions extension");
749 }
750 }
751}
752
754 for(auto v : m_versions) {
755 if(version == v) {
756 return true;
757 }
758 }
759 return false;
760}
761
763 BOTAN_ASSERT(limit >= 64, "RFC 8449 does not allow record size limits smaller than 64 bytes");
764 BOTAN_ASSERT(limit <= MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */,
765 "RFC 8449 does not allow record size limits larger than 2^14+1");
766}
767
769 if(extension_size != 2) {
770 throw TLS_Exception(Alert::DecodeError, "invalid record_size_limit extension");
771 }
772
773 m_limit = reader.get_uint16_t();
774
775 // RFC 8449 4.
776 // This value is the length of the plaintext of a protected record.
777 // The value includes the content type and padding added in TLS 1.3 (that
778 // is, the complete length of TLSInnerPlaintext).
779 //
780 // A server MUST NOT enforce this restriction; a client might advertise
781 // a higher limit that is enabled by an extension or version the server
782 // does not understand. A client MAY abort the handshake with an
783 // "illegal_parameter" alert.
784 //
785 // Note: We are currently supporting this extension in TLS 1.3 only, hence
786 // we check for the TLS 1.3 limit. The TLS 1.2 limit would not include
787 // the "content type byte" and hence be one byte less!
788 if(m_limit > MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */ && from == Connection_Side::Server) {
789 throw TLS_Exception(Alert::IllegalParameter,
790 "Server requested a record size limit larger than the protocol's maximum");
791 }
792
793 // RFC 8449 4.
794 // Endpoints MUST NOT send a "record_size_limit" extension with a value
795 // smaller than 64. An endpoint MUST treat receipt of a smaller value
796 // as a fatal error and generate an "illegal_parameter" alert.
797 if(m_limit < 64) {
798 throw TLS_Exception(Alert::IllegalParameter, "Received a record size limit smaller than 64 bytes");
799 }
800}
801
802std::vector<uint8_t> Record_Size_Limit::serialize(Connection_Side /*whoami*/) const {
803 std::vector<uint8_t> buf;
804
805 buf.push_back(get_byte<0>(m_limit));
806 buf.push_back(get_byte<1>(m_limit));
807
808 return buf;
809}
810
811} // namespace Botan::TLS
#define BOTAN_UNUSED
Definition assert.h:144
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:62
Application_Layer_Protocol_Notification(std::string_view protocol)
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Certificate_Type selected_certificate_type() const
Certificate_Type_Base(std::vector< Certificate_Type > supported_cert_types)
void validate_selection(const Certificate_Type_Base &from_server) const
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Extension_Code type() const override
Client_Certificate_Type(const Client_Certificate_Type &cct, const Policy &policy)
Certificate_Type_Base(std::vector< Certificate_Type > supported_cert_types)
virtual Extension_Code type() const =0
std::vector< uint8_t > serialize(Connection_Side whoami) const
void deserialize(TLS_Data_Reader &reader, Connection_Side from, Handshake_Type message_type)
decltype(auto) take()
std::set< Extension_Code > extension_types() const
void add(std::unique_ptr< Extension > extn)
bool contains_other_than(const std::set< Extension_Code > &allowed_extensions, bool allow_unknown_extensions=false) const
virtual bool allow_tls12() const
virtual bool allow_tls13() const
virtual bool allow_dtls12() const
std::vector< uint8_t > serialize(Connection_Side whoami) const override
SRTP_Protection_Profiles(const std::vector< uint16_t > &pp)
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Server_Certificate_Type(const Server_Certificate_Type &sct, const Policy &policy)
Certificate_Type_Base(std::vector< Certificate_Type > supported_cert_types)
std::vector< uint8_t > serialize(Connection_Side whoami) const override
static bool hostname_acceptable_for_sni(std::string_view hostname)
Server_Name_Indicator(std::string_view host_name)
Signature_Algorithms_Cert(std::vector< Signature_Scheme > schemes)
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Signature_Algorithms(std::vector< Signature_Scheme > schemes)
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Supported_Groups(const std::vector< Group_Params > &groups)
std::vector< Group_Params > ec_groups() const
const std::vector< Group_Params > & groups() const
std::vector< uint8_t > serialize(Connection_Side whoami) const override
std::vector< Group_Params > dh_groups() const
bool supports(Protocol_Version version) const
Supported_Versions(Protocol_Version version, const Policy &policy)
const std::vector< Protocol_Version > & versions() const
std::vector< uint8_t > serialize(Connection_Side whoami) const override
std::string get_string(size_t len_bytes, size_t min_bytes, size_t max_bytes)
Definition tls_reader.h:123
void discard_next(size_t bytes)
Definition tls_reader.h:51
std::vector< T > get_range(size_t len_bytes, size_t min_elems, size_t max_elems)
Definition tls_reader.h:110
size_t remaining_bytes() const
Definition tls_reader.h:37
std::vector< uint8_t > get_tls_length_value(size_t len_bytes)
Definition tls_reader.h:105
std::vector< T > get_fixed(size_t size)
Definition tls_reader.h:129
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Unknown_Extension(Extension_Code type, TLS_Data_Reader &reader, uint16_t extension_size)
Extension_Code type() const override
std::string certificate_type_to_string(Certificate_Type type)
void append_tls_length_value(std::vector< uint8_t, Alloc > &buf, const T *vals, size_t vals_size, size_t tag_size)
Definition tls_reader.h:177
@ MAX_PLAINTEXT_SIZE
Definition tls_magic.h:31
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:59
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
bool value_exists(const std::vector< T > &vec, const V &val)
Definition stl_util.h:43
std::optional< uint32_t > string_to_ipv4(std::string_view str)
Definition parsing.cpp:156