~ubuntu-branches/ubuntu/jaunty/trousers/jaunty

« back to all changes in this revision

Viewing changes to src/tcs/tcs_key.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-01-23 22:03:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080123220300-fhtqja3c0oq0gp6z
Tags: 0.3.1-4
* Added patch from Aaron M. Ucko <ucko@debian.org> to allow trousers to
  build successfully on amd64, and presumably also other 64-bit
  architectures (Closes: #457400).
* Including udev rule for /dev/tpm from William Lima
  <wlima.amadeus@gmail.com> as suggested by David Smith <dds@google.com>
  (Closes: #459682).
* Added lintian overrides.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * Licensed Materials - Property of IBM
 
4
 *
 
5
 * trousers - An open source TCG Software Stack
 
6
 *
 
7
 * (C) Copyright International Business Machines Corp. 2004-2006
 
8
 *
 
9
 */
 
10
 
 
11
 
 
12
#include <string.h>
 
13
 
 
14
#include "trousers/tss.h"
 
15
#include "trousers_types.h"
 
16
#include "req_mgr.h"
 
17
#include "tcs_tsp.h"
 
18
#include "tcslog.h"
 
19
#include "tcs_utils.h"
 
20
#include "tcs_int_literals.h"
 
21
 
 
22
struct key_mem_cache *key_mem_cache_head = NULL;
 
23
 
 
24
TSS_UUID NULL_UUID = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } };
 
25
 
 
26
 
 
27
TSS_RESULT
 
28
add_cache_entry(TCS_CONTEXT_HANDLE hContext,
 
29
                BYTE*              blob,
 
30
                TCS_KEY_HANDLE     hParent,
 
31
                TPM_KEY_HANDLE     hSlot,
 
32
                TCS_KEY_HANDLE*    new)
 
33
{
 
34
        UINT64 offset;
 
35
        TSS_RESULT result;
 
36
        TCS_KEY_HANDLE tcsHandle;
 
37
        TSS_KEY key, *pKey;
 
38
 
 
39
        if (!blob) {
 
40
                pKey = NULL;
 
41
        } else {
 
42
                offset = 0;
 
43
                if ((result = UnloadBlob_TSS_KEY(&offset, blob, &key)))
 
44
                        return result;
 
45
 
 
46
                if ((tcsHandle = mc_get_handle_by_pub(&key.pubKey, hParent)) == NULL_TCS_HANDLE) {
 
47
                        pKey = &key;
 
48
                } else {
 
49
                        mc_set_slot_by_handle(tcsHandle, hSlot);
 
50
                        *new = tcsHandle;
 
51
                        goto done;
 
52
                }
 
53
        }
 
54
 
 
55
        LogDebugFn("No existing key handle for this key, creating new one...");
 
56
        /* Get a new TCS Key Handle */
 
57
        tcsHandle = getNextTcsKeyHandle();
 
58
        LogDebugFn("calling mc_add_entry, TCS handle: 0x%x, TPM handle 0x%x", tcsHandle, hSlot);
 
59
 
 
60
        if ((result = mc_add_entry(tcsHandle, hSlot, pKey)))
 
61
                goto done;
 
62
 
 
63
        LogDebugFn("ctx_mark_key_loaded");
 
64
        if (ctx_mark_key_loaded(hContext, tcsHandle)) {
 
65
                LogError("Error marking key as loaded");
 
66
                result = TCSERR(TSS_E_INTERNAL_ERROR);
 
67
                goto done;
 
68
        }
 
69
 
 
70
        if ((result = mc_set_parent_by_handle(tcsHandle, hParent))) {
 
71
                LogError("mc_set_parent_by_handle failed.");
 
72
                goto done;
 
73
        }
 
74
 
 
75
        *new = tcsHandle;
 
76
done:
 
77
        if (blob)
 
78
                destroy_key_refs(&key);
 
79
        return result;
 
80
}
 
81
 
 
82
/* Check that the context has this key loaded and return the associated slot. Do not search PS if
 
83
 * the key is not found */
 
84
TSS_RESULT
 
85
get_slot_lite(TCS_CONTEXT_HANDLE hContext, TCS_KEY_HANDLE hKey, TPM_KEY_HANDLE *out)
 
86
{
 
87
        if (ctx_has_key_loaded(hContext, hKey)) {
 
88
                if ((*out = mc_get_slot_by_handle(hKey)) == NULL_TPM_HANDLE)
 
89
                        return TCSERR(TCS_E_INVALID_KEY);
 
90
 
 
91
                return TSS_SUCCESS;
 
92
        }
 
93
 
 
94
        return TCSERR(TCS_E_INVALID_KEY);
 
95
}
 
96
 
 
97
/* XXX Can get_slot be merged with ensureKeyIsLoaded? */
 
98
 
 
99
/* Given a handle, get_slot searches the mem cache for a mapping to a TPM handle. If there is no
 
100
 * mapping, it looks up the pub key of the handle and attempts to load it by finding its pub key
 
101
 * in the persistent store. If that's not found, return error. */
 
102
TSS_RESULT
 
103
get_slot(TCS_CONTEXT_HANDLE hContext, TCS_KEY_HANDLE hKey, TPM_KEY_HANDLE *out)
 
104
{
 
105
        TSS_RESULT result = TSS_SUCCESS;
 
106
        TPM_STORE_PUBKEY *pub = NULL;
 
107
        TPM_KEY_HANDLE slot;
 
108
 
 
109
        LogDebugFn("calling mc_get_slot_by_handle");
 
110
        if ((slot = mc_get_slot_by_handle(hKey)) == NULL_TPM_HANDLE) {
 
111
                LogDebugFn("calling mc_get_pub_by_slot");
 
112
                if ((pub = mc_get_pub_by_slot(hKey)) == NULL)
 
113
                        return TCSERR(TCS_E_KM_LOADFAILED);
 
114
 
 
115
                LogDebugFn("calling LoadKeyShim");
 
116
                /* Otherwise, try to load it using the shim */
 
117
                result = LoadKeyShim(hContext, pub, NULL, &slot);
 
118
        }
 
119
 
 
120
        if (!result)
 
121
                *out = slot;
 
122
 
 
123
        return result;
 
124
}
 
125
 
 
126
/* load_key_init is the common entry point for all load key requests to the TCSD. These can come in
 
127
 * as straight load or load2 requests, or through a transport session.
 
128
 *
 
129
 * We'll always attempt to load the key if
 
130
 * A) It requires auth (load should fail if auth is bad, even when its already been loaded by
 
131
 *    another thread)
 
132
 * B) Its in a transport session (the key blob is encrypted)
 
133
 *
 
134
 * Otherwise if the key is already loaded by another thread and it doesn't require auth, then we
 
135
 * will just set *load_key to FALSE, telling the caller that there's no need to send anything to
 
136
 * the TPM.
 
137
 */
 
138
TSS_RESULT
 
139
load_key_init(TPM_COMMAND_CODE   ord,
 
140
              TCS_CONTEXT_HANDLE hContext,
 
141
              TCS_KEY_HANDLE     parent_handle,
 
142
              UINT32             blob_size,
 
143
              BYTE*              blob,
 
144
              TSS_BOOL           encrypted,
 
145
              TPM_AUTH*          auth,
 
146
              TSS_BOOL*          load_key,
 
147
              UINT64*            out_len,
 
148
              BYTE*              out,
 
149
              TCS_KEY_HANDLE*    handle,
 
150
              TPM_KEY_HANDLE*    slot)
 
151
{
 
152
        TSS_RESULT result = TSS_SUCCESS;
 
153
        TSS_KEY key;
 
154
        UINT64 offset;
 
155
        TPM_KEY_HANDLE tpm_slot;
 
156
        TCS_KEY_HANDLE tcs_handle;
 
157
        TSS_BOOL canLoad;
 
158
 
 
159
 
 
160
        if (!encrypted) {
 
161
                offset = 0;
 
162
                memset(&key, 0, sizeof(TSS_KEY));
 
163
                if ((result = UnloadBlob_TSS_KEY(&offset, blob, &key)))
 
164
                        return result;
 
165
        }
 
166
 
 
167
        if (!auth && !encrypted) {
 
168
                LogDebugFn("Checking if LoadKeyByBlob can be avoided by using existing key");
 
169
 
 
170
                if ((tcs_handle = mc_get_handle_by_pub(&key.pubKey, parent_handle))) {
 
171
                        LogDebugFn("tcs key handle exists");
 
172
 
 
173
                        tpm_slot = mc_get_slot_by_handle(tcs_handle);
 
174
                        if (tpm_slot && (isKeyLoaded(tpm_slot) == TRUE)) {
 
175
                                LogDebugFn("Don't need to reload this key.");
 
176
                                *handle = tcs_handle;
 
177
                                *slot = tpm_slot;
 
178
                                *load_key = FALSE;
 
179
                                goto done;
 
180
                        }
 
181
                }
 
182
        }
 
183
        *load_key = TRUE;
 
184
 
 
185
        LogDebugFn("calling canILoadThisKey");
 
186
        if (!encrypted) {
 
187
                if ((result = canILoadThisKey(&(key.algorithmParms), &canLoad)))
 
188
                        goto error;
 
189
 
 
190
                if (canLoad == FALSE) {
 
191
                        LogDebugFn("calling evictFirstKey");
 
192
                        /* Evict a key that isn't the parent */
 
193
                        if ((result = evictFirstKey(parent_handle)))
 
194
                                goto error;
 
195
                }
 
196
        }
 
197
 
 
198
error:
 
199
        if (!encrypted)
 
200
                destroy_key_refs(&key);
 
201
done:
 
202
        return result;
 
203
}
 
204
 
 
205
TSS_RESULT
 
206
load_key_final(TCS_CONTEXT_HANDLE hContext,
 
207
               TCS_KEY_HANDLE     parent_handle,
 
208
               TCS_KEY_HANDLE*    tcs_handle,
 
209
               BYTE*              blob,
 
210
               TPM_KEY_HANDLE     slot)
 
211
{
 
212
        if (*tcs_handle == NULL_TCS_HANDLE)
 
213
                return add_cache_entry(hContext, blob, parent_handle, slot, tcs_handle);
 
214
        else
 
215
                return mc_set_slot_by_handle(*tcs_handle, slot);
 
216
}
 
217
 
 
218
TSS_RESULT
 
219
canILoadThisKey(TCPA_KEY_PARMS *parms, TSS_BOOL *b)
 
220
{
 
221
        UINT16 subCapLength;
 
222
        UINT64 offset;
 
223
        BYTE subCap[100];
 
224
        TCPA_RESULT result;
 
225
        UINT32 respDataLength;
 
226
        BYTE *respData;
 
227
 
 
228
        offset = 0;
 
229
        LoadBlob_KEY_PARMS(&offset, subCap, parms);
 
230
        subCapLength = offset;
 
231
 
 
232
        if ((result = TCSP_GetCapability_Internal(InternalContext, TCPA_CAP_CHECK_LOADED,
 
233
                                                  subCapLength, subCap, &respDataLength,
 
234
                                                  &respData))) {
 
235
                *b = FALSE;
 
236
                LogDebugFn("NO");
 
237
                return result;
 
238
        }
 
239
 
 
240
        *b = respData[0];
 
241
        free(respData);
 
242
        LogDebugFn("%s", *b ? "YES" : "NO");
 
243
 
 
244
        return TSS_SUCCESS;
 
245
}
 
246
 
 
247
TCPA_RESULT
 
248
internal_EvictByKeySlot(TCPA_KEY_HANDLE slot)
 
249
{
 
250
        TCPA_RESULT result;
 
251
        UINT32 paramSize;
 
252
        UINT64 offset;
 
253
        BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
 
254
 
 
255
        LogDebug("Entering Evict Key");
 
256
 
 
257
        if (TPM_VERSION_IS(1,2)) {
 
258
                LogDebugFn("Evicting key using FlushSpecific for TPM 1.2");
 
259
 
 
260
                return TCSP_FlushSpecific_Common(slot, TPM_RT_KEY);
 
261
        }
 
262
 
 
263
        offset = 10;
 
264
        LoadBlob_UINT32(&offset, slot, txBlob);
 
265
        LoadBlob_Header(TPM_TAG_RQU_COMMAND, offset, TPM_ORD_EvictKey, txBlob);
 
266
 
 
267
        if ((result = req_mgr_submit_req(txBlob)))
 
268
                return result;
 
269
 
 
270
        result = UnloadBlob_Header(txBlob, &paramSize);
 
271
 
 
272
        LogResult("Evict Key", result);
 
273
        return result;
 
274
}
 
275
 
 
276
TSS_RESULT
 
277
clearUnknownKeys(TCS_CONTEXT_HANDLE hContext, UINT32 *cleared)
 
278
{
 
279
        TSS_RESULT result = TSS_SUCCESS;
 
280
        TCPA_KEY_HANDLE_LIST keyList = { 0, NULL };
 
281
        int i;
 
282
        BYTE *respData = NULL;
 
283
        UINT32 respDataSize = 0, count = 0;
 
284
        TCPA_CAPABILITY_AREA capArea = -1;
 
285
        UINT64 offset = 0;
 
286
        TSS_BOOL found = FALSE;
 
287
        struct key_mem_cache *tmp;
 
288
 
 
289
        capArea = TCPA_CAP_KEY_HANDLE;
 
290
 
 
291
        if ((result = TCSP_GetCapability_Internal(hContext, capArea, 0, NULL, &respDataSize,
 
292
                                                  &respData)))
 
293
                return result;
 
294
 
 
295
        if ((result = UnloadBlob_KEY_HANDLE_LIST(&offset, respData, &keyList)))
 
296
                goto done;
 
297
 
 
298
#ifdef TSS_DEBUG
 
299
        LogDebug("Loaded TPM key handles:");
 
300
        for (i = 0; i < keyList.loaded; i++) {
 
301
                LogDebugFn("%d: %x", i, keyList.handle[i]);
 
302
        }
 
303
 
 
304
        LogDebug("Loaded TCSD key handles:");
 
305
        i=0;
 
306
        for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
 
307
                LogDebugFn("%d: 0x%x -> 0x%x", i++, tmp->tpm_handle,
 
308
                            tmp->tcs_handle);
 
309
        }
 
310
#endif
 
311
 
 
312
        for (i = 0; i < keyList.loaded; i++) {
 
313
                /* as long as we're only called from evictFirstKey(), we don't
 
314
                 * need to lock here */
 
315
                for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
 
316
                        if (tmp->tpm_handle == keyList.handle[i]) {
 
317
                                found = TRUE;
 
318
                                break;
 
319
                        }
 
320
                }
 
321
                if (found)
 
322
                        found = FALSE;
 
323
                else {
 
324
                        if ((result = internal_EvictByKeySlot(keyList.handle[i])))
 
325
                                goto done;
 
326
                        else
 
327
                                count++;
 
328
                }
 
329
        }
 
330
 
 
331
        *cleared = count;
 
332
done:
 
333
        free(keyList.handle);
 
334
        free(respData);
 
335
 
 
336
        return TSS_SUCCESS;
 
337
}
 
338
 
 
339
#if 0
 
340
TCPA_RESULT
 
341
clearKeysFromChip(TCS_CONTEXT_HANDLE hContext)
 
342
{
 
343
        TCPA_RESULT result;
 
344
        TCPA_KEY_HANDLE_LIST keyList;
 
345
        UINT32 i;
 
346
        BYTE *respData = 0;
 
347
        UINT32 respDataSize = 0;
 
348
        TCPA_CAPABILITY_AREA capArea = -1;
 
349
        UINT64 offset = 0;
 
350
 
 
351
        capArea = TCPA_CAP_KEY_HANDLE;
 
352
 
 
353
        if ((result = TCSP_GetCapability_Internal(hContext, capArea, 0, NULL,
 
354
                                        &respDataSize, &respData)))
 
355
                return result;
 
356
 
 
357
        if ((result = UnloadBlob_KEY_HANDLE_LIST(&offset, respData, &keyList)))
 
358
                return result;
 
359
        for (i = 0; i < keyList.loaded; i++) {
 
360
                if (keyList.handle[i] == SRK_TPM_HANDLE ||      /*can't evict SRK */
 
361
                    keyList.handle[i] == EK_TPM_HANDLE) /*can't evict EK */
 
362
                        continue;
 
363
                if ((result = internal_EvictByKeySlot(keyList.handle[i])))
 
364
                        return result;
 
365
        }
 
366
        return TSS_SUCCESS;
 
367
}
 
368
#endif
 
369
 
 
370
void
 
371
LoadBlob_KEY_PARMS(UINT64 *offset, BYTE *blob, TCPA_KEY_PARMS *keyInfo)
 
372
{
 
373
        LoadBlob_UINT32(offset, keyInfo->algorithmID, blob);
 
374
        LoadBlob_UINT16(offset, keyInfo->encScheme, blob);
 
375
        LoadBlob_UINT16(offset, keyInfo->sigScheme, blob);
 
376
        LoadBlob_UINT32(offset, keyInfo->parmSize, blob);
 
377
        LoadBlob(offset, keyInfo->parmSize, blob, keyInfo->parms);
 
378
}
 
379
 
 
380
TSS_RESULT
 
381
UnloadBlob_STORE_PUBKEY(UINT64 *offset, BYTE *blob, TCPA_STORE_PUBKEY *store)
 
382
{
 
383
        if (!store) {
 
384
                UINT32 keyLength;
 
385
 
 
386
                UnloadBlob_UINT32(offset, &keyLength, blob);
 
387
 
 
388
                if (keyLength > 0)
 
389
                        UnloadBlob(offset, keyLength, blob, NULL);
 
390
 
 
391
                return TSS_SUCCESS;
 
392
        }
 
393
 
 
394
        UnloadBlob_UINT32(offset, &store->keyLength, blob);
 
395
 
 
396
        if (store->keyLength == 0) {
 
397
                store->key = NULL;
 
398
                LogWarn("Unloading a public key of size 0!");
 
399
        } else {
 
400
                store->key = (BYTE *)malloc(store->keyLength);
 
401
                if (store->key == NULL) {
 
402
                        LogError("malloc of %u bytes failed.", store->keyLength);
 
403
                        store->keyLength = 0;
 
404
                        return TCSERR(TSS_E_OUTOFMEMORY);
 
405
                }
 
406
 
 
407
                UnloadBlob(offset, store->keyLength, blob, store->key);
 
408
        }
 
409
 
 
410
        return TSS_SUCCESS;
 
411
}
 
412
 
 
413
void
 
414
LoadBlob_STORE_PUBKEY(UINT64 *offset, BYTE * blob, TCPA_STORE_PUBKEY * store)
 
415
{
 
416
        LoadBlob_UINT32(offset, store->keyLength, blob);
 
417
        LoadBlob(offset, store->keyLength, blob, store->key);
 
418
}
 
419
 
 
420
TSS_RESULT
 
421
UnloadBlob_TSS_KEY(UINT64 *offset, BYTE *blob, TSS_KEY *key)
 
422
{
 
423
        TSS_RESULT rc;
 
424
 
 
425
        if (!key) {
 
426
                UINT32 size;
 
427
 
 
428
                /* TPM_KEY's ver and TPM_KEY12's tag/file are
 
429
                   the same size, so... */
 
430
                UnloadBlob_VERSION(offset, blob, NULL);
 
431
                UnloadBlob_UINT16(offset, NULL, blob);
 
432
                UnloadBlob_KEY_FLAGS(offset, blob, NULL);
 
433
                UnloadBlob_BOOL(offset, NULL, blob);
 
434
                if ((rc = UnloadBlob_KEY_PARMS(offset, blob, NULL)))
 
435
                        return rc;
 
436
                UnloadBlob_UINT32(offset, &size, blob);
 
437
 
 
438
                if (size > 0)
 
439
                        UnloadBlob(offset, size, blob, NULL);
 
440
 
 
441
                if ((rc = UnloadBlob_STORE_PUBKEY(offset, blob, NULL)))
 
442
                        return rc;
 
443
 
 
444
                UnloadBlob_UINT32(offset, &size, blob);
 
445
 
 
446
                if (size > 0)
 
447
                        UnloadBlob(offset, size, blob, NULL);
 
448
 
 
449
                return TSS_SUCCESS;
 
450
        }
 
451
 
 
452
        if (key->hdr.key12.tag == TPM_TAG_KEY12) {
 
453
                UnloadBlob_UINT16(offset, &key->hdr.key12.tag, blob);
 
454
                UnloadBlob_UINT16(offset, &key->hdr.key12.fill, blob);
 
455
        } else
 
456
                UnloadBlob_TCPA_VERSION(offset, blob, &key->hdr.key11.ver);
 
457
        UnloadBlob_UINT16(offset, &key->keyUsage, blob);
 
458
        UnloadBlob_KEY_FLAGS(offset, blob, &key->keyFlags);
 
459
        UnloadBlob_BOOL(offset, (TSS_BOOL *)&key->authDataUsage, blob);
 
460
        if ((rc = UnloadBlob_KEY_PARMS(offset, blob, &key->algorithmParms)))
 
461
                return rc;
 
462
        UnloadBlob_UINT32(offset, &key->PCRInfoSize, blob);
 
463
 
 
464
        if (key->PCRInfoSize == 0)
 
465
                key->PCRInfo = NULL;
 
466
        else {
 
467
                key->PCRInfo = malloc(key->PCRInfoSize);
 
468
                if (key->PCRInfo == NULL) {
 
469
                        LogError("malloc of %u bytes failed.", key->PCRInfoSize);
 
470
                        key->PCRInfoSize = 0;
 
471
                        free(key->algorithmParms.parms);
 
472
                        key->algorithmParms.parms = NULL;
 
473
                        key->algorithmParms.parmSize = 0;
 
474
                        return TCSERR(TSS_E_OUTOFMEMORY);
 
475
                }
 
476
                UnloadBlob(offset, key->PCRInfoSize, blob, key->PCRInfo);
 
477
        }
 
478
 
 
479
        if ((rc = UnloadBlob_STORE_PUBKEY(offset, blob, &key->pubKey))) {
 
480
                free(key->PCRInfo);
 
481
                key->PCRInfo = NULL;
 
482
                key->PCRInfoSize = 0;
 
483
                free(key->algorithmParms.parms);
 
484
                key->algorithmParms.parms = NULL;
 
485
                key->algorithmParms.parmSize = 0;
 
486
                return rc;
 
487
        }
 
488
        UnloadBlob_UINT32(offset, &key->encSize, blob);
 
489
 
 
490
        if (key->encSize == 0)
 
491
                key->encData = NULL;
 
492
        else {
 
493
                key->encData = (BYTE *)malloc(key->encSize);
 
494
                if (key->encData == NULL) {
 
495
                        LogError("malloc of %d bytes failed.", key->encSize);
 
496
                        key->encSize = 0;
 
497
                        free(key->algorithmParms.parms);
 
498
                        key->algorithmParms.parms = NULL;
 
499
                        key->algorithmParms.parmSize = 0;
 
500
                        free(key->PCRInfo);
 
501
                        key->PCRInfo = NULL;
 
502
                        key->PCRInfoSize = 0;
 
503
                        free(key->pubKey.key);
 
504
                        key->pubKey.key = NULL;
 
505
                        key->pubKey.keyLength = 0;
 
506
                        return TCSERR(TSS_E_OUTOFMEMORY);
 
507
                }
 
508
                UnloadBlob(offset, key->encSize, blob, key->encData);
 
509
        }
 
510
 
 
511
        return TSS_SUCCESS;
 
512
}
 
513
 
 
514
void
 
515
LoadBlob_TSS_KEY(UINT64 *offset, BYTE * blob, TSS_KEY * key)
 
516
{
 
517
        if (key->hdr.key12.tag == TPM_TAG_KEY12) {
 
518
                LoadBlob_UINT16(offset, key->hdr.key12.tag, blob);
 
519
                LoadBlob_UINT16(offset, key->hdr.key12.fill, blob);
 
520
        } else
 
521
                LoadBlob_TCPA_VERSION(offset, blob, &key->hdr.key11.ver);
 
522
        LoadBlob_UINT16(offset, key->keyUsage, blob);
 
523
        LoadBlob_KEY_FLAGS(offset, blob, &key->keyFlags);
 
524
        LoadBlob_BOOL(offset, key->authDataUsage, blob);
 
525
        LoadBlob_KEY_PARMS(offset, blob, &key->algorithmParms);
 
526
        LoadBlob_UINT32(offset, key->PCRInfoSize, blob);
 
527
        LoadBlob(offset, key->PCRInfoSize, blob, key->PCRInfo);
 
528
        LoadBlob_STORE_PUBKEY(offset, blob, &key->pubKey);
 
529
        LoadBlob_UINT32(offset, key->encSize, blob);
 
530
        LoadBlob(offset, key->encSize, blob, key->encData);
 
531
}
 
532
 
 
533
void
 
534
LoadBlob_PUBKEY(UINT64 *offset, BYTE * blob, TCPA_PUBKEY * key)
 
535
{
 
536
        LoadBlob_KEY_PARMS(offset, blob, &(key->algorithmParms));
 
537
        LoadBlob_STORE_PUBKEY(offset, blob, &(key->pubKey));
 
538
}
 
539
 
 
540
TSS_RESULT
 
541
UnloadBlob_PUBKEY(UINT64 *offset, BYTE *blob, TCPA_PUBKEY *key)
 
542
{
 
543
        TSS_RESULT rc;
 
544
 
 
545
        if (!key) {
 
546
                if ((rc = UnloadBlob_KEY_PARMS(offset, blob, NULL)))
 
547
                        return rc;
 
548
                return UnloadBlob_STORE_PUBKEY(offset, blob, NULL);
 
549
        }
 
550
 
 
551
        if ((rc = UnloadBlob_KEY_PARMS(offset, blob, &key->algorithmParms)))
 
552
                return rc;
 
553
        if ((rc = UnloadBlob_STORE_PUBKEY(offset, blob, &key->pubKey))) {
 
554
                free(key->algorithmParms.parms);
 
555
                key->algorithmParms.parms = NULL;
 
556
                key->algorithmParms.parmSize = 0;
 
557
        }
 
558
 
 
559
        return rc;
 
560
}
 
561
 
 
562
void
 
563
LoadBlob_KEY_FLAGS(UINT64 *offset, BYTE * blob, TCPA_KEY_FLAGS * flags)
 
564
{
 
565
        LoadBlob_UINT32(offset, *flags, blob);
 
566
}
 
567
 
 
568
void
 
569
destroy_key_refs(TSS_KEY *key)
 
570
{
 
571
        free(key->algorithmParms.parms);
 
572
        key->algorithmParms.parms = NULL;
 
573
        key->algorithmParms.parmSize = 0;
 
574
 
 
575
        free(key->pubKey.key);
 
576
        key->pubKey.key = NULL;
 
577
        key->pubKey.keyLength = 0;
 
578
 
 
579
        free(key->encData);
 
580
        key->encData = NULL;
 
581
        key->encSize = 0;
 
582
 
 
583
        free(key->PCRInfo);
 
584
        key->PCRInfo = NULL;
 
585
        key->PCRInfoSize = 0;
 
586
}