Botan 3.13.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/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
35namespace Botan::TLS {
36
37namespace {
38
39std::unique_ptr<Extension> make_extension(TLS_Data_Reader& reader,
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());
46 switch(code) {
48 return std::make_unique<Server_Name_Indicator>(reader, size, from);
49
51 return std::make_unique<Supported_Groups>(reader, size);
52
54 return std::make_unique<Certificate_Status_Request>(reader, size, message_type, from);
55
57 return std::make_unique<Signature_Algorithms>(reader, size);
58
60 return std::make_unique<Signature_Algorithms_Cert>(reader, size);
61
63 return std::make_unique<SRTP_Protection_Profiles>(reader, size);
64
66 return std::make_unique<Application_Layer_Protocol_Notification>(reader, size, from);
67
69 return std::make_unique<Client_Certificate_Type>(reader, size, from);
70
72 return std::make_unique<Server_Certificate_Type>(reader, size, from);
73
75 return std::make_unique<Record_Size_Limit>(reader, size, from);
76
78 return std::make_unique<Supported_Versions>(reader, size, from);
79
81 break; // RFC 7685, recognized but not implemented; falls through to Unknown_Extension
82
83#if defined(BOTAN_HAS_TLS_12)
85 return std::make_unique<Supported_Point_Formats>(reader, size);
86
88 return std::make_unique<Renegotiation_Extension>(reader, size);
89
91 return std::make_unique<Extended_Master_Secret>(reader, size);
92
94 return std::make_unique<Encrypt_then_MAC>(reader, size);
95
97 return std::make_unique<Session_Ticket_Extension>(reader, size, from);
98#else
104 break; // considered as 'unknown extension'
105#endif
106
107#if defined(BOTAN_HAS_TLS_13)
109 return std::make_unique<PSK>(reader, size, message_type);
110
112 return std::make_unique<EarlyDataIndication>(reader, size, message_type);
113
115 return std::make_unique<Cookie>(reader, size);
116
118 return std::make_unique<PSK_Key_Exchange_Modes>(reader, size);
119
121 return std::make_unique<Certificate_Authorities>(reader, size);
122
124 return std::make_unique<Key_Share>(reader, size, message_type);
125#else
132 break; // considered as 'unknown extension'
133#endif
134 }
135
136 return std::make_unique<Unknown_Extension>(code, reader, size);
137}
138
139} // namespace
140
141Extensions::~Extensions() = default;
142
144 return m_extensions.contains(type);
145}
146
148 const auto i = m_extensions.find(type);
149
150 if(i == m_extensions.end()) {
151 return nullptr;
152 } else {
153 return i->second.get();
154 }
155}
156
157void Extensions::add(std::unique_ptr<Extension> extn) {
158 const auto type = extn->type();
159 if(has(type)) {
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);
164 m_extensions.emplace(type, std::move(extn));
165}
166
167void Extensions::deserialize(TLS_Data_Reader& reader, const Connection_Side from, const Handshake_Type message_type) {
168 if(reader.has_remaining()) {
169 const uint16_t all_extn_size = reader.get_uint16_t();
170
171 if(reader.remaining_bytes() != all_extn_size) {
172 throw Decoding_Error("Bad extension size");
173 }
174
175 while(reader.has_remaining()) {
176 const uint16_t extension_code = reader.get_uint16_t();
177 const uint16_t extension_size = reader.get_uint16_t();
178
179 const auto type = static_cast<Extension_Code>(extension_code);
180
181 if(this->has(type)) {
182 throw TLS_Exception(TLS::Alert::DecodeError, "Peer sent duplicated extensions");
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);
188 m_raw_extension_data[type] = extn_data;
189 TLS_Data_Reader extn_reader("Extension", extn_data);
190 this->add(make_extension(extn_reader, type, from, message_type));
191 extn_reader.assert_done();
192 }
193 }
194}
196bool Extensions::contains_other_than(const std::set<Extension_Code>& allowed_extensions,
197 const bool allow_unknown_extensions) const {
198 const auto found = extension_types();
199
200 std::vector<Extension_Code> diff;
201 std::set_difference(
202 found.cbegin(), found.end(), allowed_extensions.cbegin(), allowed_extensions.cend(), std::back_inserter(diff));
203
204 if(allow_unknown_extensions) {
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) {
208 const auto ext = get(ext_type);
209 return ext && ext->is_implemented();
210 });
211
212 // ... if yes, `contains_other_than` is true
213 return itr != diff.cend();
214 }
215
216 return !diff.empty();
217}
218
220 auto i = m_extensions.find(type);
221
222 if(i == m_extensions.end()) {
223 return false;
224 } else {
225 m_extensions.erase(i);
226 std::erase(m_extension_codes, type);
227 m_raw_extension_data.erase(type);
228 return true;
229 }
230}
231
232std::vector<uint8_t> Extensions::serialize(Connection_Side whoami) const {
233 std::vector<uint8_t> buf(2); // 2 bytes for length field
234
235 // Serialize in the order extensions were added, which matters for TLS 1.3
236 for(const auto extn_type : m_extension_codes) {
237 const auto& extn = m_extensions.at(extn_type);
238
239 if(extn->empty()) {
240 continue;
241 }
242
243 const uint16_t extn_code = static_cast<uint16_t>(extn_type);
244
245 const std::vector<uint8_t> extn_val = extn->serialize(whoami);
246
247 // Each extension carries a uint16 length prefix.
248 BOTAN_ASSERT_NOMSG(extn_val.size() <= 0xFFFF);
249
250 buf.push_back(get_byte<0>(extn_code));
251 buf.push_back(get_byte<1>(extn_code));
252
253 buf.push_back(get_byte<0>(static_cast<uint16_t>(extn_val.size())));
254 buf.push_back(get_byte<1>(static_cast<uint16_t>(extn_val.size())));
255
256 buf += extn_val;
257 }
258
259 // The outer extensions block is itself uint16-length-prefixed.
260 BOTAN_ASSERT_NOMSG(buf.size() - 2 <= 0xFFFF);
261 const uint16_t extn_size = static_cast<uint16_t>(buf.size() - 2);
262
263 buf[0] = get_byte<0>(extn_size);
264 buf[1] = get_byte<1>(extn_size);
265
266 // avoid sending a completely empty extensions block
267 if(buf.size() == 2) {
268 return std::vector<uint8_t>();
269 }
270
271 return buf;
272}
273
274std::set<Extension_Code> Extensions::extension_types() const {
275 std::set<Extension_Code> offers;
276 for(const auto& [extn_type, extn] : m_extensions) {
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()) {
280 offers.insert(extn_type);
281 }
282 }
283 return offers;
284}
285
286void Extensions::reorder(std::span<const Extension_Code> order) {
287 const std::set<Extension_Code> in_order(order.begin(), order.end());
288
289 std::vector<Extension_Code> new_codes;
290 new_codes.reserve(m_extension_codes.size());
291
292 // First: extensions not mentioned in the order (preserving their relative order)
293 for(auto code : m_extension_codes) {
294 if(!in_order.contains(code)) {
295 new_codes.push_back(code);
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;
304 for(auto code : order) {
305 if(m_extensions.contains(code) && already_pushed.insert(code).second) {
306 new_codes.push_back(code);
307 }
308 }
309
310 m_extension_codes = std::move(new_codes);
311}
312
314 m_type(type), m_value(reader.get_fixed<uint8_t>(extension_size)) {}
315
316std::vector<uint8_t> Unknown_Extension::serialize(Connection_Side /*whoami*/) const {
317 return m_value;
318}
319
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) {
333 if(extension_size != 0) {
334 throw TLS_Exception(Alert::IllegalParameter, "Server sent non-empty SNI extension");
335 }
336 } else {
337 // Clients are required to send at least one name in the SNI
338 if(extension_size == 0) {
339 throw TLS_Exception(Alert::IllegalParameter, "Client sent empty SNI extension");
340 }
341
342 const uint16_t name_bytes = reader.get_uint16_t();
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) {
348 throw Decoding_Error("Bad encoding of SNI extension");
349 }
350
351 BOTAN_ASSERT_NOMSG(reader.remaining_bytes() == name_bytes);
352
353 while(reader.has_remaining()) {
354 const uint8_t name_type = reader.get_byte();
355
356 if(name_type == 0) {
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()) {
362 throw Decoding_Error("TLS ServerNameIndicator contains more than one host_name");
363 }
364 m_sni_host_name = reader.get_string(2, 1, 65535);
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}
379
380std::vector<uint8_t> Server_Name_Indicator::serialize(Connection_Side whoami) const {
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) {
386 return {};
387 }
388
389 std::vector<uint8_t> buf;
390
391 const size_t name_len = m_sni_host_name.size();
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);
397
398 buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len + 3)));
399 buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len + 3)));
400 buf.push_back(0); // DNS
401
402 buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len)));
403 buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len)));
404
405 buf += as_span_of_bytes(m_sni_host_name);
406
407 return buf;
408}
409
411 // Avoid sending an IPv4/IPv6 address in SNI as this is prohibited
412
413 if(hostname.empty() || hostname.size() > 255) {
414 return false;
415 }
416
417 if(auto ipv4 = IPv4Address::from_string(hostname)) {
418 return false;
419 }
420
421 if(auto ipv6 = IPv6Address::from_string(hostname)) {
422 return false;
423 }
424
425 if(auto dns = DNSName::from_string(hostname)) {
426 return true;
427 } else {
428 return false;
429 }
430}
431
433 BOTAN_ARG_CHECK(!protocol.empty(), "ALPN protocol name must not be empty");
434 BOTAN_ARG_CHECK(protocol.size() < 256, "ALPN protocol name too long");
435 m_protocols.emplace_back(protocol);
436}
437
439 m_protocols(std::move(protocols)) {
440 for(const auto& protocol : m_protocols) {
441 BOTAN_ARG_CHECK(!protocol.empty(), "ALPN protocol name must not be empty");
442 BOTAN_ARG_CHECK(protocol.size() < 256, "ALPN protocol name too long");
443 }
444}
445
447 uint16_t extension_size,
448 Connection_Side from) {
449 if(extension_size < 2) {
450 throw Decoding_Error("ALPN extension cannot be empty");
451 }
452
453 const uint16_t name_bytes = reader.get_uint16_t();
454
455 size_t bytes_remaining = extension_size - 2;
456
457 if(name_bytes != bytes_remaining) {
458 throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
459 }
460
461 // RFC 7301 3.1: ProtocolName protocol_name_list<2..2^16-1>
462 if(name_bytes == 0) {
463 throw Decoding_Error("Empty ALPN protocol_name_list not allowed");
464 }
465
466 while(bytes_remaining > 0) {
467 const std::string p = reader.get_string(1, 0, 255);
468
469 if(bytes_remaining < p.size() + 1) {
470 throw Decoding_Error("Bad encoding of ALPN, length field too long");
471 }
472
473 if(p.empty()) {
474 throw Decoding_Error("Empty ALPN protocol not allowed");
475 }
476
477 bytes_remaining -= (p.size() + 1);
478
479 m_protocols.push_back(p);
480 }
481
482 // RFC 7301 3.1
483 // The "extension_data" field of the [...] extension is structured the
484 // same as described above for the client "extension_data", except that
485 // the "ProtocolNameList" MUST contain exactly one "ProtocolName".
486 if(from == Connection_Side::Server && m_protocols.size() != 1) {
487 throw TLS_Exception(
488 Alert::DecodeError,
489 "Server sent " + std::to_string(m_protocols.size()) + " protocols in ALPN extension response");
490 }
491}
492
494 BOTAN_STATE_CHECK(m_protocols.size() == 1);
495 return m_protocols.front();
496}
497
499 std::vector<uint8_t> buf(2);
500
501 for(auto&& proto : m_protocols) {
502 if(proto.length() >= 256) {
503 throw TLS_Exception(Alert::InternalError, "ALPN name too long");
504 }
505 if(!proto.empty()) {
506 append_tls_length_value(buf, proto, 1);
507 }
508 }
509
510 // RFC 7301 3.1: ProtocolName protocol_name_list<2..2^16-1>;
511 BOTAN_ASSERT_NOMSG(buf.size() - 2 <= 0xFFFF);
512 buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
513 buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
514
515 return buf;
516}
517
518Certificate_Type_Base::Certificate_Type_Base(std::vector<Certificate_Type> supported_cert_types) :
519 m_certificate_types(std::move(supported_cert_types)), m_from(Connection_Side::Client) {
520 BOTAN_ARG_CHECK(!m_certificate_types.empty(), "at least one certificate type must be supported");
521}
522
524 Certificate_Type_Base(cct, policy.accepted_client_certificate_types()) {}
525
527 Certificate_Type_Base(sct, policy.accepted_server_certificate_types()) {}
528
530 std::span<const Certificate_Type> server_preference) :
531 m_from(Connection_Side::Server) {
532 // RFC 7250 4.2
533 // The server_certificate_type extension in the client hello indicates the
534 // types of certificates the client is able to process when provided by
535 // the server in a subsequent certificate payload. [...] With the
536 // server_certificate_type extension in the server hello, the TLS server
537 // indicates the certificate type carried in the Certificate payload.
538 for(const auto server_supported_cert_type : server_preference) {
539 if(value_exists(certificate_type_from_client.m_certificate_types, server_supported_cert_type)) {
540 m_certificate_types.push_back(server_supported_cert_type);
541 return;
542 }
543 }
544
545 // RFC 7250 4.2 (2.)
546 // The server supports the extension defined in this document, but
547 // it does not have any certificate type in common with the client.
548 // Then, the server terminates the session with a fatal alert of
549 // type "unsupported_certificate".
550 throw TLS_Exception(Alert::UnsupportedCertificate, "Failed to agree on certificate_type");
551}
552
554 m_from(from) {
555 if(extension_size == 0) {
556 throw Decoding_Error("Certificate type extension cannot be empty");
557 }
558
559 if(from == Connection_Side::Client) {
560 const auto type_bytes = reader.get_tls_length_value(1);
561 if(static_cast<size_t>(extension_size) != type_bytes.size() + 1) {
562 throw Decoding_Error("certificate type extension had inconsistent length");
563 }
564 // RFC 7250 4: {client,server}_certificate_types<1..2^8-1> so must be non-empty
565 if(type_bytes.empty()) {
566 throw Decoding_Error("Certificate type extension contains no types");
567 }
568 std::transform(
569 type_bytes.begin(), type_bytes.end(), std::back_inserter(m_certificate_types), [](const auto type_byte) {
570 return static_cast<Certificate_Type>(type_byte);
571 });
572 } else {
573 // RFC 7250 4.2
574 // Note that only a single value is permitted in the
575 // server_certificate_type extension when carried in the server hello.
576 if(extension_size != 1) {
577 throw Decoding_Error("Server's certificate type extension must be of length 1");
578 }
579 const auto type_byte = reader.get_byte();
580 m_certificate_types.push_back(static_cast<Certificate_Type>(type_byte));
581 }
582}
583
584std::vector<uint8_t> Certificate_Type_Base::serialize(Connection_Side whoami) const {
585 std::vector<uint8_t> result;
586 if(whoami == Connection_Side::Client) {
587 std::vector<uint8_t> type_bytes;
588 std::transform(
589 m_certificate_types.begin(), m_certificate_types.end(), std::back_inserter(type_bytes), [](const auto type) {
590 return static_cast<uint8_t>(type);
591 });
592 append_tls_length_value(result, type_bytes, 1);
593 } else {
594 BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
595 result.push_back(static_cast<uint8_t>(m_certificate_types.front()));
596 }
597 return result;
598}
599
602 BOTAN_ASSERT_NOMSG(from_server.m_from == Connection_Side::Server);
603
604 // RFC 7250 4.2
605 // The value conveyed in the [client_]certificate_type extension MUST be
606 // selected from one of the values provided in the [client_]certificate_type
607 // extension sent in the client hello.
608 if(!value_exists(m_certificate_types, from_server.selected_certificate_type())) {
609 throw TLS_Exception(Alert::IllegalParameter,
610 Botan::fmt("Selected certificate type was not offered: {}",
612 }
613}
614
617 BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
618 return m_certificate_types.front();
619}
620
621Supported_Groups::Supported_Groups(std::vector<Group_Params> groups) : m_groups(std::move(groups)) {}
622
623const std::vector<Group_Params>& Supported_Groups::groups() const {
624 return m_groups;
625}
626
627std::vector<Group_Params> Supported_Groups::ec_groups() const {
628 std::vector<Group_Params> ec;
629 for(auto g : m_groups) {
630 if(g.is_pure_ecc_group()) {
631 ec.push_back(g);
632 }
633 }
634 return ec;
635}
636
637std::vector<Group_Params> Supported_Groups::dh_groups() const {
638 std::vector<Group_Params> dh;
639 for(auto g : m_groups) {
640 if(g.is_in_ffdhe_range()) {
641 dh.push_back(g);
642 }
643 }
644 return dh;
645}
646
647std::vector<uint8_t> Supported_Groups::serialize(Connection_Side /*whoami*/) const {
648 std::vector<uint8_t> buf(2);
649
650 for(auto g : m_groups) {
651 const uint16_t id = g.wire_code();
652
653 if(id > 0) {
654 buf.push_back(get_byte<0>(id));
655 buf.push_back(get_byte<1>(id));
656 }
657 }
658
659 // RFC 8446 4.2.7: NamedGroup named_group_list<2..2^16-1>;
660 BOTAN_ASSERT_NOMSG(buf.size() - 2 <= 0xFFFF);
661 buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
662 buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
663
664 return buf;
665}
666
667Supported_Groups::Supported_Groups(TLS_Data_Reader& reader, uint16_t extension_size) {
668 const uint16_t len = reader.get_uint16_t();
669
670 if(len + 2 != extension_size) {
671 throw Decoding_Error("Inconsistent length field in supported groups list");
672 }
673
674 // RFC 8446 4.2.7: NamedGroup named_group_list<2..2^16-1>;
675 if(len == 0) {
676 throw Decoding_Error("Empty supported groups list");
677 }
678
679 if(len % 2 == 1) {
680 throw Decoding_Error("Supported groups list of strange size");
681 }
682
683 const size_t elems = len / 2;
684
685 std::unordered_set<uint16_t> seen;
686 for(size_t i = 0; i != elems; ++i) {
687 const auto group = static_cast<Group_Params>(reader.get_uint16_t());
688 // Note: RFC 8446 does not explicitly enforce that groups must be unique.
689 if(seen.insert(group.wire_code()).second) {
690 m_groups.push_back(group);
691 }
692 }
693}
694
695namespace {
696
697std::vector<uint8_t> serialize_signature_algorithms(const std::vector<Signature_Scheme>& schemes) {
698 BOTAN_ASSERT(schemes.size() < 256, "Too many signature schemes");
699
700 std::vector<uint8_t> buf;
701
702 const uint16_t len = static_cast<uint16_t>(schemes.size() * 2);
703
704 buf.push_back(get_byte<0>(len));
705 buf.push_back(get_byte<1>(len));
706
707 for(const Signature_Scheme scheme : schemes) {
708 buf.push_back(get_byte<0>(scheme.wire_code()));
709 buf.push_back(get_byte<1>(scheme.wire_code()));
710 }
711
712 return buf;
713}
714
715std::vector<Signature_Scheme> parse_signature_algorithms(TLS_Data_Reader& reader, uint16_t extension_size) {
716 uint16_t len = reader.get_uint16_t();
717
718 if(len + 2 != extension_size || len % 2 == 1 || len == 0) {
719 throw Decoding_Error("Bad encoding on signature algorithms extension");
720 }
721
722 std::vector<Signature_Scheme> schemes;
723 schemes.reserve(len / 2);
724 while(len > 0) {
725 schemes.emplace_back(reader.get_uint16_t());
726 len -= 2;
727 }
728
729 return schemes;
730}
731
732} // namespace
733
734std::vector<uint8_t> Signature_Algorithms::serialize(Connection_Side /*whoami*/) const {
735 return serialize_signature_algorithms(m_schemes);
736}
737
739 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
740
741std::vector<uint8_t> Signature_Algorithms_Cert::serialize(Connection_Side /*whoami*/) const {
742 return serialize_signature_algorithms(m_schemes);
743}
744
746 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
747
749 // RFC 5764 4.1.1: UseSRTPData consists of
750 // SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
751 // opaque srtp_mki<0..255>;
752 // for a wire size of 2 (profiles len) + 2*N + 1 (mki len) + mki_bytes,
753 // with N >= 1.
754 if(extension_size < 5) {
755 throw Decoding_Error("Truncated SRTP protection extension");
756 }
757 const size_t max_profile_pairs = (static_cast<size_t>(extension_size) - 3) / 2;
758 m_pp = reader.get_range<uint16_t>(2, 1, max_profile_pairs);
759 const std::vector<uint8_t> mki = reader.get_range<uint8_t>(1, 0, 255);
760
761 if(m_pp.size() * 2 + mki.size() + 3 != extension_size) {
762 throw Decoding_Error("Bad encoding for SRTP protection extension");
763 }
764
765 if(!mki.empty()) {
766 throw Decoding_Error("Unhandled non-empty MKI for SRTP protection extension");
767 }
768}
769
770std::vector<uint8_t> SRTP_Protection_Profiles::serialize(Connection_Side /*whoami*/) const {
771 std::vector<uint8_t> buf;
772
773 const uint16_t pp_len = static_cast<uint16_t>(m_pp.size() * 2);
774 buf.push_back(get_byte<0>(pp_len));
775 buf.push_back(get_byte<1>(pp_len));
776
777 for(const uint16_t pp : m_pp) {
778 buf.push_back(get_byte<0>(pp));
779 buf.push_back(get_byte<1>(pp));
780 }
781
782 buf.push_back(0); // srtp_mki, always empty here
783
784 return buf;
785}
786
787std::vector<uint8_t> Supported_Versions::serialize(Connection_Side whoami) const {
788 std::vector<uint8_t> buf;
789
790 if(whoami == Connection_Side::Server) {
791 BOTAN_ASSERT_NOMSG(m_versions.size() == 1);
792 buf.push_back(m_versions[0].major_version());
793 buf.push_back(m_versions[0].minor_version());
794 } else {
795 // RFC 8446 4.2.1: ProtocolVersion versions<2..254>; - up to 127 entries.
796 BOTAN_ASSERT_NOMSG(!m_versions.empty());
797 BOTAN_ASSERT_NOMSG(m_versions.size() <= 127);
798 const uint8_t len = static_cast<uint8_t>(m_versions.size() * 2);
799
800 buf.push_back(len);
801
802 for(const Protocol_Version version : m_versions) {
803 buf.push_back(version.major_version());
804 buf.push_back(version.minor_version());
805 }
806 }
807
808 return buf;
809}
810
812 // RFC 8446 4.2.1
813 // The extension contains a list of supported versions in preference order,
814 // with the most preferred version first. Implementations [...] MUST send
815 // this extension in the ClientHello containing all versions of TLS which
816 // they are prepared to negotiate.
817 //
818 // We simply assume that we always want the newest available TLS version.
819#if defined(BOTAN_HAS_TLS_13)
820 if(!offer.is_datagram_protocol()) {
821 if(offer >= Protocol_Version::TLS_V13 && policy.allow_tls13()) {
822 m_versions.push_back(Protocol_Version::TLS_V13);
823 }
824 }
825#endif
826
827#if defined(BOTAN_HAS_TLS_12)
828 if(offer.is_datagram_protocol()) {
829 if(offer >= Protocol_Version::DTLS_V12 && policy.allow_dtls12()) {
830 m_versions.push_back(Protocol_Version::DTLS_V12);
831 }
832 } else {
833 if(offer >= Protocol_Version::TLS_V12 && policy.allow_tls12()) {
834 m_versions.push_back(Protocol_Version::TLS_V12);
835 }
836 }
837#endif
838
839 // if no versions are supported, the input variables are not used
840 BOTAN_UNUSED(offer, policy);
841}
842
844 if(from == Connection_Side::Server) {
845 if(extension_size != 2) {
846 throw Decoding_Error("Server sent invalid supported_versions extension");
847 }
848 m_versions.push_back(Protocol_Version(reader.get_uint16_t()));
849 } else {
850 auto versions = reader.get_range<uint16_t>(1, 1, 127);
851
852 for(auto v : versions) {
853 m_versions.push_back(Protocol_Version(v));
854 }
855
856 if(extension_size != 1 + 2 * versions.size()) {
857 throw Decoding_Error("Client sent invalid supported_versions extension");
858 }
859 }
860}
861
863 for(auto v : m_versions) {
864 if(version == v) {
865 return true;
866 }
867 }
868 return false;
869}
870
872 BOTAN_ARG_CHECK(limit >= 64, "RFC 8449 does not allow record size limits smaller than 64 bytes");
873 BOTAN_ARG_CHECK(limit <= MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */,
874 "RFC 8449 does not allow record size limits larger than 2^14+1");
875}
876
878 if(extension_size != 2) {
879 throw TLS_Exception(Alert::DecodeError, "invalid record_size_limit extension");
880 }
881
882 m_limit = reader.get_uint16_t();
883
884 // RFC 8449 4.
885 // This value is the length of the plaintext of a protected record.
886 // The value includes the content type and padding added in TLS 1.3 (that
887 // is, the complete length of TLSInnerPlaintext).
888 //
889 // A server MUST NOT enforce this restriction; a client might advertise
890 // a higher limit that is enabled by an extension or version the server
891 // does not understand. A client MAY abort the handshake with an
892 // "illegal_parameter" alert.
893 //
894 // Note: We are currently supporting this extension in TLS 1.3 only, hence
895 // we check for the TLS 1.3 limit. The TLS 1.2 limit would not include
896 // the "content type byte" and hence be one byte less!
897 if(m_limit > MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */ && from == Connection_Side::Server) {
898 throw TLS_Exception(Alert::IllegalParameter,
899 "Server requested a record size limit larger than the protocol's maximum");
900 }
901
902 // RFC 8449 4.
903 // Endpoints MUST NOT send a "record_size_limit" extension with a value
904 // smaller than 64. An endpoint MUST treat receipt of a smaller value
905 // as a fatal error and generate an "illegal_parameter" alert.
906 if(m_limit < 64) {
907 throw TLS_Exception(Alert::IllegalParameter, "Received a record size limit smaller than 64 bytes");
908 }
909}
910
911std::vector<uint8_t> Record_Size_Limit::serialize(Connection_Side /*whoami*/) const {
912 std::vector<uint8_t> buf;
913
914 buf.push_back(get_byte<0>(m_limit));
915 buf.push_back(get_byte<1>(m_limit));
916
917 return buf;
918}
919
920} // 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
static std::optional< DNSName > from_string(std::string_view name)
Definition dns_name.cpp:136
static std::optional< IPv4Address > from_string(std::string_view str)
static std::optional< IPv6Address > from_string(std::string_view str)
Application_Layer_Protocol_Notification(std::string_view protocol)
const std::vector< std::string > & protocols() const
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 reorder(std::span< const Extension_Code > order)
void deserialize(TLS_Data_Reader &reader, Connection_Side from, Handshake_Type message_type)
bool remove_extension(Extension_Code type)
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(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(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:35
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:44