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

randombit / botan / 18936795343

30 Oct 2025 08:54AM UTC coverage: 90.67%. Remained the same
18936795343

push

github

web-flow
Merge pull request #5109 from KaganCanSit/reduce-cppcheck-warnings-in-test-files

Reduce cppcheck warnings in test files

100422 of 110756 relevant lines covered (90.67%)

12359432.05 hits per line

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

99.01
/src/tests/test_x509_rpki.cpp
1
/*
2
* (C) 2025 Jack Lloyd
3
* (C) 2025 Anton Einax, Dominik Schricker
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "tests.h"
9

10
#if defined(BOTAN_HAS_X509_CERTIFICATES)
11
   #include <botan/certstor.h>
12
   #include <botan/pk_algs.h>
13
   #include <botan/pubkey.h>
14
   #include <botan/x509_ca.h>
15
   #include <botan/x509_ext.h>
16
   #include <botan/x509path.h>
17
   #include <botan/x509self.h>
18
   #include <botan/internal/calendar.h>
19
#endif
20

21
#if defined(BOTAN_HAS_ECC_GROUP)
22
   #include <botan/ec_group.h>
23
#endif
24

25
namespace Botan_Tests {
26

27
namespace {
28

29
#if defined(BOTAN_HAS_X509_CERTIFICATES) && \
30
   (defined(BOTAN_HAS_ECDSA) || defined(BOTAN_HAS_RSA) || defined(BOTAN_HAS_ED25519))
31

32
struct CA_Creation_Result {
33
      Botan::X509_Certificate ca_cert;
34
      Botan::X509_CA ca;
35
      std::unique_ptr<Botan::Private_Key> sub_key;
36
      std::string sig_algo;
37
      std::string hash_fn;
38
};
39

40
Botan::X509_Time from_date(const int y, const int m, const int d) {
2,728✔
41
   const size_t this_year = Botan::calendar_point(std::chrono::system_clock::now()).year();
2,728✔
42

43
   Botan::calendar_point t(static_cast<uint32_t>(this_year + y), m, d, 0, 0, 0);
2,728✔
44
   return Botan::X509_Time(t.to_std_timepoint());
2,728✔
45
}
46

47
std::unique_ptr<Botan::Private_Key> generate_key(const std::string& algo, Botan::RandomNumberGenerator& rng) {
217✔
48
   std::string params;
217✔
49
   if(algo == "ECDSA") {
217✔
50
      params = "secp256r1";
217✔
51

52
   #if defined(BOTAN_HAS_ECC_GROUP)
53
      if(Botan::EC_Group::supports_named_group("secp192r1")) {
217✔
54
         params = "secp192r1";
217✔
55
      }
56
   #endif
57
   } else if(algo == "Ed25519") {
×
58
      params = "";
×
59
   } else if(algo == "RSA") {
×
60
      params = "1536";
×
61
   }
62

63
   return Botan::create_private_key(algo, rng, params);
217✔
64
}
217✔
65

66
Botan::X509_Cert_Options ca_opts(const std::string& sig_padding = "") {
149✔
67
   Botan::X509_Cert_Options opts("Test CA/US/Botan Project/Testing");
149✔
68

69
   opts.uri = "https://botan.randombit.net";
149✔
70
   opts.dns = "botan.randombit.net";
149✔
71
   opts.email = "testing@randombit.net";
149✔
72
   opts.set_padding_scheme(sig_padding);
149✔
73

74
   opts.CA_key(1);
149✔
75

76
   return opts;
149✔
77
}
×
78

79
Botan::X509_Cert_Options req_opts(const std::string& algo, const std::string& sig_padding = "") {
1,276✔
80
   Botan::X509_Cert_Options opts("Test User 1/US/Botan Project/Testing");
1,276✔
81

82
   opts.uri = "https://botan.randombit.net";
1,276✔
83
   opts.dns = "botan.randombit.net";
1,276✔
84
   opts.email = "testing@randombit.net";
1,276✔
85
   opts.set_padding_scheme(sig_padding);
1,276✔
86

87
   opts.not_before("160101200000Z");
1,276✔
88
   opts.not_after("300101200000Z");
1,276✔
89

90
   opts.challenge = "zoom";
1,276✔
91

92
   if(algo == "RSA") {
1,276✔
93
      opts.constraints = Botan::Key_Constraints::KeyEncipherment;
×
94
   } else if(algo == "DSA" || algo == "ECDSA" || algo == "ECGDSA" || algo == "ECKCDSA") {
1,276✔
95
      opts.constraints = Botan::Key_Constraints::DigitalSignature;
1,276✔
96
   }
97

98
   return opts;
1,276✔
99
}
×
100

101
std::tuple<std::string, std::string, std::string> get_sig_algo_padding() {
149✔
102
   #if defined(BOTAN_HAS_ECDSA)
103
   const std::string sig_algo{"ECDSA"};
149✔
104
   const std::string padding_method;
149✔
105
   const std::string hash_fn{"SHA-256"};
149✔
106
   #elif defined(BOTAN_HAS_ED25519)
107
   const std::string sig_algo{"Ed25519"};
108
   const std::string padding_method;
109
   const std::string hash_fn{"SHA-512"};
110
   #elif defined(BOTAN_HAS_RSA)
111
   const std::string sig_algo{"RSA"};
112
   const std::string padding_method{"PKCS1v15(SHA-256)"};
113
   const std::string hash_fn{"SHA-256"};
114
   #endif
115

116
   return std::make_tuple(sig_algo, padding_method, hash_fn);
298✔
117
}
149✔
118

119
Botan::X509_Certificate make_self_signed(std::unique_ptr<Botan::RandomNumberGenerator>& rng,
5✔
120
                                         const Botan::X509_Cert_Options& opts = std::move(ca_opts())) {
121
   auto [sig_algo, padding_method, hash_fn] = get_sig_algo_padding();
5✔
122
   auto key = generate_key(sig_algo, *rng);
5✔
123
   const auto cert = Botan::X509::create_self_signed_cert(opts, *key, hash_fn, *rng);
5✔
124

125
   return cert;
5✔
126
}
5✔
127

128
CA_Creation_Result make_ca(std::unique_ptr<Botan::RandomNumberGenerator>& rng,
68✔
129
                           const Botan::X509_Cert_Options& opts = std::move(ca_opts())) {
130
   auto [sig_algo, padding_method, hash_fn] = get_sig_algo_padding();
68✔
131
   auto ca_key = generate_key(sig_algo, *rng);
68✔
132
   const auto ca_cert = Botan::X509::create_self_signed_cert(opts, *ca_key, hash_fn, *rng);
68✔
133
   Botan::X509_CA ca(ca_cert, *ca_key, hash_fn, padding_method, *rng);
68✔
134
   auto sub_key = generate_key(sig_algo, *rng);
68✔
135

136
   return CA_Creation_Result{ca_cert, std::move(ca), std::move(sub_key), sig_algo, hash_fn};
204✔
137
}
136✔
138

139
std::pair<Botan::X509_Certificate, Botan::X509_CA> make_and_sign_ca(
76✔
140
   std::unique_ptr<Botan::Certificate_Extension> ext,
141
   Botan::X509_CA& parent_ca,
142
   std::unique_ptr<Botan::RandomNumberGenerator>& rng) {
143
   auto [sig_algo, padding_method, hash_fn] = get_sig_algo_padding();
76✔
144

145
   Botan::X509_Cert_Options opts = ca_opts();
76✔
146
   opts.extensions.add(std::move(ext));
76✔
147

148
   std::unique_ptr<Botan::Private_Key> key = generate_key(sig_algo, *rng);
76✔
149

150
   Botan::PKCS10_Request req = Botan::X509::create_cert_req(opts, *key, hash_fn, *rng);
76✔
151
   Botan::X509_Certificate cert = parent_ca.sign_request(req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
76✔
152
   Botan::X509_CA ca(cert, *key, hash_fn, padding_method, *rng);
76✔
153

154
   return std::make_pair(std::move(cert), std::move(ca));
76✔
155
}
152✔
156

157
constexpr auto IPv4 = Botan::Cert_Extension::IPAddressBlocks::Version::IPv4;
158
constexpr auto IPv6 = Botan::Cert_Extension::IPAddressBlocks::Version::IPv6;
159

160
   #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
161

162
Test::Result test_x509_ip_addr_blocks_extension_decode() {
1✔
163
   Test::Result result("X509 IP Address Block decode");
2✔
164
   result.start_timer();
1✔
165
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
166

167
   {
1✔
168
      const std::string filename("IPAddrBlocksAll.pem");
1✔
169
      Botan::X509_Certificate cert(Test::data_file("x509/x509test/" + filename));
2✔
170
      const auto* ip_addr_blocks = cert.v3_extensions().get_extension_object_as<IPAddressBlocks>();
1✔
171

172
      const auto& addr_blocks = ip_addr_blocks->addr_blocks();
1✔
173
      result.confirm("cert has IPAddrBlocks extension", ip_addr_blocks != nullptr, true);
2✔
174
      result.test_eq("cert has two IpAddrBlocks", addr_blocks.size(), 2);
1✔
175

176
      const auto& ipv4block = std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(addr_blocks[0].addr_choice());
1✔
177
      const auto& ipv6block = std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(addr_blocks[1].addr_choice());
1✔
178

179
      const auto& v4_blocks = ipv4block.ranges().value();
1✔
180

181
      // cert contains (in this order)
182
      // 192.168.0.0 - 192.168.127.255 (192.168.0.0/17)
183
      // 193.168.0.0 - 193.169.255.255 (193.168.0.0/15)
184
      // 194.168.0.0 - 195.175.1.2
185
      // 196.168.0.1 - 196.168.0.1 (196.168.0.1/32)
186

187
      result.test_eq("ipv4 block 0 min", v4_blocks[0].min().value(), {192, 168, 0, 0});
2✔
188
      result.test_eq("ipv4 block 0 max", v4_blocks[0].max().value(), {192, 168, 127, 255});
2✔
189

190
      result.test_eq("ipv4 block 1 min", v4_blocks[1].min().value(), {193, 168, 0, 0});
2✔
191
      result.test_eq("ipv4 block 1 max", v4_blocks[1].max().value(), {193, 169, 255, 255});
2✔
192
      result.test_eq("ipv4 block 2 min", v4_blocks[2].min().value(), {194, 168, 0, 0});
2✔
193
      result.test_eq("ipv4 block 2 max", v4_blocks[2].max().value(), {195, 175, 1, 2});
2✔
194

195
      result.test_eq("ipv4 block 3 min", v4_blocks[3].min().value(), {196, 168, 0, 1});
2✔
196
      result.test_eq("ipv4 block 3 max", v4_blocks[3].max().value(), {196, 168, 0, 1});
2✔
197

198
      const auto& v6_blocks = ipv6block.ranges().value();
1✔
199

200
      // cert contains (in this order)
201
      // fa80::/65
202
      // fe20::/37
203
      // 2003:0:6829:3435:420:10c5:0:c4/128
204
      // ab01:0:0:0:0:0:0:1-cd02:0:0:0:0:0:0:2
205

206
      result.test_eq("ipv6 block 0 min",
2✔
207
                     v6_blocks[0].min().value(),
1✔
208
                     {0x20, 0x03, 0x00, 0x00, 0x68, 0x29, 0x34, 0x35, 0x04, 0x20, 0x10, 0xc5, 0x00, 0x00, 0x00, 0xc4});
209
      result.test_eq("ipv6 block 0 max",
2✔
210
                     v6_blocks[0].max().value(),
1✔
211
                     {0x20, 0x03, 0x00, 0x00, 0x68, 0x29, 0x34, 0x35, 0x04, 0x20, 0x10, 0xc5, 0x00, 0x00, 0x00, 0xc4});
212
      result.test_eq("ipv6 block 1 min",
2✔
213
                     v6_blocks[1].min().value(),
1✔
214
                     {0xab, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01});
215
      result.test_eq("ipv6 block 1 max",
2✔
216
                     v6_blocks[1].max().value(),
1✔
217
                     {0xcd, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02});
218
      result.test_eq("ipv6 block 2 min",
2✔
219
                     v6_blocks[2].min().value(),
1✔
220
                     {0xfa, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
221
      result.test_eq("ipv6 block 2 max",
2✔
222
                     v6_blocks[2].max().value(),
1✔
223
                     {0xfa, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff});
224
      result.test_eq("ipv6 block 3 min",
2✔
225
                     v6_blocks[3].min().value(),
1✔
226
                     {0xfe, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
227
      result.test_eq("ipv6 block 3 max",
2✔
228
                     v6_blocks[3].max().value(),
1✔
229
                     {0xfe, 0x20, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff});
230
   }
1✔
231
   {
1✔
232
      const std::string filename("IPAddrBlocksUnsorted.pem");
1✔
233
      Botan::X509_Certificate cert(Test::data_file("x509/x509test/" + filename));
2✔
234
      const auto* ip_addr_blocks = cert.v3_extensions().get_extension_object_as<IPAddressBlocks>();
1✔
235

236
      // cert contains (in this order)
237
      // IPv6 (1) inherit
238
      // IPv6 0xff....0xff
239
      // IPv4 (2) inherit
240
      // IPv4 (1) 192.168.0.0 - 192.168.2.1
241
      // IPv4 (1) 192.168.2.2 - 200.0.0.0
242
      // IPv4 inherit
243

244
      // IPv4 ranges should be merged, IPv4 should come before IPv6, all should be sorted by safi
245

246
      const auto& addr_blocks = ip_addr_blocks->addr_blocks();
1✔
247
      result.test_eq("cert has two IpAddrBlocks", addr_blocks.size(), 5);
1✔
248

249
      result.test_eq("block 0 has no safi", addr_blocks[0].safi(), std::optional<uint8_t>{std::nullopt});
1✔
250
      result.confirm(
2✔
251
         "block 0 is inherited",
252
         !std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(addr_blocks[0].addr_choice()).ranges().has_value());
1✔
253

254
      result.test_eq("block 1 has correct safi", addr_blocks[1].safi(), std::optional<uint8_t>{1});
1✔
255
      const auto& block_1 =
1✔
256
         std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(addr_blocks[1].addr_choice()).ranges().value();
1✔
257

258
      result.confirm("block 1 has correct size", block_1.size() == 1);
2✔
259
      result.test_eq("block 1 min is correct", block_1[0].min().value(), {192, 168, 0, 0});
2✔
260
      result.test_eq("block 1 max is correct", block_1[0].max().value(), {200, 0, 0, 0});
2✔
261

262
      result.test_eq("block 2 has correct safi", addr_blocks[2].safi(), std::optional<uint8_t>{2});
1✔
263
      result.confirm(
2✔
264
         "block 2 is inherited",
265
         !std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(addr_blocks[2].addr_choice()).ranges().has_value());
1✔
266

267
      result.test_eq("block 3 has no safi", addr_blocks[3].safi(), std::optional<uint8_t>{std::nullopt});
1✔
268
      const auto& block_3 =
1✔
269
         std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(addr_blocks[3].addr_choice()).ranges().value();
1✔
270

271
      result.confirm("block 3 has correct size", block_3.size() == 1);
2✔
272
      result.test_eq("block 3 min is correct",
2✔
273
                     block_3[0].min().value(),
1✔
274
                     {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff});
275
      result.test_eq("block 3 max is correct",
2✔
276
                     block_3[0].max().value(),
1✔
277
                     {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff});
278

279
      result.test_eq("block 24 has correct safi", addr_blocks[4].safi(), std::optional<uint8_t>{1});
1✔
280
      result.confirm(
2✔
281
         "block 4 is inherited",
282
         !std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(addr_blocks[4].addr_choice()).ranges().has_value());
1✔
283
   }
1✔
284
   {
1✔
285
      const std::string filename("InvalidIPAddrBlocks.pem");
1✔
286
      Botan::X509_Certificate cert(Test::data_file("x509/x509test/" + filename));
2✔
287

288
      // cert contains the 10.0.32.0/20 prefix, but with a 9 for the unused bits
289

290
      result.confirm("extension is present", cert.v3_extensions().extension_set(IPAddressBlocks::static_oid()));
2✔
291

292
      const auto* ext = cert.v3_extensions().get_extension_object_as<IPAddressBlocks>();
1✔
293
      result.confirm("extension is not decoded", ext == nullptr);
2✔
294
   }
1✔
295

296
   result.end_timer();
1✔
297
   return result;
1✔
298
}
×
299

300
Test::Result test_x509_as_blocks_extension_decode() {
1✔
301
   Test::Result result("X509 AS Block decode");
1✔
302
   result.start_timer();
1✔
303
   using Botan::Cert_Extension::ASBlocks;
1✔
304

305
   {
1✔
306
      const std::string filename("ASNumberCert.pem");
1✔
307
      Botan::X509_Certificate cert(Test::data_file("x509/x509test/" + filename));
2✔
308

309
      const auto* as_blocks = cert.v3_extensions().get_extension_object_as<ASBlocks>();
1✔
310

311
      const auto& identifier = as_blocks->as_identifiers();
1✔
312
      result.confirm("cert has ASBlock extension", as_blocks != nullptr, true);
2✔
313

314
      const auto& asnum = identifier.asnum().value().ranges().value();
1✔
315
      const auto& rdi = identifier.rdi().value().ranges().value();
1✔
316

317
      // cert contains asnum 0-999, 5042, 0-4294967295
318
      result.confirm("asnum entry 0 min", asnum[0].min() == 0, true);
2✔
319
      result.confirm("asnum entry 0 max", asnum[0].max() == 4294967295, true);
2✔
320

321
      // and rdi 1234-5678, 32768, 0-4294967295
322
      result.confirm("rdi entry 0 min", rdi[0].min() == 0, true);
2✔
323
      result.confirm("rdi entry 0 max", rdi[0].max() == 4294967295, true);
2✔
324
   }
1✔
325
   {
1✔
326
      const std::string filename("ASNumberOnly.pem");
1✔
327
      Botan::X509_Certificate cert(Test::data_file("x509/x509test/" + filename));
2✔
328

329
      const auto* as_blocks = cert.v3_extensions().get_extension_object_as<ASBlocks>();
1✔
330

331
      const auto& identifier = as_blocks->as_identifiers();
1✔
332
      result.confirm("cert has ASBlock extension", as_blocks != nullptr, true);
2✔
333

334
      const auto& asnum = identifier.asnum().value().ranges().value();
1✔
335
      result.confirm("cert has no RDI entries", identifier.rdi().has_value(), false);
2✔
336

337
      // contains 0-999, 0-4294967295
338
      result.confirm("asnum entry 0 min", asnum[0].min() == 0, true);
2✔
339
      result.confirm("asnum entry 0 max", asnum[0].max() == 4294967295, true);
2✔
340
   }
1✔
341
   {
1✔
342
      const std::string filename("ASRdiOnly.pem");
1✔
343
      Botan::X509_Certificate cert(Test::data_file("x509/x509test/" + filename));
2✔
344

345
      const auto* as_blocks = cert.v3_extensions().get_extension_object_as<ASBlocks>();
1✔
346

347
      const auto& identifier = as_blocks->as_identifiers();
1✔
348
      result.confirm("cert has ASBlock extension", as_blocks != nullptr, true);
2✔
349

350
      result.confirm("cert has no ASNUM entries", identifier.asnum().has_value(), false);
2✔
351
      const auto& rdi = identifier.rdi().value().ranges().value();
1✔
352

353
      // contains 1234-5678, 0-4294967295
354
      result.confirm("rdi entry 0 min", rdi[0].min() == 0, true);
2✔
355
      result.confirm("rdi entry 0 max", rdi[0].max() == 4294967295, true);
2✔
356
   }
1✔
357
   {
1✔
358
      const std::string filename("ASNumberInherit.pem");
1✔
359
      Botan::X509_Certificate cert(Test::data_file("x509/x509test/" + filename));
2✔
360

361
      const auto* as_blocks = cert.v3_extensions().get_extension_object_as<ASBlocks>();
1✔
362

363
      const auto& identifier = as_blocks->as_identifiers();
1✔
364
      result.confirm("cert has ASBlock extension", as_blocks != nullptr, true);
2✔
365

366
      result.confirm("asnum has no entries", identifier.asnum().value().ranges().has_value(), false);
2✔
367
      const auto& rdi = identifier.rdi().value().ranges().value();
1✔
368

369
      // contains 1234-5678, 0-4294967295
370
      result.confirm("rdi entry 0 min", rdi[0].min() == 0, true);
2✔
371
      result.confirm("rdi entry 0 max", rdi[0].max() == 4294967295, true);
2✔
372
   }
1✔
373

374
   result.end_timer();
1✔
375
   return result;
1✔
376
}
×
377

378
   #endif
379

380
Test::Result test_x509_ip_addr_blocks_rfc3779_example() {
1✔
381
   Test::Result result("X509 IP Address Blocks rfc3779 example");
1✔
382
   result.start_timer();
1✔
383

384
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
385
   auto rng = Test::new_rng(__func__);
1✔
386

387
   // construct like in https://datatracker.ietf.org/doc/html/rfc3779#page-18
388
   std::unique_ptr<IPAddressBlocks> blocks_1 = std::make_unique<IPAddressBlocks>();
1✔
389
   blocks_1->add_address<IPv4>({10, 0, 32, 0}, {10, 0, 47, 255}, 1);
1✔
390
   blocks_1->add_address<IPv4>({10, 0, 64, 0}, {10, 0, 64, 255}, 1);
1✔
391
   blocks_1->add_address<IPv4>({10, 1, 0, 0}, {10, 1, 255, 255}, 1);
1✔
392
   blocks_1->add_address<IPv4>({10, 2, 48, 0}, {10, 2, 63, 255}, 1);
1✔
393
   blocks_1->add_address<IPv4>({10, 2, 64, 0}, {10, 2, 64, 255}, 1);
1✔
394
   blocks_1->add_address<IPv4>({10, 3, 0, 0}, {10, 3, 255, 255}, 1);
1✔
395
   blocks_1->inherit<IPv6>();
1✔
396

397
   Botan::X509_Cert_Options opts_1 = ca_opts();
1✔
398
   opts_1.extensions.add(std::move(blocks_1));
1✔
399

400
   auto cert_1 = make_self_signed(rng, opts_1);
1✔
401

402
   auto bits_1 = cert_1.v3_extensions().get_extension_bits(IPAddressBlocks::static_oid());
1✔
403

404
   result.test_eq(
1✔
405
      "extension is encoded as specified",
406
      bits_1,
407
      "3035302B040300010130240304040A00200304000A00400303000A01300C0304040A02300304000A02400303000A033006040200020500");
408

409
   const auto* ext_1 = cert_1.v3_extensions().get_extension_object_as<IPAddressBlocks>();
1✔
410

411
   auto ext_1_addr_fam_1 = ext_1->addr_blocks()[0];
1✔
412
   result.test_eq("extension 1 ipv4 safi", ext_1_addr_fam_1.safi(), std::optional<uint8_t>(1));
1✔
413
   auto ext_1_ranges =
1✔
414
      std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(ext_1_addr_fam_1.addr_choice()).ranges().value();
1✔
415
   result.test_eq("extension 1 range 1 min", ext_1_ranges[0].min().value(), {10, 0, 32, 0});
2✔
416
   result.test_eq("extension 1 range 1 max", ext_1_ranges[0].max().value(), {10, 0, 47, 255});
2✔
417

418
   result.test_eq("extension 1 range 2 min", ext_1_ranges[1].min().value(), {10, 0, 64, 0});
2✔
419
   result.test_eq("extension 1 range 2 max", ext_1_ranges[1].max().value(), {10, 0, 64, 255});
2✔
420

421
   result.test_eq("extension 1 range 3 min", ext_1_ranges[2].min().value(), {10, 1, 0, 0});
2✔
422
   result.test_eq("extension 1 range 3 max", ext_1_ranges[2].max().value(), {10, 1, 255, 255});
2✔
423

424
   result.test_eq("extension 1 range 4 min", ext_1_ranges[3].min().value(), {10, 2, 48, 0});
2✔
425
   result.test_eq("extension 1 range 4 max", ext_1_ranges[3].max().value(), {10, 2, 64, 255});
2✔
426

427
   result.test_eq("extension 1 range 5 min", ext_1_ranges[4].min().value(), {10, 3, 0, 0});
2✔
428
   result.test_eq("extension 1 range 5 max", ext_1_ranges[4].max().value(), {10, 3, 255, 255});
2✔
429

430
   result.test_eq("extension 1 ipv6 safi", ext_1->addr_blocks()[1].safi(), std::optional<uint8_t>{std::nullopt});
1✔
431
   result.confirm(
2✔
432
      "extension 1 ipv6 inherited",
433
      !std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(ext_1->addr_blocks()[1].addr_choice()).ranges().has_value());
1✔
434

435
   // https://datatracker.ietf.org/doc/html/rfc3779#page-20
436
   std::unique_ptr<IPAddressBlocks> blocks_2 = std::make_unique<IPAddressBlocks>();
1✔
437
   blocks_2->add_address<IPv6>(
1✔
438
      {0x20, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
439
      {0x20, 0x01, 0x00, 0x00, 0x00, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff});
440
   blocks_2->add_address<IPv4>({10, 0, 0, 0}, {10, 255, 255, 255}, 1);
1✔
441
   blocks_2->add_address<IPv4>({172, 16, 0, 0}, {172, 31, 255, 255}, 1);
1✔
442
   blocks_2->inherit<IPv4>(2);
1✔
443

444
   Botan::X509_Cert_Options opts_2 = ca_opts();
1✔
445
   opts_2.extensions.add(std::move(blocks_2));
1✔
446

447
   auto cert_2 = make_self_signed(rng, opts_2);
1✔
448

449
   auto bits_2 = cert_2.v3_extensions().get_extension_bits(IPAddressBlocks::static_oid());
1✔
450

451
   // see https://www.rfc-editor.org/errata/eid6792 as to why the B0 specified in the RFC is a AC here
452
   result.test_eq("extension is encoded as specified",
1✔
453
                  bits_2,
454
                  "302C3010040300010130090302000A030304AC10300704030001020500300F040200023009030700200100000002");
455

456
   const auto* ext_2 = cert_2.v3_extensions().get_extension_object_as<IPAddressBlocks>();
1✔
457

458
   auto ext_2_addr_fam_1 = ext_2->addr_blocks()[0];
1✔
459
   result.test_eq("extension 2 ipv4 1 safi", ext_2_addr_fam_1.safi(), std::optional<uint8_t>(1));
1✔
460
   auto ext_2_ranges_1 =
1✔
461
      std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(ext_2_addr_fam_1.addr_choice()).ranges().value();
1✔
462
   result.test_eq("extension 2 fam 1 range 1 min", ext_2_ranges_1[0].min().value(), {10, 0, 0, 0});
2✔
463
   result.test_eq("extension 2 fam 1 range 1 max", ext_2_ranges_1[0].max().value(), {10, 255, 255, 255});
2✔
464

465
   result.test_eq("extension 2 fam 1 range 2 min", ext_2_ranges_1[1].min().value(), {172, 16, 0, 0});
2✔
466
   result.test_eq("extension 2 fam 1 range 2 max", ext_2_ranges_1[1].max().value(), {172, 31, 255, 255});
2✔
467

468
   result.test_eq("extension 2 ipv4 2 safi", ext_2->addr_blocks()[1].safi(), std::optional<uint8_t>{2});
1✔
469
   result.confirm(
2✔
470
      "extension 2 ipv4 2 inherited",
471
      !std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(ext_2->addr_blocks()[1].addr_choice()).ranges().has_value());
1✔
472

473
   auto ext_2_addr_fam_3 = ext_2->addr_blocks()[2];
1✔
474
   result.test_eq("extension 2 ipv4 1 safi", ext_2_addr_fam_3.safi(), std::optional<uint8_t>(std::nullopt));
1✔
475
   auto ext_2_ranges_3 =
1✔
476
      std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(ext_2_addr_fam_3.addr_choice()).ranges().value();
1✔
477
   result.test_eq("extension 2 fam 3 range 1 min",
2✔
478
                  ext_2_ranges_3[0].min().value(),
1✔
479
                  {0x20, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
480
   result.test_eq("extension 2 fam 3 range 1 max",
2✔
481
                  ext_2_ranges_3[0].max().value(),
1✔
482
                  {0x20, 0x01, 0x00, 0x00, 0x00, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff});
483

484
   result.end_timer();
1✔
485
   return result;
2✔
486
}
7✔
487

488
Test::Result test_x509_ip_addr_blocks_encode_builder() {
1✔
489
   Test::Result result("X509 IP Address Blocks encode (builder)");
1✔
490
   result.start_timer();
1✔
491

492
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
493
   auto rng = Test::new_rng(__func__);
1✔
494

495
   std::unique_ptr<IPAddressBlocks> blocks = std::make_unique<IPAddressBlocks>();
1✔
496

497
   // 64 - 127
498
   blocks->add_address<IPv4>({192, 168, 0b01000000, 0}, {192, 168, 0b01111111, 255}, 2);
1✔
499

500
   blocks->add_address<IPv4>({255, 255, 255, 255});
1✔
501
   // encoded as prefix
502
   blocks->add_address<IPv4>({190, 5, 0, 0}, {190, 5, 0b01111111, 255});
1✔
503
   // encoded as min, max
504
   blocks->add_address<IPv4>({127, 0, 0, 1}, {189, 5, 7, 255});
1✔
505

506
   // full address range
507
   blocks->add_address<IPv4>({0, 0, 0, 0}, {255, 255, 255, 255}, 1);
1✔
508

509
   blocks->add_address<IPv4>({123, 123, 2, 1});
1✔
510

511
   Botan::X509_Cert_Options opts = ca_opts();
1✔
512
   opts.extensions.add(std::move(blocks));
1✔
513

514
   auto cert = make_self_signed(rng, opts);
1✔
515
   auto bits = cert.v3_extensions().get_extension_bits(IPAddressBlocks::static_oid());
1✔
516

517
   // hand validated with https://lapo.it/asn1js/
518
   result.test_eq(
1✔
519
      "extension is encoded as specified",
520
      bits,
521
      "304630290402000130230305007B7B0201300D0305007F000001030403BD0500030407BE0500030500FFFFFFFF300A04030001013003030100300D04030001023006030406C0A840");
522

523
   result.end_timer();
1✔
524
   return result;
1✔
525
}
2✔
526

527
namespace {
528

529
template <size_t I>
530
bool bit_set(size_t v) {
648✔
531
   if(((v >> I) & 1) == 1) {
648✔
532
      return true;
533
   } else {
534
      return false;
324✔
535
   }
536
}
537

538
}  // namespace
539

540
Test::Result test_x509_ip_addr_blocks_extension_encode_ctor() {
1✔
541
   Test::Result result("X509 IP Address Block encode (ctor)");
1✔
542
   result.start_timer();
1✔
543

544
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
545

546
   auto rng = Test::new_rng(__func__);
1✔
547

548
   auto [ca_cert, ca, sub_key, sig_algo, hash_fn] = make_ca(rng);
1✔
549

550
   for(size_t i = 0; i < 64; i++) {
65✔
551
      bool push_ipv4_ranges = bit_set<0>(i);
64✔
552
      bool push_ipv6_ranges = bit_set<1>(i);
64✔
553
      bool inherit_ipv4 = bit_set<2>(i);
64✔
554
      bool inherit_ipv6 = bit_set<3>(i);
64✔
555
      bool push_ipv4_family = bit_set<4>(i);
64✔
556
      bool push_ipv6_family = bit_set<5>(i);
64✔
557

558
      Botan::X509_Cert_Options opts = req_opts(sig_algo);
64✔
559

560
      std::vector<uint8_t> a = {123, 123, 2, 1};
64✔
561
      auto ipv4_1 = IPAddressBlocks::IPAddress<IPv4>(a);
64✔
562
      a = {255, 255, 255, 255};
64✔
563
      auto ipv4_2 = IPAddressBlocks::IPAddress<IPv4>(a);
64✔
564

565
      // encoded as min, max
566
      a = {127, 0, 0, 1};
64✔
567
      auto ipv4_range_1_min = IPAddressBlocks::IPAddress<IPv4>(a);
64✔
568
      a = {189, 5, 7, 255};
64✔
569
      auto ipv4_range_1_max = IPAddressBlocks::IPAddress<IPv4>(a);
64✔
570

571
      // encoded as prefix
572
      a = {190, 5, 0, 0};
64✔
573
      auto ipv4_range_2_min = IPAddressBlocks::IPAddress<IPv4>(a);
64✔
574
      a = {190, 5, 127, 255};
64✔
575
      auto ipv4_range_2_max = IPAddressBlocks::IPAddress<IPv4>(a);
64✔
576

577
      a = {0xAB, 0xCD, 0xDE, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
64✔
578
      auto ipv6_1 = IPAddressBlocks::IPAddress<IPv6>(a);
64✔
579
      a = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
64✔
580
      auto ipv6_2 = IPAddressBlocks::IPAddress<IPv6>(a);
64✔
581

582
      // encoded as min, max
583
      a = {0xAF, 0x23, 0x34, 0x45, 0x67, 0x2A, 0x7A, 0xEF, 0x8C, 0x00, 0x00, 0x00, 0x66, 0x00, 0x52, 0x00};
64✔
584
      auto ipv6_range_1_min = IPAddressBlocks::IPAddress<IPv6>(a);
64✔
585

586
      a = {0xAF, 0xCD, 0xDE, 0xF0, 0x00, 0x0F, 0xEE, 0x00, 0xBB, 0x4A, 0x9B, 0x00, 0x00, 0x4C, 0x00, 0xCC};
64✔
587
      auto ipv6_range_1_max = IPAddressBlocks::IPAddress<IPv6>(a);
64✔
588

589
      // encoded as prefix
590
      a = {0xBF, 0xCD, 0xDE, 0xF0, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
64✔
591
      auto ipv6_range_2_min = IPAddressBlocks::IPAddress<IPv6>(a);
64✔
592
      a = {0xBF, 0xCD, 0xDE, 0xF0, 0x00, 0x00, 0x00, 0x07, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
64✔
593
      auto ipv6_range_2_max = IPAddressBlocks::IPAddress<IPv6>(a);
64✔
594

595
      auto ipv4_range_1 = IPAddressBlocks::IPAddressOrRange<IPv4>(ipv4_1);
64✔
596
      auto ipv4_range_2 = IPAddressBlocks::IPAddressOrRange<IPv4>(ipv4_range_1_min, ipv4_range_1_max);
64✔
597
      auto ipv4_range_3 = IPAddressBlocks::IPAddressOrRange<IPv4>(ipv4_range_2_min, ipv4_range_2_max);
64✔
598
      auto ipv4_range_4 = IPAddressBlocks::IPAddressOrRange<IPv4>(ipv4_2);
64✔
599

600
      auto ipv6_range_1 = IPAddressBlocks::IPAddressOrRange<IPv6>(ipv6_1);
64✔
601
      auto ipv6_range_2 = IPAddressBlocks::IPAddressOrRange<IPv6>(ipv6_range_1_min, ipv6_range_1_max);
64✔
602
      auto ipv6_range_3 = IPAddressBlocks::IPAddressOrRange<IPv6>(ipv6_range_2_min, ipv6_range_2_max);
64✔
603
      auto ipv6_range_4 = IPAddressBlocks::IPAddressOrRange<IPv6>(ipv6_2);
64✔
604

605
      std::vector<IPAddressBlocks::IPAddressOrRange<IPv4>> ipv4_ranges;
64✔
606
      if(push_ipv4_ranges) {
64✔
607
         ipv4_ranges.push_back(ipv4_range_1);
32✔
608
         ipv4_ranges.push_back(ipv4_range_2);
32✔
609
         ipv4_ranges.push_back(ipv4_range_3);
32✔
610
         ipv4_ranges.push_back(ipv4_range_4);
32✔
611
      }
612

613
      std::vector<IPAddressBlocks::IPAddressOrRange<IPv6>> ipv6_ranges;
64✔
614
      if(push_ipv6_ranges) {
64✔
615
         ipv6_ranges.push_back(ipv6_range_1);
32✔
616
         ipv6_ranges.push_back(ipv6_range_2);
32✔
617
         ipv6_ranges.push_back(ipv6_range_3);
32✔
618
         ipv6_ranges.push_back(ipv6_range_4);
32✔
619
      }
620

621
      auto ipv4_addr_choice = IPAddressBlocks::IPAddressChoice<IPv4>();
64✔
622
      if(!inherit_ipv4) {
64✔
623
         ipv4_addr_choice = IPAddressBlocks::IPAddressChoice<IPv4>(ipv4_ranges);
96✔
624
      }
625

626
      auto ipv6_addr_choice = IPAddressBlocks::IPAddressChoice<IPv6>();
64✔
627
      if(!inherit_ipv6) {
64✔
628
         ipv6_addr_choice = IPAddressBlocks::IPAddressChoice<IPv6>(ipv6_ranges);
96✔
629
      }
630

631
      auto ipv4_addr_family = IPAddressBlocks::IPAddressFamily(ipv4_addr_choice);
160✔
632
      auto ipv6_addr_family = IPAddressBlocks::IPAddressFamily(ipv6_addr_choice);
160✔
633

634
      std::vector<IPAddressBlocks::IPAddressFamily> addr_blocks;
64✔
635
      if(push_ipv4_family) {
64✔
636
         addr_blocks.push_back(ipv4_addr_family);
32✔
637
      }
638
      if(push_ipv6_family) {
64✔
639
         addr_blocks.push_back(ipv6_addr_family);
32✔
640
      }
641

642
      std::unique_ptr<IPAddressBlocks> blocks = std::make_unique<IPAddressBlocks>(addr_blocks);
64✔
643

644
      opts.extensions.add(std::move(blocks));
64✔
645

646
      Botan::PKCS10_Request req = Botan::X509::create_cert_req(opts, *sub_key, hash_fn, *rng);
64✔
647
      Botan::X509_Certificate cert = ca.sign_request(req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
64✔
648
      {
64✔
649
         const auto* ip_blocks = cert.v3_extensions().get_extension_object_as<IPAddressBlocks>();
64✔
650
         result.confirm("cert has IPAddrBlocks extension", ip_blocks != nullptr, true);
128✔
651

652
         const auto& dec_addr_blocks = ip_blocks->addr_blocks();
64✔
653
         if(!push_ipv4_family && !push_ipv6_family) {
64✔
654
            result.confirm("no address family entries", dec_addr_blocks.empty(), true);
32✔
655
            continue;
16✔
656
         }
657

658
         if(push_ipv4_family) {
48✔
659
            auto family = dec_addr_blocks[0];
32✔
660
            result.confirm("ipv4 family afi", ipv4_addr_family.afi() == family.afi(), true);
64✔
661
            result.test_eq("ipv4 family safi", ipv4_addr_family.safi(), family.safi());
32✔
662
            auto choice = std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(family.addr_choice());
32✔
663

664
            if(!inherit_ipv4) {
32✔
665
               auto ranges = choice.ranges().value();
16✔
666
               if(push_ipv4_ranges) {
16✔
667
                  result.test_eq("ipv4 entry 0 min", ranges[0].min().value(), ipv4_range_1.min().value());
16✔
668
                  result.test_eq("ipv4 entry 0 max", ranges[0].max().value(), ipv4_range_1.max().value());
16✔
669
                  result.test_eq("ipv4 entry 1 min", ranges[1].min().value(), ipv4_range_2.min().value());
16✔
670
                  result.test_eq("ipv4 entry 1 max", ranges[1].max().value(), ipv4_range_2.max().value());
16✔
671
                  result.test_eq("ipv4 entry 2 min", ranges[2].min().value(), ipv4_range_3.min().value());
16✔
672
                  result.test_eq("ipv4 entry 2 max", ranges[2].max().value(), ipv4_range_3.max().value());
16✔
673
                  result.test_eq("ipv4 entry 3 min", ranges[3].min().value(), ipv4_range_4.min().value());
16✔
674
                  result.test_eq("ipv4 entry 3 max", ranges[3].max().value(), ipv4_range_4.max().value());
16✔
675
               } else {
676
                  result.confirm("ipv4 range has no entries", ranges.empty(), true);
16✔
677
               }
678
            } else {
16✔
679
               result.confirm("ipv4 family inherit", choice.ranges().has_value(), false);
32✔
680
            }
681
         }
64✔
682

683
         if(push_ipv6_family) {
48✔
684
            auto family = dec_addr_blocks[dec_addr_blocks.size() - 1];
32✔
685
            result.confirm("ipv6 family afi", ipv6_addr_family.afi() == family.afi(), true);
64✔
686
            result.test_eq("ipv6 family safi", ipv6_addr_family.safi(), family.safi());
32✔
687
            auto choice = std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(family.addr_choice());
32✔
688
            if(!inherit_ipv6) {
32✔
689
               auto ranges = choice.ranges().value();
16✔
690
               if(push_ipv6_ranges) {
16✔
691
                  result.test_eq("ipv6 entry 0 min", ranges[0].min().value(), ipv6_range_1.min().value());
16✔
692
                  result.test_eq("ipv6 entry 0 max", ranges[0].max().value(), ipv6_range_1.max().value());
16✔
693
                  result.test_eq("ipv6 entry 1 min", ranges[1].min().value(), ipv6_range_2.min().value());
16✔
694
                  result.test_eq("ipv6 entry 1 max", ranges[1].max().value(), ipv6_range_2.max().value());
16✔
695
                  result.test_eq("ipv6 entry 2 min", ranges[2].min().value(), ipv6_range_3.min().value());
16✔
696
                  result.test_eq("ipv6 entry 2 max", ranges[2].max().value(), ipv6_range_3.max().value());
16✔
697
                  result.test_eq("ipv6 entry 3 min", ranges[3].min().value(), ipv6_range_4.min().value());
16✔
698
                  result.test_eq("ipv6 entry 3 max", ranges[3].max().value(), ipv6_range_4.max().value());
16✔
699
               } else {
700
                  result.confirm("ipv6 range has no entries", ranges.empty(), true);
16✔
701
               }
702
            } else {
16✔
703
               result.confirm("ipv6 family inherit", choice.ranges().has_value(), false);
32✔
704
            }
705
         }
64✔
706
      }
707
   }
320✔
708

709
   result.end_timer();
1✔
710
   return result;
2✔
711
}
2✔
712

713
Test::Result test_x509_ip_addr_blocks_extension_encode_edge_cases_ctor() {
1✔
714
   Test::Result result("X509 IP Address Block encode edge cases (ctor)");
1✔
715
   result.start_timer();
1✔
716

717
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
718

719
   auto rng = Test::new_rng(__func__);
1✔
720

721
   // trailing 0s, trailing 1s, and some arbitrary values
722
   std::vector<uint8_t> edge_values = {0,  2,  4,  8,   16,  32, 64, 128, 1,   3,  7,
1✔
723
                                       15, 31, 63, 127, 255, 12, 46, 123, 160, 234};
1✔
724

725
   auto [ca_cert, ca, sub_key, sig_algo, hash_fn] = make_ca(rng);
1✔
726

727
   for(size_t i = 0; i < edge_values.size(); i++) {
22✔
728
      for(size_t j = 0; j < 4; j++) {
105✔
729
         bool modify_min = bit_set<0>(j);
84✔
730
         bool modify_max = bit_set<1>(j);
84✔
731

732
         for(size_t k = 0; k < 18; k++) {
1,219✔
733
            if(!modify_min && !modify_max && (k > 0 || i > 0)) {
1,156✔
734
               // we don't modify anything, this is the extreme edge case of 0.0 ... - 255.255. ...
735
               // so we only need to do this once
736
               break;
737
            }
738

739
            Botan::X509_Cert_Options opts = req_opts(sig_algo);
1,135✔
740

741
            std::vector<uint8_t> min_bytes(16, 0x00);
1,135✔
742
            std::vector<uint8_t> max_bytes(16, 0xFF);
1,135✔
743

744
            if(modify_min) {
1,135✔
745
               min_bytes[15 - (k < 2 ? 0 : k - 2)] = edge_values[i];
756✔
746
            }
747
            if(modify_max) {
1,135✔
748
               max_bytes[15 - (k > 15 ? 15 : k)] = edge_values[i];
756✔
749
            }
750

751
            auto address_min = IPAddressBlocks::IPAddress<IPv6>(min_bytes);
1,135✔
752
            auto address_max = IPAddressBlocks::IPAddress<IPv6>(max_bytes);
1,135✔
753

754
            auto ipv6_range = IPAddressBlocks::IPAddressOrRange<IPv6>(address_min, address_max);
1,135✔
755

756
            std::vector<IPAddressBlocks::IPAddressOrRange<IPv6>> ipv6_ranges;
1,135✔
757
            ipv6_ranges.push_back(ipv6_range);
1,135✔
758

759
            auto ipv6_addr_choice = IPAddressBlocks::IPAddressChoice<IPv6>(ipv6_ranges);
1,135✔
760

761
            auto ipv6_addr_family = IPAddressBlocks::IPAddressFamily(ipv6_addr_choice);
3,405✔
762

763
            std::vector<IPAddressBlocks::IPAddressFamily> addr_blocks;
1,135✔
764
            addr_blocks.push_back(ipv6_addr_family);
1,135✔
765

766
            std::unique_ptr<IPAddressBlocks> blocks = std::make_unique<IPAddressBlocks>(addr_blocks);
1,135✔
767

768
            opts.extensions.add(std::move(blocks));
1,135✔
769

770
            Botan::PKCS10_Request req = Botan::X509::create_cert_req(opts, *sub_key, hash_fn, *rng);
1,135✔
771
            Botan::X509_Certificate cert = ca.sign_request(req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
1,135✔
772
            {
1,135✔
773
               const auto* ip_blocks = cert.v3_extensions().get_extension_object_as<IPAddressBlocks>();
1,135✔
774
               result.confirm("cert has IPAddrBlocks extension", ip_blocks != nullptr, true);
2,270✔
775
               const auto& dec_addr_blocks = ip_blocks->addr_blocks();
1,135✔
776
               auto family = dec_addr_blocks[0];
1,135✔
777
               result.confirm("ipv6 family afi", ipv6_addr_family.afi() == family.afi(), true);
2,270✔
778
               result.test_eq("ipv6 family safi", ipv6_addr_family.safi(), family.safi());
1,135✔
779
               auto choice = std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(family.addr_choice());
1,135✔
780
               auto ranges = choice.ranges().value();
1,135✔
781

782
               result.test_eq("ipv6 edge case min", ranges[0].min().value(), ipv6_range.min().value());
2,270✔
783
               result.test_eq("ipv6 edge case max", ranges[0].max().value(), ipv6_range.max().value());
2,270✔
784
            }
2,270✔
785
         }
5,675✔
786
      }
787
   }
788
   result.end_timer();
1✔
789
   return result;
2✔
790
}
3✔
791

792
Test::Result test_x509_ip_addr_blocks_range_merge() {
1✔
793
   Test::Result result("X509 IP Address Block range merge");
1✔
794
   result.start_timer();
1✔
795

796
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
797

798
   auto rng = Test::new_rng(__func__);
1✔
799

800
   auto [ca_cert, ca, sub_key, sig_algo, hash_fn] = make_ca(rng);
1✔
801
   Botan::X509_Cert_Options opts = req_opts(sig_algo);
1✔
802

803
   std::vector<std::vector<std::vector<uint8_t>>> addresses = {
1✔
804
      {{11, 0, 0, 0}, {{11, 0, 0, 0}}},
805
      {{123, 123, 123, 123}, {123, 123, 123, 123}},
806
      {{10, 4, 5, 9}, {{10, 255, 0, 0}}},
807
      {{12, 0, 0, 0}, {191, 0, 0, 1}},
808
      {{190, 0, 0, 0}, {193, 0, 255, 255}},
809
      {{10, 10, 10, 10}, {10, 20, 20, 20}},
810
      {{5, 0, 0, 0}, {10, 255, 255, 255}},
811
      {{192, 0, 0, 0}, {192, 255, 255, 255}},
812
      {{11, 0, 0, 1}, {11, 255, 255, 255}},
813
   };
46✔
814

815
   std::vector<IPAddressBlocks::IPAddressOrRange<IPv4>> ipv6_ranges;
1✔
816
   for(auto pair : addresses) {
10✔
817
      auto address_min = IPAddressBlocks::IPAddress<IPv4>(pair[0]);
9✔
818
      auto address_max = IPAddressBlocks::IPAddress<IPv4>(pair[1]);
9✔
819
      auto range = IPAddressBlocks::IPAddressOrRange<IPv4>(address_min, address_max);
9✔
820
      ipv6_ranges.push_back(range);
9✔
821
   }
9✔
822

823
   auto ipv6_addr_choice = IPAddressBlocks::IPAddressChoice<IPv4>(ipv6_ranges);
1✔
824
   auto ipv6_addr_family = IPAddressBlocks::IPAddressFamily(ipv6_addr_choice);
3✔
825

826
   std::vector<IPAddressBlocks::IPAddressFamily> addr_blocks;
1✔
827
   addr_blocks.push_back(ipv6_addr_family);
1✔
828

829
   std::unique_ptr<IPAddressBlocks> blocks = std::make_unique<IPAddressBlocks>(addr_blocks);
1✔
830

831
   opts.extensions.add(std::move(blocks));
1✔
832

833
   Botan::PKCS10_Request req = Botan::X509::create_cert_req(opts, *sub_key, hash_fn, *rng);
1✔
834
   Botan::X509_Certificate cert = ca.sign_request(req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
1✔
835
   {
1✔
836
      const auto* ip_blocks = cert.v3_extensions().get_extension_object_as<IPAddressBlocks>();
1✔
837
      result.confirm("cert has IPAddrBlocks extension", ip_blocks != nullptr, true);
2✔
838
      const auto& dec_addr_blocks = ip_blocks->addr_blocks();
1✔
839
      auto family = dec_addr_blocks[0];
1✔
840
      auto choice = std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(family.addr_choice());
1✔
841
      auto ranges = choice.ranges().value();
1✔
842

843
      std::array<uint8_t, 4> expected_min = {5, 0, 0, 0};
1✔
844
      std::array<uint8_t, 4> expected_max = {193, 0, 255, 255};
1✔
845

846
      result.test_eq("range expected min", ranges[0].min().value(), expected_min);
2✔
847
      result.test_eq("range expected max", ranges[0].max().value(), expected_max);
2✔
848
      result.test_eq("range length", ranges.size(), 1);
1✔
849
   }
2✔
850

851
   result.end_timer();
1✔
852
   return result;
2✔
853
}
24✔
854

855
Test::Result test_x509_ip_addr_blocks_family_merge() {
1✔
856
   Test::Result result("X509 IP Address Block family merge");
1✔
857
   result.start_timer();
1✔
858

859
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
860

861
   auto rng = Test::new_rng(__func__);
1✔
862

863
   auto [ca_cert, ca, sub_key, sig_algo, hash_fn] = make_ca(rng);
1✔
864
   Botan::X509_Cert_Options opts = req_opts(sig_algo);
1✔
865

866
   std::vector<IPAddressBlocks::IPAddressFamily> addr_blocks;
1✔
867

868
   IPAddressBlocks::IPAddressChoice<IPv4> v4_empty_choice;
1✔
869
   IPAddressBlocks::IPAddressChoice<IPv6> v6_empty_choice;
1✔
870

871
   uint8_t v4_bytes_1[4] = {123, 123, 123, 123};
1✔
872
   IPAddressBlocks::IPAddress<IPv4> v4_addr_1(v4_bytes_1);
1✔
873
   // create 2 prefixes from the v4 addresses -> they should be merged
874

875
   std::vector<IPAddressBlocks::IPAddressOrRange<IPv4>> v4_choice_vec{
1✔
876
      IPAddressBlocks::IPAddressOrRange<IPv4>(IPAddressBlocks::IPAddress<IPv4>({v4_addr_1}))};
1✔
877
   IPAddressBlocks::IPAddressChoice<IPv4> v4_choice_dupl(v4_choice_vec);
1✔
878
   result.confirm(
2✔
879
      "IPAddressChoice v4 merges ranges already in constructor", v4_choice_dupl.ranges().value().size() == 1, true);
1✔
880
   IPAddressBlocks::IPAddressFamily v4_fam_dupl(v4_choice_dupl, 0);
3✔
881

882
   uint8_t v6_bytes_1[16] = {123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123};
1✔
883
   IPAddressBlocks::IPAddress<IPv6> v6_addr_1(v6_bytes_1);
1✔
884

885
   std::vector<IPAddressBlocks::IPAddressOrRange<IPv6>> v6_choice_vec{
1✔
886
      IPAddressBlocks::IPAddressOrRange<IPv6>(IPAddressBlocks::IPAddress<IPv6>({v6_addr_1}))};
1✔
887
   IPAddressBlocks::IPAddressChoice<IPv6> v6_choice_dupl(v6_choice_vec);
1✔
888
   result.confirm(
2✔
889
      "IPAddressChoice v6 merges already in constructor", v6_choice_dupl.ranges().value().size() == 1, true);
1✔
890
   IPAddressBlocks::IPAddressFamily v6_fam_dupl(v6_choice_dupl, 0);
3✔
891

892
   IPAddressBlocks::IPAddressFamily v4_empty_fam(v4_empty_choice);
2✔
893
   IPAddressBlocks::IPAddressFamily v6_empty_fam(v6_empty_choice);
2✔
894

895
   IPAddressBlocks::IPAddressFamily v4_empty_fam_safi(v4_empty_choice, 2);
2✔
896
   IPAddressBlocks::IPAddressFamily v6_empty_fam_safi(v6_empty_choice, 2);
2✔
897

898
   /*
899
   considering the push order, the resulting order should be
900
   [0] v4 no safi
901
   [2] v4 safi
902
   [1] v6 no safi
903
   [3] v6 safi
904
   */
905
   for(size_t i = 0; i < 3; i++) {
4✔
906
      addr_blocks.push_back(v4_empty_fam_safi);
3✔
907
      addr_blocks.push_back(v6_empty_fam);
3✔
908
      addr_blocks.push_back(v4_fam_dupl);
3✔
909
      addr_blocks.push_back(v6_empty_fam_safi);
3✔
910
      addr_blocks.push_back(v6_fam_dupl);
3✔
911
      addr_blocks.push_back(v4_empty_fam);
3✔
912
   }
913

914
   std::vector<IPAddressBlocks::IPAddressFamily> expected_blocks = {
1✔
915
      v4_empty_fam, v4_fam_dupl, v4_empty_fam_safi, v6_empty_fam, v6_fam_dupl, v6_empty_fam_safi};
7✔
916

917
   std::unique_ptr<IPAddressBlocks> blocks = std::make_unique<IPAddressBlocks>(addr_blocks);
1✔
918

919
   opts.extensions.add(std::move(blocks));
1✔
920

921
   Botan::PKCS10_Request req = Botan::X509::create_cert_req(opts, *sub_key, hash_fn, *rng);
1✔
922
   Botan::X509_Certificate cert = ca.sign_request(req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
1✔
923

924
   const auto* ip_blocks = cert.v3_extensions().get_extension_object_as<IPAddressBlocks>();
1✔
925
   result.confirm("cert has IPAddrBlocks extension", ip_blocks != nullptr, true);
2✔
926
   const auto& dec_blocks = ip_blocks->addr_blocks();
1✔
927

928
   result.confirm("blocks got merged lengthwise", dec_blocks.size() == expected_blocks.size(), true);
2✔
929

930
   bool sorted = true;
1✔
931
   for(size_t i = 0; i < dec_blocks.size() - 1; i++) {
6✔
932
      const IPAddressBlocks::IPAddressFamily& a = dec_blocks[i];
5✔
933
      const IPAddressBlocks::IPAddressFamily& b = dec_blocks[i + 1];
5✔
934

935
      uint32_t afam_a = a.afi();
5✔
936
      if(a.safi().has_value()) {
5✔
937
         afam_a = static_cast<uint32_t>(afam_a << 8) | a.safi().value();
3✔
938
      } else {
939
         afam_a = static_cast<uint32_t>(afam_a << 8);
2✔
940
      }
941

942
      uint32_t afam_b = b.afi();
5✔
943
      if(b.safi().has_value()) {
5✔
944
         afam_b = static_cast<uint32_t>(afam_b << 8) | b.safi().value();
4✔
945
      } else {
946
         afam_b = static_cast<uint32_t>(afam_b << 8);
1✔
947
      }
948

949
      if(afam_a > afam_b) {
5✔
950
         sorted = false;
951
         break;
952
      }
953
   }
954

955
   result.confirm("blocks got sorted", sorted, true);
2✔
956

957
   for(size_t i = 0; i < dec_blocks.size(); i++) {
7✔
958
      const IPAddressBlocks::IPAddressFamily& dec = dec_blocks[i];
6✔
959
      const IPAddressBlocks::IPAddressFamily& exp = expected_blocks[i];
6✔
960

961
      result.confirm("blocks match push order by afi at index " + std::to_string(i), dec.afi() == exp.afi(), true);
12✔
962
      result.test_eq("blocks match push order by safi at index " + std::to_string(i), dec.safi(), exp.safi());
18✔
963

964
      if((exp.afi() == 1) && (dec.afi() == 1)) {
6✔
965
         auto dec_choice = std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(dec.addr_choice());
3✔
966
         auto exp_choice = std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(exp.addr_choice());
3✔
967

968
         if(!exp_choice.ranges().has_value()) {
3✔
969
            result.confirm(
2✔
970
               "block ranges should inherit at index " + std::to_string(i), dec_choice.ranges().has_value(), false);
6✔
971
         } else {
972
            result.confirm(
1✔
973
               "block ranges should not inherit at index " + std::to_string(i), dec_choice.ranges().has_value(), true);
3✔
974

975
            if(dec_choice.ranges().has_value() == false) {
1✔
976
               continue;
×
977
            }
978

979
            auto dec_ranges = dec_choice.ranges().value();
1✔
980
            auto exp_ranges = exp_choice.ranges().value();
1✔
981
            result.confirm("block ranges got merged lengthwise at index " + std::to_string(i),
2✔
982
                           dec_ranges.size() == exp_ranges.size(),
1✔
983
                           true);
984

985
            if(dec_ranges.size() != exp_ranges.size()) {
1✔
986
               continue;
×
987
            }
988

989
            for(size_t j = 0; j < exp_ranges.size(); j++) {
2✔
990
               result.test_eq(
2✔
991
                  "block ranges min got merged valuewise at indices " + std::to_string(i) + "," + std::to_string(j),
4✔
992
                  exp_ranges[j].min().value(),
1✔
993
                  dec_ranges[j].min().value());
1✔
994
               result.test_eq(
2✔
995
                  "block ranges max got merged valuewise at indices " + std::to_string(i) + "," + std::to_string(j),
4✔
996
                  exp_ranges[j].max().value(),
1✔
997
                  dec_ranges[j].max().value());
2✔
998
            }
999
         }
1✔
1000
      } else if((exp.afi() == 2) && (dec.afi() == 2)) {
7✔
1001
         auto dec_choice = std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(dec.addr_choice());
3✔
1002
         auto exp_choice = std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(exp.addr_choice());
3✔
1003

1004
         if(!exp_choice.ranges().has_value()) {
3✔
1005
            result.confirm(
2✔
1006
               "block ranges should inherit at index " + std::to_string(i), dec_choice.ranges().has_value(), false);
6✔
1007
         } else {
1008
            result.confirm(
1✔
1009
               "block ranges should not inherit at index " + std::to_string(i), dec_choice.ranges().has_value(), true);
3✔
1010

1011
            if(dec_choice.ranges().has_value() == false) {
1✔
1012
               continue;
×
1013
            }
1014

1015
            auto dec_ranges = dec_choice.ranges().value();
1✔
1016
            auto exp_ranges = exp_choice.ranges().value();
1✔
1017
            result.confirm("block ranges got merged lengthwise at index " + std::to_string(i),
2✔
1018
                           dec_ranges.size() == exp_ranges.size(),
1✔
1019
                           true);
1020

1021
            if(dec_ranges.size() != exp_ranges.size()) {
1✔
1022
               continue;
×
1023
            }
1024

1025
            for(size_t j = 0; j < exp_ranges.size(); j++) {
2✔
1026
               result.test_eq(
2✔
1027
                  "block ranges min got merged valuewise at indices " + std::to_string(i) + "," + std::to_string(j),
4✔
1028
                  exp_ranges[j].min().value(),
1✔
1029
                  dec_ranges[j].min().value());
1✔
1030
               result.test_eq(
2✔
1031
                  "block ranges max got merged valuewise at indices " + std::to_string(i) + "," + std::to_string(j),
4✔
1032
                  exp_ranges[j].max().value(),
1✔
1033
                  dec_ranges[j].max().value());
2✔
1034
            }
1035
         }
1✔
1036
      }
4✔
1037
   }
1038

1039
   result.end_timer();
1✔
1040
   return result;
2✔
1041
}
19✔
1042

1043
Test::Result test_x509_ip_addr_blocks_path_validation_success_builder() {
1✔
1044
   Test::Result result("X509 IP Address Blocks path validation success (builder)");
1✔
1045
   result.start_timer();
1✔
1046

1047
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
1048
   auto rng = Test::new_rng(__func__);
1✔
1049

1050
   /*
1051
   Creates a certificate chain of length 4.
1052
   Root: ipv4 and ipv6
1053
   Inherit: has both values as 'inherit'
1054
   Dynamic: has either both 'inherit', both with values, or just one with a value
1055
   Subject: both ipv4 and ipv6 as a subset of Root / Dynamic
1056
   */
1057

1058
   // Root cert
1059
   std::unique_ptr<IPAddressBlocks> root_blocks = std::make_unique<IPAddressBlocks>();
1✔
1060

1061
   root_blocks->add_address<IPv4>({120, 0, 0, 1}, {130, 140, 150, 160}, 42);
1✔
1062
   root_blocks->add_address<IPv4>({10, 0, 0, 1}, {10, 255, 255, 255}, 42);
1✔
1063

1064
   root_blocks->add_address<IPv6>(
1✔
1065
      {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
1066
      {0xA0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
1067
   root_blocks->add_address<IPv6>(
1✔
1068
      {0xA2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
1069
      {0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
1070

1071
   // Inherit cert
1072
   std::unique_ptr<IPAddressBlocks> inherit_blocks = std::make_unique<IPAddressBlocks>();
1✔
1073

1074
   inherit_blocks->inherit<IPv4>(42);
1✔
1075
   inherit_blocks->inherit<IPv6>();
1✔
1076

1077
   // Subject cert
1078
   std::unique_ptr<IPAddressBlocks> sub_blocks = std::make_unique<IPAddressBlocks>();
1✔
1079

1080
   sub_blocks->add_address<IPv4>({124, 0, 255, 0}, {126, 0, 0, 1}, 42);
1✔
1081
   sub_blocks->add_address<IPv4>({10, 0, 2, 1}, {10, 42, 0, 255}, 42);
1✔
1082

1083
   sub_blocks->add_address<IPv6>(
1✔
1084
      {0x00, 0x00, 0x00, 0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
1085
      {0x0D, 0x00, 0x00, 0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
1086

1087
   Botan::X509_Cert_Options root_opts = ca_opts();
1✔
1088
   root_opts.extensions.add(std::move(root_blocks));
1✔
1089
   auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] = make_ca(rng, root_opts);
1✔
1090
   Botan::X509_Cert_Options sub_opts = req_opts(sig_algo);
1✔
1091
   sub_opts.extensions.add(std::move(sub_blocks));
1✔
1092
   auto [inherit_cert, inherit_ca] = make_and_sign_ca(std::move(inherit_blocks), root_ca, rng);
2✔
1093

1094
   Botan::Certificate_Store_In_Memory trusted;
1✔
1095
   trusted.add_certificate(root_cert);
1✔
1096

1097
   for(size_t i = 0; i < 4; i++) {
5✔
1098
      bool include_v4 = bit_set<0>(i);
4✔
1099
      bool include_v6 = bit_set<1>(i);
4✔
1100

1101
      // Dynamic Cert
1102
      std::unique_ptr<IPAddressBlocks> dyn_blocks = std::make_unique<IPAddressBlocks>();
4✔
1103
      if(include_v4) {
4✔
1104
         dyn_blocks->add_address<IPv4>({122, 0, 0, 255}, {128, 255, 255, 255}, 42);
2✔
1105
         dyn_blocks->add_address<IPv4>({10, 0, 0, 255}, {10, 255, 0, 1}, 42);
2✔
1106
      } else {
1107
         dyn_blocks->inherit<IPv4>(42);
2✔
1108
      }
1109

1110
      if(include_v6) {
4✔
1111
         dyn_blocks->add_address<IPv6>(
2✔
1112
            {0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
1113
            {0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
1114
      } else {
1115
         dyn_blocks->inherit<IPv6>();
2✔
1116
      }
1117

1118
      auto [dyn_cert, dyn_ca] = make_and_sign_ca(std::move(dyn_blocks), inherit_ca, rng);
8✔
1119

1120
      Botan::PKCS10_Request sub_req = Botan::X509::create_cert_req(sub_opts, *sub_key, hash_fn, *rng);
4✔
1121
      Botan::X509_Certificate sub_cert =
4✔
1122
         dyn_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
4✔
1123

1124
      const Botan::Path_Validation_Restrictions restrictions(false, 80);
8✔
1125
      std::vector<Botan::X509_Certificate> certs = {sub_cert, dyn_cert, inherit_cert};
16✔
1126

1127
      Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
4✔
1128
      result.require("path validation succeeds", path_result.successful_validation());
4✔
1129
   }
8✔
1130

1131
   result.end_timer();
1✔
1132
   return result;
2✔
1133
}
7✔
1134

1135
Test::Result test_x509_ip_addr_blocks_path_validation_success_ctor() {
1✔
1136
   Test::Result result("X509 IP Address Block path validation success (ctor)");
1✔
1137
   result.start_timer();
1✔
1138

1139
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
1140
   auto rng = Test::new_rng(__func__);
1✔
1141

1142
   /*
1143
   Creates a certificate chain of length 4.
1144
   Root: ipv4 and ipv6
1145
   Inherit: has both values as 'inherit'
1146
   Dynamic: has either both 'inherit', both with values, or just one with a value
1147
   Subject: both ipv4 and ipv6 as a subset of Root / Dynamic
1148
   */
1149

1150
   // Root cert
1151
   std::vector<uint8_t> a = {120, 0, 0, 1};
1✔
1152
   auto root_ipv4_range_1_min = IPAddressBlocks::IPAddress<IPv4>{a};
1✔
1153
   a = {130, 140, 150, 160};
1✔
1154
   auto root_ipv4_range_1_max = IPAddressBlocks::IPAddress<IPv4>{a};
1✔
1155

1156
   a = {10, 0, 0, 1};
1✔
1157
   auto root_ipv4_range_2_min = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1158
   a = {10, 255, 255, 255};
1✔
1159
   auto root_ipv4_range_2_max = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1160

1161
   a = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1✔
1162
   auto root_ipv6_range_1_min = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1163
   a = {0xA0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1✔
1164
   auto root_ipv6_range_1_max = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1165

1166
   a = {0xA2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1✔
1167
   auto root_ipv6_range_2_min = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1168
   a = {0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1✔
1169
   auto root_ipv6_range_2_max = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1170

1171
   auto root_ipv4_range_1 = IPAddressBlocks::IPAddressOrRange<IPv4>(root_ipv4_range_1_min, root_ipv4_range_1_max);
1✔
1172
   auto root_ipv4_range_2 = IPAddressBlocks::IPAddressOrRange<IPv4>(root_ipv4_range_2_min, root_ipv4_range_2_max);
1✔
1173
   auto root_ipv6_range_1 = IPAddressBlocks::IPAddressOrRange<IPv6>(root_ipv6_range_1_min, root_ipv6_range_1_max);
1✔
1174
   auto root_ipv6_range_2 = IPAddressBlocks::IPAddressOrRange<IPv6>(root_ipv6_range_2_min, root_ipv6_range_2_max);
1✔
1175

1176
   auto root_ipv4_ranges = {root_ipv4_range_1, root_ipv4_range_2};
1✔
1177
   auto root_ipv6_ranges = {root_ipv6_range_1, root_ipv6_range_2};
1✔
1178

1179
   auto root_ipv4_choice = IPAddressBlocks::IPAddressChoice<IPv4>(root_ipv4_ranges);
1✔
1180
   auto root_ipv6_choice = IPAddressBlocks::IPAddressChoice<IPv6>(root_ipv6_ranges);
1✔
1181

1182
   auto root_ipv4_family = IPAddressBlocks::IPAddressFamily(root_ipv4_choice, 42);
3✔
1183
   auto root_ipv6_family = IPAddressBlocks::IPAddressFamily(root_ipv6_choice);
3✔
1184

1185
   auto root_addr_blocks = {root_ipv4_family, root_ipv6_family};
6✔
1186
   std::unique_ptr<IPAddressBlocks> root_blocks = std::make_unique<IPAddressBlocks>(root_addr_blocks);
1✔
1187

1188
   // Inherit cert
1189
   auto inherit_ipv4_choice = IPAddressBlocks::IPAddressChoice<IPv4>();
1✔
1190
   auto inherit_ipv6_choice = IPAddressBlocks::IPAddressChoice<IPv6>();
1✔
1191

1192
   auto inherit_ipv4_family = IPAddressBlocks::IPAddressFamily(inherit_ipv4_choice, 42);
2✔
1193
   auto inherit_ipv6_family = IPAddressBlocks::IPAddressFamily(inherit_ipv6_choice);
2✔
1194

1195
   auto inherit_addr_blocks = {inherit_ipv4_family, inherit_ipv6_family};
6✔
1196
   std::unique_ptr<IPAddressBlocks> inherit_blocks = std::make_unique<IPAddressBlocks>(inherit_addr_blocks);
1✔
1197

1198
   // Dynamic Cert
1199
   a = {122, 0, 0, 255};
1✔
1200
   auto dyn_ipv4_range_1_min = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1201
   a = {128, 255, 255, 255};
1✔
1202
   auto dyn_ipv4_range_1_max = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1203
   a = {10, 0, 0, 255};
1✔
1204
   auto dyn_ipv4_range_2_min = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1205
   a = {10, 255, 0, 1};
1✔
1206
   auto dyn_ipv4_range_2_max = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1207

1208
   a = {0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1✔
1209
   auto dyn_ipv6_range_1_min = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1210
   a = {0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1✔
1211
   auto dyn_ipv6_range_1_max = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1212

1213
   auto dyn_ipv4_range_1 = IPAddressBlocks::IPAddressOrRange<IPv4>(dyn_ipv4_range_1_min, dyn_ipv4_range_1_max);
1✔
1214
   auto dyn_ipv4_range_2 = IPAddressBlocks::IPAddressOrRange<IPv4>(dyn_ipv4_range_2_min, dyn_ipv4_range_2_max);
1✔
1215
   auto dyn_ipv6_range = IPAddressBlocks::IPAddressOrRange<IPv6>(dyn_ipv6_range_1_min, dyn_ipv6_range_1_max);
1✔
1216

1217
   auto dyn_ipv4_ranges = {dyn_ipv4_range_1, dyn_ipv4_range_2};
1✔
1218
   auto dyn_ipv6_ranges = {dyn_ipv6_range};
1✔
1219

1220
   // Subject cert
1221
   a = {124, 0, 255, 0};
1✔
1222
   auto sub_ipv4_range_1_min = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1223
   a = {126, 0, 0, 1};
1✔
1224
   auto sub_ipv4_range_1_max = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1225

1226
   a = {10, 0, 2, 1};
1✔
1227
   auto sub_ipv4_range_2_min = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1228
   a = {10, 42, 0, 255};
1✔
1229
   auto sub_ipv4_range_2_max = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1230

1231
   a = {0x00, 0x00, 0x00, 0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1✔
1232
   auto sub_ipv6_range_1_min = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1233
   a = {0x0D, 0x00, 0x00, 0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1✔
1234
   auto sub_ipv6_range_1_max = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1235

1236
   auto sub_ipv4_range_1 = IPAddressBlocks::IPAddressOrRange<IPv4>(sub_ipv4_range_1_min, sub_ipv4_range_1_max);
1✔
1237
   auto sub_ipv4_range_2 = IPAddressBlocks::IPAddressOrRange<IPv4>(sub_ipv4_range_2_min, sub_ipv4_range_2_max);
1✔
1238
   auto sub_ipv6_range = IPAddressBlocks::IPAddressOrRange<IPv6>(sub_ipv6_range_1_min, sub_ipv6_range_1_max);
1✔
1239

1240
   auto sub_ipv4_ranges = {sub_ipv4_range_1, sub_ipv4_range_2};
1✔
1241
   auto sub_ipv6_ranges = {sub_ipv6_range};
1✔
1242

1243
   auto sub_ipv4_choice = IPAddressBlocks::IPAddressChoice<IPv4>(sub_ipv4_ranges);
1✔
1244
   auto sub_ipv6_choice = IPAddressBlocks::IPAddressChoice<IPv6>(sub_ipv6_ranges);
1✔
1245

1246
   auto sub_ipv4_family = IPAddressBlocks::IPAddressFamily(sub_ipv4_choice, 42);
3✔
1247
   auto sub_ipv6_family = IPAddressBlocks::IPAddressFamily(sub_ipv6_choice);
3✔
1248

1249
   auto sub_addr_blocks = {sub_ipv4_family, sub_ipv6_family};
6✔
1250
   std::unique_ptr<IPAddressBlocks> sub_blocks = std::make_unique<IPAddressBlocks>(sub_addr_blocks);
1✔
1251

1252
   Botan::X509_Cert_Options root_opts = ca_opts();
1✔
1253
   root_opts.extensions.add(std::move(root_blocks));
1✔
1254
   auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] = make_ca(rng, root_opts);
1✔
1255
   Botan::X509_Cert_Options sub_opts = req_opts(sig_algo);
1✔
1256
   sub_opts.extensions.add(std::move(sub_blocks));
1✔
1257
   auto [inherit_cert, inherit_ca] = make_and_sign_ca(std::move(inherit_blocks), root_ca, rng);
2✔
1258

1259
   Botan::Certificate_Store_In_Memory trusted;
1✔
1260
   trusted.add_certificate(root_cert);
1✔
1261

1262
   for(size_t i = 0; i < 4; i++) {
5✔
1263
      bool include_v4 = bit_set<0>(i);
4✔
1264
      bool include_v6 = bit_set<1>(i);
4✔
1265

1266
      auto dyn_ipv4_choice =
4✔
1267
         IPAddressBlocks::IPAddressChoice<IPv4>(include_v4 ? std::optional(dyn_ipv4_ranges) : std::nullopt);
8✔
1268
      auto dyn_ipv6_choice =
4✔
1269
         IPAddressBlocks::IPAddressChoice<IPv6>(include_v6 ? std::optional(dyn_ipv6_ranges) : std::nullopt);
8✔
1270

1271
      auto dyn_ipv4_family = IPAddressBlocks::IPAddressFamily(dyn_ipv4_choice, 42);
10✔
1272
      auto dyn_ipv6_family = IPAddressBlocks::IPAddressFamily(dyn_ipv6_choice);
10✔
1273

1274
      auto dyn_addr_blocks = {dyn_ipv4_family, dyn_ipv6_family};
24✔
1275
      std::unique_ptr<IPAddressBlocks> dyn_blocks = std::make_unique<IPAddressBlocks>(dyn_addr_blocks);
4✔
1276

1277
      auto [dyn_cert, dyn_ca] = make_and_sign_ca(std::move(dyn_blocks), inherit_ca, rng);
8✔
1278

1279
      Botan::PKCS10_Request sub_req = Botan::X509::create_cert_req(sub_opts, *sub_key, hash_fn, *rng);
4✔
1280
      Botan::X509_Certificate sub_cert =
4✔
1281
         dyn_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
4✔
1282

1283
      const Botan::Path_Validation_Restrictions restrictions(false, 80);
8✔
1284
      std::vector<Botan::X509_Certificate> certs = {sub_cert, dyn_cert, inherit_cert};
16✔
1285

1286
      Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
4✔
1287
      result.require("path validation succeeds", path_result.successful_validation());
4✔
1288
   }
28✔
1289

1290
   result.end_timer();
1✔
1291
   return result;
2✔
1292
}
24✔
1293

1294
Test::Result test_x509_ip_addr_blocks_path_validation_failure_builder() {
1✔
1295
   Test::Result result("X509 IP Address Blocks path validation failure (builder)");
1✔
1296
   result.start_timer();
1✔
1297

1298
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
1299
   auto rng = Test::new_rng(__func__);
1✔
1300

1301
   for(size_t i = 0; i < 7; i++) {
8✔
1302
      bool all_inherit = (i == 0);
7✔
1303
      bool different_safi = (i == 1);
7✔
1304
      bool too_small_subrange = (i == 2);
7✔
1305
      bool too_large_subrange = (i == 3);
7✔
1306
      bool no_more_issuer_ranges = (i == 4);
7✔
1307
      bool empty_issuer_ranges = (i == 5);
7✔
1308
      bool nullptr_extensions = (i == 6);
7✔
1309

1310
      // Root cert
1311
      std::unique_ptr<IPAddressBlocks> root_blocks = std::make_unique<IPAddressBlocks>();
7✔
1312
      if(!all_inherit) {
7✔
1313
         root_blocks->add_address<IPv4>({120, 0, 0, 1}, {130, 140, 150, 160}, 42);
6✔
1314
      } else {
1315
         root_blocks->inherit<IPv4>(42);
1✔
1316
      }
1317

1318
      Botan::X509_Cert_Options root_opts = ca_opts();
7✔
1319
      if(!nullptr_extensions) {
7✔
1320
         root_opts.extensions.add(std::move(root_blocks));
12✔
1321
      }
1322
      auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] = make_ca(rng, root_opts);
7✔
1323

1324
      // Issuer Cert
1325
      std::unique_ptr<IPAddressBlocks> iss_blocks = std::make_unique<IPAddressBlocks>();
7✔
1326
      if(!all_inherit) {
7✔
1327
         if(empty_issuer_ranges) {
6✔
1328
            iss_blocks->restrict<IPv4>(42);
1✔
1329
         } else {
1330
            iss_blocks->add_address<IPv4>({122, 0, 0, 255}, {128, 255, 255, 255}, 42);
5✔
1331
         }
1332
      } else {
1333
         iss_blocks->inherit<IPv4>(42);
1✔
1334
      }
1335

1336
      auto [iss_cert, iss_ca] = make_and_sign_ca(std::move(iss_blocks), root_ca, rng);
14✔
1337

1338
      // Subject cert
1339
      std::unique_ptr<IPAddressBlocks> sub_blocks = std::make_unique<IPAddressBlocks>();
7✔
1340

1341
      uint8_t safi = different_safi ? 41 : 42;
7✔
1342

1343
      if(!all_inherit) {
7✔
1344
         if(too_small_subrange) {
6✔
1345
            sub_blocks->add_address<IPv4>({118, 0, 255, 0}, {126, 0, 0, 1}, safi);
1✔
1346
         } else if(too_large_subrange) {
5✔
1347
            sub_blocks->add_address<IPv4>({124, 0, 255, 0}, {134, 0, 0, 1}, safi);
1✔
1348
         } else if(no_more_issuer_ranges) {
4✔
1349
            sub_blocks->add_address<IPv4>({140, 0, 0, 1}, {150, 0, 0, 1}, safi);
1✔
1350
         } else {
1351
            sub_blocks->add_address<IPv4>({124, 0, 255, 0}, {126, 0, 0, 1}, safi);
3✔
1352
         }
1353
      } else {
1354
         sub_blocks->inherit<IPv4>(safi);
1✔
1355
      }
1356

1357
      Botan::X509_Cert_Options sub_opts = req_opts(sig_algo);
7✔
1358
      sub_opts.extensions.add(std::move(sub_blocks));
7✔
1359

1360
      Botan::PKCS10_Request sub_req = Botan::X509::create_cert_req(sub_opts, *sub_key, hash_fn, *rng);
7✔
1361
      Botan::X509_Certificate sub_cert =
7✔
1362
         iss_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
7✔
1363

1364
      const Botan::Path_Validation_Restrictions restrictions(false, 80);
14✔
1365
      std::vector<Botan::X509_Certificate> certs = {sub_cert, iss_cert};
21✔
1366

1367
      Botan::Certificate_Store_In_Memory trusted;
7✔
1368
      trusted.add_certificate(root_cert);
7✔
1369

1370
      Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
7✔
1371
      result.require("path validation fails", !path_result.successful_validation());
7✔
1372
   }
15✔
1373

1374
   result.end_timer();
1✔
1375
   return result;
1✔
1376
}
8✔
1377

1378
Test::Result test_x509_ip_addr_blocks_path_validation_failure_ctor() {
1✔
1379
   Test::Result result("X509 IP Address Block path validation failure (ctor)");
1✔
1380
   result.start_timer();
1✔
1381

1382
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
1383
   auto rng = Test::new_rng(__func__);
1✔
1384

1385
   for(size_t i = 0; i < 7; i++) {
8✔
1386
      bool all_inherit = (i == 0);
7✔
1387
      bool different_safi = (i == 1);
7✔
1388
      bool too_small_subrange = (i == 2);
7✔
1389
      bool too_large_subrange = (i == 3);
7✔
1390
      bool no_more_issuer_ranges = (i == 4);
7✔
1391
      bool empty_issuer_ranges = (i == 5);
7✔
1392
      bool nullptr_extensions = (i == 6);
7✔
1393

1394
      // Root cert
1395
      std::vector<uint8_t> a = {120, 0, 0, 1};
7✔
1396
      auto root_range_1_min = IPAddressBlocks::IPAddress<IPv4>{a};
7✔
1397
      a = {130, 140, 150, 160};
7✔
1398
      auto root_range_1_max = IPAddressBlocks::IPAddress<IPv4>{a};
7✔
1399

1400
      auto root_range_1 = IPAddressBlocks::IPAddressOrRange<IPv4>(root_range_1_min, root_range_1_max);
7✔
1401
      auto root_ranges = {root_range_1};
7✔
1402
      auto root_choice =
7✔
1403
         IPAddressBlocks::IPAddressChoice<IPv4>(all_inherit ? std::nullopt : std::optional(root_ranges));
14✔
1404
      auto root_family = IPAddressBlocks::IPAddressFamily(root_choice, 42);
20✔
1405
      auto root_addr_blocks = {root_family};
21✔
1406
      std::unique_ptr<IPAddressBlocks> root_blocks = std::make_unique<IPAddressBlocks>(root_addr_blocks);
7✔
1407

1408
      Botan::X509_Cert_Options root_opts = ca_opts();
7✔
1409
      if(!nullptr_extensions) {
7✔
1410
         root_opts.extensions.add(std::move(root_blocks));
12✔
1411
      }
1412
      auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] = make_ca(rng, root_opts);
7✔
1413

1414
      // Issuer Cert
1415
      a = {122, 0, 0, 255};
7✔
1416
      auto iss_range_1_min = IPAddressBlocks::IPAddress<IPv4>(a);
7✔
1417
      a = {128, 255, 255, 255};
7✔
1418
      auto iss_range_1_max = IPAddressBlocks::IPAddress<IPv4>(a);
7✔
1419
      auto iss_range_1 = IPAddressBlocks::IPAddressOrRange<IPv4>(iss_range_1_min, iss_range_1_max);
7✔
1420

1421
      std::vector<IPAddressBlocks::IPAddressOrRange<IPv4>> iss_ranges;
7✔
1422

1423
      if(!empty_issuer_ranges) {
7✔
1424
         iss_ranges.push_back(iss_range_1);
6✔
1425
      }
1426

1427
      auto iss_choice = IPAddressBlocks::IPAddressChoice<IPv4>(all_inherit ? std::nullopt : std::optional(iss_ranges));
19✔
1428
      auto iss_family = IPAddressBlocks::IPAddressFamily(iss_choice, 42);
20✔
1429
      auto iss_addr_blocks = {iss_family};
21✔
1430
      std::unique_ptr<IPAddressBlocks> iss_blocks = std::make_unique<IPAddressBlocks>(iss_addr_blocks);
7✔
1431
      auto [iss_cert, iss_ca] = make_and_sign_ca(std::move(iss_blocks), root_ca, rng);
14✔
1432

1433
      // Subject cert
1434
      if(too_small_subrange) {
7✔
1435
         a = {118, 0, 255, 0};
2✔
1436
      } else if(no_more_issuer_ranges) {
6✔
1437
         a = {140, 0, 0, 1};
2✔
1438
      } else {
1439
         a = {124, 0, 255, 0};
10✔
1440
      }
1441

1442
      auto sub_range_1_min = IPAddressBlocks::IPAddress<IPv4>(a);
7✔
1443
      if(too_large_subrange) {
7✔
1444
         a = {134, 0, 0, 1};
2✔
1445
      } else if(no_more_issuer_ranges) {
6✔
1446
         a = {150, 0, 0, 1};
2✔
1447
      } else {
1448
         a = {126, 0, 0, 1};
10✔
1449
      }
1450
      auto sub_range_1_max = IPAddressBlocks::IPAddress<IPv4>(a);
7✔
1451

1452
      auto sub_range_1 = IPAddressBlocks::IPAddressOrRange<IPv4>(sub_range_1_min, sub_range_1_max);
7✔
1453
      auto sub_ranges = {sub_range_1};
7✔
1454
      auto sub_choice = IPAddressBlocks::IPAddressChoice<IPv4>(all_inherit ? std::nullopt : std::optional(sub_ranges));
14✔
1455
      auto sub_family = IPAddressBlocks::IPAddressFamily(sub_choice, different_safi ? 41 : 42);
26✔
1456

1457
      auto sub_addr_blocks = {sub_family};
21✔
1458
      std::unique_ptr<IPAddressBlocks> sub_blocks = std::make_unique<IPAddressBlocks>(sub_addr_blocks);
7✔
1459

1460
      Botan::X509_Cert_Options sub_opts = req_opts(sig_algo);
7✔
1461
      sub_opts.extensions.add(std::move(sub_blocks));
7✔
1462

1463
      Botan::PKCS10_Request sub_req = Botan::X509::create_cert_req(sub_opts, *sub_key, hash_fn, *rng);
7✔
1464
      Botan::X509_Certificate sub_cert =
7✔
1465
         iss_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
7✔
1466

1467
      const Botan::Path_Validation_Restrictions restrictions(false, 80);
14✔
1468
      std::vector<Botan::X509_Certificate> certs = {sub_cert, iss_cert};
21✔
1469

1470
      Botan::Certificate_Store_In_Memory trusted;
7✔
1471
      trusted.add_certificate(root_cert);
7✔
1472

1473
      Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
7✔
1474
      result.require("path validation fails", !path_result.successful_validation());
7✔
1475
   }
82✔
1476

1477
   result.end_timer();
1✔
1478
   return result;
1✔
1479
}
8✔
1480

1481
Test::Result test_x509_as_blocks_rfc3779_example() {
1✔
1482
   Test::Result result("X509 AS Blocks rfc3779 example");
1✔
1483
   result.start_timer();
1✔
1484

1485
   using Botan::Cert_Extension::ASBlocks;
1✔
1486
   auto rng = Test::new_rng(__func__);
1✔
1487

1488
   // construct like in https://datatracker.ietf.org/doc/html/rfc3779#page-21
1489
   std::unique_ptr<ASBlocks> blocks = std::make_unique<ASBlocks>();
1✔
1490
   blocks->add_asnum(135);
1✔
1491
   blocks->add_asnum(3000, 3999);
1✔
1492
   blocks->add_asnum(5001);
1✔
1493
   blocks->inherit_rdi();
1✔
1494

1495
   Botan::X509_Cert_Options opts = ca_opts();
1✔
1496
   opts.extensions.add(std::move(blocks));
1✔
1497

1498
   auto cert = make_self_signed(rng, opts);
1✔
1499
   auto bits = cert.v3_extensions().get_extension_bits(ASBlocks::static_oid());
1✔
1500

1501
   result.test_eq(
1✔
1502
      "extension is encoded as specified", bits, "301AA014301202020087300802020BB802020F9F02021389A1020500");
1503

1504
   auto as_idents = cert.v3_extensions().get_extension_object_as<ASBlocks>()->as_identifiers();
1✔
1505
   auto as_ids = as_idents.asnum().value().ranges().value();
1✔
1506

1507
   result.confirm("extension has correct data", as_ids[0].min() == 135);
2✔
1508

1509
   result.end_timer();
1✔
1510
   return result;
2✔
1511
}
3✔
1512

1513
Test::Result test_x509_as_blocks_encode_builder() {
1✔
1514
   Test::Result result("X509 IP Address Blocks encode (builder)");
1✔
1515
   result.start_timer();
1✔
1516

1517
   using Botan::Cert_Extension::ASBlocks;
1✔
1518
   auto rng = Test::new_rng(__func__);
1✔
1519

1520
   std::unique_ptr<ASBlocks> blocks = std::make_unique<ASBlocks>();
1✔
1521

1522
   blocks->add_rdi(10);
1✔
1523
   blocks->add_rdi(20, 30);
1✔
1524
   blocks->add_rdi(42, 300);
1✔
1525
   blocks->add_rdi(9, 301);
1✔
1526

1527
   blocks->inherit_asnum();
1✔
1528
   blocks->add_asnum(20);
1✔
1529
   // this overwrites the previous two
1530
   blocks->restrict_asnum();
1✔
1531

1532
   Botan::X509_Cert_Options opts = ca_opts();
1✔
1533
   opts.extensions.add(std::move(blocks));
1✔
1534

1535
   auto cert = make_self_signed(rng, opts);
1✔
1536
   auto bits = cert.v3_extensions().get_extension_bits(ASBlocks::static_oid());
1✔
1537

1538
   result.test_eq("extension is encoded as specified", bits, "3011A0023000A10B300930070201090202012D");
1✔
1539

1540
   result.end_timer();
1✔
1541
   return result;
1✔
1542
}
2✔
1543

1544
Test::Result test_x509_as_blocks_extension_encode_ctor() {
1✔
1545
   Test::Result result("X509 AS Blocks encode (ctor)");
1✔
1546
   result.start_timer();
1✔
1547

1548
   using Botan::Cert_Extension::ASBlocks;
1✔
1549

1550
   auto rng = Test::new_rng(__func__);
1✔
1551

1552
   auto [ca_cert, ca, sub_key, sig_algo, hash_fn] = make_ca(rng);
1✔
1553

1554
   for(size_t i = 0; i < 16; i++) {
17✔
1555
      bool push_asnum = bit_set<0>(i);
16✔
1556
      bool push_rdi = bit_set<1>(i);
16✔
1557
      bool include_asnum = bit_set<2>(i);
16✔
1558
      bool include_rdi = bit_set<3>(i);
16✔
1559
      if(!include_asnum && !include_rdi) {
16✔
1560
         continue;
4✔
1561
      }
1562

1563
      ASBlocks::ASIdOrRange asnum_id_or_range0 = ASBlocks::ASIdOrRange(0, 999);
12✔
1564
      ASBlocks::ASIdOrRange asnum_id_or_range1 = ASBlocks::ASIdOrRange(5042);
12✔
1565
      ASBlocks::ASIdOrRange asnum_id_or_range2 = ASBlocks::ASIdOrRange(5043, 4294967295);
12✔
1566

1567
      ASBlocks::ASIdOrRange rdi_id_or_range0 = ASBlocks::ASIdOrRange(1234, 5678);
12✔
1568
      ASBlocks::ASIdOrRange rdi_id_or_range1 = ASBlocks::ASIdOrRange(32768);
12✔
1569
      ASBlocks::ASIdOrRange rdi_id_or_range2 = ASBlocks::ASIdOrRange(32769, 4294967295);
12✔
1570

1571
      std::vector<ASBlocks::ASIdOrRange> as_ranges;
12✔
1572
      if(push_asnum) {
12✔
1573
         as_ranges.push_back(asnum_id_or_range0);
6✔
1574
         as_ranges.push_back(asnum_id_or_range1);
6✔
1575
         as_ranges.push_back(asnum_id_or_range2);
6✔
1576
      }
1577

1578
      std::vector<ASBlocks::ASIdOrRange> rdi_ranges;
12✔
1579
      if(push_rdi) {
12✔
1580
         rdi_ranges.push_back(rdi_id_or_range0);
6✔
1581
         rdi_ranges.push_back(rdi_id_or_range1);
6✔
1582
         rdi_ranges.push_back(rdi_id_or_range2);
6✔
1583
      }
1584

1585
      ASBlocks::ASIdentifierChoice asnum = ASBlocks::ASIdentifierChoice(as_ranges);
12✔
1586
      ASBlocks::ASIdentifierChoice rdi = ASBlocks::ASIdentifierChoice(rdi_ranges);
12✔
1587

1588
      ASBlocks::ASIdentifiers ident = ASBlocks::ASIdentifiers(include_asnum ? std::optional(asnum) : std::nullopt,
32✔
1589
                                                              include_rdi ? std::optional(rdi) : std::nullopt);
32✔
1590

1591
      std::unique_ptr<ASBlocks> blocks = std::make_unique<ASBlocks>(ident);
12✔
1592

1593
      Botan::X509_Cert_Options opts = req_opts(sig_algo);
12✔
1594
      opts.extensions.add(std::move(blocks));
12✔
1595

1596
      Botan::PKCS10_Request req = Botan::X509::create_cert_req(opts, *sub_key, hash_fn, *rng);
12✔
1597
      Botan::X509_Certificate cert = ca.sign_request(req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
12✔
1598

1599
      {
12✔
1600
         const auto* as_blocks = cert.v3_extensions().get_extension_object_as<ASBlocks>();
12✔
1601
         result.confirm("cert has ASBlock extension", as_blocks != nullptr, true);
24✔
1602

1603
         const auto& identifier = as_blocks->as_identifiers();
12✔
1604

1605
         if(include_asnum) {
12✔
1606
            const auto& asnum_entries = identifier.asnum().value().ranges().value();
8✔
1607

1608
            if(push_asnum) {
8✔
1609
               result.confirm("asnum entry 0 min", asnum_entries[0].min() == 0, true);
8✔
1610
               result.confirm("asnum entry 0 max", asnum_entries[0].max() == 999, true);
8✔
1611

1612
               result.confirm("asnum entry 1 min", asnum_entries[1].min() == 5042, true);
8✔
1613
               result.confirm("asnum entry 1 max", asnum_entries[1].max() == 4294967295, true);
8✔
1614
            } else {
1615
               result.confirm("asnum has no entries", asnum_entries.empty(), true);
8✔
1616
            }
1617
         } else {
1618
            result.confirm("no asnum entry", identifier.asnum().has_value(), false);
8✔
1619
         }
1620

1621
         if(include_rdi) {
12✔
1622
            const auto& rdi_entries = identifier.rdi().value().ranges().value();
8✔
1623

1624
            if(push_rdi) {
8✔
1625
               result.confirm("rdi entry 0 min", rdi_entries[0].min() == 1234, true);
8✔
1626
               result.confirm("rdi entry 0 max", rdi_entries[0].max() == 5678, true);
8✔
1627

1628
               result.confirm("rdi entry 1 min", rdi_entries[1].min() == 32768, true);
8✔
1629
               result.confirm("rdi entry 1 max", rdi_entries[1].max() == 4294967295, true);
8✔
1630
            } else {
1631
               result.confirm("rdi has no entries", rdi_entries.empty(), true);
8✔
1632
            }
1633
         } else {
1634
            result.confirm("rdi has no entry", identifier.rdi().has_value(), false);
8✔
1635
         }
1636
      }
1637
   }
36✔
1638

1639
   result.end_timer();
1✔
1640
   return result;
2✔
1641
}
2✔
1642

1643
Test::Result test_x509_as_blocks_range_merge() {
1✔
1644
   Test::Result result("X509 AS Block range merge");
1✔
1645
   result.start_timer();
1✔
1646

1647
   using Botan::Cert_Extension::ASBlocks;
1✔
1648

1649
   auto rng = Test::new_rng(__func__);
1✔
1650

1651
   auto [ca_cert, ca, sub_key, sig_algo, hash_fn] = make_ca(rng);
1✔
1652
   Botan::X509_Cert_Options opts = req_opts(sig_algo);
1✔
1653

1654
   std::vector<std::vector<uint16_t>> ranges = {
1✔
1655
      {2005, 37005},
1656
      {60, 70},
1657
      {22, 50},
1658
      {35, 2000},
1659
      {2001, 2004},
1660
      {21, 21},
1661
      {0, 20},
1662
   };
9✔
1663

1664
   std::vector<ASBlocks::ASIdOrRange> as_ranges;
1✔
1665
   for(auto pair : ranges) {
8✔
1666
      auto range = ASBlocks::ASIdOrRange(pair[0], pair[1]);
7✔
1667
      as_ranges.push_back(range);
7✔
1668
   }
7✔
1669

1670
   ASBlocks::ASIdentifierChoice asnum = ASBlocks::ASIdentifierChoice(as_ranges);
1✔
1671

1672
   ASBlocks::ASIdentifiers ident = ASBlocks::ASIdentifiers(std::optional(asnum), std::nullopt);
3✔
1673

1674
   std::unique_ptr<ASBlocks> blocks = std::make_unique<ASBlocks>(ident);
1✔
1675

1676
   opts.extensions.add(std::move(blocks));
1✔
1677

1678
   Botan::PKCS10_Request req = Botan::X509::create_cert_req(opts, *sub_key, hash_fn, *rng);
1✔
1679
   Botan::X509_Certificate cert = ca.sign_request(req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
1✔
1680
   {
1✔
1681
      const auto* as_blocks = cert.v3_extensions().get_extension_object_as<ASBlocks>();
1✔
1682
      result.confirm("cert has ASBlock extension", as_blocks != nullptr, true);
2✔
1683

1684
      const auto& identifier = as_blocks->as_identifiers();
1✔
1685

1686
      const auto& asnum_entries = identifier.asnum().value().ranges().value();
1✔
1687

1688
      result.confirm("asnum entry 0 min", asnum_entries[0].min() == 0, true);
2✔
1689
      result.confirm("asnum entry 0 max", asnum_entries[0].max() == 37005, true);
2✔
1690
      result.confirm("asnum length", asnum_entries.size() == 1, true);
2✔
1691
   }
1692

1693
   result.end_timer();
1✔
1694
   return result;
2✔
1695
}
5✔
1696

1697
Test::Result test_x509_as_blocks_path_validation_success_builder() {
1✔
1698
   Test::Result result("X509 AS Block path validation success (builder)");
1✔
1699
   result.start_timer();
1✔
1700

1701
   using Botan::Cert_Extension::ASBlocks;
1✔
1702
   auto rng = Test::new_rng(__func__);
1✔
1703

1704
   /*
1705
   Creates a certificate chain of length 4.
1706
   Root: both asnum and rdi
1707
   Inherit: has both values as 'inherit'
1708
   Dynamic: has either both 'inherit', both with values, or just one with a value
1709
   Subject: both asnum and rdi as a subset of Root / Dynamic
1710
   */
1711

1712
   // Root Cert, both as and rdi
1713

1714
   std::unique_ptr<ASBlocks> root_blocks = std::make_unique<ASBlocks>();
1✔
1715

1716
   root_blocks->add_asnum(0, 999);
1✔
1717
   root_blocks->add_asnum(5042);
1✔
1718
   root_blocks->add_asnum(5043, 4294967295);
1✔
1719

1720
   root_blocks->add_rdi(1234, 5678);
1✔
1721
   root_blocks->add_rdi(32768);
1✔
1722
   root_blocks->add_rdi(32769, 4294967295);
1✔
1723

1724
   // Inherit cert, both as 'inherit'
1725
   std::unique_ptr<ASBlocks> inherit_blocks = std::make_unique<ASBlocks>();
1✔
1726
   inherit_blocks->inherit_asnum();
1✔
1727
   inherit_blocks->inherit_rdi();
1✔
1728

1729
   // Subject cert
1730

1731
   std::unique_ptr<ASBlocks> sub_blocks = std::make_unique<ASBlocks>();
1✔
1732

1733
   sub_blocks->add_asnum(120, 180);
1✔
1734
   sub_blocks->add_asnum(220, 240);
1✔
1735
   sub_blocks->add_asnum(260, 511);
1✔
1736
   sub_blocks->add_asnum(678);
1✔
1737
   sub_blocks->add_asnum(5043, 5100);
1✔
1738

1739
   sub_blocks->add_rdi(1500, 2300);
1✔
1740
   sub_blocks->add_rdi(2500, 4000);
1✔
1741
   sub_blocks->add_rdi(1567);
1✔
1742
   sub_blocks->add_rdi(33100, 40000);
1✔
1743

1744
   Botan::X509_Cert_Options root_opts = ca_opts();
1✔
1745
   root_opts.extensions.add(std::move(root_blocks));
1✔
1746
   auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] = make_ca(rng, root_opts);
1✔
1747
   Botan::X509_Cert_Options sub_opts = req_opts(sig_algo);
1✔
1748
   sub_opts.extensions.add(std::move(sub_blocks));
1✔
1749
   auto [inherit_cert, inherit_ca] = make_and_sign_ca(std::move(inherit_blocks), root_ca, rng);
2✔
1750

1751
   Botan::Certificate_Store_In_Memory trusted;
1✔
1752
   trusted.add_certificate(root_cert);
1✔
1753

1754
   for(size_t i = 0; i < 4; i++) {
5✔
1755
      bool include_asnum = bit_set<0>(i);
4✔
1756
      bool include_rdi = bit_set<1>(i);
4✔
1757

1758
      std::unique_ptr<ASBlocks> dyn_blocks = std::make_unique<ASBlocks>();
4✔
1759
      if(include_asnum) {
4✔
1760
         dyn_blocks->add_asnum(100, 600);
2✔
1761
         dyn_blocks->add_asnum(678);
2✔
1762
         dyn_blocks->add_asnum(5042, 5101);
2✔
1763
      } else {
1764
         dyn_blocks->inherit_asnum();
2✔
1765
      }
1766

1767
      if(include_rdi) {
4✔
1768
         dyn_blocks->add_rdi(1500, 5000);
2✔
1769
         dyn_blocks->add_rdi(33000, 60000);
2✔
1770
      } else {
1771
         dyn_blocks->inherit_rdi();
2✔
1772
      }
1773

1774
      auto [dyn_cert, dyn_ca] = make_and_sign_ca(std::move(dyn_blocks), inherit_ca, rng);
8✔
1775

1776
      Botan::PKCS10_Request sub_req = Botan::X509::create_cert_req(sub_opts, *sub_key, hash_fn, *rng);
4✔
1777
      Botan::X509_Certificate sub_cert =
4✔
1778
         dyn_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
4✔
1779

1780
      const Botan::Path_Validation_Restrictions restrictions(false, 80);
8✔
1781
      std::vector<Botan::X509_Certificate> certs = {sub_cert, dyn_cert, inherit_cert};
16✔
1782

1783
      Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
4✔
1784
      result.require("path validation succeeds", path_result.successful_validation());
4✔
1785
   }
8✔
1786

1787
   result.end_timer();
1✔
1788
   return result;
2✔
1789
}
7✔
1790

1791
Test::Result test_x509_as_blocks_path_validation_success_ctor() {
1✔
1792
   Test::Result result("X509 AS Block path validation success (ctor)");
1✔
1793
   result.start_timer();
1✔
1794

1795
   using Botan::Cert_Extension::ASBlocks;
1✔
1796
   auto rng = Test::new_rng(__func__);
1✔
1797

1798
   /*
1799
   Creates a certificate chain of length 4.
1800
   Root: both asnum and rdi
1801
   Inherit: has both values as 'inherit'
1802
   Dynamic: has either both 'inherit', both with values, or just one with a value
1803
   Subject: both asnum and rdi as a subset of Root / Dynamic
1804
   */
1805

1806
   // Root Cert, both as and rdi
1807
   ASBlocks::ASIdOrRange root_asnum_id_or_range0 = ASBlocks::ASIdOrRange(0, 999);
1✔
1808
   ASBlocks::ASIdOrRange root_asnum_id_or_range1 = ASBlocks::ASIdOrRange(5042);
1✔
1809
   ASBlocks::ASIdOrRange root_asnum_id_or_range2 = ASBlocks::ASIdOrRange(5043, 4294967295);
1✔
1810

1811
   ASBlocks::ASIdOrRange root_rdi_id_or_range0 = ASBlocks::ASIdOrRange(1234, 5678);
1✔
1812
   ASBlocks::ASIdOrRange root_rdi_id_or_range1 = ASBlocks::ASIdOrRange(32768);
1✔
1813
   ASBlocks::ASIdOrRange root_rdi_id_or_range2 = ASBlocks::ASIdOrRange(32769, 4294967295);
1✔
1814

1815
   std::vector<ASBlocks::ASIdOrRange> root_as_ranges;
1✔
1816
   root_as_ranges.push_back(root_asnum_id_or_range0);
1✔
1817
   root_as_ranges.push_back(root_asnum_id_or_range1);
1✔
1818
   root_as_ranges.push_back(root_asnum_id_or_range2);
1✔
1819

1820
   std::vector<ASBlocks::ASIdOrRange> root_rdi_ranges;
1✔
1821
   root_rdi_ranges.push_back(root_rdi_id_or_range0);
1✔
1822
   root_rdi_ranges.push_back(root_rdi_id_or_range1);
1✔
1823
   root_rdi_ranges.push_back(root_rdi_id_or_range2);
1✔
1824

1825
   ASBlocks::ASIdentifierChoice root_asnum = ASBlocks::ASIdentifierChoice(root_as_ranges);
1✔
1826
   ASBlocks::ASIdentifierChoice root_rdi = ASBlocks::ASIdentifierChoice(root_rdi_ranges);
1✔
1827
   ASBlocks::ASIdentifiers root_ident = ASBlocks::ASIdentifiers(root_asnum, root_rdi);
4✔
1828
   std::unique_ptr<ASBlocks> root_blocks = std::make_unique<ASBlocks>(root_ident);
1✔
1829

1830
   // Inherit cert, both as 'inherit'
1831
   ASBlocks::ASIdentifierChoice inherit_asnum = ASBlocks::ASIdentifierChoice();
1✔
1832
   ASBlocks::ASIdentifierChoice inherit_rdi = ASBlocks::ASIdentifierChoice();
1✔
1833
   ASBlocks::ASIdentifiers inherit_ident = ASBlocks::ASIdentifiers(inherit_asnum, inherit_rdi);
2✔
1834
   std::unique_ptr<ASBlocks> inherit_blocks = std::make_unique<ASBlocks>(inherit_ident);
1✔
1835

1836
   // Dynamic cert
1837
   ASBlocks::ASIdOrRange dyn_asnum_id_or_range0 = ASBlocks::ASIdOrRange(100, 600);
1✔
1838
   ASBlocks::ASIdOrRange dyn_asnum_id_or_range1 = ASBlocks::ASIdOrRange(678);
1✔
1839
   ASBlocks::ASIdOrRange dyn_asnum_id_or_range2 = ASBlocks::ASIdOrRange(5042, 5101);
1✔
1840

1841
   ASBlocks::ASIdOrRange dyn_rdi_id_or_range0 = ASBlocks::ASIdOrRange(1500, 5000);
1✔
1842
   ASBlocks::ASIdOrRange dyn_rdi_id_or_range1 = ASBlocks::ASIdOrRange(33000, 60000);
1✔
1843

1844
   std::vector<ASBlocks::ASIdOrRange> dyn_as_ranges;
1✔
1845
   dyn_as_ranges.push_back(dyn_asnum_id_or_range0);
1✔
1846
   dyn_as_ranges.push_back(dyn_asnum_id_or_range1);
1✔
1847
   dyn_as_ranges.push_back(dyn_asnum_id_or_range2);
1✔
1848

1849
   std::vector<ASBlocks::ASIdOrRange> dyn_rdi_ranges;
1✔
1850
   dyn_rdi_ranges.push_back(dyn_rdi_id_or_range0);
1✔
1851
   dyn_rdi_ranges.push_back(dyn_rdi_id_or_range1);
1✔
1852

1853
   // Subject cert
1854
   ASBlocks::ASIdOrRange sub_asnum_id_or_range0 = ASBlocks::ASIdOrRange(120, 180);
1✔
1855
   ASBlocks::ASIdOrRange sub_asnum_id_or_range1 = ASBlocks::ASIdOrRange(220, 240);
1✔
1856
   ASBlocks::ASIdOrRange sub_asnum_id_or_range2 = ASBlocks::ASIdOrRange(260, 511);
1✔
1857
   ASBlocks::ASIdOrRange sub_asnum_id_or_range3 = ASBlocks::ASIdOrRange(678);
1✔
1858
   ASBlocks::ASIdOrRange sub_asnum_id_or_range4 = ASBlocks::ASIdOrRange(5043, 5100);
1✔
1859

1860
   ASBlocks::ASIdOrRange sub_rdi_id_or_range0 = ASBlocks::ASIdOrRange(1500, 2300);
1✔
1861
   ASBlocks::ASIdOrRange sub_rdi_id_or_range1 = ASBlocks::ASIdOrRange(2500, 4000);
1✔
1862
   ASBlocks::ASIdOrRange sub_rdi_id_or_range2 = ASBlocks::ASIdOrRange(1567);
1✔
1863
   ASBlocks::ASIdOrRange sub_rdi_id_or_range3 = ASBlocks::ASIdOrRange(33100, 40000);
1✔
1864

1865
   std::vector<ASBlocks::ASIdOrRange> sub_as_ranges;
1✔
1866
   sub_as_ranges.push_back(sub_asnum_id_or_range0);
1✔
1867
   sub_as_ranges.push_back(sub_asnum_id_or_range1);
1✔
1868
   sub_as_ranges.push_back(sub_asnum_id_or_range2);
1✔
1869
   sub_as_ranges.push_back(sub_asnum_id_or_range3);
1✔
1870
   sub_as_ranges.push_back(sub_asnum_id_or_range4);
1✔
1871

1872
   std::vector<ASBlocks::ASIdOrRange> sub_rdi_ranges;
1✔
1873
   sub_rdi_ranges.push_back(sub_rdi_id_or_range0);
1✔
1874
   sub_rdi_ranges.push_back(sub_rdi_id_or_range1);
1✔
1875
   sub_rdi_ranges.push_back(sub_rdi_id_or_range2);
1✔
1876
   sub_rdi_ranges.push_back(sub_rdi_id_or_range3);
1✔
1877

1878
   ASBlocks::ASIdentifierChoice sub_asnum = ASBlocks::ASIdentifierChoice(sub_as_ranges);
1✔
1879
   ASBlocks::ASIdentifierChoice sub_rdi = ASBlocks::ASIdentifierChoice(sub_rdi_ranges);
1✔
1880
   ASBlocks::ASIdentifiers sub_ident = ASBlocks::ASIdentifiers(sub_asnum, sub_rdi);
4✔
1881
   std::unique_ptr<ASBlocks> sub_blocks = std::make_unique<ASBlocks>(sub_ident);
1✔
1882

1883
   Botan::X509_Cert_Options root_opts = ca_opts();
1✔
1884
   root_opts.extensions.add(std::move(root_blocks));
1✔
1885
   auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] = make_ca(rng, root_opts);
1✔
1886
   Botan::X509_Cert_Options sub_opts = req_opts(sig_algo);
1✔
1887
   sub_opts.extensions.add(std::move(sub_blocks));
1✔
1888
   auto [inherit_cert, inherit_ca] = make_and_sign_ca(std::move(inherit_blocks), root_ca, rng);
2✔
1889

1890
   Botan::Certificate_Store_In_Memory trusted;
1✔
1891
   trusted.add_certificate(root_cert);
1✔
1892

1893
   for(size_t i = 0; i < 4; i++) {
5✔
1894
      bool include_asnum = bit_set<0>(i);
4✔
1895
      bool include_rdi = bit_set<1>(i);
4✔
1896

1897
      ASBlocks::ASIdentifierChoice dyn_asnum =
4✔
1898
         ASBlocks::ASIdentifierChoice(include_asnum ? std::optional(dyn_as_ranges) : std::nullopt);
6✔
1899
      ASBlocks::ASIdentifierChoice dyn_rdi =
4✔
1900
         ASBlocks::ASIdentifierChoice(include_rdi ? std::optional(dyn_rdi_ranges) : std::nullopt);
6✔
1901
      ASBlocks::ASIdentifiers dyn_ident = ASBlocks::ASIdentifiers(dyn_asnum, dyn_rdi);
12✔
1902
      std::unique_ptr<ASBlocks> dyn_blocks = std::make_unique<ASBlocks>(dyn_ident);
4✔
1903

1904
      auto [dyn_cert, dyn_ca] = make_and_sign_ca(std::move(dyn_blocks), inherit_ca, rng);
8✔
1905

1906
      Botan::PKCS10_Request sub_req = Botan::X509::create_cert_req(sub_opts, *sub_key, hash_fn, *rng);
4✔
1907
      Botan::X509_Certificate sub_cert =
4✔
1908
         dyn_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
4✔
1909

1910
      const Botan::Path_Validation_Restrictions restrictions(false, 80);
8✔
1911
      std::vector<Botan::X509_Certificate> certs = {sub_cert, dyn_cert, inherit_cert};
16✔
1912

1913
      Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
4✔
1914
      result.require("path validation succeeds", path_result.successful_validation());
4✔
1915
   }
12✔
1916

1917
   result.end_timer();
1✔
1918
   return result;
2✔
1919
}
11✔
1920

1921
Test::Result test_x509_as_blocks_path_validation_extension_not_present_builder() {
1✔
1922
   Test::Result result("X509 AS Block path validation extension not present (builder)");
1✔
1923
   result.start_timer();
1✔
1924

1925
   using Botan::Cert_Extension::ASBlocks;
1✔
1926
   auto rng = Test::new_rng(__func__);
1✔
1927

1928
   std::unique_ptr<ASBlocks> sub_blocks = std::make_unique<ASBlocks>();
1✔
1929
   sub_blocks->add_asnum(120, 180);
1✔
1930
   sub_blocks->add_asnum(220, 224);
1✔
1931
   sub_blocks->add_asnum(260, 511);
1✔
1932
   sub_blocks->add_asnum(678);
1✔
1933
   sub_blocks->add_asnum(5043, 5100);
1✔
1934

1935
   sub_blocks->add_rdi(1500, 2300);
1✔
1936
   sub_blocks->add_rdi(2500, 4000);
1✔
1937
   sub_blocks->add_rdi(1567);
1✔
1938
   sub_blocks->add_rdi(33100, 40000);
1✔
1939

1940
   // create a root ca that does not have any extension
1941
   Botan::X509_Cert_Options root_opts = ca_opts();
1✔
1942
   auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] = make_ca(rng, root_opts);
1✔
1943
   Botan::X509_Cert_Options sub_opts = req_opts(sig_algo);
1✔
1944
   sub_opts.extensions.add(std::move(sub_blocks));
1✔
1945
   Botan::PKCS10_Request sub_req = Botan::X509::create_cert_req(sub_opts, *sub_key, hash_fn, *rng);
1✔
1946
   Botan::X509_Certificate sub_cert = root_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
1✔
1947

1948
   Botan::Certificate_Store_In_Memory trusted;
1✔
1949
   trusted.add_certificate(root_cert);
1✔
1950

1951
   const Botan::Path_Validation_Restrictions restrictions(false, 80);
2✔
1952
   const std::vector<Botan::X509_Certificate> certs = {sub_cert};
2✔
1953

1954
   Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
1✔
1955
   result.require("path validation fails", !path_result.successful_validation());
1✔
1956

1957
   result.end_timer();
1✔
1958
   return result;
2✔
1959
}
3✔
1960

1961
Test::Result test_x509_as_blocks_path_validation_extension_not_present_ctor() {
1✔
1962
   Test::Result result("X509 AS Block path validation extension not present (ctor)");
1✔
1963
   result.start_timer();
1✔
1964

1965
   using Botan::Cert_Extension::ASBlocks;
1✔
1966
   auto rng = Test::new_rng(__func__);
1✔
1967

1968
   // Subject cert
1969
   ASBlocks::ASIdOrRange sub_asnum_id_or_range0 = ASBlocks::ASIdOrRange(120, 180);
1✔
1970
   ASBlocks::ASIdOrRange sub_asnum_id_or_range1 = ASBlocks::ASIdOrRange(220, 240);
1✔
1971
   ASBlocks::ASIdOrRange sub_asnum_id_or_range2 = ASBlocks::ASIdOrRange(260, 511);
1✔
1972
   ASBlocks::ASIdOrRange sub_asnum_id_or_range3 = ASBlocks::ASIdOrRange(678);
1✔
1973
   ASBlocks::ASIdOrRange sub_asnum_id_or_range4 = ASBlocks::ASIdOrRange(5043, 5100);
1✔
1974

1975
   ASBlocks::ASIdOrRange sub_rdi_id_or_range0 = ASBlocks::ASIdOrRange(1500, 2300);
1✔
1976
   ASBlocks::ASIdOrRange sub_rdi_id_or_range1 = ASBlocks::ASIdOrRange(2500, 4000);
1✔
1977
   ASBlocks::ASIdOrRange sub_rdi_id_or_range2 = ASBlocks::ASIdOrRange(1567);
1✔
1978
   ASBlocks::ASIdOrRange sub_rdi_id_or_range3 = ASBlocks::ASIdOrRange(33100, 40000);
1✔
1979

1980
   std::vector<ASBlocks::ASIdOrRange> sub_as_ranges;
1✔
1981
   sub_as_ranges.push_back(sub_asnum_id_or_range0);
1✔
1982
   sub_as_ranges.push_back(sub_asnum_id_or_range1);
1✔
1983
   sub_as_ranges.push_back(sub_asnum_id_or_range2);
1✔
1984
   sub_as_ranges.push_back(sub_asnum_id_or_range3);
1✔
1985
   sub_as_ranges.push_back(sub_asnum_id_or_range4);
1✔
1986

1987
   std::vector<ASBlocks::ASIdOrRange> sub_rdi_ranges;
1✔
1988
   sub_rdi_ranges.push_back(sub_rdi_id_or_range0);
1✔
1989
   sub_rdi_ranges.push_back(sub_rdi_id_or_range1);
1✔
1990
   sub_rdi_ranges.push_back(sub_rdi_id_or_range2);
1✔
1991
   sub_rdi_ranges.push_back(sub_rdi_id_or_range3);
1✔
1992

1993
   ASBlocks::ASIdentifierChoice sub_asnum = ASBlocks::ASIdentifierChoice(sub_as_ranges);
1✔
1994
   ASBlocks::ASIdentifierChoice sub_rdi = ASBlocks::ASIdentifierChoice(sub_rdi_ranges);
1✔
1995
   ASBlocks::ASIdentifiers sub_ident = ASBlocks::ASIdentifiers(sub_asnum, sub_rdi);
4✔
1996
   std::unique_ptr<ASBlocks> sub_blocks = std::make_unique<ASBlocks>(sub_ident);
1✔
1997

1998
   // create a root ca that does not have any extension
1999
   Botan::X509_Cert_Options root_opts = ca_opts();
1✔
2000
   auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] = make_ca(rng, root_opts);
1✔
2001
   Botan::X509_Cert_Options sub_opts = req_opts(sig_algo);
1✔
2002
   sub_opts.extensions.add(std::move(sub_blocks));
1✔
2003
   Botan::PKCS10_Request sub_req = Botan::X509::create_cert_req(sub_opts, *sub_key, hash_fn, *rng);
1✔
2004
   Botan::X509_Certificate sub_cert = root_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
1✔
2005

2006
   Botan::Certificate_Store_In_Memory trusted;
1✔
2007
   trusted.add_certificate(root_cert);
1✔
2008

2009
   const Botan::Path_Validation_Restrictions restrictions(false, 80);
2✔
2010
   const std::vector<Botan::X509_Certificate> certs = {sub_cert};
2✔
2011

2012
   Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
1✔
2013
   result.require("path validation fails", !path_result.successful_validation());
1✔
2014

2015
   result.end_timer();
1✔
2016
   return result;
2✔
2017
}
5✔
2018

2019
Test::Result test_x509_as_blocks_path_validation_failure_builder() {
1✔
2020
   Test::Result result("X509 AS Block path validation failure (builder)");
1✔
2021
   result.start_timer();
1✔
2022

2023
   using Botan::Cert_Extension::ASBlocks;
1✔
2024
   auto rng = Test::new_rng(__func__);
1✔
2025

2026
   /*
2027
   This executes a few permutations, messing around with edge cases when it comes to constructing ranges.
2028

2029
   Each test is expected to fail and creates the following certificate chain:
2030
   Root -> Issuer -> Subject
2031

2032
   00: set all the asnum choices to 'inherit' for each cert
2033
   01: 00 but for rdis
2034
   02: make smallest min asnum of the subject smaller than the smallest min asnum of the issuer
2035
   03: 02 but for rdis
2036
   04: both 02 and 03
2037
   05: make largest max asnum of the subject larger than the largest max asnum of the issuer
2038
   06: 05 but for rdis
2039
   07: both 05 and 06
2040
   08: make the certs have multiple ranges and make one asnum range that is not the smallest and not the largest overlap with it's maximum
2041
   09: 08 but for rdis
2042
   10: both 08 and 09
2043
   11: same as 08 but the range in the subject is not contiguous, instead it is the issuers range but split into two ranges (e.g issuer range is 40-60, subject ranges are 40-49 and 51-61)
2044
   12: 11 but for rdis
2045
   13: both 11 and 12
2046
   14: 08 but using the minimum instead of the maximum
2047
   15: 14 but for rdis
2048
   16: both 14 and 15
2049
   17: same as 11 but using the minimum instead of the maximum
2050
   18: 17 but for rdis
2051
   19: both 18 and 19
2052
   20: make the issuer ranges empty but have an entry in the subject ranges
2053
   */
2054
   for(size_t i = 0; i < 21; i++) {
22✔
2055
      // enable / disable all the different edge cases
2056
      bool inherit_all_asnums = (i == 0);
21✔
2057
      bool inherit_all_rdis = (i == 1);
21✔
2058
      bool push_asnum_min_edge_ranges = (i == 2) || (i == 4);
21✔
2059
      bool push_rdi_min_edge_ranges = (i == 3) || (i == 4);
21✔
2060
      bool push_asnum_max_edge_ranges = (i == 5) || (i == 7);
21✔
2061
      bool push_rdi_max_edge_ranges = (i == 6) || (i == 7);
21✔
2062
      bool push_asnum_max_middle_ranges = (i == 8) || (i == 10);
21✔
2063
      bool push_rdi_max_middle_ranges = (i == 9) || (i == 10);
21✔
2064
      bool push_asnum_max_split_ranges = (i == 11) || (i == 13);
21✔
2065
      bool push_rdi_max_split_ranges = (i == 12) || (i == 13);
21✔
2066
      bool push_asnum_min_middle_ranges = (i == 14) || (i == 16);
21✔
2067
      bool push_rdi_min_middle_ranges = (i == 15) || (i == 16);
21✔
2068
      bool push_asnum_min_split_ranges = (i == 17) || (i == 19);
21✔
2069
      bool push_rdi_min_split_ranges = (i == 18) || (i == 19);
21✔
2070
      bool empty_issuer_non_empty_subject = (i == 20);
21✔
2071

2072
      // Root cert
2073
      std::unique_ptr<ASBlocks> root_blocks = std::make_unique<ASBlocks>();
21✔
2074

2075
      if(!inherit_all_asnums) {
21✔
2076
         if(push_asnum_min_edge_ranges || push_asnum_max_edge_ranges) {
20✔
2077
            // 100-200 for 02,03,04
2078
            root_blocks->add_asnum(100, 200);
4✔
2079
         } else if(push_asnum_max_middle_ranges || push_asnum_min_middle_ranges) {
16✔
2080
            // 10-20,30-40,50-60 for 08,09,10
2081
            root_blocks->add_asnum(10, 20);
4✔
2082
            root_blocks->add_asnum(30, 40);
4✔
2083
            root_blocks->add_asnum(50, 60);
4✔
2084
         } else if(push_asnum_max_split_ranges || push_asnum_min_split_ranges) {
12✔
2085
            // 10-20,30-50,60-70 for 11,12,13
2086
            root_blocks->add_asnum(10, 20);
4✔
2087
            root_blocks->add_asnum(30, 50);
4✔
2088
            root_blocks->add_asnum(60, 70);
4✔
2089
         }
2090
      } else {
2091
         root_blocks->inherit_asnum();
1✔
2092
      }
2093

2094
      // same values but for rdis
2095
      if(!inherit_all_rdis) {
21✔
2096
         if(push_rdi_min_edge_ranges || push_rdi_max_edge_ranges) {
2097
            root_blocks->add_rdi(100, 200);
4✔
2098
         } else if(push_rdi_max_middle_ranges || push_rdi_min_middle_ranges) {
2099
            root_blocks->add_rdi(10, 20);
4✔
2100
            root_blocks->add_rdi(30, 40);
4✔
2101
            root_blocks->add_rdi(50, 60);
4✔
2102
         } else if(push_rdi_max_split_ranges || push_rdi_min_split_ranges) {
2103
            root_blocks->add_rdi(10, 20);
4✔
2104
            root_blocks->add_rdi(30, 50);
4✔
2105
            root_blocks->add_rdi(60, 70);
4✔
2106
         }
2107
      } else {
2108
         root_blocks->inherit_rdi();
1✔
2109
      }
2110

2111
      if(empty_issuer_non_empty_subject) {
21✔
2112
         root_blocks->restrict_asnum();
1✔
2113
         root_blocks->restrict_rdi();
1✔
2114
      }
2115

2116
      // Issuer cert
2117
      // the issuer cert has the same ranges as the root cert
2118
      // it is used to check that the 'inherit' check is bubbled up until the root cert is hit
2119
      auto issu_blocks = root_blocks->copy();
21✔
2120

2121
      // Subject cert
2122
      std::unique_ptr<ASBlocks> sub_blocks = std::make_unique<ASBlocks>();
21✔
2123
      if(!inherit_all_asnums) {
21✔
2124
         // assign the subject asnum ranges
2125
         if(push_asnum_min_edge_ranges) {
2126
            // 99-200 for 02 (so overlapping to the left)
2127
            sub_blocks->add_asnum(99, 200);
2✔
2128
         } else if(push_asnum_max_edge_ranges) {
2129
            // 100-201 for 03 (so overlapping to the right)
2130
            sub_blocks->add_asnum(100, 201);
2✔
2131
         } else if(push_asnum_max_middle_ranges) {
2132
            // same as root, but change the range in the middle to overlap to the right for 08
2133
            sub_blocks->add_asnum(10, 20);
2✔
2134
            sub_blocks->add_asnum(30, 41);
2✔
2135
            sub_blocks->add_asnum(50, 60);
2✔
2136
         } else if(push_asnum_max_split_ranges) {
2137
            // change the range in the middle to be cut at 45 for case 11
2138
            // the left range is 30-44
2139
            // the right range is 46-51 (overlapping the issuer range to the right)
2140
            sub_blocks->add_asnum(10, 20);
2✔
2141
            sub_blocks->add_asnum(30, 44);
2✔
2142
            sub_blocks->add_asnum(46, 51);
2✔
2143
            sub_blocks->add_asnum(60, 70);
2✔
2144
         } else if(push_asnum_min_middle_ranges) {
2145
            // just change the test in the middle to overlap to the left for case 14
2146
            sub_blocks->add_asnum(10, 20);
2✔
2147
            sub_blocks->add_asnum(29, 40);
2✔
2148
            sub_blocks->add_asnum(50, 60);
2✔
2149
         } else if(push_asnum_min_split_ranges) {
2150
            // again split the range in the middle at 45 for case 17
2151
            // creating two ranges 29-44 and 46-50 (so overlapping to the left)
2152
            sub_blocks->add_asnum(10, 20);
2✔
2153
            sub_blocks->add_asnum(29, 44);
2✔
2154
            sub_blocks->add_asnum(46, 50);
2✔
2155
            sub_blocks->add_asnum(60, 70);
2✔
2156
         } else if(empty_issuer_non_empty_subject) {
2157
            sub_blocks->add_asnum(50);
1✔
2158
         }
2159
      } else {
2160
         sub_blocks->inherit_asnum();
1✔
2161
      }
2162

2163
      if(!inherit_all_rdis) {
21✔
2164
         // same values but for rdis
2165
         if(push_rdi_min_edge_ranges) {
2166
            sub_blocks->add_rdi(99, 200);
2✔
2167
         } else if(push_rdi_max_edge_ranges) {
2168
            sub_blocks->add_rdi(100, 201);
2✔
2169
         } else if(push_rdi_max_middle_ranges) {
2170
            sub_blocks->add_rdi(10, 20);
2✔
2171
            sub_blocks->add_rdi(30, 41);
2✔
2172
            sub_blocks->add_rdi(50, 60);
2✔
2173
         } else if(push_rdi_max_split_ranges) {
2174
            sub_blocks->add_rdi(10, 20);
2✔
2175
            sub_blocks->add_rdi(30, 44);
2✔
2176
            sub_blocks->add_rdi(46, 51);
2✔
2177
            sub_blocks->add_rdi(60, 70);
2✔
2178
         } else if(push_rdi_min_middle_ranges) {
2179
            sub_blocks->add_rdi(10, 20);
2✔
2180
            sub_blocks->add_rdi(29, 40);
2✔
2181
            sub_blocks->add_rdi(50, 60);
2✔
2182
         } else if(push_rdi_min_split_ranges) {
2183
            sub_blocks->add_rdi(10, 20);
2✔
2184
            sub_blocks->add_rdi(29, 44);
2✔
2185
            sub_blocks->add_rdi(46, 50);
2✔
2186
            sub_blocks->add_rdi(60, 70);
2✔
2187
         }
2188
      } else {
2189
         sub_blocks->inherit_rdi();
1✔
2190
      }
2191

2192
      Botan::X509_Cert_Options root_opts = ca_opts();
21✔
2193
      root_opts.extensions.add(std::move(root_blocks));
21✔
2194
      auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] = make_ca(rng, root_opts);
21✔
2195
      auto [issu_cert, issu_ca] = make_and_sign_ca(std::move(issu_blocks), root_ca, rng);
42✔
2196

2197
      Botan::X509_Cert_Options sub_opts = req_opts(sig_algo);
21✔
2198
      sub_opts.extensions.add(std::move(sub_blocks));
21✔
2199
      Botan::PKCS10_Request sub_req = Botan::X509::create_cert_req(sub_opts, *sub_key, hash_fn, *rng);
21✔
2200
      Botan::X509_Certificate sub_cert =
21✔
2201
         issu_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
21✔
2202

2203
      Botan::Certificate_Store_In_Memory trusted;
21✔
2204
      trusted.add_certificate(root_cert);
21✔
2205

2206
      const Botan::Path_Validation_Restrictions restrictions(false, 80);
42✔
2207
      const std::vector<Botan::X509_Certificate> certs = {sub_cert, issu_cert};
63✔
2208

2209
      Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
21✔
2210
      // in all cases, the validation should fail, since we are creating invalid scenarios
2211
      result.confirm("path validation fails at iteration " + std::to_string(i), !path_result.successful_validation());
42✔
2212
   }
42✔
2213

2214
   result.end_timer();
1✔
2215
   return result;
1✔
2216
}
22✔
2217

2218
Test::Result test_x509_as_blocks_path_validation_failure_ctor() {
1✔
2219
   Test::Result result("X509 AS Block path validation failure (ctor)");
1✔
2220
   result.start_timer();
1✔
2221

2222
   using Botan::Cert_Extension::ASBlocks;
1✔
2223
   auto rng = Test::new_rng(__func__);
1✔
2224

2225
   /*
2226
   This executes a few permutations, messing around with edge cases when it comes to constructing ranges.
2227

2228
   Each test is expected to fail and creates the following certificate chain:
2229
   Root -> Issuer -> Subject
2230

2231
   00: set all the asnum choices to 'inherit' for each cert
2232
   01: 00 but for rdis
2233
   02: make smallest min asnum of the subject smaller than the smallest min asnum of the issuer
2234
   03: 02 but for rdis
2235
   04: both 02 and 03
2236
   05: make largest max asnum of the subject larger than the largest max asnum of the issuer
2237
   06: 05 but for rdis
2238
   07: both 05 and 06
2239
   08: make the certs have multiple ranges and make one asnum range that is not the smallest and not the largest overlap with it's maximum
2240
   09: 08 but for rdis
2241
   10: both 08 and 09
2242
   11: same as 08 but the range in the subject is not contiguous, instead it is the issuers range but split into two ranges (e.g issuer range is 40-60, subject ranges are 40-49 and 51-61)
2243
   12: 11 but for rdis
2244
   13: both 11 and 12
2245
   14: 08 but using the minimum instead of the maximum
2246
   15: 14 but for rdis
2247
   16: both 14 and 15
2248
   17: same as 11 but using the minimum instead of the maximum
2249
   18: 17 but for rdis
2250
   19: both 18 and 19
2251
   20: make the issuer ranges empty but have an entry in the subject ranges
2252
   */
2253
   for(size_t i = 0; i < 21; i++) {
22✔
2254
      // enable / disable all the different edge cases
2255
      bool inherit_all_asnums = (i == 0);
21✔
2256
      bool inherit_all_rdis = (i == 1);
21✔
2257
      bool push_asnum_min_edge_ranges = (i == 2) || (i == 4);
21✔
2258
      bool push_rdi_min_edge_ranges = (i == 3) || (i == 4);
21✔
2259
      bool push_asnum_max_edge_ranges = (i == 5) || (i == 7);
21✔
2260
      bool push_rdi_max_edge_ranges = (i == 6) || (i == 7);
21✔
2261
      bool push_asnum_max_middle_ranges = (i == 8) || (i == 10);
21✔
2262
      bool push_rdi_max_middle_ranges = (i == 9) || (i == 10);
21✔
2263
      bool push_asnum_max_split_ranges = (i == 11) || (i == 13);
21✔
2264
      bool push_rdi_max_split_ranges = (i == 12) || (i == 13);
21✔
2265
      bool push_asnum_min_middle_ranges = (i == 14) || (i == 16);
21✔
2266
      bool push_rdi_min_middle_ranges = (i == 15) || (i == 16);
21✔
2267
      bool push_asnum_min_split_ranges = (i == 17) || (i == 19);
21✔
2268
      bool push_rdi_min_split_ranges = (i == 18) || (i == 19);
21✔
2269
      bool empty_issuer_non_empty_subject = (i == 20);
21✔
2270

2271
      // Root cert
2272
      std::vector<ASBlocks::ASIdOrRange> root_as_ranges;
21✔
2273
      std::vector<ASBlocks::ASIdOrRange> root_rdi_ranges;
21✔
2274

2275
      // assign the root ranges
2276
      if(push_asnum_min_edge_ranges || push_asnum_max_edge_ranges) {
21✔
2277
         // 100-200 for 02,03,04
2278
         root_as_ranges.push_back(ASBlocks::ASIdOrRange(100, 200));
8✔
2279
      } else if(push_asnum_max_middle_ranges || push_asnum_min_middle_ranges) {
17✔
2280
         // 10-20,30-40,50-60 for 08,09,10
2281
         root_as_ranges.push_back(ASBlocks::ASIdOrRange(10, 20));
8✔
2282
         root_as_ranges.push_back(ASBlocks::ASIdOrRange(30, 40));
8✔
2283
         root_as_ranges.push_back(ASBlocks::ASIdOrRange(50, 60));
8✔
2284
      } else if(push_asnum_max_split_ranges || push_asnum_min_split_ranges) {
13✔
2285
         // 10-20,30-50,60-70 for 11,12,13
2286
         root_as_ranges.push_back(ASBlocks::ASIdOrRange(10, 20));
8✔
2287
         root_as_ranges.push_back(ASBlocks::ASIdOrRange(30, 50));
8✔
2288
         root_as_ranges.push_back(ASBlocks::ASIdOrRange(60, 70));
8✔
2289
      }
2290

2291
      // same values but for rdis
2292
      if(push_rdi_min_edge_ranges || push_rdi_max_edge_ranges) {
21✔
2293
         root_rdi_ranges.push_back(ASBlocks::ASIdOrRange(100, 200));
8✔
2294
      } else if(push_rdi_max_middle_ranges || push_rdi_min_middle_ranges) {
2295
         root_rdi_ranges.push_back(ASBlocks::ASIdOrRange(10, 20));
8✔
2296
         root_rdi_ranges.push_back(ASBlocks::ASIdOrRange(30, 40));
8✔
2297
         root_rdi_ranges.push_back(ASBlocks::ASIdOrRange(50, 60));
8✔
2298
      } else if(push_rdi_max_split_ranges || push_rdi_min_split_ranges) {
2299
         root_rdi_ranges.push_back(ASBlocks::ASIdOrRange(10, 20));
8✔
2300
         root_rdi_ranges.push_back(ASBlocks::ASIdOrRange(30, 50));
8✔
2301
         root_rdi_ranges.push_back(ASBlocks::ASIdOrRange(60, 70));
8✔
2302
      }
2303

2304
      // Issuer cert
2305
      // the issuer cert has the same ranges as the root cert
2306
      // it is used to check that the 'inherit' check is bubbled up until the root cert is hit
2307
      std::vector<ASBlocks::ASIdOrRange> issu_as_ranges;
21✔
2308
      std::vector<ASBlocks::ASIdOrRange> issu_rdi_ranges;
21✔
2309

2310
      // Subject cert
2311
      std::vector<ASBlocks::ASIdOrRange> sub_as_ranges;
21✔
2312
      std::vector<ASBlocks::ASIdOrRange> sub_rdi_ranges;
21✔
2313

2314
      // assign the subject asnum ranges
2315
      if(push_asnum_min_edge_ranges) {
21✔
2316
         // 99-200 for 02 (so overlapping to the left)
2317
         sub_as_ranges.push_back(ASBlocks::ASIdOrRange(99, 200));
4✔
2318
      } else if(push_asnum_max_edge_ranges) {
2319
         // 100-201 for 03 (so overlapping to the right)
2320
         sub_as_ranges.push_back(ASBlocks::ASIdOrRange(100, 201));
4✔
2321
      } else if(push_asnum_max_middle_ranges) {
2322
         // just change the range in the middle to overlap to the right for 08
2323
         sub_as_ranges = root_as_ranges;
2✔
2324
         sub_as_ranges[1] = ASBlocks::ASIdOrRange(30, 41);
2✔
2325
      } else if(push_asnum_max_split_ranges) {
2326
         // change the range in the middle to be cut at 45 for case 11
2327
         // the left range is 30-44
2328
         // the right range is 46-51 (overlapping the issuer range to the right)
2329
         sub_as_ranges = root_as_ranges;
2✔
2330
         sub_as_ranges[1] = ASBlocks::ASIdOrRange(30, 44);
2✔
2331
         // pushing the new range created by splitting to the back since they will be sorted anyway
2332
         sub_as_ranges.push_back(ASBlocks::ASIdOrRange(46, 51));
4✔
2333
      } else if(push_asnum_min_middle_ranges) {
2334
         // just change the test in the middle to overlap to the left for case 14
2335
         sub_as_ranges = root_as_ranges;
2✔
2336
         sub_as_ranges[1] = ASBlocks::ASIdOrRange(29, 40);
2✔
2337
      } else if(push_asnum_min_split_ranges) {
2338
         // again split the range in the middle at 45 for case 17
2339
         // creating two ranges 29-44 and 46-50 (so overlapping to the left)
2340
         sub_as_ranges = root_as_ranges;
2✔
2341
         sub_as_ranges[1] = ASBlocks::ASIdOrRange(29, 44);
2✔
2342
         sub_as_ranges.push_back(ASBlocks::ASIdOrRange(46, 50));
4✔
2343
      } else if(empty_issuer_non_empty_subject) {
2344
         sub_as_ranges.push_back(ASBlocks::ASIdOrRange(50));
1✔
2345
      }
2346

2347
      // same values but for rdis
2348
      if(push_rdi_min_edge_ranges) {
21✔
2349
         sub_rdi_ranges.push_back(ASBlocks::ASIdOrRange(99, 200));
4✔
2350
      } else if(push_rdi_max_edge_ranges) {
2351
         sub_rdi_ranges.push_back(ASBlocks::ASIdOrRange(100, 201));
4✔
2352
      } else if(push_rdi_max_middle_ranges) {
2353
         sub_rdi_ranges = root_rdi_ranges;
2✔
2354
         sub_rdi_ranges[1] = ASBlocks::ASIdOrRange(30, 41);
2✔
2355
      } else if(push_rdi_max_split_ranges) {
2356
         sub_rdi_ranges = root_rdi_ranges;
2✔
2357
         sub_rdi_ranges[1] = ASBlocks::ASIdOrRange(30, 44);
2✔
2358
         sub_rdi_ranges.push_back(ASBlocks::ASIdOrRange(46, 51));
4✔
2359
      } else if(push_rdi_min_middle_ranges) {
2360
         sub_rdi_ranges = root_rdi_ranges;
2✔
2361
         sub_rdi_ranges[1] = ASBlocks::ASIdOrRange(29, 40);
2✔
2362
      } else if(push_rdi_min_split_ranges) {
2363
         sub_rdi_ranges = root_rdi_ranges;
2✔
2364
         sub_rdi_ranges[1] = ASBlocks::ASIdOrRange(29, 44);
2✔
2365
         sub_rdi_ranges.push_back(ASBlocks::ASIdOrRange(46, 50));
4✔
2366
      }
2367

2368
      // for cases 00 and 01, set all certs to inherit (so std::nullopt)
2369
      // in all other cases use the ranges created beforehand
2370
      ASBlocks::ASIdentifierChoice root_asnum =
21✔
2371
         ASBlocks::ASIdentifierChoice(inherit_all_asnums ? std::nullopt : std::optional(root_as_ranges));
41✔
2372
      ASBlocks::ASIdentifierChoice root_rdi =
21✔
2373
         ASBlocks::ASIdentifierChoice(inherit_all_rdis ? std::nullopt : std::optional(root_rdi_ranges));
41✔
2374
      ASBlocks::ASIdentifiers root_ident = ASBlocks::ASIdentifiers(root_asnum, root_rdi);
82✔
2375
      std::unique_ptr<ASBlocks> root_blocks = std::make_unique<ASBlocks>(root_ident);
21✔
2376

2377
      ASBlocks::ASIdentifiers issu_ident = root_ident;
21✔
2378
      std::unique_ptr<ASBlocks> issu_blocks = std::make_unique<ASBlocks>(issu_ident);
21✔
2379

2380
      ASBlocks::ASIdentifierChoice sub_asnum =
21✔
2381
         ASBlocks::ASIdentifierChoice(inherit_all_asnums ? std::nullopt : std::optional(sub_as_ranges));
41✔
2382
      ASBlocks::ASIdentifierChoice sub_rdi =
21✔
2383
         ASBlocks::ASIdentifierChoice(inherit_all_rdis ? std::nullopt : std::optional(sub_rdi_ranges));
41✔
2384
      ASBlocks::ASIdentifiers sub_ident = ASBlocks::ASIdentifiers(sub_asnum, sub_rdi);
82✔
2385
      std::unique_ptr<ASBlocks> sub_blocks = std::make_unique<ASBlocks>(sub_ident);
21✔
2386

2387
      Botan::X509_Cert_Options root_opts = ca_opts();
21✔
2388
      root_opts.extensions.add(std::move(root_blocks));
21✔
2389
      auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] = make_ca(rng, root_opts);
21✔
2390
      auto [issu_cert, issu_ca] = make_and_sign_ca(std::move(issu_blocks), root_ca, rng);
42✔
2391

2392
      Botan::X509_Cert_Options sub_opts = req_opts(sig_algo);
21✔
2393
      sub_opts.extensions.add(std::move(sub_blocks));
21✔
2394
      Botan::PKCS10_Request sub_req = Botan::X509::create_cert_req(sub_opts, *sub_key, hash_fn, *rng);
21✔
2395
      Botan::X509_Certificate sub_cert =
21✔
2396
         issu_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
21✔
2397

2398
      Botan::Certificate_Store_In_Memory trusted;
21✔
2399
      trusted.add_certificate(root_cert);
21✔
2400

2401
      const Botan::Path_Validation_Restrictions restrictions(false, 80);
42✔
2402
      const std::vector<Botan::X509_Certificate> certs = {sub_cert, issu_cert};
63✔
2403

2404
      Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
21✔
2405
      // in all cases, the validation should fail, since we are creating invalid scenarios
2406
      result.confirm("path validation fails at iteration " + std::to_string(i), !path_result.successful_validation());
42✔
2407
   }
122✔
2408

2409
   result.end_timer();
1✔
2410
   return result;
1✔
2411
}
22✔
2412

2413
class X509_RPKI_Tests final : public Test {
×
2414
   public:
2415
      std::vector<Test::Result> run() override {
1✔
2416
         std::vector<Test::Result> results;
1✔
2417

2418
   #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
2419
         results.push_back(test_x509_ip_addr_blocks_extension_decode());
2✔
2420
         results.push_back(test_x509_as_blocks_extension_decode());
2✔
2421
   #endif
2422
         results.push_back(test_x509_ip_addr_blocks_rfc3779_example());
2✔
2423
         results.push_back(test_x509_ip_addr_blocks_encode_builder());
2✔
2424
         results.push_back(test_x509_ip_addr_blocks_extension_encode_ctor());
2✔
2425
         results.push_back(test_x509_ip_addr_blocks_extension_encode_edge_cases_ctor());
2✔
2426
         results.push_back(test_x509_ip_addr_blocks_range_merge());
2✔
2427
         results.push_back(test_x509_ip_addr_blocks_family_merge());
2✔
2428
         results.push_back(test_x509_ip_addr_blocks_path_validation_success_builder());
2✔
2429
         results.push_back(test_x509_ip_addr_blocks_path_validation_success_ctor());
2✔
2430
         results.push_back(test_x509_ip_addr_blocks_path_validation_failure_builder());
2✔
2431
         results.push_back(test_x509_ip_addr_blocks_path_validation_failure_ctor());
2✔
2432
         results.push_back(test_x509_as_blocks_rfc3779_example());
2✔
2433
         results.push_back(test_x509_as_blocks_encode_builder());
2✔
2434
         results.push_back(test_x509_as_blocks_extension_encode_ctor());
2✔
2435
         results.push_back(test_x509_as_blocks_range_merge());
2✔
2436
         results.push_back(test_x509_as_blocks_path_validation_success_builder());
2✔
2437
         results.push_back(test_x509_as_blocks_path_validation_success_ctor());
2✔
2438
         results.push_back(test_x509_as_blocks_path_validation_extension_not_present_builder());
2✔
2439
         results.push_back(test_x509_as_blocks_path_validation_extension_not_present_ctor());
2✔
2440
         results.push_back(test_x509_as_blocks_path_validation_failure_builder());
2✔
2441
         results.push_back(test_x509_as_blocks_path_validation_failure_ctor());
2✔
2442
         return results;
1✔
2443
      }
×
2444
};
2445

2446
BOTAN_REGISTER_TEST("x509", "x509_rpki", X509_RPKI_Tests);
2447

2448
#endif
2449

2450
}  // namespace
2451

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