Coverage Report

Created: 2021-10-13 08:49

/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.49M
   {
29
2.49M
   uint8_t b;
30
2.49M
   if(!ber->read_byte(b))
31
214k
      {
32
214k
      type_tag = ASN1_Type::NoObject;
33
214k
      class_tag = ASN1_Class::NoObject;
34
214k
      return 0;
35
214k
      }
36
37
2.27M
   if((b & 0x1F) != 0x1F)
38
2.25M
      {
39
2.25M
      type_tag = ASN1_Type(b & 0x1F);
40
2.25M
      class_tag = ASN1_Class(b & 0xE0);
41
2.25M
      return 1;
42
2.25M
      }
43
44
25.1k
   size_t tag_bytes = 1;
45
25.1k
   class_tag = ASN1_Class(b & 0xE0);
46
47
25.1k
   size_t tag_buf = 0;
48
167k
   while(true)
49
167k
      {
50
167k
      if(!ber->read_byte(b))
51
787
         throw BER_Decoding_Error("Long-form tag truncated");
52
166k
      if(tag_buf & 0xFF000000)
53
410
         throw BER_Decoding_Error("Long-form tag overflowed 32 bits");
54
165k
      ++tag_bytes;
55
165k
      tag_buf = (tag_buf << 7) | (b & 0x7F);
56
165k
      if((b & 0x80) == 0) break;
57
165k
      }
58
23.9k
   type_tag = ASN1_Type(tag_buf);
59
23.9k
   return tag_bytes;
60
25.1k
   }
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.27M
   {
72
2.27M
   uint8_t b;
73
2.27M
   if(!ber->read_byte(b))
74
2.57k
      throw BER_Decoding_Error("Length field not found");
75
2.27M
   field_size = 1;
76
2.27M
   if((b & 0x80) == 0)
77
1.87M
      return b;
78
79
397k
   field_size += (b & 0x7F);
80
397k
   if(field_size > 5)
81
2.66k
      throw BER_Decoding_Error("Length field is too large");
82
83
394k
   if(field_size == 1)
84
241k
      {
85
241k
      if(allow_indef == 0)
86
239
         {
87
239
         throw BER_Decoding_Error("Nested EOC markers too deep, rejecting to avoid stack exhaustion");
88
239
         }
89
241k
      else
90
241k
         {
91
241k
         return find_eoc(ber, allow_indef - 1);
92
241k
         }
93
241k
      }
94
95
153k
   size_t length = 0;
96
97
425k
   for(size_t i = 0; i != field_size - 1; ++i)
98
272k
      {
99
272k
      if(get_byte<0>(length) != 0)
100
0
         throw BER_Decoding_Error("Field length overflow");
101
272k
      if(!ber->read_byte(b))
102
636
         throw BER_Decoding_Error("Corrupted length field");
103
272k
      length = (length << 8) | b;
104
272k
      }
105
152k
   return length;
106
153k
   }
107
108
/*
109
* Find the EOC marker
110
*/
111
size_t find_eoc(DataSource* ber, size_t allow_indef)
112
241k
   {
113
241k
   secure_vector<uint8_t> buffer(BOTAN_DEFAULT_BUFFER_SIZE), data;
114
115
600k
   while(true)
116
600k
      {
117
600k
      const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
118
600k
      if(got == 0)
119
241k
         break;
120
121
358k
      data += std::make_pair(buffer.data(), got);
122
358k
      }
123
124
241k
   DataSource_Memory source(data);
125
241k
   data.clear();
126
127
241k
   size_t length = 0;
128
1.09M
   while(true)
129
1.09M
      {
130
1.09M
      ASN1_Type type_tag;
131
1.09M
      ASN1_Class class_tag;
132
1.09M
      size_t tag_size = decode_tag(&source, type_tag, class_tag);
133
1.09M
      if(type_tag == ASN1_Type::NoObject)
134
169k
         break;
135
136
922k
      size_t length_size = 0;
137
922k
      size_t item_size = decode_length(&source, length_size, allow_indef);
138
922k
      source.discard_next(item_size);
139
140
922k
      length = BOTAN_CHECKED_ADD(length, item_size);
141
922k
      length = BOTAN_CHECKED_ADD(length, tag_size);
142
922k
      length = BOTAN_CHECKED_ADD(length, length_size);
143
144
922k
      if(type_tag == ASN1_Type::Eoc && class_tag == ASN1_Class::Universal)
145
65.8k
         break;
146
922k
      }
147
241k
   return length;
148
241k
   }
149
150
class DataSource_BERObject final : public DataSource
151
   {
152
   public:
153
      size_t read(uint8_t out[], size_t length) override
154
15.5M
         {
155
15.5M
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
156
15.5M
         const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
157
15.5M
         copy_mem(out, m_obj.bits() + m_offset, got);
158
15.5M
         m_offset += got;
159
15.5M
         return got;
160
15.5M
         }
161
162
      size_t peek(uint8_t out[], size_t length, size_t peek_offset) const override
163
82.2k
         {
164
82.2k
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
165
82.2k
         const size_t bytes_left = m_obj.length() - m_offset;
166
167
82.2k
         if(peek_offset >= bytes_left)
168
34.4k
            return 0;
169
170
47.8k
         const size_t got = std::min(bytes_left - peek_offset, length);
171
47.8k
         copy_mem(out, m_obj.bits() + peek_offset, got);
172
47.8k
         return got;
173
82.2k
         }
174
175
      bool check_available(size_t n) override
176
877k
         {
177
877k
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
178
877k
         return (n <= (m_obj.length() - m_offset));
179
877k
         }
180
181
      bool end_of_data() const override
182
693k
         {
183
693k
         return get_bytes_read() == m_obj.length();
184
693k
         }
185
186
693k
      size_t get_bytes_read() const override { return m_offset; }
187
188
477k
      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
467k
   {
202
467k
   if(m_source->end_of_data() && !m_pushed.is_set())
203
127k
      return false;
204
339k
   return true;
205
467k
   }
206
207
/*
208
* Verify that no bytes remain in the source
209
*/
210
BER_Decoder& BER_Decoder::verify_end()
211
30.2k
   {
212
30.2k
   return verify_end("BER_Decoder::verify_end called, but data remains");
213
30.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
41.6k
   {
220
41.6k
   if(!m_source->end_of_data() || m_pushed.is_set())
221
280
      throw Decoding_Error(err);
222
41.4k
   return (*this);
223
41.6k
   }
224
225
/*
226
* Discard all the bytes remaining in the source
227
*/
228
BER_Decoder& BER_Decoder::discard_remaining()
229
15.2k
   {
230
15.2k
   uint8_t buf;
231
2.58M
   while(m_source->read_byte(buf))
232
2.56M
      {}
233
15.2k
   return (*this);
234
15.2k
   }
235
236
/*
237
* Return the BER encoding of the next object
238
*/
239
BER_Object BER_Decoder::get_next_object()
240
1.37M
   {
241
1.37M
   BER_Object next;
242
243
1.37M
   if(m_pushed.is_set())
244
82.4k
      {
245
82.4k
      std::swap(next, m_pushed);
246
82.4k
      return next;
247
82.4k
      }
248
249
1.29M
   for(;;)
250
1.39M
      {
251
1.39M
      ASN1_Type type_tag;
252
1.39M
      ASN1_Class class_tag;
253
1.39M
      decode_tag(m_source, type_tag, class_tag);
254
1.39M
      next.set_tagging(type_tag, class_tag);
255
1.39M
      if(next.is_set() == false) // no more objects
256
45.5k
         return next;
257
258
1.35M
      size_t field_size;
259
1.35M
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
260
1.35M
      if(!m_source->check_available(length))
261
6.23k
         throw BER_Decoding_Error("Value truncated");
262
263
1.34M
      uint8_t* out = next.mutable_bits(length);
264
1.34M
      if(m_source->read(out, length) != length)
265
0
         throw BER_Decoding_Error("Value truncated");
266
267
1.34M
      if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc))
268
108k
         continue;
269
1.23M
      else
270
1.23M
         break;
271
1.34M
      }
272
273
1.23M
   return next;
274
1.29M
   }
275
276
/*
277
* Push a object back into the stream
278
*/
279
void BER_Decoder::push_back(const BER_Object& obj)
280
6.75k
   {
281
6.75k
   if(m_pushed.is_set())
282
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
283
6.75k
   m_pushed = obj;
284
6.75k
   }
285
286
void BER_Decoder::push_back(BER_Object&& obj)
287
101k
   {
288
101k
   if(m_pushed.is_set())
289
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
290
101k
   m_pushed = std::move(obj);
291
101k
   }
292
293
BER_Decoder BER_Decoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
294
484k
   {
295
484k
   BER_Object obj = get_next_object();
296
484k
   obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
297
484k
   return BER_Decoder(std::move(obj), this);
298
484k
   }
299
300
/*
301
* Finish decoding a CONSTRUCTED type
302
*/
303
BER_Decoder& BER_Decoder::end_cons()
304
338k
   {
305
338k
   if(!m_parent)
306
0
      throw Invalid_State("BER_Decoder::end_cons called with null parent");
307
338k
   if(!m_source->end_of_data())
308
1.18k
      throw Decoding_Error("BER_Decoder::end_cons called with data left");
309
337k
   return (*m_parent);
310
338k
   }
311
312
BER_Decoder::BER_Decoder(BER_Object&& obj, BER_Decoder* parent)
313
477k
   {
314
477k
   m_data_src.reset(new DataSource_BERObject(std::move(obj)));
315
477k
   m_source = m_data_src.get();
316
477k
   m_parent = parent;
317
477k
   }
318
319
/*
320
* BER_Decoder Constructor
321
*/
322
BER_Decoder::BER_Decoder(DataSource& src)
323
26.1k
   {
324
26.1k
   m_source = &src;
325
26.1k
   }
326
327
/*
328
* BER_Decoder Constructor
329
 */
330
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length)
331
53.6k
   {
332
53.6k
   m_data_src.reset(new DataSource_Memory(data, length));
333
53.6k
   m_source = m_data_src.get();
334
53.6k
   }
335
336
/*
337
* BER_Decoder Constructor
338
*/
339
BER_Decoder::BER_Decoder(const secure_vector<uint8_t>& data)
340
6.06k
   {
341
6.06k
   m_data_src.reset(new DataSource_Memory(data));
342
6.06k
   m_source = m_data_src.get();
343
6.06k
   }
344
345
/*
346
* BER_Decoder Constructor
347
*/
348
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data)
349
186k
   {
350
186k
   m_data_src.reset(new DataSource_Memory(data.data(), data.size()));
351
186k
   m_source = m_data_src.get();
352
186k
   }
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
519k
   {
372
519k
   obj.decode_from(*this);
373
519k
   return (*this);
374
519k
   }
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
3.15k
   {
390
3.15k
   secure_vector<uint8_t> out_vec;
391
3.15k
   decode(out_vec, ASN1_Type::OctetString);
392
3.15k
   out = BigInt::decode(out_vec.data(), out_vec.size());
393
3.15k
   return (*this);
394
3.15k
   }
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
3.82k
   {
402
3.82k
   BER_Object obj = get_next_object();
403
3.82k
   obj.assert_is_a(type_tag, class_tag);
404
405
3.82k
   if(obj.length() != 1)
406
524
      throw BER_Decoding_Error("BER boolean value had invalid size");
407
408
3.30k
   out = (obj.bits()[0]) ? true : false;
409
3.30k
   return (*this);
410
3.82k
   }
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
22.0k
   {
419
22.0k
   BigInt integer;
420
22.0k
   decode(integer, type_tag, class_tag);
421
422
22.0k
   if(integer.is_negative())
423
927
      throw BER_Decoding_Error("Decoded small integer value was negative");
424
425
21.0k
   if(integer.bits() > 32)
426
399
      throw BER_Decoding_Error("Decoded integer value larger than expected");
427
428
20.6k
   out = 0;
429
90.1k
   for(size_t i = 0; i != 4; ++i)
430
69.4k
      out = (out << 8) | integer.byte_at(3-i);
431
432
20.6k
   return (*this);
433
21.0k
   }
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.6k
   {
465
62.6k
   BER_Object obj = get_next_object();
466
62.6k
   obj.assert_is_a(type_tag, class_tag);
467
468
62.6k
   if(obj.length() == 0)
469
9.86k
      {
470
9.86k
      out.clear();
471
9.86k
      }
472
52.7k
   else
473
52.7k
      {
474
52.7k
      const bool negative = (obj.bits()[0] & 0x80) ? true : false;
475
476
52.7k
      if(negative)
477
5.29k
         {
478
5.29k
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
479
9.43k
         for(size_t i = obj.length(); i > 0; --i)
480
9.43k
            if(vec[i-1]--)
481
5.29k
               break;
482
324k
         for(size_t i = 0; i != obj.length(); ++i)
483
318k
            vec[i] = ~vec[i];
484
5.29k
         out = BigInt(vec.data(), vec.size());
485
5.29k
         out.flip_sign();
486
5.29k
         }
487
47.4k
      else
488
47.4k
         {
489
47.4k
         out = BigInt(obj.bits(), obj.length());
490
47.4k
         }
491
52.7k
      }
492
493
62.6k
   return (*this);
494
62.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
121k
   {
505
121k
   obj.assert_is_a(type_tag, class_tag);
506
507
121k
   if(real_type == ASN1_Type::OctetString)
508
75.5k
      {
509
75.5k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
510
75.5k
      }
511
45.7k
   else
512
45.7k
      {
513
45.7k
      if(obj.length() == 0)
514
392
         throw BER_Decoding_Error("Invalid BIT STRING");
515
45.3k
      if(obj.bits()[0] >= 8)
516
248
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
517
518
45.1k
      buffer.resize(obj.length() - 1);
519
520
45.1k
      if(obj.length() > 1)
521
34.7k
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
522
45.1k
      }
523
121k
   }
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
7.17k
   {
505
7.17k
   obj.assert_is_a(type_tag, class_tag);
506
507
7.17k
   if(real_type == ASN1_Type::OctetString)
508
6.14k
      {
509
6.14k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
510
6.14k
      }
511
1.03k
   else
512
1.03k
      {
513
1.03k
      if(obj.length() == 0)
514
1
         throw BER_Decoding_Error("Invalid BIT STRING");
515
1.03k
      if(obj.bits()[0] >= 8)
516
3
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
517
518
1.02k
      buffer.resize(obj.length() - 1);
519
520
1.02k
      if(obj.length() > 1)
521
916
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
522
1.02k
      }
523
7.17k
   }
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
114k
   {
505
114k
   obj.assert_is_a(type_tag, class_tag);
506
507
114k
   if(real_type == ASN1_Type::OctetString)
508
69.3k
      {
509
69.3k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
510
69.3k
      }
511
44.7k
   else
512
44.7k
      {
513
44.7k
      if(obj.length() == 0)
514
391
         throw BER_Decoding_Error("Invalid BIT STRING");
515
44.3k
      if(obj.bits()[0] >= 8)
516
245
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
517
518
44.0k
      buffer.resize(obj.length() - 1);
519
520
44.0k
      if(obj.length() > 1)
521
33.8k
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
522
44.0k
      }
523
114k
   }
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
7.19k
   {
534
7.19k
   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
7.19k
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
538
7.19k
   return (*this);
539
7.19k
   }
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
115k
   {
545
115k
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString)
546
1.43k
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
547
548
114k
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
549
114k
   return (*this);
550
115k
   }
551
552
}