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

randombit / botan / 16249365818

13 Jul 2025 12:56PM UTC coverage: 90.618% (+0.002%) from 90.616%
16249365818

Pull #4985

github

web-flow
Merge 34acb0b10 into cf74a5db8
Pull Request #4985: Enable and fix clang-tidy warning cppcoreguidelines-prefer-member-initializer

99527 of 109831 relevant lines covered (90.62%)

12274327.89 hits per line

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

95.74
/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,146,708✔
30
   auto b = ber->read_byte();
8,146,708✔
31

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

38
   if((*b & 0x1F) != 0x1F) {
8,022,411✔
39
      type_tag = ASN1_Type(*b & 0x1F);
8,017,511✔
40
      class_tag = ASN1_Class(*b & 0xE0);
8,017,511✔
41
      return 1;
8,017,511✔
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,022,215✔
79
   uint8_t b = 0;
8,022,215✔
80
   if(ber->read_byte(b) == 0) {
8,022,215✔
81
      throw BER_Decoding_Error("Length field not found");
708✔
82
   }
83
   field_size = 1;
8,021,507✔
84
   if((b & 0x80) == 0) {
8,021,507✔
85
      return b;
7,526,531✔
86
   }
87

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

93
   if(field_size == 1) {
493,963✔
94
      if(allow_indef == 0) {
273,122✔
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,891✔
98
      }
99
   }
100

101
   size_t length = 0;
102

103
   for(size_t i = 0; i != field_size - 1; ++i) {
612,212✔
104
      if(get_byte<0>(length) != 0) {
391,435✔
105
         throw BER_Decoding_Error("Field length overflow");
×
106
      }
107
      if(ber->read_byte(b) == 0) {
391,435✔
108
         throw BER_Decoding_Error("Corrupted length field");
64✔
109
      }
110
      length = (length << 8) | b;
391,371✔
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,891✔
119
   secure_vector<uint8_t> buffer(DefaultBufferSize), data;
272,891✔
120

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

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

130
   DataSource_Memory source(data);
277,838✔
131
   data.clear();
272,891✔
132

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

142
      size_t length_size = 0;
937,478✔
143
      const size_t item_size = decode_length(&source, length_size, allow_indef);
937,478✔
144
      source.discard_next(item_size);
932,553✔
145

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

152
      if(type_tag == ASN1_Type::Eoc && class_tag == ASN1_Class::Universal) {
932,553✔
153
         break;
154
      }
155
   }
725,676✔
156
   return length;
267,944✔
157
}
807,430✔
158

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

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

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

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

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

187
      bool end_of_data() const override { return get_bytes_read() == m_obj.length(); }
1,184,121✔
188

189
      size_t get_bytes_read() const override { return m_offset; }
1,184,121✔
190

191
      explicit DataSource_BERObject(BER_Object&& obj) : m_obj(std::move(obj)) {}
858,680✔
192

193
   private:
194
      BER_Object m_obj;
195
      size_t m_offset = 0;
196
};
197

198
}  // namespace
199

200
/*
201
* Check if more objects are there
202
*/
203
bool BER_Decoder::more_items() const {
763,434✔
204
   if(m_source->end_of_data() && !m_pushed.is_set()) {
763,434✔
205
      return false;
270,589✔
206
   }
207
   return true;
208
}
209

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

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

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

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

241
   return m_pushed;
19,612✔
242
}
243

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

250
   if(m_pushed.is_set()) {
2,276,763✔
251
      std::swap(next, m_pushed);
177,673✔
252
      return next;
177,673✔
253
   }
254

255
   for(;;) {
12,197,192✔
256
      ASN1_Type type_tag = ASN1_Type::NoObject;
7,148,141✔
257
      ASN1_Class class_tag = ASN1_Class::NoObject;
7,148,141✔
258
      decode_tag(m_source, type_tag, class_tag);
7,148,141✔
259
      next.set_tagging(type_tag, class_tag);
7,148,062✔
260
      if(next.is_set() == false) {  // no more objects
7,148,062✔
261
         return next;
63,325✔
262
      }
263

264
      size_t field_size = 0;
7,084,737✔
265
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
7,084,737✔
266
      if(!m_source->check_available(length)) {
7,082,699✔
267
         throw BER_Decoding_Error("Value truncated");
3,823✔
268
      }
269

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

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

282
   return next;
2,029,825✔
283
}
5,940✔
284

285
/*
286
* Push a object back into the stream
287
*/
288
void BER_Decoder::push_back(const BER_Object& obj) {
324✔
289
   if(m_pushed.is_set()) {
324✔
290
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
×
291
   }
292
   m_pushed = obj;
324✔
293
}
324✔
294

295
void BER_Decoder::push_back(BER_Object&& obj) {
188,014✔
296
   if(m_pushed.is_set()) {
188,014✔
297
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
×
298
   }
299
   m_pushed = std::move(obj);
188,014✔
300
}
188,014✔
301

302
BER_Decoder BER_Decoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag) {
834,889✔
303
   BER_Object obj = get_next_object();
834,889✔
304
   obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
833,495✔
305
   return BER_Decoder(std::move(obj), this);
831,567✔
306
}
831,567✔
307

308
/*
309
* Finish decoding a CONSTRUCTED type
310
*/
311
BER_Decoder& BER_Decoder::end_cons() {
589,131✔
312
   if(m_parent == nullptr) {
589,131✔
313
      throw Invalid_State("BER_Decoder::end_cons called with null parent");
×
314
   }
315
   if(!m_source->end_of_data()) {
589,131✔
316
      throw Decoding_Error("BER_Decoder::end_cons called with data left");
370✔
317
   }
318
   return (*m_parent);
588,761✔
319
}
320

321
BER_Decoder::BER_Decoder(BER_Object&& obj, BER_Decoder* parent) : m_parent(parent) {
858,680✔
322
   m_data_src = std::make_unique<DataSource_BERObject>(std::move(obj));
858,680✔
323
   m_source = m_data_src.get();
858,680✔
324
}
858,680✔
325

326
/*
327
* BER_Decoder Constructor
328
*/
329
BER_Decoder::BER_Decoder(DataSource& src) : m_source(&src) {}
53,715✔
330

331
/*
332
* BER_Decoder Constructor
333
 */
334
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length) {
100,192✔
335
   m_data_src = std::make_unique<DataSource_Memory>(data, length);
100,192✔
336
   m_source = m_data_src.get();
100,192✔
337
}
100,192✔
338

339
/*
340
* BER_Decoder Constructor
341
*/
342
BER_Decoder::BER_Decoder(const secure_vector<uint8_t>& data) {
4,695✔
343
   m_data_src = std::make_unique<DataSource_Memory>(data);
4,695✔
344
   m_source = m_data_src.get();
4,695✔
345
}
4,695✔
346

347
/*
348
* BER_Decoder Constructor
349
*/
350
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data) {
251,835✔
351
   m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
251,835✔
352
   m_source = m_data_src.get();
251,835✔
353
}
251,835✔
354

355
/*
356
* BER_Decoder Copy Constructor
357
*/
358
BER_Decoder::BER_Decoder(const BER_Decoder& other) : m_parent(other.m_parent), m_source(other.m_source) {
178✔
359
   // take ownership of other's data source
360
   std::swap(m_data_src, other.m_data_src);
178✔
361
}
178✔
362

363
/*
364
* Request for an object to decode itself
365
*/
366
BER_Decoder& BER_Decoder::decode(ASN1_Object& obj, ASN1_Type /*unused*/, ASN1_Class /*unused*/) {
826,402✔
367
   obj.decode_from(*this);
826,402✔
368
   return (*this);
813,344✔
369
}
370

371
/*
372
* Decode a BER encoded NULL
373
*/
374
BER_Decoder& BER_Decoder::decode_null() {
111✔
375
   BER_Object obj = get_next_object();
111✔
376
   obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
111✔
377
   if(obj.length() > 0) {
111✔
378
      throw BER_Decoding_Error("NULL object had nonzero size");
×
379
   }
380
   return (*this);
111✔
381
}
111✔
382

383
BER_Decoder& BER_Decoder::decode_octet_string_bigint(BigInt& out) {
617✔
384
   secure_vector<uint8_t> out_vec;
617✔
385
   decode(out_vec, ASN1_Type::OctetString);
617✔
386
   out = BigInt::from_bytes(out_vec);
617✔
387
   return (*this);
615✔
388
}
615✔
389

390
/*
391
* Decode a BER encoded BOOLEAN
392
*/
393
BER_Decoder& BER_Decoder::decode(bool& out, ASN1_Type type_tag, ASN1_Class class_tag) {
47,563✔
394
   BER_Object obj = get_next_object();
47,563✔
395
   obj.assert_is_a(type_tag, class_tag);
47,632✔
396

397
   if(obj.length() != 1) {
47,563✔
398
      throw BER_Decoding_Error("BER boolean value had invalid size");
69✔
399
   }
400

401
   const uint8_t val = obj.bits()[0];
47,494✔
402

403
   // TODO if decoding DER we should reject non-canonical booleans
404
   out = (val != 0) ? true : false;
47,494✔
405

406
   return (*this);
47,494✔
407
}
47,494✔
408

409
/*
410
* Decode a small BER encoded INTEGER
411
*/
412
BER_Decoder& BER_Decoder::decode(size_t& out, ASN1_Type type_tag, ASN1_Class class_tag) {
43,658✔
413
   BigInt integer;
43,658✔
414
   decode(integer, type_tag, class_tag);
43,658✔
415

416
   if(integer.is_negative()) {
43,543✔
417
      throw BER_Decoding_Error("Decoded small integer value was negative");
112✔
418
   }
419

420
   if(integer.bits() > 32) {
43,431✔
421
      throw BER_Decoding_Error("Decoded integer value larger than expected");
29✔
422
   }
423

424
   out = 0;
43,402✔
425
   for(size_t i = 0; i != 4; ++i) {
217,010✔
426
      out = (out << 8) | integer.byte_at(3 - i);
173,608✔
427
   }
428

429
   return (*this);
43,402✔
430
}
43,658✔
431

432
/*
433
* Decode a small BER encoded INTEGER
434
*/
435
uint64_t BER_Decoder::decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes) {
3,624✔
436
   if(T_bytes > 8) {
3,624✔
437
      throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
×
438
   }
439

440
   BigInt integer;
3,624✔
441
   decode(integer, type_tag, class_tag);
3,624✔
442

443
   if(integer.bits() > 8 * T_bytes) {
3,624✔
444
      throw BER_Decoding_Error("Decoded integer value larger than expected");
×
445
   }
446

447
   uint64_t out = 0;
448
   for(size_t i = 0; i != 8; ++i) {
32,616✔
449
      out = (out << 8) | integer.byte_at(7 - i);
28,992✔
450
   }
451

452
   return out;
3,624✔
453
}
3,624✔
454

455
/*
456
* Decode a BER encoded INTEGER
457
*/
458
BER_Decoder& BER_Decoder::decode(BigInt& out, ASN1_Type type_tag, ASN1_Class class_tag) {
146,831✔
459
   BER_Object obj = get_next_object();
146,831✔
460
   obj.assert_is_a(type_tag, class_tag);
146,336✔
461

462
   if(obj.length() == 0) {
145,016✔
463
      out.clear();
2,275✔
464
   } else {
465
      const uint8_t first = obj.bits()[0];
142,741✔
466
      const bool negative = (first & 0x80) == 0x80;
142,741✔
467

468
      if(negative) {
142,741✔
469
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
6,777✔
470
         for(size_t i = obj.length(); i > 0; --i) {
11,380✔
471
            if(vec[i - 1]--) {
11,380✔
472
               break;
473
            }
474
         }
475
         for(size_t i = 0; i != obj.length(); ++i) {
49,482✔
476
            vec[i] = ~vec[i];
44,025✔
477
         }
478
         out._assign_from_bytes(vec);
5,457✔
479
         out.flip_sign();
10,914✔
480
      } else {
5,457✔
481
         out._assign_from_bytes(obj.data());
137,284✔
482
      }
483
   }
484

485
   return (*this);
145,016✔
486
}
145,016✔
487

488
namespace {
489

490
template <typename Alloc>
491
void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer,
212,807✔
492
                               const BER_Object& obj,
493
                               ASN1_Type real_type,
494
                               ASN1_Type type_tag,
495
                               ASN1_Class class_tag) {
496
   obj.assert_is_a(type_tag, class_tag);
212,807✔
497

498
   if(real_type == ASN1_Type::OctetString) {
212,502✔
499
      buffer.assign(obj.bits(), obj.bits() + obj.length());
135,714✔
500
   } else {
501
      if(obj.length() == 0) {
76,788✔
502
         throw BER_Decoding_Error("Invalid BIT STRING");
20✔
503
      }
504
      if(obj.bits()[0] >= 8) {
76,768✔
505
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
29✔
506
      }
507

508
      buffer.resize(obj.length() - 1);
76,739✔
509

510
      if(obj.length() > 1) {
76,739✔
511
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
75,121✔
512
      }
513
   }
514
}
212,453✔
515

516
}  // namespace
517

518
/*
519
* BER decode a BIT STRING or OCTET STRING
520
*/
521
BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& buffer,
12,897✔
522
                                 ASN1_Type real_type,
523
                                 ASN1_Type type_tag,
524
                                 ASN1_Class class_tag) {
525
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
12,897✔
526
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
×
527
   }
528

529
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
12,897✔
530
   return (*this);
12,862✔
531
}
532

533
BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& buffer,
202,072✔
534
                                 ASN1_Type real_type,
535
                                 ASN1_Type type_tag,
536
                                 ASN1_Class class_tag) {
537
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
202,072✔
538
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
1,802✔
539
   }
540

541
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
200,270✔
542
   return (*this);
199,591✔
543
}
544

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