Botan 3.11.0
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);
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 * This is used by the server to confirm that it knew the name
269 */
270 if(extension_size == 0) {
271 return;
272 }
273
274 uint16_t name_bytes = reader.get_uint16_t();
275
276 if(name_bytes + 2 != extension_size) {
277 throw Decoding_Error("Bad encoding of SNI extension");
278 }
279
280 while(name_bytes > 0) {
281 const uint8_t name_type = reader.get_byte();
282 name_bytes--;
283
284 if(name_type == 0) {
285 // DNS
286 m_sni_host_name = reader.get_string(2, 1, 65535);
287 name_bytes -= static_cast<uint16_t>(2 + m_sni_host_name.size());
288 } else {
289 // some other unknown name type, which we will ignore
290 reader.discard_next(name_bytes);
291 name_bytes = 0;
292 }
293 }
294}
295
296std::vector<uint8_t> Server_Name_Indicator::serialize(Connection_Side whoami) const {
297 // RFC 6066
298 // [...] the server SHALL include an extension of type "server_name" in
299 // the (extended) server hello. The "extension_data" field of this
300 // extension SHALL be empty.
301 if(whoami == Connection_Side::Server) {
302 return {};
303 }
304
305 std::vector<uint8_t> buf;
306
307 const size_t name_len = m_sni_host_name.size();
308
309 buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len + 3)));
310 buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len + 3)));
311 buf.push_back(0); // DNS
312
313 buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len)));
314 buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len)));
315
316 buf += as_span_of_bytes(m_sni_host_name);
317
318 return buf;
319}
320
322 // Avoid sending an IPv4/IPv6 address in SNI as this is prohibited
323
324 if(hostname.empty()) {
325 return false;
326 }
327
328 if(string_to_ipv4(hostname).has_value()) {
329 return false;
330 }
331
332 // IPv6? Anyway ':' is not valid in DNS
333 if(hostname.find(':') != std::string_view::npos) {
334 return false;
335 }
336
337 return true;
338}
339
341 uint16_t extension_size,
342 Connection_Side from) {
343 if(extension_size == 0) {
344 return; // empty extension
345 }
346
347 const uint16_t name_bytes = reader.get_uint16_t();
348
349 size_t bytes_remaining = extension_size - 2;
350
351 if(name_bytes != bytes_remaining) {
352 throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
353 }
354
355 while(bytes_remaining > 0) {
356 const std::string p = reader.get_string(1, 0, 255);
357
358 if(bytes_remaining < p.size() + 1) {
359 throw Decoding_Error("Bad encoding of ALPN, length field too long");
360 }
361
362 if(p.empty()) {
363 throw Decoding_Error("Empty ALPN protocol not allowed");
364 }
365
366 bytes_remaining -= (p.size() + 1);
367
368 m_protocols.push_back(p);
369 }
370
371 // RFC 7301 3.1
372 // The "extension_data" field of the [...] extension is structured the
373 // same as described above for the client "extension_data", except that
374 // the "ProtocolNameList" MUST contain exactly one "ProtocolName".
375 if(from == Connection_Side::Server && m_protocols.size() != 1) {
376 throw TLS_Exception(
377 Alert::DecodeError,
378 "Server sent " + std::to_string(m_protocols.size()) + " protocols in ALPN extension response");
379 }
380}
381
383 BOTAN_STATE_CHECK(m_protocols.size() == 1);
384 return m_protocols.front();
385}
386
388 std::vector<uint8_t> buf(2);
389
390 for(auto&& proto : m_protocols) {
391 if(proto.length() >= 256) {
392 throw TLS_Exception(Alert::InternalError, "ALPN name too long");
393 }
394 if(!proto.empty()) {
395 append_tls_length_value(buf, proto, 1);
396 }
397 }
398
399 buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
400 buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
401
402 return buf;
403}
404
405Certificate_Type_Base::Certificate_Type_Base(std::vector<Certificate_Type> supported_cert_types) :
406 m_certificate_types(std::move(supported_cert_types)), m_from(Connection_Side::Client) {
407 BOTAN_ARG_CHECK(!m_certificate_types.empty(), "at least one certificate type must be supported");
408}
409
411 Certificate_Type_Base(cct, policy.accepted_client_certificate_types()) {}
412
414 Certificate_Type_Base(sct, policy.accepted_server_certificate_types()) {}
415
417 const std::vector<Certificate_Type>& server_preference) :
418 m_from(Connection_Side::Server) {
419 // RFC 7250 4.2
420 // The server_certificate_type extension in the client hello indicates the
421 // types of certificates the client is able to process when provided by
422 // the server in a subsequent certificate payload. [...] With the
423 // server_certificate_type extension in the server hello, the TLS server
424 // indicates the certificate type carried in the Certificate payload.
425 for(const auto server_supported_cert_type : server_preference) {
426 if(value_exists(certificate_type_from_client.m_certificate_types, server_supported_cert_type)) {
427 m_certificate_types.push_back(server_supported_cert_type);
428 return;
429 }
430 }
431
432 // RFC 7250 4.2 (2.)
433 // The server supports the extension defined in this document, but
434 // it does not have any certificate type in common with the client.
435 // Then, the server terminates the session with a fatal alert of
436 // type "unsupported_certificate".
437 throw TLS_Exception(Alert::UnsupportedCertificate, "Failed to agree on certificate_type");
438}
439
441 m_from(from) {
442 if(extension_size == 0) {
443 throw Decoding_Error("Certificate type extension cannot be empty");
444 }
445
446 if(from == Connection_Side::Client) {
447 const auto type_bytes = reader.get_tls_length_value(1);
448 if(static_cast<size_t>(extension_size) != type_bytes.size() + 1) {
449 throw Decoding_Error("certificate type extension had inconsistent length");
450 }
451 std::transform(
452 type_bytes.begin(), type_bytes.end(), std::back_inserter(m_certificate_types), [](const auto type_byte) {
453 return static_cast<Certificate_Type>(type_byte);
454 });
455 } else {
456 // RFC 7250 4.2
457 // Note that only a single value is permitted in the
458 // server_certificate_type extension when carried in the server hello.
459 if(extension_size != 1) {
460 throw Decoding_Error("Server's certificate type extension must be of length 1");
461 }
462 const auto type_byte = reader.get_byte();
463 m_certificate_types.push_back(static_cast<Certificate_Type>(type_byte));
464 }
465}
466
467std::vector<uint8_t> Certificate_Type_Base::serialize(Connection_Side whoami) const {
468 std::vector<uint8_t> result;
469 if(whoami == Connection_Side::Client) {
470 std::vector<uint8_t> type_bytes;
471 std::transform(
472 m_certificate_types.begin(), m_certificate_types.end(), std::back_inserter(type_bytes), [](const auto type) {
473 return static_cast<uint8_t>(type);
474 });
475 append_tls_length_value(result, type_bytes, 1);
476 } else {
477 BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
478 result.push_back(static_cast<uint8_t>(m_certificate_types.front()));
479 }
480 return result;
481}
482
485 BOTAN_ASSERT_NOMSG(from_server.m_from == Connection_Side::Server);
486
487 // RFC 7250 4.2
488 // The value conveyed in the [client_]certificate_type extension MUST be
489 // selected from one of the values provided in the [client_]certificate_type
490 // extension sent in the client hello.
491 if(!value_exists(m_certificate_types, from_server.selected_certificate_type())) {
492 throw TLS_Exception(Alert::IllegalParameter,
493 Botan::fmt("Selected certificate type was not offered: {}",
495 }
496}
497
500 BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
501 return m_certificate_types.front();
502}
503
504Supported_Groups::Supported_Groups(const std::vector<Group_Params>& groups) : m_groups(groups) {}
505
506const std::vector<Group_Params>& Supported_Groups::groups() const {
507 return m_groups;
508}
509
510std::vector<Group_Params> Supported_Groups::ec_groups() const {
511 std::vector<Group_Params> ec;
512 for(auto g : m_groups) {
513 if(g.is_pure_ecc_group()) {
514 ec.push_back(g);
515 }
516 }
517 return ec;
518}
519
520std::vector<Group_Params> Supported_Groups::dh_groups() const {
521 std::vector<Group_Params> dh;
522 for(auto g : m_groups) {
523 if(g.is_in_ffdhe_range()) {
524 dh.push_back(g);
525 }
526 }
527 return dh;
528}
529
530std::vector<uint8_t> Supported_Groups::serialize(Connection_Side /*whoami*/) const {
531 std::vector<uint8_t> buf(2);
532
533 for(auto g : m_groups) {
534 const uint16_t id = g.wire_code();
535
536 if(id > 0) {
537 buf.push_back(get_byte<0>(id));
538 buf.push_back(get_byte<1>(id));
539 }
540 }
541
542 buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
543 buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
544
545 return buf;
546}
547
548Supported_Groups::Supported_Groups(TLS_Data_Reader& reader, uint16_t extension_size) {
549 const uint16_t len = reader.get_uint16_t();
550
551 if(len + 2 != extension_size) {
552 throw Decoding_Error("Inconsistent length field in supported groups list");
553 }
554
555 if(len % 2 == 1) {
556 throw Decoding_Error("Supported groups list of strange size");
557 }
558
559 const size_t elems = len / 2;
560
561 for(size_t i = 0; i != elems; ++i) {
562 const auto group = static_cast<Group_Params>(reader.get_uint16_t());
563 // Note: RFC 8446 does not explicitly enforce that groups must be unique.
564 if(!value_exists(m_groups, group)) {
565 m_groups.push_back(group);
566 }
567 }
568}
569
570namespace {
571
572std::vector<uint8_t> serialize_signature_algorithms(const std::vector<Signature_Scheme>& schemes) {
573 BOTAN_ASSERT(schemes.size() < 256, "Too many signature schemes");
574
575 std::vector<uint8_t> buf;
576
577 const uint16_t len = static_cast<uint16_t>(schemes.size() * 2);
578
579 buf.push_back(get_byte<0>(len));
580 buf.push_back(get_byte<1>(len));
581
582 for(const Signature_Scheme scheme : schemes) {
583 buf.push_back(get_byte<0>(scheme.wire_code()));
584 buf.push_back(get_byte<1>(scheme.wire_code()));
585 }
586
587 return buf;
588}
589
590std::vector<Signature_Scheme> parse_signature_algorithms(TLS_Data_Reader& reader, uint16_t extension_size) {
591 uint16_t len = reader.get_uint16_t();
592
593 if(len + 2 != extension_size || len % 2 == 1 || len == 0) {
594 throw Decoding_Error("Bad encoding on signature algorithms extension");
595 }
596
597 std::vector<Signature_Scheme> schemes;
598 schemes.reserve(len / 2);
599 while(len > 0) {
600 schemes.emplace_back(reader.get_uint16_t());
601 len -= 2;
602 }
603
604 return schemes;
605}
606
607} // namespace
608
609std::vector<uint8_t> Signature_Algorithms::serialize(Connection_Side /*whoami*/) const {
610 return serialize_signature_algorithms(m_schemes);
611}
612
614 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
615
616std::vector<uint8_t> Signature_Algorithms_Cert::serialize(Connection_Side /*whoami*/) const {
617 return serialize_signature_algorithms(m_schemes);
618}
619
621 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
622
624 m_pp(reader.get_range<uint16_t>(2, 0, 65535)) {
625 const std::vector<uint8_t> mki = reader.get_range<uint8_t>(1, 0, 255);
626
627 if(m_pp.size() * 2 + mki.size() + 3 != extension_size) {
628 throw Decoding_Error("Bad encoding for SRTP protection extension");
629 }
630
631 if(!mki.empty()) {
632 throw Decoding_Error("Unhandled non-empty MKI for SRTP protection extension");
633 }
634}
635
636std::vector<uint8_t> SRTP_Protection_Profiles::serialize(Connection_Side /*whoami*/) const {
637 std::vector<uint8_t> buf;
638
639 const uint16_t pp_len = static_cast<uint16_t>(m_pp.size() * 2);
640 buf.push_back(get_byte<0>(pp_len));
641 buf.push_back(get_byte<1>(pp_len));
642
643 for(const uint16_t pp : m_pp) {
644 buf.push_back(get_byte<0>(pp));
645 buf.push_back(get_byte<1>(pp));
646 }
647
648 buf.push_back(0); // srtp_mki, always empty here
649
650 return buf;
651}
652
653std::vector<uint8_t> Supported_Versions::serialize(Connection_Side whoami) const {
654 std::vector<uint8_t> buf;
655
656 if(whoami == Connection_Side::Server) {
657 BOTAN_ASSERT_NOMSG(m_versions.size() == 1);
658 buf.push_back(m_versions[0].major_version());
659 buf.push_back(m_versions[0].minor_version());
660 } else {
661 BOTAN_ASSERT_NOMSG(!m_versions.empty());
662 const uint8_t len = static_cast<uint8_t>(m_versions.size() * 2);
663
664 buf.push_back(len);
665
666 for(const Protocol_Version version : m_versions) {
667 buf.push_back(version.major_version());
668 buf.push_back(version.minor_version());
669 }
670 }
671
672 return buf;
673}
674
676 // RFC 8446 4.2.1
677 // The extension contains a list of supported versions in preference order,
678 // with the most preferred version first. Implementations [...] MUST send
679 // this extension in the ClientHello containing all versions of TLS which
680 // they are prepared to negotiate.
681 //
682 // We simply assume that we always want the newest available TLS version.
683#if defined(BOTAN_HAS_TLS_13)
684 if(!offer.is_datagram_protocol()) {
685 if(offer >= Protocol_Version::TLS_V13 && policy.allow_tls13()) {
686 m_versions.push_back(Protocol_Version::TLS_V13);
687 }
688 }
689#endif
690
691#if defined(BOTAN_HAS_TLS_12)
692 if(offer.is_datagram_protocol()) {
693 if(offer >= Protocol_Version::DTLS_V12 && policy.allow_dtls12()) {
694 m_versions.push_back(Protocol_Version::DTLS_V12);
695 }
696 } else {
697 if(offer >= Protocol_Version::TLS_V12 && policy.allow_tls12()) {
698 m_versions.push_back(Protocol_Version::TLS_V12);
699 }
700 }
701#endif
702
703 // if no versions are supported, the input variables are not used
704 BOTAN_UNUSED(offer, policy);
705}
706
708 if(from == Connection_Side::Server) {
709 if(extension_size != 2) {
710 throw Decoding_Error("Server sent invalid supported_versions extension");
711 }
712 m_versions.push_back(Protocol_Version(reader.get_uint16_t()));
713 } else {
714 auto versions = reader.get_range<uint16_t>(1, 1, 127);
715
716 for(auto v : versions) {
717 m_versions.push_back(Protocol_Version(v));
718 }
719
720 if(extension_size != 1 + 2 * versions.size()) {
721 throw Decoding_Error("Client sent invalid supported_versions extension");
722 }
723 }
724}
725
727 for(auto v : m_versions) {
728 if(version == v) {
729 return true;
730 }
731 }
732 return false;
733}
734
736 BOTAN_ASSERT(limit >= 64, "RFC 8449 does not allow record size limits smaller than 64 bytes");
737 BOTAN_ASSERT(limit <= MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */,
738 "RFC 8449 does not allow record size limits larger than 2^14+1");
739}
740
742 if(extension_size != 2) {
743 throw TLS_Exception(Alert::DecodeError, "invalid record_size_limit extension");
744 }
745
746 m_limit = reader.get_uint16_t();
747
748 // RFC 8449 4.
749 // This value is the length of the plaintext of a protected record.
750 // The value includes the content type and padding added in TLS 1.3 (that
751 // is, the complete length of TLSInnerPlaintext).
752 //
753 // A server MUST NOT enforce this restriction; a client might advertise
754 // a higher limit that is enabled by an extension or version the server
755 // does not understand. A client MAY abort the handshake with an
756 // "illegal_parameter" alert.
757 //
758 // Note: We are currently supporting this extension in TLS 1.3 only, hence
759 // we check for the TLS 1.3 limit. The TLS 1.2 limit would not include
760 // the "content type byte" and hence be one byte less!
761 if(m_limit > MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */ && from == Connection_Side::Server) {
762 throw TLS_Exception(Alert::IllegalParameter,
763 "Server requested a record size limit larger than the protocol's maximum");
764 }
765
766 // RFC 8449 4.
767 // Endpoints MUST NOT send a "record_size_limit" extension with a value
768 // smaller than 64. An endpoint MUST treat receipt of a smaller value
769 // as a fatal error and generate an "illegal_parameter" alert.
770 if(m_limit < 64) {
771 throw TLS_Exception(Alert::IllegalParameter, "Received a record size limit smaller than 64 bytes");
772 }
773}
774
775std::vector<uint8_t> Record_Size_Limit::serialize(Connection_Side /*whoami*/) const {
776 std::vector<uint8_t> buf;
777
778 buf.push_back(get_byte<0>(m_limit));
779 buf.push_back(get_byte<1>(m_limit));
780
781 return buf;
782}
783
784} // 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