Skip to content

Commit

Permalink
tpm2_gettime.c Add yaml support.
Browse files Browse the repository at this point in the history
Signed-off-by: Juergen Repp <juergen_repp@web.de>
  • Loading branch information
JuergenReppSIT committed Dec 13, 2023
1 parent cfa35e0 commit db0becc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
32 changes: 32 additions & 0 deletions lib/tpm2_yaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,32 @@ static int add_sig_hex(tpm2_yaml *y, int root, const char *key, TPMT_SIGNATURE *
sig_kvs, ARRAY_LEN(sig_kvs));
}

static int tpms_time_info_to_yaml(tpm2_yaml *y, int root,
const TPMS_TIME_INFO *time_info) {
/* time: 105594980
*/
struct key_value key_time = KVP_ADD_INT("time", time_info->time);
int rc = add_kvp(y, root, &key_time);
return_rc(rc);

/* clock_info:
* clock: 128285828
* reset_count: 1
* restart_count: 0
* safe: yes
*/

key_value clock_info[] = {
KVP_ADD_INT("clock", time_info->clockInfo.clock),
KVP_ADD_INT("reset_count", time_info->clockInfo.resetCount),
KVP_ADD_INT("restartcount", time_info->clockInfo.restartCount),
KVP_ADD_STR("safe", time_info->clockInfo.safe == TPM2_YES ? "yes" : "no"),
};

return add_mapping_root_with_items(y, root, "clock_info",
clock_info, ARRAY_LEN(clock_info));
}

static tool_rc tpm2b_to_yaml(tpm2_yaml *y, int root, const char *key, const TPM2B_NAME *name) {

struct key_value key_bits = KVP_ADD_TPM2B(key, name);
Expand Down Expand Up @@ -330,6 +356,12 @@ tool_rc tpm2_yaml_attest2b(const TPM2B_ATTEST *attest, tpm2_yaml *y) {
return tpm2b_to_yaml(y, y->root, "quoted", (TPM2B_NAME *)attest);
}

tool_rc tpm2_yaml_tpms_time_info(const TPMS_TIME_INFO *time_info, tpm2_yaml *y) {
null_ret(y, 1);
return tpms_time_info_to_yaml(y, y->root, time_info) ? tool_rc_success
: tool_rc_general_error;
}

tool_rc tpm2_yaml_named_tpm2b(const char *name, const TPM2B_NAME *tpm2b, tpm2_yaml *y) {
null_ret(y, 1);
assert(name);
Expand Down
2 changes: 2 additions & 0 deletions lib/tpm2_yaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ tool_rc tpm2_yaml_tpm2_nv_index(tpm2_yaml *y, TPM2_NV_INDEX index);
tool_rc tpm2_yaml_nv_read(const char *data, size_t data_len, const TPM2B_NV_PUBLIC *nv_public,
tpm2_yaml *y);

tool_rc tpm2_yaml_tpms_time_info(const TPMS_TIME_INFO *time_info, tpm2_yaml *y);

tool_rc tpm2_yaml_dump(tpm2_yaml *y, FILE *f);

#endif /* LIB_TPM2_YAML_H_ */
7 changes: 3 additions & 4 deletions tools/tpm2_gettime.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static tool_rc gettime(ESYS_CONTEXT *ectx) {
ctx.parameter_hash_algorithm);
}

static tool_rc process_output(ESYS_CONTEXT *ectx) {
static tool_rc process_output(ESYS_CONTEXT *ectx, tpm2_yaml *doc) {

UNUSED(ectx);
/*
Expand Down Expand Up @@ -117,9 +117,8 @@ static tool_rc process_output(ESYS_CONTEXT *ectx) {
TPMS_ATTEST attest;
rc = files_tpm2b_attest_to_tpms_attest(ctx.time_info, &attest);
if (rc == tool_rc_success) {
tpm2_util_print_time(&attest.attested.time.time);
rc = tpm2_yaml_tpms_time_info(&attest.attested.time.time, doc);
}

return rc;
}

Expand Down Expand Up @@ -325,7 +324,7 @@ static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_yaml *doc, tpm2_option_f
/*
* 4. Process outputs
*/
return process_output(ectx);
return process_output(ectx, doc);
}

static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
Expand Down

0 comments on commit db0becc

Please sign in to comment.