Coverage Report

Created: 2022-05-14 06:06

/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
#if defined(BOTAN_HAS_CECPQ1)
28
  #include <botan/cecpq1.h>
29
#endif
30
31
namespace Botan {
32
33
class Public_Key;
34
class Credentials_Manager;
35
36
namespace OCSP {
37
class Response;
38
}
39
40
namespace TLS {
41
42
class Session;
43
class Handshake_IO;
44
class Handshake_State;
45
class Hello_Retry_Request;
46
class Callbacks;
47
class Cipher_State;
48
49
std::vector<uint8_t> make_hello_random(RandomNumberGenerator& rng,
50
                                       const Policy& policy);
51
52
/**
53
* DTLS Hello Verify Request
54
*/
55
class BOTAN_UNSTABLE_API Hello_Verify_Request final : public Handshake_Message
56
   {
57
   public:
58
      std::vector<uint8_t> serialize() const override;
59
2.27k
      Handshake_Type type() const override { return HELLO_VERIFY_REQUEST; }
60
61
1.14k
      const std::vector<uint8_t>& cookie() const { return m_cookie; }
62
63
      explicit Hello_Verify_Request(const std::vector<uint8_t>& buf);
64
65
      Hello_Verify_Request(const std::vector<uint8_t>& client_hello_bits,
66
                           const std::string& client_identity,
67
                           const SymmetricKey& secret_key);
68
69
   private:
70
      std::vector<uint8_t> m_cookie;
71
   };
72
73
/**
74
* Client Hello Message
75
*/
76
class BOTAN_UNSTABLE_API Client_Hello : public Handshake_Message
77
   {
78
   public:
79
      Handshake_Type type() const override;
80
81
      /**
82
       * Return the version indicated in the ClientHello.
83
       * This may differ from the version indicated in the supported_versions extension.
84
       *
85
       * See RFC 8446 4.1.2:
86
       *   TLS 1.3, the client indicates its version preferences in the
87
       *   "supported_versions" extension (Section 4.2.1) and the
88
       *   legacy_version field MUST be set to 0x0303, which is the version
89
       *   number for TLS 1.2.
90
       */
91
      Protocol_Version legacy_version() const;
92
93
      const std::vector<uint8_t>& random() const;
94
95
      const std::vector<uint8_t>& session_id() const;
96
97
      const std::vector<uint16_t>& ciphersuites() const;
98
99
      bool offered_suite(uint16_t ciphersuite) const;
100
101
      std::vector<Signature_Scheme> signature_schemes() const;
102
103
      std::vector<Group_Params> supported_ecc_curves() const;
104
105
      std::vector<Group_Params> supported_dh_groups() const;
106
107
      std::vector<Protocol_Version> supported_versions() const;
108
109
      std::string sni_hostname() const;
110
111
      bool supports_alpn() const;
112
113
      bool sent_signature_algorithms() const;
114
115
      std::vector<std::string> next_protocols() const;
116
117
      std::vector<uint16_t> srtp_profiles() const;
118
119
      std::vector<uint8_t> serialize() const override;
120
121
122
      const std::vector<uint8_t>& cookie() const;
123
124
      std::vector<uint8_t> cookie_input_data() const;
125
126
      std::set<Handshake_Extension_Type> extension_types() const;
127
128
      const Extensions& extensions() const;
129
130
   protected:
131
1.49k
      Client_Hello() : m_comp_methods({ 0 }) {}
132
133
      explicit Client_Hello(const std::vector<uint8_t>& buf);
134
135
      const std::vector<uint8_t>& compression_methods() const;
136
137
   protected:
138
      Protocol_Version m_legacy_version;
139
      std::vector<uint8_t> m_session_id;
140
      std::vector<uint8_t> m_random;
141
      std::vector<uint16_t> m_suites;
142
      std::vector<uint8_t> m_comp_methods;
143
      Extensions m_extensions;
144
145
      std::vector<uint8_t> m_hello_cookie; // DTLS only
146
      std::vector<uint8_t> m_cookie_input_bits; // DTLS only
147
   };
148
149
class BOTAN_UNSTABLE_API Client_Hello_12 final : public Client_Hello
150
   {
151
   public:
152
      class Settings final
153
         {
154
         public:
155
            Settings(const Protocol_Version version,
156
                     const std::string& hostname = ""):
157
               m_new_session_version(version),
158
1.49k
               m_hostname(hostname) {}
159
160
2.99k
            const Protocol_Version protocol_version() const { return m_new_session_version; }
161
2.99k
            const std::string& hostname() const { return m_hostname; }
162
163
         private:
164
            const Protocol_Version m_new_session_version;
165
            const std::string m_hostname;
166
         };
167
168
   public:
169
23.8k
      explicit Client_Hello_12(const std::vector<uint8_t>& buf) : Client_Hello(buf) {}
170
171
      Client_Hello_12(Handshake_IO& io,
172
                      Handshake_Hash& hash,
173
                      const Policy& policy,
174
                      Callbacks& cb,
175
                      RandomNumberGenerator& rng,
176
                      const std::vector<uint8_t>& reneg_info,
177
                      const Settings& client_settings,
178
                      const std::vector<std::string>& next_protocols);
179
180
      Client_Hello_12(Handshake_IO& io,
181
                      Handshake_Hash& hash,
182
                      const Policy& policy,
183
                      Callbacks& cb,
184
                      RandomNumberGenerator& rng,
185
                      const std::vector<uint8_t>& reneg_info,
186
                      const Session& session,
187
                      const std::vector<std::string>& next_protocols);
188
189
      using Client_Hello::random;
190
      using Client_Hello::compression_methods;
191
192
      bool prefers_compressed_ec_points() const;
193
194
      bool secure_renegotiation() const;
195
196
      std::vector<uint8_t> renegotiation_info() const;
197
198
      bool supports_session_ticket() const;
199
200
      std::vector<uint8_t> session_ticket() const;
201
202
      bool supports_extended_master_secret() const;
203
204
      bool supports_cert_status_message() const;
205
206
      bool supports_encrypt_then_mac() const;
207
208
      void update_hello_cookie(const Hello_Verify_Request& hello_verify);
209
   };
210
211
/**
212
* Server Hello Message
213
*/
214
class BOTAN_UNSTABLE_API Server_Hello : public Handshake_Message
215
   {
216
   public:
217
      std::vector<uint8_t> serialize() const override;
218
219
      Handshake_Type type() const override;
220
221
      // methods available in both subclasses' interface
222
      uint16_t ciphersuite() const;
223
      const Extensions& extensions() const;
224
      const std::vector<uint8_t>& session_id() const;
225
226
      virtual Protocol_Version selected_version() const = 0;
227
228
   protected:
229
      /**
230
       * Version-agnostic internal server hello data container that allows
231
       * parsing Server_Hello messages without prior knowledge of the contained
232
       * protocol version.
233
       */
234
      class Internal
235
         {
236
         public:
237
            Internal(const std::vector<uint8_t>& buf);
238
239
            Internal(Protocol_Version legacy_version,
240
                     std::vector<uint8_t> session_id,
241
                     std::vector<uint8_t> random,
242
                     const uint16_t ciphersuite,
243
                     const uint8_t comp_method);
244
245
            Protocol_Version version() const;
246
247
         public:
248
            Protocol_Version legacy_version;
249
            std::vector<uint8_t> session_id;
250
            std::vector<uint8_t> random;
251
            bool is_hello_retry_request;
252
            uint16_t ciphersuite;
253
            uint8_t  comp_method;
254
255
            Extensions  extensions;
256
         };
257
258
   protected:
259
      explicit Server_Hello(std::unique_ptr<Internal> data)
260
20.8k
         : m_data(std::move(data)) {}
261
262
      // methods used internally and potentially exposed by one of the subclasses
263
      std::set<Handshake_Extension_Type> extension_types() const;
264
      const std::vector<uint8_t>& random() const;
265
      uint8_t compression_method() const;
266
      Protocol_Version legacy_version() const;
267
268
   protected:
269
      std::unique_ptr<Internal> m_data;
270
   };
271
272
class BOTAN_UNSTABLE_API Server_Hello_12 final : public Server_Hello
273
   {
274
   public:
275
      class Settings final
276
         {
277
         public:
278
            Settings(const std::vector<uint8_t> new_session_id,
279
                     Protocol_Version new_session_version,
280
                     uint16_t ciphersuite,
281
                     bool offer_session_ticket) :
282
               m_new_session_id(new_session_id),
283
               m_new_session_version(new_session_version),
284
               m_ciphersuite(ciphersuite),
285
20.7k
               m_offer_session_ticket(offer_session_ticket) {}
286
287
20.7k
            const std::vector<uint8_t>& session_id() const { return m_new_session_id; }
288
41.5k
            Protocol_Version protocol_version() const { return m_new_session_version; }
289
20.7k
            uint16_t ciphersuite() const { return m_ciphersuite; }
290
7.08k
            bool offer_session_ticket() const { return m_offer_session_ticket; }
291
292
         private:
293
            const std::vector<uint8_t> m_new_session_id;
294
            Protocol_Version m_new_session_version;
295
            uint16_t m_ciphersuite;
296
            bool m_offer_session_ticket;
297
         };
298
299
      Server_Hello_12(Handshake_IO& io,
300
                      Handshake_Hash& hash,
301
                      const Policy& policy,
302
                      Callbacks& cb,
303
                      RandomNumberGenerator& rng,
304
                      const std::vector<uint8_t>& secure_reneg_info,
305
                      const Client_Hello_12& client_hello,
306
                      const Settings& settings,
307
                      const std::string& next_protocol);
308
309
      Server_Hello_12(Handshake_IO& io,
310
                      Handshake_Hash& hash,
311
                      const Policy& policy,
312
                      Callbacks& cb,
313
                      RandomNumberGenerator& rng,
314
                      const std::vector<uint8_t>& secure_reneg_info,
315
                      const Client_Hello_12& client_hello,
316
                      Session& resumed_session,
317
                      bool offer_session_ticket,
318
                      const std::string& next_protocol);
319
320
      explicit Server_Hello_12(const std::vector<uint8_t> &buf);
321
322
   protected:
323
      friend class Server_Hello_13;  // to allow construction by Server_Hello_13::parse()
324
      explicit Server_Hello_12(std::unique_ptr<Server_Hello::Internal> data);
325
326
   public:
327
      using Server_Hello::random;
328
      using Server_Hello::compression_method;
329
      using Server_Hello::extension_types;
330
      using Server_Hello::legacy_version;
331
332
      /**
333
       * @returns the selected version as indicated in the legacy_version field
334
       */
335
      Protocol_Version selected_version() const override;
336
337
      bool secure_renegotiation() const;
338
339
      std::vector<uint8_t> renegotiation_info() const;
340
341
      std::string next_protocol() const;
342
343
      bool supports_extended_master_secret() const;
344
345
      bool supports_encrypt_then_mac() const;
346
347
      bool supports_certificate_status_message() const;
348
349
      bool supports_session_ticket() const;
350
351
      uint16_t srtp_profile() const;
352
      bool prefers_compressed_ec_points() const;
353
354
      /**
355
       * Return desired downgrade version indicated by hello random, if any.
356
       */
357
      std::optional<Protocol_Version> random_signals_downgrade() const;
358
   };
359
360
/**
361
* Client Key Exchange Message
362
*/
363
class BOTAN_UNSTABLE_API Client_Key_Exchange final : public Handshake_Message
364
   {
365
   public:
366
0
      Handshake_Type type() const override { return CLIENT_KEX; }
367
368
      const secure_vector<uint8_t>& pre_master_secret() const
369
14.3k
         { return m_pre_master; }
370
371
      Client_Key_Exchange(Handshake_IO& io,
372
                          Handshake_State& state,
373
                          const Policy& policy,
374
                          Credentials_Manager& creds,
375
                          const Public_Key* server_public_key,
376
                          const std::string& hostname,
377
                          RandomNumberGenerator& rng);
378
379
      Client_Key_Exchange(const std::vector<uint8_t>& buf,
380
                          const Handshake_State& state,
381
                          const Private_Key* server_rsa_kex_key,
382
                          Credentials_Manager& creds,
383
                          const Policy& policy,
384
                          RandomNumberGenerator& rng);
385
386
   private:
387
      std::vector<uint8_t> serialize() const override
388
0
         { return m_key_material; }
389
390
      std::vector<uint8_t> m_key_material;
391
      secure_vector<uint8_t> m_pre_master;
392
   };
393
394
/**
395
* Certificate Message of TLS 1.2
396
*/
397
class BOTAN_UNSTABLE_API Certificate_12 final : public Handshake_Message
398
   {
399
   public:
400
197
      Handshake_Type type() const override { return CERTIFICATE; }
401
99
      const std::vector<X509_Certificate>& cert_chain() const { return m_certs; }
402
403
0
      size_t count() const { return m_certs.size(); }
404
0
      bool empty() const { return m_certs.empty(); }
405
406
      Certificate_12(Handshake_IO& io,
407
                     Handshake_Hash& hash,
408
                     const std::vector<X509_Certificate>& certs);
409
410
      Certificate_12(const std::vector<uint8_t>& buf, const Policy& policy);
411
412
      std::vector<uint8_t> serialize() const override;
413
414
   private:
415
      std::vector<X509_Certificate> m_certs;
416
   };
417
418
/**
419
* Certificate Status (RFC 6066)
420
*/
421
class BOTAN_UNSTABLE_API Certificate_Status final : public Handshake_Message
422
   {
423
   public:
424
0
      Handshake_Type type() const override { return CERTIFICATE_STATUS; }
425
426
      //std::shared_ptr<const OCSP::Response> response() const { return m_response; }
427
428
0
      const std::vector<uint8_t>& response() const { return m_response; }
429
430
      explicit Certificate_Status(const std::vector<uint8_t>& buf);
431
432
      Certificate_Status(Handshake_IO& io,
433
                         Handshake_Hash& hash,
434
                         const OCSP::Response& response);
435
436
      /*
437
       * Create a Certificate_Status message using an already DER encoded OCSP response.
438
       */
439
      Certificate_Status(Handshake_IO& io,
440
                         Handshake_Hash& hash,
441
                         const std::vector<uint8_t>& raw_response_bytes);
442
443
   private:
444
      std::vector<uint8_t> serialize() const override;
445
      std::vector<uint8_t> m_response;
446
   };
447
448
/**
449
* Certificate Request Message
450
* TODO: this is 1.2 only
451
*/
452
class BOTAN_UNSTABLE_API Certificate_Req final : public Handshake_Message
453
   {
454
   public:
455
      Handshake_Type type() const override;
456
457
      const std::vector<std::string>& acceptable_cert_types() const;
458
459
      const std::vector<X509_DN>& acceptable_CAs() const;
460
461
      const std::vector<Signature_Scheme>& signature_schemes() const;
462
463
      Certificate_Req(Handshake_IO& io,
464
                      Handshake_Hash& hash,
465
                      const Policy& policy,
466
                      const std::vector<X509_DN>& allowed_cas);
467
468
      explicit Certificate_Req(const std::vector<uint8_t>& buf);
469
470
      std::vector<uint8_t> serialize() const override;
471
472
   private:
473
      std::vector<X509_DN> m_names;
474
      std::vector<std::string> m_cert_key_types;
475
      std::vector<Signature_Scheme> m_schemes;
476
   };
477
478
class BOTAN_UNSTABLE_API Certificate_Verify : public Handshake_Message
479
   {
480
   public:
481
0
      Handshake_Type type() const override { return CERTIFICATE_VERIFY; }
482
483
      Certificate_Verify(Handshake_IO& io,
484
                         Handshake_State& state,
485
                         const Policy& policy,
486
                         RandomNumberGenerator& rng,
487
                         const Private_Key* key);
488
489
      Certificate_Verify(const std::vector<uint8_t>& buf);
490
491
      std::vector<uint8_t> serialize() const override;
492
493
   protected:
494
      std::vector<uint8_t> m_signature;
495
      Signature_Scheme m_scheme = Signature_Scheme::NONE;
496
   };
497
498
/**
499
* Certificate Verify Message
500
*/
501
class BOTAN_UNSTABLE_API Certificate_Verify_12 final : public Certificate_Verify
502
   {
503
   public:
504
      using Certificate_Verify::Certificate_Verify;
505
506
      /**
507
      * Check the signature on a certificate verify message
508
      * @param cert the purported certificate
509
      * @param state the handshake state
510
      * @param policy the TLS policy
511
      */
512
      bool verify(const X509_Certificate& cert,
513
                  const Handshake_State& state,
514
                  const Policy& policy) const;
515
   };
516
517
/**
518
* Finished Message
519
*/
520
class BOTAN_UNSTABLE_API Finished : public Handshake_Message
521
   {
522
   public:
523
      explicit Finished(const std::vector<uint8_t>& buf);
524
525
690
      Handshake_Type type() const override { return FINISHED; }
526
527
      std::vector<uint8_t> verify_data() const;
528
529
      std::vector<uint8_t> serialize() const override;
530
531
   protected:
532
      using Handshake_Message::Handshake_Message;
533
      std::vector<uint8_t> m_verification_data;
534
   };
535
536
class BOTAN_UNSTABLE_API Finished_12 final : public Finished
537
   {
538
   public:
539
      using Finished::Finished;
540
      Finished_12(Handshake_IO& io,
541
                  Handshake_State& state,
542
                  Connection_Side side);
543
544
      bool verify(const Handshake_State& state, Connection_Side side) const;
545
   };
546
547
/**
548
* Hello Request Message
549
*/
550
class BOTAN_UNSTABLE_API Hello_Request final : public Handshake_Message
551
   {
552
   public:
553
0
      Handshake_Type type() const override { return HELLO_REQUEST; }
554
555
      explicit Hello_Request(Handshake_IO& io);
556
      explicit Hello_Request(const std::vector<uint8_t>& buf);
557
558
   private:
559
      std::vector<uint8_t> serialize() const override;
560
   };
561
562
/**
563
* Server Key Exchange Message
564
*/
565
class BOTAN_UNSTABLE_API Server_Key_Exchange final : public Handshake_Message
566
   {
567
   public:
568
41.3k
      Handshake_Type type() const override { return SERVER_KEX; }
569
570
20.6k
      const std::vector<uint8_t>& params() const { return m_params; }
571
572
      bool verify(const Public_Key& server_key,
573
                  const Handshake_State& state,
574
                  const Policy& policy) const;
575
576
      // Only valid for certain kex types
577
      const Private_Key& server_kex_key() const;
578
579
#if defined(BOTAN_HAS_CECPQ1)
580
      // Only valid for CECPQ1 negotiation
581
      const CECPQ1_key& cecpq1_key() const
582
0
         {
583
0
         BOTAN_ASSERT_NONNULL(m_cecpq1_key);
584
0
         return *m_cecpq1_key;
585
0
         }
Unexecuted instantiation: Botan::TLS::Server_Key_Exchange::cecpq1_key() const
Unexecuted instantiation: Botan::TLS::Server_Key_Exchange::cecpq1_key() const
586
#endif
587
588
      Server_Key_Exchange(Handshake_IO& io,
589
                          Handshake_State& state,
590
                          const Policy& policy,
591
                          Credentials_Manager& creds,
592
                          RandomNumberGenerator& rng,
593
                          const Private_Key* signing_key = nullptr);
594
595
      Server_Key_Exchange(const std::vector<uint8_t>& buf,
596
                          Kex_Algo kex_alg,
597
                          Auth_Method sig_alg,
598
                          Protocol_Version version);
599
600
   private:
601
      std::vector<uint8_t> serialize() const override;
602
603
#if defined(BOTAN_HAS_CECPQ1)
604
      std::unique_ptr<CECPQ1_key> m_cecpq1_key;
605
#endif
606
607
      std::unique_ptr<Private_Key> m_kex_key;
608
609
      std::vector<uint8_t> m_params;
610
611
      std::vector<uint8_t> m_signature;
612
      Signature_Scheme m_scheme = Signature_Scheme::NONE;
613
   };
614
615
/**
616
* Server Hello Done Message
617
*/
618
class BOTAN_UNSTABLE_API Server_Hello_Done final : public Handshake_Message
619
   {
620
   public:
621
41.3k
      Handshake_Type type() const override { return SERVER_HELLO_DONE; }
622
623
      explicit Server_Hello_Done(Handshake_IO& io, Handshake_Hash& hash);
624
      explicit Server_Hello_Done(const std::vector<uint8_t>& buf);
625
626
   private:
627
      std::vector<uint8_t> serialize() const override;
628
   };
629
630
/**
631
* New Session Ticket Message
632
*/
633
class BOTAN_UNSTABLE_API New_Session_Ticket_12 final : public Handshake_Message
634
   {
635
   public:
636
406
      Handshake_Type type() const override { return NEW_SESSION_TICKET; }
637
638
0
      uint32_t ticket_lifetime_hint() const { return m_ticket_lifetime_hint; }
639
0
      const std::vector<uint8_t>& ticket() const { return m_ticket; }
640
641
      New_Session_Ticket_12(Handshake_IO& io,
642
                            Handshake_Hash& hash,
643
                            const std::vector<uint8_t>& ticket,
644
                            uint32_t lifetime);
645
646
      New_Session_Ticket_12(Handshake_IO& io,
647
                            Handshake_Hash& hash);
648
649
      explicit New_Session_Ticket_12(const std::vector<uint8_t>& buf);
650
651
      std::vector<uint8_t> serialize() const override;
652
653
   private:
654
      uint32_t m_ticket_lifetime_hint = 0;
655
      std::vector<uint8_t> m_ticket;
656
   };
657
658
/**
659
* Change Cipher Spec
660
*/
661
class BOTAN_UNSTABLE_API Change_Cipher_Spec final : public Handshake_Message
662
   {
663
   public:
664
345
      Handshake_Type type() const override { return HANDSHAKE_CCS; }
665
666
      std::vector<uint8_t> serialize() const override
667
345
         { return std::vector<uint8_t>(1, 1); }
668
   };
669
670
}
671
672
}
673
674
#endif