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

randombit / botan / 16055899095

03 Jul 2025 04:34PM UTC coverage: 90.571% (-0.003%) from 90.574%
16055899095

push

github

web-flow
Merge pull request #4931 from randombit/jack/moar-tidy

Address various warnings from clang-tidy

99050 of 109362 relevant lines covered (90.57%)

12478386.65 hits per line

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

86.22
/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/bigint.h>
13
   #include <botan/der_enc.h>
14
   #include <botan/internal/fmt.h>
15
#endif
16

17
namespace Botan_Tests {
18

19
#if defined(BOTAN_HAS_ASN1)
20

21
namespace {
22

23
Test::Result test_ber_stack_recursion() {
1✔
24
   Test::Result result("BER stack recursion");
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

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

47
   // OSS-Fuzz #4353
48

49
   Botan::ASN1_Pretty_Printer printer;
1✔
50

51
   size_t max_eoc_allowed = 0;
1✔
52

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

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

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

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

76
   return result;
1✔
77
}
1✔
78

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

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

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

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

99
   return result;
1✔
100
}
×
101

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

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

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

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

122
   return result;
1✔
123
}
×
124

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

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

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

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

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

146
   return result;
1✔
147
}
×
148

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

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

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

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

170
   return result;
1✔
171
}
×
172

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

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

181
      Botan::DER_Encoder enc;
1✔
182

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

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

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

196
   return result;
1✔
197
}
×
198

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

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

207
      Botan::DER_Encoder enc;
1✔
208

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

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

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

222
   return result;
1✔
223
}
×
224

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

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

240
   return result;
1✔
241
}
×
242

243
Test::Result test_asn1_negative_int_encoding() {
1✔
244
   Test::Result result("DER encode/decode of negative integers");
1✔
245

246
   BigInt n(32);
1✔
247

248
   for(size_t i = 0; i != 2048; ++i) {
2,049✔
249
      n--;
2,048✔
250

251
      const auto enc = Botan::DER_Encoder().encode(n).get_contents_unlocked();
2,048✔
252

253
      BigInt n_dec;
2,048✔
254
      Botan::BER_Decoder(enc).decode(n_dec);
4,096✔
255

256
      result.test_eq("DER encoding round trips negative integers", n_dec, n);
2,048✔
257
   }
4,096✔
258

259
   return result;
1✔
260
}
1✔
261

262
}  // namespace
263

264
class ASN1_Tests final : public Test {
×
265
   public:
266
      std::vector<Test::Result> run() override {
1✔
267
         std::vector<Test::Result> results;
1✔
268

269
         results.push_back(test_ber_stack_recursion());
2✔
270
         results.push_back(test_ber_eoc_decoding_limits());
2✔
271
         results.push_back(test_asn1_utf8_ascii_parsing());
2✔
272
         results.push_back(test_asn1_utf8_parsing());
2✔
273
         results.push_back(test_asn1_ucs2_parsing());
2✔
274
         results.push_back(test_asn1_ucs4_parsing());
2✔
275
         results.push_back(test_asn1_ascii_encoding());
2✔
276
         results.push_back(test_asn1_utf8_encoding());
2✔
277
         results.push_back(test_asn1_tag_underlying_type());
2✔
278
         results.push_back(test_asn1_negative_int_encoding());
2✔
279

280
         return results;
1✔
281
      }
×
282
};
283

284
BOTAN_REGISTER_TEST("asn1", "asn1_encoding", ASN1_Tests);
285

286
class ASN1_Time_Parsing_Tests final : public Text_Based_Test {
×
287
   public:
288
      ASN1_Time_Parsing_Tests() : Text_Based_Test("asn1_time.vec", "Tspec") {}
2✔
289

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

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

295
         if(tag_str != "UTC" && tag_str != "UTC.invalid" && tag_str != "Generalized" &&
25✔
296
            tag_str != "Generalized.invalid") {
11✔
297
            throw Test_Error("Invalid tag value in ASN1 date parsing test");
×
298
         }
299

300
         const Botan::ASN1_Type tag = (tag_str == "UTC" || tag_str == "UTC.invalid")
24✔
301
                                         ? Botan::ASN1_Type::UtcTime
25✔
302
                                         : Botan::ASN1_Type::GeneralizedTime;
25✔
303

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

306
         if(valid) {
25✔
307
            Botan::ASN1_Time time(tspec, tag);
13✔
308
            result.test_success("Accepted valid time");
26✔
309
         } else {
13✔
310
            result.test_throws("Invalid time rejected", [=]() { Botan::ASN1_Time time(tspec, tag); });
108✔
311
         }
312

313
         return result;
25✔
314
      }
25✔
315
};
316

317
BOTAN_REGISTER_TEST("asn1", "asn1_time", ASN1_Time_Parsing_Tests);
318

319
class ASN1_Printer_Tests final : public Test {
×
320
   public:
321
      std::vector<Test::Result> run() override {
1✔
322
         Test::Result result("ASN1_Pretty_Printer");
1✔
323

324
         Botan::ASN1_Pretty_Printer printer;
1✔
325

326
         const size_t num_tests = 7;
1✔
327

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

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

341
         return {result};
2✔
342
      }
2✔
343
};
344

345
BOTAN_REGISTER_TEST("asn1", "asn1_printer", ASN1_Printer_Tests);
346

347
#endif
348

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