Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
williamcroberts committed Dec 7, 2023
1 parent 7156cae commit 0f4e279
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ LIB_COMMON := lib/libcommon.a
AM_CFLAGS := \
$(INCLUDE_DIRS) $(EXTRA_CFLAGS) $(TSS2_ESYS_CFLAGS) $(TSS2_MU_CFLAGS) \
$(CRYPTO_CFLAGS) $(CODE_COVERAGE_CFLAGS) $(TSS2_TCTILDR_CFLAGS) \
$(TSS2_RC_CFLAGS) $(TSS2_SYS_CFLAGS)
$(TSS2_RC_CFLAGS) $(TSS2_SYS_CFLAGS) $(YAML_CFLAGS)

AM_LDFLAGS := $(EXTRA_LDFLAGS) $(CODE_COVERAGE_LIBS)

LDADD = \
$(LIB_COMMON) $(TSS2_ESYS_LIBS) $(TSS2_MU_LIBS) $(CRYPTO_LIBS) $(TSS2_TCTILDR_LIBS) \
$(TSS2_RC_LIBS) $(TSS2_SYS_LIBS) $(EFIVAR_LIBS)
$(TSS2_RC_LIBS) $(TSS2_SYS_LIBS) $(EFIVAR_LIBS) $(CURL_LIBS) $(YAML_LIBS)

AM_DISTCHECK_CONFIGURE_FLAGS = --with-bashcompdir='$$(datarootdir)/bash-completion/completions'

Expand Down Expand Up @@ -94,7 +94,7 @@ tss2_tools = \

# Bundle all the tools into a single program similar to busybox
bin_PROGRAMS += tools/tpm2
tools_tpm2_LDADD = $(LDADD) $(CURL_LIBS)
tools_tpm2_LDADD = $(LDADD)
tools_tpm2_CFLAGS = $(AM_CFLAGS) -DTPM2_TOOLS_MAX="$(words $(tpm2_tools))"
tools_tpm2_SOURCES = \
tools/tpm2_tool.c \
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ AC_CHECK_LIB(crypto, [EVP_sm4_cfb128], [
AC_DEFINE([HAVE_EVP_SM4_CFB], [1], [Support EVP_sm4_cfb in openssl])],
[])
PKG_CHECK_MODULES([CURL], [libcurl])
PKG_CHECK_MODULES([YAML], [yaml-0.1])

# pretty print of devicepath if efivar library is present
# auto detect if not specified via the --with-efivar option.
Expand Down
1 change: 1 addition & 0 deletions lib/tpm2_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ union tpm2_option_flags {
uint8_t quiet :1;
uint8_t enable_errata :1;
uint8_t tcti_none :1;
uint8_t no_output :1;
};
uint8_t all;
};
Expand Down
2 changes: 1 addition & 1 deletion tools/tpm2_activatecredential.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ static bool tpm2_tool_onstart(tpm2_options **opts) {
return *opts != NULL;
}

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

/* opts is unused, avoid compiler warning */
UNUSED(flags);
Expand Down
64 changes: 63 additions & 1 deletion tools/tpm2_tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,52 @@ static const tpm2_tool *tpm2_tool_lookup(int *argc, char ***argv)
static struct tool_context {
ESYS_CONTEXT *ectx;
tpm2_options *tool_opts;
yaml_document_t doc;
bool doc_init;
} ctx;

static void main_onexit(void) {

if (ctx.doc_init) {
yaml_document_delete(&ctx.doc);
}
teardown_full(&ctx.ectx);
tpm2_options_free(ctx.tool_opts);
}

static tool_rc yaml_dump(yaml_document_t *doc) {

tool_rc rc = tool_rc_general_error;

yaml_emitter_t emitter = { 0 };
int r = yaml_emitter_initialize(&emitter);
if (!r) {
LOG_ERR("Could not initialize YAML emitter");
return tool_rc_general_error;
}

yaml_emitter_set_output_file(&emitter, stdout);
yaml_emitter_set_canonical(&emitter, 1);

r = yaml_emitter_dump(&emitter, doc);
if (!r) {
LOG_ERR("Could not dump YAML");
goto err;
}

r = yaml_emitter_close(&emitter);
if (!r) {
LOG_ERR("Could not close YAML emitter");
goto err;
}

rc = tool_rc_success;
err:

yaml_emitter_delete(&emitter);
return rc;
}

int main(int argc, char **argv) {

/* get rid of:
Expand Down Expand Up @@ -253,11 +291,30 @@ int main(int argc, char **argv) {
tpm2_errata_init(ctx.ectx);
}

/*
* It seems like you would want to add quiet flag to this, but callers expect the yaml
* doc to be non-null, just use quiet to not emit it at the end for simplicity.
*/
if (!flags.no_output) {
int rc = yaml_document_initialize(
&ctx.doc,
NULL, /* version */
NULL, /* start */
NULL, /* end */
0, /* implicit start */
0 /* implicit end */);
if (!rc) {
LOG_ERR("Could not initialize YAML document");
exit(tool_rc_general_error);
}
ctx.doc_init = true;
}

/*
* Call the specific tool, all tools implement this function instead of
* 'main'.
*/
ret = tool->onrun(ctx.ectx, flags);
ret = tool->onrun(ctx.ectx, &ctx.doc, flags);
if (tool->onstop) {
tool_rc tmp_rc = tool->onstop(ctx.ectx);
/* if onrun() passed, the error code should come from onstop() */
Expand All @@ -274,5 +331,10 @@ int main(int argc, char **argv) {
LOG_ERR("Unable to run %s", argv[0]);
}

/* if the tool needs output OR we were told to be quiet don't dump YAML to stdout */
if (!(flags.no_output && flags.quiet)) {
ret = yaml_dump(&ctx.doc);
}

exit(ret);
}
4 changes: 3 additions & 1 deletion tools/tpm2_tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <tss2/tss2_esys.h>
#include <stdbool.h>

#include <yaml.h>

#include "tool_rc.h"
#include "tpm2_options.h"
#include "tpm2_tool_output.h"
Expand Down Expand Up @@ -33,7 +35,7 @@ typedef bool (*tpm2_tool_onstart_t)(tpm2_options **opts);
* @return
* A tool_rc indicating status.
*/
typedef tool_rc (*tpm2_tool_onrun_t)(ESYS_CONTEXT *ectx, tpm2_option_flags flags);
typedef tool_rc (*tpm2_tool_onrun_t)(ESYS_CONTEXT *ectx, yaml_document_t *doc, tpm2_option_flags flags);

/**
* Called after tpm2_tool_onrun() is invoked. ESAPI context is still valid during this call.
Expand Down

0 comments on commit 0f4e279

Please sign in to comment.