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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

94.05
/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/loadstor.h>
12
#include <botan/internal/safeint.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) {
7,113,028✔
30
   uint8_t b;
7,113,028✔
31
   if(!ber->read_byte(b)) {
7,113,028✔
32
      type_tag = ASN1_Type::NoObject;
71,148✔
33
      class_tag = ASN1_Class::NoObject;
71,148✔
34
      return 0;
71,148✔
35
   }
36

37
   if((b & 0x1F) != 0x1F) {
7,041,880✔
38
      type_tag = ASN1_Type(b & 0x1F);
7,037,098✔
39
      class_tag = ASN1_Class(b & 0xE0);
7,037,098✔
40
      return 1;
7,037,098✔
41
   }
42

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

46
   size_t tag_buf = 0;
4,782✔
47
   while(true) {
9,325✔
48
      if(!ber->read_byte(b))
9,325✔
49
         throw BER_Decoding_Error("Long-form tag truncated");
111✔
50
      if(tag_buf & 0xFF000000)
9,214✔
51
         throw BER_Decoding_Error("Long-form tag overflowed 32 bits");
17✔
52
      ++tag_bytes;
9,197✔
53
      tag_buf = (tag_buf << 7) | (b & 0x7F);
9,197✔
54
      if((b & 0x80) == 0)
9,197✔
55
         break;
56
   }
57
   type_tag = ASN1_Type(tag_buf);
4,654✔
58
   return tag_bytes;
4,654✔
59
}
60

61
/*
62
* Find the EOC marker
63
*/
64
size_t find_eoc(DataSource* src, size_t allow_indef);
65

66
/*
67
* BER decode an ASN.1 length field
68
*/
69
size_t decode_length(DataSource* ber, size_t& field_size, size_t allow_indef) {
7,041,657✔
70
   uint8_t b;
7,041,657✔
71
   if(!ber->read_byte(b))
7,041,657✔
72
      throw BER_Decoding_Error("Length field not found");
1,952✔
73
   field_size = 1;
7,039,705✔
74
   if((b & 0x80) == 0)
7,039,705✔
75
      return b;
6,658,522✔
76

77
   field_size += (b & 0x7F);
381,183✔
78
   if(field_size > 5)
381,183✔
79
      throw BER_Decoding_Error("Length field is too large");
948✔
80

81
   if(field_size == 1) {
380,235✔
82
      if(allow_indef == 0) {
200,216✔
83
         throw BER_Decoding_Error("Nested EOC markers too deep, rejecting to avoid stack exhaustion");
229✔
84
      } else {
85
         return find_eoc(ber, allow_indef - 1);
199,987✔
86
      }
87
   }
88

89
   size_t length = 0;
90

91
   for(size_t i = 0; i != field_size - 1; ++i) {
493,091✔
92
      if(get_byte<0>(length) != 0)
313,173✔
93
         throw BER_Decoding_Error("Field length overflow");
×
94
      if(!ber->read_byte(b))
313,173✔
95
         throw BER_Decoding_Error("Corrupted length field");
101✔
96
      length = (length << 8) | b;
313,072✔
97
   }
98
   return length;
99
}
100

101
/*
102
* Find the EOC marker
103
*/
104
size_t find_eoc(DataSource* ber, size_t allow_indef) {
199,987✔
105
   secure_vector<uint8_t> buffer(BOTAN_DEFAULT_BUFFER_SIZE), data;
199,987✔
106

107
   while(true) {
611,903✔
108
      const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
405,945✔
109
      if(got == 0)
405,945✔
110
         break;
111

112
      data += std::make_pair(buffer.data(), got);
205,958✔
113
   }
205,958✔
114

115
   DataSource_Memory source(data);
199,987✔
116
   data.clear();
199,987✔
117

118
   size_t length = 0;
119
   while(true) {
1,232,555✔
120
      ASN1_Type type_tag;
716,271✔
121
      ASN1_Class class_tag;
716,271✔
122
      size_t tag_size = decode_tag(&source, type_tag, class_tag);
716,271✔
123
      if(type_tag == ASN1_Type::NoObject)
716,224✔
124
         break;
125

126
      size_t length_size = 0;
682,195✔
127
      size_t item_size = decode_length(&source, length_size, allow_indef);
682,195✔
128
      source.discard_next(item_size);
673,989✔
129

130
      length = BOTAN_CHECKED_ADD(length, item_size);
673,989✔
131
      length = BOTAN_CHECKED_ADD(length, tag_size);
673,989✔
132
      length = BOTAN_CHECKED_ADD(length, length_size);
673,989✔
133

134
      if(type_tag == ASN1_Type::Eoc && class_tag == ASN1_Class::Universal)
673,989✔
135
         break;
136
   }
516,284✔
137
   return length;
191,734✔
138
}
589,650✔
139

140
class DataSource_BERObject final : public DataSource {
141
   public:
142
      size_t read(uint8_t out[], size_t length) override {
18,274,312✔
143
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
18,274,312✔
144
         const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
18,274,312✔
145
         copy_mem(out, m_obj.bits() + m_offset, got);
18,274,312✔
146
         m_offset += got;
18,274,312✔
147
         return got;
18,274,312✔
148
      }
149

150
      size_t peek(uint8_t out[], size_t length, size_t peek_offset) const override {
12,900✔
151
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
12,900✔
152
         const size_t bytes_left = m_obj.length() - m_offset;
12,900✔
153

154
         if(peek_offset >= bytes_left)
12,900✔
155
            return 0;
156

157
         const size_t got = std::min(bytes_left - peek_offset, length);
6,446✔
158
         copy_mem(out, m_obj.bits() + peek_offset, got);
6,446✔
159
         return got;
6,446✔
160
      }
161

162
      bool check_available(size_t n) override {
891,790✔
163
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
891,790✔
164
         return (n <= (m_obj.length() - m_offset));
891,790✔
165
      }
166

167
      bool end_of_data() const override { return get_bytes_read() == m_obj.length(); }
762,652✔
168

169
      size_t get_bytes_read() const override { return m_offset; }
762,652✔
170

171
      explicit DataSource_BERObject(BER_Object&& obj) : m_obj(std::move(obj)), m_offset(0) {}
551,839✔
172

173
   private:
174
      BER_Object m_obj;
175
      size_t m_offset;
176
};
177

178
}
179

180
/*
181
* Check if more objects are there
182
*/
183
bool BER_Decoder::more_items() const {
496,763✔
184
   if(m_source->end_of_data() && !m_pushed.is_set())
496,763✔
185
      return false;
173,927✔
186
   return true;
187
}
188

189
/*
190
* Verify that no bytes remain in the source
191
*/
192
BER_Decoder& BER_Decoder::verify_end() { return verify_end("BER_Decoder::verify_end called, but data remains"); }
56,566✔
193

194
/*
195
* Verify that no bytes remain in the source
196
*/
197
BER_Decoder& BER_Decoder::verify_end(std::string_view err) {
71,614✔
198
   if(!m_source->end_of_data() || m_pushed.is_set())
71,614✔
199
      throw Decoding_Error(err);
191✔
200
   return (*this);
71,423✔
201
}
202

203
/*
204
* Discard all the bytes remaining in the source
205
*/
206
BER_Decoder& BER_Decoder::discard_remaining() {
5,775✔
207
   uint8_t buf;
5,775✔
208
   while(m_source->read_byte(buf)) {}
92,650✔
209
   return (*this);
5,775✔
210
}
211

212
/*
213
* Return the BER encoding of the next object
214
*/
215
BER_Object BER_Decoder::get_next_object() {
1,458,395✔
216
   BER_Object next;
1,458,395✔
217

218
   if(m_pushed.is_set()) {
1,458,395✔
219
      std::swap(next, m_pushed);
101,158✔
220
      return next;
101,158✔
221
   }
222

223
   for(;;) {
6,396,757✔
224
      ASN1_Type type_tag;
6,396,757✔
225
      ASN1_Class class_tag;
6,396,757✔
226
      decode_tag(m_source, type_tag, class_tag);
6,396,757✔
227
      next.set_tagging(type_tag, class_tag);
6,396,676✔
228
      if(next.is_set() == false)  // no more objects
6,396,676✔
229
         return next;
37,214✔
230

231
      size_t field_size;
6,359,462✔
232
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
6,359,462✔
233
      if(!m_source->check_available(length))
6,356,185✔
234
         throw BER_Decoding_Error("Value truncated");
4,619✔
235

236
      uint8_t* out = next.mutable_bits(length);
6,351,566✔
237
      if(m_source->read(out, length) != length)
6,351,566✔
238
         throw BER_Decoding_Error("Value truncated");
×
239

240
      if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc))
6,351,566✔
241
         continue;
5,039,520✔
242
      else
243
         break;
244
   }
245

246
   return next;
1,312,046✔
247
}
7,977✔
248

249
/*
250
* Push a object back into the stream
251
*/
252
void BER_Decoder::push_back(const BER_Object& obj) {
1,184✔
253
   if(m_pushed.is_set())
1,184✔
254
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
×
255
   m_pushed = obj;
1,184✔
256
}
1,184✔
257

258
void BER_Decoder::push_back(BER_Object&& obj) {
118,996✔
259
   if(m_pushed.is_set())
118,996✔
260
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
×
261
   m_pushed = std::move(obj);
118,996✔
262
}
118,996✔
263

264
BER_Decoder BER_Decoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag) {
539,813✔
265
   BER_Object obj = get_next_object();
539,813✔
266
   obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
536,215✔
267
   return BER_Decoder(std::move(obj), this);
534,467✔
268
}
534,467✔
269

270
/*
271
* Finish decoding a CONSTRUCTED type
272
*/
273
BER_Decoder& BER_Decoder::end_cons() {
378,248✔
274
   if(!m_parent)
378,248✔
275
      throw Invalid_State("BER_Decoder::end_cons called with null parent");
×
276
   if(!m_source->end_of_data())
378,248✔
277
      throw Decoding_Error("BER_Decoder::end_cons called with data left");
90✔
278
   return (*m_parent);
378,158✔
279
}
280

281
BER_Decoder::BER_Decoder(BER_Object&& obj, BER_Decoder* parent) {
551,839✔
282
   m_data_src = std::make_unique<DataSource_BERObject>(std::move(obj));
551,839✔
283
   m_source = m_data_src.get();
551,839✔
284
   m_parent = parent;
551,839✔
285
}
551,839✔
286

287
/*
288
* BER_Decoder Constructor
289
*/
290
BER_Decoder::BER_Decoder(DataSource& src) { m_source = &src; }
36,728✔
291

292
/*
293
* BER_Decoder Constructor
294
 */
295
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length) {
62,247✔
296
   m_data_src = std::make_unique<DataSource_Memory>(data, length);
62,247✔
297
   m_source = m_data_src.get();
62,247✔
298
}
62,247✔
299

300
/*
301
* BER_Decoder Constructor
302
*/
303
BER_Decoder::BER_Decoder(const secure_vector<uint8_t>& data) {
4,205✔
304
   m_data_src = std::make_unique<DataSource_Memory>(data);
4,205✔
305
   m_source = m_data_src.get();
4,205✔
306
}
4,205✔
307

308
/*
309
* BER_Decoder Constructor
310
*/
311
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data) {
147,274✔
312
   m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
147,274✔
313
   m_source = m_data_src.get();
147,274✔
314
}
147,274✔
315

316
/*
317
* BER_Decoder Copy Constructor
318
*/
319
BER_Decoder::BER_Decoder(const BER_Decoder& other) {
178✔
320
   m_source = other.m_source;
178✔
321

322
   // take ownership
323
   std::swap(m_data_src, other.m_data_src);
178✔
324
   m_parent = other.m_parent;
178✔
325
}
178✔
326

327
/*
328
* Request for an object to decode itself
329
*/
330
BER_Decoder& BER_Decoder::decode(ASN1_Object& obj, ASN1_Type /*unused*/, ASN1_Class /*unused*/) {
524,985✔
331
   obj.decode_from(*this);
524,985✔
332
   return (*this);
519,884✔
333
}
334

335
/*
336
* Decode a BER encoded NULL
337
*/
338
BER_Decoder& BER_Decoder::decode_null() {
×
339
   BER_Object obj = get_next_object();
×
340
   obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
×
341
   if(obj.length() > 0)
×
342
      throw BER_Decoding_Error("NULL object had nonzero size");
×
343
   return (*this);
×
344
}
×
345

346
BER_Decoder& BER_Decoder::decode_octet_string_bigint(BigInt& out) {
1,016✔
347
   secure_vector<uint8_t> out_vec;
1,016✔
348
   decode(out_vec, ASN1_Type::OctetString);
1,016✔
349
   out = BigInt::decode(out_vec.data(), out_vec.size());
992✔
350
   return (*this);
992✔
351
}
992✔
352

353
/*
354
* Decode a BER encoded BOOLEAN
355
*/
356
BER_Decoder& BER_Decoder::decode(bool& out, ASN1_Type type_tag, ASN1_Class class_tag) {
28,626✔
357
   BER_Object obj = get_next_object();
28,626✔
358
   obj.assert_is_a(type_tag, class_tag);
28,689✔
359

360
   if(obj.length() != 1)
28,626✔
361
      throw BER_Decoding_Error("BER boolean value had invalid size");
63✔
362

363
   out = (obj.bits()[0]) ? true : false;
28,563✔
364
   return (*this);
28,563✔
365
}
28,563✔
366

367
/*
368
* Decode a small BER encoded INTEGER
369
*/
370
BER_Decoder& BER_Decoder::decode(size_t& out, ASN1_Type type_tag, ASN1_Class class_tag) {
30,917✔
371
   BigInt integer;
30,917✔
372
   decode(integer, type_tag, class_tag);
30,917✔
373

374
   if(integer.is_negative())
30,619✔
375
      throw BER_Decoding_Error("Decoded small integer value was negative");
97✔
376

377
   if(integer.bits() > 32)
30,522✔
378
      throw BER_Decoding_Error("Decoded integer value larger than expected");
1,090✔
379

380
   out = 0;
29,432✔
381
   for(size_t i = 0; i != 4; ++i)
147,160✔
382
      out = (out << 8) | integer.byte_at(3 - i);
117,728✔
383

384
   return (*this);
29,432✔
385
}
29,432✔
386

387
/*
388
* Decode a small BER encoded INTEGER
389
*/
390
uint64_t BER_Decoder::decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes) {
3,640✔
391
   if(T_bytes > 8)
3,640✔
392
      throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
×
393

394
   BigInt integer;
3,640✔
395
   decode(integer, type_tag, class_tag);
3,640✔
396

397
   if(integer.bits() > 8 * T_bytes)
3,640✔
398
      throw BER_Decoding_Error("Decoded integer value larger than expected");
×
399

400
   uint64_t out = 0;
401
   for(size_t i = 0; i != 8; ++i)
32,760✔
402
      out = (out << 8) | integer.byte_at(7 - i);
29,120✔
403

404
   return out;
3,640✔
405
}
3,640✔
406

407
/*
408
* Decode a BER encoded INTEGER
409
*/
410
BER_Decoder& BER_Decoder::decode(BigInt& out, ASN1_Type type_tag, ASN1_Class class_tag) {
118,576✔
411
   BER_Object obj = get_next_object();
118,576✔
412
   obj.assert_is_a(type_tag, class_tag);
117,810✔
413

414
   if(obj.length() == 0) {
116,553✔
415
      out.clear();
2,333✔
416
   } else {
417
      const bool negative = (obj.bits()[0] & 0x80) ? true : false;
114,220✔
418

419
      if(negative) {
114,220✔
420
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
4,501✔
421
         for(size_t i = obj.length(); i > 0; --i)
8,822✔
422
            if(vec[i - 1]--)
8,822✔
423
               break;
424
         for(size_t i = 0; i != obj.length(); ++i)
46,771✔
425
            vec[i] = ~vec[i];
43,527✔
426
         out = BigInt(vec.data(), vec.size());
3,244✔
427
         out.flip_sign();
6,488✔
428
      } else {
3,244✔
429
         out = BigInt(obj.bits(), obj.length());
110,976✔
430
      }
431
   }
432

433
   return (*this);
116,553✔
434
}
116,553✔
435

436
namespace {
437

438
template <typename Alloc>
439
void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer,
128,805✔
440
                               const BER_Object& obj,
441
                               ASN1_Type real_type,
442
                               ASN1_Type type_tag,
443
                               ASN1_Class class_tag) {
444
   obj.assert_is_a(type_tag, class_tag);
128,805✔
445

446
   if(real_type == ASN1_Type::OctetString) {
128,522✔
447
      buffer.assign(obj.bits(), obj.bits() + obj.length());
79,159✔
448
   } else {
449
      if(obj.length() == 0)
49,363✔
450
         throw BER_Decoding_Error("Invalid BIT STRING");
11✔
451
      if(obj.bits()[0] >= 8)
49,352✔
452
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
18✔
453

454
      buffer.resize(obj.length() - 1);
49,334✔
455

456
      if(obj.length() > 1)
49,334✔
457
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
48,663✔
458
   }
459
}
128,493✔
460

461
}
462

463
/*
464
* BER decode a BIT STRING or OCTET STRING
465
*/
466
BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& buffer,
8,382✔
467
                                 ASN1_Type real_type,
468
                                 ASN1_Type type_tag,
469
                                 ASN1_Class class_tag) {
470
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString)
8,382✔
471
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
×
472

473
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
8,382✔
474
   return (*this);
7,788✔
475
}
476

477
BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& buffer,
123,347✔
478
                                 ASN1_Type real_type,
479
                                 ASN1_Type type_tag,
480
                                 ASN1_Class class_tag) {
481
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString)
123,347✔
482
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
1,741✔
483

484
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
121,606✔
485
   return (*this);
120,705✔
486
}
487

488
}
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