]> git.proxmox.com Git - libtpms.git/blame - src/tpm_tpm12_interface.c
Implement TPMLIB_GetInfo() to for example get TPM spec. info
[libtpms.git] / src / tpm_tpm12_interface.c
CommitLineData
39c9604a
SB
1/********************************************************************************/
2/* */
3/* LibTPM TPM 1.2 call interface functions */
4/* Written by Stefan Berger */
5/* IBM Thomas J. Watson Research Center */
6/* */
7/* (c) Copyright IBM Corporation 2015. */
8/* */
9/* All rights reserved. */
10/* */
11/* Redistribution and use in source and binary forms, with or without */
12/* modification, are permitted provided that the following conditions are */
13/* met: */
14/* */
15/* Redistributions of source code must retain the above copyright notice, */
16/* this list of conditions and the following disclaimer. */
17/* */
18/* Redistributions in binary form must reproduce the above copyright */
19/* notice, this list of conditions and the following disclaimer in the */
20/* documentation and/or other materials provided with the distribution. */
21/* */
22/* Neither the names of the IBM Corporation nor the names of its */
23/* contributors may be used to endorse or promote products derived from */
24/* this software without specific prior written permission. */
25/* */
26/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
27/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
28/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */
29/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
30/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
31/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
32/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
33/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */
34/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
35/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */
36/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
37/********************************************************************************/
38
39#include <config.h>
40
70547a75 41#define _GNU_SOURCE
39c9604a
SB
42#include <assert.h>
43#include <stdio.h>
44#include <stdlib.h>
70547a75 45#include <string.h>
39c9604a
SB
46
47#include "tpm12/tpm_debug.h"
48#include "tpm_error.h"
49#include "tpm12/tpm_init.h"
50#include "tpm_library_intern.h"
51#include "tpm12/tpm_process.h"
52#include "tpm12/tpm_startup.h"
e11dbf25
SB
53#include "tpm12/tpm_global.h"
54#include "tpm12/tpm_permanent.h"
39c9604a
SB
55
56TPM_RESULT TPM12_MainInit(void)
57{
58 return TPM_MainInit();
59}
60
61void TPM12_Terminate(void)
62{
63 TPM_Global_Delete(tpm_instances[0]);
64 free(tpm_instances[0]);
65 tpm_instances[0] = NULL;
66}
67
68TPM_RESULT TPM12_Process(unsigned char **respbuffer, uint32_t *resp_size,
69 uint32_t *respbufsize,
70 unsigned char *command, uint32_t command_size)
71{
72 *resp_size = 0;
73 return TPM_ProcessA(respbuffer, resp_size, respbufsize,
74 command, command_size);
75}
76
77TPM_RESULT TPM12_VolatileAllStore(unsigned char **buffer,
78 uint32_t *buflen)
79{
80 TPM_RESULT rc;
81 TPM_STORE_BUFFER tsb;
82 TPM_Sbuffer_Init(&tsb);
83 uint32_t total;
84
85#ifdef TPM_DEBUG
86 assert(tpm_instances[0] != NULL);
87#endif
88
89 rc = TPM_VolatileAll_Store(&tsb, tpm_instances[0]);
90
91 if (rc == TPM_SUCCESS) {
92 /* caller now owns the buffer and needs to free it */
93 TPM_Sbuffer_GetAll(&tsb, buffer, buflen, &total);
94 } else {
95 TPM_Sbuffer_Delete(&tsb);
96 *buflen = 0;
97 *buffer = NULL;
98 }
99
100 return rc;
101}
102
103TPM_RESULT TPM12_GetTPMProperty(enum TPMLIB_TPMProperty prop,
104 int *result)
105{
106 switch (prop) {
107 case TPMPROP_TPM_RSA_KEY_LENGTH_MAX:
108 *result = TPM_RSA_KEY_LENGTH_MAX;
109 break;
110
111 case TPMPROP_TPM_KEY_HANDLES:
112 *result = TPM_KEY_HANDLES;
113 break;
114
115 case TPMPROP_TPM_OWNER_EVICT_KEY_HANDLES:
116 *result = TPM_OWNER_EVICT_KEY_HANDLES;
117 break;
118
119 case TPMPROP_TPM_MIN_AUTH_SESSIONS:
120 *result = TPM_MIN_AUTH_SESSIONS;
121 break;
122
123 case TPMPROP_TPM_MIN_TRANS_SESSIONS:
124 *result = TPM_MIN_TRANS_SESSIONS;
125 break;
126
127 case TPMPROP_TPM_MIN_DAA_SESSIONS:
128 *result = TPM_MIN_DAA_SESSIONS;
129 break;
130
131 case TPMPROP_TPM_MIN_SESSION_LIST:
132 *result = TPM_MIN_SESSION_LIST;
133 break;
134
135 case TPMPROP_TPM_MIN_COUNTERS:
136 *result = TPM_MIN_COUNTERS;
137 break;
138
139 case TPMPROP_TPM_NUM_FAMILY_TABLE_ENTRY_MIN:
140 *result = TPM_NUM_FAMILY_TABLE_ENTRY_MIN;
141 break;
142
143 case TPMPROP_TPM_NUM_DELEGATE_TABLE_ENTRY_MIN:
144 *result = TPM_NUM_DELEGATE_TABLE_ENTRY_MIN;
145 break;
146
147 case TPMPROP_TPM_SPACE_SAFETY_MARGIN:
148 *result = TPM_SPACE_SAFETY_MARGIN;
149 break;
150
151 case TPMPROP_TPM_MAX_NV_SPACE:
152 /* fill up 20 kb.; this provides some safety margin (currently
153 >4Kb) for possible future expansion of this blob */
154 *result = ROUNDUP(TPM_MAX_NV_SPACE, 20 * 1024);
155 break;
156
157 case TPMPROP_TPM_MAX_SAVESTATE_SPACE:
158 *result = TPM_MAX_SAVESTATE_SPACE;
159 break;
160
161 case TPMPROP_TPM_MAX_VOLATILESTATE_SPACE:
162 *result = TPM_MAX_VOLATILESTATE_SPACE;
163 break;
164
165 default:
166 return TPM_FAIL;
167 }
168
169 return TPM_SUCCESS;
170}
171
70547a75
SB
172/*
173 * TPM12_GetInfo:
174 *
175 * @flags: logical or of flags that query for information
176 *
177 * Return a JSON document with contents queried for by the user's passed flags
178 */
179char *TPM12_GetInfo(enum TPMLIB_InfoFlags flags)
180{
181 const char *tpmspec =
182 "\"TPMSpecification\":{"
183 "\"family\":\"1.2\","
184 "\"level\":2,"
185 "\"revision\":116"
186 "}";
187 char *fmt = NULL, *buffer;
188
189 if (!(buffer = strdup("{%s%s}")))
190 return NULL;
191
192 if ((flags & TPMLIB_INFO_TPMSPECIFICATION)) {
193 fmt = buffer;
194 buffer = NULL;
195 if (asprintf(&buffer, fmt, tpmspec, "%s%s") < 0)
196 goto error;
197 free(fmt);
198 }
199
200 /* nothing else to add */
201 fmt = buffer;
202 buffer = NULL;
203 if (asprintf(&buffer, fmt, "","") < 0)
204 goto error;
205 free(fmt);
206
207 return buffer;
208
209error:
210 free(fmt);
211 free(buffer);
212
213 return NULL;
214}
215
ccdf2457
SB
216static uint32_t tpm12_buffersize = TPM_BUFFER_MAX;
217
ae3f105a
SB
218uint32_t TPM12_SetBufferSize(uint32_t wanted_size,
219 uint32_t *min_size,
220 uint32_t *max_size)
bc195a34 221{
ae3f105a 222 if (min_size)
ccdf2457 223 *min_size = TPM_BUFFER_MIN;
ae3f105a
SB
224 if (max_size)
225 *max_size = TPM_BUFFER_MAX;
ccdf2457 226
d77f29d6
SB
227 if (wanted_size == 0)
228 return tpm12_buffersize;
229
ccdf2457
SB
230 if (wanted_size > TPM_BUFFER_MAX)
231 wanted_size = TPM_BUFFER_MAX;
232 else if (wanted_size < TPM_BUFFER_MIN)
233 wanted_size = TPM_BUFFER_MIN;
234
235 tpm12_buffersize = wanted_size;
236
237 return tpm12_buffersize;
238}
239
240uint32_t TPM12_GetBufferSize(void)
241{
242 return TPM12_SetBufferSize(0, NULL, NULL);
bc195a34
SB
243}
244
e11dbf25
SB
245TPM_RESULT TPM12_ValidateState(enum TPMLIB_StateType st,
246 unsigned int flags)
247{
248 TPM_RESULT ret = TPM_SUCCESS;
249 tpm_state_t tpm_state;
250
251#ifdef TPM_LIBTPMS_CALLBACKS
252 struct libtpms_callbacks *cbs = TPMLIB_GetCallbacks();
253
254 if (cbs->tpm_nvram_init) {
255 ret = cbs->tpm_nvram_init();
256 if (ret != TPM_SUCCESS)
257 return ret;
258 }
259#endif
260
261 ret = TPM_Global_Init(&tpm_state);
262 tpm_state.tpm_number = 0;
263
aef3e6fd
SB
264 if ((ret == TPM_SUCCESS)) {
265 /* permanent state needs to be there and loaded first */
e11dbf25
SB
266 ret = TPM_PermanentAll_NVLoad(&tpm_state);
267 }
268
f9d92047 269 if ((ret == TPM_SUCCESS) && (st & TPMLIB_STATE_VOLATILE)) {
aef3e6fd 270 ret = TPM_VolatileAll_NVLoad(&tpm_state);
e11dbf25
SB
271 }
272
f9d92047 273 if ((ret == TPM_SUCCESS) && (st & TPMLIB_STATE_SAVE_STATE)) {
aef3e6fd 274 ret = TPM_SaveState_NVLoad(&tpm_state);
e11dbf25
SB
275 }
276
277 TPM_Global_Delete(&tpm_state);
278
279 return ret;
280}
281
39c9604a
SB
282const struct tpm_interface TPM12Interface = {
283 .MainInit = TPM12_MainInit,
284 .Terminate = TPM12_Terminate,
285 .Process = TPM12_Process,
286 .VolatileAllStore = TPM12_VolatileAllStore,
287 .GetTPMProperty = TPM12_GetTPMProperty,
70547a75 288 .GetInfo = TPM12_GetInfo,
39c9604a
SB
289 .TpmEstablishedGet = TPM12_IO_TpmEstablished_Get,
290 .HashStart = TPM12_IO_Hash_Start,
291 .HashData = TPM12_IO_Hash_Data,
292 .HashEnd = TPM12_IO_Hash_End,
bc195a34 293 .SetBufferSize = TPM12_SetBufferSize,
e11dbf25 294 .ValidateState = TPM12_ValidateState,
39c9604a 295};