Coverage Report

Created: 2021-11-25 09:31

/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 <botan/tls_extensions.h>
13
#include <botan/tls_handshake_msg.h>
14
#include <botan/tls_session.h>
15
#include <botan/tls_policy.h>
16
#include <botan/tls_ciphersuite.h>
17
#include <botan/pk_keys.h>
18
#include <botan/x509cert.h>
19
#include <botan/ocsp.h>
20
#include <vector>
21
#include <string>
22
#include <set>
23
24
#if defined(BOTAN_HAS_CECPQ1)
25
  #include <botan/cecpq1.h>
26
#endif
27
28
namespace Botan {
29
30
class Public_Key;
31
class Credentials_Manager;
32
33
namespace TLS {
34
35
class Session;
36
class Handshake_IO;
37
class Handshake_State;
38
class Callbacks;
39
40
std::vector<uint8_t> make_hello_random(RandomNumberGenerator& rng,
41
                                       const Policy& policy);
42
43
/**
44
* DTLS Hello Verify Request
45
*/
46
class BOTAN_UNSTABLE_API Hello_Verify_Request final : public Handshake_Message
47
   {
48
   public:
49
      std::vector<uint8_t> serialize() const override;
50
8.06k
      Handshake_Type type() const override { return HELLO_VERIFY_REQUEST; }
51
52
4.03k
      const std::vector<uint8_t>& cookie() const { return m_cookie; }
53
54
      explicit Hello_Verify_Request(const std::vector<uint8_t>& buf);
55
56
      Hello_Verify_Request(const std::vector<uint8_t>& client_hello_bits,
57
                           const std::string& client_identity,
58
                           const SymmetricKey& secret_key);
59
   private:
60
      std::vector<uint8_t> m_cookie;
61
   };
62
63
/**
64
* Client Hello Message
65
*/
66
class BOTAN_UNSTABLE_API Client_Hello final : public Handshake_Message
67
   {
68
   public:
69
      class Settings final
70
         {
71
         public:
72
            Settings(const Protocol_Version version,
73
                     const std::string& hostname = "") :
74
               m_new_session_version(version),
75
3.01k
               m_hostname(hostname) {}
76
77
3.01k
            const Protocol_Version protocol_version() const { return m_new_session_version; }
78
6.03k
            const std::string& hostname() const { return m_hostname; }
79
80
         private:
81
            const Protocol_Version m_new_session_version;
82
            const std::string m_hostname;
83
         };
84
85
6.03k
      Handshake_Type type() const override { return CLIENT_HELLO; }
86
87
24.4k
      Protocol_Version version() const { return m_version; }
88
89
      std::vector<Protocol_Version> supported_versions() const;
90
91
22.4k
      const std::vector<uint8_t>& random() const { return m_random; }
92
93
20.3k
      const std::vector<uint8_t>& session_id() const { return m_session_id; }
94
95
24.4k
      const std::vector<uint8_t>& compression_methods() const { return m_comp_methods; }
96
97
20.3k
      const std::vector<uint16_t>& ciphersuites() const { return m_suites; }
98
99
      bool offered_suite(uint16_t ciphersuite) const;
100
101
      bool sent_fallback_scsv() const;
102
103
      std::vector<Signature_Scheme> signature_schemes() const;
104
105
      std::vector<Group_Params> supported_ecc_curves() const;
106
107
      std::vector<Group_Params> supported_dh_groups() const;
108
109
      bool prefers_compressed_ec_points() const;
110
111
      std::string sni_hostname() const;
112
113
      bool secure_renegotiation() const;
114
115
      std::vector<uint8_t> renegotiation_info() const;
116
117
      bool supports_session_ticket() const;
118
119
      std::vector<uint8_t> session_ticket() const;
120
121
      bool supports_alpn() const;
122
123
      bool supports_extended_master_secret() const;
124
125
      bool supports_cert_status_message() const;
126
127
      bool supports_encrypt_then_mac() const;
128
129
      bool sent_signature_algorithms() const;
130
131
      std::vector<std::string> next_protocols() const;
132
133
      std::vector<uint16_t> srtp_profiles() const;
134
135
      void update_hello_cookie(const Hello_Verify_Request& hello_verify);
136
137
4.03k
      const std::vector<uint8_t>& cookie() const { return m_hello_cookie; }
138
139
      std::vector<uint8_t> cookie_input_data() const;
140
141
      std::set<Handshake_Extension_Type> extension_types() const
142
19.2k
         { return m_extensions.extension_types(); }
143
144
20.3k
      const Extensions& extensions() const { return m_extensions; }
145
146
      Client_Hello(Handshake_IO& io,
147
                   Handshake_Hash& hash,
148
                   const Policy& policy,
149
                   Callbacks& cb,
150
                   RandomNumberGenerator& rng,
151
                   const std::vector<uint8_t>& reneg_info,
152
                   const Client_Hello::Settings& client_settings,
153
                   const std::vector<std::string>& next_protocols);
154
155
      Client_Hello(Handshake_IO& io,
156
                   Handshake_Hash& hash,
157
                   const Policy& policy,
158
                   Callbacks& cb,
159
                   RandomNumberGenerator& rng,
160
                   const std::vector<uint8_t>& reneg_info,
161
                   const Session& resumed_session,
162
                   const std::vector<std::string>& next_protocols);
163
164
      explicit Client_Hello(const std::vector<uint8_t>& buf);
165
166
   private:
167
      std::vector<uint8_t> serialize() const override;
168
169
      Protocol_Version m_version;
170
      std::vector<uint8_t> m_session_id;
171
      std::vector<uint8_t> m_random;
172
      std::vector<uint16_t> m_suites;
173
      std::vector<uint8_t> m_comp_methods;
174
      std::vector<uint8_t> m_hello_cookie; // DTLS only
175
      std::vector<uint8_t> m_cookie_input_bits; // DTLS only
176
177
      Extensions m_extensions;
178
   };
179
180
/**
181
* Server Hello Message
182
*/
183
class BOTAN_UNSTABLE_API Server_Hello final : public Handshake_Message
184
   {
185
   public:
186
      class Settings final
187
         {
188
         public:
189
            Settings(const std::vector<uint8_t> new_session_id,
190
                     Protocol_Version new_session_version,
191
                     uint16_t ciphersuite,
192
                     bool offer_session_ticket) :
193
               m_new_session_id(new_session_id),
194
               m_new_session_version(new_session_version),
195
               m_ciphersuite(ciphersuite),
196
20.1k
               m_offer_session_ticket(offer_session_ticket) {}
197
198
20.1k
            const std::vector<uint8_t>& session_id() const { return m_new_session_id; }
199
20.1k
            Protocol_Version protocol_version() const { return m_new_session_version; }
200
20.1k
            uint16_t ciphersuite() const { return m_ciphersuite; }
201
3.14k
            bool offer_session_ticket() const { return m_offer_session_ticket; }
202
203
         private:
204
            const std::vector<uint8_t> m_new_session_id;
205
            Protocol_Version m_new_session_version;
206
            uint16_t m_ciphersuite;
207
            bool m_offer_session_ticket;
208
         };
209
210
211
40.3k
      Handshake_Type type() const override { return SERVER_HELLO; }
212
213
428
      Protocol_Version version() const { return m_version; }
214
215
22.4k
      const std::vector<uint8_t>& random() const { return m_random; }
216
217
639
      const std::vector<uint8_t>& session_id() const { return m_session_id; }
218
219
21.1k
      uint16_t ciphersuite() const { return m_ciphersuite; }
220
221
1.13k
      uint8_t compression_method() const { return m_comp_method; }
222
223
      bool secure_renegotiation() const
224
20.1k
         {
225
20.1k
         return m_extensions.has<Renegotiation_Extension>();
226
20.1k
         }
227
228
      std::vector<uint8_t> renegotiation_info() const
229
6.78k
         {
230
6.78k
         if(Renegotiation_Extension* reneg = m_extensions.get<Renegotiation_Extension>())
231
6.78k
            return reneg->renegotiation_info();
232
0
         return std::vector<uint8_t>();
233
6.78k
         }
234
235
      bool supports_extended_master_secret() const
236
13.2k
         {
237
13.2k
         return m_extensions.has<Extended_Master_Secret>();
238
13.2k
         }
239
240
      bool supports_encrypt_then_mac() const
241
1.56k
         {
242
1.56k
         return m_extensions.has<Encrypt_then_MAC>();
243
1.56k
         }
244
245
      bool supports_certificate_status_message() const
246
0
         {
247
0
         return m_extensions.has<Certificate_Status_Request>();
248
0
         }
249
250
      bool supports_session_ticket() const
251
541
         {
252
541
         return m_extensions.has<Session_Ticket>();
253
541
         }
254
255
      uint16_t srtp_profile() const
256
428
         {
257
428
         if(auto srtp = m_extensions.get<SRTP_Protection_Profiles>())
258
0
            {
259
0
            auto prof = srtp->profiles();
260
0
            if(prof.size() != 1 || prof[0] == 0)
261
0
               throw Decoding_Error("Server sent malformed DTLS-SRTP extension");
262
0
            return prof[0];
263
0
            }
264
265
428
         return 0;
266
428
         }
267
268
      std::string next_protocol() const
269
0
         {
270
0
         if(auto alpn = m_extensions.get<Application_Layer_Protocol_Notification>())
271
0
            return alpn->single_protocol();
272
0
         return "";
273
0
         }
274
275
      std::set<Handshake_Extension_Type> extension_types() const
276
0
         { return m_extensions.extension_types(); }
277
278
0
      const Extensions& extensions() const { return m_extensions; }
279
280
      bool prefers_compressed_ec_points() const
281
0
         {
282
0
         if(auto ecc_formats = m_extensions.get<Supported_Point_Formats>())
283
0
            {
284
0
            return ecc_formats->prefers_compressed();
285
0
            }
286
0
         return false;
287
0
         }
288
289
      bool random_signals_downgrade() const;
290
291
      Server_Hello(Handshake_IO& io,
292
                   Handshake_Hash& hash,
293
                   const Policy& policy,
294
                   Callbacks& cb,
295
                   RandomNumberGenerator& rng,
296
                   const std::vector<uint8_t>& secure_reneg_info,
297
                   const Client_Hello& client_hello,
298
                   const Server_Hello::Settings& settings,
299
                   const std::string next_protocol);
300
301
      Server_Hello(Handshake_IO& io,
302
                   Handshake_Hash& hash,
303
                   const Policy& policy,
304
                   Callbacks& cb,
305
                   RandomNumberGenerator& rng,
306
                   const std::vector<uint8_t>& secure_reneg_info,
307
                   const Client_Hello& client_hello,
308
                   Session& resumed_session,
309
                   bool offer_session_ticket,
310
                   const std::string& next_protocol);
311
312
      explicit Server_Hello(const std::vector<uint8_t>& buf);
313
   private:
314
      std::vector<uint8_t> serialize() const override;
315
316
      Protocol_Version m_version;
317
      std::vector<uint8_t> m_session_id, m_random;
318
      uint16_t m_ciphersuite;
319
      uint8_t m_comp_method;
320
321
      Extensions m_extensions;
322
   };
323
324
/**
325
* Client Key Exchange Message
326
*/
327
class BOTAN_UNSTABLE_API Client_Key_Exchange final : public Handshake_Message
328
   {
329
   public:
330
0
      Handshake_Type type() const override { return CLIENT_KEX; }
331
332
      const secure_vector<uint8_t>& pre_master_secret() const
333
12.8k
         { return m_pre_master; }
334
335
      Client_Key_Exchange(Handshake_IO& io,
336
                          Handshake_State& state,
337
                          const Policy& policy,
338
                          Credentials_Manager& creds,
339
                          const Public_Key* server_public_key,
340
                          const std::string& hostname,
341
                          RandomNumberGenerator& rng);
342
343
      Client_Key_Exchange(const std::vector<uint8_t>& buf,
344
                          const Handshake_State& state,
345
                          const Private_Key* server_rsa_kex_key,
346
                          Credentials_Manager& creds,
347
                          const Policy& policy,
348
                          RandomNumberGenerator& rng);
349
350
   private:
351
      std::vector<uint8_t> serialize() const override
352
0
         { return m_key_material; }
353
354
      std::vector<uint8_t> m_key_material;
355
      secure_vector<uint8_t> m_pre_master;
356
   };
357
358
/**
359
* Certificate Message
360
*/
361
class BOTAN_UNSTABLE_API Certificate final : public Handshake_Message
362
   {
363
   public:
364
316
      Handshake_Type type() const override { return CERTIFICATE; }
365
158
      const std::vector<X509_Certificate>& cert_chain() const { return m_certs; }
366
367
0
      size_t count() const { return m_certs.size(); }
368
0
      bool empty() const { return m_certs.empty(); }
369
370
      Certificate(Handshake_IO& io,
371
                  Handshake_Hash& hash,
372
                  const std::vector<X509_Certificate>& certs);
373
374
      explicit Certificate(const std::vector<uint8_t>& buf, const Policy &policy);
375
   private:
376
      std::vector<uint8_t> serialize() const override;
377
378
      std::vector<X509_Certificate> m_certs;
379
   };
380
381
/**
382
* Certificate Status (RFC 6066)
383
*/
384
class BOTAN_UNSTABLE_API Certificate_Status final : public Handshake_Message
385
   {
386
   public:
387
0
      Handshake_Type type() const override { return CERTIFICATE_STATUS; }
388
389
      //std::shared_ptr<const OCSP::Response> response() const { return m_response; }
390
391
0
      const std::vector<uint8_t>& response() const { return m_response; }
392
393
      Certificate_Status(const std::vector<uint8_t>& buf);
394
395
      Certificate_Status(Handshake_IO& io,
396
                         Handshake_Hash& hash,
397
                         std::shared_ptr<const OCSP::Response> response);
398
399
      /*
400
       * Create a Certificate_Status message using an already DER encoded OCSP response.
401
       */
402
      Certificate_Status(Handshake_IO& io,
403
                         Handshake_Hash& hash,
404
                         std::vector<uint8_t> const& raw_response_bytes );
405
406
   private:
407
      std::vector<uint8_t> serialize() const override;
408
      std::vector<uint8_t> m_response;
409
   };
410
411
/**
412
* Certificate Request Message
413
*/
414
class BOTAN_UNSTABLE_API Certificate_Req final : public Handshake_Message
415
   {
416
   public:
417
0
      Handshake_Type type() const override { return CERTIFICATE_REQUEST; }
418
419
      const std::vector<std::string>& acceptable_cert_types() const
420
0
         { return m_cert_key_types; }
421
422
0
      const std::vector<X509_DN>& acceptable_CAs() const { return m_names; }
423
424
      const std::vector<Signature_Scheme>& signature_schemes() const
425
0
         {
426
0
         return m_schemes;
427
0
         }
428
429
      Certificate_Req(Handshake_IO& io,
430
                      Handshake_Hash& hash,
431
                      const Policy& policy,
432
                      const std::vector<X509_DN>& allowed_cas);
433
434
      Certificate_Req(const std::vector<uint8_t>& buf);
435
   private:
436
      std::vector<uint8_t> serialize() const override;
437
438
      std::vector<X509_DN> m_names;
439
      std::vector<std::string> m_cert_key_types;
440
441
      std::vector<Signature_Scheme> m_schemes;
442
   };
443
444
/**
445
* Certificate Verify Message
446
*/
447
class BOTAN_UNSTABLE_API Certificate_Verify final : public Handshake_Message
448
   {
449
   public:
450
0
      Handshake_Type type() const override { return CERTIFICATE_VERIFY; }
451
452
      /**
453
      * Check the signature on a certificate verify message
454
      * @param cert the purported certificate
455
      * @param state the handshake state
456
      * @param policy the TLS policy
457
      */
458
      bool verify(const X509_Certificate& cert,
459
                  const Handshake_State& state,
460
                  const Policy& policy) const;
461
462
      Certificate_Verify(Handshake_IO& io,
463
                         Handshake_State& state,
464
                         const Policy& policy,
465
                         RandomNumberGenerator& rng,
466
                         const Private_Key* key);
467
468
      Certificate_Verify(const std::vector<uint8_t>& buf);
469
   private:
470
      std::vector<uint8_t> serialize() const override;
471
472
      std::vector<uint8_t> m_signature;
473
      Signature_Scheme m_scheme = Signature_Scheme::NONE;
474
   };
475
476
/**
477
* Finished Message
478
*/
479
class BOTAN_UNSTABLE_API Finished final : public Handshake_Message
480
   {
481
   public:
482
856
      Handshake_Type type() const override { return FINISHED; }
483
484
      std::vector<uint8_t> verify_data() const
485
0
         { return m_verification_data; }
486
487
      bool verify(const Handshake_State& state,
488
                  Connection_Side side) const;
489
490
      Finished(Handshake_IO& io,
491
               Handshake_State& state,
492
               Connection_Side side);
493
494
      explicit Finished(const std::vector<uint8_t>& buf);
495
   private:
496
      std::vector<uint8_t> serialize() const override;
497
498
      std::vector<uint8_t> m_verification_data;
499
   };
500
501
/**
502
* Hello Request Message
503
*/
504
class BOTAN_UNSTABLE_API Hello_Request final : public Handshake_Message
505
   {
506
   public:
507
0
      Handshake_Type type() const override { return HELLO_REQUEST; }
508
509
      explicit Hello_Request(Handshake_IO& io);
510
      explicit Hello_Request(const std::vector<uint8_t>& buf);
511
   private:
512
      std::vector<uint8_t> serialize() const override;
513
   };
514
515
/**
516
* Server Key Exchange Message
517
*/
518
class BOTAN_UNSTABLE_API Server_Key_Exchange final : public Handshake_Message
519
   {
520
   public:
521
40.0k
      Handshake_Type type() const override { return SERVER_KEX; }
522
523
20.0k
      const std::vector<uint8_t>& params() const { return m_params; }
524
525
      bool verify(const Public_Key& server_key,
526
                  const Handshake_State& state,
527
                  const Policy& policy) const;
528
529
      // Only valid for certain kex types
530
      const Private_Key& server_kex_key() const;
531
532
#if defined(BOTAN_HAS_CECPQ1)
533
      // Only valid for CECPQ1 negotiation
534
      const CECPQ1_key& cecpq1_key() const
535
0
         {
536
0
         BOTAN_ASSERT_NONNULL(m_cecpq1_key);
537
0
         return *m_cecpq1_key;
538
0
         }
Unexecuted instantiation: Botan::TLS::Server_Key_Exchange::cecpq1_key() const
Unexecuted instantiation: Botan::TLS::Server_Key_Exchange::cecpq1_key() const
539
#endif
540
541
      Server_Key_Exchange(Handshake_IO& io,
542
                          Handshake_State& state,
543
                          const Policy& policy,
544
                          Credentials_Manager& creds,
545
                          RandomNumberGenerator& rng,
546
                          const Private_Key* signing_key = nullptr);
547
548
      Server_Key_Exchange(const std::vector<uint8_t>& buf,
549
                          Kex_Algo kex_alg,
550
                          Auth_Method sig_alg,
551
                          Protocol_Version version);
552
553
20.0k
      ~Server_Key_Exchange() = default;
554
   private:
555
      std::vector<uint8_t> serialize() const override;
556
557
#if defined(BOTAN_HAS_CECPQ1)
558
      std::unique_ptr<CECPQ1_key> m_cecpq1_key;
559
#endif
560
561
      std::unique_ptr<Private_Key> m_kex_key;
562
563
      std::vector<uint8_t> m_params;
564
565
      std::vector<uint8_t> m_signature;
566
      Signature_Scheme m_scheme = Signature_Scheme::NONE;
567
   };
568
569
/**
570
* Server Hello Done Message
571
*/
572
class BOTAN_UNSTABLE_API Server_Hello_Done final : public Handshake_Message
573
   {
574
   public:
575
40.0k
      Handshake_Type type() const override { return SERVER_HELLO_DONE; }
576
577
      Server_Hello_Done(Handshake_IO& io, Handshake_Hash& hash);
578
      explicit Server_Hello_Done(const std::vector<uint8_t>& buf);
579
   private:
580
      std::vector<uint8_t> serialize() const override;
581
   };
582
583
/**
584
* New Session Ticket Message
585
*/
586
class BOTAN_UNSTABLE_API New_Session_Ticket final : public Handshake_Message
587
   {
588
   public:
589
630
      Handshake_Type type() const override { return NEW_SESSION_TICKET; }
590
591
0
      uint32_t ticket_lifetime_hint() const { return m_ticket_lifetime_hint; }
592
0
      const std::vector<uint8_t>& ticket() const { return m_ticket; }
593
594
      New_Session_Ticket(Handshake_IO& io,
595
                         Handshake_Hash& hash,
596
                         const std::vector<uint8_t>& ticket,
597
                         uint32_t lifetime);
598
599
      New_Session_Ticket(Handshake_IO& io,
600
                         Handshake_Hash& hash);
601
602
      explicit New_Session_Ticket(const std::vector<uint8_t>& buf);
603
   private:
604
      std::vector<uint8_t> serialize() const override;
605
606
      uint32_t m_ticket_lifetime_hint = 0;
607
      std::vector<uint8_t> m_ticket;
608
   };
609
610
/**
611
* Change Cipher Spec
612
*/
613
class BOTAN_UNSTABLE_API Change_Cipher_Spec final : public Handshake_Message
614
   {
615
   public:
616
428
      Handshake_Type type() const override { return HANDSHAKE_CCS; }
617
618
      std::vector<uint8_t> serialize() const override
619
428
         { return std::vector<uint8_t>(1, 1); }
620
   };
621
622
}
623
624
}
625
626
#endif