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

randombit / botan / 16395825001

19 Jul 2025 11:30PM UTC coverage: 90.635% (-0.07%) from 90.708%
16395825001

push

github

web-flow
Merge pull request #4998 from randombit/jack/fix-clang-tidy-readability-isolate-declaration

Enable and fix clang-tidy warning readability-isolate-declaration

99940 of 110266 relevant lines covered (90.64%)

12341110.13 hits per line

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

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

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

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

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

93
   if(field_size == 1) {
494,024✔
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,334✔
104
      if(get_byte<0>(length) != 0) {
391,496✔
105
         throw BER_Decoding_Error("Field length overflow");
×
106
      }
107
      if(ber->read_byte(b) == 0) {
391,496✔
108
         throw BER_Decoding_Error("Corrupted length field");
64✔
109
      }
110
      length = (length << 8) | b;
391,432✔
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);
272,891✔
120
   secure_vector<uint8_t> data;
272,891✔
121

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

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

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

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

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

147
      if(auto new_len = checked_add(length, item_size, tag_size, length_size)) {
1,865,106✔
148
         length = new_len.value();
932,553✔
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,553✔
154
         break;
155
      }
156
   }
725,676✔
157
   return length;
267,944✔
158
}
807,430✔
159

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

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

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

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

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

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

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

192
      explicit DataSource_BERObject(BER_Object&& obj) : m_obj(std::move(obj)) {}
858,682✔
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 {
763,444✔
205
   if(m_source->end_of_data() && !m_pushed.is_set()) {
763,444✔
206
      return false;
270,594✔
207
   }
208
   return true;
209
}
210

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

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

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

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

242
   return m_pushed;
19,612✔
243
}
244

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

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

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

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

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

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

283
   return next;
2,029,831✔
284
}
5,941✔
285

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

407
   return (*this);
47,497✔
408
}
47,497✔
409

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

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

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

425
   out = 0;
43,405✔
426
   for(size_t i = 0; i != 4; ++i) {
217,025✔
427
      out = (out << 8) | integer.byte_at(3 - i);
173,620✔
428
   }
429

430
   return (*this);
43,405✔
431
}
43,661✔
432

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

441
   BigInt integer;
3,632✔
442
   decode(integer, type_tag, class_tag);
3,632✔
443

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

448
   uint64_t out = 0;
449
   for(size_t i = 0; i != 8; ++i) {
32,688✔
450
      out = (out << 8) | integer.byte_at(7 - i);
29,056✔
451
   }
452

453
   return out;
3,632✔
454
}
3,632✔
455

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

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

469
      if(negative) {
142,749✔
470
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
6,780✔
471
         for(size_t i = obj.length(); i > 0; --i) {
11,382✔
472
            if(vec[i - 1]--) {
11,382✔
473
               break;
474
            }
475
         }
476
         for(size_t i = 0; i != obj.length(); ++i) {
49,543✔
477
            vec[i] = ~vec[i];
44,084✔
478
         }
479
         out._assign_from_bytes(vec);
5,459✔
480
         out.flip_sign();
10,918✔
481
      } else {
5,459✔
482
         out._assign_from_bytes(obj.data());
137,290✔
483
      }
484
   }
485

486
   return (*this);
145,024✔
487
}
145,024✔
488

489
namespace {
490

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

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

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

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

517
}  // namespace
518

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

530
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
12,888✔
531
   return (*this);
12,854✔
532
}
533

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

542
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
200,271✔
543
   return (*this);
199,592✔
544
}
545

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