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