Coverage Report

Created: 2022-05-14 06:06

/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.73M
   {
29
2.73M
   uint8_t b;
30
2.73M
   if(!ber->read_byte(b))
31
247k
      {
32
247k
      type_tag = ASN1_Type::NoObject;
33
247k
      class_tag = ASN1_Class::NoObject;
34
247k
      return 0;
35
247k
      }
36
37
2.48M
   if((b & 0x1F) != 0x1F)
38
2.45M
      {
39
2.45M
      type_tag = ASN1_Type(b & 0x1F);
40
2.45M
      class_tag = ASN1_Class(b & 0xE0);
41
2.45M
      return 1;
42
2.45M
      }
43
44
28.7k
   size_t tag_bytes = 1;
45
28.7k
   class_tag = ASN1_Class(b & 0xE0);
46
47
28.7k
   size_t tag_buf = 0;
48
295k
   while(true)
49
295k
      {
50
295k
      if(!ber->read_byte(b))
51
951
         throw BER_Decoding_Error("Long-form tag truncated");
52
294k
      if(tag_buf & 0xFF000000)
53
400
         throw BER_Decoding_Error("Long-form tag overflowed 32 bits");
54
293k
      ++tag_bytes;
55
293k
      tag_buf = (tag_buf << 7) | (b & 0x7F);
56
293k
      if((b & 0x80) == 0) break;
57
293k
      }
58
27.4k
   type_tag = ASN1_Type(tag_buf);
59
27.4k
   return tag_bytes;
60
28.7k
   }
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.48M
   {
72
2.48M
   uint8_t b;
73
2.48M
   if(!ber->read_byte(b))
74
2.57k
      throw BER_Decoding_Error("Length field not found");
75
2.47M
   field_size = 1;
76
2.47M
   if((b & 0x80) == 0)
77
2.03M
      return b;
78
79
438k
   field_size += (b & 0x7F);
80
438k
   if(field_size > 5)
81
2.46k
      throw BER_Decoding_Error("Length field is too large");
82
83
436k
   if(field_size == 1)
84
284k
      {
85
284k
      if(allow_indef == 0)
86
251
         {
87
251
         throw BER_Decoding_Error("Nested EOC markers too deep, rejecting to avoid stack exhaustion");
88
251
         }
89
284k
      else
90
284k
         {
91
284k
         return find_eoc(ber, allow_indef - 1);
92
284k
         }
93
284k
      }
94
95
151k
   size_t length = 0;
96
97
425k
   for(size_t i = 0; i != field_size - 1; ++i)
98
274k
      {
99
274k
      if(get_byte<0>(length) != 0)
100
0
         throw BER_Decoding_Error("Field length overflow");
101
274k
      if(!ber->read_byte(b))
102
609
         throw BER_Decoding_Error("Corrupted length field");
103
273k
      length = (length << 8) | b;
104
273k
      }
105
151k
   return length;
106
151k
   }
107
108
/*
109
* Find the EOC marker
110
*/
111
size_t find_eoc(DataSource* ber, size_t allow_indef)
112
284k
   {
113
284k
   secure_vector<uint8_t> buffer(BOTAN_DEFAULT_BUFFER_SIZE), data;
114
115
694k
   while(true)
116
694k
      {
117
694k
      const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
118
694k
      if(got == 0)
119
284k
         break;
120
121
410k
      data += std::make_pair(buffer.data(), got);
122
410k
      }
123
124
284k
   DataSource_Memory source(data);
125
284k
   data.clear();
126
127
284k
   size_t length = 0;
128
1.33M
   while(true)
129
1.33M
      {
130
1.33M
      ASN1_Type type_tag;
131
1.33M
      ASN1_Class class_tag;
132
1.33M
      size_t tag_size = decode_tag(&source, type_tag, class_tag);
133
1.33M
      if(type_tag == ASN1_Type::NoObject)
134
194k
         break;
135
136
1.13M
      size_t length_size = 0;
137
1.13M
      size_t item_size = decode_length(&source, length_size, allow_indef);
138
1.13M
      source.discard_next(item_size);
139
140
1.13M
      length = BOTAN_CHECKED_ADD(length, item_size);
141
1.13M
      length = BOTAN_CHECKED_ADD(length, tag_size);
142
1.13M
      length = BOTAN_CHECKED_ADD(length, length_size);
143
144
1.13M
      if(type_tag == ASN1_Type::Eoc && class_tag == ASN1_Class::Universal)
145
83.7k
         break;
146
1.13M
      }
147
284k
   return length;
148
284k
   }
149
150
class DataSource_BERObject final : public DataSource
151
   {
152
   public:
153
      size_t read(uint8_t out[], size_t length) override
154
15.6M
         {
155
15.6M
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
156
15.6M
         const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
157
15.6M
         copy_mem(out, m_obj.bits() + m_offset, got);
158
15.6M
         m_offset += got;
159
15.6M
         return got;
160
15.6M
         }
161
162
      size_t peek(uint8_t out[], size_t length, size_t peek_offset) const override
163
93.8k
         {
164
93.8k
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
165
93.8k
         const size_t bytes_left = m_obj.length() - m_offset;
166
167
93.8k
         if(peek_offset >= bytes_left)
168
40.0k
            return 0;
169
170
53.8k
         const size_t got = std::min(bytes_left - peek_offset, length);
171
53.8k
         copy_mem(out, m_obj.bits() + peek_offset, got);
172
53.8k
         return got;
173
93.8k
         }
174
175
      bool check_available(size_t n) override
176
865k
         {
177
865k
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
178
865k
         return (n <= (m_obj.length() - m_offset));
179
865k
         }
180
181
      bool end_of_data() const override
182
678k
         {
183
678k
         return get_bytes_read() == m_obj.length();
184
678k
         }
185
186
678k
      size_t get_bytes_read() const override { return m_offset; }
187
188
478k
      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
442k
   {
202
442k
   if(m_source->end_of_data() && !m_pushed.is_set())
203
123k
      return false;
204
318k
   return true;
205
442k
   }
206
207
/*
208
* Verify that no bytes remain in the source
209
*/
210
BER_Decoder& BER_Decoder::verify_end()
211
26.7k
   {
212
26.7k
   return verify_end("BER_Decoder::verify_end called, but data remains");
213
26.7k
   }
214
215
/*
216
* Verify that no bytes remain in the source
217
*/
218
BER_Decoder& BER_Decoder::verify_end(const std::string& err)
219
39.0k
   {
220
39.0k
   if(!m_source->end_of_data() || m_pushed.is_set())
221
341
      throw Decoding_Error(err);
222
38.7k
   return (*this);
223
39.0k
   }
224
225
/*
226
* Discard all the bytes remaining in the source
227
*/
228
BER_Decoder& BER_Decoder::discard_remaining()
229
17.1k
   {
230
17.1k
   uint8_t buf;
231
2.74M
   while(m_source->read_byte(buf))
232
2.72M
      {}
233
17.1k
   return (*this);
234
17.1k
   }
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
77.2k
      {
245
77.2k
      std::swap(next, m_pushed);
246
77.2k
      return next;
247
77.2k
      }
248
249
1.29M
   for(;;)
250
1.40M
      {
251
1.40M
      ASN1_Type type_tag;
252
1.40M
      ASN1_Class class_tag;
253
1.40M
      decode_tag(m_source, type_tag, class_tag);
254
1.40M
      next.set_tagging(type_tag, class_tag);
255
1.40M
      if(next.is_set() == false) // no more objects
256
53.7k
         return next;
257
258
1.34M
      size_t field_size;
259
1.34M
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
260
1.34M
      if(!m_source->check_available(length))
261
5.73k
         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
106k
         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.65k
   {
281
6.65k
   if(m_pushed.is_set())
282
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
283
6.65k
   m_pushed = obj;
284
6.65k
   }
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
483k
   {
295
483k
   BER_Object obj = get_next_object();
296
483k
   obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
297
483k
   return BER_Decoder(std::move(obj), this);
298
483k
   }
299
300
/*
301
* Finish decoding a CONSTRUCTED type
302
*/
303
BER_Decoder& BER_Decoder::end_cons()
304
343k
   {
305
343k
   if(!m_parent)
306
0
      throw Invalid_State("BER_Decoder::end_cons called with null parent");
307
343k
   if(!m_source->end_of_data())
308
1.17k
      throw Decoding_Error("BER_Decoder::end_cons called with data left");
309
342k
   return (*m_parent);
310
343k
   }
311
312
BER_Decoder::BER_Decoder(BER_Object&& obj, BER_Decoder* parent)
313
478k
   {
314
478k
   m_data_src.reset(new DataSource_BERObject(std::move(obj)));
315
478k
   m_source = m_data_src.get();
316
478k
   m_parent = parent;
317
478k
   }
318
319
/*
320
* BER_Decoder Constructor
321
*/
322
BER_Decoder::BER_Decoder(DataSource& src)
323
27.9k
   {
324
27.9k
   m_source = &src;
325
27.9k
   }
326
327
/*
328
* BER_Decoder Constructor
329
 */
330
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length)
331
52.5k
   {
332
52.5k
   m_data_src.reset(new DataSource_Memory(data, length));
333
52.5k
   m_source = m_data_src.get();
334
52.5k
   }
335
336
/*
337
* BER_Decoder Constructor
338
*/
339
BER_Decoder::BER_Decoder(const secure_vector<uint8_t>& data)
340
7.46k
   {
341
7.46k
   m_data_src.reset(new DataSource_Memory(data));
342
7.46k
   m_source = m_data_src.get();
343
7.46k
   }
344
345
/*
346
* BER_Decoder Constructor
347
*/
348
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data)
349
191k
   {
350
191k
   m_data_src.reset(new DataSource_Memory(data.data(), data.size()));
351
191k
   m_source = m_data_src.get();
352
191k
   }
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 /*unused*/, ASN1_Class /*unused*/)
371
524k
   {
372
524k
   obj.decode_from(*this);
373
524k
   return (*this);
374
524k
   }
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.47k
   {
390
3.47k
   secure_vector<uint8_t> out_vec;
391
3.47k
   decode(out_vec, ASN1_Type::OctetString);
392
3.47k
   out = BigInt::decode(out_vec.data(), out_vec.size());
393
3.47k
   return (*this);
394
3.47k
   }
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.23k
   {
402
3.23k
   BER_Object obj = get_next_object();
403
3.23k
   obj.assert_is_a(type_tag, class_tag);
404
405
3.23k
   if(obj.length() != 1)
406
445
      throw BER_Decoding_Error("BER boolean value had invalid size");
407
408
2.79k
   out = (obj.bits()[0]) ? true : false;
409
2.79k
   return (*this);
410
3.23k
   }
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
23.7k
   {
419
23.7k
   BigInt integer;
420
23.7k
   decode(integer, type_tag, class_tag);
421
422
23.7k
   if(integer.is_negative())
423
871
      throw BER_Decoding_Error("Decoded small integer value was negative");
424
425
22.8k
   if(integer.bits() > 32)
426
585
      throw BER_Decoding_Error("Decoded integer value larger than expected");
427
428
22.2k
   out = 0;
429
97.2k
   for(size_t i = 0; i != 4; ++i)
430
75.0k
      out = (out << 8) | integer.byte_at(3-i);
431
432
22.2k
   return (*this);
433
22.8k
   }
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
66.5k
   {
465
66.5k
   BER_Object obj = get_next_object();
466
66.5k
   obj.assert_is_a(type_tag, class_tag);
467
468
66.5k
   if(obj.length() == 0)
469
12.0k
      {
470
12.0k
      out.clear();
471
12.0k
      }
472
54.5k
   else
473
54.5k
      {
474
54.5k
      const bool negative = (obj.bits()[0] & 0x80) ? true : false;
475
476
54.5k
      if(negative)
477
5.56k
         {
478
5.56k
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
479
8.68k
         for(size_t i = obj.length(); i > 0; --i)
480
8.68k
            if(vec[i-1]--)
481
5.56k
               break;
482
317k
         for(size_t i = 0; i != obj.length(); ++i)
483
312k
            vec[i] = ~vec[i];
484
5.56k
         out = BigInt(vec.data(), vec.size());
485
5.56k
         out.flip_sign();
486
5.56k
         }
487
48.9k
      else
488
48.9k
         {
489
48.9k
         out = BigInt(obj.bits(), obj.length());
490
48.9k
         }
491
54.5k
      }
492
493
66.5k
   return (*this);
494
66.5k
   }
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
120k
   {
505
120k
   obj.assert_is_a(type_tag, class_tag);
506
507
120k
   if(real_type == ASN1_Type::OctetString)
508
71.0k
      {
509
71.0k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
510
71.0k
      }
511
49.7k
   else
512
49.7k
      {
513
49.7k
      if(obj.length() == 0)
514
386
         throw BER_Decoding_Error("Invalid BIT STRING");
515
49.3k
      if(obj.bits()[0] >= 8)
516
280
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
517
518
49.0k
      buffer.resize(obj.length() - 1);
519
520
49.0k
      if(obj.length() > 1)
521
36.6k
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
522
49.0k
      }
523
120k
   }
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
8.39k
   {
505
8.39k
   obj.assert_is_a(type_tag, class_tag);
506
507
8.39k
   if(real_type == ASN1_Type::OctetString)
508
7.04k
      {
509
7.04k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
510
7.04k
      }
511
1.35k
   else
512
1.35k
      {
513
1.35k
      if(obj.length() == 0)
514
1
         throw BER_Decoding_Error("Invalid BIT STRING");
515
1.34k
      if(obj.bits()[0] >= 8)
516
6
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
517
518
1.34k
      buffer.resize(obj.length() - 1);
519
520
1.34k
      if(obj.length() > 1)
521
1.21k
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
522
1.34k
      }
523
8.39k
   }
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
112k
   {
505
112k
   obj.assert_is_a(type_tag, class_tag);
506
507
112k
   if(real_type == ASN1_Type::OctetString)
508
63.9k
      {
509
63.9k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
510
63.9k
      }
511
48.3k
   else
512
48.3k
      {
513
48.3k
      if(obj.length() == 0)
514
385
         throw BER_Decoding_Error("Invalid BIT STRING");
515
48.0k
      if(obj.bits()[0] >= 8)
516
274
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
517
518
47.7k
      buffer.resize(obj.length() - 1);
519
520
47.7k
      if(obj.length() > 1)
521
35.4k
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
522
47.7k
      }
523
112k
   }
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
8.40k
   {
534
8.40k
   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
8.40k
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
538
8.40k
   return (*this);
539
8.40k
   }
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
114k
   {
545
114k
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString)
546
1.75k
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
547
548
112k
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
549
112k
   return (*this);
550
114k
   }
551
552
}