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

randombit / botan / 16091978769

05 Jul 2025 08:56PM UTC coverage: 90.571% (+0.001%) from 90.57%
16091978769

Pull #4959

github

web-flow
Merge 4fb822b02 into c4f937d19
Pull Request #4959: Enable and fix clang-tidy warning cppcoreguidelines-init-variables

99048 of 109359 relevant lines covered (90.57%)

12464451.9 hits per line

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

95.82
/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,129,630✔
30
   auto b = ber->read_byte();
8,129,630✔
31

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

38
   if((*b & 0x1F) != 0x1F) {
8,005,549✔
39
      type_tag = ASN1_Type(*b & 0x1F);
8,000,649✔
40
      class_tag = ASN1_Class(*b & 0xE0);
8,000,649✔
41
      return 1;
8,000,649✔
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,005,353✔
79
   uint8_t b = 0;
8,005,353✔
80
   if(ber->read_byte(b) == 0) {
8,005,353✔
81
      throw BER_Decoding_Error("Length field not found");
708✔
82
   }
83
   field_size = 1;
8,004,645✔
84
   if((b & 0x80) == 0) {
8,004,645✔
85
      return b;
7,510,497✔
86
   }
87

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

93
   if(field_size == 1) {
493,134✔
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) {
610,133✔
104
      if(get_byte<0>(length) != 0) {
390,185✔
105
         throw BER_Decoding_Error("Field length overflow");
×
106
      }
107
      if(ber->read_byte(b) == 0) {
390,185✔
108
         throw BER_Decoding_Error("Corrupted length field");
64✔
109
      }
110
      length = (length << 8) | b;
390,121✔
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,294,126✔
162
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
27,294,126✔
163
         const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
27,294,126✔
164
         copy_mem(out, m_obj.bits() + m_offset, got);
27,294,126✔
165
         m_offset += got;
27,294,126✔
166
         return got;
27,294,126✔
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,332,460✔
183
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
1,332,460✔
184
         return (n <= (m_obj.length() - m_offset));
1,332,460✔
185
      }
186

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

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

191
      explicit DataSource_BERObject(BER_Object&& obj) : m_obj(std::move(obj)) {}
851,601✔
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 {
756,099✔
204
   if(m_source->end_of_data() && !m_pushed.is_set()) {
756,099✔
205
      return false;
268,204✔
206
   }
207
   return true;
208
}
209

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

217
/*
218
* Verify that no bytes remain in the source
219
*/
220
BER_Decoder& BER_Decoder::verify_end(std::string_view err) {
120,849✔
221
   if(!m_source->end_of_data() || m_pushed.is_set()) {
120,849✔
222
      throw Decoding_Error(err);
154✔
223
   }
224
   return (*this);
120,695✔
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() {
18,388✔
237
   if(!m_pushed.is_set()) {
18,388✔
238
      m_pushed = get_next_object();
18,152✔
239
   }
240

241
   return m_pushed;
18,375✔
242
}
243

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

250
   if(m_pushed.is_set()) {
2,256,958✔
251
      std::swap(next, m_pushed);
174,946✔
252
      return next;
174,946✔
253
   }
254

255
   for(;;) {
12,180,114✔
256
      ASN1_Type type_tag = ASN1_Type::NoObject;
7,131,063✔
257
      ASN1_Class class_tag = ASN1_Class::NoObject;
7,131,063✔
258
      decode_tag(m_source, type_tag, class_tag);
7,131,063✔
259
      next.set_tagging(type_tag, class_tag);
7,130,984✔
260
      if(next.is_set() == false) {  // no more objects
7,130,984✔
261
         return next;
63,109✔
262
      }
263

264
      size_t field_size = 0;
7,067,875✔
265
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
7,067,875✔
266
      if(!m_source->check_available(length)) {
7,065,836✔
267
         throw BER_Decoding_Error("Value truncated");
3,822✔
268
      }
269

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

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

282
   return next;
2,012,963✔
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) {
186,357✔
296
   if(m_pushed.is_set()) {
186,357✔
297
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
×
298
   }
299
   m_pushed = std::move(obj);
186,357✔
300
}
186,357✔
301

302
BER_Decoder BER_Decoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag) {
827,917✔
303
   BER_Object obj = get_next_object();
827,917✔
304
   obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
826,523✔
305
   return BER_Decoder(std::move(obj), this);
824,597✔
306
}
824,597✔
307

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

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

327
/*
328
* BER_Decoder Constructor
329
*/
330
BER_Decoder::BER_Decoder(DataSource& src) {
53,271✔
331
   m_source = &src;
53,271✔
332
}
53,271✔
333

334
/*
335
* BER_Decoder Constructor
336
 */
337
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length) {
99,382✔
338
   m_data_src = std::make_unique<DataSource_Memory>(data, length);
99,382✔
339
   m_source = m_data_src.get();
99,382✔
340
}
99,382✔
341

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

350
/*
351
* BER_Decoder Constructor
352
*/
353
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data) {
250,179✔
354
   m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
250,179✔
355
   m_source = m_data_src.get();
250,179✔
356
}
250,179✔
357

358
/*
359
* BER_Decoder Copy Constructor
360
*/
361
BER_Decoder::BER_Decoder(const BER_Decoder& other) {
178✔
362
   m_source = other.m_source;
178✔
363

364
   // take ownership
365
   std::swap(m_data_src, other.m_data_src);
178✔
366
   m_parent = other.m_parent;
178✔
367
}
178✔
368

369
/*
370
* Request for an object to decode itself
371
*/
372
BER_Decoder& BER_Decoder::decode(ASN1_Object& obj, ASN1_Type /*unused*/, ASN1_Class /*unused*/) {
819,373✔
373
   obj.decode_from(*this);
819,373✔
374
   return (*this);
806,318✔
375
}
376

377
/*
378
* Decode a BER encoded NULL
379
*/
380
BER_Decoder& BER_Decoder::decode_null() {
89✔
381
   BER_Object obj = get_next_object();
89✔
382
   obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
89✔
383
   if(obj.length() > 0) {
89✔
384
      throw BER_Decoding_Error("NULL object had nonzero size");
×
385
   }
386
   return (*this);
89✔
387
}
89✔
388

389
BER_Decoder& BER_Decoder::decode_octet_string_bigint(BigInt& out) {
617✔
390
   secure_vector<uint8_t> out_vec;
617✔
391
   decode(out_vec, ASN1_Type::OctetString);
617✔
392
   out = BigInt::from_bytes(out_vec);
617✔
393
   return (*this);
615✔
394
}
615✔
395

396
/*
397
* Decode a BER encoded BOOLEAN
398
*/
399
BER_Decoder& BER_Decoder::decode(bool& out, ASN1_Type type_tag, ASN1_Class class_tag) {
47,228✔
400
   BER_Object obj = get_next_object();
47,228✔
401
   obj.assert_is_a(type_tag, class_tag);
47,297✔
402

403
   if(obj.length() != 1) {
47,228✔
404
      throw BER_Decoding_Error("BER boolean value had invalid size");
69✔
405
   }
406

407
   const uint8_t val = obj.bits()[0];
47,159✔
408

409
   // TODO if decoding DER we should reject non-canonical booleans
410
   out = (val != 0) ? true : false;
47,159✔
411

412
   return (*this);
47,159✔
413
}
47,159✔
414

415
/*
416
* Decode a small BER encoded INTEGER
417
*/
418
BER_Decoder& BER_Decoder::decode(size_t& out, ASN1_Type type_tag, ASN1_Class class_tag) {
42,565✔
419
   BigInt integer;
42,565✔
420
   decode(integer, type_tag, class_tag);
42,565✔
421

422
   if(integer.is_negative()) {
42,450✔
423
      throw BER_Decoding_Error("Decoded small integer value was negative");
112✔
424
   }
425

426
   if(integer.bits() > 32) {
42,338✔
427
      throw BER_Decoding_Error("Decoded integer value larger than expected");
29✔
428
   }
429

430
   out = 0;
42,309✔
431
   for(size_t i = 0; i != 4; ++i) {
211,545✔
432
      out = (out << 8) | integer.byte_at(3 - i);
169,236✔
433
   }
434

435
   return (*this);
42,309✔
436
}
42,565✔
437

438
/*
439
* Decode a small BER encoded INTEGER
440
*/
441
uint64_t BER_Decoder::decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes) {
3,616✔
442
   if(T_bytes > 8) {
3,616✔
443
      throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
×
444
   }
445

446
   BigInt integer;
3,616✔
447
   decode(integer, type_tag, class_tag);
3,616✔
448

449
   if(integer.bits() > 8 * T_bytes) {
3,616✔
450
      throw BER_Decoding_Error("Decoded integer value larger than expected");
×
451
   }
452

453
   uint64_t out = 0;
454
   for(size_t i = 0; i != 8; ++i) {
32,544✔
455
      out = (out << 8) | integer.byte_at(7 - i);
28,928✔
456
   }
457

458
   return out;
3,616✔
459
}
3,616✔
460

461
/*
462
* Decode a BER encoded INTEGER
463
*/
464
BER_Decoder& BER_Decoder::decode(BigInt& out, ASN1_Type type_tag, ASN1_Class class_tag) {
145,251✔
465
   BER_Object obj = get_next_object();
145,251✔
466
   obj.assert_is_a(type_tag, class_tag);
144,758✔
467

468
   if(obj.length() == 0) {
143,436✔
469
      out.clear();
2,275✔
470
   } else {
471
      const uint8_t first = obj.bits()[0];
141,161✔
472
      const bool negative = (first & 0x80) == 0x80;
141,161✔
473

474
      if(negative) {
141,161✔
475
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
6,780✔
476
         for(size_t i = obj.length(); i > 0; --i) {
11,381✔
477
            if(vec[i - 1]--) {
11,381✔
478
               break;
479
            }
480
         }
481
         for(size_t i = 0; i != obj.length(); ++i) {
49,512✔
482
            vec[i] = ~vec[i];
44,054✔
483
         }
484
         out._assign_from_bytes(vec);
5,458✔
485
         out.flip_sign();
10,916✔
486
      } else {
5,458✔
487
         out._assign_from_bytes(obj.data());
135,703✔
488
      }
489
   }
490

491
   return (*this);
143,436✔
492
}
143,436✔
493

494
namespace {
495

496
template <typename Alloc>
497
void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer,
210,903✔
498
                               const BER_Object& obj,
499
                               ASN1_Type real_type,
500
                               ASN1_Type type_tag,
501
                               ASN1_Class class_tag) {
502
   obj.assert_is_a(type_tag, class_tag);
210,903✔
503

504
   if(real_type == ASN1_Type::OctetString) {
210,596✔
505
      buffer.assign(obj.bits(), obj.bits() + obj.length());
134,361✔
506
   } else {
507
      if(obj.length() == 0) {
76,235✔
508
         throw BER_Decoding_Error("Invalid BIT STRING");
20✔
509
      }
510
      if(obj.bits()[0] >= 8) {
76,215✔
511
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
29✔
512
      }
513

514
      buffer.resize(obj.length() - 1);
76,186✔
515

516
      if(obj.length() > 1) {
76,186✔
517
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
74,568✔
518
      }
519
   }
520
}
210,547✔
521

522
}  // namespace
523

524
/*
525
* BER decode a BIT STRING or OCTET STRING
526
*/
527
BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& buffer,
12,896✔
528
                                 ASN1_Type real_type,
529
                                 ASN1_Type type_tag,
530
                                 ASN1_Class class_tag) {
531
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
12,896✔
532
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
×
533
   }
534

535
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
12,896✔
536
   return (*this);
12,857✔
537
}
538

539
BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& buffer,
200,171✔
540
                                 ASN1_Type real_type,
541
                                 ASN1_Type type_tag,
542
                                 ASN1_Class class_tag) {
543
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
200,171✔
544
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
1,802✔
545
   }
546

547
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
198,369✔
548
   return (*this);
197,690✔
549
}
550

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