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
#define INTERNAL_SEND_COMMAND TPM2_LINUX_SendCommand
#define TPM2_INTERNAL_CLEANUP(ctx)
#elif defined(WOLFTPM_SWTPM)
#define INTERNAL_SEND_COMMAND TPM2_SWTPM_SendCommand
#define TPM2_INTERNAL_CLEANUP(ctx)
#elif defined(WOLFTPM_WINAPI)
#define INTERNAL_SEND_COMMAND TPM2_WinApi_SendCommand
#define TPM2_INTERNAL_CLEANUP(ctx) TPM2_WinApi_Cleanup(ctx)
#else
#define INTERNAL_SEND_COMMAND TPM2_TIS_SendCommand
#define TPM2_INTERNAL_CLEANUP(ctx)
#endif
/******************************************************************************/
@ -387,6 +391,7 @@ TPM_RC TPM2_Cleanup(TPM2_CTX* ctx)
if (rc == TPM_RC_SUCCESS) {
if (TPM2_GetActiveCtx() == ctx) {
TPM2_INTERNAL_CLEANUP(ctx);
/* set non-active */
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 */
int TPM2_WinApi_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
{
TBS_HCONTEXT tbs_context;
TBS_CONTEXT_PARAMS2 tbs_params;
tbs_params.version = TBS_CONTEXT_VERSION_TWO;
tbs_params.includeTpm12 = 0;
@ -68,12 +67,11 @@ int TPM2_WinApi_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
int rc = 0;
(void)ctx;
/* open, if not already open */
/* open on first transmit */
if (rc == 0) {
if (ctx->winCtx.tbs_context == NULL) {
rc = Tbsi_Context_Create((TBS_CONTEXT_PARAMS*)&tbs_params,
&tbs_context);
&ctx->winCtx.tbs_context);
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;
printf("tx:\n");
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_PRIORITY_NORMAL,
packet->buf,
@ -96,7 +94,17 @@ int TPM2_WinApi_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
if (rc == 0) {
printf("rx:\n");
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);
}

View File

@ -1613,12 +1613,21 @@ static const BYTE TPM_20_EK_AUTH_POLICY[] = {
/* HAL IO Callbacks */
struct TPM2_CTX;
#ifdef WOLFTPM_SWTPM
struct wolfTPM_tcpContext {
int fd;
};
#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 */
#ifdef WOLFTPM_I2C
#undef WOLFTPM_ADV_IO
@ -1644,6 +1653,9 @@ typedef struct TPM2_CTX {
#ifdef WOLFTPM_SWTPM
struct wolfTPM_tcpContext tcpCtx;
#endif
#ifdef WOLFTPM_WINAPI
struct wolfTPM_winContext winCtx;
#endif
#ifndef WOLFTPM2_NO_WOLFCRYPT
#ifndef SINGLE_THREADED
wolfSSL_Mutex hwLock;

View File

@ -32,6 +32,9 @@
/* TPM2 IO for using TPM through the Winapi kernel driver */
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
} /* extern "C" */
#endif