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