Skip to content

Commit b6df96f

Browse files
committed
[WIP]: TPM Support
Signed-off-by: Manjeet Singh <[email protected]>
1 parent c5e7386 commit b6df96f

File tree

61 files changed

+5575
-262
lines changed

Some content is hidden

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

61 files changed

+5575
-262
lines changed

CMakeLists.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ set(GCOV ${GCOV} CACHE STRING "Choose the target of Gcov: ON OFF, and default i
3636
set(STACK_USAGE ${STACK_USAGE} CACHE STRING "Choose the target of STACK_USAGE: ON OFF, and default is OFF" FORCE)
3737
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)
3838
set(X509_IGNORE_CRITICAL ${X509_IGNORE_CRITICAL} CACHE STRING "Choose if libspdm-provided cryptography libraries (OpenSSL and MbedTLS) ignore unsupported critical extensions in certificates : ON OFF, and default is OFF" FORCE)
39+
set(DEVICE ${DEVICE} CACHE STRING "Choose the test device: sample tpm, and default is sample" FORCE)
3940

4041
if(NOT GCOV)
4142
set(GCOV "OFF")
@@ -53,6 +54,11 @@ if(NOT X509_IGNORE_CRITICAL)
5354
set(X509_IGNORE_CRITICAL "OFF")
5455
endif()
5556

57+
if (NOT DEVICE)
58+
set(DEVICE "sample")
59+
endif()
60+
61+
5662
set(LIBSPDM_DIR ${PROJECT_SOURCE_DIR})
5763

5864
#
@@ -62,6 +68,7 @@ set(COMPILED_LIBCRYPTO_PATH ${COMPILED_LIBCRYPTO_PATH} CACHE STRING "Optionally
6268
set(COMPILED_LIBSSL_PATH ${COMPILED_LIBSSL_PATH} CACHE STRING "Optionally provide a path to libssl" FORCE)
6369

6470
message("CMAKE_GENERATOR = ${CMAKE_GENERATOR}")
71+
message("DEVICE = ${DEVICE}")
6572

6673
if(ARCH STREQUAL "x64")
6774
message("ARCH = x64")
@@ -981,7 +988,7 @@ if(ENABLE_CODEQL STREQUAL "ON")
981988
add_subdirectory(os_stub/platform_lib)
982989
add_subdirectory(os_stub/platform_lib_null)
983990
add_subdirectory(os_stub/malloclib)
984-
add_subdirectory(os_stub/spdm_device_secret_lib_sample)
991+
add_subdirectory(os_stub/spdm_device_secret_lib_${DEVICE})
985992
add_subdirectory(os_stub/spdm_device_secret_lib_null)
986993
add_subdirectory(os_stub/spdm_cert_verify_callback_sample)
987994
add_subdirectory(os_stub/cryptlib_null)
@@ -1020,7 +1027,7 @@ else()
10201027
add_subdirectory(os_stub/platform_lib)
10211028
add_subdirectory(os_stub/platform_lib_null)
10221029
add_subdirectory(os_stub/malloclib)
1023-
add_subdirectory(os_stub/spdm_device_secret_lib_sample)
1030+
add_subdirectory(os_stub/spdm_device_secret_lib_${DEVICE})
10241031
add_subdirectory(os_stub/spdm_device_secret_lib_null)
10251032
add_subdirectory(os_stub/spdm_cert_verify_callback_sample)
10261033

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright Notice:
3+
* Copyright 2021-2025 DMTF. All rights reserved.
4+
* License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
5+
**/
6+
7+
#ifndef __CRYPTLIB_TPM_H__
8+
#define __CRYPTLIB_TPM_H__
9+
10+
#include <stdbool.h>
11+
12+
bool libspdm_tpm_device_init();
13+
14+
bool libspdm_tpm_get_private_key(void *handle, void **context);
15+
16+
bool libspdm_tpm_get_public_key(void *handle, void **context);
17+
18+
bool libspdm_tpm_get_certificate(void *handle, void **context);
19+
20+
bool libspdm_tpm_dump_certificate(void *context, void **buffer, size_t *size);
21+
22+
bool libspdm_tpm_read_pcr(uint32_t hash_algo, uint32_t index, void *buffer, size_t *size);
23+
24+
#endif

os_stub/cryptlib_openssl/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,10 @@ target_sources(cryptlib_openssl
5252
target_compile_options(cryptlib_openssl PRIVATE ${OPENSSL_FLAGS})
5353

5454
target_link_libraries(cryptlib_openssl PUBLIC openssllib memlib)
55+
56+
if (${DEVICE} STREQUAL "tpm")
57+
target_sources(cryptlib_openssl PRIVATE
58+
tpm/tpm.c
59+
)
60+
target_link_libraries(cryptlib_openssl PUBLIC tss2-esys tss2-tctildr)
61+
endif()

os_stub/cryptlib_openssl/pk/ec.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,13 @@ bool libspdm_ecdsa_sign(void *ec_context, size_t hash_nid,
681681
return false;
682682
}
683683

684+
char buffer[4096];
685+
BIO *bio = BIO_new(BIO_s_mem());
686+
EVP_PKEY_print_public(bio, evp_pkey, 4, NULL);
687+
int len = BIO_read(bio, (void*) buffer, sizeof(buffer));
688+
buffer[len] = '\0';
689+
printf("SIGN PUBLIC KEY: %s\n", buffer);
690+
684691
half_size = evp_pkey_get_half_size(evp_pkey);
685692
if (*sig_size < (size_t)(half_size * 2)) {
686693
*sig_size = half_size * 2;
@@ -828,6 +835,13 @@ bool libspdm_ecdsa_verify(void *ec_context, size_t hash_nid,
828835
return false;
829836
}
830837

838+
char buffer[4096];
839+
BIO *bio = BIO_new(BIO_s_mem());
840+
EVP_PKEY_print_public(bio, evp_pkey, 4, NULL);
841+
int len = BIO_read(bio, (void*) buffer, sizeof(buffer));
842+
buffer[len] = '\0';
843+
printf("VERIFY PUBLIC KEY: %s\n", buffer);
844+
831845
half_size = evp_pkey_get_half_size(evp_pkey);
832846
if (sig_size != (size_t)(half_size * 2)) {
833847
return false;

os_stub/cryptlib_openssl/pk/x509.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@
3535
static const uint8_t m_libspdm_oid_ext_key_usage[] = OID_EXT_KEY_USAGE;
3636
static const uint8_t m_libspdm_oid_basic_constraints[] = OID_BASIC_CONSTRAINTS;
3737

38+
static void dump_hex(const char* id, const unsigned char *buf, long buflen)
39+
{
40+
char buffer[4096];
41+
const unsigned char *p = buf;
42+
X509 *cert = d2i_X509(NULL, &p, buflen);
43+
if (!cert) {
44+
printf("Not an X.509 cert inside this ASN.1 object.\n");
45+
return;
46+
}
47+
48+
/* Print certificate */
49+
BIO *bio = BIO_new(BIO_s_mem());
50+
X509_print(bio, cert);
51+
int s = BIO_read(bio, (void*) buffer, sizeof(buffer));
52+
buffer[s] = '\0';
53+
printf("%s CERT: %s\n", id, buffer);
54+
X509_free(cert);
55+
}
56+
57+
//
3858
/**
3959
* Construct a X509 object from DER-encoded certificate data.
4060
*
@@ -2037,6 +2057,8 @@ bool libspdm_x509_verify_cert_chain(const uint8_t *root_cert, size_t root_cert_l
20372057

20382058
/* Verify current_cert with preceding cert;*/
20392059

2060+
dump_hex("CURRENT", current_cert, current_cert_len);
2061+
dump_hex("PRECEDING", preceding_cert, preceding_cert_len);
20402062
verify_flag =
20412063
libspdm_x509_verify_cert(current_cert, current_cert_len,
20422064
preceding_cert, preceding_cert_len);
@@ -2458,6 +2480,7 @@ bool libspdm_gen_x509_csr_with_pqc(
24582480
X509_NAME *x509_name;
24592481
EVP_PKEY *private_key;
24602482
EVP_PKEY *public_key;
2483+
bool owned_keys = false;
24612484
EVP_MD *md;
24622485
uint8_t *csr_p;
24632486
STACK_OF(X509_EXTENSION) *exts;
@@ -2536,11 +2559,13 @@ bool libspdm_gen_x509_csr_with_pqc(
25362559
EVP_PKEY_free(private_key);
25372560
EVP_PKEY_free(public_key);
25382561

2539-
private_key = EVP_PKEY_dup(ec_pkey);
2540-
public_key = EVP_PKEY_dup(ec_pkey);
2562+
// Can't DUP hardware backed keys
2563+
private_key = ec_pkey;
2564+
public_key = ec_pkey;
25412565
if (private_key == NULL || public_key == NULL) {
25422566
goto free_all;
25432567
}
2568+
owned_keys = true;
25442569
break;
25452570
}
25462571
case LIBSPDM_CRYPTO_NID_SM2_DSA_P256: {
@@ -2763,8 +2788,10 @@ bool libspdm_gen_x509_csr_with_pqc(
27632788
EVP_MD_free((EVP_MD *)md);
27642789
}
27652790
X509_REQ_free(x509_req);
2766-
EVP_PKEY_free(private_key);
2767-
EVP_PKEY_free(public_key);
2791+
if (!owned_keys) {
2792+
EVP_PKEY_free(private_key);
2793+
EVP_PKEY_free(public_key);
2794+
}
27682795

27692796
return (ret != 0);
27702797
}

0 commit comments

Comments
 (0)