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

randombit / botan / 16679948655

01 Aug 2025 04:17PM UTC coverage: 90.676% (-0.006%) from 90.682%
16679948655

Pull #4877

github

web-flow
Merge be81f40f0 into a361efb2e
Pull Request #4877: Add FFI for X.509 creation

100548 of 110887 relevant lines covered (90.68%)

12277433.11 hits per line

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

99.04
/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_builder.h>
15
   #include <botan/x509_ca.h>
16
   #include <botan/x509_ext.h>
17
   #include <botan/x509path.h>
18
   #include <botan/internal/calendar.h>
19
#endif
20

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

25
namespace Botan_Tests {
26

27
namespace {
28

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

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

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

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

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

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

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

66
Botan::CertificateParametersBuilder ca_params() {
149✔
67
   Botan::CertificateParametersBuilder builder;
149✔
68

69
   builder.add_common_name("RPKI Test CA")
149✔
70
      .add_country("US")
149✔
71
      .add_organization("Botan")
149✔
72
      .add_dns("botan.randombit.net")
149✔
73
      .set_as_ca_certificate(1);
149✔
74

75
   return builder;
149✔
76
}
×
77

78
Botan::CertificateParametersBuilder user_params() {
1,288✔
79
   Botan::CertificateParametersBuilder builder;
1,288✔
80

81
   builder.add_common_name("RPKI Test User")
1,288✔
82
      .add_country("US")
1,288✔
83
      .add_organization("Botan")
1,288✔
84
      .add_allowed_usage(Botan::Key_Constraints::DigitalSignature);
1,288✔
85

86
   return builder;
1,288✔
87
}
×
88

89
std::pair<std::string, std::string> get_sig_algo() {
149✔
90
   #if defined(BOTAN_HAS_ECDSA)
91
   const std::string sig_algo{"ECDSA"};
149✔
92
   const std::string hash_fn{"SHA-256"};
149✔
93
   #elif defined(BOTAN_HAS_ED25519)
94
   const std::string sig_algo{"Ed25519"};
95
   const std::string hash_fn{"SHA-512"};
96
   #elif defined(BOTAN_HAS_RSA)
97
   const std::string sig_algo{"RSA"};
98
   const std::string padding_method{"PKCS1v15(SHA-256)"};
99
   const std::string hash_fn{"SHA-256"};
100
   #endif
101

102
   return std::make_pair(sig_algo, hash_fn);
149✔
103
}
149✔
104

105
Botan::X509_Certificate make_self_signed(Botan::RandomNumberGenerator& rng,
5✔
106
                                         const Botan::CertificateParametersBuilder& params) {
107
   auto [sig_algo, hash_fn] = get_sig_algo();
5✔
108
   auto key = generate_key(sig_algo, rng);
5✔
109

110
   const auto now = std::chrono::system_clock::now();
5✔
111
   const auto not_before = now - std::chrono::seconds(180);
5✔
112
   const auto not_after = now + std::chrono::seconds(86400);
5✔
113

114
   return params.into_self_signed_cert(not_before, not_after, *key, rng, hash_fn);
5✔
115
}
10✔
116

117
CA_Creation_Result make_ca(Botan::RandomNumberGenerator& rng, const Botan::CertificateParametersBuilder& params) {
68✔
118
   auto [sig_algo, hash_fn] = get_sig_algo();
68✔
119
   auto ca_key = generate_key(sig_algo, rng);
68✔
120

121
   const auto now = std::chrono::system_clock::now();
68✔
122
   const auto not_before = now - std::chrono::seconds(180);
68✔
123
   const auto not_after = now + std::chrono::seconds(86400);
68✔
124

125
   const auto ca_cert = params.into_self_signed_cert(not_before, not_after, *ca_key, rng, hash_fn);
68✔
126

127
   Botan::X509_CA ca(ca_cert, *ca_key, hash_fn, rng);
68✔
128
   auto sub_key = generate_key(sig_algo, rng);
68✔
129

130
   return CA_Creation_Result{ca_cert, std::move(ca), std::move(sub_key), sig_algo, hash_fn};
204✔
131
}
204✔
132

133
CA_Creation_Result make_ca(Botan::RandomNumberGenerator& rng) {
6✔
134
   return make_ca(rng, ca_params());
6✔
135
}
136

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

142
   auto params = ca_params();
76✔
143
   params.add_extension(std::move(ext));
76✔
144

145
   std::unique_ptr<Botan::Private_Key> key = generate_key(sig_algo, rng);
76✔
146

147
   Botan::PKCS10_Request req = params.into_pkcs10_request(*key, rng, hash_fn);
76✔
148

149
   Botan::X509_Certificate cert = parent_ca.sign_request(req, rng, from_date(-1, 01, 01), from_date(2, 01, 01));
76✔
150
   Botan::X509_CA ca(cert, *key, hash_fn, rng);
76✔
151

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

155
constexpr auto IPv4 = Botan::Cert_Extension::IPAddressBlocks::Version::IPv4;
156
constexpr auto IPv6 = Botan::Cert_Extension::IPAddressBlocks::Version::IPv6;
157

158
   #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
159

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

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

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

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

177
      const auto& v4_blocks = ipv4block.ranges().value();
1✔
178

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

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

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

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

196
      const auto& v6_blocks = ipv6block.ranges().value();
1✔
197

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

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

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

242
      // IPv4 ranges should be merged, IPv4 should come before IPv6, all should be sorted by safi
243

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

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

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

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

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

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

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

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

286
      // cert contains the 10.0.32.0/20 prefix, but with a 9 for the unused bits
287

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

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

294
   result.end_timer();
1✔
295
   return result;
1✔
296
}
×
297

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

372
   result.end_timer();
1✔
373
   return result;
1✔
374
}
×
375

376
   #endif
377

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

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

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

395
   auto cert_1 = make_self_signed(*rng, ca_params().add_extension(std::move(blocks_1)));
2✔
396

397
   auto bits_1 = cert_1.v3_extensions().get_extension_bits(IPAddressBlocks::static_oid());
1✔
398

399
   result.test_eq(
1✔
400
      "extension is encoded as specified",
401
      bits_1,
402
      "3035302B040300010130240304040A00200304000A00400303000A01300C0304040A02300304000A02400303000A033006040200020500");
403

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

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

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

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

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

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

425
   result.test_eq("extension 1 ipv6 safi", ext_1->addr_blocks()[1].safi(), std::optional<uint8_t>{std::nullopt});
1✔
426
   result.confirm(
2✔
427
      "extension 1 ipv6 inherited",
428
      !std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(ext_1->addr_blocks()[1].addr_choice()).ranges().has_value());
1✔
429

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

439
   auto cert_2 = make_self_signed(*rng, ca_params().add_extension(std::move(blocks_2)));
2✔
440

441
   auto bits_2 = cert_2.v3_extensions().get_extension_bits(IPAddressBlocks::static_oid());
1✔
442

443
   // see https://www.rfc-editor.org/errata/eid6792 as to why the B0 specified in the RFC is a AC here
444
   result.test_eq("extension is encoded as specified",
1✔
445
                  bits_2,
446
                  "302C3010040300010130090302000A030304AC10300704030001020500300F040200023009030700200100000002");
447

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

450
   auto ext_2_addr_fam_1 = ext_2->addr_blocks()[0];
1✔
451
   result.test_eq("extension 2 ipv4 1 safi", ext_2_addr_fam_1.safi(), std::optional<uint8_t>(1));
1✔
452
   auto ext_2_ranges_1 =
1✔
453
      std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(ext_2_addr_fam_1.addr_choice()).ranges().value();
1✔
454
   result.test_eq("extension 2 fam 1 range 1 min", ext_2_ranges_1[0].min().value(), {10, 0, 0, 0});
2✔
455
   result.test_eq("extension 2 fam 1 range 1 max", ext_2_ranges_1[0].max().value(), {10, 255, 255, 255});
2✔
456

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

460
   result.test_eq("extension 2 ipv4 2 safi", ext_2->addr_blocks()[1].safi(), std::optional<uint8_t>{2});
1✔
461
   result.confirm(
2✔
462
      "extension 2 ipv4 2 inherited",
463
      !std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(ext_2->addr_blocks()[1].addr_choice()).ranges().has_value());
1✔
464

465
   auto ext_2_addr_fam_3 = ext_2->addr_blocks()[2];
1✔
466
   result.test_eq("extension 2 ipv4 1 safi", ext_2_addr_fam_3.safi(), std::optional<uint8_t>(std::nullopt));
1✔
467
   auto ext_2_ranges_3 =
1✔
468
      std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(ext_2_addr_fam_3.addr_choice()).ranges().value();
1✔
469
   result.test_eq("extension 2 fam 3 range 1 min",
2✔
470
                  ext_2_ranges_3[0].min().value(),
1✔
471
                  {0x20, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
472
   result.test_eq("extension 2 fam 3 range 1 max",
2✔
473
                  ext_2_ranges_3[0].max().value(),
1✔
474
                  {0x20, 0x01, 0x00, 0x00, 0x00, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff});
475

476
   result.end_timer();
1✔
477
   return result;
2✔
478
}
7✔
479

480
Test::Result test_x509_ip_addr_blocks_encode_builder() {
1✔
481
   Test::Result result("X509 IP Address Blocks encode (builder)");
1✔
482
   result.start_timer();
1✔
483

484
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
485
   auto rng = Test::new_rng(__func__);
1✔
486

487
   std::unique_ptr<IPAddressBlocks> blocks = std::make_unique<IPAddressBlocks>();
1✔
488

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

492
   blocks->add_address<IPv4>({255, 255, 255, 255});
1✔
493
   // encoded as prefix
494
   blocks->add_address<IPv4>({190, 5, 0, 0}, {190, 5, 0b01111111, 255});
1✔
495
   // encoded as min, max
496
   blocks->add_address<IPv4>({127, 0, 0, 1}, {189, 5, 7, 255});
1✔
497

498
   // full address range
499
   blocks->add_address<IPv4>({0, 0, 0, 0}, {255, 255, 255, 255}, 1);
1✔
500

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

503
   auto cert = make_self_signed(*rng, ca_params().add_extension(std::move(blocks)));
2✔
504
   auto bits = cert.v3_extensions().get_extension_bits(IPAddressBlocks::static_oid());
1✔
505

506
   // hand validated with https://lapo.it/asn1js/
507
   result.test_eq(
1✔
508
      "extension is encoded as specified",
509
      bits,
510
      "304630290402000130230305007B7B0201300D0305007F000001030403BD0500030407BE0500030500FFFFFFFF300A04030001013003030100300D04030001023006030406C0A840");
511

512
   result.end_timer();
1✔
513
   return result;
1✔
514
}
2✔
515

516
Test::Result test_x509_ip_addr_blocks_extension_encode_ctor() {
1✔
517
   Test::Result result("X509 IP Address Block encode (ctor)");
1✔
518
   result.start_timer();
1✔
519

520
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
521

522
   auto rng = Test::new_rng(__func__);
1✔
523

524
   auto [ca_cert, ca, sub_key, sig_algo, hash_fn] = make_ca(*rng);
1✔
525

526
   for(size_t i = 0; i < 64; i++) {
65✔
527
      bool push_ipv4_ranges = i & 1;
64✔
528
      bool push_ipv6_ranges = i >> 1 & 1;
64✔
529
      bool inherit_ipv4 = i >> 2 & 1;
64✔
530
      bool inherit_ipv6 = i >> 3 & 1;
64✔
531
      bool push_ipv4_family = i >> 4 & 1;
64✔
532
      bool push_ipv6_family = i >> 5 & 1;
64✔
533

534
      std::vector<uint8_t> a = {123, 123, 2, 1};
64✔
535
      auto ipv4_1 = IPAddressBlocks::IPAddress<IPv4>(a);
64✔
536
      a = {255, 255, 255, 255};
64✔
537
      auto ipv4_2 = IPAddressBlocks::IPAddress<IPv4>(a);
64✔
538

539
      // encoded as min, max
540
      a = {127, 0, 0, 1};
64✔
541
      auto ipv4_range_1_min = IPAddressBlocks::IPAddress<IPv4>(a);
64✔
542
      a = {189, 5, 7, 255};
64✔
543
      auto ipv4_range_1_max = IPAddressBlocks::IPAddress<IPv4>(a);
64✔
544

545
      // encoded as prefix
546
      a = {190, 5, 0, 0};
64✔
547
      auto ipv4_range_2_min = IPAddressBlocks::IPAddress<IPv4>(a);
64✔
548
      a = {190, 5, 127, 255};
64✔
549
      auto ipv4_range_2_max = IPAddressBlocks::IPAddress<IPv4>(a);
64✔
550

551
      a = {0xAB, 0xCD, 0xDE, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
64✔
552
      auto ipv6_1 = IPAddressBlocks::IPAddress<IPv6>(a);
64✔
553
      a = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
64✔
554
      auto ipv6_2 = IPAddressBlocks::IPAddress<IPv6>(a);
64✔
555

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

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

563
      // encoded as prefix
564
      a = {0xBF, 0xCD, 0xDE, 0xF0, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
64✔
565
      auto ipv6_range_2_min = IPAddressBlocks::IPAddress<IPv6>(a);
64✔
566
      a = {0xBF, 0xCD, 0xDE, 0xF0, 0x00, 0x00, 0x00, 0x07, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
64✔
567
      auto ipv6_range_2_max = IPAddressBlocks::IPAddress<IPv6>(a);
64✔
568

569
      auto ipv4_range_1 = IPAddressBlocks::IPAddressOrRange<IPv4>(ipv4_1);
64✔
570
      auto ipv4_range_2 = IPAddressBlocks::IPAddressOrRange<IPv4>(ipv4_range_1_min, ipv4_range_1_max);
64✔
571
      auto ipv4_range_3 = IPAddressBlocks::IPAddressOrRange<IPv4>(ipv4_range_2_min, ipv4_range_2_max);
64✔
572
      auto ipv4_range_4 = IPAddressBlocks::IPAddressOrRange<IPv4>(ipv4_2);
64✔
573

574
      auto ipv6_range_1 = IPAddressBlocks::IPAddressOrRange<IPv6>(ipv6_1);
64✔
575
      auto ipv6_range_2 = IPAddressBlocks::IPAddressOrRange<IPv6>(ipv6_range_1_min, ipv6_range_1_max);
64✔
576
      auto ipv6_range_3 = IPAddressBlocks::IPAddressOrRange<IPv6>(ipv6_range_2_min, ipv6_range_2_max);
64✔
577
      auto ipv6_range_4 = IPAddressBlocks::IPAddressOrRange<IPv6>(ipv6_2);
64✔
578

579
      std::vector<IPAddressBlocks::IPAddressOrRange<IPv4>> ipv4_ranges;
64✔
580
      if(push_ipv4_ranges) {
64✔
581
         ipv4_ranges.push_back(ipv4_range_1);
32✔
582
         ipv4_ranges.push_back(ipv4_range_2);
32✔
583
         ipv4_ranges.push_back(ipv4_range_3);
32✔
584
         ipv4_ranges.push_back(ipv4_range_4);
32✔
585
      }
586

587
      std::vector<IPAddressBlocks::IPAddressOrRange<IPv6>> ipv6_ranges;
64✔
588
      if(push_ipv6_ranges) {
64✔
589
         ipv6_ranges.push_back(ipv6_range_1);
32✔
590
         ipv6_ranges.push_back(ipv6_range_2);
32✔
591
         ipv6_ranges.push_back(ipv6_range_3);
32✔
592
         ipv6_ranges.push_back(ipv6_range_4);
32✔
593
      }
594

595
      auto ipv4_addr_choice = IPAddressBlocks::IPAddressChoice<IPv4>();
64✔
596
      if(!inherit_ipv4) {
64✔
597
         ipv4_addr_choice = IPAddressBlocks::IPAddressChoice<IPv4>(ipv4_ranges);
96✔
598
      }
599

600
      auto ipv6_addr_choice = IPAddressBlocks::IPAddressChoice<IPv6>();
64✔
601
      if(!inherit_ipv6) {
64✔
602
         ipv6_addr_choice = IPAddressBlocks::IPAddressChoice<IPv6>(ipv6_ranges);
96✔
603
      }
604

605
      auto ipv4_addr_family = IPAddressBlocks::IPAddressFamily(ipv4_addr_choice);
160✔
606
      auto ipv6_addr_family = IPAddressBlocks::IPAddressFamily(ipv6_addr_choice);
160✔
607

608
      std::vector<IPAddressBlocks::IPAddressFamily> addr_blocks;
64✔
609
      if(push_ipv4_family) {
64✔
610
         addr_blocks.push_back(ipv4_addr_family);
32✔
611
      }
612
      if(push_ipv6_family) {
64✔
613
         addr_blocks.push_back(ipv6_addr_family);
32✔
614
      }
615

616
      std::unique_ptr<IPAddressBlocks> blocks = std::make_unique<IPAddressBlocks>(addr_blocks);
64✔
617

618
      auto req = user_params().add_extension(std::move(blocks)).into_pkcs10_request(*sub_key, *rng, hash_fn);
64✔
619
      Botan::X509_Certificate cert = ca.sign_request(req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
64✔
620
      {
64✔
621
         const auto* ip_blocks = cert.v3_extensions().get_extension_object_as<IPAddressBlocks>();
64✔
622
         result.confirm("cert has IPAddrBlocks extension", ip_blocks != nullptr, true);
128✔
623

624
         const auto& dec_addr_blocks = ip_blocks->addr_blocks();
64✔
625
         if(!push_ipv4_family && !push_ipv6_family) {
64✔
626
            result.confirm("no address family entries", dec_addr_blocks.empty(), true);
32✔
627
            continue;
16✔
628
         }
629

630
         if(push_ipv4_family) {
48✔
631
            auto family = dec_addr_blocks[0];
32✔
632
            result.confirm("ipv4 family afi", ipv4_addr_family.afi() == family.afi(), true);
64✔
633
            result.test_eq("ipv4 family safi", ipv4_addr_family.safi(), family.safi());
32✔
634
            auto choice = std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(family.addr_choice());
32✔
635

636
            if(!inherit_ipv4) {
32✔
637
               auto ranges = choice.ranges().value();
16✔
638
               if(push_ipv4_ranges) {
16✔
639
                  result.test_eq("ipv4 entry 0 min", ranges[0].min().value(), ipv4_range_1.min().value());
16✔
640
                  result.test_eq("ipv4 entry 0 max", ranges[0].max().value(), ipv4_range_1.max().value());
16✔
641
                  result.test_eq("ipv4 entry 1 min", ranges[1].min().value(), ipv4_range_2.min().value());
16✔
642
                  result.test_eq("ipv4 entry 1 max", ranges[1].max().value(), ipv4_range_2.max().value());
16✔
643
                  result.test_eq("ipv4 entry 2 min", ranges[2].min().value(), ipv4_range_3.min().value());
16✔
644
                  result.test_eq("ipv4 entry 2 max", ranges[2].max().value(), ipv4_range_3.max().value());
16✔
645
                  result.test_eq("ipv4 entry 3 min", ranges[3].min().value(), ipv4_range_4.min().value());
16✔
646
                  result.test_eq("ipv4 entry 3 max", ranges[3].max().value(), ipv4_range_4.max().value());
16✔
647
               } else {
648
                  result.confirm("ipv4 range has no entries", ranges.empty(), true);
16✔
649
               }
650
            } else {
16✔
651
               result.confirm("ipv4 family inherit", choice.ranges().has_value(), false);
32✔
652
            }
653
         }
64✔
654

655
         if(push_ipv6_family) {
48✔
656
            auto family = dec_addr_blocks[dec_addr_blocks.size() - 1];
32✔
657
            result.confirm("ipv6 family afi", ipv6_addr_family.afi() == family.afi(), true);
64✔
658
            result.test_eq("ipv6 family safi", ipv6_addr_family.safi(), family.safi());
32✔
659
            auto choice = std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(family.addr_choice());
32✔
660
            if(!inherit_ipv6) {
32✔
661
               auto ranges = choice.ranges().value();
16✔
662
               if(push_ipv6_ranges) {
16✔
663
                  result.test_eq("ipv6 entry 0 min", ranges[0].min().value(), ipv6_range_1.min().value());
16✔
664
                  result.test_eq("ipv6 entry 0 max", ranges[0].max().value(), ipv6_range_1.max().value());
16✔
665
                  result.test_eq("ipv6 entry 1 min", ranges[1].min().value(), ipv6_range_2.min().value());
16✔
666
                  result.test_eq("ipv6 entry 1 max", ranges[1].max().value(), ipv6_range_2.max().value());
16✔
667
                  result.test_eq("ipv6 entry 2 min", ranges[2].min().value(), ipv6_range_3.min().value());
16✔
668
                  result.test_eq("ipv6 entry 2 max", ranges[2].max().value(), ipv6_range_3.max().value());
16✔
669
                  result.test_eq("ipv6 entry 3 min", ranges[3].min().value(), ipv6_range_4.min().value());
16✔
670
                  result.test_eq("ipv6 entry 3 max", ranges[3].max().value(), ipv6_range_4.max().value());
16✔
671
               } else {
672
                  result.confirm("ipv6 range has no entries", ranges.empty(), true);
16✔
673
               }
674
            } else {
16✔
675
               result.confirm("ipv6 family inherit", choice.ranges().has_value(), false);
32✔
676
            }
677
         }
64✔
678
      }
679
   }
320✔
680

681
   result.end_timer();
1✔
682
   return result;
2✔
683
}
2✔
684

685
Test::Result test_x509_ip_addr_blocks_extension_encode_edge_cases_ctor() {
1✔
686
   Test::Result result("X509 IP Address Block encode edge cases (ctor)");
1✔
687
   result.start_timer();
1✔
688

689
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
690

691
   auto rng = Test::new_rng(__func__);
1✔
692

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

697
   auto [ca_cert, ca, sub_key, sig_algo, hash_fn] = make_ca(*rng);
1✔
698

699
   for(size_t i = 0; i < edge_values.size(); i++) {
22✔
700
      for(size_t j = 0; j < 4; j++) {
105✔
701
         bool modify_min = j & 1;
84✔
702
         bool modify_max = (j >> 1) & 1;
84✔
703

704
         for(size_t k = 0; k < 18; k++) {
1,219✔
705
            if(!modify_min && !modify_max && (k > 0 || i > 0)) {
1,156✔
706
               // we don't modify anything, this is the extreme edge case of 0.0 ... - 255.255. ...
707
               // so we only need to do this once
708
               break;
709
            }
710

711
            std::vector<uint8_t> min_bytes(16, 0x00);
1,135✔
712
            std::vector<uint8_t> max_bytes(16, 0xFF);
1,135✔
713

714
            if(modify_min) {
1,135✔
715
               min_bytes[15 - (k < 2 ? 0 : k - 2)] = edge_values[i];
756✔
716
            }
717
            if(modify_max) {
1,135✔
718
               max_bytes[15 - (k > 15 ? 15 : k)] = edge_values[i];
756✔
719
            }
720

721
            auto address_min = IPAddressBlocks::IPAddress<IPv6>(min_bytes);
1,135✔
722
            auto address_max = IPAddressBlocks::IPAddress<IPv6>(max_bytes);
1,135✔
723

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

726
            std::vector<IPAddressBlocks::IPAddressOrRange<IPv6>> ipv6_ranges;
1,135✔
727
            ipv6_ranges.push_back(ipv6_range);
1,135✔
728

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

731
            auto ipv6_addr_family = IPAddressBlocks::IPAddressFamily(ipv6_addr_choice);
3,405✔
732

733
            std::vector<IPAddressBlocks::IPAddressFamily> addr_blocks;
1,135✔
734
            addr_blocks.push_back(ipv6_addr_family);
1,135✔
735

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

738
            auto req = user_params().add_extension(std::move(blocks)).into_pkcs10_request(*sub_key, *rng, hash_fn);
1,135✔
739
            Botan::X509_Certificate cert = ca.sign_request(req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
1,135✔
740
            {
1,135✔
741
               const auto* ip_blocks = cert.v3_extensions().get_extension_object_as<IPAddressBlocks>();
1,135✔
742
               result.confirm("cert has IPAddrBlocks extension", ip_blocks != nullptr, true);
2,270✔
743
               const auto& dec_addr_blocks = ip_blocks->addr_blocks();
1,135✔
744
               auto family = dec_addr_blocks[0];
1,135✔
745
               result.confirm("ipv6 family afi", ipv6_addr_family.afi() == family.afi(), true);
2,270✔
746
               result.test_eq("ipv6 family safi", ipv6_addr_family.safi(), family.safi());
1,135✔
747
               auto choice = std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(family.addr_choice());
1,135✔
748
               auto ranges = choice.ranges().value();
1,135✔
749

750
               result.test_eq("ipv6 edge case min", ranges[0].min().value(), ipv6_range.min().value());
2,270✔
751
               result.test_eq("ipv6 edge case max", ranges[0].max().value(), ipv6_range.max().value());
2,270✔
752
            }
2,270✔
753
         }
5,675✔
754
      }
755
   }
756
   result.end_timer();
1✔
757
   return result;
2✔
758
}
3✔
759

760
Test::Result test_x509_ip_addr_blocks_range_merge() {
1✔
761
   Test::Result result("X509 IP Address Block range merge");
1✔
762
   result.start_timer();
1✔
763

764
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
765

766
   auto rng = Test::new_rng(__func__);
1✔
767

768
   auto [ca_cert, ca, sub_key, sig_algo, hash_fn] = make_ca(*rng);
1✔
769

770
   std::vector<std::vector<std::vector<uint8_t>>> addresses = {
1✔
771
      {{11, 0, 0, 0}, {{11, 0, 0, 0}}},
772
      {{123, 123, 123, 123}, {123, 123, 123, 123}},
773
      {{10, 4, 5, 9}, {{10, 255, 0, 0}}},
774
      {{12, 0, 0, 0}, {191, 0, 0, 1}},
775
      {{190, 0, 0, 0}, {193, 0, 255, 255}},
776
      {{10, 10, 10, 10}, {10, 20, 20, 20}},
777
      {{5, 0, 0, 0}, {10, 255, 255, 255}},
778
      {{192, 0, 0, 0}, {192, 255, 255, 255}},
779
      {{11, 0, 0, 1}, {11, 255, 255, 255}},
780
   };
46✔
781

782
   std::vector<IPAddressBlocks::IPAddressOrRange<IPv4>> ipv6_ranges;
1✔
783
   for(auto pair : addresses) {
10✔
784
      auto address_min = IPAddressBlocks::IPAddress<IPv4>(pair[0]);
9✔
785
      auto address_max = IPAddressBlocks::IPAddress<IPv4>(pair[1]);
9✔
786
      auto range = IPAddressBlocks::IPAddressOrRange<IPv4>(address_min, address_max);
9✔
787
      ipv6_ranges.push_back(range);
9✔
788
   }
9✔
789

790
   auto ipv6_addr_choice = IPAddressBlocks::IPAddressChoice<IPv4>(ipv6_ranges);
1✔
791
   auto ipv6_addr_family = IPAddressBlocks::IPAddressFamily(ipv6_addr_choice);
3✔
792

793
   std::vector<IPAddressBlocks::IPAddressFamily> addr_blocks;
1✔
794
   addr_blocks.push_back(ipv6_addr_family);
1✔
795

796
   std::unique_ptr<IPAddressBlocks> blocks = std::make_unique<IPAddressBlocks>(addr_blocks);
1✔
797

798
   auto req = user_params().add_extension(std::move(blocks)).into_pkcs10_request(*sub_key, *rng, hash_fn);
1✔
799
   Botan::X509_Certificate cert = ca.sign_request(req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
1✔
800
   {
1✔
801
      const auto* ip_blocks = cert.v3_extensions().get_extension_object_as<IPAddressBlocks>();
1✔
802
      result.confirm("cert has IPAddrBlocks extension", ip_blocks != nullptr, true);
2✔
803
      const auto& dec_addr_blocks = ip_blocks->addr_blocks();
1✔
804
      auto family = dec_addr_blocks[0];
1✔
805
      auto choice = std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(family.addr_choice());
1✔
806
      auto ranges = choice.ranges().value();
1✔
807

808
      std::array<uint8_t, 4> expected_min = {5, 0, 0, 0};
1✔
809
      std::array<uint8_t, 4> expected_max = {193, 0, 255, 255};
1✔
810

811
      result.test_eq("range expected min", ranges[0].min().value(), expected_min);
2✔
812
      result.test_eq("range expected max", ranges[0].max().value(), expected_max);
2✔
813
      result.test_eq("range length", ranges.size(), 1);
1✔
814
   }
2✔
815

816
   result.end_timer();
1✔
817
   return result;
2✔
818
}
24✔
819

820
Test::Result test_x509_ip_addr_blocks_family_merge() {
1✔
821
   Test::Result result("X509 IP Address Block family merge");
1✔
822
   result.start_timer();
1✔
823

824
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
825

826
   auto rng = Test::new_rng(__func__);
1✔
827

828
   auto [ca_cert, ca, sub_key, sig_algo, hash_fn] = make_ca(*rng);
1✔
829

830
   std::vector<IPAddressBlocks::IPAddressFamily> addr_blocks;
1✔
831

832
   IPAddressBlocks::IPAddressChoice<IPv4> v4_empty_choice;
1✔
833
   IPAddressBlocks::IPAddressChoice<IPv6> v6_empty_choice;
1✔
834

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

839
   std::vector<IPAddressBlocks::IPAddressOrRange<IPv4>> v4_choice_vec{
1✔
840
      IPAddressBlocks::IPAddressOrRange<IPv4>(IPAddressBlocks::IPAddress<IPv4>({v4_addr_1}))};
1✔
841
   IPAddressBlocks::IPAddressChoice<IPv4> v4_choice_dupl(v4_choice_vec);
1✔
842
   result.confirm(
2✔
843
      "IPAddressChoice v4 merges ranges already in constructor", v4_choice_dupl.ranges().value().size() == 1, true);
1✔
844
   IPAddressBlocks::IPAddressFamily v4_fam_dupl(v4_choice_dupl, 0);
3✔
845

846
   uint8_t v6_bytes_1[16] = {123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123};
1✔
847
   IPAddressBlocks::IPAddress<IPv6> v6_addr_1(v6_bytes_1);
1✔
848

849
   std::vector<IPAddressBlocks::IPAddressOrRange<IPv6>> v6_choice_vec{
1✔
850
      IPAddressBlocks::IPAddressOrRange<IPv6>(IPAddressBlocks::IPAddress<IPv6>({v6_addr_1}))};
1✔
851
   IPAddressBlocks::IPAddressChoice<IPv6> v6_choice_dupl(v6_choice_vec);
1✔
852
   result.confirm(
2✔
853
      "IPAddressChoice v6 merges already in constructor", v6_choice_dupl.ranges().value().size() == 1, true);
1✔
854
   IPAddressBlocks::IPAddressFamily v6_fam_dupl(v6_choice_dupl, 0);
3✔
855

856
   IPAddressBlocks::IPAddressFamily v4_empty_fam(v4_empty_choice);
2✔
857
   IPAddressBlocks::IPAddressFamily v6_empty_fam(v6_empty_choice);
2✔
858

859
   IPAddressBlocks::IPAddressFamily v4_empty_fam_safi(v4_empty_choice, 2);
2✔
860
   IPAddressBlocks::IPAddressFamily v6_empty_fam_safi(v6_empty_choice, 2);
2✔
861

862
   /*
863
   considering the push order, the resulting order should be
864
   [0] v4 no safi
865
   [2] v4 safi
866
   [1] v6 no safi
867
   [3] v6 safi
868
   */
869
   for(size_t i = 0; i < 3; i++) {
4✔
870
      addr_blocks.push_back(v4_empty_fam_safi);
3✔
871
      addr_blocks.push_back(v6_empty_fam);
3✔
872
      addr_blocks.push_back(v4_fam_dupl);
3✔
873
      addr_blocks.push_back(v6_empty_fam_safi);
3✔
874
      addr_blocks.push_back(v6_fam_dupl);
3✔
875
      addr_blocks.push_back(v4_empty_fam);
3✔
876
   }
877

878
   std::vector<IPAddressBlocks::IPAddressFamily> expected_blocks = {
1✔
879
      v4_empty_fam, v4_fam_dupl, v4_empty_fam_safi, v6_empty_fam, v6_fam_dupl, v6_empty_fam_safi};
7✔
880

881
   std::unique_ptr<IPAddressBlocks> blocks = std::make_unique<IPAddressBlocks>(addr_blocks);
1✔
882

883
   auto req = user_params().add_extension(std::move(blocks)).into_pkcs10_request(*sub_key, *rng, hash_fn);
1✔
884
   Botan::X509_Certificate cert = ca.sign_request(req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
1✔
885

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

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

892
   bool sorted = true;
1✔
893
   for(size_t i = 0; i < dec_blocks.size() - 1; i++) {
6✔
894
      const IPAddressBlocks::IPAddressFamily& a = dec_blocks[i];
5✔
895
      const IPAddressBlocks::IPAddressFamily& b = dec_blocks[i + 1];
5✔
896

897
      uint32_t afam_a = a.afi();
5✔
898
      if(a.safi().has_value()) {
5✔
899
         afam_a = static_cast<uint32_t>(afam_a << 8) | a.safi().value();
3✔
900
      } else {
901
         afam_a = static_cast<uint32_t>(afam_a << 8);
2✔
902
      }
903

904
      uint32_t afam_b = b.afi();
5✔
905
      if(b.safi().has_value()) {
5✔
906
         afam_b = static_cast<uint32_t>(afam_b << 8) | b.safi().value();
4✔
907
      } else {
908
         afam_b = static_cast<uint32_t>(afam_b << 8);
1✔
909
      }
910

911
      if(afam_a > afam_b) {
5✔
912
         sorted = false;
913
         break;
914
      }
915
   }
916

917
   result.confirm("blocks got sorted", sorted, true);
2✔
918

919
   for(size_t i = 0; i < dec_blocks.size(); i++) {
7✔
920
      const IPAddressBlocks::IPAddressFamily& dec = dec_blocks[i];
6✔
921
      const IPAddressBlocks::IPAddressFamily& exp = expected_blocks[i];
6✔
922

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

926
      if((exp.afi() == 1) && (dec.afi() == 1)) {
6✔
927
         auto dec_choice = std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(dec.addr_choice());
3✔
928
         auto exp_choice = std::get<IPAddressBlocks::IPAddressChoice<IPv4>>(exp.addr_choice());
3✔
929

930
         if(!exp_choice.ranges().has_value()) {
3✔
931
            result.confirm(
2✔
932
               "block ranges should inherit at index " + std::to_string(i), dec_choice.ranges().has_value(), false);
6✔
933
         } else {
934
            result.confirm(
1✔
935
               "block ranges should not inherit at index " + std::to_string(i), dec_choice.ranges().has_value(), true);
3✔
936

937
            if(dec_choice.ranges().has_value() == false) {
1✔
938
               continue;
×
939
            }
940

941
            auto dec_ranges = dec_choice.ranges().value();
1✔
942
            auto exp_ranges = exp_choice.ranges().value();
1✔
943
            result.confirm("block ranges got merged lengthwise at index " + std::to_string(i),
2✔
944
                           dec_ranges.size() == exp_ranges.size(),
1✔
945
                           true);
946

947
            if(dec_ranges.size() != exp_ranges.size()) {
1✔
948
               continue;
×
949
            }
950

951
            for(size_t j = 0; j < exp_ranges.size(); j++) {
2✔
952
               result.test_eq(
2✔
953
                  "block ranges min got merged valuewise at indices " + std::to_string(i) + "," + std::to_string(j),
4✔
954
                  exp_ranges[j].min().value(),
1✔
955
                  dec_ranges[j].min().value());
1✔
956
               result.test_eq(
2✔
957
                  "block ranges max got merged valuewise at indices " + std::to_string(i) + "," + std::to_string(j),
4✔
958
                  exp_ranges[j].max().value(),
1✔
959
                  dec_ranges[j].max().value());
2✔
960
            }
961
         }
1✔
962
      } else if((exp.afi() == 2) && (dec.afi() == 2)) {
7✔
963
         auto dec_choice = std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(dec.addr_choice());
3✔
964
         auto exp_choice = std::get<IPAddressBlocks::IPAddressChoice<IPv6>>(exp.addr_choice());
3✔
965

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

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

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

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

987
            for(size_t j = 0; j < exp_ranges.size(); j++) {
2✔
988
               result.test_eq(
2✔
989
                  "block ranges min got merged valuewise at indices " + std::to_string(i) + "," + std::to_string(j),
4✔
990
                  exp_ranges[j].min().value(),
1✔
991
                  dec_ranges[j].min().value());
1✔
992
               result.test_eq(
2✔
993
                  "block ranges max got merged valuewise at indices " + std::to_string(i) + "," + std::to_string(j),
4✔
994
                  exp_ranges[j].max().value(),
1✔
995
                  dec_ranges[j].max().value());
2✔
996
            }
997
         }
1✔
998
      }
4✔
999
   }
1000

1001
   result.end_timer();
1✔
1002
   return result;
2✔
1003
}
19✔
1004

1005
Test::Result test_x509_ip_addr_blocks_path_validation_success_builder() {
1✔
1006
   Test::Result result("X509 IP Address Blocks path validation success (builder)");
1✔
1007
   result.start_timer();
1✔
1008

1009
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
1010
   auto rng = Test::new_rng(__func__);
1✔
1011

1012
   /*
1013
   Creates a certificate chain of length 4.
1014
   Root: ipv4 and ipv6
1015
   Inherit: has both values as 'inherit'
1016
   Dynamic: has either both 'inherit', both with values, or just one with a value
1017
   Subject: both ipv4 and ipv6 as a subset of Root / Dynamic
1018
   */
1019

1020
   // Root cert
1021
   std::unique_ptr<IPAddressBlocks> root_blocks = std::make_unique<IPAddressBlocks>();
1✔
1022

1023
   root_blocks->add_address<IPv4>({120, 0, 0, 1}, {130, 140, 150, 160}, 42);
1✔
1024
   root_blocks->add_address<IPv4>({10, 0, 0, 1}, {10, 255, 255, 255}, 42);
1✔
1025

1026
   root_blocks->add_address<IPv6>(
1✔
1027
      {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
1028
      {0xA0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
1029
   root_blocks->add_address<IPv6>(
1✔
1030
      {0xA2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
1031
      {0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
1032

1033
   // Inherit cert
1034
   std::unique_ptr<IPAddressBlocks> inherit_blocks = std::make_unique<IPAddressBlocks>();
1✔
1035

1036
   inherit_blocks->inherit<IPv4>(42);
1✔
1037
   inherit_blocks->inherit<IPv6>();
1✔
1038

1039
   // Subject cert
1040
   std::unique_ptr<IPAddressBlocks> sub_blocks = std::make_unique<IPAddressBlocks>();
1✔
1041

1042
   sub_blocks->add_address<IPv4>({124, 0, 255, 0}, {126, 0, 0, 1}, 42);
1✔
1043
   sub_blocks->add_address<IPv4>({10, 0, 2, 1}, {10, 42, 0, 255}, 42);
1✔
1044

1045
   sub_blocks->add_address<IPv6>(
1✔
1046
      {0x00, 0x00, 0x00, 0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
1047
      {0x0D, 0x00, 0x00, 0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
1048

1049
   auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] =
1✔
1050
      make_ca(*rng, ca_params().add_extension(std::move(root_blocks)));
2✔
1051
   auto [inherit_cert, inherit_ca] = make_and_sign_ca(std::move(inherit_blocks), root_ca, *rng);
2✔
1052

1053
   Botan::Certificate_Store_In_Memory trusted;
1✔
1054
   trusted.add_certificate(root_cert);
1✔
1055

1056
   for(size_t i = 0; i < 4; i++) {
5✔
1057
      bool include_v4 = i & 1;
4✔
1058
      bool include_v6 = (i >> 1) & 1;
4✔
1059

1060
      // Dynamic Cert
1061
      std::unique_ptr<IPAddressBlocks> dyn_blocks = std::make_unique<IPAddressBlocks>();
4✔
1062
      if(include_v4) {
4✔
1063
         dyn_blocks->add_address<IPv4>({122, 0, 0, 255}, {128, 255, 255, 255}, 42);
2✔
1064
         dyn_blocks->add_address<IPv4>({10, 0, 0, 255}, {10, 255, 0, 1}, 42);
2✔
1065
      } else {
1066
         dyn_blocks->inherit<IPv4>(42);
2✔
1067
      }
1068

1069
      if(include_v6) {
4✔
1070
         dyn_blocks->add_address<IPv6>(
2✔
1071
            {0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
1072
            {0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
1073
      } else {
1074
         dyn_blocks->inherit<IPv6>();
2✔
1075
      }
1076

1077
      auto [dyn_cert, dyn_ca] = make_and_sign_ca(std::move(dyn_blocks), inherit_ca, *rng);
8✔
1078

1079
      const auto sub_req = user_params().add_extension(sub_blocks->copy()).into_pkcs10_request(*sub_key, *rng, hash_fn);
4✔
1080

1081
      const auto sub_cert = dyn_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
4✔
1082

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

1086
      Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
4✔
1087
      result.require("path validation succeeds", path_result.successful_validation());
4✔
1088
   }
8✔
1089

1090
   result.end_timer();
1✔
1091
   return result;
2✔
1092
}
7✔
1093

1094
Test::Result test_x509_ip_addr_blocks_path_validation_success_ctor() {
1✔
1095
   Test::Result result("X509 IP Address Block path validation success (ctor)");
1✔
1096
   result.start_timer();
1✔
1097

1098
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
1099
   auto rng = Test::new_rng(__func__);
1✔
1100

1101
   /*
1102
   Creates a certificate chain of length 4.
1103
   Root: ipv4 and ipv6
1104
   Inherit: has both values as 'inherit'
1105
   Dynamic: has either both 'inherit', both with values, or just one with a value
1106
   Subject: both ipv4 and ipv6 as a subset of Root / Dynamic
1107
   */
1108

1109
   // Root cert
1110
   std::vector<uint8_t> a = {120, 0, 0, 1};
1✔
1111
   auto root_ipv4_range_1_min = IPAddressBlocks::IPAddress<IPv4>{a};
1✔
1112
   a = {130, 140, 150, 160};
1✔
1113
   auto root_ipv4_range_1_max = IPAddressBlocks::IPAddress<IPv4>{a};
1✔
1114

1115
   a = {10, 0, 0, 1};
1✔
1116
   auto root_ipv4_range_2_min = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1117
   a = {10, 255, 255, 255};
1✔
1118
   auto root_ipv4_range_2_max = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1119

1120
   a = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1✔
1121
   auto root_ipv6_range_1_min = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1122
   a = {0xA0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1✔
1123
   auto root_ipv6_range_1_max = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1124

1125
   a = {0xA2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1✔
1126
   auto root_ipv6_range_2_min = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1127
   a = {0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1✔
1128
   auto root_ipv6_range_2_max = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1129

1130
   auto root_ipv4_range_1 = IPAddressBlocks::IPAddressOrRange<IPv4>(root_ipv4_range_1_min, root_ipv4_range_1_max);
1✔
1131
   auto root_ipv4_range_2 = IPAddressBlocks::IPAddressOrRange<IPv4>(root_ipv4_range_2_min, root_ipv4_range_2_max);
1✔
1132
   auto root_ipv6_range_1 = IPAddressBlocks::IPAddressOrRange<IPv6>(root_ipv6_range_1_min, root_ipv6_range_1_max);
1✔
1133
   auto root_ipv6_range_2 = IPAddressBlocks::IPAddressOrRange<IPv6>(root_ipv6_range_2_min, root_ipv6_range_2_max);
1✔
1134

1135
   auto root_ipv4_ranges = {root_ipv4_range_1, root_ipv4_range_2};
1✔
1136
   auto root_ipv6_ranges = {root_ipv6_range_1, root_ipv6_range_2};
1✔
1137

1138
   auto root_ipv4_choice = IPAddressBlocks::IPAddressChoice<IPv4>(root_ipv4_ranges);
1✔
1139
   auto root_ipv6_choice = IPAddressBlocks::IPAddressChoice<IPv6>(root_ipv6_ranges);
1✔
1140

1141
   auto root_ipv4_family = IPAddressBlocks::IPAddressFamily(root_ipv4_choice, 42);
3✔
1142
   auto root_ipv6_family = IPAddressBlocks::IPAddressFamily(root_ipv6_choice);
3✔
1143

1144
   auto root_addr_blocks = {root_ipv4_family, root_ipv6_family};
6✔
1145
   std::unique_ptr<IPAddressBlocks> root_blocks = std::make_unique<IPAddressBlocks>(root_addr_blocks);
1✔
1146

1147
   // Inherit cert
1148
   auto inherit_ipv4_choice = IPAddressBlocks::IPAddressChoice<IPv4>();
1✔
1149
   auto inherit_ipv6_choice = IPAddressBlocks::IPAddressChoice<IPv6>();
1✔
1150

1151
   auto inherit_ipv4_family = IPAddressBlocks::IPAddressFamily(inherit_ipv4_choice, 42);
2✔
1152
   auto inherit_ipv6_family = IPAddressBlocks::IPAddressFamily(inherit_ipv6_choice);
2✔
1153

1154
   auto inherit_addr_blocks = {inherit_ipv4_family, inherit_ipv6_family};
6✔
1155
   std::unique_ptr<IPAddressBlocks> inherit_blocks = std::make_unique<IPAddressBlocks>(inherit_addr_blocks);
1✔
1156

1157
   // Dynamic Cert
1158
   a = {122, 0, 0, 255};
1✔
1159
   auto dyn_ipv4_range_1_min = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1160
   a = {128, 255, 255, 255};
1✔
1161
   auto dyn_ipv4_range_1_max = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1162
   a = {10, 0, 0, 255};
1✔
1163
   auto dyn_ipv4_range_2_min = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1164
   a = {10, 255, 0, 1};
1✔
1165
   auto dyn_ipv4_range_2_max = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1166

1167
   a = {0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1✔
1168
   auto dyn_ipv6_range_1_min = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1169
   a = {0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1✔
1170
   auto dyn_ipv6_range_1_max = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1171

1172
   auto dyn_ipv4_range_1 = IPAddressBlocks::IPAddressOrRange<IPv4>(dyn_ipv4_range_1_min, dyn_ipv4_range_1_max);
1✔
1173
   auto dyn_ipv4_range_2 = IPAddressBlocks::IPAddressOrRange<IPv4>(dyn_ipv4_range_2_min, dyn_ipv4_range_2_max);
1✔
1174
   auto dyn_ipv6_range = IPAddressBlocks::IPAddressOrRange<IPv6>(dyn_ipv6_range_1_min, dyn_ipv6_range_1_max);
1✔
1175

1176
   auto dyn_ipv4_ranges = {dyn_ipv4_range_1, dyn_ipv4_range_2};
1✔
1177
   auto dyn_ipv6_ranges = {dyn_ipv6_range};
1✔
1178

1179
   // Subject cert
1180
   a = {124, 0, 255, 0};
1✔
1181
   auto sub_ipv4_range_1_min = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1182
   a = {126, 0, 0, 1};
1✔
1183
   auto sub_ipv4_range_1_max = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1184

1185
   a = {10, 0, 2, 1};
1✔
1186
   auto sub_ipv4_range_2_min = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1187
   a = {10, 42, 0, 255};
1✔
1188
   auto sub_ipv4_range_2_max = IPAddressBlocks::IPAddress<IPv4>(a);
1✔
1189

1190
   a = {0x00, 0x00, 0x00, 0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1✔
1191
   auto sub_ipv6_range_1_min = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1192
   a = {0x0D, 0x00, 0x00, 0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1✔
1193
   auto sub_ipv6_range_1_max = IPAddressBlocks::IPAddress<IPv6>(a);
1✔
1194

1195
   auto sub_ipv4_range_1 = IPAddressBlocks::IPAddressOrRange<IPv4>(sub_ipv4_range_1_min, sub_ipv4_range_1_max);
1✔
1196
   auto sub_ipv4_range_2 = IPAddressBlocks::IPAddressOrRange<IPv4>(sub_ipv4_range_2_min, sub_ipv4_range_2_max);
1✔
1197
   auto sub_ipv6_range = IPAddressBlocks::IPAddressOrRange<IPv6>(sub_ipv6_range_1_min, sub_ipv6_range_1_max);
1✔
1198

1199
   auto sub_ipv4_ranges = {sub_ipv4_range_1, sub_ipv4_range_2};
1✔
1200
   auto sub_ipv6_ranges = {sub_ipv6_range};
1✔
1201

1202
   auto sub_ipv4_choice = IPAddressBlocks::IPAddressChoice<IPv4>(sub_ipv4_ranges);
1✔
1203
   auto sub_ipv6_choice = IPAddressBlocks::IPAddressChoice<IPv6>(sub_ipv6_ranges);
1✔
1204

1205
   auto sub_ipv4_family = IPAddressBlocks::IPAddressFamily(sub_ipv4_choice, 42);
3✔
1206
   auto sub_ipv6_family = IPAddressBlocks::IPAddressFamily(sub_ipv6_choice);
3✔
1207

1208
   auto sub_addr_blocks = {sub_ipv4_family, sub_ipv6_family};
6✔
1209
   std::unique_ptr<IPAddressBlocks> sub_blocks = std::make_unique<IPAddressBlocks>(sub_addr_blocks);
1✔
1210

1211
   auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] =
1✔
1212
      make_ca(*rng, ca_params().add_extension(std::move(root_blocks)));
2✔
1213
   auto [inherit_cert, inherit_ca] = make_and_sign_ca(std::move(inherit_blocks), root_ca, *rng);
2✔
1214

1215
   Botan::Certificate_Store_In_Memory trusted;
1✔
1216
   trusted.add_certificate(root_cert);
1✔
1217

1218
   for(size_t i = 0; i < 4; i++) {
5✔
1219
      bool include_v4 = i & 1;
4✔
1220
      bool include_v6 = (i >> 1) & 1;
4✔
1221

1222
      auto dyn_ipv4_choice =
4✔
1223
         IPAddressBlocks::IPAddressChoice<IPv4>(include_v4 ? std::optional(dyn_ipv4_ranges) : std::nullopt);
8✔
1224
      auto dyn_ipv6_choice =
4✔
1225
         IPAddressBlocks::IPAddressChoice<IPv6>(include_v6 ? std::optional(dyn_ipv6_ranges) : std::nullopt);
8✔
1226

1227
      auto dyn_ipv4_family = IPAddressBlocks::IPAddressFamily(dyn_ipv4_choice, 42);
10✔
1228
      auto dyn_ipv6_family = IPAddressBlocks::IPAddressFamily(dyn_ipv6_choice);
10✔
1229

1230
      auto dyn_addr_blocks = {dyn_ipv4_family, dyn_ipv6_family};
24✔
1231
      std::unique_ptr<IPAddressBlocks> dyn_blocks = std::make_unique<IPAddressBlocks>(dyn_addr_blocks);
4✔
1232

1233
      auto [dyn_cert, dyn_ca] = make_and_sign_ca(std::move(dyn_blocks), inherit_ca, *rng);
8✔
1234

1235
      const auto sub_req = user_params().add_extension(sub_blocks->copy()).into_pkcs10_request(*sub_key, *rng, hash_fn);
4✔
1236
      const auto sub_cert = dyn_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
4✔
1237

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

1241
      Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
4✔
1242
      result.require("path validation succeeds", path_result.successful_validation());
4✔
1243
   }
28✔
1244

1245
   result.end_timer();
1✔
1246
   return result;
2✔
1247
}
25✔
1248

1249
Test::Result test_x509_ip_addr_blocks_path_validation_failure_builder() {
1✔
1250
   Test::Result result("X509 IP Address Blocks path validation failure (builder)");
1✔
1251
   result.start_timer();
1✔
1252

1253
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
1254
   auto rng = Test::new_rng(__func__);
1✔
1255

1256
   for(size_t i = 0; i < 7; i++) {
8✔
1257
      bool all_inherit = (i == 0);
7✔
1258
      bool different_safi = (i == 1);
7✔
1259
      bool too_small_subrange = (i == 2);
7✔
1260
      bool too_large_subrange = (i == 3);
7✔
1261
      bool no_more_issuer_ranges = (i == 4);
7✔
1262
      bool empty_issuer_ranges = (i == 5);
7✔
1263
      bool nullptr_extensions = (i == 6);
7✔
1264

1265
      // Root cert
1266
      std::unique_ptr<IPAddressBlocks> root_blocks = std::make_unique<IPAddressBlocks>();
7✔
1267
      if(!all_inherit) {
7✔
1268
         root_blocks->add_address<IPv4>({120, 0, 0, 1}, {130, 140, 150, 160}, 42);
6✔
1269
      } else {
1270
         root_blocks->inherit<IPv4>(42);
1✔
1271
      }
1272

1273
      auto root_params = ca_params();
7✔
1274
      if(!nullptr_extensions) {
7✔
1275
         root_params.add_extension(std::move(root_blocks));
12✔
1276
      }
1277
      auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] = make_ca(*rng, root_params);
7✔
1278

1279
      // Issuer Cert
1280
      std::unique_ptr<IPAddressBlocks> iss_blocks = std::make_unique<IPAddressBlocks>();
7✔
1281
      if(!all_inherit) {
7✔
1282
         if(empty_issuer_ranges) {
6✔
1283
            iss_blocks->restrict<IPv4>(42);
1✔
1284
         } else {
1285
            iss_blocks->add_address<IPv4>({122, 0, 0, 255}, {128, 255, 255, 255}, 42);
5✔
1286
         }
1287
      } else {
1288
         iss_blocks->inherit<IPv4>(42);
1✔
1289
      }
1290

1291
      auto [iss_cert, iss_ca] = make_and_sign_ca(std::move(iss_blocks), root_ca, *rng);
14✔
1292

1293
      // Subject cert
1294
      std::unique_ptr<IPAddressBlocks> sub_blocks = std::make_unique<IPAddressBlocks>();
7✔
1295

1296
      uint8_t safi = different_safi ? 41 : 42;
7✔
1297

1298
      if(!all_inherit) {
7✔
1299
         if(too_small_subrange) {
6✔
1300
            sub_blocks->add_address<IPv4>({118, 0, 255, 0}, {126, 0, 0, 1}, safi);
1✔
1301
         } else if(too_large_subrange) {
5✔
1302
            sub_blocks->add_address<IPv4>({124, 0, 255, 0}, {134, 0, 0, 1}, safi);
1✔
1303
         } else if(no_more_issuer_ranges) {
4✔
1304
            sub_blocks->add_address<IPv4>({140, 0, 0, 1}, {150, 0, 0, 1}, safi);
1✔
1305
         } else {
1306
            sub_blocks->add_address<IPv4>({124, 0, 255, 0}, {126, 0, 0, 1}, safi);
3✔
1307
         }
1308
      } else {
1309
         sub_blocks->inherit<IPv4>(safi);
1✔
1310
      }
1311

1312
      auto sub_req = user_params().add_extension(std::move(sub_blocks)).into_pkcs10_request(*sub_key, *rng, hash_fn);
7✔
1313
      Botan::X509_Certificate sub_cert =
7✔
1314
         iss_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
7✔
1315

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

1319
      Botan::Certificate_Store_In_Memory trusted;
7✔
1320
      trusted.add_certificate(root_cert);
7✔
1321

1322
      Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
7✔
1323
      result.require("path validation fails", !path_result.successful_validation());
7✔
1324
   }
15✔
1325

1326
   result.end_timer();
1✔
1327
   return result;
1✔
1328
}
8✔
1329

1330
Test::Result test_x509_ip_addr_blocks_path_validation_failure_ctor() {
1✔
1331
   Test::Result result("X509 IP Address Block path validation failure (ctor)");
1✔
1332
   result.start_timer();
1✔
1333

1334
   using Botan::Cert_Extension::IPAddressBlocks;
1✔
1335
   auto rng = Test::new_rng(__func__);
1✔
1336

1337
   for(size_t i = 0; i < 7; i++) {
8✔
1338
      bool all_inherit = (i == 0);
7✔
1339
      bool different_safi = (i == 1);
7✔
1340
      bool too_small_subrange = (i == 2);
7✔
1341
      bool too_large_subrange = (i == 3);
7✔
1342
      bool no_more_issuer_ranges = (i == 4);
7✔
1343
      bool empty_issuer_ranges = (i == 5);
7✔
1344
      bool nullptr_extensions = (i == 6);
7✔
1345

1346
      // Root cert
1347
      std::vector<uint8_t> a = {120, 0, 0, 1};
7✔
1348
      auto root_range_1_min = IPAddressBlocks::IPAddress<IPv4>{a};
7✔
1349
      a = {130, 140, 150, 160};
7✔
1350
      auto root_range_1_max = IPAddressBlocks::IPAddress<IPv4>{a};
7✔
1351

1352
      auto root_range_1 = IPAddressBlocks::IPAddressOrRange<IPv4>(root_range_1_min, root_range_1_max);
7✔
1353
      auto root_ranges = {root_range_1};
7✔
1354
      auto root_choice =
7✔
1355
         IPAddressBlocks::IPAddressChoice<IPv4>(all_inherit ? std::nullopt : std::optional(root_ranges));
14✔
1356
      auto root_family = IPAddressBlocks::IPAddressFamily(root_choice, 42);
20✔
1357
      auto root_addr_blocks = {root_family};
21✔
1358
      std::unique_ptr<IPAddressBlocks> root_blocks = std::make_unique<IPAddressBlocks>(root_addr_blocks);
7✔
1359

1360
      auto root_params = ca_params();
7✔
1361
      if(!nullptr_extensions) {
7✔
1362
         root_params.add_extension(std::move(root_blocks));
12✔
1363
      }
1364
      auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] = make_ca(*rng, root_params);
7✔
1365

1366
      // Issuer Cert
1367
      a = {122, 0, 0, 255};
7✔
1368
      auto iss_range_1_min = IPAddressBlocks::IPAddress<IPv4>(a);
7✔
1369
      a = {128, 255, 255, 255};
7✔
1370
      auto iss_range_1_max = IPAddressBlocks::IPAddress<IPv4>(a);
7✔
1371
      auto iss_range_1 = IPAddressBlocks::IPAddressOrRange<IPv4>(iss_range_1_min, iss_range_1_max);
7✔
1372

1373
      std::vector<IPAddressBlocks::IPAddressOrRange<IPv4>> iss_ranges;
7✔
1374

1375
      if(!empty_issuer_ranges) {
7✔
1376
         iss_ranges.push_back(iss_range_1);
6✔
1377
      }
1378

1379
      auto iss_choice = IPAddressBlocks::IPAddressChoice<IPv4>(all_inherit ? std::nullopt : std::optional(iss_ranges));
19✔
1380
      auto iss_family = IPAddressBlocks::IPAddressFamily(iss_choice, 42);
20✔
1381
      auto iss_addr_blocks = {iss_family};
21✔
1382
      std::unique_ptr<IPAddressBlocks> iss_blocks = std::make_unique<IPAddressBlocks>(iss_addr_blocks);
7✔
1383
      auto [iss_cert, iss_ca] = make_and_sign_ca(std::move(iss_blocks), root_ca, *rng);
14✔
1384

1385
      // Subject cert
1386
      if(too_small_subrange) {
7✔
1387
         a = {118, 0, 255, 0};
2✔
1388
      } else if(no_more_issuer_ranges) {
6✔
1389
         a = {140, 0, 0, 1};
2✔
1390
      } else {
1391
         a = {124, 0, 255, 0};
10✔
1392
      }
1393

1394
      auto sub_range_1_min = IPAddressBlocks::IPAddress<IPv4>(a);
7✔
1395
      if(too_large_subrange) {
7✔
1396
         a = {134, 0, 0, 1};
2✔
1397
      } else if(no_more_issuer_ranges) {
6✔
1398
         a = {150, 0, 0, 1};
2✔
1399
      } else {
1400
         a = {126, 0, 0, 1};
10✔
1401
      }
1402
      auto sub_range_1_max = IPAddressBlocks::IPAddress<IPv4>(a);
7✔
1403

1404
      auto sub_range_1 = IPAddressBlocks::IPAddressOrRange<IPv4>(sub_range_1_min, sub_range_1_max);
7✔
1405
      auto sub_ranges = {sub_range_1};
7✔
1406
      auto sub_choice = IPAddressBlocks::IPAddressChoice<IPv4>(all_inherit ? std::nullopt : std::optional(sub_ranges));
14✔
1407
      auto sub_family = IPAddressBlocks::IPAddressFamily(sub_choice, different_safi ? 41 : 42);
26✔
1408

1409
      auto sub_addr_blocks = {sub_family};
21✔
1410
      std::unique_ptr<IPAddressBlocks> sub_blocks = std::make_unique<IPAddressBlocks>(sub_addr_blocks);
7✔
1411

1412
      auto sub_req = user_params().add_extension(std::move(sub_blocks)).into_pkcs10_request(*sub_key, *rng, hash_fn);
7✔
1413
      Botan::X509_Certificate sub_cert =
7✔
1414
         iss_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
7✔
1415

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

1419
      Botan::Certificate_Store_In_Memory trusted;
7✔
1420
      trusted.add_certificate(root_cert);
7✔
1421

1422
      Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
7✔
1423
      result.require("path validation fails", !path_result.successful_validation());
7✔
1424
   }
82✔
1425

1426
   result.end_timer();
1✔
1427
   return result;
1✔
1428
}
8✔
1429

1430
Test::Result test_x509_as_blocks_rfc3779_example() {
1✔
1431
   Test::Result result("X509 AS Blocks rfc3779 example");
1✔
1432
   result.start_timer();
1✔
1433

1434
   using Botan::Cert_Extension::ASBlocks;
1✔
1435
   auto rng = Test::new_rng(__func__);
1✔
1436

1437
   // construct like in https://datatracker.ietf.org/doc/html/rfc3779#page-21
1438
   std::unique_ptr<ASBlocks> blocks = std::make_unique<ASBlocks>();
1✔
1439
   blocks->add_asnum(135);
1✔
1440
   blocks->add_asnum(3000, 3999);
1✔
1441
   blocks->add_asnum(5001);
1✔
1442
   blocks->inherit_rdi();
1✔
1443

1444
   auto cert = make_self_signed(*rng, ca_params().add_extension(std::move(blocks)));
2✔
1445
   auto bits = cert.v3_extensions().get_extension_bits(ASBlocks::static_oid());
1✔
1446

1447
   result.test_eq(
1✔
1448
      "extension is encoded as specified", bits, "301AA014301202020087300802020BB802020F9F02021389A1020500");
1449

1450
   auto as_idents = cert.v3_extensions().get_extension_object_as<ASBlocks>()->as_identifiers();
1✔
1451
   auto as_ids = as_idents.asnum().value().ranges().value();
1✔
1452

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

1455
   result.end_timer();
1✔
1456
   return result;
2✔
1457
}
3✔
1458

1459
Test::Result test_x509_as_blocks_encode_builder() {
1✔
1460
   Test::Result result("X509 IP Address Blocks encode (builder)");
1✔
1461
   result.start_timer();
1✔
1462

1463
   using Botan::Cert_Extension::ASBlocks;
1✔
1464
   auto rng = Test::new_rng(__func__);
1✔
1465

1466
   std::unique_ptr<ASBlocks> blocks = std::make_unique<ASBlocks>();
1✔
1467

1468
   blocks->add_rdi(10);
1✔
1469
   blocks->add_rdi(20, 30);
1✔
1470
   blocks->add_rdi(42, 300);
1✔
1471
   blocks->add_rdi(9, 301);
1✔
1472

1473
   blocks->inherit_asnum();
1✔
1474
   blocks->add_asnum(20);
1✔
1475
   // this overwrites the previous two
1476
   blocks->restrict_asnum();
1✔
1477

1478
   auto cert = make_self_signed(*rng, ca_params().add_extension(std::move(blocks)));
2✔
1479
   auto bits = cert.v3_extensions().get_extension_bits(ASBlocks::static_oid());
1✔
1480

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

1483
   result.end_timer();
1✔
1484
   return result;
1✔
1485
}
2✔
1486

1487
Test::Result test_x509_as_blocks_extension_encode_ctor() {
1✔
1488
   Test::Result result("X509 AS Blocks encode (ctor)");
1✔
1489
   result.start_timer();
1✔
1490

1491
   using Botan::Cert_Extension::ASBlocks;
1✔
1492

1493
   auto rng = Test::new_rng(__func__);
1✔
1494

1495
   auto [ca_cert, ca, sub_key, sig_algo, hash_fn] = make_ca(*rng);
1✔
1496

1497
   for(size_t i = 0; i < 16; i++) {
17✔
1498
      bool push_asnum = i & 1;
16✔
1499
      bool push_rdi = (i >> 1) & 1;
16✔
1500
      bool include_asnum = (i >> 2) & 1;
16✔
1501
      bool include_rdi = (i >> 3) & 1;
16✔
1502
      if(!include_asnum && !include_rdi) {
16✔
1503
         continue;
4✔
1504
      }
1505

1506
      ASBlocks::ASIdOrRange asnum_id_or_range0 = ASBlocks::ASIdOrRange(0, 999);
12✔
1507
      ASBlocks::ASIdOrRange asnum_id_or_range1 = ASBlocks::ASIdOrRange(5042);
12✔
1508
      ASBlocks::ASIdOrRange asnum_id_or_range2 = ASBlocks::ASIdOrRange(5043, 4294967295);
12✔
1509

1510
      ASBlocks::ASIdOrRange rdi_id_or_range0 = ASBlocks::ASIdOrRange(1234, 5678);
12✔
1511
      ASBlocks::ASIdOrRange rdi_id_or_range1 = ASBlocks::ASIdOrRange(32768);
12✔
1512
      ASBlocks::ASIdOrRange rdi_id_or_range2 = ASBlocks::ASIdOrRange(32769, 4294967295);
12✔
1513

1514
      std::vector<ASBlocks::ASIdOrRange> as_ranges;
12✔
1515
      if(push_asnum) {
12✔
1516
         as_ranges.push_back(asnum_id_or_range0);
6✔
1517
         as_ranges.push_back(asnum_id_or_range1);
6✔
1518
         as_ranges.push_back(asnum_id_or_range2);
6✔
1519
      }
1520

1521
      std::vector<ASBlocks::ASIdOrRange> rdi_ranges;
12✔
1522
      if(push_rdi) {
12✔
1523
         rdi_ranges.push_back(rdi_id_or_range0);
6✔
1524
         rdi_ranges.push_back(rdi_id_or_range1);
6✔
1525
         rdi_ranges.push_back(rdi_id_or_range2);
6✔
1526
      }
1527

1528
      ASBlocks::ASIdentifierChoice asnum = ASBlocks::ASIdentifierChoice(as_ranges);
12✔
1529
      ASBlocks::ASIdentifierChoice rdi = ASBlocks::ASIdentifierChoice(rdi_ranges);
12✔
1530

1531
      ASBlocks::ASIdentifiers ident = ASBlocks::ASIdentifiers(include_asnum ? std::optional(asnum) : std::nullopt,
32✔
1532
                                                              include_rdi ? std::optional(rdi) : std::nullopt);
32✔
1533

1534
      std::unique_ptr<ASBlocks> blocks = std::make_unique<ASBlocks>(ident);
12✔
1535

1536
      auto req = user_params().add_extension(std::move(blocks)).into_pkcs10_request(*sub_key, *rng, hash_fn);
12✔
1537
      Botan::X509_Certificate cert = ca.sign_request(req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
12✔
1538

1539
      {
12✔
1540
         const auto* as_blocks = cert.v3_extensions().get_extension_object_as<ASBlocks>();
12✔
1541
         result.confirm("cert has ASBlock extension", as_blocks != nullptr, true);
24✔
1542

1543
         const auto& identifier = as_blocks->as_identifiers();
12✔
1544

1545
         if(include_asnum) {
12✔
1546
            const auto& asnum_entries = identifier.asnum().value().ranges().value();
8✔
1547

1548
            if(push_asnum) {
8✔
1549
               result.confirm("asnum entry 0 min", asnum_entries[0].min() == 0, true);
8✔
1550
               result.confirm("asnum entry 0 max", asnum_entries[0].max() == 999, true);
8✔
1551

1552
               result.confirm("asnum entry 1 min", asnum_entries[1].min() == 5042, true);
8✔
1553
               result.confirm("asnum entry 1 max", asnum_entries[1].max() == 4294967295, true);
8✔
1554
            } else {
1555
               result.confirm("asnum has no entries", asnum_entries.empty(), true);
8✔
1556
            }
1557
         } else {
1558
            result.confirm("no asnum entry", identifier.asnum().has_value(), false);
8✔
1559
         }
1560

1561
         if(include_rdi) {
12✔
1562
            const auto& rdi_entries = identifier.rdi().value().ranges().value();
8✔
1563

1564
            if(push_rdi) {
8✔
1565
               result.confirm("rdi entry 0 min", rdi_entries[0].min() == 1234, true);
8✔
1566
               result.confirm("rdi entry 0 max", rdi_entries[0].max() == 5678, true);
8✔
1567

1568
               result.confirm("rdi entry 1 min", rdi_entries[1].min() == 32768, true);
8✔
1569
               result.confirm("rdi entry 1 max", rdi_entries[1].max() == 4294967295, true);
8✔
1570
            } else {
1571
               result.confirm("rdi has no entries", rdi_entries.empty(), true);
8✔
1572
            }
1573
         } else {
1574
            result.confirm("rdi has no entry", identifier.rdi().has_value(), false);
8✔
1575
         }
1576
      }
1577
   }
36✔
1578

1579
   result.end_timer();
1✔
1580
   return result;
2✔
1581
}
2✔
1582

1583
Test::Result test_x509_as_blocks_range_merge() {
1✔
1584
   Test::Result result("X509 AS Block range merge");
1✔
1585
   result.start_timer();
1✔
1586

1587
   using Botan::Cert_Extension::ASBlocks;
1✔
1588

1589
   auto rng = Test::new_rng(__func__);
1✔
1590

1591
   auto [ca_cert, ca, sub_key, sig_algo, hash_fn] = make_ca(*rng);
1✔
1592

1593
   std::vector<std::vector<uint16_t>> ranges = {
1✔
1594
      {2005, 37005},
1595
      {60, 70},
1596
      {22, 50},
1597
      {35, 2000},
1598
      {2001, 2004},
1599
      {21, 21},
1600
      {0, 20},
1601
   };
9✔
1602

1603
   std::vector<ASBlocks::ASIdOrRange> as_ranges;
1✔
1604
   for(auto pair : ranges) {
8✔
1605
      auto range = ASBlocks::ASIdOrRange(pair[0], pair[1]);
7✔
1606
      as_ranges.push_back(range);
7✔
1607
   }
7✔
1608

1609
   ASBlocks::ASIdentifierChoice asnum = ASBlocks::ASIdentifierChoice(as_ranges);
1✔
1610

1611
   ASBlocks::ASIdentifiers ident = ASBlocks::ASIdentifiers(std::optional(asnum), std::nullopt);
3✔
1612

1613
   std::unique_ptr<ASBlocks> blocks = std::make_unique<ASBlocks>(ident);
1✔
1614

1615
   const auto req = user_params().add_extension(std::move(blocks)).into_pkcs10_request(*sub_key, *rng, hash_fn);
1✔
1616
   Botan::X509_Certificate cert = ca.sign_request(req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
1✔
1617
   {
1✔
1618
      const auto* as_blocks = cert.v3_extensions().get_extension_object_as<ASBlocks>();
1✔
1619
      result.confirm("cert has ASBlock extension", as_blocks != nullptr, true);
2✔
1620

1621
      const auto& identifier = as_blocks->as_identifiers();
1✔
1622

1623
      const auto& asnum_entries = identifier.asnum().value().ranges().value();
1✔
1624

1625
      result.confirm("asnum entry 0 min", asnum_entries[0].min() == 0, true);
2✔
1626
      result.confirm("asnum entry 0 max", asnum_entries[0].max() == 37005, true);
2✔
1627
      result.confirm("asnum length", asnum_entries.size() == 1, true);
2✔
1628
   }
1629

1630
   result.end_timer();
1✔
1631
   return result;
2✔
1632
}
5✔
1633

1634
Test::Result test_x509_as_blocks_path_validation_success_builder() {
1✔
1635
   Test::Result result("X509 AS Block path validation success (builder)");
1✔
1636
   result.start_timer();
1✔
1637

1638
   using Botan::Cert_Extension::ASBlocks;
1✔
1639
   auto rng = Test::new_rng(__func__);
1✔
1640

1641
   /*
1642
   Creates a certificate chain of length 4.
1643
   Root: both asnum and rdi
1644
   Inherit: has both values as 'inherit'
1645
   Dynamic: has either both 'inherit', both with values, or just one with a value
1646
   Subject: both asnum and rdi as a subset of Root / Dynamic
1647
   */
1648

1649
   // Root Cert, both as and rdi
1650

1651
   std::unique_ptr<ASBlocks> root_blocks = std::make_unique<ASBlocks>();
1✔
1652

1653
   root_blocks->add_asnum(0, 999);
1✔
1654
   root_blocks->add_asnum(5042);
1✔
1655
   root_blocks->add_asnum(5043, 4294967295);
1✔
1656

1657
   root_blocks->add_rdi(1234, 5678);
1✔
1658
   root_blocks->add_rdi(32768);
1✔
1659
   root_blocks->add_rdi(32769, 4294967295);
1✔
1660

1661
   // Inherit cert, both as 'inherit'
1662
   std::unique_ptr<ASBlocks> inherit_blocks = std::make_unique<ASBlocks>();
1✔
1663
   inherit_blocks->inherit_asnum();
1✔
1664
   inherit_blocks->inherit_rdi();
1✔
1665

1666
   // Subject cert
1667

1668
   std::unique_ptr<ASBlocks> sub_blocks = std::make_unique<ASBlocks>();
1✔
1669

1670
   sub_blocks->add_asnum(120, 180);
1✔
1671
   sub_blocks->add_asnum(220, 240);
1✔
1672
   sub_blocks->add_asnum(260, 511);
1✔
1673
   sub_blocks->add_asnum(678);
1✔
1674
   sub_blocks->add_asnum(5043, 5100);
1✔
1675

1676
   sub_blocks->add_rdi(1500, 2300);
1✔
1677
   sub_blocks->add_rdi(2500, 4000);
1✔
1678
   sub_blocks->add_rdi(1567);
1✔
1679
   sub_blocks->add_rdi(33100, 40000);
1✔
1680

1681
   auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] =
1✔
1682
      make_ca(*rng, ca_params().add_extension(std::move(root_blocks)));
2✔
1683
   auto [inherit_cert, inherit_ca] = make_and_sign_ca(std::move(inherit_blocks), root_ca, *rng);
2✔
1684

1685
   Botan::Certificate_Store_In_Memory trusted;
1✔
1686
   trusted.add_certificate(root_cert);
1✔
1687

1688
   for(size_t i = 0; i < 4; i++) {
5✔
1689
      bool include_asnum = i & 1;
4✔
1690
      bool include_rdi = (i >> 1) & 1;
4✔
1691

1692
      std::unique_ptr<ASBlocks> dyn_blocks = std::make_unique<ASBlocks>();
4✔
1693
      if(include_asnum) {
4✔
1694
         dyn_blocks->add_asnum(100, 600);
2✔
1695
         dyn_blocks->add_asnum(678);
2✔
1696
         dyn_blocks->add_asnum(5042, 5101);
2✔
1697
      } else {
1698
         dyn_blocks->inherit_asnum();
2✔
1699
      }
1700

1701
      if(include_rdi) {
4✔
1702
         dyn_blocks->add_rdi(1500, 5000);
2✔
1703
         dyn_blocks->add_rdi(33000, 60000);
2✔
1704
      } else {
1705
         dyn_blocks->inherit_rdi();
2✔
1706
      }
1707

1708
      auto [dyn_cert, dyn_ca] = make_and_sign_ca(std::move(dyn_blocks), inherit_ca, *rng);
8✔
1709

1710
      const auto sub_req = user_params().add_extension(sub_blocks->copy()).into_pkcs10_request(*sub_key, *rng, hash_fn);
4✔
1711
      Botan::X509_Certificate sub_cert =
4✔
1712
         dyn_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
4✔
1713

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

1717
      Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
4✔
1718
      result.require("path validation succeeds", path_result.successful_validation());
4✔
1719
   }
8✔
1720

1721
   result.end_timer();
1✔
1722
   return result;
2✔
1723
}
7✔
1724

1725
Test::Result test_x509_as_blocks_path_validation_success_ctor() {
1✔
1726
   Test::Result result("X509 AS Block path validation success (ctor)");
1✔
1727
   result.start_timer();
1✔
1728

1729
   using Botan::Cert_Extension::ASBlocks;
1✔
1730
   auto rng = Test::new_rng(__func__);
1✔
1731

1732
   /*
1733
   Creates a certificate chain of length 4.
1734
   Root: both asnum and rdi
1735
   Inherit: has both values as 'inherit'
1736
   Dynamic: has either both 'inherit', both with values, or just one with a value
1737
   Subject: both asnum and rdi as a subset of Root / Dynamic
1738
   */
1739

1740
   // Root Cert, both as and rdi
1741
   ASBlocks::ASIdOrRange root_asnum_id_or_range0 = ASBlocks::ASIdOrRange(0, 999);
1✔
1742
   ASBlocks::ASIdOrRange root_asnum_id_or_range1 = ASBlocks::ASIdOrRange(5042);
1✔
1743
   ASBlocks::ASIdOrRange root_asnum_id_or_range2 = ASBlocks::ASIdOrRange(5043, 4294967295);
1✔
1744

1745
   ASBlocks::ASIdOrRange root_rdi_id_or_range0 = ASBlocks::ASIdOrRange(1234, 5678);
1✔
1746
   ASBlocks::ASIdOrRange root_rdi_id_or_range1 = ASBlocks::ASIdOrRange(32768);
1✔
1747
   ASBlocks::ASIdOrRange root_rdi_id_or_range2 = ASBlocks::ASIdOrRange(32769, 4294967295);
1✔
1748

1749
   std::vector<ASBlocks::ASIdOrRange> root_as_ranges;
1✔
1750
   root_as_ranges.push_back(root_asnum_id_or_range0);
1✔
1751
   root_as_ranges.push_back(root_asnum_id_or_range1);
1✔
1752
   root_as_ranges.push_back(root_asnum_id_or_range2);
1✔
1753

1754
   std::vector<ASBlocks::ASIdOrRange> root_rdi_ranges;
1✔
1755
   root_rdi_ranges.push_back(root_rdi_id_or_range0);
1✔
1756
   root_rdi_ranges.push_back(root_rdi_id_or_range1);
1✔
1757
   root_rdi_ranges.push_back(root_rdi_id_or_range2);
1✔
1758

1759
   ASBlocks::ASIdentifierChoice root_asnum = ASBlocks::ASIdentifierChoice(root_as_ranges);
1✔
1760
   ASBlocks::ASIdentifierChoice root_rdi = ASBlocks::ASIdentifierChoice(root_rdi_ranges);
1✔
1761
   ASBlocks::ASIdentifiers root_ident = ASBlocks::ASIdentifiers(root_asnum, root_rdi);
4✔
1762
   std::unique_ptr<ASBlocks> root_blocks = std::make_unique<ASBlocks>(root_ident);
1✔
1763

1764
   // Inherit cert, both as 'inherit'
1765
   ASBlocks::ASIdentifierChoice inherit_asnum = ASBlocks::ASIdentifierChoice();
1✔
1766
   ASBlocks::ASIdentifierChoice inherit_rdi = ASBlocks::ASIdentifierChoice();
1✔
1767
   ASBlocks::ASIdentifiers inherit_ident = ASBlocks::ASIdentifiers(inherit_asnum, inherit_rdi);
2✔
1768
   std::unique_ptr<ASBlocks> inherit_blocks = std::make_unique<ASBlocks>(inherit_ident);
1✔
1769

1770
   // Dynamic cert
1771
   ASBlocks::ASIdOrRange dyn_asnum_id_or_range0 = ASBlocks::ASIdOrRange(100, 600);
1✔
1772
   ASBlocks::ASIdOrRange dyn_asnum_id_or_range1 = ASBlocks::ASIdOrRange(678);
1✔
1773
   ASBlocks::ASIdOrRange dyn_asnum_id_or_range2 = ASBlocks::ASIdOrRange(5042, 5101);
1✔
1774

1775
   ASBlocks::ASIdOrRange dyn_rdi_id_or_range0 = ASBlocks::ASIdOrRange(1500, 5000);
1✔
1776
   ASBlocks::ASIdOrRange dyn_rdi_id_or_range1 = ASBlocks::ASIdOrRange(33000, 60000);
1✔
1777

1778
   std::vector<ASBlocks::ASIdOrRange> dyn_as_ranges;
1✔
1779
   dyn_as_ranges.push_back(dyn_asnum_id_or_range0);
1✔
1780
   dyn_as_ranges.push_back(dyn_asnum_id_or_range1);
1✔
1781
   dyn_as_ranges.push_back(dyn_asnum_id_or_range2);
1✔
1782

1783
   std::vector<ASBlocks::ASIdOrRange> dyn_rdi_ranges;
1✔
1784
   dyn_rdi_ranges.push_back(dyn_rdi_id_or_range0);
1✔
1785
   dyn_rdi_ranges.push_back(dyn_rdi_id_or_range1);
1✔
1786

1787
   // Subject cert
1788
   ASBlocks::ASIdOrRange sub_asnum_id_or_range0 = ASBlocks::ASIdOrRange(120, 180);
1✔
1789
   ASBlocks::ASIdOrRange sub_asnum_id_or_range1 = ASBlocks::ASIdOrRange(220, 240);
1✔
1790
   ASBlocks::ASIdOrRange sub_asnum_id_or_range2 = ASBlocks::ASIdOrRange(260, 511);
1✔
1791
   ASBlocks::ASIdOrRange sub_asnum_id_or_range3 = ASBlocks::ASIdOrRange(678);
1✔
1792
   ASBlocks::ASIdOrRange sub_asnum_id_or_range4 = ASBlocks::ASIdOrRange(5043, 5100);
1✔
1793

1794
   ASBlocks::ASIdOrRange sub_rdi_id_or_range0 = ASBlocks::ASIdOrRange(1500, 2300);
1✔
1795
   ASBlocks::ASIdOrRange sub_rdi_id_or_range1 = ASBlocks::ASIdOrRange(2500, 4000);
1✔
1796
   ASBlocks::ASIdOrRange sub_rdi_id_or_range2 = ASBlocks::ASIdOrRange(1567);
1✔
1797
   ASBlocks::ASIdOrRange sub_rdi_id_or_range3 = ASBlocks::ASIdOrRange(33100, 40000);
1✔
1798

1799
   std::vector<ASBlocks::ASIdOrRange> sub_as_ranges;
1✔
1800
   sub_as_ranges.push_back(sub_asnum_id_or_range0);
1✔
1801
   sub_as_ranges.push_back(sub_asnum_id_or_range1);
1✔
1802
   sub_as_ranges.push_back(sub_asnum_id_or_range2);
1✔
1803
   sub_as_ranges.push_back(sub_asnum_id_or_range3);
1✔
1804
   sub_as_ranges.push_back(sub_asnum_id_or_range4);
1✔
1805

1806
   std::vector<ASBlocks::ASIdOrRange> sub_rdi_ranges;
1✔
1807
   sub_rdi_ranges.push_back(sub_rdi_id_or_range0);
1✔
1808
   sub_rdi_ranges.push_back(sub_rdi_id_or_range1);
1✔
1809
   sub_rdi_ranges.push_back(sub_rdi_id_or_range2);
1✔
1810
   sub_rdi_ranges.push_back(sub_rdi_id_or_range3);
1✔
1811

1812
   ASBlocks::ASIdentifierChoice sub_asnum = ASBlocks::ASIdentifierChoice(sub_as_ranges);
1✔
1813
   ASBlocks::ASIdentifierChoice sub_rdi = ASBlocks::ASIdentifierChoice(sub_rdi_ranges);
1✔
1814
   ASBlocks::ASIdentifiers sub_ident = ASBlocks::ASIdentifiers(sub_asnum, sub_rdi);
4✔
1815
   std::unique_ptr<ASBlocks> sub_blocks = std::make_unique<ASBlocks>(sub_ident);
1✔
1816

1817
   auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] =
1✔
1818
      make_ca(*rng, ca_params().add_extension(std::move(root_blocks)));
2✔
1819
   auto [inherit_cert, inherit_ca] = make_and_sign_ca(std::move(inherit_blocks), root_ca, *rng);
2✔
1820

1821
   Botan::Certificate_Store_In_Memory trusted;
1✔
1822
   trusted.add_certificate(root_cert);
1✔
1823

1824
   for(size_t i = 0; i < 4; i++) {
5✔
1825
      bool include_asnum = i & 1;
4✔
1826
      bool include_rdi = (i >> 1) & 1;
4✔
1827

1828
      ASBlocks::ASIdentifierChoice dyn_asnum =
4✔
1829
         ASBlocks::ASIdentifierChoice(include_asnum ? std::optional(dyn_as_ranges) : std::nullopt);
6✔
1830
      ASBlocks::ASIdentifierChoice dyn_rdi =
4✔
1831
         ASBlocks::ASIdentifierChoice(include_rdi ? std::optional(dyn_rdi_ranges) : std::nullopt);
6✔
1832
      ASBlocks::ASIdentifiers dyn_ident = ASBlocks::ASIdentifiers(dyn_asnum, dyn_rdi);
12✔
1833
      std::unique_ptr<ASBlocks> dyn_blocks = std::make_unique<ASBlocks>(dyn_ident);
4✔
1834

1835
      auto [dyn_cert, dyn_ca] = make_and_sign_ca(std::move(dyn_blocks), inherit_ca, *rng);
8✔
1836

1837
      const auto sub_req = user_params().add_extension(sub_blocks->copy()).into_pkcs10_request(*sub_key, *rng, hash_fn);
4✔
1838
      Botan::X509_Certificate sub_cert =
4✔
1839
         dyn_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
4✔
1840

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

1844
      Botan::Path_Validation_Result path_result = Botan::x509_path_validate(certs, restrictions, trusted);
4✔
1845
      result.require("path validation succeeds", path_result.successful_validation());
4✔
1846
   }
12✔
1847

1848
   result.end_timer();
1✔
1849
   return result;
2✔
1850
}
12✔
1851

1852
Test::Result test_x509_as_blocks_path_validation_extension_not_present_builder() {
1✔
1853
   Test::Result result("X509 AS Block path validation extension not present (builder)");
1✔
1854
   result.start_timer();
1✔
1855

1856
   using Botan::Cert_Extension::ASBlocks;
1✔
1857
   auto rng = Test::new_rng(__func__);
1✔
1858

1859
   std::unique_ptr<ASBlocks> sub_blocks = std::make_unique<ASBlocks>();
1✔
1860
   sub_blocks->add_asnum(120, 180);
1✔
1861
   sub_blocks->add_asnum(220, 224);
1✔
1862
   sub_blocks->add_asnum(260, 511);
1✔
1863
   sub_blocks->add_asnum(678);
1✔
1864
   sub_blocks->add_asnum(5043, 5100);
1✔
1865

1866
   sub_blocks->add_rdi(1500, 2300);
1✔
1867
   sub_blocks->add_rdi(2500, 4000);
1✔
1868
   sub_blocks->add_rdi(1567);
1✔
1869
   sub_blocks->add_rdi(33100, 40000);
1✔
1870

1871
   // create a root ca that does not have any extension
1872
   auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] = make_ca(*rng, ca_params());
1✔
1873
   auto sub_req = user_params().add_extension(std::move(sub_blocks)).into_pkcs10_request(*sub_key, *rng, hash_fn);
1✔
1874
   Botan::X509_Certificate sub_cert = root_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
1✔
1875

1876
   Botan::Certificate_Store_In_Memory trusted;
1✔
1877
   trusted.add_certificate(root_cert);
1✔
1878

1879
   const Botan::Path_Validation_Restrictions restrictions(false, 80);
2✔
1880
   const std::vector<Botan::X509_Certificate> certs = {sub_cert};
2✔
1881

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

1885
   result.end_timer();
1✔
1886
   return result;
2✔
1887
}
3✔
1888

1889
Test::Result test_x509_as_blocks_path_validation_extension_not_present_ctor() {
1✔
1890
   Test::Result result("X509 AS Block path validation extension not present (ctor)");
1✔
1891
   result.start_timer();
1✔
1892

1893
   using Botan::Cert_Extension::ASBlocks;
1✔
1894
   auto rng = Test::new_rng(__func__);
1✔
1895

1896
   // Subject cert
1897
   ASBlocks::ASIdOrRange sub_asnum_id_or_range0 = ASBlocks::ASIdOrRange(120, 180);
1✔
1898
   ASBlocks::ASIdOrRange sub_asnum_id_or_range1 = ASBlocks::ASIdOrRange(220, 240);
1✔
1899
   ASBlocks::ASIdOrRange sub_asnum_id_or_range2 = ASBlocks::ASIdOrRange(260, 511);
1✔
1900
   ASBlocks::ASIdOrRange sub_asnum_id_or_range3 = ASBlocks::ASIdOrRange(678);
1✔
1901
   ASBlocks::ASIdOrRange sub_asnum_id_or_range4 = ASBlocks::ASIdOrRange(5043, 5100);
1✔
1902

1903
   ASBlocks::ASIdOrRange sub_rdi_id_or_range0 = ASBlocks::ASIdOrRange(1500, 2300);
1✔
1904
   ASBlocks::ASIdOrRange sub_rdi_id_or_range1 = ASBlocks::ASIdOrRange(2500, 4000);
1✔
1905
   ASBlocks::ASIdOrRange sub_rdi_id_or_range2 = ASBlocks::ASIdOrRange(1567);
1✔
1906
   ASBlocks::ASIdOrRange sub_rdi_id_or_range3 = ASBlocks::ASIdOrRange(33100, 40000);
1✔
1907

1908
   std::vector<ASBlocks::ASIdOrRange> sub_as_ranges;
1✔
1909
   sub_as_ranges.push_back(sub_asnum_id_or_range0);
1✔
1910
   sub_as_ranges.push_back(sub_asnum_id_or_range1);
1✔
1911
   sub_as_ranges.push_back(sub_asnum_id_or_range2);
1✔
1912
   sub_as_ranges.push_back(sub_asnum_id_or_range3);
1✔
1913
   sub_as_ranges.push_back(sub_asnum_id_or_range4);
1✔
1914

1915
   std::vector<ASBlocks::ASIdOrRange> sub_rdi_ranges;
1✔
1916
   sub_rdi_ranges.push_back(sub_rdi_id_or_range0);
1✔
1917
   sub_rdi_ranges.push_back(sub_rdi_id_or_range1);
1✔
1918
   sub_rdi_ranges.push_back(sub_rdi_id_or_range2);
1✔
1919
   sub_rdi_ranges.push_back(sub_rdi_id_or_range3);
1✔
1920

1921
   ASBlocks::ASIdentifierChoice sub_asnum = ASBlocks::ASIdentifierChoice(sub_as_ranges);
1✔
1922
   ASBlocks::ASIdentifierChoice sub_rdi = ASBlocks::ASIdentifierChoice(sub_rdi_ranges);
1✔
1923
   ASBlocks::ASIdentifiers sub_ident = ASBlocks::ASIdentifiers(sub_asnum, sub_rdi);
4✔
1924
   std::unique_ptr<ASBlocks> sub_blocks = std::make_unique<ASBlocks>(sub_ident);
1✔
1925

1926
   // create a root ca that does not have any extension
1927
   auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] = make_ca(*rng, ca_params());
1✔
1928
   const auto sub_req = user_params().add_extension(std::move(sub_blocks)).into_pkcs10_request(*sub_key, *rng, hash_fn);
1✔
1929
   Botan::X509_Certificate sub_cert = root_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
1✔
1930

1931
   Botan::Certificate_Store_In_Memory trusted;
1✔
1932
   trusted.add_certificate(root_cert);
1✔
1933

1934
   const Botan::Path_Validation_Restrictions restrictions(false, 80);
2✔
1935
   const std::vector<Botan::X509_Certificate> certs = {sub_cert};
2✔
1936

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

1940
   result.end_timer();
1✔
1941
   return result;
2✔
1942
}
5✔
1943

1944
Test::Result test_x509_as_blocks_path_validation_failure_builder() {
1✔
1945
   Test::Result result("X509 AS Block path validation failure (builder)");
1✔
1946
   result.start_timer();
1✔
1947

1948
   using Botan::Cert_Extension::ASBlocks;
1✔
1949
   auto rng = Test::new_rng(__func__);
1✔
1950

1951
   /*
1952
   This executes a few permutations, messing around with edge cases when it comes to constructing ranges.
1953

1954
   Each test is expected to fail and creates the following certificate chain:
1955
   Root -> Issuer -> Subject
1956

1957
   00: set all the asnum choices to 'inherit' for each cert
1958
   01: 00 but for rdis
1959
   02: make smallest min asnum of the subject smaller than the smallest min asnum of the issuer
1960
   03: 02 but for rdis
1961
   04: both 02 and 03
1962
   05: make largest max asnum of the subject larger than the largest max asnum of the issuer
1963
   06: 05 but for rdis
1964
   07: both 05 and 06
1965
   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
1966
   09: 08 but for rdis
1967
   10: both 08 and 09
1968
   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)
1969
   12: 11 but for rdis
1970
   13: both 11 and 12
1971
   14: 08 but using the minimum instead of the maximum
1972
   15: 14 but for rdis
1973
   16: both 14 and 15
1974
   17: same as 11 but using the minimum instead of the maximum
1975
   18: 17 but for rdis
1976
   19: both 18 and 19
1977
   20: make the issuer ranges empty but have an entry in the subject ranges
1978
   */
1979
   for(size_t i = 0; i < 21; i++) {
22✔
1980
      // enable / disable all the different edge cases
1981
      bool inherit_all_asnums = (i == 0);
21✔
1982
      bool inherit_all_rdis = (i == 1);
21✔
1983
      bool push_asnum_min_edge_ranges = (i == 2) || (i == 4);
21✔
1984
      bool push_rdi_min_edge_ranges = (i == 3) || (i == 4);
21✔
1985
      bool push_asnum_max_edge_ranges = (i == 5) || (i == 7);
21✔
1986
      bool push_rdi_max_edge_ranges = (i == 6) || (i == 7);
21✔
1987
      bool push_asnum_max_middle_ranges = (i == 8) || (i == 10);
21✔
1988
      bool push_rdi_max_middle_ranges = (i == 9) || (i == 10);
21✔
1989
      bool push_asnum_max_split_ranges = (i == 11) || (i == 13);
21✔
1990
      bool push_rdi_max_split_ranges = (i == 12) || (i == 13);
21✔
1991
      bool push_asnum_min_middle_ranges = (i == 14) || (i == 16);
21✔
1992
      bool push_rdi_min_middle_ranges = (i == 15) || (i == 16);
21✔
1993
      bool push_asnum_min_split_ranges = (i == 17) || (i == 19);
21✔
1994
      bool push_rdi_min_split_ranges = (i == 18) || (i == 19);
21✔
1995
      bool empty_issuer_non_empty_subject = (i == 20);
21✔
1996

1997
      // Root cert
1998
      std::unique_ptr<ASBlocks> root_blocks = std::make_unique<ASBlocks>();
21✔
1999

2000
      if(!inherit_all_asnums) {
21✔
2001
         if(push_asnum_min_edge_ranges || push_asnum_max_edge_ranges) {
20✔
2002
            // 100-200 for 02,03,04
2003
            root_blocks->add_asnum(100, 200);
4✔
2004
         } else if(push_asnum_max_middle_ranges || push_asnum_min_middle_ranges) {
16✔
2005
            // 10-20,30-40,50-60 for 08,09,10
2006
            root_blocks->add_asnum(10, 20);
4✔
2007
            root_blocks->add_asnum(30, 40);
4✔
2008
            root_blocks->add_asnum(50, 60);
4✔
2009
         } else if(push_asnum_max_split_ranges || push_asnum_min_split_ranges) {
12✔
2010
            // 10-20,30-50,60-70 for 11,12,13
2011
            root_blocks->add_asnum(10, 20);
4✔
2012
            root_blocks->add_asnum(30, 50);
4✔
2013
            root_blocks->add_asnum(60, 70);
4✔
2014
         }
2015
      } else {
2016
         root_blocks->inherit_asnum();
1✔
2017
      }
2018

2019
      // same values but for rdis
2020
      if(!inherit_all_rdis) {
21✔
2021
         if(push_rdi_min_edge_ranges || push_rdi_max_edge_ranges) {
2022
            root_blocks->add_rdi(100, 200);
4✔
2023
         } else if(push_rdi_max_middle_ranges || push_rdi_min_middle_ranges) {
2024
            root_blocks->add_rdi(10, 20);
4✔
2025
            root_blocks->add_rdi(30, 40);
4✔
2026
            root_blocks->add_rdi(50, 60);
4✔
2027
         } else if(push_rdi_max_split_ranges || push_rdi_min_split_ranges) {
2028
            root_blocks->add_rdi(10, 20);
4✔
2029
            root_blocks->add_rdi(30, 50);
4✔
2030
            root_blocks->add_rdi(60, 70);
4✔
2031
         }
2032
      } else {
2033
         root_blocks->inherit_rdi();
1✔
2034
      }
2035

2036
      if(empty_issuer_non_empty_subject) {
21✔
2037
         root_blocks->restrict_asnum();
1✔
2038
         root_blocks->restrict_rdi();
1✔
2039
      }
2040

2041
      // Issuer cert
2042
      // the issuer cert has the same ranges as the root cert
2043
      // it is used to check that the 'inherit' check is bubbled up until the root cert is hit
2044
      auto issu_blocks = root_blocks->copy();
21✔
2045

2046
      // Subject cert
2047
      std::unique_ptr<ASBlocks> sub_blocks = std::make_unique<ASBlocks>();
21✔
2048

2049
      std::vector<ASBlocks::ASIdOrRange> sub_as_ranges;
21✔
2050
      std::vector<ASBlocks::ASIdOrRange> sub_rdi_ranges;
21✔
2051

2052
      if(!inherit_all_asnums) {
21✔
2053
         // assign the subject asnum ranges
2054
         if(push_asnum_min_edge_ranges) {
2055
            // 99-200 for 02 (so overlapping to the left)
2056
            sub_blocks->add_asnum(99, 200);
2✔
2057
         } else if(push_asnum_max_edge_ranges) {
2058
            // 100-201 for 03 (so overlapping to the right)
2059
            sub_blocks->add_asnum(100, 201);
2✔
2060
         } else if(push_asnum_max_middle_ranges) {
2061
            // same as root, but change the range in the middle to overlap to the right for 08
2062
            sub_blocks->add_asnum(10, 20);
2✔
2063
            sub_blocks->add_asnum(30, 41);
2✔
2064
            sub_blocks->add_asnum(50, 60);
2✔
2065
         } else if(push_asnum_max_split_ranges) {
2066
            // change the range in the middle to be cut at 45 for case 11
2067
            // the left range is 30-44
2068
            // the right range is 46-51 (overlapping the issuer range to the right)
2069
            sub_blocks->add_asnum(10, 20);
2✔
2070
            sub_blocks->add_asnum(30, 44);
2✔
2071
            sub_blocks->add_asnum(46, 51);
2✔
2072
            sub_blocks->add_asnum(60, 70);
2✔
2073
         } else if(push_asnum_min_middle_ranges) {
2074
            // just change the test in the middle to overlap to the left for case 14
2075
            sub_blocks->add_asnum(10, 20);
2✔
2076
            sub_blocks->add_asnum(29, 40);
2✔
2077
            sub_blocks->add_asnum(50, 60);
2✔
2078
         } else if(push_asnum_min_split_ranges) {
2079
            // again split the range in the middle at 45 for case 17
2080
            // creating two ranges 29-44 and 46-50 (so overlapping to the left)
2081
            sub_blocks->add_asnum(10, 20);
2✔
2082
            sub_blocks->add_asnum(29, 44);
2✔
2083
            sub_blocks->add_asnum(46, 50);
2✔
2084
            sub_blocks->add_asnum(60, 70);
2✔
2085
         } else if(empty_issuer_non_empty_subject) {
2086
            sub_blocks->add_asnum(50);
1✔
2087
         }
2088
      } else {
2089
         sub_blocks->inherit_asnum();
1✔
2090
      }
2091

2092
      if(!inherit_all_rdis) {
21✔
2093
         // same values but for rdis
2094
         if(push_rdi_min_edge_ranges) {
2095
            sub_blocks->add_rdi(99, 200);
2✔
2096
         } else if(push_rdi_max_edge_ranges) {
2097
            sub_blocks->add_rdi(100, 201);
2✔
2098
         } else if(push_rdi_max_middle_ranges) {
2099
            sub_blocks->add_rdi(10, 20);
2✔
2100
            sub_blocks->add_rdi(30, 41);
2✔
2101
            sub_blocks->add_rdi(50, 60);
2✔
2102
         } else if(push_rdi_max_split_ranges) {
2103
            sub_blocks->add_rdi(10, 20);
2✔
2104
            sub_blocks->add_rdi(30, 44);
2✔
2105
            sub_blocks->add_rdi(46, 51);
2✔
2106
            sub_blocks->add_rdi(60, 70);
2✔
2107
         } else if(push_rdi_min_middle_ranges) {
2108
            sub_blocks->add_rdi(10, 20);
2✔
2109
            sub_blocks->add_rdi(29, 40);
2✔
2110
            sub_blocks->add_rdi(50, 60);
2✔
2111
         } else if(push_rdi_min_split_ranges) {
2112
            sub_blocks->add_rdi(10, 20);
2✔
2113
            sub_blocks->add_rdi(29, 44);
2✔
2114
            sub_blocks->add_rdi(46, 50);
2✔
2115
            sub_blocks->add_rdi(60, 70);
2✔
2116
         }
2117
      } else {
2118
         sub_blocks->inherit_rdi();
1✔
2119
      }
2120

2121
      auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] =
21✔
2122
         make_ca(*rng, ca_params().add_extension(std::move(root_blocks)));
42✔
2123
      auto [issu_cert, issu_ca] = make_and_sign_ca(std::move(issu_blocks), root_ca, *rng);
42✔
2124

2125
      const auto sub_req =
21✔
2126
         user_params().add_extension(std::move(sub_blocks)).into_pkcs10_request(*sub_key, *rng, hash_fn);
21✔
2127
      Botan::X509_Certificate sub_cert =
21✔
2128
         issu_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
21✔
2129

2130
      Botan::Certificate_Store_In_Memory trusted;
21✔
2131
      trusted.add_certificate(root_cert);
21✔
2132

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

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

2141
   result.end_timer();
1✔
2142
   return result;
1✔
2143
}
22✔
2144

2145
Test::Result test_x509_as_blocks_path_validation_failure_ctor() {
1✔
2146
   Test::Result result("X509 AS Block path validation failure (ctor)");
1✔
2147
   result.start_timer();
1✔
2148

2149
   using Botan::Cert_Extension::ASBlocks;
1✔
2150
   auto rng = Test::new_rng(__func__);
1✔
2151

2152
   /*
2153
   This executes a few permutations, messing around with edge cases when it comes to constructing ranges.
2154

2155
   Each test is expected to fail and creates the following certificate chain:
2156
   Root -> Issuer -> Subject
2157

2158
   00: set all the asnum choices to 'inherit' for each cert
2159
   01: 00 but for rdis
2160
   02: make smallest min asnum of the subject smaller than the smallest min asnum of the issuer
2161
   03: 02 but for rdis
2162
   04: both 02 and 03
2163
   05: make largest max asnum of the subject larger than the largest max asnum of the issuer
2164
   06: 05 but for rdis
2165
   07: both 05 and 06
2166
   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
2167
   09: 08 but for rdis
2168
   10: both 08 and 09
2169
   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)
2170
   12: 11 but for rdis
2171
   13: both 11 and 12
2172
   14: 08 but using the minimum instead of the maximum
2173
   15: 14 but for rdis
2174
   16: both 14 and 15
2175
   17: same as 11 but using the minimum instead of the maximum
2176
   18: 17 but for rdis
2177
   19: both 18 and 19
2178
   20: make the issuer ranges empty but have an entry in the subject ranges
2179
   */
2180
   for(size_t i = 0; i < 21; i++) {
22✔
2181
      // enable / disable all the different edge cases
2182
      bool inherit_all_asnums = (i == 0);
21✔
2183
      bool inherit_all_rdis = (i == 1);
21✔
2184
      bool push_asnum_min_edge_ranges = (i == 2) || (i == 4);
21✔
2185
      bool push_rdi_min_edge_ranges = (i == 3) || (i == 4);
21✔
2186
      bool push_asnum_max_edge_ranges = (i == 5) || (i == 7);
21✔
2187
      bool push_rdi_max_edge_ranges = (i == 6) || (i == 7);
21✔
2188
      bool push_asnum_max_middle_ranges = (i == 8) || (i == 10);
21✔
2189
      bool push_rdi_max_middle_ranges = (i == 9) || (i == 10);
21✔
2190
      bool push_asnum_max_split_ranges = (i == 11) || (i == 13);
21✔
2191
      bool push_rdi_max_split_ranges = (i == 12) || (i == 13);
21✔
2192
      bool push_asnum_min_middle_ranges = (i == 14) || (i == 16);
21✔
2193
      bool push_rdi_min_middle_ranges = (i == 15) || (i == 16);
21✔
2194
      bool push_asnum_min_split_ranges = (i == 17) || (i == 19);
21✔
2195
      bool push_rdi_min_split_ranges = (i == 18) || (i == 19);
21✔
2196
      bool empty_issuer_non_empty_subject = (i == 20);
21✔
2197

2198
      // Root cert
2199
      std::vector<ASBlocks::ASIdOrRange> root_as_ranges;
21✔
2200
      std::vector<ASBlocks::ASIdOrRange> root_rdi_ranges;
21✔
2201

2202
      // assign the root ranges
2203
      if(push_asnum_min_edge_ranges || push_asnum_max_edge_ranges) {
21✔
2204
         // 100-200 for 02,03,04
2205
         root_as_ranges.push_back(ASBlocks::ASIdOrRange(100, 200));
8✔
2206
      } else if(push_asnum_max_middle_ranges || push_asnum_min_middle_ranges) {
17✔
2207
         // 10-20,30-40,50-60 for 08,09,10
2208
         root_as_ranges.push_back(ASBlocks::ASIdOrRange(10, 20));
8✔
2209
         root_as_ranges.push_back(ASBlocks::ASIdOrRange(30, 40));
8✔
2210
         root_as_ranges.push_back(ASBlocks::ASIdOrRange(50, 60));
8✔
2211
      } else if(push_asnum_max_split_ranges || push_asnum_min_split_ranges) {
13✔
2212
         // 10-20,30-50,60-70 for 11,12,13
2213
         root_as_ranges.push_back(ASBlocks::ASIdOrRange(10, 20));
8✔
2214
         root_as_ranges.push_back(ASBlocks::ASIdOrRange(30, 50));
8✔
2215
         root_as_ranges.push_back(ASBlocks::ASIdOrRange(60, 70));
8✔
2216
      }
2217

2218
      // same values but for rdis
2219
      if(push_rdi_min_edge_ranges || push_rdi_max_edge_ranges) {
21✔
2220
         root_rdi_ranges.push_back(ASBlocks::ASIdOrRange(100, 200));
8✔
2221
      } else if(push_rdi_max_middle_ranges || push_rdi_min_middle_ranges) {
2222
         root_rdi_ranges.push_back(ASBlocks::ASIdOrRange(10, 20));
8✔
2223
         root_rdi_ranges.push_back(ASBlocks::ASIdOrRange(30, 40));
8✔
2224
         root_rdi_ranges.push_back(ASBlocks::ASIdOrRange(50, 60));
8✔
2225
      } else if(push_rdi_max_split_ranges || push_rdi_min_split_ranges) {
2226
         root_rdi_ranges.push_back(ASBlocks::ASIdOrRange(10, 20));
8✔
2227
         root_rdi_ranges.push_back(ASBlocks::ASIdOrRange(30, 50));
8✔
2228
         root_rdi_ranges.push_back(ASBlocks::ASIdOrRange(60, 70));
8✔
2229
      }
2230

2231
      // Issuer cert
2232
      // the issuer cert has the same ranges as the root cert
2233
      // it is used to check that the 'inherit' check is bubbled up until the root cert is hit
2234
      std::vector<ASBlocks::ASIdOrRange> issu_as_ranges;
21✔
2235
      std::vector<ASBlocks::ASIdOrRange> issu_rdi_ranges;
21✔
2236

2237
      // Subject cert
2238
      std::vector<ASBlocks::ASIdOrRange> sub_as_ranges;
21✔
2239
      std::vector<ASBlocks::ASIdOrRange> sub_rdi_ranges;
21✔
2240

2241
      // assign the subject asnum ranges
2242
      if(push_asnum_min_edge_ranges) {
21✔
2243
         // 99-200 for 02 (so overlapping to the left)
2244
         sub_as_ranges.push_back(ASBlocks::ASIdOrRange(99, 200));
4✔
2245
      } else if(push_asnum_max_edge_ranges) {
2246
         // 100-201 for 03 (so overlapping to the right)
2247
         sub_as_ranges.push_back(ASBlocks::ASIdOrRange(100, 201));
4✔
2248
      } else if(push_asnum_max_middle_ranges) {
2249
         // just change the range in the middle to overlap to the right for 08
2250
         sub_as_ranges = root_as_ranges;
2✔
2251
         sub_as_ranges[1] = ASBlocks::ASIdOrRange(30, 41);
2✔
2252
      } else if(push_asnum_max_split_ranges) {
2253
         // change the range in the middle to be cut at 45 for case 11
2254
         // the left range is 30-44
2255
         // the right range is 46-51 (overlapping the issuer range to the right)
2256
         sub_as_ranges = root_as_ranges;
2✔
2257
         sub_as_ranges[1] = ASBlocks::ASIdOrRange(30, 44);
2✔
2258
         // pushing the new range created by splitting to the back since they will be sorted anyway
2259
         sub_as_ranges.push_back(ASBlocks::ASIdOrRange(46, 51));
4✔
2260
      } else if(push_asnum_min_middle_ranges) {
2261
         // just change the test in the middle to overlap to the left for case 14
2262
         sub_as_ranges = root_as_ranges;
2✔
2263
         sub_as_ranges[1] = ASBlocks::ASIdOrRange(29, 40);
2✔
2264
      } else if(push_asnum_min_split_ranges) {
2265
         // again split the range in the middle at 45 for case 17
2266
         // creating two ranges 29-44 and 46-50 (so overlapping to the left)
2267
         sub_as_ranges = root_as_ranges;
2✔
2268
         sub_as_ranges[1] = ASBlocks::ASIdOrRange(29, 44);
2✔
2269
         sub_as_ranges.push_back(ASBlocks::ASIdOrRange(46, 50));
4✔
2270
      } else if(empty_issuer_non_empty_subject) {
2271
         sub_as_ranges.push_back(ASBlocks::ASIdOrRange(50));
1✔
2272
      }
2273

2274
      // same values but for rdis
2275
      if(push_rdi_min_edge_ranges) {
21✔
2276
         sub_rdi_ranges.push_back(ASBlocks::ASIdOrRange(99, 200));
4✔
2277
      } else if(push_rdi_max_edge_ranges) {
2278
         sub_rdi_ranges.push_back(ASBlocks::ASIdOrRange(100, 201));
4✔
2279
      } else if(push_rdi_max_middle_ranges) {
2280
         sub_rdi_ranges = root_rdi_ranges;
2✔
2281
         sub_rdi_ranges[1] = ASBlocks::ASIdOrRange(30, 41);
2✔
2282
      } else if(push_rdi_max_split_ranges) {
2283
         sub_rdi_ranges = root_rdi_ranges;
2✔
2284
         sub_rdi_ranges[1] = ASBlocks::ASIdOrRange(30, 44);
2✔
2285
         sub_rdi_ranges.push_back(ASBlocks::ASIdOrRange(46, 51));
4✔
2286
      } else if(push_rdi_min_middle_ranges) {
2287
         sub_rdi_ranges = root_rdi_ranges;
2✔
2288
         sub_rdi_ranges[1] = ASBlocks::ASIdOrRange(29, 40);
2✔
2289
      } else if(push_rdi_min_split_ranges) {
2290
         sub_rdi_ranges = root_rdi_ranges;
2✔
2291
         sub_rdi_ranges[1] = ASBlocks::ASIdOrRange(29, 44);
2✔
2292
         sub_rdi_ranges.push_back(ASBlocks::ASIdOrRange(46, 50));
4✔
2293
      }
2294

2295
      // for cases 00 and 01, set all certs to inherit (so std::nullopt)
2296
      // in all other cases use the ranges created beforehand
2297
      ASBlocks::ASIdentifierChoice root_asnum =
21✔
2298
         ASBlocks::ASIdentifierChoice(inherit_all_asnums ? std::nullopt : std::optional(root_as_ranges));
41✔
2299
      ASBlocks::ASIdentifierChoice root_rdi =
21✔
2300
         ASBlocks::ASIdentifierChoice(inherit_all_rdis ? std::nullopt : std::optional(root_rdi_ranges));
41✔
2301
      ASBlocks::ASIdentifiers root_ident = ASBlocks::ASIdentifiers(root_asnum, root_rdi);
82✔
2302
      std::unique_ptr<ASBlocks> root_blocks = std::make_unique<ASBlocks>(root_ident);
21✔
2303

2304
      ASBlocks::ASIdentifiers issu_ident = root_ident;
21✔
2305
      std::unique_ptr<ASBlocks> issu_blocks = std::make_unique<ASBlocks>(issu_ident);
21✔
2306

2307
      ASBlocks::ASIdentifierChoice sub_asnum =
21✔
2308
         ASBlocks::ASIdentifierChoice(inherit_all_asnums ? std::nullopt : std::optional(sub_as_ranges));
41✔
2309
      ASBlocks::ASIdentifierChoice sub_rdi =
21✔
2310
         ASBlocks::ASIdentifierChoice(inherit_all_rdis ? std::nullopt : std::optional(sub_rdi_ranges));
41✔
2311
      ASBlocks::ASIdentifiers sub_ident = ASBlocks::ASIdentifiers(sub_asnum, sub_rdi);
82✔
2312
      std::unique_ptr<ASBlocks> sub_blocks = std::make_unique<ASBlocks>(sub_ident);
21✔
2313

2314
      auto [root_cert, root_ca, sub_key, sig_algo, hash_fn] =
21✔
2315
         make_ca(*rng, ca_params().add_extension(std::move(root_blocks)));
42✔
2316
      auto [issu_cert, issu_ca] = make_and_sign_ca(std::move(issu_blocks), root_ca, *rng);
42✔
2317

2318
      const auto sub_req =
21✔
2319
         user_params().add_extension(std::move(sub_blocks)).into_pkcs10_request(*sub_key, *rng, hash_fn);
21✔
2320
      Botan::X509_Certificate sub_cert =
21✔
2321
         issu_ca.sign_request(sub_req, *rng, from_date(-1, 01, 01), from_date(2, 01, 01));
21✔
2322

2323
      Botan::Certificate_Store_In_Memory trusted;
21✔
2324
      trusted.add_certificate(root_cert);
21✔
2325

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

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

2334
   result.end_timer();
1✔
2335
   return result;
1✔
2336
}
22✔
2337

2338
class X509_RPKI_Tests final : public Test {
×
2339
   public:
2340
      std::vector<Test::Result> run() override {
1✔
2341
         std::vector<Test::Result> results;
1✔
2342

2343
   #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
2344
         results.push_back(test_x509_ip_addr_blocks_extension_decode());
2✔
2345
         results.push_back(test_x509_as_blocks_extension_decode());
2✔
2346
   #endif
2347
         results.push_back(test_x509_ip_addr_blocks_rfc3779_example());
2✔
2348
         results.push_back(test_x509_ip_addr_blocks_encode_builder());
2✔
2349
         results.push_back(test_x509_ip_addr_blocks_extension_encode_ctor());
2✔
2350
         results.push_back(test_x509_ip_addr_blocks_extension_encode_edge_cases_ctor());
2✔
2351
         results.push_back(test_x509_ip_addr_blocks_range_merge());
2✔
2352
         results.push_back(test_x509_ip_addr_blocks_family_merge());
2✔
2353
         results.push_back(test_x509_ip_addr_blocks_path_validation_success_builder());
2✔
2354
         results.push_back(test_x509_ip_addr_blocks_path_validation_success_ctor());
2✔
2355
         results.push_back(test_x509_ip_addr_blocks_path_validation_failure_builder());
2✔
2356
         results.push_back(test_x509_ip_addr_blocks_path_validation_failure_ctor());
2✔
2357
         results.push_back(test_x509_as_blocks_rfc3779_example());
2✔
2358
         results.push_back(test_x509_as_blocks_encode_builder());
2✔
2359
         results.push_back(test_x509_as_blocks_extension_encode_ctor());
2✔
2360
         results.push_back(test_x509_as_blocks_range_merge());
2✔
2361
         results.push_back(test_x509_as_blocks_path_validation_success_builder());
2✔
2362
         results.push_back(test_x509_as_blocks_path_validation_success_ctor());
2✔
2363
         results.push_back(test_x509_as_blocks_path_validation_extension_not_present_builder());
2✔
2364
         results.push_back(test_x509_as_blocks_path_validation_extension_not_present_ctor());
2✔
2365
         results.push_back(test_x509_as_blocks_path_validation_failure_builder());
2✔
2366
         results.push_back(test_x509_as_blocks_path_validation_failure_ctor());
2✔
2367
         return results;
1✔
2368
      }
×
2369
};
2370

2371
BOTAN_REGISTER_TEST("x509", "x509_rpki", X509_RPKI_Tests);
2372

2373
#endif
2374

2375
}  // namespace
2376

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