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 "trousers_types.h"
19#include "tcs_tsp.h"
20#include "tcs_utils.h"
21#include "tcs_int_literals.h"
22#include "capabilities.h"
23#include "tcslog.h"
24#include "tcsd_wrap.h"
25#include "tcsd.h"
26#include "tcs_utils.h"
27#include "rpc_tcstp_tcs.h"
28
29
30TSS_RESULT
31tcs_wrap_Quote(struct tcsd_thread_data *data)
32{
33	TCS_CONTEXT_HANDLE hContext;
34	TCS_KEY_HANDLE hKey;
35	TCPA_NONCE antiReplay;
36	UINT32 pcrDataSizeIn;
37	BYTE *pcrDataIn;
38
39	TPM_AUTH privAuth;
40	TPM_AUTH *pPrivAuth;
41
42	UINT32 pcrDataSizeOut;
43	BYTE *pcrDataOut;
44	UINT32 sigSize;
45	BYTE *sig;
46	TSS_RESULT result;
47
48	int i;
49
50	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
51		return TCSERR(TSS_E_INTERNAL_ERROR);
52
53	if ((result = ctx_verify_context(hContext)))
54		goto done;
55
56	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
57
58	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &data->comm))
59		return TCSERR(TSS_E_INTERNAL_ERROR);
60	if (getData(TCSD_PACKET_TYPE_NONCE, 2, &antiReplay, 0, &data->comm))
61		return TCSERR(TSS_E_INTERNAL_ERROR);
62	if (getData(TCSD_PACKET_TYPE_UINT32, 3, &pcrDataSizeIn, 0, &data->comm))
63		return TCSERR(TSS_E_INTERNAL_ERROR);
64	pcrDataIn = (BYTE *)calloc(1, pcrDataSizeIn);
65	if (pcrDataIn == NULL) {
66		LogError("malloc of %d bytes failed.", pcrDataSizeIn);
67		return TCSERR(TSS_E_OUTOFMEMORY);
68	}
69	if (getData(TCSD_PACKET_TYPE_PBYTE, 4, pcrDataIn, pcrDataSizeIn, &data->comm)) {
70		free(pcrDataIn);
71		return TCSERR(TSS_E_INTERNAL_ERROR);
72	}
73
74	result = getData(TCSD_PACKET_TYPE_AUTH, 5, &privAuth, 0, &data->comm);
75	if (result == TSS_TCP_RPC_BAD_PACKET_TYPE)
76		pPrivAuth = NULL;
77	else if (result) {
78		free(pcrDataIn);
79		return result;
80	} else
81		pPrivAuth = &privAuth;
82
83	MUTEX_LOCK(tcsp_lock);
84
85	result = TCSP_Quote_Internal(hContext, hKey, antiReplay, pcrDataSizeIn, pcrDataIn,
86				     pPrivAuth, &pcrDataSizeOut, &pcrDataOut, &sigSize, &sig);
87
88	MUTEX_UNLOCK(tcsp_lock);
89	free(pcrDataIn);
90
91	if (result == TSS_SUCCESS) {
92		i = 0;
93		initData(&data->comm, 5);
94		if (pPrivAuth != NULL) {
95			if (setData(TCSD_PACKET_TYPE_AUTH, i++, pPrivAuth, 0, &data->comm)) {
96				free(pcrDataOut);
97				free(sig);
98				return TCSERR(TSS_E_INTERNAL_ERROR);
99			}
100		}
101		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &pcrDataSizeOut, 0, &data->comm)) {
102			free(pcrDataOut);
103			free(sig);
104			return TCSERR(TSS_E_INTERNAL_ERROR);
105		}
106		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, pcrDataOut, pcrDataSizeOut, &data->comm)) {
107			free(pcrDataOut);
108			free(sig);
109			return TCSERR(TSS_E_INTERNAL_ERROR);
110		}
111		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &sigSize, 0, &data->comm)) {
112			free(pcrDataOut);
113			free(sig);
114			return TCSERR(TSS_E_INTERNAL_ERROR);
115		}
116		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, sig, sigSize, &data->comm)) {
117			free(pcrDataOut);
118			free(sig);
119			return TCSERR(TSS_E_INTERNAL_ERROR);
120		}
121
122		free(pcrDataOut);
123		free(sig);
124	} else
125done:		initData(&data->comm, 0);
126
127	data->comm.hdr.u.result = result;
128	return TSS_SUCCESS;
129}
130