Coverage Report

Created: 2023-02-22 06:39

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