Skip to content

Commit

Permalink
testing: Add some debugging output into swtpm_ioctl's -h path
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanberger committed Jan 18, 2020
1 parent 930c7ba commit 343a6bd
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/swtpm_ioctl/tpm_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,16 @@ static int do_hash_start_data_end(int fd, bool is_chardev, const char *input)

memset(&hdata, 0, sizeof(hdata));

fprintf(stderr, "%s @ %d\n", __func__, __LINE__);

if (!input) {
fprintf(stderr,
"Input buffer for hashing must not be NULL.\n");
return 1;
}

fprintf(stderr, "%s @ %d\n", __func__, __LINE__);

/* hash string given on command line */
n = ctrlcmd(fd, PTM_HASH_START, &res, 0, sizeof(res));
if (n < 0) {
Expand All @@ -169,18 +173,21 @@ static int do_hash_start_data_end(int fd, bool is_chardev, const char *input)
"%s\n", strerror(errno));
return 1;
}
fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
if (devtoh32(is_chardev, res) != 0) {
fprintf(stderr,
"TPM result from PTM_HASH_START: 0x%x\n",
devtoh32(is_chardev, res));
return 1;
}
fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
if (strlen(input) == 1 && input[0] == '-') {
/* read data from stdin */
while (1) {
idx = 0;
int c = 0;

fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
while (idx < sizeof(hdata.u.req.data)) {
c = fgetc(stdin);
if (c == EOF)
Expand All @@ -191,10 +198,12 @@ static int do_hash_start_data_end(int fd, bool is_chardev, const char *input)
}
hdata.u.req.length = htodev32(is_chardev, idx);

fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
n = ctrlcmd(fd, PTM_HASH_DATA, &hdata,
offsetof(ptm_hdata, u.req.data) + idx,
sizeof(hdata.u.resp));

fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
res = devtoh32(is_chardev, hdata.u.resp.tpm_result);
if (n < 0 || res != 0 || c == EOF)
break;
Expand All @@ -204,40 +213,49 @@ static int do_hash_start_data_end(int fd, bool is_chardev, const char *input)
while (idx < strlen(input)) {
size_t tocopy = strlen(input) - idx;

fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
if (tocopy > sizeof(hdata.u.req.data))
tocopy = sizeof(hdata.u.req.data);

fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
hdata.u.req.length = htodev32(is_chardev, tocopy);
memcpy(hdata.u.req.data, &input[idx], tocopy);
idx += tocopy;

fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
n = ctrlcmd(fd, PTM_HASH_DATA, &hdata,
offsetof(ptm_hdata, u.req.data) + tocopy,
sizeof(hdata.u.resp));

fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
res = devtoh32(is_chardev, hdata.u.resp.tpm_result);
if (n < 0 || res != 0)
break;
}
}
fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
if (n < 0) {
fprintf(stderr,
"Could not execute ioctl PTM_HASH_DATA: "
"%s\n", strerror(errno));
return 1;
}
fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
if (res != 0) {
fprintf(stderr,
"TPM result from PTM_HASH_DATA: 0x%x\n", res);
return 1;
}
fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
n = ctrlcmd(fd, PTM_HASH_END, &res, 0, sizeof(res));
fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
if (n < 0) {
fprintf(stderr,
"Could not execute ioctl PTM_HASH_END: "
"%s\n", strerror(errno));
return 1;
}
fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
if (devtoh32(is_chardev, res) != 0) {
fprintf(stderr,
"TPM result from PTM_HASH_END: 0x%x\n",
Expand Down Expand Up @@ -984,6 +1002,10 @@ int main(int argc, char *argv[])
goto exit;
}

if (command && !strcmp(command, "-h")) {
fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
}

if (!tpm_device && !tcp_hostname && !unix_path) {
if (optind == argc) {
fprintf(stderr, "Error: Missing device name.\n");
Expand All @@ -995,6 +1017,10 @@ int main(int argc, char *argv[])
}
}

if (command && !strcmp(command, "-h")) {
fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
}

is_chardev = (tpm_device != NULL);
if (is_chardev) {
tmp = getenv("SWTPM_IOCTL_BUFFERSIZE");
Expand All @@ -1004,11 +1030,20 @@ int main(int argc, char *argv[])
}
}

if (command && !strcmp(command, "-h")) {
fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
}

fd = open_connection(tpm_device, tcp_hostname, tcp_port, unix_path);
if (fd < 0) {
fprintf(stderr, "Could not create connection\n");
goto exit;
}

if (command && !strcmp(command, "-h")) {
fprintf(stderr, "%s @ %d\n", __func__, __LINE__);
}

if (!strcmp(command, "-c")) {
n = ctrlcmd(fd, PTM_GET_CAPABILITY, &cap, 0, sizeof(cap));
if (n < 0) {
Expand Down Expand Up @@ -1122,10 +1157,12 @@ int main(int argc, char *argv[])
}

} else if (!strcmp(command, "-h")) {
fprintf(stderr, "hashing now!\n");
if (do_hash_start_data_end(fd, is_chardev, hashdata)) {
fprintf(stderr, "hashing failed\n");
goto exit;
}

fprintf(stderr, "hashing succeeded\n");
} else if (!strcmp(command, "-C")) {
n = ctrlcmd(fd, PTM_CANCEL_TPM_CMD, &res, 0, sizeof(res));
if (n < 0) {
Expand Down Expand Up @@ -1231,5 +1268,10 @@ int main(int argc, char *argv[])

exit:
free(tcp_hostname);

if (command && !strcmp(command, "-h")) {
fprintf(stderr, "Exiting with ret=%d\n", ret);
}

return ret;
}

0 comments on commit 343a6bd

Please sign in to comment.