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_Sign(struct tcsd_thread_data *data)
32{
33	TCS_CONTEXT_HANDLE hContext;
34	TCS_KEY_HANDLE hKey;
35	UINT32 areaToSignSize;
36	BYTE *areaToSign;
37
38	TPM_AUTH auth;
39	TPM_AUTH *pAuth;
40
41	UINT32 sigSize;
42	BYTE *sig;
43	TSS_RESULT result;
44
45	int i;
46
47	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
48		return TCSERR(TSS_E_INTERNAL_ERROR);
49
50	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
51
52	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &data->comm))
53		return TCSERR(TSS_E_INTERNAL_ERROR);
54	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &areaToSignSize, 0, &data->comm))
55		return TCSERR(TSS_E_INTERNAL_ERROR);
56
57	areaToSign = calloc(1, areaToSignSize);
58	if (areaToSign == NULL) {
59		LogError("malloc of %d bytes failed.", areaToSignSize);
60		return TCSERR(TSS_E_OUTOFMEMORY);
61	}
62	if (getData(TCSD_PACKET_TYPE_PBYTE, 3, areaToSign, areaToSignSize, &data->comm)) {
63		free(areaToSign);
64		return TCSERR(TSS_E_INTERNAL_ERROR);
65	}
66	result = getData(TCSD_PACKET_TYPE_AUTH, 4, &auth, 0, &data->comm);
67	if (result == TSS_TCP_RPC_BAD_PACKET_TYPE)
68		pAuth = NULL;
69	else if (result) {
70		free(areaToSign);
71		return result;
72	} else
73		pAuth = &auth;
74
75	MUTEX_LOCK(tcsp_lock);
76
77	result = TCSP_Sign_Internal(hContext, hKey, areaToSignSize, areaToSign, pAuth, &sigSize,
78				    &sig);
79
80	MUTEX_UNLOCK(tcsp_lock);
81	free(areaToSign);
82
83	if (result == TSS_SUCCESS) {
84		i = 0;
85		initData(&data->comm, 3);
86		if (pAuth != NULL) {
87			if (setData(TCSD_PACKET_TYPE_AUTH, i++, &auth, 0, &data->comm)) {
88				free(sig);
89				return TCSERR(TSS_E_INTERNAL_ERROR);
90			}
91		}
92		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &sigSize, 0, &data->comm)) {
93			free(sig);
94			return TCSERR(TSS_E_INTERNAL_ERROR);
95		}
96		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, sig, sigSize, &data->comm)) {
97			free(sig);
98			return TCSERR(TSS_E_INTERNAL_ERROR);
99		}
100		free(sig);
101	} else
102		initData(&data->comm, 0);
103
104	data->comm.hdr.u.result = result;
105	return TSS_SUCCESS;
106}
107