2 * The Initial Developer of the Original Code is International
3 * Business Machines Corporation. Portions created by IBM
4 * Corporation are Copyright (C) 2007,2011 International Business
5 * Machines Corporation. All Rights Reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the Common Public License as published by
9 * IBM Corporation; either version 1 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * Common Public License for more details.
17 * You should have received a copy of the Common Public License
18 * along with this program; if not, a copy can be viewed at
19 * http://www.opensource.org/licenses/cpl1.0.php.
24 * \file src/tpm_readpcr.c
25 * \brief Read PCR values from TPM/TSS
26 * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
28 * cleanup 2011-04-26 SM
30 * Copy from tools v0.1.X
37 #include <unistd.h> // getopt
39 #include <tss/platform.h>
40 #include <tss/tss_defines.h>
41 #include <tss/tss_typedef.h>
42 #include <tss/tss_structs.h>
43 #include <tss/tss_error.h>
46 #include <openpts_log.h>
51 #define MAX_PCRNUM 24 // TPM v1.2
62 int hex2bin(void *dest, const void *src, size_t n) {
64 unsigned char *usdest = (unsigned char *) dest;
65 unsigned char *ussrc = (unsigned char *) src;
68 ERROR("ERROR: hex2bin wrong size %d\n", (int)n);
72 for (i = 0; i < (int)n / 2; i++) {
75 if ((0x30 <= ussrc[j]) && (ussrc[j] <= 0x39)) {
76 usdest[i] = (ussrc[j] - 0x30) << 4;
78 if ((0x41 <= ussrc[j]) && (ussrc[j] <= 0x46)) {
79 usdest[i] = (ussrc[j] - 56) << 4;
81 if ((0x61 <= ussrc[j]) && (ussrc[j] <= 0x66)) {
82 usdest[i] = (ussrc[j] - 87) << 4;
85 if ((0x30 <= ussrc[j + 1]) && (ussrc[j + 1] <= 0x39)) {
86 usdest[i] |= ussrc[j + 1] - 0x30;
88 if ((0x41 <= ussrc[j + 1]) && (ussrc[j + 1] <= 0x46)) {
89 usdest[i] |= ussrc[j + 1] - 56;
91 if ((0x61 <= ussrc[j + 1]) && (ussrc[j + 1] <= 0x66)) {
92 usdest[i] |= ussrc[j + 1] - 87;
99 void printhex(char *str, unsigned char *buf, int len) {
102 for (i = 0; i < len; i++)
103 printf("%02x", buf[i]);
107 void fprinthex(FILE * fp, char *str, unsigned char *buf, int len) {
109 fprintf(fp, "%s", str);
110 for (i = 0; i < len; i++)
111 fprintf(fp, "%02x", buf[i]);
115 void fprinthex2(FILE * fp, char *str, unsigned char *buf, int len) {
117 for (i = 0; i < len; i++) {
118 fprintf(fp, "%s", str);
119 fprintf(fp, "%02X", buf[i]);
126 fprintf(stderr, NLS(MS_OPENPTS, OPENPTS_TPM_READPCR_USAGE,
127 "OpenPTS command\n\n"
128 "Usage: tpm_readpcr [options]\n\n"
130 " -p pcr_index Set PCR index to read\n"
131 " -a Show all PCRs value (default)\n"
132 " -k Display PCR same as kernel format (/sys/class/misc/tpm0/device/pcrs)\n"
133 " -o filename Output to file (default is STDOUT)\n"
139 int main(int argc, char *argv[]) {
140 TSS_RESULT result = 0;
141 TSS_HCONTEXT hContext;
147 BYTE pcrSelect[MAX_PCRNUM];
157 char *filename = NULL;
162 memset(pcrSelect, 0, MAX_PCRNUM);
164 /* we parse the option args */
166 while ((ch = getopt(argc, argv, "p:hako:")) != EOF) {
170 pcrindex = atoi(optarg);
171 pcrSelect[pcrindex] = 1;
173 case 'a': /* all (default) */
176 case 'k': /* kernel */
179 case 'o': /* output file name */
181 fp = fopen(filename, "w");
193 memset(pcrSelect, 1, MAX_PCRNUM);
196 /* Connect to TCSD */
198 result = Tspi_Context_Create(&hContext);
199 if (result != TSS_SUCCESS) {
200 ERROR("ERROR: Tspi_Context_Create failed rc=0x%x\n",
205 result = Tspi_Context_Connect(hContext, SERVER);
206 if (result != TSS_SUCCESS) {
207 ERROR("ERROR: Tspi_Context_Connect failed rc=0x%x\n",
213 /* Get TPM handles */
214 result = Tspi_Context_GetTpmObject(hContext, &hTPM);
215 if (result != TSS_SUCCESS) {
216 ERROR("ERROR: Tspi_Context_GetTpmObject failed rc=0x%x\n",
222 subCap = TSS_TPMCAP_PROP_PCR;
224 Tspi_TPM_GetCapability(
231 pcrNum = *(UINT32 *) blob;
233 if (result != TSS_SUCCESS) {
234 ERROR("ERROR: Tspi_TPM_GetCapability failed rc=0x%x\n", result);
239 for (i = 0; i < pcrNum; i++) {
240 if (pcrSelect[i] == 1) {
242 Tspi_TPM_PcrRead(hTPM, i, &blobLength,
245 if (result != TSS_SUCCESS) {
246 ERROR("ERROR: Tspi_TPM_PcrRead failed rc=0x%x\n", result);
251 fprintf(fp, "PCR-%02d:", i);
252 fprinthex2(fp, " ", blob, blobLength);
254 fprintf(fp, "pcr.%d=", i);
255 fprinthex(fp, "", blob, blobLength);
257 Tspi_Context_FreeMemory(hContext, blob);
263 Tspi_Context_FreeMemory(hContext, NULL);
267 Tspi_Context_Close(hContext);