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

randombit / botan / 11844561993

14 Nov 2024 07:58PM UTC coverage: 91.178% (+0.1%) from 91.072%
11844561993

Pull #4435

github

web-flow
Merge 81dcb29da into e430f157a
Pull Request #4435: Test duration values ​​are now presented in seconds with six digits of precision. Tests without time measurements have been edited.

91856 of 100744 relevant lines covered (91.18%)

9311006.71 hits per line

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

86.7
/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
   result.start_timer();
1✔
25

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

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

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

39
   result.test_success("No crash");
1✔
40
   result.end_timer();
1✔
41
   return result;
1✔
42
}
×
43

44
Test::Result test_ber_eoc_decoding_limits() {
1✔
45
   Test::Result result("BER nested indefinite length");
1✔
46
   result.start_timer();
1✔
47

48
   // OSS-Fuzz #4353
49

50
   Botan::ASN1_Pretty_Printer printer;
1✔
51

52
   size_t max_eoc_allowed = 0;
1✔
53

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

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

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

75
   result.test_eq("EOC limited to prevent stack exhaustion", max_eoc_allowed, 16);
1✔
76
   result.end_timer();
1✔
77
   return result;
1✔
78
}
1✔
79

80
Test::Result test_asn1_utf8_ascii_parsing() {
1✔
81
   Test::Result result("ASN.1 ASCII parsing");
1✔
82
   result.start_timer();
1✔
83

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

93
      Botan::ASN1_String str;
1✔
94
      str.decode_from(dec);
1✔
95

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

101
   result.end_timer();
1✔
102
   return result;
1✔
103
}
×
104

105
Test::Result test_asn1_utf8_parsing() {
1✔
106
   Test::Result result("ASN.1 UTF-8 parsing");
1✔
107
   result.start_timer();
1✔
108

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

118
      Botan::ASN1_String str;
1✔
119
      str.decode_from(dec);
1✔
120

121
      result.test_eq("value()", str.value(), moscow_plain);
1✔
122
   } catch(const Botan::Decoding_Error& ex) {
2✔
123
      result.test_failure(ex.what());
×
124
   }
×
125

126
   result.end_timer();
1✔
127
   return result;
1✔
128
}
×
129

130
Test::Result test_asn1_ucs2_parsing() {
1✔
131
   Test::Result result("ASN.1 BMP string (UCS-2) parsing");
1✔
132
   result.start_timer();
1✔
133

134
   try {
1✔
135
      // \x1E     - ASN1 tag for 'BMP (UCS-2) string'
136
      // \x0C     - 12 characters of payload
137
      // ...      - UCS-2 encoding for Moscow in cyrillic script
138
      const std::string moscow = "\x1E\x0C\x04\x1C\x04\x3E\x04\x41\x04\x3A\x04\x32\x04\x30";
1✔
139
      const std::string moscow_plain = "\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
140

141
      Botan::DataSource_Memory input(moscow);
1✔
142
      Botan::BER_Decoder dec(input);
1✔
143

144
      Botan::ASN1_String str;
1✔
145
      str.decode_from(dec);
1✔
146

147
      result.test_eq("value()", str.value(), moscow_plain);
1✔
148
   } catch(const Botan::Decoding_Error& ex) {
2✔
149
      result.test_failure(ex.what());
×
150
   }
×
151

152
   result.end_timer();
1✔
153
   return result;
1✔
154
}
×
155

156
Test::Result test_asn1_ucs4_parsing() {
1✔
157
   Test::Result result("ASN.1 universal string (UCS-4) parsing");
1✔
158
   result.start_timer();
1✔
159

160
   try {
1✔
161
      // \x1C - ASN1 tag for 'universal string'
162
      // \x18 - 24 characters of payload
163
      // ...  - UCS-4 encoding for Moscow in cyrillic script
164
      const uint8_t moscow[] =
1✔
165
         "\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";
166
      const std::string moscow_plain = "\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
167
      Botan::DataSource_Memory input(moscow, sizeof(moscow));
1✔
168
      Botan::BER_Decoder dec(input);
1✔
169

170
      Botan::ASN1_String str;
1✔
171
      str.decode_from(dec);
1✔
172

173
      result.test_eq("value()", str.value(), moscow_plain);
1✔
174
   } catch(const Botan::Decoding_Error& ex) {
2✔
175
      result.test_failure(ex.what());
×
176
   }
×
177

178
   result.end_timer();
1✔
179
   return result;
1✔
180
}
×
181

182
Test::Result test_asn1_ascii_encoding() {
1✔
183
   Test::Result result("ASN.1 ASCII encoding");
1✔
184
   result.start_timer();
1✔
185

186
   try {
1✔
187
      // UTF-8 encoded (ASCII chars only) word 'Moscow'
188
      const std::string moscow = "\x4D\x6F\x73\x63\x6F\x77";
1✔
189
      Botan::ASN1_String str(moscow);
1✔
190

191
      Botan::DER_Encoder enc;
1✔
192

193
      str.encode_into(enc);
1✔
194
      auto encodingResult = enc.get_contents();
1✔
195

196
      // \x13 - ASN1 tag for 'printable string'
197
      // \x06 - 6 characters of payload
198
      const auto moscowEncoded = Botan::hex_decode("13064D6F73636F77");
1✔
199
      result.test_eq("encoding result", encodingResult, moscowEncoded);
1✔
200

201
      result.test_success("No crash");
2✔
202
   } catch(const std::exception& ex) {
2✔
203
      result.test_failure(ex.what());
×
204
   }
×
205

206
   result.end_timer();
1✔
207
   return result;
1✔
208
}
×
209

210
Test::Result test_asn1_utf8_encoding() {
1✔
211
   Test::Result result("ASN.1 UTF-8 encoding");
1✔
212
   result.start_timer();
1✔
213

214
   try {
1✔
215
      // UTF-8 encoded russian word for Moscow in cyrillic script
216
      const std::string moscow = "\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
217
      Botan::ASN1_String str(moscow);
1✔
218

219
      Botan::DER_Encoder enc;
1✔
220

221
      str.encode_into(enc);
1✔
222
      auto encodingResult = enc.get_contents();
1✔
223

224
      // \x0C - ASN1 tag for 'UTF8 string'
225
      // \x0C - 12 characters of payload
226
      const auto moscowEncoded = Botan::hex_decode("0C0CD09CD0BED181D0BAD0B2D0B0");
1✔
227
      result.test_eq("encoding result", encodingResult, moscowEncoded);
1✔
228

229
      result.test_success("No crash");
2✔
230
   } catch(const std::exception& ex) {
2✔
231
      result.test_failure(ex.what());
×
232
   }
×
233

234
   result.end_timer();
1✔
235
   return result;
1✔
236
}
×
237

238
Test::Result test_asn1_tag_underlying_type() {
1✔
239
   Test::Result result("ASN.1 class and type underlying type");
1✔
240
   result.start_timer();
1✔
241

242
   if constexpr(std::is_same_v<std::underlying_type_t<Botan::ASN1_Class>, std::underlying_type_t<Botan::ASN1_Type>>) {
1✔
243
      if constexpr(!std::is_same_v<std::underlying_type_t<Botan::ASN1_Class>,
1✔
244
                                   std::invoke_result_t<decltype(&Botan::BER_Object::tagging), Botan::BER_Object>>) {
245
         result.test_failure(
246
            "Return type of BER_Object::tagging() is different than the underlying type of ASN1_Class");
247
      } else {
248
         result.test_success("Same types");
1✔
249
      }
250
   } else {
251
      result.test_failure("ASN1_Class and ASN1_Type have different underlying types");
252
   }
253

254
   result.end_timer();
1✔
255
   return result;
1✔
256
}
×
257

258
}  // namespace
259

260
class ASN1_Tests final : public Test {
×
261
   public:
262
      std::vector<Test::Result> run() override {
1✔
263
         std::vector<Test::Result> results;
1✔
264

265
         results.push_back(test_ber_stack_recursion());
2✔
266
         results.push_back(test_ber_eoc_decoding_limits());
2✔
267
         results.push_back(test_asn1_utf8_ascii_parsing());
2✔
268
         results.push_back(test_asn1_utf8_parsing());
2✔
269
         results.push_back(test_asn1_ucs2_parsing());
2✔
270
         results.push_back(test_asn1_ucs4_parsing());
2✔
271
         results.push_back(test_asn1_ascii_encoding());
2✔
272
         results.push_back(test_asn1_utf8_encoding());
2✔
273
         results.push_back(test_asn1_tag_underlying_type());
2✔
274

275
         return results;
1✔
276
      }
×
277
};
278

279
BOTAN_REGISTER_TEST("asn1", "asn1_encoding", ASN1_Tests);
280

281
class ASN1_Time_Parsing_Tests final : public Text_Based_Test {
×
282
   public:
283
      ASN1_Time_Parsing_Tests() : Text_Based_Test("asn1_time.vec", "Tspec") {}
2✔
284

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

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

290
         if(tag_str != "UTC" && tag_str != "UTC.invalid" && tag_str != "Generalized" &&
25✔
291
            tag_str != "Generalized.invalid") {
11✔
292
            throw Test_Error("Invalid tag value in ASN1 date parsing test");
×
293
         }
294

295
         const Botan::ASN1_Type tag = (tag_str == "UTC" || tag_str == "UTC.invalid")
24✔
296
                                         ? Botan::ASN1_Type::UtcTime
25✔
297
                                         : Botan::ASN1_Type::GeneralizedTime;
25✔
298

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

301
         if(valid) {
25✔
302
            Botan::ASN1_Time time(tspec, tag);
13✔
303
            result.test_success("Accepted valid time");
26✔
304
         } else {
13✔
305
            result.test_throws("Invalid time rejected", [=]() { Botan::ASN1_Time time(tspec, tag); });
108✔
306
         }
307

308
         return result;
25✔
309
      }
25✔
310
};
311

312
BOTAN_REGISTER_TEST("asn1", "asn1_time", ASN1_Time_Parsing_Tests);
313

314
class ASN1_Printer_Tests final : public Test {
×
315
   public:
316
      std::vector<Test::Result> run() override {
1✔
317
         Test::Result result("ASN1_Pretty_Printer");
1✔
318
         result.start_timer();
1✔
319

320
         Botan::ASN1_Pretty_Printer printer;
1✔
321

322
         const size_t num_tests = 7;
1✔
323

324
         for(size_t i = 1; i <= num_tests; ++i) {
8✔
325
            std::string i_str = std::to_string(i);
7✔
326
            const std::vector<uint8_t> input_data = Test::read_binary_data_file("asn1_print/input" + i_str + ".der");
21✔
327
            const std::string expected_output = Test::read_data_file("asn1_print/output" + i_str + ".txt");
21✔
328

329
            try {
7✔
330
               const std::string output = printer.print(input_data);
7✔
331
               result.test_eq("Test " + i_str, output, expected_output);
14✔
332
            } catch(Botan::Exception& e) {
7✔
333
               result.test_failure(Botan::fmt("Printing test {} failed with an exception: '{}'", i, e.what()));
×
334
            }
×
335
         }
14✔
336

337
         result.end_timer();
1✔
338
         return {result};
2✔
339
      }
2✔
340
};
341

342
BOTAN_REGISTER_TEST("asn1", "asn1_printer", ASN1_Printer_Tests);
343

344
#endif
345

346
}  // 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

© 2025 Coveralls, Inc