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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

92.17
/src/tests/test_asn1.cpp
1
/*
2
* (C) 2017 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#if defined(BOTAN_HAS_ASN1)
10
   #include <botan/asn1_print.h>
11
   #include <botan/ber_dec.h>
12
   #include <botan/der_enc.h>
13
#endif
14

15
namespace Botan_Tests {
16

17
#if defined(BOTAN_HAS_ASN1)
18

19
namespace {
20

21
Test::Result test_ber_stack_recursion() {
1✔
22
   Test::Result result("BER stack recursion");
1✔
23

24
   // OSS-Fuzz #813 GitHub #989
25

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

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

37
   result.test_success("No crash");
1✔
38

39
   return result;
1✔
40
}
×
41

42
Test::Result test_ber_eoc_decoding_limits() {
1✔
43
   Test::Result result("BER nested indefinite length");
1✔
44

45
   // OSS-Fuzz #4353
46

47
   Botan::ASN1_Pretty_Printer printer;
1✔
48

49
   size_t max_eoc_allowed = 0;
1✔
50

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

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

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

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

74
   return result;
1✔
75
}
1✔
76

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

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

89
      Botan::ASN1_String str;
1✔
90
      str.decode_from(dec);
1✔
91

92
      result.test_eq("value()", str.value(), moscow_plain);
1✔
93
   } catch(const Botan::Decoding_Error& ex) { result.test_failure(ex.what()); }
2✔
94

95
   return result;
1✔
96
}
×
97

98
Test::Result test_asn1_utf8_parsing() {
1✔
99
   Test::Result result("ASN.1 UTF-8 parsing");
1✔
100

101
   try {
1✔
102
      // \x0C - ASN1 tag for 'UTF8 string'
103
      // \x0C - 12 characters of payload
104
      // ...  - UTF-8 encoded russian word for Moscow in cyrillic script
105
      const std::string moscow = "\x0C\x0C\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
106
      const std::string moscow_plain = "\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
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_eq("value()", str.value(), moscow_plain);
1✔
114
   } catch(const Botan::Decoding_Error& ex) { result.test_failure(ex.what()); }
2✔
115

116
   return result;
1✔
117
}
×
118

119
Test::Result test_asn1_ucs2_parsing() {
1✔
120
   Test::Result result("ASN.1 BMP string (UCS-2) parsing");
1✔
121

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

129
      Botan::DataSource_Memory input(moscow);
1✔
130
      Botan::BER_Decoder dec(input);
1✔
131

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

135
      result.test_eq("value()", str.value(), moscow_plain);
1✔
136
   } catch(const Botan::Decoding_Error& ex) { result.test_failure(ex.what()); }
2✔
137

138
   return result;
1✔
139
}
×
140

141
Test::Result test_asn1_ucs4_parsing() {
1✔
142
   Test::Result result("ASN.1 universal string (UCS-4) parsing");
1✔
143

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

154
      Botan::ASN1_String str;
1✔
155
      str.decode_from(dec);
1✔
156

157
      result.test_eq("value()", str.value(), moscow_plain);
1✔
158
   } catch(const Botan::Decoding_Error& ex) { result.test_failure(ex.what()); }
2✔
159

160
   return result;
1✔
161
}
×
162

163
Test::Result test_asn1_ascii_encoding() {
1✔
164
   Test::Result result("ASN.1 ASCII encoding");
1✔
165

166
   try {
1✔
167
      // UTF-8 encoded (ASCII chars only) word 'Moscow'
168
      const std::string moscow = "\x4D\x6F\x73\x63\x6F\x77";
1✔
169
      Botan::ASN1_String str(moscow);
1✔
170

171
      Botan::DER_Encoder enc;
1✔
172

173
      str.encode_into(enc);
1✔
174
      auto encodingResult = enc.get_contents();
1✔
175

176
      // \x13 - ASN1 tag for 'printable string'
177
      // \x06 - 6 characters of payload
178
      const auto moscowEncoded = Botan::hex_decode("13064D6F73636F77");
1✔
179
      result.test_eq("encoding result", encodingResult, moscowEncoded);
1✔
180

181
      result.test_success("No crash");
2✔
182
   } catch(const std::exception& ex) { result.test_failure(ex.what()); }
2✔
183

184
   return result;
1✔
185
}
×
186

187
Test::Result test_asn1_utf8_encoding() {
1✔
188
   Test::Result result("ASN.1 UTF-8 encoding");
1✔
189

190
   try {
1✔
191
      // UTF-8 encoded russian word for Moscow in cyrillic script
192
      const std::string moscow = "\xD0\x9C\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB0";
1✔
193
      Botan::ASN1_String str(moscow);
1✔
194

195
      Botan::DER_Encoder enc;
1✔
196

197
      str.encode_into(enc);
1✔
198
      auto encodingResult = enc.get_contents();
1✔
199

200
      // \x0C - ASN1 tag for 'UTF8 string'
201
      // \x0C - 12 characters of payload
202
      const auto moscowEncoded = Botan::hex_decode("0C0CD09CD0BED181D0BAD0B2D0B0");
1✔
203
      result.test_eq("encoding result", encodingResult, moscowEncoded);
1✔
204

205
      result.test_success("No crash");
2✔
206
   } catch(const std::exception& ex) { result.test_failure(ex.what()); }
2✔
207

208
   return result;
1✔
209
}
×
210

211
Test::Result test_asn1_tag_underlying_type() {
1✔
212
   Test::Result result("ASN.1 class and type underlying type");
1✔
213

214
   if constexpr(std::is_same_v<std::underlying_type_t<Botan::ASN1_Class>, std::underlying_type_t<Botan::ASN1_Type>>) {
1✔
215
      if constexpr(!std::is_same_v<std::underlying_type_t<Botan::ASN1_Class>,
1✔
216
                                   std::invoke_result_t<decltype(&Botan::BER_Object::tagging), Botan::BER_Object>>) {
217
         result.test_failure(
218
            "Return type of BER_Object::tagging() is different than the underlying type of ASN1_Class");
219
      } else {
220
         result.test_success("Same types");
1✔
221
      }
222
   } else {
223
      result.test_failure("ASN1_Class and ASN1_Type have different underlying types");
224
   }
225

226
   return result;
1✔
227
}
×
228

229
}
230

231
class ASN1_Tests final : public Test {
×
232
   public:
233
      std::vector<Test::Result> run() override {
1✔
234
         std::vector<Test::Result> results;
1✔
235

236
         results.push_back(test_ber_stack_recursion());
2✔
237
         results.push_back(test_ber_eoc_decoding_limits());
2✔
238
         results.push_back(test_asn1_utf8_ascii_parsing());
2✔
239
         results.push_back(test_asn1_utf8_parsing());
2✔
240
         results.push_back(test_asn1_ucs2_parsing());
2✔
241
         results.push_back(test_asn1_ucs4_parsing());
2✔
242
         results.push_back(test_asn1_ascii_encoding());
2✔
243
         results.push_back(test_asn1_utf8_encoding());
2✔
244
         results.push_back(test_asn1_tag_underlying_type());
2✔
245

246
         return results;
1✔
247
      }
×
248
};
249

250
BOTAN_REGISTER_TEST("asn1", "asn1_encoding", ASN1_Tests);
251

252
class ASN1_Time_Parsing_Tests final : public Text_Based_Test {
×
253
   public:
254
      ASN1_Time_Parsing_Tests() : Text_Based_Test("asn1_time.vec", "Tspec") {}
2✔
255

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

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

261
         if(tag_str != "UTC" && tag_str != "UTC.invalid" && tag_str != "Generalized" &&
25✔
262
            tag_str != "Generalized.invalid") {
11✔
263
            throw Test_Error("Invalid tag value in ASN1 date parsing test");
×
264
         }
265

266
         const Botan::ASN1_Type tag = (tag_str == "UTC" || tag_str == "UTC.invalid")
24✔
267
                                         ? Botan::ASN1_Type::UtcTime
25✔
268
                                         : Botan::ASN1_Type::GeneralizedTime;
25✔
269

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

272
         if(valid) {
25✔
273
            Botan::ASN1_Time time(tspec, tag);
13✔
274
            result.test_success("Accepted valid time");
26✔
275
         } else {
13✔
276
            result.test_throws("Invalid time rejected", [=]() { Botan::ASN1_Time time(tspec, tag); });
108✔
277
         }
278

279
         return result;
25✔
280
      }
25✔
281
};
282

283
BOTAN_REGISTER_TEST("asn1", "asn1_time", ASN1_Time_Parsing_Tests);
284

285
class ASN1_Printer_Tests final : public Test {
×
286
   public:
287
      std::vector<Test::Result> run() override {
1✔
288
         Test::Result result("ASN1_Pretty_Printer");
1✔
289

290
         Botan::ASN1_Pretty_Printer printer;
1✔
291

292
         const size_t num_tests = 6;
1✔
293

294
         for(size_t i = 1; i <= num_tests; ++i) {
7✔
295
            std::string i_str = std::to_string(i);
6✔
296
            const std::vector<uint8_t> input1 = Test::read_binary_data_file("asn1_print/input" + i_str + ".der");
12✔
297
            const std::string expected1 = Test::read_data_file("asn1_print/output" + i_str + ".txt");
12✔
298

299
            result.test_eq("Test " + i_str, printer.print(input1), expected1);
18✔
300
         }
12✔
301

302
         return {result};
2✔
303
      }
1✔
304
};
305

306
BOTAN_REGISTER_TEST("asn1", "asn1_printer", ASN1_Printer_Tests);
307

308
#endif
309

310
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc