Coverage Report

Created: 2025-12-31 06:16

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