-
Notifications
You must be signed in to change notification settings - Fork 133
[WIP] Add initial TPM2 support to libspdm #3285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
itsManjeet
wants to merge
1
commit into
DMTF:main
Choose a base branch
from
itsManjeet:tpm-device-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| cmake_minimum_required(VERSION 3.5) | ||
|
|
||
| add_library(spdm_device_secret_lib_tpm STATIC "") | ||
|
|
||
| target_include_directories(spdm_device_secret_lib_tpm | ||
| PRIVATE | ||
| ${LIBSPDM_DIR}/os_stub/spdm_device_secret_lib_tpm | ||
| ${LIBSPDM_DIR}/include | ||
| ${LIBSPDM_DIR}/include/hal | ||
| ${LIBSPDM_DIR}/os_stub | ||
| ) | ||
|
|
||
| target_sources(spdm_device_secret_lib_tpm | ||
| PRIVATE | ||
| lib.c | ||
| sign.c | ||
| chal.c | ||
| measurment.c | ||
| key_pair.c | ||
|
|
||
| crypto_stub.c | ||
|
|
||
| ../spdm_device_secret_lib_sample/read_priv_key_pem.c | ||
| ../spdm_device_secret_lib_sample/read_priv_key_pem_pqc.c | ||
| ../spdm_device_secret_lib_sample/read_priv_key_raw_data.c | ||
| ../spdm_device_secret_lib_sample/read_priv_key_raw_data_pqc.c | ||
| ../spdm_device_secret_lib_sample/read_pub_cert.c | ||
| ../spdm_device_secret_lib_sample/read_pub_cert_pqc.c | ||
| ../spdm_device_secret_lib_sample/read_pub_key_der.c | ||
| ../spdm_device_secret_lib_sample/read_pub_key_der_pqc.c | ||
| ../spdm_device_secret_lib_sample/read_special_cert.c | ||
| # ../spdm_device_secret_lib_sample/set_cert.c | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| #include "spdm_device_secret_lib_internal.h" | ||
| #include "crypto_stub_internal.h" | ||
|
|
||
|
|
||
| #if LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP | ||
| size_t libspdm_secret_lib_challenge_opaque_data_size; | ||
| bool libspdm_challenge_opaque_data( | ||
| void *spdm_context, | ||
| spdm_version_number_t spdm_version, | ||
| uint8_t slot_id, | ||
| uint8_t *measurement_summary_hash, | ||
| size_t measurement_summary_hash_size, | ||
| void *opaque_data, | ||
| size_t *opaque_data_size) | ||
| { | ||
| size_t index; | ||
|
|
||
| LIBSPDM_ASSERT(libspdm_secret_lib_challenge_opaque_data_size <= *opaque_data_size); | ||
|
|
||
| *opaque_data_size = libspdm_secret_lib_challenge_opaque_data_size; | ||
|
|
||
| for (index = 0; index < *opaque_data_size; index++) | ||
| { | ||
| ((uint8_t *)opaque_data)[index] = (uint8_t)index; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| #endif /* LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP */ | ||
|
|
||
| #if LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP | ||
| bool libspdm_encap_challenge_opaque_data( | ||
| void *spdm_context, | ||
| spdm_version_number_t spdm_version, | ||
| uint8_t slot_id, | ||
| uint8_t *measurement_summary_hash, | ||
| size_t measurement_summary_hash_size, | ||
| void *opaque_data, | ||
| size_t *opaque_data_size) | ||
| { | ||
| size_t index; | ||
|
|
||
| LIBSPDM_ASSERT(libspdm_secret_lib_challenge_opaque_data_size <= *opaque_data_size); | ||
|
|
||
| *opaque_data_size = libspdm_secret_lib_challenge_opaque_data_size; | ||
|
|
||
| for (index = 0; index < *opaque_data_size; index++) | ||
| { | ||
| ((uint8_t *)opaque_data)[index] = (uint8_t)index; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| #endif /* LIBSPDM_ENABLE_CAPABILITY_CHAL_CAP */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * TODO: APIs in this file need to be the part of crypto libraries | ||
| */ | ||
|
|
||
| #include <dlfcn.h> | ||
|
|
||
| #include "crypto_stub_internal.h" | ||
|
|
||
|
|
||
| bool g_tpm_device_initialized = false; | ||
| OSSL_LIB_CTX* g_libctx = NULL; | ||
|
|
||
| void libspdm_tpm_device_init() { | ||
| if (g_tpm_device_initialized) | ||
| return; | ||
|
|
||
| g_libctx = OSSL_LIB_CTX_new(); | ||
|
|
||
| void *handle = dlopen("tpm2.so", RTLD_GLOBAL | RTLD_NOW); | ||
| if (handle == NULL){ | ||
| fprintf(stderr, "dlopen: %s\n", dlerror()); | ||
| exit(EXIT_FAILURE); | ||
| } | ||
|
|
||
| OSSL_provider_init_fn *fun = (OSSL_provider_init_fn *)dlsym(handle, "OSSL_provider_init"); | ||
| if (fun == NULL){ | ||
| fprintf(stderr, "dlsym: %s\n", dlerror()); | ||
| exit(EXIT_FAILURE); | ||
| } | ||
|
|
||
| if (OSSL_PROVIDER_add_builtin(g_libctx, "tpm2", fun) <= 0){ | ||
| fprintf(stderr, "ERROR: failed to add builtin\n"); | ||
| exit(EXIT_FAILURE); | ||
| } | ||
|
|
||
| OSSL_PROVIDER *tpm_provider = NULL; | ||
|
|
||
| if ((tpm_provider = OSSL_PROVIDER_load(g_libctx, "tpm2")) == NULL){ | ||
| fprintf(stderr, "ERROR: failed to load tpm2\n"); | ||
| exit(EXIT_FAILURE); | ||
| } | ||
|
|
||
| fprintf(stdout, "SELF TEST %d\n", OSSL_PROVIDER_self_test(tpm_provider)); | ||
|
|
||
| fprintf(stdout, "************************************\n" | ||
| " Loaded tpm2 module successfully\n"); | ||
| fprintf(stdout, "TPM2: %p\n", (void *)tpm_provider); | ||
|
|
||
| g_tpm_device_initialized = true; | ||
| } | ||
|
|
||
|
|
||
| bool libspdm_read_private_key_from_tpm(const char *handle, void **context) | ||
| { | ||
| OSSL_STORE_CTX *store_ctx = NULL; | ||
| OSSL_STORE_INFO *info = NULL; | ||
| EVP_PKEY *pkey = NULL; | ||
|
|
||
| fprintf(stdout, "IS TPM AVAIABLE %d\n", OSSL_PROVIDER_available(g_libctx, "tpm2")); | ||
|
|
||
| /* handle must look like: "tpm2tss:0x81010002" */ | ||
| store_ctx = OSSL_STORE_open_ex("handle:0x81010003", g_libctx, "provider=tpm2", NULL, NULL, NULL, NULL, NULL); | ||
| if (!store_ctx){ | ||
| return false; | ||
| } | ||
|
|
||
| while ((info = OSSL_STORE_load(store_ctx)) != NULL) | ||
| { | ||
| if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PKEY){ | ||
| pkey = OSSL_STORE_INFO_get1_PKEY(info); | ||
| OSSL_STORE_INFO_free(info); | ||
| break; | ||
| } | ||
| OSSL_STORE_INFO_free(info); | ||
| } | ||
|
|
||
| OSSL_STORE_close(store_ctx); | ||
|
|
||
| if (pkey == NULL){ | ||
| fprintf(stderr, "no private key found in tpm handle %s\n", handle); | ||
| return false; | ||
| } | ||
|
|
||
| *context = pkey; | ||
| return true; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #ifndef __CRYPTO_SUB_INTERNAL_H__ | ||
| #define __CRYPTO_SUB_INTERNAL_H__ | ||
|
|
||
| #include <stdbool.h> | ||
|
|
||
| #include "openssllib/openssl/include/openssl/provider.h" | ||
| #include "openssllib/openssl/include/openssl/store.h" | ||
|
|
||
| void libspdm_tpm_device_init(); | ||
|
|
||
| bool libspdm_read_private_key_from_tpm(const char *handle, void **context); | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #include "spdm_device_secret_lib_internal.h" | ||
| #include "crypto_stub_internal.h" | ||
|
|
||
|
|
||
| #if LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP | ||
|
|
||
| /** | ||
| * read the key pair info of the key_pair_id. | ||
| * | ||
| * @param spdm_context A pointer to the SPDM context. | ||
| * @param key_pair_id Indicate which key pair ID's information to retrieve. | ||
| * | ||
| * @param capabilities Indicate the capabilities of the requested key pairs. | ||
| * @param key_usage_capabilities Indicate the key usages the responder allows. | ||
| * @param current_key_usage Indicate the currently configured key usage for the requested key pairs ID. | ||
| * @param asym_algo_capabilities Indicate the asymmetric algorithms the Responder supports for this key pair ID. | ||
| * @param current_asym_algo Indicate the currently configured asymmetric algorithm for this key pair ID. | ||
| * @param assoc_cert_slot_mask This field is a bit mask representing the currently associated certificate slots. | ||
| * @param public_key_info_len On input, indicate the size in bytes of the destination buffer to store. | ||
| * On output, indicate the size in bytes of the public_key_info. | ||
| * It can be NULL, if public_key_info is not required. | ||
| * @param public_key_info A pointer to a destination buffer to store the public_key_info. | ||
| * It can be NULL, if public_key_info is not required. | ||
| * | ||
| * @retval true get key pair info successfully. | ||
| * @retval false get key pair info failed. | ||
| **/ | ||
| bool libspdm_read_key_pair_info( | ||
| void *spdm_context, | ||
| uint8_t key_pair_id, | ||
| uint16_t *capabilities, | ||
| uint16_t *key_usage_capabilities, | ||
| uint16_t *current_key_usage, | ||
| uint32_t *asym_algo_capabilities, | ||
| uint32_t *current_asym_algo, | ||
| uint32_t *pqc_asym_algo_capabilities, | ||
| uint32_t *current_pqc_asym_algo, | ||
| uint8_t *assoc_cert_slot_mask, | ||
| uint16_t *public_key_info_len, | ||
| uint8_t *public_key_info) | ||
| { | ||
| return false; | ||
| } | ||
| #endif /* LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP */ | ||
|
|
||
| #if LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP | ||
| bool libspdm_write_key_pair_info( | ||
| void *spdm_context, | ||
| uint8_t key_pair_id, | ||
| uint8_t operation, | ||
| uint16_t desired_key_usage, | ||
| uint32_t desired_asym_algo, | ||
| uint32_t desired_pqc_asym_algo, | ||
| uint8_t desired_assoc_cert_slot_mask, | ||
| bool *need_reset) | ||
| { | ||
| return false; | ||
| } | ||
| #endif /* #if LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP */ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add license header.