• 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_ECDH_KeyGen
23  *
24  * This function invokes the TPM2_ECDH_KeyGen 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]  keyHandle Handle of a loaded ECC key public area..
31  * @param[in]  shandle1 First session handle.
32  * @param[in]  shandle2 Second session handle.
33  * @param[in]  shandle3 Third session handle.
34  * @param[out] zPoint Results of P := h[de]Qs.
35  *             (callee-allocated)
36  * @param[out] pubPoint Generated ephemeral public point (Qe).
37  *             (callee-allocated)
38  * @retval TSS2_RC_SUCCESS if the function call was a success.
39  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
40  *         pointers or required output handle references are NULL.
41  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
42  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
43  *         internal operations or return parameters.
44  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
45  *         operation already pending.
46  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
47  *          at least contain the tag, response length, and response code.
48  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
49  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
50            did not verify.
51  * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
52  *         the 'decrypt' attribute bit set.
53  * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
54  *         the 'encrypt' attribute bit set.
55  * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
56  *         to the ESYS_CONTEXT or are of the wrong type or if required
57  *         ESYS_TR objects are ESYS_TR_NONE.
58  * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
59  *         'decrypt' attribute set and the command does not support encryption
60  *         of the first command parameter.
61  * @retval TSS2_RCs produced by lower layers of the software stack may be
62  *         returned to the caller unaltered unless handled internally.
63  */
64 TSS2_RC
Esys_ECDH_KeyGen(ESYS_CONTEXT * esysContext,ESYS_TR keyHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,TPM2B_ECC_POINT ** zPoint,TPM2B_ECC_POINT ** pubPoint)65 Esys_ECDH_KeyGen(
66     ESYS_CONTEXT *esysContext,
67     ESYS_TR keyHandle,
68     ESYS_TR shandle1,
69     ESYS_TR shandle2,
70     ESYS_TR shandle3,
71     TPM2B_ECC_POINT **zPoint,
72     TPM2B_ECC_POINT **pubPoint)
73 {
74     TSS2_RC r;
75 
76     r = Esys_ECDH_KeyGen_Async(esysContext, keyHandle, shandle1, shandle2,
77                                shandle3);
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_ECDH_KeyGen_Finish(esysContext, zPoint, pubPoint);
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_ECDH_KeyGen
107  *
108  * This function invokes the TPM2_ECDH_KeyGen 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_ECDH_KeyGen_Finish.
112  *
113  * @param[in,out] esysContext The ESYS_CONTEXT.
114  * @param[in]  keyHandle Handle of a loaded ECC key public area..
115  * @param[in]  shandle1 First session handle.
116  * @param[in]  shandle2 Second session handle.
117  * @param[in]  shandle3 Third session handle.
118  * @retval ESYS_RC_SUCCESS if the function call was a success.
119  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
120  *         pointers or required output handle references are NULL.
121  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
122  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
123  *         internal operations or return parameters.
124  * @retval TSS2_RCs produced by lower layers of the software stack may be
125            returned to the caller unaltered unless handled internally.
126  * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
127  *         the 'decrypt' attribute bit set.
128  * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
129  *         the 'encrypt' attribute bit set.
130  * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
131  *         to the ESYS_CONTEXT or are of the wrong type or if required
132  *         ESYS_TR objects are ESYS_TR_NONE.
133  * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
134  *         'decrypt' attribute set and the command does not support encryption
135  *         of the first command parameter.
136  */
137 TSS2_RC
Esys_ECDH_KeyGen_Async(ESYS_CONTEXT * esysContext,ESYS_TR keyHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3)138 Esys_ECDH_KeyGen_Async(
139     ESYS_CONTEXT *esysContext,
140     ESYS_TR keyHandle,
141     ESYS_TR shandle1,
142     ESYS_TR shandle2,
143     ESYS_TR shandle3)
144 {
145     TSS2_RC r;
146     LOG_TRACE("context=%p, keyHandle=%"PRIx32 "",
147               esysContext, keyHandle);
148     TSS2L_SYS_AUTH_COMMAND auths;
149     RSRC_NODE_T *keyHandleNode;
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, keyHandle, &keyHandleNode);
167     return_state_if_error(r, _ESYS_STATE_INIT, "keyHandle unknown.");
168 
169     /* Initial invocation of SAPI to prepare the command buffer with parameters */
170     r = Tss2_Sys_ECDH_KeyGen_Prepare(esysContext->sys,
171                                      (keyHandleNode == NULL) ? TPM2_RH_NULL
172                                       : keyHandleNode->rsrc.handle);
173     return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
174 
175     /* Calculate the cpHash Values */
176     r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
177     return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
178     iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
179     iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
180     iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
181 
182     /* Generate the auth values and set them in the SAPI command buffer */
183     r = iesys_gen_auths(esysContext, keyHandleNode, NULL, NULL, &auths);
184     return_state_if_error(r, _ESYS_STATE_INIT,
185                           "Error in computation of auth values");
186 
187     esysContext->authsCount = auths.count;
188     if (auths.count > 0) {
189         r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
190         return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
191     }
192 
193     /* Trigger execution and finish the async invocation */
194     r = Tss2_Sys_ExecuteAsync(esysContext->sys);
195     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
196                           "Finish (Execute Async)");
197 
198     esysContext->state = _ESYS_STATE_SENT;
199 
200     return r;
201 }
202 
203 /** Asynchronous finish function for TPM2_ECDH_KeyGen
204  *
205  * This function returns the results of a TPM2_ECDH_KeyGen command
206  * invoked via Esys_ECDH_KeyGen_Finish. All non-simple output parameters
207  * are allocated by the function's implementation. NULL can be passed for every
208  * output parameter if the value is not required.
209  *
210  * @param[in,out] esysContext The ESYS_CONTEXT.
211  * @param[out] zPoint Results of P := h[de]Qs.
212  *             (callee-allocated)
213  * @param[out] pubPoint Generated ephemeral public point (Qe).
214  *             (callee-allocated)
215  * @retval TSS2_RC_SUCCESS on success
216  * @retval ESYS_RC_SUCCESS if the function call was a success.
217  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
218  *         pointers or required output handle references are NULL.
219  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
220  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
221  *         internal operations or return parameters.
222  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
223  *         operation already pending.
224  * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
225  *         TPM response is received.
226  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
227  *         at least contain the tag, response length, and response code.
228  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
229  *         not verify.
230  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
231  * @retval TSS2_RCs produced by lower layers of the software stack may be
232  *         returned to the caller unaltered unless handled internally.
233  */
234 TSS2_RC
Esys_ECDH_KeyGen_Finish(ESYS_CONTEXT * esysContext,TPM2B_ECC_POINT ** zPoint,TPM2B_ECC_POINT ** pubPoint)235 Esys_ECDH_KeyGen_Finish(
236     ESYS_CONTEXT *esysContext,
237     TPM2B_ECC_POINT **zPoint,
238     TPM2B_ECC_POINT **pubPoint)
239 {
240     TSS2_RC r;
241     LOG_TRACE("context=%p, zPoint=%p, pubPoint=%p",
242               esysContext, zPoint, pubPoint);
243 
244     if (esysContext == NULL) {
245         LOG_ERROR("esyscontext is NULL.");
246         return TSS2_ESYS_RC_BAD_REFERENCE;
247     }
248 
249     /* Check for correct sequence and set sequence to irregular for now */
250     if (esysContext->state != _ESYS_STATE_SENT &&
251         esysContext->state != _ESYS_STATE_RESUBMISSION) {
252         LOG_ERROR("Esys called in bad sequence.");
253         return TSS2_ESYS_RC_BAD_SEQUENCE;
254     }
255     esysContext->state = _ESYS_STATE_INTERNALERROR;
256 
257     /* Allocate memory for response parameters */
258     if (zPoint != NULL) {
259         *zPoint = calloc(sizeof(TPM2B_ECC_POINT), 1);
260         if (*zPoint == NULL) {
261             return_error(TSS2_ESYS_RC_MEMORY, "Out of memory");
262         }
263     }
264     if (pubPoint != NULL) {
265         *pubPoint = calloc(sizeof(TPM2B_ECC_POINT), 1);
266         if (*pubPoint == NULL) {
267             goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
268         }
269     }
270 
271     /*Receive the TPM response and handle resubmissions if necessary. */
272     r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
273     if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
274         LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
275         esysContext->state = _ESYS_STATE_SENT;
276         goto error_cleanup;
277     }
278     /* This block handle the resubmission of TPM commands given a certain set of
279      * TPM response codes. */
280     if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
281         LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
282             "resubmission: %" PRIx32, r);
283         if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
284             LOG_WARNING("Maximum number of (re)submissions has been reached.");
285             esysContext->state = _ESYS_STATE_INIT;
286             goto error_cleanup;
287         }
288         esysContext->state = _ESYS_STATE_RESUBMISSION;
289         r = Tss2_Sys_ExecuteAsync(esysContext->sys);
290         if (r != TSS2_RC_SUCCESS) {
291             LOG_WARNING("Error attempting to resubmit");
292             /* We do not set esysContext->state here but inherit the most recent
293              * state of the _async function. */
294             goto error_cleanup;
295         }
296         r = TSS2_ESYS_RC_TRY_AGAIN;
297         LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
298         goto error_cleanup;
299     }
300     /* The following is the "regular error" handling. */
301     if (iesys_tpm_error(r)) {
302         LOG_WARNING("Received TPM Error");
303         esysContext->state = _ESYS_STATE_INIT;
304         goto error_cleanup;
305     } else if (r != TSS2_RC_SUCCESS) {
306         LOG_ERROR("Received a non-TPM Error");
307         esysContext->state = _ESYS_STATE_INTERNALERROR;
308         goto error_cleanup;
309     }
310 
311     /*
312      * Now the verification of the response (hmac check) and if necessary the
313      * parameter decryption have to be done.
314      */
315     r = iesys_check_response(esysContext);
316     goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
317                         error_cleanup);
318 
319     /*
320      * After the verification of the response we call the complete function
321      * to deliver the result.
322      */
323     r = Tss2_Sys_ECDH_KeyGen_Complete(esysContext->sys,
324                                       (zPoint != NULL) ? *zPoint : NULL,
325                                       (pubPoint != NULL) ? *pubPoint : NULL);
326     goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
327                         "Received error from SAPI unmarshaling" ,
328                         error_cleanup);
329 
330     esysContext->state = _ESYS_STATE_INIT;
331 
332     return TSS2_RC_SUCCESS;
333 
334 error_cleanup:
335     if (zPoint != NULL)
336         SAFE_FREE(*zPoint);
337     if (pubPoint != NULL)
338         SAFE_FREE(*pubPoint);
339 
340     return r;
341 }
342