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 * trousers - An open source TCG Software Stack
7 *
8 * Author: james.xu@intel.com Rossey.liu@intel.com
9 *
10 */
11
12#include <stdlib.h>
13#include <stdio.h>
14#include <string.h>
15#include <inttypes.h>
16
17#include "trousers/tss.h"
18#include "trousers_types.h"
19#include "tcs_tsp.h"
20#include "tcsps.h"
21#include "tcs_utils.h"
22#include "tcs_int_literals.h"
23#include "capabilities.h"
24#include "tcslog.h"
25#include "req_mgr.h"
26#include "tcsd_wrap.h"
27#include "tcsd.h"
28
29TSS_RESULT
30TCSP_NV_DefineOrReleaseSpace_Internal(TCS_CONTEXT_HANDLE hContext, /* in */
31				      UINT32 cPubInfoSize,	/* in */
32				      BYTE* pPubInfo,	/* in */
33				      TPM_ENCAUTH encAuth,	/* in */
34				      TPM_AUTH* pAuth)	/* in, out */
35{
36	UINT64 offset = 0;
37	UINT32 paramSize;
38	TSS_RESULT result;
39	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
40
41	LogDebugFn("Enter");
42	if ((result = ctx_verify_context(hContext)))
43		return result;
44
45	if (pAuth) {
46		if ((result = auth_mgr_check(hContext, &pAuth->AuthHandle)))
47			goto done;
48	}
49
50	if ((result = tpm_rqu_build(TPM_ORD_NV_DefineSpace, &offset, txBlob, cPubInfoSize, pPubInfo,
51				    TPM_ENCAUTH_SIZE, encAuth.authdata, pAuth)))
52		return result;
53
54	LogDebug("req_mgr_submit_req  (oldOffset=%" PRIu64 ")", offset);
55	if ((result = req_mgr_submit_req(txBlob)))
56		goto done;
57
58	result = UnloadBlob_Header(txBlob, &paramSize);
59	LogDebug("UnloadBlob  (paramSize=%u) result=%u", paramSize, result);
60	if (!result) {
61		result = tpm_rsp_parse(TPM_ORD_NV_DefineSpace, txBlob, paramSize, pAuth);
62	}
63done:
64	LogDebug("Leaving DefineSpace with result:%u", result);
65	auth_mgr_release_auth(pAuth, NULL, hContext);
66	return result;
67}
68
69TSS_RESULT
70TCSP_NV_WriteValue_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
71			    TSS_NV_INDEX hNVStore,	/* in */
72			    UINT32 offset,		/* in */
73			    UINT32 ulDataLength,	/* in */
74			    BYTE * rgbDataToWrite,	/* in */
75			    TPM_AUTH * privAuth)	/* in, out */
76{
77	UINT64 off_set = 0;
78	UINT32 paramSize;
79	TSS_RESULT result;
80	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
81
82	LogDebugFn("Enter");
83	if ( (result = ctx_verify_context(hContext)))
84		return result;
85	if (privAuth) {
86		if ((result = auth_mgr_check(hContext, &privAuth->AuthHandle)))
87			goto done;
88	}
89
90	if ((result = tpm_rqu_build(TPM_ORD_NV_WriteValue, &off_set, txBlob, hNVStore, offset,
91				    ulDataLength, rgbDataToWrite, privAuth)))
92		return result;
93
94	LogDebug("req_mgr_submit_req  (oldOffset=%" PRIu64 ")", off_set);
95	if ((result = req_mgr_submit_req(txBlob)))
96		goto done;
97
98	result = UnloadBlob_Header(txBlob, &paramSize);
99	LogDebug("UnloadBlob  (paramSize=%u) result=%u", paramSize, result);
100	if (!result) {
101		result = tpm_rsp_parse(TPM_ORD_NV_WriteValue, txBlob, paramSize, privAuth);
102	}
103done:
104	LogDebug("Leaving NVWriteValue with result:%u", result);
105	auth_mgr_release_auth(privAuth, NULL, hContext);
106	return result;
107}
108
109TSS_RESULT
110TCSP_NV_WriteValueAuth_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
111				TSS_NV_INDEX hNVStore,	/* in */
112				UINT32 offset,		/* in */
113				UINT32 ulDataLength,	/* in */
114				BYTE * rgbDataToWrite,	/* in */
115				TPM_AUTH * NVAuth)	/* in, out */
116{
117	UINT64 off_set = 0;
118	UINT32 paramSize;
119	TSS_RESULT result;
120	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
121
122	LogDebugFn("Enter");
123	if ((result = ctx_verify_context(hContext)))
124		return result;
125	if ((result = auth_mgr_check(hContext, &NVAuth->AuthHandle)))
126		goto done;
127
128	if ((result = tpm_rqu_build(TPM_ORD_NV_WriteValueAuth, &off_set, txBlob, hNVStore, offset,
129				    ulDataLength, rgbDataToWrite, NVAuth)))
130		return result;
131
132	LogDebug("req_mgr_submit_req  (oldOffset=%" PRIu64 ")", off_set);
133	if ((result = req_mgr_submit_req(txBlob)))
134		goto done;
135
136	result = UnloadBlob_Header(txBlob, &paramSize);
137	LogDebug("UnloadBlob  (paramSize=%u) result=%u", paramSize, result);
138	if (!result) {
139		result = tpm_rsp_parse(TPM_ORD_NV_WriteValueAuth, txBlob, paramSize, NVAuth);
140	}
141done:
142	LogDebug("Leaving NVWriteValueAuth with result:%u", result);
143	auth_mgr_release_auth(NVAuth, NULL, hContext);
144	return result;
145}
146
147TSS_RESULT
148TCSP_NV_ReadValue_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
149			   TSS_NV_INDEX hNVStore,	/* in */
150			   UINT32 offset,	/* in */
151			   UINT32 * pulDataLength,	/* in, out */
152			   TPM_AUTH * privAuth,	/* in, out */
153			   BYTE ** rgbDataRead)	/* out */
154{
155	UINT64 off_set = 0;
156	UINT32 paramSize;
157	TSS_RESULT result;
158	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
159
160	LogDebugFn("Enter");
161	if ((result = ctx_verify_context(hContext)))
162		return result;
163
164	if (privAuth) {
165		if ((result = auth_mgr_check(hContext, &privAuth->AuthHandle)))
166			goto done;
167	}
168
169	if ((result = tpm_rqu_build(TPM_ORD_NV_ReadValue, &off_set, txBlob, hNVStore, offset,
170				    *pulDataLength, privAuth)))
171		return result;
172
173	LogDebug("req_mgr_submit_req  (oldOffset=%" PRIu64 ")", off_set);
174	if ((result = req_mgr_submit_req(txBlob)))
175		goto done;
176
177	result = UnloadBlob_Header(txBlob, &paramSize);
178	LogDebug("UnloadBlob  (paramSize=%u) result=%u", paramSize, result);
179	if (!result) {
180		result = tpm_rsp_parse(TPM_ORD_NV_ReadValue, txBlob, paramSize, pulDataLength,
181				       rgbDataRead, privAuth, NULL);
182	}
183done:
184	LogDebug("Leaving NVReadValue with result:%u", result);
185	auth_mgr_release_auth(privAuth, NULL, hContext);
186	return result;
187}
188
189TSS_RESULT
190TCSP_NV_ReadValueAuth_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
191			       TSS_NV_INDEX hNVStore,	/* in */
192			       UINT32 offset,		/* in */
193			       UINT32 * pulDataLength,	/* in, out */
194			       TPM_AUTH * NVAuth,	/* in, out */
195			       BYTE ** rgbDataRead)	/* out */
196{
197	UINT64 off_set = 0;
198	UINT32 paramSize;
199	TSS_RESULT result;
200	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
201
202	LogDebugFn("Enter");
203	if ((result = ctx_verify_context(hContext)))
204		return result;
205	if ((NVAuth != NULL) && (result = auth_mgr_check(hContext, &NVAuth->AuthHandle)))
206		goto done;
207
208	if ((result = tpm_rqu_build(TPM_ORD_NV_ReadValueAuth, &off_set, txBlob, hNVStore, offset,
209				    *pulDataLength, NVAuth)))
210		return result;
211
212	LogDebug("req_mgr_submit_req  (oldOffset=%" PRIu64 ")", off_set);
213	if ((result = req_mgr_submit_req(txBlob)))
214		goto done;
215
216	result = UnloadBlob_Header(txBlob, &paramSize);
217	LogDebug("UnloadBlob  (paramSize=%u) result=%u", paramSize, result);
218	if (!result) {
219		result = tpm_rsp_parse(TPM_ORD_NV_ReadValueAuth, txBlob, paramSize, pulDataLength,
220				       rgbDataRead, NVAuth, NULL);
221	}
222done:
223	LogDebug("Leaving NVReadValueAuth with result:%u", result);
224	auth_mgr_release_auth(NVAuth, NULL, hContext);
225	return result;
226}
227
228