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