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
|
/*
* (C) 2017 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "tests.h"
#if defined(BOTAN_HAS_ASN1)
#include <botan/der_enc.h>
#include <botan/ber_dec.h>
#include <botan/asn1_str.h>
#include <botan/asn1_print.h>
#endif
#include <fstream>
namespace Botan_Tests {
#if defined(BOTAN_HAS_ASN1)
namespace {
Test::Result test_ber_stack_recursion()
{
Test::Result result("BER stack recursion");
// OSS-Fuzz #813 GitHub #989
try
{
const std::vector<uint8_t> in(10000000, 0);
Botan::DataSource_Memory input(in.data(), in.size());
Botan::BER_Decoder dec(input);
while(dec.more_items())
{
Botan::BER_Object obj;
dec.get_next(obj);
}
}
catch(Botan::Decoding_Error&)
{
}
result.test_success("No crash");
return result;
}
Test::Result test_ber_eoc_decoding_limits()
{
Test::Result result("BER nested indefinite length");
// OSS-Fuzz #4353
Botan::ASN1_Pretty_Printer printer;
size_t max_eoc_allowed = 0;
for(size_t len = 1; len < 1024; ++len)
{
std::vector<uint8_t> buf(4*len);
/*
This constructs a len deep sequence of SEQUENCES each with
an indefinite length
*/
for(size_t i = 0; i != 2*len; i += 2)
{
buf[i ] = 0x30;
buf[i+1] = 0x80;
}
// remainder of values left as zeros (EOC markers)
try
{
printer.print(buf);
}
catch(Botan::BER_Decoding_Error&)
{
max_eoc_allowed = len - 1;
break;
}
}
result.test_eq("EOC limited to prevent stack exhaustion", max_eoc_allowed, 16);
return result;
}
Test::Result test_asn1_utf8_ascii_parsing()
{
Test::Result result("ASN.1 ASCII parsing");
try
{
// \x13 - ASN1 tag for 'printable string'
// \x06 - 6 characters of payload
// ... - UTF-8 encoded (ASCII chars only) word 'Moscow'
const std::string moscow =
"\x13\x06\x4D\x6F\x73\x63\x6F\x77";
const std::string moscow_plain = "Moscow";
Botan::DataSource_Memory input(moscow.data());
Botan::BER_Decoder dec(input);
Botan::ASN1_String str;
str.decode_from(dec);
result.test_eq("value()", str.value(), moscow_plain);
}
catch(const Botan::Decoding_Error &ex)
{
result.test_failure(ex.what());
}
return result;
}
Test::Result test_asn1_utf8_parsing()
{
Test::Result result("ASN.1 UTF-8 parsing");
try
{
// \x0C - ASN1 tag for 'UTF8 string'
// \x0C - 12 characters of payload
// ... - UTF-8 encoded russian word for Moscow in cyrillic script
const std::string moscow =
"\x0C\x0C\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
const std::string moscow_plain =
"\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
Botan::DataSource_Memory input(moscow.data());
Botan::BER_Decoder dec(input);
Botan::ASN1_String str;
str.decode_from(dec);
result.test_eq("value()", str.value(), moscow_plain);
}
catch(const Botan::Decoding_Error &ex)
{
result.test_failure(ex.what());
}
return result;
}
Test::Result test_asn1_ucs2_parsing()
{
Test::Result result("ASN.1 BMP string (UCS-2) parsing");
try
{
// \x1E - ASN1 tag for 'BMP (UCS-2) string'
// \x0C - 12 characters of payload
// ... - UCS-2 encoding for Moscow in cyrillic script
const std::string moscow =
"\x1E\x0C\x04\x1C\x04\x3E\x04\x41\x04\x3A\x04\x32\x04\x30";
const std::string moscow_plain =
"\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
Botan::DataSource_Memory input(moscow.data());
Botan::BER_Decoder dec(input);
Botan::ASN1_String str;
str.decode_from(dec);
result.test_eq("value()", str.value(), moscow_plain);
}
catch(const Botan::Decoding_Error &ex)
{
result.test_failure(ex.what());
}
return result;
}
Test::Result test_asn1_ucs4_parsing()
{
Test::Result result("ASN.1 universal string (UCS-4) parsing");
try
{
// \x1C - ASN1 tag for 'universal string'
// \x18 - 24 characters of payload
// ... - UCS-4 encoding for Moscow in cyrillic script
const Botan::byte moscow[] =
"\x1C\x18\x00\x00\x04\x1C\x00\x00\x04\x3E\x00\x00\x04\x41\x00\x00\x04\x3A\x00\x00\x04\x32\x00\x00\x04\x30";
const std::string moscow_plain =
"\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
Botan::DataSource_Memory input(moscow, sizeof(moscow));
Botan::BER_Decoder dec(input);
Botan::ASN1_String str;
str.decode_from(dec);
result.test_eq("value()", str.value(), moscow_plain);
}
catch(const Botan::Decoding_Error &ex)
{
result.test_failure(ex.what());
}
return result;
}
Test::Result test_asn1_ascii_encoding()
{
Test::Result result("ASN.1 ASCII encoding");
try
{
// UTF-8 encoded (ASCII chars only) word 'Moscow'
const std::string moscow =
"\x4D\x6F\x73\x63\x6F\x77";
Botan::ASN1_String str(moscow);
Botan::DER_Encoder enc;
str.encode_into(enc);
auto encodingResult = enc.get_contents();
// \x13 - ASN1 tag for 'printable string'
// \x06 - 6 characters of payload
const auto moscowEncoded = Botan::hex_decode("13064D6F73636F77");
result.test_eq("encoding result", encodingResult, moscowEncoded);
result.test_success("No crash");
}
catch(const std::exception &ex)
{
result.test_failure(ex.what());
}
return result;
}
Test::Result test_asn1_utf8_encoding()
{
Test::Result result("ASN.1 UTF-8 encoding");
try
{
// UTF-8 encoded russian word for Moscow in cyrillic script
const std::string moscow =
"\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
Botan::ASN1_String str(moscow);
Botan::DER_Encoder enc;
str.encode_into(enc);
auto encodingResult = enc.get_contents();
// \x0C - ASN1 tag for 'UTF8 string'
// \x0C - 12 characters of payload
const auto moscowEncoded =
Botan::hex_decode("0C0CD09CD0BED181D0BAD0B2D0B0");
result.test_eq("encoding result", encodingResult, moscowEncoded);
result.test_success("No crash");
}
catch(const std::exception &ex)
{
result.test_failure(ex.what());
}
return result;
}
}
class ASN1_Tests final : public Test
{
public:
std::vector<Test::Result> run() override
{
std::vector<Test::Result> results;
results.push_back(test_ber_stack_recursion());
results.push_back(test_ber_eoc_decoding_limits());
results.push_back(test_asn1_utf8_ascii_parsing());
results.push_back(test_asn1_utf8_parsing());
results.push_back(test_asn1_ucs2_parsing());
results.push_back(test_asn1_ucs4_parsing());
results.push_back(test_asn1_ascii_encoding());
results.push_back(test_asn1_utf8_encoding());
return results;
}
};
BOTAN_REGISTER_TEST("asn1", ASN1_Tests);
class ASN1_Printer_Tests final : public Test
{
public:
std::vector<Test::Result> run() override
{
Test::Result result("ASN1_Pretty_Printer");
Botan::ASN1_Pretty_Printer printer;
const size_t num_tests = 5;
for(size_t i = 1; i <= num_tests; ++i)
{
std::string i_str = std::to_string(i);
const std::vector<uint8_t> input1 = Test::read_binary_data_file("asn1_print/input" + i_str + ".der");
const std::string expected1 = Test::read_data_file("asn1_print/output" + i_str + ".txt");
result.test_eq("Test " + i_str, printer.print(input1), expected1);
}
return {result};
}
};
BOTAN_REGISTER_TEST("asn1_printer", ASN1_Printer_Tests);
#endif
}
|