• 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 /** Store command parameters inside the ESYS_CONTEXT for use during _Finish */
store_input_parameters(ESYS_CONTEXT * esysContext,ESYS_TR nvIndex)23 static void store_input_parameters (
24     ESYS_CONTEXT *esysContext,
25     ESYS_TR nvIndex)
26 {
27     esysContext->in.NV.nvIndex = nvIndex;
28 }
29 
30 /** One-Call function for TPM2_NV_Increment
31  *
32  * This function invokes the TPM2_NV_Increment command in a one-call
33  * variant. This means the function will block until the TPM response is
34  * available. All input parameters are const. The memory for non-simple output
35  * parameters is allocated by the function implementation.
36  *
37  * @param[in,out] esysContext The ESYS_CONTEXT.
38  * @param[in]  authHandle Handle indicating the source of the authorization
39  *             value.
40  * @param[in]  nvIndex The NV Index to increment.
41  * @param[in]  shandle1 Session handle for authorization of authHandle
42  * @param[in]  shandle2 Second session handle.
43  * @param[in]  shandle3 Third session handle.
44  * @retval TSS2_RC_SUCCESS if the function call was a success.
45  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
46  *         pointers or required output handle references are NULL.
47  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
48  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
49  *         internal operations or return parameters.
50  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
51  *         operation already pending.
52  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
53  *          at least contain the tag, response length, and response code.
54  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
55  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
56            did not verify.
57  * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
58  *         the 'decrypt' attribute bit set.
59  * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
60  *         the 'encrypt' attribute bit set.
61  * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
62  *         to the ESYS_CONTEXT or are of the wrong type or if required
63  *         ESYS_TR objects are ESYS_TR_NONE.
64  * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
65  *         'decrypt' attribute set and the command does not support encryption
66  *         of the first command parameter.
67  * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
68  *         'encrypt' attribute set and the command does not support encryption
69  *          of the first response parameter.
70  * @retval TSS2_RCs produced by lower layers of the software stack may be
71  *         returned to the caller unaltered unless handled internally.
72  */
73 TSS2_RC
Esys_NV_Increment(ESYS_CONTEXT * esysContext,ESYS_TR authHandle,ESYS_TR nvIndex,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3)74 Esys_NV_Increment(
75     ESYS_CONTEXT *esysContext,
76     ESYS_TR authHandle,
77     ESYS_TR nvIndex,
78     ESYS_TR shandle1,
79     ESYS_TR shandle2,
80     ESYS_TR shandle3)
81 {
82     TSS2_RC r;
83 
84     r = Esys_NV_Increment_Async(esysContext, authHandle, nvIndex, shandle1,
85                                 shandle2, shandle3);
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_NV_Increment_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_NV_Increment
115  *
116  * This function invokes the TPM2_NV_Increment 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_NV_Increment_Finish.
120  *
121  * @param[in,out] esysContext The ESYS_CONTEXT.
122  * @param[in]  authHandle Handle indicating the source of the authorization
123  *             value.
124  * @param[in]  nvIndex The NV Index to increment.
125  * @param[in]  shandle1 Session handle for authorization of authHandle
126  * @param[in]  shandle2 Second session handle.
127  * @param[in]  shandle3 Third session handle.
128  * @retval ESYS_RC_SUCCESS if the function call was a success.
129  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
130  *         pointers or required output handle references are NULL.
131  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
132  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
133  *         internal operations or return parameters.
134  * @retval TSS2_RCs produced by lower layers of the software stack may be
135            returned to the caller unaltered unless handled internally.
136  * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
137  *         the 'decrypt' attribute bit set.
138  * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
139  *         the 'encrypt' attribute bit set.
140  * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
141  *         to the ESYS_CONTEXT or are of the wrong type or if required
142  *         ESYS_TR objects are ESYS_TR_NONE.
143  * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
144  *         'decrypt' attribute set and the command does not support encryption
145  *         of the first command parameter.
146  * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
147  *         'encrypt' attribute set and the command does not support encryption
148  *          of the first response parameter.
149  */
150 TSS2_RC
Esys_NV_Increment_Async(ESYS_CONTEXT * esysContext,ESYS_TR authHandle,ESYS_TR nvIndex,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3)151 Esys_NV_Increment_Async(
152     ESYS_CONTEXT *esysContext,
153     ESYS_TR authHandle,
154     ESYS_TR nvIndex,
155     ESYS_TR shandle1,
156     ESYS_TR shandle2,
157     ESYS_TR shandle3)
158 {
159     TSS2_RC r;
160     LOG_TRACE("context=%p, authHandle=%"PRIx32 ", nvIndex=%"PRIx32 "",
161               esysContext, authHandle, nvIndex);
162     TSS2L_SYS_AUTH_COMMAND auths;
163     RSRC_NODE_T *authHandleNode;
164     RSRC_NODE_T *nvIndexNode;
165 
166     /* Check context, sequence correctness and set state to error for now */
167     if (esysContext == NULL) {
168         LOG_ERROR("esyscontext is NULL.");
169         return TSS2_ESYS_RC_BAD_REFERENCE;
170     }
171     r = iesys_check_sequence_async(esysContext);
172     if (r != TSS2_RC_SUCCESS)
173         return r;
174     esysContext->state = _ESYS_STATE_INTERNALERROR;
175 
176     /* Check input parameters */
177     r = check_session_feasibility(shandle1, shandle2, shandle3, 1);
178     return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
179     store_input_parameters(esysContext, nvIndex);
180 
181     /* Retrieve the metadata objects for provided handles */
182     r = esys_GetResourceObject(esysContext, authHandle, &authHandleNode);
183     return_state_if_error(r, _ESYS_STATE_INIT, "authHandle unknown.");
184     r = esys_GetResourceObject(esysContext, nvIndex, &nvIndexNode);
185     return_state_if_error(r, _ESYS_STATE_INIT, "nvIndex unknown.");
186 
187     /* Initial invocation of SAPI to prepare the command buffer with parameters */
188     r = Tss2_Sys_NV_Increment_Prepare(esysContext->sys,
189                                       (authHandleNode == NULL) ? TPM2_RH_NULL
190                                        : authHandleNode->rsrc.handle,
191                                       (nvIndexNode == NULL) ? TPM2_RH_NULL
192                                        : nvIndexNode->rsrc.handle);
193     return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
194 
195     /* Calculate the cpHash Values */
196     r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
197     return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
198     if (authHandleNode != NULL)
199         iesys_compute_session_value(esysContext->session_tab[0],
200                 &authHandleNode->rsrc.name, &authHandleNode->auth);
201     else
202         iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
203 
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, authHandleNode, nvIndexNode, 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_NV_Increment
229  *
230  * This function returns the results of a TPM2_NV_Increment command
231  * invoked via Esys_NV_Increment_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_NV_Increment_Finish(ESYS_CONTEXT * esysContext)256 Esys_NV_Increment_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_NV_Increment_Complete(esysContext->sys);
329     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
330                           "Received error from SAPI unmarshaling" );
331 
332 
333     ESYS_TR nvIndex = esysContext->in.NV.nvIndex;
334         RSRC_NODE_T *nvIndexNode;
335     r = esys_GetResourceObject(esysContext, nvIndex, &nvIndexNode);
336     return_if_error(r, "get resource");
337 
338     /* Update name in meta data because of possibly changed attributes */
339     if (nvIndexNode != NULL) {
340         nvIndexNode->rsrc.misc.rsrc_nv_pub.nvPublic.attributes |= TPMA_NV_WRITTEN;
341         r = iesys_nv_get_name(&nvIndexNode->rsrc.misc.rsrc_nv_pub,
342                               &nvIndexNode->rsrc.name);
343         return_if_error(r, "Error get nvname")
344     }
345     esysContext->state = _ESYS_STATE_INIT;
346 
347     return TSS2_RC_SUCCESS;
348 }
349