OSDN Git Service

aix feeadback
[openpts/openpts.git] / src / tpm_readpcr.c
1 /*
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.
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
20  */
21
22
23 /**
24  * \file src/tpm_readpcr.c
25  * \brief Read PCR values from TPM/TSS
26  * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
27  * @date 2011-03-15
28  * cleanup 2011-04-26 SM
29  *
30  * Copy from tools v0.1.X
31  *
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>  // getopt
38
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>
44 #include <tss/tspi.h>
45
46 #include <openpts_log.h>
47
48 // Local TCSD
49 #define SERVER    NULL
50
51 #define MAX_PCRNUM 24  // TPM v1.2
52
53 /*
54
55  TPM PCR Read
56
57  Usage:
58   tpm_readpcr -p index
59
60  */
61
62 int hex2bin(void *dest, const void *src, size_t n) {
63     int i, j;
64     unsigned char *usdest = (unsigned char *) dest;
65     unsigned char *ussrc = (unsigned char *) src;
66
67     if (n & 0x01) {
68         ERROR("ERROR: hex2bin wrong size %d\n", (int)n);
69         return -1;
70     }
71
72     for (i = 0; i < (int)n / 2; i++) {
73         j = i * 2;
74         usdest[i] = 0;
75         if ((0x30 <= ussrc[j]) && (ussrc[j] <= 0x39)) {
76             usdest[i] = (ussrc[j] - 0x30) << 4;
77         }
78         if ((0x41 <= ussrc[j]) && (ussrc[j] <= 0x46)) {
79             usdest[i] = (ussrc[j] - 56) << 4;
80         }
81         if ((0x61 <= ussrc[j]) && (ussrc[j] <= 0x66)) {
82             usdest[i] = (ussrc[j] - 87) << 4;
83         }
84
85         if ((0x30 <= ussrc[j + 1]) && (ussrc[j + 1] <= 0x39)) {
86             usdest[i] |= ussrc[j + 1] - 0x30;
87         }
88         if ((0x41 <= ussrc[j + 1]) && (ussrc[j + 1] <= 0x46)) {
89             usdest[i] |= ussrc[j + 1] - 56;
90         }
91         if ((0x61 <= ussrc[j + 1]) && (ussrc[j + 1] <= 0x66)) {
92             usdest[i] |= ussrc[j + 1] - 87;
93         }
94     }
95
96     return i;
97 }
98
99 void printhex(char *str, unsigned char *buf, int len) {
100     int i;
101     printf("%s", str);
102     for (i = 0; i < len; i++)
103         printf("%02x", buf[i]);
104     printf("\n");
105 }
106
107 void fprinthex(FILE * fp, char *str, unsigned char *buf, int len) {
108     int i;
109     fprintf(fp, "%s", str);
110     for (i = 0; i < len; i++)
111         fprintf(fp, "%02x", buf[i]);
112     fprintf(fp, "\n");
113 }
114
115 void fprinthex2(FILE * fp, char *str, unsigned char *buf, int len) {
116     int i;
117     for (i = 0; i < len; i++) {
118         fprintf(fp, "%s", str);
119         fprintf(fp, "%02X", buf[i]);
120     }
121     fprintf(fp, "\n");
122 }
123
124
125 void usage(void) {
126     fprintf(stderr, NLS(MS_OPENPTS, OPENPTS_TPM_READPCR_USAGE,
127                     "OpenPTS command\n\n"
128                     "Usage: tpm_readpcr [options]\n\n"
129                     "Options:\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"
134                     "  -h                    Help\n"
135                     "\n"));
136 }
137
138
139 int main(int argc, char *argv[]) {
140     TSS_RESULT result = 0;
141     TSS_HCONTEXT hContext;
142
143     TSS_HTPM hTPM;
144
145     BYTE *blob;
146     UINT32 blobLength;
147     BYTE pcrSelect[MAX_PCRNUM];
148     UINT32 subCap;
149
150     int ch;
151     int pcrindex;
152     int i;
153     int pcrNum =16;
154     int kernel = 0;
155     int all = 1;
156
157     char *filename = NULL;
158     FILE *fp = stdout;
159
160     initCatalog();
161
162     memset(pcrSelect, 0, MAX_PCRNUM);
163
164     /* we parse the option args */
165
166     while ((ch = getopt(argc, argv, "p:hako:")) != EOF) {
167         switch (ch) {
168         case 'p':  /* PCR */
169             all = 0;
170             pcrindex = atoi(optarg);
171             pcrSelect[pcrindex] = 1;
172             break;
173         case 'a':  /* all (default) */
174             all = 1;
175             break;
176         case 'k':  /* kernel */
177             kernel = 1;
178             break;
179         case 'o':  /* output file name */
180             filename = optarg;
181             fp = fopen(filename, "w");
182             break;
183         case 'h':  /* help */
184             usage();
185             goto fclose;
186         default:
187             usage();
188             goto fclose;
189         }
190     }
191
192     if (all == 1) {
193         memset(pcrSelect, 1, MAX_PCRNUM);
194     }
195
196     /* Connect to TCSD */
197
198     result = Tspi_Context_Create(&hContext);
199     if (result != TSS_SUCCESS) {
200         ERROR("ERROR: Tspi_Context_Create failed rc=0x%x\n",
201               result);
202         goto close;
203     }
204
205     result = Tspi_Context_Connect(hContext, SERVER);
206     if (result != TSS_SUCCESS) {
207         ERROR("ERROR: Tspi_Context_Connect failed rc=0x%x\n",
208               result);
209         goto close;
210     }
211
212
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",
217               result);
218         goto close;
219     }
220
221     /* get PCR num */
222     subCap = TSS_TPMCAP_PROP_PCR;
223     result =
224         Tspi_TPM_GetCapability(
225             hTPM,
226             TSS_TPMCAP_PROPERTY,
227             sizeof(UINT32),
228             (BYTE*) &subCap,
229             &blobLength,
230             &blob);
231     pcrNum = *(UINT32 *) blob;
232
233     if (result != TSS_SUCCESS) {
234         ERROR("ERROR: Tspi_TPM_GetCapability failed rc=0x%x\n", result);
235         goto free;
236     }
237
238     /* Print */
239     for (i = 0; i < pcrNum; i++) {
240         if (pcrSelect[i] == 1) {
241             result =
242                 Tspi_TPM_PcrRead(hTPM, i, &blobLength,
243                                  &blob);
244
245             if (result != TSS_SUCCESS) {
246                 ERROR("ERROR: Tspi_TPM_PcrRead failed rc=0x%x\n", result);
247                 goto free;
248             }
249
250             if (kernel == 1) {
251                 fprintf(fp, "PCR-%02d:", i);
252                 fprinthex2(fp, " ", blob, blobLength);
253             } else {
254                 fprintf(fp, "pcr.%d=", i);
255                 fprinthex(fp, "", blob, blobLength);
256             }
257             Tspi_Context_FreeMemory(hContext, blob);
258         }
259     }
260
261
262   free:
263     Tspi_Context_FreeMemory(hContext, NULL);
264
265     /* Close TSS/TPM */
266   close:
267     Tspi_Context_Close(hContext);
268
269   fclose:
270     fclose(fp);
271
272     return result;
273 }