Coverage Report

Created: 2024-11-29 06:10

/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
10
#include <botan/bigint.h>
11
#include <botan/internal/int_utils.h>
12
#include <botan/internal/loadstor.h>
13
#include <memory>
14
15
namespace Botan {
16
17
namespace {
18
19
/*
20
* This value is somewhat arbitrary. OpenSSL allows up to 128 nested
21
* indefinite length sequences. If you increase this, also increase the
22
* limit in the test in test_asn1.cpp
23
*/
24
const size_t ALLOWED_EOC_NESTINGS = 16;
25
26
/*
27
* BER decode an ASN.1 type tag
28
*/
29
4.11M
size_t decode_tag(DataSource* ber, ASN1_Type& type_tag, ASN1_Class& class_tag) {
30
4.11M
   uint8_t b;
31
4.11M
   if(!ber->read_byte(b)) {
32
289k
      type_tag = ASN1_Type::NoObject;
33
289k
      class_tag = ASN1_Class::NoObject;
34
289k
      return 0;
35
289k
   }
36
37
3.82M
   if((b & 0x1F) != 0x1F) {
38
3.77M
      type_tag = ASN1_Type(b & 0x1F);
39
3.77M
      class_tag = ASN1_Class(b & 0xE0);
40
3.77M
      return 1;
41
3.77M
   }
42
43
45.4k
   size_t tag_bytes = 1;
44
45.4k
   class_tag = ASN1_Class(b & 0xE0);
45
46
45.4k
   size_t tag_buf = 0;
47
609k
   while(true) {
48
609k
      if(!ber->read_byte(b)) {
49
1.10k
         throw BER_Decoding_Error("Long-form tag truncated");
50
1.10k
      }
51
608k
      if(tag_buf & 0xFF000000) {
52
472
         throw BER_Decoding_Error("Long-form tag overflowed 32 bits");
53
472
      }
54
      // This is required even by BER (see X.690 section 8.1.2.4.2 sentence c)
55
607k
      if(tag_bytes == 0 && b == 0x80) {
56
0
         throw BER_Decoding_Error("Long form tag with leading zero");
57
0
      }
58
607k
      ++tag_bytes;
59
607k
      tag_buf = (tag_buf << 7) | (b & 0x7F);
60
607k
      if((b & 0x80) == 0) {
61
43.8k
         break;
62
43.8k
      }
63
607k
   }
64
43.8k
   type_tag = ASN1_Type(tag_buf);
65
43.8k
   return tag_bytes;
66
45.4k
}
67
68
/*
69
* Find the EOC marker
70
*/
71
size_t find_eoc(DataSource* src, size_t allow_indef);
72
73
/*
74
* BER decode an ASN.1 length field
75
*/
76
3.82M
size_t decode_length(DataSource* ber, size_t& field_size, size_t allow_indef) {
77
3.82M
   uint8_t b;
78
3.82M
   if(!ber->read_byte(b)) {
79
3.09k
      throw BER_Decoding_Error("Length field not found");
80
3.09k
   }
81
3.81M
   field_size = 1;
82
3.81M
   if((b & 0x80) == 0) {
83
3.25M
      return b;
84
3.25M
   }
85
86
558k
   field_size += (b & 0x7F);
87
558k
   if(field_size > 5) {
88
3.87k
      throw BER_Decoding_Error("Length field is too large");
89
3.87k
   }
90
91
554k
   if(field_size == 1) {
92
313k
      if(allow_indef == 0) {
93
267
         throw BER_Decoding_Error("Nested EOC markers too deep, rejecting to avoid stack exhaustion");
94
313k
      } else {
95
313k
         return find_eoc(ber, allow_indef - 1);
96
313k
      }
97
313k
   }
98
99
240k
   size_t length = 0;
100
101
646k
   for(size_t i = 0; i != field_size - 1; ++i) {
102
407k
      if(get_byte<0>(length) != 0) {
103
0
         throw BER_Decoding_Error("Field length overflow");
104
0
      }
105
407k
      if(!ber->read_byte(b)) {
106
947
         throw BER_Decoding_Error("Corrupted length field");
107
947
      }
108
406k
      length = (length << 8) | b;
109
406k
   }
110
239k
   return length;
111
240k
}
112
113
/*
114
* Find the EOC marker
115
*/
116
313k
size_t find_eoc(DataSource* ber, size_t allow_indef) {
117
313k
   secure_vector<uint8_t> buffer(BOTAN_DEFAULT_BUFFER_SIZE), data;
118
119
628k
   while(true) {
120
628k
      const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
121
628k
      if(got == 0) {
122
313k
         break;
123
313k
      }
124
125
315k
      data += std::make_pair(buffer.data(), got);
126
315k
   }
127
128
313k
   DataSource_Memory source(data);
129
313k
   data.clear();
130
131
313k
   size_t length = 0;
132
1.97M
   while(true) {
133
1.97M
      ASN1_Type type_tag;
134
1.97M
      ASN1_Class class_tag;
135
1.97M
      const size_t tag_size = decode_tag(&source, type_tag, class_tag);
136
1.97M
      if(type_tag == ASN1_Type::NoObject) {
137
218k
         break;
138
218k
      }
139
140
1.75M
      size_t length_size = 0;
141
1.75M
      const size_t item_size = decode_length(&source, length_size, allow_indef);
142
1.75M
      source.discard_next(item_size);
143
144
1.75M
      if(auto new_len = checked_add(length, item_size, tag_size, length_size)) {
145
1.74M
         length = new_len.value();
146
1.74M
      } else {
147
6.93k
         throw Decoding_Error("Integer overflow while decoding DER");
148
6.93k
      }
149
150
1.74M
      if(type_tag == ASN1_Type::Eoc && class_tag == ASN1_Class::Universal) {
151
87.2k
         break;
152
87.2k
      }
153
1.74M
   }
154
306k
   return length;
155
313k
}
156
157
class DataSource_BERObject final : public DataSource {
158
   public:
159
23.7M
      size_t read(uint8_t out[], size_t length) override {
160
23.7M
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
161
23.7M
         const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
162
23.7M
         copy_mem(out, m_obj.bits() + m_offset, got);
163
23.7M
         m_offset += got;
164
23.7M
         return got;
165
23.7M
      }
166
167
93.4k
      size_t peek(uint8_t out[], size_t length, size_t peek_offset) const override {
168
93.4k
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
169
93.4k
         const size_t bytes_left = m_obj.length() - m_offset;
170
171
93.4k
         if(peek_offset >= bytes_left) {
172
48.7k
            return 0;
173
48.7k
         }
174
175
44.6k
         const size_t got = std::min(bytes_left - peek_offset, length);
176
44.6k
         copy_mem(out, m_obj.bits() + m_offset + peek_offset, got);
177
44.6k
         return got;
178
93.4k
      }
179
180
1.42M
      bool check_available(size_t n) override {
181
1.42M
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
182
1.42M
         return (n <= (m_obj.length() - m_offset));
183
1.42M
      }
184
185
1.02M
      bool end_of_data() const override { return get_bytes_read() == m_obj.length(); }
186
187
1.02M
      size_t get_bytes_read() const override { return m_offset; }
188
189
684k
      explicit DataSource_BERObject(BER_Object&& obj) : m_obj(std::move(obj)), m_offset(0) {}
190
191
   private:
192
      BER_Object m_obj;
193
      size_t m_offset;
194
};
195
196
}  // namespace
197
198
/*
199
* Check if more objects are there
200
*/
201
707k
bool BER_Decoder::more_items() const {
202
707k
   if(m_source->end_of_data() && !m_pushed.is_set()) {
203
194k
      return false;
204
194k
   }
205
513k
   return true;
206
707k
}
207
208
/*
209
* Verify that no bytes remain in the source
210
*/
211
43.5k
BER_Decoder& BER_Decoder::verify_end() {
212
43.5k
   return verify_end("BER_Decoder::verify_end called, but data remains");
213
43.5k
}
214
215
/*
216
* Verify that no bytes remain in the source
217
*/
218
61.1k
BER_Decoder& BER_Decoder::verify_end(std::string_view err) {
219
61.1k
   if(!m_source->end_of_data() || m_pushed.is_set()) {
220
475
      throw Decoding_Error(err);
221
475
   }
222
60.6k
   return (*this);
223
61.1k
}
224
225
/*
226
* Discard all the bytes remaining in the source
227
*/
228
9.88k
BER_Decoder& BER_Decoder::discard_remaining() {
229
9.88k
   uint8_t buf;
230
34.2k
   while(m_source->read_byte(buf)) {}
231
9.88k
   return (*this);
232
9.88k
}
233
234
11.5k
const BER_Object& BER_Decoder::peek_next_object() {
235
11.5k
   if(!m_pushed.is_set()) {
236
8.77k
      m_pushed = get_next_object();
237
8.77k
   }
238
239
11.5k
   return m_pushed;
240
11.5k
}
241
242
/*
243
* Return the BER encoding of the next object
244
*/
245
1.94M
BER_Object BER_Decoder::get_next_object() {
246
1.94M
   BER_Object next;
247
248
1.94M
   if(m_pushed.is_set()) {
249
101k
      std::swap(next, m_pushed);
250
101k
      return next;
251
101k
   }
252
253
2.14M
   for(;;) {
254
2.14M
      ASN1_Type type_tag;
255
2.14M
      ASN1_Class class_tag;
256
2.14M
      decode_tag(m_source, type_tag, class_tag);
257
2.14M
      next.set_tagging(type_tag, class_tag);
258
2.14M
      if(next.is_set() == false) {  // no more objects
259
71.9k
         return next;
260
71.9k
      }
261
262
2.06M
      size_t field_size;
263
2.06M
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
264
2.06M
      if(!m_source->check_available(length)) {
265
7.64k
         throw BER_Decoding_Error("Value truncated");
266
7.64k
      }
267
268
2.06M
      uint8_t* out = next.mutable_bits(length);
269
2.06M
      if(m_source->read(out, length) != length) {
270
0
         throw BER_Decoding_Error("Value truncated");
271
0
      }
272
273
2.06M
      if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc)) {
274
294k
         continue;
275
1.76M
      } else {
276
1.76M
         break;
277
1.76M
      }
278
2.06M
   }
279
280
1.76M
   return next;
281
1.84M
}
282
283
/*
284
* Push a object back into the stream
285
*/
286
1.76k
void BER_Decoder::push_back(const BER_Object& obj) {
287
1.76k
   if(m_pushed.is_set()) {
288
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
289
0
   }
290
1.76k
   m_pushed = obj;
291
1.76k
}
292
293
133k
void BER_Decoder::push_back(BER_Object&& obj) {
294
133k
   if(m_pushed.is_set()) {
295
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
296
0
   }
297
133k
   m_pushed = std::move(obj);
298
133k
}
299
300
694k
BER_Decoder BER_Decoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag) {
301
694k
   BER_Object obj = get_next_object();
302
694k
   obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
303
694k
   return BER_Decoder(std::move(obj), this);
304
694k
}
305
306
/*
307
* Finish decoding a CONSTRUCTED type
308
*/
309
483k
BER_Decoder& BER_Decoder::end_cons() {
310
483k
   if(!m_parent) {
311
0
      throw Invalid_State("BER_Decoder::end_cons called with null parent");
312
0
   }
313
483k
   if(!m_source->end_of_data()) {
314
1.96k
      throw Decoding_Error("BER_Decoder::end_cons called with data left");
315
1.96k
   }
316
481k
   return (*m_parent);
317
483k
}
318
319
684k
BER_Decoder::BER_Decoder(BER_Object&& obj, BER_Decoder* parent) {
320
684k
   m_data_src = std::make_unique<DataSource_BERObject>(std::move(obj));
321
684k
   m_source = m_data_src.get();
322
684k
   m_parent = parent;
323
684k
}
324
325
/*
326
* BER_Decoder Constructor
327
*/
328
39.5k
BER_Decoder::BER_Decoder(DataSource& src) {
329
39.5k
   m_source = &src;
330
39.5k
}
331
332
/*
333
* BER_Decoder Constructor
334
 */
335
78.8k
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length) {
336
78.8k
   m_data_src = std::make_unique<DataSource_Memory>(data, length);
337
78.8k
   m_source = m_data_src.get();
338
78.8k
}
339
340
/*
341
* BER_Decoder Constructor
342
*/
343
9.73k
BER_Decoder::BER_Decoder(const secure_vector<uint8_t>& data) {
344
9.73k
   m_data_src = std::make_unique<DataSource_Memory>(data);
345
9.73k
   m_source = m_data_src.get();
346
9.73k
}
347
348
/*
349
* BER_Decoder Constructor
350
*/
351
228k
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data) {
352
228k
   m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
353
228k
   m_source = m_data_src.get();
354
228k
}
355
356
/*
357
* BER_Decoder Copy Constructor
358
*/
359
0
BER_Decoder::BER_Decoder(const BER_Decoder& other) {
360
0
   m_source = other.m_source;
361
362
   // take ownership
363
0
   std::swap(m_data_src, other.m_data_src);
364
0
   m_parent = other.m_parent;
365
0
}
366
367
/*
368
* Request for an object to decode itself
369
*/
370
721k
BER_Decoder& BER_Decoder::decode(ASN1_Object& obj, ASN1_Type /*unused*/, ASN1_Class /*unused*/) {
371
721k
   obj.decode_from(*this);
372
721k
   return (*this);
373
721k
}
374
375
/*
376
* Decode a BER encoded NULL
377
*/
378
0
BER_Decoder& BER_Decoder::decode_null() {
379
0
   BER_Object obj = get_next_object();
380
0
   obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
381
0
   if(obj.length() > 0) {
382
0
      throw BER_Decoding_Error("NULL object had nonzero size");
383
0
   }
384
0
   return (*this);
385
0
}
386
387
1.65k
BER_Decoder& BER_Decoder::decode_octet_string_bigint(BigInt& out) {
388
1.65k
   secure_vector<uint8_t> out_vec;
389
1.65k
   decode(out_vec, ASN1_Type::OctetString);
390
1.65k
   out = BigInt::from_bytes(out_vec);
391
1.65k
   return (*this);
392
1.65k
}
393
394
/*
395
* Decode a BER encoded BOOLEAN
396
*/
397
13.0k
BER_Decoder& BER_Decoder::decode(bool& out, ASN1_Type type_tag, ASN1_Class class_tag) {
398
13.0k
   BER_Object obj = get_next_object();
399
13.0k
   obj.assert_is_a(type_tag, class_tag);
400
401
13.0k
   if(obj.length() != 1) {
402
334
      throw BER_Decoding_Error("BER boolean value had invalid size");
403
334
   }
404
405
12.6k
   out = (obj.bits()[0]) ? true : false;
406
12.6k
   return (*this);
407
13.0k
}
408
409
/*
410
* Decode a small BER encoded INTEGER
411
*/
412
36.2k
BER_Decoder& BER_Decoder::decode(size_t& out, ASN1_Type type_tag, ASN1_Class class_tag) {
413
36.2k
   BigInt integer;
414
36.2k
   decode(integer, type_tag, class_tag);
415
416
36.2k
   if(integer.is_negative()) {
417
770
      throw BER_Decoding_Error("Decoded small integer value was negative");
418
770
   }
419
420
35.4k
   if(integer.bits() > 32) {
421
904
      throw BER_Decoding_Error("Decoded integer value larger than expected");
422
904
   }
423
424
34.5k
   out = 0;
425
162k
   for(size_t i = 0; i != 4; ++i) {
426
127k
      out = (out << 8) | integer.byte_at(3 - i);
427
127k
   }
428
429
34.5k
   return (*this);
430
35.4k
}
431
432
/*
433
* Decode a small BER encoded INTEGER
434
*/
435
0
uint64_t BER_Decoder::decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes) {
436
0
   if(T_bytes > 8) {
437
0
      throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
438
0
   }
439
440
0
   BigInt integer;
441
0
   decode(integer, type_tag, class_tag);
442
443
0
   if(integer.bits() > 8 * T_bytes) {
444
0
      throw BER_Decoding_Error("Decoded integer value larger than expected");
445
0
   }
446
447
0
   uint64_t out = 0;
448
0
   for(size_t i = 0; i != 8; ++i) {
449
0
      out = (out << 8) | integer.byte_at(7 - i);
450
0
   }
451
452
0
   return out;
453
0
}
454
455
/*
456
* Decode a BER encoded INTEGER
457
*/
458
131k
BER_Decoder& BER_Decoder::decode(BigInt& out, ASN1_Type type_tag, ASN1_Class class_tag) {
459
131k
   BER_Object obj = get_next_object();
460
131k
   obj.assert_is_a(type_tag, class_tag);
461
462
131k
   if(obj.length() == 0) {
463
14.6k
      out.clear();
464
116k
   } else {
465
116k
      const bool negative = (obj.bits()[0] & 0x80) ? true : false;
466
467
116k
      if(negative) {
468
7.33k
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
469
11.3k
         for(size_t i = obj.length(); i > 0; --i) {
470
11.3k
            if(vec[i - 1]--) {
471
7.33k
               break;
472
7.33k
            }
473
11.3k
         }
474
430k
         for(size_t i = 0; i != obj.length(); ++i) {
475
423k
            vec[i] = ~vec[i];
476
423k
         }
477
7.33k
         out._assign_from_bytes(vec);
478
7.33k
         out.flip_sign();
479
109k
      } else {
480
109k
         out._assign_from_bytes(obj.data());
481
109k
      }
482
116k
   }
483
484
131k
   return (*this);
485
131k
}
486
487
namespace {
488
489
template <typename Alloc>
490
void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer,
491
                               const BER_Object& obj,
492
                               ASN1_Type real_type,
493
                               ASN1_Type type_tag,
494
145k
                               ASN1_Class class_tag) {
495
145k
   obj.assert_is_a(type_tag, class_tag);
496
497
145k
   if(real_type == ASN1_Type::OctetString) {
498
83.4k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
499
83.4k
   } else {
500
61.8k
      if(obj.length() == 0) {
501
381
         throw BER_Decoding_Error("Invalid BIT STRING");
502
381
      }
503
61.4k
      if(obj.bits()[0] >= 8) {
504
260
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
505
260
      }
506
507
61.1k
      buffer.resize(obj.length() - 1);
508
509
61.1k
      if(obj.length() > 1) {
510
45.0k
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
511
45.0k
      }
512
61.1k
   }
513
145k
}
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
494
11.8k
                               ASN1_Class class_tag) {
495
11.8k
   obj.assert_is_a(type_tag, class_tag);
496
497
11.8k
   if(real_type == ASN1_Type::OctetString) {
498
11.4k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
499
11.4k
   } else {
500
474
      if(obj.length() == 0) {
501
1
         throw BER_Decoding_Error("Invalid BIT STRING");
502
1
      }
503
473
      if(obj.bits()[0] >= 8) {
504
10
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
505
10
      }
506
507
463
      buffer.resize(obj.length() - 1);
508
509
463
      if(obj.length() > 1) {
510
294
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
511
294
      }
512
463
   }
513
11.8k
}
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
494
133k
                               ASN1_Class class_tag) {
495
133k
   obj.assert_is_a(type_tag, class_tag);
496
497
133k
   if(real_type == ASN1_Type::OctetString) {
498
72.0k
      buffer.assign(obj.bits(), obj.bits() + obj.length());
499
72.0k
   } else {
500
61.3k
      if(obj.length() == 0) {
501
380
         throw BER_Decoding_Error("Invalid BIT STRING");
502
380
      }
503
60.9k
      if(obj.bits()[0] >= 8) {
504
250
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
505
250
      }
506
507
60.7k
      buffer.resize(obj.length() - 1);
508
509
60.7k
      if(obj.length() > 1) {
510
44.7k
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
511
44.7k
      }
512
60.7k
   }
513
133k
}
514
515
}  // namespace
516
517
/*
518
* BER decode a BIT STRING or OCTET STRING
519
*/
520
BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& buffer,
521
                                 ASN1_Type real_type,
522
                                 ASN1_Type type_tag,
523
11.8k
                                 ASN1_Class class_tag) {
524
11.8k
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
525
0
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
526
0
   }
527
528
11.8k
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
529
11.8k
   return (*this);
530
11.8k
}
531
532
BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& buffer,
533
                                 ASN1_Type real_type,
534
                                 ASN1_Type type_tag,
535
135k
                                 ASN1_Class class_tag) {
536
135k
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
537
1.68k
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
538
1.68k
   }
539
540
133k
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
541
133k
   return (*this);
542
135k
}
543
544
}  // namespace Botan