• 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_Commit
23  *
24  * This function invokes the TPM2_Commit 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]  signHandle Handle of the key that will be used in the signing
31  *             operation.
32  * @param[in]  shandle1 Session handle for authorization of signHandle
33  * @param[in]  shandle2 Second session handle.
34  * @param[in]  shandle3 Third session handle.
35  * @param[in]  P1 A point (M) on the curve used by signHandle .
36  * @param[in]  s2 Octet array used to derive x-coordinate of a base point.
37  * @param[in]  y2 Y coordinate of the point associated with s2.
38  * @param[out] K ECC point K := [ds](x2, y2).
39  *             (callee-allocated)
40  * @param[out] L ECC point L := [r](x2, y2).
41  *             (callee-allocated)
42  * @param[out] E ECC point E := [r]P1.
43  *             (callee-allocated)
44  * @param[out] counter Least-significant 16 bits of commitCount.
45  *             (callee-allocated)
46  * @retval TSS2_RC_SUCCESS if the function call was a success.
47  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
48  *         pointers or required output handle references are NULL.
49  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
50  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
51  *         internal operations or return parameters.
52  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
53  *         operation already pending.
54  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
55  *          at least contain the tag, response length, and response code.
56  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
57  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
58            did not verify.
59  * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
60  *         the 'decrypt' attribute bit set.
61  * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
62  *         the 'encrypt' attribute bit set.
63  * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
64  *         to the ESYS_CONTEXT or are of the wrong type or if required
65  *         ESYS_TR objects are ESYS_TR_NONE.
66  * @retval TSS2_RCs produced by lower layers of the software stack may be
67  *         returned to the caller unaltered unless handled internally.
68  */
69 TSS2_RC
Esys_Commit(ESYS_CONTEXT * esysContext,ESYS_TR signHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_ECC_POINT * P1,const TPM2B_SENSITIVE_DATA * s2,const TPM2B_ECC_PARAMETER * y2,TPM2B_ECC_POINT ** K,TPM2B_ECC_POINT ** L,TPM2B_ECC_POINT ** E,UINT16 * counter)70 Esys_Commit(
71     ESYS_CONTEXT *esysContext,
72     ESYS_TR signHandle,
73     ESYS_TR shandle1,
74     ESYS_TR shandle2,
75     ESYS_TR shandle3,
76     const TPM2B_ECC_POINT *P1,
77     const TPM2B_SENSITIVE_DATA *s2,
78     const TPM2B_ECC_PARAMETER *y2,
79     TPM2B_ECC_POINT **K,
80     TPM2B_ECC_POINT **L,
81     TPM2B_ECC_POINT **E,
82     UINT16 *counter)
83 {
84     TSS2_RC r;
85 
86     r = Esys_Commit_Async(esysContext, signHandle, shandle1, shandle2, shandle3,
87                           P1, s2, y2);
88     return_if_error(r, "Error in async function");
89 
90     /* Set the timeout to indefinite for now, since we want _Finish to block */
91     int32_t timeouttmp = esysContext->timeout;
92     esysContext->timeout = -1;
93     /*
94      * Now we call the finish function, until return code is not equal to
95      * from TSS2_BASE_RC_TRY_AGAIN.
96      * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
97      * have set the timeout to -1. This occurs for example if the TPM requests
98      * a retransmission of the command via TPM2_RC_YIELDED.
99      */
100     do {
101         r = Esys_Commit_Finish(esysContext, K, L, E, counter);
102         /* This is just debug information about the reattempt to finish the
103            command */
104         if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
105             LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
106                       " => resubmitting command", r);
107     } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
108 
109     /* Restore the timeout value to the original value */
110     esysContext->timeout = timeouttmp;
111     return_if_error(r, "Esys Finish");
112 
113     return TSS2_RC_SUCCESS;
114 }
115 
116 /** Asynchronous function for TPM2_Commit
117  *
118  * This function invokes the TPM2_Commit command in a asynchronous
119  * variant. This means the function will return as soon as the command has been
120  * sent downwards the stack to the TPM. All input parameters are const.
121  * In order to retrieve the TPM's response call Esys_Commit_Finish.
122  *
123  * @param[in,out] esysContext The ESYS_CONTEXT.
124  * @param[in]  signHandle Handle of the key that will be used in the signing
125  *             operation.
126  * @param[in]  shandle1 Session handle for authorization of signHandle
127  * @param[in]  shandle2 Second session handle.
128  * @param[in]  shandle3 Third session handle.
129  * @param[in]  P1 A point (M) on the curve used by signHandle .
130  * @param[in]  s2 Octet array used to derive x-coordinate of a base point.
131  * @param[in]  y2 Y coordinate of the point associated with s2.
132  * @retval ESYS_RC_SUCCESS if the function call was a success.
133  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
134  *         pointers or required output handle references are NULL.
135  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
136  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
137  *         internal operations or return parameters.
138  * @retval TSS2_RCs produced by lower layers of the software stack may be
139            returned to the caller unaltered unless handled internally.
140  * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
141  *         the 'decrypt' attribute bit set.
142  * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
143  *         the 'encrypt' attribute bit set.
144  * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
145  *         to the ESYS_CONTEXT or are of the wrong type or if required
146  *         ESYS_TR objects are ESYS_TR_NONE.
147  */
148 TSS2_RC
Esys_Commit_Async(ESYS_CONTEXT * esysContext,ESYS_TR signHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_ECC_POINT * P1,const TPM2B_SENSITIVE_DATA * s2,const TPM2B_ECC_PARAMETER * y2)149 Esys_Commit_Async(
150     ESYS_CONTEXT *esysContext,
151     ESYS_TR signHandle,
152     ESYS_TR shandle1,
153     ESYS_TR shandle2,
154     ESYS_TR shandle3,
155     const TPM2B_ECC_POINT *P1,
156     const TPM2B_SENSITIVE_DATA *s2,
157     const TPM2B_ECC_PARAMETER *y2)
158 {
159     TSS2_RC r;
160     LOG_TRACE("context=%p, signHandle=%"PRIx32 ", P1=%p,"
161               "s2=%p, y2=%p",
162               esysContext, signHandle, P1, s2, y2);
163     TSS2L_SYS_AUTH_COMMAND auths;
164     RSRC_NODE_T *signHandleNode;
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 
180     /* Retrieve the metadata objects for provided handles */
181     r = esys_GetResourceObject(esysContext, signHandle, &signHandleNode);
182     return_state_if_error(r, _ESYS_STATE_INIT, "signHandle unknown.");
183 
184     /* Initial invocation of SAPI to prepare the command buffer with parameters */
185     r = Tss2_Sys_Commit_Prepare(esysContext->sys,
186                                 (signHandleNode == NULL) ? TPM2_RH_NULL
187                                  : signHandleNode->rsrc.handle, P1, s2, y2);
188     return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
189 
190     /* Calculate the cpHash Values */
191     r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
192     return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
193     if (signHandleNode != NULL)
194         iesys_compute_session_value(esysContext->session_tab[0],
195                 &signHandleNode->rsrc.name, &signHandleNode->auth);
196     else
197         iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
198 
199     iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
200     iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
201 
202     /* Generate the auth values and set them in the SAPI command buffer */
203     r = iesys_gen_auths(esysContext, signHandleNode, NULL, NULL, &auths);
204     return_state_if_error(r, _ESYS_STATE_INIT,
205                           "Error in computation of auth values");
206 
207     esysContext->authsCount = auths.count;
208     if (auths.count > 0) {
209         r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
210         return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
211     }
212 
213     /* Trigger execution and finish the async invocation */
214     r = Tss2_Sys_ExecuteAsync(esysContext->sys);
215     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
216                           "Finish (Execute Async)");
217 
218     esysContext->state = _ESYS_STATE_SENT;
219 
220     return r;
221 }
222 
223 /** Asynchronous finish function for TPM2_Commit
224  *
225  * This function returns the results of a TPM2_Commit command
226  * invoked via Esys_Commit_Finish. All non-simple output parameters
227  * are allocated by the function's implementation. NULL can be passed for every
228  * output parameter if the value is not required.
229  *
230  * @param[in,out] esysContext The ESYS_CONTEXT.
231  * @param[out] K ECC point K := [ds](x2, y2).
232  *             (callee-allocated)
233  * @param[out] L ECC point L := [r](x2, y2).
234  *             (callee-allocated)
235  * @param[out] E ECC point E := [r]P1.
236  *             (callee-allocated)
237  * @param[out] counter Least-significant 16 bits of commitCount.
238  *             (callee-allocated)
239  * @retval TSS2_RC_SUCCESS on success
240  * @retval ESYS_RC_SUCCESS if the function call was a success.
241  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
242  *         pointers or required output handle references are NULL.
243  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
244  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
245  *         internal operations or return parameters.
246  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
247  *         operation already pending.
248  * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
249  *         TPM response is received.
250  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
251  *         at least contain the tag, response length, and response code.
252  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
253  *         not verify.
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.
257  */
258 TSS2_RC
Esys_Commit_Finish(ESYS_CONTEXT * esysContext,TPM2B_ECC_POINT ** K,TPM2B_ECC_POINT ** L,TPM2B_ECC_POINT ** E,UINT16 * counter)259 Esys_Commit_Finish(
260     ESYS_CONTEXT *esysContext,
261     TPM2B_ECC_POINT **K,
262     TPM2B_ECC_POINT **L,
263     TPM2B_ECC_POINT **E,
264     UINT16 *counter)
265 {
266     TSS2_RC r;
267     LOG_TRACE("context=%p, K=%p, L=%p,"
268               "E=%p, counter=%p",
269               esysContext, K, L,
270               E, counter);
271 
272     if (esysContext == NULL) {
273         LOG_ERROR("esyscontext is NULL.");
274         return TSS2_ESYS_RC_BAD_REFERENCE;
275     }
276 
277     /* Check for correct sequence and set sequence to irregular for now */
278     if (esysContext->state != _ESYS_STATE_SENT &&
279         esysContext->state != _ESYS_STATE_RESUBMISSION) {
280         LOG_ERROR("Esys called in bad sequence.");
281         return TSS2_ESYS_RC_BAD_SEQUENCE;
282     }
283     esysContext->state = _ESYS_STATE_INTERNALERROR;
284 
285     /* Initialize parameter to avoid unitialized usage */
286     if (E != NULL)
287         *E = NULL;
288 
289     /* Allocate memory for response parameters */
290     if (K != NULL) {
291         *K = calloc(sizeof(TPM2B_ECC_POINT), 1);
292         if (*K == NULL) {
293             return_error(TSS2_ESYS_RC_MEMORY, "Out of memory");
294         }
295     }
296     if (L != NULL) {
297         *L = calloc(sizeof(TPM2B_ECC_POINT), 1);
298         if (*L == NULL) {
299             goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
300         }
301     }
302     if (E != NULL) {
303         *E = calloc(sizeof(TPM2B_ECC_POINT), 1);
304         if (*E == NULL) {
305             goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
306         }
307     }
308 
309     /*Receive the TPM response and handle resubmissions if necessary. */
310     r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
311     if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
312         LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
313         esysContext->state = _ESYS_STATE_SENT;
314         goto error_cleanup;
315     }
316     /* This block handle the resubmission of TPM commands given a certain set of
317      * TPM response codes. */
318     if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
319         LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
320             "resubmission: %" PRIx32, r);
321         if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
322             LOG_WARNING("Maximum number of (re)submissions has been reached.");
323             esysContext->state = _ESYS_STATE_INIT;
324             goto error_cleanup;
325         }
326         esysContext->state = _ESYS_STATE_RESUBMISSION;
327         r = Tss2_Sys_ExecuteAsync(esysContext->sys);
328         if (r != TSS2_RC_SUCCESS) {
329             LOG_WARNING("Error attempting to resubmit");
330             /* We do not set esysContext->state here but inherit the most recent
331              * state of the _async function. */
332             goto error_cleanup;
333         }
334         r = TSS2_ESYS_RC_TRY_AGAIN;
335         LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
336         goto error_cleanup;
337     }
338     /* The following is the "regular error" handling. */
339     if (iesys_tpm_error(r)) {
340         LOG_WARNING("Received TPM Error");
341         esysContext->state = _ESYS_STATE_INIT;
342         goto error_cleanup;
343     } else if (r != TSS2_RC_SUCCESS) {
344         LOG_ERROR("Received a non-TPM Error");
345         esysContext->state = _ESYS_STATE_INTERNALERROR;
346         goto error_cleanup;
347     }
348 
349     /*
350      * Now the verification of the response (hmac check) and if necessary the
351      * parameter decryption have to be done.
352      */
353     r = iesys_check_response(esysContext);
354     goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
355                         error_cleanup);
356 
357     /*
358      * After the verification of the response we call the complete function
359      * to deliver the result.
360      */
361     r = Tss2_Sys_Commit_Complete(esysContext->sys,
362                                  (K != NULL) ? *K : NULL,
363                                  (L != NULL) ? *L : NULL,
364                                  (E != NULL) ? *E : NULL, counter);
365     goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
366                         "Received error from SAPI unmarshaling" ,
367                         error_cleanup);
368 
369     esysContext->state = _ESYS_STATE_INIT;
370 
371     return TSS2_RC_SUCCESS;
372 
373 error_cleanup:
374     if (K != NULL)
375         SAFE_FREE(*K);
376     if (L != NULL)
377         SAFE_FREE(*L);
378     if (E != NULL)
379         SAFE_FREE(*E);
380 
381     return r;
382 }
383