03-test_fipsinstall.t 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #! /usr/bin/env perl
  2. # Copyright 2019-2022 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 strict;
  9. use warnings;
  10. use File::Spec::Functions qw(:DEFAULT abs2rel);
  11. use File::Copy;
  12. use OpenSSL::Glob;
  13. use OpenSSL::Test qw/:DEFAULT srctop_dir srctop_file bldtop_dir bldtop_file/;
  14. use OpenSSL::Test::Utils;
  15. BEGIN {
  16. setup("test_fipsinstall");
  17. }
  18. use lib srctop_dir('Configurations');
  19. use lib bldtop_dir('.');
  20. use platform;
  21. plan skip_all => "Test only supported in a fips build" if disabled("fips");
  22. plan tests => 31;
  23. my $infile = bldtop_file('providers', platform->dso('fips'));
  24. my $fipskey = $ENV{FIPSKEY} // config('FIPSKEY') // '00';
  25. my $provconf = srctop_file("test", "fips-and-base.cnf");
  26. # Read in a text $infile and replace the regular expression in $srch with the
  27. # value in $repl and output to a new file $outfile.
  28. sub replace_line_file_internal {
  29. my ($infile, $srch, $repl, $outfile) = @_;
  30. my $msg;
  31. open(my $in, "<", $infile) or return 0;
  32. read($in, $msg, 1024);
  33. close $in;
  34. $msg =~ s/$srch/$repl/;
  35. open(my $fh, ">", $outfile) or return 0;
  36. print $fh $msg;
  37. close $fh;
  38. return 1;
  39. }
  40. # Read in the text input file 'fips.cnf'
  41. # and replace a single Key = Value line with a new value in $value.
  42. # OR remove the Key = Value line if the passed in $value is empty.
  43. # and then output a new file $outfile.
  44. # $key is the Key to find
  45. sub replace_line_file {
  46. my ($key, $value, $outfile) = @_;
  47. my $srch = qr/$key\s*=\s*\S*\n/;
  48. my $rep;
  49. if ($value eq "") {
  50. $rep = "";
  51. } else {
  52. $rep = "$key = $value\n";
  53. }
  54. return replace_line_file_internal('fips.cnf', $srch, $rep, $outfile);
  55. }
  56. # Read in the text input file 'test/fips.cnf'
  57. # and replace the .cnf file used in
  58. # .include fipsmodule.cnf with a new value in $value.
  59. # and then output a new file $outfile.
  60. # $key is the Key to find
  61. sub replace_parent_line_file {
  62. my ($value, $outfile) = @_;
  63. my $srch = qr/fipsmodule.cnf/;
  64. my $rep = "$value";
  65. return replace_line_file_internal(srctop_file("test", 'fips.cnf'),
  66. $srch, $rep, $outfile);
  67. }
  68. # fail if no module name
  69. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module',
  70. '-provider_name', 'fips',
  71. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  72. '-section_name', 'fips_sect'])),
  73. "fipsinstall fail");
  74. # fail to verify if the configuration file is missing
  75. ok(!run(app(['openssl', 'fipsinstall', '-in', 'dummy.tmp', '-module', $infile,
  76. '-provider_name', 'fips', '-mac_name', 'HMAC',
  77. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  78. '-section_name', 'fips_sect', '-verify'])),
  79. "fipsinstall verify fail");
  80. # output a fips.cnf file containing mac data
  81. ok(run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
  82. '-provider_name', 'fips', '-mac_name', 'HMAC',
  83. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  84. '-section_name', 'fips_sect'])),
  85. "fipsinstall");
  86. # verify the fips.cnf file
  87. ok(run(app(['openssl', 'fipsinstall', '-in', 'fips.cnf', '-module', $infile,
  88. '-provider_name', 'fips', '-mac_name', 'HMAC',
  89. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  90. '-section_name', 'fips_sect', '-verify'])),
  91. "fipsinstall verify");
  92. ok(replace_line_file('module-mac', '', 'fips_no_module_mac.cnf')
  93. && !run(app(['openssl', 'fipsinstall',
  94. '-in', 'fips_no_module_mac.cnf',
  95. '-module', $infile,
  96. '-provider_name', 'fips', '-mac_name', 'HMAC',
  97. '-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
  98. '-section_name', 'fips_sect', '-verify'])),
  99. "fipsinstall verify fail no module mac");
  100. ok(replace_line_file('install-mac', '', 'fips_no_install_mac.cnf')
  101. && !run(app(['openssl', 'fipsinstall',
  102. '-in', 'fips_no_install_mac.cnf',
  103. '-module', $infile,
  104. '-provider_name', 'fips', '-mac_name', 'HMAC',
  105. '-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
  106. '-section_name', 'fips_sect', '-verify'])),
  107. "fipsinstall verify fail no install indicator mac");
  108. ok(replace_line_file('module-mac', '00:00:00:00:00:00',
  109. 'fips_bad_module_mac.cnf')
  110. && !run(app(['openssl', 'fipsinstall',
  111. '-in', 'fips_bad_module_mac.cnf',
  112. '-module', $infile,
  113. '-provider_name', 'fips', '-mac_name', 'HMAC',
  114. '-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
  115. '-section_name', 'fips_sect', '-verify'])),
  116. "fipsinstall verify fail if invalid module integrity value");
  117. ok(replace_line_file('install-mac', '00:00:00:00:00:00',
  118. 'fips_bad_install_mac.cnf')
  119. && !run(app(['openssl', 'fipsinstall',
  120. '-in', 'fips_bad_install_mac.cnf',
  121. '-module', $infile,
  122. '-provider_name', 'fips', '-mac_name', 'HMAC',
  123. '-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
  124. '-section_name', 'fips_sect', '-verify'])),
  125. "fipsinstall verify fail if invalid install indicator integrity value");
  126. ok(replace_line_file('install-status', 'INCORRECT_STATUS_STRING',
  127. 'fips_bad_indicator.cnf')
  128. && !run(app(['openssl', 'fipsinstall',
  129. '-in', 'fips_bad_indicator.cnf',
  130. '-module', $infile,
  131. '-provider_name', 'fips', '-mac_name', 'HMAC',
  132. '-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
  133. '-section_name', 'fips_sect', '-verify'])),
  134. "fipsinstall verify fail if invalid install indicator status");
  135. # fail to verify the fips.cnf file if a different key is used
  136. ok(!run(app(['openssl', 'fipsinstall', '-in', 'fips.cnf', '-module', $infile,
  137. '-provider_name', 'fips', '-mac_name', 'HMAC',
  138. '-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
  139. '-section_name', 'fips_sect', '-verify'])),
  140. "fipsinstall verify fail bad key");
  141. # fail to verify the fips.cnf file if a different mac digest is used
  142. ok(!run(app(['openssl', 'fipsinstall', '-in', 'fips.cnf', '-module', $infile,
  143. '-provider_name', 'fips', '-mac_name', 'HMAC',
  144. '-macopt', 'digest:SHA512', '-macopt', "hexkey:$fipskey",
  145. '-section_name', 'fips_sect', '-verify'])),
  146. "fipsinstall verify fail incorrect digest");
  147. # corrupt the module hmac
  148. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
  149. '-provider_name', 'fips', '-mac_name', 'HMAC',
  150. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  151. '-section_name', 'fips_sect', '-corrupt_desc', 'HMAC'])),
  152. "fipsinstall fails when the module integrity is corrupted");
  153. # corrupt the first digest
  154. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips_fail.cnf', '-module', $infile,
  155. '-provider_name', 'fips', '-mac_name', 'HMAC',
  156. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  157. '-section_name', 'fips_sect', '-corrupt_desc', 'SHA1'])),
  158. "fipsinstall fails when the digest result is corrupted");
  159. # corrupt another digest
  160. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips_fail.cnf', '-module', $infile,
  161. '-provider_name', 'fips', '-mac_name', 'HMAC',
  162. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  163. '-section_name', 'fips_sect', '-corrupt_desc', 'SHA3'])),
  164. "fipsinstall fails when the digest result is corrupted");
  165. # corrupt cipher encrypt test
  166. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips_fail.cnf', '-module', $infile,
  167. '-provider_name', 'fips', '-mac_name', 'HMAC',
  168. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  169. '-section_name', 'fips_sect', '-corrupt_desc', 'AES_GCM'])),
  170. "fipsinstall fails when the AES_GCM result is corrupted");
  171. # corrupt cipher decrypt test
  172. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips_fail.cnf', '-module', $infile,
  173. '-provider_name', 'fips', '-mac_name', 'HMAC',
  174. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  175. '-section_name', 'fips_sect', '-corrupt_desc', 'AES_ECB_Decrypt'])),
  176. "fipsinstall fails when the AES_ECB result is corrupted");
  177. # corrupt DRBG
  178. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips_fail.cnf', '-module', $infile,
  179. '-provider_name', 'fips', '-mac_name', 'HMAC',
  180. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  181. '-section_name', 'fips_sect', '-corrupt_desc', 'CTR'])),
  182. "fipsinstall fails when the DRBG CTR result is corrupted");
  183. # corrupt a KAS test
  184. SKIP: {
  185. skip "Skipping KAS DH corruption test because of no dh in this build", 1
  186. if disabled("dh");
  187. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
  188. '-provider_name', 'fips', '-mac_name', 'HMAC',
  189. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  190. '-section_name', 'fips_sect',
  191. '-corrupt_desc', 'DH',
  192. '-corrupt_type', 'KAT_KA'])),
  193. "fipsinstall fails when the kas result is corrupted");
  194. }
  195. # corrupt a Signature test - 140-3 requires a known answer test
  196. SKIP: {
  197. skip "Skipping Signature DSA corruption test because of no dsa in this build", 1
  198. if disabled("dsa");
  199. run(test(["fips_version_test", "-config", $provconf, ">=3.1.0"]),
  200. capture => 1, statusvar => \my $exit);
  201. skip "FIPS provider version is too old for KAT DSA signature test", 1
  202. if !$exit;
  203. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
  204. '-provider_name', 'fips', '-mac_name', 'HMAC',
  205. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  206. '-section_name', 'fips_sect', '-self_test_oninstall',
  207. '-corrupt_desc', 'DSA',
  208. '-corrupt_type', 'KAT_Signature'])),
  209. "fipsinstall fails when the signature result is corrupted");
  210. }
  211. # corrupt a Signature test - 140-2 allows a pairwise consistency test
  212. SKIP: {
  213. skip "Skipping Signature DSA corruption test because of no dsa in this build", 1
  214. if disabled("dsa");
  215. run(test(["fips_version_test", "-config", $provconf, "<3.1.0"]),
  216. capture => 1, statusvar => \my $exit);
  217. skip "FIPS provider version is too new for PCT DSA signature test", 1
  218. if !$exit;
  219. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
  220. '-provider_name', 'fips', '-mac_name', 'HMAC',
  221. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  222. '-section_name', 'fips_sect',
  223. '-corrupt_desc', 'DSA',
  224. '-corrupt_type', 'PCT_Signature'])),
  225. "fipsinstall fails when the signature result is corrupted");
  226. }
  227. # corrupt an Asymmetric cipher test
  228. SKIP: {
  229. skip "Skipping Asymmetric RSA corruption test because of no rsa in this build", 1
  230. if disabled("rsa");
  231. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
  232. '-corrupt_desc', 'RSA_Encrypt',
  233. '-corrupt_type', 'KAT_AsymmetricCipher'])),
  234. "fipsinstall fails when the asymmetric cipher result is corrupted");
  235. }
  236. # 'local' ensures that this change is only done in this file.
  237. local $ENV{OPENSSL_CONF_INCLUDE} = abs2rel(curdir());
  238. ok(replace_parent_line_file('fips.cnf', 'fips_parent.cnf')
  239. && run(app(['openssl', 'fipsinstall', '-config', 'fips_parent.cnf'])),
  240. "verify fips provider loads from a configuration file");
  241. ok(replace_parent_line_file('fips_no_module_mac.cnf',
  242. 'fips_parent_no_module_mac.cnf')
  243. && !run(app(['openssl', 'fipsinstall',
  244. '-config', 'fips_parent_no_module_mac.cnf'])),
  245. "verify load config fail no module mac");
  246. SKIP: {
  247. run(test(["fips_version_test", "-config", $provconf, "<3.1.0"]),
  248. capture => 1, statusvar => \my $exit);
  249. skip "FIPS provider version doesn't support self test indicator", 3
  250. if !$exit;
  251. ok(replace_parent_line_file('fips_no_install_mac.cnf',
  252. 'fips_parent_no_install_mac.cnf')
  253. && !run(app(['openssl', 'fipsinstall',
  254. '-config', 'fips_parent_no_install_mac.cnf'])),
  255. "verify load config fail no install mac");
  256. ok(replace_parent_line_file('fips_bad_indicator.cnf',
  257. 'fips_parent_bad_indicator.cnf')
  258. && !run(app(['openssl', 'fipsinstall',
  259. '-config', 'fips_parent_bad_indicator.cnf'])),
  260. "verify load config fail bad indicator");
  261. ok(replace_parent_line_file('fips_bad_install_mac.cnf',
  262. 'fips_parent_bad_install_mac.cnf')
  263. && !run(app(['openssl', 'fipsinstall',
  264. '-config', 'fips_parent_bad_install_mac.cnf'])),
  265. "verify load config fail bad install mac");
  266. }
  267. ok(replace_parent_line_file('fips_bad_module_mac.cnf',
  268. 'fips_parent_bad_module_mac.cnf')
  269. && !run(app(['openssl', 'fipsinstall',
  270. '-config', 'fips_parent_bad_module_mac.cnf'])),
  271. "verify load config fail bad module mac");
  272. SKIP: {
  273. run(test(["fips_version_test", "-config", $provconf, "<3.1.0"]),
  274. capture => 1, statusvar => \my $exit);
  275. skip "FIPS provider version doesn't support self test indicator", 3
  276. if !$exit;
  277. my $stconf = "fipsmodule_selftest.cnf";
  278. ok(run(app(['openssl', 'fipsinstall', '-out', $stconf,
  279. '-module', $infile, '-self_test_onload'])),
  280. "fipsinstall config saved without self test indicator");
  281. ok(!run(app(['openssl', 'fipsinstall', '-in', $stconf,
  282. '-module', $infile, '-verify'])),
  283. "fipsinstall config verify fails without self test indicator");
  284. ok(run(app(['openssl', 'fipsinstall', '-in', $stconf,
  285. '-module', $infile, '-self_test_onload', '-verify'])),
  286. "fipsinstall config verify passes when self test indicator is not present");
  287. }
  288. SKIP: {
  289. run(test(["fips_version_test", "-config", $provconf, ">=3.1.0"]),
  290. capture => 1, statusvar => \my $exit);
  291. skip "FIPS provider version can run self tests on install", 1
  292. if !$exit;
  293. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
  294. '-provider_name', 'fips', '-mac_name', 'HMAC',
  295. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  296. '-section_name', 'fips_sect', '-self_test_oninstall'])),
  297. "fipsinstall fails when attempting to run self tests on install");
  298. }