• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5170091191

04 Jun 2023 04:54PM UTC coverage: 91.747% (+0.01%) from 91.734%
5170091191

push

github

randombit
Merge GH #3575 Add a testcase from #3544

76201 of 83056 relevant lines covered (91.75%)

11827977.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.25
/src/tests/test_asn1.cpp
1
/*
2
* (C) 2017 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#if defined(BOTAN_HAS_ASN1)
10
   #include <botan/asn1_print.h>
11
   #include <botan/ber_dec.h>
12
   #include <botan/der_enc.h>
13
   #include <botan/internal/fmt.h>
14
#endif
15

16
namespace Botan_Tests {
17

18
#if defined(BOTAN_HAS_ASN1)
19

20
namespace {
21

22
Test::Result test_ber_stack_recursion() {
1✔
23
   Test::Result result("BER stack recursion");
1✔
24

25
   // OSS-Fuzz #813 GitHub #989
26

27
   try {
1✔
28
      const std::vector<uint8_t> in(10000000, 0);
1✔
29
      Botan::DataSource_Memory input(in.data(), in.size());
1✔
30
      Botan::BER_Decoder dec(input);
1✔
31

32
      while(dec.more_items()) {
2✔
33
         Botan::BER_Object obj;
1✔
34
         dec.get_next(obj);
1✔
35
      }
1✔
36
   } catch(Botan::Decoding_Error&) {}
3✔
37

38
   result.test_success("No crash");
1✔
39

40
   return result;
1✔
41
}
×
42

43
Test::Result test_ber_eoc_decoding_limits() {
1✔
44
   Test::Result result("BER nested indefinite length");
1✔
45

46
   // OSS-Fuzz #4353
47

48
   Botan::ASN1_Pretty_Printer printer;
1✔
49

50
   size_t max_eoc_allowed = 0;
1✔
51

52
   for(size_t len = 1; len < 1024; ++len) {
17✔
53
      std::vector<uint8_t> buf(4 * len);
17✔
54

55
      /*
56
      This constructs a len deep sequence of SEQUENCES each with
57
      an indefinite length
58
      */
59
      for(size_t i = 0; i != 2 * len; i += 2) {
170✔
60
         buf[i] = 0x30;
153✔
61
         buf[i + 1] = 0x80;
153✔
62
      }
63
      // remainder of values left as zeros (EOC markers)
64

65
      try {
17✔
66
         printer.print(buf);
33✔
67
      } catch(Botan::BER_Decoding_Error&) {
1✔
68
         max_eoc_allowed = len - 1;
1✔
69
         break;
1✔
70
      }
1✔
71
   }
17✔
72

73
   result.test_eq("EOC limited to prevent stack exhaustion", max_eoc_allowed, 16);
1✔
74

75
   return result;
1✔
76
}
1✔
77

78
Test::Result test_asn1_utf8_ascii_parsing() {
1✔
79
   Test::Result result("ASN.1 ASCII parsing");
1✔
80

81
   try {
1✔
82
      // \x13 - ASN1 tag for 'printable string'
83
      // \x06 - 6 characters of payload
84
      // ...  - UTF-8 encoded (ASCII chars only) word 'Moscow'
85
      const std::string moscow = "\x13\x06\x4D\x6F\x73\x63\x6F\x77";
1✔
86
      const std::string moscow_plain = "Moscow";
1✔
87
      Botan::DataSource_Memory input(moscow);
1✔
88
      Botan::BER_Decoder dec(input);
1✔
89

90
      Botan::ASN1_String str;
1✔
91
      str.decode_from(dec);
1✔
92

93
      result.test_eq("value()", str.value(), moscow_plain);
1✔
94
   } catch(const Botan::Decoding_Error& ex) {
2✔
95
      result.test_failure(ex.what());
×
96
   }
×
97

98
   return result;
1✔
99
}
×
100

101
Test::Result test_asn1_utf8_parsing() {
1✔
102
   Test::Result result("ASN.1 UTF-8 parsing");
1✔
103

104
   try {
1✔
105
      // \x0C - ASN1 tag for 'UTF8 string'
106
      // \x0C - 12 characters of payload
107
      // ...  - UTF-8 encoded russian word for Moscow in cyrillic script
108
      const std::string moscow = "\x0C\x0C\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
109
      const std::string moscow_plain = "\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
110
      Botan::DataSource_Memory input(moscow);
1✔
111
      Botan::BER_Decoder dec(input);
1✔
112

113
      Botan::ASN1_String str;
1✔
114
      str.decode_from(dec);
1✔
115

116
      result.test_eq("value()", str.value(), moscow_plain);
1✔
117
   } catch(const Botan::Decoding_Error& ex) {
2✔
118
      result.test_failure(ex.what());
×
119
   }
×
120

121
   return result;
1✔
122
}
×
123

124
Test::Result test_asn1_ucs2_parsing() {
1✔
125
   Test::Result result("ASN.1 BMP string (UCS-2) parsing");
1✔
126

127
   try {
1✔
128
      // \x1E     - ASN1 tag for 'BMP (UCS-2) string'
129
      // \x0C     - 12 characters of payload
130
      // ...      - UCS-2 encoding for Moscow in cyrillic script
131
      const std::string moscow = "\x1E\x0C\x04\x1C\x04\x3E\x04\x41\x04\x3A\x04\x32\x04\x30";
1✔
132
      const std::string moscow_plain = "\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
133

134
      Botan::DataSource_Memory input(moscow);
1✔
135
      Botan::BER_Decoder dec(input);
1✔
136

137
      Botan::ASN1_String str;
1✔
138
      str.decode_from(dec);
1✔
139

140
      result.test_eq("value()", str.value(), moscow_plain);
1✔
141
   } catch(const Botan::Decoding_Error& ex) {
2✔
142
      result.test_failure(ex.what());
×
143
   }
×
144

145
   return result;
1✔
146
}
×
147

148
Test::Result test_asn1_ucs4_parsing() {
1✔
149
   Test::Result result("ASN.1 universal string (UCS-4) parsing");
1✔
150

151
   try {
1✔
152
      // \x1C - ASN1 tag for 'universal string'
153
      // \x18 - 24 characters of payload
154
      // ...  - UCS-4 encoding for Moscow in cyrillic script
155
      const uint8_t moscow[] =
1✔
156
         "\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";
157
      const std::string moscow_plain = "\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
158
      Botan::DataSource_Memory input(moscow, sizeof(moscow));
1✔
159
      Botan::BER_Decoder dec(input);
1✔
160

161
      Botan::ASN1_String str;
1✔
162
      str.decode_from(dec);
1✔
163

164
      result.test_eq("value()", str.value(), moscow_plain);
1✔
165
   } catch(const Botan::Decoding_Error& ex) {
2✔
166
      result.test_failure(ex.what());
×
167
   }
×
168

169
   return result;
1✔
170
}
×
171

172
Test::Result test_asn1_ascii_encoding() {
1✔
173
   Test::Result result("ASN.1 ASCII encoding");
1✔
174

175
   try {
1✔
176
      // UTF-8 encoded (ASCII chars only) word 'Moscow'
177
      const std::string moscow = "\x4D\x6F\x73\x63\x6F\x77";
1✔
178
      Botan::ASN1_String str(moscow);
1✔
179

180
      Botan::DER_Encoder enc;
1✔
181

182
      str.encode_into(enc);
1✔
183
      auto encodingResult = enc.get_contents();
1✔
184

185
      // \x13 - ASN1 tag for 'printable string'
186
      // \x06 - 6 characters of payload
187
      const auto moscowEncoded = Botan::hex_decode("13064D6F73636F77");
1✔
188
      result.test_eq("encoding result", encodingResult, moscowEncoded);
1✔
189

190
      result.test_success("No crash");
2✔
191
   } catch(const std::exception& ex) {
2✔
192
      result.test_failure(ex.what());
×
193
   }
×
194

195
   return result;
1✔
196
}
×
197

198
Test::Result test_asn1_utf8_encoding() {
1✔
199
   Test::Result result("ASN.1 UTF-8 encoding");
1✔
200

201
   try {
1✔
202
      // UTF-8 encoded russian word for Moscow in cyrillic script
203
      const std::string moscow = "\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
204
      Botan::ASN1_String str(moscow);
1✔
205

206
      Botan::DER_Encoder enc;
1✔
207

208
      str.encode_into(enc);
1✔
209
      auto encodingResult = enc.get_contents();
1✔
210

211
      // \x0C - ASN1 tag for 'UTF8 string'
212
      // \x0C - 12 characters of payload
213
      const auto moscowEncoded = Botan::hex_decode("0C0CD09CD0BED181D0BAD0B2D0B0");
1✔
214
      result.test_eq("encoding result", encodingResult, moscowEncoded);
1✔
215

216
      result.test_success("No crash");
2✔
217
   } catch(const std::exception& ex) {
2✔
218
      result.test_failure(ex.what());
×
219
   }
×
220

221
   return result;
1✔
222
}
×
223

224
Test::Result test_asn1_tag_underlying_type() {
1✔
225
   Test::Result result("ASN.1 class and type underlying type");
1✔
226

227
   if constexpr(std::is_same_v<std::underlying_type_t<Botan::ASN1_Class>, std::underlying_type_t<Botan::ASN1_Type>>) {
1✔
228
      if constexpr(!std::is_same_v<std::underlying_type_t<Botan::ASN1_Class>,
1✔
229
                                   std::invoke_result_t<decltype(&Botan::BER_Object::tagging), Botan::BER_Object>>) {
230
         result.test_failure(
231
            "Return type of BER_Object::tagging() is different than the underlying type of ASN1_Class");
232
      } else {
233
         result.test_success("Same types");
1✔
234
      }
235
   } else {
236
      result.test_failure("ASN1_Class and ASN1_Type have different underlying types");
237
   }
238

239
   return result;
1✔
240
}
×
241

242
}  // namespace
243

244
class ASN1_Tests final : public Test {
×
245
   public:
246
      std::vector<Test::Result> run() override {
1✔
247
         std::vector<Test::Result> results;
1✔
248

249
         results.push_back(test_ber_stack_recursion());
2✔
250
         results.push_back(test_ber_eoc_decoding_limits());
2✔
251
         results.push_back(test_asn1_utf8_ascii_parsing());
2✔
252
         results.push_back(test_asn1_utf8_parsing());
2✔
253
         results.push_back(test_asn1_ucs2_parsing());
2✔
254
         results.push_back(test_asn1_ucs4_parsing());
2✔
255
         results.push_back(test_asn1_ascii_encoding());
2✔
256
         results.push_back(test_asn1_utf8_encoding());
2✔
257
         results.push_back(test_asn1_tag_underlying_type());
2✔
258

259
         return results;
1✔
260
      }
×
261
};
262

263
BOTAN_REGISTER_TEST("asn1", "asn1_encoding", ASN1_Tests);
264

265
class ASN1_Time_Parsing_Tests final : public Text_Based_Test {
×
266
   public:
267
      ASN1_Time_Parsing_Tests() : Text_Based_Test("asn1_time.vec", "Tspec") {}
2✔
268

269
      Test::Result run_one_test(const std::string& tag_str, const VarMap& vars) override {
25✔
270
         Test::Result result("ASN.1 date parsing");
25✔
271

272
         const std::string tspec = vars.get_req_str("Tspec");
25✔
273

274
         if(tag_str != "UTC" && tag_str != "UTC.invalid" && tag_str != "Generalized" &&
25✔
275
            tag_str != "Generalized.invalid") {
11✔
276
            throw Test_Error("Invalid tag value in ASN1 date parsing test");
×
277
         }
278

279
         const Botan::ASN1_Type tag = (tag_str == "UTC" || tag_str == "UTC.invalid")
24✔
280
                                         ? Botan::ASN1_Type::UtcTime
25✔
281
                                         : Botan::ASN1_Type::GeneralizedTime;
25✔
282

283
         const bool valid = tag_str.find(".invalid") == std::string::npos;
25✔
284

285
         if(valid) {
25✔
286
            Botan::ASN1_Time time(tspec, tag);
13✔
287
            result.test_success("Accepted valid time");
26✔
288
         } else {
13✔
289
            result.test_throws("Invalid time rejected", [=]() { Botan::ASN1_Time time(tspec, tag); });
108✔
290
         }
291

292
         return result;
25✔
293
      }
25✔
294
};
295

296
BOTAN_REGISTER_TEST("asn1", "asn1_time", ASN1_Time_Parsing_Tests);
297

298
class ASN1_Printer_Tests final : public Test {
×
299
   public:
300
      std::vector<Test::Result> run() override {
1✔
301
         Test::Result result("ASN1_Pretty_Printer");
1✔
302

303
         Botan::ASN1_Pretty_Printer printer;
1✔
304

305
         const size_t num_tests = 7;
1✔
306

307
         for(size_t i = 1; i <= num_tests; ++i) {
8✔
308
            std::string i_str = std::to_string(i);
7✔
309
            const std::vector<uint8_t> input_data = Test::read_binary_data_file("asn1_print/input" + i_str + ".der");
14✔
310
            const std::string expected_output = Test::read_data_file("asn1_print/output" + i_str + ".txt");
14✔
311

312
            try {
7✔
313
               const std::string output = printer.print(input_data);
7✔
314
               result.test_eq("Test " + i_str, output, expected_output);
14✔
315
            } catch(Botan::Exception& e) {
7✔
316
               result.test_failure(Botan::fmt("Printing test {} failed with an exception: '{}'", i, e.what()));
×
317
            }
×
318
         }
14✔
319

320
         return {result};
2✔
321
      }
1✔
322
};
323

324
BOTAN_REGISTER_TEST("asn1", "asn1_printer", ASN1_Printer_Tests);
325

326
#endif
327

328
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc