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

randombit / botan / 20579846577

29 Dec 2025 06:24PM UTC coverage: 90.415% (+0.2%) from 90.243%
20579846577

push

github

web-flow
Merge pull request #5167 from randombit/jack/src-size-reductions

Changes to reduce unnecessary inclusions

101523 of 112285 relevant lines covered (90.42%)

12817276.56 hits per line

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

87.24
/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_obj.h>
11
   #include <botan/asn1_print.h>
12
   #include <botan/ber_dec.h>
13
   #include <botan/bigint.h>
14
   #include <botan/der_enc.h>
15
   #include <botan/internal/fmt.h>
16
#endif
17

18
namespace Botan_Tests {
19

20
#if defined(BOTAN_HAS_ASN1)
21

22
namespace {
23

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

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

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

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

40
   result.test_success("No crash");
1✔
41

42
   return result;
1✔
43
}
×
44

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

48
   // OSS-Fuzz #4353
49

50
   const 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

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

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

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

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

100
   return result;
1✔
101
}
×
102

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

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

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

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

123
   return result;
1✔
124
}
×
125

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

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

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

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

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

147
   return result;
1✔
148
}
×
149

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

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

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

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

171
   return result;
1✔
172
}
×
173

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

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

182
      Botan::DER_Encoder enc;
1✔
183

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

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

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

197
   return result;
1✔
198
}
×
199

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

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

208
      Botan::DER_Encoder enc;
1✔
209

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

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

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

223
   return result;
1✔
224
}
×
225

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

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

241
   return result;
1✔
242
}
×
243

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

247
   BigInt n(32);
1✔
248

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

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

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

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

260
   return result;
1✔
261
}
1✔
262

263
}  // namespace
264

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

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

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

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

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

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

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

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

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

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

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

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

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

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

325
         const Botan::ASN1_Pretty_Printer printer;
1✔
326

327
         const size_t num_tests = 7;
1✔
328

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

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

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

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

348
#endif
349

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