From 19dd6c2b7345f4dfd011c87157fe0ad6282f4a72 Mon Sep 17 00:00:00 2001 From: Ionut Ursachi Date: Tue, 10 Oct 2023 17:45:36 +0000 Subject: [PATCH 1/4] Added shared library and pkg-config support for LibSPDM. Changes: - spdm_device_secret_lib_sample has its own dump_hex_str_sample method - added pkg-config libspdm.pc.in file for cmake to use as template CMakeLists.txt changes: - added BUILD_SHARED_LIB cmake parameter (ON/OFF) to enable/disable building of libspdm.so in addition to the static libraries - added two build paths for libspdm.so that generate distinct pkg-config pc files, for runtime linking with either mbedtls or openssl Signed-off-by: Ionut Ursachi --- CMakeLists.txt | 59 +++++++++++++++++++++ libspdm.pc.in | 12 +++++ os_stub/spdm_device_secret_lib_sample/lib.c | 11 +++- 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 libspdm.pc.in diff --git a/CMakeLists.txt b/CMakeLists.txt index abedce31133..70f2e319989 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,11 @@ cmake_minimum_required(VERSION 2.8.12) project("libspdm" C) +SET(LIB_NAME "spdm") +SET(CMAKE_PROJECT_NAME "LibSPDM") +SET(CMAKE_PROJECT_DESCRIPTION "LibSPDM Shared Library") +FILE(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/VERSION.md CMAKE_INPUT_PROJECT_VERSION) +string(REPLACE " " "_" CMAKE_PROJECT_VERSION ${CMAKE_INPUT_PROJECT_VERSION}) # # Build Configuration Macro Definition @@ -16,6 +21,7 @@ SET(CMAKE_BUILD_TYPE ${TARGET} CACHE STRING "Choose the target of build: Debug R SET(CRYPTO ${CRYPTO} CACHE STRING "Choose the crypto of build: mbedtls openssl" FORCE) SET(GCOV ${GCOV} CACHE STRING "Choose the target of Gcov: ON OFF, and default is OFF" FORCE) SET(STACK_USAGE ${STACK_USAGE} CACHE STRING "Choose the target of STACK_USAGE: ON OFF, and default is OFF" FORCE) +SET(BUILD_SHARED_LIB ${BUILD_SHARED_LIB} CACHE STRING "Choose if shared library libspdm.co should be built: ON OFF, and default is OFF" FORCE) if(NOT GCOV) SET(GCOV "OFF") @@ -25,6 +31,10 @@ if(NOT STACK_USAGE) SET(STACK_USAGE "OFF") endif() +if(NOT BUILD_SHARED_LIB) + SET(BUILD_SHARED_LIB "OFF") +endif() + SET(LIBSPDM_DIR ${PROJECT_SOURCE_DIR}) # @@ -974,4 +984,53 @@ else() endif() endif() endif() + + if (BUILD_SHARED_LIB STREQUAL "ON") + if(CRYPTO STREQUAL "mbedtls") + SET(CRYPTO_DEPS "-lmbedtls -lmbedx509 -lmbedcrypto") + add_library(${LIB_NAME} SHARED + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + ) + + elseif(CRYPTO STREQUAL "openssl") + SET(CRYPTO_DEPS "-lssl -lcrypto") + add_library(${LIB_NAME} SHARED + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + ) + endif() + + install(DIRECTORY include DESTINATION include/libspdm) + configure_file(libspdm.pc.in lib/pkgconfig/libspdm.pc @ONLY) + install(FILES ${CMAKE_BINARY_DIR}/lib/pkgconfig/libspdm.pc DESTINATION lib/pkgconfig/) + install(TARGETS ${LIB_NAME} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) + endif() endif() diff --git a/libspdm.pc.in b/libspdm.pc.in new file mode 100644 index 00000000000..f0ec86c4ff5 --- /dev/null +++ b/libspdm.pc.in @@ -0,0 +1,12 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=@CMAKE_INSTALL_PREFIX@ +libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@/lib +includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@/include/lib@PROJECT_NAME@ + +Name: @CMAKE_PROJECT_NAME@ +Description: @CMAKE_PROJECT_DESCRIPTION@ +Version: @CMAKE_PROJECT_VERSION@ + +Requires: +Cflags: -I${includedir} -I${includedir}/include +Libs: -L${libdir} -l@PROJECT_NAME@ @CRYPTO_DEPS@ diff --git a/os_stub/spdm_device_secret_lib_sample/lib.c b/os_stub/spdm_device_secret_lib_sample/lib.c index 2edfce192b4..3ed99e9fb70 100644 --- a/os_stub/spdm_device_secret_lib_sample/lib.c +++ b/os_stub/spdm_device_secret_lib_sample/lib.c @@ -1495,6 +1495,15 @@ uint8_t m_libspdm_bin_str0[0x11] = { 0x64, 0x65, 0x72, 0x69, 0x76, 0x65, 0x64, }; +void libspdm_dump_hex_str_sample(const uint8_t *buffer, size_t buffer_size) +{ + size_t index; + + for (index = 0; index < buffer_size; index++) { + printf("%02x", buffer[index]); + } +} + bool libspdm_psk_handshake_secret_hkdf_expand( spdm_version_number_t spdm_version, uint32_t base_hash_algo, @@ -1522,7 +1531,7 @@ bool libspdm_psk_handshake_secret_hkdf_expand( return false; } printf("[PSK]: "); - libspdm_dump_hex_str(psk, psk_size); + libspdm_dump_hex_str_sample(psk, psk_size); printf("\n"); hash_size = libspdm_get_hash_size(base_hash_algo); From 3052cd15f70cafcfe066f9513f536b5fc89d5d68 Mon Sep 17 00:00:00 2001 From: Ionut Ursachi Date: Wed, 11 Oct 2023 20:49:04 +0000 Subject: [PATCH 2/4] Renamed BUILD_SHARED_LIB to BUILD_LINUX_SHARED_LIB and placed all definitions related to it in the same cmake block. Updated pkg-config pc.in file to fix a small path naming error. Signed-off-by: Ionut Ursachi --- CMakeLists.txt | 15 ++++++++------- libspdm.pc.in | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 70f2e319989..999dd30ce1f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,6 @@ cmake_minimum_required(VERSION 2.8.12) project("libspdm" C) -SET(LIB_NAME "spdm") -SET(CMAKE_PROJECT_NAME "LibSPDM") -SET(CMAKE_PROJECT_DESCRIPTION "LibSPDM Shared Library") FILE(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/VERSION.md CMAKE_INPUT_PROJECT_VERSION) string(REPLACE " " "_" CMAKE_PROJECT_VERSION ${CMAKE_INPUT_PROJECT_VERSION}) @@ -21,7 +18,7 @@ SET(CMAKE_BUILD_TYPE ${TARGET} CACHE STRING "Choose the target of build: Debug R SET(CRYPTO ${CRYPTO} CACHE STRING "Choose the crypto of build: mbedtls openssl" FORCE) SET(GCOV ${GCOV} CACHE STRING "Choose the target of Gcov: ON OFF, and default is OFF" FORCE) SET(STACK_USAGE ${STACK_USAGE} CACHE STRING "Choose the target of STACK_USAGE: ON OFF, and default is OFF" FORCE) -SET(BUILD_SHARED_LIB ${BUILD_SHARED_LIB} CACHE STRING "Choose if shared library libspdm.co should be built: ON OFF, and default is OFF" FORCE) +SET(BUILD_LINUX_SHARED_LIB ${BUILD_LINUX_SHARED_LIB} CACHE STRING "Choose if libspdm shared library should be built for linux: ON OFF, and default is OFF" FORCE) if(NOT GCOV) SET(GCOV "OFF") @@ -31,8 +28,8 @@ if(NOT STACK_USAGE) SET(STACK_USAGE "OFF") endif() -if(NOT BUILD_SHARED_LIB) - SET(BUILD_SHARED_LIB "OFF") +if(NOT BUILD_LINUX_SHARED_LIB) + SET(BUILD_LINUX_SHARED_LIB "OFF") endif() SET(LIBSPDM_DIR ${PROJECT_SOURCE_DIR}) @@ -985,7 +982,11 @@ else() endif() endif() - if (BUILD_SHARED_LIB STREQUAL "ON") + if ((BUILD_LINUX_SHARED_LIB STREQUAL "ON") AND (CMAKE_SYSTEM_NAME MATCHES "Linux")) + SET(LIB_NAME "spdm") + SET(CMAKE_PROJECT_NAME "libspdm") + SET(CMAKE_PROJECT_DESCRIPTION "libspdm shared library") + if(CRYPTO STREQUAL "mbedtls") SET(CRYPTO_DEPS "-lmbedtls -lmbedx509 -lmbedcrypto") add_library(${LIB_NAME} SHARED diff --git a/libspdm.pc.in b/libspdm.pc.in index f0ec86c4ff5..ae1a89957cf 100644 --- a/libspdm.pc.in +++ b/libspdm.pc.in @@ -1,7 +1,7 @@ prefix=@CMAKE_INSTALL_PREFIX@ exec_prefix=@CMAKE_INSTALL_PREFIX@ libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@/lib -includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@/include/lib@PROJECT_NAME@ +includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@/include/lib@LIB_NAME@ Name: @CMAKE_PROJECT_NAME@ Description: @CMAKE_PROJECT_DESCRIPTION@ @@ -9,4 +9,4 @@ Version: @CMAKE_PROJECT_VERSION@ Requires: Cflags: -I${includedir} -I${includedir}/include -Libs: -L${libdir} -l@PROJECT_NAME@ @CRYPTO_DEPS@ +Libs: -L${libdir} -l@LIB_NAME@ @CRYPTO_DEPS@ From 68ac749f15965521ce2fb3ae7160cbdf0cdb1f02 Mon Sep 17 00:00:00 2001 From: Ionut Ursachi Date: Sun, 29 Oct 2023 07:33:43 +0000 Subject: [PATCH 3/4] When BUILD_LINUX_SHARED_LIB is set to ON, three shared libraries are generated and installed: libspdm.so libspdm_platform.so and libspdm_crypto.so. Added more information about them in build.md documentation. Shared libraries now properly link with their dependencies at runtime. Signed-off-by: Ionut Ursachi --- CMakeLists.txt | 84 +++++++++++++++++++++++++++----------------------- doc/build.md | 21 +++++++++++++ libspdm.pc.in | 6 ++-- 3 files changed, 70 insertions(+), 41 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 999dd30ce1f..266d4e54433 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -987,51 +987,59 @@ else() SET(CMAKE_PROJECT_NAME "libspdm") SET(CMAKE_PROJECT_DESCRIPTION "libspdm shared library") + SET(CMAKE_SKIP_BUILD_RPATH FALSE) + SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) + SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") + SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) + + add_library(${LIB_NAME} SHARED + $ + $ + $ + $ + $ + $ + $ + ) + + add_library(${LIB_NAME}_platform SHARED + $ + $ + $ + $ + $ + ) + + add_library(${LIB_NAME}_crypto SHARED + $ + $ + ) + if(CRYPTO STREQUAL "mbedtls") SET(CRYPTO_DEPS "-lmbedtls -lmbedx509 -lmbedcrypto") - add_library(${LIB_NAME} SHARED - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - ) - + TARGET_LINK_LIBRARIES(${LIB_NAME}_crypto + PUBLIC mbedtls + PUBLIC mbedx509 + PUBLIC mbedcrypto + ) elseif(CRYPTO STREQUAL "openssl") SET(CRYPTO_DEPS "-lssl -lcrypto") - add_library(${LIB_NAME} SHARED - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - ) - endif() + TARGET_LINK_LIBRARIES(${LIB_NAME}_crypto + PUBLIC ssl + PUBLIC crypto + ) + endif() + + TARGET_LINK_LIBRARIES(${LIB_NAME} + PUBLIC ${LIB_NAME}_platform + PUBLIC ${LIB_NAME}_crypto + ) install(DIRECTORY include DESTINATION include/libspdm) configure_file(libspdm.pc.in lib/pkgconfig/libspdm.pc @ONLY) install(FILES ${CMAKE_BINARY_DIR}/lib/pkgconfig/libspdm.pc DESTINATION lib/pkgconfig/) - install(TARGETS ${LIB_NAME} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) + install(TARGETS ${LIB_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) + install(TARGETS ${LIB_NAME}_platform LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) + install(TARGETS ${LIB_NAME}_crypto LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) endif() endif() diff --git a/doc/build.md b/doc/build.md index 2abf2fafcb3..7b4e1923f20 100644 --- a/doc/build.md +++ b/doc/build.md @@ -263,6 +263,27 @@ cmake -DARCH= -DTOOLCHAIN=NONE -DTARGET= -DCRYPTO= Date: Tue, 31 Oct 2023 08:48:33 +0000 Subject: [PATCH 4/4] Included feedback into build.md Removed changes to lib.c from spdm_device_secret_lib_sample Signed-off-by: Ionut Ursachi --- doc/build.md | 6 +++--- os_stub/spdm_device_secret_lib_sample/lib.c | 11 +---------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/doc/build.md b/doc/build.md index 7b4e1923f20..fd67e15b8aa 100644 --- a/doc/build.md +++ b/doc/build.md @@ -269,9 +269,9 @@ Supports shared libraries building and pkg-config '.pc' file generation and inst Will generate: - libspdm.so - main library code, all subprojects from "library" folder - libspdm_platform.so - subprojects in the "os_stub" folder related to platform code, like memory allocation, random number generator, etc. - - libspdm_crypto.so - cryptography related code for libspdm linking to either mbedtls or openssl libraries -All three libraries are required for an application that uses libspdm, but the developer is free to implement their own versions of libspdm_platform or libspdm_crypto libraries and link with their implementations. -Will install pc file and all required headers and shared libraries (except for spdm_device_secret_lib_sample which developer has to implement), so application developers can use 'pkg-config --libs libspdm' and 'pkg-config --cflags libspdm' to link with libspdm + - libspdm_crypto.so - cryptography related code for libspdm to dynamically link to either Mbed TLS or OpenSSL shared libraries. +All three libraries are required for an application that uses libspdm, but the integrator is free to implement their own versions of libspdm_platform or libspdm_crypto libraries and link with their implementations. +Will install pc file and all required headers and shared libraries (except for spdm_device_secret_lib_sample which the integrator has to implement), so application developers can use 'pkg-config --libs libspdm' and 'pkg-config --cflags libspdm' to link with libspdm To build with shared library support: ``` diff --git a/os_stub/spdm_device_secret_lib_sample/lib.c b/os_stub/spdm_device_secret_lib_sample/lib.c index 3ed99e9fb70..2edfce192b4 100644 --- a/os_stub/spdm_device_secret_lib_sample/lib.c +++ b/os_stub/spdm_device_secret_lib_sample/lib.c @@ -1495,15 +1495,6 @@ uint8_t m_libspdm_bin_str0[0x11] = { 0x64, 0x65, 0x72, 0x69, 0x76, 0x65, 0x64, }; -void libspdm_dump_hex_str_sample(const uint8_t *buffer, size_t buffer_size) -{ - size_t index; - - for (index = 0; index < buffer_size; index++) { - printf("%02x", buffer[index]); - } -} - bool libspdm_psk_handshake_secret_hkdf_expand( spdm_version_number_t spdm_version, uint32_t base_hash_algo, @@ -1531,7 +1522,7 @@ bool libspdm_psk_handshake_secret_hkdf_expand( return false; } printf("[PSK]: "); - libspdm_dump_hex_str_sample(psk, psk_size); + libspdm_dump_hex_str(psk, psk_size); printf("\n"); hash_size = libspdm_get_hash_size(base_hash_algo);