forked from tpm2-software/tpm2-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of the tpm2_contextload tool.
- Loading branch information
Showing
2 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |