Coverage Report

Created: 2023-12-08 07:00

/src/botan/src/lib/asn1/ber_dec.cpp
Line
Count
Source (jump to first uncovered line)
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
0
size_t decode_tag(DataSource* ber, ASN1_Type& type_tag, ASN1_Class& class_tag) {
30
0
   uint8_t b;
31
0
   if(!ber->read_byte(b)) {
32
0
      type_tag = ASN1_Type::NoObject;
33
0
      class_tag = ASN1_Class::NoObject;
34
0
      return 0;
35
0
   }
36
37
0
   if((b & 0x1F) != 0x1F) {
38
0
      type_tag = ASN1_Type(b & 0x1F);
39
0
      class_tag = ASN1_Class(b & 0xE0);
40
0
      return 1;
41
0
   }
42
43
0
   size_t tag_bytes = 1;
44
0
   class_tag = ASN1_Class(b & 0xE0);
45
46
0
   size_t tag_buf = 0;
47
0
   while(true) {
48
0
      if(!ber->read_byte(b)) {
49
0
         throw BER_Decoding_Error("Long-form tag truncated");
50
0
      }
51
0
      if(tag_buf & 0xFF000000) {
52
0
         throw BER_Decoding_Error("Long-form tag overflowed 32 bits");
53
0
      }
54
0
      ++tag_bytes;
55
0
      tag_buf = (tag_buf << 7) | (b & 0x7F);
56
0
      if((b & 0x80) == 0) {
57
0
         break;
58
0
      }
59
0
   }
60
0
   type_tag = ASN1_Type(tag_buf);
61
0
   return tag_bytes;
62
0
}
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
0
size_t decode_length(DataSource* ber, size_t& field_size, size_t allow_indef) {
73
0
   uint8_t b;
74
0
   if(!ber->read_byte(b)) {
75
0
      throw BER_Decoding_Error("Length field not found");
76
0
   }
77
0
   field_size = 1;
78
0
   if((b & 0x80) == 0) {
79
0
      return b;
80
0
   }
81
82
0
   field_size += (b & 0x7F);
83
0
   if(field_size > 5) {
84
0
      throw BER_Decoding_Error("Length field is too large");
85
0
   }
86
87
0
   if(field_size == 1) {
88
0
      if(allow_indef == 0) {
89
0
         throw BER_Decoding_Error("Nested EOC markers too deep, rejecting to avoid stack exhaustion");
90
0
      } else {
91
0
         return find_eoc(ber, allow_indef - 1);
92
0
      }
93
0
   }
94
95
0
   size_t length = 0;
96
97
0
   for(size_t i = 0; i != field_size - 1; ++i) {
98
0
      if(get_byte<0>(length) != 0) {
99
0
         throw BER_Decoding_Error("Field length overflow");
100
0
      }
101
0
      if(!ber->read_byte(b)) {
102
0
         throw BER_Decoding_Error("Corrupted length field");
103
0
      }
104
0
      length = (length << 8) | b;
105
0
   }
106
0
   return length;
107
0
}
108
109
/*
110
* Find the EOC marker
111
*/
112
0
size_t find_eoc(DataSource* ber, size_t allow_indef) {
113
0
   secure_vector<uint8_t> buffer(BOTAN_DEFAULT_BUFFER_SIZE), data;
114
115
0
   while(true) {
116
0
      const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
117
0
      if(got == 0) {
118
0
         break;
119
0
      }
120
121
0
      data += std::make_pair(buffer.data(), got);
122
0
   }
123
124
0
   DataSource_Memory source(data);
125
0
   data.clear();
126
127
0
   size_t length = 0;
128
0
   while(true) {
129
0
      ASN1_Type type_tag;
130
0
      ASN1_Class class_tag;
131
0
      size_t tag_size = decode_tag(&source, type_tag, class_tag);
132
0
      if(type_tag == ASN1_Type::NoObject) {
133
0
         break;
134
0
      }
135
136
0
      size_t length_size = 0;
137
0
      size_t item_size = decode_length(&source, length_size, allow_indef);
138
0
      source.discard_next(item_size);
139
140
0
      length = BOTAN_CHECKED_ADD(length, item_size);
141
0
      length = BOTAN_CHECKED_ADD(length, tag_size);
142
0
      length = BOTAN_CHECKED_ADD(length, length_size);
143
144
0
      if(type_tag == ASN1_Type::Eoc && class_tag == ASN1_Class::Universal) {
145
0
         break;
146
0
      }
147
0
   }
148
0
   return length;
149
0
}
150
151
class DataSource_BERObject final : public DataSource {
152
   public:
153
0
      size_t read(uint8_t out[], size_t length) override {
154
0
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
155
0
         const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
156
0
         copy_mem(out, m_obj.bits() + m_offset, got);
157
0
         m_offset += got;
158
0
         return got;
159
0
      }
160
161
0
      size_t peek(uint8_t out[], size_t length, size_t peek_offset) const override {
162
0
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
163
0
         const size_t bytes_left = m_obj.length() - m_offset;
164
165
0
         if(peek_offset >= bytes_left) {
166
0
            return 0;
167
0
         }
168
169
0
         const size_t got = std::min(bytes_left - peek_offset, length);
170
0
         copy_mem(out, m_obj.bits() + m_offset + peek_offset, got);
171
0
         return got;
172
0
      }
173
174
0
      bool check_available(size_t n) override {
175
0
         BOTAN_ASSERT_NOMSG(m_offset <= m_obj.length());
176
0
         return (n <= (m_obj.length() - m_offset));
177
0
      }
178
179
0
      bool end_of_data() const override { return get_bytes_read() == m_obj.length(); }
180
181
0
      size_t get_bytes_read() const override { return m_offset; }
182
183
0
      explicit DataSource_BERObject(BER_Object&& obj) : m_obj(std::move(obj)), m_offset(0) {}
184
185
   private:
186
      BER_Object m_obj;
187
      size_t m_offset;
188
};
189
190
}  // namespace
191
192
/*
193
* Check if more objects are there
194
*/
195
0
bool BER_Decoder::more_items() const {
196
0
   if(m_source->end_of_data() && !m_pushed.is_set()) {
197
0
      return false;
198
0
   }
199
0
   return true;
200
0
}
201
202
/*
203
* Verify that no bytes remain in the source
204
*/
205
0
BER_Decoder& BER_Decoder::verify_end() {
206
0
   return verify_end("BER_Decoder::verify_end called, but data remains");
207
0
}
208
209
/*
210
* Verify that no bytes remain in the source
211
*/
212
0
BER_Decoder& BER_Decoder::verify_end(std::string_view err) {
213
0
   if(!m_source->end_of_data() || m_pushed.is_set()) {
214
0
      throw Decoding_Error(err);
215
0
   }
216
0
   return (*this);
217
0
}
218
219
/*
220
* Discard all the bytes remaining in the source
221
*/
222
0
BER_Decoder& BER_Decoder::discard_remaining() {
223
0
   uint8_t buf;
224
0
   while(m_source->read_byte(buf)) {}
225
0
   return (*this);
226
0
}
227
228
/*
229
* Return the BER encoding of the next object
230
*/
231
0
BER_Object BER_Decoder::get_next_object() {
232
0
   BER_Object next;
233
234
0
   if(m_pushed.is_set()) {
235
0
      std::swap(next, m_pushed);
236
0
      return next;
237
0
   }
238
239
0
   for(;;) {
240
0
      ASN1_Type type_tag;
241
0
      ASN1_Class class_tag;
242
0
      decode_tag(m_source, type_tag, class_tag);
243
0
      next.set_tagging(type_tag, class_tag);
244
0
      if(next.is_set() == false) {  // no more objects
245
0
         return next;
246
0
      }
247
248
0
      size_t field_size;
249
0
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
250
0
      if(!m_source->check_available(length)) {
251
0
         throw BER_Decoding_Error("Value truncated");
252
0
      }
253
254
0
      uint8_t* out = next.mutable_bits(length);
255
0
      if(m_source->read(out, length) != length) {
256
0
         throw BER_Decoding_Error("Value truncated");
257
0
      }
258
259
0
      if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc)) {
260
0
         continue;
261
0
      } else {
262
0
         break;
263
0
      }
264
0
   }
265
266
0
   return next;
267
0
}
268
269
/*
270
* Push a object back into the stream
271
*/
272
0
void BER_Decoder::push_back(const BER_Object& obj) {
273
0
   if(m_pushed.is_set()) {
274
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
275
0
   }
276
0
   m_pushed = obj;
277
0
}
278
279
0
void BER_Decoder::push_back(BER_Object&& obj) {
280
0
   if(m_pushed.is_set()) {
281
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
282
0
   }
283
0
   m_pushed = std::move(obj);
284
0
}
285
286
0
BER_Decoder BER_Decoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag) {
287
0
   BER_Object obj = get_next_object();
288
0
   obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
289
0
   return BER_Decoder(std::move(obj), this);
290
0
}
291
292
/*
293
* Finish decoding a CONSTRUCTED type
294
*/
295
0
BER_Decoder& BER_Decoder::end_cons() {
296
0
   if(!m_parent) {
297
0
      throw Invalid_State("BER_Decoder::end_cons called with null parent");
298
0
   }
299
0
   if(!m_source->end_of_data()) {
300
0
      throw Decoding_Error("BER_Decoder::end_cons called with data left");
301
0
   }
302
0
   return (*m_parent);
303
0
}
304
305
0
BER_Decoder::BER_Decoder(BER_Object&& obj, BER_Decoder* parent) {
306
0
   m_data_src = std::make_unique<DataSource_BERObject>(std::move(obj));
307
0
   m_source = m_data_src.get();
308
0
   m_parent = parent;
309
0
}
310
311
/*
312
* BER_Decoder Constructor
313
*/
314
0
BER_Decoder::BER_Decoder(DataSource& src) {
315
0
   m_source = &src;
316
0
}
317
318
/*
319
* BER_Decoder Constructor
320
 */
321
0
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length) {
322
0
   m_data_src = std::make_unique<DataSource_Memory>(data, length);
323
0
   m_source = m_data_src.get();
324
0
}
325
326
/*
327
* BER_Decoder Constructor
328
*/
329
0
BER_Decoder::BER_Decoder(const secure_vector<uint8_t>& data) {
330
0
   m_data_src = std::make_unique<DataSource_Memory>(data);
331
0
   m_source = m_data_src.get();
332
0
}
333
334
/*
335
* BER_Decoder Constructor
336
*/
337
0
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data) {
338
0
   m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
339
0
   m_source = m_data_src.get();
340
0
}
341
342
/*
343
* BER_Decoder Copy Constructor
344
*/
345
0
BER_Decoder::BER_Decoder(const BER_Decoder& other) {
346
0
   m_source = other.m_source;
347
348
   // take ownership
349
0
   std::swap(m_data_src, other.m_data_src);
350
0
   m_parent = other.m_parent;
351
0
}
352
353
/*
354
* Request for an object to decode itself
355
*/
356
0
BER_Decoder& BER_Decoder::decode(ASN1_Object& obj, ASN1_Type /*unused*/, ASN1_Class /*unused*/) {
357
0
   obj.decode_from(*this);
358
0
   return (*this);
359
0
}
360
361
/*
362
* Decode a BER encoded NULL
363
*/
364
0
BER_Decoder& BER_Decoder::decode_null() {
365
0
   BER_Object obj = get_next_object();
366
0
   obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
367
0
   if(obj.length() > 0) {
368
0
      throw BER_Decoding_Error("NULL object had nonzero size");
369
0
   }
370
0
   return (*this);
371
0
}
372
373
0
BER_Decoder& BER_Decoder::decode_octet_string_bigint(BigInt& out) {
374
0
   secure_vector<uint8_t> out_vec;
375
0
   decode(out_vec, ASN1_Type::OctetString);
376
0
   out = BigInt::decode(out_vec.data(), out_vec.size());
377
0
   return (*this);
378
0
}
379
380
/*
381
* Decode a BER encoded BOOLEAN
382
*/
383
0
BER_Decoder& BER_Decoder::decode(bool& out, ASN1_Type type_tag, ASN1_Class class_tag) {
384
0
   BER_Object obj = get_next_object();
385
0
   obj.assert_is_a(type_tag, class_tag);
386
387
0
   if(obj.length() != 1) {
388
0
      throw BER_Decoding_Error("BER boolean value had invalid size");
389
0
   }
390
391
0
   out = (obj.bits()[0]) ? true : false;
392
0
   return (*this);
393
0
}
394
395
/*
396
* Decode a small BER encoded INTEGER
397
*/
398
0
BER_Decoder& BER_Decoder::decode(size_t& out, ASN1_Type type_tag, ASN1_Class class_tag) {
399
0
   BigInt integer;
400
0
   decode(integer, type_tag, class_tag);
401
402
0
   if(integer.is_negative()) {
403
0
      throw BER_Decoding_Error("Decoded small integer value was negative");
404
0
   }
405
406
0
   if(integer.bits() > 32) {
407
0
      throw BER_Decoding_Error("Decoded integer value larger than expected");
408
0
   }
409
410
0
   out = 0;
411
0
   for(size_t i = 0; i != 4; ++i) {
412
0
      out = (out << 8) | integer.byte_at(3 - i);
413
0
   }
414
415
0
   return (*this);
416
0
}
417
418
/*
419
* Decode a small BER encoded INTEGER
420
*/
421
0
uint64_t BER_Decoder::decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes) {
422
0
   if(T_bytes > 8) {
423
0
      throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
424
0
   }
425
426
0
   BigInt integer;
427
0
   decode(integer, type_tag, class_tag);
428
429
0
   if(integer.bits() > 8 * T_bytes) {
430
0
      throw BER_Decoding_Error("Decoded integer value larger than expected");
431
0
   }
432
433
0
   uint64_t out = 0;
434
0
   for(size_t i = 0; i != 8; ++i) {
435
0
      out = (out << 8) | integer.byte_at(7 - i);
436
0
   }
437
438
0
   return out;
439
0
}
440
441
/*
442
* Decode a BER encoded INTEGER
443
*/
444
0
BER_Decoder& BER_Decoder::decode(BigInt& out, ASN1_Type type_tag, ASN1_Class class_tag) {
445
0
   BER_Object obj = get_next_object();
446
0
   obj.assert_is_a(type_tag, class_tag);
447
448
0
   if(obj.length() == 0) {
449
0
      out.clear();
450
0
   } else {
451
0
      const bool negative = (obj.bits()[0] & 0x80) ? true : false;
452
453
0
      if(negative) {
454
0
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
455
0
         for(size_t i = obj.length(); i > 0; --i) {
456
0
            if(vec[i - 1]--) {
457
0
               break;
458
0
            }
459
0
         }
460
0
         for(size_t i = 0; i != obj.length(); ++i) {
461
0
            vec[i] = ~vec[i];
462
0
         }
463
0
         out = BigInt(vec.data(), vec.size());
464
0
         out.flip_sign();
465
0
      } else {
466
0
         out = BigInt(obj.bits(), obj.length());
467
0
      }
468
0
   }
469
470
0
   return (*this);
471
0
}
472
473
namespace {
474
475
template <typename Alloc>
476
void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer,
477
                               const BER_Object& obj,
478
                               ASN1_Type real_type,
479
                               ASN1_Type type_tag,
480
0
                               ASN1_Class class_tag) {
481
0
   obj.assert_is_a(type_tag, class_tag);
482
483
0
   if(real_type == ASN1_Type::OctetString) {
484
0
      buffer.assign(obj.bits(), obj.bits() + obj.length());
485
0
   } else {
486
0
      if(obj.length() == 0) {
487
0
         throw BER_Decoding_Error("Invalid BIT STRING");
488
0
      }
489
0
      if(obj.bits()[0] >= 8) {
490
0
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
491
0
      }
492
493
0
      buffer.resize(obj.length() - 1);
494
495
0
      if(obj.length() > 1) {
496
0
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
497
0
      }
498
0
   }
499
0
}
Unexecuted instantiation: ber_dec.cpp:void Botan::(anonymous namespace)::asn1_decode_binary_string<Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, Botan::BER_Object const&, Botan::ASN1_Type, Botan::ASN1_Type, Botan::ASN1_Class)
Unexecuted instantiation: ber_dec.cpp:void Botan::(anonymous namespace)::asn1_decode_binary_string<std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&, Botan::BER_Object const&, Botan::ASN1_Type, Botan::ASN1_Type, Botan::ASN1_Class)
500
501
}  // namespace
502
503
/*
504
* BER decode a BIT STRING or OCTET STRING
505
*/
506
BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& buffer,
507
                                 ASN1_Type real_type,
508
                                 ASN1_Type type_tag,
509
0
                                 ASN1_Class class_tag) {
510
0
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
511
0
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
512
0
   }
513
514
0
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
515
0
   return (*this);
516
0
}
517
518
BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& buffer,
519
                                 ASN1_Type real_type,
520
                                 ASN1_Type type_tag,
521
0
                                 ASN1_Class class_tag) {
522
0
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
523
0
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
524
0
   }
525
526
0
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
527
0
   return (*this);
528
0
}
529
530
}  // namespace Botan