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