Coverage Report

Created: 2019-09-11 14:12

/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/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_Tag& type_tag, ASN1_Tag& class_tag)
28
2.24M
   {
29
2.24M
   uint8_t b;
30
2.24M
   if(!ber->read_byte(b))
31
166k
      {
32
166k
      class_tag = type_tag = NO_OBJECT;
33
166k
      return 0;
34
166k
      }
35
2.07M
36
2.07M
   if((b & 0x1F) != 0x1F)
37
2.04M
      {
38
2.04M
      type_tag = ASN1_Tag(b & 0x1F);
39
2.04M
      class_tag = ASN1_Tag(b & 0xE0);
40
2.04M
      return 1;
41
2.04M
      }
42
30.3k
43
30.3k
   size_t tag_bytes = 1;
44
30.3k
   class_tag = ASN1_Tag(b & 0xE0);
45
30.3k
46
30.3k
   size_t tag_buf = 0;
47
47.8k
   while(true)
48
47.8k
      {
49
47.8k
      if(!ber->read_byte(b))
50
605
         throw BER_Decoding_Error("Long-form tag truncated");
51
47.2k
      if(tag_buf & 0xFF000000)
52
378
         throw BER_Decoding_Error("Long-form tag overflowed 32 bits");
53
46.9k
      ++tag_bytes;
54
46.9k
      tag_buf = (tag_buf << 7) | (b & 0x7F);
55
46.9k
      if((b & 0x80) == 0) break;
56
46.9k
      }
57
30.3k
   type_tag = ASN1_Tag(tag_buf);
58
29.3k
   return tag_bytes;
59
30.3k
   }
60
61
/*
62
* Find the EOC marker
63
*/
64
size_t find_eoc(DataSource* src, size_t allow_indef);
65
66
/*
67
* BER decode an ASN.1 length field
68
*/
69
size_t decode_length(DataSource* ber, size_t& field_size, size_t allow_indef)
70
2.07M
   {
71
2.07M
   uint8_t b;
72
2.07M
   if(!ber->read_byte(b))
73
1.97k
      throw BER_Decoding_Error("Length field not found");
74
2.07M
   field_size = 1;
75
2.07M
   if((b & 0x80) == 0)
76
1.74M
      return b;
77
331k
78
331k
   field_size += (b & 0x7F);
79
331k
   if(field_size > 5)
80
2.50k
      throw BER_Decoding_Error("Length field is too large");
81
329k
82
329k
   if(field_size == 1)
83
202k
      {
84
202k
      if(allow_indef == 0)
85
244
         {
86
244
         throw BER_Decoding_Error("Nested EOC markers too deep, rejecting to avoid stack exhaustion");
87
244
         }
88
202k
      else
89
202k
         {
90
202k
         return find_eoc(ber, allow_indef - 1);
91
202k
         }
92
126k
      }
93
126k
94
126k
   size_t length = 0;
95
126k
96
348k
   for(size_t i = 0; i != field_size - 1; ++i)
97
222k
      {
98
222k
      if(get_byte(0, length) != 0)
99
0
         throw BER_Decoding_Error("Field length overflow");
100
222k
      if(!ber->read_byte(b))
101
420
         throw BER_Decoding_Error("Corrupted length field");
102
221k
      length = (length << 8) | b;
103
221k
      }
104
126k
   return length;
105
126k
   }
106
107
/*
108
* Find the EOC marker
109
*/
110
size_t find_eoc(DataSource* ber, size_t allow_indef)
111
202k
   {
112
202k
   secure_vector<uint8_t> buffer(BOTAN_DEFAULT_BUFFER_SIZE), data;
113
202k
114
450k
   while(true)
115
450k
      {
116
450k
      const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
117
450k
      if(got == 0)
118
202k
         break;
119
247k
120
247k
      data += std::make_pair(buffer.data(), got);
121
247k
      }
122
202k
123
202k
   DataSource_Memory source(data);
124
202k
   data.clear();
125
202k
126
202k
   size_t length = 0;
127
854k
   while(true)
128
848k
      {
129
848k
      ASN1_Tag type_tag, class_tag;
130
848k
      size_t tag_size = decode_tag(&source, type_tag, class_tag);
131
848k
      if(type_tag == NO_OBJECT)
132
122k
         break;
133
726k
134
726k
      size_t length_size = 0;
135
726k
      size_t item_size = decode_length(&source, length_size, allow_indef);
136
726k
      source.discard_next(item_size);
137
726k
138
726k
      length = BOTAN_CHECKED_ADD(length, item_size);
139
726k
      length = BOTAN_CHECKED_ADD(length, tag_size);
140
726k
      length = BOTAN_CHECKED_ADD(length, length_size);
141
726k
142
726k
      if(type_tag == EOC && class_tag == UNIVERSAL)
143
74.4k
         break;
144
726k
      }
145
202k
   return length;
146
202k
   }
147
148
class DataSource_BERObject final : public DataSource
149
   {
150
   public:
151
      size_t read(uint8_t out[], size_t length) override
152
14.6M
         {
153
14.6M
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
154
14.6M
         const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
155
14.6M
         copy_mem(out, m_obj.bits() + m_offset, got);
156
14.6M
         m_offset += got;
157
14.6M
         return got;
158
14.6M
         }
159
160
      size_t peek(uint8_t out[], size_t length, size_t peek_offset) const override
161
39.2k
         {
162
39.2k
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
163
39.2k
         const size_t bytes_left = m_obj.length() - m_offset;
164
39.2k
165
39.2k
         if(peek_offset >= bytes_left)
166
19.0k
            return 0;
167
20.2k
168
20.2k
         const size_t got = std::min(bytes_left - peek_offset, length);
169
20.2k
         copy_mem(out, m_obj.bits() + peek_offset, got);
170
20.2k
         return got;
171
20.2k
         }
172
173
      bool check_available(size_t n) override
174
845k
         {
175
845k
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
176
845k
         return (n <= (m_obj.length() - m_offset));
177
845k
         }
178
179
      bool end_of_data() const override
180
828k
         {
181
828k
         return get_bytes_read() == m_obj.length();
182
828k
         }
183
184
828k
      size_t get_bytes_read() const override { return m_offset; }
185
186
515k
      explicit DataSource_BERObject(BER_Object&& obj) : m_obj(std::move(obj)), m_offset(0) {}
187
188
   private:
189
      BER_Object m_obj;
190
      size_t m_offset;
191
   };
192
193
}
194
195
/*
196
* Check if more objects are there
197
*/
198
bool BER_Decoder::more_items() const
199
501k
   {
200
501k
   if(m_source->end_of_data() && !m_pushed.is_set())
201
166k
      return false;
202
335k
   return true;
203
335k
   }
204
205
/*
206
* Verify that no bytes remain in the source
207
*/
208
BER_Decoder& BER_Decoder::verify_end()
209
29.9k
   {
210
29.9k
   return verify_end("BER_Decoder::verify_end called, but data remains");
211
29.9k
   }
212
213
/*
214
* Verify that no bytes remain in the source
215
*/
216
BER_Decoder& BER_Decoder::verify_end(const std::string& err)
217
149k
   {
218
149k
   if(!m_source->end_of_data() || m_pushed.is_set())
219
332
      throw Decoding_Error(err);
220
149k
   return (*this);
221
149k
   }
222
223
/*
224
* Discard all the bytes remaining in the source
225
*/
226
BER_Decoder& BER_Decoder::discard_remaining()
227
16.1k
   {
228
16.1k
   uint8_t buf;
229
2.49M
   while(m_source->read_byte(buf))
230
2.48M
      {}
231
16.1k
   return (*this);
232
16.1k
   }
233
234
/*
235
* Return the BER encoding of the next object
236
*/
237
BER_Object BER_Decoder::get_next_object()
238
1.38M
   {
239
1.38M
   BER_Object next;
240
1.38M
241
1.38M
   if(m_pushed.is_set())
242
70.7k
      {
243
70.7k
      std::swap(next, m_pushed);
244
70.7k
      return next;
245
70.7k
      }
246
1.31M
247
1.31M
   for(;;)
248
1.39M
      {
249
1.39M
      ASN1_Tag type_tag, class_tag;
250
1.39M
      decode_tag(m_source, type_tag, class_tag);
251
1.39M
      next.set_tagging(type_tag, class_tag);
252
1.39M
      if(next.is_set() == false) // no more objects
253
44.5k
         return next;
254
1.35M
255
1.35M
      size_t field_size;
256
1.35M
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
257
1.35M
      if(!m_source->check_available(length))
258
5.49k
         throw BER_Decoding_Error("Value truncated");
259
1.34M
260
1.34M
      uint8_t* out = next.mutable_bits(length);
261
1.34M
      if(m_source->read(out, length) != length)
262
0
         throw BER_Decoding_Error("Value truncated");
263
1.34M
264
1.34M
      if(next.tagging() == EOC)
265
77.9k
         continue;
266
1.26M
      else
267
1.26M
         break;
268
1.34M
      }
269
1.31M
270
1.31M
   return next;
271
1.31M
   }
272
273
/*
274
* Push a object back into the stream
275
*/
276
void BER_Decoder::push_back(const BER_Object& obj)
277
6.14k
   {
278
6.14k
   if(m_pushed.is_set())
279
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
280
6.14k
   m_pushed = obj;
281
6.14k
   }
282
283
void BER_Decoder::push_back(BER_Object&& obj)
284
89.2k
   {
285
89.2k
   if(m_pushed.is_set())
286
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
287
89.2k
   m_pushed = std::move(obj);
288
89.2k
   }
289
290
BER_Decoder BER_Decoder::start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag)
291
516k
   {
292
516k
   BER_Object obj = get_next_object();
293
516k
   obj.assert_is_a(type_tag, ASN1_Tag(class_tag | CONSTRUCTED));
294
516k
   return BER_Decoder(std::move(obj), this);
295
516k
   }
296
297
/*
298
* Finish decoding a CONSTRUCTED type
299
*/
300
BER_Decoder& BER_Decoder::end_cons()
301
351k
   {
302
351k
   if(!m_parent)
303
0
      throw Invalid_State("BER_Decoder::end_cons called with null parent");
304
351k
   if(!m_source->end_of_data())
305
618
      throw Decoding_Error("BER_Decoder::end_cons called with data left");
306
350k
   return (*m_parent);
307
350k
   }
308
309
BER_Decoder::BER_Decoder(BER_Object&& obj, BER_Decoder* parent)
310
515k
   {
311
515k
   m_data_src.reset(new DataSource_BERObject(std::move(obj)));
312
515k
   m_source = m_data_src.get();
313
515k
   m_parent = parent;
314
515k
   }
315
316
/*
317
* BER_Decoder Constructor
318
*/
319
BER_Decoder::BER_Decoder(DataSource& src)
320
29.6k
   {
321
29.6k
   m_source = &src;
322
29.6k
   }
323
324
/*
325
* BER_Decoder Constructor
326
 */
327
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length)
328
50.9k
   {
329
50.9k
   m_data_src.reset(new DataSource_Memory(data, length));
330
50.9k
   m_source = m_data_src.get();
331
50.9k
   }
332
333
/*
334
* BER_Decoder Constructor
335
*/
336
BER_Decoder::BER_Decoder(const secure_vector<uint8_t>& data)
337
4.17k
   {
338
4.17k
   m_data_src.reset(new DataSource_Memory(data));
339
4.17k
   m_source = m_data_src.get();
340
4.17k
   }
341
342
/*
343
* BER_Decoder Constructor
344
*/
345
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data)
346
195k
   {
347
195k
   m_data_src.reset(new DataSource_Memory(data.data(), data.size()));
348
195k
   m_source = m_data_src.get();
349
195k
   }
350
351
/*
352
* BER_Decoder Copy Constructor
353
*/
354
BER_Decoder::BER_Decoder(const BER_Decoder& other)
355
0
   {
356
0
   m_source = other.m_source;
357
0
358
0
   // take ownership
359
0
   std::swap(m_data_src, other.m_data_src);
360
0
   m_parent = other.m_parent;
361
0
   }
362
363
/*
364
* Request for an object to decode itself
365
*/
366
BER_Decoder& BER_Decoder::decode(ASN1_Object& obj,
367
                                 ASN1_Tag, ASN1_Tag)
368
555k
   {
369
555k
   obj.decode_from(*this);
370
555k
   return (*this);
371
555k
   }
372
373
/*
374
* Decode a BER encoded NULL
375
*/
376
BER_Decoder& BER_Decoder::decode_null()
377
0
   {
378
0
   BER_Object obj = get_next_object();
379
0
   obj.assert_is_a(NULL_TAG, UNIVERSAL);
380
0
   if(obj.length() > 0)
381
0
      throw BER_Decoding_Error("NULL object had nonzero size");
382
0
   return (*this);
383
0
   }
384
385
BER_Decoder& BER_Decoder::decode_octet_string_bigint(BigInt& out)
386
1.77k
   {
387
1.77k
   secure_vector<uint8_t> out_vec;
388
1.77k
   decode(out_vec, OCTET_STRING);
389
1.77k
   out = BigInt::decode(out_vec.data(), out_vec.size());
390
1.77k
   return (*this);
391
1.77k
   }
392
393
/*
394
* Decode a BER encoded BOOLEAN
395
*/
396
BER_Decoder& BER_Decoder::decode(bool& out,
397
                                 ASN1_Tag type_tag, ASN1_Tag class_tag)
398
4.58k
   {
399
4.58k
   BER_Object obj = get_next_object();
400
4.58k
   obj.assert_is_a(type_tag, class_tag);
401
4.58k
402
4.58k
   if(obj.length() != 1)
403
314
      throw BER_Decoding_Error("BER boolean value had invalid size");
404
4.26k
405
4.26k
   out = (obj.bits()[0]) ? true : false;
406
4.26k
   return (*this);
407
4.26k
   }
408
409
/*
410
* Decode a small BER encoded INTEGER
411
*/
412
BER_Decoder& BER_Decoder::decode(size_t& out,
413
                                 ASN1_Tag type_tag,
414
                                 ASN1_Tag class_tag)
415
19.6k
   {
416
19.6k
   BigInt integer;
417
19.6k
   decode(integer, type_tag, class_tag);
418
19.6k
419
19.6k
   if(integer.is_negative())
420
350
      throw BER_Decoding_Error("Decoded small integer value was negative");
421
19.2k
422
19.2k
   if(integer.bits() > 32)
423
178
      throw BER_Decoding_Error("Decoded integer value larger than expected");
424
19.1k
425
19.1k
   out = 0;
426
85.9k
   for(size_t i = 0; i != 4; ++i)
427
66.8k
      out = (out << 8) | integer.byte_at(3-i);
428
19.1k
429
19.1k
   return (*this);
430
19.1k
   }
431
432
/*
433
* Decode a small BER encoded INTEGER
434
*/
435
uint64_t BER_Decoder::decode_constrained_integer(ASN1_Tag type_tag,
436
                                                 ASN1_Tag class_tag,
437
                                                 size_t T_bytes)
438
0
   {
439
0
   if(T_bytes > 8)
440
0
      throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
441
0
442
0
   BigInt integer;
443
0
   decode(integer, type_tag, class_tag);
444
0
445
0
   if(integer.bits() > 8*T_bytes)
446
0
      throw BER_Decoding_Error("Decoded integer value larger than expected");
447
0
448
0
   uint64_t out = 0;
449
0
   for(size_t i = 0; i != 8; ++i)
450
0
      out = (out << 8) | integer.byte_at(7-i);
451
0
452
0
   return out;
453
0
   }
454
455
/*
456
* Decode a BER encoded INTEGER
457
*/
458
BER_Decoder& BER_Decoder::decode(BigInt& out,
459
                                 ASN1_Tag type_tag,
460
                                 ASN1_Tag class_tag)
461
68.6k
   {
462
68.6k
   BER_Object obj = get_next_object();
463
68.6k
   obj.assert_is_a(type_tag, class_tag);
464
68.6k
465
68.6k
   if(obj.length() == 0)
466
6.51k
      {
467
6.51k
      out = 0;
468
6.51k
      }
469
62.1k
   else
470
62.1k
      {
471
62.1k
      const bool negative = (obj.bits()[0] & 0x80) ? true : false;
472
62.1k
473
62.1k
      if(negative)
474
4.35k
         {
475
4.35k
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
476
8.61k
         for(size_t i = obj.length(); i > 0; --i)
477
8.61k
            if(vec[i-1]--)
478
4.35k
               break;
479
227k
         for(size_t i = 0; i != obj.length(); ++i)
480
222k
            vec[i] = ~vec[i];
481
4.35k
         out = BigInt(vec.data(), vec.size());
482
4.35k
         out.flip_sign();
483
4.35k
         }
484
57.7k
      else
485
57.7k
         {
486
57.7k
         out = BigInt(obj.bits(), obj.length());
487
57.7k
         }
488
62.1k
      }
489
68.6k
490
68.6k
   return (*this);
491
68.6k
   }
492
493
namespace {
494
495
template<typename Alloc>
496
void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer,
497
                               const BER_Object& obj,
498
                               ASN1_Tag real_type,
499
                               ASN1_Tag type_tag,
500
                               ASN1_Tag class_tag)
501
108k
   {
502
108k
   obj.assert_is_a(type_tag, class_tag);
503
108k
504
108k
   if(real_type == OCTET_STRING)
505
60.2k
      {
506
60.2k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
507
60.2k
      }
508
47.7k
   else
509
47.7k
      {
510
47.7k
      if(obj.length() == 0)
511
313
         throw BER_Decoding_Error("Invalid BIT STRING");
512
47.4k
      if(obj.bits()[0] >= 8)
513
246
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
514
47.2k
515
47.2k
      buffer.resize(obj.length() - 1);
516
47.2k
517
47.2k
      if(obj.length() > 1)
518
40.6k
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
519
47.2k
      }
520
108k
   }
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_Tag, Botan::ASN1_Tag, Botan::ASN1_Tag)
Line
Count
Source
501
4.56k
   {
502
4.56k
   obj.assert_is_a(type_tag, class_tag);
503
4.56k
504
4.56k
   if(real_type == OCTET_STRING)
505
3.71k
      {
506
3.71k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
507
3.71k
      }
508
856
   else
509
856
      {
510
856
      if(obj.length() == 0)
511
2
         throw BER_Decoding_Error("Invalid BIT STRING");
512
854
      if(obj.bits()[0] >= 8)
513
6
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
514
848
515
848
      buffer.resize(obj.length() - 1);
516
848
517
848
      if(obj.length() > 1)
518
698
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
519
848
      }
520
4.56k
   }
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_Tag, Botan::ASN1_Tag, Botan::ASN1_Tag)
Line
Count
Source
501
103k
   {
502
103k
   obj.assert_is_a(type_tag, class_tag);
503
103k
504
103k
   if(real_type == OCTET_STRING)
505
56.5k
      {
506
56.5k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
507
56.5k
      }
508
46.9k
   else
509
46.9k
      {
510
46.9k
      if(obj.length() == 0)
511
311
         throw BER_Decoding_Error("Invalid BIT STRING");
512
46.6k
      if(obj.bits()[0] >= 8)
513
240
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
514
46.3k
515
46.3k
      buffer.resize(obj.length() - 1);
516
46.3k
517
46.3k
      if(obj.length() > 1)
518
39.9k
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
519
46.3k
      }
520
103k
   }
521
522
}
523
524
/*
525
* BER decode a BIT STRING or OCTET STRING
526
*/
527
BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& buffer,
528
                                 ASN1_Tag real_type,
529
                                 ASN1_Tag type_tag, ASN1_Tag class_tag)
530
4.57k
   {
531
4.57k
   if(real_type != OCTET_STRING && real_type != BIT_STRING)
532
0
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type);
533
4.57k
534
4.57k
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
535
4.57k
   return (*this);
536
4.57k
   }
537
538
BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& buffer,
539
                                 ASN1_Tag real_type,
540
                                 ASN1_Tag type_tag, ASN1_Tag class_tag)
541
104k
   {
542
104k
   if(real_type != OCTET_STRING && real_type != BIT_STRING)
543
1.23k
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type);
544
103k
545
103k
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
546
103k
   return (*this);
547
103k
   }
548
549
}