Showing with 536 additions and 75 deletions.
  1. +3 −0 Makefile.am
  2. +26 −20 lib/tpm2_options.c
  3. +15 −4 lib/tpm2_options.h
  4. +39 −2 lib/tpm2_util.c
  5. +33 −1 lib/tpm2_util.h
  6. +51 −0 man/tpm2_print.1.md
  7. +318 −0 tools/aux/tpm2_print.c
  8. +1 −1 tools/tpm2_activatecredential.c
  9. +1 −1 tools/tpm2_certify.c
  10. +1 −1 tools/tpm2_changeauth.c
  11. +1 −1 tools/tpm2_clear.c
  12. +1 −1 tools/tpm2_clearlock.c
  13. +1 −1 tools/tpm2_create.c
  14. +1 −1 tools/tpm2_createpolicy.c
  15. +1 −1 tools/tpm2_createprimary.c
  16. +1 −1 tools/tpm2_dictionarylockout.c
  17. +1 −1 tools/tpm2_encryptdecrypt.c
  18. +1 −1 tools/tpm2_evictcontrol.c
  19. +1 −1 tools/tpm2_flushcontext.c
  20. +1 −1 tools/tpm2_getcap.c
  21. +1 −1 tools/tpm2_getmanufec.c
  22. +1 −1 tools/tpm2_getpubak.c
  23. +1 −1 tools/tpm2_getpubek.c
  24. +1 −1 tools/tpm2_getrandom.c
  25. +1 −1 tools/tpm2_hash.c
  26. +1 −1 tools/tpm2_hmac.c
  27. +1 −1 tools/tpm2_import.c
  28. +1 −1 tools/tpm2_listpersistent.c
  29. +1 −1 tools/tpm2_load.c
  30. +1 −1 tools/tpm2_loadexternal.c
  31. +1 −1 tools/tpm2_makecredential.c
  32. +1 −1 tools/tpm2_nvdefine.c
  33. +1 −1 tools/tpm2_nvread.c
  34. +1 −1 tools/tpm2_nvreadlock.c
  35. +1 −1 tools/tpm2_nvrelease.c
  36. +1 −1 tools/tpm2_nvwrite.c
  37. +1 −1 tools/tpm2_pcrevent.c
  38. +1 −1 tools/tpm2_pcrextend.c
  39. +1 −1 tools/tpm2_pcrlist.c
  40. +1 −1 tools/tpm2_policypcr.c
  41. +1 −1 tools/tpm2_policyrestart.c
  42. +1 −1 tools/tpm2_quote.c
  43. +1 −1 tools/tpm2_rc_decode.c
  44. +1 −1 tools/tpm2_readpublic.c
  45. +1 −1 tools/tpm2_rsadecrypt.c
  46. +1 −1 tools/tpm2_rsaencrypt.c
  47. +1 −1 tools/tpm2_send.c
  48. +1 −1 tools/tpm2_sign.c
  49. +1 −1 tools/tpm2_startauthsession.c
  50. +1 −1 tools/tpm2_startup.c
  51. +6 −3 tools/tpm2_tool.c
  52. +1 −1 tools/tpm2_unseal.c
  53. +1 −1 tools/tpm2_verifysignature.c
3 changes: 3 additions & 0 deletions Makefile.am
Expand Up @@ -48,6 +48,7 @@ LDADD = \

# keep me sorted
bin_PROGRAMS = \
tools/aux/tpm2_print \
tools/tpm2_activatecredential \
tools/tpm2_certify \
tools/tpm2_changeauth \
Expand Down Expand Up @@ -155,6 +156,8 @@ lib_libcommon_a_SOURCES = \

TOOL_SRC := tools/tpm2_tool.c tools/tpm2_tool.h

tools_aux_tpm2_print_SOURCES = tools/aux/tpm2_print.c $(TOOL_SRC)

tools_tpm2_clear_SOURCES = tools/tpm2_clear.c $(TOOL_SRC)
tools_tpm2_clearlock_SOURCES = tools/tpm2_clearlock.c $(TOOL_SRC)
tools_tpm2_create_SOURCES = tools/tpm2_create.c $(TOOL_SRC)
Expand Down
46 changes: 26 additions & 20 deletions lib/tpm2_options.c
Expand Up @@ -82,7 +82,7 @@

tpm2_options *tpm2_options_new(const char *short_opts, size_t len,
const struct option *long_opts, tpm2_option_handler on_opt,
tpm2_arg_handler on_arg, bool show_usage) {
tpm2_arg_handler on_arg, UINT32 flags) {

tpm2_options *opts = calloc(1, sizeof(*opts) + (sizeof(*long_opts) * len));
if (!opts) {
Expand All @@ -108,7 +108,7 @@ tpm2_options *tpm2_options_new(const char *short_opts, size_t len,
opts->callbacks.on_opt = on_opt;
opts->callbacks.on_arg = on_arg;
opts->len = len;
opts->show_usage = show_usage;
opts->flags = flags;
memcpy(opts->long_opts, long_opts, len * sizeof(*long_opts));

return opts;
Expand Down Expand Up @@ -143,7 +143,7 @@ bool tpm2_options_cat(tpm2_options **dest, tpm2_options *src) {

d->callbacks.on_arg = src->callbacks.on_arg;
d->callbacks.on_opt = src->callbacks.on_opt;
d->show_usage = src->show_usage;
d->flags = src->flags;

memcpy(&d->long_opts[d->len], src->long_opts, src->len * sizeof(src->long_opts[0]));

Expand Down Expand Up @@ -256,7 +256,7 @@ static void show_version (const char *name) {
void tpm2_print_usage(const char *command, struct tpm2_options *tool_opts) {
unsigned int i;

if (!tool_opts || !tool_opts->show_usage) {
if (!tool_opts || !(tool_opts->flags & TPM2_OPTIONS_SHOW_USAGE)) {
return;
}

Expand Down Expand Up @@ -310,7 +310,11 @@ tpm2_option_code tpm2_handle_options (int argc, char **argv, char **envp,
tcti_name = env_str ? env_str : tcti_name;

/* handle any options */
tpm2_options *opts = tpm2_options_new("T:hvVQZ",
const char* common_short_opts = "T:hvVQZ";
if (tool_opts && (tool_opts->flags & TPM2_OPTIONS_NO_SAPI)) {
common_short_opts = "hvVQZ";
}
tpm2_options *opts = tpm2_options_new(common_short_opts,
ARRAY_LEN(long_options), long_options, NULL, NULL, true);
if (!opts) {
return tpm2_option_code_err;
Expand Down Expand Up @@ -388,24 +392,26 @@ tpm2_option_code tpm2_handle_options (int argc, char **argv, char **envp,
}
}

size_t i;
bool found = false;
for(i=0; i < ARRAY_LEN(tcti_map_table); i++) {

char *name = tcti_map_table[i].name;
tcti_init init = tcti_map_table[i].init;
if (!strcmp(tcti_name, name)) {
found = true;
*tcti = init(tcti_opts);
if (!*tcti) {
goto out;
if (!(opts->flags & TPM2_OPTIONS_NO_SAPI)) {
size_t i;
bool found = false;
for(i=0; i < ARRAY_LEN(tcti_map_table); i++) {

char *name = tcti_map_table[i].name;
tcti_init init = tcti_map_table[i].init;
if (!strcmp(tcti_name, name)) {
found = true;
*tcti = init(tcti_opts);
if (!*tcti) {
goto out;
}
}
}
}

if (!found) {
LOG_ERR("Unknown tcti, got: \"%s\"", tcti_name);
goto out;
if (!found) {
LOG_ERR("Unknown tcti, got: \"%s\"", tcti_name);
goto out;
}
}

if (!flags->enable_errata) {
Expand Down
19 changes: 15 additions & 4 deletions lib/tpm2_options.h
Expand Up @@ -95,14 +95,25 @@ typedef bool (*tpm2_option_handler)(char key, char *value);
*/
typedef bool (*tpm2_arg_handler)(int argc, char **argv);

/**
* TPM2_OPTIONS_* flags change default behavior of the argument parser
*
* TPM2_OPTIONS_SHOW_USAGE:
* Enable printing a short usage summary (I.e. help)
* TPM2_OPTIONS_NO_SAPI:
* Skip SAPI initialization. Removes the "-T" common option.
*/
#define TPM2_OPTIONS_SHOW_USAGE 0x1
#define TPM2_OPTIONS_NO_SAPI 0x2

struct tpm2_options {
struct {
tpm2_option_handler on_opt;
tpm2_arg_handler on_arg;
} callbacks;
char *short_opts;
size_t len;
bool show_usage;
UINT32 flags;
struct option long_opts[];
};

Expand All @@ -123,14 +134,14 @@ typedef struct tpm2_options tpm2_options;
* @param on_arg
* An argument handling callback, which may be null if you don't wish
* to handle arguments.
* @param show_usage
* Whether the tool wants a usage short summary text to be printed.
* @param flags
* TPM2_OPTIONS_* bit flags
* @return
* NULL on failure or an initialized tpm2_options object.
*/
tpm2_options *tpm2_options_new(const char *short_opts, size_t len,
const struct option *long_opts, tpm2_option_handler on_opt,
tpm2_arg_handler on_arg, bool show_usage);
tpm2_arg_handler on_arg, UINT32 flags);

/**
* Concatenates two tpm2_options objects, with src appended on
Expand Down
41 changes: 39 additions & 2 deletions lib/tpm2_util.c
Expand Up @@ -31,9 +31,9 @@
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>

#include "log.h"
#include "files.h"
#include "tpm2_alg_util.h"
#include "tpm2_attr_util.h"
#include "tpm2_tool.h"
Expand Down Expand Up @@ -132,7 +132,7 @@ int tpm2_util_hex_to_byte_structure(const char *inStr, UINT16 *byteLength,
return 0;
}

void tpm2_util_hexdump(BYTE *data, size_t len, bool plain) {
void tpm2_util_hexdump(const BYTE *data, size_t len, bool plain) {

if (!output_enabled) {
return;
Expand Down Expand Up @@ -170,6 +170,37 @@ void tpm2_util_hexdump(BYTE *data, size_t len, bool plain) {
}
}

bool tpm2_util_hexdump_file(FILE *fd, size_t len, bool plain) {
BYTE* buff = (BYTE*)malloc(len);
if (!buff) {
LOG_ERR("malloc() failed");
return false;
}

bool res = files_read_bytes(fd, buff, len);
if (!res) {
LOG_ERR("Failed to read file");
free(buff);
return false;
}

tpm2_util_hexdump(buff, len, plain);

free(buff);
return true;
}

bool tpm2_util_print_tpm2b_file(FILE *fd)
{
UINT16 len;
bool res = files_read_16(fd, &len);
if(!res) {
LOG_ERR("File read failed");
return false;
}
return tpm2_util_hexdump_file(fd, len, true);
}

/* TODO OPTIMIZE ME */
UINT16 tpm2_util_copy_tpm2b(TPM2B *dest, TPM2B *src) {
int i;
Expand Down Expand Up @@ -315,6 +346,12 @@ static void tpm2_util_public_to_keydata(TPM2B_PUBLIC *public, tpm2_util_keydata
return;
}

void print_yaml_indent(size_t indent_count) {
while (indent_count--) {
tpm2_tool_output(" ");
}
}

void tpm2_util_public_to_yaml(TPM2B_PUBLIC *public) {

tpm2_tool_output("algorithm:\n");
Expand Down
34 changes: 33 additions & 1 deletion lib/tpm2_util.h
Expand Up @@ -33,6 +33,7 @@

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#include <sapi/tpm20.h>

Expand Down Expand Up @@ -159,7 +160,24 @@ bool tpm2_util_string_to_uint16(const char *str, uint16_t *value);
* true for a plain hex string false for an xxd compatable
* dump.
*/
void tpm2_util_hexdump(BYTE *data, size_t len, bool plain);
void tpm2_util_hexdump(const BYTE *data, size_t len, bool plain);

/**
* Prints an xxd compatible hexdump to stdout if output is enabled,
* ie no -Q option.
*
* @param fd
* A readable open file.
* @param len
* The length of the data to read and print.
* @param plain
* true for a plain hex string false for an xxd compatable
* dump.
* @return
* true if len bytes were successfully read and printed,
* false otherwise
*/
bool tpm2_util_hexdump_file(FILE *fd, size_t len, bool plain);

/**
* Prints a TPM2B as a hex dump.
Expand All @@ -170,6 +188,13 @@ static inline void tpm2_util_print_tpm2b(TPM2B *buffer) {
return tpm2_util_hexdump(buffer->buffer, buffer->size, true);
}

/**
* Reads a TPM2B object from FILE* and prints data in hex.
* @param fd
* A readable open file.
*/
bool tpm2_util_print_tpm2b_file(FILE *fd);

/**
* Copies a tpm2b from dest to src and clears dest if src is NULL.
* If src is NULL, it is a NOP.
Expand Down Expand Up @@ -255,6 +280,13 @@ UINT64 tpm2_util_ntoh_64(UINT64 data);
*/
UINT32 tpm2_util_pop_count(UINT32 data);

/**
* Prints whitespace indention for yaml output.
* @param indent_count
* Number of times to indent
*/
void print_yaml_indent(size_t indent_count);

/**
* Convert a TPM2B_PUBLIC into a yaml format and output if not quiet.
* @param public
Expand Down
51 changes: 51 additions & 0 deletions man/tpm2_print.1.md
@@ -0,0 +1,51 @@
% tpm2_print(1) tpm2-tools | General Commands Manual
%
% DECEMBER 2017

# NAME

**tpm2_print**(1) - Prints TPM data structures

# SYNOPSIS

**tpm2_print** [*OPTIONS*]

# DESCRIPTION

**tpm2_print**(1) decodes a TPM data structure and prints enclosed
elements to stdout as YAML.

# OPTIONS

* **-t**, **--type**:

Required. Type of data structure. Only TPMS_ATTEST is presently
supported.

* **-f**, **--file**:

Optional. File containing TPM object. Reads from stdin if unspecified.

[common options](common/options.md)

[common tcti options](common/tcti.md)

# EXAMPLES

```
tpm2_print -t TPMS_ATTEST -f /path/to/tpm/quote
tpm2_print --type=TPMS_ATTEST --file=/path/to/tpm/quote
cat /path/to/tpm/quote | tpm2_print --type=TPMS_ATTEST
```

# RETURNS

0 on success. Non-zero otherwise.

# BUGS

[Github Issues](https://github.com/01org/tpm2-tools/issues)

# HELP

See the [Mailing List](https://lists.01.org/mailman/listinfo/tpm2)