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

randombit / botan / 23949924622

03 Apr 2026 02:35PM UTC coverage: 89.531% (+0.05%) from 89.479%
23949924622

Pull #5478

github

web-flow
Merge 543049103 into eaf12915e
Pull Request #5478: Add PKCS#12 KDF

105670 of 118026 relevant lines covered (89.53%)

11548366.4 hits per line

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

92.22
/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,234,444✔
31
   auto b = ber->read_byte();
8,234,444✔
32

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

39
   if((*b & 0x1F) != 0x1F) {
8,109,577✔
40
      type_tag = ASN1_Type(*b & 0x1F);
8,104,691✔
41
      class_tag = ASN1_Class(*b & 0xE0);
8,104,691✔
42
      return 1;
8,104,691✔
43
   }
44

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

48
   size_t tag_buf = 0;
4,886✔
49
   while(true) {
9,887✔
50
      b = ber->read_byte();
9,887✔
51
      if(!b) {
9,887✔
52
         throw BER_Decoding_Error("Long-form tag truncated");
87✔
53
      }
54
      if((tag_buf >> 24) != 0) {
9,800✔
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,786✔
59
         throw BER_Decoding_Error("Long form tag with leading zero");
×
60
      }
61
      ++tag_bytes;
9,786✔
62
      tag_buf = (tag_buf << 7) | (*b & 0x7F);
9,786✔
63
      if((*b & 0x80) == 0) {
9,786✔
64
         break;
65
      }
66
   }
67
   type_tag = ASN1_Type(tag_buf);
4,785✔
68
   return tag_bytes;
4,785✔
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,109,381✔
80
   uint8_t b = 0;
8,109,381✔
81
   if(ber->read_byte(b) == 0) {
8,109,381✔
82
      throw BER_Decoding_Error("Length field not found");
693✔
83
   }
84
   field_size = 1;
8,108,688✔
85
   if((b & 0x80) == 0) {
8,108,688✔
86
      return b;
7,594,921✔
87
   }
88

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

94
   if(field_size == 1) {
512,727✔
95
      if(allow_indef == 0) {
273,120✔
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,889✔
99
      }
100
   }
101

102
   size_t length = 0;
103

104
   for(size_t i = 0; i != field_size - 1; ++i) {
663,919✔
105
      if(get_byte<0>(length) != 0) {
424,376✔
106
         throw BER_Decoding_Error("Field length overflow");
×
107
      }
108
      if(ber->read_byte(b) == 0) {
424,376✔
109
         throw BER_Decoding_Error("Corrupted length field");
64✔
110
      }
111
      length = (length << 8) | b;
424,312✔
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,889✔
120
   secure_vector<uint8_t> buffer(DefaultBufferSize);
272,889✔
121
   secure_vector<uint8_t> data;
272,889✔
122

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

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

132
   DataSource_Memory source(data);
277,837✔
133
   data.clear();
272,889✔
134

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

144
      size_t length_size = 0;
937,473✔
145
      const size_t item_size = decode_length(&source, length_size, allow_indef);
937,473✔
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,423✔
160

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

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

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

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

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

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

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

193
      explicit DataSource_BERObject(BER_Object&& obj) : m_obj(std::move(obj)) {}
898,239✔
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,589,514✔
203

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

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

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

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

240
std::optional<uint8_t> BER_Decoder::read_next_byte() {
23,914,775✔
241
   BOTAN_ASSERT_NOMSG(m_source != nullptr);
23,914,775✔
242
   uint8_t b = 0;
23,914,775✔
243
   if(m_source->read_byte(b) != 0) {
23,914,775✔
244
      return b;
23,716,855✔
245
   } else {
246
      return {};
197,920✔
247
   }
248
}
249

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

255
   return m_pushed;
19,724✔
256
}
257

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

264
   if(m_pushed.is_set()) {
2,369,700✔
265
      std::swap(next, m_pushed);
182,990✔
266
      return next;
182,990✔
267
   }
268

269
   for(;;) {
12,285,054✔
270
      ASN1_Type type_tag = ASN1_Type::NoObject;
7,235,882✔
271
      ASN1_Class class_tag = ASN1_Class::NoObject;
7,235,882✔
272
      decode_tag(m_source, type_tag, class_tag);
7,235,882✔
273
      next.set_tagging(type_tag, class_tag);
7,235,803✔
274
      if(next.is_set() == false) {  // no more objects
7,235,803✔
275
         return next;
63,895✔
276
      }
277

278
      size_t field_size = 0;
7,171,908✔
279
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
7,171,908✔
280
      if(!m_source->check_available(length)) {
7,169,858✔
281
         throw BER_Decoding_Error("Value truncated");
3,920✔
282
      }
283

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

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

296
   return next;
2,116,766✔
297
}
6,049✔
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) {
326✔
315
   if(m_pushed.is_set()) {
326✔
316
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
×
317
   }
318
   m_pushed = obj;
326✔
319
}
326✔
320

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

328
BER_Decoder BER_Decoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag) {
873,519✔
329
   BER_Object obj = get_next_object();
873,519✔
330
   obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
872,076✔
331
   return BER_Decoder(std::move(obj), this);
1,740,214✔
332
}
872,076✔
333

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

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

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

357
/*
358
* BER_Decoder Constructor
359
 */
360
BER_Decoder::BER_Decoder(std::span<const uint8_t> buf) {
367,462✔
361
   m_data_src = std::make_unique<DataSource_Memory>(buf);
367,462✔
362
   m_source = m_data_src.get();
367,462✔
363
}
367,462✔
364

365
/*
366
* BER_Decoder Copy Constructor
367
*/
368
BER_Decoder::BER_Decoder(const BER_Decoder& other) : m_parent(other.m_parent), m_source(other.m_source) {
178✔
369
   // take ownership of other's data source
370
   std::swap(m_data_src, other.m_data_src);
178✔
371
}
178✔
372

373
BER_Decoder::BER_Decoder(BER_Decoder&& other) noexcept = default;
×
374

375
BER_Decoder& BER_Decoder::operator=(BER_Decoder&&) noexcept = default;
×
376

377
/*
378
* Request for an object to decode itself
379
*/
380
BER_Decoder& BER_Decoder::decode(ASN1_Object& obj, ASN1_Type /*unused*/, ASN1_Class /*unused*/) {
861,713✔
381
   obj.decode_from(*this);
861,713✔
382
   return (*this);
848,118✔
383
}
384

385
/*
386
* Decode a BER encoded NULL
387
*/
388
BER_Decoder& BER_Decoder::decode_null() {
111✔
389
   const BER_Object obj = get_next_object();
111✔
390
   obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
111✔
391
   if(obj.length() > 0) {
111✔
392
      throw BER_Decoding_Error("NULL object had nonzero size");
×
393
   }
394
   return (*this);
111✔
395
}
111✔
396

397
BER_Decoder& BER_Decoder::decode_octet_string_bigint(BigInt& out) {
653✔
398
   secure_vector<uint8_t> out_vec;
653✔
399
   decode(out_vec, ASN1_Type::OctetString);
653✔
400
   out = BigInt::from_bytes(out_vec);
653✔
401
   return (*this);
651✔
402
}
651✔
403

404
/*
405
* Decode a BER encoded BOOLEAN
406
*/
407
BER_Decoder& BER_Decoder::decode(bool& out, ASN1_Type type_tag, ASN1_Class class_tag) {
48,922✔
408
   const BER_Object obj = get_next_object();
48,922✔
409
   obj.assert_is_a(type_tag, class_tag);
48,922✔
410

411
   if(obj.length() != 1) {
48,922✔
412
      throw BER_Decoding_Error("BER boolean value had invalid size");
69✔
413
   }
414

415
   const uint8_t val = obj.bits()[0];
48,853✔
416

417
   // TODO if decoding DER we should reject non-canonical booleans
418
   out = (val != 0) ? true : false;
48,853✔
419

420
   return (*this);
48,853✔
421
}
48,922✔
422

423
/*
424
* Decode a small BER encoded INTEGER
425
*/
426
BER_Decoder& BER_Decoder::decode(size_t& out, ASN1_Type type_tag, ASN1_Class class_tag) {
45,011✔
427
   BigInt integer;
45,011✔
428
   decode(integer, type_tag, class_tag);
45,011✔
429

430
   if(integer.is_negative()) {
44,896✔
431
      throw BER_Decoding_Error("Decoded small integer value was negative");
113✔
432
   }
433

434
   if(integer.bits() > 32) {
44,783✔
435
      throw BER_Decoding_Error("Decoded integer value larger than expected");
29✔
436
   }
437

438
   out = 0;
44,754✔
439
   for(size_t i = 0; i != 4; ++i) {
223,770✔
440
      out = (out << 8) | integer.byte_at(3 - i);
179,016✔
441
   }
442

443
   return (*this);
44,754✔
444
}
45,011✔
445

446
/*
447
* Decode a small BER encoded INTEGER
448
*/
449
uint64_t BER_Decoder::decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes) {
3,616✔
450
   if(T_bytes > 8) {
3,616✔
451
      throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
×
452
   }
453

454
   BigInt integer;
3,616✔
455
   decode(integer, type_tag, class_tag);
3,616✔
456

457
   if(integer.bits() > 8 * T_bytes) {
3,616✔
458
      throw BER_Decoding_Error("Decoded integer value larger than expected");
×
459
   }
460

461
   uint64_t out = 0;
462
   for(size_t i = 0; i != 8; ++i) {
32,544✔
463
      out = (out << 8) | integer.byte_at(7 - i);
28,928✔
464
   }
465

466
   return out;
3,616✔
467
}
3,616✔
468

469
/*
470
* Decode a BER encoded INTEGER
471
*/
472
BER_Decoder& BER_Decoder::decode(BigInt& out, ASN1_Type type_tag, ASN1_Class class_tag) {
156,106✔
473
   const BER_Object obj = get_next_object();
156,106✔
474
   obj.assert_is_a(type_tag, class_tag);
155,629✔
475

476
   if(obj.length() == 0) {
154,444✔
477
      out.clear();
2,276✔
478
   } else {
479
      const uint8_t first = obj.bits()[0];
152,168✔
480
      const bool negative = (first & 0x80) == 0x80;
152,168✔
481

482
      if(negative) {
152,168✔
483
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
5,467✔
484
         for(size_t i = obj.length(); i > 0; --i) {
11,392✔
485
            const bool gt0 = (vec[i - 1] > 0);
11,392✔
486
            vec[i - 1] -= 1;
11,392✔
487
            if(gt0) {
11,392✔
488
               break;
489
            }
490
         }
491
         for(size_t i = 0; i != obj.length(); ++i) {
49,583✔
492
            vec[i] = ~vec[i];
44,116✔
493
         }
494
         out._assign_from_bytes(vec);
5,467✔
495
         out.flip_sign();
10,934✔
496
      } else {
5,467✔
497
         out._assign_from_bytes(obj.data());
146,701✔
498
      }
499
   }
500

501
   return (*this);
154,444✔
502
}
155,629✔
503

504
namespace {
505

506
template <typename Alloc>
507
void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer,
221,706✔
508
                               const BER_Object& obj,
509
                               ASN1_Type real_type,
510
                               ASN1_Type type_tag,
511
                               ASN1_Class class_tag) {
512
   obj.assert_is_a(type_tag, class_tag);
221,706✔
513

514
   if(real_type == ASN1_Type::OctetString) {
221,395✔
515
      buffer.assign(obj.bits(), obj.bits() + obj.length());
139,418✔
516
   } else {
517
      if(obj.length() == 0) {
81,977✔
518
         throw BER_Decoding_Error("Invalid BIT STRING");
20✔
519
      }
520
      if(obj.bits()[0] >= 8) {
81,957✔
521
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
30✔
522
      }
523

524
      buffer.resize(obj.length() - 1);
81,927✔
525

526
      if(obj.length() > 1) {
81,927✔
527
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
80,309✔
528
      }
529
   }
530
}
221,345✔
531

532
}  // namespace
533

534
/*
535
* BER decode a BIT STRING or OCTET STRING
536
*/
537
BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& buffer,
12,973✔
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) {
12,973✔
542
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
×
543
   }
544

545
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
12,973✔
546
   return (*this);
12,935✔
547
}
548

549
BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& buffer,
210,897✔
550
                                 ASN1_Type real_type,
551
                                 ASN1_Type type_tag,
552
                                 ASN1_Class class_tag) {
553
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
210,897✔
554
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
1,802✔
555
   }
556

557
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
209,095✔
558
   return (*this);
208,410✔
559
}
560

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