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