Skip to content

Commit

Permalink
tools: Fix save and load for TPM2B_PRIVATE
Browse files Browse the repository at this point in the history
The tpm2_create tools is only storing the TPM2B_PRIVATE .buffer field but
not it's .size field. This causes the private keys stored with the stable
version of the tools, to fail loading when using the latest master branch.

Fix this by storing the whole TPM2B_PRIVATE object, including size field.

Fixes: #976

Signed-off-by: William Roberts <william.c.roberts@intel.com>
  • Loading branch information
William Roberts authored and martinezjavier committed Apr 30, 2018
1 parent b0c6f5b commit bd5cbb1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,9 @@ bool files_load_bytes_from_file_or_stdin(const char *path, UINT16 *size, BYTE *b
return rc == TPM_RC_SUCCESS; \
}

SAVE_TYPE(TPM2B_PRIVATE, private)
LOAD_TYPE(TPM2B_PRIVATE, private)

SAVE_TYPE(TPM2B_PUBLIC, public)
LOAD_TYPE(TPM2B_PUBLIC, public)

Expand Down
22 changes: 22 additions & 0 deletions lib/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@ bool files_save_tpm_context_to_file(TSS2_SYS_CONTEXT *sapi_context, TPM_HANDLE h
*/
bool files_load_tpm_context_from_file(TSS2_SYS_CONTEXT *sapi_context, TPM_HANDLE *handle, const char *path);

/**
* Serializes a TPM2B_PPRIVATE to the file path provided.
* @param private
* The TPM2B_PRIVATE to save to disk.
* @param path
* The path to save to.
* @return
* true on success, false on error.
*/
bool files_save_private(TPM2B_PRIVATE *private, const char *path);

/**
* Loads a TPM2B_PRIVATE from disk that was saved with files_save_private()
* @param path
* The path to load from.
* @param private
* The TPM2B_PRIVATE to load.
* @return
* true on success, false on error.
*/
bool files_load_private(const char *path, TPM2B_PRIVATE *private);

/**
* Serializes a TPM2B_PUBLIC to the file path provided.
* @param public
Expand Down
2 changes: 1 addition & 1 deletion tools/tpm2_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ int create(TSS2_SYS_CONTEXT *sapi_context)
}

if (ctx.flags.O) {
bool res = files_save_bytes_to_file(ctx.opr_path, outPrivate.t.buffer, outPrivate.t.size);
bool res = files_save_private(&outPrivate, ctx.opr_path);
if (!res) {
return -4;
}
Expand Down
3 changes: 1 addition & 2 deletions tools/tpm2_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ static bool on_option(char key, char *value) {
ctx.flags.u = 1;
break;
case 'r':
ctx.in_private.t.size = sizeof(ctx.in_private.t.buffer);
if(!files_load_bytes_from_path(value, ctx.in_private.t.buffer, &ctx.in_private.t.size)) {
if(!files_load_private(value, &ctx.in_private)) {
return false;
}
ctx.flags.r = 1;
Expand Down

0 comments on commit bd5cbb1

Please sign in to comment.