summaryrefslogtreecommitdiff
path: root/src/tcs/tcsi_delegate.c
blob: b46501c7172717f3d1d634b621c055ed49dd1378 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366

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


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

#include "trousers/tss.h"
#include "trousers_types.h"
#include "tcs_utils.h"
#include "tcslog.h"
#include "req_mgr.h"

TSS_RESULT
TCSP_Delegate_Manage_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
			      TPM_FAMILY_ID familyID,		/* in */
			      TPM_FAMILY_OPERATION opFlag,	/* in */
			      UINT32 opDataSize,		/* in */
			      BYTE *opData,			/* in */
			      TPM_AUTH *ownerAuth,		/* in/out */
			      UINT32 *retDataSize,		/* out */
			      BYTE **retData)			/* out */
{
	TSS_RESULT result;
	UINT64 offset = 0;
	UINT32 paramSize;
	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];

	LogDebugFn("Enter");

	if ((result = ctx_verify_context(hContext)))
		return result;

	if (ownerAuth) {
		if ((result = auth_mgr_check(hContext, &ownerAuth->AuthHandle)))
			return result;
	}

	if ((result = tpm_rqu_build(TPM_ORD_Delegate_Manage, &offset, txBlob, familyID, opFlag,
				    opDataSize, opData, ownerAuth)))
		goto done;

	if ((result = req_mgr_submit_req(txBlob)))
		goto done;

	result = UnloadBlob_Header(txBlob, &paramSize);
	if (!result) {
		result = tpm_rsp_parse(TPM_ORD_Delegate_Manage, txBlob, paramSize, retDataSize,
				       retData, ownerAuth, NULL);
	}

	LogResult("Delegate_Manage", result);

done:
	auth_mgr_release_auth(ownerAuth, NULL, hContext);

	return result;
}

TSS_RESULT
TCSP_Delegate_CreateKeyDelegation_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
					   TCS_KEY_HANDLE hKey,		/* in */
					   UINT32 publicInfoSize,	/* in */
					   BYTE *publicInfo,		/* in */
					   TPM_ENCAUTH *encDelAuth,	/* in */
					   TPM_AUTH *keyAuth,		/* in, out */
					   UINT32 *blobSize,		/* out */
					   BYTE **blob)			/* out */
{
	TSS_RESULT result;
	TCPA_KEY_HANDLE keySlot;
	UINT64 offset = 0;
	UINT32 paramSize;
	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];

	LogDebugFn("Enter");

	if ((result = ctx_verify_context(hContext)))
		return result;

	if (keyAuth) {
		if ((result = auth_mgr_check(hContext, &keyAuth->AuthHandle)))
			return result;
	}

	if ((result = ensureKeyIsLoaded(hContext, hKey, &keySlot)))
		goto done;

	if ((result = tpm_rqu_build(TPM_ORD_Delegate_CreateKeyDelegation, &offset, txBlob, keySlot,
				    publicInfoSize, publicInfo, encDelAuth, keyAuth)))
		goto done;

	if ((result = req_mgr_submit_req(txBlob)))
		goto done;

	result = UnloadBlob_Header(txBlob, &paramSize);
	if (!result) {
		result = tpm_rsp_parse(TPM_ORD_Delegate_CreateKeyDelegation, txBlob, paramSize,
				       blobSize, blob, keyAuth, NULL);
	}

	LogResult("Delegate_CreateKeyDelegation", result);

done:
	auth_mgr_release_auth(keyAuth, NULL, hContext);

	return result;
}

TSS_RESULT
TCSP_Delegate_CreateOwnerDelegation_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
					     TSS_BOOL increment,		/* in */
					     UINT32 publicInfoSize,		/* in */
					     BYTE *publicInfo,			/* in */
					     TPM_ENCAUTH *encDelAuth,		/* in */
					     TPM_AUTH *ownerAuth,		/* in, out */
					     UINT32 *blobSize,			/* out */
					     BYTE **blob)			/* out */
{
	TSS_RESULT result;
	UINT64 offset = 0;
	UINT32 paramSize;
	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];

	LogDebugFn("Enter");

	if ((result = ctx_verify_context(hContext)))
		return result;

	if (ownerAuth) {
		if ((result = auth_mgr_check(hContext, &ownerAuth->AuthHandle)))
			return result;
	}

	if ((result = tpm_rqu_build(TPM_ORD_Delegate_CreateOwnerDelegation, &offset, txBlob,
				    increment, publicInfoSize, publicInfo, encDelAuth, ownerAuth)))
		goto done;

	if ((result = req_mgr_submit_req(txBlob)))
		goto done;

	result = UnloadBlob_Header(txBlob, &paramSize);
	if (!result) {
		result = tpm_rsp_parse(TPM_ORD_Delegate_CreateOwnerDelegation, txBlob, paramSize,
				       blobSize, blob, ownerAuth, NULL);
	}

	LogResult("Delegate_CreateOwnerDelegation", result);

done:
	auth_mgr_release_auth(ownerAuth, NULL, hContext);

	return result;
}

TSS_RESULT
TCSP_Delegate_LoadOwnerDelegation_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
					   TPM_DELEGATE_INDEX index,	/* in */
					   UINT32 blobSize,		/* in */
					   BYTE *blob,			/* in */
					   TPM_AUTH *ownerAuth)		/* in, out */
{
	TSS_RESULT result;
	UINT64 offset = 0;
	UINT32 paramSize;
	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];

	LogDebugFn("Enter");

	if ((result = ctx_verify_context(hContext)))
		return result;

	if (ownerAuth) {
		if ((result = auth_mgr_check(hContext, &ownerAuth->AuthHandle)))
			return result;
	}

	if ((result = tpm_rqu_build(TPM_ORD_Delegate_LoadOwnerDelegation, &offset, txBlob, index,
				    blobSize, blob, ownerAuth)))
		goto done;

	if ((result = req_mgr_submit_req(txBlob)))
		goto done;

	result = UnloadBlob_Header(txBlob, &paramSize);
	if (!result) {
		result = tpm_rsp_parse(TPM_ORD_Delegate_LoadOwnerDelegation, txBlob, paramSize,
				       ownerAuth);
	}

	LogResult("Delegate_LoadOwnerDelegation", result);

done:
	auth_mgr_release_auth(ownerAuth, NULL, hContext);

	return result;
}

TSS_RESULT
TCSP_Delegate_ReadTable_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
				 UINT32 *familyTableSize,	/* out */
				 BYTE **familyTable,		/* out */
				 UINT32 *delegateTableSize,	/* out */
				 BYTE **delegateTable)		/* out */
{
	TSS_RESULT result;
	UINT64 offset = 0;
	UINT32 paramSize;
	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];

	LogDebugFn("Enter");

	if ((result = ctx_verify_context(hContext)))
		return result;

	if ((result = tpm_rqu_build(TPM_ORD_Delegate_ReadTable, &offset, txBlob, NULL)))
		return result;

	if ((result = req_mgr_submit_req(txBlob)))
		return result;

	result = UnloadBlob_Header(txBlob, &paramSize);
	if (!result) {
		result = tpm_rsp_parse(TPM_ORD_Delegate_ReadTable, txBlob, paramSize,
				       familyTableSize, familyTable, delegateTableSize,
				       delegateTable, NULL, NULL);
	}

	LogResult("Delegate_ReadTable", result);

	return result;
}

TSS_RESULT
TCSP_Delegate_UpdateVerificationCount_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
					       UINT32 inputSize,		/* in */
					       BYTE *input,			/* in */
					       TPM_AUTH *ownerAuth,		/* in, out */
					       UINT32 *outputSize,		/* out */
					       BYTE **output)			/* out */
{
	TSS_RESULT result;
	UINT64 offset = 0;
	UINT32 paramSize;
	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];

	LogDebugFn("Enter");

	if ((result = ctx_verify_context(hContext)))
		return result;

	if (ownerAuth) {
		if ((result = auth_mgr_check(hContext, &ownerAuth->AuthHandle)))
			return result;
	}

	if ((result = tpm_rqu_build(TPM_ORD_Delegate_UpdateVerification, &offset, txBlob, inputSize,
				    inputSize, input, ownerAuth, NULL)))
		goto done;

	if ((result = req_mgr_submit_req(txBlob)))
		goto done;

	result = UnloadBlob_Header(txBlob, &paramSize);
	if (!result) {
		result = tpm_rsp_parse(TPM_ORD_Delegate_UpdateVerification, txBlob, paramSize,
				       outputSize, output, ownerAuth, NULL);
	}

	LogResult("Delegate_UpdateVerificationCount", result);

done:
	auth_mgr_release_auth(ownerAuth, NULL, hContext);

	return result;
}

TSS_RESULT
TCSP_Delegate_VerifyDelegation_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
					UINT32 delegateSize,		/* in */
					BYTE *delegate)			/* in */
{
	TSS_RESULT result;
	UINT64 offset = 0;
	UINT32 paramSize;
	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];

	LogDebugFn("Enter");

	if ((result = ctx_verify_context(hContext)))
		return result;

	if ((result = tpm_rqu_build(TPM_ORD_Delegate_VerifyDelegation, &offset, txBlob,
				    delegateSize, delegateSize, delegate, NULL, NULL)))
		return result;

	if ((result = req_mgr_submit_req(txBlob)))
		return result;

	result = UnloadBlob_Header(txBlob, &paramSize);

	LogResult("Delegate_VerifyDelegation", result);

	return result;
}

TSS_RESULT
TCSP_DSAP_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
		   TPM_ENTITY_TYPE entityType,	/* in */
		   TCS_KEY_HANDLE keyHandle,	/* in */
		   TPM_NONCE *nonceOddDSAP,	/* in */
		   UINT32 entityValueSize,	/* in */
		   BYTE *entityValue,		/* in */
		   TCS_AUTHHANDLE *authHandle,	/* out */
		   TPM_NONCE *nonceEven,	/* out */
		   TPM_NONCE *nonceEvenDSAP)	/* out */
{
	TSS_RESULT result;
	UINT64 offset = 0;
	UINT32 paramSize;
	TPM_KEY_HANDLE tpmKeyHandle;
	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];

	LogDebugFn("Enter");

	if ((result = ctx_verify_context(hContext)))
		return result;

	if (ensureKeyIsLoaded(hContext, keyHandle, &tpmKeyHandle))
		return TCSERR(TSS_E_KEY_NOT_LOADED);

	/* are the maximum number of auth sessions open? */
	if (auth_mgr_req_new(hContext) == FALSE) {
		if ((result = auth_mgr_swap_out(hContext)))
			goto done;
	}

	if ((result = tpm_rqu_build(TPM_ORD_DSAP, &offset, txBlob, entityType, tpmKeyHandle,
				    nonceOddDSAP, entityValueSize, entityValue)))
		return result;

	if ((result = req_mgr_submit_req(txBlob)))
		return result;

	result = UnloadBlob_Header(txBlob, &paramSize);
	if (!result) {
		if ((result = tpm_rsp_parse(TPM_ORD_DSAP, txBlob, paramSize, authHandle,
					    nonceEven->nonce, nonceEvenDSAP->nonce)))
			goto done;

		/* success, add an entry to the table */
		result = auth_mgr_add(hContext, *authHandle);
	}
done:
	LogResult("DSAP", result);

	return result;
}