Add TBS context that persists across commands

pull/127/head
Elms 2020-10-12 15:41:59 -07:00
parent a1996ff57b
commit 3f79e7adfb
4 changed files with 39 additions and 11 deletions

View File

@ -39,12 +39,16 @@ static volatile int gWolfCryptRefCount = 0;
#ifdef WOLFTPM_LINUX_DEV #ifdef WOLFTPM_LINUX_DEV
#define INTERNAL_SEND_COMMAND TPM2_LINUX_SendCommand #define INTERNAL_SEND_COMMAND TPM2_LINUX_SendCommand
#define TPM2_INTERNAL_CLEANUP(ctx)
#elif defined(WOLFTPM_SWTPM) #elif defined(WOLFTPM_SWTPM)
#define INTERNAL_SEND_COMMAND TPM2_SWTPM_SendCommand #define INTERNAL_SEND_COMMAND TPM2_SWTPM_SendCommand
#define TPM2_INTERNAL_CLEANUP(ctx)
#elif defined(WOLFTPM_WINAPI) #elif defined(WOLFTPM_WINAPI)
#define INTERNAL_SEND_COMMAND TPM2_WinApi_SendCommand #define INTERNAL_SEND_COMMAND TPM2_WinApi_SendCommand
#define TPM2_INTERNAL_CLEANUP(ctx) TPM2_WinApi_Cleanup(ctx)
#else #else
#define INTERNAL_SEND_COMMAND TPM2_TIS_SendCommand #define INTERNAL_SEND_COMMAND TPM2_TIS_SendCommand
#define TPM2_INTERNAL_CLEANUP(ctx)
#endif #endif
/******************************************************************************/ /******************************************************************************/
@ -387,6 +391,7 @@ TPM_RC TPM2_Cleanup(TPM2_CTX* ctx)
if (rc == TPM_RC_SUCCESS) { if (rc == TPM_RC_SUCCESS) {
if (TPM2_GetActiveCtx() == ctx) { if (TPM2_GetActiveCtx() == ctx) {
TPM2_INTERNAL_CLEANUP(ctx);
/* set non-active */ /* set non-active */
TPM2_SetActiveCtx(NULL); TPM2_SetActiveCtx(NULL);
} }

View File

@ -60,7 +60,6 @@ typedef const TBS_CONTEXT_PARAMS2 *PCTBS_CONTEXT_PARAMS2;
/* Talk to a TPM device using Windows TBS */ /* Talk to a TPM device using Windows TBS */
int TPM2_WinApi_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet) int TPM2_WinApi_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
{ {
TBS_HCONTEXT tbs_context;
TBS_CONTEXT_PARAMS2 tbs_params; TBS_CONTEXT_PARAMS2 tbs_params;
tbs_params.version = TBS_CONTEXT_VERSION_TWO; tbs_params.version = TBS_CONTEXT_VERSION_TWO;
tbs_params.includeTpm12 = 0; tbs_params.includeTpm12 = 0;
@ -68,12 +67,11 @@ int TPM2_WinApi_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
int rc = 0; int rc = 0;
(void)ctx; /* open, if not already open */
/* open on first transmit */ if (ctx->winCtx.tbs_context == NULL) {
if (rc == 0) {
rc = Tbsi_Context_Create((TBS_CONTEXT_PARAMS*)&tbs_params, rc = Tbsi_Context_Create((TBS_CONTEXT_PARAMS*)&tbs_params,
&tbs_context); &ctx->winCtx.tbs_context);
printf("create rc: %d\n", rc); printf("create rc: %d\n", rc);
} }
@ -82,7 +80,7 @@ int TPM2_WinApi_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
uint32_t tmp = packet->size; uint32_t tmp = packet->size;
printf("tx:\n"); printf("tx:\n");
TPM2_PrintBin(packet->buf, packet->pos); TPM2_PrintBin(packet->buf, packet->pos);
rc = Tbsip_Submit_Command(tbs_context, rc = Tbsip_Submit_Command(ctx->winCtx.tbs_context,
TBS_COMMAND_LOCALITY_ZERO, TBS_COMMAND_LOCALITY_ZERO,
TBS_COMMAND_PRIORITY_NORMAL, TBS_COMMAND_PRIORITY_NORMAL,
packet->buf, packet->buf,
@ -96,7 +94,17 @@ int TPM2_WinApi_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
if (rc == 0) { if (rc == 0) {
printf("rx:\n"); printf("rx:\n");
TPM2_PrintBin(packet->buf, packet->pos); TPM2_PrintBin(packet->buf, packet->pos);
rc = Tbsip_Context_Close(tbs_context); }
return rc;
}
int TPM2_WinApi_Cleanup(TPM2_CTX* ctx)
{
int rc = TPM_RC_SUCCESS;
if (ctx->winCtx.tbs_context != NULL) {
rc = Tbsip_Context_Close(ctx->winCtx.tbs_context);
ctx->winCtx.tbs_context = NULL;
printf("close rc: %d\n", rc); printf("close rc: %d\n", rc);
} }

View File

@ -1613,12 +1613,21 @@ static const BYTE TPM_20_EK_AUTH_POLICY[] = {
/* HAL IO Callbacks */ /* HAL IO Callbacks */
struct TPM2_CTX; struct TPM2_CTX;
#ifdef WOLFTPM_SWTPM #ifdef WOLFTPM_SWTPM
struct wolfTPM_tcpContext { struct wolfTPM_tcpContext {
int fd; int fd;
}; };
#endif /* WOLFTPM_SWTPM */ #endif /* WOLFTPM_SWTPM */
#ifdef WOLFTPM_WINAPI
#include <tbs.h>
struct wolfTPM_winContext {
TBS_HCONTEXT tbs_context;
};
#endif /* WOLFTPM_SWTPM */
/* make sure advanced IO is enabled for I2C */ /* make sure advanced IO is enabled for I2C */
#ifdef WOLFTPM_I2C #ifdef WOLFTPM_I2C
#undef WOLFTPM_ADV_IO #undef WOLFTPM_ADV_IO
@ -1644,6 +1653,9 @@ typedef struct TPM2_CTX {
#ifdef WOLFTPM_SWTPM #ifdef WOLFTPM_SWTPM
struct wolfTPM_tcpContext tcpCtx; struct wolfTPM_tcpContext tcpCtx;
#endif #endif
#ifdef WOLFTPM_WINAPI
struct wolfTPM_winContext winCtx;
#endif
#ifndef WOLFTPM2_NO_WOLFCRYPT #ifndef WOLFTPM2_NO_WOLFCRYPT
#ifndef SINGLE_THREADED #ifndef SINGLE_THREADED
wolfSSL_Mutex hwLock; wolfSSL_Mutex hwLock;

View File

@ -32,6 +32,9 @@
/* TPM2 IO for using TPM through the Winapi kernel driver */ /* TPM2 IO for using TPM through the Winapi kernel driver */
WOLFTPM_LOCAL int TPM2_WinApi_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet); WOLFTPM_LOCAL int TPM2_WinApi_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet);
/* Cleanup winpi context */
WOLFTPM_LOCAL int TPM2_WinApi_Cleanup(TPM2_CTX* ctx);
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */
#endif #endif