Skip to content

Commit 8a1c655

Browse files
author
coder-sageres
committed
Remove deprecated interfaces and internal interfaces, and use modern OpenSSL interfaces
Signed-off-by: coder-sageres <[email protected]>
1 parent 22d10f4 commit 8a1c655

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+2211
-25500
lines changed

CMakeLists.txt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,11 @@ if(CMAKE_SYSTEM_NAME MATCHES "Linux")
693693
set(CMAKE_EXE_LINKER_FLAGS "")
694694

695695
set(CMAKE_C_LINK_EXECUTABLE "")
696+
elseif(TOOLCHAIN STREQUAL "NONE")
697+
# Use native toolchain with group linking for static libraries
698+
set(CMAKE_LINKER gcc)
699+
set(CMAKE_EXE_LINKER_FLAGS "-no-pie")
700+
set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_LINKER> <LINK_FLAGS> <OBJECTS> -o <TARGET> -Wl,--start-group <LINK_LIBRARIES> -Wl,--end-group")
696701
endif()
697702

698703
if(NOT TOOLCHAIN STREQUAL "NIOS2_GCC")
@@ -1164,17 +1169,12 @@ else()
11641169
)
11651170
elseif(CRYPTO STREQUAL "openssl")
11661171
set(CRYPTO_DEPS "-lssl -lcrypto")
1167-
if(TOOLCHAIN STREQUAL "NONE")
1168-
target_link_libraries(${LIB_NAME}_crypto
1169-
PUBLIC openssllib
1170-
PUBLIC cryptlib_openssl
1171-
)
1172-
else()
1173-
target_link_libraries(${LIB_NAME}_crypto
1174-
PUBLIC ssl
1175-
PUBLIC crypto
1176-
)
1177-
endif()
1172+
# Always link to the OpenSSL library built from source (openssllib target)
1173+
# openssllib will be STATIC when TOOLCHAIN=NONE, SHARED otherwise
1174+
target_link_libraries(${LIB_NAME}_crypto
1175+
PUBLIC openssllib
1176+
PUBLIC cryptlib_openssl
1177+
)
11781178
endif()
11791179

11801180
target_link_libraries(${LIB_NAME}

library/spdm_crypt_lib/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,11 @@ target_sources(spdm_crypt_lib
3838
fips/libspdm_selftest_mldsa_vec.c
3939
fips/libspdm_selftest_slhdsa.c
4040
fips/libspdm_selftest_slhdsa_vec.c
41-
)
41+
)
42+
43+
# Link to cryptlib implementation
44+
if(TARGET cryptlib_openssl)
45+
target_link_libraries(spdm_crypt_lib INTERFACE cryptlib_openssl)
46+
elseif(TARGET cryptlib_mbedtls)
47+
target_link_libraries(spdm_crypt_lib INTERFACE cryptlib_mbedtls)
48+
endif()

os_stub/cryptlib_openssl/CMakeLists.txt

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ target_include_directories(cryptlib_openssl
99
${LIBSPDM_DIR}/os_stub/include
1010
${LIBSPDM_DIR}/os_stub/cryptlib_openssl
1111
${LIBSPDM_DIR}/os_stub/openssllib/include
12-
${LIBSPDM_DIR}/os_stub/openssllib/openssl_gen
13-
${LIBSPDM_DIR}/os_stub/openssllib/openssl/include
14-
${LIBSPDM_DIR}/os_stub/openssllib/openssl/crypto/include
15-
${LIBSPDM_DIR}/os_stub/openssllib/openssl
1612
)
1713

14+
# Ensure OpenSSL is built before cryptlib_openssl
15+
add_dependencies(cryptlib_openssl openssllib)
16+
1817
target_sources(cryptlib_openssl
1918
PRIVATE
2019
cipher/aead_aes_gcm.c
@@ -47,25 +46,33 @@ target_sources(cryptlib_openssl
4746
pk/x509.c
4847
pk/x509_pqc.c
4948
rand/rand.c
49+
rsa_context.c
50+
pqc_context.c
5051
sys_call/crt_wrapper_host.c
5152
)
5253

5354
target_compile_options(cryptlib_openssl PRIVATE ${OPENSSL_FLAGS})
5455

55-
if(ARCH STREQUAL "x64")
56-
target_compile_options(cryptlib_openssl PRIVATE -DLIBSPDM_CPU_X64)
57-
elseif(ARCH STREQUAL "ia32")
58-
target_compile_options(cryptlib_openssl PRIVATE -DLIBSPDM_CPU_IA32)
59-
elseif(ARCH STREQUAL "aarch64")
60-
target_compile_options(cryptlib_openssl PRIVATE -DLIBSPDM_CPU_AARCH64)
61-
elseif(ARCH STREQUAL "riscv32")
62-
target_compile_options(cryptlib_openssl PRIVATE -DLIBSPDM_CPU_RISCV32)
63-
elseif(ARCH STREQUAL "riscv64")
64-
target_compile_options(cryptlib_openssl PRIVATE -DLIBSPDM_CPU_RISCV64)
65-
elseif((ARCH STREQUAL "arm") OR (ARCH STREQUAL "aarch64"))
66-
target_compile_options(cryptlib_openssl PRIVATE -DLIBSPDM_CPU_ARM)
67-
elseif(ARCH STREQUAL "loongarch64")
68-
target_compile_options(cryptlib_openssl PRIVATE -DLIBSPDM_CPU_LOONGARCH64)
69-
else()
70-
message(FATAL_ERROR "Unknown ARCH")
71-
endif()
56+
# Suppress deprecated warnings for OpenSSL deprecated APIs
57+
#
58+
# Why suppress instead of fixing:
59+
# 1. EC Key Management: libspdm_ec_set_pub_key/libspdm_ec_set_priv_key use deprecated EC_KEY APIs
60+
# as fallback when modern EVP_PKEY_set* methods fail. This ensures compatibility when keys
61+
# are set separately (public first, then private).
62+
#
63+
# 2. SM2 Implementation: os_stub/cryptlib_openssl/pk/sm2.c uses deprecated EC_KEY APIs throughout.
64+
# Full migration to modern APIs requires significant refactoring.
65+
#
66+
# 3. FIPS Mode: libspdm_ecdsa_sign_ex uses deprecated APIs for custom random number injection,
67+
# which is required for FIPS testing (only compiled when LIBSPDM_FIPS_MODE is enabled).
68+
#
69+
# Cross-platform suppression:
70+
# - GCC/Clang (Linux/macOS): -Wno-deprecated-declarations
71+
# - MSVC (Windows): /wd4996
72+
if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
73+
target_compile_options(cryptlib_openssl PRIVATE -Wno-deprecated-declarations)
74+
elseif(CMAKE_C_COMPILER_ID MATCHES "MSVC")
75+
target_compile_options(cryptlib_openssl PRIVATE /wd4996)
76+
endif()
77+
78+
target_link_libraries(cryptlib_openssl PUBLIC openssllib memlib)

os_stub/cryptlib_openssl/der/der.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
#include <openssl/x509.h>
1313
#include <openssl/evp.h>
1414
#include <openssl/decoder.h>
15+
#include "rsa_context.h"
1516

1617
#if (LIBSPDM_RSA_SSA_SUPPORT) || (LIBSPDM_RSA_PSS_SUPPORT)
18+
1719
/**
1820
* Retrieve the RSA Public key from the DER key data.
1921
*
@@ -39,6 +41,7 @@ bool libspdm_rsa_get_public_key_from_der(const uint8_t *der_data,
3941
{
4042
bool status;
4143
BIO *der_bio;
44+
EVP_PKEY *pkey;
4245

4346
/* Check input parameters.*/
4447

@@ -47,6 +50,7 @@ bool libspdm_rsa_get_public_key_from_der(const uint8_t *der_data,
4750
}
4851

4952
status = false;
53+
pkey = NULL;
5054

5155
/* Read DER data.*/
5256

@@ -59,11 +63,25 @@ bool libspdm_rsa_get_public_key_from_der(const uint8_t *der_data,
5963
goto done;
6064
}
6165

62-
/* Retrieve RSA Public key from DER data.*/
63-
64-
*rsa_context = d2i_RSA_PUBKEY_bio(der_bio, NULL);
65-
if (*rsa_context != NULL) {
66+
/* Retrieve RSA Public key from DER data as EVP_PKEY.*/
67+
pkey = d2i_PUBKEY_bio(der_bio, NULL);
68+
if (pkey == NULL) {
69+
goto done;
70+
}
71+
if (EVP_PKEY_base_id(pkey) != EVP_PKEY_RSA) {
72+
goto done;
73+
}
74+
{
75+
libspdm_rsa_ctx_evp_t *ctx;
76+
ctx = (libspdm_rsa_ctx_evp_t *)allocate_pool(sizeof(libspdm_rsa_ctx_evp_t));
77+
if (ctx == NULL) {
78+
goto done;
79+
}
80+
libspdm_zero_mem(ctx, sizeof(*ctx));
81+
ctx->pkey = pkey;
82+
*rsa_context = (void *)ctx;
6683
status = true;
84+
pkey = NULL; /* ownership moved */
6785
}
6886

6987
done:
@@ -199,9 +217,18 @@ bool libspdm_ecd_get_public_key_from_der(const uint8_t *der_data,
199217
}
200218
type = EVP_PKEY_id(pkey);
201219
if ((type != EVP_PKEY_ED25519) && (type != EVP_PKEY_ED448)) {
220+
EVP_PKEY_free(pkey);
221+
goto done;
222+
}
223+
224+
/* Create double pointer structure for EdDSA context (compatible with libspdm_ecd_new_by_nid) */
225+
EVP_PKEY **ecd_context_ptr = malloc(sizeof(EVP_PKEY *));
226+
if (ecd_context_ptr == NULL) {
227+
EVP_PKEY_free(pkey);
202228
goto done;
203229
}
204-
*ecd_context = pkey;
230+
*ecd_context_ptr = pkey;
231+
*ecd_context = ecd_context_ptr;
205232
status = true;
206233

207234
done:

0 commit comments

Comments
 (0)