1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*******************************************************************************
3 * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
4 * All rights reserved.
5 ******************************************************************************/
6
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include "tss2_mu.h"
12 #include "tss2_sys.h"
13 #include "tss2_esys.h"
14
15 #include "esys_types.h"
16 #include "esys_iutil.h"
17 #include "esys_mu.h"
18 #define LOGMODULE esys
19 #include "util/log.h"
20 #include "util/aux_util.h"
21
22 /** One-Call function for TPM2_NV_Read
23 *
24 * This function invokes the TPM2_NV_Read command in a one-call
25 * variant. This means the function will block until the TPM response is
26 * available. All input parameters are const. The memory for non-simple output
27 * parameters is allocated by the function implementation.
28 *
29 * @param[in,out] esysContext The ESYS_CONTEXT.
30 * @param[in] authHandle The handle indicating the source of the authorization
31 * value.
32 * @param[in] nvIndex The NV Index to be read.
33 * @param[in] shandle1 Session handle for authorization of authHandle
34 * @param[in] shandle2 Second session handle.
35 * @param[in] shandle3 Third session handle.
36 * @param[in] size Number of octets to read.
37 * @param[in] offset Octet offset into the area.
38 * @param[out] data The data read.
39 * (callee-allocated)
40 * @retval TSS2_RC_SUCCESS if the function call was a success.
41 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
42 * pointers or required output handle references are NULL.
43 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
44 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
45 * internal operations or return parameters.
46 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
47 * operation already pending.
48 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
49 * at least contain the tag, response length, and response code.
50 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
51 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
52 did not verify.
53 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
54 * the 'decrypt' attribute bit set.
55 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
56 * the 'encrypt' attribute bit set.
57 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
58 * to the ESYS_CONTEXT or are of the wrong type or if required
59 * ESYS_TR objects are ESYS_TR_NONE.
60 * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
61 * 'decrypt' attribute set and the command does not support encryption
62 * of the first command parameter.
63 * @retval TSS2_RCs produced by lower layers of the software stack may be
64 * returned to the caller unaltered unless handled internally.
65 */
66 TSS2_RC
Esys_NV_Read(ESYS_CONTEXT * esysContext,ESYS_TR authHandle,ESYS_TR nvIndex,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,UINT16 size,UINT16 offset,TPM2B_MAX_NV_BUFFER ** data)67 Esys_NV_Read(
68 ESYS_CONTEXT *esysContext,
69 ESYS_TR authHandle,
70 ESYS_TR nvIndex,
71 ESYS_TR shandle1,
72 ESYS_TR shandle2,
73 ESYS_TR shandle3,
74 UINT16 size,
75 UINT16 offset,
76 TPM2B_MAX_NV_BUFFER **data)
77 {
78 TSS2_RC r;
79
80 r = Esys_NV_Read_Async(esysContext, authHandle, nvIndex, shandle1, shandle2,
81 shandle3, size, offset);
82 return_if_error(r, "Error in async function");
83
84 /* Set the timeout to indefinite for now, since we want _Finish to block */
85 int32_t timeouttmp = esysContext->timeout;
86 esysContext->timeout = -1;
87 /*
88 * Now we call the finish function, until return code is not equal to
89 * from TSS2_BASE_RC_TRY_AGAIN.
90 * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
91 * have set the timeout to -1. This occurs for example if the TPM requests
92 * a retransmission of the command via TPM2_RC_YIELDED.
93 */
94 do {
95 r = Esys_NV_Read_Finish(esysContext, data);
96 /* This is just debug information about the reattempt to finish the
97 command */
98 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
99 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
100 " => resubmitting command", r);
101 } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
102
103 /* Restore the timeout value to the original value */
104 esysContext->timeout = timeouttmp;
105 return_if_error(r, "Esys Finish");
106
107 return TSS2_RC_SUCCESS;
108 }
109
110 /** Asynchronous function for TPM2_NV_Read
111 *
112 * This function invokes the TPM2_NV_Read command in a asynchronous
113 * variant. This means the function will return as soon as the command has been
114 * sent downwards the stack to the TPM. All input parameters are const.
115 * In order to retrieve the TPM's response call Esys_NV_Read_Finish.
116 *
117 * @param[in,out] esysContext The ESYS_CONTEXT.
118 * @param[in] authHandle The handle indicating the source of the authorization
119 * value.
120 * @param[in] nvIndex The NV Index to be read.
121 * @param[in] shandle1 Session handle for authorization of authHandle
122 * @param[in] shandle2 Second session handle.
123 * @param[in] shandle3 Third session handle.
124 * @param[in] size Number of octets to read.
125 * @param[in] offset Octet offset into the area.
126 * @retval ESYS_RC_SUCCESS if the function call was a success.
127 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
128 * pointers or required output handle references are NULL.
129 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
130 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
131 * internal operations or return parameters.
132 * @retval TSS2_RCs produced by lower layers of the software stack may be
133 returned to the caller unaltered unless handled internally.
134 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
135 * the 'decrypt' attribute bit set.
136 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
137 * the 'encrypt' attribute bit set.
138 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
139 * to the ESYS_CONTEXT or are of the wrong type or if required
140 * ESYS_TR objects are ESYS_TR_NONE.
141 * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
142 * 'decrypt' attribute set and the command does not support encryption
143 * of the first command parameter.
144 */
145 TSS2_RC
Esys_NV_Read_Async(ESYS_CONTEXT * esysContext,ESYS_TR authHandle,ESYS_TR nvIndex,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,UINT16 size,UINT16 offset)146 Esys_NV_Read_Async(
147 ESYS_CONTEXT *esysContext,
148 ESYS_TR authHandle,
149 ESYS_TR nvIndex,
150 ESYS_TR shandle1,
151 ESYS_TR shandle2,
152 ESYS_TR shandle3,
153 UINT16 size,
154 UINT16 offset)
155 {
156 TSS2_RC r;
157 LOG_TRACE("context=%p, authHandle=%"PRIx32 ", nvIndex=%"PRIx32 ","
158 "size=%04"PRIx16", offset=%04"PRIx16"",
159 esysContext, authHandle, nvIndex, size, offset);
160 TSS2L_SYS_AUTH_COMMAND auths;
161 RSRC_NODE_T *authHandleNode;
162 RSRC_NODE_T *nvIndexNode;
163
164 /* Check context, sequence correctness and set state to error for now */
165 if (esysContext == NULL) {
166 LOG_ERROR("esyscontext is NULL.");
167 return TSS2_ESYS_RC_BAD_REFERENCE;
168 }
169 r = iesys_check_sequence_async(esysContext);
170 if (r != TSS2_RC_SUCCESS)
171 return r;
172 esysContext->state = _ESYS_STATE_INTERNALERROR;
173
174 /* Check input parameters */
175 r = check_session_feasibility(shandle1, shandle2, shandle3, 1);
176 return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
177
178 /* Retrieve the metadata objects for provided handles */
179 r = esys_GetResourceObject(esysContext, authHandle, &authHandleNode);
180 return_state_if_error(r, _ESYS_STATE_INIT, "authHandle unknown.");
181 r = esys_GetResourceObject(esysContext, nvIndex, &nvIndexNode);
182 return_state_if_error(r, _ESYS_STATE_INIT, "nvIndex unknown.");
183
184 /* Initial invocation of SAPI to prepare the command buffer with parameters */
185 r = Tss2_Sys_NV_Read_Prepare(esysContext->sys,
186 (authHandleNode == NULL) ? TPM2_RH_NULL
187 : authHandleNode->rsrc.handle,
188 (nvIndexNode == NULL) ? TPM2_RH_NULL
189 : nvIndexNode->rsrc.handle, size, offset);
190 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
191
192 /* Calculate the cpHash Values */
193 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
194 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
195 if (authHandleNode != NULL)
196 iesys_compute_session_value(esysContext->session_tab[0],
197 &authHandleNode->rsrc.name, &authHandleNode->auth);
198 else
199 iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
200
201 iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
202 iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
203
204 /* Generate the auth values and set them in the SAPI command buffer */
205 r = iesys_gen_auths(esysContext, authHandleNode, nvIndexNode, NULL, &auths);
206 return_state_if_error(r, _ESYS_STATE_INIT,
207 "Error in computation of auth values");
208
209 esysContext->authsCount = auths.count;
210 if (auths.count > 0) {
211 r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
212 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
213 }
214
215 /* Trigger execution and finish the async invocation */
216 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
217 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
218 "Finish (Execute Async)");
219
220 esysContext->state = _ESYS_STATE_SENT;
221
222 return r;
223 }
224
225 /** Asynchronous finish function for TPM2_NV_Read
226 *
227 * This function returns the results of a TPM2_NV_Read command
228 * invoked via Esys_NV_Read_Finish. All non-simple output parameters
229 * are allocated by the function's implementation. NULL can be passed for every
230 * output parameter if the value is not required.
231 *
232 * @param[in,out] esysContext The ESYS_CONTEXT.
233 * @param[out] data The data read.
234 * (callee-allocated)
235 * @retval TSS2_RC_SUCCESS on success
236 * @retval ESYS_RC_SUCCESS if the function call was a success.
237 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
238 * pointers or required output handle references are NULL.
239 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
240 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
241 * internal operations or return parameters.
242 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
243 * operation already pending.
244 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
245 * TPM response is received.
246 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
247 * at least contain the tag, response length, and response code.
248 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
249 * not verify.
250 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
251 * @retval TSS2_RCs produced by lower layers of the software stack may be
252 * returned to the caller unaltered unless handled internally.
253 */
254 TSS2_RC
Esys_NV_Read_Finish(ESYS_CONTEXT * esysContext,TPM2B_MAX_NV_BUFFER ** data)255 Esys_NV_Read_Finish(
256 ESYS_CONTEXT *esysContext,
257 TPM2B_MAX_NV_BUFFER **data)
258 {
259 TSS2_RC r;
260 LOG_TRACE("context=%p, data=%p",
261 esysContext, data);
262
263 if (esysContext == NULL) {
264 LOG_ERROR("esyscontext is NULL.");
265 return TSS2_ESYS_RC_BAD_REFERENCE;
266 }
267
268 /* Check for correct sequence and set sequence to irregular for now */
269 if (esysContext->state != _ESYS_STATE_SENT &&
270 esysContext->state != _ESYS_STATE_RESUBMISSION) {
271 LOG_ERROR("Esys called in bad sequence.");
272 return TSS2_ESYS_RC_BAD_SEQUENCE;
273 }
274 esysContext->state = _ESYS_STATE_INTERNALERROR;
275
276 /* Allocate memory for response parameters */
277 if (data != NULL) {
278 *data = calloc(sizeof(TPM2B_MAX_NV_BUFFER), 1);
279 if (*data == NULL) {
280 return_error(TSS2_ESYS_RC_MEMORY, "Out of memory");
281 }
282 }
283
284 /*Receive the TPM response and handle resubmissions if necessary. */
285 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
286 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
287 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
288 esysContext->state = _ESYS_STATE_SENT;
289 goto error_cleanup;
290 }
291 /* This block handle the resubmission of TPM commands given a certain set of
292 * TPM response codes. */
293 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
294 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
295 "resubmission: %" PRIx32, r);
296 if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
297 LOG_WARNING("Maximum number of (re)submissions has been reached.");
298 esysContext->state = _ESYS_STATE_INIT;
299 goto error_cleanup;
300 }
301 esysContext->state = _ESYS_STATE_RESUBMISSION;
302 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
303 if (r != TSS2_RC_SUCCESS) {
304 LOG_WARNING("Error attempting to resubmit");
305 /* We do not set esysContext->state here but inherit the most recent
306 * state of the _async function. */
307 goto error_cleanup;
308 }
309 r = TSS2_ESYS_RC_TRY_AGAIN;
310 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
311 goto error_cleanup;
312 }
313 /* The following is the "regular error" handling. */
314 if (iesys_tpm_error(r)) {
315 LOG_WARNING("Received TPM Error");
316 esysContext->state = _ESYS_STATE_INIT;
317 goto error_cleanup;
318 } else if (r != TSS2_RC_SUCCESS) {
319 LOG_ERROR("Received a non-TPM Error");
320 esysContext->state = _ESYS_STATE_INTERNALERROR;
321 goto error_cleanup;
322 }
323
324 /*
325 * Now the verification of the response (hmac check) and if necessary the
326 * parameter decryption have to be done.
327 */
328 r = iesys_check_response(esysContext);
329 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
330 error_cleanup);
331
332 /*
333 * After the verification of the response we call the complete function
334 * to deliver the result.
335 */
336 r = Tss2_Sys_NV_Read_Complete(esysContext->sys,
337 (data != NULL) ? *data : NULL);
338 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
339 "Received error from SAPI unmarshaling" ,
340 error_cleanup);
341
342 esysContext->state = _ESYS_STATE_INIT;
343
344 return TSS2_RC_SUCCESS;
345
346 error_cleanup:
347 if (data != NULL)
348 SAFE_FREE(*data);
349
350 return r;
351 }
352