6 changes: 3 additions & 3 deletions Makefile.am
Expand Up @@ -97,8 +97,6 @@ bin_PROGRAMS = \

noinst_LIBRARIES = $(LIB_COMMON)
lib_libcommon_a_SOURCES = \
lib/conversion.c \
lib/conversion.h \
lib/files.c \
lib/files.h \
lib/log.c \
Expand Down Expand Up @@ -134,7 +132,9 @@ lib_libcommon_a_SOURCES = \
lib/tpm2_session.c \
lib/tpm2_session.h \
lib/tpm2_tcti_ldr.c \
lib/tpm2_tcti_ldr.h
lib/tpm2_tcti_ldr.h \
lib/tpm2_convert.c \
lib/tpm2_convert.h

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

Expand Down
14 changes: 7 additions & 7 deletions lib/conversion.c → lib/tpm2_convert.c
Expand Up @@ -37,15 +37,15 @@
#include <openssl/bn.h>
#include <openssl/err.h>

#include "conversion.h"
#include "files.h"
#include "log.h"
#include "tpm2_alg_util.h"
#include "tpm2_convert.h"
#include "tpm2_util.h"

static bool tpm2_convert_pubkey_ssl(TPMT_PUBLIC *public, pubkey_format format, const char *path);
static bool tpm2_convert_pubkey_ssl(TPMT_PUBLIC *public, tpm2_convert_pubkey_fmt format, const char *path);

pubkey_format tpm2_parse_pubkey_format(const char *label) {
tpm2_convert_pubkey_fmt tpm2_convert_pubkey_fmt_from_optarg(const char *label) {
if (strcasecmp(label, "der") == 0) {
return pubkey_format_der;
}
Expand All @@ -61,7 +61,7 @@ pubkey_format tpm2_parse_pubkey_format(const char *label) {
return pubkey_format_err;
}

signature_format tpm2_parse_signature_format(const char *label) {
tpm2_convert_sig_fmt tpm2_convert_sig_fmt_from_optarg(const char *label) {
if (strcasecmp(label, "tss") == 0) {
return signature_format_tss;
}
Expand All @@ -82,7 +82,7 @@ static void print_ssl_error(const char *failed_action) {
LOG_ERR("%s: %s", failed_action, errstr);
}

bool tpm2_convert_pubkey(TPM2B_PUBLIC *public, pubkey_format format, const char *path) {
bool tpm2_convert_pubkey_save(TPM2B_PUBLIC *public, tpm2_convert_pubkey_fmt format, const char *path) {

if (format == pubkey_format_der || format == pubkey_format_pem) {
return tpm2_convert_pubkey_ssl(&public->publicArea, format, path);
Expand All @@ -95,7 +95,7 @@ bool tpm2_convert_pubkey(TPM2B_PUBLIC *public, pubkey_format format, const char
return false;
}

static bool tpm2_convert_pubkey_ssl(TPMT_PUBLIC *public, pubkey_format format, const char *path) {
static bool tpm2_convert_pubkey_ssl(TPMT_PUBLIC *public, tpm2_convert_pubkey_fmt format, const char *path) {
bool ret = false;
FILE *fp = NULL;
RSA *ssl_rsa_key = NULL;
Expand Down Expand Up @@ -191,7 +191,7 @@ static bool tpm2_convert_pubkey_ssl(TPMT_PUBLIC *public, pubkey_format format, c
return ret;
}

bool tpm2_convert_signature(TPMT_SIGNATURE *signature, signature_format format, const char *path) {
bool tpm2_convert_sig(TPMT_SIGNATURE *signature, tpm2_convert_sig_fmt format, const char *path) {

switch(format) {
case signature_format_tss:
Expand Down
30 changes: 17 additions & 13 deletions lib/conversion.h → lib/tpm2_convert.h
Expand Up @@ -32,16 +32,19 @@

#include <sapi/tpm20.h>

typedef enum pubkey_format pubkey_format;

enum pubkey_format {
pubkey_format_tss, pubkey_format_pem, pubkey_format_der, pubkey_format_err
typedef enum tpm2_convert_pubkey_fmt tpm2_convert_pubkey_fmt;
enum tpm2_convert_pubkey_fmt {
pubkey_format_tss,
pubkey_format_pem,
pubkey_format_der,
pubkey_format_err
};

typedef enum signature_format signature_format;

enum signature_format {
signature_format_tss, signature_format_plain, signature_format_err
typedef enum tpm2_convert_sig_fmt tpm2_convert_sig_fmt;
enum tpm2_convert_sig_fmt {
signature_format_tss,
signature_format_plain,
signature_format_err
};

/**
Expand All @@ -53,15 +56,15 @@ enum signature_format {
* @return
* On error pubkey_format_err is returned.
*/
pubkey_format tpm2_parse_pubkey_format(const char *label);
tpm2_convert_pubkey_fmt tpm2_convert_pubkey_fmt_from_optarg(const char *label);

/**
* Converts the given public key structure into the requested target format
* and writes the result to the given file system path.
*
* LOG_ERR is used to communicate errors.
*/
bool tpm2_convert_pubkey(TPM2B_PUBLIC *public, pubkey_format format, const char *path);
bool tpm2_convert_pubkey_save(TPM2B_PUBLIC *public, tpm2_convert_pubkey_fmt format, const char *path);

/**
* Loads a public key in the TSS format from a file.
Expand All @@ -71,7 +74,7 @@ bool tpm2_convert_pubkey(TPM2B_PUBLIC *public, pubkey_format format, const char
* @param path
* @return
*/
bool tpm2_conversion_load_pubkey(TPM2B_PUBLIC *public, const char *path);
bool tpm2_convert_pubkey_load(TPM2B_PUBLIC *public, const char *path);

/**
* Parses the given command line signature format option string and returns
Expand All @@ -82,14 +85,15 @@ bool tpm2_conversion_load_pubkey(TPM2B_PUBLIC *public, const char *path);
* @return
* On error signature_format_err is returned.
*/
signature_format tpm2_parse_signature_format(const char *label);
tpm2_convert_sig_fmt tpm2_convert_sig_fmt_from_optarg(const char *label);

/**
* Converts the given signature data into the requested target format and
* writes the result to the given file system path.
*
* LOG_ERR is used to communicate errors.
*/
bool tpm2_convert_signature(TPMT_SIGNATURE *signature, signature_format format, const char *path);
bool tpm2_convert_sig(TPMT_SIGNATURE *signature, tpm2_convert_sig_fmt format,
const char *path);

#endif /* CONVERSION_H */
14 changes: 13 additions & 1 deletion lib/tpm2_error.c
Expand Up @@ -458,13 +458,25 @@ static const char *tpm2_err_handler_fmt0(TSS2_RC rc) {
NULL,
// 0x19 - TPM2_RC_HMAC
"not currently used",
// 0x1A - EMPTY
NULL,
// 0x1B - EMPTY
NULL,
// 0x1C - EMPTY
NULL,
// 0x1D - EMPTY
NULL,
// 0x1E - EMPTY
NULL,
// 0x1F - EMPTY
NULL,
// 0x20 - TPM2_RC_DISABLED
"the command is disabled",
// 0x21 - TPM2_RC_EXCLUSIVE
"command failed because audit sequence required exclusivity",
// 0x22 - EMPTY
NULL,
// 0x32 - EMPTY,
// 0x23 - EMPTY,
NULL,
// 0x24 - TPM2_RC_AUTH_TYPE
"authorization handle is not correct for command",
Expand Down
8 changes: 4 additions & 4 deletions tools/tpm2_certify.c
Expand Up @@ -37,7 +37,7 @@
#include <limits.h>
#include <sapi/tpm20.h>

#include "conversion.h"
#include "tpm2_convert.h"
#include "tpm2_options.h"
#include "tpm2_password_util.h"
#include "tpm2_util.h"
Expand Down Expand Up @@ -74,7 +74,7 @@ struct tpm_certify_ctx {
} flags;
char *context_file;
char *context_key_file;
signature_format sig_fmt;
tpm2_convert_sig_fmt sig_fmt;
};

static tpm_certify_ctx ctx = {
Expand Down Expand Up @@ -179,7 +179,7 @@ static bool certify_and_save_data(TSS2_SYS_CONTEXT *sapi_context) {
return false;
}

return tpm2_convert_signature(&signature, ctx.sig_fmt, ctx.file_path.sig);
return tpm2_convert_sig(&signature, ctx.sig_fmt, ctx.file_path.sig);
}

static bool on_option(char key, char *value) {
Expand Down Expand Up @@ -261,7 +261,7 @@ static bool on_option(char key, char *value) {
break;
case 'f':
ctx.flags.f = 1;
ctx.sig_fmt = tpm2_parse_signature_format(value);
ctx.sig_fmt = tpm2_convert_sig_fmt_from_optarg(value);

if (ctx.sig_fmt == signature_format_err) {
return false;
Expand Down
6 changes: 3 additions & 3 deletions tools/tpm2_getpubak.c
Expand Up @@ -38,7 +38,7 @@

#include <sapi/tpm20.h>

#include "conversion.h"
#include "tpm2_convert.h"
#include "tpm2_options.h"
#include "tpm2_password_util.h"
#include "files.h"
Expand Down Expand Up @@ -342,7 +342,7 @@ static bool create_ak(TSS2_SYS_CONTEXT *sapi_context) {

/* Output in YAML format */
tpm2_tool_output("loaded-key:\n");
tpm2_tool_output(" handle: %8.8x\n name: ", loaded_sha1_key_handle);
tpm2_tool_output(" handle: 0x%X\n name: ", loaded_sha1_key_handle);
tpm2_util_print_tpm2b((TPM2B *)&name);
tpm2_tool_output("\n");

Expand Down Expand Up @@ -383,7 +383,7 @@ static bool create_ak(TSS2_SYS_CONTEXT *sapi_context) {
}
LOG_INFO("Flush transient AK succ.");

return tpm2_convert_pubkey(&out_public, pubkey_format_tss, ctx.output_file);
return tpm2_convert_pubkey_save(&out_public, pubkey_format_tss, ctx.output_file);
}

static bool on_option(char key, char *value) {
Expand Down
4 changes: 2 additions & 2 deletions tools/tpm2_getpubek.c
Expand Up @@ -36,7 +36,7 @@

#include <sapi/tpm20.h>

#include "conversion.h"
#include "tpm2_convert.h"
#include "files.h"
#include "log.h"
#include "tpm2_alg_util.h"
Expand Down Expand Up @@ -223,7 +223,7 @@ static bool create_ek_handle(TSS2_SYS_CONTEXT *sapi_context) {

LOG_INFO("Flush transient EK success.");

return tpm2_convert_pubkey(&outPublic, pubkey_format_tss, ctx.out_file_path);
return tpm2_convert_pubkey_save(&outPublic, pubkey_format_tss, ctx.out_file_path);
}

static bool on_option(char key, char *value) {
Expand Down
8 changes: 4 additions & 4 deletions tools/tpm2_quote.c
Expand Up @@ -36,10 +36,10 @@

#include <sapi/tpm20.h>

#include "tpm2_convert.h"
#include "files.h"
#include "log.h"
#include "pcr.h"
#include "conversion.h"
#include "tpm2_alg_util.h"
#include "tpm2_password_util.h"
#include "tpm2_session.h"
Expand All @@ -55,7 +55,7 @@ static TPMS_AUTH_COMMAND sessionData;
static char *outFilePath;
static char *signature_path;
static char *message_path;
static signature_format sig_format;
static tpm2_convert_sig_fmt sig_format;
static TPMI_ALG_HASH sig_hash_algorithm;
static TPM2B_DATA qualifyingData = TPM2B_EMPTY_INIT;
static TPML_PCR_SELECTION pcrSelections;
Expand All @@ -68,7 +68,7 @@ static bool write_output_files(TPM2B_ATTEST *quoted, TPMT_SIGNATURE *signature)

bool res = true;
if (signature_path) {
res &= tpm2_convert_signature(signature, sig_format, signature_path);
res &= tpm2_convert_sig(signature, sig_format, signature_path);
}

if (message_path) {
Expand Down Expand Up @@ -200,7 +200,7 @@ static bool on_option(char key, char *value) {
message_path = value;
break;
case 'f':
sig_format = tpm2_parse_signature_format(value);
sig_format = tpm2_convert_sig_fmt_from_optarg(value);

if (sig_format == signature_format_err) {
return false;
Expand Down
8 changes: 4 additions & 4 deletions tools/tpm2_readpublic.c
Expand Up @@ -35,7 +35,7 @@

#include <sapi/tpm20.h>

#include "conversion.h"
#include "tpm2_convert.h"
#include "files.h"
#include "log.h"
#include "tpm2_alg_util.h"
Expand All @@ -54,7 +54,7 @@ struct tpm_readpub_ctx {
TPMI_DH_OBJECT objectHandle;
char *outFilePath;
char *context_file;
pubkey_format format;
tpm2_convert_pubkey_fmt format;
};

static tpm_readpub_ctx ctx = {
Expand Down Expand Up @@ -94,7 +94,7 @@ static int read_public_and_save(TSS2_SYS_CONTEXT *sapi_context) {
tpm2_util_public_to_yaml(&public);

return ctx.outFilePath ?
tpm2_convert_pubkey(&public, ctx.format, ctx.outFilePath) : true;
tpm2_convert_pubkey_save(&public, ctx.format, ctx.outFilePath) : true;
}

static bool on_option(char key, char *value) {
Expand All @@ -116,7 +116,7 @@ static bool on_option(char key, char *value) {
ctx.flags.c = 1;
break;
case 'f':
ctx.format = tpm2_parse_pubkey_format(value);
ctx.format = tpm2_convert_pubkey_fmt_from_optarg(value);
if (ctx.format == pubkey_format_err) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion tools/tpm2_rsadecrypt.c
Expand Up @@ -80,7 +80,7 @@ static bool rsa_decrypt_and_save(TSS2_SYS_CONTEXT *sapi_context) {
&sessions_data, &ctx.cipher_text, &inScheme, &label, &message,
&sessions_data_out));
if (rval != TPM2_RC_SUCCESS) {
LOG_ERR("rsaDecrypt failed, error code: 0x%x", rval);
LOG_PERR(Tss2_Sys_RSA_Decrypt, rval);
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions tools/tpm2_sign.c
Expand Up @@ -38,7 +38,7 @@
#include <getopt.h>
#include <sapi/tpm20.h>

#include "conversion.h"
#include "tpm2_convert.h"
#include "files.h"
#include "log.h"
#include "tpm2_hash.h"
Expand All @@ -61,7 +61,7 @@ struct tpm_sign_ctx {
UINT16 length;
char *contextKeyFile;
char *inMsgFileName;
signature_format sig_format;
tpm2_convert_sig_fmt sig_format;
struct {
UINT16 k : 1;
UINT16 P : 1;
Expand Down Expand Up @@ -112,7 +112,7 @@ static bool sign_and_save(TSS2_SYS_CONTEXT *sapi_context) {
return false;
}

return tpm2_convert_signature(&signature, ctx.sig_format, ctx.outFilePath);
return tpm2_convert_sig(&signature, ctx.sig_format, ctx.outFilePath);
}

static bool init(TSS2_SYS_CONTEXT *sapi_context) {
Expand Down Expand Up @@ -258,7 +258,7 @@ static bool on_option(char key, char *value) {
} break;
case 'f':
ctx.flags.f = 1;
ctx.sig_format = tpm2_parse_signature_format(value);
ctx.sig_format = tpm2_convert_sig_fmt_from_optarg(value);

if (ctx.sig_format == signature_format_err) {
return false;
Expand Down