~ubuntu-branches/ubuntu/precise/trousers/precise-proposed

« back to all changes in this revision

Viewing changes to src/tcsd/tcsd_wrap.c

  • Committer: Bazaar Package Importer
  • Author(s): William Lima
  • Date: 2007-04-18 16:39:38 UTC
  • Revision ID: james.westby@ubuntu.com-20070418163938-opscl2mvvi76jiec
Tags: upstream-0.2.9.1
ImportĀ upstreamĀ versionĀ 0.2.9.1

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
#include <stdlib.h>
 
12
#include <stdio.h>
 
13
#include <syslog.h>
 
14
#include <string.h>
 
15
#include <netdb.h>
 
16
 
 
17
#include "trousers/tss.h"
 
18
#include "spi_internal_types.h"
 
19
#include "tcs_internal_types.h"
 
20
#include "tcs_tsp.h"
 
21
#include "tcs_utils.h"
 
22
#include "tcs_int_literals.h"
 
23
#include "capabilities.h"
 
24
#include "tcslog.h"
 
25
#include "tcsd_wrap.h"
 
26
#include "tcsd.h"
 
27
#include "tcs_utils.h"
 
28
 
 
29
pthread_mutex_t tcsp_lock = PTHREAD_MUTEX_INITIALIZER;
 
30
 
 
31
void
 
32
LoadBlob_Auth_Special(UINT64 *offset, BYTE *blob, TPM_AUTH *auth)
 
33
{
 
34
        LoadBlob(offset, TCPA_SHA1BASED_NONCE_LEN, blob, auth->NonceEven.nonce, NULL);
 
35
        LoadBlob_BOOL(offset, auth->fContinueAuthSession, blob, NULL);
 
36
        LoadBlob(offset, TCPA_SHA1BASED_NONCE_LEN, blob, (BYTE *)&auth->HMAC, NULL);
 
37
}
 
38
 
 
39
void
 
40
UnloadBlob_Auth_Special(UINT64 *offset, BYTE *blob, TPM_AUTH *auth)
 
41
{
 
42
        UnloadBlob_UINT32(offset, &auth->AuthHandle, blob, NULL);
 
43
        UnloadBlob(offset, TCPA_SHA1BASED_NONCE_LEN, blob, auth->NonceOdd.nonce, NULL);
 
44
        UnloadBlob_BOOL(offset, &auth->fContinueAuthSession, blob, NULL);
 
45
        UnloadBlob(offset, TCPA_SHA1BASED_NONCE_LEN, blob, (BYTE *)&auth->HMAC, NULL);
 
46
}
 
47
 
 
48
void
 
49
LoadBlob_KM_KEYINFO(UINT64 *offset, BYTE *blob, TSS_KM_KEYINFO *info)
 
50
{
 
51
        LoadBlob_VERSION(offset, blob, (TCPA_VERSION *)&(info->versionInfo));
 
52
        LoadBlob_UUID(offset, blob, info->keyUUID);
 
53
        LoadBlob_UUID(offset, blob, info->parentKeyUUID);
 
54
        LoadBlob_BYTE(offset, info->bAuthDataUsage, blob, NULL);
 
55
        LoadBlob_BOOL(offset, info->fIsLoaded, blob, NULL);
 
56
        LoadBlob_UINT32(offset, info->ulVendorDataLength, blob, NULL);
 
57
        LoadBlob(offset, info->ulVendorDataLength, blob, info->rgbVendorData, NULL);
 
58
}
 
59
 
 
60
void
 
61
UnloadBlob_KM_KEYINFO(UINT64 *offset, BYTE *blob, TSS_KM_KEYINFO *info)
 
62
{
 
63
        UnloadBlob_VERSION(offset, blob, (TCPA_VERSION *)&(info->versionInfo));
 
64
        UnloadBlob_UUID(offset, blob, &info->keyUUID);
 
65
        UnloadBlob_UUID(offset, blob, &info->parentKeyUUID);
 
66
        UnloadBlob_BYTE(offset, blob, &info->bAuthDataUsage, NULL);
 
67
        UnloadBlob_BOOL(offset, &info->fIsLoaded, blob, NULL);
 
68
        UnloadBlob_UINT32(offset, &info->ulVendorDataLength, blob, NULL);
 
69
        UnloadBlob(offset, info->ulVendorDataLength, info->rgbVendorData, blob, NULL);
 
70
}
 
71
 
 
72
void
 
73
LoadBlob_LOADKEY_INFO(UINT64 *offset, BYTE *blob, TCS_LOADKEY_INFO *info)
 
74
{
 
75
        LoadBlob_UUID(offset, blob, info->keyUUID);
 
76
        LoadBlob_UUID(offset, blob, info->parentKeyUUID);
 
77
        LoadBlob(offset, TCPA_DIGEST_SIZE, blob, info->paramDigest.digest, NULL);
 
78
        LoadBlob_UINT32(offset, info->authData.AuthHandle, blob, NULL);
 
79
        LoadBlob(offset, TCPA_NONCE_SIZE, blob, info->authData.NonceOdd.nonce, NULL);
 
80
        LoadBlob(offset, TCPA_NONCE_SIZE, blob, info->authData.NonceEven.nonce, NULL);
 
81
        LoadBlob_BOOL(offset, info->authData.fContinueAuthSession, blob, NULL);
 
82
        LoadBlob(offset, TCPA_AUTHDATA_SIZE, blob, (BYTE *)&info->authData.HMAC, NULL);
 
83
}
 
84
 
 
85
void
 
86
UnloadBlob_LOADKEY_INFO(UINT64 *offset, BYTE *blob, TCS_LOADKEY_INFO *info)
 
87
{
 
88
        UnloadBlob_UUID(offset, blob, &info->keyUUID);
 
89
        UnloadBlob_UUID(offset, blob, &info->parentKeyUUID);
 
90
        UnloadBlob(offset, TCPA_DIGEST_SIZE, blob, info->paramDigest.digest, NULL);
 
91
        UnloadBlob_UINT32(offset, &info->authData.AuthHandle, blob, NULL);
 
92
        UnloadBlob(offset, TCPA_NONCE_SIZE, blob, (BYTE *)&info->authData.NonceOdd.nonce, NULL);
 
93
        UnloadBlob(offset, TCPA_NONCE_SIZE, blob, (BYTE *)&info->authData.NonceEven.nonce, NULL);
 
94
        UnloadBlob_BOOL(offset, &info->authData.fContinueAuthSession, blob, NULL);
 
95
        UnloadBlob(offset, TCPA_DIGEST_SIZE, blob, (BYTE *)&info->authData.HMAC, NULL);
 
96
}
 
97
 
 
98
void
 
99
LoadBlob_PCR_EVENT(UINT64 *offset, BYTE *blob, TSS_PCR_EVENT *event)
 
100
{
 
101
        LoadBlob_VERSION(offset, blob, (TCPA_VERSION *)&(event->versionInfo));
 
102
        LoadBlob_UINT32(offset, event->ulPcrIndex, blob, NULL);
 
103
        LoadBlob_UINT32(offset, event->eventType, blob, NULL);
 
104
 
 
105
        LoadBlob_UINT32(offset, event->ulPcrValueLength, blob, NULL);
 
106
        if (event->ulPcrValueLength > 0)
 
107
                LoadBlob(offset, event->ulPcrValueLength, blob, event->rgbPcrValue, NULL);
 
108
 
 
109
        LoadBlob_UINT32(offset, event->ulEventLength, blob, NULL);
 
110
        if (event->ulEventLength > 0)
 
111
                LoadBlob(offset, event->ulEventLength, blob, event->rgbEvent, NULL);
 
112
 
 
113
}
 
114
 
 
115
TSS_RESULT
 
116
UnloadBlob_PCR_EVENT(UINT64 *offset, BYTE *blob, TSS_PCR_EVENT *event)
 
117
{
 
118
        UnloadBlob_VERSION(offset, blob, (TCPA_VERSION *)&(event->versionInfo));
 
119
        UnloadBlob_UINT32(offset, &event->ulPcrIndex, blob, NULL);
 
120
        UnloadBlob_UINT32(offset, &event->eventType, blob, NULL);
 
121
 
 
122
        UnloadBlob_UINT32(offset, &event->ulPcrValueLength, blob, NULL);
 
123
        if (event->ulPcrValueLength > 0) {
 
124
                event->rgbPcrValue = malloc(event->ulPcrValueLength);
 
125
                if (event->rgbPcrValue == NULL) {
 
126
                        LogError("malloc of %d bytes failed.", event->ulPcrValueLength);
 
127
                        return TCSERR(TSS_E_OUTOFMEMORY);
 
128
                }
 
129
 
 
130
                UnloadBlob(offset, event->ulPcrValueLength, blob, event->rgbPcrValue, NULL);
 
131
        } else {
 
132
                event->rgbPcrValue = NULL;
 
133
        }
 
134
 
 
135
        UnloadBlob_UINT32(offset, &event->ulEventLength, blob, NULL);
 
136
        if (event->ulEventLength > 0) {
 
137
                event->rgbEvent = malloc(event->ulEventLength);
 
138
                if (event->rgbEvent == NULL) {
 
139
                        LogError("malloc of %d bytes failed.", event->ulEventLength);
 
140
                        free(event->rgbPcrValue);
 
141
                        return TCSERR(TSS_E_OUTOFMEMORY);
 
142
                }
 
143
 
 
144
                UnloadBlob(offset, event->ulEventLength, blob, event->rgbEvent, NULL);
 
145
        } else {
 
146
                event->rgbEvent = NULL;
 
147
        }
 
148
 
 
149
        return TSS_SUCCESS;
 
150
}
 
151
 
 
152
void
 
153
initData(struct tcsd_comm_data *comm, int parm_count)
 
154
{
 
155
        /* min packet size should be the size of the header */
 
156
        memset(&comm->hdr, 0, sizeof(struct tcsd_packet_hdr));
 
157
        comm->hdr.packet_size = sizeof(struct tcsd_packet_hdr);
 
158
        if (parm_count > 0) {
 
159
                comm->hdr.type_offset = sizeof(struct tcsd_packet_hdr);
 
160
                comm->hdr.parm_offset = comm->hdr.type_offset +
 
161
                                        (sizeof(TCSD_PACKET_TYPE) * parm_count);
 
162
                comm->hdr.packet_size = comm->hdr.parm_offset;
 
163
        }
 
164
 
 
165
        memset(comm->buf, 0, comm->buf_size);
 
166
}
 
167
 
 
168
int
 
169
loadData(UINT64 *offset, TCSD_PACKET_TYPE data_type, void *data, int data_size, BYTE *blob)
 
170
{
 
171
        switch (data_type) {
 
172
        case TCSD_PACKET_TYPE_BYTE:
 
173
                LoadBlob_BYTE(offset, *((BYTE *) (data)), blob, NULL);
 
174
                break;
 
175
        case TCSD_PACKET_TYPE_BOOL:
 
176
                LoadBlob_BOOL(offset, *((TSS_BOOL *) (data)), blob, NULL);
 
177
                break;
 
178
        case TCSD_PACKET_TYPE_UINT16:
 
179
                LoadBlob_UINT16(offset, *((UINT16 *) (data)), blob, NULL);
 
180
                break;
 
181
        case TCSD_PACKET_TYPE_UINT32:
 
182
                LoadBlob_UINT32(offset, *((UINT32 *) (data)), blob, NULL);
 
183
                break;
 
184
        case TCSD_PACKET_TYPE_PBYTE:
 
185
                LoadBlob(offset, data_size, blob, data, NULL);
 
186
                break;
 
187
        case TCSD_PACKET_TYPE_NONCE:
 
188
                LoadBlob(offset, sizeof(TCPA_NONCE), blob, ((TCPA_NONCE *)data)->nonce, NULL);
 
189
                break;
 
190
        case TCSD_PACKET_TYPE_DIGEST:
 
191
                LoadBlob(offset, sizeof(TCPA_DIGEST), blob, ((TCPA_DIGEST *)data)->digest, NULL);
 
192
                break;
 
193
        case TCSD_PACKET_TYPE_AUTH:
 
194
                LoadBlob_Auth_Special(offset, blob, ((TPM_AUTH *)data));
 
195
                break;
 
196
        case TCSD_PACKET_TYPE_UUID:
 
197
                LoadBlob_UUID(offset, blob, *((TSS_UUID *)data));
 
198
                break;
 
199
        case TCSD_PACKET_TYPE_ENCAUTH:
 
200
                LoadBlob(offset, sizeof(TCPA_ENCAUTH), blob, ((TCPA_ENCAUTH *)data)->authdata, NULL);
 
201
                break;
 
202
        case TCSD_PACKET_TYPE_VERSION:
 
203
                LoadBlob_VERSION(offset, blob, ((TCPA_VERSION *)data));
 
204
                break;
 
205
        case TCSD_PACKET_TYPE_KM_KEYINFO:
 
206
                LoadBlob_KM_KEYINFO(offset, blob, ((TSS_KM_KEYINFO *)data));
 
207
                break;
 
208
        case TCSD_PACKET_TYPE_LOADKEY_INFO:
 
209
                LoadBlob_LOADKEY_INFO(offset, blob, ((TCS_LOADKEY_INFO *)data));
 
210
                break;
 
211
        case TCSD_PACKET_TYPE_PCR_EVENT:
 
212
                LoadBlob_PCR_EVENT(offset, blob, ((TSS_PCR_EVENT *)data));
 
213
                break;
 
214
        default:
 
215
                LogError("TCSD packet type unknown! (0x%x)", data_type & 0xff);
 
216
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
217
        }
 
218
 
 
219
        return TSS_SUCCESS;
 
220
}
 
221
 
 
222
int
 
223
setData(TCSD_PACKET_TYPE dataType, int index, void *theData, int theDataSize, struct tcsd_comm_data *comm)
 
224
{
 
225
        UINT64 old_offset, offset;
 
226
        TSS_RESULT result;
 
227
        TCSD_PACKET_TYPE *type;
 
228
 
 
229
        /* Calculate the size of the area needed (use NULL for blob address) */
 
230
        offset = 0;
 
231
        if ((result = loadData(&offset, dataType, theData, theDataSize, NULL)) != TSS_SUCCESS)
 
232
                return result;
 
233
        if (((int)comm->hdr.packet_size + (int)offset) < 0) {
 
234
                LogError("Too much data to be transmitted!");
 
235
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
236
        }
 
237
        if (((int)comm->hdr.packet_size + (int)offset) > comm->buf_size) {
 
238
                /* reallocate the buffer */
 
239
                BYTE *buffer;
 
240
                int buffer_size = comm->hdr.packet_size + offset;
 
241
#if 0
 
242
                static int realloc_scalar = 1;
 
243
 
 
244
                realloc_scalar *= TCSD_COMMBUF_REALLOC_SCALAR;
 
245
 
 
246
                if (((int)offset * realloc_scalar) + buffer_size < 0)
 
247
                        buffer_size = INT_MAX;
 
248
                else
 
249
                        buffer_size += (int)offset * realloc_scalar;
 
250
 
 
251
                LogDebug("Increasing communication buffer by %d bytes.",
 
252
                         (int)offset * realloc_scalar);
 
253
#endif
 
254
                LogDebug("Increasing communication buffer to %d bytes.", buffer_size);
 
255
                buffer = realloc(comm->buf, buffer_size);
 
256
                if (buffer == NULL) {
 
257
                        LogError("realloc of %d bytes failed.", buffer_size);
 
258
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
259
                }
 
260
                comm->buf_size = buffer_size;
 
261
                comm->buf = buffer;
 
262
        }
 
263
 
 
264
        offset = old_offset = comm->hdr.parm_offset + comm->hdr.parm_size;
 
265
        if ((result = loadData(&offset, dataType, theData, theDataSize, comm->buf)) != TSS_SUCCESS)
 
266
                return result;
 
267
        type = (TCSD_PACKET_TYPE *)(comm->buf + comm->hdr.type_offset) + index;
 
268
        *type = dataType;
 
269
        comm->hdr.type_size += sizeof(TCSD_PACKET_TYPE);
 
270
        comm->hdr.parm_size += (offset - old_offset);
 
271
 
 
272
        comm->hdr.packet_size = offset;
 
273
        comm->hdr.num_parms++;
 
274
 
 
275
        return TSS_SUCCESS;
 
276
}
 
277
 
 
278
UINT32
 
279
getData(TCSD_PACKET_TYPE dataType, int index, void *theData, int theDataSize, struct tcsd_comm_data *comm)
 
280
{
 
281
        TSS_RESULT result;
 
282
        UINT64 old_offset, offset;
 
283
        TCSD_PACKET_TYPE *type = (TCSD_PACKET_TYPE *)(comm->buf + comm->hdr.type_offset) +
 
284
                                 index;
 
285
 
 
286
        if ((UINT32)index >= comm->hdr.num_parms || dataType != *type) {
 
287
                LogDebug("Data type of TCS packet element %d doesn't match.", index);
 
288
                return TSS_TCP_RPC_BAD_PACKET_TYPE;
 
289
        }
 
290
        old_offset = offset = comm->hdr.parm_offset;
 
291
        switch (dataType) {
 
292
        case TCSD_PACKET_TYPE_BYTE:
 
293
                UnloadBlob_BYTE(&offset, (BYTE *) (theData), comm->buf, NULL);
 
294
                break;
 
295
        case TCSD_PACKET_TYPE_BOOL:
 
296
                UnloadBlob_BOOL(&offset, (TSS_BOOL *) (theData), comm->buf, NULL);
 
297
                break;
 
298
        case TCSD_PACKET_TYPE_UINT16:
 
299
                UnloadBlob_UINT16(&offset, (UINT16 *) (theData), comm->buf, NULL);
 
300
                break;
 
301
        case TCSD_PACKET_TYPE_UINT32:
 
302
                UnloadBlob_UINT32(&offset, (UINT32 *) (theData), comm->buf, NULL);
 
303
                break;
 
304
        case TCSD_PACKET_TYPE_PBYTE:
 
305
                UnloadBlob(&offset, theDataSize, comm->buf, theData, NULL);
 
306
                break;
 
307
        case TCSD_PACKET_TYPE_NONCE:
 
308
                UnloadBlob(&offset, sizeof(TCPA_NONCE), comm->buf,
 
309
                           ((TCPA_NONCE *) (theData))->nonce, NULL);
 
310
                break;
 
311
        case TCSD_PACKET_TYPE_DIGEST:
 
312
                UnloadBlob(&offset, sizeof(TCPA_DIGEST), comm->buf,
 
313
                           ((TCPA_DIGEST *) (theData))->digest, NULL);
 
314
                break;
 
315
        case TCSD_PACKET_TYPE_AUTH:
 
316
                UnloadBlob_Auth_Special(&offset, comm->buf, ((TPM_AUTH *) theData));
 
317
                break;
 
318
        case TCSD_PACKET_TYPE_UUID:
 
319
                UnloadBlob_UUID(&offset, comm->buf, (TSS_UUID *) theData);
 
320
                break;
 
321
        case TCSD_PACKET_TYPE_ENCAUTH:
 
322
                UnloadBlob(&offset, sizeof(TCPA_ENCAUTH), comm->buf,
 
323
                           ((TCPA_ENCAUTH *) theData)->authdata, NULL);
 
324
                break;
 
325
        case TCSD_PACKET_TYPE_VERSION:
 
326
                UnloadBlob_VERSION(&offset, comm->buf, ((TCPA_VERSION *) theData));
 
327
                break;
 
328
        case TCSD_PACKET_TYPE_KM_KEYINFO:
 
329
                UnloadBlob_KM_KEYINFO(&offset, comm->buf, ((TSS_KM_KEYINFO*)theData));
 
330
                break;
 
331
        case TCSD_PACKET_TYPE_LOADKEY_INFO:
 
332
                UnloadBlob_LOADKEY_INFO(&offset, comm->buf, ((TCS_LOADKEY_INFO *)theData));
 
333
                break;
 
334
        case TCSD_PACKET_TYPE_PCR_EVENT:
 
335
                if ((result = UnloadBlob_PCR_EVENT(&offset, comm->buf, ((TSS_PCR_EVENT *)theData))))
 
336
                        return result;
 
337
                break;
 
338
        default:
 
339
                LogError("TCSD packet type unknown! (0x%x)", dataType & 0xff);
 
340
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
341
        }
 
342
        comm->hdr.parm_offset = offset;
 
343
        comm->hdr.parm_size -= (offset - old_offset);
 
344
 
 
345
        return TSS_SUCCESS;
 
346
}
 
347
 
 
348
TSS_RESULT
 
349
tcs_wrap_Error(struct tcsd_thread_data *data)
 
350
{
 
351
        LogError("%s reached.", __FUNCTION__);
 
352
 
 
353
        initData(&data->comm, 0);
 
354
 
 
355
        data->comm.hdr.u.result = TCSERR(TSS_E_FAIL);
 
356
 
 
357
        return TSS_SUCCESS;
 
358
}
 
359
 
 
360
TSS_RESULT
 
361
tcs_wrap_OpenContext(struct tcsd_thread_data *data)
 
362
{
 
363
        TCS_CONTEXT_HANDLE hContext;
 
364
        TSS_RESULT result;
 
365
 
 
366
        LogDebug("thread %x servicing a %s request", (UINT32)pthread_self(), __FUNCTION__);
 
367
 
 
368
        result = TCS_OpenContext_Internal(&hContext);
 
369
        if (result == TSS_SUCCESS) {
 
370
                initData(&data->comm, 1);
 
371
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
372
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
373
 
 
374
                /* Set the context in the thread's object. Later, if something goes wrong
 
375
                 * and the connection can't be closed cleanly, we'll still have a reference
 
376
                 * to what resources need to be freed. */
 
377
                data->context = hContext;
 
378
        } else
 
379
                initData(&data->comm, 0);
 
380
 
 
381
        data->comm.hdr.u.result = result;
 
382
 
 
383
        return TSS_SUCCESS;
 
384
}
 
385
 
 
386
TSS_RESULT
 
387
tcs_wrap_GetRandom(struct tcsd_thread_data *data)
 
388
{
 
389
        TCS_CONTEXT_HANDLE hContext;
 
390
        UINT32 bytesRequested;
 
391
        BYTE *randomBytes = NULL;
 
392
        TSS_RESULT result;
 
393
 
 
394
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
395
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
396
 
 
397
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
398
 
 
399
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &bytesRequested, 0, &data->comm))
 
400
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
401
 
 
402
        pthread_mutex_lock(&tcsp_lock);
 
403
 
 
404
        result = TCSP_GetRandom_Internal(hContext, &bytesRequested, &randomBytes);
 
405
 
 
406
        pthread_mutex_unlock(&tcsp_lock);
 
407
 
 
408
        if (result == TSS_SUCCESS) {
 
409
                initData(&data->comm, 2);
 
410
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &bytesRequested, 0, &data->comm)) {
 
411
                        free(randomBytes);
 
412
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
413
                }
 
414
                if (setData(TCSD_PACKET_TYPE_PBYTE, 1, randomBytes, bytesRequested, &data->comm)) {
 
415
                        free(randomBytes);
 
416
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
417
                }
 
418
                free(randomBytes);
 
419
        } else
 
420
                initData(&data->comm, 0);
 
421
 
 
422
        data->comm.hdr.u.result = result;
 
423
 
 
424
        return TSS_SUCCESS;
 
425
}
 
426
 
 
427
TSS_RESULT
 
428
tcs_wrap_CreateEndorsementKeyPair(struct tcsd_thread_data *data)
 
429
{
 
430
        TCS_CONTEXT_HANDLE hContext;
 
431
        TCPA_NONCE antiReplay;
 
432
        UINT32 eKPtrSize;
 
433
        BYTE *eKPtr;
 
434
        UINT32 eKSize;
 
435
        BYTE* eK;
 
436
        TCPA_DIGEST checksum;
 
437
        TSS_RESULT result;
 
438
 
 
439
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
440
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
441
 
 
442
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
443
 
 
444
        if (getData(TCSD_PACKET_TYPE_NONCE, 1, &antiReplay, 0, &data->comm))
 
445
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
446
 
 
447
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &eKPtrSize, 0, &data->comm))
 
448
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
449
 
 
450
        if (eKPtrSize == 0)
 
451
                eKPtr = NULL;
 
452
        else {
 
453
                eKPtr = calloc(1, eKPtrSize);
 
454
                if (eKPtr == NULL) {
 
455
                        LogError("malloc of %d bytes failed.", eKPtrSize);
 
456
                        return TCSERR(TSS_E_OUTOFMEMORY);
 
457
                }
 
458
                if (getData(TCSD_PACKET_TYPE_PBYTE, 3, eKPtr, eKPtrSize, &data->comm)) {
 
459
                        free(eKPtr);
 
460
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
461
                }
 
462
        }
 
463
 
 
464
        pthread_mutex_lock(&tcsp_lock);
 
465
 
 
466
        result = TCSP_CreateEndorsementKeyPair_Internal(hContext, antiReplay, eKPtrSize, eKPtr, &eKSize, &eK, &checksum);
 
467
 
 
468
        pthread_mutex_unlock(&tcsp_lock);
 
469
 
 
470
        free(eKPtr);
 
471
 
 
472
        if (result == TSS_SUCCESS) {
 
473
                initData(&data->comm, 3);
 
474
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &eKSize, 0, &data->comm)) {
 
475
                        free(eK);
 
476
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
477
                }
 
478
                if (setData(TCSD_PACKET_TYPE_PBYTE, 1, eK, eKSize, &data->comm)) {
 
479
                        free(eK);
 
480
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
481
                }
 
482
                free(eK);
 
483
                if (setData(TCSD_PACKET_TYPE_DIGEST, 2, &checksum, 0, &data->comm))
 
484
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
485
                }
 
486
        else
 
487
                initData(&data->comm, 0);
 
488
 
 
489
        data->comm.hdr.u.result = result;
 
490
 
 
491
        return TSS_SUCCESS;
 
492
}
 
493
 
 
494
TSS_RESULT
 
495
tcs_wrap_ReadPubek(struct tcsd_thread_data *data)
 
496
{
 
497
        TCS_CONTEXT_HANDLE hContext;
 
498
        TCPA_NONCE antiReplay;
 
499
        UINT32 pubEKSize;
 
500
        BYTE *pubEK;
 
501
        TCPA_DIGEST checksum;
 
502
        TSS_RESULT result;
 
503
 
 
504
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
505
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
506
 
 
507
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
508
 
 
509
        if (getData(TCSD_PACKET_TYPE_NONCE, 1, &antiReplay, 0, &data->comm))
 
510
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
511
 
 
512
        pthread_mutex_lock(&tcsp_lock);
 
513
 
 
514
        result = TCSP_ReadPubek_Internal(hContext, antiReplay, &pubEKSize, &pubEK,
 
515
                                    &checksum);
 
516
 
 
517
        pthread_mutex_unlock(&tcsp_lock);
 
518
 
 
519
        if (result == TSS_SUCCESS) {
 
520
                initData(&data->comm, 3);
 
521
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &pubEKSize, 0, &data->comm)) {
 
522
                        free(pubEK);
 
523
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
524
                }
 
525
                if (setData(TCSD_PACKET_TYPE_PBYTE, 1, pubEK, pubEKSize, &data->comm)) {
 
526
                        free(pubEK);
 
527
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
528
                }
 
529
                free(pubEK);
 
530
                if (setData(TCSD_PACKET_TYPE_DIGEST, 2, &checksum, 0, &data->comm))
 
531
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
532
        } else
 
533
                initData(&data->comm, 0);
 
534
 
 
535
        data->comm.hdr.u.result = result;
 
536
 
 
537
        return TSS_SUCCESS;
 
538
}
 
539
 
 
540
TSS_RESULT
 
541
tcs_wrap_OwnerReadPubek(struct tcsd_thread_data *data)
 
542
{
 
543
        TCS_CONTEXT_HANDLE hContext;
 
544
        UINT32 pubEKSize;
 
545
        BYTE *pubEK;
 
546
        TSS_RESULT result;
 
547
        TPM_AUTH auth;
 
548
 
 
549
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
550
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
551
 
 
552
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
553
 
 
554
        if (getData(TCSD_PACKET_TYPE_AUTH, 1, &auth, 0, &data->comm))
 
555
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
556
 
 
557
        pthread_mutex_lock(&tcsp_lock);
 
558
 
 
559
        result = TCSP_OwnerReadPubek_Internal(hContext, &auth, &pubEKSize, &pubEK);
 
560
 
 
561
        pthread_mutex_unlock(&tcsp_lock);
 
562
 
 
563
        if (result == TSS_SUCCESS) {
 
564
                initData(&data->comm, 3);
 
565
                if (setData(TCSD_PACKET_TYPE_AUTH, 0, &auth, 0, &data->comm)) {
 
566
                        free(pubEK);
 
567
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
568
                }
 
569
                if (setData(TCSD_PACKET_TYPE_UINT32, 1, &pubEKSize, 0, &data->comm)) {
 
570
                        free(pubEK);
 
571
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
572
                }
 
573
                if (setData(TCSD_PACKET_TYPE_PBYTE, 2, pubEK, pubEKSize, &data->comm)) {
 
574
                        free(pubEK);
 
575
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
576
                }
 
577
                free(pubEK);
 
578
        } else
 
579
                initData(&data->comm, 0);
 
580
 
 
581
        data->comm.hdr.u.result = result;
 
582
 
 
583
        return TSS_SUCCESS;
 
584
}
 
585
 
 
586
TSS_RESULT
 
587
tcs_wrap_DisablePubekRead(struct tcsd_thread_data *data)
 
588
{
 
589
        TCS_CONTEXT_HANDLE hContext;
 
590
        TSS_RESULT result;
 
591
        TPM_AUTH auth;
 
592
 
 
593
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
594
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
595
 
 
596
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
597
 
 
598
        if (getData(TCSD_PACKET_TYPE_AUTH, 1, &auth, 0, &data->comm))
 
599
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
600
 
 
601
        pthread_mutex_lock(&tcsp_lock);
 
602
 
 
603
        result = TCSP_DisablePubekRead_Internal(hContext, &auth);
 
604
 
 
605
        pthread_mutex_unlock(&tcsp_lock);
 
606
 
 
607
        if (result == TSS_SUCCESS) {
 
608
                initData(&data->comm, 1);
 
609
                if (setData(TCSD_PACKET_TYPE_AUTH, 0, &auth, 0, &data->comm))
 
610
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
611
        } else
 
612
                initData(&data->comm, 0);
 
613
 
 
614
        data->comm.hdr.u.result = result;
 
615
 
 
616
        return TSS_SUCCESS;
 
617
}
 
618
 
 
619
TSS_RESULT
 
620
tcs_wrap_CloseContext(struct tcsd_thread_data *data)
 
621
{
 
622
        TCS_CONTEXT_HANDLE hContext;
 
623
        TSS_RESULT result;
 
624
 
 
625
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
626
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
627
 
 
628
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
629
 
 
630
        result = TCS_CloseContext_Internal(hContext);
 
631
 
 
632
        /* This will signal the thread that the connection has been closed cleanly */
 
633
        if (result == TSS_SUCCESS)
 
634
                data->context = NULL_TCS_HANDLE;
 
635
 
 
636
        initData(&data->comm, 0);
 
637
 
 
638
        data->comm.hdr.u.result = result;
 
639
 
 
640
        return TSS_SUCCESS;
 
641
}
 
642
 
 
643
TSS_RESULT
 
644
tcs_wrap_EvictKey(struct tcsd_thread_data *data)
 
645
{
 
646
        TCS_CONTEXT_HANDLE hContext;
 
647
        TCS_KEY_HANDLE hKey;
 
648
        TSS_RESULT result;
 
649
 
 
650
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
651
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
652
 
 
653
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
654
 
 
655
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &data->comm))
 
656
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
657
 
 
658
        pthread_mutex_lock(&tcsp_lock);
 
659
 
 
660
        result = key_mgr_evict(hContext, hKey);
 
661
 
 
662
        pthread_mutex_unlock(&tcsp_lock);
 
663
 
 
664
        initData(&data->comm, 0);
 
665
 
 
666
        data->comm.hdr.u.result = result;
 
667
 
 
668
        return TSS_SUCCESS;
 
669
}
 
670
 
 
671
TSS_RESULT
 
672
tcs_wrap_GetPubkey(struct tcsd_thread_data *data)
 
673
{
 
674
        TCS_CONTEXT_HANDLE hContext;
 
675
        TCS_KEY_HANDLE hKey;
 
676
        TPM_AUTH auth;
 
677
        TPM_AUTH *pAuth;
 
678
        UINT32 pubKeySize;
 
679
        BYTE *pubKey;
 
680
        TSS_RESULT result;
 
681
        int i;
 
682
 
 
683
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
684
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
685
 
 
686
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
687
 
 
688
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &data->comm))
 
689
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
690
 
 
691
        result = getData(TCSD_PACKET_TYPE_AUTH, 2, &auth, 0, &data->comm);
 
692
        if (result == TSS_TCP_RPC_BAD_PACKET_TYPE)
 
693
                pAuth = NULL;
 
694
        else if (result)
 
695
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
696
        else
 
697
                pAuth = &auth;
 
698
 
 
699
        pthread_mutex_lock(&tcsp_lock);
 
700
 
 
701
        result = TCSP_GetPubKey_Internal(hContext, hKey, pAuth, &pubKeySize, &pubKey);
 
702
 
 
703
        pthread_mutex_unlock(&tcsp_lock);
 
704
 
 
705
        if (result == TSS_SUCCESS) {
 
706
                i = 0;
 
707
                initData(&data->comm, 3);
 
708
                if (pAuth != NULL)
 
709
                        if (setData(TCSD_PACKET_TYPE_AUTH, i++, pAuth, 0, &data->comm)) {
 
710
                                free(pubKey);
 
711
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
712
                        }
 
713
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &pubKeySize, 0, &data->comm)) {
 
714
                        free(pubKey);
 
715
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
716
                }
 
717
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, pubKey, pubKeySize, &data->comm)) {
 
718
                        free(pubKey);
 
719
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
720
                }
 
721
                free(pubKey);
 
722
        } else
 
723
                initData(&data->comm, 0);
 
724
 
 
725
        data->comm.hdr.u.result = result;
 
726
 
 
727
        return TSS_SUCCESS;
 
728
}
 
729
 
 
730
TSS_RESULT
 
731
tcs_wrap_OIAP(struct tcsd_thread_data *data)
 
732
{
 
733
        TCS_CONTEXT_HANDLE hContext;
 
734
        TCS_AUTHHANDLE authHandle;
 
735
        TCPA_NONCE n0;
 
736
        TSS_RESULT result;
 
737
 
 
738
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
739
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
740
 
 
741
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
742
 
 
743
        pthread_mutex_lock(&tcsp_lock);
 
744
 
 
745
        result = auth_mgr_oiap(hContext, &authHandle, &n0);
 
746
 
 
747
        pthread_mutex_unlock(&tcsp_lock);
 
748
 
 
749
        if (result == TSS_SUCCESS) {
 
750
                initData(&data->comm, 2);
 
751
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &authHandle, 0, &data->comm))
 
752
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
753
                if (setData(TCSD_PACKET_TYPE_NONCE, 1, &n0, 0, &data->comm))
 
754
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
755
        } else
 
756
                initData(&data->comm, 0);
 
757
 
 
758
        data->comm.hdr.u.result = result;
 
759
 
 
760
        return TSS_SUCCESS;
 
761
}
 
762
 
 
763
TSS_RESULT
 
764
tcs_wrap_TerminateHandle(struct tcsd_thread_data *data)
 
765
{
 
766
        TCS_CONTEXT_HANDLE hContext;
 
767
        TCS_AUTHHANDLE authHandle;
 
768
        TSS_RESULT result;
 
769
 
 
770
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
771
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
772
 
 
773
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
774
 
 
775
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &authHandle, 0, &data->comm))
 
776
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
777
 
 
778
        pthread_mutex_lock(&tcsp_lock);
 
779
 
 
780
        result = TCSP_TerminateHandle_Internal(hContext, authHandle);
 
781
 
 
782
        pthread_mutex_unlock(&tcsp_lock);
 
783
 
 
784
        initData(&data->comm, 0);
 
785
 
 
786
        data->comm.hdr.u.result = result;
 
787
 
 
788
        return TSS_SUCCESS;
 
789
}
 
790
 
 
791
TSS_RESULT
 
792
tcs_wrap_Extend(struct tcsd_thread_data *data)
 
793
{
 
794
        TCS_CONTEXT_HANDLE hContext;
 
795
        UINT32 pcrIndex;
 
796
        TCPA_DIGEST inDigest;
 
797
        TSS_RESULT result;
 
798
        TCPA_DIGEST outDigest;
 
799
 
 
800
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
801
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
802
 
 
803
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
804
 
 
805
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &pcrIndex, 0, &data->comm))
 
806
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
807
        if (getData(TCSD_PACKET_TYPE_DIGEST, 2, &inDigest, 0, &data->comm))
 
808
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
809
 
 
810
        pthread_mutex_lock(&tcsp_lock);
 
811
 
 
812
        result = TCSP_Extend_Internal(hContext, pcrIndex, inDigest, &outDigest);
 
813
 
 
814
        pthread_mutex_unlock(&tcsp_lock);
 
815
 
 
816
        if (result == TSS_SUCCESS) {
 
817
                initData(&data->comm, 1);
 
818
                if (setData(TCSD_PACKET_TYPE_DIGEST, 0, &outDigest, 0, &data->comm))
 
819
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
820
        } else
 
821
                initData(&data->comm, 0);
 
822
 
 
823
        data->comm.hdr.u.result = result;
 
824
 
 
825
        return TSS_SUCCESS;
 
826
}
 
827
 
 
828
TSS_RESULT
 
829
tcs_wrap_PcrRead(struct tcsd_thread_data *data)
 
830
{
 
831
        TCS_CONTEXT_HANDLE hContext;
 
832
        UINT32 pcrIndex;
 
833
        TCPA_DIGEST digest;
 
834
        TSS_RESULT result;
 
835
 
 
836
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
837
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
838
 
 
839
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
840
 
 
841
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &pcrIndex, 0, &data->comm))
 
842
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
843
 
 
844
        pthread_mutex_lock(&tcsp_lock);
 
845
 
 
846
        result = TCSP_PcrRead_Internal(hContext, pcrIndex, &digest);
 
847
 
 
848
        pthread_mutex_unlock(&tcsp_lock);
 
849
 
 
850
        if (result == TSS_SUCCESS) {
 
851
                initData(&data->comm, 1);
 
852
                if (setData(TCSD_PACKET_TYPE_DIGEST, 0, &digest, 0, &data->comm))
 
853
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
854
        } else
 
855
                initData(&data->comm, 0);
 
856
 
 
857
        data->comm.hdr.u.result = result;
 
858
 
 
859
        return TSS_SUCCESS;
 
860
}
 
861
 
 
862
TSS_RESULT
 
863
tcs_wrap_GetCapability(struct tcsd_thread_data *data)
 
864
{
 
865
        TCS_CONTEXT_HANDLE hContext;
 
866
        TCPA_CAPABILITY_AREA capArea;
 
867
        UINT32 subCapSize;
 
868
        BYTE *subCap;
 
869
        UINT32 respSize;
 
870
        BYTE *resp;
 
871
        TSS_RESULT result;
 
872
 
 
873
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
874
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
875
 
 
876
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
877
 
 
878
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &capArea, 0, &data->comm))
 
879
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
880
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &subCapSize, 0, &data->comm))
 
881
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
882
 
 
883
        if (subCapSize == 0)
 
884
                subCap = NULL;
 
885
        else {
 
886
                subCap = calloc(1, subCapSize);
 
887
                if (subCap == NULL) {
 
888
                        LogError("malloc of %d bytes failed.", subCapSize);
 
889
                        return TCSERR(TSS_E_OUTOFMEMORY);
 
890
                }
 
891
                if (getData(TCSD_PACKET_TYPE_PBYTE, 3, subCap, subCapSize, &data->comm)) {
 
892
                        free(subCap);
 
893
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
894
                }
 
895
        }
 
896
 
 
897
        pthread_mutex_lock(&tcsp_lock);
 
898
 
 
899
        result = TCSP_GetCapability_Internal(hContext, capArea, subCapSize, subCap,
 
900
                                        &respSize, &resp);
 
901
 
 
902
        pthread_mutex_unlock(&tcsp_lock);
 
903
 
 
904
        free(subCap);
 
905
 
 
906
        if (result == TSS_SUCCESS) {
 
907
                initData(&data->comm, 2);
 
908
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &respSize, 0, &data->comm)) {
 
909
                        free(resp);
 
910
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
911
                }
 
912
                if (setData(TCSD_PACKET_TYPE_PBYTE, 1, resp, respSize, &data->comm)) {
 
913
                        free(resp);
 
914
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
915
                }
 
916
                free(resp);
 
917
        } else
 
918
                initData(&data->comm, 0);
 
919
 
 
920
        data->comm.hdr.u.result = result;
 
921
 
 
922
        return TSS_SUCCESS;
 
923
}
 
924
 
 
925
TSS_RESULT
 
926
tcs_wrap_GetCapabilityOwner(struct tcsd_thread_data *data)
 
927
{
 
928
        TCS_CONTEXT_HANDLE hContext;
 
929
        TPM_AUTH ownerAuth;
 
930
        TCPA_VERSION version;
 
931
        UINT32 nonVol;
 
932
        UINT32 vol;
 
933
        TSS_RESULT result;
 
934
 
 
935
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
936
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
937
 
 
938
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
939
 
 
940
        if (getData(TCSD_PACKET_TYPE_AUTH, 1, &ownerAuth, 0, &data->comm))
 
941
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
942
 
 
943
        pthread_mutex_lock(&tcsp_lock);
 
944
 
 
945
        result = TCSP_GetCapabilityOwner_Internal(hContext, &ownerAuth, &version,
 
946
                                             &nonVol, &vol);
 
947
 
 
948
        pthread_mutex_unlock(&tcsp_lock);
 
949
 
 
950
        if (result == TSS_SUCCESS) {
 
951
                initData(&data->comm, 4);
 
952
                if (setData(TCSD_PACKET_TYPE_VERSION, 0, &version, 0, &data->comm))
 
953
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
954
                if (setData(TCSD_PACKET_TYPE_UINT32, 1, &nonVol, 0, &data->comm))
 
955
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
956
                if (setData(TCSD_PACKET_TYPE_UINT32, 2, &vol, 0, &data->comm))
 
957
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
958
                if (setData(TCSD_PACKET_TYPE_AUTH, 3, &ownerAuth, 0, &data->comm))
 
959
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
960
        } else
 
961
                initData(&data->comm, 0);
 
962
 
 
963
        data->comm.hdr.u.result = result;
 
964
 
 
965
        return TSS_SUCCESS;
 
966
}
 
967
 
 
968
TSS_RESULT
 
969
tcs_wrap_TCSGetCapability(struct tcsd_thread_data *data)
 
970
{
 
971
        TCS_CONTEXT_HANDLE hContext;
 
972
        TCPA_CAPABILITY_AREA capArea;
 
973
        UINT32 subCapSize;
 
974
        BYTE *subCap;
 
975
        UINT32 respSize;
 
976
        BYTE *resp;
 
977
        TSS_RESULT result;
 
978
 
 
979
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
980
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
981
 
 
982
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
983
 
 
984
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &capArea, 0, &data->comm))
 
985
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
986
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &subCapSize, 0, &data->comm))
 
987
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
988
 
 
989
        subCap = calloc(1, subCapSize);
 
990
        if (subCap == NULL) {
 
991
                LogError("malloc of %d bytes failed.", subCapSize);
 
992
                return TCSERR(TSS_E_OUTOFMEMORY);
 
993
        }
 
994
        if (getData(TCSD_PACKET_TYPE_PBYTE, 3, subCap, subCapSize, &data->comm)) {
 
995
                free(subCap);
 
996
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
997
        }
 
998
 
 
999
        result = TCS_GetCapability_Internal(hContext, capArea, subCapSize, subCap,
 
1000
                                       &respSize, &resp);
 
1001
        free(subCap);
 
1002
 
 
1003
        if (result == TSS_SUCCESS) {
 
1004
                initData(&data->comm, 2);
 
1005
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &respSize, 0, &data->comm)) {
 
1006
                        free(resp);
 
1007
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1008
                }
 
1009
                if (setData(TCSD_PACKET_TYPE_PBYTE, 1, resp, respSize, &data->comm)) {
 
1010
                        free(resp);
 
1011
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1012
                }
 
1013
                free(resp);
 
1014
        } else
 
1015
                initData(&data->comm, 0);
 
1016
 
 
1017
        data->comm.hdr.u.result = result;
 
1018
 
 
1019
        return TSS_SUCCESS;
 
1020
}
 
1021
 
 
1022
TSS_RESULT
 
1023
tcs_wrap_PhysicalSetDeactivated(struct tcsd_thread_data *data)
 
1024
{
 
1025
        TCS_CONTEXT_HANDLE hContext;
 
1026
        TSS_BOOL state;
 
1027
        TSS_RESULT result;
 
1028
 
 
1029
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1030
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1031
 
 
1032
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1033
 
 
1034
        if (getData(TCSD_PACKET_TYPE_BOOL, 1, &state, 0, &data->comm))
 
1035
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1036
 
 
1037
        pthread_mutex_lock(&tcsp_lock);
 
1038
 
 
1039
        result = TCSP_PhysicalSetDeactivated_Internal(hContext, state);
 
1040
 
 
1041
        pthread_mutex_unlock(&tcsp_lock);
 
1042
 
 
1043
        initData(&data->comm, 0);
 
1044
 
 
1045
        data->comm.hdr.u.result = result;
 
1046
 
 
1047
        return TSS_SUCCESS;
 
1048
}
 
1049
 
 
1050
TSS_RESULT
 
1051
tcs_wrap_TakeOwnership(struct tcsd_thread_data *data)
 
1052
{
 
1053
        TCS_CONTEXT_HANDLE hContext;
 
1054
        UINT16 protocolID;
 
1055
        UINT32 encOwnerAuthSize;
 
1056
        BYTE *encOwnerAuth;
 
1057
        UINT32 encSrkAuthSize;
 
1058
        BYTE *encSrkAuth;
 
1059
        UINT32 srkInfoSize;
 
1060
        BYTE *srkInfo;
 
1061
        TPM_AUTH ownerAuth;
 
1062
 
 
1063
        UINT32 srkKeySize;
 
1064
        BYTE *srkKey;
 
1065
        TSS_RESULT result;
 
1066
 
 
1067
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1068
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1069
 
 
1070
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1071
 
 
1072
        if (getData(TCSD_PACKET_TYPE_UINT16, 1, &protocolID, 0, &data->comm))
 
1073
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1074
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &encOwnerAuthSize, 0, &data->comm))
 
1075
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1076
        encOwnerAuth = calloc(1, encOwnerAuthSize);
 
1077
        if (encOwnerAuth == NULL) {
 
1078
                LogError("malloc of %d bytes failed.", encOwnerAuthSize);
 
1079
                return TCSERR(TSS_E_OUTOFMEMORY);
 
1080
        }
 
1081
        if (getData(TCSD_PACKET_TYPE_PBYTE, 3, encOwnerAuth, encOwnerAuthSize, &data->comm)) {
 
1082
                free(encOwnerAuth);
 
1083
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1084
        }
 
1085
        if (getData(TCSD_PACKET_TYPE_UINT32, 4, &encSrkAuthSize, 0, &data->comm)) {
 
1086
                free(encOwnerAuth);
 
1087
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1088
        }
 
1089
        encSrkAuth = calloc(1, encSrkAuthSize);
 
1090
        if (encSrkAuth == NULL) {
 
1091
                LogError("malloc of %d bytes failed.", encSrkAuthSize);
 
1092
                free(encOwnerAuth);
 
1093
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1094
        }
 
1095
        if (getData(TCSD_PACKET_TYPE_PBYTE, 5, encSrkAuth, encSrkAuthSize, &data->comm)) {
 
1096
                free(encOwnerAuth);
 
1097
                free(encSrkAuth);
 
1098
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1099
        }
 
1100
        if (getData(TCSD_PACKET_TYPE_UINT32, 6, &srkInfoSize, 0, &data->comm)) {
 
1101
                free(encOwnerAuth);
 
1102
                free(encSrkAuth);
 
1103
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1104
        }
 
1105
 
 
1106
        srkInfo = calloc(1, srkInfoSize);
 
1107
        if (srkInfo == NULL) {
 
1108
                LogError("malloc of %d bytes failed.", srkInfoSize);
 
1109
                free(encOwnerAuth);
 
1110
                free(encSrkAuth);
 
1111
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1112
        }
 
1113
        if (getData(TCSD_PACKET_TYPE_PBYTE, 7, srkInfo, srkInfoSize, &data->comm)) {
 
1114
                free(encOwnerAuth);
 
1115
                free(encSrkAuth);
 
1116
                free(srkInfo);
 
1117
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1118
        }
 
1119
        if (getData(TCSD_PACKET_TYPE_AUTH, 8, &ownerAuth, 0, &data->comm)) {
 
1120
                free(encOwnerAuth);
 
1121
                free(encSrkAuth);
 
1122
                free(srkInfo);
 
1123
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1124
        }
 
1125
 
 
1126
        pthread_mutex_lock(&tcsp_lock);
 
1127
 
 
1128
        result = TCSP_TakeOwnership_Internal(hContext, protocolID, encOwnerAuthSize,
 
1129
                                        encOwnerAuth, encSrkAuthSize,
 
1130
                                        encSrkAuth, srkInfoSize, srkInfo,
 
1131
                                        &ownerAuth, &srkKeySize, &srkKey);
 
1132
 
 
1133
        pthread_mutex_unlock(&tcsp_lock);
 
1134
 
 
1135
        free(encOwnerAuth);
 
1136
        free(encSrkAuth);
 
1137
        free(srkInfo);
 
1138
 
 
1139
        if (result == TSS_SUCCESS) {
 
1140
                initData(&data->comm, 3);
 
1141
                if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm)) {
 
1142
                        free(srkKey);
 
1143
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1144
                }
 
1145
                if (setData(TCSD_PACKET_TYPE_UINT32, 1, &srkKeySize, 0, &data->comm)) {
 
1146
                        free(srkKey);
 
1147
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1148
                }
 
1149
                if (setData(TCSD_PACKET_TYPE_PBYTE, 2, srkKey, srkKeySize, &data->comm)) {
 
1150
                        free(srkKey);
 
1151
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1152
                }
 
1153
                free(srkKey);
 
1154
        } else
 
1155
                initData(&data->comm, 0);
 
1156
 
 
1157
        data->comm.hdr.u.result = result;
 
1158
 
 
1159
        return TSS_SUCCESS;
 
1160
}
 
1161
 
 
1162
TSS_RESULT
 
1163
tcs_wrap_OwnerClear(struct tcsd_thread_data *data)
 
1164
{
 
1165
        TCS_CONTEXT_HANDLE hContext;
 
1166
        TSS_RESULT result;
 
1167
        TPM_AUTH auth;
 
1168
 
 
1169
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1170
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1171
 
 
1172
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1173
 
 
1174
        if (getData(TCSD_PACKET_TYPE_AUTH, 1, &auth, 0, &data->comm))
 
1175
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1176
 
 
1177
        pthread_mutex_lock(&tcsp_lock);
 
1178
 
 
1179
        result = TCSP_OwnerClear_Internal(hContext, &auth);
 
1180
 
 
1181
        pthread_mutex_unlock(&tcsp_lock);
 
1182
 
 
1183
        if (result == TSS_SUCCESS) {
 
1184
                initData(&data->comm, 1);
 
1185
                if (setData(TCSD_PACKET_TYPE_AUTH, 0, &auth, 0, &data->comm))
 
1186
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1187
        } else
 
1188
                initData(&data->comm, 0);
 
1189
 
 
1190
        data->comm.hdr.u.result = result;
 
1191
 
 
1192
        return TSS_SUCCESS;
 
1193
}
 
1194
 
 
1195
TSS_RESULT
 
1196
tcs_wrap_DisableOwnerClear(struct tcsd_thread_data *data)
 
1197
{
 
1198
        TCS_CONTEXT_HANDLE hContext;
 
1199
        TSS_RESULT result;
 
1200
        TPM_AUTH auth;
 
1201
 
 
1202
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1203
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1204
 
 
1205
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1206
 
 
1207
        if (getData(TCSD_PACKET_TYPE_AUTH, 1, &auth, 0, &data->comm))
 
1208
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1209
 
 
1210
        pthread_mutex_lock(&tcsp_lock);
 
1211
 
 
1212
        result = TCSP_DisableOwnerClear_Internal(hContext, &auth);
 
1213
 
 
1214
        pthread_mutex_unlock(&tcsp_lock);
 
1215
 
 
1216
        if (result == TSS_SUCCESS) {
 
1217
                initData(&data->comm, 1);
 
1218
                if (setData(TCSD_PACKET_TYPE_AUTH, 0, &auth, 0, &data->comm))
 
1219
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1220
        } else
 
1221
                initData(&data->comm, 0);
 
1222
 
 
1223
        data->comm.hdr.u.result = result;
 
1224
 
 
1225
        return TSS_SUCCESS;
 
1226
}
 
1227
 
 
1228
TSS_RESULT
 
1229
tcs_wrap_ForceClear(struct tcsd_thread_data *data)
 
1230
{
 
1231
        TCS_CONTEXT_HANDLE hContext;
 
1232
        TSS_RESULT result;
 
1233
 
 
1234
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1235
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1236
 
 
1237
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1238
 
 
1239
        pthread_mutex_lock(&tcsp_lock);
 
1240
 
 
1241
        result = TCSP_ForceClear_Internal(hContext);
 
1242
 
 
1243
        pthread_mutex_unlock(&tcsp_lock);
 
1244
 
 
1245
        initData(&data->comm, 0);
 
1246
 
 
1247
        data->comm.hdr.u.result = result;
 
1248
 
 
1249
        return TSS_SUCCESS;
 
1250
}
 
1251
 
 
1252
TSS_RESULT
 
1253
tcs_wrap_DisableForceClear(struct tcsd_thread_data *data)
 
1254
{
 
1255
        TCS_CONTEXT_HANDLE hContext;
 
1256
        TSS_RESULT result;
 
1257
 
 
1258
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1259
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1260
 
 
1261
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1262
 
 
1263
        pthread_mutex_lock(&tcsp_lock);
 
1264
 
 
1265
        result = TCSP_DisableForceClear_Internal(hContext);
 
1266
 
 
1267
        pthread_mutex_unlock(&tcsp_lock);
 
1268
 
 
1269
        initData(&data->comm, 0);
 
1270
 
 
1271
        data->comm.hdr.u.result = result;
 
1272
 
 
1273
        return TSS_SUCCESS;
 
1274
}
 
1275
 
 
1276
TSS_RESULT
 
1277
tcs_wrap_PhysicalEnable(struct tcsd_thread_data *data)
 
1278
{
 
1279
        TCS_CONTEXT_HANDLE hContext;
 
1280
        TSS_RESULT result;
 
1281
 
 
1282
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1283
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1284
 
 
1285
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1286
 
 
1287
        pthread_mutex_lock(&tcsp_lock);
 
1288
 
 
1289
        result = TCSP_PhysicalEnable_Internal(hContext);
 
1290
 
 
1291
        pthread_mutex_unlock(&tcsp_lock);
 
1292
 
 
1293
        initData(&data->comm, 0);
 
1294
 
 
1295
        data->comm.hdr.u.result = result;
 
1296
 
 
1297
        return TSS_SUCCESS;
 
1298
}
 
1299
 
 
1300
TSS_RESULT
 
1301
tcs_wrap_RegisterKey(struct tcsd_thread_data *data)
 
1302
{
 
1303
        TCS_CONTEXT_HANDLE hContext;
 
1304
        TSS_UUID WrappingKeyUUID;
 
1305
        TSS_UUID KeyUUID;
 
1306
        UINT32 cKeySize;
 
1307
        BYTE *rgbKey;
 
1308
        UINT32 cVendorData;
 
1309
        BYTE *gbVendorData;
 
1310
        TSS_RESULT result;
 
1311
 
 
1312
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1313
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1314
 
 
1315
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1316
 
 
1317
        if (getData(TCSD_PACKET_TYPE_UUID, 1, &WrappingKeyUUID, 0, &data->comm))
 
1318
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1319
        if (getData(TCSD_PACKET_TYPE_UUID, 2, &KeyUUID, 0, &data->comm))
 
1320
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1321
        if (getData(TCSD_PACKET_TYPE_UINT32, 3, &cKeySize, 0, &data->comm))
 
1322
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1323
 
 
1324
        rgbKey = calloc(1, cKeySize);
 
1325
        if (rgbKey == NULL) {
 
1326
                LogError("malloc of %d bytes failed.", cKeySize);
 
1327
                return TCSERR(TSS_E_OUTOFMEMORY);
 
1328
        }
 
1329
        if (getData(TCSD_PACKET_TYPE_PBYTE, 4, rgbKey, cKeySize, &data->comm)) {
 
1330
                free(rgbKey);
 
1331
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1332
        }
 
1333
        if (getData(TCSD_PACKET_TYPE_UINT32, 5, &cVendorData, 0, &data->comm)) {
 
1334
                free(rgbKey);
 
1335
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1336
        }
 
1337
 
 
1338
        if (cVendorData == 0)
 
1339
                gbVendorData = NULL;
 
1340
        else {
 
1341
                gbVendorData = calloc(1, cVendorData);
 
1342
                if (gbVendorData == NULL) {
 
1343
                        LogError("malloc of %d bytes failed.", cVendorData);
 
1344
                        free(rgbKey);
 
1345
                        return TCSERR(TSS_E_OUTOFMEMORY);
 
1346
                }
 
1347
 
 
1348
                if (getData(TCSD_PACKET_TYPE_PBYTE, 6, gbVendorData, cVendorData, &data->comm)) {
 
1349
                        free(rgbKey);
 
1350
                        free(gbVendorData);
 
1351
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1352
                }
 
1353
        }
 
1354
 
 
1355
        result = TCS_RegisterKey_Internal(hContext, &WrappingKeyUUID, &KeyUUID,
 
1356
                                     cKeySize, rgbKey, cVendorData,
 
1357
                                     gbVendorData);
 
1358
        free(rgbKey);
 
1359
        free(gbVendorData);
 
1360
 
 
1361
        initData(&data->comm, 0);
 
1362
 
 
1363
        data->comm.hdr.u.result = result;
 
1364
 
 
1365
        return TSS_SUCCESS;
 
1366
}
 
1367
 
 
1368
TSS_RESULT
 
1369
tcs_wrap_UnregisterKey(struct tcsd_thread_data *data)
 
1370
{
 
1371
        TCS_CONTEXT_HANDLE hContext;
 
1372
        TSS_UUID uuid;
 
1373
        TSS_RESULT result;
 
1374
 
 
1375
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1376
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1377
 
 
1378
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1379
 
 
1380
        if (getData(TCSD_PACKET_TYPE_UUID, 1, &uuid, 0, &data->comm))
 
1381
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1382
 
 
1383
        result = TCSP_UnregisterKey_Internal(hContext, uuid);
 
1384
 
 
1385
        initData(&data->comm, 0);
 
1386
 
 
1387
        data->comm.hdr.u.result = result;
 
1388
 
 
1389
        return TSS_SUCCESS;
 
1390
}
 
1391
 
 
1392
TSS_RESULT
 
1393
tcs_wrap_GetRegisteredKeyBlob(struct tcsd_thread_data *data)
 
1394
{
 
1395
        TCS_CONTEXT_HANDLE hContext;
 
1396
        TSS_UUID uuid;
 
1397
        UINT32 pcKeySize;
 
1398
        BYTE *prgbKey;
 
1399
        TSS_RESULT result;
 
1400
 
 
1401
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1402
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1403
 
 
1404
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1405
 
 
1406
        if (getData(TCSD_PACKET_TYPE_UUID, 1, &uuid, 0, &data->comm))
 
1407
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1408
 
 
1409
        result = TCS_GetRegisteredKeyBlob_Internal(hContext, &uuid, &pcKeySize,
 
1410
                                              &prgbKey);
 
1411
        if (result == TSS_SUCCESS) {
 
1412
                initData(&data->comm, 2);
 
1413
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &pcKeySize, 0, &data->comm)) {
 
1414
                        free(prgbKey);
 
1415
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1416
                }
 
1417
                if (setData(TCSD_PACKET_TYPE_PBYTE, 1, prgbKey, pcKeySize, &data->comm)) {
 
1418
                        free(prgbKey);
 
1419
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1420
                }
 
1421
                free(prgbKey);
 
1422
        } else
 
1423
                initData(&data->comm, 0);
 
1424
 
 
1425
        data->comm.hdr.u.result = result;
 
1426
 
 
1427
        return TSS_SUCCESS;
 
1428
}
 
1429
 
 
1430
TSS_RESULT
 
1431
tcs_wrap_LoadKeyByBlob(struct tcsd_thread_data *data)
 
1432
{
 
1433
        TCS_CONTEXT_HANDLE hContext;
 
1434
        TCS_KEY_HANDLE hUnwrappingKey;
 
1435
        UINT32 cWrappedKeyBlob;
 
1436
        BYTE *rgbWrappedKeyBlob;
 
1437
 
 
1438
        TPM_AUTH auth;
 
1439
 
 
1440
        TCS_KEY_HANDLE phKeyTCSI;
 
1441
        TCS_KEY_HANDLE phKeyHMAC;
 
1442
 
 
1443
        TPM_AUTH *pAuth;
 
1444
        TSS_RESULT result;
 
1445
        int i;
 
1446
 
 
1447
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1448
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1449
 
 
1450
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1451
 
 
1452
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hUnwrappingKey, 0, &data->comm))
 
1453
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1454
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &cWrappedKeyBlob, 0, &data->comm))
 
1455
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1456
 
 
1457
        rgbWrappedKeyBlob = calloc(1, cWrappedKeyBlob);
 
1458
        if (rgbWrappedKeyBlob == NULL) {
 
1459
                LogError("malloc of %d bytes failed.", cWrappedKeyBlob);
 
1460
                return TCSERR(TSS_E_OUTOFMEMORY);
 
1461
        }
 
1462
        if (getData(TCSD_PACKET_TYPE_PBYTE, 3, rgbWrappedKeyBlob, cWrappedKeyBlob, &data->comm)) {
 
1463
                free(rgbWrappedKeyBlob);
 
1464
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1465
        }
 
1466
        result = getData(TCSD_PACKET_TYPE_AUTH, 4, &auth, 0, &data->comm);
 
1467
        if (result == TSS_TCP_RPC_BAD_PACKET_TYPE)
 
1468
                pAuth = NULL;
 
1469
        else if (result) {
 
1470
                free(rgbWrappedKeyBlob);
 
1471
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1472
        } else
 
1473
                pAuth = &auth;
 
1474
 
 
1475
        pthread_mutex_lock(&tcsp_lock);
 
1476
 
 
1477
        result = key_mgr_load_by_blob(hContext, hUnwrappingKey, cWrappedKeyBlob, rgbWrappedKeyBlob,
 
1478
                                      pAuth, &phKeyTCSI, &phKeyHMAC);
 
1479
 
 
1480
        if (!result)
 
1481
                result = ctx_mark_key_loaded(hContext, phKeyTCSI);
 
1482
 
 
1483
        pthread_mutex_unlock(&tcsp_lock);
 
1484
 
 
1485
        free(rgbWrappedKeyBlob);
 
1486
 
 
1487
        if (result == TSS_SUCCESS) {
 
1488
                i = 0;
 
1489
                initData(&data->comm, 3);
 
1490
                if (pAuth != NULL) {
 
1491
                        if (setData(TCSD_PACKET_TYPE_AUTH, i++, pAuth, 0, &data->comm))
 
1492
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1493
                        }
 
1494
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &phKeyTCSI, 0, &data->comm))
 
1495
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1496
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &phKeyHMAC, 0, &data->comm))
 
1497
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1498
        } else
 
1499
                initData(&data->comm, 0);
 
1500
 
 
1501
        data->comm.hdr.u.result = result;
 
1502
 
 
1503
        return TSS_SUCCESS;
 
1504
}
 
1505
 
 
1506
TSS_RESULT
 
1507
tcs_wrap_LoadKeyByUUID(struct tcsd_thread_data *data)
 
1508
{
 
1509
        TCS_CONTEXT_HANDLE hContext;
 
1510
        TSS_UUID uuid;
 
1511
        TCS_LOADKEY_INFO info, *pInfo;
 
1512
        TCS_KEY_HANDLE phKeyTCSI;
 
1513
        TSS_RESULT result;
 
1514
 
 
1515
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1516
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1517
 
 
1518
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1519
 
 
1520
        if (getData(TCSD_PACKET_TYPE_UUID, 1, &uuid, 0, &data->comm))
 
1521
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1522
 
 
1523
        result = getData(TCSD_PACKET_TYPE_LOADKEY_INFO, 2, &info, 0, &data->comm);
 
1524
        if (result == TSS_TCP_RPC_BAD_PACKET_TYPE)
 
1525
                pInfo = NULL;
 
1526
        else if (result)
 
1527
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1528
        else
 
1529
                pInfo = &info;
 
1530
 
 
1531
        pthread_mutex_lock(&tcsp_lock);
 
1532
 
 
1533
        result = key_mgr_load_by_uuid(hContext, &uuid, pInfo, &phKeyTCSI);
 
1534
 
 
1535
        pthread_mutex_unlock(&tcsp_lock);
 
1536
 
 
1537
        if (result == TSS_SUCCESS) {
 
1538
                initData(&data->comm, 2);
 
1539
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &phKeyTCSI, 0, &data->comm))
 
1540
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1541
                if (pInfo != NULL) {
 
1542
                        if (setData(TCSD_PACKET_TYPE_LOADKEY_INFO, 1, pInfo, 0, &data->comm))
 
1543
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1544
                        }
 
1545
        } else {
 
1546
                if (result == TCSERR(TCS_E_KM_LOADFAILED) && pInfo != NULL) {
 
1547
                        initData(&data->comm, 1);
 
1548
                        if (setData(TCSD_PACKET_TYPE_LOADKEY_INFO, 0, pInfo, 0, &data->comm))
 
1549
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1550
                } else
 
1551
                        initData(&data->comm, 0);
 
1552
        }
 
1553
 
 
1554
        data->comm.hdr.u.result = result;
 
1555
 
 
1556
        return TSS_SUCCESS;
 
1557
}
 
1558
 
 
1559
TSS_RESULT
 
1560
tcs_wrap_OSAP(struct tcsd_thread_data *data)
 
1561
{
 
1562
        TCS_CONTEXT_HANDLE hContext;
 
1563
        TCPA_ENTITY_TYPE entityType;
 
1564
        UINT32 entityValue;
 
1565
        TCPA_NONCE nonceOddOSAP;
 
1566
 
 
1567
        TCS_AUTHHANDLE authHandle;
 
1568
        TCPA_NONCE nonceEven;
 
1569
        TCPA_NONCE nonceEvenOSAP;
 
1570
        TSS_RESULT result;
 
1571
 
 
1572
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1573
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1574
 
 
1575
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1576
 
 
1577
        if (getData(TCSD_PACKET_TYPE_UINT16, 1, &entityType, 0, &data->comm))
 
1578
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1579
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &entityValue, 0, &data->comm))
 
1580
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1581
        if (getData(TCSD_PACKET_TYPE_NONCE, 3, &nonceOddOSAP, 0, &data->comm))
 
1582
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1583
 
 
1584
        pthread_mutex_lock(&tcsp_lock);
 
1585
 
 
1586
        result = auth_mgr_osap(hContext, entityType, entityValue, nonceOddOSAP,
 
1587
                               &authHandle, &nonceEven, &nonceEvenOSAP);
 
1588
 
 
1589
        pthread_mutex_unlock(&tcsp_lock);
 
1590
 
 
1591
        if (result == TSS_SUCCESS) {
 
1592
                initData(&data->comm, 3);
 
1593
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &authHandle, 0, &data->comm))
 
1594
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1595
                if (setData(TCSD_PACKET_TYPE_NONCE, 1, &nonceEven, 0, &data->comm))
 
1596
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1597
                if (setData(TCSD_PACKET_TYPE_NONCE, 2, &nonceEvenOSAP, 0, &data->comm))
 
1598
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1599
        } else
 
1600
                initData(&data->comm, 0);
 
1601
 
 
1602
        data->comm.hdr.u.result = result;
 
1603
 
 
1604
        return TSS_SUCCESS;
 
1605
}
 
1606
 
 
1607
TSS_RESULT
 
1608
tcs_wrap_Sign(struct tcsd_thread_data *data)
 
1609
{
 
1610
        TCS_CONTEXT_HANDLE hContext;
 
1611
        TCS_KEY_HANDLE hKey;
 
1612
        UINT32 areaToSignSize;
 
1613
        BYTE *areaToSign;
 
1614
 
 
1615
        TPM_AUTH auth;
 
1616
        TPM_AUTH *pAuth;
 
1617
 
 
1618
        UINT32 sigSize;
 
1619
        BYTE *sig;
 
1620
        TSS_RESULT result;
 
1621
 
 
1622
        int i;
 
1623
 
 
1624
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1625
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1626
 
 
1627
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1628
 
 
1629
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &data->comm))
 
1630
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1631
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &areaToSignSize, 0, &data->comm))
 
1632
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1633
 
 
1634
        areaToSign = calloc(1, areaToSignSize);
 
1635
        if (areaToSign == NULL) {
 
1636
                LogError("malloc of %d bytes failed.", areaToSignSize);
 
1637
                return TCSERR(TSS_E_OUTOFMEMORY);
 
1638
        }
 
1639
        if (getData(TCSD_PACKET_TYPE_PBYTE, 3, areaToSign, areaToSignSize, &data->comm)) {
 
1640
                free(areaToSign);
 
1641
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1642
        }
 
1643
        result = getData(TCSD_PACKET_TYPE_AUTH, 4, &auth, 0, &data->comm);
 
1644
        if (result == TSS_TCP_RPC_BAD_PACKET_TYPE)
 
1645
                pAuth = NULL;
 
1646
        else if (result) {
 
1647
                free(areaToSign);
 
1648
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1649
        } else
 
1650
                pAuth = &auth;
 
1651
 
 
1652
        pthread_mutex_lock(&tcsp_lock);
 
1653
 
 
1654
        result = TCSP_Sign_Internal(hContext, hKey, areaToSignSize, areaToSign,
 
1655
                               pAuth, &sigSize, &sig);
 
1656
 
 
1657
        pthread_mutex_unlock(&tcsp_lock);
 
1658
 
 
1659
        free(areaToSign);
 
1660
 
 
1661
        if (result == TSS_SUCCESS) {
 
1662
                i = 0;
 
1663
                initData(&data->comm, 3);
 
1664
                if (pAuth != NULL) {
 
1665
                        if (setData(TCSD_PACKET_TYPE_AUTH, i++, &auth, 0, &data->comm)) {
 
1666
                                free(sig);
 
1667
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1668
                        }
 
1669
                }
 
1670
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &sigSize, 0, &data->comm)) {
 
1671
                        free(sig);
 
1672
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1673
                }
 
1674
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, sig, sigSize, &data->comm)) {
 
1675
                        free(sig);
 
1676
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1677
                }
 
1678
                free(sig);
 
1679
        } else
 
1680
                initData(&data->comm, 0);
 
1681
 
 
1682
        data->comm.hdr.u.result = result;
 
1683
 
 
1684
        return TSS_SUCCESS;
 
1685
}
 
1686
 
 
1687
TSS_RESULT
 
1688
tcs_wrap_DirWriteAuth(struct tcsd_thread_data *data)
 
1689
{
 
1690
        TSS_RESULT result;
 
1691
        TSS_HCONTEXT hContext;
 
1692
        TCPA_DIRINDEX dirIndex;
 
1693
        TCPA_DIGEST dirDigest;
 
1694
        TPM_AUTH auth;
 
1695
 
 
1696
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1697
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1698
 
 
1699
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1700
 
 
1701
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &dirIndex, 0, &data->comm))
 
1702
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1703
        if (getData(TCSD_PACKET_TYPE_DIGEST, 2, &dirDigest, 0, &data->comm))
 
1704
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1705
        if (getData(TCSD_PACKET_TYPE_AUTH, 3, &auth, 0, &data->comm))
 
1706
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1707
 
 
1708
        pthread_mutex_lock(&tcsp_lock);
 
1709
 
 
1710
        result = TCSP_DirWriteAuth_Internal(hContext, dirIndex, dirDigest, &auth);
 
1711
 
 
1712
        pthread_mutex_unlock(&tcsp_lock);
 
1713
 
 
1714
        if (result == TSS_SUCCESS) {
 
1715
                initData(&data->comm, 1);
 
1716
                if (setData(TCSD_PACKET_TYPE_AUTH, 0, &auth, 0, &data->comm))
 
1717
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1718
        } else
 
1719
                initData(&data->comm, 0);
 
1720
 
 
1721
        data->comm.hdr.u.result = result;
 
1722
 
 
1723
        return TSS_SUCCESS;
 
1724
}
 
1725
 
 
1726
TSS_RESULT
 
1727
tcs_wrap_DirRead(struct tcsd_thread_data *data)
 
1728
{
 
1729
        TSS_RESULT result;
 
1730
        TSS_HCONTEXT hContext;
 
1731
        TCPA_DIRINDEX dirIndex;
 
1732
        TCPA_DIRVALUE dirValue;
 
1733
 
 
1734
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1735
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1736
 
 
1737
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1738
 
 
1739
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &dirIndex, 0, &data->comm))
 
1740
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1741
 
 
1742
        pthread_mutex_lock(&tcsp_lock);
 
1743
 
 
1744
        result = TCSP_DirRead_Internal(hContext, dirIndex, &dirValue);
 
1745
 
 
1746
        pthread_mutex_unlock(&tcsp_lock);
 
1747
 
 
1748
        if (result == TSS_SUCCESS) {
 
1749
                initData(&data->comm, 1);
 
1750
                if (setData(TCSD_PACKET_TYPE_DIGEST, 0, &dirValue, 0, &data->comm))
 
1751
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1752
        } else
 
1753
                initData(&data->comm, 0);
 
1754
 
 
1755
        data->comm.hdr.u.result = result;
 
1756
 
 
1757
        return TSS_SUCCESS;
 
1758
}
 
1759
 
 
1760
TSS_RESULT
 
1761
tcs_wrap_Seal(struct tcsd_thread_data *data)
 
1762
{
 
1763
        TSS_RESULT result;
 
1764
        TCS_CONTEXT_HANDLE hContext;
 
1765
        TCS_KEY_HANDLE keyHandle;
 
1766
        TCPA_ENCAUTH KeyUsageAuth;
 
1767
        UINT32 PCRInfoSize, inDataSize;
 
1768
        BYTE *PCRInfo = NULL, *inData = NULL;
 
1769
        TPM_AUTH emptyAuth, pubAuth, *pAuth;
 
1770
        UINT32 outDataSize;
 
1771
        BYTE *outData;
 
1772
 
 
1773
        int i = 0;
 
1774
 
 
1775
        memset(&emptyAuth, 0, sizeof(TPM_AUTH));
 
1776
        memset(&pubAuth, 0, sizeof(TPM_AUTH));
 
1777
 
 
1778
        if (getData(TCSD_PACKET_TYPE_UINT32, i++, &hContext, 0, &data->comm))
 
1779
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1780
 
 
1781
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1782
 
 
1783
        if (getData(TCSD_PACKET_TYPE_UINT32, i++, &keyHandle, 0, &data->comm))
 
1784
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1785
        if (getData(TCSD_PACKET_TYPE_ENCAUTH, i++, &KeyUsageAuth, 0, &data->comm))
 
1786
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1787
        if (getData(TCSD_PACKET_TYPE_UINT32, i++, &PCRInfoSize, 0, &data->comm))
 
1788
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1789
 
 
1790
        if (PCRInfoSize > 0) {
 
1791
                PCRInfo = calloc(1, PCRInfoSize);
 
1792
                if (PCRInfo == NULL) {
 
1793
                        LogError("malloc of %u bytes failed.", PCRInfoSize);
 
1794
                        return TCSERR(TSS_E_OUTOFMEMORY);
 
1795
                }
 
1796
 
 
1797
                if (getData(TCSD_PACKET_TYPE_PBYTE, i++, PCRInfo, PCRInfoSize, &data->comm)) {
 
1798
                        free(PCRInfo);
 
1799
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1800
                }
 
1801
        }
 
1802
 
 
1803
        if (getData(TCSD_PACKET_TYPE_UINT32, i++, &inDataSize, 0, &data->comm)) {
 
1804
                free(PCRInfo);
 
1805
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1806
        }
 
1807
 
 
1808
        if (inDataSize > 0) {
 
1809
                inData = calloc(1, inDataSize);
 
1810
                if (inData == NULL) {
 
1811
                        LogError("malloc of %u bytes failed.", inDataSize);
 
1812
                        free(PCRInfo);
 
1813
                        return TCSERR(TSS_E_OUTOFMEMORY);
 
1814
                }
 
1815
 
 
1816
                if (getData(TCSD_PACKET_TYPE_PBYTE, i++, inData, inDataSize, &data->comm)) {
 
1817
                        free(inData);
 
1818
                        free(PCRInfo);
 
1819
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1820
                }
 
1821
        }
 
1822
 
 
1823
        result = getData(TCSD_PACKET_TYPE_AUTH, i++, &pubAuth, 0, &data->comm);
 
1824
        if (result == TSS_TCP_RPC_BAD_PACKET_TYPE)
 
1825
                pAuth = NULL;
 
1826
        else if (result) {
 
1827
                free(inData);
 
1828
                free(PCRInfo);
 
1829
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1830
        } else
 
1831
                pAuth = &pubAuth;
 
1832
 
 
1833
        pthread_mutex_lock(&tcsp_lock);
 
1834
 
 
1835
        result = TCSP_Seal_Internal(hContext, keyHandle, KeyUsageAuth, PCRInfoSize, PCRInfo,
 
1836
                        inDataSize, inData, pAuth, &outDataSize, &outData);
 
1837
 
 
1838
        pthread_mutex_unlock(&tcsp_lock);
 
1839
 
 
1840
        free(inData);
 
1841
        free(PCRInfo);
 
1842
 
 
1843
        if (result == TSS_SUCCESS) {
 
1844
                initData(&data->comm, 3);
 
1845
                if (pAuth != NULL) {
 
1846
                        if (setData(TCSD_PACKET_TYPE_AUTH, 0, pAuth, 0, &data->comm)) {
 
1847
                                free(outData);
 
1848
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1849
                        }
 
1850
                }
 
1851
 
 
1852
                if (setData(TCSD_PACKET_TYPE_UINT32, 1, &outDataSize, 0, &data->comm)) {
 
1853
                        free(outData);
 
1854
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1855
                }
 
1856
                if (setData(TCSD_PACKET_TYPE_PBYTE, 2, outData, outDataSize, &data->comm)) {
 
1857
                        free(outData);
 
1858
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1859
                }
 
1860
                free(outData);
 
1861
        } else
 
1862
                initData(&data->comm, 0);
 
1863
 
 
1864
        data->comm.hdr.u.result = result;
 
1865
 
 
1866
        return TSS_SUCCESS;
 
1867
}
 
1868
 
 
1869
TSS_RESULT
 
1870
tcs_wrap_UnSeal(struct tcsd_thread_data *data)
 
1871
{
 
1872
        TCS_CONTEXT_HANDLE hContext;
 
1873
        TCS_KEY_HANDLE parentHandle;
 
1874
        UINT32 inDataSize;
 
1875
        BYTE *inData;
 
1876
 
 
1877
        TPM_AUTH parentAuth, dataAuth, emptyAuth;
 
1878
        TPM_AUTH *pParentAuth, *pDataAuth;
 
1879
 
 
1880
        UINT32 outDataSize;
 
1881
        BYTE *outData;
 
1882
        TSS_RESULT result;
 
1883
 
 
1884
        memset(&emptyAuth, 0, sizeof(TPM_AUTH));
 
1885
 
 
1886
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
1887
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1888
 
 
1889
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
1890
 
 
1891
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &parentHandle, 0, &data->comm))
 
1892
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1893
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &inDataSize, 0, &data->comm))
 
1894
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1895
 
 
1896
        inData = calloc(1, inDataSize);
 
1897
        if (inData == NULL) {
 
1898
                LogError("malloc of %d bytes failed.", inDataSize);
 
1899
                return TCSERR(TSS_E_OUTOFMEMORY);
 
1900
        }
 
1901
 
 
1902
        if (getData(TCSD_PACKET_TYPE_PBYTE, 3, inData, inDataSize, &data->comm)) {
 
1903
                free(inData);
 
1904
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1905
        }
 
1906
#if 0
 
1907
        if (getData(TCSD_PACKET_TYPE_AUTH, 4, &parentAuth, 0, &data->comm)) {
 
1908
                free(inData);
 
1909
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1910
        }
 
1911
        if (!memcmp(&emptyAuth, &parentAuth, sizeof(TPM_AUTH)))
 
1912
                pParentAuth = NULL;
 
1913
        else
 
1914
                pParentAuth = &parentAuth;
 
1915
 
 
1916
        if (getData(TCSD_PACKET_TYPE_AUTH, 5, &dataAuth, 0, &data->comm)) {
 
1917
                free(inData);
 
1918
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1919
        }
 
1920
#else
 
1921
        result = getData(TCSD_PACKET_TYPE_AUTH, 4, &parentAuth, 0, &data->comm);
 
1922
        if (result == TSS_TCP_RPC_BAD_PACKET_TYPE)
 
1923
                pParentAuth = NULL;
 
1924
        else if (result) {
 
1925
                free(inData);
 
1926
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1927
        } else
 
1928
                pParentAuth = &parentAuth;
 
1929
 
 
1930
        result = getData(TCSD_PACKET_TYPE_AUTH, 5, &dataAuth, 0, &data->comm);
 
1931
        if (result == TSS_TCP_RPC_BAD_PACKET_TYPE) {
 
1932
                pDataAuth = pParentAuth;
 
1933
                pParentAuth = NULL;
 
1934
        } else if (result) {
 
1935
                free(inData);
 
1936
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1937
        } else
 
1938
                pDataAuth = &dataAuth;
 
1939
#endif
 
1940
        pthread_mutex_lock(&tcsp_lock);
 
1941
 
 
1942
        result = TCSP_Unseal_Internal(hContext, parentHandle, inDataSize, inData,
 
1943
                                 pParentAuth, pDataAuth, &outDataSize, &outData);
 
1944
 
 
1945
        pthread_mutex_unlock(&tcsp_lock);
 
1946
 
 
1947
        free(inData);
 
1948
 
 
1949
        if (result == TSS_SUCCESS) {
 
1950
                initData(&data->comm, 4);
 
1951
                if (pParentAuth != NULL) {
 
1952
                        if (setData(TCSD_PACKET_TYPE_AUTH, 0, pParentAuth, 0, &data->comm)) {
 
1953
                                free(outData);
 
1954
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1955
                        }
 
1956
                } else {
 
1957
                        if (setData(TCSD_PACKET_TYPE_AUTH, 0, &emptyAuth, 0, &data->comm)) {
 
1958
                                free(outData);
 
1959
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
1960
                        }
 
1961
                }
 
1962
 
 
1963
                if (setData(TCSD_PACKET_TYPE_AUTH, 1, &dataAuth, 0, &data->comm)) {
 
1964
                        free(outData);
 
1965
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1966
                }
 
1967
                if (setData(TCSD_PACKET_TYPE_UINT32, 2, &outDataSize, 0, &data->comm)) {
 
1968
                        free(outData);
 
1969
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1970
                }
 
1971
                if (setData(TCSD_PACKET_TYPE_PBYTE, 3, outData, outDataSize, &data->comm)) {
 
1972
                        free(outData);
 
1973
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
1974
                }
 
1975
                free(outData);
 
1976
        } else
 
1977
                initData(&data->comm, 0);
 
1978
 
 
1979
        data->comm.hdr.u.result = result;
 
1980
 
 
1981
        return TSS_SUCCESS;
 
1982
}
 
1983
 
 
1984
TSS_RESULT
 
1985
tcs_wrap_UnBind(struct tcsd_thread_data *data)
 
1986
{
 
1987
        TCS_CONTEXT_HANDLE hContext;
 
1988
        TCS_KEY_HANDLE keyHandle;
 
1989
        UINT32 inDataSize;
 
1990
        BYTE *inData;
 
1991
 
 
1992
        TPM_AUTH privAuth;
 
1993
        TPM_AUTH *pPrivAuth;
 
1994
 
 
1995
        UINT32 outDataSize;
 
1996
        BYTE *outData;
 
1997
        TSS_RESULT result;
 
1998
 
 
1999
        int i;
 
2000
 
 
2001
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2002
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2003
 
 
2004
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
2005
 
 
2006
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &keyHandle, 0, &data->comm))
 
2007
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2008
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &inDataSize, 0, &data->comm))
 
2009
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2010
 
 
2011
        inData = calloc(1, inDataSize);
 
2012
        if (inData == NULL) {
 
2013
                LogError("malloc of %d bytes failed.", inDataSize);
 
2014
                return TCSERR(TSS_E_OUTOFMEMORY);
 
2015
        }
 
2016
        if (getData(TCSD_PACKET_TYPE_PBYTE, 3, inData, inDataSize, &data->comm)) {
 
2017
                free(inData);
 
2018
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2019
        }
 
2020
#if 0
 
2021
        if (getData(TCSD_PACKET_TYPE_AUTH, 4, &privAuth, 0, &data->comm))
 
2022
                pPrivAuth = NULL;
 
2023
        else
 
2024
                pPrivAuth = &privAuth;
 
2025
#else
 
2026
        result = getData(TCSD_PACKET_TYPE_AUTH, 4, &privAuth, 0, &data->comm);
 
2027
        if (result == TSS_TCP_RPC_BAD_PACKET_TYPE)
 
2028
                pPrivAuth = NULL;
 
2029
        else if (result) {
 
2030
                free(inData);
 
2031
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2032
        } else
 
2033
                pPrivAuth = &privAuth;
 
2034
#endif
 
2035
        pthread_mutex_lock(&tcsp_lock);
 
2036
 
 
2037
        result = TCSP_UnBind_Internal(hContext, keyHandle, inDataSize, inData,
 
2038
                                 pPrivAuth, &outDataSize, &outData);
 
2039
 
 
2040
        pthread_mutex_unlock(&tcsp_lock);
 
2041
 
 
2042
        free(inData);
 
2043
 
 
2044
        if (result == TSS_SUCCESS) {
 
2045
                i = 0;
 
2046
                initData(&data->comm, 3);
 
2047
                if (pPrivAuth != NULL) {
 
2048
                        if (setData(TCSD_PACKET_TYPE_AUTH, i++, pPrivAuth, 0, &data->comm))
 
2049
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2050
                        }
 
2051
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &outDataSize, 0, &data->comm))
 
2052
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2053
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, outData, outDataSize, &data->comm))
 
2054
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2055
                free(outData);
 
2056
        } else
 
2057
                initData(&data->comm, 0);
 
2058
 
 
2059
        data->comm.hdr.u.result = result;
 
2060
 
 
2061
        return TSS_SUCCESS;
 
2062
}
 
2063
 
 
2064
TSS_RESULT
 
2065
tcs_wrap_StirRandom(struct tcsd_thread_data *data)
 
2066
{
 
2067
        TCS_CONTEXT_HANDLE hContext;
 
2068
        UINT32 inDataSize;
 
2069
        BYTE *inData;
 
2070
        TSS_RESULT result;
 
2071
 
 
2072
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2073
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2074
 
 
2075
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
2076
 
 
2077
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &inDataSize, 0, &data->comm))
 
2078
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2079
 
 
2080
        inData = calloc(1, inDataSize);
 
2081
        if (inData == NULL) {
 
2082
                LogError("malloc of %d bytes failed.", inDataSize);
 
2083
                return TCSERR(TSS_E_OUTOFMEMORY);
 
2084
        }
 
2085
        if (getData(TCSD_PACKET_TYPE_PBYTE, 2, inData, inDataSize, &data->comm)) {
 
2086
                free(inData);
 
2087
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2088
        }
 
2089
 
 
2090
        pthread_mutex_lock(&tcsp_lock);
 
2091
 
 
2092
        result = TCSP_StirRandom_Internal(hContext, inDataSize, inData);
 
2093
 
 
2094
        pthread_mutex_unlock(&tcsp_lock);
 
2095
 
 
2096
        free(inData);
 
2097
 
 
2098
        initData(&data->comm, 0);
 
2099
 
 
2100
        data->comm.hdr.u.result = result;
 
2101
 
 
2102
        return TSS_SUCCESS;
 
2103
}
 
2104
 
 
2105
TSS_RESULT
 
2106
tcs_wrap_CreateWrapKey(struct tcsd_thread_data *data)
 
2107
{
 
2108
        TCS_CONTEXT_HANDLE hContext;
 
2109
        TCS_KEY_HANDLE hWrappingKey;
 
2110
        TCPA_ENCAUTH KeyUsageAuth;
 
2111
        TCPA_ENCAUTH KeyMigrationAuth;
 
2112
        UINT32 keyInfoSize;
 
2113
        BYTE *keyInfo;
 
2114
 
 
2115
        TPM_AUTH pAuth;
 
2116
 
 
2117
        UINT32 keyDataSize;
 
2118
        BYTE *keyData;
 
2119
        TSS_RESULT result;
 
2120
 
 
2121
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2122
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2123
 
 
2124
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
2125
 
 
2126
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hWrappingKey, 0, &data->comm))
 
2127
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2128
        if (getData(TCSD_PACKET_TYPE_ENCAUTH, 2, &KeyUsageAuth, 0, &data->comm))
 
2129
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2130
        if (getData(TCSD_PACKET_TYPE_ENCAUTH, 3, &KeyMigrationAuth, 0, &data->comm))
 
2131
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2132
        if (getData(TCSD_PACKET_TYPE_UINT32, 4, &keyInfoSize, 0, &data->comm))
 
2133
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2134
 
 
2135
        keyInfo = calloc(1, keyInfoSize);
 
2136
        if (keyInfo == NULL) {
 
2137
                LogError("malloc of %d bytes failed.", keyInfoSize);
 
2138
                return TCSERR(TSS_E_OUTOFMEMORY);
 
2139
        }
 
2140
        if (getData(TCSD_PACKET_TYPE_PBYTE, 5, keyInfo, keyInfoSize, &data->comm)) {
 
2141
                free(keyInfo);
 
2142
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2143
        }
 
2144
        if (getData(TCSD_PACKET_TYPE_AUTH, 6, &pAuth, 0, &data->comm)) {
 
2145
                free(keyInfo);
 
2146
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2147
        }
 
2148
 
 
2149
        pthread_mutex_lock(&tcsp_lock);
 
2150
 
 
2151
        result = TCSP_CreateWrapKey_Internal(hContext, hWrappingKey, KeyUsageAuth,
 
2152
                                        KeyMigrationAuth, keyInfoSize, keyInfo,
 
2153
                                        &keyDataSize, &keyData, &pAuth);
 
2154
 
 
2155
        pthread_mutex_unlock(&tcsp_lock);
 
2156
 
 
2157
        free(keyInfo);
 
2158
        if (result == TSS_SUCCESS) {
 
2159
                initData(&data->comm, 3);
 
2160
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &keyDataSize, 0, &data->comm)) {
 
2161
                        free(keyData);
 
2162
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2163
                }
 
2164
                if (setData(TCSD_PACKET_TYPE_PBYTE, 1, keyData, keyDataSize, &data->comm)) {
 
2165
                        free(keyData);
 
2166
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2167
                }
 
2168
                free(keyData);
 
2169
                if (setData(TCSD_PACKET_TYPE_AUTH, 2, &pAuth, 0, &data->comm))
 
2170
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2171
        } else
 
2172
                initData(&data->comm, 0);
 
2173
 
 
2174
        data->comm.hdr.u.result = result;
 
2175
 
 
2176
        return TSS_SUCCESS;
 
2177
}
 
2178
 
 
2179
TSS_RESULT
 
2180
tcs_wrap_ChangeAuth(struct tcsd_thread_data *data)
 
2181
{
 
2182
        TCS_CONTEXT_HANDLE hContext;
 
2183
        TCS_KEY_HANDLE parentHandle;
 
2184
        TCPA_PROTOCOL_ID protocolID;
 
2185
        TCPA_ENCAUTH newAuth;
 
2186
        TCPA_ENTITY_TYPE entityType;
 
2187
        UINT32 encDataSize;
 
2188
        BYTE *encData;
 
2189
 
 
2190
        TPM_AUTH ownerAuth;
 
2191
        TPM_AUTH entityAuth;
 
2192
 
 
2193
        UINT32 outDataSize;
 
2194
        BYTE *outData;
 
2195
        TSS_RESULT result;
 
2196
 
 
2197
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2198
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2199
 
 
2200
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
2201
 
 
2202
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &parentHandle, 0, &data->comm))
 
2203
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2204
        if (getData(TCSD_PACKET_TYPE_UINT16, 2, &protocolID, 0, &data->comm))
 
2205
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2206
        if (getData(TCSD_PACKET_TYPE_ENCAUTH, 3, &newAuth, 0, &data->comm))
 
2207
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2208
        if (getData(TCSD_PACKET_TYPE_UINT16, 4, &entityType, 0, &data->comm))
 
2209
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2210
        if (getData(TCSD_PACKET_TYPE_UINT32, 5, &encDataSize, 0, &data->comm))
 
2211
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2212
        encData = calloc(1, encDataSize);
 
2213
        if (encData == NULL) {
 
2214
                LogError("malloc of %d bytes failed.", encDataSize);
 
2215
                return TCSERR(TSS_E_OUTOFMEMORY);
 
2216
        }
 
2217
        if (getData(TCSD_PACKET_TYPE_PBYTE, 6, encData, encDataSize, &data->comm)) {
 
2218
                free(encData);
 
2219
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2220
        }
 
2221
        if (getData(TCSD_PACKET_TYPE_AUTH, 7, &ownerAuth, 0, &data->comm)) {
 
2222
                free(encData);
 
2223
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2224
        }
 
2225
        if (getData(TCSD_PACKET_TYPE_AUTH, 8, &entityAuth, 0, &data->comm)) {
 
2226
                free(encData);
 
2227
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2228
        }
 
2229
 
 
2230
        pthread_mutex_lock(&tcsp_lock);
 
2231
 
 
2232
        result = TCSP_ChangeAuth_Internal(hContext, parentHandle, protocolID,
 
2233
                                     newAuth, entityType, encDataSize, encData,
 
2234
                                     &ownerAuth, &entityAuth, &outDataSize,
 
2235
                                     &outData);
 
2236
 
 
2237
        pthread_mutex_unlock(&tcsp_lock);
 
2238
 
 
2239
        free(encData);
 
2240
        if (result == TSS_SUCCESS) {
 
2241
                initData(&data->comm, 4);
 
2242
                if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm)) {
 
2243
                        free(outData);
 
2244
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2245
                }
 
2246
                if (setData(TCSD_PACKET_TYPE_AUTH, 1, &entityAuth, 0, &data->comm)) {
 
2247
                        free(outData);
 
2248
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2249
                }
 
2250
                if (setData(TCSD_PACKET_TYPE_UINT32, 2, &outDataSize, 0, &data->comm)) {
 
2251
                        free(outData);
 
2252
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2253
                }
 
2254
                if (setData(TCSD_PACKET_TYPE_PBYTE, 3, outData, outDataSize, &data->comm)) {
 
2255
                        free(outData);
 
2256
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2257
                }
 
2258
                free(outData);
 
2259
        } else
 
2260
                initData(&data->comm, 0);
 
2261
 
 
2262
        data->comm.hdr.u.result = result;
 
2263
 
 
2264
        return TSS_SUCCESS;
 
2265
}
 
2266
 
 
2267
TSS_RESULT
 
2268
tcs_wrap_ChangeAuthOwner(struct tcsd_thread_data *data)
 
2269
{
 
2270
 
 
2271
        TCS_CONTEXT_HANDLE hContext;
 
2272
        TCPA_PROTOCOL_ID protocolID;
 
2273
        TCPA_ENCAUTH newAuth;
 
2274
        TCPA_ENTITY_TYPE entityType;
 
2275
 
 
2276
        TPM_AUTH ownerAuth;
 
2277
        TSS_RESULT result;
 
2278
 
 
2279
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2280
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2281
 
 
2282
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
2283
 
 
2284
        if (getData(TCSD_PACKET_TYPE_UINT16, 1, &protocolID, 0, &data->comm))
 
2285
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2286
        if (getData(TCSD_PACKET_TYPE_ENCAUTH, 2, &newAuth, 0, &data->comm))
 
2287
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2288
        if (getData(TCSD_PACKET_TYPE_UINT16, 3, &entityType, 0, &data->comm))
 
2289
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2290
        if (getData(TCSD_PACKET_TYPE_AUTH, 4, &ownerAuth, 0, &data->comm))
 
2291
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2292
 
 
2293
        pthread_mutex_lock(&tcsp_lock);
 
2294
 
 
2295
        result = TCSP_ChangeAuthOwner_Internal(hContext, protocolID, newAuth,
 
2296
                                          entityType, &ownerAuth);
 
2297
 
 
2298
        pthread_mutex_unlock(&tcsp_lock);
 
2299
 
 
2300
        if (result == TSS_SUCCESS) {
 
2301
                initData(&data->comm, 1);
 
2302
                if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm))
 
2303
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2304
        } else
 
2305
                initData(&data->comm, 0);
 
2306
 
 
2307
        data->comm.hdr.u.result = result;
 
2308
 
 
2309
        return TSS_SUCCESS;
 
2310
}
 
2311
 
 
2312
TSS_RESULT
 
2313
tcs_wrap_Quote(struct tcsd_thread_data *data)
 
2314
{
 
2315
        TCS_CONTEXT_HANDLE hContext;
 
2316
        TCS_KEY_HANDLE hKey;
 
2317
        TCPA_NONCE antiReplay;
 
2318
        UINT32 pcrDataSizeIn;
 
2319
        BYTE *pcrDataIn;
 
2320
 
 
2321
        TPM_AUTH privAuth;
 
2322
        TPM_AUTH *pPrivAuth;
 
2323
 
 
2324
        UINT32 pcrDataSizeOut;
 
2325
        BYTE *pcrDataOut;
 
2326
        UINT32 sigSize;
 
2327
        BYTE *sig;
 
2328
        TSS_RESULT result;
 
2329
 
 
2330
        int i;
 
2331
 
 
2332
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2333
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2334
 
 
2335
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
2336
 
 
2337
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &data->comm))
 
2338
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2339
        if (getData(TCSD_PACKET_TYPE_NONCE, 2, &antiReplay, 0, &data->comm))
 
2340
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2341
        if (getData(TCSD_PACKET_TYPE_UINT32, 3, &pcrDataSizeIn, 0, &data->comm))
 
2342
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2343
        pcrDataIn = (BYTE *)calloc(1, pcrDataSizeIn);
 
2344
        if (pcrDataIn == NULL) {
 
2345
                LogError("malloc of %d bytes failed.", pcrDataSizeIn);
 
2346
                return TCSERR(TSS_E_OUTOFMEMORY);
 
2347
        }
 
2348
        if (getData(TCSD_PACKET_TYPE_PBYTE, 4, pcrDataIn, pcrDataSizeIn, &data->comm)) {
 
2349
                free(pcrDataIn);
 
2350
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2351
        }
 
2352
#if 0
 
2353
        if (getData(TCSD_PACKET_TYPE_AUTH, 5, &privAuth, 0, &data->comm))
 
2354
                pPrivAuth = NULL;
 
2355
        else
 
2356
                pPrivAuth = &privAuth;
 
2357
#else
 
2358
        result = getData(TCSD_PACKET_TYPE_AUTH, 5, &privAuth, 0, &data->comm);
 
2359
        if (result == TSS_TCP_RPC_BAD_PACKET_TYPE)
 
2360
                pPrivAuth = NULL;
 
2361
        else if (result) {
 
2362
                free(pcrDataIn);
 
2363
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2364
        } else
 
2365
                pPrivAuth = &privAuth;
 
2366
#endif
 
2367
 
 
2368
        pthread_mutex_lock(&tcsp_lock);
 
2369
 
 
2370
        result = TCSP_Quote_Internal(hContext, hKey, antiReplay, pcrDataSizeIn,
 
2371
                                pcrDataIn, pPrivAuth, &pcrDataSizeOut,
 
2372
                                &pcrDataOut, &sigSize, &sig);
 
2373
 
 
2374
        pthread_mutex_unlock(&tcsp_lock);
 
2375
 
 
2376
        free(pcrDataIn);
 
2377
        if (result == TSS_SUCCESS) {
 
2378
                i = 0;
 
2379
                initData(&data->comm, 5);
 
2380
                if (pPrivAuth != NULL) {
 
2381
                        if (setData(TCSD_PACKET_TYPE_AUTH, i++, pPrivAuth, 0, &data->comm)) {
 
2382
                                free(pcrDataOut);
 
2383
                                free(sig);
 
2384
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2385
                        }
 
2386
                }
 
2387
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &pcrDataSizeOut, 0, &data->comm)) {
 
2388
                        free(pcrDataOut);
 
2389
                        free(sig);
 
2390
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2391
                }
 
2392
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, pcrDataOut, pcrDataSizeOut, &data->comm)) {
 
2393
                        free(pcrDataOut);
 
2394
                        free(sig);
 
2395
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2396
                }
 
2397
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &sigSize, 0, &data->comm)) {
 
2398
                        free(pcrDataOut);
 
2399
                        free(sig);
 
2400
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2401
                }
 
2402
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, sig, sigSize, &data->comm)) {
 
2403
                        free(pcrDataOut);
 
2404
                        free(sig);
 
2405
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2406
                }
 
2407
 
 
2408
                free(pcrDataOut);
 
2409
                free(sig);
 
2410
        } else
 
2411
                initData(&data->comm, 0);
 
2412
 
 
2413
        data->comm.hdr.u.result = result;
 
2414
 
 
2415
        return TSS_SUCCESS;
 
2416
}
 
2417
 
 
2418
TSS_RESULT
 
2419
tcs_wrap_MakeIdentity(struct tcsd_thread_data *data)
 
2420
{
 
2421
        TCS_CONTEXT_HANDLE hContext;
 
2422
        TCPA_ENCAUTH identityAuth;
 
2423
        TCPA_CHOSENID_HASH privCAHash;
 
2424
        UINT32 idKeyInfoSize;
 
2425
        BYTE *idKeyInfo = NULL;
 
2426
 
 
2427
        TPM_AUTH auth1, auth2;
 
2428
        TPM_AUTH *pSRKAuth, *pOwnerAuth;
 
2429
 
 
2430
        UINT32 idKeySize;
 
2431
        BYTE *idKey = NULL;
 
2432
        UINT32 pcIDBindSize;
 
2433
        BYTE *prgbIDBind = NULL;
 
2434
        UINT32 pcECSize;
 
2435
        BYTE *prgbEC = NULL;
 
2436
        UINT32 pcPlatCredSize;
 
2437
        BYTE *prgbPlatCred = NULL;
 
2438
        UINT32 pcConfCredSize;
 
2439
        BYTE *prgbConfCred = NULL;
 
2440
        TSS_RESULT result;
 
2441
 
 
2442
        int i;
 
2443
 
 
2444
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2445
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2446
 
 
2447
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
2448
 
 
2449
        if (getData(TCSD_PACKET_TYPE_ENCAUTH, 1, &identityAuth, 0, &data->comm))
 
2450
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2451
        if (getData(TCSD_PACKET_TYPE_DIGEST, 2, &privCAHash, 0, &data->comm))
 
2452
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2453
 
 
2454
        if (getData(TCSD_PACKET_TYPE_UINT32, 3, &idKeyInfoSize, 0, &data->comm))
 
2455
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2456
        idKeyInfo = (BYTE *) calloc(1, idKeyInfoSize);
 
2457
        if (idKeyInfo == NULL) {
 
2458
                LogError("malloc of %d bytes failed.", idKeyInfoSize);
 
2459
                return TCSERR(TSS_E_OUTOFMEMORY);
 
2460
        }
 
2461
        if (getData(TCSD_PACKET_TYPE_PBYTE, 4, idKeyInfo, idKeyInfoSize, &data->comm)) {
 
2462
                free(idKeyInfo);
 
2463
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2464
        }
 
2465
        if (getData(TCSD_PACKET_TYPE_AUTH, 5, &auth1, 0, &data->comm)) {
 
2466
                free(idKeyInfo);
 
2467
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2468
        }
 
2469
#if 0
 
2470
        /* if the next one is missing, then the previous was really the owner auth */
 
2471
        if (getData(TCSD_PACKET_TYPE_AUTH, 6, &ownerAuth, 0, &data->comm)) {
 
2472
                LogDebug("Failed to get ownerAuth.  SRK auth is really NULL "
 
2473
                          "and single auth is ownerAuth");
 
2474
                pSRKAuth = NULL;
 
2475
                memcpy(&ownerAuth, &srkAuth, sizeof (TPM_AUTH));
 
2476
        } else {
 
2477
                LogDebug("two Auth");
 
2478
                pSRKAuth = &srkAuth;
 
2479
        }
 
2480
#else
 
2481
        result = getData(TCSD_PACKET_TYPE_AUTH, 6, &auth2, 0, &data->comm);
 
2482
        if (result == TSS_TCP_RPC_BAD_PACKET_TYPE) {
 
2483
                pOwnerAuth = &auth1;
 
2484
                pSRKAuth = NULL;
 
2485
        } else if (result) {
 
2486
                free(idKeyInfo);
 
2487
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2488
        } else {
 
2489
                pOwnerAuth = &auth2;
 
2490
                pSRKAuth = &auth1;
 
2491
        }
 
2492
#endif
 
2493
 
 
2494
        pthread_mutex_lock(&tcsp_lock);
 
2495
 
 
2496
        result = TCSP_MakeIdentity_Internal(hContext, identityAuth, privCAHash,
 
2497
                                       idKeyInfoSize, idKeyInfo, pSRKAuth,
 
2498
                                       pOwnerAuth, &idKeySize, &idKey,
 
2499
                                       &pcIDBindSize, &prgbIDBind, &pcECSize,
 
2500
                                       &prgbEC, &pcPlatCredSize, &prgbPlatCred,
 
2501
                                       &pcConfCredSize, &prgbConfCred);
 
2502
 
 
2503
        pthread_mutex_unlock(&tcsp_lock);
 
2504
 
 
2505
        free(idKeyInfo);
 
2506
 
 
2507
        if (result == TSS_SUCCESS) {
 
2508
                i = 0;
 
2509
                initData(&data->comm, 12);
 
2510
                if (pSRKAuth) {
 
2511
                        if (setData(TCSD_PACKET_TYPE_AUTH, i++, pSRKAuth, 0, &data->comm))
 
2512
                                goto internal_error;
 
2513
                }
 
2514
                if (setData(TCSD_PACKET_TYPE_AUTH, i++, pOwnerAuth, 0, &data->comm))
 
2515
                        goto internal_error;
 
2516
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &idKeySize, 0, &data->comm))
 
2517
                        goto internal_error;
 
2518
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, idKey, idKeySize, &data->comm))
 
2519
                        goto internal_error;
 
2520
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &pcIDBindSize, 0, &data->comm))
 
2521
                        goto internal_error;
 
2522
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, prgbIDBind, pcIDBindSize, &data->comm))
 
2523
                        goto internal_error;
 
2524
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &pcECSize, 0, &data->comm))
 
2525
                        goto internal_error;
 
2526
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, prgbEC, pcECSize, &data->comm))
 
2527
                        goto internal_error;
 
2528
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &pcPlatCredSize, 0, &data->comm))
 
2529
                        goto internal_error;
 
2530
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, prgbPlatCred, pcPlatCredSize, &data->comm))
 
2531
                        goto internal_error;
 
2532
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &pcConfCredSize, 0, &data->comm))
 
2533
                        goto internal_error;
 
2534
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, prgbConfCred, pcConfCredSize, &data->comm))
 
2535
                        goto internal_error;
 
2536
 
 
2537
                free(idKey);
 
2538
                free(prgbIDBind);
 
2539
                free(prgbEC);
 
2540
                free(prgbPlatCred);
 
2541
                free(prgbConfCred);
 
2542
        } else
 
2543
                initData(&data->comm, 0);
 
2544
 
 
2545
        data->comm.hdr.u.result = result;
 
2546
 
 
2547
        return TSS_SUCCESS;
 
2548
 
 
2549
internal_error:
 
2550
        free(idKey);
 
2551
        free(prgbIDBind);
 
2552
        free(prgbEC);
 
2553
        free(prgbPlatCred);
 
2554
        free(prgbConfCred);
 
2555
        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2556
}
 
2557
 
 
2558
TSS_RESULT
 
2559
tcs_wrap_EnumRegisteredKeys(struct tcsd_thread_data *data)
 
2560
{
 
2561
        TCS_CONTEXT_HANDLE hContext;
 
2562
        TSS_UUID uuid, *pUuid;
 
2563
        UINT32 cKeyHierarchySize;
 
2564
        TSS_KM_KEYINFO *pKeyHierarchy;
 
2565
        unsigned int i, j;
 
2566
        TSS_RESULT result;
 
2567
 
 
2568
        /* Receive */
 
2569
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2570
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2571
 
 
2572
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
2573
 
 
2574
#if 0
 
2575
        if (getData(TCSD_PACKET_TYPE_UUID , 1, &uuid, 0, &data->comm))
 
2576
                pUuid = NULL;
 
2577
        else
 
2578
                pUuid = &uuid;
 
2579
#else
 
2580
        result = getData(TCSD_PACKET_TYPE_UUID , 1, &uuid, 0, &data->comm);
 
2581
        if (result == TSS_TCP_RPC_BAD_PACKET_TYPE)
 
2582
                pUuid = NULL;
 
2583
        else if (result)
 
2584
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2585
        else
 
2586
                pUuid = &uuid;
 
2587
#endif
 
2588
        result = TCS_EnumRegisteredKeys_Internal(
 
2589
                        hContext,
 
2590
                        pUuid,
 
2591
                        &cKeyHierarchySize,
 
2592
                        &pKeyHierarchy);
 
2593
        if (result == TSS_SUCCESS) {
 
2594
                i = 0;
 
2595
                initData(&data->comm, 1 + cKeyHierarchySize);
 
2596
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &cKeyHierarchySize, 0, &data->comm)) {
 
2597
                        free(pKeyHierarchy);
 
2598
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2599
                }
 
2600
 
 
2601
                for (j = 0; j < cKeyHierarchySize; j++) {
 
2602
                        if (setData(TCSD_PACKET_TYPE_KM_KEYINFO, i++, &pKeyHierarchy[j], 0, &data->comm)) {
 
2603
                                free(pKeyHierarchy);
 
2604
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2605
                        }
 
2606
                }
 
2607
                free(pKeyHierarchy);
 
2608
        } else
 
2609
                initData(&data->comm, 0);
 
2610
 
 
2611
        data->comm.hdr.u.result = result;
 
2612
 
 
2613
        return TSS_SUCCESS;
 
2614
}
 
2615
 
 
2616
TSS_RESULT
 
2617
tcs_wrap_LogPcrEvent(struct tcsd_thread_data *data)
 
2618
{
 
2619
        TCS_CONTEXT_HANDLE hContext;
 
2620
        TSS_PCR_EVENT event;
 
2621
        TSS_RESULT result;
 
2622
        UINT32 number;
 
2623
 
 
2624
        /* Receive */
 
2625
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2626
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2627
 
 
2628
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
2629
 
 
2630
        if (getData(TCSD_PACKET_TYPE_PCR_EVENT , 1, &event, 0, &data->comm))
 
2631
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2632
 
 
2633
        result = TCS_LogPcrEvent_Internal(hContext, event, &number);
 
2634
        if (result == TSS_SUCCESS) {
 
2635
                initData(&data->comm, 1);
 
2636
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &number, 0, &data->comm))
 
2637
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2638
        } else
 
2639
                initData(&data->comm, 0);
 
2640
 
 
2641
        data->comm.hdr.u.result = result;
 
2642
 
 
2643
        return TSS_SUCCESS;
 
2644
}
 
2645
 
 
2646
TSS_RESULT
 
2647
tcs_wrap_GetPcrEvent(struct tcsd_thread_data *data)
 
2648
{
 
2649
        TCS_CONTEXT_HANDLE hContext;
 
2650
        TSS_PCR_EVENT *pEvent = NULL;
 
2651
        TSS_RESULT result;
 
2652
        UINT32 pcrIndex, number, totalSize;
 
2653
        BYTE lengthOnly;
 
2654
 
 
2655
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2656
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2657
 
 
2658
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
2659
 
 
2660
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &pcrIndex, 0, &data->comm))
 
2661
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2662
 
 
2663
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &number, 0, &data->comm))
 
2664
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2665
 
 
2666
        if (getData(TCSD_PACKET_TYPE_BYTE, 3, &lengthOnly, 0, &data->comm))
 
2667
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2668
 
 
2669
        if (lengthOnly)
 
2670
                result = TCS_GetPcrEvent_Internal(hContext, pcrIndex, &number, NULL);
 
2671
        else
 
2672
                result = TCS_GetPcrEvent_Internal(hContext, pcrIndex, &number, &pEvent);
 
2673
 
 
2674
        if (result == TSS_SUCCESS) {
 
2675
                if (lengthOnly == FALSE)
 
2676
                        totalSize = get_pcr_event_size(pEvent);
 
2677
                else
 
2678
                        totalSize = 0;
 
2679
 
 
2680
                initData(&data->comm, 2);
 
2681
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &number, 0, &data->comm)) {
 
2682
                        if (lengthOnly == FALSE)
 
2683
                                free_external_events(1, pEvent);
 
2684
                        free(pEvent);
 
2685
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2686
                }
 
2687
 
 
2688
                if (lengthOnly == FALSE) {
 
2689
                        if (setData(TCSD_PACKET_TYPE_PCR_EVENT, 1, pEvent, 0, &data->comm)) {
 
2690
                                free_external_events(1, pEvent);
 
2691
                                free(pEvent);
 
2692
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2693
                        }
 
2694
                        free_external_events(1, pEvent);
 
2695
                        free(pEvent);
 
2696
                }
 
2697
        } else
 
2698
                initData(&data->comm, 0);
 
2699
 
 
2700
        data->comm.hdr.u.result = result;
 
2701
 
 
2702
        return TSS_SUCCESS;
 
2703
}
 
2704
 
 
2705
TSS_RESULT
 
2706
tcs_wrap_GetPcrEventsByPcr(struct tcsd_thread_data *data)
 
2707
{
 
2708
        TCS_CONTEXT_HANDLE hContext;
 
2709
        TSS_PCR_EVENT *ppEvents = NULL;
 
2710
        TSS_RESULT result;
 
2711
        UINT32 firstEvent, eventCount, totalSize, pcrIndex, i, j;
 
2712
 
 
2713
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2714
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2715
 
 
2716
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
2717
 
 
2718
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &pcrIndex, 0, &data->comm))
 
2719
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2720
 
 
2721
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &firstEvent, 0, &data->comm))
 
2722
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2723
 
 
2724
        if (getData(TCSD_PACKET_TYPE_UINT32, 3, &eventCount, 0, &data->comm))
 
2725
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2726
 
 
2727
        result = TCS_GetPcrEventsByPcr_Internal(hContext, pcrIndex, firstEvent, &eventCount, &ppEvents);
 
2728
        if (result == TSS_SUCCESS) {
 
2729
                for (i = 0, totalSize = 0; i < eventCount; i++)
 
2730
                        totalSize += get_pcr_event_size(&(ppEvents[i]));
 
2731
 
 
2732
                initData(&data->comm, 1 + eventCount);
 
2733
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &eventCount, 0, &data->comm)) {
 
2734
                        free_external_events(eventCount, ppEvents);
 
2735
                        free(ppEvents);
 
2736
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2737
                }
 
2738
 
 
2739
                i = 1;
 
2740
                for (j = 0; j < eventCount; j++) {
 
2741
                        if (setData(TCSD_PACKET_TYPE_PCR_EVENT, i++, &(ppEvents[j]), 0, &data->comm)) {
 
2742
                                free_external_events(eventCount, ppEvents);
 
2743
                                free(ppEvents);
 
2744
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2745
                        }
 
2746
                }
 
2747
 
 
2748
                free_external_events(eventCount, ppEvents);
 
2749
                free(ppEvents);
 
2750
        } else
 
2751
                initData(&data->comm, 0);
 
2752
 
 
2753
        data->comm.hdr.u.result = result;
 
2754
 
 
2755
        return TSS_SUCCESS;
 
2756
}
 
2757
 
 
2758
TSS_RESULT
 
2759
tcs_wrap_GetPcrEventLog(struct tcsd_thread_data *data)
 
2760
{
 
2761
        TCS_CONTEXT_HANDLE hContext;
 
2762
        TSS_PCR_EVENT *ppEvents;
 
2763
        TSS_RESULT result;
 
2764
        UINT32 eventCount, totalSize, i, j;
 
2765
 
 
2766
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2767
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2768
 
 
2769
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
2770
 
 
2771
        result = TCS_GetPcrEventLog_Internal(hContext, &eventCount, &ppEvents);
 
2772
        if (result == TSS_SUCCESS) {
 
2773
                for (i = 0, totalSize = 0; i < eventCount; i++)
 
2774
                        totalSize += get_pcr_event_size(&(ppEvents[i]));
 
2775
 
 
2776
                initData(&data->comm, 1 + eventCount);
 
2777
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &eventCount, 0, &data->comm)) {
 
2778
                        free_external_events(eventCount, ppEvents);
 
2779
                        free(ppEvents);
 
2780
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2781
                }
 
2782
 
 
2783
                i = 1;
 
2784
                for (j = 0; j < eventCount; j++) {
 
2785
                        if (setData(TCSD_PACKET_TYPE_PCR_EVENT, i++, &(ppEvents[j]), 0, &data->comm)) {
 
2786
                                free_external_events(eventCount, ppEvents);
 
2787
                                free(ppEvents);
 
2788
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2789
                        }
 
2790
                }
 
2791
 
 
2792
                free_external_events(eventCount, ppEvents);
 
2793
                free(ppEvents);
 
2794
        } else
 
2795
                initData(&data->comm, 0);
 
2796
 
 
2797
        data->comm.hdr.u.result = result;
 
2798
 
 
2799
        return TSS_SUCCESS;
 
2800
}
 
2801
 
 
2802
TSS_RESULT
 
2803
tcs_wrap_SelfTestFull(struct tcsd_thread_data *data)
 
2804
{
 
2805
        TCS_CONTEXT_HANDLE hContext;
 
2806
        TSS_RESULT result;
 
2807
 
 
2808
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2809
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2810
 
 
2811
        LogDebug("thread %x servicing a %s request", (UINT32)pthread_self(), __FUNCTION__);
 
2812
 
 
2813
        pthread_mutex_lock(&tcsp_lock);
 
2814
 
 
2815
        result = TCSP_SelfTestFull_Internal(hContext);
 
2816
 
 
2817
        pthread_mutex_unlock(&tcsp_lock);
 
2818
 
 
2819
        initData(&data->comm, 0);
 
2820
 
 
2821
        data->comm.hdr.u.result = result;
 
2822
 
 
2823
        return TSS_SUCCESS;
 
2824
}
 
2825
 
 
2826
TSS_RESULT
 
2827
tcs_wrap_CertifySelfTest(struct tcsd_thread_data *data)
 
2828
{
 
2829
        TCS_CONTEXT_HANDLE hContext;
 
2830
        TSS_RESULT result;
 
2831
        UINT32 sigSize;
 
2832
        BYTE *sigData = NULL;
 
2833
        TCS_KEY_HANDLE hKey;
 
2834
        TCPA_NONCE antiReplay;
 
2835
        TPM_AUTH privAuth;
 
2836
        TPM_AUTH *pPrivAuth;
 
2837
        int i;
 
2838
 
 
2839
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2840
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2841
 
 
2842
        LogDebug("thread %x servicing a %s request", (UINT32)pthread_self(), __FUNCTION__);
 
2843
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &data->comm))
 
2844
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2845
        if (getData(TCSD_PACKET_TYPE_NONCE, 2, &antiReplay, 0, &data->comm))
 
2846
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2847
#if 0
 
2848
        if (getData(TCSD_PACKET_TYPE_AUTH, 3, &privAuth, 0, &data->comm))
 
2849
                pPrivAuth = NULL;
 
2850
        else
 
2851
                pPrivAuth = &privAuth;
 
2852
#else
 
2853
        result = getData(TCSD_PACKET_TYPE_AUTH, 3, &privAuth, 0, &data->comm);
 
2854
        if (result == TSS_TCP_RPC_BAD_PACKET_TYPE)
 
2855
                pPrivAuth = NULL;
 
2856
        else if (result)
 
2857
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2858
        else
 
2859
                pPrivAuth = &privAuth;
 
2860
 
 
2861
#endif
 
2862
        pthread_mutex_lock(&tcsp_lock);
 
2863
 
 
2864
        result = TCSP_CertifySelfTest_Internal(hContext, hKey, antiReplay, pPrivAuth, &sigSize, &sigData);
 
2865
 
 
2866
        pthread_mutex_unlock(&tcsp_lock);
 
2867
 
 
2868
        if (result == TSS_SUCCESS) {
 
2869
                i = 0;
 
2870
                initData(&data->comm, 3);
 
2871
                if (pPrivAuth != NULL) {
 
2872
                        if (setData(TCSD_PACKET_TYPE_AUTH, i++, pPrivAuth, 0, &data->comm)) {
 
2873
                                free(sigData);
 
2874
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2875
                        }
 
2876
                }
 
2877
 
 
2878
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &sigSize, 0, &data->comm)) {
 
2879
                        free(sigData);
 
2880
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2881
                }
 
2882
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, sigData, sigSize, &data->comm)) {
 
2883
                        free(sigData);
 
2884
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2885
                }
 
2886
                free(sigData);
 
2887
        } else
 
2888
                initData(&data->comm, 0);
 
2889
 
 
2890
        data->comm.hdr.u.result = result;
 
2891
 
 
2892
        return TSS_SUCCESS;
 
2893
}
 
2894
 
 
2895
TSS_RESULT
 
2896
tcs_wrap_GetTestResult(struct tcsd_thread_data *data)
 
2897
{
 
2898
        TCS_CONTEXT_HANDLE hContext;
 
2899
        TSS_RESULT result;
 
2900
        UINT32 resultDataSize;
 
2901
        BYTE *resultData = NULL;
 
2902
 
 
2903
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2904
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2905
 
 
2906
        LogDebug("thread %x servicing a %s request", (UINT32)pthread_self(), __FUNCTION__);
 
2907
 
 
2908
        pthread_mutex_lock(&tcsp_lock);
 
2909
 
 
2910
        result = TCSP_GetTestResult_Internal(hContext, &resultDataSize, &resultData);
 
2911
 
 
2912
        pthread_mutex_unlock(&tcsp_lock);
 
2913
 
 
2914
        if (result == TSS_SUCCESS) {
 
2915
                initData(&data->comm, 2);
 
2916
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &resultDataSize, 0, &data->comm)) {
 
2917
                        free(resultData);
 
2918
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2919
                }
 
2920
                if (setData(TCSD_PACKET_TYPE_PBYTE, 1, resultData, resultDataSize, &data->comm)) {
 
2921
                        free(resultData);
 
2922
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2923
                }
 
2924
                free(resultData);
 
2925
        } else
 
2926
                initData(&data->comm, 0);
 
2927
 
 
2928
        data->comm.hdr.u.result = result;
 
2929
 
 
2930
        return TSS_SUCCESS;
 
2931
}
 
2932
 
 
2933
TSS_RESULT
 
2934
tcs_wrap_SetOwnerInstall(struct tcsd_thread_data *data)
 
2935
{
 
2936
        TCS_CONTEXT_HANDLE hContext;
 
2937
        TSS_BOOL state;
 
2938
        TSS_RESULT result;
 
2939
 
 
2940
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2941
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2942
 
 
2943
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
2944
 
 
2945
        if (getData(TCSD_PACKET_TYPE_BOOL, 1, &state, 0, &data->comm))
 
2946
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2947
 
 
2948
        pthread_mutex_lock(&tcsp_lock);
 
2949
 
 
2950
        result = TCSP_SetOwnerInstall_Internal(hContext, state);
 
2951
 
 
2952
        pthread_mutex_unlock(&tcsp_lock);
 
2953
 
 
2954
        initData(&data->comm, 0);
 
2955
 
 
2956
        data->comm.hdr.u.result = result;
 
2957
 
 
2958
        return TSS_SUCCESS;
 
2959
}
 
2960
 
 
2961
TSS_RESULT
 
2962
tcs_wrap_OwnerSetDisable(struct tcsd_thread_data *data)
 
2963
{
 
2964
        TCS_CONTEXT_HANDLE hContext;
 
2965
        TSS_BOOL disableState;
 
2966
        TPM_AUTH ownerAuth;
 
2967
        TSS_RESULT result;
 
2968
 
 
2969
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
2970
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2971
 
 
2972
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
2973
 
 
2974
        if (getData(TCSD_PACKET_TYPE_BOOL, 1, &disableState, 0, &data->comm))
 
2975
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2976
 
 
2977
        if (getData(TCSD_PACKET_TYPE_AUTH, 2, &ownerAuth, 0, &data->comm))
 
2978
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
2979
 
 
2980
        pthread_mutex_lock(&tcsp_lock);
 
2981
 
 
2982
        result = TCSP_OwnerSetDisable_Internal(hContext, disableState, &ownerAuth);
 
2983
 
 
2984
        pthread_mutex_unlock(&tcsp_lock);
 
2985
 
 
2986
        if (result == TSS_SUCCESS) {
 
2987
                initData(&data->comm, 1);
 
2988
                if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm))
 
2989
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
2990
        } else
 
2991
                initData(&data->comm, 0);
 
2992
 
 
2993
        data->comm.hdr.u.result = result;
 
2994
 
 
2995
        return TSS_SUCCESS;
 
2996
}
 
2997
 
 
2998
TSS_RESULT
 
2999
tcs_wrap_PhysicalDisable(struct tcsd_thread_data *data)
 
3000
{
 
3001
        TCS_CONTEXT_HANDLE hContext;
 
3002
        TSS_RESULT result;
 
3003
 
 
3004
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
3005
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3006
 
 
3007
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
3008
 
 
3009
        pthread_mutex_lock(&tcsp_lock);
 
3010
 
 
3011
        result = TCSP_PhysicalDisable_Internal(hContext);
 
3012
 
 
3013
        pthread_mutex_unlock(&tcsp_lock);
 
3014
 
 
3015
        initData(&data->comm, 0);
 
3016
 
 
3017
        data->comm.hdr.u.result = result;
 
3018
 
 
3019
        return TSS_SUCCESS;
 
3020
}
 
3021
 
 
3022
TSS_RESULT
 
3023
tcs_wrap_PhysicalPresence(struct tcsd_thread_data *data)
 
3024
{
 
3025
        TCS_CONTEXT_HANDLE hContext;
 
3026
        TSS_RESULT result;
 
3027
        TCPA_PHYSICAL_PRESENCE phyPresFlags;
 
3028
 
 
3029
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
3030
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3031
 
 
3032
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
3033
 
 
3034
        if (getData(TCSD_PACKET_TYPE_UINT16, 1, &phyPresFlags, 0, &data->comm))
 
3035
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3036
 
 
3037
        pthread_mutex_lock(&tcsp_lock);
 
3038
 
 
3039
        result = TCSP_PhysicalPresence_Internal(hContext, phyPresFlags);
 
3040
 
 
3041
        pthread_mutex_unlock(&tcsp_lock);
 
3042
 
 
3043
        initData(&data->comm, 0);
 
3044
 
 
3045
        data->comm.hdr.u.result = result;
 
3046
 
 
3047
        return TSS_SUCCESS;
 
3048
}
 
3049
 
 
3050
TSS_RESULT
 
3051
tcs_wrap_SetTempDeactivated(struct tcsd_thread_data *data)
 
3052
{
 
3053
        TCS_CONTEXT_HANDLE hContext;
 
3054
        TSS_RESULT result;
 
3055
 
 
3056
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
3057
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3058
 
 
3059
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
3060
 
 
3061
        pthread_mutex_lock(&tcsp_lock);
 
3062
 
 
3063
        result = TCSP_SetTempDeactivated_Internal(hContext);
 
3064
 
 
3065
        pthread_mutex_unlock(&tcsp_lock);
 
3066
 
 
3067
        initData(&data->comm, 0);
 
3068
 
 
3069
        data->comm.hdr.u.result = result;
 
3070
 
 
3071
        return TSS_SUCCESS;
 
3072
}
 
3073
 
 
3074
TSS_RESULT
 
3075
tcs_wrap_CertifyKey(struct tcsd_thread_data *data)
 
3076
{
 
3077
        TCS_CONTEXT_HANDLE hContext;
 
3078
        TCS_KEY_HANDLE certHandle, keyHandle;
 
3079
        TPM_AUTH *pCertAuth = NULL, *pKeyAuth = NULL, certAuth, keyAuth, nullAuth;
 
3080
        UINT32 CertifyInfoSize, outDataSize;
 
3081
        BYTE *CertifyInfo, *outData;
 
3082
        TCPA_NONCE antiReplay;
 
3083
        TSS_RESULT result;
 
3084
 
 
3085
        memset(&nullAuth, 0, sizeof(TPM_AUTH));
 
3086
        memset(&certAuth, 0, sizeof(TPM_AUTH));
 
3087
        memset(&keyAuth, 0, sizeof(TPM_AUTH));
 
3088
 
 
3089
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
3090
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3091
 
 
3092
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
3093
 
 
3094
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &certHandle, 0, &data->comm))
 
3095
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3096
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &keyHandle, 0, &data->comm))
 
3097
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3098
        if (getData(TCSD_PACKET_TYPE_NONCE, 3, &antiReplay, 0, &data->comm))
 
3099
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3100
 
 
3101
        if (getData(TCSD_PACKET_TYPE_AUTH, 4, &certAuth, 0, &data->comm))
 
3102
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3103
        if (getData(TCSD_PACKET_TYPE_AUTH, 5, &keyAuth, 0, &data->comm))
 
3104
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3105
 
 
3106
        if (memcmp(&nullAuth, &certAuth, sizeof(TPM_AUTH)))
 
3107
                pCertAuth = &certAuth;
 
3108
 
 
3109
        if (memcmp(&nullAuth, &keyAuth, sizeof(TPM_AUTH)))
 
3110
                pKeyAuth = &keyAuth;
 
3111
 
 
3112
        pthread_mutex_lock(&tcsp_lock);
 
3113
 
 
3114
        result = TCSP_CertifyKey_Internal(hContext, certHandle, keyHandle,
 
3115
                        antiReplay, pCertAuth, pKeyAuth, &CertifyInfoSize,
 
3116
                        &CertifyInfo, &outDataSize, &outData);
 
3117
 
 
3118
        pthread_mutex_unlock(&tcsp_lock);
 
3119
 
 
3120
        if (result == TSS_SUCCESS) {
 
3121
                int i = 0;
 
3122
                initData(&data->comm, 6);
 
3123
                if (pCertAuth) {
 
3124
                        if (setData(TCSD_PACKET_TYPE_AUTH, i++, pCertAuth, 0, &data->comm)) {
 
3125
                                free(CertifyInfo);
 
3126
                                free(outData);
 
3127
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3128
                        }
 
3129
                }
 
3130
                if (pKeyAuth) {
 
3131
                        if (setData(TCSD_PACKET_TYPE_AUTH, i++, pKeyAuth, 0, &data->comm)) {
 
3132
                                free(CertifyInfo);
 
3133
                                free(outData);
 
3134
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3135
                        }
 
3136
                }
 
3137
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &CertifyInfoSize, 0, &data->comm)) {
 
3138
                        free(CertifyInfo);
 
3139
                        free(outData);
 
3140
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3141
                }
 
3142
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, CertifyInfo, CertifyInfoSize, &data->comm)) {
 
3143
                        free(CertifyInfo);
 
3144
                        free(outData);
 
3145
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3146
                }
 
3147
                free(CertifyInfo);
 
3148
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &outDataSize, 0, &data->comm)) {
 
3149
                        free(outData);
 
3150
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3151
                }
 
3152
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, outData, outDataSize, &data->comm)) {
 
3153
                        free(outData);
 
3154
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3155
                }
 
3156
                free(outData);
 
3157
        } else
 
3158
                initData(&data->comm, 0);
 
3159
 
 
3160
        data->comm.hdr.u.result = result;
 
3161
 
 
3162
        return TSS_SUCCESS;
 
3163
}
 
3164
 
 
3165
TSS_RESULT
 
3166
tcs_wrap_GetRegisteredKeyByPublicInfo(struct tcsd_thread_data *data)
 
3167
{
 
3168
        TCS_CONTEXT_HANDLE hContext;
 
3169
        TSS_RESULT result;
 
3170
        UINT32 algId, ulPublicInfoLength, keySize;
 
3171
        BYTE *rgbPublicInfo, *keyBlob;
 
3172
 
 
3173
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
3174
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3175
 
 
3176
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
3177
 
 
3178
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &algId, 0, &data->comm))
 
3179
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3180
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &ulPublicInfoLength, 0, &data->comm))
 
3181
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3182
 
 
3183
        rgbPublicInfo = (BYTE *)calloc(1, ulPublicInfoLength);
 
3184
        if (rgbPublicInfo == NULL) {
 
3185
                LogError("malloc of %d bytes failed.", ulPublicInfoLength);
 
3186
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3187
        }
 
3188
        if (getData(TCSD_PACKET_TYPE_PBYTE, 3, rgbPublicInfo, ulPublicInfoLength, &data->comm)) {
 
3189
                free(rgbPublicInfo);
 
3190
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3191
        }
 
3192
 
 
3193
        pthread_mutex_lock(&tcsp_lock);
 
3194
 
 
3195
        result = TCSP_GetRegisteredKeyByPublicInfo_Internal(hContext, algId,
 
3196
                        ulPublicInfoLength, rgbPublicInfo, &keySize, &keyBlob);
 
3197
 
 
3198
        pthread_mutex_unlock(&tcsp_lock);
 
3199
 
 
3200
        free(rgbPublicInfo);
 
3201
        if (result == TSS_SUCCESS) {
 
3202
                initData(&data->comm, 2);
 
3203
                if (setData(TCSD_PACKET_TYPE_UINT32, 0, &keySize, 0, &data->comm)) {
 
3204
                        free(keyBlob);
 
3205
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3206
                }
 
3207
                if (setData(TCSD_PACKET_TYPE_PBYTE, 1, keyBlob, keySize, &data->comm)) {
 
3208
                        free(keyBlob);
 
3209
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3210
                }
 
3211
                free(keyBlob);
 
3212
        } else
 
3213
                initData(&data->comm, 0);
 
3214
 
 
3215
        data->comm.hdr.u.result = result;
 
3216
 
 
3217
        return TSS_SUCCESS;
 
3218
}
 
3219
 
 
3220
TSS_RESULT
 
3221
tcs_wrap_ActivateIdentity(struct tcsd_thread_data *data)
 
3222
{
 
3223
        TCS_CONTEXT_HANDLE hContext;
 
3224
        TCS_KEY_HANDLE idKeyHandle;
 
3225
        TPM_AUTH *pIdKeyAuth = NULL, *pOwnerAuth = NULL, auth1, auth2;
 
3226
        UINT32 SymmetricKeySize, blobSize;
 
3227
        BYTE *SymmetricKey, *blob;
 
3228
        TSS_RESULT result;
 
3229
 
 
3230
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
3231
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3232
 
 
3233
        LogDebug("thread %x context %x: %s", (UINT32)pthread_self(), hContext, __FUNCTION__);
 
3234
 
 
3235
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &idKeyHandle, 0, &data->comm))
 
3236
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3237
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &blobSize, 0, &data->comm))
 
3238
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3239
 
 
3240
        if ((blob = malloc(blobSize)) == NULL)
 
3241
                return TCSERR(TSS_E_OUTOFMEMORY);
 
3242
 
 
3243
        if (getData(TCSD_PACKET_TYPE_PBYTE, 3, blob, blobSize, &data->comm)) {
 
3244
                free(blob);
 
3245
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3246
        }
 
3247
 
 
3248
        if (getData(TCSD_PACKET_TYPE_AUTH, 4, &auth1, 0, &data->comm)) {
 
3249
                free(blob);
 
3250
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3251
        }
 
3252
#if 0
 
3253
        if (getData(TCSD_PACKET_TYPE_AUTH, 5, &auth2, 0, &data->comm)) {
 
3254
                LogDebugFn("No auth for identity key");
 
3255
                pOwnerAuth = &auth1;
 
3256
        } else {
 
3257
                pIdKeyAuth = &auth1;
 
3258
                pOwnerAuth = &auth2;
 
3259
        }
 
3260
#else
 
3261
        result = getData(TCSD_PACKET_TYPE_AUTH, 5, &auth2, 0, &data->comm);
 
3262
        if (result == TSS_TCP_RPC_BAD_PACKET_TYPE)
 
3263
                pOwnerAuth = &auth1;
 
3264
        else if (result) {
 
3265
                free(blob);
 
3266
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3267
        } else {
 
3268
                pIdKeyAuth = &auth1;
 
3269
                pOwnerAuth = &auth2;
 
3270
        }
 
3271
#endif
 
3272
        pthread_mutex_lock(&tcsp_lock);
 
3273
 
 
3274
        result = TCSP_ActivateTPMIdentity_Internal(hContext, idKeyHandle, blobSize,
 
3275
                                                   blob, pIdKeyAuth, pOwnerAuth,
 
3276
                                                   &SymmetricKeySize,
 
3277
                                                   &SymmetricKey);
 
3278
 
 
3279
        pthread_mutex_unlock(&tcsp_lock);
 
3280
 
 
3281
        free(blob);
 
3282
 
 
3283
        if (result == TSS_SUCCESS) {
 
3284
                int i = 0;
 
3285
                initData(&data->comm, 4);
 
3286
                if (pIdKeyAuth) {
 
3287
                        if (setData(TCSD_PACKET_TYPE_AUTH, i++, pIdKeyAuth, 0, &data->comm)) {
 
3288
                                free(SymmetricKey);
 
3289
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3290
                        }
 
3291
                }
 
3292
                if (setData(TCSD_PACKET_TYPE_AUTH, i++, pOwnerAuth, 0, &data->comm)) {
 
3293
                        free(SymmetricKey);
 
3294
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3295
                }
 
3296
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &SymmetricKeySize, 0, &data->comm)) {
 
3297
                        free(SymmetricKey);
 
3298
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3299
                }
 
3300
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, SymmetricKey, SymmetricKeySize, &data->comm)) {
 
3301
                        free(SymmetricKey);
 
3302
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3303
                }
 
3304
                free(SymmetricKey);
 
3305
        } else
 
3306
                initData(&data->comm, 0);
 
3307
 
 
3308
        data->comm.hdr.u.result = result;
 
3309
 
 
3310
        return TSS_SUCCESS;
 
3311
}
 
3312
 
 
3313
TSS_RESULT
 
3314
tcs_wrap_CreateMigrationBlob(struct tcsd_thread_data *data)
 
3315
{
 
3316
        TCS_CONTEXT_HANDLE hContext;
 
3317
        TSS_RESULT result;
 
3318
        TCS_KEY_HANDLE parentHandle;
 
3319
        TSS_MIGRATE_SCHEME migrationType;
 
3320
        UINT32 MigrationKeyAuthSize, encDataSize, randomSize, outDataSize;
 
3321
        BYTE *MigrationKeyAuth, *encData, *random, *outData;
 
3322
        TPM_AUTH auth1, auth2, *pParentAuth, *pEntityAuth;
 
3323
 
 
3324
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
3325
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3326
 
 
3327
        LogDebugFn("thread %x context %x", (UINT32)pthread_self(), hContext);
 
3328
 
 
3329
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &parentHandle, 0, &data->comm))
 
3330
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3331
        if (getData(TCSD_PACKET_TYPE_UINT16, 2, &migrationType, 0, &data->comm))
 
3332
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3333
 
 
3334
        if (getData(TCSD_PACKET_TYPE_UINT32, 3, &MigrationKeyAuthSize, 0, &data->comm))
 
3335
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3336
 
 
3337
        MigrationKeyAuth = (BYTE *)malloc(MigrationKeyAuthSize);
 
3338
        if (MigrationKeyAuth == NULL) {
 
3339
                LogError("malloc of %d bytes failed.", MigrationKeyAuthSize);
 
3340
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3341
        }
 
3342
        if (getData(TCSD_PACKET_TYPE_PBYTE, 4, MigrationKeyAuth, MigrationKeyAuthSize, &data->comm)) {
 
3343
                free(MigrationKeyAuth);
 
3344
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3345
        }
 
3346
 
 
3347
        if (getData(TCSD_PACKET_TYPE_UINT32, 5, &encDataSize, 0, &data->comm)) {
 
3348
                free(MigrationKeyAuth);
 
3349
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3350
        }
 
3351
 
 
3352
        encData = (BYTE *)malloc(encDataSize);
 
3353
        if (encData == NULL) {
 
3354
                free(MigrationKeyAuth);
 
3355
                LogError("malloc of %d bytes failed.", encDataSize);
 
3356
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3357
        }
 
3358
        if (getData(TCSD_PACKET_TYPE_PBYTE, 6, encData, encDataSize, &data->comm)) {
 
3359
                free(MigrationKeyAuth);
 
3360
                free(encData);
 
3361
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3362
        }
 
3363
 
 
3364
        if (getData(TCSD_PACKET_TYPE_AUTH, 7, &auth1, 0, &data->comm)) {
 
3365
                free(MigrationKeyAuth);
 
3366
                free(encData);
 
3367
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3368
        }
 
3369
 
 
3370
        if (getData(TCSD_PACKET_TYPE_AUTH, 8, &auth2, 0, &data->comm)) {
 
3371
                /* If loading the 2nd auth fails, the first one was entity auth */
 
3372
                pParentAuth = NULL;
 
3373
                pEntityAuth = &auth1;
 
3374
        } else {
 
3375
                /* If loading the 2nd auth succeeds, the first one was parent auth */
 
3376
                pParentAuth = &auth1;
 
3377
                pEntityAuth = &auth2;
 
3378
        }
 
3379
 
 
3380
        pthread_mutex_lock(&tcsp_lock);
 
3381
 
 
3382
        result = TCSP_CreateMigrationBlob_Internal(hContext, parentHandle, migrationType,
 
3383
                                                   MigrationKeyAuthSize, MigrationKeyAuth,
 
3384
                                                   encDataSize, encData, pParentAuth, pEntityAuth,
 
3385
                                                   &randomSize, &random, &outDataSize, &outData);
 
3386
 
 
3387
        pthread_mutex_unlock(&tcsp_lock);
 
3388
 
 
3389
        free(MigrationKeyAuth);
 
3390
        free(encData);
 
3391
        if (result == TSS_SUCCESS) {
 
3392
                int i = 0;
 
3393
                initData(&data->comm, 6);
 
3394
                if (pParentAuth) {
 
3395
                        if (setData(TCSD_PACKET_TYPE_AUTH, i++, pParentAuth, 0, &data->comm)) {
 
3396
                                free(random);
 
3397
                                free(outData);
 
3398
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3399
                        }
 
3400
                }
 
3401
 
 
3402
                if (setData(TCSD_PACKET_TYPE_AUTH, i++, pEntityAuth, 0, &data->comm)) {
 
3403
                        free(random);
 
3404
                        free(outData);
 
3405
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3406
                }
 
3407
 
 
3408
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &randomSize, 0, &data->comm)) {
 
3409
                        free(random);
 
3410
                        free(outData);
 
3411
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3412
                }
 
3413
                if (randomSize > 0) {
 
3414
                        if (setData(TCSD_PACKET_TYPE_PBYTE, i++, random, randomSize, &data->comm)) {
 
3415
                                free(random);
 
3416
                                free(outData);
 
3417
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3418
                        }
 
3419
                }
 
3420
 
 
3421
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &outDataSize, 0, &data->comm)) {
 
3422
                        free(random);
 
3423
                        free(outData);
 
3424
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3425
                }
 
3426
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, outData, outDataSize, &data->comm)) {
 
3427
                        free(random);
 
3428
                        free(outData);
 
3429
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3430
                }
 
3431
 
 
3432
                free(random);
 
3433
                free(outData);
 
3434
        } else
 
3435
                initData(&data->comm, 0);
 
3436
 
 
3437
        data->comm.hdr.u.result = result;
 
3438
 
 
3439
        return TSS_SUCCESS;
 
3440
}
 
3441
 
 
3442
TSS_RESULT
 
3443
tcs_wrap_ConvertMigrationBlob(struct tcsd_thread_data *data)
 
3444
{
 
3445
        TCS_CONTEXT_HANDLE hContext;
 
3446
        TSS_RESULT result;
 
3447
        TCS_KEY_HANDLE parentHandle;
 
3448
        UINT32 outDataSize, randomSize, inDataSize;
 
3449
        BYTE *outData, *random, *inData;
 
3450
        TPM_AUTH parentAuth, *pParentAuth;
 
3451
 
 
3452
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
3453
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3454
 
 
3455
        LogDebugFn("thread %x context %x", (UINT32)pthread_self(), hContext);
 
3456
 
 
3457
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &parentHandle, 0, &data->comm))
 
3458
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3459
 
 
3460
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &inDataSize, 0, &data->comm))
 
3461
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3462
 
 
3463
        inData = (BYTE *)malloc(inDataSize);
 
3464
        if (inData == NULL) {
 
3465
                LogError("malloc of %d bytes failed.", inDataSize);
 
3466
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3467
        }
 
3468
        if (getData(TCSD_PACKET_TYPE_PBYTE, 3, inData, inDataSize, &data->comm)) {
 
3469
                free(inData);
 
3470
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3471
        }
 
3472
 
 
3473
        if (getData(TCSD_PACKET_TYPE_UINT32, 4, &randomSize, 0, &data->comm)) {
 
3474
                free(inData);
 
3475
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3476
        }
 
3477
 
 
3478
        random = (BYTE *)malloc(randomSize);
 
3479
        if (random == NULL) {
 
3480
                free(inData);
 
3481
                LogError("malloc of %d bytes failed.", randomSize);
 
3482
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3483
        }
 
3484
        if (getData(TCSD_PACKET_TYPE_PBYTE, 5, random, randomSize, &data->comm)) {
 
3485
                free(inData);
 
3486
                free(random);
 
3487
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3488
        }
 
3489
 
 
3490
        if (getData(TCSD_PACKET_TYPE_AUTH, 6, &parentAuth, 0, &data->comm))
 
3491
                pParentAuth = NULL;
 
3492
        else
 
3493
                pParentAuth = &parentAuth;
 
3494
 
 
3495
 
 
3496
        pthread_mutex_lock(&tcsp_lock);
 
3497
 
 
3498
        result = TCSP_ConvertMigrationBlob_Internal(hContext, parentHandle, inDataSize, inData,
 
3499
                                                    randomSize, random, pParentAuth, &outDataSize,
 
3500
                                                    &outData);
 
3501
 
 
3502
        pthread_mutex_unlock(&tcsp_lock);
 
3503
 
 
3504
        free(inData);
 
3505
        free(random);
 
3506
        if (result == TSS_SUCCESS) {
 
3507
                int i = 0;
 
3508
                initData(&data->comm, 3);
 
3509
                if (pParentAuth) {
 
3510
                        if (setData(TCSD_PACKET_TYPE_AUTH, i++, pParentAuth, 0, &data->comm)) {
 
3511
                                free(outData);
 
3512
                                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3513
                        }
 
3514
                }
 
3515
 
 
3516
                if (setData(TCSD_PACKET_TYPE_UINT32, i++, &outDataSize, 0, &data->comm)) {
 
3517
                        free(outData);
 
3518
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3519
                }
 
3520
                if (setData(TCSD_PACKET_TYPE_PBYTE, i++, outData, outDataSize, &data->comm)) {
 
3521
                        free(outData);
 
3522
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3523
                }
 
3524
 
 
3525
                free(outData);
 
3526
        } else
 
3527
                initData(&data->comm, 0);
 
3528
 
 
3529
        data->comm.hdr.u.result = result;
 
3530
 
 
3531
        return TSS_SUCCESS;
 
3532
}
 
3533
 
 
3534
TSS_RESULT
 
3535
tcs_wrap_AuthorizeMigrationKey(struct tcsd_thread_data *data)
 
3536
{
 
3537
        TCS_CONTEXT_HANDLE hContext;
 
3538
        TSS_RESULT result;
 
3539
        TSS_MIGRATE_SCHEME migrateScheme;
 
3540
        UINT32 MigrationKeySize, MigrationKeyAuthSize;
 
3541
        BYTE *MigrationKey, *MigrationKeyAuth;
 
3542
        TPM_AUTH ownerAuth;
 
3543
 
 
3544
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
3545
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3546
 
 
3547
        LogDebugFn("thread %x context %x", (UINT32)pthread_self(), hContext);
 
3548
 
 
3549
        if (getData(TCSD_PACKET_TYPE_UINT16, 1, &migrateScheme, 0, &data->comm))
 
3550
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3551
 
 
3552
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &MigrationKeySize, 0, &data->comm))
 
3553
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3554
 
 
3555
        MigrationKey = (BYTE *)malloc(MigrationKeySize);
 
3556
        if (MigrationKey == NULL) {
 
3557
                LogError("malloc of %d bytes failed.", MigrationKeySize);
 
3558
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3559
        }
 
3560
        if (getData(TCSD_PACKET_TYPE_PBYTE, 3, MigrationKey, MigrationKeySize, &data->comm)) {
 
3561
                free(MigrationKey);
 
3562
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3563
        }
 
3564
 
 
3565
        if (getData(TCSD_PACKET_TYPE_AUTH, 4, &ownerAuth, 0, &data->comm)) {
 
3566
                free(MigrationKey);
 
3567
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3568
        }
 
3569
 
 
3570
        pthread_mutex_lock(&tcsp_lock);
 
3571
 
 
3572
        result = TCSP_AuthorizeMigrationKey_Internal(hContext, migrateScheme, MigrationKeySize,
 
3573
                                                     MigrationKey, &ownerAuth,
 
3574
                                                     &MigrationKeyAuthSize, &MigrationKeyAuth);
 
3575
 
 
3576
        pthread_mutex_unlock(&tcsp_lock);
 
3577
 
 
3578
        free(MigrationKey);
 
3579
        if (result == TSS_SUCCESS) {
 
3580
                initData(&data->comm, 3);
 
3581
                if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm)) {
 
3582
                        free(MigrationKeyAuth);
 
3583
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3584
                }
 
3585
 
 
3586
                if (setData(TCSD_PACKET_TYPE_UINT32, 1, &MigrationKeyAuthSize, 0, &data->comm)) {
 
3587
                        free(MigrationKeyAuth);
 
3588
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3589
                }
 
3590
                if (setData(TCSD_PACKET_TYPE_PBYTE, 2, MigrationKeyAuth, MigrationKeyAuthSize,
 
3591
                            &data->comm)) {
 
3592
                        free(MigrationKeyAuth);
 
3593
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3594
                }
 
3595
 
 
3596
                free(MigrationKeyAuth);
 
3597
        } else
 
3598
                initData(&data->comm, 0);
 
3599
 
 
3600
        data->comm.hdr.u.result = result;
 
3601
 
 
3602
        return TSS_SUCCESS;
 
3603
}
 
3604
 
 
3605
TSS_RESULT
 
3606
tcs_wrap_KillMaintenanceFeature(struct tcsd_thread_data *data)
 
3607
{
 
3608
        TCS_CONTEXT_HANDLE hContext;
 
3609
        TSS_RESULT result;
 
3610
        TPM_AUTH ownerAuth;
 
3611
 
 
3612
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
3613
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3614
 
 
3615
        LogDebugFn("thread %x context %x", (UINT32)pthread_self(), hContext);
 
3616
 
 
3617
        if (getData(TCSD_PACKET_TYPE_AUTH, 1, &ownerAuth, 0, &data->comm))
 
3618
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3619
 
 
3620
        pthread_mutex_lock(&tcsp_lock);
 
3621
 
 
3622
        result = TCSP_KillMaintenanceFeature_Internal(hContext, &ownerAuth);
 
3623
 
 
3624
        pthread_mutex_unlock(&tcsp_lock);
 
3625
 
 
3626
        if (result == TSS_SUCCESS) {
 
3627
                initData(&data->comm, 1);
 
3628
                if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm))
 
3629
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3630
        } else
 
3631
                initData(&data->comm, 0);
 
3632
 
 
3633
        data->comm.hdr.u.result = result;
 
3634
 
 
3635
        return TSS_SUCCESS;
 
3636
}
 
3637
 
 
3638
TSS_RESULT
 
3639
tcs_wrap_CreateMaintenanceArchive(struct tcsd_thread_data *data)
 
3640
{
 
3641
        TCS_CONTEXT_HANDLE hContext;
 
3642
        TSS_RESULT result;
 
3643
        TPM_AUTH ownerAuth;
 
3644
        TSS_BOOL generateRandom;
 
3645
        UINT32 randomSize, archiveSize;
 
3646
        BYTE *random, *archive;
 
3647
 
 
3648
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
3649
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3650
 
 
3651
        LogDebugFn("thread %x context %x", (UINT32)pthread_self(), hContext);
 
3652
 
 
3653
        if (getData(TCSD_PACKET_TYPE_BOOL, 1, &generateRandom, 0, &data->comm))
 
3654
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3655
 
 
3656
        if (getData(TCSD_PACKET_TYPE_AUTH, 2, &ownerAuth, 0, &data->comm))
 
3657
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3658
 
 
3659
        pthread_mutex_lock(&tcsp_lock);
 
3660
 
 
3661
        result = TCSP_CreateMaintenanceArchive_Internal(hContext, generateRandom, &ownerAuth,
 
3662
                                                        &randomSize, &random, &archiveSize,
 
3663
                                                        &archive);
 
3664
 
 
3665
        pthread_mutex_unlock(&tcsp_lock);
 
3666
 
 
3667
        if (result == TSS_SUCCESS) {
 
3668
                initData(&data->comm, 5);
 
3669
                if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm)) {
 
3670
                        free(random);
 
3671
                        free(archive);
 
3672
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3673
                }
 
3674
 
 
3675
                if (setData(TCSD_PACKET_TYPE_UINT32, 1, &randomSize, 0, &data->comm)) {
 
3676
                        free(random);
 
3677
                        free(archive);
 
3678
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3679
                }
 
3680
                if (setData(TCSD_PACKET_TYPE_PBYTE, 2, random, randomSize, &data->comm)) {
 
3681
                        free(random);
 
3682
                        free(archive);
 
3683
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3684
                }
 
3685
 
 
3686
                if (setData(TCSD_PACKET_TYPE_UINT32, 3, &archiveSize, 0, &data->comm)) {
 
3687
                        free(random);
 
3688
                        free(archive);
 
3689
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3690
                }
 
3691
                if (setData(TCSD_PACKET_TYPE_PBYTE, 4, archive, archiveSize, &data->comm)) {
 
3692
                        free(random);
 
3693
                        free(archive);
 
3694
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3695
                }
 
3696
 
 
3697
                free(random);
 
3698
                free(archive);
 
3699
        } else
 
3700
                initData(&data->comm, 0);
 
3701
 
 
3702
        data->comm.hdr.u.result = result;
 
3703
 
 
3704
        return TSS_SUCCESS;
 
3705
}
 
3706
 
 
3707
TSS_RESULT
 
3708
tcs_wrap_LoadMaintenanceArchive(struct tcsd_thread_data *data)
 
3709
{
 
3710
        TCS_CONTEXT_HANDLE hContext;
 
3711
        TSS_RESULT result;
 
3712
        TPM_AUTH ownerAuth;
 
3713
        UINT32 dataInSize, dataOutSize;
 
3714
        BYTE *dataIn, *dataOut;
 
3715
 
 
3716
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
3717
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3718
 
 
3719
        LogDebugFn("thread %x context %x", (UINT32)pthread_self(), hContext);
 
3720
 
 
3721
        if (getData(TCSD_PACKET_TYPE_UINT32, 1, &dataInSize, 0, &data->comm))
 
3722
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3723
 
 
3724
        dataIn = (BYTE *)malloc(dataInSize);
 
3725
        if (dataIn == NULL) {
 
3726
                LogError("malloc of %d bytes failed.", dataInSize);
 
3727
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3728
        }
 
3729
        if (getData(TCSD_PACKET_TYPE_PBYTE, 2, dataIn, dataInSize, &data->comm)) {
 
3730
                free(dataIn);
 
3731
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3732
        }
 
3733
 
 
3734
        if (getData(TCSD_PACKET_TYPE_AUTH, 3, &ownerAuth, 0, &data->comm)) {
 
3735
                free(dataIn);
 
3736
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3737
        }
 
3738
 
 
3739
        pthread_mutex_lock(&tcsp_lock);
 
3740
 
 
3741
        result = TCSP_LoadMaintenanceArchive_Internal(hContext, dataInSize, dataIn, &ownerAuth,
 
3742
                                                        &dataOutSize, &dataOut);
 
3743
 
 
3744
        pthread_mutex_unlock(&tcsp_lock);
 
3745
        free(dataIn);
 
3746
 
 
3747
        if (result == TSS_SUCCESS) {
 
3748
                initData(&data->comm, 3);
 
3749
                if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm)) {
 
3750
                        free(dataOut);
 
3751
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3752
                }
 
3753
 
 
3754
                if (setData(TCSD_PACKET_TYPE_UINT32, 1, &dataOutSize, 0, &data->comm)) {
 
3755
                        free(dataOut);
 
3756
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3757
                }
 
3758
                if (setData(TCSD_PACKET_TYPE_PBYTE, 2, dataOut, dataOutSize, &data->comm)) {
 
3759
                        free(dataOut);
 
3760
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3761
                }
 
3762
 
 
3763
                free(dataOut);
 
3764
        } else
 
3765
                initData(&data->comm, 0);
 
3766
 
 
3767
        data->comm.hdr.u.result = result;
 
3768
 
 
3769
        return TSS_SUCCESS;
 
3770
}
 
3771
 
 
3772
TSS_RESULT
 
3773
tcs_wrap_LoadManuMaintPub(struct tcsd_thread_data *data)
 
3774
{
 
3775
        TCS_CONTEXT_HANDLE hContext;
 
3776
        TSS_RESULT result;
 
3777
        UINT32 pubKeySize;
 
3778
        BYTE *pubKey;
 
3779
        TCPA_NONCE antiReplay;
 
3780
        TCPA_DIGEST checksum;
 
3781
 
 
3782
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
3783
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3784
 
 
3785
        LogDebugFn("thread %x context %x", (UINT32)pthread_self(), hContext);
 
3786
 
 
3787
        if (getData(TCSD_PACKET_TYPE_NONCE, 1, &antiReplay, 0, &data->comm))
 
3788
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3789
 
 
3790
        if (getData(TCSD_PACKET_TYPE_UINT32, 2, &pubKeySize, 0, &data->comm))
 
3791
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3792
 
 
3793
        pubKey = (BYTE *)malloc(pubKeySize);
 
3794
        if (pubKey == NULL) {
 
3795
                LogError("malloc of %d bytes failed.", pubKeySize);
 
3796
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3797
        }
 
3798
 
 
3799
        if (getData(TCSD_PACKET_TYPE_PBYTE, 3, pubKey, pubKeySize, &data->comm)) {
 
3800
                free(pubKey);
 
3801
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3802
        }
 
3803
 
 
3804
        pthread_mutex_lock(&tcsp_lock);
 
3805
 
 
3806
        result = TCSP_LoadManuMaintPub_Internal(hContext, antiReplay, pubKeySize, pubKey,
 
3807
                                                &checksum);
 
3808
 
 
3809
        pthread_mutex_unlock(&tcsp_lock);
 
3810
        free(pubKey);
 
3811
 
 
3812
        if (result == TSS_SUCCESS) {
 
3813
#if 0
 
3814
                *hdr = calloc(1, size + sizeof(TCPA_DIGEST));
 
3815
                if (*hdr == NULL) {
 
3816
                        LogError("malloc of %zu bytes failed.", size + sizeof(TCPA_DIGEST));
 
3817
                        return TCSERR(TSS_E_OUTOFMEMORY);
 
3818
                }
 
3819
 
 
3820
                if (setData(TCSD_PACKET_TYPE_DIGEST, 0, &checksum, 0, *hdr)) {
 
3821
                        free(*hdr);
 
3822
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3823
                }
 
3824
#else
 
3825
                initData(&data->comm, 1);
 
3826
                if (setData(TCSD_PACKET_TYPE_DIGEST, 0, &checksum, 0, &data->comm))
 
3827
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3828
#endif
 
3829
        } else {
 
3830
#if 0
 
3831
                *hdr = calloc(1, size);
 
3832
                if (*hdr == NULL) {
 
3833
                        LogError("malloc of %u bytes failed.", size);
 
3834
                        return TCSERR(TSS_E_OUTOFMEMORY);
 
3835
                }
 
3836
                (*hdr)->packet_size = size;
 
3837
#else
 
3838
                initData(&data->comm, 0);
 
3839
#endif
 
3840
        }
 
3841
#if 0
 
3842
        (*hdr)->result = result;
 
3843
#else
 
3844
        data->comm.hdr.u.result = result;
 
3845
#endif
 
3846
 
 
3847
        return TSS_SUCCESS;
 
3848
}
 
3849
#if 0
 
3850
TSS_RESULT
 
3851
tcs_wrap_ReadManuMaintPub(struct tcsd_thread_data *data,
 
3852
                          struct tsp_packet *tsp_data,
 
3853
                          struct tcsd_packet_hdr **hdr)
 
3854
{
 
3855
        TCS_CONTEXT_HANDLE hContext;
 
3856
        TSS_RESULT result;
 
3857
        TCPA_NONCE antiReplay;
 
3858
        TCPA_DIGEST checksum;
 
3859
        UINT32 size = sizeof(struct tcsd_packet_hdr);
 
3860
 
 
3861
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, tsp_data))
 
3862
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3863
 
 
3864
        LogDebugFn("thread %x context %x", (UINT32)pthread_self(), hContext);
 
3865
 
 
3866
        if (getData(TCSD_PACKET_TYPE_NONCE, 1, &antiReplay, 0, tsp_data))
 
3867
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3868
 
 
3869
        pthread_mutex_lock(&tcsp_lock);
 
3870
 
 
3871
        result = TCSP_ReadManuMaintPub_Internal(hContext, antiReplay, &checksum);
 
3872
 
 
3873
        pthread_mutex_unlock(&tcsp_lock);
 
3874
 
 
3875
        if (result == TSS_SUCCESS) {
 
3876
                *hdr = calloc(1, size + sizeof(TCPA_DIGEST));
 
3877
                if (*hdr == NULL) {
 
3878
                        LogError("malloc of %zu bytes failed.", size + sizeof(TCPA_DIGEST));
 
3879
                        return TCSERR(TSS_E_OUTOFMEMORY);
 
3880
                }
 
3881
 
 
3882
                if (setData(TCSD_PACKET_TYPE_DIGEST, 0, &checksum, 0, *hdr)) {
 
3883
                        free(*hdr);
 
3884
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3885
                }
 
3886
        } else {
 
3887
                *hdr = calloc(1, size);
 
3888
                if (*hdr == NULL) {
 
3889
                        LogError("malloc of %u bytes failed.", size);
 
3890
                        return TCSERR(TSS_E_OUTOFMEMORY);
 
3891
                }
 
3892
                (*hdr)->packet_size = size;
 
3893
        }
 
3894
        (*hdr)->result = result;
 
3895
 
 
3896
        return TSS_SUCCESS;
 
3897
}
 
3898
#else
 
3899
TSS_RESULT
 
3900
tcs_wrap_ReadManuMaintPub(struct tcsd_thread_data *data)
 
3901
{
 
3902
        TCS_CONTEXT_HANDLE hContext;
 
3903
        TSS_RESULT result;
 
3904
        TCPA_NONCE antiReplay;
 
3905
        TCPA_DIGEST checksum;
 
3906
 
 
3907
        if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
 
3908
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3909
 
 
3910
        LogDebugFn("thread %x context %x", (UINT32)pthread_self(), hContext);
 
3911
 
 
3912
        if (getData(TCSD_PACKET_TYPE_NONCE, 1, &antiReplay, 0, &data->comm))
 
3913
                return TCSERR(TSS_E_INTERNAL_ERROR);
 
3914
 
 
3915
        pthread_mutex_lock(&tcsp_lock);
 
3916
 
 
3917
        result = TCSP_ReadManuMaintPub_Internal(hContext, antiReplay, &checksum);
 
3918
 
 
3919
        pthread_mutex_unlock(&tcsp_lock);
 
3920
 
 
3921
        if (result == TSS_SUCCESS) {
 
3922
                initData(&data->comm, 1);
 
3923
                if (setData(TCSD_PACKET_TYPE_DIGEST, 0, &checksum, 0, &data->comm))
 
3924
                        return TCSERR(TSS_E_INTERNAL_ERROR);
 
3925
        } else
 
3926
                initData(&data->comm, 0);
 
3927
 
 
3928
        data->comm.hdr.u.result = result;
 
3929
 
 
3930
        return TSS_SUCCESS;
 
3931
}
 
3932
#endif
 
3933
 
 
3934
/* Dispatch */
 
3935
typedef struct tdDispatchTable {
 
3936
        TSS_RESULT (*Func) (struct tcsd_thread_data *);
 
3937
        const char *name;
 
3938
} DispatchTable;
 
3939
 
 
3940
DispatchTable tcs_func_table[TCSD_MAX_NUM_ORDS] = {
 
3941
        {tcs_wrap_Error,"Error"},   /* 0 */
 
3942
        {tcs_wrap_OpenContext,"OpenContext"},
 
3943
        {tcs_wrap_CloseContext,"CloseContext"},
 
3944
        {tcs_wrap_Error,"Error"},
 
3945
        {tcs_wrap_TCSGetCapability,"TCSGetCapability"},
 
3946
        {tcs_wrap_RegisterKey,"RegisterKey"}, /* 5 */
 
3947
        {tcs_wrap_UnregisterKey,"UnregisterKey"},
 
3948
        {tcs_wrap_EnumRegisteredKeys,"EnumRegisteredKeys"},
 
3949
        {tcs_wrap_Error,"Error"},
 
3950
        {tcs_wrap_GetRegisteredKeyBlob,"GetRegisteredKeyBlob"},
 
3951
        {tcs_wrap_GetRegisteredKeyByPublicInfo,"GetRegisteredKeyByPublicInfo"}, /* 10 */
 
3952
        {tcs_wrap_LoadKeyByBlob,"LoadKeyByBlob"},
 
3953
        {tcs_wrap_LoadKeyByUUID,"LoadKeyByUUID"},
 
3954
        {tcs_wrap_EvictKey,"EvictKey"},
 
3955
        {tcs_wrap_CreateWrapKey,"CreateWrapKey"},
 
3956
        {tcs_wrap_GetPubkey,"GetPubkey"}, /* 15 */
 
3957
        {tcs_wrap_MakeIdentity,"MakeIdentity"},
 
3958
        {tcs_wrap_LogPcrEvent,"LogPcrEvent"},
 
3959
        {tcs_wrap_GetPcrEvent,"GetPcrEvent"},
 
3960
        {tcs_wrap_GetPcrEventsByPcr,"GetPcrEventsByPcr"},
 
3961
        {tcs_wrap_GetPcrEventLog,"GetPcrEventLog"}, /* 20 */
 
3962
        {tcs_wrap_SetOwnerInstall,"SetOwnerInstall"},
 
3963
        {tcs_wrap_TakeOwnership,"TakeOwnership"},
 
3964
        {tcs_wrap_OIAP,"OIAP"},
 
3965
        {tcs_wrap_OSAP,"OSAP"},
 
3966
        {tcs_wrap_ChangeAuth,"ChangeAuth"}, /* 25 */
 
3967
        {tcs_wrap_ChangeAuthOwner,"ChangeAuthOwner"},
 
3968
        {tcs_wrap_Error,"Error"},
 
3969
        {tcs_wrap_Error,"Error"},
 
3970
        {tcs_wrap_TerminateHandle,"TerminateHandle"},
 
3971
        {tcs_wrap_ActivateIdentity,"ActivateIdentity"}, /* 30 */
 
3972
        {tcs_wrap_Extend,"Extend"},
 
3973
        {tcs_wrap_PcrRead,"PcrRead"},
 
3974
        {tcs_wrap_Quote,"Quote"},
 
3975
        {tcs_wrap_DirWriteAuth,"DirWriteAuth"},
 
3976
        {tcs_wrap_DirRead,"DirRead"}, /* 35 */
 
3977
        {tcs_wrap_Seal,"Seal"},
 
3978
        {tcs_wrap_UnSeal,"UnSeal"},
 
3979
        {tcs_wrap_UnBind,"UnBind"},
 
3980
        {tcs_wrap_CreateMigrationBlob,"CreateMigrationBlob"},
 
3981
        {tcs_wrap_ConvertMigrationBlob,"ConvertMigrationBlob"}, /* 40 */
 
3982
        {tcs_wrap_AuthorizeMigrationKey,"AuthorizeMigrationKey"},
 
3983
        {tcs_wrap_CertifyKey,"CertifyKey"},
 
3984
        {tcs_wrap_Sign,"Sign"},
 
3985
        {tcs_wrap_GetRandom,"GetRandom"},
 
3986
        {tcs_wrap_StirRandom,"StirRandom"}, /* 45 */
 
3987
        {tcs_wrap_GetCapability,"GetCapability"},
 
3988
        {tcs_wrap_Error,"Error"},
 
3989
        {tcs_wrap_GetCapabilityOwner,"GetCapabilityOwner"},
 
3990
        {tcs_wrap_CreateEndorsementKeyPair,"CreateEndorsementKeyPair"},
 
3991
        {tcs_wrap_ReadPubek,"ReadPubek"}, /* 50 */
 
3992
        {tcs_wrap_DisablePubekRead,"DisablePubekRead"},
 
3993
        {tcs_wrap_OwnerReadPubek,"OwnerReadPubek"},
 
3994
        {tcs_wrap_SelfTestFull,"SelfTestFull"},
 
3995
        {tcs_wrap_CertifySelfTest,"CertifySelfTest"},
 
3996
        {tcs_wrap_Error,"Error"}, /* 55 */
 
3997
        {tcs_wrap_GetTestResult,"GetTestResult"},
 
3998
        {tcs_wrap_OwnerSetDisable,"OwnerSetDisable"},
 
3999
        {tcs_wrap_OwnerClear,"OwnerClear"},
 
4000
        {tcs_wrap_DisableOwnerClear,"DisableOwnerClear"},
 
4001
        {tcs_wrap_ForceClear,"ForceClear"}, /* 60 */
 
4002
        {tcs_wrap_DisableForceClear,"DisableForceClear"},
 
4003
        {tcs_wrap_PhysicalDisable,"PhysicalDisable"},
 
4004
        {tcs_wrap_PhysicalEnable,"PhysicalEnable"},
 
4005
        {tcs_wrap_PhysicalSetDeactivated,"PhysicalSetDeactivated"},
 
4006
        {tcs_wrap_SetTempDeactivated,"SetTempDeactivated"}, /* 65 */
 
4007
        {tcs_wrap_PhysicalPresence,"PhysicalPresence"},
 
4008
        {tcs_wrap_Error,"Error"},
 
4009
        {tcs_wrap_Error,"Error"},
 
4010
        {tcs_wrap_CreateMaintenanceArchive,"CreateMaintenanceArchive"},
 
4011
        {tcs_wrap_LoadMaintenanceArchive,"LoadMaintenanceArchive"}, /* 70 */
 
4012
        {tcs_wrap_KillMaintenanceFeature,"KillMaintenanceFeature"},
 
4013
        {tcs_wrap_LoadManuMaintPub,"LoadManuMaintPub"},
 
4014
        {tcs_wrap_ReadManuMaintPub,"ReadManuMaintPub"}
 
4015
};
 
4016
 
 
4017
int
 
4018
access_control(struct tcsd_thread_data *thread_data)
 
4019
{
 
4020
        int i = 0;
 
4021
        struct hostent *local_hostent = NULL;
 
4022
 
 
4023
        if ((local_hostent = gethostbyname("localhost")) == NULL) {
 
4024
                LogError("Error resolving localhost: %s", hstrerror(h_errno));
 
4025
                return 1;
 
4026
        }
 
4027
 
 
4028
        /* if the request comes from localhost, or is in the accepted ops list,
 
4029
         * approve it */
 
4030
        if (!strncmp(thread_data->hostname, local_hostent->h_name,
 
4031
                     MIN((size_t)local_hostent->h_length, strlen(thread_data->hostname)))) {
 
4032
                return 0;
 
4033
        } else {
 
4034
                while (tcsd_options.remote_ops[i]) {
 
4035
                        if ((UINT32)tcsd_options.remote_ops[i] == thread_data->comm.hdr.u.ordinal) {
 
4036
                                LogInfo("Accepted %s operation from %s",
 
4037
                                        tcs_func_table[thread_data->comm.hdr.u.ordinal].name,
 
4038
                                        thread_data->hostname);
 
4039
                                return 0;
 
4040
                        }
 
4041
                        i++;
 
4042
                }
 
4043
        }
 
4044
 
 
4045
        return 1;
 
4046
}
 
4047
 
 
4048
TSS_RESULT
 
4049
dispatchCommand(struct tcsd_thread_data *data)
 
4050
{
 
4051
        UINT64 offset;
 
4052
        TSS_RESULT result;
 
4053
 
 
4054
        /* First, check the ordinal bounds */
 
4055
        if (data->comm.hdr.u.ordinal >= TCSD_MAX_NUM_ORDS) {
 
4056
                LogError("Illegal TCSD Ordinal");
 
4057
                return TCSERR(TSS_E_FAIL);
 
4058
        }
 
4059
 
 
4060
        LogDebug("Dispatching ordinal %u", data->comm.hdr.u.ordinal);
 
4061
        if (access_control(data)) {
 
4062
                LogWarn("Denied %s operation from %s",
 
4063
                        tcs_func_table[data->comm.hdr.u.ordinal].name, data->hostname);
 
4064
 
 
4065
                /* set platform header */
 
4066
                memset(&data->comm.hdr, 0, sizeof(data->comm.hdr));
 
4067
                data->comm.hdr.packet_size = sizeof(struct tcsd_packet_hdr);
 
4068
                data->comm.hdr.u.result = TCSERR(TSS_E_FAIL);
 
4069
 
 
4070
                /* set the comm buffer */
 
4071
                memset(data->comm.buf, 0, data->comm.buf_size);
 
4072
                offset = 0;
 
4073
                LoadBlob_UINT32(&offset, data->comm.hdr.packet_size, data->comm.buf, NULL);
 
4074
                LoadBlob_UINT32(&offset, data->comm.hdr.u.result, data->comm.buf, NULL);
 
4075
 
 
4076
                return TSS_SUCCESS;
 
4077
        }
 
4078
 
 
4079
        /* Now, dispatch */
 
4080
        if ((result = tcs_func_table[data->comm.hdr.u.ordinal].Func(data)) == TSS_SUCCESS) {
 
4081
                /* set the comm buffer */
 
4082
                offset = 0;
 
4083
                LoadBlob_UINT32(&offset, data->comm.hdr.packet_size, data->comm.buf, NULL);
 
4084
                LoadBlob_UINT32(&offset, data->comm.hdr.u.result, data->comm.buf, NULL);
 
4085
                LoadBlob_UINT32(&offset, data->comm.hdr.num_parms, data->comm.buf, NULL);
 
4086
                LoadBlob_UINT32(&offset, data->comm.hdr.type_size, data->comm.buf, NULL);
 
4087
                LoadBlob_UINT32(&offset, data->comm.hdr.type_offset, data->comm.buf, NULL);
 
4088
                LoadBlob_UINT32(&offset, data->comm.hdr.parm_size, data->comm.buf, NULL);
 
4089
                LoadBlob_UINT32(&offset, data->comm.hdr.parm_offset, data->comm.buf, NULL);
 
4090
        }
 
4091
 
 
4092
        return result;
 
4093
}
 
4094
 
 
4095
TSS_RESULT
 
4096
getTCSDPacket(struct tcsd_thread_data *data)
 
4097
{
 
4098
        /* make sure the all the data is present */
 
4099
        if (data->comm.hdr.num_parms > 0 &&
 
4100
            data->comm.hdr.packet_size !=
 
4101
                    (UINT32)(data->comm.hdr.parm_offset + data->comm.hdr.parm_size))
 
4102
                return TSPERR(TSS_E_INTERNAL_ERROR);
 
4103
 
 
4104
        /* dispatch the command to the TCS */
 
4105
        return dispatchCommand(data);
 
4106
}