Coverage Report

Created: 2024-06-28 06:39

/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
240
size_t decode_tag(DataSource* ber, ASN1_Type& type_tag, ASN1_Class& class_tag) {
30
240
   uint8_t b;
31
240
   if(!ber->read_byte(b)) {
32
0
      type_tag = ASN1_Type::NoObject;
33
0
      class_tag = ASN1_Class::NoObject;
34
0
      return 0;
35
0
   }
36
37
240
   if((b & 0x1F) != 0x1F) {
38
240
      type_tag = ASN1_Type(b & 0x1F);
39
240
      class_tag = ASN1_Class(b & 0xE0);
40
240
      return 1;
41
240
   }
42
43
0
   size_t tag_bytes = 1;
44
0
   class_tag = ASN1_Class(b & 0xE0);
45
46
0
   size_t tag_buf = 0;
47
0
   while(true) {
48
0
      if(!ber->read_byte(b)) {
49
0
         throw BER_Decoding_Error("Long-form tag truncated");
50
0
      }
51
0
      if(tag_buf & 0xFF000000) {
52
0
         throw BER_Decoding_Error("Long-form tag overflowed 32 bits");
53
0
      }
54
      // This is required even by BER (see X.690 section 8.1.2.4.2 sentence c)
55
0
      if(tag_bytes == 0 && b == 0x80) {
56
0
         throw BER_Decoding_Error("Long form tag with leading zero");
57
0
      }
58
0
      ++tag_bytes;
59
0
      tag_buf = (tag_buf << 7) | (b & 0x7F);
60
0
      if((b & 0x80) == 0) {
61
0
         break;
62
0
      }
63
0
   }
64
0
   type_tag = ASN1_Type(tag_buf);
65
0
   return tag_bytes;
66
0
}
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
240
size_t decode_length(DataSource* ber, size_t& field_size, size_t allow_indef) {
77
240
   uint8_t b;
78
240
   if(!ber->read_byte(b)) {
79
0
      throw BER_Decoding_Error("Length field not found");
80
0
   }
81
240
   field_size = 1;
82
240
   if((b & 0x80) == 0) {
83
237
      return b;
84
237
   }
85
86
3
   field_size += (b & 0x7F);
87
3
   if(field_size > 5) {
88
0
      throw BER_Decoding_Error("Length field is too large");
89
0
   }
90
91
3
   if(field_size == 1) {
92
0
      if(allow_indef == 0) {
93
0
         throw BER_Decoding_Error("Nested EOC markers too deep, rejecting to avoid stack exhaustion");
94
0
      } else {
95
0
         return find_eoc(ber, allow_indef - 1);
96
0
      }
97
0
   }
98
99
3
   size_t length = 0;
100
101
6
   for(size_t i = 0; i != field_size - 1; ++i) {
102
3
      if(get_byte<0>(length) != 0) {
103
0
         throw BER_Decoding_Error("Field length overflow");
104
0
      }
105
3
      if(!ber->read_byte(b)) {
106
0
         throw BER_Decoding_Error("Corrupted length field");
107
0
      }
108
3
      length = (length << 8) | b;
109
3
   }
110
3
   return length;
111
3
}
112
113
/*
114
* Find the EOC marker
115
*/
116
0
size_t find_eoc(DataSource* ber, size_t allow_indef) {
117
0
   secure_vector<uint8_t> buffer(BOTAN_DEFAULT_BUFFER_SIZE), data;
118
119
0
   while(true) {
120
0
      const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
121
0
      if(got == 0) {
122
0
         break;
123
0
      }
124
125
0
      data += std::make_pair(buffer.data(), got);
126
0
   }
127
128
0
   DataSource_Memory source(data);
129
0
   data.clear();
130
131
0
   size_t length = 0;
132
0
   while(true) {
133
0
      ASN1_Type type_tag;
134
0
      ASN1_Class class_tag;
135
0
      const size_t tag_size = decode_tag(&source, type_tag, class_tag);
136
0
      if(type_tag == ASN1_Type::NoObject) {
137
0
         break;
138
0
      }
139
140
0
      size_t length_size = 0;
141
0
      const size_t item_size = decode_length(&source, length_size, allow_indef);
142
0
      source.discard_next(item_size);
143
144
0
      if(auto new_len = checked_add(length, item_size, tag_size, length_size)) {
145
0
         length = new_len.value();
146
0
      } else {
147
0
         throw Decoding_Error("Integer overflow while decoding DER");
148
0
      }
149
150
0
      if(type_tag == ASN1_Type::Eoc && class_tag == ASN1_Class::Universal) {
151
0
         break;
152
0
      }
153
0
   }
154
0
   return length;
155
0
}
156
157
class DataSource_BERObject final : public DataSource {
158
   public:
159
480
      size_t read(uint8_t out[], size_t length) override {
160
480
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
161
480
         const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
162
480
         copy_mem(out, m_obj.bits() + m_offset, got);
163
480
         m_offset += got;
164
480
         return got;
165
480
      }
166
167
0
      size_t peek(uint8_t out[], size_t length, size_t peek_offset) const override {
168
0
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
169
0
         const size_t bytes_left = m_obj.length() - m_offset;
170
171
0
         if(peek_offset >= bytes_left) {
172
0
            return 0;
173
0
         }
174
175
0
         const size_t got = std::min(bytes_left - peek_offset, length);
176
0
         copy_mem(out, m_obj.bits() + m_offset + peek_offset, got);
177
0
         return got;
178
0
      }
179
180
160
      bool check_available(size_t n) override {
181
160
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
182
160
         return (n <= (m_obj.length() - m_offset));
183
160
      }
184
185
240
      bool end_of_data() const override { return get_bytes_read() == m_obj.length(); }
186
187
240
      size_t get_bytes_read() const override { return m_offset; }
188
189
80
      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
240
bool BER_Decoder::more_items() const {
202
240
   if(m_source->end_of_data() && !m_pushed.is_set()) {
203
80
      return false;
204
80
   }
205
160
   return true;
206
240
}
207
208
/*
209
* Verify that no bytes remain in the source
210
*/
211
0
BER_Decoder& BER_Decoder::verify_end() {
212
0
   return verify_end("BER_Decoder::verify_end called, but data remains");
213
0
}
214
215
/*
216
* Verify that no bytes remain in the source
217
*/
218
0
BER_Decoder& BER_Decoder::verify_end(std::string_view err) {
219
0
   if(!m_source->end_of_data() || m_pushed.is_set()) {
220
0
      throw Decoding_Error(err);
221
0
   }
222
0
   return (*this);
223
0
}
224
225
/*
226
* Discard all the bytes remaining in the source
227
*/
228
0
BER_Decoder& BER_Decoder::discard_remaining() {
229
0
   uint8_t buf;
230
0
   while(m_source->read_byte(buf)) {}
231
0
   return (*this);
232
0
}
233
234
0
const BER_Object& BER_Decoder::peek_next_object() {
235
0
   if(!m_pushed.is_set()) {
236
0
      m_pushed = get_next_object();
237
0
   }
238
239
0
   return m_pushed;
240
0
}
241
242
/*
243
* Return the BER encoding of the next object
244
*/
245
240
BER_Object BER_Decoder::get_next_object() {
246
240
   BER_Object next;
247
248
240
   if(m_pushed.is_set()) {
249
0
      std::swap(next, m_pushed);
250
0
      return next;
251
0
   }
252
253
240
   for(;;) {
254
240
      ASN1_Type type_tag;
255
240
      ASN1_Class class_tag;
256
240
      decode_tag(m_source, type_tag, class_tag);
257
240
      next.set_tagging(type_tag, class_tag);
258
240
      if(next.is_set() == false) {  // no more objects
259
0
         return next;
260
0
      }
261
262
240
      size_t field_size;
263
240
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
264
240
      if(!m_source->check_available(length)) {
265
0
         throw BER_Decoding_Error("Value truncated");
266
0
      }
267
268
240
      uint8_t* out = next.mutable_bits(length);
269
240
      if(m_source->read(out, length) != length) {
270
0
         throw BER_Decoding_Error("Value truncated");
271
0
      }
272
273
240
      if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc)) {
274
0
         continue;
275
240
      } else {
276
240
         break;
277
240
      }
278
240
   }
279
280
240
   return next;
281
240
}
282
283
/*
284
* Push a object back into the stream
285
*/
286
0
void BER_Decoder::push_back(const BER_Object& obj) {
287
0
   if(m_pushed.is_set()) {
288
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
289
0
   }
290
0
   m_pushed = obj;
291
0
}
292
293
0
void BER_Decoder::push_back(BER_Object&& obj) {
294
0
   if(m_pushed.is_set()) {
295
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
296
0
   }
297
0
   m_pushed = std::move(obj);
298
0
}
299
300
80
BER_Decoder BER_Decoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag) {
301
80
   BER_Object obj = get_next_object();
302
80
   obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
303
80
   return BER_Decoder(std::move(obj), this);
304
80
}
305
306
/*
307
* Finish decoding a CONSTRUCTED type
308
*/
309
0
BER_Decoder& BER_Decoder::end_cons() {
310
0
   if(!m_parent) {
311
0
      throw Invalid_State("BER_Decoder::end_cons called with null parent");
312
0
   }
313
0
   if(!m_source->end_of_data()) {
314
0
      throw Decoding_Error("BER_Decoder::end_cons called with data left");
315
0
   }
316
0
   return (*m_parent);
317
0
}
318
319
80
BER_Decoder::BER_Decoder(BER_Object&& obj, BER_Decoder* parent) {
320
80
   m_data_src = std::make_unique<DataSource_BERObject>(std::move(obj));
321
80
   m_source = m_data_src.get();
322
80
   m_parent = parent;
323
80
}
324
325
/*
326
* BER_Decoder Constructor
327
*/
328
0
BER_Decoder::BER_Decoder(DataSource& src) {
329
0
   m_source = &src;
330
0
}
331
332
/*
333
* BER_Decoder Constructor
334
 */
335
0
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length) {
336
0
   m_data_src = std::make_unique<DataSource_Memory>(data, length);
337
0
   m_source = m_data_src.get();
338
0
}
339
340
/*
341
* BER_Decoder Constructor
342
*/
343
0
BER_Decoder::BER_Decoder(const secure_vector<uint8_t>& data) {
344
0
   m_data_src = std::make_unique<DataSource_Memory>(data);
345
0
   m_source = m_data_src.get();
346
0
}
347
348
/*
349
* BER_Decoder Constructor
350
*/
351
80
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data) {
352
80
   m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
353
80
   m_source = m_data_src.get();
354
80
}
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
0
BER_Decoder& BER_Decoder::decode(ASN1_Object& obj, ASN1_Type /*unused*/, ASN1_Class /*unused*/) {
371
0
   obj.decode_from(*this);
372
0
   return (*this);
373
0
}
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
0
BER_Decoder& BER_Decoder::decode_octet_string_bigint(BigInt& out) {
388
0
   secure_vector<uint8_t> out_vec;
389
0
   decode(out_vec, ASN1_Type::OctetString);
390
0
   out = BigInt::from_bytes(out_vec);
391
0
   return (*this);
392
0
}
393
394
/*
395
* Decode a BER encoded BOOLEAN
396
*/
397
0
BER_Decoder& BER_Decoder::decode(bool& out, ASN1_Type type_tag, ASN1_Class class_tag) {
398
0
   BER_Object obj = get_next_object();
399
0
   obj.assert_is_a(type_tag, class_tag);
400
401
0
   if(obj.length() != 1) {
402
0
      throw BER_Decoding_Error("BER boolean value had invalid size");
403
0
   }
404
405
0
   out = (obj.bits()[0]) ? true : false;
406
0
   return (*this);
407
0
}
408
409
/*
410
* Decode a small BER encoded INTEGER
411
*/
412
0
BER_Decoder& BER_Decoder::decode(size_t& out, ASN1_Type type_tag, ASN1_Class class_tag) {
413
0
   BigInt integer;
414
0
   decode(integer, type_tag, class_tag);
415
416
0
   if(integer.is_negative()) {
417
0
      throw BER_Decoding_Error("Decoded small integer value was negative");
418
0
   }
419
420
0
   if(integer.bits() > 32) {
421
0
      throw BER_Decoding_Error("Decoded integer value larger than expected");
422
0
   }
423
424
0
   out = 0;
425
0
   for(size_t i = 0; i != 4; ++i) {
426
0
      out = (out << 8) | integer.byte_at(3 - i);
427
0
   }
428
429
0
   return (*this);
430
0
}
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
160
BER_Decoder& BER_Decoder::decode(BigInt& out, ASN1_Type type_tag, ASN1_Class class_tag) {
459
160
   BER_Object obj = get_next_object();
460
160
   obj.assert_is_a(type_tag, class_tag);
461
462
160
   if(obj.length() == 0) {
463
0
      out.clear();
464
160
   } else {
465
160
      const bool negative = (obj.bits()[0] & 0x80) ? true : false;
466
467
160
      if(negative) {
468
0
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
469
0
         for(size_t i = obj.length(); i > 0; --i) {
470
0
            if(vec[i - 1]--) {
471
0
               break;
472
0
            }
473
0
         }
474
0
         for(size_t i = 0; i != obj.length(); ++i) {
475
0
            vec[i] = ~vec[i];
476
0
         }
477
0
         out._assign_from_bytes(vec);
478
0
         out.flip_sign();
479
160
      } else {
480
160
         out._assign_from_bytes(obj.data());
481
160
      }
482
160
   }
483
484
160
   return (*this);
485
160
}
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
0
                               ASN1_Class class_tag) {
495
0
   obj.assert_is_a(type_tag, class_tag);
496
497
0
   if(real_type == ASN1_Type::OctetString) {
498
0
      buffer.assign(obj.bits(), obj.bits() + obj.length());
499
0
   } else {
500
0
      if(obj.length() == 0) {
501
0
         throw BER_Decoding_Error("Invalid BIT STRING");
502
0
      }
503
0
      if(obj.bits()[0] >= 8) {
504
0
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
505
0
      }
506
507
0
      buffer.resize(obj.length() - 1);
508
509
0
      if(obj.length() > 1) {
510
0
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
511
0
      }
512
0
   }
513
0
}
Unexecuted instantiation: 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)
Unexecuted instantiation: 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)
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
0
                                 ASN1_Class class_tag) {
524
0
   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
0
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
529
0
   return (*this);
530
0
}
531
532
BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& buffer,
533
                                 ASN1_Type real_type,
534
                                 ASN1_Type type_tag,
535
0
                                 ASN1_Class class_tag) {
536
0
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
537
0
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
538
0
   }
539
540
0
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
541
0
   return (*this);
542
0
}
543
544
}  // namespace Botan