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

randombit / botan / 20160266155

12 Dec 2025 08:01AM UTC coverage: 90.357% (-0.001%) from 90.358%
20160266155

push

github

web-flow
Merge pull request #5172 from randombit/jack/fix-clang-tidy-misc-const-correctness

Fix and enable clang-tidy warning misc-const-correctness

100951 of 111725 relevant lines covered (90.36%)

12817908.54 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
   const 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
   const std::unique_ptr<Botan::Private_Key> key = generate_key(sig_algo, *rng);
76✔
149

150
   const 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
      const 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
      const 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
      const 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
      const 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
      const 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
      const 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
      const 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
      const bool push_ipv4_ranges = bit_set<0>(i);
64✔
552
      const bool push_ipv6_ranges = bit_set<1>(i);
64✔
553
      const bool inherit_ipv4 = bit_set<2>(i);
64✔
554
      const bool inherit_ipv6 = bit_set<3>(i);
64✔
555
      const bool push_ipv4_family = bit_set<4>(i);
64✔
556
      const 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
      const Botan::PKCS10_Request req = Botan::X509::create_cert_req(opts, *sub_key, hash_fn, *rng);
64✔
647
      const 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
         const bool modify_min = bit_set<0>(j);
84✔
730
         const 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
            const Botan::PKCS10_Request req = Botan::X509::create_cert_req(opts, *sub_key, hash_fn, *rng);
1,135✔
771
            const Botan::X509_Certificate cert =
1,135✔
772
               ca.sign_request(req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
1,135✔
773
            {
1,135✔
774
               const auto* ip_blocks = cert.v3_extensions().get_extension_object_as<IPAddressBlocks>();
1,135✔
775
               result.confirm("cert has IPAddrBlocks extension", ip_blocks != nullptr, true);
2,270✔
776
               const auto& dec_addr_blocks = ip_blocks->addr_blocks();
1,135✔
777
               auto family = dec_addr_blocks[0];
1,135✔
778
               result.confirm("ipv6 family afi", ipv6_addr_family.afi() == family.afi(), true);
2,270✔
779
               result.test_eq("ipv6 family safi", ipv6_addr_family.safi(), family.safi());
1,135✔
780
               auto choice = std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(family.addr_choice());
1,135✔
781
               auto ranges = choice.ranges().value();
1,135✔
782

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1713
   // Root Cert, both as and rdi
1714

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

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

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

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

1730
   // Subject cert
1731

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1950
   Botan::Certificate_Store_In_Memory trusted;
1✔
1951
   trusted.add_certificate(root_cert);
1✔
1952

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

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

1959
   result.end_timer();
1✔
1960
   return result;
2✔
1961
}
3✔
1962

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

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

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

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

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

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

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

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

2009
   Botan::Certificate_Store_In_Memory trusted;
1✔
2010
   trusted.add_certificate(root_cert);
1✔
2011

2012
   const Botan::Path_Validation_Restrictions restrictions(false, 80);
2✔
2013
   const std::vector<Botan::X509_Certificate> certs = {sub_cert};
2✔
2014

2015
   const Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
1✔
2016
   result.require("path validation fails", !path_result.successful_validation());
1✔
2017

2018
   result.end_timer();
1✔
2019
   return result;
2✔
2020
}
5✔
2021

2022
Test::Result test_x509_as_blocks_path_validation_failure_builder() {
1✔
2023
   Test::Result result("X509 AS Block path validation failure (builder)");
1✔
2024
   result.start_timer();
1✔
2025

2026
   using Botan::Cert_Extension::ASBlocks;
1✔
2027
   auto rng = Test::new_rng(__func__);
1✔
2028

2029
   /*
2030
   This executes a few permutations, messing around with edge cases when it comes to constructing ranges.
2031

2032
   Each test is expected to fail and creates the following certificate chain:
2033
   Root -> Issuer -> Subject
2034

2035
   00: set all the asnum choices to 'inherit' for each cert
2036
   01: 00 but for rdis
2037
   02: make smallest min asnum of the subject smaller than the smallest min asnum of the issuer
2038
   03: 02 but for rdis
2039
   04: both 02 and 03
2040
   05: make largest max asnum of the subject larger than the largest max asnum of the issuer
2041
   06: 05 but for rdis
2042
   07: both 05 and 06
2043
   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
2044
   09: 08 but for rdis
2045
   10: both 08 and 09
2046
   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)
2047
   12: 11 but for rdis
2048
   13: both 11 and 12
2049
   14: 08 but using the minimum instead of the maximum
2050
   15: 14 but for rdis
2051
   16: both 14 and 15
2052
   17: same as 11 but using the minimum instead of the maximum
2053
   18: 17 but for rdis
2054
   19: both 18 and 19
2055
   20: make the issuer ranges empty but have an entry in the subject ranges
2056
   */
2057
   for(size_t i = 0; i < 21; i++) {
22✔
2058
      // enable / disable all the different edge cases
2059
      const bool inherit_all_asnums = (i == 0);
21✔
2060
      const bool inherit_all_rdis = (i == 1);
21✔
2061
      const bool push_asnum_min_edge_ranges = (i == 2) || (i == 4);
21✔
2062
      const bool push_rdi_min_edge_ranges = (i == 3) || (i == 4);
21✔
2063
      const bool push_asnum_max_edge_ranges = (i == 5) || (i == 7);
21✔
2064
      const bool push_rdi_max_edge_ranges = (i == 6) || (i == 7);
21✔
2065
      const bool push_asnum_max_middle_ranges = (i == 8) || (i == 10);
21✔
2066
      const bool push_rdi_max_middle_ranges = (i == 9) || (i == 10);
21✔
2067
      const bool push_asnum_max_split_ranges = (i == 11) || (i == 13);
21✔
2068
      const bool push_rdi_max_split_ranges = (i == 12) || (i == 13);
21✔
2069
      const bool push_asnum_min_middle_ranges = (i == 14) || (i == 16);
21✔
2070
      const bool push_rdi_min_middle_ranges = (i == 15) || (i == 16);
21✔
2071
      const bool push_asnum_min_split_ranges = (i == 17) || (i == 19);
21✔
2072
      const bool push_rdi_min_split_ranges = (i == 18) || (i == 19);
21✔
2073
      const bool empty_issuer_non_empty_subject = (i == 20);
21✔
2074

2075
      // Root cert
2076
      std::unique_ptr<ASBlocks> root_blocks = std::make_unique<ASBlocks>();
21✔
2077

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

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

2114
      if(empty_issuer_non_empty_subject) {
21✔
2115
         root_blocks->restrict_asnum();
1✔
2116
         root_blocks->restrict_rdi();
1✔
2117
      }
2118

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

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

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

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

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

2206
      Botan::Certificate_Store_In_Memory trusted;
21✔
2207
      trusted.add_certificate(root_cert);
21✔
2208

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

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

2217
   result.end_timer();
1✔
2218
   return result;
1✔
2219
}
22✔
2220

2221
Test::Result test_x509_as_blocks_path_validation_failure_ctor() {
1✔
2222
   Test::Result result("X509 AS Block path validation failure (ctor)");
1✔
2223
   result.start_timer();
1✔
2224

2225
   using Botan::Cert_Extension::ASBlocks;
1✔
2226
   auto rng = Test::new_rng(__func__);
1✔
2227

2228
   /*
2229
   This executes a few permutations, messing around with edge cases when it comes to constructing ranges.
2230

2231
   Each test is expected to fail and creates the following certificate chain:
2232
   Root -> Issuer -> Subject
2233

2234
   00: set all the asnum choices to 'inherit' for each cert
2235
   01: 00 but for rdis
2236
   02: make smallest min asnum of the subject smaller than the smallest min asnum of the issuer
2237
   03: 02 but for rdis
2238
   04: both 02 and 03
2239
   05: make largest max asnum of the subject larger than the largest max asnum of the issuer
2240
   06: 05 but for rdis
2241
   07: both 05 and 06
2242
   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
2243
   09: 08 but for rdis
2244
   10: both 08 and 09
2245
   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)
2246
   12: 11 but for rdis
2247
   13: both 11 and 12
2248
   14: 08 but using the minimum instead of the maximum
2249
   15: 14 but for rdis
2250
   16: both 14 and 15
2251
   17: same as 11 but using the minimum instead of the maximum
2252
   18: 17 but for rdis
2253
   19: both 18 and 19
2254
   20: make the issuer ranges empty but have an entry in the subject ranges
2255
   */
2256
   for(size_t i = 0; i < 21; i++) {
22✔
2257
      // enable / disable all the different edge cases
2258
      const bool inherit_all_asnums = (i == 0);
21✔
2259
      const bool inherit_all_rdis = (i == 1);
21✔
2260
      const bool push_asnum_min_edge_ranges = (i == 2) || (i == 4);
21✔
2261
      const bool push_rdi_min_edge_ranges = (i == 3) || (i == 4);
21✔
2262
      const bool push_asnum_max_edge_ranges = (i == 5) || (i == 7);
21✔
2263
      const bool push_rdi_max_edge_ranges = (i == 6) || (i == 7);
21✔
2264
      const bool push_asnum_max_middle_ranges = (i == 8) || (i == 10);
21✔
2265
      const bool push_rdi_max_middle_ranges = (i == 9) || (i == 10);
21✔
2266
      const bool push_asnum_max_split_ranges = (i == 11) || (i == 13);
21✔
2267
      const bool push_rdi_max_split_ranges = (i == 12) || (i == 13);
21✔
2268
      const bool push_asnum_min_middle_ranges = (i == 14) || (i == 16);
21✔
2269
      const bool push_rdi_min_middle_ranges = (i == 15) || (i == 16);
21✔
2270
      const bool push_asnum_min_split_ranges = (i == 17) || (i == 19);
21✔
2271
      const bool push_rdi_min_split_ranges = (i == 18) || (i == 19);
21✔
2272
      const bool empty_issuer_non_empty_subject = (i == 20);
21✔
2273

2274
      // Root cert
2275
      std::vector<ASBlocks::ASIdOrRange> root_as_ranges;
21✔
2276
      std::vector<ASBlocks::ASIdOrRange> root_rdi_ranges;
21✔
2277

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

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

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

2313
      // Subject cert
2314
      std::vector<ASBlocks::ASIdOrRange> sub_as_ranges;
21✔
2315
      std::vector<ASBlocks::ASIdOrRange> sub_rdi_ranges;
21✔
2316

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

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

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

2380
      const ASBlocks::ASIdentifiers& issu_ident = root_ident;
21✔
2381
      std::unique_ptr<ASBlocks> issu_blocks = std::make_unique<ASBlocks>(issu_ident);
21✔
2382

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

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

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

2401
      Botan::Certificate_Store_In_Memory trusted;
21✔
2402
      trusted.add_certificate(root_cert);
21✔
2403

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

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

2412
   result.end_timer();
1✔
2413
   return result;
1✔
2414
}
22✔
2415

2416
class X509_RPKI_Tests final : public Test {
×
2417
   public:
2418
      std::vector<Test::Result> run() override {
1✔
2419
         std::vector<Test::Result> results;
1✔
2420

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

2449
BOTAN_REGISTER_TEST("x509", "x509_rpki", X509_RPKI_Tests);
2450

2451
#endif
2452

2453
}  // namespace
2454

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