90-test_store.t 26 KB

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