Skip to content

Commit d02ae73

Browse files
authored
Fix ECC not recognizing lower case sha strings, fix how test scripts … (#289)
* Fix ECC not recognizing lower case sha strings, fix how test scripts check for default replace. * Fix new req tests with proper force fail handling * Skip req tests for FIPS * Fix WP file read to consume BIO to EOF on early error, guarding against infinite loop in openssl binary * Remove check for default replace * Update req script WPFF check handling
1 parent 0fd5c04 commit d02ae73

File tree

8 files changed

+236
-37
lines changed

8 files changed

+236
-37
lines changed

scripts/cmd_test/clean-cmd-test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ clean_all_cmd_tests() {
3333
clean_cmd_test "aes"
3434
clean_cmd_test "ecc"
3535
clean_cmd_test "hash"
36+
clean_cmd_test "req"
3637
clean_cmd_test "rsa"
3738
}

scripts/cmd_test/do-cmd-tests.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,13 @@ echo -e "\n=== Running ECC Key Generation Test ==="
8484
"${REPO_ROOT}/scripts/cmd_test/ecc-cmd-test.sh"
8585
ECC_RESULT=$?
8686

87+
# Run the Certificate Request test
88+
echo -e "\n=== Running Certificate Request Test ==="
89+
"${REPO_ROOT}/scripts/cmd_test/req-cmd-test.sh"
90+
REQ_RESULT=$?
91+
8792
# Check results
88-
if [ $HASH_RESULT -eq 0 ] && [ $AES_RESULT -eq 0 ] && [ $RSA_RESULT -eq 0 ] && [ $ECC_RESULT -eq 0 ]; then
93+
if [ $HASH_RESULT -eq 0 ] && [ $AES_RESULT -eq 0 ] && [ $RSA_RESULT -eq 0 ] && [ $ECC_RESULT -eq 0 ] && [ $REQ_RESULT -eq 0 ]; then
8994
echo -e "\n=== All Command-Line Tests Passed ==="
9095
if [ "${WOLFPROV_FORCE_FAIL}" = "1" ]; then
9196
echo "Force fail mode was enabled"
@@ -97,6 +102,7 @@ if [ $HASH_RESULT -eq 0 ] && [ $AES_RESULT -eq 0 ] && [ $RSA_RESULT -eq 0 ] && [
97102
echo "AES Test Result: $AES_RESULT (0=success)"
98103
echo "RSA Test Result: $RSA_RESULT (0=success)"
99104
echo "ECC Test Result: $ECC_RESULT (0=success)"
105+
echo "REQ Test Result: $REQ_RESULT (0=success)"
100106
exit 0
101107
else
102108
echo -e "\n=== Command-Line Tests Failed ==="
@@ -110,5 +116,6 @@ else
110116
echo "AES Test Result: $AES_RESULT (0=success)"
111117
echo "RSA Test Result: $RSA_RESULT (0=success)"
112118
echo "ECC Test Result: $ECC_RESULT (0=success)"
119+
echo "REQ Test Result: $REQ_RESULT (0=success)"
113120
exit 1
114121
fi

scripts/cmd_test/req-cmd-test.sh

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/bin/bash
2+
# req-cmd-test.sh - Certificate request test for wolfProvider
3+
4+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
5+
source "${SCRIPT_DIR}/cmd-test-common.sh"
6+
source "${SCRIPT_DIR}/clean-cmd-test.sh"
7+
cmd_test_env_setup "req-test.log"
8+
clean_cmd_test "req"
9+
10+
exec > >(tee -a "$LOG_FILE") 2>&1
11+
mkdir -p req_outputs
12+
13+
CURVES=("prime256v1" "secp384r1" "secp521r1")
14+
HASH_ALGORITHMS=("sha256" "sha384" "sha512")
15+
PROVIDER_ARGS=("-provider-path $WOLFPROV_PATH -provider libwolfprov" "-provider default")
16+
17+
echo "=== Running Certificate Request (X.509) Tests ==="
18+
19+
# Skip tests for FIPS mode (unless force-failing)
20+
if [ "${WOLFSSL_ISFIPS}" = "1" ] && [ "${WOLFPROV_FORCE_FAIL}" != "1" ]; then
21+
echo "INFO: FIPS mode detected"
22+
echo "INFO: Skipping req tests for FIPS mode"
23+
echo "SUCCESS: Certificate Request tests skipped for FIPS build"
24+
exit 0
25+
fi
26+
27+
# Function to test certificate creation
28+
test_cert_creation() {
29+
local curve=$1
30+
local hash_alg=$2
31+
local req_provider_args=$3
32+
33+
req_provider_name=$(get_provider_name "$req_provider_args")
34+
local key_file="req_outputs/key_${curve}_${hash_alg}.pem"
35+
local cert_file="req_outputs/cert_${curve}_${hash_alg}_${req_provider_name//lib/}.pem"
36+
37+
echo -e "\n=== Testing Certificate Creation (${curve}/${hash_alg}) - req with ${req_provider_name} ==="
38+
39+
# Generate EC key with default provider
40+
echo "Generating EC key with curve ${curve} using default provider..."
41+
use_default_provider
42+
if $OPENSSL_BIN ecparam -genkey -name ${curve} -out "$key_file" \
43+
-provider default 2>/dev/null; then
44+
echo "[PASS] EC key generation successful"
45+
# Don't call check_force_fail for default provider operations in force fail mode
46+
# as default provider operations are expected to succeed
47+
if [ "${WOLFPROV_FORCE_FAIL}" != "1" ]; then
48+
check_force_fail
49+
fi
50+
else
51+
echo "[FAIL] EC key generation failed"
52+
FAIL=1
53+
return
54+
fi
55+
56+
# Set provider for req command
57+
if [[ "$req_provider_args" == *"libwolfprov"* ]]; then
58+
use_wolf_provider
59+
else
60+
use_default_provider
61+
fi
62+
63+
# Create certificate with specified provider
64+
echo "Creating self-signed certificate with ${hash_alg} using ${req_provider_name}..."
65+
if $OPENSSL_BIN req -x509 -new -key "$key_file" -${hash_alg} -days 365 \
66+
-out "$cert_file" -subj "/CN=test-${curve}-${hash_alg}" ${req_provider_args} 2>/dev/null; then
67+
echo "[PASS] Certificate creation successful"
68+
# Only call check_force_fail for wolfProvider operations, or when not in force fail mode
69+
if [[ "$req_provider_args" == *"libwolfprov"* ]] || [ "${WOLFPROV_FORCE_FAIL}" != "1" ]; then
70+
check_force_fail
71+
fi
72+
else
73+
echo "[FAIL] Certificate creation failed"
74+
FAIL=1
75+
return
76+
fi
77+
78+
# Check if certificate file exists and is non-empty
79+
if [ -s "$cert_file" ]; then
80+
echo "[PASS] Certificate file exists and is non-empty"
81+
# Only call check_force_fail for wolfProvider operations, or when not in force fail mode
82+
if [[ "$req_provider_args" == *"libwolfprov"* ]] || [ "${WOLFPROV_FORCE_FAIL}" != "1" ]; then
83+
check_force_fail
84+
fi
85+
else
86+
echo "[FAIL] Certificate file does not exist or is empty"
87+
FAIL=1
88+
fi
89+
}
90+
91+
# Main test execution
92+
echo "Starting certificate request tests..."
93+
94+
for curve in "${CURVES[@]}"; do
95+
for hash_alg in "${HASH_ALGORITHMS[@]}"; do
96+
for provider_arg in "${PROVIDER_ARGS[@]}"; do
97+
test_cert_creation "$curve" "$hash_alg" "$provider_arg"
98+
done
99+
done
100+
done
101+
102+
# Force-fail handling (same pattern as other cmd tests)
103+
if [ "${WOLFPROV_FORCE_FAIL}" = "1" ]; then
104+
if [ $FORCE_FAIL_PASSED -eq 1 ]; then
105+
echo -e "\n=== Certificate Request Tests Failed With Force Fail Enabled ==="
106+
echo "ERROR: Some tests passed when they should have failed"
107+
exit 1
108+
else
109+
echo -e "\n=== Certificate Request Tests Passed With Force Fail Enabled ==="
110+
echo "SUCCESS: All tests failed as expected"
111+
exit 0
112+
fi
113+
else
114+
if [ $FAIL -eq 0 ]; then
115+
echo -e "\n=== All Certificate Request tests completed successfully ==="
116+
exit 0
117+
else
118+
echo -e "\n=== Certificate Request tests completed with failures ==="
119+
exit 1
120+
fi
121+
fi

src/wp_ecdsa_sig.c

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -725,32 +725,35 @@ static int wp_ecdsa_digest_verify_final(wp_EcdsaSigCtx *ctx, unsigned char *sig,
725725
* @return 1 on success.
726726
* @return 0 on failure.
727727
*/
728-
static int wp_ecdsa_get_alg_id(wp_EcdsaSigCtx *ctx, OSSL_PARAM *p)
729-
{
730-
int ok = 0;
731-
732-
if (XMEMCMP(ctx->mdName, "SHA256", 7) == 0) {
733-
static const unsigned char ecdsa_sha256[] = {
734-
0x30, 0x0a, 0x06, 0x08, 42, 134, 72, 206, 61, 4, 3, 2
735-
};
736-
ok = OSSL_PARAM_set_octet_string(p, ecdsa_sha256, sizeof(ecdsa_sha256));
737-
}
738-
if (XMEMCMP(ctx->mdName, "SHA384", 7) == 0) {
739-
static const unsigned char ecdsa_sha384[] = {
740-
0x30, 0x0a, 0x06, 0x08, 42, 134, 72, 206, 61, 4, 3, 3
741-
};
742-
ok = OSSL_PARAM_set_octet_string(p, ecdsa_sha384, sizeof(ecdsa_sha384));
743-
}
744-
if (XMEMCMP(ctx->mdName, "SHA512", 7) == 0) {
745-
static const unsigned char ecdsa_sha512[] = {
746-
0x30, 0x0a, 0x06, 0x08, 42, 134, 72, 206, 61, 4, 3, 4
747-
};
748-
ok = OSSL_PARAM_set_octet_string(p, ecdsa_sha512, sizeof(ecdsa_sha512));
749-
}
750-
/* TODO: support more digests */
751-
752-
return ok;
753-
}
728+
static int wp_ecdsa_get_alg_id(wp_EcdsaSigCtx *ctx, OSSL_PARAM *p)
729+
{
730+
int ok = 0;
731+
732+
if ((XMEMCMP(ctx->mdName, "SHA256", 7) == 0) ||
733+
(XMEMCMP(ctx->mdName, "sha256", 7) == 0)) {
734+
static const unsigned char ecdsa_sha256[] = {
735+
0x30, 0x0a, 0x06, 0x08, 42, 134, 72, 206, 61, 4, 3, 2
736+
};
737+
ok = OSSL_PARAM_set_octet_string(p, ecdsa_sha256, sizeof(ecdsa_sha256));
738+
}
739+
if ((XMEMCMP(ctx->mdName, "SHA384", 7) == 0) ||
740+
(XMEMCMP(ctx->mdName, "sha384", 7) == 0)) {
741+
static const unsigned char ecdsa_sha384[] = {
742+
0x30, 0x0a, 0x06, 0x08, 42, 134, 72, 206, 61, 4, 3, 3
743+
};
744+
ok = OSSL_PARAM_set_octet_string(p, ecdsa_sha384, sizeof(ecdsa_sha384));
745+
}
746+
if ((XMEMCMP(ctx->mdName, "SHA512", 7) == 0) ||
747+
(XMEMCMP(ctx->mdName, "sha512", 7) == 0)) {
748+
static const unsigned char ecdsa_sha512[] = {
749+
0x30, 0x0a, 0x06, 0x08, 42, 134, 72, 206, 61, 4, 3, 4
750+
};
751+
ok = OSSL_PARAM_set_octet_string(p, ecdsa_sha512, sizeof(ecdsa_sha512));
752+
}
753+
/* TODO: support more digests */
754+
755+
return ok;
756+
}
754757

755758
/**
756759
* Put data from ECDSA signture context object into parameter objects.

src/wp_file_store.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,17 @@ static OSSL_DECODER_CTX* wp_file_setup_decoders(wp_FileCtx* ctx)
431431
return decCtx;
432432
}
433433

434+
static void wp_bio_consume_all(BIO* bio)
435+
{
436+
char buffer[128];
437+
int bytes_read = 0;
438+
439+
/* Consume everything */
440+
do {
441+
bytes_read = BIO_read(bio, buffer, sizeof(buffer));
442+
} while (bytes_read > 0);
443+
}
444+
434445
/**
435446
* Load the data from a file.
436447
*
@@ -454,6 +465,9 @@ static int wp_file_load(wp_FileCtx* ctx, OSSL_CALLBACK* objCb, void* objCbArg,
454465
}
455466
if (ctx->decCtx == NULL) {
456467
ok = 0;
468+
/* If we error here, we dont consume the BIO at all and simply return 0,
469+
* however callers loop is until EOF. Set BIO to EOF on early error */
470+
wp_bio_consume_all(ctx->bio);
457471
}
458472

459473
if (ok) {

test/standalone/test_common.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
# test_common.sh - Common utilities for standalone tests
3+
#
4+
# Copyright (C) 2006-2025 wolfSSL Inc.
5+
#
6+
# This file is part of wolfProvider.
7+
#
8+
# wolfProvider is free software; you can redistribute it and/or modify
9+
# it under the terms of the GNU General Public License as published by
10+
# the Free Software Foundation; either version 3 of the License, or
11+
# (at your option) any later version.
12+
#
13+
# wolfProvider is distributed in the hope that it will be useful,
14+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
# GNU General Public License for more details.
17+
#
18+
# You should have received a copy of the GNU General Public License
19+
# along with wolfProvider. If not, see <http://www.gnu.org/licenses/>.
20+
21+
# Function to detect if wolfProvider was built with --replace-default
22+
# Returns 0 if replace-default is detected, 1 otherwise
23+
detect_replace_default_build() {
24+
local libcrypto_path=""
25+
26+
# Try common locations relative to the test root
27+
local test_root="${ROOT_DIR:-}"
28+
29+
if [ -z "$test_root" ]; then
30+
# Fallback: try to determine root from current location
31+
test_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &>/dev/null && pwd)"
32+
fi
33+
34+
# Try common locations
35+
if [ -n "${OPENSSL_LIB_PATH:-}" ] && [ -f "${OPENSSL_LIB_PATH}/libcrypto.so" ]; then
36+
libcrypto_path="${OPENSSL_LIB_PATH}/libcrypto.so"
37+
elif [ -f "${test_root}/openssl-install/lib64/libcrypto.so" ]; then
38+
libcrypto_path="${test_root}/openssl-install/lib64/libcrypto.so"
39+
elif [ -f "${test_root}/openssl-install/lib/libcrypto.so" ]; then
40+
libcrypto_path="${test_root}/openssl-install/lib/libcrypto.so"
41+
else
42+
return 1 # Can't find libcrypto, assume standard build
43+
fi
44+
45+
# Check for replace-default patch symbols in libcrypto
46+
if strings "$libcrypto_path" 2>/dev/null | grep -q "load_wolfprov_and_init"; then
47+
return 0 # Replace-default build detected
48+
else
49+
return 1 # Standard build
50+
fi
51+
}

test/standalone/tests/hardload/run.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ if ! source "$ROOT_DIR/scripts/env-setup" >/dev/null; then
2424
exit 1
2525
fi
2626

27+
# Source common test utilities
28+
source "$ROOT_DIR/test/standalone/test_common.sh"
29+
30+
# Check if this is a replace-default build
2731
WP_USING_REPLACE_DEFAULT="0"
28-
if [ -f "$OPENSSL_LIB_PATH/libcrypto.so" ]; then
29-
# Check for wolfProvider symbols in libcrypto
30-
if nm -D "$OPENSSL_LIB_PATH/libcrypto.so" 2>/dev/null | grep -q "wolfprov_provider_init"; then
31-
WP_USING_REPLACE_DEFAULT="1"
32-
fi
32+
if detect_replace_default_build; then
33+
WP_USING_REPLACE_DEFAULT="1"
3334
fi
3435

3536
# Configure environment based on build type

test/standalone/tests/sha256_simple/run.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ if ! source "$ROOT_DIR/scripts/env-setup" >/dev/null; then
2424
exit 1
2525
fi
2626

27+
# Source common test utilities
28+
source "$ROOT_DIR/test/standalone/test_common.sh"
29+
30+
# Check if this is a replace-default build
2731
WP_USING_REPLACE_DEFAULT="0"
28-
if [ -f "$OPENSSL_LIB_PATH/libcrypto.so" ]; then
29-
# Check for wolfProvider symbols in libcrypto
30-
if nm -D "$OPENSSL_LIB_PATH/libcrypto.so" 2>/dev/null | grep -q "wolfprov_provider_init"; then
31-
WP_USING_REPLACE_DEFAULT="1"
32-
fi
32+
if detect_replace_default_build; then
33+
WP_USING_REPLACE_DEFAULT="1"
3334
fi
3435

3536
# Configure environment based on build type

0 commit comments

Comments
 (0)