aboutsummaryrefslogtreecommitdiffstats
path: root/src/tls/tls_messages.h
blob: ff6ebda4d9441bbf6d2fa871eecae5d3dde4c25b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
* TLS Messages
* (C) 2004-2011 Jack Lloyd
*
* Released under the terms of the Botan license
*/

#ifndef BOTAN_TLS_MESSAGES_H__
#define BOTAN_TLS_MESSAGES_H__

#include <botan/internal/tls_handshake_state.h>
#include <botan/tls_session.h>
#include <botan/tls_policy.h>
#include <botan/tls_magic.h>
#include <botan/tls_ciphersuite.h>
#include <botan/bigint.h>
#include <botan/pkcs8.h>
#include <botan/x509cert.h>
#include <vector>

namespace Botan {

class Credentials_Manager;

namespace TLS {

class Record_Writer;
class Record_Reader;

/**
* TLS Handshake Message Base Class
*/
class Handshake_Message
   {
   public:
      virtual MemoryVector<byte> serialize() const = 0;
      virtual Handshake_Type type() const = 0;

      Handshake_Message() {}
      virtual ~Handshake_Message() {}
   private:
      Handshake_Message(const Handshake_Message&) {}
      Handshake_Message& operator=(const Handshake_Message&) { return (*this); }
   };

MemoryVector<byte> make_hello_random(RandomNumberGenerator& rng);

/**
* DTLS Hello Verify Request
*/
class Hello_Verify_Request : public Handshake_Message
   {
   public:
      MemoryVector<byte> serialize() const;
      Handshake_Type type() const { return HELLO_VERIFY_REQUEST; }

      MemoryVector<byte> cookie() const { return m_cookie; }

      Hello_Verify_Request(const MemoryRegion<byte>& buf);

      Hello_Verify_Request(const MemoryVector<byte>& client_hello_bits,
                           const std::string& client_identity,
                           const SymmetricKey& secret_key);
   private:
      MemoryVector<byte> m_cookie;
   };

/**
* Client Hello Message
*/
class Client_Hello : public Handshake_Message
   {
   public:
      Handshake_Type type() const { return CLIENT_HELLO; }
      Protocol_Version version() const { return m_version; }
      const MemoryVector<byte>& session_id() const { return m_session_id; }

      std::vector<byte> session_id_vector() const
         {
         std::vector<byte> v;
         v.insert(v.begin(), &m_session_id[0], &m_session_id[m_session_id.size()]);
         return v;
         }

      const std::vector<std::pair<std::string, std::string> >& supported_algos() const
         { return m_supported_algos; }

      const std::vector<std::string>& supported_ecc_curves() const
         { return m_supported_curves; }

      std::vector<u16bit> ciphersuites() const { return m_suites; }
      std::vector<byte> compression_methods() const { return m_comp_methods; }

      const MemoryVector<byte>& random() const { return m_random; }

      std::string sni_hostname() const { return m_hostname; }

      std::string srp_identifier() const { return m_srp_identifier; }

      bool secure_renegotiation() const { return m_secure_renegotiation; }

      const MemoryVector<byte>& renegotiation_info()
         { return m_renegotiation_info; }

      bool offered_suite(u16bit ciphersuite) const;

      bool next_protocol_notification() const { return m_next_protocol; }

      size_t fragment_size() const { return m_fragment_size; }

      bool supports_session_ticket() const { return m_supports_session_ticket; }

      const MemoryRegion<byte>& session_ticket() const
         { return m_session_ticket; }

      Client_Hello(Record_Writer& writer,
                   Handshake_Hash& hash,
                   const Policy& policy,
                   RandomNumberGenerator& rng,
                   const MemoryRegion<byte>& reneg_info,
                   bool next_protocol = false,
                   const std::string& hostname = "",
                   const std::string& srp_identifier = "");

      Client_Hello(Record_Writer& writer,
                   Handshake_Hash& hash,
                   const Policy& policy,
                   RandomNumberGenerator& rng,
                   const Session& resumed_session,
                   bool next_protocol = false);

      Client_Hello(const MemoryRegion<byte>& buf,
                   Handshake_Type type);

   private:
      MemoryVector<byte> serialize() const;
      void deserialize(const MemoryRegion<byte>& buf);
      void deserialize_sslv2(const MemoryRegion<byte>& buf);

      Protocol_Version m_version;
      MemoryVector<byte> m_session_id, m_random;
      std::vector<u16bit> m_suites;
      std::vector<byte> m_comp_methods;
      std::string m_hostname;
      std::string m_srp_identifier;
      bool m_next_protocol;

      size_t m_fragment_size;
      bool m_secure_renegotiation;
      MemoryVector<byte> m_renegotiation_info;

      std::vector<std::pair<std::string, std::string> > m_supported_algos;
      std::vector<std::string> m_supported_curves;

      bool m_supports_session_ticket;
      MemoryVector<byte> m_session_ticket;
   };

/**
* Server Hello Message
*/
class Server_Hello : public Handshake_Message
   {
   public:
      Handshake_Type type() const { return SERVER_HELLO; }
      Protocol_Version version() { return m_version; }
      const MemoryVector<byte>& session_id() const { return m_session_id; }
      u16bit ciphersuite() const { return m_ciphersuite; }
      byte compression_method() const { return m_comp_method; }

      std::vector<byte> session_id_vector() const
         {
         std::vector<byte> v;
         v.insert(v.begin(), &m_session_id[0], &m_session_id[m_session_id.size()]);
         return v;
         }

      bool secure_renegotiation() const { return m_secure_renegotiation; }

      bool next_protocol_notification() const { return m_next_protocol; }

      bool supports_session_ticket() const { return m_supports_session_ticket; }

      const std::vector<std::string>& next_protocols() const
         { return m_next_protocols; }

      size_t fragment_size() const { return m_fragment_size; }

      const MemoryVector<byte>& renegotiation_info()
         { return m_renegotiation_info; }

      const MemoryVector<byte>& random() const { return m_random; }

      Server_Hello(Record_Writer& writer,
                   Handshake_Hash& hash,
                   const MemoryRegion<byte>& session_id,
                   Protocol_Version ver,
                   u16bit ciphersuite,
                   byte compression,
                   size_t max_fragment_size,
                   bool client_has_secure_renegotiation,
                   const MemoryRegion<byte>& reneg_info,
                   bool offer_session_ticket,
                   bool client_has_npn,
                   const std::vector<std::string>& next_protocols,
                   RandomNumberGenerator& rng);

      Server_Hello(const MemoryRegion<byte>& buf);
   private:
      MemoryVector<byte> serialize() const;

      Protocol_Version m_version;
      MemoryVector<byte> m_session_id, m_random;
      u16bit m_ciphersuite;
      byte m_comp_method;

      size_t m_fragment_size;
      bool m_secure_renegotiation;
      MemoryVector<byte> m_renegotiation_info;

      bool m_next_protocol;
      std::vector<std::string> m_next_protocols;
      bool m_supports_session_ticket;
   };

/**
* Client Key Exchange Message
*/
class Client_Key_Exchange : public Handshake_Message
   {
   public:
      Handshake_Type type() const { return CLIENT_KEX; }

      const SecureVector<byte>& pre_master_secret() const
         { return pre_master; }

      Client_Key_Exchange(Record_Writer& output,
                          Handshake_State* state,
                          Credentials_Manager& creds,
                          const std::vector<X509_Certificate>& peer_certs,
                          const std::string& hostname,
                          RandomNumberGenerator& rng);

      Client_Key_Exchange(const MemoryRegion<byte>& buf,
                          const Handshake_State* state,
                          Credentials_Manager& creds,
                          const Policy& policy,
                          RandomNumberGenerator& rng);

   private:
      MemoryVector<byte> serialize() const { return key_material; }

      SecureVector<byte> key_material, pre_master;
   };

/**
* Certificate Message
*/
class Certificate : public Handshake_Message
   {
   public:
      Handshake_Type type() const { return CERTIFICATE; }
      const std::vector<X509_Certificate>& cert_chain() const { return m_certs; }

      size_t count() const { return m_certs.size(); }
      bool empty() const { return m_certs.empty(); }

      Certificate(Record_Writer& writer,
                  Handshake_Hash& hash,
                  const std::vector<X509_Certificate>& certs);

      Certificate(const MemoryRegion<byte>& buf);
   private:
      MemoryVector<byte> serialize() const;

      std::vector<X509_Certificate> m_certs;
   };

/**
* Certificate Request Message
*/
class Certificate_Req : public Handshake_Message
   {
   public:
      Handshake_Type type() const { return CERTIFICATE_REQUEST; }

      const std::vector<std::string>& acceptable_cert_types() const
         { return cert_key_types; }

      std::vector<X509_DN> acceptable_CAs() const { return names; }

      std::vector<std::pair<std::string, std::string> > supported_algos() const
         { return m_supported_algos; }

      Certificate_Req(Record_Writer& writer,
                      Handshake_Hash& hash,
                      const Policy& policy,
                      const std::vector<X509_Certificate>& allowed_cas,
                      Protocol_Version version);

      Certificate_Req(const MemoryRegion<byte>& buf,
                      Protocol_Version version);
   private:
      MemoryVector<byte> serialize() const;

      std::vector<X509_DN> names;
      std::vector<std::string> cert_key_types;

      std::vector<std::pair<std::string, std::string> > m_supported_algos;
   };

/**
* Certificate Verify Message
*/
class Certificate_Verify : public Handshake_Message
   {
   public:
      Handshake_Type type() const { return CERTIFICATE_VERIFY; }

      /**
      * Check the signature on a certificate verify message
      * @param cert the purported certificate
      * @param state the handshake state
      */
      bool verify(const X509_Certificate& cert,
                  Handshake_State* state);

      Certificate_Verify(Record_Writer& writer,
                         Handshake_State* state,
                         RandomNumberGenerator& rng,
                         const Private_Key* key);

      Certificate_Verify(const MemoryRegion<byte>& buf,
                         Protocol_Version version);
   private:
      MemoryVector<byte> serialize() const;

      std::string sig_algo; // sig algo used to create signature
      std::string hash_algo; // hash used to create signature
      MemoryVector<byte> signature;
   };

/**
* Finished Message
*/
class Finished : public Handshake_Message
   {
   public:
      Handshake_Type type() const { return FINISHED; }

      MemoryVector<byte> verify_data() const
         { return verification_data; }

      bool verify(Handshake_State* state,
                  Connection_Side side);

      Finished(Record_Writer& writer,
               Handshake_State* state,
               Connection_Side side);

      Finished(const MemoryRegion<byte>& buf);
   private:
      MemoryVector<byte> serialize() const;

      Connection_Side side;
      MemoryVector<byte> verification_data;
   };

/**
* Hello Request Message
*/
class Hello_Request : public Handshake_Message
   {
   public:
      Handshake_Type type() const { return HELLO_REQUEST; }

      Hello_Request(Record_Writer& writer);
      Hello_Request(const MemoryRegion<byte>& buf);
   private:
      MemoryVector<byte> serialize() const;
   };

/**
* Server Key Exchange Message
*/
class Server_Key_Exchange : public Handshake_Message
   {
   public:
      Handshake_Type type() const { return SERVER_KEX; }

      const MemoryVector<byte>& params() const { return m_params; }

      bool verify(const X509_Certificate& cert,
                  Handshake_State* state) const;

      // Only valid for certain kex types
      const Private_Key& server_kex_key() const;

      Server_Key_Exchange(Record_Writer& writer,
                          Handshake_State* state,
                          const Policy& policy,
                          Credentials_Manager& creds,
                          RandomNumberGenerator& rng,
                          const Private_Key* signing_key = 0);

      Server_Key_Exchange(const MemoryRegion<byte>& buf,
                          const std::string& kex_alg,
                          const std::string& sig_alg,
                          Protocol_Version version);

      ~Server_Key_Exchange() { delete m_kex_key; }
   private:
      MemoryVector<byte> serialize() const;

      Private_Key* m_kex_key;

      MemoryVector<byte> m_params;

      std::string m_sig_algo; // sig algo used to create signature
      std::string m_hash_algo; // hash used to create signature
      MemoryVector<byte> m_signature;
   };

/**
* Server Hello Done Message
*/
class Server_Hello_Done : public Handshake_Message
   {
   public:
      Handshake_Type type() const { return SERVER_HELLO_DONE; }

      Server_Hello_Done(Record_Writer& writer, Handshake_Hash& hash);
      Server_Hello_Done(const MemoryRegion<byte>& buf);
   private:
      MemoryVector<byte> serialize() const;
   };

/**
* Next Protocol Message
*/
class Next_Protocol : public Handshake_Message
   {
   public:
      Handshake_Type type() const { return NEXT_PROTOCOL; }

      std::string protocol() const { return m_protocol; }

      Next_Protocol(Record_Writer& writer,
                    Handshake_Hash& hash,
                    const std::string& protocol);

      Next_Protocol(const MemoryRegion<byte>& buf);
   private:
      MemoryVector<byte> serialize() const;

      std::string m_protocol;
   };

class New_Session_Ticket : public Handshake_Message
   {
   public:
      Handshake_Type type() const { return NEW_SESSION_TICKET; }

      u32bit ticket_lifetime_hint() const { return m_ticket_lifetime_hint; }
      const MemoryVector<byte>& ticket() const { return m_ticket; }

      New_Session_Ticket(Record_Writer& writer,
                         Handshake_Hash& hash,
                         const MemoryRegion<byte>& ticket,
                         u32bit lifetime);

      New_Session_Ticket(Record_Writer& writer,
                         Handshake_Hash& hash);

      New_Session_Ticket(const MemoryRegion<byte>& buf);
   private:
      MemoryVector<byte> serialize() const;

      u32bit m_ticket_lifetime_hint;
      MemoryVector<byte> m_ticket;
   };

}

}

#endif