blob: 6077114bda48c07796a05f77edc718d8f60948be [file] [log] [blame]
Peter Huewed5a36f62018-06-12 00:59:26 +02001/* SPDX-License-Identifier: BSD-2 */
Juergen Reppff821bd2017-12-11 15:21:42 +01002/*******************************************************************************
3 * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
4 * All rights reserved.
Juergen Reppff821bd2017-12-11 15:21:42 +01005 ******************************************************************************/
6
Philip Tricca910f17c2018-03-15 12:38:37 -07007#include "tss2_mu.h"
8#include "tss2_sys.h"
Philip Tricca8ffd3c42018-03-09 16:27:24 -08009#include "tss2_esys.h"
Juergen Repped6e6e22018-03-19 17:34:32 +010010
11#include "esys_types.h"
Juergen Reppff821bd2017-12-11 15:21:42 +010012#include "esys_iutil.h"
13#include "esys_mu.h"
Juergen Reppff821bd2017-12-11 15:21:42 +010014#define LOGMODULE esys
Philip Triccaa7c51ce2018-03-10 18:28:25 -080015#include "util/log.h"
Juergen Reppff821bd2017-12-11 15:21:42 +010016
Juergen Reppadd438d2018-04-09 10:00:19 +020017/** Store command parameters inside the ESYS_CONTEXT for use during _Finish */
Juergen Reppff821bd2017-12-11 15:21:42 +010018static void store_input_parameters (
19 ESYS_CONTEXT *esysContext,
20 ESYS_TR policySession,
21 const TPM2B_OPERAND *operandB,
22 UINT16 offset,
23 TPM2_EO operation)
24{
25 esysContext->in.PolicyCounterTimer.policySession = policySession;
26 esysContext->in.PolicyCounterTimer.offset = offset;
27 esysContext->in.PolicyCounterTimer.operation = operation;
28 if (operandB == NULL) {
29 esysContext->in.PolicyCounterTimer.operandB = NULL;
30 } else {
31 esysContext->in.PolicyCounterTimer.operandBData = *operandB;
32 esysContext->in.PolicyCounterTimer.operandB =
33 &esysContext->in.PolicyCounterTimer.operandBData;
34 }
35}
36
37/** One-Call function for TPM2_PolicyCounterTimer
38 *
39 * This function invokes the TPM2_PolicyCounterTimer command in a one-call
40 * variant. This means the function will block until the TPM response is
41 * available. All input parameters are const. The memory for non-simple output
42 * parameters is allocated by the function implementation.
43 *
44 * @param[in,out] esysContext The ESYS_CONTEXT.
Juergen Reppf7e5bd32018-04-26 11:11:32 +020045 * @param[in] policySession Handle for the policy session being extended.
46 * @param[in] shandle1 First session handle.
47 * @param[in] shandle2 Second session handle.
48 * @param[in] shandle3 Third session handle.
49 * @param[in] operandB The second operand.
50 * @param[in] offset The offset in TPMS_TIME_INFO structure for the start of
51 * operand A.
52 * @param[in] operation The comparison to make.
Juergen Reppff821bd2017-12-11 15:21:42 +010053 * @retval TSS2_RC_SUCCESS on success
Juergen Reppf7e5bd32018-04-26 11:11:32 +020054 * @retval ESYS_RC_SUCCESS if the function call was a success.
55 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
56 * pointers or required output handle references are NULL.
57 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
58 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
59 * internal operations or return parameters.
60 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
61 * operation already pending.
62 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
63 * at least contain the tag, response length, and response code.
64 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
65 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
66 * the 'decrypt' attribute bit set.
67 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
68 * the 'encrypt' attribute bit set.
69 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown to the
70 * ESYS_CONTEXT or are of the wrong type or if required ESYS_TR objects
71 * are ESYS_TR_NONE.
72 * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
73 * 'encrypt' attribute set and the command does not support encryption
74 * of the first response parameter.
75 * @retval TSS2_RCs produced by lower layers of the software stack may be
76 * returned to the caller unaltered unless handled internally.
Juergen Reppff821bd2017-12-11 15:21:42 +010077 */
78TSS2_RC
79Esys_PolicyCounterTimer(
80 ESYS_CONTEXT *esysContext,
81 ESYS_TR policySession,
82 ESYS_TR shandle1,
83 ESYS_TR shandle2,
84 ESYS_TR shandle3,
85 const TPM2B_OPERAND *operandB,
86 UINT16 offset,
87 TPM2_EO operation)
88{
Juergen Repp3b7b41b2018-03-19 15:58:04 +010089 TSS2_RC r;
Juergen Reppff821bd2017-12-11 15:21:42 +010090
Juergen Reppadd438d2018-04-09 10:00:19 +020091 r = Esys_PolicyCounterTimer_Async(esysContext,
Juergen Reppff821bd2017-12-11 15:21:42 +010092 policySession,
93 shandle1,
94 shandle2,
95 shandle3,
96 operandB,
97 offset,
98 operation);
99 return_if_error(r, "Error in async function");
100
Juergen Reppadd438d2018-04-09 10:00:19 +0200101 /* Set the timeout to indefinite for now, since we want _Finish to block */
Juergen Reppff821bd2017-12-11 15:21:42 +0100102 int32_t timeouttmp = esysContext->timeout;
103 esysContext->timeout = -1;
104 /*
105 * Now we call the finish function, until return code is not equal to
106 * from TSS2_BASE_RC_TRY_AGAIN.
107 * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
108 * have set the timeout to -1. This occurs for example if the TPM requests
109 * a retransmission of the command via TPM2_RC_YIELDED.
110 */
111 do {
Juergen Reppadd438d2018-04-09 10:00:19 +0200112 r = Esys_PolicyCounterTimer_Finish(esysContext);
Juergen Reppff821bd2017-12-11 15:21:42 +0100113 /* This is just debug information about the reattempt to finish the
114 command */
115 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
116 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
117 " => resubmitting command", r);
118 } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
119
120 /* Restore the timeout value to the original value */
121 esysContext->timeout = timeouttmp;
122 return_if_error(r, "Esys Finish");
123
124 return TSS2_RC_SUCCESS;
125}
126
127/** Asynchronous function for TPM2_PolicyCounterTimer
128 *
129 * This function invokes the TPM2_PolicyCounterTimer command in a asynchronous
130 * variant. This means the function will return as soon as the command has been
131 * sent downwards the stack to the TPM. All input parameters are const.
Juergen Reppadd438d2018-04-09 10:00:19 +0200132 * In order to retrieve the TPM's response call Esys_PolicyCounterTimer_Finish.
Juergen Reppff821bd2017-12-11 15:21:42 +0100133 *
134 * @param[in,out] esysContext The ESYS_CONTEXT.
Juergen Reppf7e5bd32018-04-26 11:11:32 +0200135 * @param[in] policySession Handle for the policy session being extended.
136 * @param[in] shandle1 First session handle.
137 * @param[in] shandle2 Second session handle.
138 * @param[in] shandle3 Third session handle.
139 * @param[in] operandB The second operand.
140 * @param[in] offset The offset in TPMS_TIME_INFO structure for the start of
141 * operand A.
142 * @param[in] operation The comparison to make.
143 * @retval ESYS_RC_SUCCESS if the function call was a success.
144 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
145 * pointers or required output handle references are NULL.
146 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
147 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
148 * internal operations or return parameters.
149 * @retval TSS2_RCs produced by lower layers of the software stack may be
150 returned to the caller unaltered unless handled internally.
151 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
152 * the 'decrypt' attribute bit set.
153 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
154 * the 'encrypt' attribute bit set.
155 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown to the
156 ESYS_CONTEXT or are of the wrong type or if required ESYS_TR objects
157 are ESYS_TR_NONE.
158 * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
159 * 'encrypt' attribute set and the command does not support encryption
160 * of the first response parameter.
Juergen Reppff821bd2017-12-11 15:21:42 +0100161 */
162TSS2_RC
Juergen Reppadd438d2018-04-09 10:00:19 +0200163Esys_PolicyCounterTimer_Async(
Juergen Reppff821bd2017-12-11 15:21:42 +0100164 ESYS_CONTEXT *esysContext,
165 ESYS_TR policySession,
166 ESYS_TR shandle1,
167 ESYS_TR shandle2,
168 ESYS_TR shandle3,
169 const TPM2B_OPERAND *operandB,
170 UINT16 offset,
171 TPM2_EO operation)
172{
Juergen Repp3b7b41b2018-03-19 15:58:04 +0100173 TSS2_RC r;
Andreas Fuchs15bbb672018-03-27 13:21:13 +0200174 LOG_TRACE("context=%p, policySession=%"PRIx32 ", operandB=%p,"
175 "offset=%04"PRIx16", operation=%04"PRIx16"",
176 esysContext, policySession, operandB, offset, operation);
Andreas Fuchsc5a73eb2018-03-19 16:01:00 +0100177 TSS2L_SYS_AUTH_COMMAND auths;
Juergen Reppff821bd2017-12-11 15:21:42 +0100178 RSRC_NODE_T *policySessionNode;
179
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100180 /* Check context, sequence correctness and set state to error for now */
Juergen Reppff821bd2017-12-11 15:21:42 +0100181 if (esysContext == NULL) {
182 LOG_ERROR("esyscontext is NULL.");
183 return TSS2_ESYS_RC_BAD_REFERENCE;
184 }
185 r = iesys_check_sequence_async(esysContext);
186 if (r != TSS2_RC_SUCCESS)
187 return r;
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100188 esysContext->state = _ESYS_STATE_INTERNALERROR;
Juergen Reppff821bd2017-12-11 15:21:42 +0100189
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100190 /* Check and store input parameters */
dantpmf6ef2472018-04-06 15:21:59 -0700191 r = check_session_feasibility(shandle1, shandle2, shandle3, 0);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100192 return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
Juergen Reppff821bd2017-12-11 15:21:42 +0100193 store_input_parameters(esysContext, policySession,
194 operandB,
195 offset,
196 operation);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100197
198 /* Retrieve the metadata objects for provided handles */
Juergen Reppff821bd2017-12-11 15:21:42 +0100199 r = esys_GetResourceObject(esysContext, policySession, &policySessionNode);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100200 return_state_if_error(r, _ESYS_STATE_INIT, "policySession unknown.");
201
202 /* Initial invocation of SAPI to prepare the command buffer with parameters */
Juergen Reppff821bd2017-12-11 15:21:42 +0100203 r = Tss2_Sys_PolicyCounterTimer_Prepare(esysContext->sys,
204 (policySessionNode == NULL) ? TPM2_RH_NULL : policySessionNode->rsrc.handle,
205 operandB,
206 offset,
207 operation);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100208 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
Juergen Reppff821bd2017-12-11 15:21:42 +0100209
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100210 /* Calculate the cpHash Values */
211 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
212 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
Juergen Reppff821bd2017-12-11 15:21:42 +0100213 iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
214 iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
215 iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
Juergen Reppff821bd2017-12-11 15:21:42 +0100216
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100217 /* Generate the auth values and set them in the SAPI command buffer */
218 r = iesys_gen_auths(esysContext, policySessionNode, NULL, NULL, &auths);
219 return_state_if_error(r, _ESYS_STATE_INIT, "Error in computation of auth values");
Juergen Reppff821bd2017-12-11 15:21:42 +0100220 esysContext->authsCount = auths.count;
221 r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100222 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
Juergen Reppff821bd2017-12-11 15:21:42 +0100223
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100224 /* Trigger execution and finish the async invocation */
Juergen Reppff821bd2017-12-11 15:21:42 +0100225 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100226 return_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Finish (Execute Async)");
Juergen Reppff821bd2017-12-11 15:21:42 +0100227
228 esysContext->state = _ESYS_STATE_SENT;
229
230 return r;
231}
232
233/** Asynchronous finish function for TPM2_PolicyCounterTimer
234 *
235 * This function returns the results of a TPM2_PolicyCounterTimer command
Juergen Reppadd438d2018-04-09 10:00:19 +0200236 * invoked via Esys_PolicyCounterTimer_Finish. All non-simple output parameters
Juergen Reppff821bd2017-12-11 15:21:42 +0100237 * are allocated by the function's implementation. NULL can be passed for every
238 * output parameter if the value is not required.
239 *
240 * @param[in,out] esysContext The ESYS_CONTEXT.
241 * @retval TSS2_RC_SUCCESS on success
Juergen Reppf7e5bd32018-04-26 11:11:32 +0200242 * @retval ESYS_RC_SUCCESS if the function call was a success.
243 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
244 * pointers or required output handle references are NULL.
245 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
246 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
247 * internal operations or return parameters.
248 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
249 * operation already pending.
250 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
251 * TPM response is received.
252 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
253 * at least contain the tag, response length, and response code.
254 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
255 * @retval TSS2_RCs produced by lower layers of the software stack may be
256 * returned to the caller unaltered unless handled internally.
Juergen Reppff821bd2017-12-11 15:21:42 +0100257 */
258TSS2_RC
Juergen Reppadd438d2018-04-09 10:00:19 +0200259Esys_PolicyCounterTimer_Finish(
Juergen Reppff821bd2017-12-11 15:21:42 +0100260 ESYS_CONTEXT *esysContext)
261{
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100262 TSS2_RC r;
Andreas Fuchs15bbb672018-03-27 13:21:13 +0200263 LOG_TRACE("context=%p",
264 esysContext);
265
Juergen Reppff821bd2017-12-11 15:21:42 +0100266 if (esysContext == NULL) {
267 LOG_ERROR("esyscontext is NULL.");
268 return TSS2_ESYS_RC_BAD_REFERENCE;
269 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100270
271 /* Check for correct sequence and set sequence to irregular for now */
Juergen Reppff821bd2017-12-11 15:21:42 +0100272 if (esysContext->state != _ESYS_STATE_SENT) {
273 LOG_ERROR("Esys called in bad sequence.");
274 return TSS2_ESYS_RC_BAD_SEQUENCE;
275 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100276 esysContext->state = _ESYS_STATE_INTERNALERROR;
277
278 /*Receive the TPM response and handle resubmissions if necessary. */
Juergen Reppff821bd2017-12-11 15:21:42 +0100279 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
280 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
281 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100282 esysContext->state = _ESYS_STATE_SENT;
Juergen Reppff821bd2017-12-11 15:21:42 +0100283 return r;
284 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100285 /* This block handle the resubmission of TPM commands given a certain set of
286 * TPM response codes. */
Juergen Reppff821bd2017-12-11 15:21:42 +0100287 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
288 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
289 "resubmission: %" PRIx32, r);
Andreas Fuchs631d7e62018-03-19 10:54:56 +0100290 if (esysContext->submissionCount >= _ESYS_MAX_SUBMISSIONS) {
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100291 LOG_WARNING("Maximum number of (re)submissions has been reached.");
292 esysContext->state = _ESYS_STATE_INIT;
Juergen Reppff821bd2017-12-11 15:21:42 +0100293 return r;
294 }
295 esysContext->state = _ESYS_STATE_RESUBMISSION;
Juergen Reppadd438d2018-04-09 10:00:19 +0200296 r = Esys_PolicyCounterTimer_Async(esysContext,
Juergen Reppff821bd2017-12-11 15:21:42 +0100297 esysContext->in.PolicyCounterTimer.policySession,
298 esysContext->session_type[0],
299 esysContext->session_type[1],
300 esysContext->session_type[2],
301 esysContext->in.PolicyCounterTimer.operandB,
302 esysContext->in.PolicyCounterTimer.offset,
303 esysContext->in.PolicyCounterTimer.operation);
304 if (r != TSS2_RC_SUCCESS) {
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100305 LOG_WARNING("Error attempting to resubmit");
306 /* We do not set esysContext->state here but inherit the most recent
307 * state of the _async function. */
Juergen Reppff821bd2017-12-11 15:21:42 +0100308 return r;
309 }
310 r = TSS2_ESYS_RC_TRY_AGAIN;
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100311 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
Juergen Reppff821bd2017-12-11 15:21:42 +0100312 return r;
313 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100314 /* The following is the "regular error" handling. */
Juergen Repp033da112018-07-10 13:06:18 +0200315 if (iesys_tpm_error(r)) {
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100316 LOG_WARNING("Received TPM Error");
317 esysContext->state = _ESYS_STATE_INIT;
318 return r;
319 } else if (r != TSS2_RC_SUCCESS) {
320 LOG_ERROR("Received a non-TPM Error");
321 esysContext->state = _ESYS_STATE_INTERNALERROR;
Juergen Reppff821bd2017-12-11 15:21:42 +0100322 return r;
323 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100324
Juergen Reppff821bd2017-12-11 15:21:42 +0100325 /*
326 * Now the verification of the response (hmac check) and if necessary the
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100327 * parameter decryption have to be done.
Juergen Reppff821bd2017-12-11 15:21:42 +0100328 */
Juergen Repp109de7d2018-02-23 13:42:08 +0100329 r = iesys_check_response(esysContext);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100330 return_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response");
Juergen Reppff821bd2017-12-11 15:21:42 +0100331 /*
332 * After the verification of the response we call the complete function
333 * to deliver the result.
334 */
335 r = Tss2_Sys_PolicyCounterTimer_Complete(esysContext->sys);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100336 return_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Received error from SAPI"
dantpmf6ef2472018-04-06 15:21:59 -0700337 " unmarshaling" );
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100338 esysContext->state = _ESYS_STATE_INIT;
Juergen Reppff821bd2017-12-11 15:21:42 +0100339
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100340 return TSS2_RC_SUCCESS;
Philip Tricca910f17c2018-03-15 12:38:37 -0700341}