Botan 3.14.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/dns_name.h>
16#include <botan/exceptn.h>
17#include <botan/ipv4_address.h>
18#include <botan/ipv6_address.h>
19#include <botan/tls_exceptn.h>
20#include <botan/tls_policy.h>
21#include <botan/internal/fmt.h>
22#include <botan/internal/stl_util.h>
23#include <botan/internal/tls_messages_internal.h>
24#include <botan/internal/tls_reader.h>
25#include <algorithm>
26#include <unordered_set>
27
28#if defined(BOTAN_HAS_TLS_13)
29 #include <botan/tls_extensions_13.h>
30#endif
31
32#if defined(BOTAN_HAS_TLS_12)
33 #include <botan/tls_extensions_12.h>
34#endif
35
36namespace Botan::TLS {
37
38namespace {
39
40/*
41* Padding (RFC 7685) carries no information, but is a recognized extension
42* which may only appear in a ClientHello
43*/
44class Padding_Extension final : public Extension {
45 public:
46 static Extension_Code static_type() { return Extension_Code::Padding; }
47
48 Extension_Code type() const override { return static_type(); }
49
50 Padding_Extension(TLS_Data_Reader& reader, uint16_t extension_size) : m_size(extension_size) {
51 // RFC 7685 3.
52 // The client MUST fill the padding extension completely with zero
53 // bytes, although the padding extension_data field may be empty.
54 const auto padding = reader.get_fixed<uint8_t>(extension_size);
55 if(std::ranges::any_of(padding, [](uint8_t b) { return b != 0; })) {
56 throw Decoding_Error("Padding extension contains non-zero bytes");
57 }
58 }
59
60 std::vector<uint8_t> serialize(Connection_Side /*whoami*/) const override { return std::vector<uint8_t>(m_size); }
61
62 bool empty() const override {
63 // Even without any padding bytes the extension must be serialized
64 return false;
65 }
66
67 private:
68 size_t m_size;
69};
70
71std::unique_ptr<Extension> make_extension(TLS_Data_Reader& reader,
72 Extension_Code code,
73 const Connection_Side from,
74 const Handshake_Type message_type) {
75 // This cast is safe because we read exactly a 16 bit length field for
76 // the extension in Extensions::deserialize
77 const uint16_t size = static_cast<uint16_t>(reader.remaining_bytes());
78 switch(code) {
80 return std::make_unique<Server_Name_Indicator>(reader, size, from);
81
83 return std::make_unique<Supported_Groups>(reader, size);
84
86 return make_certificate_status_request(reader, size, message_type, from);
87
89 return std::make_unique<Signature_Algorithms>(reader, size);
90
92 return std::make_unique<Signature_Algorithms_Cert>(reader, size);
93
95 return std::make_unique<SRTP_Protection_Profiles>(reader, size, from);
96
98 return std::make_unique<Application_Layer_Protocol_Notification>(reader, size, from);
99
101 return std::make_unique<Client_Certificate_Type>(reader, size, from);
102
104 return std::make_unique<Server_Certificate_Type>(reader, size, from);
105
107 return std::make_unique<Record_Size_Limit>(reader, size, from);
108
110 return std::make_unique<Supported_Versions>(reader, size, from);
111
113 return std::make_unique<Padding_Extension>(reader, size);
114
115#if defined(BOTAN_HAS_TLS_12)
117 return std::make_unique<Supported_Point_Formats>(reader, size);
118
120 return std::make_unique<Renegotiation_Extension>(reader, size);
121
123 return std::make_unique<Extended_Master_Secret>(reader, size);
124
126 return std::make_unique<Encrypt_then_MAC>(reader, size);
127
129 return std::make_unique<Session_Ticket_Extension>(reader, size, from);
130#else
136 break; // considered as 'unknown extension'
137#endif
138
139#if defined(BOTAN_HAS_TLS_13)
141 return std::make_unique<PSK>(reader, size, message_type);
142
144 return std::make_unique<EarlyDataIndication>(reader, size, message_type);
145
147 return std::make_unique<Cookie>(reader, size);
148
150 return std::make_unique<PSK_Key_Exchange_Modes>(reader, size);
151
153 return std::make_unique<Certificate_Authorities>(reader, size);
154
156 return std::make_unique<Key_Share>(reader, size, message_type);
157#else
164 break; // considered as 'unknown extension'
165#endif
166 }
167
168 return std::make_unique<Unknown_Extension>(code, reader, size);
169}
170
171} // namespace
172
173Extensions::~Extensions() = default;
174
176 return m_extensions.contains(type);
177}
178
180 const auto i = m_extensions.find(type);
181
182 if(i == m_extensions.end()) {
183 return nullptr;
184 } else {
185 return i->second.get();
186 }
187}
188
189void Extensions::add(std::unique_ptr<Extension> extn) {
190 const auto type = extn->type();
191 if(has(type)) {
192 throw Invalid_Argument("cannot add the same extension twice: " + std::to_string(static_cast<uint16_t>(type)));
193 }
195 m_extension_codes.push_back(type);
196 m_extensions.emplace(type, std::move(extn));
197}
198
199void Extensions::deserialize(TLS_Data_Reader& reader, const Connection_Side from, const Handshake_Type message_type) {
200 if(reader.has_remaining()) {
201 const uint16_t all_extn_size = reader.get_uint16_t();
202
203 if(reader.remaining_bytes() != all_extn_size) {
204 throw Decoding_Error("Bad extension size");
205 }
206
207 while(reader.has_remaining()) {
208 const uint16_t extension_code = reader.get_uint16_t();
209 const uint16_t extension_size = reader.get_uint16_t();
210
211 const auto type = static_cast<Extension_Code>(extension_code);
212
213 if(this->has(type)) {
214 throw TLS_Exception(TLS::Alert::DecodeError, "Peer sent duplicated extensions");
215 }
216
217 // TODO offer a function on reader that returns a byte range as a reference
218 // to avoid this copy of the extension data
219 const std::vector<uint8_t> extn_data = reader.get_fixed<uint8_t>(extension_size);
220 m_raw_extension_data[type] = extn_data;
221 TLS_Data_Reader extn_reader("Extension", extn_data);
222 this->add(make_extension(extn_reader, type, from, message_type));
223 extn_reader.assert_done();
224 }
225 }
226}
227
228bool Extensions::contains_other_than(const std::set<Extension_Code>& allowed_extensions,
229 const bool allow_unknown_extensions) const {
230 const auto found = extension_types();
231
232 std::vector<Extension_Code> diff;
233 std::set_difference(
234 found.cbegin(), found.end(), allowed_extensions.cbegin(), allowed_extensions.cend(), std::back_inserter(diff));
235
236 if(allow_unknown_extensions) {
237 // Go through the found unexpected extensions whether any of those
238 // is known to this TLS implementation.
239 const auto itr = std::find_if(diff.cbegin(), diff.cend(), [this](const auto ext_type) {
240 const auto ext = get(ext_type);
241 return ext && ext->is_implemented();
242 });
243
244 // ... if yes, `contains_other_than` is true
245 return itr != diff.cend();
246 }
247
248 return !diff.empty();
249}
250
252 const auto i = m_extensions.find(type);
253
254 if(i == m_extensions.end()) {
255 return false;
256 } else {
257 m_extensions.erase(i);
258 std::erase(m_extension_codes, type);
259 m_raw_extension_data.erase(type);
260 return true;
261 }
262}
263
264std::vector<uint8_t> Extensions::serialize(Connection_Side whoami) const {
265 std::vector<uint8_t> buf(2); // 2 bytes for length field
266
267 // Serialize in the order extensions were added, which matters for TLS 1.3
268 for(const auto extn_type : m_extension_codes) {
269 const auto& extn = m_extensions.at(extn_type);
270
271 if(extn->empty()) {
272 continue;
273 }
274
275 const uint16_t extn_code = static_cast<uint16_t>(extn_type);
276
277 const std::vector<uint8_t> extn_val = extn->serialize(whoami);
278
279 // Each extension carries a uint16 length prefix.
280 if(extn_val.size() > 0xFFFF) {
281 throw Encoding_Error("TLS extension is too large to encode");
282 }
283
284 buf.push_back(get_byte<0>(extn_code));
285 buf.push_back(get_byte<1>(extn_code));
286
287 buf.push_back(get_byte<0>(static_cast<uint16_t>(extn_val.size())));
288 buf.push_back(get_byte<1>(static_cast<uint16_t>(extn_val.size())));
289
290 buf += extn_val;
291 }
292
293 // The outer extensions block is itself uint16-length-prefixed.
294 if(buf.size() - 2 > 0xFFFF) {
295 throw Encoding_Error("TLS extensions are too large to encode");
296 }
297 const uint16_t extn_size = static_cast<uint16_t>(buf.size() - 2);
298
299 buf[0] = get_byte<0>(extn_size);
300 buf[1] = get_byte<1>(extn_size);
301
302 // avoid sending a completely empty extensions block
303 if(buf.size() == 2) {
304 return std::vector<uint8_t>();
305 }
306
307 return buf;
308}
309
310std::set<Extension_Code> Extensions::extension_types() const {
311 std::set<Extension_Code> offers;
312 for(const auto& [extn_type, extn] : m_extensions) {
313 // Consistent with serialize(): empty extensions are not placed on
314 // the wire so they must not appear in the "offered" set either. An
315 // extension received from the peer was on the wire, whatever it holds.
316 if(!extn->empty() || m_raw_extension_data.contains(extn_type)) {
317 offers.insert(extn_type);
318 }
319 }
320 return offers;
321}
322
323void Extensions::reorder(std::span<const Extension_Code> order) {
324 const std::set<Extension_Code> in_order(order.begin(), order.end());
325
326 std::vector<Extension_Code> new_codes;
327 new_codes.reserve(m_extension_codes.size());
328
329 // First: extensions not mentioned in the order (preserving their relative order)
330 for(const auto code : m_extension_codes) {
331 if(!in_order.contains(code)) {
332 new_codes.push_back(code);
333 }
334 }
335
336 // Then: extensions in the specified order. Deduplicate so a caller that
337 // accidentally lists the same code twice doesn't cause it to be
338 // serialized twice (which would also break peers that reject duplicate
339 // extension codes per RFC 8446 4.2 / RFC 5246 7.4.1.4).
340 std::unordered_set<Extension_Code> already_pushed;
341 for(const auto code : order) {
342 if(m_extensions.contains(code) && already_pushed.insert(code).second) {
343 new_codes.push_back(code);
344 }
345 }
346
347 m_extension_codes = std::move(new_codes);
348}
349
351 m_type(type), m_value(reader.get_fixed<uint8_t>(extension_size)) {}
352
353std::vector<uint8_t> Unknown_Extension::serialize(Connection_Side /*whoami*/) const {
354 return m_value;
355}
356
358 /*
359 RFC 6066 Section 3
360
361 A server that receives a client hello containing the "server_name"
362 extension MAY use the information contained in the extension to guide
363 its selection of an appropriate certificate to return to the client,
364 and/or other aspects of security policy. In this event, the server
365 SHALL include an extension of type "server_name" in the (extended)
366 server hello. The "extension_data" field of this extension SHALL be
367 empty.
368 */
369 if(from == Connection_Side::Server) {
370 if(extension_size != 0) {
371 throw TLS_Exception(Alert::IllegalParameter, "Server sent non-empty SNI extension");
372 }
373 } else {
374 // Clients are required to send at least one name in the SNI
375 if(extension_size == 0) {
376 throw TLS_Exception(Alert::IllegalParameter, "Client sent empty SNI extension");
377 }
378
379 const uint16_t name_bytes = reader.get_uint16_t();
380
381 // RFC 6066 3: a ServerName carrying a host_name (the only NameType
382 // currently defined and the only one this implementation acts on)
383 // requires at least 1 byte name_type + 2 byte length + 1 byte HostName.
384 if(name_bytes + 2 != extension_size || name_bytes < 4) {
385 throw Decoding_Error("Bad encoding of SNI extension");
386 }
387
388 BOTAN_ASSERT_NOMSG(reader.remaining_bytes() == name_bytes);
389
390 std::vector<uint8_t> seen_name_types;
391 while(reader.has_remaining()) {
392 const uint8_t name_type = reader.get_byte();
393
394 /*
395 RFC 6066 Section 3
396 The ServerNameList MUST NOT contain more than one name of the same name_type.
397 */
398 if(value_exists(seen_name_types, name_type)) {
399 throw Decoding_Error("TLS ServerNameIndicator contains more than one name of the same type");
400 }
401 seen_name_types.push_back(name_type);
402
403 if(name_type == 0) {
404 const std::string host_name = reader.get_string(2, 1, 65535);
405
406 /*
407 RFC 6066 Section 3
408 The hostname is represented as a byte string using ASCII
409 encoding without a trailing dot. [...] Literal IPv4 and IPv6
410 addresses are not permitted in "HostName".
411 */
412 if(auto dns_name = DNSName::from_string(host_name)) {
413 m_sni_host_name = dns_name->to_string();
414 } else {
415 throw TLS_Exception(Alert::IllegalParameter,
416 "TLS ServerNameIndicator host_name is not a valid DNS name");
417 }
418 } else {
419 /*
420 Unknown name type - skip its length-prefixed value and continue
421
422 RFC 6066 Section 3
423 For backward compatibility, all future data structures associated
424 with new NameTypes MUST begin with a 16-bit length field.
425 */
426 const uint16_t unknown_name_len = reader.get_uint16_t();
427 reader.discard_next(unknown_name_len);
428 }
429 }
430 }
431}
432
433std::vector<uint8_t> Server_Name_Indicator::serialize(Connection_Side whoami) const {
434 // RFC 6066
435 // [...] the server SHALL include an extension of type "server_name" in
436 // the (extended) server hello. The "extension_data" field of this
437 // extension SHALL be empty.
438 if(whoami == Connection_Side::Server) {
439 return {};
440 }
441
442 std::vector<uint8_t> buf;
443
444 const size_t name_len = m_sni_host_name.size();
445
446 // RFC 6066 3: HostName<1..2^16-1>; the outer ServerNameList wraps a
447 // 1-byte name_type and a 2-byte length so the whole entry must fit in
448 // a uint16_t too.
449 if(name_len + 3 > 0xFFFF) {
450 throw Encoding_Error("TLS server name is too long to encode");
451 }
452
453 buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len + 3)));
454 buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len + 3)));
455 buf.push_back(0); // DNS
456
457 buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len)));
458 buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len)));
459
460 buf += as_span_of_bytes(m_sni_host_name);
461
462 return buf;
463}
464
466 // Avoid sending an IPv4/IPv6 address in SNI as this is prohibited
467
468 if(hostname.empty() || hostname.size() > 255) {
469 return false;
470 }
471
472 if(const auto ipv4 = IPv4Address::from_string(hostname)) {
473 return false;
474 }
475
476 if(const auto ipv6 = IPv6Address::from_string(hostname)) {
477 return false;
478 }
479
480 if(const auto dns = DNSName::from_string(hostname)) {
481 return true;
482 } else {
483 return false;
484 }
485}
486
488 BOTAN_ARG_CHECK(!protocol.empty(), "ALPN protocol name must not be empty");
489 BOTAN_ARG_CHECK(protocol.size() < 256, "ALPN protocol name too long");
490 m_protocols.emplace_back(protocol);
491}
492
494 m_protocols(std::move(protocols)) {
495 for(const auto& protocol : m_protocols) {
496 BOTAN_ARG_CHECK(!protocol.empty(), "ALPN protocol name must not be empty");
497 BOTAN_ARG_CHECK(protocol.size() < 256, "ALPN protocol name too long");
498 }
499}
500
502 uint16_t extension_size,
503 Connection_Side from) {
504 if(extension_size < 2) {
505 throw Decoding_Error("ALPN extension cannot be empty");
506 }
507
508 const uint16_t name_bytes = reader.get_uint16_t();
509
510 size_t bytes_remaining = extension_size - 2;
511
512 if(name_bytes != bytes_remaining) {
513 throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
514 }
515
516 // RFC 7301 3.1: ProtocolName protocol_name_list<2..2^16-1>
517 if(name_bytes == 0) {
518 throw Decoding_Error("Empty ALPN protocol_name_list not allowed");
519 }
520
521 while(bytes_remaining > 0) {
522 const std::string p = reader.get_string(1, 0, 255);
523
524 if(bytes_remaining < p.size() + 1) {
525 throw Decoding_Error("Bad encoding of ALPN, length field too long");
526 }
527
528 if(p.empty()) {
529 throw Decoding_Error("Empty ALPN protocol not allowed");
530 }
531
532 bytes_remaining -= (p.size() + 1);
533
534 m_protocols.push_back(p);
535 }
536
537 // RFC 7301 3.1
538 // The "extension_data" field of the [...] extension is structured the
539 // same as described above for the client "extension_data", except that
540 // the "ProtocolNameList" MUST contain exactly one "ProtocolName".
541 if(from == Connection_Side::Server && m_protocols.size() != 1) {
542 throw TLS_Exception(
543 Alert::DecodeError,
544 "Server sent " + std::to_string(m_protocols.size()) + " protocols in ALPN extension response");
545 }
546}
547
549 BOTAN_STATE_CHECK(m_protocols.size() == 1);
550 return m_protocols.front();
551}
552
554 std::vector<uint8_t> buf(2);
555
556 for(auto&& proto : m_protocols) {
557 if(proto.length() >= 256) {
558 throw TLS_Exception(Alert::InternalError, "ALPN name too long");
559 }
560 if(!proto.empty()) {
561 append_tls_length_value(buf, proto, 1);
562 }
563 }
564
565 // RFC 7301 3.1: ProtocolName protocol_name_list<2..2^16-1>;
566 if(buf.size() - 2 > 0xFFFF) {
567 throw Encoding_Error("TLS ALPN protocol list is too long to encode");
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
575Certificate_Type_Base::Certificate_Type_Base(std::vector<Certificate_Type> supported_cert_types) :
576 m_certificate_types(std::move(supported_cert_types)), m_from(Connection_Side::Client) {
577 BOTAN_ARG_CHECK(!m_certificate_types.empty(), "at least one certificate type must be supported");
578}
579
581 Certificate_Type_Base(cct, policy.accepted_client_certificate_types()) {}
582
584 Certificate_Type_Base(sct, policy.accepted_server_certificate_types()) {}
585
587 std::span<const Certificate_Type> server_preference) :
588 m_from(Connection_Side::Server) {
589 // RFC 7250 4.2
590 // The server_certificate_type extension in the client hello indicates the
591 // types of certificates the client is able to process when provided by
592 // the server in a subsequent certificate payload. [...] With the
593 // server_certificate_type extension in the server hello, the TLS server
594 // indicates the certificate type carried in the Certificate payload.
595 for(const auto server_supported_cert_type : server_preference) {
596 if(value_exists(certificate_type_from_client.m_certificate_types, server_supported_cert_type)) {
597 m_certificate_types.push_back(server_supported_cert_type);
598 return;
599 }
600 }
601
602 // RFC 7250 4.2 (2.)
603 // The server supports the extension defined in this document, but
604 // it does not have any certificate type in common with the client.
605 // Then, the server terminates the session with a fatal alert of
606 // type "unsupported_certificate".
607 throw TLS_Exception(Alert::UnsupportedCertificate, "Failed to agree on certificate_type");
608}
609
611 m_from(from) {
612 if(extension_size == 0) {
613 throw Decoding_Error("Certificate type extension cannot be empty");
614 }
615
616 if(from == Connection_Side::Client) {
617 const auto type_bytes = reader.get_tls_length_value(1);
618 if(static_cast<size_t>(extension_size) != type_bytes.size() + 1) {
619 throw Decoding_Error("certificate type extension had inconsistent length");
620 }
621 // RFC 7250 4: {client,server}_certificate_types<1..2^8-1> so must be non-empty
622 if(type_bytes.empty()) {
623 throw Decoding_Error("Certificate type extension contains no types");
624 }
625 std::transform(
626 type_bytes.begin(), type_bytes.end(), std::back_inserter(m_certificate_types), [](const auto type_byte) {
627 return static_cast<Certificate_Type>(type_byte);
628 });
629
630 // RFC 7250 4.1
631 // If the client has no remaining certificate types to send in the
632 // client hello, other than the default X.509 type, it MUST omit the
633 // client_certificate_type extension in the client hello.
634 // [...]
635 // If the client has no remaining certificate types to send in the
636 // client hello, other than the default X.509 certificate type, it
637 // MUST omit the entire server_certificate_type extension from the
638 // client hello.
639 if(m_certificate_types.size() == 1 && m_certificate_types.front() == Certificate_Type::X509) {
640 throw TLS_Exception(Alert::IllegalParameter, "Certificate type extension contains only the default type");
641 }
642 } else {
643 // RFC 7250 4.2
644 // Note that only a single value is permitted in the
645 // server_certificate_type extension when carried in the server hello.
646 if(extension_size != 1) {
647 throw Decoding_Error("Server's certificate type extension must be of length 1");
648 }
649 const auto type_byte = reader.get_byte();
650 m_certificate_types.push_back(static_cast<Certificate_Type>(type_byte));
651 }
652}
653
654std::vector<uint8_t> Certificate_Type_Base::serialize(Connection_Side whoami) const {
655 std::vector<uint8_t> result;
656 if(whoami == Connection_Side::Client) {
657 std::vector<uint8_t> type_bytes;
658 std::transform(
659 m_certificate_types.begin(), m_certificate_types.end(), std::back_inserter(type_bytes), [](const auto type) {
660 return static_cast<uint8_t>(type);
661 });
662 append_tls_length_value(result, type_bytes, 1);
663 } else {
664 BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
665 result.push_back(static_cast<uint8_t>(m_certificate_types.front()));
666 }
667 return result;
668}
669
672 BOTAN_ASSERT_NOMSG(from_server.m_from == Connection_Side::Server);
673
674 // RFC 7250 4.2
675 // The value conveyed in the [client_]certificate_type extension MUST be
676 // selected from one of the values provided in the [client_]certificate_type
677 // extension sent in the client hello.
678 if(!value_exists(m_certificate_types, from_server.selected_certificate_type())) {
679 throw TLS_Exception(Alert::IllegalParameter,
680 Botan::fmt("Selected certificate type was not offered: {}",
682 }
683}
684
687 BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
688 return m_certificate_types.front();
689}
690
691Supported_Groups::Supported_Groups(std::vector<Group_Params> groups) : m_groups(std::move(groups)) {}
692
693const std::vector<Group_Params>& Supported_Groups::groups() const {
694 return m_groups;
695}
696
697std::vector<Group_Params> Supported_Groups::ec_groups() const {
698 std::vector<Group_Params> ec;
699 for(const auto g : m_groups) {
700 if(group_may_be_ecc(g)) {
701 ec.push_back(g);
702 }
703 }
704 return ec;
705}
706
707std::vector<Group_Params> Supported_Groups::dh_groups() const {
708 std::vector<Group_Params> dh;
709 for(const auto g : m_groups) {
710 if(g.is_in_ffdhe_range()) {
711 dh.push_back(g);
712 }
713 }
714 return dh;
715}
716
717std::vector<uint8_t> Supported_Groups::serialize(Connection_Side /*whoami*/) const {
718 std::vector<uint8_t> buf(2);
719
720 for(const auto g : m_groups) {
721 const uint16_t id = g.wire_code();
722
723 if(id > 0) {
724 buf.push_back(get_byte<0>(id));
725 buf.push_back(get_byte<1>(id));
726 }
727 }
728
729 // RFC 8446 4.2.7: NamedGroup named_group_list<2..2^16-1>;
730 BOTAN_ASSERT_NOMSG(buf.size() - 2 <= 0xFFFF);
731 buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
732 buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
733
734 return buf;
735}
736
737Supported_Groups::Supported_Groups(TLS_Data_Reader& reader, uint16_t extension_size) {
738 const uint16_t len = reader.get_uint16_t();
739
740 if(len + 2 != extension_size) {
741 throw Decoding_Error("Inconsistent length field in supported groups list");
742 }
743
744 // RFC 8446 4.2.7: NamedGroup named_group_list<2..2^16-1>;
745 if(len == 0) {
746 throw Decoding_Error("Empty supported groups list");
747 }
748
749 if(len % 2 == 1) {
750 throw Decoding_Error("Supported groups list of strange size");
751 }
752
753 const size_t elems = len / 2;
754
755 std::unordered_set<uint16_t> seen;
756 for(size_t i = 0; i != elems; ++i) {
757 const auto group = static_cast<Group_Params>(reader.get_uint16_t());
758 // Note: RFC 8446 does not explicitly enforce that groups must be unique.
759 if(seen.insert(group.wire_code()).second) {
760 m_groups.push_back(group);
761 }
762 }
763}
764
765namespace {
766
767std::vector<uint8_t> serialize_signature_algorithms(const std::vector<Signature_Scheme>& schemes) {
768 BOTAN_ASSERT(schemes.size() < 256, "Too many signature schemes");
769
770 std::vector<uint8_t> buf;
771
772 const uint16_t len = static_cast<uint16_t>(schemes.size() * 2);
773
774 buf.push_back(get_byte<0>(len));
775 buf.push_back(get_byte<1>(len));
776
777 for(const Signature_Scheme scheme : schemes) {
778 buf.push_back(get_byte<0>(scheme.wire_code()));
779 buf.push_back(get_byte<1>(scheme.wire_code()));
780 }
781
782 return buf;
783}
784
785std::vector<Signature_Scheme> parse_signature_algorithms(TLS_Data_Reader& reader, uint16_t extension_size) {
786 uint16_t len = reader.get_uint16_t();
787
788 if(len + 2 != extension_size || len % 2 == 1 || len == 0) {
789 throw Decoding_Error("Bad encoding on signature algorithms extension");
790 }
791
792 std::vector<Signature_Scheme> schemes;
793 schemes.reserve(len / 2);
794 while(len > 0) {
795 schemes.emplace_back(reader.get_uint16_t());
796 len -= 2;
797 }
798
799 return schemes;
800}
801
802} // namespace
803
804std::vector<uint8_t> Signature_Algorithms::serialize(Connection_Side /*whoami*/) const {
805 return serialize_signature_algorithms(m_schemes);
806}
807
809 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
810
811std::vector<uint8_t> Signature_Algorithms_Cert::serialize(Connection_Side /*whoami*/) const {
812 return serialize_signature_algorithms(m_schemes);
813}
814
816 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
817
819 uint16_t extension_size,
820 Connection_Side from) {
821 // RFC 5764 4.1.1: UseSRTPData consists of
822 // SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
823 // opaque srtp_mki<0..255>;
824 // for a wire size of 2 (profiles len) + 2*N + 1 (mki len) + mki_bytes,
825 // with N >= 1.
826 if(extension_size < 5) {
827 throw Decoding_Error("Truncated SRTP protection extension");
828 }
829 const size_t max_profile_pairs = (static_cast<size_t>(extension_size) - 3) / 2;
830 m_pp = reader.get_range<uint16_t>(2, 1, max_profile_pairs);
831 const std::vector<uint8_t> mki = reader.get_range<uint8_t>(1, 0, 255);
832
833 if(m_pp.size() * 2 + mki.size() + 3 != extension_size) {
834 throw Decoding_Error("Bad encoding for SRTP protection extension");
835 }
836
837 // RFC 5764 4.1.1
838 // The extension_data field MUST contain a [...] single
839 // SRTPProtectionProfile value that the server has chosen for use with
840 // this connection.
841 if(from == Connection_Side::Server && (m_pp.size() != 1 || m_pp.front() == 0)) {
842 throw TLS_Exception(Alert::DecodeError, "Server sent malformed DTLS-SRTP extension");
843 }
844
845 // Any srtp_mki the client offers is ignored; serialize() always answers
846 // with an empty srtp_mki, per RFC 5764 4.1.3:
847 // 2. return an empty "srtp_mki" value to indicate that it cannot make
848 // use of the MKI.
849 //
850 // As a client we always offer an empty MKI, so a server cannot select one:
851 //
852 // RFC 5764 4.1.3
853 // If the client detects a nonzero-length MKI in the server's response
854 // that is different than the one the client offered, then the client
855 // MUST abort the handshake and SHOULD send an invalid_parameter alert.
856 if(from == Connection_Side::Server && !mki.empty()) {
857 throw TLS_Exception(Alert::IllegalParameter, "Server selected a DTLS-SRTP MKI that was not offered");
858 }
859}
860
861std::vector<uint8_t> SRTP_Protection_Profiles::serialize(Connection_Side /*whoami*/) const {
862 std::vector<uint8_t> buf;
863
864 const uint16_t pp_len = static_cast<uint16_t>(m_pp.size() * 2);
865 buf.push_back(get_byte<0>(pp_len));
866 buf.push_back(get_byte<1>(pp_len));
867
868 for(const uint16_t pp : m_pp) {
869 buf.push_back(get_byte<0>(pp));
870 buf.push_back(get_byte<1>(pp));
871 }
872
873 buf.push_back(0); // srtp_mki, always empty here
874
875 return buf;
876}
877
878std::vector<uint8_t> Supported_Versions::serialize(Connection_Side whoami) const {
879 std::vector<uint8_t> buf;
880
881 if(whoami == Connection_Side::Server) {
882 BOTAN_ASSERT_NOMSG(m_versions.size() == 1);
883 buf.push_back(m_versions[0].major_version());
884 buf.push_back(m_versions[0].minor_version());
885 } else {
886 // RFC 8446 4.2.1: ProtocolVersion versions<2..254>; - up to 127 entries.
887 BOTAN_ASSERT_NOMSG(!m_versions.empty());
888 BOTAN_ASSERT_NOMSG(m_versions.size() <= 127);
889 const uint8_t len = static_cast<uint8_t>(m_versions.size() * 2);
890
891 buf.push_back(len);
892
893 for(const Protocol_Version version : m_versions) {
894 buf.push_back(version.major_version());
895 buf.push_back(version.minor_version());
896 }
897 }
898
899 return buf;
900}
901
903 // RFC 8446 4.2.1
904 // The extension contains a list of supported versions in preference order,
905 // with the most preferred version first. Implementations [...] MUST send
906 // this extension in the ClientHello containing all versions of TLS which
907 // they are prepared to negotiate.
908 //
909 // We simply assume that we always want the newest available TLS version.
910#if defined(BOTAN_HAS_TLS_13)
911 if(!offer.is_datagram_protocol()) {
912 if(offer >= Protocol_Version::TLS_V13 && policy.allow_tls13()) {
913 m_versions.push_back(Protocol_Version::TLS_V13);
914 }
915 }
916#endif
917
918#if defined(BOTAN_HAS_TLS_12)
919 if(offer.is_datagram_protocol()) {
920 if(offer >= Protocol_Version::DTLS_V12 && policy.allow_dtls12()) {
921 m_versions.push_back(Protocol_Version::DTLS_V12);
922 }
923 } else {
924 if(offer >= Protocol_Version::TLS_V12 && policy.allow_tls12()) {
925 m_versions.push_back(Protocol_Version::TLS_V12);
926 }
927 }
928#endif
929
930 // if no versions are supported, the input variables are not used
931 BOTAN_UNUSED(offer, policy);
932}
933
935 if(from == Connection_Side::Server) {
936 if(extension_size != 2) {
937 throw Decoding_Error("Server sent invalid supported_versions extension");
938 }
939 m_versions.push_back(Protocol_Version(reader.get_uint16_t()));
940 } else {
941 const auto versions = reader.get_range<uint16_t>(1, 1, 127);
942
943 for(const auto v : versions) {
944 m_versions.push_back(Protocol_Version(v));
945 }
946
947 if(extension_size != 1 + 2 * versions.size()) {
948 throw Decoding_Error("Client sent invalid supported_versions extension");
949 }
950 }
951}
952
954 for(const auto v : m_versions) {
955 if(version == v) {
956 return true;
957 }
958 }
959 return false;
960}
961
963 BOTAN_ARG_CHECK(limit >= 64, "RFC 8449 does not allow record size limits smaller than 64 bytes");
964 BOTAN_ARG_CHECK(limit <= MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */,
965 "RFC 8449 does not allow record size limits larger than 2^14+1");
966}
967
969 if(extension_size != 2) {
970 throw TLS_Exception(Alert::DecodeError, "invalid record_size_limit extension");
971 }
972
973 m_limit = reader.get_uint16_t();
974
975 // RFC 8449 4.
976 // This value is the length of the plaintext of a protected record.
977 // The value includes the content type and padding added in TLS 1.3 (that
978 // is, the complete length of TLSInnerPlaintext).
979 //
980 // A server MUST NOT enforce this restriction; a client might advertise
981 // a higher limit that is enabled by an extension or version the server
982 // does not understand. A client MAY abort the handshake with an
983 // "illegal_parameter" alert.
984 //
985 // Note: We are currently supporting this extension in TLS 1.3 only, hence
986 // we check for the TLS 1.3 limit. The TLS 1.2 limit would not include
987 // the "content type byte" and hence be one byte less!
988 if(m_limit > MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */ && from == Connection_Side::Server) {
989 throw TLS_Exception(Alert::IllegalParameter,
990 "Server requested a record size limit larger than the protocol's maximum");
991 }
992
993 // RFC 8449 4.
994 // Endpoints MUST NOT send a "record_size_limit" extension with a value
995 // smaller than 64. An endpoint MUST treat receipt of a smaller value
996 // as a fatal error and generate an "illegal_parameter" alert.
997 if(m_limit < 64) {
998 throw TLS_Exception(Alert::IllegalParameter, "Received a record size limit smaller than 64 bytes");
999 }
1000}
1001
1002std::vector<uint8_t> Record_Size_Limit::serialize(Connection_Side /*whoami*/) const {
1003 std::vector<uint8_t> buf;
1004
1005 buf.push_back(get_byte<0>(m_limit));
1006 buf.push_back(get_byte<1>(m_limit));
1007
1008 return buf;
1009}
1010
1011} // namespace Botan::TLS
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#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:69
static std::optional< DNSName > from_string(std::string_view name)
Definition dns_name.cpp:134
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:128
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:115
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:110
std::vector< T > get_fixed(size_t size)
Definition tls_reader.h:134
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:182
@ MAX_PLAINTEXT_SIZE
Definition tls_magic.h:37
bool group_may_be_ecc(Group_Params group)
std::unique_ptr< Extension > make_certificate_status_request(TLS_Data_Reader &reader, uint16_t extension_size, Handshake_Type message_type, Connection_Side from)
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