Skip to content

Commit 37e58c7

Browse files
Wenxing-houjyao1
authored andcommitted
Fix memory leak for set raw data key
Signed-off-by: Wenxing Hou <wenxing.hou@intel.com>
1 parent a3016ed commit 37e58c7

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

os_stub/cryptlib_mbedtls/pk/rsa_basic.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ bool libspdm_rsa_set_key(void *rsa_context, const libspdm_rsa_key_tag_t key_tag,
9797
if (big_number) {
9898
ret = mbedtls_mpi_read_binary(&value, big_number, bn_size);
9999
if (ret != 0) {
100+
mbedtls_mpi_free(&value);
100101
return false;
101102
}
102103
}
@@ -130,6 +131,7 @@ bool libspdm_rsa_set_key(void *rsa_context, const libspdm_rsa_key_tag_t key_tag,
130131
break;
131132
}
132133

134+
mbedtls_mpi_free(&value);
133135
return ret == 0;
134136
}
135137
#endif /* (LIBSPDM_RSA_SSA_SUPPORT) || (LIBSPDM_RSA_PSS_SUPPORT) */

os_stub/spdm_device_secret_lib_sample/lib.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "spdm_device_secret_lib_internal.h"
3030

3131
#ifndef LIBSPDM_PRIVATE_KEY_USE_PEM
32-
#define LIBSPDM_PRIVATE_KEY_USE_PEM 0
32+
#define LIBSPDM_PRIVATE_KEY_USE_PEM 1
3333
#endif
3434

3535
#if !LIBSPDM_PRIVATE_KEY_USE_PEM
@@ -538,6 +538,7 @@ bool libspdm_get_responder_private_key_from_raw_data(uint32_t base_asym_algo, vo
538538
#endif /*LIBSPDM_ECDSA_SUPPORT*/
539539

540540
switch (base_asym_algo) {
541+
#if (LIBSPDM_RSA_SSA_SUPPORT) || (LIBSPDM_RSA_PSS_SUPPORT)
541542
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048:
542543
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_2048:
543544
rsa_n = m_libspdm_rsa2048_res_n;
@@ -565,6 +566,9 @@ bool libspdm_get_responder_private_key_from_raw_data(uint32_t base_asym_algo, vo
565566
rsa_e_size = sizeof(m_libspdm_rsa4096_res_e);
566567
rsa_d_size = sizeof(m_libspdm_rsa4096_res_d);
567568
break;
569+
#endif /* (LIBSPDM_RSA_SSA_SUPPORT) || (LIBSPDM_RSA_PSS_SUPPORT) */
570+
571+
#if LIBSPDM_ECDSA_SUPPORT
568572
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P256:
569573
ec_nid = LIBSPDM_CRYPTO_NID_ECDSA_NIST_P256;
570574
ec_public = m_libspdm_ec256_responder_public_key;
@@ -586,6 +590,7 @@ bool libspdm_get_responder_private_key_from_raw_data(uint32_t base_asym_algo, vo
586590
ec_public_size = sizeof(m_libspdm_ec521_responder_public_key);
587591
ec_private_size = sizeof(m_libspdm_ec521_responder_private_key);
588592
break;
593+
#endif /*LIBSPDM_ECDSA_SUPPORT*/
589594
default:
590595
LIBSPDM_ASSERT(false);
591596
return false;
@@ -677,6 +682,7 @@ bool libspdm_get_requester_private_key_from_raw_data(uint32_t base_asym_algo, vo
677682
#endif /*LIBSPDM_ECDSA_SUPPORT*/
678683

679684
switch (base_asym_algo) {
685+
#if (LIBSPDM_RSA_SSA_SUPPORT) || (LIBSPDM_RSA_PSS_SUPPORT)
680686
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048:
681687
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_2048:
682688
rsa_n = m_libspdm_rsa2048_req_n;
@@ -704,6 +710,9 @@ bool libspdm_get_requester_private_key_from_raw_data(uint32_t base_asym_algo, vo
704710
rsa_e_size = sizeof(m_libspdm_rsa4096_req_e);
705711
rsa_d_size = sizeof(m_libspdm_rsa4096_req_d);
706712
break;
713+
#endif /* (LIBSPDM_RSA_SSA_SUPPORT) || (LIBSPDM_RSA_PSS_SUPPORT) */
714+
715+
#if LIBSPDM_ECDSA_SUPPORT
707716
case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P256:
708717
ec_nid = LIBSPDM_CRYPTO_NID_ECDSA_NIST_P256;
709718
ec_public = m_libspdm_ec256_requester_public_key;
@@ -725,6 +734,7 @@ bool libspdm_get_requester_private_key_from_raw_data(uint32_t base_asym_algo, vo
725734
ec_public_size = sizeof(m_libspdm_ec521_requester_public_key);
726735
ec_private_size = sizeof(m_libspdm_ec521_requester_private_key);
727736
break;
737+
#endif /*LIBSPDM_ECDSA_SUPPORT*/
728738
default:
729739
LIBSPDM_ASSERT(false);
730740
return false;

0 commit comments

Comments
 (0)