Coverage Report

Created: 2023-06-07 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() { return verify_end("BER_Decoder::verify_end called, but data remains"); }
206
207
/*
208
* Verify that no bytes remain in the source
209
*/
210
0
BER_Decoder& BER_Decoder::verify_end(std::string_view err) {
211
0
   if(!m_source->end_of_data() || m_pushed.is_set()) {
212
0
      throw Decoding_Error(err);
213
0
   }
214
0
   return (*this);
215
0
}
216
217
/*
218
* Discard all the bytes remaining in the source
219
*/
220
0
BER_Decoder& BER_Decoder::discard_remaining() {
221
0
   uint8_t buf;
222
0
   while(m_source->read_byte(buf)) {}
223
0
   return (*this);
224
0
}
225
226
/*
227
* Return the BER encoding of the next object
228
*/
229
0
BER_Object BER_Decoder::get_next_object() {
230
0
   BER_Object next;
231
232
0
   if(m_pushed.is_set()) {
233
0
      std::swap(next, m_pushed);
234
0
      return next;
235
0
   }
236
237
0
   for(;;) {
238
0
      ASN1_Type type_tag;
239
0
      ASN1_Class class_tag;
240
0
      decode_tag(m_source, type_tag, class_tag);
241
0
      next.set_tagging(type_tag, class_tag);
242
0
      if(next.is_set() == false) {  // no more objects
243
0
         return next;
244
0
      }
245
246
0
      size_t field_size;
247
0
      const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
248
0
      if(!m_source->check_available(length)) {
249
0
         throw BER_Decoding_Error("Value truncated");
250
0
      }
251
252
0
      uint8_t* out = next.mutable_bits(length);
253
0
      if(m_source->read(out, length) != length) {
254
0
         throw BER_Decoding_Error("Value truncated");
255
0
      }
256
257
0
      if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc)) {
258
0
         continue;
259
0
      } else {
260
0
         break;
261
0
      }
262
0
   }
263
264
0
   return next;
265
0
}
266
267
/*
268
* Push a object back into the stream
269
*/
270
0
void BER_Decoder::push_back(const BER_Object& obj) {
271
0
   if(m_pushed.is_set()) {
272
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
273
0
   }
274
0
   m_pushed = obj;
275
0
}
276
277
0
void BER_Decoder::push_back(BER_Object&& obj) {
278
0
   if(m_pushed.is_set()) {
279
0
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
280
0
   }
281
0
   m_pushed = std::move(obj);
282
0
}
283
284
0
BER_Decoder BER_Decoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag) {
285
0
   BER_Object obj = get_next_object();
286
0
   obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
287
0
   return BER_Decoder(std::move(obj), this);
288
0
}
289
290
/*
291
* Finish decoding a CONSTRUCTED type
292
*/
293
0
BER_Decoder& BER_Decoder::end_cons() {
294
0
   if(!m_parent) {
295
0
      throw Invalid_State("BER_Decoder::end_cons called with null parent");
296
0
   }
297
0
   if(!m_source->end_of_data()) {
298
0
      throw Decoding_Error("BER_Decoder::end_cons called with data left");
299
0
   }
300
0
   return (*m_parent);
301
0
}
302
303
0
BER_Decoder::BER_Decoder(BER_Object&& obj, BER_Decoder* parent) {
304
0
   m_data_src = std::make_unique<DataSource_BERObject>(std::move(obj));
305
0
   m_source = m_data_src.get();
306
0
   m_parent = parent;
307
0
}
308
309
/*
310
* BER_Decoder Constructor
311
*/
312
0
BER_Decoder::BER_Decoder(DataSource& src) { m_source = &src; }
313
314
/*
315
* BER_Decoder Constructor
316
 */
317
0
BER_Decoder::BER_Decoder(const uint8_t data[], size_t length) {
318
0
   m_data_src = std::make_unique<DataSource_Memory>(data, length);
319
0
   m_source = m_data_src.get();
320
0
}
321
322
/*
323
* BER_Decoder Constructor
324
*/
325
0
BER_Decoder::BER_Decoder(const secure_vector<uint8_t>& data) {
326
0
   m_data_src = std::make_unique<DataSource_Memory>(data);
327
0
   m_source = m_data_src.get();
328
0
}
329
330
/*
331
* BER_Decoder Constructor
332
*/
333
0
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& data) {
334
0
   m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
335
0
   m_source = m_data_src.get();
336
0
}
337
338
/*
339
* BER_Decoder Copy Constructor
340
*/
341
0
BER_Decoder::BER_Decoder(const BER_Decoder& other) {
342
0
   m_source = other.m_source;
343
344
   // take ownership
345
0
   std::swap(m_data_src, other.m_data_src);
346
0
   m_parent = other.m_parent;
347
0
}
348
349
/*
350
* Request for an object to decode itself
351
*/
352
0
BER_Decoder& BER_Decoder::decode(ASN1_Object& obj, ASN1_Type /*unused*/, ASN1_Class /*unused*/) {
353
0
   obj.decode_from(*this);
354
0
   return (*this);
355
0
}
356
357
/*
358
* Decode a BER encoded NULL
359
*/
360
0
BER_Decoder& BER_Decoder::decode_null() {
361
0
   BER_Object obj = get_next_object();
362
0
   obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
363
0
   if(obj.length() > 0) {
364
0
      throw BER_Decoding_Error("NULL object had nonzero size");
365
0
   }
366
0
   return (*this);
367
0
}
368
369
0
BER_Decoder& BER_Decoder::decode_octet_string_bigint(BigInt& out) {
370
0
   secure_vector<uint8_t> out_vec;
371
0
   decode(out_vec, ASN1_Type::OctetString);
372
0
   out = BigInt::decode(out_vec.data(), out_vec.size());
373
0
   return (*this);
374
0
}
375
376
/*
377
* Decode a BER encoded BOOLEAN
378
*/
379
0
BER_Decoder& BER_Decoder::decode(bool& out, ASN1_Type type_tag, ASN1_Class class_tag) {
380
0
   BER_Object obj = get_next_object();
381
0
   obj.assert_is_a(type_tag, class_tag);
382
383
0
   if(obj.length() != 1) {
384
0
      throw BER_Decoding_Error("BER boolean value had invalid size");
385
0
   }
386
387
0
   out = (obj.bits()[0]) ? true : false;
388
0
   return (*this);
389
0
}
390
391
/*
392
* Decode a small BER encoded INTEGER
393
*/
394
0
BER_Decoder& BER_Decoder::decode(size_t& out, ASN1_Type type_tag, ASN1_Class class_tag) {
395
0
   BigInt integer;
396
0
   decode(integer, type_tag, class_tag);
397
398
0
   if(integer.is_negative()) {
399
0
      throw BER_Decoding_Error("Decoded small integer value was negative");
400
0
   }
401
402
0
   if(integer.bits() > 32) {
403
0
      throw BER_Decoding_Error("Decoded integer value larger than expected");
404
0
   }
405
406
0
   out = 0;
407
0
   for(size_t i = 0; i != 4; ++i) {
408
0
      out = (out << 8) | integer.byte_at(3 - i);
409
0
   }
410
411
0
   return (*this);
412
0
}
413
414
/*
415
* Decode a small BER encoded INTEGER
416
*/
417
0
uint64_t BER_Decoder::decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes) {
418
0
   if(T_bytes > 8) {
419
0
      throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
420
0
   }
421
422
0
   BigInt integer;
423
0
   decode(integer, type_tag, class_tag);
424
425
0
   if(integer.bits() > 8 * T_bytes) {
426
0
      throw BER_Decoding_Error("Decoded integer value larger than expected");
427
0
   }
428
429
0
   uint64_t out = 0;
430
0
   for(size_t i = 0; i != 8; ++i) {
431
0
      out = (out << 8) | integer.byte_at(7 - i);
432
0
   }
433
434
0
   return out;
435
0
}
436
437
/*
438
* Decode a BER encoded INTEGER
439
*/
440
0
BER_Decoder& BER_Decoder::decode(BigInt& out, ASN1_Type type_tag, ASN1_Class class_tag) {
441
0
   BER_Object obj = get_next_object();
442
0
   obj.assert_is_a(type_tag, class_tag);
443
444
0
   if(obj.length() == 0) {
445
0
      out.clear();
446
0
   } else {
447
0
      const bool negative = (obj.bits()[0] & 0x80) ? true : false;
448
449
0
      if(negative) {
450
0
         secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
451
0
         for(size_t i = obj.length(); i > 0; --i) {
452
0
            if(vec[i - 1]--) {
453
0
               break;
454
0
            }
455
0
         }
456
0
         for(size_t i = 0; i != obj.length(); ++i) {
457
0
            vec[i] = ~vec[i];
458
0
         }
459
0
         out = BigInt(vec.data(), vec.size());
460
0
         out.flip_sign();
461
0
      } else {
462
0
         out = BigInt(obj.bits(), obj.length());
463
0
      }
464
0
   }
465
466
0
   return (*this);
467
0
}
468
469
namespace {
470
471
template <typename Alloc>
472
void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer,
473
                               const BER_Object& obj,
474
                               ASN1_Type real_type,
475
                               ASN1_Type type_tag,
476
0
                               ASN1_Class class_tag) {
477
0
   obj.assert_is_a(type_tag, class_tag);
478
479
0
   if(real_type == ASN1_Type::OctetString) {
480
0
      buffer.assign(obj.bits(), obj.bits() + obj.length());
481
0
   } else {
482
0
      if(obj.length() == 0) {
483
0
         throw BER_Decoding_Error("Invalid BIT STRING");
484
0
      }
485
0
      if(obj.bits()[0] >= 8) {
486
0
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
487
0
      }
488
489
0
      buffer.resize(obj.length() - 1);
490
491
0
      if(obj.length() > 1) {
492
0
         copy_mem(buffer.data(), obj.bits() + 1, obj.length() - 1);
493
0
      }
494
0
   }
495
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)
496
497
}  // namespace
498
499
/*
500
* BER decode a BIT STRING or OCTET STRING
501
*/
502
BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& buffer,
503
                                 ASN1_Type real_type,
504
                                 ASN1_Type type_tag,
505
0
                                 ASN1_Class class_tag) {
506
0
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
507
0
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
508
0
   }
509
510
0
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
511
0
   return (*this);
512
0
}
513
514
BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& buffer,
515
                                 ASN1_Type real_type,
516
                                 ASN1_Type type_tag,
517
0
                                 ASN1_Class class_tag) {
518
0
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
519
0
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
520
0
   }
521
522
0
   asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
523
0
   return (*this);
524
0
}
525
526
}  // namespace Botan