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_ZGen_2Phase
23 *
24 * This function invokes the TPM2_ZGen_2Phase 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] keyA Handle of an unrestricted decryption key ECC.
31 * @param[in] shandle1 Session handle for authorization of keyA
32 * @param[in] shandle2 Second session handle.
33 * @param[in] shandle3 Third session handle.
34 * @param[in] inQsB Other party's static public key (Qs,B = (Xs,B, Ys,B)).
35 * @param[in] inQeB Other party's ephemeral public key (Qe,B = (Xe,B, Ye,B)).
36 * @param[in] inScheme The key exchange scheme.
37 * @param[in] counter Value returned by TPM2_EC_Ephemeral().
38 * @param[out] outZ1 X and Y coordinates of the computed value (scheme
39 * dependent).
40 * (callee-allocated)
41 * @param[out] outZ2 X and Y coordinates of the second computed value (scheme
42 * dependent).
43 * (callee-allocated)
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_RCs produced by lower layers of the software stack may be
65 * returned to the caller unaltered unless handled internally.
66 */
67 TSS2_RC
Esys_ZGen_2Phase(ESYS_CONTEXT * esysContext,ESYS_TR keyA,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_ECC_POINT * inQsB,const TPM2B_ECC_POINT * inQeB,TPMI_ECC_KEY_EXCHANGE inScheme,UINT16 counter,TPM2B_ECC_POINT ** outZ1,TPM2B_ECC_POINT ** outZ2)68 Esys_ZGen_2Phase(
69 ESYS_CONTEXT *esysContext,
70 ESYS_TR keyA,
71 ESYS_TR shandle1,
72 ESYS_TR shandle2,
73 ESYS_TR shandle3,
74 const TPM2B_ECC_POINT *inQsB,
75 const TPM2B_ECC_POINT *inQeB,
76 TPMI_ECC_KEY_EXCHANGE inScheme,
77 UINT16 counter,
78 TPM2B_ECC_POINT **outZ1,
79 TPM2B_ECC_POINT **outZ2)
80 {
81 TSS2_RC r;
82
83 r = Esys_ZGen_2Phase_Async(esysContext, keyA, shandle1, shandle2, shandle3,
84 inQsB, inQeB, inScheme, counter);
85 return_if_error(r, "Error in async function");
86
87 /* Set the timeout to indefinite for now, since we want _Finish to block */
88 int32_t timeouttmp = esysContext->timeout;
89 esysContext->timeout = -1;
90 /*
91 * Now we call the finish function, until return code is not equal to
92 * from TSS2_BASE_RC_TRY_AGAIN.
93 * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
94 * have set the timeout to -1. This occurs for example if the TPM requests
95 * a retransmission of the command via TPM2_RC_YIELDED.
96 */
97 do {
98 r = Esys_ZGen_2Phase_Finish(esysContext, outZ1, outZ2);
99 /* This is just debug information about the reattempt to finish the
100 command */
101 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
102 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
103 " => resubmitting command", r);
104 } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
105
106 /* Restore the timeout value to the original value */
107 esysContext->timeout = timeouttmp;
108 return_if_error(r, "Esys Finish");
109
110 return TSS2_RC_SUCCESS;
111 }
112
113 /** Asynchronous function for TPM2_ZGen_2Phase
114 *
115 * This function invokes the TPM2_ZGen_2Phase command in a asynchronous
116 * variant. This means the function will return as soon as the command has been
117 * sent downwards the stack to the TPM. All input parameters are const.
118 * In order to retrieve the TPM's response call Esys_ZGen_2Phase_Finish.
119 *
120 * @param[in,out] esysContext The ESYS_CONTEXT.
121 * @param[in] keyA Handle of an unrestricted decryption key ECC.
122 * @param[in] shandle1 Session handle for authorization of keyA
123 * @param[in] shandle2 Second session handle.
124 * @param[in] shandle3 Third session handle.
125 * @param[in] inQsB Other party's static public key (Qs,B = (Xs,B, Ys,B)).
126 * @param[in] inQeB Other party's ephemeral public key (Qe,B = (Xe,B, Ye,B)).
127 * @param[in] inScheme The key exchange scheme.
128 * @param[in] counter Value returned by TPM2_EC_Ephemeral().
129 * @retval ESYS_RC_SUCCESS if the function call was a success.
130 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
131 * pointers or required output handle references are NULL.
132 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
133 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
134 * internal operations or return parameters.
135 * @retval TSS2_RCs produced by lower layers of the software stack may be
136 returned to the caller unaltered unless handled internally.
137 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
138 * the 'decrypt' attribute bit set.
139 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
140 * the 'encrypt' attribute bit set.
141 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
142 * to the ESYS_CONTEXT or are of the wrong type or if required
143 * ESYS_TR objects are ESYS_TR_NONE.
144 */
145 TSS2_RC
Esys_ZGen_2Phase_Async(ESYS_CONTEXT * esysContext,ESYS_TR keyA,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_ECC_POINT * inQsB,const TPM2B_ECC_POINT * inQeB,TPMI_ECC_KEY_EXCHANGE inScheme,UINT16 counter)146 Esys_ZGen_2Phase_Async(
147 ESYS_CONTEXT *esysContext,
148 ESYS_TR keyA,
149 ESYS_TR shandle1,
150 ESYS_TR shandle2,
151 ESYS_TR shandle3,
152 const TPM2B_ECC_POINT *inQsB,
153 const TPM2B_ECC_POINT *inQeB,
154 TPMI_ECC_KEY_EXCHANGE inScheme,
155 UINT16 counter)
156 {
157 TSS2_RC r;
158 LOG_TRACE("context=%p, keyA=%"PRIx32 ", inQsB=%p,"
159 "inQeB=%p, inScheme=%04"PRIx16", counter=%04"PRIx16"",
160 esysContext, keyA, inQsB, inQeB, inScheme,
161 counter);
162 TSS2L_SYS_AUTH_COMMAND auths;
163 RSRC_NODE_T *keyANode;
164
165 /* Check context, sequence correctness and set state to error for now */
166 if (esysContext == NULL) {
167 LOG_ERROR("esyscontext is NULL.");
168 return TSS2_ESYS_RC_BAD_REFERENCE;
169 }
170 r = iesys_check_sequence_async(esysContext);
171 if (r != TSS2_RC_SUCCESS)
172 return r;
173 esysContext->state = _ESYS_STATE_INTERNALERROR;
174
175 /* Check input parameters */
176 r = check_session_feasibility(shandle1, shandle2, shandle3, 1);
177 return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
178
179 /* Retrieve the metadata objects for provided handles */
180 r = esys_GetResourceObject(esysContext, keyA, &keyANode);
181 return_state_if_error(r, _ESYS_STATE_INIT, "keyA unknown.");
182
183 /* Initial invocation of SAPI to prepare the command buffer with parameters */
184 r = Tss2_Sys_ZGen_2Phase_Prepare(esysContext->sys,
185 (keyANode == NULL) ? TPM2_RH_NULL
186 : keyANode->rsrc.handle, inQsB, inQeB,
187 inScheme, counter);
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 (keyANode != NULL)
194 iesys_compute_session_value(esysContext->session_tab[0],
195 &keyANode->rsrc.name, &keyANode->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, keyANode, 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_ZGen_2Phase
224 *
225 * This function returns the results of a TPM2_ZGen_2Phase command
226 * invoked via Esys_ZGen_2Phase_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] outZ1 X and Y coordinates of the computed value (scheme
232 * dependent).
233 * (callee-allocated)
234 * @param[out] outZ2 X and Y coordinates of the second computed value (scheme
235 * dependent).
236 * (callee-allocated)
237 * @retval TSS2_RC_SUCCESS on success
238 * @retval ESYS_RC_SUCCESS if the function call was a success.
239 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
240 * pointers or required output handle references are NULL.
241 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
242 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
243 * internal operations or return parameters.
244 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
245 * operation already pending.
246 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
247 * TPM response is received.
248 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
249 * at least contain the tag, response length, and response code.
250 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
251 * not verify.
252 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
253 * @retval TSS2_RCs produced by lower layers of the software stack may be
254 * returned to the caller unaltered unless handled internally.
255 */
256 TSS2_RC
Esys_ZGen_2Phase_Finish(ESYS_CONTEXT * esysContext,TPM2B_ECC_POINT ** outZ1,TPM2B_ECC_POINT ** outZ2)257 Esys_ZGen_2Phase_Finish(
258 ESYS_CONTEXT *esysContext,
259 TPM2B_ECC_POINT **outZ1,
260 TPM2B_ECC_POINT **outZ2)
261 {
262 TSS2_RC r;
263 LOG_TRACE("context=%p, outZ1=%p, outZ2=%p",
264 esysContext, outZ1, outZ2);
265
266 if (esysContext == NULL) {
267 LOG_ERROR("esyscontext is NULL.");
268 return TSS2_ESYS_RC_BAD_REFERENCE;
269 }
270
271 /* Check for correct sequence and set sequence to irregular for now */
272 if (esysContext->state != _ESYS_STATE_SENT &&
273 esysContext->state != _ESYS_STATE_RESUBMISSION) {
274 LOG_ERROR("Esys called in bad sequence.");
275 return TSS2_ESYS_RC_BAD_SEQUENCE;
276 }
277 esysContext->state = _ESYS_STATE_INTERNALERROR;
278
279 /* Allocate memory for response parameters */
280 if (outZ1 != NULL) {
281 *outZ1 = calloc(sizeof(TPM2B_ECC_POINT), 1);
282 if (*outZ1 == NULL) {
283 return_error(TSS2_ESYS_RC_MEMORY, "Out of memory");
284 }
285 }
286 if (outZ2 != NULL) {
287 *outZ2 = calloc(sizeof(TPM2B_ECC_POINT), 1);
288 if (*outZ2 == NULL) {
289 goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
290 }
291 }
292
293 /*Receive the TPM response and handle resubmissions if necessary. */
294 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
295 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
296 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
297 esysContext->state = _ESYS_STATE_SENT;
298 goto error_cleanup;
299 }
300 /* This block handle the resubmission of TPM commands given a certain set of
301 * TPM response codes. */
302 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
303 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
304 "resubmission: %" PRIx32, r);
305 if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
306 LOG_WARNING("Maximum number of (re)submissions has been reached.");
307 esysContext->state = _ESYS_STATE_INIT;
308 goto error_cleanup;
309 }
310 esysContext->state = _ESYS_STATE_RESUBMISSION;
311 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
312 if (r != TSS2_RC_SUCCESS) {
313 LOG_WARNING("Error attempting to resubmit");
314 /* We do not set esysContext->state here but inherit the most recent
315 * state of the _async function. */
316 goto error_cleanup;
317 }
318 r = TSS2_ESYS_RC_TRY_AGAIN;
319 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
320 goto error_cleanup;
321 }
322 /* The following is the "regular error" handling. */
323 if (iesys_tpm_error(r)) {
324 LOG_WARNING("Received TPM Error");
325 esysContext->state = _ESYS_STATE_INIT;
326 goto error_cleanup;
327 } else if (r != TSS2_RC_SUCCESS) {
328 LOG_ERROR("Received a non-TPM Error");
329 esysContext->state = _ESYS_STATE_INTERNALERROR;
330 goto error_cleanup;
331 }
332
333 /*
334 * Now the verification of the response (hmac check) and if necessary the
335 * parameter decryption have to be done.
336 */
337 r = iesys_check_response(esysContext);
338 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
339 error_cleanup);
340
341 /*
342 * After the verification of the response we call the complete function
343 * to deliver the result.
344 */
345 r = Tss2_Sys_ZGen_2Phase_Complete(esysContext->sys,
346 (outZ1 != NULL) ? *outZ1 : NULL,
347 (outZ2 != NULL) ? *outZ2 : NULL);
348 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
349 "Received error from SAPI unmarshaling" ,
350 error_cleanup);
351
352 esysContext->state = _ESYS_STATE_INIT;
353
354 return TSS2_RC_SUCCESS;
355
356 error_cleanup:
357 if (outZ1 != NULL)
358 SAFE_FREE(*outZ1);
359 if (outZ2 != NULL)
360 SAFE_FREE(*outZ2);
361
362 return r;
363 }
364