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