Skip to content

Commit e48e3a9

Browse files
committed
[WIP]: TPM Support
1 parent 703651f commit e48e3a9

File tree

70 files changed

+6457
-256
lines changed

Some content is hidden

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

70 files changed

+6457
-256
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
#endif

library/spdm_crypt_lib/libspdm_crypt_cert.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
**/
66

77
#include "internal/libspdm_crypt_lib.h"
8+
#include <openssl/x509.h>
89

910
#if LIBSPDM_CERT_PARSE_SUPPORT
1011

@@ -180,6 +181,25 @@ typedef bool (*libspdm_asym_get_public_key_from_x509_func)(const uint8_t *cert,
180181
size_t cert_size,
181182
void **context);
182183

184+
static void dump_hex(const char* id, const unsigned char *buf, long buflen)
185+
{
186+
char buffer[4096];
187+
const unsigned char *p = buf;
188+
X509 *cert = d2i_X509(NULL, &p, buflen);
189+
if (!cert) {
190+
printf("Not an X.509 cert inside this ASN.1 object.\n");
191+
return;
192+
}
193+
194+
/* Print certificate */
195+
BIO *bio = BIO_new(BIO_s_mem());
196+
X509_print(bio, cert);
197+
int s = BIO_read(bio, (void*) buffer, sizeof(buffer));
198+
buffer[s] = '\0';
199+
printf("%s CERT: %s\n", id, buffer);
200+
X509_free(cert);
201+
}
202+
183203
/**
184204
* Return asymmetric GET_PUBLIC_KEY_FROM_X509 function, based upon the negotiated asymmetric algorithm.
185205
*
@@ -2143,6 +2163,8 @@ bool libspdm_verify_cert_chain_data_with_pqc(
21432163
return false;
21442164
}
21452165

2166+
dump_hex("ROOT", root_cert_buffer, root_cert_buffer_size);
2167+
21462168
if (!libspdm_x509_verify_cert_chain(root_cert_buffer, root_cert_buffer_size,
21472169
cert_chain_data, cert_chain_data_size)) {
21482170
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
@@ -2158,6 +2180,8 @@ bool libspdm_verify_cert_chain_data_with_pqc(
21582180
return false;
21592181
}
21602182

2183+
dump_hex("LEAF", leaf_cert_buffer, leaf_cert_buffer_size);
2184+
21612185
if (!libspdm_x509_certificate_check_with_pqc(leaf_cert_buffer, leaf_cert_buffer_size,
21622186
base_asym_algo, pqc_asym_algo, base_hash_algo,
21632187
is_requester_cert, is_device_cert_model)) {

os_stub/cryptlib_openssl/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ target_sources(cryptlib_openssl
4747
pk/x509_pqc.c
4848
rand/rand.c
4949
sys_call/crt_wrapper_host.c
50+
tpm/tpm.c
5051
)
5152

5253
target_compile_options(cryptlib_openssl PRIVATE ${OPENSSL_FLAGS})

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;

0 commit comments

Comments
 (0)