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
12 use OpenSSL::Test qw(:DEFAULT srctop_file srctop_dir bldtop_file data_file);
13 use OpenSSL::Test::Utils;
15 my $test_name = "test_store";
19 ( "test/blahdiblah.pem",
20 "test/blahdibleh.der" );
22 ( "test/testx509.pem",
24 "test/testrsapub.pem",
29 ### generated from the source files
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",
60 my %generated_file_files =
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" )
68 my @noexist_file_files =
69 ( "file:blahdiblah.pem",
70 "file:test/blahdibleh.der" );
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)
82 indir "store_$$" => sub {
85 skip "failed initialisation", $n unless init();
87 # test PEM_read_bio_PrivateKey
88 ok(run(app(["openssl", "rsa", "-in", "rsa-key-pkcs8-pbes2-sha256.pem",
89 "-passin", "pass:password"])));
91 foreach (@noexist_files) {
92 my $file = srctop_file($_);
94 ok(!run(app(["openssl", "storeutl", $file])));
95 ok(!run(app(["openssl", "storeutl", to_abs_file($file)])));
97 local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
99 ok(!run(app(["openssl", "storeutl", to_abs_file_uri($file)])));
102 foreach (@src_files) {
103 my $file = srctop_file($_);
105 ok(run(app(["openssl", "storeutl", $file])));
106 ok(run(app(["openssl", "storeutl", to_abs_file($file)])));
108 local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
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")])));
119 foreach (@generated_files) {
120 ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
122 ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
126 local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
128 ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
129 to_abs_file_uri($_)])));
130 ok(!run(app(["openssl", "storeutl", "-passin", "pass:password",
134 foreach (values %generated_file_files) {
135 local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
137 ok(run(app(["openssl", "storeutl", $_])));
139 foreach (@noexist_file_files) {
140 local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
142 ok(!run(app(["openssl", "storeutl", $_])));
145 my $dir = srctop_dir("test", "certs");
147 ok(run(app(["openssl", "storeutl", $dir])));
148 ok(run(app(["openssl", "storeutl", to_abs_file($dir, 1)])));
150 local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
152 ok(run(app(["openssl", "storeutl", to_abs_file_uri($dir, 1)])));
156 }, create => 1, cleanup => 1;
161 run(app(["openssl", "genrsa",
162 "-out", "rsa-key-pkcs1.pem", "2432"]))
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"]))
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
192 (my $srcfile = $dstfile)
193 =~ s/-key-pkcs8-pbes1-sha1-3des\.pem$
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
203 (my $srcfile = $dstfile)
204 =~ s/-key-pkcs8-pbes1-md5-des\.pem$
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
214 (my $srcfile = $dstfile)
215 =~ s/-key-pkcs8-pbes2-sha1\.pem$
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
225 (my $srcfile = $dstfile)
226 =~ s/-key-pkcs8-pbes2-sha256\.pem$
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"]))
239 (my $dstfile = $srckey) =~ s|-key-pkcs8\.|-cert.|;
240 (my $csr = $dstfile) =~ s|\.pem|.csr|;
242 (run(app(["openssl", "req", "-new",
243 "-config", data_file("user.cnf"),
244 "-key", $srckey, "-out", $csr]))
246 run(app(["openssl", "x509", "-days", "3650",
248 "-CAkey", "cakey.pem",
249 "-set_serial", time(), "-req",
250 "-in", $csr, "-out", $dstfile])));
251 }, grep(/-key-pkcs8\.pem$/, @generated_files))
255 my ($type, $certpbe_index, $keypbe_index,
257 $dstfile =~ m{^(.*)-key-(?|
258 # cert and key PBE are same
260 ([^-]*-[^-]*)- # key & cert PBE
263 # cert and key PBE are not same
264 ([^-]*-[^-]*)- # cert PBE
265 ([^-]*-[^-]*)- # key PBE
268 if (!$certpbe_index) {
269 $certpbe_index = $keypbe_index;
271 my $srckey = "$type-key-pkcs8.pem";
272 my $srccert = "$type-cert.pem";
275 "sha1-3des" => "pbeWithSHA1And3-KeyTripleDES-CBC",
276 "md5-des" => "pbeWithMD5AndDES-CBC",
277 "aes256-cbc" => "AES-256-CBC",
282 "sha256" => "SHA256",
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";
298 run(app(["openssl", "pkcs12", "-inkey", $srckey,
299 "-in", $srccert, "-passout", "pass:password",
300 "-export", "-macalg", $macalg,
301 "-certpbe", $certpbe, "-keypbe", $keypbe,
303 }, grep(/\.p12/, @generated_files))
304 # *.der (the end all init)
307 (my $srcfile = $dstfile) =~ s/\.der$/.pem/i;
309 $srcfile = srctop_file("test", $srcfile);
312 unless (open $infh, $srcfile) {
316 while (($l = <$infh>) !~ /^-----BEGIN\s/
317 || $l =~ /^-----BEGIN.*PARAMETERS-----/) {
320 while (($l = <$infh>) !~ /^-----END\s/) {
322 $b64 .= $l unless $l =~ /:/;
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";
333 unless (open $outfh, ">:raw", $dstfile) {
339 }, grep(/\.der$/, @generated_files))
342 my $dstfile = $generated_file_files{$srcfile};
344 unless (copy srctop_file($srcfile), $dstfile) {
349 }, keys %generated_file_files)
354 my ($function, @items) = @_;
357 return 0 unless $function->($_);
362 # According to RFC8089, a relative file: path is invalid. We still produce
363 # them for testing purposes.
365 my ($file, $isdir, $authority) = @_;
369 die "to_file_uri: No file given\n" if !defined($file) || $file eq '';
371 ($vol, $dir, $file) = File::Spec->splitpath($file, $isdir // 0);
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)
378 $newdir =~ s|/[^/]*[^/\.]+[^/]*/\.\./|/|g;
379 last if $newdir eq $dir;
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|^[^/]*[^/\.]+[^/]*/\.\.$||;
388 $dir =~ s|/$|| if $dir ne '/';
390 $dir .= '/' if $dir ne '' && $dir !~ m|/$|;
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
399 $vol =~ s|:||g if ($^O eq "VMS");
400 $dir = '/' . $dir if $dir ne '' && $dir !~ m|^/|;
401 $dir = '/' . $vol . $dir;
403 $file = $dir . $file;
405 return "file://$authority$file" if defined $authority;
412 return File::Spec->rel2abs($file);
415 sub to_abs_file_uri {
416 my ($file, $isdir, $authority) = @_;
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);