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

randombit / botan / 11087146043

28 Sep 2024 09:28PM UTC coverage: 92.003% (+0.7%) from 91.274%
11087146043

push

github

web-flow
Create terraform.yml

82959 of 90170 relevant lines covered (92.0%)

9376319.11 hits per line

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

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

37
   if((b & 0x1F) != 0x1F) {
7,932,455✔
38
      type_tag = ASN1_Type(b & 0x1F);
7,923,470✔
39
      class_tag = ASN1_Class(b & 0xE0);
7,923,470✔
40
      return 1;
7,923,470✔
41
   }
42

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

46
   size_t tag_buf = 0;
8,985✔
47
   while(true) {
14,827✔
48
      if(!ber->read_byte(b)) {
14,827✔
49
         throw BER_Decoding_Error("Long-form tag truncated");
86✔
50
      }
51
      if(tag_buf & 0xFF000000) {
14,741✔
52
         throw BER_Decoding_Error("Long-form tag overflowed 32 bits");
17✔
53
      }
54
      ++tag_bytes;
14,724✔
55
      tag_buf = (tag_buf << 7) | (b & 0x7F);
14,724✔
56
      if((b & 0x80) == 0) {
14,724✔
57
         break;
58
      }
59
   }
60
   type_tag = ASN1_Type(tag_buf);
8,882✔
61
   return tag_bytes;
8,882✔
62
}
63

64
/*
65
* Find the EOC marker
66
*/
67
size_t find_eoc(DataSource* src, size_t allow_indef);
68

69
/*
70
* BER decode an ASN.1 length field
71
*/
72
size_t decode_length(DataSource* ber, size_t& field_size, size_t allow_indef) {
7,932,257✔
73
   uint8_t b;
7,932,257✔
74
   if(!ber->read_byte(b)) {
7,932,257✔
75
      throw BER_Decoding_Error("Length field not found");
720✔
76
   }
77
   field_size = 1;
7,931,537✔
78
   if((b & 0x80) == 0) {
7,931,537✔
79
      return b;
7,375,540✔
80
   }
81

82
   field_size += (b & 0x7F);
555,997✔
83
   if(field_size > 5) {
555,997✔
84
      throw BER_Decoding_Error("Length field is too large");
1,020✔
85
   }
86

87
   if(field_size == 1) {
554,977✔
88
      if(allow_indef == 0) {
354,501✔
89
         throw BER_Decoding_Error("Nested EOC markers too deep, rejecting to avoid stack exhaustion");
232✔
90
      } else {
91
         return find_eoc(ber, allow_indef - 1);
354,269✔
92
      }
93
   }
94

95
   size_t length = 0;
96

97
   for(size_t i = 0; i != field_size - 1; ++i) {
550,672✔
98
      if(get_byte<0>(length) != 0) {
350,254✔
99
         throw BER_Decoding_Error("Field length overflow");
×
100
      }
101
      if(!ber->read_byte(b)) {
350,254✔
102
         throw BER_Decoding_Error("Corrupted length field");
58✔
103
      }
104
      length = (length << 8) | b;
350,196✔
105
   }
106
   return length;
107
}
108

109
/*
110
* Find the EOC marker
111
*/
112
size_t find_eoc(DataSource* ber, size_t allow_indef) {
354,269✔
113
   secure_vector<uint8_t> buffer(BOTAN_DEFAULT_BUFFER_SIZE), data;
354,269✔
114

115
   while(true) {
1,030,213✔
116
      const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
692,241✔
117
      if(got == 0) {
692,241✔
118
         break;
119
      }
120

121
      data += std::make_pair(buffer.data(), got);
337,972✔
122
   }
337,972✔
123

124
   DataSource_Memory source(data);
354,269✔
125
   data.clear();
354,269✔
126

127
   size_t length = 0;
128
   while(true) {
2,199,963✔
129
      ASN1_Type type_tag;
1,277,116✔
130
      ASN1_Class class_tag;
1,277,116✔
131
      const size_t tag_size = decode_tag(&source, type_tag, class_tag);
1,277,116✔
132
      if(type_tag == ASN1_Type::NoObject) {
1,277,094✔
133
         break;
134
      }
135

136
      size_t length_size = 0;
1,130,491✔
137
      const size_t item_size = decode_length(&source, length_size, allow_indef);
1,130,491✔
138
      source.discard_next(item_size);
1,125,552✔
139

140
      if(auto new_len = checked_add(length, item_size, tag_size, length_size)) {
1,125,552✔
141
         length = new_len.value();
1,125,552✔
142
      } else {
143
         throw Decoding_Error("Integer overflow while decoding DER");
×
144
      }
145

146
      if(type_tag == ASN1_Type::Eoc && class_tag == ASN1_Class::Universal) {
1,125,552✔
147
         break;
148
      }
149
   }
922,847✔
150
   return length;
349,308✔
151
}
1,032,925✔
152

153
class DataSource_BERObject final : public DataSource {
154
   public:
155
      size_t read(uint8_t out[], size_t length) override {
23,337,032✔
156
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
23,337,032✔
157
         const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
23,337,032✔
158
         copy_mem(out, m_obj.bits() + m_offset, got);
23,337,032✔
159
         m_offset += got;
23,337,032✔
160
         return got;
23,337,032✔
161
      }
162

163
      size_t peek(uint8_t out[], size_t length, size_t peek_offset) const override {
107,675✔
164
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
107,675✔
165
         const size_t bytes_left = m_obj.length() - m_offset;
107,675✔
166

167
         if(peek_offset >= bytes_left) {
107,675✔
168
            return 0;
169
         }
170

171
         const size_t got = std::min(bytes_left - peek_offset, length);
52,353✔
172
         copy_mem(out, m_obj.bits() + m_offset + peek_offset, got);
52,353✔
173
         return got;
52,353✔
174
      }
175

176
      bool check_available(size_t n) override {
1,223,393✔
177
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
1,223,393✔
178
         return (n <= (m_obj.length() - m_offset));
1,223,393✔
179
      }
180

181
      bool end_of_data() const override { return get_bytes_read() == m_obj.length(); }
1,057,064✔
182

183
      size_t get_bytes_read() const override { return m_offset; }
1,057,064✔
184

185
      explicit DataSource_BERObject(BER_Object&& obj) : m_obj(std::move(obj)), m_offset(0) {}
739,039✔
186

187
   private:
188
      BER_Object m_obj;
189
      size_t m_offset;
190
};
191

192
}  // namespace
193

194
/*
195
* Check if more objects are there
196
*/
197
bool BER_Decoder::more_items() const {
698,424✔
198
   if(m_source->end_of_data() && !m_pushed.is_set()) {
698,424✔
199
      return false;
228,742✔
200
   }
201
   return true;
202
}
203

204
/*
205
* Verify that no bytes remain in the source
206
*/
207
BER_Decoder& BER_Decoder::verify_end() {
71,211✔
208
   return verify_end("BER_Decoder::verify_end called, but data remains");
71,211✔
209
}
210

211
/*
212
* Verify that no bytes remain in the source
213
*/
214
BER_Decoder& BER_Decoder::verify_end(std::string_view err) {
89,048✔
215
   if(!m_source->end_of_data() || m_pushed.is_set()) {
89,048✔
216
      throw Decoding_Error(err);
274✔
217
   }
218
   return (*this);
88,774✔
219
}
220

221
/*
222
* Discard all the bytes remaining in the source
223
*/
224
BER_Decoder& BER_Decoder::discard_remaining() {
8,718✔
225
   uint8_t buf;
8,718✔
226
   while(m_source->read_byte(buf)) {}
120,940✔
227
   return (*this);
8,718✔
228
}
229

230
/*
231
* Return the BER encoding of the next object
232
*/
233
BER_Object BER_Decoder::get_next_object() {
1,953,160✔
234
   BER_Object next;
1,953,160✔
235

236
   if(m_pushed.is_set()) {
1,953,160✔
237
      std::swap(next, m_pushed);
147,293✔
238
      return next;
147,293✔
239
   }
240

241
   for(;;) {
6,852,789✔
242
      ASN1_Type type_tag;
6,852,789✔
243
      ASN1_Class class_tag;
6,852,789✔
244
      decode_tag(m_source, type_tag, class_tag);
6,852,789✔
245
      next.set_tagging(type_tag, class_tag);
6,852,708✔
246
      if(next.is_set() == false) {  // no more objects
6,852,708✔
247
         return next;
50,942✔
248
      }
249

250
      size_t field_size;
6,801,766✔
251
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
6,801,766✔
252
      if(!m_source->check_available(length)) {
6,799,714✔
253
         throw BER_Decoding_Error("Value truncated");
2,684✔
254
      }
255

256
      uint8_t* out = next.mutable_bits(length);
6,797,030✔
257
      if(m_source->read(out, length) != length) {
6,797,030✔
258
         throw BER_Decoding_Error("Value truncated");
×
259
      }
260

261
      if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc)) {
6,797,030✔
262
         continue;
5,046,922✔
263
      } else {
264
         break;
265
      }
266
   }
267

268
   return next;
1,750,108✔
269
}
4,817✔
270

271
/*
272
* Push a object back into the stream
273
*/
274
void BER_Decoder::push_back(const BER_Object& obj) {
7,163✔
275
   if(m_pushed.is_set()) {
7,163✔
276
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
×
277
   }
278
   m_pushed = obj;
7,163✔
279
}
7,163✔
280

281
void BER_Decoder::push_back(BER_Object&& obj) {
172,607✔
282
   if(m_pushed.is_set()) {
172,607✔
283
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
×
284
   }
285
   m_pushed = std::move(obj);
172,607✔
286
}
172,607✔
287

288
BER_Decoder BER_Decoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag) {
722,100✔
289
   BER_Object obj = get_next_object();
722,100✔
290
   obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
720,670✔
291
   return BER_Decoder(std::move(obj), this);
717,391✔
292
}
717,391✔
293

294
/*
295
* Finish decoding a CONSTRUCTED type
296
*/
297
BER_Decoder& BER_Decoder::end_cons() {
507,607✔
298
   if(!m_parent) {
507,607✔
299
      throw Invalid_State("BER_Decoder::end_cons called with null parent");
×
300
   }
301
   if(!m_source->end_of_data()) {
507,607✔
302
      throw Decoding_Error("BER_Decoder::end_cons called with data left");
422✔
303
   }
304
   return (*m_parent);
507,185✔
305
}
306

307
BER_Decoder::BER_Decoder(BER_Object&& obj, BER_Decoder* parent) {
739,039✔
308
   m_data_src = std::make_unique<DataSource_BERObject>(std::move(obj));
739,039✔
309
   m_source = m_data_src.get();
739,039✔
310
   m_parent = parent;
739,039✔
311
}
739,039✔
312

313
/*
314
* BER_Decoder Constructor
315
*/
316
BER_Decoder::BER_Decoder(DataSource& src) {
41,639✔
317
   m_source = &src;
41,639✔
318
}
41,639✔
319

320
/*
321
* BER_Decoder Constructor
322
 */
323
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length) {
72,427✔
324
   m_data_src = std::make_unique<DataSource_Memory>(data, length);
72,427✔
325
   m_source = m_data_src.get();
72,427✔
326
}
72,427✔
327

328
/*
329
* BER_Decoder Constructor
330
*/
331
BER_Decoder::BER_Decoder(const secure_vector<uint8_t>& data) {
4,378✔
332
   m_data_src = std::make_unique<DataSource_Memory>(data);
4,378✔
333
   m_source = m_data_src.get();
4,378✔
334
}
4,378✔
335

336
/*
337
* BER_Decoder Constructor
338
*/
339
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data) {
189,200✔
340
   m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
189,200✔
341
   m_source = m_data_src.get();
189,200✔
342
}
189,200✔
343

344
/*
345
* BER_Decoder Copy Constructor
346
*/
347
BER_Decoder::BER_Decoder(const BER_Decoder& other) {
178✔
348
   m_source = other.m_source;
178✔
349

350
   // take ownership
351
   std::swap(m_data_src, other.m_data_src);
178✔
352
   m_parent = other.m_parent;
178✔
353
}
178✔
354

355
/*
356
* Request for an object to decode itself
357
*/
358
BER_Decoder& BER_Decoder::decode(ASN1_Object& obj, ASN1_Type /*unused*/, ASN1_Class /*unused*/) {
709,585✔
359
   obj.decode_from(*this);
709,585✔
360
   return (*this);
697,448✔
361
}
362

363
/*
364
* Decode a BER encoded NULL
365
*/
366
BER_Decoder& BER_Decoder::decode_null() {
×
367
   BER_Object obj = get_next_object();
×
368
   obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
×
369
   if(obj.length() > 0) {
×
370
      throw BER_Decoding_Error("NULL object had nonzero size");
×
371
   }
372
   return (*this);
×
373
}
×
374

375
BER_Decoder& BER_Decoder::decode_octet_string_bigint(BigInt& out) {
1,536✔
376
   secure_vector<uint8_t> out_vec;
1,536✔
377
   decode(out_vec, ASN1_Type::OctetString);
1,536✔
378
   out = BigInt::decode(out_vec.data(), out_vec.size());
1,532✔
379
   return (*this);
1,532✔
380
}
1,532✔
381

382
/*
383
* Decode a BER encoded BOOLEAN
384
*/
385
BER_Decoder& BER_Decoder::decode(bool& out, ASN1_Type type_tag, ASN1_Class class_tag) {
36,289✔
386
   BER_Object obj = get_next_object();
36,289✔
387
   obj.assert_is_a(type_tag, class_tag);
36,354✔
388

389
   if(obj.length() != 1) {
36,289✔
390
      throw BER_Decoding_Error("BER boolean value had invalid size");
65✔
391
   }
392

393
   out = (obj.bits()[0]) ? true : false;
36,224✔
394
   return (*this);
36,224✔
395
}
36,224✔
396

397
/*
398
* Decode a small BER encoded INTEGER
399
*/
400
BER_Decoder& BER_Decoder::decode(size_t& out, ASN1_Type type_tag, ASN1_Class class_tag) {
36,723✔
401
   BigInt integer;
36,723✔
402
   decode(integer, type_tag, class_tag);
36,723✔
403

404
   if(integer.is_negative()) {
36,597✔
405
      throw BER_Decoding_Error("Decoded small integer value was negative");
524✔
406
   }
407

408
   if(integer.bits() > 32) {
36,073✔
409
      throw BER_Decoding_Error("Decoded integer value larger than expected");
36✔
410
   }
411

412
   out = 0;
36,037✔
413
   for(size_t i = 0; i != 4; ++i) {
180,185✔
414
      out = (out << 8) | integer.byte_at(3 - i);
144,148✔
415
   }
416

417
   return (*this);
36,037✔
418
}
36,037✔
419

420
/*
421
* Decode a small BER encoded INTEGER
422
*/
423
uint64_t BER_Decoder::decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes) {
3,624✔
424
   if(T_bytes > 8) {
3,624✔
425
      throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
×
426
   }
427

428
   BigInt integer;
3,624✔
429
   decode(integer, type_tag, class_tag);
3,624✔
430

431
   if(integer.bits() > 8 * T_bytes) {
3,624✔
432
      throw BER_Decoding_Error("Decoded integer value larger than expected");
×
433
   }
434

435
   uint64_t out = 0;
436
   for(size_t i = 0; i != 8; ++i) {
32,616✔
437
      out = (out << 8) | integer.byte_at(7 - i);
28,992✔
438
   }
439

440
   return out;
3,624✔
441
}
3,624✔
442

443
/*
444
* Decode a BER encoded INTEGER
445
*/
446
BER_Decoder& BER_Decoder::decode(BigInt& out, ASN1_Type type_tag, ASN1_Class class_tag) {
132,501✔
447
   BER_Object obj = get_next_object();
132,501✔
448
   obj.assert_is_a(type_tag, class_tag);
131,978✔
449

450
   if(obj.length() == 0) {
130,628✔
451
      out.clear();
3,378✔
452
   } else {
453
      const bool negative = (obj.bits()[0] & 0x80) ? true : false;
127,250✔
454

455
      if(negative) {
127,250✔
456
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
5,020✔
457
         for(size_t i = obj.length(); i > 0; --i) {
10,146✔
458
            if(vec[i - 1]--) {
10,146✔
459
               break;
460
            }
461
         }
462
         for(size_t i = 0; i != obj.length(); ++i) {
53,229✔
463
            vec[i] = ~vec[i];
49,559✔
464
         }
465
         out = BigInt(vec.data(), vec.size());
3,670✔
466
         out.flip_sign();
7,340✔
467
      } else {
3,670✔
468
         out = BigInt(obj.bits(), obj.length());
123,580✔
469
      }
470
   }
471

472
   return (*this);
130,628✔
473
}
130,628✔
474

475
namespace {
476

477
template <typename Alloc>
478
void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer,
174,069✔
479
                               const BER_Object& obj,
480
                               ASN1_Type real_type,
481
                               ASN1_Type type_tag,
482
                               ASN1_Class class_tag) {
483
   obj.assert_is_a(type_tag, class_tag);
174,069✔
484

485
   if(real_type == ASN1_Type::OctetString) {
173,694✔
486
      buffer.assign(obj.bits(), obj.bits() + obj.length());
112,975✔
487
   } else {
488
      if(obj.length() == 0) {
60,719✔
489
         throw BER_Decoding_Error("Invalid BIT STRING");
18✔
490
      }
491
      if(obj.bits()[0] >= 8) {
60,701✔
492
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
21✔
493
      }
494

495
      buffer.resize(obj.length() - 1);
60,680✔
496

497
      if(obj.length() > 1) {
60,680✔
498
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
59,094✔
499
      }
500
   }
501
}
173,655✔
502

503
}  // namespace
504

505
/*
506
* BER decode a BIT STRING or OCTET STRING
507
*/
508
BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& buffer,
9,628✔
509
                                 ASN1_Type real_type,
510
                                 ASN1_Type type_tag,
511
                                 ASN1_Class class_tag) {
512
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
9,628✔
513
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
×
514
   }
515

516
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
9,628✔
517
   return (*this);
9,582✔
518
}
519

520
BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& buffer,
166,546✔
521
                                 ASN1_Type real_type,
522
                                 ASN1_Type type_tag,
523
                                 ASN1_Class class_tag) {
524
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
166,546✔
525
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
1,749✔
526
   }
527

528
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
164,797✔
529
   return (*this);
164,073✔
530
}
531

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