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