Coverage Report

Created: 2021-02-21 07:20

/src/botan/src/lib/tls/tls_record.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* TLS Record Handling
3
* (C) 2012,2013,2014,2015,2016,2019 Jack Lloyd
4
*     2016 Juraj Somorovsky
5
*     2016 Matthias Gierlings
6
*
7
* Botan is released under the Simplified BSD License (see license.txt)
8
*/
9
10
#include <botan/internal/tls_record.h>
11
#include <botan/tls_ciphersuite.h>
12
#include <botan/tls_exceptn.h>
13
#include <botan/internal/loadstor.h>
14
#include <botan/internal/tls_seq_numbers.h>
15
#include <botan/internal/tls_session_key.h>
16
#include <botan/internal/ct_utils.h>
17
#include <botan/rng.h>
18
19
#if defined(BOTAN_HAS_TLS_CBC)
20
  #include <botan/internal/tls_cbc.h>
21
#endif
22
23
namespace Botan {
24
25
namespace TLS {
26
27
Connection_Cipher_State::Connection_Cipher_State(Protocol_Version version,
28
                                                 Connection_Side side,
29
                                                 bool our_side,
30
                                                 const Ciphersuite& suite,
31
                                                 const Session_Keys& keys,
32
                                                 bool uses_encrypt_then_mac) :
33
   m_start_time(std::chrono::system_clock::now())
34
2.69k
   {
35
2.69k
   m_nonce_format = suite.nonce_format();
36
2.69k
   m_nonce_bytes_from_record = suite.nonce_bytes_from_record(version);
37
2.69k
   m_nonce_bytes_from_handshake = suite.nonce_bytes_from_handshake();
38
39
2.69k
   const secure_vector<uint8_t>& aead_key = keys.aead_key(side);
40
2.69k
   m_nonce = keys.nonce(side);
41
42
2.69k
   BOTAN_ASSERT_NOMSG(m_nonce.size() == m_nonce_bytes_from_handshake);
43
44
2.69k
   if(nonce_format() == Nonce_Format::CBC_MODE)
45
1.02k
      {
46
1.02k
#if defined(BOTAN_HAS_TLS_CBC)
47
      // legacy CBC+HMAC mode
48
1.02k
      auto mac = MessageAuthenticationCode::create_or_throw("HMAC(" + suite.mac_algo() + ")");
49
1.02k
      auto cipher = BlockCipher::create_or_throw(suite.cipher_algo());
50
51
1.02k
      if(our_side)
52
573
         {
53
573
         m_aead.reset(new TLS_CBC_HMAC_AEAD_Encryption(
54
573
                         std::move(cipher),
55
573
                         std::move(mac),
56
573
                         suite.cipher_keylen(),
57
573
                         suite.mac_keylen(),
58
573
                         version,
59
573
                         uses_encrypt_then_mac));
60
573
         }
61
454
      else
62
454
         {
63
454
         m_aead.reset(new TLS_CBC_HMAC_AEAD_Decryption(
64
454
                         std::move(cipher),
65
454
                         std::move(mac),
66
454
                         suite.cipher_keylen(),
67
454
                         suite.mac_keylen(),
68
454
                         version,
69
454
                         uses_encrypt_then_mac));
70
454
         }
71
72
#else
73
      BOTAN_UNUSED(uses_encrypt_then_mac);
74
      throw Internal_Error("Negotiated disabled TLS CBC+HMAC ciphersuite");
75
#endif
76
1.02k
      }
77
1.67k
   else
78
1.67k
      {
79
910
      m_aead = AEAD_Mode::create_or_throw(suite.cipher_algo(), our_side ? ENCRYPTION : DECRYPTION);
80
1.67k
      }
81
82
2.69k
   m_aead->set_key(aead_key);
83
2.69k
   }
84
85
std::vector<uint8_t> Connection_Cipher_State::aead_nonce(uint64_t seq, RandomNumberGenerator& rng)
86
2.30k
   {
87
2.30k
   switch(m_nonce_format)
88
2.30k
      {
89
858
      case Nonce_Format::CBC_MODE:
90
858
         {
91
858
         if(m_nonce.size())
92
573
            {
93
573
            std::vector<uint8_t> nonce;
94
573
            nonce.swap(m_nonce);
95
573
            return nonce;
96
573
            }
97
285
         std::vector<uint8_t> nonce(nonce_bytes_from_record());
98
285
         rng.randomize(nonce.data(), nonce.size());
99
285
         return nonce;
100
285
         }
101
426
      case Nonce_Format::AEAD_XOR_12:
102
426
         {
103
426
         std::vector<uint8_t> nonce(12);
104
426
         store_be(seq, nonce.data() + 4);
105
426
         xor_buf(nonce, m_nonce.data(), m_nonce.size());
106
426
         return nonce;
107
285
         }
108
1.01k
      case Nonce_Format::AEAD_IMPLICIT_4:
109
1.01k
         {
110
1.01k
         BOTAN_ASSERT_NOMSG(m_nonce.size() == 4);
111
1.01k
         std::vector<uint8_t> nonce(12);
112
1.01k
         copy_mem(&nonce[0], m_nonce.data(), 4);
113
1.01k
         store_be(seq, &nonce[nonce_bytes_from_handshake()]);
114
1.01k
         return nonce;
115
0
         }
116
0
      }
117
118
0
   throw Invalid_State("Unknown nonce format specified");
119
0
   }
120
121
std::vector<uint8_t>
122
Connection_Cipher_State::aead_nonce(const uint8_t record[], size_t record_len, uint64_t seq)
123
870
   {
124
870
   switch(m_nonce_format)
125
870
      {
126
294
      case Nonce_Format::CBC_MODE:
127
294
         {
128
294
         if(nonce_bytes_from_record() == 0 && m_nonce.size())
129
0
            {
130
0
            std::vector<uint8_t> nonce;
131
0
            nonce.swap(m_nonce);
132
0
            return nonce;
133
0
            }
134
294
         if(record_len < nonce_bytes_from_record())
135
10
            throw Decoding_Error("Invalid CBC packet too short to be valid");
136
284
         std::vector<uint8_t> nonce(record, record + nonce_bytes_from_record());
137
284
         return nonce;
138
284
         }
139
207
      case Nonce_Format::AEAD_XOR_12:
140
207
         {
141
207
         std::vector<uint8_t> nonce(12);
142
207
         store_be(seq, nonce.data() + 4);
143
207
         xor_buf(nonce, m_nonce.data(), m_nonce.size());
144
207
         return nonce;
145
284
         }
146
369
      case Nonce_Format::AEAD_IMPLICIT_4:
147
369
         {
148
369
         BOTAN_ASSERT_NOMSG(m_nonce.size() == 4);
149
369
         if(record_len < nonce_bytes_from_record())
150
7
            throw Decoding_Error("Invalid AEAD packet too short to be valid");
151
362
         std::vector<uint8_t> nonce(12);
152
362
         copy_mem(&nonce[0], m_nonce.data(), 4);
153
362
         copy_mem(&nonce[nonce_bytes_from_handshake()], record, nonce_bytes_from_record());
154
362
         return nonce;
155
362
         }
156
0
      }
157
158
0
   throw Invalid_State("Unknown nonce format specified");
159
0
   }
160
161
std::vector<uint8_t>
162
Connection_Cipher_State::format_ad(uint64_t msg_sequence,
163
                                   uint8_t msg_type,
164
                                   Protocol_Version version,
165
                                   uint16_t msg_length)
166
3.13k
   {
167
3.13k
   std::vector<uint8_t> ad(13);
168
169
3.13k
   store_be(msg_sequence, &ad[0]);
170
3.13k
   ad[8] = msg_type;
171
3.13k
   ad[9] = version.major_version();
172
3.13k
   ad[10] = version.minor_version();
173
3.13k
   ad[11] = get_byte(0, msg_length);
174
3.13k
   ad[12] = get_byte(1, msg_length);
175
176
3.13k
   return ad;
177
3.13k
   }
178
179
namespace {
180
181
inline void append_u16_len(secure_vector<uint8_t>& output, size_t len_field)
182
91.7k
   {
183
91.7k
   const uint16_t len16 = static_cast<uint16_t>(len_field);
184
91.7k
   BOTAN_ASSERT_EQUAL(len_field, len16, "No truncation");
185
91.7k
   output.push_back(get_byte(0, len16));
186
91.7k
   output.push_back(get_byte(1, len16));
187
91.7k
   }
188
189
void write_record_header(secure_vector<uint8_t>& output,
190
                         uint8_t record_type,
191
                         Protocol_Version version,
192
                         uint64_t record_sequence)
193
91.7k
   {
194
91.7k
   output.clear();
195
196
91.7k
   output.push_back(record_type);
197
91.7k
   output.push_back(version.major_version());
198
91.7k
   output.push_back(version.minor_version());
199
200
91.7k
   if(version.is_datagram_protocol())
201
8.80k
      {
202
79.2k
      for(size_t i = 0; i != 8; ++i)
203
70.4k
         output.push_back(get_byte(i, record_sequence));
204
8.80k
      }
205
91.7k
   }
206
207
}
208
209
void write_unencrypted_record(secure_vector<uint8_t>& output,
210
                              uint8_t record_type,
211
                              Protocol_Version version,
212
                              uint64_t record_sequence,
213
                              const uint8_t* message,
214
                              size_t message_len)
215
89.4k
   {
216
89.4k
   if(record_type == APPLICATION_DATA)
217
0
      throw Internal_Error("Writing an unencrypted TLS application data record");
218
89.4k
   write_record_header(output, record_type, version, record_sequence);
219
89.4k
   append_u16_len(output, message_len);
220
89.4k
   output.insert(output.end(), message, message + message_len);
221
89.4k
   }
222
223
void write_record(secure_vector<uint8_t>& output,
224
                  uint8_t record_type,
225
                  Protocol_Version version,
226
                  uint64_t record_sequence,
227
                  const uint8_t* message,
228
                  size_t message_len,
229
                  Connection_Cipher_State& cs,
230
                  RandomNumberGenerator& rng)
231
2.30k
   {
232
2.30k
   write_record_header(output, record_type, version, record_sequence);
233
234
2.30k
   AEAD_Mode& aead = cs.aead();
235
2.30k
   std::vector<uint8_t> aad = cs.format_ad(record_sequence, record_type, version, static_cast<uint16_t>(message_len));
236
237
2.30k
   const size_t ctext_size = aead.output_length(message_len);
238
239
2.30k
   const size_t rec_size = ctext_size + cs.nonce_bytes_from_record();
240
241
2.30k
   aead.set_ad(aad);
242
243
2.30k
   const std::vector<uint8_t> nonce = cs.aead_nonce(record_sequence, rng);
244
245
2.30k
   append_u16_len(output, rec_size);
246
247
2.30k
   if(cs.nonce_bytes_from_record() > 0)
248
1.87k
      {
249
1.87k
      if(cs.nonce_format() == Nonce_Format::CBC_MODE)
250
858
         output += nonce;
251
1.01k
      else
252
1.01k
         output += std::make_pair(&nonce[cs.nonce_bytes_from_handshake()], cs.nonce_bytes_from_record());
253
1.87k
      }
254
255
2.30k
   const size_t header_size = output.size();
256
2.30k
   output += std::make_pair(message, message_len);
257
258
2.30k
   aead.start(nonce);
259
2.30k
   aead.finish(output, header_size);
260
261
2.30k
   BOTAN_ASSERT(output.size() < MAX_CIPHERTEXT_SIZE,
262
2.30k
                "Produced ciphertext larger than protocol allows");
263
2.30k
   }
264
265
namespace {
266
267
size_t fill_buffer_to(secure_vector<uint8_t>& readbuf,
268
                      const uint8_t*& input,
269
                      size_t& input_size,
270
                      size_t& input_consumed,
271
                      size_t desired)
272
276k
   {
273
276k
   if(readbuf.size() >= desired)
274
454
      return 0; // already have it
275
276
275k
   const size_t taken = std::min(input_size, desired - readbuf.size());
277
278
275k
   readbuf.insert(readbuf.end(), input, input + taken);
279
275k
   input_consumed += taken;
280
275k
   input_size -= taken;
281
275k
   input += taken;
282
283
275k
   return (desired - readbuf.size()); // how many bytes do we still need?
284
275k
   }
285
286
void decrypt_record(secure_vector<uint8_t>& output,
287
                    uint8_t record_contents[], size_t record_len,
288
                    uint64_t record_sequence,
289
                    Protocol_Version record_version,
290
                    Record_Type record_type,
291
                    Connection_Cipher_State& cs)
292
870
   {
293
870
   AEAD_Mode& aead = cs.aead();
294
295
870
   const std::vector<uint8_t> nonce = cs.aead_nonce(record_contents, record_len, record_sequence);
296
870
   const uint8_t* msg = &record_contents[cs.nonce_bytes_from_record()];
297
870
   const size_t msg_length = record_len - cs.nonce_bytes_from_record();
298
299
   /*
300
   * This early rejection is based just on public information (length of the
301
   * encrypted packet) and so does not leak any information. We used to use
302
   * decode_error here which really is more appropriate, but that confuses some
303
   * tools which are attempting automated detection of padding oracles,
304
   * including older versions of TLS-Attacker.
305
   */
306
870
   if(msg_length < aead.minimum_final_size())
307
23
      throw TLS_Exception(Alert::BAD_RECORD_MAC, "AEAD packet is shorter than the tag");
308
309
847
   const size_t ptext_size = aead.output_length(msg_length);
310
311
847
   aead.set_associated_data_vec(
312
847
      cs.format_ad(record_sequence,
313
847
                   static_cast<uint8_t>(record_type),
314
847
                   record_version,
315
847
                   static_cast<uint16_t>(ptext_size))
316
847
      );
317
318
847
   aead.start(nonce);
319
320
847
   output.assign(msg, msg + msg_length);
321
847
   aead.finish(output, 0);
322
847
   }
323
324
Record_Header read_tls_record(secure_vector<uint8_t>& readbuf,
325
                              const uint8_t input[],
326
                              size_t input_len,
327
                              size_t& consumed,
328
                              secure_vector<uint8_t>& recbuf,
329
                              Connection_Sequence_Numbers* sequence_numbers,
330
                              get_cipherstate_fn get_cipherstate)
331
137k
   {
332
137k
   if(readbuf.size() < TLS_HEADER_SIZE) // header incomplete?
333
137k
      {
334
137k
      if(size_t needed = fill_buffer_to(readbuf, input, input_len, consumed, TLS_HEADER_SIZE))
335
445
         {
336
445
         return Record_Header(needed);
337
445
         }
338
339
136k
      BOTAN_ASSERT_EQUAL(readbuf.size(), TLS_HEADER_SIZE, "Have an entire header");
340
136k
      }
341
342
136k
   const Protocol_Version version(readbuf[1], readbuf[2]);
343
344
136k
   if(version.is_datagram_protocol())
345
65
      throw TLS_Exception(Alert::PROTOCOL_VERSION,
346
65
                          "Expected TLS but got a record with DTLS version");
347
348
136k
   const size_t record_size = make_uint16(readbuf[TLS_HEADER_SIZE-2],
349
136k
                                          readbuf[TLS_HEADER_SIZE-1]);
350
351
136k
   if(record_size > MAX_CIPHERTEXT_SIZE)
352
244
      throw TLS_Exception(Alert::RECORD_OVERFLOW,
353
244
                          "Received a record that exceeds maximum size");
354
355
136k
   if(record_size == 0)
356
130
      throw TLS_Exception(Alert::DECODE_ERROR,
357
130
                          "Received a completely empty record");
358
359
136k
   if(size_t needed = fill_buffer_to(readbuf, input, input_len, consumed, TLS_HEADER_SIZE + record_size))
360
707
      {
361
707
      return Record_Header(needed);
362
707
      }
363
364
135k
   BOTAN_ASSERT_EQUAL(static_cast<size_t>(TLS_HEADER_SIZE) + record_size,
365
135k
                      readbuf.size(),
366
135k
                      "Have the full record");
367
368
135k
   const Record_Type type = static_cast<Record_Type>(readbuf[0]);
369
370
135k
   uint16_t epoch = 0;
371
372
135k
   uint64_t sequence = 0;
373
135k
   if(sequence_numbers)
374
129k
      {
375
129k
      sequence = sequence_numbers->next_read_sequence();
376
129k
      epoch = sequence_numbers->current_read_epoch();
377
129k
      }
378
6.05k
   else
379
6.05k
      {
380
      // server initial handshake case
381
6.05k
      epoch = 0;
382
6.05k
      }
383
384
135k
   if(epoch == 0) // Unencrypted initial handshake
385
134k
      {
386
134k
      recbuf.assign(readbuf.begin() + TLS_HEADER_SIZE, readbuf.begin() + TLS_HEADER_SIZE + record_size);
387
134k
      readbuf.clear();
388
134k
      return Record_Header(sequence, version, type);
389
134k
      }
390
391
   // Otherwise, decrypt, check MAC, return plaintext
392
870
   auto cs = get_cipherstate(epoch);
393
394
870
   BOTAN_ASSERT(cs, "Have cipherstate for this epoch");
395
396
870
   decrypt_record(recbuf,
397
870
                  &readbuf[TLS_HEADER_SIZE],
398
870
                  record_size,
399
870
                  sequence,
400
870
                  version,
401
870
                  type,
402
870
                  *cs);
403
404
870
   if(sequence_numbers)
405
0
      sequence_numbers->read_accept(sequence);
406
407
870
   readbuf.clear();
408
870
   return Record_Header(sequence, version, type);
409
870
   }
410
411
Record_Header read_dtls_record(secure_vector<uint8_t>& readbuf,
412
                               const uint8_t input[],
413
                               size_t input_len,
414
                               size_t& consumed,
415
                               secure_vector<uint8_t>& recbuf,
416
                               Connection_Sequence_Numbers* sequence_numbers,
417
                               get_cipherstate_fn get_cipherstate,
418
                               bool allow_epoch0_restart)
419
1.19k
   {
420
1.19k
   if(readbuf.size() < DTLS_HEADER_SIZE) // header incomplete?
421
1.19k
      {
422
1.19k
      if(fill_buffer_to(readbuf, input, input_len, consumed, DTLS_HEADER_SIZE))
423
34
         {
424
34
         readbuf.clear();
425
34
         return Record_Header(0);
426
34
         }
427
428
1.16k
      BOTAN_ASSERT_EQUAL(readbuf.size(), DTLS_HEADER_SIZE, "Have an entire header");
429
1.16k
      }
430
431
1.16k
   const Protocol_Version version(readbuf[1], readbuf[2]);
432
433
1.16k
   if(version.is_datagram_protocol() == false)
434
5
      {
435
5
      readbuf.clear();
436
5
      return Record_Header(0);
437
5
      }
438
439
1.16k
   const size_t record_size = make_uint16(readbuf[DTLS_HEADER_SIZE-2],
440
1.16k
                                          readbuf[DTLS_HEADER_SIZE-1]);
441
442
1.16k
   if(record_size > MAX_CIPHERTEXT_SIZE)
443
4
      {
444
      // Too large to be valid, ignore it
445
4
      readbuf.clear();
446
4
      return Record_Header(0);
447
4
      }
448
449
1.15k
   if(fill_buffer_to(readbuf, input, input_len, consumed, DTLS_HEADER_SIZE + record_size))
450
15
      {
451
      // Truncated packet?
452
15
      readbuf.clear();
453
15
      return Record_Header(0);
454
15
      }
455
456
1.14k
   BOTAN_ASSERT_EQUAL(static_cast<size_t>(DTLS_HEADER_SIZE) + record_size, readbuf.size(),
457
1.14k
                      "Have the full record");
458
459
1.14k
   const Record_Type type = static_cast<Record_Type>(readbuf[0]);
460
461
1.14k
   const uint64_t sequence = load_be<uint64_t>(&readbuf[3], 0);
462
1.14k
   const uint16_t epoch = (sequence >> 48);
463
464
1.14k
   const bool already_seen = sequence_numbers && sequence_numbers->already_seen(sequence);
465
466
1.14k
   if(already_seen && !(epoch == 0 && allow_epoch0_restart))
467
44
      {
468
44
      readbuf.clear();
469
44
      return Record_Header(0);
470
44
      }
471
472
1.09k
   if(epoch == 0) // Unencrypted initial handshake
473
1.02k
      {
474
1.02k
      recbuf.assign(readbuf.begin() + DTLS_HEADER_SIZE, readbuf.begin() + DTLS_HEADER_SIZE + record_size);
475
1.02k
      readbuf.clear();
476
1.02k
      if(sequence_numbers)
477
281
         sequence_numbers->read_accept(sequence);
478
1.02k
      return Record_Header(sequence, version, type);
479
1.02k
      }
480
481
72
   try
482
72
      {
483
      // Otherwise, decrypt, check MAC, return plaintext
484
72
      auto cs = get_cipherstate(epoch);
485
486
72
      BOTAN_ASSERT(cs, "Have cipherstate for this epoch");
487
488
72
      decrypt_record(recbuf,
489
72
                     &readbuf[DTLS_HEADER_SIZE],
490
72
                     record_size,
491
72
                     sequence,
492
72
                     version,
493
72
                     type,
494
72
                     *cs);
495
72
      }
496
72
   catch(std::exception&)
497
72
      {
498
72
      readbuf.clear();
499
72
      return Record_Header(0);
500
72
      }
501
502
0
   if(sequence_numbers)
503
0
      sequence_numbers->read_accept(sequence);
504
505
0
   readbuf.clear();
506
0
   return Record_Header(sequence, version, type);
507
0
   }
508
509
}
510
511
Record_Header read_record(bool is_datagram,
512
                          secure_vector<uint8_t>& readbuf,
513
                          const uint8_t input[],
514
                          size_t input_len,
515
                          size_t& consumed,
516
                          secure_vector<uint8_t>& recbuf,
517
                          Connection_Sequence_Numbers* sequence_numbers,
518
                          get_cipherstate_fn get_cipherstate,
519
                          bool allow_epoch0_restart)
520
138k
   {
521
138k
   if(is_datagram)
522
1.19k
      return read_dtls_record(readbuf, input, input_len, consumed,
523
1.19k
                              recbuf, sequence_numbers, get_cipherstate, allow_epoch0_restart);
524
137k
   else
525
137k
      return read_tls_record(readbuf, input, input_len, consumed,
526
137k
                             recbuf, sequence_numbers, get_cipherstate);
527
138k
   }
528
529
}
530
531
}