2019-08-24 16:56:34 +08:00
|
|
|
#! /usr/bin/env perl
|
2021-03-11 21:27:36 +08:00
|
|
|
# Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2019-08-24 16:56:34 +08:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
# this file except in compliance with the License. You can obtain a copy
|
|
|
|
# in the file LICENSE in the source distribution or at
|
|
|
|
# https://www.openssl.org/source/license.html
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2020-09-29 16:26:19 +08:00
|
|
|
use File::Spec::Functions qw(:DEFAULT abs2rel);
|
2019-08-24 16:56:34 +08:00
|
|
|
use File::Copy;
|
|
|
|
use OpenSSL::Glob;
|
2020-07-21 14:30:02 +08:00
|
|
|
use OpenSSL::Test qw/:DEFAULT srctop_dir srctop_file bldtop_dir bldtop_file/;
|
2019-08-24 16:56:34 +08:00
|
|
|
use OpenSSL::Test::Utils;
|
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
setup("test_fipsinstall");
|
|
|
|
}
|
|
|
|
use lib srctop_dir('Configurations');
|
|
|
|
use lib bldtop_dir('.');
|
|
|
|
use platform;
|
|
|
|
|
|
|
|
plan skip_all => "Test only supported in a fips build" if disabled("fips");
|
|
|
|
|
2021-05-05 08:36:41 +08:00
|
|
|
plan tests => 29;
|
2019-08-24 16:56:34 +08:00
|
|
|
|
|
|
|
my $infile = bldtop_file('providers', platform->dso('fips'));
|
2020-06-29 10:20:41 +08:00
|
|
|
my $fipskey = $ENV{FIPSKEY} // '00';
|
2019-08-24 16:56:34 +08:00
|
|
|
|
2020-07-21 14:30:02 +08:00
|
|
|
# Read in a text $infile and replace the regular expression in $srch with the
|
|
|
|
# value in $repl and output to a new file $outfile.
|
|
|
|
sub replace_line_file_internal {
|
|
|
|
|
|
|
|
my ($infile, $srch, $repl, $outfile) = @_;
|
|
|
|
my $msg;
|
|
|
|
|
|
|
|
open(my $in, "<", $infile) or return 0;
|
|
|
|
read($in, $msg, 1024);
|
|
|
|
close $in;
|
|
|
|
|
|
|
|
$msg =~ s/$srch/$repl/;
|
|
|
|
|
|
|
|
open(my $fh, ">", $outfile) or return 0;
|
|
|
|
print $fh $msg;
|
|
|
|
close $fh;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Read in the text input file 'fips.cnf'
|
|
|
|
# and replace a single Key = Value line with a new value in $value.
|
|
|
|
# OR remove the Key = Value line if the passed in $value is empty.
|
|
|
|
# and then output a new file $outfile.
|
|
|
|
# $key is the Key to find
|
|
|
|
sub replace_line_file {
|
|
|
|
my ($key, $value, $outfile) = @_;
|
|
|
|
|
|
|
|
my $srch = qr/$key\s*=\s*\S*\n/;
|
|
|
|
my $rep;
|
|
|
|
if ($value eq "") {
|
|
|
|
$rep = "";
|
|
|
|
} else {
|
|
|
|
$rep = "$key = $value\n";
|
|
|
|
}
|
|
|
|
return replace_line_file_internal('fips.cnf', $srch, $rep, $outfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Read in the text input file 'test/fips.cnf'
|
|
|
|
# and replace the .cnf file used in
|
|
|
|
# .include fipsmodule.cnf with a new value in $value.
|
|
|
|
# and then output a new file $outfile.
|
|
|
|
# $key is the Key to find
|
|
|
|
sub replace_parent_line_file {
|
|
|
|
my ($value, $outfile) = @_;
|
|
|
|
my $srch = qr/fipsmodule.cnf/;
|
|
|
|
my $rep = "$value";
|
|
|
|
return replace_line_file_internal(srctop_file("test", 'fips.cnf'),
|
|
|
|
$srch, $rep, $outfile);
|
|
|
|
}
|
|
|
|
|
2019-11-23 15:54:29 +08:00
|
|
|
# fail if no module name
|
2020-02-26 02:25:13 +08:00
|
|
|
ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module',
|
2019-08-24 16:56:34 +08:00
|
|
|
'-provider_name', 'fips',
|
2020-06-29 10:20:41 +08:00
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
|
2020-07-21 14:30:02 +08:00
|
|
|
'-section_name', 'fips_sect'])),
|
2019-11-23 15:54:29 +08:00
|
|
|
"fipsinstall fail");
|
2019-08-24 16:56:34 +08:00
|
|
|
|
2019-11-23 15:54:29 +08:00
|
|
|
# fail to verify if the configuration file is missing
|
2019-08-24 16:56:34 +08:00
|
|
|
ok(!run(app(['openssl', 'fipsinstall', '-in', 'dummy.tmp', '-module', $infile,
|
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
2020-06-29 10:20:41 +08:00
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
|
2020-07-21 14:30:02 +08:00
|
|
|
'-section_name', 'fips_sect', '-verify'])),
|
2019-11-23 15:54:29 +08:00
|
|
|
"fipsinstall verify fail");
|
2019-08-24 16:56:34 +08:00
|
|
|
|
|
|
|
|
2020-02-26 02:25:13 +08:00
|
|
|
# output a fips.cnf file containing mac data
|
|
|
|
ok(run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
|
2019-08-24 16:56:34 +08:00
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
2020-06-29 10:20:41 +08:00
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
|
2020-07-21 14:30:02 +08:00
|
|
|
'-section_name', 'fips_sect'])),
|
2019-11-23 15:54:29 +08:00
|
|
|
"fipsinstall");
|
2019-08-24 16:56:34 +08:00
|
|
|
|
2020-02-26 02:25:13 +08:00
|
|
|
# verify the fips.cnf file
|
|
|
|
ok(run(app(['openssl', 'fipsinstall', '-in', 'fips.cnf', '-module', $infile,
|
2019-08-24 16:56:34 +08:00
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
2020-06-29 10:20:41 +08:00
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
|
2020-07-21 14:30:02 +08:00
|
|
|
'-section_name', 'fips_sect', '-verify'])),
|
2019-11-23 15:54:29 +08:00
|
|
|
"fipsinstall verify");
|
2019-08-24 16:56:34 +08:00
|
|
|
|
2020-07-21 14:30:02 +08:00
|
|
|
ok(replace_line_file('module-mac', '', 'fips_no_module_mac.cnf')
|
|
|
|
&& !run(app(['openssl', 'fipsinstall',
|
|
|
|
'-in', 'fips_no_module_mac.cnf',
|
|
|
|
'-module', $infile,
|
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
|
|
|
|
'-section_name', 'fips_sect', '-verify'])),
|
|
|
|
"fipsinstall verify fail no module mac");
|
|
|
|
|
|
|
|
ok(replace_line_file('install-mac', '', 'fips_no_install_mac.cnf')
|
|
|
|
&& !run(app(['openssl', 'fipsinstall',
|
|
|
|
'-in', 'fips_no_install_mac.cnf',
|
|
|
|
'-module', $infile,
|
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
|
|
|
|
'-section_name', 'fips_sect', '-verify'])),
|
|
|
|
"fipsinstall verify fail no install indicator mac");
|
|
|
|
|
|
|
|
ok(replace_line_file('module-mac', '00:00:00:00:00:00',
|
|
|
|
'fips_bad_module_mac.cnf')
|
|
|
|
&& !run(app(['openssl', 'fipsinstall',
|
|
|
|
'-in', 'fips_bad_module_mac.cnf',
|
|
|
|
'-module', $infile,
|
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
|
|
|
|
'-section_name', 'fips_sect', '-verify'])),
|
|
|
|
"fipsinstall verify fail if invalid module integrity value");
|
|
|
|
|
|
|
|
ok(replace_line_file('install-mac', '00:00:00:00:00:00',
|
|
|
|
'fips_bad_install_mac.cnf')
|
|
|
|
&& !run(app(['openssl', 'fipsinstall',
|
|
|
|
'-in', 'fips_bad_install_mac.cnf',
|
|
|
|
'-module', $infile,
|
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
|
|
|
|
'-section_name', 'fips_sect', '-verify'])),
|
|
|
|
"fipsinstall verify fail if invalid install indicator integrity value");
|
|
|
|
|
|
|
|
ok(replace_line_file('install-status', 'INCORRECT_STATUS_STRING',
|
|
|
|
'fips_bad_indicator.cnf')
|
|
|
|
&& !run(app(['openssl', 'fipsinstall',
|
|
|
|
'-in', 'fips_bad_indicator.cnf',
|
|
|
|
'-module', $infile,
|
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
|
|
|
|
'-section_name', 'fips_sect', '-verify'])),
|
|
|
|
"fipsinstall verify fail if invalid install indicator status");
|
|
|
|
|
2020-02-26 02:25:13 +08:00
|
|
|
# fail to verify the fips.cnf file if a different key is used
|
|
|
|
ok(!run(app(['openssl', 'fipsinstall', '-in', 'fips.cnf', '-module', $infile,
|
2019-08-24 16:56:34 +08:00
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
2020-06-29 10:20:41 +08:00
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:01",
|
2020-07-21 14:30:02 +08:00
|
|
|
'-section_name', 'fips_sect', '-verify'])),
|
2019-11-23 15:54:29 +08:00
|
|
|
"fipsinstall verify fail bad key");
|
2019-08-24 16:56:34 +08:00
|
|
|
|
2020-02-26 02:25:13 +08:00
|
|
|
# fail to verify the fips.cnf file if a different mac digest is used
|
|
|
|
ok(!run(app(['openssl', 'fipsinstall', '-in', 'fips.cnf', '-module', $infile,
|
2019-08-24 16:56:34 +08:00
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
2020-06-29 10:20:41 +08:00
|
|
|
'-macopt', 'digest:SHA512', '-macopt', "hexkey:$fipskey",
|
2020-07-21 14:30:02 +08:00
|
|
|
'-section_name', 'fips_sect', '-verify'])),
|
2019-11-23 15:54:29 +08:00
|
|
|
"fipsinstall verify fail incorrect digest");
|
2020-01-15 08:48:01 +08:00
|
|
|
|
|
|
|
# corrupt the module hmac
|
2020-02-26 02:25:13 +08:00
|
|
|
ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
|
2020-01-15 08:48:01 +08:00
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
2020-06-29 10:20:41 +08:00
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
|
2020-07-21 14:30:02 +08:00
|
|
|
'-section_name', 'fips_sect', '-corrupt_desc', 'HMAC'])),
|
2020-01-15 08:48:01 +08:00
|
|
|
"fipsinstall fails when the module integrity is corrupted");
|
|
|
|
|
|
|
|
# corrupt the first digest
|
2020-07-21 14:30:02 +08:00
|
|
|
ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips_fail.cnf', '-module', $infile,
|
2020-01-15 08:48:01 +08:00
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
2020-06-29 10:20:41 +08:00
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
|
2020-07-21 14:30:02 +08:00
|
|
|
'-section_name', 'fips_sect', '-corrupt_desc', 'SHA1'])),
|
2020-01-15 08:48:01 +08:00
|
|
|
"fipsinstall fails when the digest result is corrupted");
|
|
|
|
|
|
|
|
# corrupt another digest
|
2020-07-21 14:30:02 +08:00
|
|
|
ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips_fail.cnf', '-module', $infile,
|
2020-01-15 08:48:01 +08:00
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
2020-06-29 10:20:41 +08:00
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
|
2020-07-21 14:30:02 +08:00
|
|
|
'-section_name', 'fips_sect', '-corrupt_desc', 'SHA3'])),
|
2020-01-15 08:48:01 +08:00
|
|
|
"fipsinstall fails when the digest result is corrupted");
|
2020-01-31 05:53:04 +08:00
|
|
|
|
2021-04-12 08:38:16 +08:00
|
|
|
# corrupt cipher encrypt test
|
|
|
|
ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips_fail.cnf', '-module', $infile,
|
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
|
|
|
|
'-section_name', 'fips_sect', '-corrupt_desc', 'AES_GCM_Encrypt'])),
|
|
|
|
"fipsinstall fails when the AES_GCM result is corrupted");
|
|
|
|
|
|
|
|
# corrupt cipher decrypt test
|
|
|
|
ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips_fail.cnf', '-module', $infile,
|
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
|
|
|
|
'-section_name', 'fips_sect', '-corrupt_desc', 'AES_ECB_Decrypt'])),
|
|
|
|
"fipsinstall fails when the AES_ECB result is corrupted");
|
|
|
|
|
2020-01-31 05:53:04 +08:00
|
|
|
# corrupt DRBG
|
2020-07-21 14:30:02 +08:00
|
|
|
ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips_fail.cnf', '-module', $infile,
|
2020-01-31 05:53:04 +08:00
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
2020-06-29 10:20:41 +08:00
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
|
2020-07-21 14:30:02 +08:00
|
|
|
'-section_name', 'fips_sect', '-corrupt_desc', 'CTR'])),
|
2020-01-31 05:53:04 +08:00
|
|
|
"fipsinstall fails when the DRBG CTR result is corrupted");
|
2020-04-03 14:50:36 +08:00
|
|
|
|
|
|
|
# corrupt a KAS test
|
2020-04-20 21:05:47 +08:00
|
|
|
SKIP: {
|
|
|
|
skip "Skipping KAS DH corruption test because of no dh in this build", 1
|
|
|
|
if disabled("dh");
|
|
|
|
|
2020-08-06 13:07:39 +08:00
|
|
|
ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
|
2020-04-20 21:05:47 +08:00
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
2020-06-29 10:20:41 +08:00
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
|
2020-07-21 14:30:02 +08:00
|
|
|
'-section_name', 'fips_sect',
|
2020-04-20 21:05:47 +08:00
|
|
|
'-corrupt_desc', 'DH',
|
|
|
|
'-corrupt_type', 'KAT_KA'])),
|
|
|
|
"fipsinstall fails when the kas result is corrupted");
|
|
|
|
}
|
2020-04-03 14:50:36 +08:00
|
|
|
|
|
|
|
# corrupt a Signature test
|
2020-04-20 21:18:36 +08:00
|
|
|
SKIP: {
|
|
|
|
skip "Skipping Signature DSA corruption test because of no dsa in this build", 1
|
|
|
|
if disabled("dsa");
|
2020-08-06 13:07:39 +08:00
|
|
|
ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
|
2020-04-20 21:18:36 +08:00
|
|
|
'-provider_name', 'fips', '-mac_name', 'HMAC',
|
2020-06-29 10:20:41 +08:00
|
|
|
'-macopt', 'digest:SHA256', '-macopt', "hexkey:$fipskey",
|
2020-07-21 14:30:02 +08:00
|
|
|
'-section_name', 'fips_sect',
|
2020-04-20 21:18:36 +08:00
|
|
|
'-corrupt_desc', 'DSA',
|
|
|
|
'-corrupt_type', 'KAT_Signature'])),
|
|
|
|
"fipsinstall fails when the signature result is corrupted");
|
|
|
|
}
|
2020-07-21 14:30:02 +08:00
|
|
|
|
2020-09-16 12:10:23 +08:00
|
|
|
# corrupt an Asymmetric cipher test
|
|
|
|
SKIP: {
|
|
|
|
skip "Skipping Asymmetric RSA corruption test because of no rsa in this build", 1
|
|
|
|
if disabled("rsa");
|
|
|
|
ok(!run(app(['openssl', 'fipsinstall', '-out', 'fips.cnf', '-module', $infile,
|
|
|
|
'-corrupt_desc', 'RSA_Encrypt',
|
|
|
|
'-corrupt_type', 'KAT_AsymmetricCipher'])),
|
|
|
|
"fipsinstall fails when the asymmetric cipher result is corrupted");
|
|
|
|
}
|
|
|
|
|
2020-09-29 16:26:19 +08:00
|
|
|
# 'local' ensures that this change is only done in this file.
|
|
|
|
local $ENV{OPENSSL_CONF_INCLUDE} = abs2rel(curdir());
|
2020-07-21 14:30:02 +08:00
|
|
|
|
|
|
|
ok(replace_parent_line_file('fips.cnf', 'fips_parent.cnf')
|
|
|
|
&& run(app(['openssl', 'fipsinstall', '-config', 'fips_parent.cnf'])),
|
|
|
|
"verify fips provider loads from a configuration file");
|
|
|
|
|
|
|
|
ok(replace_parent_line_file('fips_no_module_mac.cnf',
|
|
|
|
'fips_parent_no_module_mac.cnf')
|
|
|
|
&& !run(app(['openssl', 'fipsinstall',
|
|
|
|
'-config', 'fips_parent_no_module_mac.cnf'])),
|
|
|
|
"verify load config fail no module mac");
|
|
|
|
|
|
|
|
ok(replace_parent_line_file('fips_no_install_mac.cnf',
|
|
|
|
'fips_parent_no_install_mac.cnf')
|
|
|
|
&& !run(app(['openssl', 'fipsinstall',
|
|
|
|
'-config', 'fips_parent_no_install_mac.cnf'])),
|
|
|
|
"verify load config fail no install mac");
|
|
|
|
|
|
|
|
ok(replace_parent_line_file('fips_bad_indicator.cnf',
|
|
|
|
'fips_parent_bad_indicator.cnf')
|
|
|
|
&& !run(app(['openssl', 'fipsinstall',
|
|
|
|
'-config', 'fips_parent_bad_indicator.cnf'])),
|
|
|
|
"verify load config fail bad indicator");
|
|
|
|
|
|
|
|
|
|
|
|
ok(replace_parent_line_file('fips_bad_install_mac.cnf',
|
|
|
|
'fips_parent_bad_install_mac.cnf')
|
|
|
|
&& !run(app(['openssl', 'fipsinstall',
|
|
|
|
'-config', 'fips_parent_bad_install_mac.cnf'])),
|
|
|
|
"verify load config fail bad install mac");
|
|
|
|
|
|
|
|
ok(replace_parent_line_file('fips_bad_module_mac.cnf',
|
|
|
|
'fips_parent_bad_module_mac.cnf')
|
|
|
|
&& !run(app(['openssl', 'fipsinstall',
|
|
|
|
'-config', 'fips_parent_bad_module_mac.cnf'])),
|
|
|
|
"verify load config fail bad module mac");
|
2021-05-05 08:36:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
my $stconf = "fipsmodule_selftest.cnf";
|
|
|
|
|
|
|
|
ok(run(app(['openssl', 'fipsinstall', '-out', $stconf,
|
|
|
|
'-module', $infile, '-self_test_onload'])),
|
|
|
|
"fipsinstall config saved without self test indicator");
|
|
|
|
|
|
|
|
ok(!run(app(['openssl', 'fipsinstall', '-in', $stconf,
|
|
|
|
'-module', $infile, '-verify'])),
|
|
|
|
"fipsinstall config verify fails without self test indicator");
|
|
|
|
|
|
|
|
ok(run(app(['openssl', 'fipsinstall', '-in', $stconf,
|
|
|
|
'-module', $infile, '-self_test_onload', '-verify'])),
|
|
|
|
"fipsinstall config verify passes when self test indicator is not present");
|