Coverage Report

Created: 2019-12-03 15:21

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