90-test_store.t 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #! /usr/bin/env perl
  2. # Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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. use File::Spec::Functions;
  9. use File::Copy;
  10. use MIME::Base64;
  11. use OpenSSL::Test qw(:DEFAULT srctop_file srctop_dir bldtop_file bldtop_dir
  12. data_file);
  13. use OpenSSL::Test::Utils;
  14. my $test_name = "test_store";
  15. setup($test_name);
  16. my $use_md5 = !disabled("md5");
  17. my $use_des = !(disabled("des") || disabled("legacy")); # also affects 3des and pkcs12 app
  18. my $use_dsa = !disabled("dsa");
  19. my $use_ecc = !disabled("ec");
  20. my @noexist_files =
  21. ( "test/blahdiblah.pem",
  22. "test/blahdibleh.der" );
  23. my @src_files =
  24. ( "test/testx509.pem",
  25. "test/testrsa.pem",
  26. "test/testrsapub.pem",
  27. "test/testcrl.pem",
  28. "apps/server.pem" );
  29. my @data_files =
  30. ( "testrsa.msb" );
  31. push(@data_files,
  32. ( "testrsa.pvk" ))
  33. unless disabled("legacy") || disabled("rc4");
  34. my @src_rsa_files =
  35. ( "test/testrsa.pem",
  36. "test/testrsapub.pem" );
  37. my @generated_files =
  38. (
  39. ### generated from the source files
  40. "testx509.der",
  41. "testrsa.der",
  42. "testrsapub.der",
  43. "testcrl.der",
  44. ### generated locally
  45. "rsa-key-pkcs1.pem", "rsa-key-pkcs1.der",
  46. "rsa-key-pkcs1-aes128.pem",
  47. "rsa-key-pkcs8.pem", "rsa-key-pkcs8.der",
  48. "rsa-key-pkcs8-pbes2-sha1.pem", "rsa-key-pkcs8-pbes2-sha1.der",
  49. "rsa-key-pkcs8-pbes2-sha256.pem", "rsa-key-pkcs8-pbes2-sha256.der",
  50. );
  51. push(@generated_files, (
  52. "rsa-key-pkcs8-pbes1-sha1-3des.pem", "rsa-key-pkcs8-pbes1-sha1-3des.der",
  53. )) if $use_des;
  54. push(@generated_files, (
  55. "rsa-key-sha1-3des-sha1.p12", "rsa-key-sha1-3des-sha256.p12",
  56. "rsa-key-aes256-cbc-sha256.p12",
  57. "rsa-key-md5-des-sha1.p12",
  58. "rsa-key-aes256-cbc-md5-des-sha256.p12"
  59. )) if $use_des;
  60. push(@generated_files, (
  61. "rsa-key-pkcs8-pbes1-md5-des.pem", "rsa-key-pkcs8-pbes1-md5-des.der"
  62. )) if $use_md5 && $use_des;
  63. push(@generated_files, (
  64. "dsa-key-pkcs1.pem", "dsa-key-pkcs1.der",
  65. "dsa-key-pkcs1-aes128.pem",
  66. "dsa-key-pkcs8.pem", "dsa-key-pkcs8.der",
  67. "dsa-key-pkcs8-pbes2-sha1.pem", "dsa-key-pkcs8-pbes2-sha1.der",
  68. )) if $use_dsa;
  69. push(@generated_files, "dsa-key-aes256-cbc-sha256.p12") if $use_dsa && $use_des;
  70. push(@generated_files, (
  71. "ec-key-pkcs1.pem", "ec-key-pkcs1.der",
  72. "ec-key-pkcs1-aes128.pem",
  73. "ec-key-pkcs8.pem", "ec-key-pkcs8.der",
  74. "ec-key-pkcs8-pbes2-sha1.pem", "ec-key-pkcs8-pbes2-sha1.der",
  75. )) if $use_ecc;
  76. push(@generated_files, "ec-key-aes256-cbc-sha256.p12") if $use_ecc && $use_des;
  77. my %generated_file_files =
  78. $^O eq 'linux'
  79. ? ( "test/testx509.pem" => "file:testx509.pem",
  80. "test/testrsa.pem" => "file:testrsa.pem",
  81. "test/testrsapub.pem" => "file:testrsapub.pem",
  82. "test/testcrl.pem" => "file:testcrl.pem",
  83. "apps/server.pem" => "file:server.pem" )
  84. : ();
  85. my @noexist_file_files =
  86. ( "file:blahdiblah.pem",
  87. "file:test/blahdibleh.der" );
  88. # There is more than one method to get a 'file:' loader.
  89. # The default is a built-in provider implementation.
  90. # However, there is also an engine, specially for testing purposes.
  91. #
  92. # @methods is a collection of extra 'openssl storeutl' arguments used to
  93. # try the different methods.
  94. my @methods;
  95. my @prov_method = qw(-provider default);
  96. push @prov_method, qw(-provider legacy) unless disabled('legacy');
  97. push @methods, [ @prov_method ];
  98. push @methods, [qw(-engine loader_attic)]
  99. unless disabled('loadereng');
  100. my $n = scalar @methods
  101. * ( (3 * scalar @noexist_files)
  102. + (6 * scalar @src_files)
  103. + (2 * scalar @data_files)
  104. + (4 * scalar @generated_files)
  105. + (scalar keys %generated_file_files)
  106. + (scalar @noexist_file_files)
  107. + 3
  108. + 11 );
  109. # Test doesn't work under msys because the file name munging doesn't work
  110. # correctly with the "ot:" prefix
  111. my $do_test_ossltest_store =
  112. !(disabled("engine") || disabled("dynamic-engine") || $^O =~ /^msys$/);
  113. if ($do_test_ossltest_store) {
  114. # test loading with apps 'org.openssl.engine:' loader, using the
  115. # ossltest engine.
  116. $n += 4 * scalar @src_rsa_files;
  117. }
  118. plan skip_all => "No plan" if $n == 0;
  119. plan tests => $n;
  120. indir "store_$$" => sub {
  121. if ($do_test_ossltest_store) {
  122. # ossltest loads PEM files, with names prefixed with 'ot:'.
  123. # This prefix ensures that the files are, in fact, loaded through
  124. # that engine and not mistakenly going through the 'file:' loader.
  125. my $engine_scheme = 'org.openssl.engine:';
  126. $ENV{OPENSSL_ENGINES} = bldtop_dir("engines");
  127. foreach (@src_rsa_files) {
  128. my $file = srctop_file($_);
  129. my $file_abs = to_abs_file($file);
  130. my @pubin = $_ =~ m|pub\.pem$| ? ("-pubin") : ();
  131. ok(run(app(["openssl", "rsa", "-text", "-noout", @pubin,
  132. "-engine", "ossltest", "-inform", "engine",
  133. "-in", "ot:$file"])));
  134. ok(run(app(["openssl", "rsa", "-text", "-noout", @pubin,
  135. "-engine", "ossltest", "-inform", "engine",
  136. "-in", "ot:$file_abs"])));
  137. ok(run(app(["openssl", "rsa", "-text", "-noout", @pubin,
  138. "-in", "${engine_scheme}ossltest:ot:$file"])));
  139. ok(run(app(["openssl", "rsa", "-text", "-noout", @pubin,
  140. "-in", "${engine_scheme}ossltest:ot:$file_abs"])));
  141. }
  142. }
  143. SKIP:
  144. {
  145. init() or die "init failed";
  146. my $rehash = init_rehash();
  147. foreach my $method (@methods) {
  148. my @storeutl = ( qw(openssl storeutl), @$method );
  149. foreach (@noexist_files) {
  150. my $file = srctop_file($_);
  151. ok(!run(app([@storeutl, "-noout", $file])));
  152. ok(!run(app([@storeutl, "-noout", to_abs_file($file)])));
  153. {
  154. local $ENV{MSYS2_ARG_CONV_EXCL} = "file:";
  155. ok(!run(app([@storeutl, "-noout",
  156. to_abs_file_uri($file)])));
  157. }
  158. }
  159. foreach (@src_files) {
  160. my $file = srctop_file($_);
  161. ok(run(app([@storeutl, "-noout", $file])));
  162. ok(run(app([@storeutl, "-noout", to_abs_file($file)])));
  163. SKIP:
  164. {
  165. skip "file: tests disabled on MingW", 4 if $^O =~ /^msys$/;
  166. ok(run(app([@storeutl, "-noout",
  167. to_abs_file_uri($file)])));
  168. ok(run(app([@storeutl, "-noout",
  169. to_abs_file_uri($file, 0, "")])));
  170. ok(run(app([@storeutl, "-noout",
  171. to_abs_file_uri($file, 0, "localhost")])));
  172. ok(!run(app([@storeutl, "-noout",
  173. to_abs_file_uri($file, 0, "dummy")])));
  174. }
  175. }
  176. foreach (@data_files) {
  177. my $file = data_file($_);
  178. ok(run(app([@storeutl, "-noout", "-passin", "pass:password",
  179. $file])));
  180. ok(run(app([@storeutl, "-noout", "-passin", "pass:password",
  181. to_abs_file($file)])));
  182. }
  183. foreach (@generated_files) {
  184. ok(run(app([@storeutl, "-noout", "-passin", "pass:password",
  185. $_])));
  186. ok(run(app([@storeutl, "-noout", "-passin", "pass:password",
  187. to_abs_file($_)])));
  188. SKIP:
  189. {
  190. skip "file: tests disabled on MingW", 2 if $^O =~ /^msys$/;
  191. ok(run(app([@storeutl, "-noout", "-passin",
  192. "pass:password", to_abs_file_uri($_)])));
  193. ok(!run(app([@storeutl, "-noout", "-passin",
  194. "pass:password", to_file_uri($_)])));
  195. }
  196. }
  197. foreach (values %generated_file_files) {
  198. SKIP:
  199. {
  200. skip "file: tests disabled on MingW", 1 if $^O =~ /^msys$/;
  201. ok(run(app([@storeutl, "-noout", $_])));
  202. }
  203. }
  204. foreach (@noexist_file_files) {
  205. SKIP:
  206. {
  207. skip "file: tests disabled on MingW", 1 if $^O =~ /^msys$/;
  208. ok(!run(app([@storeutl, "-noout", $_])));
  209. }
  210. }
  211. {
  212. my $dir = srctop_dir("test", "certs");
  213. ok(run(app([@storeutl, "-noout", $dir])));
  214. ok(run(app([@storeutl, "-noout", to_abs_file($dir, 1)])));
  215. SKIP:
  216. {
  217. skip "file: tests disabled on MingW", 1 if $^O =~ /^msys$/;
  218. ok(run(app([@storeutl, "-noout",
  219. to_abs_file_uri($dir, 1)])));
  220. }
  221. }
  222. ok(!run(app([@storeutl, '-noout',
  223. '-subject', '/C=AU/ST=QLD/CN=SSLeay\/rsa test cert',
  224. srctop_file('test', 'testx509.pem')])),
  225. "Checking that -subject can't be used with a single file");
  226. ok(run(app([@storeutl, '-certs', '-noout',
  227. srctop_file('test', 'testx509.pem')])),
  228. "Checking that -certs returns 1 object on a certificate file");
  229. ok(run(app([@storeutl, '-certs', '-noout',
  230. srctop_file('test', 'testcrl.pem')])),
  231. "Checking that -certs returns 0 objects on a CRL file");
  232. ok(run(app([@storeutl, '-crls', '-noout',
  233. srctop_file('test', 'testx509.pem')])),
  234. "Checking that -crls returns 0 objects on a certificate file");
  235. ok(run(app([@storeutl, '-crls', '-noout',
  236. srctop_file('test', 'testcrl.pem')])),
  237. "Checking that -crls returns 1 object on a CRL file");
  238. SKIP: {
  239. skip "failed rehash initialisation", 6 unless $rehash;
  240. # subject from testx509.pem:
  241. # '/C=AU/ST=QLD/CN=SSLeay\/rsa test cert'
  242. # issuer from testcrl.pem:
  243. # '/C=US/O=RSA Data Security, Inc./OU=Secure Server Certification Authority'
  244. ok(run(app([@storeutl, '-noout',
  245. '-subject', '/C=AU/ST=QLD/CN=SSLeay\/rsa test cert',
  246. catdir(curdir(), 'rehash')])));
  247. ok(run(app([@storeutl, '-noout',
  248. '-subject',
  249. '/C=US/O=RSA Data Security, Inc./OU=Secure Server Certification Authority',
  250. catdir(curdir(), 'rehash')])));
  251. ok(run(app([@storeutl, '-noout', '-certs',
  252. '-subject', '/C=AU/ST=QLD/CN=SSLeay\/rsa test cert',
  253. catdir(curdir(), 'rehash')])));
  254. ok(run(app([@storeutl, '-noout', '-crls',
  255. '-subject', '/C=AU/ST=QLD/CN=SSLeay\/rsa test cert',
  256. catdir(curdir(), 'rehash')])));
  257. ok(run(app([@storeutl, '-noout', '-certs',
  258. '-subject',
  259. '/C=US/O=RSA Data Security, Inc./OU=Secure Server Certification Authority',
  260. catdir(curdir(), 'rehash')])));
  261. ok(run(app([@storeutl, '-noout', '-crls',
  262. '-subject',
  263. '/C=US/O=RSA Data Security, Inc./OU=Secure Server Certification Authority',
  264. catdir(curdir(), 'rehash')])));
  265. }
  266. }
  267. }
  268. }, create => 1, cleanup => 1;
  269. sub init {
  270. my $cnf = srctop_file('test', 'ca-and-certs.cnf');
  271. my $cakey = srctop_file('test', 'certs', 'ca-key.pem');
  272. my @std_args = qw(-provider default);
  273. push @std_args, qw(-provider legacy)
  274. unless disabled('legacy');
  275. return (
  276. # rsa-key-pkcs1.pem
  277. run(app(["openssl", "pkey", @std_args,
  278. "-in", data_file("rsa-key-2432.pem"),
  279. "-out", "rsa-key-pkcs1.pem"]))
  280. # rsa-key-pkcs1-aes128.pem
  281. && run(app(["openssl", "rsa", @std_args,
  282. "-passout", "pass:password", "-aes128",
  283. "-in", "rsa-key-pkcs1.pem",
  284. "-out", "rsa-key-pkcs1-aes128.pem"]))
  285. # dsa-key-pkcs1.pem
  286. && (!$use_dsa
  287. || run(app(["openssl", "gendsa", @std_args,
  288. "-out", "dsa-key-pkcs1.pem",
  289. data_file("dsaparam.pem")])))
  290. # dsa-key-pkcs1-aes128.pem
  291. && (!$use_dsa
  292. || run(app(["openssl", "dsa", @std_args,
  293. "-passout", "pass:password", "-aes128",
  294. "-in", "dsa-key-pkcs1.pem",
  295. "-out", "dsa-key-pkcs1-aes128.pem"])))
  296. # ec-key-pkcs1.pem (one might think that 'genec' would be practical)
  297. && (!$use_ecc
  298. || run(app(["openssl", "ecparam", @std_args,
  299. "-genkey",
  300. "-name", "prime256v1",
  301. "-out", "ec-key-pkcs1.pem"])))
  302. # ec-key-pkcs1-aes128.pem
  303. && (!$use_ecc
  304. || run(app(["openssl", "ec", @std_args,
  305. "-passout", "pass:password", "-aes128",
  306. "-in", "ec-key-pkcs1.pem",
  307. "-out", "ec-key-pkcs1-aes128.pem"])))
  308. # *-key-pkcs8.pem
  309. && runall(sub {
  310. my $dstfile = shift;
  311. (my $srcfile = $dstfile)
  312. =~ s/-key-pkcs8\.pem$/-key-pkcs1.pem/i;
  313. run(app(["openssl", "pkcs8", @std_args,
  314. "-topk8", "-nocrypt",
  315. "-in", $srcfile, "-out", $dstfile]));
  316. }, grep(/-key-pkcs8\.pem$/, @generated_files))
  317. # *-key-pkcs8-pbes1-sha1-3des.pem
  318. && runall(sub {
  319. my $dstfile = shift;
  320. (my $srcfile = $dstfile)
  321. =~ s/-key-pkcs8-pbes1-sha1-3des\.pem$
  322. /-key-pkcs8.pem/ix;
  323. run(app(["openssl", "pkcs8", @std_args,
  324. "-topk8",
  325. "-passout", "pass:password",
  326. "-v1", "pbeWithSHA1And3-KeyTripleDES-CBC",
  327. "-in", $srcfile, "-out", $dstfile]));
  328. }, grep(/-key-pkcs8-pbes1-sha1-3des\.pem$/, @generated_files))
  329. # *-key-pkcs8-pbes1-md5-des.pem
  330. && runall(sub {
  331. my $dstfile = shift;
  332. (my $srcfile = $dstfile)
  333. =~ s/-key-pkcs8-pbes1-md5-des\.pem$
  334. /-key-pkcs8.pem/ix;
  335. run(app(["openssl", "pkcs8", @std_args,
  336. "-topk8",
  337. "-passout", "pass:password",
  338. "-v1", "pbeWithSHA1And3-KeyTripleDES-CBC",
  339. "-in", $srcfile, "-out", $dstfile]));
  340. }, grep(/-key-pkcs8-pbes1-md5-des\.pem$/, @generated_files))
  341. # *-key-pkcs8-pbes2-sha1.pem
  342. && runall(sub {
  343. my $dstfile = shift;
  344. (my $srcfile = $dstfile)
  345. =~ s/-key-pkcs8-pbes2-sha1\.pem$
  346. /-key-pkcs8.pem/ix;
  347. run(app(["openssl", "pkcs8", @std_args,
  348. "-topk8",
  349. "-passout", "pass:password",
  350. "-v2", "aes256", "-v2prf", "hmacWithSHA1",
  351. "-in", $srcfile, "-out", $dstfile]));
  352. }, grep(/-key-pkcs8-pbes2-sha1\.pem$/, @generated_files))
  353. # *-key-pkcs8-pbes2-sha1.pem
  354. && runall(sub {
  355. my $dstfile = shift;
  356. (my $srcfile = $dstfile)
  357. =~ s/-key-pkcs8-pbes2-sha256\.pem$
  358. /-key-pkcs8.pem/ix;
  359. run(app(["openssl", "pkcs8", @std_args,
  360. "-topk8",
  361. "-passout", "pass:password",
  362. "-v2", "aes256", "-v2prf", "hmacWithSHA256",
  363. "-in", $srcfile, "-out", $dstfile]));
  364. }, grep(/-key-pkcs8-pbes2-sha256\.pem$/, @generated_files))
  365. # *-cert.pem (intermediary for the .p12 inits)
  366. && run(app(["openssl", "req", "-x509", @std_args,
  367. "-config", $cnf, "-noenc",
  368. "-key", $cakey, "-out", "cacert.pem"]))
  369. && runall(sub {
  370. my $srckey = shift;
  371. (my $dstfile = $srckey) =~ s|-key-pkcs8\.|-cert.|;
  372. (my $csr = $dstfile) =~ s|\.pem|.csr|;
  373. (run(app(["openssl", "req", "-new", @std_args,
  374. "-config", $cnf, "-section", "userreq",
  375. "-key", $srckey, "-out", $csr]))
  376. &&
  377. run(app(["openssl", "x509", @std_args,
  378. "-days", "3650",
  379. "-CA", "cacert.pem",
  380. "-CAkey", $cakey,
  381. "-set_serial", time(), "-req",
  382. "-in", $csr, "-out", $dstfile])));
  383. }, grep(/-key-pkcs8\.pem$/, @generated_files))
  384. # *.p12
  385. && runall(sub {
  386. my $dstfile = shift;
  387. my ($type, $certpbe_index, $keypbe_index,
  388. $macalg_index) =
  389. $dstfile =~ m{^(.*)-key-(?|
  390. # cert and key PBE are same
  391. () #
  392. ([^-]*-[^-]*)- # key & cert PBE
  393. ([^-]*) # MACalg
  394. |
  395. # cert and key PBE are not same
  396. ([^-]*-[^-]*)- # cert PBE
  397. ([^-]*-[^-]*)- # key PBE
  398. ([^-]*) # MACalg
  399. )\.}x;
  400. if (!$certpbe_index) {
  401. $certpbe_index = $keypbe_index;
  402. }
  403. my $srckey = "$type-key-pkcs8.pem";
  404. my $srccert = "$type-cert.pem";
  405. my %pbes =
  406. (
  407. "sha1-3des" => "pbeWithSHA1And3-KeyTripleDES-CBC",
  408. "md5-des" => "pbeWithMD5AndDES-CBC",
  409. "aes256-cbc" => "AES-256-CBC",
  410. );
  411. my %macalgs =
  412. (
  413. "sha1" => "SHA1",
  414. "sha256" => "SHA256",
  415. );
  416. my $certpbe = $pbes{$certpbe_index};
  417. my $keypbe = $pbes{$keypbe_index};
  418. my $macalg = $macalgs{$macalg_index};
  419. if (!defined($certpbe) || !defined($keypbe)
  420. || !defined($macalg)) {
  421. print STDERR "Cert PBE for $certpbe_index not defined\n"
  422. unless defined $certpbe;
  423. print STDERR "Key PBE for $keypbe_index not defined\n"
  424. unless defined $keypbe;
  425. print STDERR "MACALG for $macalg_index not defined\n"
  426. unless defined $macalg;
  427. print STDERR "(destination file was $dstfile)\n";
  428. return 0;
  429. }
  430. run(app(["openssl", "pkcs12", @std_args,
  431. "-inkey", $srckey,
  432. "-in", $srccert, "-passout", "pass:password",
  433. "-chain", "-CAfile", "cacert.pem",
  434. "-export", "-macalg", $macalg,
  435. "-certpbe", $certpbe, "-keypbe", $keypbe,
  436. "-out", $dstfile]));
  437. }, grep(/\.p12/, @generated_files))
  438. # *.der (the end all init)
  439. && runall(sub {
  440. my $dstfile = shift;
  441. (my $srcfile = $dstfile) =~ s/\.der$/.pem/i;
  442. if (! -f $srcfile) {
  443. $srcfile = srctop_file("test", $srcfile);
  444. }
  445. my $infh;
  446. unless (open $infh, $srcfile) {
  447. return 0;
  448. }
  449. my $l;
  450. while (($l = <$infh>) !~ /^-----BEGIN\s/
  451. || $l =~ /^-----BEGIN.*PARAMETERS-----/) {
  452. }
  453. my $b64 = "";
  454. while (($l = <$infh>) !~ /^-----END\s/) {
  455. $l =~ s|\R$||;
  456. $b64 .= $l unless $l =~ /:/;
  457. }
  458. close $infh;
  459. my $der = decode_base64($b64);
  460. unless (length($b64) / 4 * 3 - length($der) < 3) {
  461. print STDERR "Length error, ",length($b64),
  462. " bytes of base64 became ",length($der),
  463. " bytes of der? ($srcfile => $dstfile)\n";
  464. return 0;
  465. }
  466. my $outfh;
  467. unless (open $outfh, ">:raw", $dstfile) {
  468. return 0;
  469. }
  470. print $outfh $der;
  471. close $outfh;
  472. return 1;
  473. }, grep(/\.der$/, @generated_files))
  474. && runall(sub {
  475. my $srcfile = shift;
  476. my $dstfile = $generated_file_files{$srcfile};
  477. unless (copy srctop_file($srcfile), $dstfile) {
  478. warn "$!\n";
  479. return 0;
  480. }
  481. return 1;
  482. }, keys %generated_file_files)
  483. );
  484. }
  485. sub init_rehash {
  486. return (
  487. mkdir(catdir(curdir(), 'rehash'))
  488. && copy(srctop_file('test', 'testx509.pem'),
  489. catdir(curdir(), 'rehash'))
  490. && copy(srctop_file('test', 'testcrl.pem'),
  491. catdir(curdir(), 'rehash'))
  492. && run(app(['openssl', 'rehash', catdir(curdir(), 'rehash')]))
  493. );
  494. }
  495. sub runall {
  496. my ($function, @items) = @_;
  497. foreach (@items) {
  498. return 0 unless $function->($_);
  499. }
  500. return 1;
  501. }
  502. # According to RFC8089, a relative file: path is invalid. We still produce
  503. # them for testing purposes.
  504. sub to_file_uri {
  505. my ($file, $isdir, $authority) = @_;
  506. my $vol;
  507. my $dir;
  508. die "to_file_uri: No file given\n" if !defined($file) || $file eq '';
  509. ($vol, $dir, $file) = File::Spec->splitpath($file, $isdir // 0);
  510. # Make sure we have a Unix style directory.
  511. $dir = join('/', File::Spec->splitdir($dir));
  512. # Canonicalise it (note: it seems to be only needed on Unix)
  513. while (1) {
  514. my $newdir = $dir;
  515. $newdir =~ s|/[^/]*[^/\.]+[^/]*/\.\./|/|g;
  516. last if $newdir eq $dir;
  517. $dir = $newdir;
  518. }
  519. # Take care of the corner cases the loop can't handle, and that $dir
  520. # ends with a / unless it's empty
  521. $dir =~ s|/[^/]*[^/\.]+[^/]*/\.\.$|/|;
  522. $dir =~ s|^[^/]*[^/\.]+[^/]*/\.\./|/|;
  523. $dir =~ s|^[^/]*[^/\.]+[^/]*/\.\.$||;
  524. if ($isdir // 0) {
  525. $dir =~ s|/$|| if $dir ne '/';
  526. } else {
  527. $dir .= '/' if $dir ne '' && $dir !~ m|/$|;
  528. }
  529. # If the file system has separate volumes (at present, Windows and VMS)
  530. # we need to handle them. In URIs, they are invariably the first
  531. # component of the path, which is always absolute.
  532. # On VMS, user:[foo.bar] translates to /user/foo/bar
  533. # On Windows, c:\Users\Foo translates to /c:/Users/Foo
  534. if ($vol ne '') {
  535. $vol =~ s|:||g if ($^O eq "VMS");
  536. $dir = '/' . $dir if $dir ne '' && $dir !~ m|^/|;
  537. $dir = '/' . $vol . $dir;
  538. }
  539. $file = $dir . $file;
  540. return "file://$authority$file" if defined $authority;
  541. return "file:$file";
  542. }
  543. sub to_abs_file {
  544. my ($file) = @_;
  545. return File::Spec->rel2abs($file);
  546. }
  547. sub to_abs_file_uri {
  548. my ($file, $isdir, $authority) = @_;
  549. die "to_abs_file_uri: No file given\n" if !defined($file) || $file eq '';
  550. return to_file_uri(to_abs_file($file), $isdir, $authority);
  551. }