• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 21768358452

06 Feb 2026 10:35PM UTC coverage: 90.064% (-0.003%) from 90.067%
21768358452

Pull #5289

github

web-flow
Merge f589db195 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102238 of 113517 relevant lines covered (90.06%)

11357432.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.94
/src/lib/asn1/ber_dec.cpp
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
size_t decode_tag(DataSource* ber, ASN1_Type& type_tag, ASN1_Class& class_tag) {
8,223,180✔
30
   auto b = ber->read_byte();
8,223,180✔
31

32
   if(!b) {
8,223,180✔
33
      type_tag = ASN1_Type::NoObject;
124,884✔
34
      class_tag = ASN1_Class::NoObject;
124,884✔
35
      return 0;
124,884✔
36
   }
37

38
   if((*b & 0x1F) != 0x1F) {
8,098,296✔
39
      type_tag = ASN1_Type(*b & 0x1F);
8,093,396✔
40
      class_tag = ASN1_Class(*b & 0xE0);
8,093,396✔
41
      return 1;
8,093,396✔
42
   }
43

44
   size_t tag_bytes = 1;
4,900✔
45
   class_tag = ASN1_Class(*b & 0xE0);
4,900✔
46

47
   size_t tag_buf = 0;
4,900✔
48
   while(true) {
9,901✔
49
      b = ber->read_byte();
9,901✔
50
      if(!b) {
9,901✔
51
         throw BER_Decoding_Error("Long-form tag truncated");
87✔
52
      }
53
      if((tag_buf >> 24) != 0) {
9,814✔
54
         throw BER_Decoding_Error("Long-form tag overflowed 32 bits");
14✔
55
      }
56
      // This is required even by BER (see X.690 section 8.1.2.4.2 sentence c)
57
      if(tag_bytes == 0 && b == 0x80) {
9,800✔
58
         throw BER_Decoding_Error("Long form tag with leading zero");
×
59
      }
60
      ++tag_bytes;
9,800✔
61
      tag_buf = (tag_buf << 7) | (*b & 0x7F);
9,800✔
62
      if((*b & 0x80) == 0) {
9,800✔
63
         break;
64
      }
65
   }
66
   type_tag = ASN1_Type(tag_buf);
4,799✔
67
   return tag_bytes;
4,799✔
68
}
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
size_t decode_length(DataSource* ber, size_t& field_size, size_t allow_indef) {
8,098,100✔
79
   uint8_t b = 0;
8,098,100✔
80
   if(ber->read_byte(b) == 0) {
8,098,100✔
81
      throw BER_Decoding_Error("Length field not found");
706✔
82
   }
83
   field_size = 1;
8,097,394✔
84
   if((b & 0x80) == 0) {
8,097,394✔
85
      return b;
7,585,130✔
86
   }
87

88
   field_size += (b & 0x7F);
512,264✔
89
   if(field_size > 5) {
512,264✔
90
      throw BER_Decoding_Error("Length field is too large");
1,013✔
91
   }
92

93
   if(field_size == 1) {
511,251✔
94
      if(allow_indef == 0) {
273,119✔
95
         throw BER_Decoding_Error("Nested EOC markers too deep, rejecting to avoid stack exhaustion");
231✔
96
      } else {
97
         return find_eoc(ber, allow_indef - 1);
272,888✔
98
      }
99
   }
100

101
   size_t length = 0;
102

103
   for(size_t i = 0; i != field_size - 1; ++i) {
659,756✔
104
      if(get_byte<0>(length) != 0) {
421,688✔
105
         throw BER_Decoding_Error("Field length overflow");
×
106
      }
107
      if(ber->read_byte(b) == 0) {
421,688✔
108
         throw BER_Decoding_Error("Corrupted length field");
64✔
109
      }
110
      length = (length << 8) | b;
421,624✔
111
   }
112
   return length;
113
}
114

115
/*
116
* Find the EOC marker
117
*/
118
size_t find_eoc(DataSource* ber, size_t allow_indef) {
272,888✔
119
   secure_vector<uint8_t> buffer(DefaultBufferSize);
272,888✔
120
   secure_vector<uint8_t> data;
272,888✔
121

122
   while(true) {
548,161✔
123
      const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
1,096,322✔
124
      if(got == 0) {
548,161✔
125
         break;
126
      }
127

128
      data += std::make_pair(buffer.data(), got);
823,434✔
129
   }
130

131
   DataSource_Memory source(data);
277,835✔
132
   data.clear();
272,888✔
133

134
   size_t length = 0;
135
   while(true) {
1,724,234✔
136
      ASN1_Type type_tag = ASN1_Type::NoObject;
998,561✔
137
      ASN1_Class class_tag = ASN1_Class::NoObject;
998,561✔
138
      const size_t tag_size = decode_tag(&source, type_tag, class_tag);
998,561✔
139
      if(type_tag == ASN1_Type::NoObject) {
998,539✔
140
         break;
141
      }
142

143
      size_t length_size = 0;
937,472✔
144
      const size_t item_size = decode_length(&source, length_size, allow_indef);
937,472✔
145
      source.discard_next(item_size);
932,547✔
146

147
      if(auto new_len = checked_add(length, item_size, tag_size, length_size)) {
1,865,094✔
148
         length = new_len.value();
932,547✔
149
      } else {
150
         throw Decoding_Error("Integer overflow while decoding DER");
×
151
      }
152

153
      if(type_tag == ASN1_Type::Eoc && class_tag == ASN1_Class::Universal) {
932,547✔
154
         break;
155
      }
156
   }
725,673✔
157
   return length;
267,941✔
158
}
807,421✔
159

160
class DataSource_BERObject final : public DataSource {
161
   public:
162
      size_t read(uint8_t out[], size_t length) override {
28,176,600✔
163
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
28,176,600✔
164
         const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
28,176,600✔
165
         copy_mem(out, m_obj.bits() + m_offset, got);
28,176,600✔
166
         m_offset += got;
28,176,600✔
167
         return got;
28,176,600✔
168
      }
169

170
      size_t peek(uint8_t out[], size_t length, size_t peek_offset) const override {
44,239✔
171
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
44,239✔
172
         const size_t bytes_left = m_obj.length() - m_offset;
44,239✔
173

174
         if(peek_offset >= bytes_left) {
44,239✔
175
            return 0;
176
         }
177

178
         const size_t got = std::min(bytes_left - peek_offset, length);
22,048✔
179
         copy_mem(out, m_obj.bits() + m_offset + peek_offset, got);
22,048✔
180
         return got;
22,048✔
181
      }
182

183
      bool check_available(size_t n) override {
1,396,755✔
184
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
1,396,755✔
185
         return (n <= (m_obj.length() - m_offset));
1,396,755✔
186
      }
187

188
      bool end_of_data() const override { return get_bytes_read() == m_obj.length(); }
1,227,761✔
189

190
      size_t get_bytes_read() const override { return m_offset; }
1,227,761✔
191

192
      explicit DataSource_BERObject(BER_Object&& obj) : m_obj(std::move(obj)) {}
892,586✔
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
bool BER_Decoder::more_items() const {
787,887✔
205
   if(m_source->end_of_data() && !m_pushed.is_set()) {
787,887✔
206
      return false;
279,425✔
207
   }
208
   return true;
209
}
210

211
/*
212
* Verify that no bytes remain in the source
213
*/
214
BER_Decoder& BER_Decoder::verify_end() {
102,701✔
215
   return verify_end("BER_Decoder::verify_end called, but data remains");
102,701✔
216
}
217

218
/*
219
* Verify that no bytes remain in the source
220
*/
221
BER_Decoder& BER_Decoder::verify_end(std::string_view err) {
124,936✔
222
   if(!m_source->end_of_data() || m_pushed.is_set()) {
124,936✔
223
      throw Decoding_Error(err);
154✔
224
   }
225
   return (*this);
124,782✔
226
}
227

228
/*
229
* Discard all the bytes remaining in the source
230
*/
231
BER_Decoder& BER_Decoder::discard_remaining() {
7,125✔
232
   uint8_t buf = 0;
7,125✔
233
   while(m_source->read_byte(buf) != 0) {}
99,599✔
234
   return (*this);
7,125✔
235
}
236

237
const BER_Object& BER_Decoder::peek_next_object() {
19,690✔
238
   if(!m_pushed.is_set()) {
19,690✔
239
      m_pushed = get_next_object();
19,446✔
240
   }
241

242
   return m_pushed;
19,677✔
243
}
244

245
/*
246
* Return the BER encoding of the next object
247
*/
248
BER_Object BER_Decoder::get_next_object() {
2,358,229✔
249
   BER_Object next;
2,358,229✔
250

251
   if(m_pushed.is_set()) {
2,358,229✔
252
      std::swap(next, m_pushed);
182,661✔
253
      return next;
182,661✔
254
   }
255

256
   for(;;) {
12,273,670✔
257
      ASN1_Type type_tag = ASN1_Type::NoObject;
7,224,619✔
258
      ASN1_Class class_tag = ASN1_Class::NoObject;
7,224,619✔
259
      decode_tag(m_source, type_tag, class_tag);
7,224,619✔
260
      next.set_tagging(type_tag, class_tag);
7,224,540✔
261
      if(next.is_set() == false) {  // no more objects
7,224,540✔
262
         return next;
63,912✔
263
      }
264

265
      size_t field_size = 0;
7,160,628✔
266
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
7,160,628✔
267
      if(!m_source->check_available(length)) {
7,158,592✔
268
         throw BER_Decoding_Error("Value truncated");
3,823✔
269
      }
270

271
      uint8_t* out = next.mutable_bits(length);
7,154,769✔
272
      if(m_source->read(out, length) != length) {
7,154,769✔
273
         throw BER_Decoding_Error("Value truncated");
×
274
      }
275

276
      if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc)) {
7,154,769✔
277
         continue;
5,049,051✔
278
      } else {
279
         break;
280
      }
281
   }
282

283
   return next;
2,105,718✔
284
}
5,938✔
285

286
BER_Object BER_Decoder::get_next_value(size_t sizeofT, ASN1_Type type_tag, ASN1_Class class_tag) {
×
287
   const BER_Object obj = get_next_object();
×
288
   obj.assert_is_a(type_tag, class_tag);
×
289

290
   if(obj.length() != sizeofT) {
×
291
      throw BER_Decoding_Error("Size mismatch. Object value size is " + std::to_string(obj.length()) +
×
292
                               "; Output type size is " + std::to_string(sizeofT));
×
293
   }
294

295
   return obj;
×
296
}
×
297

298
/*
299
* Push a object back into the stream
300
*/
301
void BER_Decoder::push_back(const BER_Object& obj) {
324✔
302
   if(m_pushed.is_set()) {
324✔
303
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
×
304
   }
305
   m_pushed = obj;
324✔
306
}
324✔
307

308
void BER_Decoder::push_back(BER_Object&& obj) {
193,350✔
309
   if(m_pushed.is_set()) {
193,350✔
310
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
×
311
   }
312
   m_pushed = std::move(obj);
193,350✔
313
}
193,350✔
314

315
BER_Decoder BER_Decoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag) {
868,124✔
316
   BER_Object obj = get_next_object();
868,124✔
317
   obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
866,730✔
318
   return BER_Decoder(std::move(obj), this);
864,800✔
319
}
864,800✔
320

321
/*
322
* Finish decoding a CONSTRUCTED type
323
*/
324
BER_Decoder& BER_Decoder::end_cons() {
614,659✔
325
   if(m_parent == nullptr) {
614,659✔
326
      throw Invalid_State("BER_Decoder::end_cons called with null parent");
×
327
   }
328
   if(!m_source->end_of_data()) {
614,659✔
329
      throw Decoding_Error("BER_Decoder::end_cons called with data left");
367✔
330
   }
331
   return (*m_parent);
614,292✔
332
}
333

334
BER_Decoder::BER_Decoder(BER_Object&& obj, BER_Decoder* parent) : m_parent(parent) {
892,586✔
335
   m_data_src = std::make_unique<DataSource_BERObject>(std::move(obj));
892,586✔
336
   m_source = m_data_src.get();
892,586✔
337
}
892,586✔
338

339
/*
340
* BER_Decoder Constructor
341
*/
342
BER_Decoder::BER_Decoder(DataSource& src) : m_source(&src) {}
57,833✔
343

344
/*
345
* BER_Decoder Constructor
346
 */
347
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length) {
104,229✔
348
   m_data_src = std::make_unique<DataSource_Memory>(data, length);
104,229✔
349
   m_source = m_data_src.get();
104,229✔
350
}
104,229✔
351

352
/*
353
* BER_Decoder Constructor
354
*/
355
BER_Decoder::BER_Decoder(const secure_vector<uint8_t>& data) {
4,692✔
356
   m_data_src = std::make_unique<DataSource_Memory>(data);
4,692✔
357
   m_source = m_data_src.get();
4,692✔
358
}
4,692✔
359

360
/*
361
* BER_Decoder Constructor
362
*/
363
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data) {
256,890✔
364
   m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
256,890✔
365
   m_source = m_data_src.get();
256,890✔
366
}
256,890✔
367

368
/*
369
* BER_Decoder Copy Constructor
370
*/
371
BER_Decoder::BER_Decoder(const BER_Decoder& other) : m_parent(other.m_parent), m_source(other.m_source) {
178✔
372
   // take ownership of other's data source
373
   std::swap(m_data_src, other.m_data_src);
178✔
374
}
178✔
375

376
/*
377
* Request for an object to decode itself
378
*/
379
BER_Decoder& BER_Decoder::decode(ASN1_Object& obj, ASN1_Type /*unused*/, ASN1_Class /*unused*/) {
856,678✔
380
   obj.decode_from(*this);
856,678✔
381
   return (*this);
843,619✔
382
}
383

384
/*
385
* Decode a BER encoded NULL
386
*/
387
BER_Decoder& BER_Decoder::decode_null() {
111✔
388
   const BER_Object obj = get_next_object();
111✔
389
   obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
111✔
390
   if(obj.length() > 0) {
111✔
391
      throw BER_Decoding_Error("NULL object had nonzero size");
×
392
   }
393
   return (*this);
111✔
394
}
111✔
395

396
BER_Decoder& BER_Decoder::decode_octet_string_bigint(BigInt& out) {
653✔
397
   secure_vector<uint8_t> out_vec;
653✔
398
   decode(out_vec, ASN1_Type::OctetString);
653✔
399
   out = BigInt::from_bytes(out_vec);
653✔
400
   return (*this);
651✔
401
}
651✔
402

403
/*
404
* Decode a BER encoded BOOLEAN
405
*/
406
BER_Decoder& BER_Decoder::decode(bool& out, ASN1_Type type_tag, ASN1_Class class_tag) {
48,868✔
407
   const BER_Object obj = get_next_object();
48,868✔
408
   obj.assert_is_a(type_tag, class_tag);
48,937✔
409

410
   if(obj.length() != 1) {
48,868✔
411
      throw BER_Decoding_Error("BER boolean value had invalid size");
69✔
412
   }
413

414
   const uint8_t val = obj.bits()[0];
48,799✔
415

416
   // TODO if decoding DER we should reject non-canonical booleans
417
   out = (val != 0) ? true : false;
48,799✔
418

419
   return (*this);
48,799✔
420
}
48,799✔
421

422
/*
423
* Decode a small BER encoded INTEGER
424
*/
425
BER_Decoder& BER_Decoder::decode(size_t& out, ASN1_Type type_tag, ASN1_Class class_tag) {
44,653✔
426
   BigInt integer;
44,653✔
427
   decode(integer, type_tag, class_tag);
44,653✔
428

429
   if(integer.is_negative()) {
44,538✔
430
      throw BER_Decoding_Error("Decoded small integer value was negative");
112✔
431
   }
432

433
   if(integer.bits() > 32) {
44,426✔
434
      throw BER_Decoding_Error("Decoded integer value larger than expected");
29✔
435
   }
436

437
   out = 0;
44,397✔
438
   for(size_t i = 0; i != 4; ++i) {
221,985✔
439
      out = (out << 8) | integer.byte_at(3 - i);
177,588✔
440
   }
441

442
   return (*this);
44,397✔
443
}
44,653✔
444

445
/*
446
* Decode a small BER encoded INTEGER
447
*/
448
uint64_t BER_Decoder::decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes) {
3,624✔
449
   if(T_bytes > 8) {
3,624✔
450
      throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
×
451
   }
452

453
   BigInt integer;
3,624✔
454
   decode(integer, type_tag, class_tag);
3,624✔
455

456
   if(integer.bits() > 8 * T_bytes) {
3,624✔
457
      throw BER_Decoding_Error("Decoded integer value larger than expected");
×
458
   }
459

460
   uint64_t out = 0;
461
   for(size_t i = 0; i != 8; ++i) {
32,616✔
462
      out = (out << 8) | integer.byte_at(7 - i);
28,992✔
463
   }
464

465
   return out;
3,624✔
466
}
3,624✔
467

468
/*
469
* Decode a BER encoded INTEGER
470
*/
471
BER_Decoder& BER_Decoder::decode(BigInt& out, ASN1_Type type_tag, ASN1_Class class_tag) {
155,272✔
472
   const BER_Object obj = get_next_object();
155,272✔
473
   obj.assert_is_a(type_tag, class_tag);
154,778✔
474

475
   if(obj.length() == 0) {
153,457✔
476
      out.clear();
2,275✔
477
   } else {
478
      const uint8_t first = obj.bits()[0];
151,182✔
479
      const bool negative = (first & 0x80) == 0x80;
151,182✔
480

481
      if(negative) {
151,182✔
482
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
6,783✔
483
         for(size_t i = obj.length(); i > 0; --i) {
11,385✔
484
            const bool gt0 = (vec[i - 1] > 0);
11,385✔
485
            vec[i - 1] -= 1;
11,385✔
486
            if(gt0) {
11,385✔
487
               break;
488
            }
489
         }
490
         for(size_t i = 0; i != obj.length(); ++i) {
49,586✔
491
            vec[i] = ~vec[i];
44,124✔
492
         }
493
         out._assign_from_bytes(vec);
5,462✔
494
         out.flip_sign();
10,924✔
495
      } else {
5,462✔
496
         out._assign_from_bytes(obj.data());
145,720✔
497
      }
498
   }
499

500
   return (*this);
153,457✔
501
}
153,457✔
502

503
namespace {
504

505
template <typename Alloc>
506
void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer,
220,986✔
507
                               const BER_Object& obj,
508
                               ASN1_Type real_type,
509
                               ASN1_Type type_tag,
510
                               ASN1_Class class_tag) {
511
   obj.assert_is_a(type_tag, class_tag);
220,986✔
512

513
   if(real_type == ASN1_Type::OctetString) {
220,679✔
514
      buffer.assign(obj.bits(), obj.bits() + obj.length());
139,169✔
515
   } else {
516
      if(obj.length() == 0) {
81,510✔
517
         throw BER_Decoding_Error("Invalid BIT STRING");
20✔
518
      }
519
      if(obj.bits()[0] >= 8) {
81,490✔
520
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
29✔
521
      }
522

523
      buffer.resize(obj.length() - 1);
81,461✔
524

525
      if(obj.length() > 1) {
81,461✔
526
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
79,843✔
527
      }
528
   }
529
}
220,630✔
530

531
}  // namespace
532

533
/*
534
* BER decode a BIT STRING or OCTET STRING
535
*/
536
BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& buffer,
12,929✔
537
                                 ASN1_Type real_type,
538
                                 ASN1_Type type_tag,
539
                                 ASN1_Class class_tag) {
540
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
12,929✔
541
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
×
542
   }
543

544
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
12,929✔
545
   return (*this);
12,894✔
546
}
547

548
BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& buffer,
210,218✔
549
                                 ASN1_Type real_type,
550
                                 ASN1_Type type_tag,
551
                                 ASN1_Class class_tag) {
552
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
210,218✔
553
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
1,802✔
554
   }
555

556
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
208,416✔
557
   return (*this);
207,736✔
558
}
559

560
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc