Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option -x to display non ascii characters in hex format. #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions eltt2.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int main(int argc, char **argv)
opterr = 0; // Disable getopt error messages in case of unknown parameters; we want to use our own error messages.

// Loop through parameters with getopt.
while (-1 != (option = getopt(argc, argv, "cgvhTa:A:b:d:e:E:G:r:R:s:S:t:u:z:")))
while (-1 != (option = getopt(argc, argv, "cgxvhTa:A:b:d:e:E:G:r:R:s:S:t:u:z:")))
{
switch (option)
{
Expand Down Expand Up @@ -161,6 +161,7 @@ int main(int argc, char **argv)
ret_val = tpmtool_transmit(input_bytes, input_bytes_size, tpm_response_buf, &tpm_response_buf_size);
break;

case 'x': // TPM_CC_GetCapability (hex output for non ascii chars)
case 'g': // TPM_CC_GetCapability
ret_val = tpmtool_transmit(tpm2_getcapability_fixed, sizeof(tpm2_getcapability_fixed), tpm_response_buf, &tpm_response_buf_size);
break;
Expand Down Expand Up @@ -556,11 +557,15 @@ static int response_print(uint8_t *response_buf, size_t resp_size, int option)
break;

case 'g': // Print the fixed capability flags.
ret_val = print_capability_flags(response_buf, PT_FIXED_SELECTOR);
ret_val = print_capability_flags(response_buf, PT_FIXED_SELECTOR, DONT_CHECK_ASCII);
break;

case 'x': // Print the fixed capability flags (not printable characters in hex format).
ret_val = print_capability_flags(response_buf, PT_FIXED_SELECTOR, CHECK_ASCII);
break;

case 'v': // Print the variable capability flags.
ret_val = print_capability_flags(response_buf, PT_VAR_SELECTOR);
ret_val = print_capability_flags(response_buf, PT_VAR_SELECTOR, DONT_CHECK_ASCII);
break;

case 'G': // Print the returned random value in hex.
Expand Down Expand Up @@ -731,6 +736,7 @@ static void print_help()
printf(" -> PCR index: Enter the PCR index in hex like '17' for 0x17\n");
printf(" PCR digest: Enter the value to extend the PCR with in hex like '0f56...' for {0x0f, 0x56, ...}\n");
printf("'-g': Get fixed capability values\n");
printf("'-x': Get fixed capability values (print not printable chars in hex format)\n");
printf("'-v': Get variable capability values\n");
printf("'-G <byte count>': Get Random\n");
printf(" -> Enter desired number of random bytes in hex like '20' for 0x20 (=32 bytes, maximum)\n");
Expand All @@ -752,7 +758,18 @@ static void print_help()
printf(" -> PCR index: Enter PCR number in hex like '17' for 0x17\n");
}

static int print_capability_flags(uint8_t *response_buf, uint8_t cap_selector)
static void print_chars(uint8_t *characters, int num_chars, int check_ascii) {
int i;
for (i = 0; i < num_chars; i++) {
if (check_ascii && !isprint(characters[i])) {
printf("(0x%x)", characters[i]);
} else {
printf("%c", characters[i]);
}
}
}

static int print_capability_flags(uint8_t *response_buf, uint8_t cap_selector, int check_ascii)
{
int ret_val = EXIT_SUCCESS; // Return value.
uint64_t propertyValue = 0; // Value of the read property.
Expand All @@ -777,7 +794,9 @@ static int print_capability_flags(uint8_t *response_buf, uint8_t cap_selector)
switch(propertyKey)
{
case 0x100:
printf("TPM_PT_FAMILY_INDICATOR: %c%c%c%c\n", response_buf[x+4], response_buf[x+5], response_buf[x+6], response_buf[x+7]);
printf("TPM_PT_FAMILY_INDICATOR: ");
print_chars(&response_buf[4], 4, check_ascii);
printf("\n");
break;
case 0x100+1:
printf("TPM_PT_LEVEL: %" PRIu64 "\n", propertyValue);
Expand All @@ -792,20 +811,23 @@ static int print_capability_flags(uint8_t *response_buf, uint8_t cap_selector)
printf("TPM_PT_YEAR: %" PRIu64 "\n", propertyValue);
break;
case 0x100+5:
printf("TPM_PT_MANUFACTURER: %c%c%c%c\n", response_buf[x+4], response_buf[x+5], response_buf[x+6], response_buf[x+7]);
printf("TPM_PT_MANUFACTURER: ");
print_chars(&response_buf[x+4], 4, check_ascii);
printf("\n");
break;
case 0x100+6:
printf("TPM_PT_VENDOR_STRING: ");
printf("%c%c%c%c", response_buf[x+4], response_buf[x+5], response_buf[x+6], response_buf[x+7]);
print_chars(&response_buf[x+4], 4, check_ascii);
break;
case 0x100+7: // it is assumed that TPM_PT_VENDOR_STRING_2 follows _1
printf("%c%c%c%c", response_buf[x+4], response_buf[x+5], response_buf[x+6], response_buf[x+7]);
print_chars(&response_buf[x+4], 4, check_ascii);
break;
case 0x100+8:
printf("%c%c%c%c", response_buf[x+4], response_buf[x+5], response_buf[x+6], response_buf[x+7]);
print_chars(&response_buf[x+4], 4, check_ascii);
break;
case 0x100+9:
printf("%c%c%c%c\n", response_buf[x+4], response_buf[x+5], response_buf[x+6], response_buf[x+7]);
print_chars(&response_buf[x+4], 4, check_ascii);
printf("\n");
break;
case 0x100+10:
printf("TPM_PT_VENDOR_TPM_TYPE: %" PRIu64 "\n", propertyValue);
Expand Down
7 changes: 6 additions & 1 deletion eltt2.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@
// TPM_PT constants
#define PT_FIXED_SELECTOR 1 ///< Fixed GetCapability Flags
#define PT_VAR_SELECTOR 2 ///< Variable GetCapability Flags
#define CHECK_ASCII 1 ///< Check printable characters of properties.
#define DONT_CHECK_ASCII 0 ///< Don't check printable characters of properties.

//-------------"Macros"-------------
// Null pointer check
Expand Down Expand Up @@ -284,11 +286,14 @@ static int tpmtool_transmit(const uint8_t *buf, ssize_t length, uint8_t *respons
* @retval buf_to_uint64 All error codes from buf_to_uint64.
* @date 2014/06/26
*/
static int print_capability_flags(uint8_t *response_buf, uint8_t cap_selector);
static int print_capability_flags(uint8_t *response_buf, uint8_t cap_selector, int check_ascii);

/**
* @brief Print the clock info.
* @param [in] *response_buf TPM response.
* @param [in] cap_selector The tag for the selected capability.
* @param [in] check_ascii Flag whether it should be checked whether characters to be printed
* are ASCII characters.
* @return One of the listed return codes.
* @retval EINVAL In case of a NULL pointer.
* @retval EXIT_SUCCESS In case of success.
Expand Down