Coverage Report

Created: 2021-02-21 07:20

/src/botan/src/lib/asn1/ber_dec.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* BER Decoder
3
* (C) 1999-2008,2015,2017,2018 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/ber_dec.h>
9
#include <botan/bigint.h>
10
#include <botan/internal/loadstor.h>
11
#include <botan/internal/safeint.h>
12
13
namespace Botan {
14
15
namespace {
16
17
/*
18
* This value is somewhat arbitrary. OpenSSL allows up to 128 nested
19
* indefinite length sequences. If you increase this, also increase the
20
* limit in the test in test_asn1.cpp
21
*/
22
const size_t ALLOWED_EOC_NESTINGS = 16;
23
24
/*
25
* BER decode an ASN.1 type tag
26
*/
27
size_t decode_tag(DataSource* ber, ASN1_Type& type_tag, ASN1_Class& class_tag)
28
2.35M
   {
29
2.35M
   uint8_t b;
30
2.35M
   if(!ber->read_byte(b))
31
195k
      {
32
195k
      type_tag = ASN1_Type::NoObject;
33
195k
      class_tag = ASN1_Class::NoObject;
34
195k
      return 0;
35
195k
      }
36
37
2.16M
   if((b & 0x1F) != 0x1F)
38
2.13M
      {
39
2.13M
      type_tag = ASN1_Type(b & 0x1F);
40
2.13M
      class_tag = ASN1_Class(b & 0xE0);
41
2.13M
      return 1;
42
2.13M
      }
43
44
21.9k
   size_t tag_bytes = 1;
45
21.9k
   class_tag = ASN1_Class(b & 0xE0);
46
47
21.9k
   size_t tag_buf = 0;
48
55.1k
   while(true)
49
55.1k
      {
50
55.1k
      if(!ber->read_byte(b))
51
649
         throw BER_Decoding_Error("Long-form tag truncated");
52
54.4k
      if(tag_buf & 0xFF000000)
53
393
         throw BER_Decoding_Error("Long-form tag overflowed 32 bits");
54
54.0k
      ++tag_bytes;
55
54.0k
      tag_buf = (tag_buf << 7) | (b & 0x7F);
56
54.0k
      if((b & 0x80) == 0) break;
57
54.0k
      }
58
20.8k
   type_tag = ASN1_Type(tag_buf);
59
20.8k
   return tag_bytes;
60
21.9k
   }
61
62
/*
63
* Find the EOC marker
64
*/
65
size_t find_eoc(DataSource* src, size_t allow_indef);
66
67
/*
68
* BER decode an ASN.1 length field
69
*/
70
size_t decode_length(DataSource* ber, size_t& field_size, size_t allow_indef)
71
2.16M
   {
72
2.16M
   uint8_t b;
73
2.16M
   if(!ber->read_byte(b))
74
1.92k
      throw BER_Decoding_Error("Length field not found");
75
2.15M
   field_size = 1;
76
2.15M
   if((b & 0x80) == 0)
77
1.82M
      return b;
78
79
332k
   field_size += (b & 0x7F);
80
332k
   if(field_size > 5)
81
2.83k
      throw BER_Decoding_Error("Length field is too large");
82
83
329k
   if(field_size == 1)
84
208k
      {
85
208k
      if(allow_indef == 0)
86
255
         {
87
255
         throw BER_Decoding_Error("Nested EOC markers too deep, rejecting to avoid stack exhaustion");
88
255
         }
89
208k
      else
90
208k
         {
91
208k
         return find_eoc(ber, allow_indef - 1);
92
208k
         }
93
121k
      }
94
95
121k
   size_t length = 0;
96
97
341k
   for(size_t i = 0; i != field_size - 1; ++i)
98
220k
      {
99
220k
      if(get_byte(0, length) != 0)
100
0
         throw BER_Decoding_Error("Field length overflow");
101
220k
      if(!ber->read_byte(b))
102
1.11k
         throw BER_Decoding_Error("Corrupted length field");
103
219k
      length = (length << 8) | b;
104
219k
      }
105
120k
   return length;
106
121k
   }
107
108
/*
109
* Find the EOC marker
110
*/
111
size_t find_eoc(DataSource* ber, size_t allow_indef)
112
208k
   {
113
208k
   secure_vector<uint8_t> buffer(BOTAN_DEFAULT_BUFFER_SIZE), data;
114
115
462k
   while(true)
116
462k
      {
117
462k
      const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
118
462k
      if(got == 0)
119
208k
         break;
120
121
254k
      data += std::make_pair(buffer.data(), got);
122
254k
      }
123
124
208k
   DataSource_Memory source(data);
125
208k
   data.clear();
126
127
208k
   size_t length = 0;
128
894k
   while(true)
129
887k
      {
130
887k
      ASN1_Type type_tag;
131
887k
      ASN1_Class class_tag;
132
887k
      size_t tag_size = decode_tag(&source, type_tag, class_tag);
133
887k
      if(type_tag == ASN1_Type::NoObject)
134
148k
         break;
135
136
739k
      size_t length_size = 0;
137
739k
      size_t item_size = decode_length(&source, length_size, allow_indef);
138
739k
      source.discard_next(item_size);
139
140
739k
      length = BOTAN_CHECKED_ADD(length, item_size);
141
739k
      length = BOTAN_CHECKED_ADD(length, tag_size);
142
739k
      length = BOTAN_CHECKED_ADD(length, length_size);
143
144
739k
      if(type_tag == ASN1_Type::Eoc && class_tag == ASN1_Class::Universal)
145
53.4k
         break;
146
739k
      }
147
208k
   return length;
148
208k
   }
149
150
class DataSource_BERObject final : public DataSource
151
   {
152
   public:
153
      size_t read(uint8_t out[], size_t length) override
154
15.2M
         {
155
15.2M
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
156
15.2M
         const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
157
15.2M
         copy_mem(out, m_obj.bits() + m_offset, got);
158
15.2M
         m_offset += got;
159
15.2M
         return got;
160
15.2M
         }
161
162
      size_t peek(uint8_t out[], size_t length, size_t peek_offset) const override
163
52.9k
         {
164
52.9k
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
165
52.9k
         const size_t bytes_left = m_obj.length() - m_offset;
166
167
52.9k
         if(peek_offset >= bytes_left)
168
26.4k
            return 0;
169
170
26.4k
         const size_t got = std::min(bytes_left - peek_offset, length);
171
26.4k
         copy_mem(out, m_obj.bits() + peek_offset, got);
172
26.4k
         return got;
173
26.4k
         }
174
175
      bool check_available(size_t n) override
176
904k
         {
177
904k
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
178
904k
         return (n <= (m_obj.length() - m_offset));
179
904k
         }
180
181
      bool end_of_data() const override
182
884k
         {
183
884k
         return get_bytes_read() == m_obj.length();
184
884k
         }
185
186
884k
      size_t get_bytes_read() const override { return m_offset; }
187
188
545k
      explicit DataSource_BERObject(BER_Object&& obj) : m_obj(std::move(obj)), m_offset(0) {}
189
190
   private:
191
      BER_Object m_obj;
192
      size_t m_offset;
193
   };
194
195
}
196
197
/*
198
* Check if more objects are there
199
*/
200
bool BER_Decoder::more_items() const
201
546k
   {
202
546k
   if(m_source->end_of_data() && !m_pushed.is_set())
203
178k
      return false;
204
368k
   return true;
205
368k
   }
206
207
/*
208
* Verify that no bytes remain in the source
209
*/
210
BER_Decoder& BER_Decoder::verify_end()
211
29.8k
   {
212
29.8k
   return verify_end("BER_Decoder::verify_end called, but data remains");
213
29.8k
   }
214
215
/*
216
* Verify that no bytes remain in the source
217
*/
218
BER_Decoder& BER_Decoder::verify_end(const std::string& err)
219
153k
   {
220
153k
   if(!m_source->end_of_data() || m_pushed.is_set())
221
292
      throw Decoding_Error(err);
222
152k
   return (*this);
223
152k
   }
224
225
/*
226
* Discard all the bytes remaining in the source
227
*/
228
BER_Decoder& BER_Decoder::discard_remaining()
229
16.4k
   {
230
16.4k
   uint8_t buf;
231
2.53M
   while(m_source->read_byte(buf))
232
2.51M
      {}
233
16.4k
   return (*this);
234
16.4k
   }
235
236
/*
237
* Return the BER encoding of the next object
238
*/
239
BER_Object BER_Decoder::get_next_object()
240
1.46M
   {
241
1.46M
   BER_Object next;
242
243
1.46M
   if(m_pushed.is_set())
244
79.1k
      {
245
79.1k
      std::swap(next, m_pushed);
246
79.1k
      return next;
247
79.1k
      }
248
249
1.38M
   for(;;)
250
1.46M
      {
251
1.46M
      ASN1_Type type_tag;
252
1.46M
      ASN1_Class class_tag;
253
1.46M
      decode_tag(m_source, type_tag, class_tag);
254
1.46M
      next.set_tagging(type_tag, class_tag);
255
1.46M
      if(next.is_set() == false) // no more objects
256
47.4k
         return next;
257
258
1.42M
      size_t field_size;
259
1.42M
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
260
1.42M
      if(!m_source->check_available(length))
261
6.21k
         throw BER_Decoding_Error("Value truncated");
262
263
1.41M
      uint8_t* out = next.mutable_bits(length);
264
1.41M
      if(m_source->read(out, length) != length)
265
0
         throw BER_Decoding_Error("Value truncated");
266
267
1.41M
      if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc))
268
87.3k
         continue;
269
1.32M
      else
270
1.32M
         break;
271
1.41M
      }
272
273
1.32M
   return next;
274
1.38M
   }
275
276
/*
277
* Push a object back into the stream
278
*/
279
void BER_Decoder::push_back(const BER_Object& obj)
280
7.31k
   {
281
7.31k
   if(m_pushed.is_set())
282
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
283
7.31k
   m_pushed = obj;
284
7.31k
   }
285
286
void BER_Decoder::push_back(BER_Object&& obj)
287
98.3k
   {
288
98.3k
   if(m_pushed.is_set())
289
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
290
98.3k
   m_pushed = std::move(obj);
291
98.3k
   }
292
293
BER_Decoder BER_Decoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
294
549k
   {
295
549k
   BER_Object obj = get_next_object();
296
549k
   obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
297
549k
   return BER_Decoder(std::move(obj), this);
298
549k
   }
299
300
/*
301
* Finish decoding a CONSTRUCTED type
302
*/
303
BER_Decoder& BER_Decoder::end_cons()
304
375k
   {
305
375k
   if(!m_parent)
306
0
      throw Invalid_State("BER_Decoder::end_cons called with null parent");
307
375k
   if(!m_source->end_of_data())
308
903
      throw Decoding_Error("BER_Decoder::end_cons called with data left");
309
374k
   return (*m_parent);
310
374k
   }
311
312
BER_Decoder::BER_Decoder(BER_Object&& obj, BER_Decoder* parent)
313
545k
   {
314
545k
   m_data_src.reset(new DataSource_BERObject(std::move(obj)));
315
545k
   m_source = m_data_src.get();
316
545k
   m_parent = parent;
317
545k
   }
318
319
/*
320
* BER_Decoder Constructor
321
*/
322
BER_Decoder::BER_Decoder(DataSource& src)
323
29.9k
   {
324
29.9k
   m_source = &src;
325
29.9k
   }
326
327
/*
328
* BER_Decoder Constructor
329
 */
330
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length)
331
60.7k
   {
332
60.7k
   m_data_src.reset(new DataSource_Memory(data, length));
333
60.7k
   m_source = m_data_src.get();
334
60.7k
   }
335
336
/*
337
* BER_Decoder Constructor
338
*/
339
BER_Decoder::BER_Decoder(const secure_vector<uint8_t>& data)
340
4.72k
   {
341
4.72k
   m_data_src.reset(new DataSource_Memory(data));
342
4.72k
   m_source = m_data_src.get();
343
4.72k
   }
344
345
/*
346
* BER_Decoder Constructor
347
*/
348
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data)
349
197k
   {
350
197k
   m_data_src.reset(new DataSource_Memory(data.data(), data.size()));
351
197k
   m_source = m_data_src.get();
352
197k
   }
353
354
/*
355
* BER_Decoder Copy Constructor
356
*/
357
BER_Decoder::BER_Decoder(const BER_Decoder& other)
358
0
   {
359
0
   m_source = other.m_source;
360
361
   // take ownership
362
0
   std::swap(m_data_src, other.m_data_src);
363
0
   m_parent = other.m_parent;
364
0
   }
365
366
/*
367
* Request for an object to decode itself
368
*/
369
BER_Decoder& BER_Decoder::decode(ASN1_Object& obj,
370
                                 ASN1_Type, ASN1_Class)
371
583k
   {
372
583k
   obj.decode_from(*this);
373
583k
   return (*this);
374
583k
   }
375
376
/*
377
* Decode a BER encoded NULL
378
*/
379
BER_Decoder& BER_Decoder::decode_null()
380
0
   {
381
0
   BER_Object obj = get_next_object();
382
0
   obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
383
0
   if(obj.length() > 0)
384
0
      throw BER_Decoding_Error("NULL object had nonzero size");
385
0
   return (*this);
386
0
   }
387
388
BER_Decoder& BER_Decoder::decode_octet_string_bigint(BigInt& out)
389
1.88k
   {
390
1.88k
   secure_vector<uint8_t> out_vec;
391
1.88k
   decode(out_vec, ASN1_Type::OctetString);
392
1.88k
   out = BigInt::decode(out_vec.data(), out_vec.size());
393
1.88k
   return (*this);
394
1.88k
   }
395
396
/*
397
* Decode a BER encoded BOOLEAN
398
*/
399
BER_Decoder& BER_Decoder::decode(bool& out,
400
                                 ASN1_Type type_tag, ASN1_Class class_tag)
401
4.34k
   {
402
4.34k
   BER_Object obj = get_next_object();
403
4.34k
   obj.assert_is_a(type_tag, class_tag);
404
405
4.34k
   if(obj.length() != 1)
406
455
      throw BER_Decoding_Error("BER boolean value had invalid size");
407
408
3.89k
   out = (obj.bits()[0]) ? true : false;
409
3.89k
   return (*this);
410
3.89k
   }
411
412
/*
413
* Decode a small BER encoded INTEGER
414
*/
415
BER_Decoder& BER_Decoder::decode(size_t& out,
416
                                 ASN1_Type type_tag,
417
                                 ASN1_Class class_tag)
418
18.5k
   {
419
18.5k
   BigInt integer;
420
18.5k
   decode(integer, type_tag, class_tag);
421
422
18.5k
   if(integer.is_negative())
423
422
      throw BER_Decoding_Error("Decoded small integer value was negative");
424
425
18.1k
   if(integer.bits() > 32)
426
171
      throw BER_Decoding_Error("Decoded integer value larger than expected");
427
428
17.9k
   out = 0;
429
80.9k
   for(size_t i = 0; i != 4; ++i)
430
62.9k
      out = (out << 8) | integer.byte_at(3-i);
431
432
17.9k
   return (*this);
433
17.9k
   }
434
435
/*
436
* Decode a small BER encoded INTEGER
437
*/
438
uint64_t BER_Decoder::decode_constrained_integer(ASN1_Type type_tag,
439
                                                 ASN1_Class class_tag,
440
                                                 size_t T_bytes)
441
0
   {
442
0
   if(T_bytes > 8)
443
0
      throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
444
445
0
   BigInt integer;
446
0
   decode(integer, type_tag, class_tag);
447
448
0
   if(integer.bits() > 8*T_bytes)
449
0
      throw BER_Decoding_Error("Decoded integer value larger than expected");
450
451
0
   uint64_t out = 0;
452
0
   for(size_t i = 0; i != 8; ++i)
453
0
      out = (out << 8) | integer.byte_at(7-i);
454
455
0
   return out;
456
0
   }
457
458
/*
459
* Decode a BER encoded INTEGER
460
*/
461
BER_Decoder& BER_Decoder::decode(BigInt& out,
462
                                 ASN1_Type type_tag,
463
                                 ASN1_Class class_tag)
464
67.6k
   {
465
67.6k
   BER_Object obj = get_next_object();
466
67.6k
   obj.assert_is_a(type_tag, class_tag);
467
468
67.6k
   if(obj.length() == 0)
469
9.11k
      {
470
9.11k
      out = 0;
471
9.11k
      }
472
58.5k
   else
473
58.5k
      {
474
53.8k
      const bool negative = (obj.bits()[0] & 0x80) ? true : false;
475
476
58.5k
      if(negative)
477
4.62k
         {
478
4.62k
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
479
9.15k
         for(size_t i = obj.length(); i > 0; --i)
480
9.15k
            if(vec[i-1]--)
481
4.62k
               break;
482
226k
         for(size_t i = 0; i != obj.length(); ++i)
483
221k
            vec[i] = ~vec[i];
484
4.62k
         out = BigInt(vec.data(), vec.size());
485
4.62k
         out.flip_sign();
486
4.62k
         }
487
53.8k
      else
488
53.8k
         {
489
53.8k
         out = BigInt(obj.bits(), obj.length());
490
53.8k
         }
491
58.5k
      }
492
493
67.6k
   return (*this);
494
67.6k
   }
495
496
namespace {
497
498
template<typename Alloc>
499
void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer,
500
                               const BER_Object& obj,
501
                               ASN1_Type real_type,
502
                               ASN1_Type type_tag,
503
                               ASN1_Class class_tag)
504
114k
   {
505
114k
   obj.assert_is_a(type_tag, class_tag);
506
507
114k
   if(real_type == ASN1_Type::OctetString)
508
65.6k
      {
509
65.6k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
510
65.6k
      }
511
48.6k
   else
512
48.6k
      {
513
48.6k
      if(obj.length() == 0)
514
243
         throw BER_Decoding_Error("Invalid BIT STRING");
515
48.3k
      if(obj.bits()[0] >= 8)
516
254
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
517
518
48.1k
      buffer.resize(obj.length() - 1);
519
520
48.1k
      if(obj.length() > 1)
521
39.8k
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
522
48.1k
      }
523
114k
   }
ber_dec.cpp:void Botan::(anonymous namespace)::asn1_decode_binary_string<Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, Botan::BER_Object const&, Botan::ASN1_Type, Botan::ASN1_Type, Botan::ASN1_Class)
Line
Count
Source
504
4.96k
   {
505
4.96k
   obj.assert_is_a(type_tag, class_tag);
506
507
4.96k
   if(real_type == ASN1_Type::OctetString)
508
4.09k
      {
509
4.09k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
510
4.09k
      }
511
871
   else
512
871
      {
513
871
      if(obj.length() == 0)
514
1
         throw BER_Decoding_Error("Invalid BIT STRING");
515
870
      if(obj.bits()[0] >= 8)
516
8
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
517
518
862
      buffer.resize(obj.length() - 1);
519
520
862
      if(obj.length() > 1)
521
714
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
522
862
      }
523
4.96k
   }
ber_dec.cpp:void Botan::(anonymous namespace)::asn1_decode_binary_string<std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&, Botan::BER_Object const&, Botan::ASN1_Type, Botan::ASN1_Type, Botan::ASN1_Class)
Line
Count
Source
504
109k
   {
505
109k
   obj.assert_is_a(type_tag, class_tag);
506
507
109k
   if(real_type == ASN1_Type::OctetString)
508
61.5k
      {
509
61.5k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
510
61.5k
      }
511
47.7k
   else
512
47.7k
      {
513
47.7k
      if(obj.length() == 0)
514
242
         throw BER_Decoding_Error("Invalid BIT STRING");
515
47.5k
      if(obj.bits()[0] >= 8)
516
246
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
517
518
47.2k
      buffer.resize(obj.length() - 1);
519
520
47.2k
      if(obj.length() > 1)
521
39.1k
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
522
47.2k
      }
523
109k
   }
524
525
}
526
527
/*
528
* BER decode a BIT STRING or OCTET STRING
529
*/
530
BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& buffer,
531
                                 ASN1_Type real_type,
532
                                 ASN1_Type type_tag, ASN1_Class class_tag)
533
4.97k
   {
534
4.97k
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString)
535
0
       throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
536
537
4.97k
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
538
4.97k
   return (*this);
539
4.97k
   }
540
541
BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& buffer,
542
                                 ASN1_Type real_type,
543
                                 ASN1_Type type_tag, ASN1_Class class_tag)
544
110k
   {
545
110k
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString)
546
1.36k
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
547
548
109k
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
549
109k
   return (*this);
550
109k
   }
551
552
}