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_ChangeAuth(struct tcsd_thread_data * data)31 tcs_wrap_ChangeAuth(struct tcsd_thread_data *data)
32 {
33 	TCS_CONTEXT_HANDLE hContext;
34 	TCS_KEY_HANDLE parentHandle;
35 	TCPA_PROTOCOL_ID protocolID;
36 	TCPA_ENCAUTH newAuth;
37 	TCPA_ENTITY_TYPE entityType;
38 	UINT32 encDataSize;
39 	BYTE *encData;
40 
41 	TPM_AUTH ownerAuth;
42 	TPM_AUTH entityAuth;
43 
44 	UINT32 outDataSize;
45 	BYTE *outData;
46 	TSS_RESULT result;
47 
48 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
49 		return TCSERR(TSS_E_INTERNAL_ERROR);
50 
51 	if ((result = ctx_verify_context(hContext)))
52 		goto done;
53 
54 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
55 
56 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &parentHandle, 0, &data->comm))
57 		return TCSERR(TSS_E_INTERNAL_ERROR);
58 	if (getData(TCSD_PACKET_TYPE_UINT16, 2, &protocolID, 0, &data->comm))
59 		return TCSERR(TSS_E_INTERNAL_ERROR);
60 	if (getData(TCSD_PACKET_TYPE_ENCAUTH, 3, &newAuth, 0, &data->comm))
61 		return TCSERR(TSS_E_INTERNAL_ERROR);
62 	if (getData(TCSD_PACKET_TYPE_UINT16, 4, &entityType, 0, &data->comm))
63 		return TCSERR(TSS_E_INTERNAL_ERROR);
64 	if (getData(TCSD_PACKET_TYPE_UINT32, 5, &encDataSize, 0, &data->comm))
65 		return TCSERR(TSS_E_INTERNAL_ERROR);
66 	encData = calloc(1, encDataSize);
67 	if (encData == NULL) {
68 		LogError("malloc of %d bytes failed.", encDataSize);
69 		return TCSERR(TSS_E_OUTOFMEMORY);
70 	}
71 	if (getData(TCSD_PACKET_TYPE_PBYTE, 6, encData, encDataSize, &data->comm)) {
72 		free(encData);
73 		return TCSERR(TSS_E_INTERNAL_ERROR);
74 	}
75 	if (getData(TCSD_PACKET_TYPE_AUTH, 7, &ownerAuth, 0, &data->comm)) {
76 		free(encData);
77 		return TCSERR(TSS_E_INTERNAL_ERROR);
78 	}
79 	if (getData(TCSD_PACKET_TYPE_AUTH, 8, &entityAuth, 0, &data->comm)) {
80 		free(encData);
81 		return TCSERR(TSS_E_INTERNAL_ERROR);
82 	}
83 
84 	MUTEX_LOCK(tcsp_lock);
85 
86 	result = TCSP_ChangeAuth_Internal(hContext, parentHandle, protocolID, newAuth, entityType,
87 					  encDataSize, encData, &ownerAuth, &entityAuth,
88 					  &outDataSize, &outData);
89 
90 	MUTEX_UNLOCK(tcsp_lock);
91 	free(encData);
92 	if (result == TSS_SUCCESS) {
93 		initData(&data->comm, 4);
94 		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm)) {
95 			free(outData);
96 			return TCSERR(TSS_E_INTERNAL_ERROR);
97 		}
98 		if (setData(TCSD_PACKET_TYPE_AUTH, 1, &entityAuth, 0, &data->comm)) {
99 			free(outData);
100 			return TCSERR(TSS_E_INTERNAL_ERROR);
101 		}
102 		if (setData(TCSD_PACKET_TYPE_UINT32, 2, &outDataSize, 0, &data->comm)) {
103 			free(outData);
104 			return TCSERR(TSS_E_INTERNAL_ERROR);
105 		}
106 		if (setData(TCSD_PACKET_TYPE_PBYTE, 3, outData, outDataSize, &data->comm)) {
107 			free(outData);
108 			return TCSERR(TSS_E_INTERNAL_ERROR);
109 		}
110 		free(outData);
111 	} else
112 done:		initData(&data->comm, 0);
113 
114 	data->comm.hdr.u.result = result;
115 	return TSS_SUCCESS;
116 }
117 
118 TSS_RESULT
tcs_wrap_ChangeAuthOwner(struct tcsd_thread_data * data)119 tcs_wrap_ChangeAuthOwner(struct tcsd_thread_data *data)
120 {
121 
122 	TCS_CONTEXT_HANDLE hContext;
123 	TCPA_PROTOCOL_ID protocolID;
124 	TCPA_ENCAUTH newAuth;
125 	TCPA_ENTITY_TYPE entityType;
126 
127 	TPM_AUTH ownerAuth;
128 	TSS_RESULT result;
129 
130 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
131 		return TCSERR(TSS_E_INTERNAL_ERROR);
132 
133 	if ((result = ctx_verify_context(hContext)))
134 		goto done;
135 
136 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
137 
138 	if (getData(TCSD_PACKET_TYPE_UINT16, 1, &protocolID, 0, &data->comm))
139 		return TCSERR(TSS_E_INTERNAL_ERROR);
140 	if (getData(TCSD_PACKET_TYPE_ENCAUTH, 2, &newAuth, 0, &data->comm))
141 		return TCSERR(TSS_E_INTERNAL_ERROR);
142 	if (getData(TCSD_PACKET_TYPE_UINT16, 3, &entityType, 0, &data->comm))
143 		return TCSERR(TSS_E_INTERNAL_ERROR);
144 	if (getData(TCSD_PACKET_TYPE_AUTH, 4, &ownerAuth, 0, &data->comm))
145 		return TCSERR(TSS_E_INTERNAL_ERROR);
146 
147 	MUTEX_LOCK(tcsp_lock);
148 
149 	result = TCSP_ChangeAuthOwner_Internal(hContext, protocolID, newAuth, entityType,
150 					       &ownerAuth);
151 
152 	MUTEX_UNLOCK(tcsp_lock);
153 
154 	if (result == TSS_SUCCESS) {
155 		initData(&data->comm, 1);
156 		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm)) {
157 			return TCSERR(TSS_E_INTERNAL_ERROR);
158 		}
159 	} else
160 done:		initData(&data->comm, 0);
161 
162 	data->comm.hdr.u.result = result;
163 	return TSS_SUCCESS;
164 }
165