Coverage Report

Created: 2020-02-14 15:38

/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.30M
   {
29
2.30M
   uint8_t b;
30
2.30M
   if(!ber->read_byte(b))
31
172k
      {
32
172k
      class_tag = type_tag = NO_OBJECT;
33
172k
      return 0;
34
172k
      }
35
2.12M
36
2.12M
   if((b & 0x1F) != 0x1F)
37
2.10M
      {
38
2.10M
      type_tag = ASN1_Tag(b & 0x1F);
39
2.10M
      class_tag = ASN1_Tag(b & 0xE0);
40
2.10M
      return 1;
41
2.10M
      }
42
28.8k
43
28.8k
   size_t tag_bytes = 1;
44
28.8k
   class_tag = ASN1_Tag(b & 0xE0);
45
28.8k
46
28.8k
   size_t tag_buf = 0;
47
47.1k
   while(true)
48
47.1k
      {
49
47.1k
      if(!ber->read_byte(b))
50
671
         throw BER_Decoding_Error("Long-form tag truncated");
51
46.4k
      if(tag_buf & 0xFF000000)
52
359
         throw BER_Decoding_Error("Long-form tag overflowed 32 bits");
53
46.1k
      ++tag_bytes;
54
46.1k
      tag_buf = (tag_buf << 7) | (b & 0x7F);
55
46.1k
      if((b & 0x80) == 0) break;
56
46.1k
      }
57
28.8k
   type_tag = ASN1_Tag(tag_buf);
58
27.7k
   return tag_bytes;
59
28.8k
   }
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.12M
   {
71
2.12M
   uint8_t b;
72
2.12M
   if(!ber->read_byte(b))
73
1.97k
      throw BER_Decoding_Error("Length field not found");
74
2.12M
   field_size = 1;
75
2.12M
   if((b & 0x80) == 0)
76
1.79M
      return b;
77
327k
78
327k
   field_size += (b & 0x7F);
79
327k
   if(field_size > 5)
80
2.75k
      throw BER_Decoding_Error("Length field is too large");
81
324k
82
324k
   if(field_size == 1)
83
199k
      {
84
199k
      if(allow_indef == 0)
85
237
         {
86
237
         throw BER_Decoding_Error("Nested EOC markers too deep, rejecting to avoid stack exhaustion");
87
237
         }
88
199k
      else
89
199k
         {
90
199k
         return find_eoc(ber, allow_indef - 1);
91
199k
         }
92
125k
      }
93
125k
94
125k
   size_t length = 0;
95
125k
96
348k
   for(size_t i = 0; i != field_size - 1; ++i)
97
223k
      {
98
223k
      if(get_byte(0, length) != 0)
99
0
         throw BER_Decoding_Error("Field length overflow");
100
223k
      if(!ber->read_byte(b))
101
383
         throw BER_Decoding_Error("Corrupted length field");
102
222k
      length = (length << 8) | b;
103
222k
      }
104
125k
   return length;
105
125k
   }
106
107
/*
108
* Find the EOC marker
109
*/
110
size_t find_eoc(DataSource* ber, size_t allow_indef)
111
199k
   {
112
199k
   secure_vector<uint8_t> buffer(BOTAN_DEFAULT_BUFFER_SIZE), data;
113
199k
114
438k
   while(true)
115
438k
      {
116
438k
      const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
117
438k
      if(got == 0)
118
199k
         break;
119
239k
120
239k
      data += std::make_pair(buffer.data(), got);
121
239k
      }
122
199k
123
199k
   DataSource_Memory source(data);
124
199k
   data.clear();
125
199k
126
199k
   size_t length = 0;
127
900k
   while(true)
128
894k
      {
129
894k
      ASN1_Tag type_tag, class_tag;
130
894k
      size_t tag_size = decode_tag(&source, type_tag, class_tag);
131
894k
      if(type_tag == NO_OBJECT)
132
127k
         break;
133
767k
134
767k
      size_t length_size = 0;
135
767k
      size_t item_size = decode_length(&source, length_size, allow_indef);
136
767k
      source.discard_next(item_size);
137
767k
138
767k
      length = BOTAN_CHECKED_ADD(length, item_size);
139
767k
      length = BOTAN_CHECKED_ADD(length, tag_size);
140
767k
      length = BOTAN_CHECKED_ADD(length, length_size);
141
767k
142
767k
      if(type_tag == EOC && class_tag == UNIVERSAL)
143
66.0k
         break;
144
767k
      }
145
199k
   return length;
146
199k
   }
147
148
class DataSource_BERObject final : public DataSource
149
   {
150
   public:
151
      size_t read(uint8_t out[], size_t length) override
152
14.8M
         {
153
14.8M
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
154
14.8M
         const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
155
14.8M
         copy_mem(out, m_obj.bits() + m_offset, got);
156
14.8M
         m_offset += got;
157
14.8M
         return got;
158
14.8M
         }
159
160
      size_t peek(uint8_t out[], size_t length, size_t peek_offset) const override
161
39.7k
         {
162
39.7k
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
163
39.7k
         const size_t bytes_left = m_obj.length() - m_offset;
164
39.7k
165
39.7k
         if(peek_offset >= bytes_left)
166
19.0k
            return 0;
167
20.6k
168
20.6k
         const size_t got = std::min(bytes_left - peek_offset, length);
169
20.6k
         copy_mem(out, m_obj.bits() + peek_offset, got);
170
20.6k
         return got;
171
20.6k
         }
172
173
      bool check_available(size_t n) override
174
856k
         {
175
856k
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
176
856k
         return (n <= (m_obj.length() - m_offset));
177
856k
         }
178
179
      bool end_of_data() const override
180
843k
         {
181
843k
         return get_bytes_read() == m_obj.length();
182
843k
         }
183
184
843k
      size_t get_bytes_read() const override { return m_offset; }
185
186
523k
      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
511k
   {
200
511k
   if(m_source->end_of_data() && !m_pushed.is_set())
201
169k
      return false;
202
341k
   return true;
203
341k
   }
204
205
/*
206
* Verify that no bytes remain in the source
207
*/
208
BER_Decoder& BER_Decoder::verify_end()
209
28.4k
   {
210
28.4k
   return verify_end("BER_Decoder::verify_end called, but data remains");
211
28.4k
   }
212
213
/*
214
* Verify that no bytes remain in the source
215
*/
216
BER_Decoder& BER_Decoder::verify_end(const std::string& err)
217
152k
   {
218
152k
   if(!m_source->end_of_data() || m_pushed.is_set())
219
319
      throw Decoding_Error(err);
220
151k
   return (*this);
221
151k
   }
222
223
/*
224
* Discard all the bytes remaining in the source
225
*/
226
BER_Decoder& BER_Decoder::discard_remaining()
227
16.2k
   {
228
16.2k
   uint8_t buf;
229
2.56M
   while(m_source->read_byte(buf))
230
2.55M
      {}
231
16.2k
   return (*this);
232
16.2k
   }
233
234
/*
235
* Return the BER encoding of the next object
236
*/
237
BER_Object BER_Decoder::get_next_object()
238
1.39M
   {
239
1.39M
   BER_Object next;
240
1.39M
241
1.39M
   if(m_pushed.is_set())
242
70.3k
      {
243
70.3k
      std::swap(next, m_pushed);
244
70.3k
      return next;
245
70.3k
      }
246
1.32M
247
1.32M
   for(;;)
248
1.40M
      {
249
1.40M
      ASN1_Tag type_tag, class_tag;
250
1.40M
      decode_tag(m_source, type_tag, class_tag);
251
1.40M
      next.set_tagging(type_tag, class_tag);
252
1.40M
      if(next.is_set() == false) // no more objects
253
45.3k
         return next;
254
1.36M
255
1.36M
      size_t field_size;
256
1.36M
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
257
1.36M
      if(!m_source->check_available(length))
258
5.40k
         throw BER_Decoding_Error("Value truncated");
259
1.35M
260
1.35M
      uint8_t* out = next.mutable_bits(length);
261
1.35M
      if(m_source->read(out, length) != length)
262
0
         throw BER_Decoding_Error("Value truncated");
263
1.35M
264
1.35M
      if(next.tagging() == EOC)
265
80.0k
         continue;
266
1.27M
      else
267
1.27M
         break;
268
1.35M
      }
269
1.32M
270
1.32M
   return next;
271
1.32M
   }
272
273
/*
274
* Push a object back into the stream
275
*/
276
void BER_Decoder::push_back(const BER_Object& obj)
277
6.20k
   {
278
6.20k
   if(m_pushed.is_set())
279
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
280
6.20k
   m_pushed = obj;
281
6.20k
   }
282
283
void BER_Decoder::push_back(BER_Object&& obj)
284
88.9k
   {
285
88.9k
   if(m_pushed.is_set())
286
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
287
88.9k
   m_pushed = std::move(obj);
288
88.9k
   }
289
290
BER_Decoder BER_Decoder::start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag)
291
525k
   {
292
525k
   BER_Object obj = get_next_object();
293
525k
   obj.assert_is_a(type_tag, ASN1_Tag(class_tag | CONSTRUCTED));
294
525k
   return BER_Decoder(std::move(obj), this);
295
525k
   }
296
297
/*
298
* Finish decoding a CONSTRUCTED type
299
*/
300
BER_Decoder& BER_Decoder::end_cons()
301
356k
   {
302
356k
   if(!m_parent)
303
0
      throw Invalid_State("BER_Decoder::end_cons called with null parent");
304
356k
   if(!m_source->end_of_data())
305
610
      throw Decoding_Error("BER_Decoder::end_cons called with data left");
306
355k
   return (*m_parent);
307
355k
   }
308
309
BER_Decoder::BER_Decoder(BER_Object&& obj, BER_Decoder* parent)
310
523k
   {
311
523k
   m_data_src.reset(new DataSource_BERObject(std::move(obj)));
312
523k
   m_source = m_data_src.get();
313
523k
   m_parent = parent;
314
523k
   }
315
316
/*
317
* BER_Decoder Constructor
318
*/
319
BER_Decoder::BER_Decoder(DataSource& src)
320
29.7k
   {
321
29.7k
   m_source = &src;
322
29.7k
   }
323
324
/*
325
* BER_Decoder Constructor
326
 */
327
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length)
328
49.0k
   {
329
49.0k
   m_data_src.reset(new DataSource_Memory(data, length));
330
49.0k
   m_source = m_data_src.get();
331
49.0k
   }
332
333
/*
334
* BER_Decoder Constructor
335
*/
336
BER_Decoder::BER_Decoder(const secure_vector<uint8_t>& data)
337
4.34k
   {
338
4.34k
   m_data_src.reset(new DataSource_Memory(data));
339
4.34k
   m_source = m_data_src.get();
340
4.34k
   }
341
342
/*
343
* BER_Decoder Constructor
344
*/
345
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data)
346
191k
   {
347
191k
   m_data_src.reset(new DataSource_Memory(data.data(), data.size()));
348
191k
   m_source = m_data_src.get();
349
191k
   }
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
563k
   {
369
563k
   obj.decode_from(*this);
370
563k
   return (*this);
371
563k
   }
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.76k
   {
387
1.76k
   secure_vector<uint8_t> out_vec;
388
1.76k
   decode(out_vec, OCTET_STRING);
389
1.76k
   out = BigInt::decode(out_vec.data(), out_vec.size());
390
1.76k
   return (*this);
391
1.76k
   }
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.80k
   {
399
4.80k
   BER_Object obj = get_next_object();
400
4.80k
   obj.assert_is_a(type_tag, class_tag);
401
4.80k
402
4.80k
   if(obj.length() != 1)
403
335
      throw BER_Decoding_Error("BER boolean value had invalid size");
404
4.46k
405
4.46k
   out = (obj.bits()[0]) ? true : false;
406
4.46k
   return (*this);
407
4.46k
   }
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.2k
   {
416
19.2k
   BigInt integer;
417
19.2k
   decode(integer, type_tag, class_tag);
418
19.2k
419
19.2k
   if(integer.is_negative())
420
298
      throw BER_Decoding_Error("Decoded small integer value was negative");
421
18.9k
422
18.9k
   if(integer.bits() > 32)
423
203
      throw BER_Decoding_Error("Decoded integer value larger than expected");
424
18.7k
425
18.7k
   out = 0;
426
85.0k
   for(size_t i = 0; i != 4; ++i)
427
66.3k
      out = (out << 8) | integer.byte_at(3-i);
428
18.7k
429
18.7k
   return (*this);
430
18.7k
   }
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
67.7k
   {
462
67.7k
   BER_Object obj = get_next_object();
463
67.7k
   obj.assert_is_a(type_tag, class_tag);
464
67.7k
465
67.7k
   if(obj.length() == 0)
466
7.10k
      {
467
7.10k
      out = 0;
468
7.10k
      }
469
60.6k
   else
470
60.6k
      {
471
60.6k
      const bool negative = (obj.bits()[0] & 0x80) ? true : false;
472
60.6k
473
60.6k
      if(negative)
474
3.94k
         {
475
3.94k
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
476
7.72k
         for(size_t i = obj.length(); i > 0; --i)
477
7.72k
            if(vec[i-1]--)
478
3.94k
               break;
479
246k
         for(size_t i = 0; i != obj.length(); ++i)
480
242k
            vec[i] = ~vec[i];
481
3.94k
         out = BigInt(vec.data(), vec.size());
482
3.94k
         out.flip_sign();
483
3.94k
         }
484
56.7k
      else
485
56.7k
         {
486
56.7k
         out = BigInt(obj.bits(), obj.length());
487
56.7k
         }
488
60.6k
      }
489
67.7k
490
67.7k
   return (*this);
491
67.7k
   }
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
59.6k
      {
506
59.6k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
507
59.6k
      }
508
48.4k
   else
509
48.4k
      {
510
48.4k
      if(obj.length() == 0)
511
324
         throw BER_Decoding_Error("Invalid BIT STRING");
512
48.0k
      if(obj.bits()[0] >= 8)
513
259
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
514
47.8k
515
47.8k
      buffer.resize(obj.length() - 1);
516
47.8k
517
47.8k
      if(obj.length() > 1)
518
40.5k
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
519
47.8k
      }
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.58k
   {
502
4.58k
   obj.assert_is_a(type_tag, class_tag);
503
4.58k
504
4.58k
   if(real_type == OCTET_STRING)
505
3.74k
      {
506
3.74k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
507
3.74k
      }
508
840
   else
509
840
      {
510
840
      if(obj.length() == 0)
511
1
         throw BER_Decoding_Error("Invalid BIT STRING");
512
839
      if(obj.bits()[0] >= 8)
513
6
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
514
833
515
833
      buffer.resize(obj.length() - 1);
516
833
517
833
      if(obj.length() > 1)
518
692
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
519
833
      }
520
4.58k
   }
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
55.9k
      {
506
55.9k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
507
55.9k
      }
508
47.5k
   else
509
47.5k
      {
510
47.5k
      if(obj.length() == 0)
511
323
         throw BER_Decoding_Error("Invalid BIT STRING");
512
47.2k
      if(obj.bits()[0] >= 8)
513
253
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
514
46.9k
515
46.9k
      buffer.resize(obj.length() - 1);
516
46.9k
517
46.9k
      if(obj.length() > 1)
518
39.8k
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
519
46.9k
      }
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.59k
   {
531
4.59k
   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.59k
534
4.59k
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
535
4.59k
   return (*this);
536
4.59k
   }
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
105k
   {
542
105k
   if(real_type != OCTET_STRING && real_type != BIT_STRING)
543
1.19k
      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
}