summaryrefslogtreecommitdiff
path: root/src/tcs/tcs_context.c
blob: 1e9f4b86abd07a3a9a4d60bf398c6474ce07f040 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

/*
 * Licensed Materials - Property of IBM
 *
 * trousers - An open source TCG Software Stack
 *
 * (C) Copyright International Business Machines Corp. 2004
 *
 */


#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "trousers/tss.h"
#include "trousers_types.h"
#include "tcs_context.h"
#include "tcs_tsp.h"
#include "tcs_utils.h"
#include "tcs_int_literals.h"
#include "capabilities.h"
#include "tcslog.h"
#include "tcsd_wrap.h"
#include "tcsd.h"


unsigned long nextContextHandle = 0xA0000000;
struct tcs_context *tcs_context_table = NULL;

MUTEX_DECLARE_INIT(tcs_ctx_lock);

TCS_CONTEXT_HANDLE getNextHandle();
struct tcs_context *create_tcs_context();
struct tcs_context *get_context(TCS_CONTEXT_HANDLE);
struct tcs_context *get_previous_context(TCS_CONTEXT_HANDLE);

TSS_BOOL initContextHandle = 1;

TCS_CONTEXT_HANDLE
getNextHandle()
{
	UINT32 tempRand;
	time_t currentTime;

	if (initContextHandle) {
		currentTime = time(NULL);
		srand(currentTime);
		tempRand = rand();
		tempRand = tempRand << 16;
		tempRand &= 0x00FF0000;
		nextContextHandle |= tempRand;
		initContextHandle = 0;
	}
	currentTime = time(NULL);
	srand(currentTime + 1);
	tempRand = rand();
	tempRand = tempRand << 8;
	tempRand &= 0x0000FF00;
	if (nextContextHandle == 0)
		return getNextHandle();
	else
		return ((nextContextHandle++) | tempRand);
}

struct tcs_context *
create_tcs_context()
{
	struct tcs_context *ret = (struct tcs_context *)calloc(1, sizeof(struct tcs_context));

	if (ret != NULL) {
		ret->handle = getNextHandle();
		COND_INIT(ret->cond);
	}
	return ret;
}

struct tcs_context *
get_context(TCS_CONTEXT_HANDLE handle)
{
	struct tcs_context *index;
	index = tcs_context_table;
	while (index) {
		if (index->handle == handle)
			break;
		index = index->next;
	}

	return index;
}

struct tcs_context *
get_previous_context(TCS_CONTEXT_HANDLE handle)
{
	struct tcs_context *index;
	index = tcs_context_table;
	while (index) {
		if (index->next) {
			if (index->next->handle == handle)
				return index;
		}
		index = index->next;
	}

	return 0;
}

void
destroy_context(TCS_CONTEXT_HANDLE handle)
{
	struct tcs_context *toKill;
	struct tcs_context *previous;

	MUTEX_LOCK(tcs_ctx_lock);

	toKill = get_context(handle);
	previous = get_previous_context(handle);

	if (!previous && tcs_context_table->handle == handle) {
		/* this means that toKill is the first one */
		tcs_context_table = tcs_context_table->next;
	} else if (previous && toKill) {
		/* both are found */
		previous->next = toKill->next;
	} else {
		MUTEX_UNLOCK(tcs_ctx_lock);
		return;
	}

	MUTEX_UNLOCK(tcs_ctx_lock);

	CTX_ref_count_keys(toKill);

#ifdef TSS_BUILD_TRANSPORT
	/* Free existing transport session if necessary */
	if (toKill->transHandle)
		TCSP_FlushSpecific_Common(toKill->transHandle, TPM_RT_TRANS);
#endif

	free(toKill);
}

TCS_CONTEXT_HANDLE
make_context()
{
	struct tcs_context *index;

	MUTEX_LOCK(tcs_ctx_lock);

	index = tcs_context_table;

	if (!index) {
		tcs_context_table = create_tcs_context();
		if (tcs_context_table == NULL) {
			LogError("Malloc Failure.");
			MUTEX_UNLOCK(tcs_ctx_lock);
			return 0;
		}
		index = tcs_context_table;
	} else {
		while (index->next) {
			index = index->next;
		}
		index->next = create_tcs_context();
		if (index->next == NULL) {
			LogError("Malloc Failure.");
			MUTEX_UNLOCK(tcs_ctx_lock);
			return 0;
		}
		index = index->next;
	}

	MUTEX_UNLOCK(tcs_ctx_lock);

	return index->handle;
}


TSS_RESULT
ctx_verify_context(TCS_CONTEXT_HANDLE tcsContext)
{
	struct tcs_context *c;

	if (tcsContext == InternalContext) {
		LogDebug("Success: %x is an Internal Context", tcsContext);
		return TSS_SUCCESS;
	}

	MUTEX_LOCK(tcs_ctx_lock);

	c = get_context(tcsContext);

	MUTEX_UNLOCK(tcs_ctx_lock);

	if (c == NULL) {
		LogDebug("Fail: Context %x not found", tcsContext);
		return TCSERR(TCS_E_INVALID_CONTEXTHANDLE);
	}

	return TSS_SUCCESS;
}


COND_VAR *
ctx_get_cond_var(TCS_CONTEXT_HANDLE tcs_handle)
{
	struct tcs_context *c;
	COND_VAR *ret = NULL;

	MUTEX_LOCK(tcs_ctx_lock);

	c = get_context(tcs_handle);

	if (c != NULL)
		ret = &c->cond;

	MUTEX_UNLOCK(tcs_ctx_lock);

	return ret;
}

/* the only transport flag at the TCS level is whether the session is exclusive or not. If the app
 * is requesting an exclusive transport session, check that no other exclusive sessions exist and
 * if not, flag this context as being the one. If so, return internal error. */
TSS_RESULT
ctx_req_exclusive_transport(TCS_CONTEXT_HANDLE tcsContext)
{
	TSS_RESULT result = TSS_SUCCESS;
	struct tcs_context *tmp, *self = NULL;

	/* If the daemon is configured to ignore apps that want an exclusive transport, just
	 * return */
	if (!tcsd_options.exclusive_transport)
		return result;

	MUTEX_LOCK(tcs_ctx_lock);

	tmp = tcs_context_table;
	while (tmp) {
		if (tmp->flags & TSS_CONTEXT_FLAG_TRANSPORT_EXCLUSIVE) {
			result = TCSERR(TSS_E_INTERNAL_ERROR);
			goto done;
		}

		if (tmp->handle == tcsContext)
			self = tmp;

		tmp = tmp->next;
	}

	if (self)
		self->flags |= TSS_CONTEXT_FLAG_TRANSPORT_EXCLUSIVE;
	else
		result = TCSERR(TCS_E_INVALID_CONTEXTHANDLE);
done:
	MUTEX_UNLOCK(tcs_ctx_lock);

	return result;
}

TSS_RESULT
ctx_set_transport_enabled(TCS_CONTEXT_HANDLE tcsContext, UINT32 hTransHandle)
{
	TSS_RESULT result = TSS_SUCCESS;
	struct tcs_context *tmp, *self = NULL;

	MUTEX_LOCK(tcs_ctx_lock);

	tmp = tcs_context_table;
	while (tmp) {
		if (tmp->handle == tcsContext) {
			self = tmp;
			break;
		}

		tmp = tmp->next;
	}

	if (self) {
		self->flags |= TSS_CONTEXT_FLAG_TRANSPORT_ENABLED;
		self->transHandle = hTransHandle;
	} else
		result = TCSERR(TCS_E_INVALID_CONTEXTHANDLE);

	MUTEX_UNLOCK(tcs_ctx_lock);

	return result;
}

TSS_RESULT
ctx_set_transport_disabled(TCS_CONTEXT_HANDLE tcsContext, TCS_HANDLE *transHandle)
{
	TSS_RESULT result = TSS_SUCCESS;
	struct tcs_context *tmp, *self = NULL;

	MUTEX_LOCK(tcs_ctx_lock);

	tmp = tcs_context_table;
	while (tmp) {
		if (tmp->handle == tcsContext) {
			self = tmp;
			break;
		}

		tmp = tmp->next;
	}

	if (self) {
		if (!transHandle || *transHandle == self->transHandle) {
			self->transHandle = 0;
			self->flags &= ~TSS_CONTEXT_FLAG_TRANSPORT_ENABLED;
		}
	} else
		result = TCSERR(TCS_E_INVALID_CONTEXTHANDLE);

	MUTEX_UNLOCK(tcs_ctx_lock);

	return result;
}