Coverage Report

Created: 2021-05-04 09:02

/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.28M
   {
29
2.28M
   uint8_t b;
30
2.28M
   if(!ber->read_byte(b))
31
210k
      {
32
210k
      type_tag = ASN1_Type::NoObject;
33
210k
      class_tag = ASN1_Class::NoObject;
34
210k
      return 0;
35
210k
      }
36
37
2.07M
   if((b & 0x1F) != 0x1F)
38
2.04M
      {
39
2.04M
      type_tag = ASN1_Type(b & 0x1F);
40
2.04M
      class_tag = ASN1_Class(b & 0xE0);
41
2.04M
      return 1;
42
2.04M
      }
43
44
24.2k
   size_t tag_bytes = 1;
45
24.2k
   class_tag = ASN1_Class(b & 0xE0);
46
47
24.2k
   size_t tag_buf = 0;
48
45.7k
   while(true)
49
45.7k
      {
50
45.7k
      if(!ber->read_byte(b))
51
751
         throw BER_Decoding_Error("Long-form tag truncated");
52
45.0k
      if(tag_buf & 0xFF000000)
53
395
         throw BER_Decoding_Error("Long-form tag overflowed 32 bits");
54
44.6k
      ++tag_bytes;
55
44.6k
      tag_buf = (tag_buf << 7) | (b & 0x7F);
56
44.6k
      if((b & 0x80) == 0) break;
57
44.6k
      }
58
23.1k
   type_tag = ASN1_Type(tag_buf);
59
23.1k
   return tag_bytes;
60
24.2k
   }
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.06M
   {
72
2.06M
   uint8_t b;
73
2.06M
   if(!ber->read_byte(b))
74
2.05k
      throw BER_Decoding_Error("Length field not found");
75
2.06M
   field_size = 1;
76
2.06M
   if((b & 0x80) == 0)
77
1.71M
      return b;
78
79
349k
   field_size += (b & 0x7F);
80
349k
   if(field_size > 5)
81
2.45k
      throw BER_Decoding_Error("Length field is too large");
82
83
347k
   if(field_size == 1)
84
223k
      {
85
223k
      if(allow_indef == 0)
86
249
         {
87
249
         throw BER_Decoding_Error("Nested EOC markers too deep, rejecting to avoid stack exhaustion");
88
249
         }
89
223k
      else
90
223k
         {
91
223k
         return find_eoc(ber, allow_indef - 1);
92
223k
         }
93
123k
      }
94
95
123k
   size_t length = 0;
96
97
351k
   for(size_t i = 0; i != field_size - 1; ++i)
98
229k
      {
99
229k
      if(get_byte<0>(length) != 0)
100
0
         throw BER_Decoding_Error("Field length overflow");
101
229k
      if(!ber->read_byte(b))
102
761
         throw BER_Decoding_Error("Corrupted length field");
103
228k
      length = (length << 8) | b;
104
228k
      }
105
122k
   return length;
106
123k
   }
107
108
/*
109
* Find the EOC marker
110
*/
111
size_t find_eoc(DataSource* ber, size_t allow_indef)
112
223k
   {
113
223k
   secure_vector<uint8_t> buffer(BOTAN_DEFAULT_BUFFER_SIZE), data;
114
115
510k
   while(true)
116
510k
      {
117
510k
      const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
118
510k
      if(got == 0)
119
223k
         break;
120
121
286k
      data += std::make_pair(buffer.data(), got);
122
286k
      }
123
124
223k
   DataSource_Memory source(data);
125
223k
   data.clear();
126
127
223k
   size_t length = 0;
128
953k
   while(true)
129
947k
      {
130
947k
      ASN1_Type type_tag;
131
947k
      ASN1_Class class_tag;
132
947k
      size_t tag_size = decode_tag(&source, type_tag, class_tag);
133
947k
      if(type_tag == ASN1_Type::NoObject)
134
162k
         break;
135
136
784k
      size_t length_size = 0;
137
784k
      size_t item_size = decode_length(&source, length_size, allow_indef);
138
784k
      source.discard_next(item_size);
139
140
784k
      length = BOTAN_CHECKED_ADD(length, item_size);
141
784k
      length = BOTAN_CHECKED_ADD(length, tag_size);
142
784k
      length = BOTAN_CHECKED_ADD(length, length_size);
143
144
784k
      if(type_tag == ASN1_Type::Eoc && class_tag == ASN1_Class::Universal)
145
54.4k
         break;
146
784k
      }
147
223k
   return length;
148
223k
   }
149
150
class DataSource_BERObject final : public DataSource
151
   {
152
   public:
153
      size_t read(uint8_t out[], size_t length) override
154
14.0M
         {
155
14.0M
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
156
14.0M
         const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
157
14.0M
         copy_mem(out, m_obj.bits() + m_offset, got);
158
14.0M
         m_offset += got;
159
14.0M
         return got;
160
14.0M
         }
161
162
      size_t peek(uint8_t out[], size_t length, size_t peek_offset) const override
163
58.6k
         {
164
58.6k
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
165
58.6k
         const size_t bytes_left = m_obj.length() - m_offset;
166
167
58.6k
         if(peek_offset >= bytes_left)
168
27.5k
            return 0;
169
170
31.1k
         const size_t got = std::min(bytes_left - peek_offset, length);
171
31.1k
         copy_mem(out, m_obj.bits() + peek_offset, got);
172
31.1k
         return got;
173
31.1k
         }
174
175
      bool check_available(size_t n) override
176
851k
         {
177
851k
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
178
851k
         return (n <= (m_obj.length() - m_offset));
179
851k
         }
180
181
      bool end_of_data() const override
182
695k
         {
183
695k
         return get_bytes_read() == m_obj.length();
184
695k
         }
185
186
695k
      size_t get_bytes_read() const override { return m_offset; }
187
188
468k
      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
479k
   {
202
479k
   if(m_source->end_of_data() && !m_pushed.is_set())
203
137k
      return false;
204
342k
   return true;
205
342k
   }
206
207
/*
208
* Verify that no bytes remain in the source
209
*/
210
BER_Decoder& BER_Decoder::verify_end()
211
23.2k
   {
212
23.2k
   return verify_end("BER_Decoder::verify_end called, but data remains");
213
23.2k
   }
214
215
/*
216
* Verify that no bytes remain in the source
217
*/
218
BER_Decoder& BER_Decoder::verify_end(const std::string& err)
219
33.2k
   {
220
33.2k
   if(!m_source->end_of_data() || m_pushed.is_set())
221
252
      throw Decoding_Error(err);
222
33.0k
   return (*this);
223
33.0k
   }
224
225
/*
226
* Discard all the bytes remaining in the source
227
*/
228
BER_Decoder& BER_Decoder::discard_remaining()
229
13.1k
   {
230
13.1k
   uint8_t buf;
231
2.34M
   while(m_source->read_byte(buf))
232
2.33M
      {}
233
13.1k
   return (*this);
234
13.1k
   }
235
236
/*
237
* Return the BER encoding of the next object
238
*/
239
BER_Object BER_Decoder::get_next_object()
240
1.32M
   {
241
1.32M
   BER_Object next;
242
243
1.32M
   if(m_pushed.is_set())
244
68.3k
      {
245
68.3k
      std::swap(next, m_pushed);
246
68.3k
      return next;
247
68.3k
      }
248
249
1.25M
   for(;;)
250
1.33M
      {
251
1.33M
      ASN1_Type type_tag;
252
1.33M
      ASN1_Class class_tag;
253
1.33M
      decode_tag(m_source, type_tag, class_tag);
254
1.33M
      next.set_tagging(type_tag, class_tag);
255
1.33M
      if(next.is_set() == false) // no more objects
256
48.4k
         return next;
257
258
1.28M
      size_t field_size;
259
1.28M
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
260
1.28M
      if(!m_source->check_available(length))
261
5.40k
         throw BER_Decoding_Error("Value truncated");
262
263
1.27M
      uint8_t* out = next.mutable_bits(length);
264
1.27M
      if(m_source->read(out, length) != length)
265
0
         throw BER_Decoding_Error("Value truncated");
266
267
1.27M
      if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc))
268
81.1k
         continue;
269
1.19M
      else
270
1.19M
         break;
271
1.27M
      }
272
273
1.19M
   return next;
274
1.25M
   }
275
276
/*
277
* Push a object back into the stream
278
*/
279
void BER_Decoder::push_back(const BER_Object& obj)
280
6.72k
   {
281
6.72k
   if(m_pushed.is_set())
282
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
283
6.72k
   m_pushed = obj;
284
6.72k
   }
285
286
void BER_Decoder::push_back(BER_Object&& obj)
287
89.1k
   {
288
89.1k
   if(m_pushed.is_set())
289
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
290
89.1k
   m_pushed = std::move(obj);
291
89.1k
   }
292
293
BER_Decoder BER_Decoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
294
475k
   {
295
475k
   BER_Object obj = get_next_object();
296
475k
   obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
297
475k
   return BER_Decoder(std::move(obj), this);
298
475k
   }
299
300
/*
301
* Finish decoding a CONSTRUCTED type
302
*/
303
BER_Decoder& BER_Decoder::end_cons()
304
330k
   {
305
330k
   if(!m_parent)
306
0
      throw Invalid_State("BER_Decoder::end_cons called with null parent");
307
330k
   if(!m_source->end_of_data())
308
823
      throw Decoding_Error("BER_Decoder::end_cons called with data left");
309
329k
   return (*m_parent);
310
329k
   }
311
312
BER_Decoder::BER_Decoder(BER_Object&& obj, BER_Decoder* parent)
313
468k
   {
314
468k
   m_data_src.reset(new DataSource_BERObject(std::move(obj)));
315
468k
   m_source = m_data_src.get();
316
468k
   m_parent = parent;
317
468k
   }
318
319
/*
320
* BER_Decoder Constructor
321
*/
322
BER_Decoder::BER_Decoder(DataSource& src)
323
23.5k
   {
324
23.5k
   m_source = &src;
325
23.5k
   }
326
327
/*
328
* BER_Decoder Constructor
329
 */
330
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length)
331
43.8k
   {
332
43.8k
   m_data_src.reset(new DataSource_Memory(data, length));
333
43.8k
   m_source = m_data_src.get();
334
43.8k
   }
335
336
/*
337
* BER_Decoder Constructor
338
*/
339
BER_Decoder::BER_Decoder(const secure_vector<uint8_t>& data)
340
5.04k
   {
341
5.04k
   m_data_src.reset(new DataSource_Memory(data));
342
5.04k
   m_source = m_data_src.get();
343
5.04k
   }
344
345
/*
346
* BER_Decoder Constructor
347
*/
348
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data)
349
173k
   {
350
173k
   m_data_src.reset(new DataSource_Memory(data.data(), data.size()));
351
173k
   m_source = m_data_src.get();
352
173k
   }
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
523k
   {
372
523k
   obj.decode_from(*this);
373
523k
   return (*this);
374
523k
   }
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
2.20k
   {
390
2.20k
   secure_vector<uint8_t> out_vec;
391
2.20k
   decode(out_vec, ASN1_Type::OctetString);
392
2.20k
   out = BigInt::decode(out_vec.data(), out_vec.size());
393
2.20k
   return (*this);
394
2.20k
   }
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
2.54k
   {
402
2.54k
   BER_Object obj = get_next_object();
403
2.54k
   obj.assert_is_a(type_tag, class_tag);
404
405
2.54k
   if(obj.length() != 1)
406
343
      throw BER_Decoding_Error("BER boolean value had invalid size");
407
408
2.20k
   out = (obj.bits()[0]) ? true : false;
409
2.20k
   return (*this);
410
2.20k
   }
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
16.2k
   {
419
16.2k
   BigInt integer;
420
16.2k
   decode(integer, type_tag, class_tag);
421
422
16.2k
   if(integer.is_negative())
423
450
      throw BER_Decoding_Error("Decoded small integer value was negative");
424
425
15.8k
   if(integer.bits() > 32)
426
190
      throw BER_Decoding_Error("Decoded integer value larger than expected");
427
428
15.6k
   out = 0;
429
68.4k
   for(size_t i = 0; i != 4; ++i)
430
52.8k
      out = (out << 8) | integer.byte_at(3-i);
431
432
15.6k
   return (*this);
433
15.6k
   }
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
62.4k
   {
465
62.4k
   BER_Object obj = get_next_object();
466
62.4k
   obj.assert_is_a(type_tag, class_tag);
467
468
62.4k
   if(obj.length() == 0)
469
9.38k
      {
470
9.38k
      out.clear();
471
9.38k
      }
472
53.0k
   else
473
53.0k
      {
474
48.4k
      const bool negative = (obj.bits()[0] & 0x80) ? true : false;
475
476
53.0k
      if(negative)
477
4.58k
         {
478
4.58k
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
479
8.88k
         for(size_t i = obj.length(); i > 0; --i)
480
8.88k
            if(vec[i-1]--)
481
4.58k
               break;
482
239k
         for(size_t i = 0; i != obj.length(); ++i)
483
234k
            vec[i] = ~vec[i];
484
4.58k
         out = BigInt(vec.data(), vec.size());
485
4.58k
         out.flip_sign();
486
4.58k
         }
487
48.4k
      else
488
48.4k
         {
489
48.4k
         out = BigInt(obj.bits(), obj.length());
490
48.4k
         }
491
53.0k
      }
492
493
62.4k
   return (*this);
494
62.4k
   }
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
103k
   {
505
103k
   obj.assert_is_a(type_tag, class_tag);
506
507
103k
   if(real_type == ASN1_Type::OctetString)
508
62.9k
      {
509
62.9k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
510
62.9k
      }
511
40.4k
   else
512
40.4k
      {
513
40.4k
      if(obj.length() == 0)
514
305
         throw BER_Decoding_Error("Invalid BIT STRING");
515
40.1k
      if(obj.bits()[0] >= 8)
516
256
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
517
518
39.9k
      buffer.resize(obj.length() - 1);
519
520
39.9k
      if(obj.length() > 1)
521
31.1k
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
522
39.9k
      }
523
103k
   }
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
5.44k
   {
505
5.44k
   obj.assert_is_a(type_tag, class_tag);
506
507
5.44k
   if(real_type == ASN1_Type::OctetString)
508
4.56k
      {
509
4.56k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
510
4.56k
      }
511
880
   else
512
880
      {
513
880
      if(obj.length() == 0)
514
1
         throw BER_Decoding_Error("Invalid BIT STRING");
515
879
      if(obj.bits()[0] >= 8)
516
12
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
517
518
867
      buffer.resize(obj.length() - 1);
519
520
867
      if(obj.length() > 1)
521
729
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
522
867
      }
523
5.44k
   }
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
97.9k
   {
505
97.9k
   obj.assert_is_a(type_tag, class_tag);
506
507
97.9k
   if(real_type == ASN1_Type::OctetString)
508
58.3k
      {
509
58.3k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
510
58.3k
      }
511
39.6k
   else
512
39.6k
      {
513
39.6k
      if(obj.length() == 0)
514
304
         throw BER_Decoding_Error("Invalid BIT STRING");
515
39.3k
      if(obj.bits()[0] >= 8)
516
244
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
517
518
39.0k
      buffer.resize(obj.length() - 1);
519
520
39.0k
      if(obj.length() > 1)
521
30.3k
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
522
39.0k
      }
523
97.9k
   }
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
5.45k
   {
534
5.45k
   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
5.45k
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
538
5.45k
   return (*this);
539
5.45k
   }
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
99.4k
   {
545
99.4k
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString)
546
1.24k
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
547
548
98.1k
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
549
98.1k
   return (*this);
550
98.1k
   }
551
552
}