xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tcs/rpc/tcstp/rpc_sign.c (revision 1023804e3833a0bd94414f2545512128f6502c74)
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 
30 TSS_RESULT
tcs_wrap_Sign(struct tcsd_thread_data * data)31 tcs_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 	if ((result = ctx_verify_context(hContext)))
51 		goto done;
52 
53 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
54 
55 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &data->comm))
56 		return TCSERR(TSS_E_INTERNAL_ERROR);
57 	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &areaToSignSize, 0, &data->comm))
58 		return TCSERR(TSS_E_INTERNAL_ERROR);
59 
60 	areaToSign = calloc(1, areaToSignSize);
61 	if (areaToSign == NULL) {
62 		LogError("malloc of %d bytes failed.", areaToSignSize);
63 		return TCSERR(TSS_E_OUTOFMEMORY);
64 	}
65 	if (getData(TCSD_PACKET_TYPE_PBYTE, 3, areaToSign, areaToSignSize, &data->comm)) {
66 		free(areaToSign);
67 		return TCSERR(TSS_E_INTERNAL_ERROR);
68 	}
69 	result = getData(TCSD_PACKET_TYPE_AUTH, 4, &auth, 0, &data->comm);
70 	if (result == TSS_TCP_RPC_BAD_PACKET_TYPE)
71 		pAuth = NULL;
72 	else if (result) {
73 		free(areaToSign);
74 		return result;
75 	} else
76 		pAuth = &auth;
77 
78 	MUTEX_LOCK(tcsp_lock);
79 
80 	result = TCSP_Sign_Internal(hContext, hKey, areaToSignSize, areaToSign, pAuth, &sigSize,
81 				    &sig);
82 
83 	MUTEX_UNLOCK(tcsp_lock);
84 	free(areaToSign);
85 
86 	if (result == TSS_SUCCESS) {
87 		i = 0;
88 		initData(&data->comm, 3);
89 		if (pAuth != NULL) {
90 			if (setData(TCSD_PACKET_TYPE_AUTH, i++, &auth, 0, &data->comm)) {
91 				free(sig);
92 				return TCSERR(TSS_E_INTERNAL_ERROR);
93 			}
94 		}
95 		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &sigSize, 0, &data->comm)) {
96 			free(sig);
97 			return TCSERR(TSS_E_INTERNAL_ERROR);
98 		}
99 		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, sig, sigSize, &data->comm)) {
100 			free(sig);
101 			return TCSERR(TSS_E_INTERNAL_ERROR);
102 		}
103 		free(sig);
104 	} else
105 done:		initData(&data->comm, 0);
106 
107 	data->comm.hdr.u.result = result;
108 	return TSS_SUCCESS;
109 }
110