90-test_store.t 22 KB

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