Skip to content

Commit

Permalink
tpm2_rsaencrypt.c: make -o optional
Browse files Browse the repository at this point in the history
Make -o option optional and default to stdout. When
printing to stdout, use an xxd compatible hexdump
format.

Signed-off-by: William Roberts <william.c.roberts@intel.com>
  • Loading branch information
William Roberts committed Nov 9, 2017
1 parent cc66580 commit c937989
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
4 changes: 3 additions & 1 deletion man/tpm2_rsaencrypt.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ The key referenced by keyHandle is **required** to be:

* **-o**, **--out-file**=_OUTPUT\_FILE_:

Output file path, record the decrypted data.
Output file path, record the decrypted data. The default is to print an
xxd compatible hexdump to stdout. If a file is specified, raw binary
output is performed.

* **-S**, **--input-session-handle**=_SESSION\_HANDLE_:

Expand Down
3 changes: 3 additions & 0 deletions test/system/test_tpm2_rsaencrypt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,7 @@ tpm2_loadexternal -Q -H n -u $file_rsaencrypt_key_pub -C $file_rsaencrypt_key
#./tpm2_rsaencrypt -c context_loadexternal_out6.out -I secret.data -o rsa_en.out
tpm2_rsaencrypt -Q -c $file_rsaencrypt_key_ctx -I $file_input_data -o $file_rsa_en_output_data

# Test stdout for -o and ensure that output is xxd format
tpm2_rsaencrypt -c $file_rsaencrypt_key_ctx -I $file_input_data | xxd -r > /dev/null

exit 0
31 changes: 16 additions & 15 deletions tools/tpm2_rsaencrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//**********************************************************************;

#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include <sapi/tpm20.h>

#include "tpm2_options.h"
#include "files.h"
#include "log.h"
#include "tpm2_options.h"
#include "tpm2_tool.h"
#include "tpm2_util.h"

Expand All @@ -46,14 +47,13 @@ struct tpm_rsaencrypt_ctx {
struct {
UINT8 k : 1;
UINT8 I : 1;
UINT8 o : 1;
UINT8 c : 1;
UINT8 unused : 4;
UINT8 unused : 5;
} flags;
char *context_key_file;
TPMI_DH_OBJECT key_handle;
TPM2B_PUBLIC_KEY_RSA message;
char *output_file_path;
char *output_path;
};

static tpm_rsaencrypt_ctx ctx;
Expand Down Expand Up @@ -84,8 +84,14 @@ static bool rsa_encrypt_and_save(TSS2_SYS_CONTEXT *sapi_context) {
return false;
}

return files_save_bytes_to_file(ctx.output_file_path, out_data.t.buffer,
if (ctx.output_path) {
return files_save_bytes_to_file(ctx.output_path, out_data.t.buffer,
out_data.t.size);
}

tpm2_util_print_tpm2b(&out_data.b);

return true;
}

static bool on_option(char key, char *value) {
Expand All @@ -95,7 +101,7 @@ static bool on_option(char key, char *value) {
bool result = tpm2_util_string_to_uint32(value, &ctx.key_handle);
if (!result) {
LOG_ERR("Could not convert key handle to number, got: \"%s\"",
optarg);
value);
return false;
}
ctx.flags.k = 1;
Expand All @@ -112,16 +118,11 @@ static bool on_option(char key, char *value) {
}
break;
case 'o': {
bool result = files_does_file_exist(optarg);
if (result) {
return false;
}
ctx.output_file_path = optarg;
ctx.flags.o = 1;
ctx.output_path = value;
}
break;
case 'c':
ctx.context_key_file = optarg;
ctx.context_key_file = value;
ctx.flags.c = 1;
break;
/* no default */
Expand All @@ -147,8 +148,8 @@ bool tpm2_tool_onstart(tpm2_options **opts) {

static bool init(TSS2_SYS_CONTEXT *sapi_context) {

if (!((ctx.flags.k || ctx.flags.c) && ctx.flags.I && ctx.flags.o)) {
LOG_ERR("Expected options I and o and (k or c)");
if (!((ctx.flags.k || ctx.flags.c) && ctx.flags.I)) {
LOG_ERR("Expected options I and (k or c)");
return false;
}

Expand Down

0 comments on commit c937989

Please sign in to comment.