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

randombit / botan / 16052725896

03 Jul 2025 02:09PM UTC coverage: 90.57% (-0.003%) from 90.573%
16052725896

Pull #4931

github

web-flow
Merge d969ea76a into 750c4cef7
Pull Request #4931: Address various warnings from clang-tidy

99049 of 109362 relevant lines covered (90.57%)

12428027.87 hits per line

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

95.8
/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,615✔
30
   uint8_t b;
8,129,615✔
31
   if(ber->read_byte(b) == 0) {
8,129,615✔
32
      type_tag = ASN1_Type::NoObject;
124,083✔
33
      class_tag = ASN1_Class::NoObject;
124,083✔
34
      return 0;
124,083✔
35
   }
36

37
   if((b & 0x1F) != 0x1F) {
8,005,532✔
38
      type_tag = ASN1_Type(b & 0x1F);
8,000,632✔
39
      class_tag = ASN1_Class(b & 0xE0);
8,000,632✔
40
      return 1;
8,000,632✔
41
   }
42

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

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

68
/*
69
* Find the EOC marker
70
*/
71
size_t find_eoc(DataSource* src, size_t allow_indef);
72

73
/*
74
* BER decode an ASN.1 length field
75
*/
76
size_t decode_length(DataSource* ber, size_t& field_size, size_t allow_indef) {
8,005,336✔
77
   uint8_t b;
8,005,336✔
78
   if(ber->read_byte(b) == 0) {
8,005,336✔
79
      throw BER_Decoding_Error("Length field not found");
706✔
80
   }
81
   field_size = 1;
8,004,630✔
82
   if((b & 0x80) == 0) {
8,004,630✔
83
      return b;
7,510,482✔
84
   }
85

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

91
   if(field_size == 1) {
493,134✔
92
      if(allow_indef == 0) {
273,122✔
93
         throw BER_Decoding_Error("Nested EOC markers too deep, rejecting to avoid stack exhaustion");
231✔
94
      } else {
95
         return find_eoc(ber, allow_indef - 1);
272,891✔
96
      }
97
   }
98

99
   size_t length = 0;
100

101
   for(size_t i = 0; i != field_size - 1; ++i) {
610,133✔
102
      if(get_byte<0>(length) != 0) {
390,185✔
103
         throw BER_Decoding_Error("Field length overflow");
×
104
      }
105
      if(ber->read_byte(b) == 0) {
390,185✔
106
         throw BER_Decoding_Error("Corrupted length field");
64✔
107
      }
108
      length = (length << 8) | b;
390,121✔
109
   }
110
   return length;
111
}
112

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

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

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

128
   DataSource_Memory source(data);
277,838✔
129
   data.clear();
272,891✔
130

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

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

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

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

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

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

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

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

180
      bool check_available(size_t n) override {
1,332,444✔
181
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
1,332,444✔
182
         return (n <= (m_obj.length() - m_offset));
1,332,444✔
183
      }
184

185
      bool end_of_data() const override { return get_bytes_read() == m_obj.length(); }
1,173,028✔
186

187
      size_t get_bytes_read() const override { return m_offset; }
1,173,028✔
188

189
      explicit DataSource_BERObject(BER_Object&& obj) : m_obj(std::move(obj)) {}
851,597✔
190

191
   private:
192
      BER_Object m_obj;
193
      size_t m_offset = 0;
194
};
195

196
}  // namespace
197

198
/*
199
* Check if more objects are there
200
*/
201
bool BER_Decoder::more_items() const {
756,103✔
202
   if(m_source->end_of_data() && !m_pushed.is_set()) {
756,103✔
203
      return false;
268,206✔
204
   }
205
   return true;
206
}
207

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

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

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

234
const BER_Object& BER_Decoder::peek_next_object() {
18,388✔
235
   if(!m_pushed.is_set()) {
18,388✔
236
      m_pushed = get_next_object();
18,152✔
237
   }
238

239
   return m_pushed;
18,375✔
240
}
241

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

248
   if(m_pushed.is_set()) {
2,256,941✔
249
      std::swap(next, m_pushed);
174,946✔
250
      return next;
174,946✔
251
   }
252

253
   for(;;) {
12,180,101✔
254
      ASN1_Type type_tag;
7,131,048✔
255
      ASN1_Class class_tag;
7,131,048✔
256
      decode_tag(m_source, type_tag, class_tag);
7,131,048✔
257
      next.set_tagging(type_tag, class_tag);
7,130,969✔
258
      if(next.is_set() == false) {  // no more objects
7,130,969✔
259
         return next;
63,111✔
260
      }
261

262
      size_t field_size;
7,067,858✔
263
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
7,067,858✔
264
      if(!m_source->check_available(length)) {
7,065,821✔
265
         throw BER_Decoding_Error("Value truncated");
3,825✔
266
      }
267

268
      uint8_t* out = next.mutable_bits(length);
7,061,996✔
269
      if(m_source->read(out, length) != length) {
7,061,996✔
270
         throw BER_Decoding_Error("Value truncated");
×
271
      }
272

273
      if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc)) {
7,061,996✔
274
         continue;
5,049,053✔
275
      } else {
276
         break;
277
      }
278
   }
279

280
   return next;
2,012,943✔
281
}
5,941✔
282

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

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

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

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

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

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

332
/*
333
* BER_Decoder Constructor
334
 */
335
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length) {
99,382✔
336
   m_data_src = std::make_unique<DataSource_Memory>(data, length);
99,382✔
337
   m_source = m_data_src.get();
99,382✔
338
}
99,382✔
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) {
250,179✔
352
   m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
250,179✔
353
   m_source = m_data_src.get();
250,179✔
354
}
250,179✔
355

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

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

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

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

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

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

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

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

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

410
   return (*this);
47,159✔
411
}
47,159✔
412

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

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

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

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

433
   return (*this);
42,309✔
434
}
42,565✔
435

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

444
   BigInt integer;
3,616✔
445
   decode(integer, type_tag, class_tag);
3,616✔
446

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

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

456
   return out;
3,616✔
457
}
3,616✔
458

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

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

472
      if(negative) {
141,156✔
473
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
6,779✔
474
         for(size_t i = obj.length(); i > 0; --i) {
11,382✔
475
            if(vec[i - 1]--) {
11,382✔
476
               break;
477
            }
478
         }
479
         for(size_t i = 0; i != obj.length(); ++i) {
49,518✔
480
            vec[i] = ~vec[i];
44,060✔
481
         }
482
         out._assign_from_bytes(vec);
5,458✔
483
         out.flip_sign();
10,916✔
484
      } else {
5,458✔
485
         out._assign_from_bytes(obj.data());
135,698✔
486
      }
487
   }
488

489
   return (*this);
143,431✔
490
}
143,431✔
491

492
namespace {
493

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

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

512
      buffer.resize(obj.length() - 1);
76,186✔
513

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

520
}  // namespace
521

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

533
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
12,884✔
534
   return (*this);
12,848✔
535
}
536

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

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

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