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 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
35 ### These examples were pilfered from OpenConnect's test suite
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",
60 my $n = (2 * scalar @noexist_files)
61 + (5 * scalar @src_files)
62 + (3 * 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_file_uri($file)])));
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,
82 ok(run(app(["openssl", "storeutl", to_file_uri($file, 0,
84 ok(!run(app(["openssl", "storeutl", to_file_uri($file, 0,
87 foreach (@generated_files) {
88 ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
90 ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
92 ok(!run(app(["openssl", "storeutl", "-passin", "pass:password",
93 to_rel_file_uri($_)])));
96 }, create => 1, cleanup => 1;
101 run(app(["openssl", "genrsa",
102 "-out", "rsa-key-pkcs1.pem", "2432"]))
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"]))
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
132 (my $srcfile = $dstfile)
133 =~ s/-key-pkcs8-pbes1-sha1-3des\.pem$
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
143 (my $srcfile = $dstfile)
144 =~ s/-key-pkcs8-pbes1-md5-des\.pem$
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
154 (my $srcfile = $dstfile)
155 =~ s/-key-pkcs8-pbes2-sha1\.pem$
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
165 (my $srcfile = $dstfile)
166 =~ s/-key-pkcs8-pbes2-sha256\.pem$
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"]))
179 (my $dstfile = $srckey) =~ s|-key-pkcs8\.|-cert.|;
180 (my $csr = $dstfile) =~ s|\.pem|.csr|;
182 (run(app(["openssl", "req", "-new",
183 "-config", data_file("user.cnf"),
184 "-key", $srckey, "-out", $csr]))
186 run(app(["openssl", "x509", "-days", "3650",
188 "-CAkey", "cakey.pem",
189 "-set_serial", time(), "-req",
190 "-in", $csr, "-out", $dstfile])));
191 }, grep(/-key-pkcs8\.pem$/, @generated_files))
195 my ($type, $certpbe_index, $keypbe_index,
197 $dstfile =~ m{^(.*)-key-(?|
198 # cert and key PBE are same
200 ([^-]*-[^-]*)- # key & cert PBE
203 # cert and key PBE are not same
204 ([^-]*-[^-]*)- # cert PBE
205 ([^-]*-[^-]*)- # key PBE
208 if (!$certpbe_index) {
209 $certpbe_index = $keypbe_index;
211 my $srckey = "$type-key-pkcs8.pem";
212 my $srccert = "$type-cert.pem";
215 "sha1-3des" => "pbeWithSHA1And3-KeyTripleDES-CBC",
216 "md5-des" => "pbeWithMD5AndDES-CBC",
217 "aes256-cbc" => "AES-256-CBC",
222 "sha256" => "SHA256",
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";
238 run(app(["openssl", "pkcs12", "-inkey", $srckey,
239 "-in", $srccert, "-passout", "pass:password",
240 "-export", "-macalg", $macalg,
241 "-certpbe", $certpbe, "-keypbe", $keypbe,
243 }, grep(/\.p12/, @generated_files))
244 # *.der (the end all init)
247 (my $srcfile = $dstfile) =~ s/\.der$/.pem/i;
249 $srcfile = srctop_file("test", $srcfile);
252 unless (open $infh, $srcfile) {
256 while (($l = <$infh>) !~ /^-----BEGIN\s/
257 || $l =~ /^-----BEGIN.*PARAMETERS-----/) {
260 while (($l = <$infh>) !~ /^-----END\s/) {
262 $b64 .= $l unless $l =~ /:/;
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";
273 unless (open $outfh, ">:raw", $dstfile) {
279 }, grep(/\.der$/, @generated_files))
284 my ($function, @items) = @_;
287 return 0 unless $function->($_);
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) = @_;
299 die "to_rel_file_uri: No file given\n" if !defined($file) || $file eq '';
301 ($vol, $dir, $file) = File::Spec->splitpath($file, $isdir // 0);
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)
308 $newdir =~ s|/[^/]*[^/\.]+[^/]*/\.\./|/|g;
309 last if $newdir eq $dir;
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|^[^/]*[^/\.]+[^/]*/\.\.$||;
318 $dir =~ s|/$|| if $dir ne '/';
320 $dir .= '/' if $dir ne '' && $dir !~ m|/$|;
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
329 $vol =~ s|:||g if ($^O eq "VMS");
330 $dir = '/' . $dir if $dir ne '' && $dir !~ m|^/|;
331 $dir = '/' . $vol . $dir;
333 $file = $dir . $file;
335 return "file://$authority$file" if defined $authority;
340 my ($file, $isdir, $authority) = @_;
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);