diff options
Diffstat (limited to 'TPM2-Plugin')
-rw-r--r-- | TPM2-Plugin/lib/include/hwpluginif.h | 8 | ||||
-rw-r--r-- | TPM2-Plugin/lib/tpm2_plugin_api.c | 65 | ||||
-rw-r--r-- | TPM2-Plugin/test/main.c | 5 |
3 files changed, 75 insertions, 3 deletions
diff --git a/TPM2-Plugin/lib/include/hwpluginif.h b/TPM2-Plugin/lib/include/hwpluginif.h index 0bbafc6..aa40411 100644 --- a/TPM2-Plugin/lib/include/hwpluginif.h +++ b/TPM2-Plugin/lib/include/hwpluginif.h @@ -88,6 +88,14 @@ typedef struct sshsm_hw_plugin_activate_in_info_s { buffer_info_t *buffer_info[MAX_BUFFER_SEGMENTS]; }SSHSM_HW_PLUGIN_ACTIVATE_LOAD_IN_INFO_t; +typedef struct sshsm_hw_plugin_import_public_key_info_s { + unsigned long modulus_size; + unsigned char *modulus; + unsigned long exponent_size; + //unsigned char *exponent; + unsigned int *exponent; +}SSHSM_HW_PLUGIN_IMPORT_PUBLIC_KEY_INFO_t; + typedef int (*sshsm_hw_plugin_activate)( SSHSM_HW_PLUGIN_ACTIVATE_LOAD_IN_INFO_t *activate_in_info ); diff --git a/TPM2-Plugin/lib/tpm2_plugin_api.c b/TPM2-Plugin/lib/tpm2_plugin_api.c index a080980..861c2aa 100644 --- a/TPM2-Plugin/lib/tpm2_plugin_api.c +++ b/TPM2-Plugin/lib/tpm2_plugin_api.c @@ -27,6 +27,8 @@ #ifdef HAVE_TCTI_TABRMD #include <tcti/tcti-tabrmd.h> #endif +#define ARRAY_LEN(x) (sizeof(x)/sizeof(x[0])) + bool output_enabled = true; bool hexPasswd = false; TPM_HANDLE handle2048rsa; @@ -336,10 +338,66 @@ int load_key(TSS2_SYS_CONTEXT *sapi_context, return 0; } +int read_public(TSS2_SYS_CONTEXT *sapi_context, + TPM_HANDLE handle, + SSHSM_HW_PLUGIN_IMPORT_PUBLIC_KEY_INFO_t *importkey_info) +{ + + TPMS_AUTH_RESPONSE session_out_data; + TSS2_SYS_RSP_AUTHS sessions_out_data; + TPMS_AUTH_RESPONSE *session_out_data_array[1]; + + TPM2B_PUBLIC public = { + { 0, } + }; + + TPM2B_NAME name = TPM2B_TYPE_INIT(TPM2B_NAME, name); + + TPM2B_NAME qualified_name = TPM2B_TYPE_INIT(TPM2B_NAME, name); + + session_out_data_array[0] = &session_out_data; + sessions_out_data.rspAuths = &session_out_data_array[0]; + sessions_out_data.rspAuthsCount = ARRAY_LEN(session_out_data_array); + + TPM_RC rval = Tss2_Sys_ReadPublic(sapi_context, handle, 0, + &public, &name, &qualified_name, &sessions_out_data); + if (rval != TPM_RC_SUCCESS) { + printf("TPM2_ReadPublic error: rval = 0x%0x", rval); + return false; + } + + printf("\nTPM2_ReadPublic OutPut: \n"); + printf("name: \n"); + UINT16 i; + for (i = 0; i < name.t.size; i++) + printf("%02x ", name.t.name[i]); + printf("\n"); + + printf("qualified_name: \n"); + for (i = 0; i < qualified_name.t.size; i++) + printf("%02x ", qualified_name.t.name[i]); + printf("\n"); + + printf("public.t.publicArea.parameters.rsaDetail.keyBits = %d \n", public.t.publicArea.parameters.rsaDetail.keyBits); + printf("public.t.publicArea.parameters.rsaDetail.exponent = %d \n", public.t.publicArea.parameters.rsaDetail.exponent); + + importkey_info->modulus_size = public.t.publicArea.unique.rsa.t.size; + printf("importkey_info->modulus_size = %ld \n", importkey_info->modulus_size); + memcpy(importkey_info->modulus, &public.t.publicArea.unique.rsa.t.buffer, importkey_info->modulus_size); + + importkey_info->exponent_size = sizeof(public.t.publicArea.parameters.rsaDetail.exponent); + printf("importkey_info->exponent_size = %ld \n", importkey_info->exponent_size); + memcpy(importkey_info->exponent, &public.t.publicArea.parameters.rsaDetail.exponent, importkey_info->exponent_size); + //*importkey_info->exponent = public.t.publicArea.parameters.rsaDetail.exponent; + + return 0; +} + TPMS_CONTEXT loaded_key_context; int load_key_execute(SSHSM_HW_PLUGIN_ACTIVATE_LOAD_IN_INFO_t *loadkey_in_info, - void **keyHandle, TSS2_SYS_CONTEXT *sapi_context) + void **keyHandle, TSS2_SYS_CONTEXT *sapi_context, + SSHSM_HW_PLUGIN_IMPORT_PUBLIC_KEY_INFO_t *importkey_info) { TPMI_DH_OBJECT parentHandle; @@ -369,6 +427,9 @@ int load_key_execute(SSHSM_HW_PLUGIN_ACTIVATE_LOAD_IN_INFO_t *loadkey_in_info, &inPublic, &inPrivate, 0); + returnVal = read_public(sapi_context, + handle2048rsa, + importkey_info); TPM_RC rval = Tss2_Sys_ContextSave(sapi_context, handle2048rsa, &loaded_key_context); if (rval != TPM_RC_SUCCESS) { @@ -399,7 +460,7 @@ int tpm2_plugin_load_key(SSHSM_HW_PLUGIN_ACTIVATE_LOAD_IN_INFO_t *loadkey_in_inf } } - ret = load_key_execute(loadkey_in_info, keyHandle, sapi_context); + ret = load_key_execute(loadkey_in_info, keyHandle, sapi_context, importkey_info); if (ret !=0) printf("Load key API failed in TPM plugin ! \n"); diff --git a/TPM2-Plugin/test/main.c b/TPM2-Plugin/test/main.c index 9426529..046b3d7 100644 --- a/TPM2-Plugin/test/main.c +++ b/TPM2-Plugin/test/main.c @@ -33,6 +33,9 @@ void main(void) SSHSM_HW_PLUGIN_ACTIVATE_LOAD_IN_INFO_t *activate_in_info; activate_in_info = malloc(sizeof(SSHSM_HW_PLUGIN_ACTIVATE_LOAD_IN_INFO_t)); + SSHSM_HW_PLUGIN_IMPORT_PUBLIC_KEY_INFO_t *importkey_info; + importkey_info = malloc(sizeof(SSHSM_HW_PLUGIN_IMPORT_PUBLIC_KEY_INFO_t)); + SSHSM_HW_PLUGIN_ACTIVATE_LOAD_IN_INFO_t *loadkey_in_info; loadkey_in_info = malloc(sizeof(SSHSM_HW_PLUGIN_ACTIVATE_LOAD_IN_INFO_t)); loadkey_in_info->num_buffers = 2; @@ -60,7 +63,7 @@ void main(void) tpm2_plugin_rsa_sign_init(keyHandle_sign, mechanism, param, len, (void *)hSession); printf("---------------------------------------------\n"); - tpm2_plugin_load_key(loadkey_in_info, keyHandle); + tpm2_plugin_load_key(loadkey_in_info, keyHandle, importkey_info); printf("---------------------------------------------\n"); tpm2_plugin_rsa_sign(keyHandle_sign, mechanism, msg, msg_len, (void *)hSession, sig, sig_len); |