1
2/*
3 * The Initial Developer of the Original Code is Intel Corporation.
4 * Portions created by Intel Corporation are Copyright (C) 2007 Intel Corporation.
5 * All Rights Reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the Common Public License as published by
9 * IBM Corporation; either version 1 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * Common Public License for more details.
16 *
17 * You should have received a copy of the Common Public License
18 * along with this program; if not, a copy can be viewed at
19 * http://www.opensource.org/licenses/cpl1.0.php.
20 *
21 * trousers - An open source TCG Software Stack
22 *
23 * Author: james.xu@intel.com Rossey.liu@intel.com
24 *
25 */
26
27#include <stdlib.h>
28#include <stdio.h>
29#include <string.h>
30#include <inttypes.h>
31
32#include "trousers/tss.h"
33#include "trousers_types.h"
34#include "tcs_tsp.h"
35#include "tcsps.h"
36#include "tcs_utils.h"
37#include "tcs_int_literals.h"
38#include "capabilities.h"
39#include "tcslog.h"
40#include "req_mgr.h"
41#include "tcsd_wrap.h"
42#include "tcsd.h"
43
44TSS_RESULT
45TCSP_NV_DefineOrReleaseSpace_Internal(TCS_CONTEXT_HANDLE hContext, /* in */
46				      UINT32 cPubInfoSize,	/* in */
47				      BYTE* pPubInfo,	/* in */
48				      TPM_ENCAUTH encAuth,	/* in */
49				      TPM_AUTH* pAuth)	/* in, out */
50{
51	UINT64 offset = 0;
52	UINT32 paramSize;
53	TSS_RESULT result;
54	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
55
56	LogDebugFn("Enter");
57	if ((result = ctx_verify_context(hContext)))
58		return result;
59
60	if (pAuth) {
61		if ((result = auth_mgr_check(hContext, &pAuth->AuthHandle)))
62			goto done;
63	}
64
65	if ((result = tpm_rqu_build(TPM_ORD_NV_DefineSpace, &offset, txBlob, cPubInfoSize, pPubInfo,
66				    TPM_ENCAUTH_SIZE, encAuth.authdata, pAuth)))
67		return result;
68
69	LogDebug("req_mgr_submit_req  (oldOffset=%" PRIu64 ")", offset);
70	if ((result = req_mgr_submit_req(txBlob)))
71		goto done;
72
73	result = UnloadBlob_Header(txBlob, &paramSize);
74	LogDebug("UnloadBlob  (paramSize=%u) result=%u", paramSize, result);
75	if (!result) {
76		result = tpm_rsp_parse(TPM_ORD_NV_DefineSpace, txBlob, paramSize, pAuth);
77	}
78done:
79	LogDebug("Leaving DefineSpace with result:%u", result);
80	auth_mgr_release_auth(pAuth, NULL, hContext);
81	return result;
82}
83
84TSS_RESULT
85TCSP_NV_WriteValue_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
86			    TSS_NV_INDEX hNVStore,	/* in */
87			    UINT32 offset,		/* in */
88			    UINT32 ulDataLength,	/* in */
89			    BYTE * rgbDataToWrite,	/* in */
90			    TPM_AUTH * privAuth)	/* in, out */
91{
92	UINT64 off_set = 0;
93	UINT32 paramSize;
94	TSS_RESULT result;
95	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
96
97	LogDebugFn("Enter");
98	if ( (result = ctx_verify_context(hContext)))
99		return result;
100	if (privAuth) {
101		if ((result = auth_mgr_check(hContext, &privAuth->AuthHandle)))
102			goto done;
103	}
104
105	if ((result = tpm_rqu_build(TPM_ORD_NV_WriteValue, &off_set, txBlob, hNVStore, offset,
106				    ulDataLength, rgbDataToWrite, privAuth)))
107		return result;
108
109	LogDebug("req_mgr_submit_req  (oldOffset=%" PRIu64 ")", off_set);
110	if ((result = req_mgr_submit_req(txBlob)))
111		goto done;
112
113	result = UnloadBlob_Header(txBlob, &paramSize);
114	LogDebug("UnloadBlob  (paramSize=%u) result=%u", paramSize, result);
115	if (!result) {
116		result = tpm_rsp_parse(TPM_ORD_NV_WriteValue, txBlob, paramSize, privAuth);
117	}
118done:
119	LogDebug("Leaving NVWriteValue with result:%u", result);
120	auth_mgr_release_auth(privAuth, NULL, hContext);
121	return result;
122}
123
124TSS_RESULT
125TCSP_NV_WriteValueAuth_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
126				TSS_NV_INDEX hNVStore,	/* in */
127				UINT32 offset,		/* in */
128				UINT32 ulDataLength,	/* in */
129				BYTE * rgbDataToWrite,	/* in */
130				TPM_AUTH * NVAuth)	/* in, out */
131{
132	UINT64 off_set = 0;
133	UINT32 paramSize;
134	TSS_RESULT result;
135	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
136
137	LogDebugFn("Enter");
138	if ((result = ctx_verify_context(hContext)))
139		return result;
140	if ((result = auth_mgr_check(hContext, &NVAuth->AuthHandle)))
141		goto done;
142
143	if ((result = tpm_rqu_build(TPM_ORD_NV_WriteValueAuth, &off_set, txBlob, hNVStore, offset,
144				    ulDataLength, rgbDataToWrite, NVAuth)))
145		return result;
146
147	LogDebug("req_mgr_submit_req  (oldOffset=%" PRIu64 ")", off_set);
148	if ((result = req_mgr_submit_req(txBlob)))
149		goto done;
150
151	result = UnloadBlob_Header(txBlob, &paramSize);
152	LogDebug("UnloadBlob  (paramSize=%u) result=%u", paramSize, result);
153	if (!result) {
154		result = tpm_rsp_parse(TPM_ORD_NV_WriteValueAuth, txBlob, paramSize, NVAuth);
155	}
156done:
157	LogDebug("Leaving NVWriteValueAuth with result:%u", result);
158	auth_mgr_release_auth(NVAuth, NULL, hContext);
159	return result;
160}
161
162TSS_RESULT
163TCSP_NV_ReadValue_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
164			   TSS_NV_INDEX hNVStore,	/* in */
165			   UINT32 offset,	/* in */
166			   UINT32 * pulDataLength,	/* in, out */
167			   TPM_AUTH * privAuth,	/* in, out */
168			   BYTE ** rgbDataRead)	/* out */
169{
170	UINT64 off_set = 0;
171	UINT32 paramSize;
172	TSS_RESULT result;
173	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
174
175	LogDebugFn("Enter");
176	if ((result = ctx_verify_context(hContext)))
177		return result;
178
179	if (privAuth) {
180		if ((result = auth_mgr_check(hContext, &privAuth->AuthHandle)))
181			goto done;
182	}
183
184	if ((result = tpm_rqu_build(TPM_ORD_NV_ReadValue, &off_set, txBlob, hNVStore, offset,
185				    *pulDataLength, privAuth)))
186		return result;
187
188	LogDebug("req_mgr_submit_req  (oldOffset=%" PRIu64 ")", off_set);
189	if ((result = req_mgr_submit_req(txBlob)))
190		goto done;
191
192	result = UnloadBlob_Header(txBlob, &paramSize);
193	LogDebug("UnloadBlob  (paramSize=%u) result=%u", paramSize, result);
194	if (!result) {
195		result = tpm_rsp_parse(TPM_ORD_NV_ReadValue, txBlob, paramSize, pulDataLength,
196				       rgbDataRead, privAuth, NULL);
197	}
198done:
199	LogDebug("Leaving NVReadValue with result:%u", result);
200	auth_mgr_release_auth(privAuth, NULL, hContext);
201	return result;
202}
203
204TSS_RESULT
205TCSP_NV_ReadValueAuth_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
206			       TSS_NV_INDEX hNVStore,	/* in */
207			       UINT32 offset,		/* in */
208			       UINT32 * pulDataLength,	/* in, out */
209			       TPM_AUTH * NVAuth,	/* in, out */
210			       BYTE ** rgbDataRead)	/* out */
211{
212	UINT64 off_set = 0;
213	UINT32 paramSize;
214	TSS_RESULT result;
215	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
216
217	LogDebugFn("Enter");
218	if ((result = ctx_verify_context(hContext)))
219		return result;
220	if ((result = auth_mgr_check(hContext, &NVAuth->AuthHandle)))
221		goto done;
222
223	if ((result = tpm_rqu_build(TPM_ORD_NV_ReadValueAuth, &off_set, txBlob, hNVStore, offset,
224				    *pulDataLength, NVAuth)))
225		return result;
226
227	LogDebug("req_mgr_submit_req  (oldOffset=%" PRIu64 ")", off_set);
228	if ((result = req_mgr_submit_req(txBlob)))
229		goto done;
230
231	result = UnloadBlob_Header(txBlob, &paramSize);
232	LogDebug("UnloadBlob  (paramSize=%u) result=%u", paramSize, result);
233	if (!result) {
234		result = tpm_rsp_parse(TPM_ORD_NV_ReadValueAuth, txBlob, paramSize, pulDataLength,
235				       rgbDataRead, NVAuth, NULL);
236	}
237done:
238	LogDebug("Leaving NVReadValueAuth with result:%u", result);
239	auth_mgr_release_auth(NVAuth, NULL, hContext);
240	return result;
241}
242
243