Skip to content

Commit

Permalink
make qualifying data a hex array or file
Browse files Browse the repository at this point in the history
Unify the -q option by allowing them to be a file or hex array for
inputs.

Fixes: tpm2-software#1631

Signed-off-by: William Roberts <william.c.roberts@intel.com>
  • Loading branch information
William Roberts committed Oct 29, 2019
1 parent cc54cb5 commit ba17904
Show file tree
Hide file tree
Showing 27 changed files with 179 additions and 218 deletions.
4 changes: 2 additions & 2 deletions lib/files.c
Expand Up @@ -76,7 +76,7 @@ bool files_get_file_size(FILE *fp, unsigned long *file_size, const char *path) {
return true;
}

static bool read_bytes_from_file(FILE *f, UINT8 *buf, UINT16 *size,
bool file_read_bytes_from_file(FILE *f, UINT8 *buf, UINT16 *size,
const char *path) {
unsigned long file_size;
bool result = files_get_file_size(f, &file_size, path);
Expand Down Expand Up @@ -119,7 +119,7 @@ bool files_load_bytes_from_path(const char *path, UINT8 *buf, UINT16 *size) {
return false;
}

bool result = read_bytes_from_file(f, buf, size, path);
bool result = file_read_bytes_from_file(f, buf, size, path);

fclose(f);
return result;
Expand Down
16 changes: 16 additions & 0 deletions lib/files.h
Expand Up @@ -27,6 +27,22 @@
*/
bool files_load_bytes_from_path(const char *path, UINT8 *buf, UINT16 *size);

/**
* Like files_load_bytes_from_path() but uses a FILE pointer.
* @param f
* The FILE pointer to read from.
* @param buf
* The buffer to store the data.
* @param size
* On input the max size of the buffer, on success the actual count of bytes read.
* @param path
* A possible path for error reporting, can be NULL to silence error reporting.
* @return
* True on success, false otherwise.
*/
bool file_read_bytes_from_file(FILE *f, UINT8 *buf, UINT16 *size,
const char *path);

/**
* Loads data from an input buffer or file path or stdin enforcing an upper bound on size.
* @param input_buffer
Expand Down
80 changes: 28 additions & 52 deletions lib/tpm2_policy.c
Expand Up @@ -156,7 +156,7 @@ tool_rc tpm2_policy_build_pcr(ESYS_CONTEXT *ectx, tpm2_session *policy_session,

tool_rc tpm2_policy_build_policyauthorize(ESYS_CONTEXT *ectx,
tpm2_session *policy_session, const char *policy_digest_path,
const char *policy_qualifier_path,
const char *qualifying_data,
const char *verifying_pubkey_name_path, const char *ticket_path) {

bool result = true;
Expand All @@ -173,24 +173,19 @@ tool_rc tpm2_policy_build_policyauthorize(ESYS_CONTEXT *ectx,
/*
* Qualifier data is optional. If not specified default to 0
*/
unsigned long file_size = 0;
if (policy_qualifier_path) {
result = files_get_file_size_path(policy_qualifier_path, &file_size);
if (!result) {
return tool_rc_general_error;
}
}

TPM2B_NONCE policy_qualifier = { .size = (uint16_t) file_size };
TPM2B_NONCE policy_qualifier = { .size = 0 };

if (file_size != 0) {
result = files_load_bytes_from_path(policy_qualifier_path,
policy_qualifier.buffer, &policy_qualifier.size);
if (qualifying_data) {
policy_qualifier.size = sizeof(policy_qualifier.buffer);
result = tpm2_util_bin_from_hex_or_file(qualifying_data,
&policy_qualifier.size, policy_qualifier.buffer);
if (!result) {
return tool_rc_general_error;
}
}

unsigned long file_size = 0;
result = files_get_file_size_path(verifying_pubkey_name_path, &file_size);
if (!result) {
return tool_rc_general_error;
Expand Down Expand Up @@ -265,25 +260,17 @@ tool_rc tpm2_policy_build_policysecret(ESYS_CONTEXT *ectx,
tpm2_session *policy_session, tpm2_loaded_object *auth_entity_obj,
INT32 expiration, TPMT_TK_AUTH **policy_ticket,
TPM2B_TIMEOUT **timeout, TPM2B_NONCE *nonce_tpm,
const char *policy_qualifier_path) {
const char *policy_qualifier_data) {

/*
* Qualifier data is optional. If not specified default to 0
*/
unsigned long file_size = 0;
bool result = true;
if (policy_qualifier_path) {
result = files_get_file_size_path(policy_qualifier_path, &file_size);
if (!result) {
return tool_rc_general_error;
}
}

TPM2B_NONCE policy_qualifier = { .size = (uint16_t) file_size };

if (file_size != 0) {
result = files_load_bytes_from_path(policy_qualifier_path,
policy_qualifier.buffer, &policy_qualifier.size);
TPM2B_NONCE policy_qualifier = TPM2B_EMPTY_INIT;
if (policy_qualifier_data) {
policy_qualifier.size = sizeof(policy_qualifier.buffer);
bool result = tpm2_util_bin_from_hex_or_file(policy_qualifier_data,
&policy_qualifier.size,
policy_qualifier.buffer);
if (!result) {
return tool_rc_general_error;
}
Expand All @@ -297,7 +284,7 @@ tool_rc tpm2_policy_build_policysecret(ESYS_CONTEXT *ectx,

tool_rc tpm2_policy_build_policyticket(ESYS_CONTEXT *ectx,
tpm2_session *policy_session, char *policy_timeout_path,
const char *qualifier_data_path, char *policy_ticket_path,
const char *qualifier_data, char *policy_ticket_path,
const char *auth_name_path) {

unsigned long file_size = 0;
Expand Down Expand Up @@ -328,20 +315,14 @@ tool_rc tpm2_policy_build_policyticket(ESYS_CONTEXT *ectx,
}
}

TPM2B_NONCE policyref = { 0 };
if (qualifier_data_path) {
result = files_get_file_size_path(qualifier_data_path, &file_size);
TPM2B_NONCE policyref = TPM2B_EMPTY_INIT;
if (qualifier_data) {
policyref.size = sizeof(policyref.buffer);
result = tpm2_util_bin_from_hex_or_file(qualifier_data, &policyref.size,
policyref.buffer);
if (!result) {
return tool_rc_general_error;
}
policyref.size = (uint16_t) file_size;
if (policyref.size) {
result = files_load_bytes_from_path(qualifier_data_path,
policyref.buffer, &policyref.size);
if (!result) {
return tool_rc_general_error;
}
}
}

TPMT_TK_AUTH ticket = { 0 };
Expand All @@ -362,26 +343,21 @@ tool_rc tpm2_policy_build_policyticket(ESYS_CONTEXT *ectx,
tool_rc tpm2_policy_build_policysigned(ESYS_CONTEXT *ectx,
tpm2_session *policy_session, tpm2_loaded_object *auth_entity_obj,
TPMT_SIGNATURE *signature, INT32 expiration, TPM2B_TIMEOUT **timeout,
TPMT_TK_AUTH **policy_ticket, const char *policy_qualifier_path,
TPMT_TK_AUTH **policy_ticket, const char *policy_qualifier_data,
TPM2B_NONCE *nonce_tpm) {

bool result = true;

/*
* Qualifier data is optional. If not specified default to 0
*/
unsigned long file_size = 0;
if (policy_qualifier_path) {
result = files_get_file_size_path(policy_qualifier_path, &file_size);
if (!result) {
return tool_rc_general_error;
}
}

TPM2B_NONCE policy_qualifier = { .size = (uint16_t) file_size };
TPM2B_NONCE policy_qualifier = TPM2B_EMPTY_INIT;

if (file_size != 0) {
result = files_load_bytes_from_path(policy_qualifier_path,
policy_qualifier.buffer, &policy_qualifier.size);
if (policy_qualifier_data) {
policy_qualifier.size = sizeof(policy_qualifier.buffer);
result = tpm2_util_bin_from_hex_or_file(policy_qualifier_data,
&policy_qualifier.size,
policy_qualifier.buffer);
if (!result) {
return tool_rc_general_error;
}
Expand Down
7 changes: 4 additions & 3 deletions lib/tpm2_policy.h
Expand Up @@ -38,8 +38,9 @@ tool_rc tpm2_policy_build_pcr(ESYS_CONTEXT *context,
* The policy session that has the policy digest to be authorized
* @param policy_digest_path
* The policy digest file that needs to be authorized by signing authority
* @param policy_qualifier_path
* The policy qualifier data that concatenates with approved policies
* @param policy_qualifier
* The policy qualifier data that concatenates with approved policies. Can be
* either a path to a file or a hex string.
* @param verifying_pubkey_name_path
* The name of the public key that verifies the signature of the signer
* @param ticket_path
Expand All @@ -49,7 +50,7 @@ tool_rc tpm2_policy_build_pcr(ESYS_CONTEXT *context,
*/
tool_rc tpm2_policy_build_policyauthorize(ESYS_CONTEXT *ectx,
tpm2_session *policy_session, const char *policy_digest_path,
const char *policy_qualifier_path,
const char *policy_qualifier,
const char *verifying_pubkey_name_path, const char *ticket_path);

/**
Expand Down
21 changes: 21 additions & 0 deletions lib/tpm2_util.c
Expand Up @@ -182,6 +182,27 @@ int tpm2_util_hex_to_byte_structure(const char *input_string, UINT16 *byte_lengt
return 0;
}

bool tpm2_util_bin_from_hex_or_file(const char *input, UINT16 *len, BYTE *buffer) {

bool result = false;

FILE *f = fopen(input, "rb");
if (!f) {
result = tpm2_util_hex_to_byte_structure(input, len, buffer) == 0;
goto out;
}

result = file_read_bytes_from_file(f, buffer, len, input);
fclose(f);
out:
if (!result) {
LOG_ERR("Could not convert \"%s\". Neither a file path nor hex string.",
input);
}

return result;
}

void tpm2_util_hexdump2(FILE *f, const BYTE *data, size_t len) {

size_t i;
Expand Down
14 changes: 14 additions & 0 deletions lib/tpm2_util.h
Expand Up @@ -181,6 +181,20 @@ void tpm2_util_hexdump(const BYTE *data, size_t len);
*/
void tpm2_util_hexdump2(FILE *f, const BYTE *data, size_t len);

/**
* Read a hex string converting it to binary or a binary file and
* store into a binary buffer.
* @param input
* Either a hex string or a file path.
* @param len
* The maximum length of the buffer.
* @param buffer
* The buffer to read into.
* @return
* True on success, False otherwise.
*/
bool tpm2_util_bin_from_hex_or_file(const char *input, UINT16 *len, BYTE *buffer);

/**
* Prints a file as a hex string to stdout if quiet mode
* is not enabled.
Expand Down
6 changes: 3 additions & 3 deletions man/tpm2_certifycreation.1.md
Expand Up @@ -58,10 +58,10 @@ created with either **TPM2_CreatePrimary** or **TPM2_Create** commands.

The attestation data of the type TPM2_CREATION_INFO signed with signing key.

* **-q**, **\--qualification**=_FILE_:
* **-q**, **\--qualification**=_FILE\_OR\_HEX_:

The policy qualifier data that the signer can choose to include in the
signature.
Optional, the policy qualifier data that the signer can choose to include in the
signature. Can either be a path or hex string.

## References

Expand Down
6 changes: 3 additions & 3 deletions man/tpm2_checkquote.1.md
Expand Up @@ -44,10 +44,10 @@ those in the quote.
Optional PCR input file to save the list of PCR values that were included
in the quote.

* **-q**, **\--qualification**=_HEX\_STRING_:
* **-q**, **\--qualification**=_HEX\_STRING\_OR\_PATH_:

Qualification data for the quote. This is typically used to add a nonce
against replay attacks.
Qualification data for the quote. Can either be a hex string or path.
This is typically used to add a nonce against replay attacks.

## References

Expand Down
6 changes: 3 additions & 3 deletions man/tpm2_create.1.md
Expand Up @@ -95,10 +95,10 @@ These options for creating the TPM entity:

An optional file output that saves the creation hash for certification.

* **-q**, **\--outside-info**=_FILE_:
* **-q**, **\--outside-info**=_HEX\_STR\_OR\_FILE_:

An optional file to add unique data to the creation data. Note that it does
not contribute in creating statistically unique object.
An optional hex string or path to add unique data to the creation data.
Note that it does not contribute in creating statistically unique object.

* **-l**, **\--pcr-list**=_PCR_:

Expand Down
6 changes: 3 additions & 3 deletions man/tpm2_createprimary.1.md
Expand Up @@ -86,10 +86,10 @@ future interactions with the created primary.

An optional file output that saves the creation hash for certification.

* **-q**, **\--outside-info**=_FILE_:
* **-q**, **\--outside-info**=_FILE\_OR\_HEX_:

An optional file to add unique data to the creation data. Note that it does
not contribute in creating statistically unique object.
An optional file or hex string to add unique data to the creation data.
Note that it does not contribute in creating statistically unique object.

* **-l**, **\--pcr-list**=_PCR_:

Expand Down
6 changes: 3 additions & 3 deletions man/tpm2_gettime.1.md
Expand Up @@ -71,10 +71,10 @@ clock_info:
If left unspecified, a default signature scheme for the key type will
be used.

* **-q**, **\--qualification**=_FILE_:
* **-q**, **\--qualification**=_FILE\_OR\_HEX\_STR_:

The policy qualifier data that the signer can choose to include in the
signature.
Optional, the policy qualifier data that the signer can choose to include in the
signature. Can be either a hex string or path.

* **-o**, **\--signature**=_FILE_:

Expand Down
6 changes: 3 additions & 3 deletions man/tpm2_nvcertify.1.md
Expand Up @@ -51,10 +51,10 @@ These options control the certification:

Output file name for the signature data.

* **-q**, **\--qualification**=_FILE_:
* **-q**, **\--qualification**=_FILE\_OR\_HEX\_STR_:

The policy qualifier data that the signer can choose to include in the
signature.
Optional, the policy qualifier data that the signer can choose to include in the
signature. Can be either a hex string or path.

* **\--size**=_NATURAL_NUMBER_:

Expand Down
5 changes: 3 additions & 2 deletions man/tpm2_policyauthorize.1.md
Expand Up @@ -38,10 +38,11 @@ in the policy digest.

The policy digest that has to be authorized.

* **-q**, **\--qualification**=_FILE_:
* **-q**, **\--qualification**=_FILE\_OR\_HEX_:

The policy qualifier data signed in conjunction with the input policy digest.
This is a unique data that the signer can choose to include in the signature.
This is unique data that the signer can choose to include in the signature
and can either be a path or hex string.

* **-n**, **\--name**=_FILE_:

Expand Down
6 changes: 3 additions & 3 deletions man/tpm2_policysecret.1.md
Expand Up @@ -56,10 +56,10 @@ object use.
limited to the current session. This can be specified as a file or can take
a stdin input if the option argument value is a hyphen "-".

* **-q**, **\--qualification**=_FILE_:
* **-q**, **\--qualification**=_FILE\_OR\_HEX\_STR_:

The policy qualifier data that the signer can choose to include in the
signature.
Optional, the policy qualifier data that the signer can choose to include in the
signature. Can be either a hex string or path.

* **ARGUMENT** the command line argument specifies the _AUTH_ to be set for
the object specified with **-c**.
Expand Down
6 changes: 3 additions & 3 deletions man/tpm2_policysigned.1.md
Expand Up @@ -61,10 +61,10 @@ The optional TPM2 parameters being cpHashA, nonceTPM, policyRef and expiration.

The file path to record the timeout structure returned.

* **-q**, **\--qualification**=_FILE_:
* **-q**, **\--qualification**=_FILE\_OR\_HEX\_STR_:

The policy qualifier data that the signer can choose to include in the
signature.
Optional, the policy qualifier data that the signer can choose to include in the
signature. Can be either a hex string or path.

* **-x**, **\--nonce-tpm**=_FILE_OR_STDIN_:

Expand Down
6 changes: 3 additions & 3 deletions man/tpm2_policyticket.1.md
Expand Up @@ -39,10 +39,10 @@ it.

The file path to record the timeout structure returned.

* **-q**, **\--qualification**=_FILE_:
* **-q**, **\--qualification**=_FILE\_OR\_HEX\_STR_:

The policy qualifier data that the signer can choose to include in the
signature.
Optional, the policy qualifier data that the signer can choose to include in the
signature. Can be either a hex string or path.

## References

Expand Down

0 comments on commit ba17904

Please sign in to comment.