summaryrefslogtreecommitdiff
path: root/test/recipes/90-test_store.t
blob: c0bf1d1bb0aefe8f3bb31408060356a9f002cc65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#! /usr/bin/env perl
# Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the Apache License 2.0 (the "License").  You may not use
# this file except in compliance with the License.  You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html

use File::Spec::Functions;
use File::Copy;
use MIME::Base64;
use OpenSSL::Test qw(:DEFAULT srctop_file srctop_dir bldtop_file bldtop_dir
                     data_file);
use OpenSSL::Test::Utils;

my $test_name = "test_store";
setup($test_name);

my $use_md5 = !disabled("md5");
my $use_des = !(disabled("des") || disabled("legacy")); # also affects 3des and pkcs12 app
my $use_dsa = !disabled("dsa");
my $use_ecc = !disabled("ec");

my @noexist_files =
    ( "test/blahdiblah.pem",
      "test/blahdibleh.der" );
my @src_files =
    ( "test/testx509.pem",
      "test/testrsa.pem",
      "test/testrsapub.pem",
      "test/testcrl.pem",
      "apps/server.pem" );
my @data_files =
    ( "testrsa.msb" );
push(@data_files,
     ( "testrsa.pvk" ))
    unless disabled("legacy") || disabled("rc4");
my @src_rsa_files =
    ( "test/testrsa.pem",
      "test/testrsapub.pem" );
my @generated_files =
    (
     ### generated from the source files

     "testx509.der",
     "testrsa.der",
     "testrsapub.der",
     "testcrl.der",

     ### generated locally

     "rsa-key-pkcs1.pem", "rsa-key-pkcs1.der",
     "rsa-key-pkcs1-aes128.pem",
     "rsa-key-pkcs8.pem", "rsa-key-pkcs8.der",
     "rsa-key-pkcs8-pbes2-sha1.pem", "rsa-key-pkcs8-pbes2-sha1.der",
     "rsa-key-pkcs8-pbes2-sha256.pem", "rsa-key-pkcs8-pbes2-sha256.der",
    );
push(@generated_files, (
     "rsa-key-pkcs8-pbes1-sha1-3des.pem", "rsa-key-pkcs8-pbes1-sha1-3des.der",
    )) if $use_des;
push(@generated_files, (
     "rsa-key-sha1-3des-sha1.p12", "rsa-key-sha1-3des-sha256.p12",
     "rsa-key-aes256-cbc-sha256.p12",
     "rsa-key-md5-des-sha1.p12",
     "rsa-key-aes256-cbc-md5-des-sha256.p12"
     )) if $use_des;
push(@generated_files, (
     "rsa-key-pkcs8-pbes1-md5-des.pem", "rsa-key-pkcs8-pbes1-md5-des.der"
     )) if $use_md5 && $use_des;
push(@generated_files, (
     "dsa-key-pkcs1.pem", "dsa-key-pkcs1.der",
     "dsa-key-pkcs1-aes128.pem",
     "dsa-key-pkcs8.pem", "dsa-key-pkcs8.der",
     "dsa-key-pkcs8-pbes2-sha1.pem", "dsa-key-pkcs8-pbes2-sha1.der",
     )) if $use_dsa;
push(@generated_files, "dsa-key-aes256-cbc-sha256.p12") if $use_dsa && $use_des;
push(@generated_files, (
     "ec-key-pkcs1.pem", "ec-key-pkcs1.der",
     "ec-key-pkcs1-aes128.pem",
     "ec-key-pkcs8.pem", "ec-key-pkcs8.der",
     "ec-key-pkcs8-pbes2-sha1.pem", "ec-key-pkcs8-pbes2-sha1.der",
     )) if $use_ecc;
push(@generated_files, "ec-key-aes256-cbc-sha256.p12") if $use_ecc && $use_des;
my %generated_file_files =
    $^O eq 'linux'
    ? ( "test/testx509.pem" => "file:testx509.pem",
        "test/testrsa.pem" => "file:testrsa.pem",
        "test/testrsapub.pem" => "file:testrsapub.pem",
        "test/testcrl.pem" => "file:testcrl.pem",
        "apps/server.pem" => "file:server.pem" )
    : ();
my @noexist_file_files =
    ( "file:blahdiblah.pem",
      "file:test/blahdibleh.der" );

# There is more than one method to get a 'file:' loader.
# The default is a built-in provider implementation.
# However, there is also an engine, specially for testing purposes.
#
# @methods is a collection of extra 'openssl storeutl' arguments used to
# try the different methods.
my @methods;
my @prov_method = qw(-provider default);
push @prov_method, qw(-provider legacy) unless disabled('legacy');
push @methods, [ @prov_method ];
push @methods, [qw(-engine loader_attic)]
    unless disabled('loadereng');

my $n = 2 + scalar @methods
    * ( (3 * scalar @noexist_files)
        + (6 * scalar @src_files)
        + (2 * scalar @data_files)
        + (4 * scalar @generated_files)
        + (scalar keys %generated_file_files)
        + (scalar @noexist_file_files)
        + 3
        + 11 );

# Test doesn't work under msys because the file name munging doesn't work
# correctly with the "ot:" prefix
my $do_test_ossltest_store =
    !(disabled("engine") || disabled("dynamic-engine") || $^O =~ /^msys$/);

if ($do_test_ossltest_store) {
    # test loading with apps 'org.openssl.engine:' loader, using the
    # ossltest engine.
    $n += 4 * scalar @src_rsa_files;
}

plan skip_all => "No plan" if $n == 0;

plan tests => $n;

my $test_x509 = srctop_file('test', 'testx509.pem');

ok(run(app(["openssl", "storeutl",  "-crls", $test_x509])),
   "storeutil with -crls option");

ok(!run(app(["openssl", "storeutl", $test_x509, "-crls"])),
   "storeutil with extra parameter (at end) should fail");

indir "store_$$" => sub {
    if ($do_test_ossltest_store) {
        # ossltest loads PEM files, with names prefixed with 'ot:'.
        # This prefix ensures that the files are, in fact, loaded through
        # that engine and not mistakenly going through the 'file:' loader.

        my $engine_scheme = 'org.openssl.engine:';
        $ENV{OPENSSL_ENGINES} = bldtop_dir("engines");

        foreach (@src_rsa_files) {
            my $file = srctop_file($_);
            my $file_abs = to_abs_file($file);
            my @pubin = $_ =~ m|pub\.pem$| ? ("-pubin") : ();

            ok(run(app(["openssl", "rsa", "-text", "-noout", @pubin,
                        "-engine", "ossltest", "-inform", "engine",
                        "-in", "ot:$file"])));
            ok(run(app(["openssl", "rsa", "-text", "-noout", @pubin,
                        "-engine", "ossltest", "-inform", "engine",
                        "-in", "ot:$file_abs"])));
            ok(run(app(["openssl", "rsa", "-text", "-noout", @pubin,
                        "-in", "${engine_scheme}ossltest:ot:$file"])));
            ok(run(app(["openssl", "rsa", "-text", "-noout", @pubin,
                        "-in", "${engine_scheme}ossltest:ot:$file_abs"])));
        }
    }

 SKIP:
    {
        init() or die "init failed";

        my $rehash = init_rehash();

        foreach my $method (@methods) {
            my @storeutl = ( qw(openssl storeutl), @$method );

            foreach (@noexist_files) {
                my $file = srctop_file($_);

                ok(!run(app([@storeutl, "-noout", $file])));
                ok(!run(app([@storeutl, "-noout", to_abs_file($file)])));
                {
                    local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";

                    ok(!run(app([@storeutl, "-noout",
                                 to_abs_file_uri($file)])));
                }
            }
            foreach (@src_files) {
                my $file = srctop_file($_);

                ok(run(app([@storeutl, "-noout", $file])));
                ok(run(app([@storeutl, "-noout", to_abs_file($file)])));
              SKIP:
                {
                    skip "file: tests disabled on MingW", 4  if $^O =~ /^msys$/;

                    ok(run(app([@storeutl, "-noout",
                                to_abs_file_uri($file)])));
                    ok(run(app([@storeutl, "-noout",
                                to_abs_file_uri($file, 0, "")])));
                    ok(run(app([@storeutl, "-noout",
                                to_abs_file_uri($file, 0, "localhost")])));
                    ok(!run(app([@storeutl, "-noout",
                                 to_abs_file_uri($file, 0, "dummy")])));
                }
            }
            foreach (@data_files) {
                my $file = data_file($_);

                ok(run(app([@storeutl, "-noout", "-passin", "pass:password",
                            $file])));
                ok(run(app([@storeutl, "-noout", "-passin", "pass:password",
                            to_abs_file($file)])));
            }
            foreach (@generated_files) {
                ok(run(app([@storeutl, "-noout", "-passin", "pass:password",
                            $_])));
                ok(run(app([@storeutl,  "-noout", "-passin", "pass:password",
                            to_abs_file($_)])));

              SKIP:
                {
                    skip "file: tests disabled on MingW", 2  if $^O =~ /^msys$/;

                    ok(run(app([@storeutl, "-noout", "-passin",
                                "pass:password", to_abs_file_uri($_)])));
                    ok(!run(app([@storeutl, "-noout", "-passin",
                                 "pass:password", to_file_uri($_)])));
                }
            }
            foreach (values %generated_file_files) {
              SKIP:
                {
                    skip "file: tests disabled on MingW", 1  if $^O =~ /^msys$/;

                    ok(run(app([@storeutl,  "-noout", $_])));
                }
            }
            foreach (@noexist_file_files) {
              SKIP:
                {
                    skip "file: tests disabled on MingW", 1  if $^O =~ /^msys$/;

                    ok(!run(app([@storeutl,  "-noout", $_])));
                }
            }
            {
                my $dir = srctop_dir("test", "certs");

                ok(run(app([@storeutl,  "-noout", $dir])));
                ok(run(app([@storeutl,  "-noout", to_abs_file($dir, 1)])));
              SKIP:
                {
                    skip "file: tests disabled on MingW", 1  if $^O =~ /^msys$/;

                    ok(run(app([@storeutl,  "-noout",
                                to_abs_file_uri($dir, 1)])));
                }
            }

            ok(!run(app([@storeutl, '-noout',
                         '-subject', '/C=AU/ST=QLD/CN=SSLeay\/rsa test cert',
                         srctop_file('test', 'testx509.pem')])),
               "Checking that -subject can't be used with a single file");

            ok(run(app([@storeutl, '-certs', '-noout',
                        srctop_file('test', 'testx509.pem')])),
               "Checking that -certs returns 1 object on a certificate file");
            ok(run(app([@storeutl, '-certs', '-noout',
                        srctop_file('test', 'testcrl.pem')])),
               "Checking that -certs returns 0 objects on a CRL file");

            ok(run(app([@storeutl, '-crls', '-noout',
                        srctop_file('test', 'testx509.pem')])),
               "Checking that -crls returns 0 objects on a certificate file");
            ok(run(app([@storeutl, '-crls', '-noout',
                        srctop_file('test', 'testcrl.pem')])),
               "Checking that -crls returns 1 object on a CRL file");

          SKIP: {
              skip "failed rehash initialisation", 6 unless $rehash;

              # subject from testx509.pem:
              # '/C=AU/ST=QLD/CN=SSLeay\/rsa test cert'
              # issuer from testcrl.pem:
              # '/C=US/O=RSA Data Security, Inc./OU=Secure Server Certification Authority'
              ok(run(app([@storeutl, '-noout',
                          '-subject', '/C=AU/ST=QLD/CN=SSLeay\/rsa test cert',
                          catdir(curdir(), 'rehash')])));
              ok(run(app([@storeutl, '-noout',
                          '-subject',
                          '/C=US/O=RSA Data Security, Inc./OU=Secure Server Certification Authority',
                          catdir(curdir(), 'rehash')])));
              ok(run(app([@storeutl, '-noout', '-certs',
                          '-subject', '/C=AU/ST=QLD/CN=SSLeay\/rsa test cert',
                          catdir(curdir(), 'rehash')])));
              ok(run(app([@storeutl, '-noout', '-crls',
                          '-subject', '/C=AU/ST=QLD/CN=SSLeay\/rsa test cert',
                          catdir(curdir(), 'rehash')])));
              ok(run(app([@storeutl, '-noout', '-certs',
                          '-subject',
                          '/C=US/O=RSA Data Security, Inc./OU=Secure Server Certification Authority',
                          catdir(curdir(), 'rehash')])));
              ok(run(app([@storeutl, '-noout', '-crls',
                          '-subject',
                          '/C=US/O=RSA Data Security, Inc./OU=Secure Server Certification Authority',
                          catdir(curdir(), 'rehash')])));
            }
        }
    }
}, create => 1, cleanup => 1;

sub init {
    my $cnf = srctop_file('test', 'ca-and-certs.cnf');
    my $cakey = srctop_file('test', 'certs', 'ca-key.pem');
    my @std_args = qw(-provider default);
    push @std_args, qw(-provider legacy)
        unless disabled('legacy');
    return (
            # rsa-key-pkcs1.pem
            run(app(["openssl", "pkey", @std_args,
                     "-in", data_file("rsa-key-2432.pem"),
                     "-out", "rsa-key-pkcs1.pem"]))
            # rsa-key-pkcs1-aes128.pem
            && run(app(["openssl", "rsa", @std_args,
                        "-passout", "pass:password", "-aes128",
                        "-in", "rsa-key-pkcs1.pem",
                        "-out", "rsa-key-pkcs1-aes128.pem"]))
            # dsa-key-pkcs1.pem
            && (!$use_dsa
                || run(app(["openssl", "gendsa", @std_args,
                            "-out", "dsa-key-pkcs1.pem",
                            data_file("dsaparam.pem")])))
            # dsa-key-pkcs1-aes128.pem
            && (!$use_dsa
                || run(app(["openssl", "dsa", @std_args,
                            "-passout", "pass:password", "-aes128",
                            "-in", "dsa-key-pkcs1.pem",
                            "-out", "dsa-key-pkcs1-aes128.pem"])))
            # ec-key-pkcs1.pem (one might think that 'genec' would be practical)
            && (!$use_ecc
                || run(app(["openssl", "ecparam", @std_args,
                            "-genkey",
                            "-name", "prime256v1",
                            "-out", "ec-key-pkcs1.pem"])))
            # ec-key-pkcs1-aes128.pem
            && (!$use_ecc
                || run(app(["openssl", "ec", @std_args,
                            "-passout", "pass:password", "-aes128",
                            "-in", "ec-key-pkcs1.pem",
                            "-out", "ec-key-pkcs1-aes128.pem"])))
            # *-key-pkcs8.pem
            && runall(sub {
                          my $dstfile = shift;
                          (my $srcfile = $dstfile)
                              =~ s/-key-pkcs8\.pem$/-key-pkcs1.pem/i;
                          run(app(["openssl", "pkcs8", @std_args,
                                   "-topk8", "-nocrypt",
                                   "-in", $srcfile, "-out", $dstfile]));
                      }, grep(/-key-pkcs8\.pem$/, @generated_files))
            # *-key-pkcs8-pbes1-sha1-3des.pem
            && runall(sub {
                          my $dstfile = shift;
                          (my $srcfile = $dstfile)
                              =~ s/-key-pkcs8-pbes1-sha1-3des\.pem$
                                  /-key-pkcs8.pem/ix;
                          run(app(["openssl", "pkcs8", @std_args,
                                   "-topk8",
                                   "-passout", "pass:password",
                                   "-v1", "pbeWithSHA1And3-KeyTripleDES-CBC",
                                   "-in", $srcfile, "-out", $dstfile]));
                      }, grep(/-key-pkcs8-pbes1-sha1-3des\.pem$/, @generated_files))
            # *-key-pkcs8-pbes1-md5-des.pem
            && runall(sub {
                          my $dstfile = shift;
                          (my $srcfile = $dstfile)
                              =~ s/-key-pkcs8-pbes1-md5-des\.pem$
                                  /-key-pkcs8.pem/ix;
                          run(app(["openssl", "pkcs8", @std_args,
                                   "-topk8",
                                   "-passout", "pass:password",
                                   "-v1", "pbeWithSHA1And3-KeyTripleDES-CBC",
                                   "-in", $srcfile, "-out", $dstfile]));
                      }, grep(/-key-pkcs8-pbes1-md5-des\.pem$/, @generated_files))
            # *-key-pkcs8-pbes2-sha1.pem
            && runall(sub {
                          my $dstfile = shift;
                          (my $srcfile = $dstfile)
                              =~ s/-key-pkcs8-pbes2-sha1\.pem$
                                  /-key-pkcs8.pem/ix;
                          run(app(["openssl", "pkcs8", @std_args,
                                   "-topk8",
                                   "-passout", "pass:password",
                                   "-v2", "aes256", "-v2prf", "hmacWithSHA1",
                                   "-in", $srcfile, "-out", $dstfile]));
                      }, grep(/-key-pkcs8-pbes2-sha1\.pem$/, @generated_files))
            # *-key-pkcs8-pbes2-sha1.pem
            && runall(sub {
                          my $dstfile = shift;
                          (my $srcfile = $dstfile)
                              =~ s/-key-pkcs8-pbes2-sha256\.pem$
                                  /-key-pkcs8.pem/ix;
                          run(app(["openssl", "pkcs8", @std_args,
                                   "-topk8",
                                   "-passout", "pass:password",
                                   "-v2", "aes256", "-v2prf", "hmacWithSHA256",
                                   "-in", $srcfile, "-out", $dstfile]));
                      }, grep(/-key-pkcs8-pbes2-sha256\.pem$/, @generated_files))
            # *-cert.pem (intermediary for the .p12 inits)
            && run(app(["openssl", "req", "-x509", @std_args,
                        "-config", $cnf, "-reqexts", "v3_ca", "-noenc",
                        "-key", $cakey, "-out", "cacert.pem"]))
            && runall(sub {
                          my $srckey = shift;
                          (my $dstfile = $srckey) =~ s|-key-pkcs8\.|-cert.|;
                          (my $csr = $dstfile) =~ s|\.pem|.csr|;

                          (run(app(["openssl", "req", "-new", @std_args,
                                    "-config", $cnf, "-section", "userreq",
                                    "-key", $srckey, "-out", $csr]))
                           &&
                           run(app(["openssl", "x509", @std_args,
                                    "-days", "3650",
                                    "-CA", "cacert.pem",
                                    "-CAkey", $cakey,
                                    "-set_serial", time(), "-req",
                                    "-in", $csr, "-out", $dstfile])));
                      }, grep(/-key-pkcs8\.pem$/, @generated_files))
            # *.p12
            && runall(sub {
                          my $dstfile = shift;
                          my ($type, $certpbe_index, $keypbe_index,
                              $macalg_index) =
                              $dstfile =~ m{^(.*)-key-(?|
                                                # cert and key PBE are same
                                                ()             #
                                                ([^-]*-[^-]*)- # key & cert PBE
                                                ([^-]*)        # MACalg
                                            |
                                                # cert and key PBE are not same
                                                ([^-]*-[^-]*)- # cert PBE
                                                ([^-]*-[^-]*)- # key PBE
                                                ([^-]*)        # MACalg
                                            )\.}x;
                          if (!$certpbe_index) {
                              $certpbe_index = $keypbe_index;
                          }
                          my $srckey = "$type-key-pkcs8.pem";
                          my $srccert = "$type-cert.pem";
                          my %pbes =
                              (
                               "sha1-3des" => "pbeWithSHA1And3-KeyTripleDES-CBC",
                               "md5-des" => "pbeWithMD5AndDES-CBC",
                               "aes256-cbc" => "AES-256-CBC",
                              );
                          my %macalgs =
                              (
                               "sha1" => "SHA1",
                               "sha256" => "SHA256",
                              );
                          my $certpbe = $pbes{$certpbe_index};
                          my $keypbe = $pbes{$keypbe_index};
                          my $macalg = $macalgs{$macalg_index};
                          if (!defined($certpbe) || !defined($keypbe)
                              || !defined($macalg)) {
                              print STDERR "Cert PBE for $certpbe_index not defined\n"
                                  unless defined $certpbe;
                              print STDERR "Key PBE for $keypbe_index not defined\n"
                                  unless defined $keypbe;
                              print STDERR "MACALG for $macalg_index not defined\n"
                                  unless defined $macalg;
                              print STDERR "(destination file was $dstfile)\n";
                              return 0;
                          }
                          run(app(["openssl", "pkcs12", @std_args,
                                   "-inkey", $srckey,
                                   "-in", $srccert, "-passout", "pass:password",
                                   "-chain", "-CAfile", "cacert.pem",
                                   "-export", "-macalg", $macalg,
                                   "-certpbe", $certpbe, "-keypbe", $keypbe,
                                   "-out", $dstfile]));
                      }, grep(/\.p12/, @generated_files))
            # *.der (the end all init)
            && runall(sub {
                          my $dstfile = shift;
                          (my $srcfile = $dstfile) =~ s/\.der$/.pem/i;
                          if (! -f $srcfile) {
                              $srcfile = srctop_file("test", $srcfile);
                          }
                          my $infh;
                          unless (open $infh, $srcfile) {
                              return 0;
                          }
                          my $l;
                          while (($l = <$infh>) !~ /^-----BEGIN\s/
                                 || $l =~ /^-----BEGIN.*PARAMETERS-----/) {
                          }
                          my $b64 = "";
                          while (($l = <$infh>) !~ /^-----END\s/) {
                              $l =~ s|\R$||;
                              $b64 .= $l unless $l =~ /:/;
                          }
                          close $infh;
                          my $der = decode_base64($b64);
                          unless (length($b64) / 4 * 3 - length($der) < 3) {
                              print STDERR "Length error, ",length($b64),
                                  " bytes of base64 became ",length($der),
                                  " bytes of der? ($srcfile => $dstfile)\n";
                              return 0;
                          }
                          my $outfh;
                          unless (open $outfh, ">:raw", $dstfile) {
                              return 0;
                          }
                          print $outfh $der;
                          close $outfh;
                          return 1;
                      }, grep(/\.der$/, @generated_files))
            && runall(sub {
                          my $srcfile = shift;
                          my $dstfile = $generated_file_files{$srcfile};

                          unless (copy srctop_file($srcfile), $dstfile) {
                              warn "$!\n";
                              return 0;
                          }
                          return 1;
                      }, keys %generated_file_files)
           );
}

sub init_rehash {
    return (
            mkdir(catdir(curdir(), 'rehash'))
            && copy(srctop_file('test', 'testx509.pem'),
                    catdir(curdir(), 'rehash'))
            && copy(srctop_file('test', 'testcrl.pem'),
                    catdir(curdir(), 'rehash'))
            && run(app(['openssl', 'rehash', catdir(curdir(), 'rehash')]))
           );
}

sub runall {
    my ($function, @items) = @_;

    foreach (@items) {
        return 0 unless $function->($_);
    }
    return 1;
}

# According to RFC8089, a relative file: path is invalid.  We still produce
# them for testing purposes.
sub to_file_uri {
    my ($file, $isdir, $authority) = @_;
    my $vol;
    my $dir;

    die "to_file_uri: No file given\n" if !defined($file) || $file eq '';

    ($vol, $dir, $file) = File::Spec->splitpath($file, $isdir // 0);

    # Make sure we have a Unix style directory.
    $dir = join('/', File::Spec->splitdir($dir));
    # Canonicalise it (note: it seems to be only needed on Unix)
    while (1) {
        my $newdir = $dir;
        $newdir =~ s|/[^/]*[^/\.]+[^/]*/\.\./|/|g;
        last if $newdir eq $dir;
        $dir = $newdir;
    }
    # Take care of the corner cases the loop can't handle, and that $dir
    # ends with a / unless it's empty
    $dir =~ s|/[^/]*[^/\.]+[^/]*/\.\.$|/|;
    $dir =~ s|^[^/]*[^/\.]+[^/]*/\.\./|/|;
    $dir =~ s|^[^/]*[^/\.]+[^/]*/\.\.$||;
    if ($isdir // 0) {
        $dir =~ s|/$|| if $dir ne '/';
    } else {
        $dir .= '/' if $dir ne '' && $dir !~ m|/$|;
    }

    # If the file system has separate volumes (at present, Windows and VMS)
    # we need to handle them.  In URIs, they are invariably the first
    # component of the path, which is always absolute.
    # On VMS, user:[foo.bar] translates to /user/foo/bar
    # On Windows, c:\Users\Foo translates to /c:/Users/Foo
    if ($vol ne '') {
        $vol =~ s|:||g if ($^O eq "VMS");
        $dir = '/' . $dir if $dir ne '' && $dir !~ m|^/|;
        $dir = '/' . $vol . $dir;
    }
    $file = $dir . $file;

    return "file://$authority$file" if defined $authority;
    return "file:$file";
}

sub to_abs_file {
    my ($file) = @_;

    return File::Spec->rel2abs($file);
}

sub to_abs_file_uri {
    my ($file, $isdir, $authority) = @_;

    die "to_abs_file_uri: No file given\n" if !defined($file) || $file eq '';
    return to_file_uri(to_abs_file($file), $isdir, $authority);
}