03-test_fipsinstall.t 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #! /usr/bin/env perl
  2. # Copyright 2019-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 strict;
  9. use warnings;
  10. use File::Spec;
  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 => 24;
  23. my $infile = bldtop_file('providers', platform->dso('fips'));
  24. my $fipskey = $ENV{FIPSKEY} // '00';
  25. # Read in a text $infile and replace the regular expression in $srch with the
  26. # value in $repl and output to a new file $outfile.
  27. sub replace_line_file_internal {
  28. my ($infile, $srch, $repl, $outfile) = @_;
  29. my $msg;
  30. open(my $in, "<", $infile) or return 0;
  31. read($in, $msg, 1024);
  32. close $in;
  33. $msg =~ s/$srch/$repl/;
  34. open(my $fh, ">", $outfile) or return 0;
  35. print $fh $msg;
  36. close $fh;
  37. return 1;
  38. }
  39. # Read in the text input file 'fips.cnf'
  40. # and replace a single Key = Value line with a new value in $value.
  41. # OR remove the Key = Value line if the passed in $value is empty.
  42. # and then output a new file $outfile.
  43. # $key is the Key to find
  44. sub replace_line_file {
  45. my ($key, $value, $outfile) = @_;
  46. my $srch = qr/$key\s*=\s*\S*\n/;
  47. my $rep;
  48. if ($value eq "") {
  49. $rep = "";
  50. } else {
  51. $rep = "$key = $value\n";
  52. }
  53. return replace_line_file_internal('fips.cnf', $srch, $rep, $outfile);
  54. }
  55. # Read in the text input file 'test/fips.cnf'
  56. # and replace the .cnf file used in
  57. # .include fipsmodule.cnf with a new value in $value.
  58. # and then output a new file $outfile.
  59. # $key is the Key to find
  60. sub replace_parent_line_file {
  61. my ($value, $outfile) = @_;
  62. my $srch = qr/fipsmodule.cnf/;
  63. my $rep = "$value";
  64. return replace_line_file_internal(srctop_file("test", 'fips.cnf'),
  65. $srch, $rep, $outfile);
  66. }
  67. # fail if no module name
  68. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module',
  69. '-provider_name', 'fips',
  70. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  71. '-section_name', 'fips_sect'])),
  72. "fipsinstall fail");
  73. # fail to verify if the configuration file is missing
  74. ok(!run(app(['openssl', 'fipsinstall', '-in', 'dummy.tmp', '-module', $infile,
  75. '-provider_name', 'fips', '-mac_name', 'HMAC',
  76. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  77. '-section_name', 'fips_sect', '-verify'])),
  78. "fipsinstall verify fail");
  79. # output a fips.cnf file containing mac data
  80. ok(run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
  81. '-provider_name', 'fips', '-mac_name', 'HMAC',
  82. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  83. '-section_name', 'fips_sect'])),
  84. "fipsinstall");
  85. # verify the fips.cnf file
  86. ok(run(app(['openssl', 'fipsinstall', '-in', 'fips.cnf', '-module', $infile,
  87. '-provider_name', 'fips', '-mac_name', 'HMAC',
  88. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  89. '-section_name', 'fips_sect', '-verify'])),
  90. "fipsinstall verify");
  91. ok(replace_line_file('module-mac', '', 'fips_no_module_mac.cnf')
  92. && !run(app(['openssl', 'fipsinstall',
  93. '-in', 'fips_no_module_mac.cnf',
  94. '-module', $infile,
  95. '-provider_name', 'fips', '-mac_name', 'HMAC',
  96. '-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
  97. '-section_name', 'fips_sect', '-verify'])),
  98. "fipsinstall verify fail no module mac");
  99. ok(replace_line_file('install-mac', '', 'fips_no_install_mac.cnf')
  100. && !run(app(['openssl', 'fipsinstall',
  101. '-in', 'fips_no_install_mac.cnf',
  102. '-module', $infile,
  103. '-provider_name', 'fips', '-mac_name', 'HMAC',
  104. '-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
  105. '-section_name', 'fips_sect', '-verify'])),
  106. "fipsinstall verify fail no install indicator mac");
  107. ok(replace_line_file('module-mac', '00:00:00:00:00:00',
  108. 'fips_bad_module_mac.cnf')
  109. && !run(app(['openssl', 'fipsinstall',
  110. '-in', 'fips_bad_module_mac.cnf',
  111. '-module', $infile,
  112. '-provider_name', 'fips', '-mac_name', 'HMAC',
  113. '-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
  114. '-section_name', 'fips_sect', '-verify'])),
  115. "fipsinstall verify fail if invalid module integrity value");
  116. ok(replace_line_file('install-mac', '00:00:00:00:00:00',
  117. 'fips_bad_install_mac.cnf')
  118. && !run(app(['openssl', 'fipsinstall',
  119. '-in', 'fips_bad_install_mac.cnf',
  120. '-module', $infile,
  121. '-provider_name', 'fips', '-mac_name', 'HMAC',
  122. '-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
  123. '-section_name', 'fips_sect', '-verify'])),
  124. "fipsinstall verify fail if invalid install indicator integrity value");
  125. ok(replace_line_file('install-status', 'INCORRECT_STATUS_STRING',
  126. 'fips_bad_indicator.cnf')
  127. && !run(app(['openssl', 'fipsinstall',
  128. '-in', 'fips_bad_indicator.cnf',
  129. '-module', $infile,
  130. '-provider_name', 'fips', '-mac_name', 'HMAC',
  131. '-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
  132. '-section_name', 'fips_sect', '-verify'])),
  133. "fipsinstall verify fail if invalid install indicator status");
  134. # fail to verify the fips.cnf file if a different key is used
  135. ok(!run(app(['openssl', 'fipsinstall', '-in', 'fips.cnf', '-module', $infile,
  136. '-provider_name', 'fips', '-mac_name', 'HMAC',
  137. '-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
  138. '-section_name', 'fips_sect', '-verify'])),
  139. "fipsinstall verify fail bad key");
  140. # fail to verify the fips.cnf file if a different mac digest is used
  141. ok(!run(app(['openssl', 'fipsinstall', '-in', 'fips.cnf', '-module', $infile,
  142. '-provider_name', 'fips', '-mac_name', 'HMAC',
  143. '-macopt', 'digest:SHA512', '-macopt', "hexkey:$fipskey",
  144. '-section_name', 'fips_sect', '-verify'])),
  145. "fipsinstall verify fail incorrect digest");
  146. # corrupt the module hmac
  147. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
  148. '-provider_name', 'fips', '-mac_name', 'HMAC',
  149. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  150. '-section_name', 'fips_sect', '-corrupt_desc', 'HMAC'])),
  151. "fipsinstall fails when the module integrity is corrupted");
  152. # corrupt the first digest
  153. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips_fail.cnf', '-module', $infile,
  154. '-provider_name', 'fips', '-mac_name', 'HMAC',
  155. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  156. '-section_name', 'fips_sect', '-corrupt_desc', 'SHA1'])),
  157. "fipsinstall fails when the digest result is corrupted");
  158. # corrupt another digest
  159. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips_fail.cnf', '-module', $infile,
  160. '-provider_name', 'fips', '-mac_name', 'HMAC',
  161. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  162. '-section_name', 'fips_sect', '-corrupt_desc', 'SHA3'])),
  163. "fipsinstall fails when the digest result is corrupted");
  164. # corrupt DRBG
  165. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips_fail.cnf', '-module', $infile,
  166. '-provider_name', 'fips', '-mac_name', 'HMAC',
  167. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  168. '-section_name', 'fips_sect', '-corrupt_desc', 'CTR'])),
  169. "fipsinstall fails when the DRBG CTR result is corrupted");
  170. # corrupt a KAS test
  171. SKIP: {
  172. skip "Skipping KAS DH corruption test because of no dh in this build", 1
  173. if disabled("dh");
  174. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
  175. '-provider_name', 'fips', '-mac_name', 'HMAC',
  176. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  177. '-section_name', 'fips_sect',
  178. '-corrupt_desc', 'DH',
  179. '-corrupt_type', 'KAT_KA'])),
  180. "fipsinstall fails when the kas result is corrupted");
  181. }
  182. # corrupt a Signature test
  183. SKIP: {
  184. skip "Skipping Signature DSA corruption test because of no dsa in this build", 1
  185. if disabled("dsa");
  186. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
  187. '-provider_name', 'fips', '-mac_name', 'HMAC',
  188. '-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
  189. '-section_name', 'fips_sect',
  190. '-corrupt_desc', 'DSA',
  191. '-corrupt_type', 'KAT_Signature'])),
  192. "fipsinstall fails when the signature result is corrupted");
  193. }
  194. # corrupt an Asymmetric cipher test
  195. SKIP: {
  196. skip "Skipping Asymmetric RSA corruption test because of no rsa in this build", 1
  197. if disabled("rsa");
  198. ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
  199. '-corrupt_desc', 'RSA_Encrypt',
  200. '-corrupt_type', 'KAT_AsymmetricCipher'])),
  201. "fipsinstall fails when the asymmetric cipher result is corrupted");
  202. }
  203. $ENV{OPENSSL_CONF_INCLUDE} = ".";
  204. ok(replace_parent_line_file('fips.cnf', 'fips_parent.cnf')
  205. && run(app(['openssl', 'fipsinstall', '-config', 'fips_parent.cnf'])),
  206. "verify fips provider loads from a configuration file");
  207. ok(replace_parent_line_file('fips_no_module_mac.cnf',
  208. 'fips_parent_no_module_mac.cnf')
  209. && !run(app(['openssl', 'fipsinstall',
  210. '-config', 'fips_parent_no_module_mac.cnf'])),
  211. "verify load config fail no module mac");
  212. ok(replace_parent_line_file('fips_no_install_mac.cnf',
  213. 'fips_parent_no_install_mac.cnf')
  214. && !run(app(['openssl', 'fipsinstall',
  215. '-config', 'fips_parent_no_install_mac.cnf'])),
  216. "verify load config fail no install mac");
  217. ok(replace_parent_line_file('fips_bad_indicator.cnf',
  218. 'fips_parent_bad_indicator.cnf')
  219. && !run(app(['openssl', 'fipsinstall',
  220. '-config', 'fips_parent_bad_indicator.cnf'])),
  221. "verify load config fail bad indicator");
  222. ok(replace_parent_line_file('fips_bad_install_mac.cnf',
  223. 'fips_parent_bad_install_mac.cnf')
  224. && !run(app(['openssl', 'fipsinstall',
  225. '-config', 'fips_parent_bad_install_mac.cnf'])),
  226. "verify load config fail bad install mac");
  227. ok(replace_parent_line_file('fips_bad_module_mac.cnf',
  228. 'fips_parent_bad_module_mac.cnf')
  229. && !run(app(['openssl', 'fipsinstall',
  230. '-config', 'fips_parent_bad_module_mac.cnf'])),
  231. "verify load config fail bad module mac");
  232. delete $ENV{OPENSSL_CONF_INCLUDE};