]> git.proxmox.com Git - libtpms.git/blob - src/tpm_library.c
Fix compile error on cygwin
[libtpms.git] / src / tpm_library.c
1 /********************************************************************************/
2 /* */
3 /* LibTPM interface functions */
4 /* Written by Stefan Berger */
5 /* IBM Thomas J. Watson Research Center */
6 /* $Id: tpm_library.c 4615 2011-08-30 15:35:24Z stefanb $ */
7 /* */
8 /* (c) Copyright IBM Corporation 2010. */
9 /* */
10 /* All rights reserved. */
11 /* */
12 /* Redistribution and use in source and binary forms, with or without */
13 /* modification, are permitted provided that the following conditions are */
14 /* met: */
15 /* */
16 /* Redistributions of source code must retain the above copyright notice, */
17 /* this list of conditions and the following disclaimer. */
18 /* */
19 /* Redistributions in binary form must reproduce the above copyright */
20 /* notice, this list of conditions and the following disclaimer in the */
21 /* documentation and/or other materials provided with the distribution. */
22 /* */
23 /* Neither the names of the IBM Corporation nor the names of its */
24 /* contributors may be used to endorse or promote products derived from */
25 /* this software without specific prior written permission. */
26 /* */
27 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
28 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
29 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */
30 /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
31 /* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
32 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
33 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
34 /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */
35 /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
36 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */
37 /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
38 /********************************************************************************/
39
40 #include <config.h>
41
42 #include <assert.h>
43 #include <string.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <ctype.h>
47
48 #ifdef USE_FREEBL_CRYPTO_LIBRARY
49 # include <plbase64.h>
50 #endif
51
52 #ifdef USE_OPENSSL_CRYPTO_LIBRARY
53 # include <openssl/bio.h>
54 # include <openssl/evp.h>
55 #endif
56
57 #include "tpm_debug.h"
58 #include "tpm_error.h"
59 #include "tpm_init.h"
60 #include "tpm_library.h"
61 #include "tpm_library_intern.h"
62 #include "tpm_key.h"
63 #include "tpm_memory.h"
64 #include "tpm_process.h"
65 #include "tpm_startup.h"
66
67 #define ROUNDUP(VAL, SIZE) \
68 ( ( (VAL) + (SIZE) - 1 ) / (SIZE) ) * (SIZE)
69
70
71
72 static const struct tags_and_indices {
73 const char *starttag;
74 const char *endtag;
75 } tags_and_indices[] = {
76 [TPMLIB_BLOB_TYPE_INITSTATE] =
77 {
78 .starttag = TPMLIB_INITSTATE_START_TAG,
79 .endtag = TPMLIB_INITSTATE_END_TAG,
80 },
81 };
82
83
84
85 uint32_t TPMLIB_GetVersion(void)
86 {
87 return TPM_LIBRARY_VERSION;
88 }
89
90 TPM_RESULT TPMLIB_MainInit(void)
91 {
92 return TPM_MainInit();
93 }
94
95
96 void TPMLIB_Terminate(void)
97 {
98 TPM_Global_Delete(tpm_instances[0]);
99 }
100
101
102 /*
103 * Send a command to the TPM. The command buffer must hold a well formatted
104 * TPM command and the command_size indicate the size of the command.
105 * The respbuffer parameter may be provided by the user and grow if
106 * the respbufsize size indicator is determined to be too small for the
107 * response. In that case a new buffer will be allocated and the size of that
108 * buffer returned in the respbufsize parameter. resp_size describes the
109 * size of the actual response within the respbuffer.
110 */
111 TPM_RESULT TPMLIB_Process(unsigned char **respbuffer, uint32_t *resp_size,
112 uint32_t *respbufsize,
113 unsigned char *command, uint32_t command_size)
114 {
115 *resp_size = 0;
116 return TPM_ProcessA(respbuffer, resp_size, respbufsize,
117 command, command_size);
118 }
119
120
121 /*
122 * Get the volatile state from the TPM. This function will return the
123 * buffer and the length of the buffer to the caller in case everything
124 * went alright.
125 */
126 TPM_RESULT TPMLIB_VolatileAll_Store(unsigned char **buffer,
127 uint32_t *buflen)
128 {
129 TPM_RESULT rc;
130 TPM_STORE_BUFFER tsb;
131 TPM_Sbuffer_Init(&tsb);
132 uint32_t total;
133
134 #ifdef TPM_DEBUG
135 assert(tpm_instances[0] != NULL);
136 #endif
137
138 rc = TPM_VolatileAll_Store(&tsb, tpm_instances[0]);
139
140 if (rc == TPM_SUCCESS) {
141 /* caller now owns the buffer and needs to free it */
142 TPM_Sbuffer_GetAll(&tsb, buffer, buflen, &total);
143 } else {
144 TPM_Sbuffer_Delete(&tsb);
145 *buflen = 0;
146 *buffer = NULL;
147 }
148
149 return rc;
150 }
151
152
153 /*
154 * Get a property of the TPM. The functions currently only
155 * return compile-time #defines but this may change in future
156 * versions where we may return parameters with which the TPM
157 * was created (rather than compiled).
158 */
159 TPM_RESULT TPMLIB_GetTPMProperty(enum TPMLIB_TPMProperty prop,
160 int *result)
161 {
162 switch (prop) {
163 case TPMPROP_TPM_RSA_KEY_LENGTH_MAX:
164 *result = TPM_RSA_KEY_LENGTH_MAX;
165 break;
166
167 case TPMPROP_TPM_BUFFER_MAX:
168 *result = TPM_BUFFER_MAX;
169 break;
170
171 case TPMPROP_TPM_KEY_HANDLES:
172 *result = TPM_KEY_HANDLES;
173 break;
174
175 case TPMPROP_TPM_OWNER_EVICT_KEY_HANDLES:
176 *result = TPM_OWNER_EVICT_KEY_HANDLES;
177 break;
178
179 case TPMPROP_TPM_MIN_AUTH_SESSIONS:
180 *result = TPM_MIN_AUTH_SESSIONS;
181 break;
182
183 case TPMPROP_TPM_MIN_TRANS_SESSIONS:
184 *result = TPM_MIN_TRANS_SESSIONS;
185 break;
186
187 case TPMPROP_TPM_MIN_DAA_SESSIONS:
188 *result = TPM_MIN_DAA_SESSIONS;
189 break;
190
191 case TPMPROP_TPM_MIN_SESSION_LIST:
192 *result = TPM_MIN_SESSION_LIST;
193 break;
194
195 case TPMPROP_TPM_MIN_COUNTERS:
196 *result = TPM_MIN_COUNTERS;
197 break;
198
199 case TPMPROP_TPM_NUM_FAMILY_TABLE_ENTRY_MIN:
200 *result = TPM_NUM_FAMILY_TABLE_ENTRY_MIN;
201 break;
202
203 case TPMPROP_TPM_NUM_DELEGATE_TABLE_ENTRY_MIN:
204 *result = TPM_NUM_DELEGATE_TABLE_ENTRY_MIN;
205 break;
206
207 case TPMPROP_TPM_SPACE_SAFETY_MARGIN:
208 *result = TPM_SPACE_SAFETY_MARGIN;
209 break;
210
211 case TPMPROP_TPM_MAX_NV_SPACE:
212 /* fill up 20 kb.; this provides some safety margin (currently
213 >4Kb) for possible future expansion of this blob */
214 *result = ROUNDUP(TPM_MAX_NV_SPACE, 20 * 1024);
215 break;
216
217 case TPMPROP_TPM_MAX_SAVESTATE_SPACE:
218 *result = TPM_MAX_SAVESTATE_SPACE;
219 break;
220
221 case TPMPROP_TPM_MAX_VOLATILESTATE_SPACE:
222 *result = TPM_MAX_VOLATILESTATE_SPACE;
223 break;
224
225 default:
226 return TPM_FAIL;
227 }
228
229 return TPM_SUCCESS;
230 }
231
232 static struct libtpms_callbacks libtpms_cbs;
233
234 struct libtpms_callbacks *TPMLIB_GetCallbacks(void)
235 {
236 return &libtpms_cbs;
237 }
238
239
240 TPM_RESULT TPMLIB_RegisterCallbacks(struct libtpms_callbacks *callbacks)
241 {
242 int max_size = sizeof(struct libtpms_callbacks);
243
244 /* restrict the size of the structure to what we know currently
245 future versions may know more callbacks */
246 if (callbacks->sizeOfStruct < max_size)
247 max_size = callbacks->sizeOfStruct;
248
249 /* clear the internal callback structure and copy the user provided
250 callbacks into it */
251 memset(&libtpms_cbs, 0x0, sizeof(libtpms_cbs));
252 memcpy(&libtpms_cbs, callbacks, max_size);
253
254 return TPM_SUCCESS;
255 }
256
257
258 static int is_base64ltr(char c)
259 {
260 return ((c >= 'A' && c <= 'Z') ||
261 (c >= 'a' && c <= 'z') ||
262 (c >= '0' && c <= '9') ||
263 c == '+' ||
264 c == '/' ||
265 c == '=');
266 }
267
268 #ifdef USE_OPENSSL_CRYPTO_LIBRARY
269 static unsigned char *TPMLIB_OpenSSL_Base64Decode(char *input,
270 unsigned int outputlen)
271 {
272 BIO *b64, *bmem;
273 unsigned char *res = NULL;
274 int n;
275 TPM_RESULT rc;
276
277 b64 = BIO_new(BIO_f_base64());
278 if (!b64) {
279 return NULL;
280 }
281
282 bmem = BIO_new_mem_buf(input, strlen(input));
283 if (!bmem) {
284 BIO_free(b64);
285 goto cleanup;
286 }
287 bmem = BIO_push(b64, bmem);
288 BIO_set_flags(bmem, BIO_FLAGS_BASE64_NO_NL);
289
290 rc = TPM_Malloc(&res, outputlen);
291 if (rc != TPM_SUCCESS) {
292 goto cleanup;
293 }
294
295 n = BIO_read(bmem, res, outputlen);
296 if (n <= 0) {
297 TPM_Free(res);
298 res = NULL;
299 goto cleanup;
300 }
301
302 cleanup:
303 BIO_free_all(bmem);
304
305 return res;
306 }
307 #endif
308
309 /*
310 * Base64 decode the string starting at 'start' and the last
311 * valid character may be a 'end'. The length of the decoded string
312 * is returned in *length.
313 */
314 static unsigned char *TPMLIB_Base64Decode(const char *start, const char *end,
315 size_t *length)
316 {
317 unsigned char *ret = NULL;
318 char *input = NULL, *d;
319 const char *s;
320 char c;
321 unsigned int numbase64chars = 0;
322
323 if (end < start)
324 return NULL;
325
326 while (end > start && !is_base64ltr(*end))
327 end--;
328
329 end++;
330
331 if (TPM_Malloc((unsigned char **)&input, end - start + 1) != TPM_SUCCESS)
332 return NULL;
333
334 /* copy from source string skipping '\n' and '\r' and using
335 '=' to calculate the exact length */
336 d = input;
337 s = start;
338
339 while (s < end) {
340 c = *s;
341 if (is_base64ltr(c)) {
342 *d = c;
343 d++;
344 if (c != '=') {
345 numbase64chars++;
346 }
347 } else if (c == 0) {
348 break;
349 }
350 s++;
351 }
352 *d = 0;
353
354 *length = (numbase64chars / 4) * 3;
355 switch (numbase64chars % 4) {
356 case 2:
357 case 3:
358 *length += (numbase64chars % 4) - 1;
359 break;
360 case 0:
361 break;
362 case 1:
363 fprintf(stderr,"malformed base64\n");
364 goto err_exit;
365 break;
366 }
367
368 #ifdef USE_FREEBL_CRYPTO_LIBRARY
369 ret = (unsigned char *)PL_Base64Decode(input, 0, NULL);
370 #endif
371
372 #ifdef USE_OPENSSL_CRYPTO_LIBRARY
373 ret = TPMLIB_OpenSSL_Base64Decode(input, *length);
374 #endif
375
376 err_exit:
377 free(input);
378
379 return ret;
380 }
381
382
383 static unsigned char *TPMLIB_GetPlaintext(const char *stream,
384 const char *starttag,
385 const char *endtag,
386 size_t *length)
387 {
388 char *start, *end;
389 unsigned char *plaintext = NULL;
390
391 start = strstr(stream, starttag);
392 if (start) {
393 start += strlen(starttag);
394 while (isspace((int)*start))
395 start++;
396 end = strstr(start, endtag);
397 if (end) {
398 plaintext = TPMLIB_Base64Decode(start, --end, length);
399 }
400 }
401 return plaintext;
402 }
403
404
405 TPM_RESULT TPMLIB_DecodeBlob(const char *buffer, enum TPMLIB_BlobType type,
406 unsigned char **result, size_t *result_len)
407 {
408 TPM_RESULT res = TPM_SUCCESS;
409
410 *result = TPMLIB_GetPlaintext(buffer,
411 tags_and_indices[type].starttag,
412 tags_and_indices[type].endtag,
413 result_len);
414
415 if (*result == NULL) {
416 res = TPM_FAIL;
417 }
418
419 return res;
420 }
421