Enable TLSProxy tests on Windows
[openssl.git] / test / recipes / 90-test_store.t
1 #! /usr/bin/env perl
2 # Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the OpenSSL license (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9 use File::Spec;
10 use File::Copy;
11 use MIME::Base64;
12 use OpenSSL::Test qw(:DEFAULT srctop_file srctop_dir bldtop_file data_file);
13 use OpenSSL::Test::Utils;
14
15 my $test_name = "test_store";
16 setup($test_name);
17
18 my @noexist_files =
19     ( "test/blahdiblah.pem",
20       "test/blahdibleh.der" );
21 my @src_files =
22     ( "test/testx509.pem",
23       "test/testrsa.pem",
24       "test/testrsapub.pem",
25       "test/testcrl.pem",
26       "apps/server.pem" );
27 my @generated_files =
28     (
29      ### generated from the source files
30
31      "testx509.der",
32      "testrsa.der",
33      "testrsapub.der",
34      "testcrl.der",
35
36      ### generated locally
37
38      "rsa-key-pkcs1.pem", "rsa-key-pkcs1.der",
39      "rsa-key-pkcs1-aes128.pem",
40      "rsa-key-pkcs8.pem", "rsa-key-pkcs8.der",
41      "rsa-key-pkcs8-pbes1-sha1-3des.pem", "rsa-key-pkcs8-pbes1-sha1-3des.der",
42      "rsa-key-pkcs8-pbes2-sha1.pem", "rsa-key-pkcs8-pbes2-sha1.der",
43      "rsa-key-sha1-3des-sha1.p12", "rsa-key-sha1-3des-sha256.p12",
44      "rsa-key-aes256-cbc-sha256.p12",
45      "rsa-key-md5-des-sha1.p12",
46      "rsa-key-aes256-cbc-md5-des-sha256.p12",
47      "rsa-key-pkcs8-pbes2-sha256.pem", "rsa-key-pkcs8-pbes2-sha256.der",
48      "rsa-key-pkcs8-pbes1-md5-des.pem", "rsa-key-pkcs8-pbes1-md5-des.der",
49      "dsa-key-pkcs1.pem", "dsa-key-pkcs1.der",
50      "dsa-key-pkcs1-aes128.pem",
51      "dsa-key-pkcs8.pem", "dsa-key-pkcs8.der",
52      "dsa-key-pkcs8-pbes2-sha1.pem", "dsa-key-pkcs8-pbes2-sha1.der",
53      "dsa-key-aes256-cbc-sha256.p12",
54      "ec-key-pkcs1.pem", "ec-key-pkcs1.der",
55      "ec-key-pkcs1-aes128.pem",
56      "ec-key-pkcs8.pem", "ec-key-pkcs8.der",
57      "ec-key-pkcs8-pbes2-sha1.pem", "ec-key-pkcs8-pbes2-sha1.der",
58      "ec-key-aes256-cbc-sha256.p12",
59     );
60 my %generated_file_files =
61     $^O eq 'linux'
62     ? ( "test/testx509.pem" => "file:testx509.pem",
63         "test/testrsa.pem" => "file:testrsa.pem",
64         "test/testrsapub.pem" => "file:testrsapub.pem",
65         "test/testcrl.pem" => "file:testcrl.pem",
66         "apps/server.pem" => "file:server.pem" )
67     : ();
68 my @noexist_file_files =
69     ( "file:blahdiblah.pem",
70       "file:test/blahdibleh.der" );
71
72
73 my $n = (3 * scalar @noexist_files)
74     + (6 * scalar @src_files)
75     + (4 * scalar @generated_files)
76     + (scalar keys %generated_file_files)
77     + (scalar @noexist_file_files)
78     + 4;
79
80 plan tests => $n;
81
82 indir "store_$$" => sub {
83  SKIP:
84     {
85         skip "failed initialisation", $n unless init();
86
87         # test PEM_read_bio_PrivateKey
88         ok(run(app(["openssl", "rsa", "-in", "rsa-key-pkcs8-pbes2-sha256.pem",
89                     "-passin", "pass:password"])));
90
91         foreach (@noexist_files) {
92             my $file = srctop_file($_);
93
94             ok(!run(app(["openssl", "storeutl", $file])));
95             ok(!run(app(["openssl", "storeutl", to_abs_file($file)])));
96             {
97                 local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
98
99                 ok(!run(app(["openssl", "storeutl", to_abs_file_uri($file)])));
100             }
101         }
102         foreach (@src_files) {
103             my $file = srctop_file($_);
104
105             ok(run(app(["openssl", "storeutl", $file])));
106             ok(run(app(["openssl", "storeutl", to_abs_file($file)])));
107             {
108                 local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
109
110                 ok(run(app(["openssl", "storeutl", to_abs_file_uri($file)])));
111                 ok(run(app(["openssl", "storeutl",
112                             to_abs_file_uri($file, 0, "")])));
113                 ok(run(app(["openssl", "storeutl",
114                             to_abs_file_uri($file, 0, "localhost")])));
115                 ok(!run(app(["openssl", "storeutl",
116                              to_abs_file_uri($file, 0, "dummy")])));
117             }
118         }
119         foreach (@generated_files) {
120             ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
121                         $_])));
122             ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
123                         to_abs_file($_)])));
124
125             {
126                 local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
127
128                 ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
129                             to_abs_file_uri($_)])));
130                 ok(!run(app(["openssl", "storeutl", "-passin", "pass:password",
131                              to_file_uri($_)])));
132             }
133         }
134         foreach (values %generated_file_files) {
135             local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
136
137             ok(run(app(["openssl", "storeutl", $_])));
138         }
139         foreach (@noexist_file_files) {
140             local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
141
142             ok(!run(app(["openssl", "storeutl", $_])));
143         }
144         {
145             my $dir = srctop_dir("test", "certs");
146
147             ok(run(app(["openssl", "storeutl", $dir])));
148             ok(run(app(["openssl", "storeutl", to_abs_file($dir, 1)])));
149             {
150                 local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
151
152                 ok(run(app(["openssl", "storeutl", to_abs_file_uri($dir, 1)])));
153             }
154         }
155     }
156 }, create => 1, cleanup => 1;
157
158 sub init {
159     return (
160             # rsa-key-pkcs1.pem
161             run(app(["openssl", "genrsa",
162                      "-out", "rsa-key-pkcs1.pem", "2432"]))
163             # dsa-key-pkcs1.pem
164             && run(app(["openssl", "dsaparam", "-genkey",
165                         "-out", "dsa-key-pkcs1.pem", "1024"]))
166             # ec-key-pkcs1.pem (one might think that 'genec' would be practical)
167             && run(app(["openssl", "ecparam", "-genkey", "-name", "prime256v1",
168                         "-out", "ec-key-pkcs1.pem"]))
169             # rsa-key-pkcs1-aes128.pem
170             && run(app(["openssl", "rsa", "-passout", "pass:password", "-aes128",
171                         "-in", "rsa-key-pkcs1.pem",
172                         "-out", "rsa-key-pkcs1-aes128.pem"]))
173             # dsa-key-pkcs1-aes128.pem
174             && run(app(["openssl", "dsa", "-passout", "pass:password", "-aes128",
175                         "-in", "dsa-key-pkcs1.pem",
176                         "-out", "dsa-key-pkcs1-aes128.pem"]))
177             # ec-key-pkcs1-aes128.pem
178             && run(app(["openssl", "ec", "-passout", "pass:password", "-aes128",
179                         "-in", "ec-key-pkcs1.pem",
180                         "-out", "ec-key-pkcs1-aes128.pem"]))
181             # *-key-pkcs8.pem
182             && runall(sub {
183                           my $dstfile = shift;
184                           (my $srcfile = $dstfile)
185                               =~ s/-key-pkcs8\.pem$/-key-pkcs1.pem/i;
186                           run(app(["openssl", "pkcs8", "-topk8", "-nocrypt",
187                                    "-in", $srcfile, "-out", $dstfile]));
188                       }, grep(/-key-pkcs8\.pem$/, @generated_files))
189             # *-key-pkcs8-pbes1-sha1-3des.pem
190             && runall(sub {
191                           my $dstfile = shift;
192                           (my $srcfile = $dstfile)
193                               =~ s/-key-pkcs8-pbes1-sha1-3des\.pem$
194                                   /-key-pkcs8.pem/ix;
195                           run(app(["openssl", "pkcs8", "-topk8",
196                                    "-passout", "pass:password",
197                                    "-v1", "pbeWithSHA1And3-KeyTripleDES-CBC",
198                                    "-in", $srcfile, "-out", $dstfile]));
199                       }, grep(/-key-pkcs8-pbes1-sha1-3des\.pem$/, @generated_files))
200             # *-key-pkcs8-pbes1-md5-des.pem
201             && runall(sub {
202                           my $dstfile = shift;
203                           (my $srcfile = $dstfile)
204                               =~ s/-key-pkcs8-pbes1-md5-des\.pem$
205                                   /-key-pkcs8.pem/ix;
206                           run(app(["openssl", "pkcs8", "-topk8",
207                                    "-passout", "pass:password",
208                                    "-v1", "pbeWithSHA1And3-KeyTripleDES-CBC",
209                                    "-in", $srcfile, "-out", $dstfile]));
210                       }, grep(/-key-pkcs8-pbes1-md5-des\.pem$/, @generated_files))
211             # *-key-pkcs8-pbes2-sha1.pem
212             && runall(sub {
213                           my $dstfile = shift;
214                           (my $srcfile = $dstfile)
215                               =~ s/-key-pkcs8-pbes2-sha1\.pem$
216                                   /-key-pkcs8.pem/ix;
217                           run(app(["openssl", "pkcs8", "-topk8",
218                                    "-passout", "pass:password",
219                                    "-v2", "aes256", "-v2prf", "hmacWithSHA1",
220                                    "-in", $srcfile, "-out", $dstfile]));
221                       }, grep(/-key-pkcs8-pbes2-sha1\.pem$/, @generated_files))
222             # *-key-pkcs8-pbes2-sha1.pem
223             && runall(sub {
224                           my $dstfile = shift;
225                           (my $srcfile = $dstfile)
226                               =~ s/-key-pkcs8-pbes2-sha256\.pem$
227                                   /-key-pkcs8.pem/ix;
228                           run(app(["openssl", "pkcs8", "-topk8",
229                                    "-passout", "pass:password",
230                                    "-v2", "aes256", "-v2prf", "hmacWithSHA256",
231                                    "-in", $srcfile, "-out", $dstfile]));
232                       }, grep(/-key-pkcs8-pbes2-sha256\.pem$/, @generated_files))
233             # *-cert.pem (intermediary for the .p12 inits)
234             && run(app(["openssl", "req", "-x509",
235                         "-config", data_file("ca.cnf"), "-nodes",
236                         "-out", "cacert.pem", "-keyout", "cakey.pem"]))
237             && runall(sub {
238                           my $srckey = shift;
239                           (my $dstfile = $srckey) =~ s|-key-pkcs8\.|-cert.|;
240                           (my $csr = $dstfile) =~ s|\.pem|.csr|;
241
242                           (run(app(["openssl", "req", "-new",
243                                     "-config", data_file("user.cnf"),
244                                     "-key", $srckey, "-out", $csr]))
245                            &&
246                            run(app(["openssl", "x509", "-days", "3650",
247                                     "-CA", "cacert.pem",
248                                     "-CAkey", "cakey.pem",
249                                     "-set_serial", time(), "-req",
250                                     "-in", $csr, "-out", $dstfile])));
251                       }, grep(/-key-pkcs8\.pem$/, @generated_files))
252             # *.p12
253             && runall(sub {
254                           my $dstfile = shift;
255                           my ($type, $certpbe_index, $keypbe_index,
256                               $macalg_index) =
257                               $dstfile =~ m{^(.*)-key-(?|
258                                                 # cert and key PBE are same
259                                                 ()             #
260                                                 ([^-]*-[^-]*)- # key & cert PBE
261                                                 ([^-]*)        # MACalg
262                                             |
263                                                 # cert and key PBE are not same
264                                                 ([^-]*-[^-]*)- # cert PBE
265                                                 ([^-]*-[^-]*)- # key PBE
266                                                 ([^-]*)        # MACalg
267                                             )\.}x;
268                           if (!$certpbe_index) {
269                               $certpbe_index = $keypbe_index;
270                           }
271                           my $srckey = "$type-key-pkcs8.pem";
272                           my $srccert = "$type-cert.pem";
273                           my %pbes =
274                               (
275                                "sha1-3des" => "pbeWithSHA1And3-KeyTripleDES-CBC",
276                                "md5-des" => "pbeWithMD5AndDES-CBC",
277                                "aes256-cbc" => "AES-256-CBC",
278                               );
279                           my %macalgs =
280                               (
281                                "sha1" => "SHA1",
282                                "sha256" => "SHA256",
283                               );
284                           my $certpbe = $pbes{$certpbe_index};
285                           my $keypbe = $pbes{$keypbe_index};
286                           my $macalg = $macalgs{$macalg_index};
287                           if (!defined($certpbe) || !defined($keypbe)
288                               || !defined($macalg)) {
289                               print STDERR "Cert PBE for $pbe_index not defined\n"
290                                   unless defined $certpbe;
291                               print STDERR "Key PBE for $pbe_index not defined\n"
292                                   unless defined $keypbe;
293                               print STDERR "MACALG for $macalg_index not defined\n"
294                                   unless defined $macalg;
295                               print STDERR "(destination file was $dstfile)\n";
296                               return 0;
297                           }
298                           run(app(["openssl", "pkcs12", "-inkey", $srckey,
299                                    "-in", $srccert, "-passout", "pass:password",
300                                    "-export", "-macalg", $macalg,
301                                    "-certpbe", $certpbe, "-keypbe", $keypbe,
302                                    "-out", $dstfile]));
303                       }, grep(/\.p12/, @generated_files))
304             # *.der (the end all init)
305             && runall(sub {
306                           my $dstfile = shift;
307                           (my $srcfile = $dstfile) =~ s/\.der$/.pem/i;
308                           if (! -f $srcfile) {
309                               $srcfile = srctop_file("test", $srcfile);
310                           }
311                           my $infh;
312                           unless (open $infh, $srcfile) {
313                               return 0;
314                           }
315                           my $l;
316                           while (($l = <$infh>) !~ /^-----BEGIN\s/
317                                  || $l =~ /^-----BEGIN.*PARAMETERS-----/) {
318                           }
319                           my $b64 = "";
320                           while (($l = <$infh>) !~ /^-----END\s/) {
321                               $l =~ s|\R$||;
322                               $b64 .= $l unless $l =~ /:/;
323                           }
324                           close $infh;
325                           my $der = decode_base64($b64);
326                           unless (length($b64) / 4 * 3 - length($der) < 3) {
327                               print STDERR "Length error, ",length($b64),
328                                   " bytes of base64 became ",length($der),
329                                   " bytes of der? ($srcfile => $dstfile)\n";
330                               return 0;
331                           }
332                           my $outfh;
333                           unless (open $outfh, ">:raw", $dstfile) {
334                               return 0;
335                           }
336                           print $outfh $der;
337                           close $outfh;
338                           return 1;
339                       }, grep(/\.der$/, @generated_files))
340             && runall(sub {
341                           my $srcfile = shift;
342                           my $dstfile = $generated_file_files{$srcfile};
343
344                           unless (copy srctop_file($srcfile), $dstfile) {
345                               warn "$!\n";
346                               return 0;
347                           }
348                           return 1;
349                       }, keys %generated_file_files)
350            );
351 }
352
353 sub runall {
354     my ($function, @items) = @_;
355
356     foreach (@items) {
357         return 0 unless $function->($_);
358     }
359     return 1;
360 }
361
362 # According to RFC8089, a relative file: path is invalid.  We still produce
363 # them for testing purposes.
364 sub to_file_uri {
365     my ($file, $isdir, $authority) = @_;
366     my $vol;
367     my $dir;
368
369     die "to_file_uri: No file given\n" if !defined($file) || $file eq '';
370
371     ($vol, $dir, $file) = File::Spec->splitpath($file, $isdir // 0);
372
373     # Make sure we have a Unix style directory.
374     $dir = join('/', File::Spec->splitdir($dir));
375     # Canonicalise it (note: it seems to be only needed on Unix)
376     while (1) {
377         my $newdir = $dir;
378         $newdir =~ s|/[^/]*[^/\.]+[^/]*/\.\./|/|g;
379         last if $newdir eq $dir;
380         $dir = $newdir;
381     }
382     # Take care of the corner cases the loop can't handle, and that $dir
383     # ends with a / unless it's empty
384     $dir =~ s|/[^/]*[^/\.]+[^/]*/\.\.$|/|;
385     $dir =~ s|^[^/]*[^/\.]+[^/]*/\.\./|/|;
386     $dir =~ s|^[^/]*[^/\.]+[^/]*/\.\.$||;
387     if ($isdir // 0) {
388         $dir =~ s|/$|| if $dir ne '/';
389     } else {
390         $dir .= '/' if $dir ne '' && $dir !~ m|/$|;
391     }
392
393     # If the file system has separate volumes (at present, Windows and VMS)
394     # we need to handle them.  In URIs, they are invariably the first
395     # component of the path, which is always absolute.
396     # On VMS, user:[foo.bar] translates to /user/foo/bar
397     # On Windows, c:\Users\Foo translates to /c:/Users/Foo
398     if ($vol ne '') {
399         $vol =~ s|:||g if ($^O eq "VMS");
400         $dir = '/' . $dir if $dir ne '' && $dir !~ m|^/|;
401         $dir = '/' . $vol . $dir;
402     }
403     $file = $dir . $file;
404
405     return "file://$authority$file" if defined $authority;
406     return "file:$file";
407 }
408
409 sub to_abs_file {
410     my ($file) = @_;
411
412     return File::Spec->rel2abs($file);
413 }
414
415 sub to_abs_file_uri {
416     my ($file, $isdir, $authority) = @_;
417
418     die "to_abs_file_uri: No file given\n" if !defined($file) || $file eq '';
419     return to_file_uri(to_abs_file($file), $isdir, $authority);
420 }