2 # Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 use OpenSSL::Test qw(:DEFAULT srctop_file srctop_dir bldtop_file data_file);
13 my $test_name = "test_store";
17 ( "test/blahdiblah.pem",
18 "test/blahdibleh.der" );
20 ( "test/testx509.pem",
22 "test/testrsapub.pem",
27 ### generated from the source files
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",
59 my $n = (3 * scalar @noexist_files)
60 + (6 * scalar @src_files)
61 + (4 * scalar @generated_files)
66 indir "store_$$" => sub {
69 skip "failed initialisation", $n unless init();
71 foreach (@noexist_files) {
72 my $file = srctop_file($_);
73 ok(!run(app(["openssl", "storeutl", $file])));
74 ok(!run(app(["openssl", "storeutl", to_abs_file($file)])));
75 ok(!run(app(["openssl", "storeutl", to_abs_file_uri($file)])));
77 foreach (@src_files) {
78 my $file = srctop_file($_);
79 ok(run(app(["openssl", "storeutl", $file])));
80 ok(run(app(["openssl", "storeutl", to_abs_file($file)])));
81 ok(run(app(["openssl", "storeutl", to_abs_file_uri($file)])));
82 ok(run(app(["openssl", "storeutl", to_abs_file_uri($file, 0,
84 ok(run(app(["openssl", "storeutl", to_abs_file_uri($file, 0,
86 ok(!run(app(["openssl", "storeutl", to_abs_file_uri($file, 0,
89 foreach (@generated_files) {
90 ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
92 ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
94 ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
95 to_abs_file_uri($_)])));
96 ok(!run(app(["openssl", "storeutl", "-passin", "pass:password",
100 my $dir = srctop_dir("test", "certs");
101 ok(run(app(["openssl", "storeutl", $dir])));
102 ok(run(app(["openssl", "storeutl", to_abs_file($dir, 1)])));
103 ok(run(app(["openssl", "storeutl", to_abs_file_uri($dir, 1)])));
106 }, create => 1, cleanup => 1;
111 run(app(["openssl", "genrsa",
112 "-out", "rsa-key-pkcs1.pem", "2432"]))
114 && run(app(["openssl", "dsaparam", "-genkey",
115 "-out", "dsa-key-pkcs1.pem", "1024"]))
116 # ec-key-pkcs1.pem (one might think that 'genec' would be practical)
117 && run(app(["openssl", "ecparam", "-genkey", "-name", "prime256v1",
118 "-out", "ec-key-pkcs1.pem"]))
119 # rsa-key-pkcs1-aes128.pem
120 && run(app(["openssl", "rsa", "-passout", "pass:password", "-aes128",
121 "-in", "rsa-key-pkcs1.pem",
122 "-out", "rsa-key-pkcs1-aes128.pem"]))
123 # dsa-key-pkcs1-aes128.pem
124 && run(app(["openssl", "dsa", "-passout", "pass:password", "-aes128",
125 "-in", "dsa-key-pkcs1.pem",
126 "-out", "dsa-key-pkcs1-aes128.pem"]))
127 # ec-key-pkcs1-aes128.pem
128 && run(app(["openssl", "ec", "-passout", "pass:password", "-aes128",
129 "-in", "ec-key-pkcs1.pem",
130 "-out", "ec-key-pkcs1-aes128.pem"]))
134 (my $srcfile = $dstfile)
135 =~ s/-key-pkcs8\.pem$/-key-pkcs1.pem/i;
136 run(app(["openssl", "pkcs8", "-topk8", "-nocrypt",
137 "-in", $srcfile, "-out", $dstfile]));
138 }, grep(/-key-pkcs8\.pem$/, @generated_files))
139 # *-key-pkcs8-pbes1-sha1-3des.pem
142 (my $srcfile = $dstfile)
143 =~ s/-key-pkcs8-pbes1-sha1-3des\.pem$
145 run(app(["openssl", "pkcs8", "-topk8",
146 "-passout", "pass:password",
147 "-v1", "pbeWithSHA1And3-KeyTripleDES-CBC",
148 "-in", $srcfile, "-out", $dstfile]));
149 }, grep(/-key-pkcs8-pbes1-sha1-3des\.pem$/, @generated_files))
150 # *-key-pkcs8-pbes1-md5-des.pem
153 (my $srcfile = $dstfile)
154 =~ s/-key-pkcs8-pbes1-md5-des\.pem$
156 run(app(["openssl", "pkcs8", "-topk8",
157 "-passout", "pass:password",
158 "-v1", "pbeWithSHA1And3-KeyTripleDES-CBC",
159 "-in", $srcfile, "-out", $dstfile]));
160 }, grep(/-key-pkcs8-pbes1-md5-des\.pem$/, @generated_files))
161 # *-key-pkcs8-pbes2-sha1.pem
164 (my $srcfile = $dstfile)
165 =~ s/-key-pkcs8-pbes2-sha1\.pem$
167 run(app(["openssl", "pkcs8", "-topk8",
168 "-passout", "pass:password",
169 "-v2", "aes256", "-v2prf", "hmacWithSHA1",
170 "-in", $srcfile, "-out", $dstfile]));
171 }, grep(/-key-pkcs8-pbes2-sha1\.pem$/, @generated_files))
172 # *-key-pkcs8-pbes2-sha1.pem
175 (my $srcfile = $dstfile)
176 =~ s/-key-pkcs8-pbes2-sha256\.pem$
178 run(app(["openssl", "pkcs8", "-topk8",
179 "-passout", "pass:password",
180 "-v2", "aes256", "-v2prf", "hmacWithSHA256",
181 "-in", $srcfile, "-out", $dstfile]));
182 }, grep(/-key-pkcs8-pbes2-sha256\.pem$/, @generated_files))
183 # *-cert.pem (intermediary for the .p12 inits)
184 && run(app(["openssl", "req", "-x509",
185 "-config", data_file("ca.cnf"), "-nodes",
186 "-out", "cacert.pem", "-keyout", "cakey.pem"]))
189 (my $dstfile = $srckey) =~ s|-key-pkcs8\.|-cert.|;
190 (my $csr = $dstfile) =~ s|\.pem|.csr|;
192 (run(app(["openssl", "req", "-new",
193 "-config", data_file("user.cnf"),
194 "-key", $srckey, "-out", $csr]))
196 run(app(["openssl", "x509", "-days", "3650",
198 "-CAkey", "cakey.pem",
199 "-set_serial", time(), "-req",
200 "-in", $csr, "-out", $dstfile])));
201 }, grep(/-key-pkcs8\.pem$/, @generated_files))
205 my ($type, $certpbe_index, $keypbe_index,
207 $dstfile =~ m{^(.*)-key-(?|
208 # cert and key PBE are same
210 ([^-]*-[^-]*)- # key & cert PBE
213 # cert and key PBE are not same
214 ([^-]*-[^-]*)- # cert PBE
215 ([^-]*-[^-]*)- # key PBE
218 if (!$certpbe_index) {
219 $certpbe_index = $keypbe_index;
221 my $srckey = "$type-key-pkcs8.pem";
222 my $srccert = "$type-cert.pem";
225 "sha1-3des" => "pbeWithSHA1And3-KeyTripleDES-CBC",
226 "md5-des" => "pbeWithMD5AndDES-CBC",
227 "aes256-cbc" => "AES-256-CBC",
232 "sha256" => "SHA256",
234 my $certpbe = $pbes{$certpbe_index};
235 my $keypbe = $pbes{$keypbe_index};
236 my $macalg = $macalgs{$macalg_index};
237 if (!defined($certpbe) || !defined($keypbe)
238 || !defined($macalg)) {
239 print STDERR "Cert PBE for $pbe_index not defined\n"
240 unless defined $certpbe;
241 print STDERR "Key PBE for $pbe_index not defined\n"
242 unless defined $keypbe;
243 print STDERR "MACALG for $macalg_index not defined\n"
244 unless defined $macalg;
245 print STDERR "(destination file was $dstfile)\n";
248 run(app(["openssl", "pkcs12", "-inkey", $srckey,
249 "-in", $srccert, "-passout", "pass:password",
250 "-export", "-macalg", $macalg,
251 "-certpbe", $certpbe, "-keypbe", $keypbe,
253 }, grep(/\.p12/, @generated_files))
254 # *.der (the end all init)
257 (my $srcfile = $dstfile) =~ s/\.der$/.pem/i;
259 $srcfile = srctop_file("test", $srcfile);
262 unless (open $infh, $srcfile) {
266 while (($l = <$infh>) !~ /^-----BEGIN\s/
267 || $l =~ /^-----BEGIN.*PARAMETERS-----/) {
270 while (($l = <$infh>) !~ /^-----END\s/) {
272 $b64 .= $l unless $l =~ /:/;
275 my $der = decode_base64($b64);
276 unless (length($b64) / 4 * 3 - length($der) < 3) {
277 print STDERR "Length error, ",length($b64),
278 " bytes of base64 became ",length($der),
279 " bytes of der? ($srcfile => $dstfile)\n";
283 unless (open $outfh, ">:raw", $dstfile) {
289 }, grep(/\.der$/, @generated_files))
294 my ($function, @items) = @_;
297 return 0 unless $function->($_);
302 # According to RFC8089, a relative file: path is invalid. We still produce
303 # them for testing purposes.
305 my ($file, $isdir, $authority) = @_;
309 die "to_file_uri: No file given\n" if !defined($file) || $file eq '';
311 ($vol, $dir, $file) = File::Spec->splitpath($file, $isdir // 0);
313 # Make sure we have a Unix style directory.
314 $dir = join('/', File::Spec->splitdir($dir));
315 # Canonicalise it (note: it seems to be only needed on Unix)
318 $newdir =~ s|/[^/]*[^/\.]+[^/]*/\.\./|/|g;
319 last if $newdir eq $dir;
322 # Take care of the corner cases the loop can't handle, and that $dir
323 # ends with a / unless it's empty
324 $dir =~ s|/[^/]*[^/\.]+[^/]*/\.\.$|/|;
325 $dir =~ s|^[^/]*[^/\.]+[^/]*/\.\./|/|;
326 $dir =~ s|^[^/]*[^/\.]+[^/]*/\.\.$||;
328 $dir =~ s|/$|| if $dir ne '/';
330 $dir .= '/' if $dir ne '' && $dir !~ m|/$|;
333 # If the file system has separate volumes (at present, Windows and VMS)
334 # we need to handle them. In URIs, they are invariably the first
335 # component of the path, which is always absolute.
336 # On VMS, user:[foo.bar] translates to /user/foo/bar
337 # On Windows, c:\Users\Foo translates to /c:/Users/Foo
339 $vol =~ s|:||g if ($^O eq "VMS");
340 $dir = '/' . $dir if $dir ne '' && $dir !~ m|^/|;
341 $dir = '/' . $vol . $dir;
343 $file = $dir . $file;
345 return "file://$authority$file" if defined $authority;
352 return File::Spec->rel2abs($file);
355 sub to_abs_file_uri {
356 my ($file, $isdir, $authority) = @_;
358 die "to_abs_file_uri: No file given\n" if !defined($file) || $file eq '';
359 return to_file_uri(to_abs_file($file), $isdir, $authority);