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