Skip to content

Commit 823dab8

Browse files
authored
Merge pull request #320 from padelsbach/wp-cmd-test-updates
Update command line tests for WPFF
2 parents 6208cb2 + a686bc4 commit 823dab8

File tree

6 files changed

+123
-75
lines changed

6 files changed

+123
-75
lines changed

scripts/cmd_test/cmd-test-common.sh

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,54 @@
1717
# You should have received a copy of the GNU General Public License
1818
# along with wolfProvider. If not, see <http://www.gnu.org/licenses/>.
1919

20+
COMMON_SETUP_DONE=0
21+
2022
cmd_test_env_setup() {
23+
# Fail flags
24+
FAIL=0
25+
FORCE_FAIL_PASSED=0
26+
27+
if [ $COMMON_SETUP_DONE -ne 0 ]; then
28+
echo "Setup already completed, skipping."
29+
return
30+
fi
31+
2132
local log_file_name=$1
2233
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
2334
# Set up environment
2435
export LOG_FILE="${SCRIPT_DIR}/${log_file_name}"
2536
touch "$LOG_FILE"
2637

27-
# OPENSSL_BIN must be set by the caller
38+
# If OPENSSL_BIN is not set, assume we are using a local build
2839
if [ -z "${OPENSSL_BIN:-}" ]; then
29-
echo "Error: OPENSSL_BIN environment variable is not set" | tee -a "$LOG_FILE"
30-
exit 1
31-
fi
40+
echo "OPENSSL_BIN not set, assuming local build"
41+
# Check if the install directories exist
42+
if [ ! -d "${REPO_ROOT}/openssl-install" ] ||
43+
[ ! -d "${REPO_ROOT}/wolfssl-install" ]; then
44+
echo "[FAIL] OpenSSL or wolfSSL install directories not found"
45+
echo "Please set OPENSSL_BIN or run build-wolfprovider.sh first"
46+
exit 1
47+
fi
3248

33-
# Fail flags
34-
FAIL=0
35-
FORCE_FAIL_PASSED=0
49+
# Setup the environment for a local build
50+
source "${REPO_ROOT}/scripts/env-setup"
51+
else
52+
echo "Using user-provided OPENSSL_BIN: ${OPENSSL_BIN}"
53+
# We are using a user-provided OpenSSL binary, manually set the test
54+
# environment variables rather than using env-setup.
55+
# Find the location of the wolfProvider modules
56+
if [ -z "${WOLFPROV_PATH:-}" ]; then
57+
export WOLFPROV_PATH=$(find /usr/lib /usr/local/lib -type d -name ossl-modules 2>/dev/null | head -n 1)
58+
fi
59+
# Set the path to the wolfProvider config file
60+
if [ -z "${WOLFPROV_CONFIG:-}" ]; then
61+
if [ "${WOLFSSL_ISFIPS:-0}" = "1" ]; then
62+
export WOLFPROV_CONFIG="${REPO_ROOT}/provider-fips.conf"
63+
else
64+
export WOLFPROV_CONFIG="${REPO_ROOT}/provider.conf"
65+
fi
66+
fi
67+
fi
3668

3769
# Get the force fail parameter
3870
if [ "${WOLFPROV_FORCE_FAIL}" = "1" ]; then
@@ -46,6 +78,17 @@ cmd_test_env_setup() {
4678
echo "Environment variables:"
4779
echo "OPENSSL_MODULES: ${OPENSSL_MODULES}"
4880
echo "OPENSSL_BIN: ${OPENSSL_BIN}"
81+
echo "WOLFPROV_PATH: ${WOLFPROV_PATH}"
82+
echo "WOLFPROV_CONFIG: ${WOLFPROV_CONFIG}"
83+
echo "LOG_FILE: ${LOG_FILE}"
84+
85+
COMMON_SETUP_DONE=1
86+
}
87+
88+
# Check if default provider is in use
89+
# Note that this may be wolfProvider if built as replace-default
90+
is_default_provider() {
91+
return $($OPENSSL_BIN list -providers | grep -qi "default")
4992
}
5093

5194
# Function to use default provider only
@@ -54,29 +97,41 @@ use_default_provider() {
5497
unset OPENSSL_CONF
5598

5699
# Verify that we are using the default provider
57-
if ${OPENSSL_BIN} list -providers | grep -q "wolfprov"; then
58-
echo "FAIL: unable to switch to default provider, wolfProvider is still active"
100+
if ! is_default_provider; then
101+
echo "FAIL: unable to switch to default provider"
102+
$OPENSSL_BIN list -providers
59103
exit 1
60104
fi
61105
echo "Switched to default provider"
62106
}
63107

108+
is_wolf_provider() {
109+
return $($OPENSSL_BIN list -providers | grep -qi "wolfSSL Provider")
110+
}
111+
64112
# Function to use wolf provider only
65113
use_wolf_provider() {
66114
export OPENSSL_MODULES=$WOLFPROV_PATH
67115
export OPENSSL_CONF=${WOLFPROV_CONFIG}
68116

69117
# Verify that we are using wolfProvider
70-
if ! ${OPENSSL_BIN} list -providers | grep -q "wolfprov"; then
71-
echo "FAIL: unable to switch to wolfProvider, default provider is still active"
118+
if ! is_wolf_provider; then
119+
echo "FAIL: unable to switch to wolfProvider"
120+
$OPENSSL_BIN list -providers
72121
exit 1
73122
fi
74123
echo "Switched to wolfProvider"
75124
}
76125

126+
is_replace_default() {
127+
return $($OPENSSL_BIN list -providers | grep -qi "wolfSSL Provider")
128+
}
129+
77130
# Helper function to handle force fail checks
78131
check_force_fail() {
79-
if [ "${WOLFPROV_FORCE_FAIL}" = "1" ]; then
132+
if is_default_provider && ! is_replace_default; then
133+
echo "OPENSSL Default provider active, no forced failures expected."
134+
elif [ "${WOLFPROV_FORCE_FAIL}" = "1" ]; then
80135
echo "[PASS] Test passed when force fail was enabled"
81136
FORCE_FAIL_PASSED=1
82137
fi

scripts/cmd_test/do-cmd-tests.sh

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,7 @@ REPO_ROOT="$( cd "${SCRIPT_DIR}/../.." &> /dev/null && pwd )"
2525
UTILS_DIR="${REPO_ROOT}/scripts"
2626

2727
source "${SCRIPT_DIR}/cmd-test-common.sh"
28-
29-
# If OPENSSL_BIN is not set, assume we are using a local build
30-
if [ -z "${OPENSSL_BIN:-}" ]; then
31-
# Check if the install directories exist
32-
if [ ! -d "${REPO_ROOT}/openssl-install" ] ||
33-
[ ! -d "${REPO_ROOT}/wolfssl-install" ]; then
34-
echo "[FAIL] OpenSSL or wolfSSL install directories not found"
35-
echo "Please set OPENSSL_BIN or run build-wolfprovider.sh first"
36-
exit 1
37-
fi
38-
39-
# Setup the environment for a local build
40-
source "${REPO_ROOT}/scripts/env-setup"
41-
else
42-
# We are using a user-provided OpenSSL binary, manually set the test
43-
# environment variables rather than using env-setup.
44-
# Find the location of the wolfProvider modules
45-
if [ -z "${WOLFPROV_PATH:-}" ]; then
46-
export WOLFPROV_PATH=$(find /usr/lib /usr/local/lib -type d -name ossl-modules 2>/dev/null | head -n 1)
47-
fi
48-
# Set the path to the wolfProvider config file
49-
if [ -z "${WOLFPROV_CONFIG:-}" ]; then
50-
if [ "${WOLFSSL_ISFIPS:-0}" = "1" ]; then
51-
export WOLFPROV_CONFIG="${REPO_ROOT}/provider-fips.conf"
52-
else
53-
export WOLFPROV_CONFIG="${REPO_ROOT}/provider.conf"
54-
fi
55-
fi
56-
fi
28+
cmd_test_env_setup
5729

5830
echo "=== Running wolfProvider Command-Line Tests ==="
5931
echo "Using OPENSSL_BIN: ${OPENSSL_BIN}"

scripts/cmd_test/ecc-cmd-test.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ test_sign_verify_pkeyutl() {
117117
local data_file="ecc_outputs/test_data.txt"
118118

119119
echo -e "\n=== Testing ECC (${curve}) Sign/Verify with pkeyutl Using ${provider_name} ==="
120+
121+
if [ ! -f "$key_file" ] || [ ! -f "$pub_key_file" ]; then
122+
echo "[FAIL] Key files for ECC (${curve}) not found, cannot run sign/verify tests"
123+
FAIL=1
124+
exit 1
125+
fi
126+
127+
if [ ! -f "$data_file" ]; then
128+
echo "[FAIL] Test data file not found, cannot run sign/verify tests"
129+
FAIL=1
130+
exit 1
131+
fi
120132

121133
# Test 1: Sign and verify with OpenSSL default
122134
use_default_provider
@@ -193,6 +205,12 @@ generate_and_test_key() {
193205
provider_name=$(get_provider_name "$provider_args")
194206

195207
echo -e "\n=== Testing ECC Key Generation (${curve}) with ${provider_name} ==="
208+
209+
if [ -f "$output_file" ]; then
210+
echo "ECC key file $output_file already exists, removing it."
211+
rm -f "$output_file"
212+
fi
213+
196214
echo "Generating ECC key (${curve})..."
197215

198216
if $OPENSSL_BIN genpkey -algorithm EC \
@@ -239,6 +257,14 @@ for curve in "${CURVES[@]}"; do
239257
# Generate key with current provider
240258
generate_and_test_key "$curve" "$test_provider"
241259

260+
# If WPFF is set, we need to run again to actually create the
261+
# key files
262+
if [ $WOLFPROV_FORCE_FAIL -ne 0 ]; then
263+
WOLFPROV_FORCE_FAIL=0
264+
generate_and_test_key "$curve" "$test_provider"
265+
WOLFPROV_FORCE_FAIL=1
266+
fi
267+
242268
# Test sign/verify interoperability
243269
test_sign_verify_pkeyutl "$curve" "$test_provider"
244270
done

scripts/cmd_test/req-cmd-test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ test_cert_creation() {
3535
local cert_file="req_outputs/cert_${curve}_${hash_alg}_${req_provider_name//lib/}.pem"
3636

3737
echo -e "\n=== Testing Certificate Creation (${curve}/${hash_alg}) - req with ${req_provider_name} ==="
38+
39+
if [ -f "$key_file" ]; then
40+
echo "Key file $key_file already exists, removing it."
41+
rm -f "$key_file"
42+
fi
43+
44+
if [ -f "$cert_file" ]; then
45+
echo "Certificate file $cert_file already exists, removing it."
46+
rm -f "$cert_file"
47+
fi
3848

3949
# Generate EC key with default provider
4050
echo "Generating EC key with curve ${curve} using default provider..."

scripts/cmd_test/rsa-cmd-test.sh

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,8 @@ KEY_TYPES=("RSA" "RSA-PSS")
3737
KEY_SIZES=("2048" "3072" "4096")
3838
PROVIDER_ARGS=("-provider-path $WOLFPROV_PATH -provider libwolfprov" "-provider default")
3939

40-
OPENSSL_BIN=${OPENSSL_BIN:-openssl}
41-
4240
echo "=== Running RSA Key Generation Tests ==="
4341

44-
rsa_check_force_fail() {
45-
local openssl_providers=$($OPENSSL_BIN list -providers)
46-
is_openssl_default_provider=$(echo "$openssl_providers" | grep -qi "OpenSSL Default Provider" && echo 1 || echo 0)
47-
if [ $is_openssl_default_provider -eq 1 ]; then
48-
# With the OpenSSL provider, don't expect failures
49-
echo "OPENSSL Default provider active, no forced failures expected."
50-
elif [ "${WOLFPROV_FORCE_FAIL}" = "1" ]; then
51-
echo "[PASS] Test passed when force fail was enabled"
52-
FORCE_FAIL_PASSED=1
53-
exit 1
54-
fi
55-
}
56-
5742
# Function to validate key
5843
validate_key() {
5944
local key_type=$1
@@ -76,15 +61,15 @@ validate_key() {
7661
return
7762
else
7863
echo "[PASS] ${key_type} key file exists and has content"
79-
rsa_check_force_fail
64+
check_force_fail
8065
fi
8166

8267
# Only try to extract public key if file exists and has content
8368
local pub_key_file="rsa_outputs/${key_type}_${key_size}_pub.pem"
8469
if $OPENSSL_BIN pkey -in "$key_file" -pubout -out "$pub_key_file" \
8570
${provider_args} -passin pass: >/dev/null; then
8671
echo "[PASS] ${key_type} Public key extraction successful"
87-
rsa_check_force_fail
72+
check_force_fail
8873
else
8974
echo "[FAIL] ${key_type} Public key extraction failed"
9075
FAIL=1
@@ -169,6 +154,8 @@ test_sign_verify_pkeyutl() {
169154

170155
# Get the provider name
171156
provider_name=$(get_provider_name "$provider_args")
157+
158+
echo -e "\n=== Testing ${key_type} (${key_size}) Sign/Verify with pkeyutl Using ${provider_name} ==="
172159

173160
# Handle different key naming conventions
174161
local key_prefix="${key_type}"
@@ -192,18 +179,16 @@ test_sign_verify_pkeyutl() {
192179
exit 1
193180
fi
194181

195-
echo -e "\n=== Testing ${key_type} (${key_size}) Sign/Verify with pkeyutl Using ${provider_name} ==="
196-
197182
# Test 1: Sign and verify with OpenSSL default
198183
use_default_provider
199184
echo "Test 1: Sign and verify with OpenSSL default (${key_type})"
200185
local default_sig_file="rsa_outputs/${key_prefix}_${key_size}_default_sig.bin"
201186
if $sign_func "$key_file" "$data_file" "$default_sig_file" "$provider_args"; then
202187
echo "[PASS] Signing with OpenSSL default successful"
203-
rsa_check_force_fail
188+
check_force_fail
204189
if $verify_func "$pub_key_file" "$data_file" "$default_sig_file" "$provider_args"; then
205190
echo "[PASS] Default provider verify successful"
206-
rsa_check_force_fail
191+
check_force_fail
207192
else
208193
echo "[FAIL] Default provider verify failed"
209194
FAIL=1
@@ -219,10 +204,10 @@ test_sign_verify_pkeyutl() {
219204
local wolf_sig_file="rsa_outputs/${key_prefix}_${key_size}_wolf_sig.bin"
220205
if $sign_func "$key_file" "$data_file" "$wolf_sig_file" "$provider_args"; then
221206
echo "[PASS] Signing with wolfProvider successful"
222-
rsa_check_force_fail
207+
check_force_fail
223208
if $verify_func "$pub_key_file" "$data_file" "$wolf_sig_file" "$provider_args"; then
224209
echo "[PASS] wolfProvider sign/verify successful"
225-
rsa_check_force_fail
210+
check_force_fail
226211
else
227212
echo "[FAIL] wolfProvider verify failed"
228213
FAIL=1
@@ -238,7 +223,7 @@ test_sign_verify_pkeyutl() {
238223
use_wolf_provider
239224
if $verify_func "$pub_key_file" "$data_file" "$default_sig_file" "$provider_args"; then
240225
echo "[PASS] wolfProvider can verify OpenSSL default signature"
241-
rsa_check_force_fail
226+
check_force_fail
242227
else
243228
echo "[FAIL] wolfProvider cannot verify OpenSSL default signature"
244229
FAIL=1
@@ -248,7 +233,7 @@ test_sign_verify_pkeyutl() {
248233
echo "Test 4: Cross-provider verification (wolf sign, default verify)"
249234
if $verify_func "$pub_key_file" "$data_file" "$wolf_sig_file" "$provider_args"; then
250235
echo "[PASS] OpenSSL default can verify wolfProvider signature"
251-
rsa_check_force_fail
236+
check_force_fail
252237
else
253238
echo "[FAIL] OpenSSL default cannot verify wolfProvider signature"
254239
FAIL=1
@@ -263,15 +248,16 @@ generate_and_test_key() {
263248
local provider_args=$3
264249
local output_file="rsa_outputs/${key_type}_${key_size}.pem"
265250

251+
# Get the provider name
252+
provider_name=$(get_provider_name "$provider_args")
253+
254+
echo -e "\n=== Testing ${key_type} Key Generation (${key_size}) with ${provider_name} ==="
255+
266256
if [ -f "$output_file" ]; then
267257
echo "Output file $output_file already exists, removing it."
268258
rm -f "$output_file"
269259
fi
270260

271-
# Get the provider name
272-
provider_name=$(get_provider_name "$provider_args")
273-
274-
echo -e "\n=== Testing ${key_type} Key Generation (${key_size}) with ${provider_name} ==="
275261
echo "Generating ${key_type} key (${key_size})..."
276262
if [ "$key_type" = "RSA-PSS" ]; then
277263
# For RSA-PSS, specify all parameters
@@ -283,7 +269,7 @@ generate_and_test_key() {
283269
-pkeyopt rsa_pss_keygen_saltlen:-1 \
284270
-out "$output_file" 2>/dev/null; then
285271
echo "[PASS] RSA-PSS key generation successful"
286-
rsa_check_force_fail
272+
check_force_fail
287273
else
288274
echo "[FAIL] RSA-PSS key generation failed"
289275
FAIL=1
@@ -295,7 +281,7 @@ generate_and_test_key() {
295281
-pkeyopt rsa_keygen_bits:${key_size} \
296282
-out "$output_file" 2>/dev/null; then
297283
echo "[PASS] RSA key generation successful"
298-
rsa_check_force_fail
284+
check_force_fail
299285
else
300286
echo "[FAIL] RSA key generation failed"
301287
FAIL=1
@@ -305,7 +291,7 @@ generate_and_test_key() {
305291
# Verify the key was generated
306292
if [ -s "$output_file" ]; then
307293
echo "[PASS] ${key_type} key (${key_size}) generation successful"
308-
rsa_check_force_fail
294+
check_force_fail
309295
else
310296
echo "[FAIL] ${key_type} key (${key_size}) generation failed"
311297
FAIL=1
@@ -322,7 +308,7 @@ generate_and_test_key() {
322308
if $OPENSSL_BIN pkey -in "$output_file" -check \
323309
${provider_args} -passin pass: >/dev/null; then
324310
echo "[PASS] ${provider_name} can use ${key_type} key (${key_size})"
325-
rsa_check_force_fail
311+
check_force_fail
326312
else
327313
echo "[FAIL] ${provider_name} cannot use ${key_type} key (${key_size})"
328314
FAIL=1

scripts/verify-install.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ verify_wolfprovider() {
249249
elif [ $is_wp_default -ne 1 ]; then
250250
handle_error "wolfProvider is not the default provider"
251251
fi
252-
253252
else
254253
if [ $is_openssl_replace_default -eq 1 ]; then
255254
handle_error "OpenSSL is replace default"

0 commit comments

Comments
 (0)