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