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

randombit / botan / 28002582618

22 Jun 2026 07:30PM UTC coverage: 89.368% (+0.007%) from 89.361%
28002582618

push

github

web-flow
Merge pull request #5685 from randombit/jack/support-1950-timepoint

Extend calendar_point/ASN1_Time to support timepoint conversions for 1950-1970

111874 of 125184 relevant lines covered (89.37%)

11150060.87 hits per line

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

89.23
/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/asn1_time.h>
13
   #include <botan/ber_dec.h>
14
   #include <botan/bigint.h>
15
   #include <botan/data_src.h>
16
   #include <botan/der_enc.h>
17
   #include <botan/pss_params.h>
18
   #include <botan/internal/fmt.h>
19
   #include <botan/internal/parsing.h>
20
#endif
21

22
namespace Botan_Tests {
23

24
namespace {
25

26
#if defined(BOTAN_HAS_ASN1)
27

28
Test::Result test_ber_stack_recursion() {
1✔
29
   Test::Result result("BER stack recursion");
1✔
30

31
   // OSS-Fuzz #813 GitHub #989
32

33
   try {
1✔
34
      const std::vector<uint8_t> in(10000000, 0);
1✔
35
      Botan::DataSource_Memory input(in.data(), in.size());
1✔
36
      Botan::BER_Decoder dec(input);
1✔
37

38
      while(dec.more_items()) {
2✔
39
         Botan::BER_Object obj;
1✔
40
         dec.get_next(obj);
1✔
41
      }
1✔
42
   } catch(Botan::Decoding_Error&) {}
2✔
43

44
   result.test_success("No crash");
1✔
45

46
   return result;
1✔
47
}
×
48

49
Test::Result test_ber_eoc_decoding_limits() {
1✔
50
   Test::Result result("BER nested indefinite length");
1✔
51

52
   // OSS-Fuzz #4353
53

54
   const Botan::ASN1_Pretty_Printer printer;
1✔
55

56
   size_t max_eoc_allowed = 0;
1✔
57

58
   for(size_t len = 1; len < 1024; ++len) {
17✔
59
      std::vector<uint8_t> buf(4 * len);
17✔
60

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

71
      try {
17✔
72
         printer.print(buf);
17✔
73
      } catch(Botan::BER_Decoding_Error&) {
1✔
74
         max_eoc_allowed = len - 1;
1✔
75
         break;
1✔
76
      }
1✔
77
   }
17✔
78

79
   result.test_sz_eq("EOC limited to prevent stack exhaustion", max_eoc_allowed, 16);
1✔
80

81
   return result;
1✔
82
}
1✔
83

84
Test::Result test_asn1_utf8_ascii_parsing() {
1✔
85
   Test::Result result("ASN.1 ASCII parsing");
1✔
86

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

96
      Botan::ASN1_String str;
1✔
97
      str.decode_from(dec);
1✔
98

99
      result.test_str_eq("value()", str.value(), moscow_plain);
1✔
100
   } catch(const Botan::Decoding_Error& ex) {
2✔
101
      result.test_failure(ex.what());
×
102
   }
×
103

104
   return result;
1✔
105
}
×
106

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

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

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

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

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

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

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

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

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

151
   return result;
1✔
152
}
×
153

154
Test::Result test_asn1_ucs4_parsing() {
1✔
155
   Test::Result result("ASN.1 universal string (UCS-4) parsing");
1✔
156

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

167
      Botan::ASN1_String str;
1✔
168
      str.decode_from(dec);
1✔
169

170
      result.test_str_eq("value()", str.value(), moscow_plain);
1✔
171
   } catch(const Botan::Decoding_Error& ex) {
2✔
172
      result.test_failure(ex.what());
×
173
   }
×
174

175
   return result;
1✔
176
}
×
177

178
Test::Result test_asn1_ucs_invalid_codepoint_rejection() {
1✔
179
   Test::Result result("ASN.1 UCS-2/UCS-4 invalid codepoint rejection");
1✔
180

181
   auto expect_decode_throws = [&](const char* what, const std::vector<uint8_t>& wire) {
7✔
182
      result.test_throws(what, [&]() {
6✔
183
         Botan::DataSource_Memory input(wire.data(), wire.size());
6✔
184
         Botan::BER_Decoder dec(input);
6✔
185
         Botan::ASN1_String str;
6✔
186
         str.decode_from(dec);
6✔
187
      });
6✔
188
   };
6✔
189

190
   auto expect_decode_ok = [&](const char* what, const std::vector<uint8_t>& wire) {
2✔
191
      try {
1✔
192
         Botan::DataSource_Memory input(wire.data(), wire.size());
1✔
193
         Botan::BER_Decoder dec(input);
1✔
194
         Botan::ASN1_String str;
1✔
195
         str.decode_from(dec);
1✔
196
         result.test_success(what);
1✔
197
      } catch(const std::exception& ex) {
2✔
198
         result.test_failure(Botan::fmt("{}: unexpected throw: {}", what, ex.what()));
×
199
      }
×
200
   };
1✔
201

202
   // UniversalString (tag 0x1C) with codepoint 0x00110000 - one past Unicode max
203
   expect_decode_throws("UniversalString rejects codepoint > 0x10FFFF", {0x1C, 0x04, 0x00, 0x11, 0x00, 0x00});
1✔
204

205
   // UniversalString with codepoint 0xFFFFFFFF (clearly out of range)
206
   expect_decode_throws("UniversalString rejects codepoint 0xFFFFFFFF", {0x1C, 0x04, 0xFF, 0xFF, 0xFF, 0xFF});
1✔
207

208
   // UniversalString with high surrogate 0xD800
209
   expect_decode_throws("UniversalString rejects surrogate codepoint", {0x1C, 0x04, 0x00, 0x00, 0xD8, 0x00});
1✔
210

211
   // UniversalString boundary case: 0x10FFFF is the highest valid codepoint
212
   expect_decode_ok("UniversalString accepts codepoint 0x10FFFF", {0x1C, 0x04, 0x00, 0x10, 0xFF, 0xFF});
1✔
213

214
   // BmpString (tag 0x1E) with high surrogate
215
   expect_decode_throws("BmpString rejects surrogate codepoint", {0x1E, 0x02, 0xD8, 0x00});
1✔
216

217
   // BmpString with odd length is malformed
218
   expect_decode_throws("BmpString rejects odd-length payload", {0x1E, 0x03, 0x00, 0x41, 0x00});
1✔
219

220
   // UniversalString with non-multiple-of-4 length is malformed
221
   expect_decode_throws("UniversalString rejects non-multiple-of-4 payload",
1✔
222
                        {0x1C, 0x05, 0x00, 0x00, 0x00, 0x41, 0x00});
223

224
   return result;
1✔
225
}
×
226

227
Test::Result test_asn1_ascii_encoding() {
1✔
228
   Test::Result result("ASN.1 ASCII encoding");
1✔
229

230
   try {
1✔
231
      // UTF-8 encoded (ASCII chars only) word 'Moscow'
232
      const std::string moscow = "Moscow";
1✔
233
      const Botan::ASN1_String str(moscow);
1✔
234

235
      Botan::DER_Encoder enc;
1✔
236

237
      str.encode_into(enc);
1✔
238
      auto encodingResult = enc.get_contents();
1✔
239

240
      // \x13 - ASN1 tag for 'printable string'
241
      // \x06 - 6 characters of payload
242
      result.test_bin_eq("encoding result", encodingResult, "13064D6F73636F77");
1✔
243

244
      result.test_success("No crash");
1✔
245
   } catch(const std::exception& ex) {
2✔
246
      result.test_failure(ex.what());
×
247
   }
×
248

249
   return result;
1✔
250
}
×
251

252
Test::Result test_asn1_utf8_encoding() {
1✔
253
   Test::Result result("ASN.1 UTF-8 encoding");
1✔
254

255
   try {
1✔
256
      // UTF-8 encoded russian word for Moscow in cyrillic script
257
      const std::string moscow = "\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
258
      const Botan::ASN1_String str(moscow);
1✔
259

260
      Botan::DER_Encoder enc;
1✔
261

262
      str.encode_into(enc);
1✔
263
      auto encodingResult = enc.get_contents();
1✔
264

265
      // \x0C - ASN1 tag for 'UTF8 string'
266
      // \x0C - 12 characters of payload
267
      result.test_bin_eq("encoding result", encodingResult, "0C0CD09CD0BED181D0BAD0B2D0B0");
1✔
268

269
      result.test_success("No crash");
1✔
270
   } catch(const std::exception& ex) {
2✔
271
      result.test_failure(ex.what());
×
272
   }
×
273

274
   return result;
1✔
275
}
×
276

277
Test::Result test_asn1_tag_underlying_type() {
1✔
278
   Test::Result result("ASN.1 class and type underlying type");
1✔
279

280
   if constexpr(std::is_same_v<std::underlying_type_t<Botan::ASN1_Class>, std::underlying_type_t<Botan::ASN1_Type>>) {
1✔
281
      if constexpr(!std::is_same_v<std::underlying_type_t<Botan::ASN1_Class>,
1✔
282
                                   std::invoke_result_t<decltype(&Botan::BER_Object::tagging), Botan::BER_Object>>) {
283
         result.test_failure(
284
            "Return type of BER_Object::tagging() is different than the underlying type of ASN1_Class");
285
      } else {
286
         result.test_success("Same types");
1✔
287
      }
288
   } else {
289
      result.test_failure("ASN1_Class and ASN1_Type have different underlying types");
290
   }
291

292
   return result;
1✔
293
}
×
294

295
Test::Result test_asn1_negative_int_encoding() {
1✔
296
   Test::Result result("DER encode/decode of negative integers");
1✔
297

298
   BigInt n(32);
1✔
299

300
   for(size_t i = 0; i != 2048; ++i) {
2,049✔
301
      n--;
2,048✔
302

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

305
      BigInt n_dec;
2,048✔
306
      Botan::BER_Decoder(enc, Botan::BER_Decoder::Limits::DER()).decode(n_dec);
4,096✔
307

308
      result.test_bn_eq("DER encoding round trips negative integers", n_dec, n);
2,048✔
309
   }
2,048✔
310

311
   return result;
1✔
312
}
1✔
313

314
Test::Result test_der_constructed_tag_17_not_sorted() {
1✔
315
   Test::Result result("DER constructed [17] is not SET-sorted");
1✔
316

317
   // Two INTEGERs in descending order. A universal SET would lex-sort and put
318
   // 0x01 before 0x02; a non-universal constructed [17] must preserve order.
319
   const std::vector<uint8_t> first = {0x02, 0x01, 0x02};   // INTEGER 2
1✔
320
   const std::vector<uint8_t> second = {0x02, 0x01, 0x01};  // INTEGER 1
1✔
321

322
   auto encode_with = [&](auto starter) {
5✔
323
      Botan::DER_Encoder enc;
4✔
324
      starter(enc).raw_bytes(first).raw_bytes(second).end_cons();
4✔
325
      return enc.get_contents_unlocked();
8✔
326
   };
5✔
327

328
   // Reference: a universal SET of the same children gets sorted
329
   const auto set_enc = encode_with([](Botan::DER_Encoder& e) -> Botan::DER_Encoder& { return e.start_set(); });
2✔
330
   const std::vector<uint8_t> set_expected = {0x31, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02};
1✔
331
   result.test_bin_eq("universal SET is lex-sorted", set_enc, set_expected);
1✔
332

333
   // start_context_specific(17): tag byte = ContextSpecific | Constructed | 17 = 0xB1
334
   const auto ctx_enc =
1✔
335
      encode_with([](Botan::DER_Encoder& e) -> Botan::DER_Encoder& { return e.start_context_specific(17); });
2✔
336
   const std::vector<uint8_t> ctx_expected = {0xB1, 0x06, 0x02, 0x01, 0x02, 0x02, 0x01, 0x01};
1✔
337
   result.test_bin_eq("context-specific [17] preserves order", ctx_enc, ctx_expected);
1✔
338

339
   // start_explicit_context_specific(17): same tag byte 0xB1
340
   const auto explicit_ctx_enc =
1✔
341
      encode_with([](Botan::DER_Encoder& e) -> Botan::DER_Encoder& { return e.start_explicit_context_specific(17); });
2✔
342
   result.test_bin_eq("explicit context-specific [17] preserves order", explicit_ctx_enc, ctx_expected);
1✔
343

344
   // start_explicit(17): used to throw Internal_Error; must now produce [17] in order
345
   const auto explicit_enc =
1✔
346
      encode_with([](Botan::DER_Encoder& e) -> Botan::DER_Encoder& { return e.start_explicit(17); });
2✔
347
   result.test_bin_eq("start_explicit(17) preserves order", explicit_enc, ctx_expected);
1✔
348

349
   return result;
1✔
350
}
1✔
351

352
Test::Result test_ber_indefinite_length_trailing_data() {
1✔
353
   Test::Result result("BER indefinite length trailing data");
1✔
354

355
   // Case 1: verify_end after consuming indef SEQUENCE
356
   try {
1✔
357
      const std::vector<uint8_t> enc = {0x30, 0x80, 0x02, 0x01, 0x42, 0x00, 0x00};
1✔
358
      Botan::BER_Decoder dec(enc);
1✔
359
      Botan::BigInt x;
1✔
360
      dec.start_sequence().decode(x).end_cons();
1✔
361
      dec.verify_end();
1✔
362
      result.test_bn_eq("verify_end decoded x", x, Botan::BigInt(0x42));
1✔
363
   } catch(Botan::Exception& e) {
1✔
364
      result.test_failure("verify_end after indef SEQUENCE", e.what());
×
365
   }
×
366

367
   // Case 2: two back-to-back indef SEQUENCES at top level
368
   try {
1✔
369
      const std::vector<uint8_t> enc = {
1✔
370
         0x30, 0x80, 0x02, 0x01, 0x42, 0x00, 0x00, 0x30, 0x80, 0x02, 0x01, 0x43, 0x00, 0x00};
1✔
371
      Botan::BER_Decoder dec(enc);
1✔
372
      Botan::BigInt x;
1✔
373
      Botan::BigInt y;
1✔
374
      dec.start_sequence().decode(x).end_cons();
1✔
375
      dec.start_sequence().decode(y).end_cons();
1✔
376
      dec.verify_end();
1✔
377
      result.test_bn_eq("back-to-back x", x, Botan::BigInt(0x42));
1✔
378
      result.test_bn_eq("back-to-back y", y, Botan::BigInt(0x43));
1✔
379
   } catch(Botan::Exception& e) {
1✔
380
      result.test_failure("two back-to-back indef SEQUENCES", e.what());
×
381
   }
×
382

383
   // Case 3: nested indef SEQUENCES
384
   try {
1✔
385
      const std::vector<uint8_t> enc = {0x30, 0x80, 0x30, 0x80, 0x02, 0x01, 0x42, 0x00, 0x00, 0x00, 0x00};
1✔
386
      Botan::BER_Decoder dec(enc);
1✔
387
      Botan::BigInt x;
1✔
388
      auto outer = dec.start_sequence();
1✔
389
      outer.start_sequence().decode(x).end_cons();
1✔
390
      outer.end_cons();
1✔
391
      dec.verify_end();
1✔
392
      result.test_bn_eq("nested x", x, Botan::BigInt(0x42));
1✔
393
   } catch(Botan::Exception& e) {
1✔
394
      result.test_failure("nested indef SEQUENCE", e.what());
×
395
   }
×
396

397
   // Case 4: while(more_items()) loop over an indef SEQUENCE
398
   try {
1✔
399
      const std::vector<uint8_t> enc = {0x30, 0x80, 0x02, 0x01, 0x42, 0x02, 0x01, 0x43, 0x00, 0x00};
1✔
400
      Botan::BER_Decoder dec(enc);
1✔
401
      auto seq = dec.start_sequence();
1✔
402
      std::vector<Botan::BigInt> xs;
1✔
403
      while(seq.more_items()) {
3✔
404
         Botan::BigInt x;
2✔
405
         seq.decode(x);
2✔
406
         xs.push_back(x);
2✔
407
      }
2✔
408
      seq.end_cons();
1✔
409
      dec.verify_end();
1✔
410
      result.test_sz_eq("more_items count", xs.size(), 2);
1✔
411
      if(xs.size() == 2) {
1✔
412
         result.test_bn_eq("more_items xs[0]", xs[0], Botan::BigInt(0x42));
1✔
413
         result.test_bn_eq("more_items xs[1]", xs[1], Botan::BigInt(0x43));
1✔
414
      }
415
   } catch(Botan::Exception& e) {
1✔
416
      result.test_failure("more_items loop over indef SEQUENCE", e.what());
×
417
   }
×
418

419
   return result;
1✔
420
}
×
421

422
Test::Result test_ber_find_eoc() {
1✔
423
   Test::Result result("BER indefinite length EOC matching");
1✔
424

425
   const size_t num_siblings = 4096;
1✔
426

427
   std::vector<uint8_t> ber;
1✔
428
   ber.push_back(0x30);  // outer SEQUENCE | CONSTRUCTED
1✔
429
   ber.push_back(0x80);  // indefinite length
1✔
430
   for(size_t i = 0; i != num_siblings; ++i) {
4,097✔
431
      ber.push_back(0x30);  // inner SEQUENCE | CONSTRUCTED
4,096✔
432
      ber.push_back(0x80);  // indefinite length
4,096✔
433
      ber.push_back(0x00);  // EOC tag
4,096✔
434
      ber.push_back(0x00);  // EOC length
4,096✔
435
   }
436
   ber.push_back(0x00);  // outer EOC tag
1✔
437
   ber.push_back(0x00);  // outer EOC length
1✔
438

439
   try {
1✔
440
      Botan::BER_Decoder dec(ber);
1✔
441
      const Botan::BER_Object obj = dec.get_next_object();
1✔
442

443
      result.test_sz_eq("object body includes children", obj.length(), num_siblings * 4);
1✔
444
   } catch(Botan::Exception& e) {
1✔
445
      result.test_failure("decode failed", e.what());
×
446
   }
×
447

448
   return result;
1✔
449
}
1✔
450

451
Test::Result test_asn1_string_zero_length_roundtrip() {
1✔
452
   Test::Result result("ASN.1 String zero-length round-trip");
1✔
453

454
   auto roundtrip = [&](const char* what, const std::vector<uint8_t>& wire) {
4✔
455
      try {
3✔
456
         Botan::DataSource_Memory input(wire.data(), wire.size());
3✔
457
         Botan::BER_Decoder dec(input);
3✔
458
         Botan::ASN1_String str;
3✔
459
         str.decode_from(dec);
3✔
460

461
         Botan::DER_Encoder enc;
3✔
462
         str.encode_into(enc);
3✔
463
         const auto out = enc.get_contents();
3✔
464
         result.test_bin_eq(what, std::span{out}, std::span{wire});
3✔
465
      } catch(const std::exception& ex) {
9✔
466
         result.test_failure(Botan::fmt("{}: unexpected throw: {}", what, ex.what()));
×
467
      }
×
468
   };
3✔
469

470
   roundtrip("BmpString 1E 00", {0x1E, 0x00});
1✔
471
   roundtrip("UniversalString 1C 00", {0x1C, 0x00});
1✔
472
   roundtrip("TeletexString 14 00", {0x14, 0x00});
1✔
473

474
   return result;
1✔
475
}
×
476

477
Test::Result test_pss_params_rejects_trailing_data_in_mgf1_params() {
1✔
478
   Test::Result result("PSS-Params rejects trailing data in MGF1 parameters");
1✔
479

480
   const Botan::AlgorithmIdentifier sha256_alg_id("SHA-256", Botan::AlgorithmIdentifier::USE_NULL_PARAM);
1✔
481
   const auto sha256_der = sha256_alg_id.BER_encode();
1✔
482

483
   auto encode_pss_params = [&](const std::vector<uint8_t>& mgf_params) {
3✔
484
      const Botan::AlgorithmIdentifier mgf("MGF1", mgf_params);
2✔
485
      Botan::DER_Encoder enc;
2✔
486
      enc.start_sequence()
2✔
487
         .start_context_specific(0)
2✔
488
         .encode(sha256_alg_id)
2✔
489
         .end_cons()
2✔
490
         .start_context_specific(1)
2✔
491
         .encode(mgf)
2✔
492
         .end_cons()
2✔
493
         .start_context_specific(2)
2✔
494
         .encode(static_cast<size_t>(32))
2✔
495
         .end_cons()
2✔
496
         .end_cons();
2✔
497
      return enc.get_contents();
2✔
498
   };
4✔
499

500
   try {
1✔
501
      const auto clean_der = encode_pss_params(sha256_der);
1✔
502
      const Botan::PSS_Params clean(clean_der);
1✔
503
      result.test_success("control: clean PSS-Params decodes");
1✔
504
   } catch(const std::exception& e) {
2✔
505
      result.test_failure(Botan::fmt("clean PSS-Params unexpected throw: {}", e.what()));
×
506
   }
×
507

508
   std::vector<uint8_t> mgf_params_with_junk = sha256_der;
1✔
509
   const std::vector<uint8_t> trailing_junk{0x02, 0x01, 0x00};
1✔
510
   mgf_params_with_junk.insert(mgf_params_with_junk.end(), trailing_junk.begin(), trailing_junk.end());
1✔
511
   const auto bad_der = encode_pss_params(mgf_params_with_junk);
1✔
512

513
   result.test_throws<Botan::Decoding_Error>("PSS-Params rejects trailing data in MGF1 parameters",
1✔
514
                                             [&]() { const Botan::PSS_Params bad(bad_der); });
2✔
515

516
   return result;
1✔
517
}
2✔
518

519
class ASN1_Tests final : public Test {
1✔
520
   public:
521
      std::vector<Test::Result> run() override {
1✔
522
         std::vector<Test::Result> results;
1✔
523

524
         results.push_back(test_ber_stack_recursion());
2✔
525
         results.push_back(test_ber_eoc_decoding_limits());
2✔
526
         results.push_back(test_ber_indefinite_length_trailing_data());
2✔
527
         results.push_back(test_ber_find_eoc());
2✔
528
         results.push_back(test_asn1_utf8_ascii_parsing());
2✔
529
         results.push_back(test_asn1_utf8_parsing());
2✔
530
         results.push_back(test_asn1_ucs2_parsing());
2✔
531
         results.push_back(test_asn1_ucs4_parsing());
2✔
532
         results.push_back(test_asn1_ucs_invalid_codepoint_rejection());
2✔
533
         results.push_back(test_asn1_ascii_encoding());
2✔
534
         results.push_back(test_asn1_utf8_encoding());
2✔
535
         results.push_back(test_asn1_tag_underlying_type());
2✔
536
         results.push_back(test_asn1_negative_int_encoding());
2✔
537
         results.push_back(test_der_constructed_tag_17_not_sorted());
2✔
538
         results.push_back(test_asn1_string_zero_length_roundtrip());
2✔
539
         results.push_back(test_pss_params_rejects_trailing_data_in_mgf1_params());
2✔
540

541
         return results;
1✔
542
      }
×
543
};
544

545
BOTAN_REGISTER_TEST("asn1", "asn1_encoding", ASN1_Tests);
546

547
class ASN1_Time_Parsing_Tests final : public Text_Based_Test {
×
548
   public:
549
      ASN1_Time_Parsing_Tests() : Text_Based_Test("asn1_time.vec", "Tspec") {}
2✔
550

551
      Test::Result run_one_test(const std::string& tag_str, const VarMap& vars) override {
26✔
552
         Test::Result result("ASN.1 date parsing");
26✔
553

554
         const std::string tspec = vars.get_req_str("Tspec");
26✔
555

556
         if(tag_str != "UTC" && tag_str != "UTC.invalid" && tag_str != "Generalized" &&
26✔
557
            tag_str != "Generalized.invalid") {
13✔
558
            throw Test_Error("Invalid tag value in ASN1 date parsing test");
×
559
         }
560

561
         const bool out_of_range = [&]() -> bool {
78✔
562
            if(tspec.size() == 15) {
26✔
563
               const size_t year = Botan::to_u32bit(std::string_view(tspec).substr(0, 4));
24✔
564
               if(year >= 2262) {
24✔
565
                  return true;
566
               }
567
               if(year >= 2038 && sizeof(time_t) == 4) {
568
                  return true;
569
               }
570
            }
571

572
            return false;
573
         }();
26✔
574

575
         const Botan::ASN1_Type tag = (tag_str == "UTC" || tag_str == "UTC.invalid")
25✔
576
                                         ? Botan::ASN1_Type::UtcTime
26✔
577
                                         : Botan::ASN1_Type::GeneralizedTime;
26✔
578

579
         const bool valid = tag_str.find(".invalid") == std::string::npos;
26✔
580

581
         if(valid) {
26✔
582
            const Botan::ASN1_Time time(tspec, tag);
12✔
583
            result.test_success("Accepted valid time");
12✔
584

585
            try {
12✔
586
               const auto std_timepoint = time.to_std_timepoint();
12✔
587
               result.test_success("Was able to convert time to std timepoint");
10✔
588

589
               const auto from_std_timepoint = Botan::ASN1_Time::from_time_point(std_timepoint);
10✔
590
               result.test_is_true("ASN1_Time from std timepoint matches input", from_std_timepoint == time);
10✔
591
            } catch(std::exception& e) {
12✔
592
               if(out_of_range) {
2✔
593
                  result.test_str_contains("Exception message", e.what(), "time is outside the representable range");
2✔
594
               } else {
595
                  result.test_failure("Was not able to convert time to std timepoint", e.what());
×
596
               }
597
            }
2✔
598
         } else {
12✔
599
            result.test_throws("Invalid time rejected", [=]() { const Botan::ASN1_Time time(tspec, tag); });
70✔
600
         }
601

602
         return result;
26✔
603
      }
26✔
604
};
605

606
BOTAN_REGISTER_TEST("asn1", "asn1_time", ASN1_Time_Parsing_Tests);
607

608
class ASN1_String_Validation_Tests final : public Text_Based_Test {
×
609
   public:
610
      ASN1_String_Validation_Tests() :
1✔
611
            Text_Based_Test("asn1_string_validation.vec",
612
                            "Input,ValidNumeric,ValidPrintable,ValidIa5,ValidVisible,ValidUtf8") {}
2✔
613

614
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
7✔
615
         Test::Result result("ASN.1 string validation");
7✔
616

617
         const auto input = vars.get_req_str("Input");
7✔
618
         const bool valid_numeric = vars.get_req_bool("ValidNumeric");
7✔
619
         const bool valid_printable = vars.get_req_bool("ValidPrintable");
7✔
620
         const bool valid_ia5 = vars.get_req_bool("ValidIa5");
7✔
621
         const bool valid_visible = vars.get_req_bool("ValidVisible");
7✔
622
         const bool valid_utf8 = vars.get_req_bool("ValidUtf8");
7✔
623

624
         test_string_type(result, input, "NumericString", Botan::ASN1_Type::NumericString, valid_numeric);
7✔
625
         test_string_type(result, input, "PrintableString", Botan::ASN1_Type::PrintableString, valid_printable);
7✔
626
         test_string_type(result, input, "Ia5String", Botan::ASN1_Type::Ia5String, valid_ia5);
7✔
627
         test_string_type(result, input, "VisibleString", Botan::ASN1_Type::VisibleString, valid_visible);
7✔
628
         test_string_type(result, input, "Utf8String", Botan::ASN1_Type::Utf8String, valid_utf8);
7✔
629

630
         if(valid_utf8) {
7✔
631
            try {
7✔
632
               const Botan::ASN1_String str(input);
7✔
633
               const auto expected_tag =
14✔
634
                  valid_printable ? Botan::ASN1_Type::PrintableString : Botan::ASN1_Type::Utf8String;
7✔
635
               result.test_u32_eq("String tagging categorization",
7✔
636
                                  static_cast<uint32_t>(str.tagging()),
7✔
637
                                  static_cast<uint32_t>(expected_tag));
638
            } catch(const std::exception& ex) {
7✔
639
               result.test_failure(Botan::fmt("default constructor unexpectedly rejected '{}': {}", input, ex.what()));
×
640
            }
×
641
         }
642

643
         return result;
7✔
644
      }
7✔
645

646
   private:
647
      void test_string_type(Test::Result& result,
35✔
648
                            std::string_view input,
649
                            std::string_view type,
650
                            Botan::ASN1_Type tag,
651
                            bool expected_valid) {
652
         if(expected_valid) {
35✔
653
            try {
24✔
654
               const Botan::ASN1_String str(input, tag);
24✔
655
               result.test_str_eq(Botan::fmt("{} constructor value", type), str.value(), input);
24✔
656

657
               const auto enc = raw_encode_string(input, tag);
24✔
658
               Botan::BER_Decoder dec(enc);
24✔
659
               Botan::ASN1_String decoded;
24✔
660
               decoded.decode_from(dec);
24✔
661
               result.test_str_eq(Botan::fmt("{} decode value", type), decoded.value(), input);
48✔
662
            } catch(const std::exception& e) {
48✔
663
               result.test_failure(Botan::fmt("{} unexpectedly rejected '{}': {}", type, input, e.what()));
×
664
            }
×
665
         } else {
666
            result.test_throws(Botan::fmt("{} constructor rejects", type),
22✔
667
                               [&]() { const Botan::ASN1_String str(input, tag); });
22✔
668

669
            result.test_throws(Botan::fmt("{} decode rejects", type), [&]() {
22✔
670
               const auto enc = raw_encode_string(input, tag);
11✔
671
               Botan::BER_Decoder dec(enc);
11✔
672
               Botan::ASN1_String decoded;
11✔
673
               decoded.decode_from(dec);
11✔
674
            });
22✔
675
         }
676
      }
35✔
677

678
      static std::vector<uint8_t> raw_encode_string(std::string_view input, Botan::ASN1_Type tag) {
35✔
679
         std::vector<uint8_t> encoding;
35✔
680
         Botan::DER_Encoder der(encoding);
35✔
681
         der.add_object(tag, Botan::ASN1_Class::Universal, input);
35✔
682
         return encoding;
35✔
683
      }
35✔
684
};
685

686
BOTAN_REGISTER_TEST("asn1", "asn1_string_validation", ASN1_String_Validation_Tests);
687

688
class ASN1_Printer_Tests final : public Test {
1✔
689
   public:
690
      std::vector<Test::Result> run() override {
1✔
691
         Test::Result result("ASN1_Pretty_Printer");
1✔
692

693
         const Botan::ASN1_Pretty_Printer printer;
1✔
694

695
         const size_t num_tests = 8;
1✔
696

697
         for(size_t i = 1; i <= num_tests; ++i) {
9✔
698
            const std::string i_str = std::to_string(i);
8✔
699
            const std::vector<uint8_t> input_data = Test::read_binary_data_file("asn1_print/input" + i_str + ".der");
24✔
700
            const std::string expected_output = Test::read_data_file("asn1_print/output" + i_str + ".txt");
24✔
701

702
            try {
8✔
703
               const std::string output = printer.print(input_data);
8✔
704
               result.test_str_eq("Test " + i_str, output, expected_output);
16✔
705
            } catch(Botan::Exception& e) {
8✔
706
               result.test_failure(Botan::fmt("Printing test {} failed with an exception: '{}'", i, e.what()));
×
707
            }
×
708
         }
8✔
709

710
         return {result};
2✔
711
      }
10✔
712
};
713

714
BOTAN_REGISTER_TEST("asn1", "asn1_printer", ASN1_Printer_Tests);
715

716
class ASN1_Decoding_Tests final : public Text_Based_Test {
×
717
   public:
718
      ASN1_Decoding_Tests() : Text_Based_Test("asn1_decoding.vec", "Input,ResultBER", "ResultDER") {}
2✔
719

720
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
55✔
721
         const auto input = vars.get_req_bin("Input");
55✔
722
         const std::string expected_ber = vars.get_req_str("ResultBER");
55✔
723
         const std::string expected_der = vars.get_opt_str("ResultDER", expected_ber);
55✔
724

725
         Test::Result result("ASN1 decoding");
55✔
726

727
         decoding_test(result, input, expected_ber, false);
55✔
728
         decoding_test(result, input, expected_der, true);
55✔
729

730
         return result;
110✔
731
      }
55✔
732

733
   private:
734
      static void decoding_test(Test::Result& result,
110✔
735
                                std::span<const uint8_t> input,
736
                                std::string_view expected,
737
                                bool require_der) {
738
         const Botan::ASN1_Pretty_Printer printer(4096, 2048, true, 0, 60, 64, require_der);
110✔
739
         const std::string mode = require_der ? "DER" : "BER";
165✔
740
         std::ostringstream sink;
110✔
741

742
         try {
110✔
743
            printer.print_to_stream(sink, input.data(), input.size());
110✔
744

745
            if(expected == "OK") {
52✔
746
               result.test_success();
52✔
747
            } else {
748
               result.test_failure(Botan::fmt("Accepted invalid {} input, expected error {}", mode, expected));
×
749
            }
750
         } catch(const std::exception& e) {
58✔
751
            if(expected == "OK") {
58✔
752
               result.test_failure(Botan::fmt("Rejected valid {} input with {}", mode, e.what()));
×
753
            } else {
754
               // BER_Decoding_Error prepends "BER: " to the message
755
               std::string msg = e.what();
58✔
756
               if(msg.starts_with("BER: ")) {
58✔
757
                  msg = msg.substr(5);
38✔
758
               }
759
               result.test_str_eq("error message", msg, expected);
58✔
760
            }
58✔
761
         }
58✔
762
      }
148✔
763
};
764

765
BOTAN_REGISTER_TEST("asn1", "asn1_decoding", ASN1_Decoding_Tests);
766

767
#endif
768

769
}  // namespace
770

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