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