Coverage Report

Created: 2023-01-25 06:35

/src/botan/build/include/botan/tls_messages.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* TLS Messages
3
* (C) 2004-2011,2015 Jack Lloyd
4
*     2016 Matthias Gierlings
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8
9
#ifndef BOTAN_TLS_MESSAGES_H_
10
#define BOTAN_TLS_MESSAGES_H_
11
12
#include <vector>
13
#include <string>
14
#include <set>
15
#include <memory>
16
#include <optional>
17
#include <variant>
18
19
#include <botan/tls_extensions.h>
20
#include <botan/tls_handshake_msg.h>
21
#include <botan/tls_session.h>
22
#include <botan/tls_policy.h>
23
#include <botan/tls_ciphersuite.h>
24
#include <botan/pk_keys.h>
25
#include <botan/x509cert.h>
26
27
namespace Botan {
28
29
class Public_Key;
30
class Credentials_Manager;
31
32
namespace OCSP {
33
class Response;
34
}
35
36
namespace TLS {
37
38
class Session;
39
class Handshake_IO;
40
class Handshake_State;
41
class Hello_Retry_Request;
42
class Callbacks;
43
class Cipher_State;
44
45
std::vector<uint8_t> make_hello_random(RandomNumberGenerator& rng,
46
                                       Callbacks& cb,
47
                                       const Policy& policy);
48
49
/**
50
* DTLS Hello Verify Request
51
*/
52
class BOTAN_UNSTABLE_API Hello_Verify_Request final : public Handshake_Message
53
   {
54
   public:
55
      std::vector<uint8_t> serialize() const override;
56
4.76k
      Handshake_Type type() const override { return HELLO_VERIFY_REQUEST; }
57
58
2.39k
      const std::vector<uint8_t>& cookie() const { return m_cookie; }
59
60
      explicit Hello_Verify_Request(const std::vector<uint8_t>& buf);
61
62
      Hello_Verify_Request(const std::vector<uint8_t>& client_hello_bits,
63
                           const std::string& client_identity,
64
                           const SymmetricKey& secret_key);
65
66
   private:
67
      std::vector<uint8_t> m_cookie;
68
   };
69
70
class Client_Hello_Internal;
71
72
/**
73
* Client Hello Message
74
*/
75
class BOTAN_UNSTABLE_API Client_Hello : public Handshake_Message
76
   {
77
   public:
78
      Client_Hello(const Client_Hello&) = delete;
79
      Client_Hello& operator=(const Client_Hello&) = delete;
80
      Client_Hello(Client_Hello&&);
81
      Client_Hello& operator=(Client_Hello&&);
82
83
      ~Client_Hello();
84
85
      Handshake_Type type() const override;
86
87
      /**
88
       * Return the version indicated in the ClientHello.
89
       * This may differ from the version indicated in the supported_versions extension.
90
       *
91
       * See RFC 8446 4.1.2:
92
       *   TLS 1.3, the client indicates its version preferences in the
93
       *   "supported_versions" extension (Section 4.2.1) and the
94
       *   legacy_version field MUST be set to 0x0303, which is the version
95
       *   number for TLS 1.2.
96
       */
97
      Protocol_Version legacy_version() const;
98
99
      const std::vector<uint8_t>& random() const;
100
101
      const std::vector<uint8_t>& session_id() const;
102
103
      const std::vector<uint16_t>& ciphersuites() const;
104
105
      bool offered_suite(uint16_t ciphersuite) const;
106
107
      std::vector<Signature_Scheme> signature_schemes() const;
108
109
      std::vector<Group_Params> supported_ecc_curves() const;
110
111
      std::vector<Group_Params> supported_dh_groups() const;
112
113
      std::vector<Protocol_Version> supported_versions() const;
114
115
      std::string sni_hostname() const;
116
117
      bool supports_alpn() const;
118
119
      bool sent_signature_algorithms() const;
120
121
      std::vector<std::string> next_protocols() const;
122
123
      std::vector<uint16_t> srtp_profiles() const;
124
125
      std::vector<uint8_t> serialize() const override;
126
127
128
      const std::vector<uint8_t>& cookie() const;
129
130
      std::vector<uint8_t> cookie_input_data() const;
131
132
      std::set<Extension_Code> extension_types() const;
133
134
      const Extensions& extensions() const;
135
136
   protected:
137
      Client_Hello();
138
      explicit Client_Hello(std::unique_ptr<Client_Hello_Internal> data);
139
140
      const std::vector<uint8_t>& compression_methods() const;
141
142
   protected:
143
      std::unique_ptr<Client_Hello_Internal> m_data;
144
   };
145
146
class BOTAN_UNSTABLE_API Client_Hello_12 final : public Client_Hello
147
   {
148
   public:
149
      class Settings final
150
         {
151
         public:
152
            Settings(const Protocol_Version version,
153
                     const std::string& hostname = ""):
154
               m_new_session_version(version),
155
2.39k
               m_hostname(hostname) {}
156
157
4.79k
            const Protocol_Version protocol_version() const { return m_new_session_version; }
158
4.79k
            const std::string& hostname() const { return m_hostname; }
159
160
         private:
161
            const Protocol_Version m_new_session_version;
162
            const std::string m_hostname;
163
         };
164
165
   public:
166
      explicit Client_Hello_12(const std::vector<uint8_t>& buf);
167
168
      Client_Hello_12(Handshake_IO& io,
169
                      Handshake_Hash& hash,
170
                      const Policy& policy,
171
                      Callbacks& cb,
172
                      RandomNumberGenerator& rng,
173
                      const std::vector<uint8_t>& reneg_info,
174
                      const Settings& client_settings,
175
                      const std::vector<std::string>& next_protocols);
176
177
      Client_Hello_12(Handshake_IO& io,
178
                      Handshake_Hash& hash,
179
                      const Policy& policy,
180
                      Callbacks& cb,
181
                      RandomNumberGenerator& rng,
182
                      const std::vector<uint8_t>& reneg_info,
183
                      const Session& session,
184
                      const std::vector<std::string>& next_protocols);
185
186
   protected:
187
      friend class Client_Hello_13;  // to allow construction by Client_Hello_13::parse()
188
      Client_Hello_12(std::unique_ptr<Client_Hello_Internal> data);
189
190
   public:
191
      using Client_Hello::random;
192
      using Client_Hello::compression_methods;
193
194
      bool prefers_compressed_ec_points() const;
195
196
      bool secure_renegotiation() const;
197
198
      std::vector<uint8_t> renegotiation_info() const;
199
200
      bool supports_session_ticket() const;
201
202
      std::vector<uint8_t> session_ticket() const;
203
204
      bool supports_extended_master_secret() const;
205
206
      bool supports_cert_status_message() const;
207
208
      bool supports_encrypt_then_mac() const;
209
210
      void update_hello_cookie(const Hello_Verify_Request& hello_verify);
211
   };
212
213
#if defined(BOTAN_HAS_TLS_13)
214
215
class BOTAN_UNSTABLE_API Client_Hello_13 final : public Client_Hello
216
   {
217
   public:
218
      Client_Hello_13(const Policy& policy,
219
                      Callbacks& cb,
220
                      RandomNumberGenerator& rng,
221
                      const std::string& hostname,
222
                      const std::vector<std::string>& next_protocols,
223
                      const std::optional<Session>& session = std::nullopt);
224
225
      static std::variant<Client_Hello_13, Client_Hello_12>
226
      parse(const std::vector<uint8_t>& buf);
227
228
      void retry(const Hello_Retry_Request& hrr,
229
                 const Transcript_Hash_State& transcript_hash_state,
230
                 Callbacks& cb,
231
                 RandomNumberGenerator& rng);
232
233
      /**
234
       * Select the highest protocol version from the list of versions
235
       * supported by the client. If no such version can be determind this
236
       * returns std::nullopt.
237
       */
238
      std::optional<Protocol_Version> highest_supported_version(const Policy& policy) const;
239
240
      /**
241
       * This validates that a Client Hello received after sending a Hello
242
       * Retry Request was updated in accordance with RFC 8446 4.1.2. If issues
243
       * are found, this method throws accordingly.
244
       */
245
      void validate_updates(const Client_Hello_13& new_ch);
246
247
   private:
248
      Client_Hello_13(std::unique_ptr<Client_Hello_Internal> data);
249
250
      /**
251
       * If the Client Hello contains a PSK extensions with identities this will
252
       * generate the PSK binders as described in RFC 8446 4.2.11.2.
253
       * Note that the passed in \p transcript_hash_state might be virgin for
254
       * the initial Client Hello and should be primed with ClientHello1 and
255
       * HelloRetryRequest for an updated Client Hello.
256
       */
257
      void calculate_psk_binders(Transcript_Hash_State transcript_hash_state);
258
   };
259
260
#endif // BOTAN_HAS_TLS_13
261
262
class Server_Hello_Internal;
263
264
/**
265
* Server Hello Message
266
*/
267
class BOTAN_UNSTABLE_API Server_Hello : public Handshake_Message
268
   {
269
   public:
270
      Server_Hello(const Server_Hello&) = delete;
271
      Server_Hello& operator=(const Server_Hello&) = delete;
272
      Server_Hello(Server_Hello&&);
273
      Server_Hello& operator=(Server_Hello&&);
274
275
      ~Server_Hello();
276
277
      std::vector<uint8_t> serialize() const override;
278
279
      Handshake_Type type() const override;
280
281
      // methods available in both subclasses' interface
282
      uint16_t ciphersuite() const;
283
      const Extensions& extensions() const;
284
      const std::vector<uint8_t>& session_id() const;
285
286
      virtual Protocol_Version selected_version() const = 0;
287
288
   protected:
289
      explicit Server_Hello(std::unique_ptr<Server_Hello_Internal> data);
290
291
      // methods used internally and potentially exposed by one of the subclasses
292
      std::set<Extension_Code> extension_types() const;
293
      const std::vector<uint8_t>& random() const;
294
      uint8_t compression_method() const;
295
      Protocol_Version legacy_version() const;
296
297
   protected:
298
      std::unique_ptr<Server_Hello_Internal> m_data;
299
   };
300
301
class BOTAN_UNSTABLE_API Server_Hello_12 final : public Server_Hello
302
   {
303
   public:
304
      class Settings final
305
         {
306
         public:
307
            Settings(const std::vector<uint8_t> new_session_id,
308
                     Protocol_Version new_session_version,
309
                     uint16_t ciphersuite,
310
                     bool offer_session_ticket) :
311
               m_new_session_id(new_session_id),
312
               m_new_session_version(new_session_version),
313
               m_ciphersuite(ciphersuite),
314
20.9k
               m_offer_session_ticket(offer_session_ticket) {}
315
316
20.9k
            const std::vector<uint8_t>& session_id() const { return m_new_session_id; }
317
41.9k
            Protocol_Version protocol_version() const { return m_new_session_version; }
318
20.9k
            uint16_t ciphersuite() const { return m_ciphersuite; }
319
6.11k
            bool offer_session_ticket() const { return m_offer_session_ticket; }
320
321
         private:
322
            const std::vector<uint8_t> m_new_session_id;
323
            Protocol_Version m_new_session_version;
324
            uint16_t m_ciphersuite;
325
            bool m_offer_session_ticket;
326
         };
327
328
      Server_Hello_12(Handshake_IO& io,
329
                      Handshake_Hash& hash,
330
                      const Policy& policy,
331
                      Callbacks& cb,
332
                      RandomNumberGenerator& rng,
333
                      const std::vector<uint8_t>& secure_reneg_info,
334
                      const Client_Hello_12& client_hello,
335
                      const Settings& settings,
336
                      const std::string& next_protocol);
337
338
      Server_Hello_12(Handshake_IO& io,
339
                      Handshake_Hash& hash,
340
                      const Policy& policy,
341
                      Callbacks& cb,
342
                      RandomNumberGenerator& rng,
343
                      const std::vector<uint8_t>& secure_reneg_info,
344
                      const Client_Hello_12& client_hello,
345
                      Session& resumed_session,
346
                      bool offer_session_ticket,
347
                      const std::string& next_protocol);
348
349
      explicit Server_Hello_12(const std::vector<uint8_t> &buf);
350
351
   protected:
352
      friend class Server_Hello_13;  // to allow construction by Server_Hello_13::parse()
353
      explicit Server_Hello_12(std::unique_ptr<Server_Hello_Internal> data);
354
355
   public:
356
      using Server_Hello::random;
357
      using Server_Hello::compression_method;
358
      using Server_Hello::extension_types;
359
      using Server_Hello::legacy_version;
360
361
      /**
362
       * @returns the selected version as indicated in the legacy_version field
363
       */
364
      Protocol_Version selected_version() const override;
365
366
      bool secure_renegotiation() const;
367
368
      std::vector<uint8_t> renegotiation_info() const;
369
370
      std::string next_protocol() const;
371
372
      bool supports_extended_master_secret() const;
373
374
      bool supports_encrypt_then_mac() const;
375
376
      bool supports_certificate_status_message() const;
377
378
      bool supports_session_ticket() const;
379
380
      uint16_t srtp_profile() const;
381
      bool prefers_compressed_ec_points() const;
382
383
      /**
384
       * Return desired downgrade version indicated by hello random, if any.
385
       */
386
      std::optional<Protocol_Version> random_signals_downgrade() const;
387
   };
388
389
#if defined(BOTAN_HAS_TLS_13)
390
391
class Hello_Retry_Request;
392
393
class BOTAN_UNSTABLE_API Server_Hello_13 : public Server_Hello
394
   {
395
   protected:
396
      static struct Server_Hello_Tag {} as_server_hello;
397
      static struct Hello_Retry_Request_Tag {} as_hello_retry_request;
398
      static struct Hello_Retry_Request_Creation_Tag {} as_new_hello_retry_request;
399
400
      // These constructors are meant for instantiating Server Hellos
401
      // after parsing a peer's message. They perform basic validation
402
      // and are therefore not suitable for constructing a message to
403
      // be sent to a client.
404
      explicit Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data, Server_Hello_Tag tag = as_server_hello);
405
      explicit Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data, Hello_Retry_Request_Tag tag);
406
      void basic_validation() const;
407
408
      // Instantiate a Server Hello as response to a client's Client Hello
409
      // (called from Server_Hello_13::create())
410
      Server_Hello_13(const Client_Hello_13& ch, std::optional<Named_Group> key_exchange_group, RandomNumberGenerator& rng, Callbacks& cb, const Policy& policy);
411
412
      explicit Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data, Hello_Retry_Request_Creation_Tag tag);
413
414
   public:
415
      static std::variant<Hello_Retry_Request, Server_Hello_13>
416
      create(const Client_Hello_13& ch, bool hello_retry_request_allowed, RandomNumberGenerator& rng, const Policy& policy, Callbacks& cb);
417
418
      static std::variant<Hello_Retry_Request, Server_Hello_13, Server_Hello_12>
419
      parse(const std::vector<uint8_t>& buf);
420
421
      /**
422
       * Return desired downgrade version indicated by hello random, if any.
423
       */
424
      std::optional<Protocol_Version> random_signals_downgrade() const;
425
426
      /**
427
       * @returns the selected version as indicated by the supported_versions extension
428
       */
429
      Protocol_Version selected_version() const override;
430
   };
431
432
class BOTAN_UNSTABLE_API Hello_Retry_Request final : public Server_Hello_13
433
   {
434
   protected:
435
      friend class Server_Hello_13;  // to allow construction by Server_Hello_13::parse() and ::create()
436
      explicit Hello_Retry_Request(std::unique_ptr<Server_Hello_Internal> data);
437
      Hello_Retry_Request(const Client_Hello_13& ch, Named_Group selected_group, const Policy& policy, Callbacks& cb);
438
439
   public:
440
0
      Handshake_Type type() const override { return HELLO_RETRY_REQUEST; }
441
0
      Handshake_Type wire_type() const override { return SERVER_HELLO; }
442
   };
443
444
#endif // BOTAN_HAS_TLS_13
445
446
class BOTAN_UNSTABLE_API Encrypted_Extensions final : public Handshake_Message
447
   {
448
   public:
449
      explicit Encrypted_Extensions(const std::vector<uint8_t>& buf);
450
      Encrypted_Extensions(const Client_Hello_13& client_hello, const Policy& policy, Callbacks& cb);
451
452
8.60k
      Handshake_Type type() const override { return Handshake_Type::ENCRYPTED_EXTENSIONS; }
453
454
0
      const Extensions& extensions() const { return m_extensions; }
455
456
      std::vector<uint8_t> serialize() const override;
457
458
   private:
459
      Extensions m_extensions;
460
   };
461
462
/**
463
* Client Key Exchange Message
464
*/
465
class BOTAN_UNSTABLE_API Client_Key_Exchange final : public Handshake_Message
466
   {
467
   public:
468
0
      Handshake_Type type() const override { return CLIENT_KEX; }
469
470
      const secure_vector<uint8_t>& pre_master_secret() const
471
14.2k
         { return m_pre_master; }
472
473
      Client_Key_Exchange(Handshake_IO& io,
474
                          Handshake_State& state,
475
                          const Policy& policy,
476
                          Credentials_Manager& creds,
477
                          const Public_Key* server_public_key,
478
                          const std::string& hostname,
479
                          RandomNumberGenerator& rng);
480
481
      Client_Key_Exchange(const std::vector<uint8_t>& buf,
482
                          const Handshake_State& state,
483
                          const Private_Key* server_rsa_kex_key,
484
                          Credentials_Manager& creds,
485
                          const Policy& policy,
486
                          RandomNumberGenerator& rng);
487
488
   private:
489
      std::vector<uint8_t> serialize() const override
490
0
         { return m_key_material; }
491
492
      std::vector<uint8_t> m_key_material;
493
      secure_vector<uint8_t> m_pre_master;
494
   };
495
496
/**
497
* Certificate Message of TLS 1.2
498
*/
499
class BOTAN_UNSTABLE_API Certificate_12 final : public Handshake_Message
500
   {
501
   public:
502
267
      Handshake_Type type() const override { return CERTIFICATE; }
503
134
      const std::vector<X509_Certificate>& cert_chain() const { return m_certs; }
504
505
0
      size_t count() const { return m_certs.size(); }
506
0
      bool empty() const { return m_certs.empty(); }
507
508
      Certificate_12(Handshake_IO& io,
509
                     Handshake_Hash& hash,
510
                     const std::vector<X509_Certificate>& certs);
511
512
      Certificate_12(const std::vector<uint8_t>& buf, const Policy& policy);
513
514
      std::vector<uint8_t> serialize() const override;
515
516
   private:
517
      std::vector<X509_Certificate> m_certs;
518
   };
519
520
class Certificate_Request_13;
521
522
/**
523
* Certificate Message of TLS 1.3
524
*/
525
class BOTAN_UNSTABLE_API Certificate_13 final : public Handshake_Message
526
   {
527
   public:
528
      struct Certificate_Entry
529
         {
530
         // TODO: RFC 8446 4.4.2 specifies the possibility to negotiate the usage
531
         //       of a single raw public key in lieu of the X.509 certificate
532
         //       chain. This is left for future work.
533
         X509_Certificate certificate;
534
         Extensions       extensions;
535
         };
536
537
   public:
538
0
      Handshake_Type type() const override { return CERTIFICATE; }
539
      std::vector<X509_Certificate> cert_chain() const;
540
541
0
      size_t count() const { return m_entries.size(); }
542
0
      bool empty() const { return m_entries.empty(); }
543
      const X509_Certificate& leaf() const;
544
0
      const std::vector<uint8_t>& request_context() const { return m_request_context; }
545
546
      /**
547
       * Create a Client Certificate message
548
       * ... in response to a Certificate Request message.
549
       */
550
      Certificate_13(const Certificate_Request_13& cert_request,
551
                     const std::string& hostname,
552
                     Credentials_Manager& credentials_manager,
553
                     Callbacks& callbacks);
554
555
      /**
556
       * Create a Server Certificate message
557
       * ... in response to a Client Hello indicating the need to authenticate
558
       *     with a server certificate.
559
       */
560
      Certificate_13(const Client_Hello_13& client_hello,
561
                     Credentials_Manager& credentials_manager,
562
                     Callbacks& callbacks);
563
564
      /**
565
      * Deserialize a Certificate message
566
      * @param buf the serialized message
567
      * @param policy the TLS policy
568
      * @param side is this a SERVER or CLIENT certificate message
569
      */
570
      Certificate_13(const std::vector<uint8_t>& buf,
571
                     const Policy& policy,
572
                     const Connection_Side side);
573
574
      /**
575
      * Validate a Certificate message regarding what extensions are expected based on
576
      * previous handshake messages. Also call the tls_examine_extenions() callback
577
      * for each entry.
578
      *
579
      * @param requested_extensions Extensions of Client_Hello or Certificate_Request messages
580
      */
581
      void validate_extensions(const std::set<Extension_Code>& requested_extensions, Callbacks& cb) const;
582
583
      /**
584
       * Verify the certificate chain
585
       *
586
       * @throws if verification fails.
587
       */
588
      void verify(Callbacks& callbacks,
589
                  const Policy& policy,
590
                  Credentials_Manager& creds,
591
                  const std::string& hostname,
592
                  bool use_ocsp) const;
593
594
      std::vector<uint8_t> serialize() const override;
595
596
   private:
597
      void setup_entries(std::vector<X509_Certificate> cert_chain,
598
                         const Certificate_Status_Request* csr,
599
                         Callbacks& callbacks);
600
601
   private:
602
      std::vector<uint8_t>           m_request_context;
603
      std::vector<Certificate_Entry> m_entries;
604
      Connection_Side                m_side;
605
   };
606
607
/**
608
* Certificate Status (RFC 6066)
609
*/
610
class BOTAN_UNSTABLE_API Certificate_Status final : public Handshake_Message
611
   {
612
   public:
613
0
      Handshake_Type type() const override { return CERTIFICATE_STATUS; }
614
615
      //std::shared_ptr<const OCSP::Response> response() const { return m_response; }
616
617
0
      const std::vector<uint8_t>& response() const { return m_response; }
618
619
      explicit Certificate_Status(const std::vector<uint8_t>& buf, const Connection_Side from);
620
621
      Certificate_Status(Handshake_IO& io,
622
                         Handshake_Hash& hash,
623
                         const OCSP::Response& response);
624
625
      /*
626
       * Create a Certificate_Status message using an already DER encoded OCSP response.
627
       */
628
      Certificate_Status(Handshake_IO& io,
629
                         Handshake_Hash& hash,
630
                         std::vector<uint8_t> raw_response_bytes);
631
632
      Certificate_Status(std::vector<uint8_t> raw_response_bytes);
633
634
      std::vector<uint8_t> serialize() const override;
635
636
   private:
637
      std::vector<uint8_t> m_response;
638
   };
639
640
/**
641
* Certificate Request Message (TLS 1.2)
642
*/
643
class BOTAN_UNSTABLE_API Certificate_Request_12 final : public Handshake_Message
644
   {
645
   public:
646
      Handshake_Type type() const override;
647
648
      const std::vector<std::string>& acceptable_cert_types() const;
649
650
      const std::vector<X509_DN>& acceptable_CAs() const;
651
652
      const std::vector<Signature_Scheme>& signature_schemes() const;
653
654
      Certificate_Request_12(Handshake_IO& io,
655
                      Handshake_Hash& hash,
656
                      const Policy& policy,
657
                      const std::vector<X509_DN>& allowed_cas);
658
659
      explicit Certificate_Request_12(const std::vector<uint8_t>& buf);
660
661
      std::vector<uint8_t> serialize() const override;
662
663
   private:
664
      std::vector<X509_DN> m_names;
665
      std::vector<std::string> m_cert_key_types;
666
      std::vector<Signature_Scheme> m_schemes;
667
   };
668
669
#if defined(BOTAN_HAS_TLS_13)
670
671
class BOTAN_UNSTABLE_API Certificate_Request_13 final : public Handshake_Message
672
   {
673
   public:
674
      Handshake_Type type() const override;
675
676
      Certificate_Request_13(const std::vector<uint8_t>& buf, const Connection_Side side);
677
678
      //! Creates a Certificate_Request message if it is required by the configuration
679
      //! @return std::nullopt if configuration does not require client authentication
680
      static std::optional<Certificate_Request_13> maybe_create(const Client_Hello_13& sni_hostname,
681
                                                                Credentials_Manager& cred_mgr,
682
                                                                Callbacks& callbacks,
683
                                                                const Policy& policy);
684
685
      std::vector<X509_DN> acceptable_CAs() const;
686
      const std::vector<Signature_Scheme>& signature_schemes() const;
687
0
      const Extensions& extensions() const { return m_extensions; }
688
689
      std::vector<uint8_t> serialize() const override;
690
691
0
      const std::vector<uint8_t> context() const { return m_context; }
692
693
   private:
694
      Certificate_Request_13(std::vector<Signature_Scheme> signature_schemes,
695
                             std::vector<X509_DN> acceptable_CAs,
696
                             Callbacks& callbacks);
697
698
   private:
699
      std::vector<uint8_t> m_context;
700
      Extensions m_extensions;
701
   };
702
703
#endif
704
705
class BOTAN_UNSTABLE_API Certificate_Verify : public Handshake_Message
706
   {
707
   public:
708
0
      Handshake_Type type() const override { return CERTIFICATE_VERIFY; }
709
710
0
      Signature_Scheme signature_scheme() const { return m_scheme; }
711
712
      Certificate_Verify(const std::vector<uint8_t>& buf);
713
0
      Certificate_Verify() = default;
714
715
      std::vector<uint8_t> serialize() const override;
716
717
   protected:
718
      std::vector<uint8_t> m_signature;
719
      Signature_Scheme m_scheme;
720
   };
721
722
/**
723
* Certificate Verify Message
724
*/
725
class BOTAN_UNSTABLE_API Certificate_Verify_12 final : public Certificate_Verify
726
   {
727
   public:
728
      using Certificate_Verify::Certificate_Verify;
729
730
      Certificate_Verify_12(Handshake_IO& io,
731
                            Handshake_State& state,
732
                            const Policy& policy,
733
                            RandomNumberGenerator& rng,
734
                            const Private_Key* key);
735
736
      /**
737
      * Check the signature on a certificate verify message
738
      * @param cert the purported certificate
739
      * @param state the handshake state
740
      * @param policy the TLS policy
741
      */
742
      bool verify(const X509_Certificate& cert,
743
                  const Handshake_State& state,
744
                  const Policy& policy) const;
745
   };
746
747
#if defined(BOTAN_HAS_TLS_13)
748
749
/**
750
* Certificate Verify Message
751
*/
752
class BOTAN_UNSTABLE_API Certificate_Verify_13 final : public Certificate_Verify
753
   {
754
   public:
755
      /**
756
      * Deserialize a Certificate message
757
      * @param buf the serialized message
758
      * @param side is this a SERVER or CLIENT certificate message
759
      */
760
      Certificate_Verify_13(const std::vector<uint8_t>& buf,
761
                            const Connection_Side side);
762
763
      Certificate_Verify_13(
764
            const Certificate_13& certificate_message,
765
            const std::vector<Signature_Scheme>& peer_allowed_schemes,
766
            const std::string& hostname,
767
            const Transcript_Hash& hash,
768
            Connection_Side whoami,
769
            Credentials_Manager& creds_mgr,
770
            const Policy& policy,
771
            Callbacks& callbacks,
772
            RandomNumberGenerator& rng);
773
774
      bool verify(const X509_Certificate& cert,
775
                  Callbacks& callbacks,
776
                  const Transcript_Hash& transcript_hash) const;
777
778
   private:
779
      Connection_Side m_side;
780
   };
781
782
#endif
783
784
/**
785
* Finished Message
786
*/
787
class BOTAN_UNSTABLE_API Finished : public Handshake_Message
788
   {
789
   public:
790
      explicit Finished(const std::vector<uint8_t>& buf);
791
792
710
      Handshake_Type type() const override { return FINISHED; }
793
794
      std::vector<uint8_t> verify_data() const;
795
796
      std::vector<uint8_t> serialize() const override;
797
798
   protected:
799
      using Handshake_Message::Handshake_Message;
800
      std::vector<uint8_t> m_verification_data;
801
   };
802
803
class BOTAN_UNSTABLE_API Finished_12 final : public Finished
804
   {
805
   public:
806
      using Finished::Finished;
807
      Finished_12(Handshake_IO& io,
808
                  Handshake_State& state,
809
                  Connection_Side side);
810
811
      bool verify(const Handshake_State& state, Connection_Side side) const;
812
   };
813
814
#if defined(BOTAN_HAS_TLS_13)
815
class BOTAN_UNSTABLE_API Finished_13 final : public Finished
816
   {
817
   public:
818
      using Finished::Finished;
819
      Finished_13(Cipher_State* cipher_state,
820
                  const Transcript_Hash& transcript_hash);
821
822
      bool verify(Cipher_State* cipher_state,
823
                  const Transcript_Hash& transcript_hash) const;
824
   };
825
#endif
826
827
/**
828
* Hello Request Message
829
*/
830
class BOTAN_UNSTABLE_API Hello_Request final : public Handshake_Message
831
   {
832
   public:
833
0
      Handshake_Type type() const override { return HELLO_REQUEST; }
834
835
      explicit Hello_Request(Handshake_IO& io);
836
      explicit Hello_Request(const std::vector<uint8_t>& buf);
837
838
   private:
839
      std::vector<uint8_t> serialize() const override;
840
   };
841
842
/**
843
* Server Key Exchange Message
844
*/
845
class BOTAN_UNSTABLE_API Server_Key_Exchange final : public Handshake_Message
846
   {
847
   public:
848
41.6k
      Handshake_Type type() const override { return SERVER_KEX; }
849
850
20.8k
      const std::vector<uint8_t>& params() const { return m_params; }
851
852
      bool verify(const Public_Key& server_key,
853
                  const Handshake_State& state,
854
                  const Policy& policy) const;
855
856
      // Only valid for certain kex types
857
      const Private_Key& server_kex_key() const;
858
859
      Server_Key_Exchange(Handshake_IO& io,
860
                          Handshake_State& state,
861
                          const Policy& policy,
862
                          Credentials_Manager& creds,
863
                          RandomNumberGenerator& rng,
864
                          const Private_Key* signing_key = nullptr);
865
866
      Server_Key_Exchange(const std::vector<uint8_t>& buf,
867
                          Kex_Algo kex_alg,
868
                          Auth_Method sig_alg,
869
                          Protocol_Version version);
870
871
   private:
872
      std::vector<uint8_t> serialize() const override;
873
874
      std::unique_ptr<Private_Key> m_kex_key;
875
876
      std::vector<uint8_t> m_params;
877
878
      std::vector<uint8_t> m_signature;
879
      Signature_Scheme m_scheme;
880
   };
881
882
/**
883
* Server Hello Done Message
884
*/
885
class BOTAN_UNSTABLE_API Server_Hello_Done final : public Handshake_Message
886
   {
887
   public:
888
41.6k
      Handshake_Type type() const override { return SERVER_HELLO_DONE; }
889
890
      explicit Server_Hello_Done(Handshake_IO& io, Handshake_Hash& hash);
891
      explicit Server_Hello_Done(const std::vector<uint8_t>& buf);
892
893
   private:
894
      std::vector<uint8_t> serialize() const override;
895
   };
896
897
/**
898
* New Session Ticket Message
899
*/
900
class BOTAN_UNSTABLE_API New_Session_Ticket_12 final : public Handshake_Message
901
   {
902
   public:
903
402
      Handshake_Type type() const override { return NEW_SESSION_TICKET; }
904
905
0
      uint32_t ticket_lifetime_hint() const { return m_ticket_lifetime_hint; }
906
0
      const std::vector<uint8_t>& ticket() const { return m_ticket; }
907
908
      New_Session_Ticket_12(Handshake_IO& io,
909
                            Handshake_Hash& hash,
910
                            const std::vector<uint8_t>& ticket,
911
                            uint32_t lifetime);
912
913
      New_Session_Ticket_12(Handshake_IO& io,
914
                            Handshake_Hash& hash);
915
916
      explicit New_Session_Ticket_12(const std::vector<uint8_t>& buf);
917
918
      std::vector<uint8_t> serialize() const override;
919
920
   private:
921
      uint32_t m_ticket_lifetime_hint = 0;
922
      std::vector<uint8_t> m_ticket;
923
   };
924
925
#if defined(BOTAN_HAS_TLS_13)
926
927
class BOTAN_UNSTABLE_API New_Session_Ticket_13 final : public Handshake_Message
928
   {
929
   public:
930
0
      Handshake_Type type() const override { return NEW_SESSION_TICKET; }
931
932
      New_Session_Ticket_13(const std::vector<uint8_t>& buf,
933
                            Connection_Side from);
934
935
      std::vector<uint8_t> serialize() const override;
936
937
0
      const Extensions& extensions() const { return m_extensions; }
938
939
0
      const std::vector<uint8_t>& ticket() const { return m_ticket; }
940
0
      const std::vector<uint8_t>& nonce() const { return m_ticket_nonce; }
941
0
      uint32_t ticket_age_add() const { return m_ticket_age_add; }
942
0
      uint32_t lifetime_hint() const { return m_ticket_lifetime_hint; }
943
944
      /**
945
       * @return  the number of bytes allowed for early data or std::nullopt
946
       *          when early data is not allowed at all
947
       */
948
      std::optional<uint32_t> early_data_byte_limit() const;
949
950
   private:
951
      // RFC 8446 4.6.1
952
      //    Clients MUST NOT cache tickets for longer than 7 days, regardless of
953
      //    the ticket_lifetime, and MAY delete tickets earlier based on local
954
      //    policy.  A server MAY treat a ticket as valid for a shorter period
955
      //    of time than what is stated in the ticket_lifetime.
956
      //
957
      // ... hence we call it 'lifetime hint'.
958
      uint32_t m_ticket_lifetime_hint;
959
      uint32_t m_ticket_age_add;
960
      std::vector<uint8_t> m_ticket_nonce;
961
      std::vector<uint8_t> m_ticket;
962
      Extensions m_extensions;
963
   };
964
965
#endif
966
967
/**
968
* Change Cipher Spec
969
*/
970
class BOTAN_UNSTABLE_API Change_Cipher_Spec final : public Handshake_Message
971
   {
972
   public:
973
355
      Handshake_Type type() const override { return HANDSHAKE_CCS; }
974
975
      std::vector<uint8_t> serialize() const override
976
355
         { return std::vector<uint8_t>(1, 1); }
977
   };
978
979
class BOTAN_UNSTABLE_API Key_Update final : public Handshake_Message
980
   {
981
   public:
982
0
      Handshake_Type type() const override { return  KEY_UPDATE; }
983
984
      explicit Key_Update(const bool request_peer_update);
985
      explicit Key_Update(const std::vector<uint8_t>& buf);
986
987
      std::vector<uint8_t> serialize() const override;
988
989
0
      bool expects_reciprocation() const { return m_update_requested; }
990
991
   private:
992
      bool m_update_requested;
993
   };
994
995
#if defined(BOTAN_HAS_TLS_13)
996
997
namespace {
998
template <typename T>
999
struct as_wrapped_references
1000
   {
1001
   };
1002
1003
template <typename... AlternativeTs>
1004
struct as_wrapped_references<std::variant<AlternativeTs...>>
1005
   {
1006
   using type = std::variant<std::reference_wrapper<AlternativeTs>...>;
1007
   };
1008
1009
template <typename T>
1010
using as_wrapped_references_t = typename as_wrapped_references<T>::type;
1011
}
1012
1013
// Handshake message types from RFC 8446 4.
1014
using Handshake_Message_13 = std::variant<
1015
                             Client_Hello_13,
1016
                             Client_Hello_12,
1017
                             Server_Hello_13,
1018
                             Server_Hello_12,
1019
                             Hello_Retry_Request,
1020
                             // End_Of_Early_Data,
1021
                             Encrypted_Extensions,
1022
                             Certificate_13,
1023
                             Certificate_Request_13,
1024
                             Certificate_Verify_13,
1025
                             Finished_13>;
1026
using Handshake_Message_13_Ref = as_wrapped_references_t<Handshake_Message_13>;
1027
1028
using Post_Handshake_Message_13 = std::variant<
1029
                                  New_Session_Ticket_13,
1030
                                  Key_Update>;
1031
1032
// Key_Update is handled generically by the Channel. The messages assigned
1033
// to those variants are the ones that need to be handled by the specific
1034
// client and/or server implementations.
1035
using Server_Post_Handshake_13_Message = std::variant<New_Session_Ticket_13, Key_Update>;
1036
using Client_Post_Handshake_13_Message = std::variant<Key_Update>;
1037
1038
using Server_Handshake_13_Message = std::variant<
1039
                                    Server_Hello_13,
1040
                                    Server_Hello_12,  // indicates a TLS version downgrade
1041
                                    Hello_Retry_Request,
1042
                                    Encrypted_Extensions,
1043
                                    Certificate_13,
1044
                                    Certificate_Request_13,
1045
                                    Certificate_Verify_13,
1046
                                    Finished_13>;
1047
using Server_Handshake_13_Message_Ref = as_wrapped_references_t<Server_Handshake_13_Message>;
1048
1049
using Client_Handshake_13_Message = std::variant<
1050
                                    Client_Hello_13,
1051
                                    Client_Hello_12,  // indicates a TLS peer that does not offer TLS 1.3
1052
                                    Certificate_13,
1053
                                    Certificate_Verify_13,
1054
                                    Finished_13>;
1055
using Client_Handshake_13_Message_Ref = as_wrapped_references_t<Client_Handshake_13_Message>;
1056
1057
#endif // BOTAN_HAS_TLS_13
1058
1059
}
1060
1061
}
1062
1063
#endif