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

randombit / botan / 21888402193

11 Feb 2026 12:53AM UTC coverage: 90.068% (-1.6%) from 91.638%
21888402193

push

github

web-flow
Merge pull request #5302 from randombit/jack/header-patrol-4

Cleanup various header inclusions

102229 of 113502 relevant lines covered (90.07%)

11489469.59 hits per line

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

92.45
/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/data_src.h>
12
#include <botan/internal/int_utils.h>
13
#include <botan/internal/loadstor.h>
14
#include <memory>
15

16
namespace Botan {
17

18
namespace {
19

20
/*
21
* This value is somewhat arbitrary. OpenSSL allows up to 128 nested
22
* indefinite length sequences. If you increase this, also increase the
23
* limit in the test in test_asn1.cpp
24
*/
25
const size_t ALLOWED_EOC_NESTINGS = 16;
26

27
/*
28
* BER decode an ASN.1 type tag
29
*/
30
size_t decode_tag(DataSource* ber, ASN1_Type& type_tag, ASN1_Class& class_tag) {
8,223,604✔
31
   auto b = ber->read_byte();
8,223,604✔
32

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

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

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

48
   size_t tag_buf = 0;
4,900✔
49
   while(true) {
9,901✔
50
      b = ber->read_byte();
9,901✔
51
      if(!b) {
9,901✔
52
         throw BER_Decoding_Error("Long-form tag truncated");
87✔
53
      }
54
      if((tag_buf >> 24) != 0) {
9,814✔
55
         throw BER_Decoding_Error("Long-form tag overflowed 32 bits");
14✔
56
      }
57
      // This is required even by BER (see X.690 section 8.1.2.4.2 sentence c)
58
      if(tag_bytes == 0 && b == 0x80) {
9,800✔
59
         throw BER_Decoding_Error("Long form tag with leading zero");
×
60
      }
61
      ++tag_bytes;
9,800✔
62
      tag_buf = (tag_buf << 7) | (*b & 0x7F);
9,800✔
63
      if((*b & 0x80) == 0) {
9,800✔
64
         break;
65
      }
66
   }
67
   type_tag = ASN1_Type(tag_buf);
4,799✔
68
   return tag_bytes;
4,799✔
69
}
70

71
/*
72
* Find the EOC marker
73
*/
74
size_t find_eoc(DataSource* src, size_t allow_indef);
75

76
/*
77
* BER decode an ASN.1 length field
78
*/
79
size_t decode_length(DataSource* ber, size_t& field_size, size_t allow_indef) {
8,098,520✔
80
   uint8_t b = 0;
8,098,520✔
81
   if(ber->read_byte(b) == 0) {
8,098,520✔
82
      throw BER_Decoding_Error("Length field not found");
708✔
83
   }
84
   field_size = 1;
8,097,812✔
85
   if((b & 0x80) == 0) {
8,097,812✔
86
      return b;
7,585,491✔
87
   }
88

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

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

102
   size_t length = 0;
103

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

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

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

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

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

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

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

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

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

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

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

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

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

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

189
      bool end_of_data() const override { return get_bytes_read() == m_obj.length(); }
1,228,012✔
190

191
      size_t get_bytes_read() const override { return m_offset; }
1,228,012✔
192

193
      explicit DataSource_BERObject(BER_Object&& obj) : m_obj(std::move(obj)) {}
892,783✔
194

195
   private:
196
      BER_Object m_obj;
197
      size_t m_offset = 0;
198
};
199

200
}  // namespace
201

202
BER_Decoder::~BER_Decoder() = default;
2,575,860✔
203

204
/*
205
* Check if more objects are there
206
*/
207
bool BER_Decoder::more_items() const {
788,036✔
208
   if(m_source->end_of_data() && !m_pushed.is_set()) {
788,036✔
209
      return false;
279,480✔
210
   }
211
   return true;
212
}
213

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

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

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

240
std::optional<uint8_t> BER_Decoder::read_next_byte() {
23,668,260✔
241
   BOTAN_ASSERT_NOMSG(m_source != nullptr);
23,668,260✔
242
   uint8_t b = 0;
23,668,260✔
243
   if(m_source->read_byte(b) != 0) {
23,668,260✔
244
      return b;
23,471,976✔
245
   } else {
246
      return {};
196,284✔
247
   }
248
}
249

250
const BER_Object& BER_Decoder::peek_next_object() {
19,694✔
251
   if(!m_pushed.is_set()) {
19,694✔
252
      m_pushed = get_next_object();
19,450✔
253
   }
254

255
   return m_pushed;
19,681✔
256
}
257

258
/*
259
* Return the BER encoding of the next object
260
*/
261
BER_Object BER_Decoder::get_next_object() {
2,358,702✔
262
   BER_Object next;
2,358,702✔
263

264
   if(m_pushed.is_set()) {
2,358,702✔
265
      std::swap(next, m_pushed);
182,710✔
266
      return next;
182,710✔
267
   }
268

269
   for(;;) {
12,274,094✔
270
      ASN1_Type type_tag = ASN1_Type::NoObject;
7,225,043✔
271
      ASN1_Class class_tag = ASN1_Class::NoObject;
7,225,043✔
272
      decode_tag(m_source, type_tag, class_tag);
7,225,043✔
273
      next.set_tagging(type_tag, class_tag);
7,224,964✔
274
      if(next.is_set() == false) {  // no more objects
7,224,964✔
275
         return next;
63,916✔
276
      }
277

278
      size_t field_size = 0;
7,161,048✔
279
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
7,161,048✔
280
      if(!m_source->check_available(length)) {
7,159,007✔
281
         throw BER_Decoding_Error("Value truncated");
3,824✔
282
      }
283

284
      uint8_t* out = next.mutable_bits(length);
7,155,183✔
285
      if(m_source->read(out, length) != length) {
7,155,183✔
286
         throw BER_Decoding_Error("Value truncated");
×
287
      }
288

289
      if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc)) {
7,155,183✔
290
         continue;
5,049,051✔
291
      } else {
292
         break;
293
      }
294
   }
295

296
   return next;
2,106,132✔
297
}
5,944✔
298

299
BER_Object BER_Decoder::get_next_value(size_t sizeofT, ASN1_Type type_tag, ASN1_Class class_tag) {
×
300
   const BER_Object obj = get_next_object();
×
301
   obj.assert_is_a(type_tag, class_tag);
×
302

303
   if(obj.length() != sizeofT) {
×
304
      throw BER_Decoding_Error("Size mismatch. Object value size is " + std::to_string(obj.length()) +
×
305
                               "; Output type size is " + std::to_string(sizeofT));
×
306
   }
307

308
   return obj;
×
309
}
×
310

311
/*
312
* Push a object back into the stream
313
*/
314
void BER_Decoder::push_back(const BER_Object& obj) {
324✔
315
   if(m_pushed.is_set()) {
324✔
316
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
×
317
   }
318
   m_pushed = obj;
324✔
319
}
324✔
320

321
void BER_Decoder::push_back(BER_Object&& obj) {
193,401✔
322
   if(m_pushed.is_set()) {
193,401✔
323
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
×
324
   }
325
   m_pushed = std::move(obj);
193,401✔
326
}
193,401✔
327

328
BER_Decoder BER_Decoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag) {
868,315✔
329
   BER_Object obj = get_next_object();
868,315✔
330
   obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
866,918✔
331
   return BER_Decoder(std::move(obj), this);
864,991✔
332
}
864,991✔
333

334
/*
335
* Finish decoding a CONSTRUCTED type
336
*/
337
BER_Decoder& BER_Decoder::end_cons() {
614,800✔
338
   if(m_parent == nullptr) {
614,800✔
339
      throw Invalid_State("BER_Decoder::end_cons called with null parent");
×
340
   }
341
   if(!m_source->end_of_data()) {
614,800✔
342
      throw Decoding_Error("BER_Decoder::end_cons called with data left");
367✔
343
   }
344
   return (*m_parent);
614,433✔
345
}
346

347
BER_Decoder::BER_Decoder(BER_Object&& obj, BER_Decoder* parent) : m_parent(parent) {
892,783✔
348
   m_data_src = std::make_unique<DataSource_BERObject>(std::move(obj));
892,783✔
349
   m_source = m_data_src.get();
892,783✔
350
}
892,783✔
351

352
/*
353
* BER_Decoder Constructor
354
*/
355
BER_Decoder::BER_Decoder(DataSource& src) : m_source(&src) {}
57,362✔
356

357
/*
358
* BER_Decoder Constructor
359
 */
360
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length) {
104,744✔
361
   m_data_src = std::make_unique<DataSource_Memory>(data, length);
104,744✔
362
   m_source = m_data_src.get();
104,744✔
363
}
104,744✔
364

365
/*
366
* BER_Decoder Constructor
367
*/
368
BER_Decoder::BER_Decoder(const secure_vector<uint8_t>& data) {
4,692✔
369
   m_data_src = std::make_unique<DataSource_Memory>(data);
4,692✔
370
   m_source = m_data_src.get();
4,692✔
371
}
4,692✔
372

373
/*
374
* BER_Decoder Constructor
375
*/
376
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data) {
256,941✔
377
   m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
256,941✔
378
   m_source = m_data_src.get();
256,941✔
379
}
256,941✔
380

381
/*
382
* BER_Decoder Copy Constructor
383
*/
384
BER_Decoder::BER_Decoder(const BER_Decoder& other) : m_parent(other.m_parent), m_source(other.m_source) {
178✔
385
   // take ownership of other's data source
386
   std::swap(m_data_src, other.m_data_src);
178✔
387
}
178✔
388

389
BER_Decoder::BER_Decoder(BER_Decoder&& other) noexcept = default;
×
390

391
BER_Decoder& BER_Decoder::operator=(BER_Decoder&&) noexcept = default;
×
392

393
/*
394
* Request for an object to decode itself
395
*/
396
BER_Decoder& BER_Decoder::decode(ASN1_Object& obj, ASN1_Type /*unused*/, ASN1_Class /*unused*/) {
856,861✔
397
   obj.decode_from(*this);
856,861✔
398
   return (*this);
843,802✔
399
}
400

401
/*
402
* Decode a BER encoded NULL
403
*/
404
BER_Decoder& BER_Decoder::decode_null() {
111✔
405
   const BER_Object obj = get_next_object();
111✔
406
   obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
111✔
407
   if(obj.length() > 0) {
111✔
408
      throw BER_Decoding_Error("NULL object had nonzero size");
×
409
   }
410
   return (*this);
111✔
411
}
111✔
412

413
BER_Decoder& BER_Decoder::decode_octet_string_bigint(BigInt& out) {
653✔
414
   secure_vector<uint8_t> out_vec;
653✔
415
   decode(out_vec, ASN1_Type::OctetString);
653✔
416
   out = BigInt::from_bytes(out_vec);
653✔
417
   return (*this);
651✔
418
}
651✔
419

420
/*
421
* Decode a BER encoded BOOLEAN
422
*/
423
BER_Decoder& BER_Decoder::decode(bool& out, ASN1_Type type_tag, ASN1_Class class_tag) {
48,874✔
424
   const BER_Object obj = get_next_object();
48,874✔
425
   obj.assert_is_a(type_tag, class_tag);
48,943✔
426

427
   if(obj.length() != 1) {
48,874✔
428
      throw BER_Decoding_Error("BER boolean value had invalid size");
69✔
429
   }
430

431
   const uint8_t val = obj.bits()[0];
48,805✔
432

433
   // TODO if decoding DER we should reject non-canonical booleans
434
   out = (val != 0) ? true : false;
48,805✔
435

436
   return (*this);
48,805✔
437
}
48,805✔
438

439
/*
440
* Decode a small BER encoded INTEGER
441
*/
442
BER_Decoder& BER_Decoder::decode(size_t& out, ASN1_Type type_tag, ASN1_Class class_tag) {
44,656✔
443
   BigInt integer;
44,656✔
444
   decode(integer, type_tag, class_tag);
44,656✔
445

446
   if(integer.is_negative()) {
44,541✔
447
      throw BER_Decoding_Error("Decoded small integer value was negative");
112✔
448
   }
449

450
   if(integer.bits() > 32) {
44,429✔
451
      throw BER_Decoding_Error("Decoded integer value larger than expected");
29✔
452
   }
453

454
   out = 0;
44,400✔
455
   for(size_t i = 0; i != 4; ++i) {
222,000✔
456
      out = (out << 8) | integer.byte_at(3 - i);
177,600✔
457
   }
458

459
   return (*this);
44,400✔
460
}
44,656✔
461

462
/*
463
* Decode a small BER encoded INTEGER
464
*/
465
uint64_t BER_Decoder::decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes) {
3,608✔
466
   if(T_bytes > 8) {
3,608✔
467
      throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
×
468
   }
469

470
   BigInt integer;
3,608✔
471
   decode(integer, type_tag, class_tag);
3,608✔
472

473
   if(integer.bits() > 8 * T_bytes) {
3,608✔
474
      throw BER_Decoding_Error("Decoded integer value larger than expected");
×
475
   }
476

477
   uint64_t out = 0;
478
   for(size_t i = 0; i != 8; ++i) {
32,472✔
479
      out = (out << 8) | integer.byte_at(7 - i);
28,864✔
480
   }
481

482
   return out;
3,608✔
483
}
3,608✔
484

485
/*
486
* Decode a BER encoded INTEGER
487
*/
488
BER_Decoder& BER_Decoder::decode(BigInt& out, ASN1_Type type_tag, ASN1_Class class_tag) {
155,289✔
489
   const BER_Object obj = get_next_object();
155,289✔
490
   obj.assert_is_a(type_tag, class_tag);
154,794✔
491

492
   if(obj.length() == 0) {
153,472✔
493
      out.clear();
2,276✔
494
   } else {
495
      const uint8_t first = obj.bits()[0];
151,196✔
496
      const bool negative = (first & 0x80) == 0x80;
151,196✔
497

498
      if(negative) {
151,196✔
499
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
6,785✔
500
         for(size_t i = obj.length(); i > 0; --i) {
11,386✔
501
            const bool gt0 = (vec[i - 1] > 0);
11,386✔
502
            vec[i - 1] -= 1;
11,386✔
503
            if(gt0) {
11,386✔
504
               break;
505
            }
506
         }
507
         for(size_t i = 0; i != obj.length(); ++i) {
49,620✔
508
            vec[i] = ~vec[i];
44,157✔
509
         }
510
         out._assign_from_bytes(vec);
5,463✔
511
         out.flip_sign();
10,926✔
512
      } else {
5,463✔
513
         out._assign_from_bytes(obj.data());
145,733✔
514
      }
515
   }
516

517
   return (*this);
153,472✔
518
}
153,472✔
519

520
namespace {
521

522
template <typename Alloc>
523
void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer,
221,038✔
524
                               const BER_Object& obj,
525
                               ASN1_Type real_type,
526
                               ASN1_Type type_tag,
527
                               ASN1_Class class_tag) {
528
   obj.assert_is_a(type_tag, class_tag);
221,038✔
529

530
   if(real_type == ASN1_Type::OctetString) {
220,732✔
531
      buffer.assign(obj.bits(), obj.bits() + obj.length());
139,198✔
532
   } else {
533
      if(obj.length() == 0) {
81,534✔
534
         throw BER_Decoding_Error("Invalid BIT STRING");
20✔
535
      }
536
      if(obj.bits()[0] >= 8) {
81,514✔
537
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
29✔
538
      }
539

540
      buffer.resize(obj.length() - 1);
81,485✔
541

542
      if(obj.length() > 1) {
81,485✔
543
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
79,867✔
544
      }
545
   }
546
}
220,683✔
547

548
}  // namespace
549

550
/*
551
* BER decode a BIT STRING or OCTET STRING
552
*/
553
BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& buffer,
12,928✔
554
                                 ASN1_Type real_type,
555
                                 ASN1_Type type_tag,
556
                                 ASN1_Class class_tag) {
557
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
12,928✔
558
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
×
559
   }
560

561
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
12,928✔
562
   return (*this);
12,892✔
563
}
564

565
BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& buffer,
210,273✔
566
                                 ASN1_Type real_type,
567
                                 ASN1_Type type_tag,
568
                                 ASN1_Class class_tag) {
569
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
210,273✔
570
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
1,802✔
571
   }
572

573
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
208,471✔
574
   return (*this);
207,791✔
575
}
576

577
}  // 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