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

randombit / botan / 28147269151

24 Jun 2026 08:07PM UTC coverage: 89.355% (-0.01%) from 89.368%
28147269151

push

github

web-flow
Merge pull request #5693 from randombit/jack/asn1-bit-string

Improve ASN.1 APIs for implicit tag and bitstring handling

112042 of 125390 relevant lines covered (89.35%)

11185217.34 hits per line

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

90.33
/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
class ASN1_Test_Sequence final : public Botan::ASN1_Object {
2✔
29
   public:
30
      explicit ASN1_Test_Sequence(size_t value = 0) : m_value(value) {}
2✔
31

32
      void encode_into(Botan::DER_Encoder& der) const override { der.start_sequence().encode(m_value).end_cons(); }
1✔
33

34
      void decode_from(Botan::BER_Decoder& ber) override { ber.start_sequence().decode(m_value).end_cons(); }
1✔
35

36
      size_t value() const { return m_value; }
1✔
37

38
   private:
39
      size_t m_value;
40
};
41

42
Test::Result test_ber_stack_recursion() {
1✔
43
   Test::Result result("BER stack recursion");
1✔
44

45
   // OSS-Fuzz #813 GitHub #989
46

47
   try {
1✔
48
      const std::vector<uint8_t> in(10000000, 0);
1✔
49
      Botan::DataSource_Memory input(in.data(), in.size());
1✔
50
      Botan::BER_Decoder dec(input);
1✔
51

52
      while(dec.more_items()) {
2✔
53
         Botan::BER_Object obj;
1✔
54
         dec.get_next(obj);
1✔
55
      }
1✔
56
   } catch(Botan::Decoding_Error&) {}
2✔
57

58
   result.test_success("No crash");
1✔
59

60
   return result;
1✔
61
}
×
62

63
Test::Result test_ber_eoc_decoding_limits() {
1✔
64
   Test::Result result("BER nested indefinite length");
1✔
65

66
   // OSS-Fuzz #4353
67

68
   const Botan::ASN1_Pretty_Printer printer;
1✔
69

70
   size_t max_eoc_allowed = 0;
1✔
71

72
   for(size_t len = 1; len < 1024; ++len) {
17✔
73
      std::vector<uint8_t> buf(4 * len);
17✔
74

75
      /*
76
      This constructs a len deep sequence of SEQUENCES each with
77
      an indefinite length
78
      */
79
      for(size_t i = 0; i != 2 * len; i += 2) {
170✔
80
         buf[i] = 0x30;
153✔
81
         buf[i + 1] = 0x80;
153✔
82
      }
83
      // remainder of values left as zeros (EOC markers)
84

85
      try {
17✔
86
         printer.print(buf);
17✔
87
      } catch(Botan::BER_Decoding_Error&) {
1✔
88
         max_eoc_allowed = len - 1;
1✔
89
         break;
1✔
90
      }
1✔
91
   }
17✔
92

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

95
   return result;
1✔
96
}
1✔
97

98
Test::Result test_asn1_utf8_ascii_parsing() {
1✔
99
   Test::Result result("ASN.1 ASCII parsing");
1✔
100

101
   try {
1✔
102
      // \x13 - ASN1 tag for 'printable string'
103
      // \x06 - 6 characters of payload
104
      // ...  - UTF-8 encoded (ASCII chars only) word 'Moscow'
105
      const std::string moscow = "\x13\x06\x4D\x6F\x73\x63\x6F\x77";
1✔
106
      const std::string moscow_plain = "Moscow";
1✔
107
      Botan::DataSource_Memory input(moscow);
1✔
108
      Botan::BER_Decoder dec(input);
1✔
109

110
      Botan::ASN1_String str;
1✔
111
      str.decode_from(dec);
1✔
112

113
      result.test_str_eq("value()", str.value(), moscow_plain);
1✔
114
   } catch(const Botan::Decoding_Error& ex) {
2✔
115
      result.test_failure(ex.what());
×
116
   }
×
117

118
   return result;
1✔
119
}
×
120

121
Test::Result test_asn1_utf8_parsing() {
1✔
122
   Test::Result result("ASN.1 UTF-8 parsing");
1✔
123

124
   try {
1✔
125
      // \x0C - ASN1 tag for 'UTF8 string'
126
      // \x0C - 12 characters of payload
127
      // ...  - UTF-8 encoded russian word for Moscow in cyrillic script
128
      const std::string moscow = "\x0C\x0C\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
129
      const std::string moscow_plain = "\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
130
      Botan::DataSource_Memory input(moscow);
1✔
131
      Botan::BER_Decoder dec(input);
1✔
132

133
      Botan::ASN1_String str;
1✔
134
      str.decode_from(dec);
1✔
135

136
      result.test_str_eq("value()", str.value(), moscow_plain);
1✔
137
   } catch(const Botan::Decoding_Error& ex) {
2✔
138
      result.test_failure(ex.what());
×
139
   }
×
140

141
   return result;
1✔
142
}
×
143

144
Test::Result test_asn1_ucs2_parsing() {
1✔
145
   Test::Result result("ASN.1 BMP string (UCS-2) parsing");
1✔
146

147
   try {
1✔
148
      // \x1E     - ASN1 tag for 'BMP (UCS-2) string'
149
      // \x0C     - 12 characters of payload
150
      // ...      - UCS-2 encoding for Moscow in cyrillic script
151
      const std::string moscow = "\x1E\x0C\x04\x1C\x04\x3E\x04\x41\x04\x3A\x04\x32\x04\x30";
1✔
152
      const std::string moscow_plain = "\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
153

154
      Botan::DataSource_Memory input(moscow);
1✔
155
      Botan::BER_Decoder dec(input);
1✔
156

157
      Botan::ASN1_String str;
1✔
158
      str.decode_from(dec);
1✔
159

160
      result.test_str_eq("value()", str.value(), moscow_plain);
1✔
161
   } catch(const Botan::Decoding_Error& ex) {
2✔
162
      result.test_failure(ex.what());
×
163
   }
×
164

165
   return result;
1✔
166
}
×
167

168
Test::Result test_asn1_ucs4_parsing() {
1✔
169
   Test::Result result("ASN.1 universal string (UCS-4) parsing");
1✔
170

171
   try {
1✔
172
      // \x1C - ASN1 tag for 'universal string'
173
      // \x18 - 24 characters of payload
174
      // ...  - UCS-4 encoding for Moscow in cyrillic script
175
      const uint8_t moscow[] =
1✔
176
         "\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";
177
      const std::string moscow_plain = "\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
178
      Botan::DataSource_Memory input(moscow, sizeof(moscow));
1✔
179
      Botan::BER_Decoder dec(input);
1✔
180

181
      Botan::ASN1_String str;
1✔
182
      str.decode_from(dec);
1✔
183

184
      result.test_str_eq("value()", str.value(), moscow_plain);
1✔
185
   } catch(const Botan::Decoding_Error& ex) {
2✔
186
      result.test_failure(ex.what());
×
187
   }
×
188

189
   return result;
1✔
190
}
×
191

192
Test::Result test_asn1_ucs_invalid_codepoint_rejection() {
1✔
193
   Test::Result result("ASN.1 UCS-2/UCS-4 invalid codepoint rejection");
1✔
194

195
   auto expect_decode_throws = [&](const char* what, const std::vector<uint8_t>& wire) {
7✔
196
      result.test_throws(what, [&]() {
6✔
197
         Botan::DataSource_Memory input(wire.data(), wire.size());
6✔
198
         Botan::BER_Decoder dec(input);
6✔
199
         Botan::ASN1_String str;
6✔
200
         str.decode_from(dec);
6✔
201
      });
6✔
202
   };
6✔
203

204
   auto expect_decode_ok = [&](const char* what, const std::vector<uint8_t>& wire) {
2✔
205
      try {
1✔
206
         Botan::DataSource_Memory input(wire.data(), wire.size());
1✔
207
         Botan::BER_Decoder dec(input);
1✔
208
         Botan::ASN1_String str;
1✔
209
         str.decode_from(dec);
1✔
210
         result.test_success(what);
1✔
211
      } catch(const std::exception& ex) {
2✔
212
         result.test_failure(Botan::fmt("{}: unexpected throw: {}", what, ex.what()));
×
213
      }
×
214
   };
1✔
215

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

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

222
   // UniversalString with high surrogate 0xD800
223
   expect_decode_throws("UniversalString rejects surrogate codepoint", {0x1C, 0x04, 0x00, 0x00, 0xD8, 0x00});
1✔
224

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

228
   // BmpString (tag 0x1E) with high surrogate
229
   expect_decode_throws("BmpString rejects surrogate codepoint", {0x1E, 0x02, 0xD8, 0x00});
1✔
230

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

234
   // UniversalString with non-multiple-of-4 length is malformed
235
   expect_decode_throws("UniversalString rejects non-multiple-of-4 payload",
1✔
236
                        {0x1C, 0x05, 0x00, 0x00, 0x00, 0x41, 0x00});
237

238
   return result;
1✔
239
}
×
240

241
Test::Result test_asn1_ascii_encoding() {
1✔
242
   Test::Result result("ASN.1 ASCII encoding");
1✔
243

244
   try {
1✔
245
      // UTF-8 encoded (ASCII chars only) word 'Moscow'
246
      const std::string moscow = "Moscow";
1✔
247
      const Botan::ASN1_String str(moscow);
1✔
248

249
      Botan::DER_Encoder enc;
1✔
250

251
      str.encode_into(enc);
1✔
252
      auto encodingResult = enc.get_contents();
1✔
253

254
      // \x13 - ASN1 tag for 'printable string'
255
      // \x06 - 6 characters of payload
256
      result.test_bin_eq("encoding result", encodingResult, "13064D6F73636F77");
1✔
257

258
      result.test_success("No crash");
1✔
259
   } catch(const std::exception& ex) {
2✔
260
      result.test_failure(ex.what());
×
261
   }
×
262

263
   return result;
1✔
264
}
×
265

266
Test::Result test_asn1_utf8_encoding() {
1✔
267
   Test::Result result("ASN.1 UTF-8 encoding");
1✔
268

269
   try {
1✔
270
      // UTF-8 encoded russian word for Moscow in cyrillic script
271
      const std::string moscow = "\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
272
      const Botan::ASN1_String str(moscow);
1✔
273

274
      Botan::DER_Encoder enc;
1✔
275

276
      str.encode_into(enc);
1✔
277
      auto encodingResult = enc.get_contents();
1✔
278

279
      // \x0C - ASN1 tag for 'UTF8 string'
280
      // \x0C - 12 characters of payload
281
      result.test_bin_eq("encoding result", encodingResult, "0C0CD09CD0BED181D0BAD0B2D0B0");
1✔
282

283
      result.test_success("No crash");
1✔
284
   } catch(const std::exception& ex) {
2✔
285
      result.test_failure(ex.what());
×
286
   }
×
287

288
   return result;
1✔
289
}
×
290

291
Test::Result test_asn1_tag_underlying_type() {
1✔
292
   Test::Result result("ASN.1 class and type underlying type");
1✔
293

294
   if constexpr(std::is_same_v<std::underlying_type_t<Botan::ASN1_Class>, std::underlying_type_t<Botan::ASN1_Type>>) {
1✔
295
      if constexpr(!std::is_same_v<std::underlying_type_t<Botan::ASN1_Class>,
1✔
296
                                   std::invoke_result_t<decltype(&Botan::BER_Object::tagging), Botan::BER_Object>>) {
297
         result.test_failure(
298
            "Return type of BER_Object::tagging() is different than the underlying type of ASN1_Class");
299
      } else {
300
         result.test_success("Same types");
1✔
301
      }
302
   } else {
303
      result.test_failure("ASN1_Class and ASN1_Type have different underlying types");
304
   }
305

306
   return result;
1✔
307
}
×
308

309
Test::Result test_asn1_negative_int_encoding() {
1✔
310
   Test::Result result("DER encode/decode of negative integers");
1✔
311

312
   BigInt n(32);
1✔
313

314
   for(size_t i = 0; i != 2048; ++i) {
2,049✔
315
      n--;
2,048✔
316

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

319
      BigInt n_dec;
2,048✔
320
      Botan::BER_Decoder(enc, Botan::BER_Decoder::Limits::DER()).decode(n_dec);
4,096✔
321

322
      result.test_bn_eq("DER encoding round trips negative integers", n_dec, n);
2,048✔
323
   }
2,048✔
324

325
   return result;
1✔
326
}
1✔
327

328
Test::Result test_der_constructed_tag_17_not_sorted() {
1✔
329
   Test::Result result("DER constructed [17] is not SET-sorted");
1✔
330

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

336
   auto encode_with = [&](auto starter) {
5✔
337
      Botan::DER_Encoder enc;
4✔
338
      starter(enc).raw_bytes(first).raw_bytes(second).end_cons();
4✔
339
      return enc.get_contents_unlocked();
8✔
340
   };
5✔
341

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

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

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

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

363
   return result;
1✔
364
}
1✔
365

366
Test::Result test_der_implicit_tagging_helpers() {
1✔
367
   Test::Result result("DER implicit tagging helpers");
1✔
368

369
   const std::vector<uint8_t> first = {0x02, 0x01, 0x02};   // INTEGER 2
1✔
370
   const std::vector<uint8_t> second = {0x02, 0x01, 0x01};  // INTEGER 1
1✔
371

372
   Botan::DER_Encoder set_enc;
1✔
373
   set_enc.start_set(23).raw_bytes(first).raw_bytes(second).end_cons();
1✔
374
   const auto implicit_set = set_enc.get_contents_unlocked();
1✔
375
   result.test_bin_eq("implicit SET is still sorted", implicit_set, "B706020101020102");
1✔
376

377
   const ASN1_Test_Sequence seq(42);
1✔
378
   const auto implicit_seq = Botan::DER_Encoder().encode_implicit(seq, Botan::ASN1_Type(3)).get_contents_unlocked();
1✔
379
   result.test_bin_eq("implicit constructed object keeps constructed bit", implicit_seq, "A30302012A");
1✔
380

381
   ASN1_Test_Sequence decoded;
1✔
382
   Botan::BER_Decoder(implicit_seq, Botan::BER_Decoder::Limits::DER())
2✔
383
      .decode_implicit(decoded,
1✔
384
                       Botan::ASN1_Type(3),
385
                       Botan::ASN1_Class::ContextSpecific | Botan::ASN1_Class::Constructed,
386
                       Botan::ASN1_Type::Sequence,
387
                       Botan::ASN1_Class::Constructed)
388
      .verify_end();
1✔
389
   result.test_sz_eq("implicit constructed object decodes", decoded.value(), 42);
1✔
390

391
   const std::vector<uint8_t> one_bit = {0x80};
1✔
392
   const auto implicit_bitstring =
1✔
393
      Botan::DER_Encoder()
1✔
394
         .encode_bitstring(one_bit, 7, Botan::ASN1_Type(1), Botan::ASN1_Class::ContextSpecific)
2✔
395
         .get_contents_unlocked();
1✔
396
   result.test_bin_eq("implicit BIT STRING keeps unused bit count", implicit_bitstring, "81020780");
1✔
397

398
   const std::vector<uint8_t> bad_padding = {0x81};
1✔
399
   result.test_throws<Botan::Invalid_Argument>("BIT STRING unused bits must be zero",
1✔
400
                                               [&] { Botan::DER_Encoder().encode_bitstring(bad_padding, 7); });
2✔
401

402
   return result;
1✔
403
}
1✔
404

405
Test::Result test_asn1_bitstring_helpers() {
1✔
406
   Test::Result result("ASN.1 BIT STRING helpers");
1✔
407

408
   const std::vector<uint8_t> raw_der = {0x03, 0x03, 0x03, 0xA8, 0x00};
1✔
409
   Botan::ASN1_BitString raw_bits;
1✔
410
   Botan::BER_Decoder(raw_der, Botan::BER_Decoder::Limits::DER()).decode_bitstring(raw_bits).verify_end();
1✔
411

412
   result.test_sz_eq("raw bytes", raw_bits.bytes().size(), 2);
1✔
413
   result.test_sz_eq("raw unused bits", raw_bits.unused_bits(), 3);
1✔
414
   result.test_sz_eq("raw bit length", raw_bits.bit_length(), 13);
1✔
415
   result.test_is_true("raw bit 0", raw_bits.bit_at(0));
1✔
416
   result.test_is_false("raw bit 1", raw_bits.bit_at(1));
1✔
417
   result.test_is_true("raw bit 2", raw_bits.bit_at(2));
1✔
418

419
   const auto raw_reencoded = Botan::DER_Encoder().encode_bitstring(raw_bits).get_contents_unlocked();
1✔
420
   result.test_bin_eq("raw BIT STRING re-encodes", raw_reencoded, raw_der);
1✔
421

422
   const std::vector<uint8_t> octet_aligned_der = {0x03, 0x02, 0x00, 0xAA};
1✔
423
   std::vector<uint8_t> octets;
1✔
424
   Botan::BER_Decoder(octet_aligned_der, Botan::BER_Decoder::Limits::DER())
2✔
425
      .decode_octet_aligned_bitstring(octets)
1✔
426
      .verify_end();
1✔
427
   const std::vector<uint8_t> expected_octets = {0xAA};
1✔
428
   result.test_bin_eq("octet-aligned BIT STRING decodes as bytes", octets, expected_octets);
1✔
429

430
   const std::vector<uint8_t> non_octet_aligned_der = {0x03, 0x02, 0x01, 0x80};
1✔
431
   result.test_throws<Botan::Decoding_Error>("octet-aligned BIT STRING rejects unused bits", [&] {
1✔
432
      std::vector<uint8_t> rejected;
1✔
433
      Botan::BER_Decoder(non_octet_aligned_der, Botan::BER_Decoder::Limits::DER())
3✔
434
         .decode_octet_aligned_bitstring(rejected)
1✔
435
         .verify_end();
×
436
   });
1✔
437

438
   const uint64_t named = (uint64_t(1) << 15) | (uint64_t(1) << 7);
1✔
439
   const auto named_der = Botan::DER_Encoder().encode_named_bitstring(named, 16).get_contents_unlocked();
1✔
440
   const std::vector<uint8_t> expected_named_der = {0x03, 0x03, 0x07, 0x80, 0x80};
1✔
441
   result.test_bin_eq("named BIT STRING uses DER minimum length", named_der, expected_named_der);
1✔
442

443
   uint64_t decoded_named = 0;
1✔
444
   Botan::BER_Decoder(named_der, Botan::BER_Decoder::Limits::DER())
2✔
445
      .decode_named_bitstring(decoded_named, 16)
1✔
446
      .verify_end();
1✔
447
   result.test_u64_eq("named BIT STRING round-trips", decoded_named, named);
1✔
448

449
   const auto width9_der = Botan::DER_Encoder().encode_named_bitstring(1, 9).get_contents_unlocked();
1✔
450
   const std::vector<uint8_t> expected_width9_der = {0x03, 0x03, 0x07, 0x00, 0x80};
1✔
451
   result.test_bin_eq("named BIT STRING handles non-byte width", width9_der, expected_width9_der);
1✔
452

453
   const std::vector<uint8_t> non_minimal_named_der = {0x03, 0x02, 0x00, 0x80};
1✔
454
   result.test_throws<Botan::BER_Decoding_Error>("DER named BIT STRING rejects trailing zero bits", [&] {
1✔
455
      uint64_t rejected = 0;
1✔
456
      Botan::BER_Decoder(non_minimal_named_der, Botan::BER_Decoder::Limits::DER())
2✔
457
         .decode_named_bitstring(rejected, 16)
1✔
458
         .verify_end();
×
459
   });
×
460

461
   uint64_t non_minimal_named = 0;
1✔
462
   Botan::BER_Decoder(non_minimal_named_der, Botan::BER_Decoder::Limits::BER())
2✔
463
      .decode_named_bitstring(non_minimal_named, 16)
1✔
464
      .verify_end();
1✔
465
   result.test_u64_eq("BER named BIT STRING accepts trailing zero bits", non_minimal_named, uint64_t(1) << 15);
1✔
466

467
   return result;
1✔
468
}
1✔
469

470
Test::Result test_ber_indefinite_length_trailing_data() {
1✔
471
   Test::Result result("BER indefinite length trailing data");
1✔
472

473
   // Case 1: verify_end after consuming indef SEQUENCE
474
   try {
1✔
475
      const std::vector<uint8_t> enc = {0x30, 0x80, 0x02, 0x01, 0x42, 0x00, 0x00};
1✔
476
      Botan::BER_Decoder dec(enc);
1✔
477
      Botan::BigInt x;
1✔
478
      dec.start_sequence().decode(x).end_cons();
1✔
479
      dec.verify_end();
1✔
480
      result.test_bn_eq("verify_end decoded x", x, Botan::BigInt(0x42));
1✔
481
   } catch(Botan::Exception& e) {
1✔
482
      result.test_failure("verify_end after indef SEQUENCE", e.what());
×
483
   }
×
484

485
   // Case 2: two back-to-back indef SEQUENCES at top level
486
   try {
1✔
487
      const std::vector<uint8_t> enc = {
1✔
488
         0x30, 0x80, 0x02, 0x01, 0x42, 0x00, 0x00, 0x30, 0x80, 0x02, 0x01, 0x43, 0x00, 0x00};
1✔
489
      Botan::BER_Decoder dec(enc);
1✔
490
      Botan::BigInt x;
1✔
491
      Botan::BigInt y;
1✔
492
      dec.start_sequence().decode(x).end_cons();
1✔
493
      dec.start_sequence().decode(y).end_cons();
1✔
494
      dec.verify_end();
1✔
495
      result.test_bn_eq("back-to-back x", x, Botan::BigInt(0x42));
1✔
496
      result.test_bn_eq("back-to-back y", y, Botan::BigInt(0x43));
1✔
497
   } catch(Botan::Exception& e) {
1✔
498
      result.test_failure("two back-to-back indef SEQUENCES", e.what());
×
499
   }
×
500

501
   // Case 3: nested indef SEQUENCES
502
   try {
1✔
503
      const std::vector<uint8_t> enc = {0x30, 0x80, 0x30, 0x80, 0x02, 0x01, 0x42, 0x00, 0x00, 0x00, 0x00};
1✔
504
      Botan::BER_Decoder dec(enc);
1✔
505
      Botan::BigInt x;
1✔
506
      auto outer = dec.start_sequence();
1✔
507
      outer.start_sequence().decode(x).end_cons();
1✔
508
      outer.end_cons();
1✔
509
      dec.verify_end();
1✔
510
      result.test_bn_eq("nested x", x, Botan::BigInt(0x42));
1✔
511
   } catch(Botan::Exception& e) {
1✔
512
      result.test_failure("nested indef SEQUENCE", e.what());
×
513
   }
×
514

515
   // Case 4: while(more_items()) loop over an indef SEQUENCE
516
   try {
1✔
517
      const std::vector<uint8_t> enc = {0x30, 0x80, 0x02, 0x01, 0x42, 0x02, 0x01, 0x43, 0x00, 0x00};
1✔
518
      Botan::BER_Decoder dec(enc);
1✔
519
      auto seq = dec.start_sequence();
1✔
520
      std::vector<Botan::BigInt> xs;
1✔
521
      while(seq.more_items()) {
3✔
522
         Botan::BigInt x;
2✔
523
         seq.decode(x);
2✔
524
         xs.push_back(x);
2✔
525
      }
2✔
526
      seq.end_cons();
1✔
527
      dec.verify_end();
1✔
528
      result.test_sz_eq("more_items count", xs.size(), 2);
1✔
529
      if(xs.size() == 2) {
1✔
530
         result.test_bn_eq("more_items xs[0]", xs[0], Botan::BigInt(0x42));
1✔
531
         result.test_bn_eq("more_items xs[1]", xs[1], Botan::BigInt(0x43));
1✔
532
      }
533
   } catch(Botan::Exception& e) {
1✔
534
      result.test_failure("more_items loop over indef SEQUENCE", e.what());
×
535
   }
×
536

537
   return result;
1✔
538
}
×
539

540
Test::Result test_ber_find_eoc() {
1✔
541
   Test::Result result("BER indefinite length EOC matching");
1✔
542

543
   const size_t num_siblings = 4096;
1✔
544

545
   std::vector<uint8_t> ber;
1✔
546
   ber.push_back(0x30);  // outer SEQUENCE | CONSTRUCTED
1✔
547
   ber.push_back(0x80);  // indefinite length
1✔
548
   for(size_t i = 0; i != num_siblings; ++i) {
4,097✔
549
      ber.push_back(0x30);  // inner SEQUENCE | CONSTRUCTED
4,096✔
550
      ber.push_back(0x80);  // indefinite length
4,096✔
551
      ber.push_back(0x00);  // EOC tag
4,096✔
552
      ber.push_back(0x00);  // EOC length
4,096✔
553
   }
554
   ber.push_back(0x00);  // outer EOC tag
1✔
555
   ber.push_back(0x00);  // outer EOC length
1✔
556

557
   try {
1✔
558
      Botan::BER_Decoder dec(ber);
1✔
559
      const Botan::BER_Object obj = dec.get_next_object();
1✔
560

561
      result.test_sz_eq("object body includes children", obj.length(), num_siblings * 4);
1✔
562
   } catch(Botan::Exception& e) {
1✔
563
      result.test_failure("decode failed", e.what());
×
564
   }
×
565

566
   return result;
1✔
567
}
1✔
568

569
Test::Result test_asn1_string_zero_length_roundtrip() {
1✔
570
   Test::Result result("ASN.1 String zero-length round-trip");
1✔
571

572
   auto roundtrip = [&](const char* what, const std::vector<uint8_t>& wire) {
4✔
573
      try {
3✔
574
         Botan::DataSource_Memory input(wire.data(), wire.size());
3✔
575
         Botan::BER_Decoder dec(input);
3✔
576
         Botan::ASN1_String str;
3✔
577
         str.decode_from(dec);
3✔
578

579
         Botan::DER_Encoder enc;
3✔
580
         str.encode_into(enc);
3✔
581
         const auto out = enc.get_contents();
3✔
582
         result.test_bin_eq(what, std::span{out}, std::span{wire});
3✔
583
      } catch(const std::exception& ex) {
9✔
584
         result.test_failure(Botan::fmt("{}: unexpected throw: {}", what, ex.what()));
×
585
      }
×
586
   };
3✔
587

588
   roundtrip("BmpString 1E 00", {0x1E, 0x00});
1✔
589
   roundtrip("UniversalString 1C 00", {0x1C, 0x00});
1✔
590
   roundtrip("TeletexString 14 00", {0x14, 0x00});
1✔
591

592
   return result;
1✔
593
}
×
594

595
Test::Result test_pss_params_rejects_trailing_data_in_mgf1_params() {
1✔
596
   Test::Result result("PSS-Params rejects trailing data in MGF1 parameters");
1✔
597

598
   const Botan::AlgorithmIdentifier sha256_alg_id("SHA-256", Botan::AlgorithmIdentifier::USE_NULL_PARAM);
1✔
599
   const auto sha256_der = sha256_alg_id.BER_encode();
1✔
600

601
   auto encode_pss_params = [&](const std::vector<uint8_t>& mgf_params) {
3✔
602
      const Botan::AlgorithmIdentifier mgf("MGF1", mgf_params);
2✔
603
      Botan::DER_Encoder enc;
2✔
604
      enc.start_sequence()
2✔
605
         .start_context_specific(0)
2✔
606
         .encode(sha256_alg_id)
2✔
607
         .end_cons()
2✔
608
         .start_context_specific(1)
2✔
609
         .encode(mgf)
2✔
610
         .end_cons()
2✔
611
         .start_context_specific(2)
2✔
612
         .encode(static_cast<size_t>(32))
2✔
613
         .end_cons()
2✔
614
         .end_cons();
2✔
615
      return enc.get_contents();
2✔
616
   };
4✔
617

618
   try {
1✔
619
      const auto clean_der = encode_pss_params(sha256_der);
1✔
620
      const Botan::PSS_Params clean(clean_der);
1✔
621
      result.test_success("control: clean PSS-Params decodes");
1✔
622
   } catch(const std::exception& e) {
2✔
623
      result.test_failure(Botan::fmt("clean PSS-Params unexpected throw: {}", e.what()));
×
624
   }
×
625

626
   std::vector<uint8_t> mgf_params_with_junk = sha256_der;
1✔
627
   const std::vector<uint8_t> trailing_junk{0x02, 0x01, 0x00};
1✔
628
   mgf_params_with_junk.insert(mgf_params_with_junk.end(), trailing_junk.begin(), trailing_junk.end());
1✔
629
   const auto bad_der = encode_pss_params(mgf_params_with_junk);
1✔
630

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

634
   return result;
1✔
635
}
2✔
636

637
class ASN1_Tests final : public Test {
1✔
638
   public:
639
      std::vector<Test::Result> run() override {
1✔
640
         std::vector<Test::Result> results;
1✔
641

642
         results.push_back(test_ber_stack_recursion());
2✔
643
         results.push_back(test_ber_eoc_decoding_limits());
2✔
644
         results.push_back(test_ber_indefinite_length_trailing_data());
2✔
645
         results.push_back(test_ber_find_eoc());
2✔
646
         results.push_back(test_asn1_utf8_ascii_parsing());
2✔
647
         results.push_back(test_asn1_utf8_parsing());
2✔
648
         results.push_back(test_asn1_ucs2_parsing());
2✔
649
         results.push_back(test_asn1_ucs4_parsing());
2✔
650
         results.push_back(test_asn1_ucs_invalid_codepoint_rejection());
2✔
651
         results.push_back(test_asn1_ascii_encoding());
2✔
652
         results.push_back(test_asn1_utf8_encoding());
2✔
653
         results.push_back(test_asn1_tag_underlying_type());
2✔
654
         results.push_back(test_asn1_negative_int_encoding());
2✔
655
         results.push_back(test_der_constructed_tag_17_not_sorted());
2✔
656
         results.push_back(test_der_implicit_tagging_helpers());
2✔
657
         results.push_back(test_asn1_bitstring_helpers());
2✔
658
         results.push_back(test_asn1_string_zero_length_roundtrip());
2✔
659
         results.push_back(test_pss_params_rejects_trailing_data_in_mgf1_params());
2✔
660

661
         return results;
1✔
662
      }
×
663
};
664

665
BOTAN_REGISTER_TEST("asn1", "asn1_encoding", ASN1_Tests);
666

667
class ASN1_Time_Parsing_Tests final : public Text_Based_Test {
×
668
   public:
669
      ASN1_Time_Parsing_Tests() : Text_Based_Test("asn1_time.vec", "Tspec") {}
2✔
670

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

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

676
         if(tag_str != "UTC" && tag_str != "UTC.invalid" && tag_str != "Generalized" &&
26✔
677
            tag_str != "Generalized.invalid") {
13✔
678
            throw Test_Error("Invalid tag value in ASN1 date parsing test");
×
679
         }
680

681
         const bool out_of_range = [&]() -> bool {
78✔
682
            if(tspec.size() == 15) {
26✔
683
               const size_t year = Botan::to_u32bit(std::string_view(tspec).substr(0, 4));
24✔
684
               if(year >= 2262) {
24✔
685
                  return true;
686
               }
687
               if(year >= 2038 && sizeof(time_t) == 4) {
688
                  return true;
689
               }
690
            }
691

692
            return false;
693
         }();
26✔
694

695
         const Botan::ASN1_Type tag = (tag_str == "UTC" || tag_str == "UTC.invalid")
25✔
696
                                         ? Botan::ASN1_Type::UtcTime
26✔
697
                                         : Botan::ASN1_Type::GeneralizedTime;
26✔
698

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

701
         if(valid) {
26✔
702
            const Botan::ASN1_Time time(tspec, tag);
12✔
703
            result.test_success("Accepted valid time");
12✔
704

705
            try {
12✔
706
               const auto std_timepoint = time.to_std_timepoint();
12✔
707
               result.test_success("Was able to convert time to std timepoint");
10✔
708

709
               const auto from_std_timepoint = Botan::ASN1_Time::from_time_point(std_timepoint);
10✔
710
               result.test_is_true("ASN1_Time from std timepoint matches input", from_std_timepoint == time);
10✔
711
            } catch(std::exception& e) {
12✔
712
               if(out_of_range) {
2✔
713
                  result.test_str_contains("Exception message", e.what(), "time is outside the representable range");
2✔
714
               } else {
715
                  result.test_failure("Was not able to convert time to std timepoint", e.what());
×
716
               }
717
            }
2✔
718
         } else {
12✔
719
            result.test_throws("Invalid time rejected", [=]() { const Botan::ASN1_Time time(tspec, tag); });
70✔
720
         }
721

722
         return result;
26✔
723
      }
26✔
724
};
725

726
BOTAN_REGISTER_TEST("asn1", "asn1_time", ASN1_Time_Parsing_Tests);
727

728
class ASN1_String_Validation_Tests final : public Text_Based_Test {
×
729
   public:
730
      ASN1_String_Validation_Tests() :
1✔
731
            Text_Based_Test("asn1_string_validation.vec",
732
                            "Input,ValidNumeric,ValidPrintable,ValidIa5,ValidVisible,ValidUtf8") {}
2✔
733

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

737
         const auto input = vars.get_req_str("Input");
7✔
738
         const bool valid_numeric = vars.get_req_bool("ValidNumeric");
7✔
739
         const bool valid_printable = vars.get_req_bool("ValidPrintable");
7✔
740
         const bool valid_ia5 = vars.get_req_bool("ValidIa5");
7✔
741
         const bool valid_visible = vars.get_req_bool("ValidVisible");
7✔
742
         const bool valid_utf8 = vars.get_req_bool("ValidUtf8");
7✔
743

744
         test_string_type(result, input, "NumericString", Botan::ASN1_Type::NumericString, valid_numeric);
7✔
745
         test_string_type(result, input, "PrintableString", Botan::ASN1_Type::PrintableString, valid_printable);
7✔
746
         test_string_type(result, input, "Ia5String", Botan::ASN1_Type::Ia5String, valid_ia5);
7✔
747
         test_string_type(result, input, "VisibleString", Botan::ASN1_Type::VisibleString, valid_visible);
7✔
748
         test_string_type(result, input, "Utf8String", Botan::ASN1_Type::Utf8String, valid_utf8);
7✔
749

750
         if(valid_utf8) {
7✔
751
            try {
7✔
752
               const Botan::ASN1_String str(input);
7✔
753
               const auto expected_tag =
14✔
754
                  valid_printable ? Botan::ASN1_Type::PrintableString : Botan::ASN1_Type::Utf8String;
7✔
755
               result.test_u32_eq("String tagging categorization",
7✔
756
                                  static_cast<uint32_t>(str.tagging()),
7✔
757
                                  static_cast<uint32_t>(expected_tag));
758
            } catch(const std::exception& ex) {
7✔
759
               result.test_failure(Botan::fmt("default constructor unexpectedly rejected '{}': {}", input, ex.what()));
×
760
            }
×
761
         }
762

763
         return result;
7✔
764
      }
7✔
765

766
   private:
767
      void test_string_type(Test::Result& result,
35✔
768
                            std::string_view input,
769
                            std::string_view type,
770
                            Botan::ASN1_Type tag,
771
                            bool expected_valid) {
772
         if(expected_valid) {
35✔
773
            try {
24✔
774
               const Botan::ASN1_String str(input, tag);
24✔
775
               result.test_str_eq(Botan::fmt("{} constructor value", type), str.value(), input);
24✔
776

777
               const auto enc = raw_encode_string(input, tag);
24✔
778
               Botan::BER_Decoder dec(enc);
24✔
779
               Botan::ASN1_String decoded;
24✔
780
               decoded.decode_from(dec);
24✔
781
               result.test_str_eq(Botan::fmt("{} decode value", type), decoded.value(), input);
48✔
782
            } catch(const std::exception& e) {
48✔
783
               result.test_failure(Botan::fmt("{} unexpectedly rejected '{}': {}", type, input, e.what()));
×
784
            }
×
785
         } else {
786
            result.test_throws(Botan::fmt("{} constructor rejects", type),
22✔
787
                               [&]() { const Botan::ASN1_String str(input, tag); });
22✔
788

789
            result.test_throws(Botan::fmt("{} decode rejects", type), [&]() {
22✔
790
               const auto enc = raw_encode_string(input, tag);
11✔
791
               Botan::BER_Decoder dec(enc);
11✔
792
               Botan::ASN1_String decoded;
11✔
793
               decoded.decode_from(dec);
11✔
794
            });
22✔
795
         }
796
      }
35✔
797

798
      static std::vector<uint8_t> raw_encode_string(std::string_view input, Botan::ASN1_Type tag) {
35✔
799
         std::vector<uint8_t> encoding;
35✔
800
         Botan::DER_Encoder der(encoding);
35✔
801
         der.add_object(tag, Botan::ASN1_Class::Universal, input);
35✔
802
         return encoding;
35✔
803
      }
35✔
804
};
805

806
BOTAN_REGISTER_TEST("asn1", "asn1_string_validation", ASN1_String_Validation_Tests);
807

808
class ASN1_Printer_Tests final : public Test {
1✔
809
   public:
810
      std::vector<Test::Result> run() override {
1✔
811
         Test::Result result("ASN1_Pretty_Printer");
1✔
812

813
         const Botan::ASN1_Pretty_Printer printer;
1✔
814

815
         const size_t num_tests = 8;
1✔
816

817
         for(size_t i = 1; i <= num_tests; ++i) {
9✔
818
            const std::string i_str = std::to_string(i);
8✔
819
            const std::vector<uint8_t> input_data = Test::read_binary_data_file("asn1_print/input" + i_str + ".der");
24✔
820
            const std::string expected_output = Test::read_data_file("asn1_print/output" + i_str + ".txt");
24✔
821

822
            try {
8✔
823
               const std::string output = printer.print(input_data);
8✔
824
               result.test_str_eq("Test " + i_str, output, expected_output);
16✔
825
            } catch(Botan::Exception& e) {
8✔
826
               result.test_failure(Botan::fmt("Printing test {} failed with an exception: '{}'", i, e.what()));
×
827
            }
×
828
         }
8✔
829

830
         return {result};
2✔
831
      }
10✔
832
};
833

834
BOTAN_REGISTER_TEST("asn1", "asn1_printer", ASN1_Printer_Tests);
835

836
class ASN1_Decoding_Tests final : public Text_Based_Test {
×
837
   public:
838
      ASN1_Decoding_Tests() : Text_Based_Test("asn1_decoding.vec", "Input,ResultBER", "ResultDER") {}
2✔
839

840
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
55✔
841
         const auto input = vars.get_req_bin("Input");
55✔
842
         const std::string expected_ber = vars.get_req_str("ResultBER");
55✔
843
         const std::string expected_der = vars.get_opt_str("ResultDER", expected_ber);
55✔
844

845
         Test::Result result("ASN1 decoding");
55✔
846

847
         decoding_test(result, input, expected_ber, false);
55✔
848
         decoding_test(result, input, expected_der, true);
55✔
849

850
         return result;
110✔
851
      }
55✔
852

853
   private:
854
      static void decoding_test(Test::Result& result,
110✔
855
                                std::span<const uint8_t> input,
856
                                std::string_view expected,
857
                                bool require_der) {
858
         const Botan::ASN1_Pretty_Printer printer(4096, 2048, true, 0, 60, 64, require_der);
110✔
859
         const std::string mode = require_der ? "DER" : "BER";
165✔
860
         std::ostringstream sink;
110✔
861

862
         try {
110✔
863
            printer.print_to_stream(sink, input.data(), input.size());
110✔
864

865
            if(expected == "OK") {
52✔
866
               result.test_success();
52✔
867
            } else {
868
               result.test_failure(Botan::fmt("Accepted invalid {} input, expected error {}", mode, expected));
×
869
            }
870
         } catch(const std::exception& e) {
58✔
871
            if(expected == "OK") {
58✔
872
               result.test_failure(Botan::fmt("Rejected valid {} input with {}", mode, e.what()));
×
873
            } else {
874
               // BER_Decoding_Error prepends "BER: " to the message
875
               std::string msg = e.what();
58✔
876
               if(msg.starts_with("BER: ")) {
58✔
877
                  msg = msg.substr(5);
38✔
878
               }
879
               result.test_str_eq("error message", msg, expected);
58✔
880
            }
58✔
881
         }
58✔
882
      }
148✔
883
};
884

885
BOTAN_REGISTER_TEST("asn1", "asn1_decoding", ASN1_Decoding_Tests);
886

887
#endif
888

889
}  // namespace
890

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