Skip to content

Commit

Permalink
Initial implementation of the tpm2_contextload tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
jopelima committed Apr 9, 2021
1 parent 44bc689 commit be3d011
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ tpm2_tools = \
tools/tpm2_ecdhkeygen.c \
tools/tpm2_ecdhzgen.c \
tools/tpm2_zgen2phase.c \
tools/tpm2_sessionconfig.c
tools/tpm2_sessionconfig.c \
tools/tpm2_contextload.c

# Create the symlinks for each tool to the tpm2 and optional tss2 bundled executables
install-exec-hook:
Expand Down
75 changes: 75 additions & 0 deletions tools/tpm2_contextload.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* SPDX-License-Identifier: BSD-3-Clause */

#include <stdbool.h>
#include <stdlib.h>

#include "files.h"
#include "log.h"
#include "pcr.h"
#include "tpm2_policy.h"
#include "tpm2_tool.h"
#include "tpm2_session.h"

typedef struct tpm2_contextload_ctx tpm2_contextload_ctx;
struct tpm2_contextload_ctx {
const char *session_path;
tpm2_session *session;
};

static tpm2_contextload_ctx ctx;

static bool on_option(char key, char *value) {

switch (key) {
case 'S':
ctx.session_path = value;
break;
}
return true;
}

static bool tpm2_tool_onstart(tpm2_options **opts) {

static struct option topts[] = {
{ "session", required_argument, NULL, 'S' },
};

*opts = tpm2_options_new("S:", ARRAY_LEN(topts), topts, on_option,
NULL, 0);

return *opts != NULL;
}

static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {

UNUSED(flags);

bool option_fail = false;
if (!ctx.session_path) {
LOG_ERR("Must specify -S session file.");
option_fail = true;
}

if (option_fail) {
return tool_rc_general_error;
}

tool_rc rc = tpm2_session_restore(ectx, ctx.session_path, false,
&ctx.session);
if (rc != tool_rc_success) {
return rc;
}

tpm2_tool_output("session handle: 0x%x\n", tpm2_session_get_handle(ctx.session));

return rc;
}

static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
UNUSED(ectx);

return tpm2_session_close(&ctx.session);
}

// Register this tool with tpm2_tool.c
TPM2_TOOL_REGISTER("contextload", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)

0 comments on commit be3d011

Please sign in to comment.