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 /** Store command parameters inside the ESYS_CONTEXT for use during _Finish */
store_input_parameters(ESYS_CONTEXT * esysContext,const TPM2B_SENSITIVE_CREATE * inSensitive)23 static void store_input_parameters (
24 ESYS_CONTEXT *esysContext,
25 const TPM2B_SENSITIVE_CREATE *inSensitive)
26 {
27 if (inSensitive == NULL) {
28 esysContext->in.CreatePrimary.inSensitive = NULL;
29 } else {
30 esysContext->in.CreatePrimary.inSensitiveData = *inSensitive;
31 esysContext->in.CreatePrimary.inSensitive =
32 &esysContext->in.CreatePrimary.inSensitiveData;
33 }
34 }
35
36 /** One-Call function for TPM2_CreatePrimary
37 *
38 * This function invokes the TPM2_CreatePrimary command in a one-call
39 * variant. This means the function will block until the TPM response is
40 * available. All input parameters are const. The memory for non-simple output
41 * parameters is allocated by the function implementation.
42 *
43 * @param[in,out] esysContext The ESYS_CONTEXT.
44 * @param[in] primaryHandle TPM2_RH_ENDORSEMENT, TPM2_RH_OWNER,
45 * TPM2_RH_PLATFORM+{PP}, or TPM2_RH_NULL.
46 * @param[in] shandle1 Session handle for authorization of primaryHandle
47 * @param[in] shandle2 Second session handle.
48 * @param[in] shandle3 Third session handle.
49 * @param[in] inSensitive The sensitive data, see TPM 2.0 Part 1 Sensitive
50 * Values.
51 * @param[in] inPublic The public template.
52 * @param[in] outsideInfo Data that will be included in the creation data for
53 * this object to provide permanent, verifiable linkage between
54 * this object and some object owner data.
55 * @param[in] creationPCR PCR that will be used in creation data.
56 * @param[out] outPublic The public portion of the created object.
57 * (callee-allocated)
58 * @param[out] creationData Contains a TPMT_CREATION_DATA.
59 * (callee-allocated)
60 * @param[out] creationHash Digest of creationData using nameAlg of outPublic.
61 * (callee-allocated)
62 * @param[out] creationTicket Ticket used by TPM2_CertifyCreation() to validate
63 * that the creation data was produced by the TPM.
64 * (callee-allocated)
65 * @param[out] objectHandle ESYS_TR handle of ESYS resource for TPM2_HANDLE.
66 * @retval TSS2_RC_SUCCESS if the function call was a success.
67 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
68 * pointers or required output handle references are NULL.
69 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
70 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
71 * internal operations or return parameters.
72 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
73 * operation already pending.
74 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
75 * at least contain the tag, response length, and response code.
76 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
77 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
78 did not verify.
79 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
80 * the 'decrypt' attribute bit set.
81 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
82 * the 'encrypt' attribute bit set.
83 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
84 * to the ESYS_CONTEXT or are of the wrong type or if required
85 * ESYS_TR objects are ESYS_TR_NONE.
86 * @retval TSS2_RCs produced by lower layers of the software stack may be
87 * returned to the caller unaltered unless handled internally.
88 */
89 TSS2_RC
Esys_CreatePrimary(ESYS_CONTEXT * esysContext,ESYS_TR primaryHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_SENSITIVE_CREATE * inSensitive,const TPM2B_PUBLIC * inPublic,const TPM2B_DATA * outsideInfo,const TPML_PCR_SELECTION * creationPCR,ESYS_TR * objectHandle,TPM2B_PUBLIC ** outPublic,TPM2B_CREATION_DATA ** creationData,TPM2B_DIGEST ** creationHash,TPMT_TK_CREATION ** creationTicket)90 Esys_CreatePrimary(
91 ESYS_CONTEXT *esysContext,
92 ESYS_TR primaryHandle,
93 ESYS_TR shandle1,
94 ESYS_TR shandle2,
95 ESYS_TR shandle3,
96 const TPM2B_SENSITIVE_CREATE *inSensitive,
97 const TPM2B_PUBLIC *inPublic,
98 const TPM2B_DATA *outsideInfo,
99 const TPML_PCR_SELECTION *creationPCR, ESYS_TR *objectHandle,
100 TPM2B_PUBLIC **outPublic,
101 TPM2B_CREATION_DATA **creationData,
102 TPM2B_DIGEST **creationHash,
103 TPMT_TK_CREATION **creationTicket)
104 {
105 TSS2_RC r;
106
107 r = Esys_CreatePrimary_Async(esysContext, primaryHandle, shandle1, shandle2,
108 shandle3, inSensitive, inPublic, outsideInfo,
109 creationPCR);
110 return_if_error(r, "Error in async function");
111
112 /* Set the timeout to indefinite for now, since we want _Finish to block */
113 int32_t timeouttmp = esysContext->timeout;
114 esysContext->timeout = -1;
115 /*
116 * Now we call the finish function, until return code is not equal to
117 * from TSS2_BASE_RC_TRY_AGAIN.
118 * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
119 * have set the timeout to -1. This occurs for example if the TPM requests
120 * a retransmission of the command via TPM2_RC_YIELDED.
121 */
122 do {
123 r = Esys_CreatePrimary_Finish(esysContext, objectHandle, outPublic,
124 creationData, creationHash,
125 creationTicket);
126 /* This is just debug information about the reattempt to finish the
127 command */
128 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
129 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
130 " => resubmitting command", r);
131 } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
132
133 /* Restore the timeout value to the original value */
134 esysContext->timeout = timeouttmp;
135 return_if_error(r, "Esys Finish");
136
137 return TSS2_RC_SUCCESS;
138 }
139
140 /** Asynchronous function for TPM2_CreatePrimary
141 *
142 * This function invokes the TPM2_CreatePrimary command in a asynchronous
143 * variant. This means the function will return as soon as the command has been
144 * sent downwards the stack to the TPM. All input parameters are const.
145 * In order to retrieve the TPM's response call Esys_CreatePrimary_Finish.
146 *
147 * @param[in,out] esysContext The ESYS_CONTEXT.
148 * @param[in] primaryHandle TPM2_RH_ENDORSEMENT, TPM2_RH_OWNER,
149 * TPM2_RH_PLATFORM+{PP}, or TPM2_RH_NULL.
150 * @param[in] shandle1 Session handle for authorization of primaryHandle
151 * @param[in] shandle2 Second session handle.
152 * @param[in] shandle3 Third session handle.
153 * @param[in] inSensitive The sensitive data, see TPM 2.0 Part 1 Sensitive
154 * Values.
155 * @param[in] inPublic The public template.
156 * @param[in] outsideInfo Data that will be included in the creation data for
157 * this object to provide permanent, verifiable linkage between
158 * this object and some object owner data.
159 * @param[in] creationPCR PCR that will be used in creation data.
160 * @retval ESYS_RC_SUCCESS if the function call was a success.
161 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
162 * pointers or required output handle references are NULL.
163 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
164 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
165 * internal operations or return parameters.
166 * @retval TSS2_RCs produced by lower layers of the software stack may be
167 returned to the caller unaltered unless handled internally.
168 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
169 * the 'decrypt' attribute bit set.
170 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
171 * the 'encrypt' attribute bit set.
172 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
173 * to the ESYS_CONTEXT or are of the wrong type or if required
174 * ESYS_TR objects are ESYS_TR_NONE.
175 */
176 TSS2_RC
Esys_CreatePrimary_Async(ESYS_CONTEXT * esysContext,ESYS_TR primaryHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_SENSITIVE_CREATE * inSensitive,const TPM2B_PUBLIC * inPublic,const TPM2B_DATA * outsideInfo,const TPML_PCR_SELECTION * creationPCR)177 Esys_CreatePrimary_Async(
178 ESYS_CONTEXT *esysContext,
179 ESYS_TR primaryHandle,
180 ESYS_TR shandle1,
181 ESYS_TR shandle2,
182 ESYS_TR shandle3,
183 const TPM2B_SENSITIVE_CREATE *inSensitive,
184 const TPM2B_PUBLIC *inPublic,
185 const TPM2B_DATA *outsideInfo,
186 const TPML_PCR_SELECTION *creationPCR)
187 {
188 TSS2_RC r;
189 LOG_TRACE("context=%p, primaryHandle=%"PRIx32 ", inSensitive=%p,"
190 "inPublic=%p, outsideInfo=%p, creationPCR=%p",
191 esysContext, primaryHandle, inSensitive, inPublic, outsideInfo,
192 creationPCR);
193 TSS2L_SYS_AUTH_COMMAND auths;
194 RSRC_NODE_T *primaryHandleNode;
195
196 /* Check context, sequence correctness and set state to error for now */
197 if (esysContext == NULL) {
198 LOG_ERROR("esyscontext is NULL.");
199 return TSS2_ESYS_RC_BAD_REFERENCE;
200 }
201 r = iesys_check_sequence_async(esysContext);
202 if (r != TSS2_RC_SUCCESS)
203 return r;
204 esysContext->state = _ESYS_STATE_INTERNALERROR;
205
206 /* Check input parameters */
207 r = check_session_feasibility(shandle1, shandle2, shandle3, 1);
208 return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
209 store_input_parameters (esysContext, inSensitive);
210
211 /* Retrieve the metadata objects for provided handles */
212 r = esys_GetResourceObject(esysContext, primaryHandle, &primaryHandleNode);
213 return_state_if_error(r, _ESYS_STATE_INIT, "primaryHandle unknown.");
214
215 /* Initial invocation of SAPI to prepare the command buffer with parameters */
216 r = Tss2_Sys_CreatePrimary_Prepare(esysContext->sys,
217 (primaryHandleNode == NULL) ? TPM2_RH_NULL
218 : primaryHandleNode->rsrc.handle,
219 inSensitive, inPublic, outsideInfo,
220 creationPCR);
221 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
222
223 /* Calculate the cpHash Values */
224 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
225 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
226 if (primaryHandleNode != NULL)
227 iesys_compute_session_value(esysContext->session_tab[0],
228 &primaryHandleNode->rsrc.name, &primaryHandleNode->auth);
229 else
230 iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
231
232 iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
233 iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
234
235 /* Generate the auth values and set them in the SAPI command buffer */
236 r = iesys_gen_auths(esysContext, primaryHandleNode, NULL, NULL, &auths);
237 return_state_if_error(r, _ESYS_STATE_INIT,
238 "Error in computation of auth values");
239
240 esysContext->authsCount = auths.count;
241 if (auths.count > 0) {
242 r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
243 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
244 }
245
246 /* Trigger execution and finish the async invocation */
247 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
248 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
249 "Finish (Execute Async)");
250
251 esysContext->state = _ESYS_STATE_SENT;
252
253 return r;
254 }
255
256 /** Asynchronous finish function for TPM2_CreatePrimary
257 *
258 * This function returns the results of a TPM2_CreatePrimary command
259 * invoked via Esys_CreatePrimary_Finish. All non-simple output parameters
260 * are allocated by the function's implementation. NULL can be passed for every
261 * output parameter if the value is not required.
262 *
263 * @param[in,out] esysContext The ESYS_CONTEXT.
264 * @param[out] outPublic The public portion of the created object.
265 * (callee-allocated)
266 * @param[out] creationData Contains a TPMT_CREATION_DATA.
267 * (callee-allocated)
268 * @param[out] creationHash Digest of creationData using nameAlg of outPublic.
269 * (callee-allocated)
270 * @param[out] creationTicket Ticket used by TPM2_CertifyCreation() to validate
271 * that the creation data was produced by the TPM.
272 * (callee-allocated)
273 * @param[out] objectHandle ESYS_TR handle of ESYS resource for TPM2_HANDLE.
274 * @retval TSS2_RC_SUCCESS on success
275 * @retval ESYS_RC_SUCCESS if the function call was a success.
276 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
277 * pointers or required output handle references are NULL.
278 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
279 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
280 * internal operations or return parameters.
281 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
282 * operation already pending.
283 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
284 * TPM response is received.
285 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
286 * at least contain the tag, response length, and response code.
287 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
288 * not verify.
289 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
290 * @retval TSS2_RCs produced by lower layers of the software stack may be
291 * returned to the caller unaltered unless handled internally.
292 */
293 TSS2_RC
Esys_CreatePrimary_Finish(ESYS_CONTEXT * esysContext,ESYS_TR * objectHandle,TPM2B_PUBLIC ** outPublic,TPM2B_CREATION_DATA ** creationData,TPM2B_DIGEST ** creationHash,TPMT_TK_CREATION ** creationTicket)294 Esys_CreatePrimary_Finish(
295 ESYS_CONTEXT *esysContext, ESYS_TR *objectHandle,
296 TPM2B_PUBLIC **outPublic,
297 TPM2B_CREATION_DATA **creationData,
298 TPM2B_DIGEST **creationHash,
299 TPMT_TK_CREATION **creationTicket)
300 {
301 TPM2B_PUBLIC *loutPublic = NULL;
302 TSS2_RC r;
303 LOG_TRACE("context=%p, objectHandle=%p, outPublic=%p,"
304 "creationData=%p, creationHash=%p, creationTicket=%p",
305 esysContext, objectHandle, outPublic,
306 creationData, creationHash, creationTicket);
307
308 if (esysContext == NULL) {
309 LOG_ERROR("esyscontext is NULL.");
310 return TSS2_ESYS_RC_BAD_REFERENCE;
311 }
312
313 /* Check for correct sequence and set sequence to irregular for now */
314 if (esysContext->state != _ESYS_STATE_SENT &&
315 esysContext->state != _ESYS_STATE_RESUBMISSION) {
316 LOG_ERROR("Esys called in bad sequence.");
317 return TSS2_ESYS_RC_BAD_SEQUENCE;
318 }
319 esysContext->state = _ESYS_STATE_INTERNALERROR;
320 TPM2B_NAME name;
321 RSRC_NODE_T *objectHandleNode = NULL;
322
323 /* Initialize parameter to avoid unitialized usage */
324 if (creationHash != NULL)
325 *creationHash = NULL;
326 if (creationTicket != NULL)
327 *creationTicket = NULL;
328
329 /* Allocate memory for response parameters */
330 if (objectHandle == NULL) {
331 LOG_ERROR("Handle objectHandle may not be NULL");
332 return TSS2_ESYS_RC_BAD_REFERENCE;
333 }
334 *objectHandle = esysContext->esys_handle_cnt++;
335 r = esys_CreateResourceObject(esysContext, *objectHandle, &objectHandleNode);
336 if (r != TSS2_RC_SUCCESS)
337 return r;
338
339 loutPublic = calloc(sizeof(TPM2B_PUBLIC), 1);
340 if (loutPublic == NULL) {
341 goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
342 }
343 if (creationData != NULL) {
344 *creationData = calloc(sizeof(TPM2B_CREATION_DATA), 1);
345 if (*creationData == NULL) {
346 goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
347 }
348 }
349 if (creationHash != NULL) {
350 *creationHash = calloc(sizeof(TPM2B_DIGEST), 1);
351 if (*creationHash == NULL) {
352 goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
353 }
354 }
355 if (creationTicket != NULL) {
356 *creationTicket = calloc(sizeof(TPMT_TK_CREATION), 1);
357 if (*creationTicket == NULL) {
358 goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
359 }
360 }
361
362 /*Receive the TPM response and handle resubmissions if necessary. */
363 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
364 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
365 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
366 esysContext->state = _ESYS_STATE_SENT;
367 goto error_cleanup;
368 }
369 /* This block handle the resubmission of TPM commands given a certain set of
370 * TPM response codes. */
371 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
372 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
373 "resubmission: %" PRIx32, r);
374 if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
375 LOG_WARNING("Maximum number of (re)submissions has been reached.");
376 esysContext->state = _ESYS_STATE_INIT;
377 goto error_cleanup;
378 }
379 esysContext->state = _ESYS_STATE_RESUBMISSION;
380 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
381 if (r != TSS2_RC_SUCCESS) {
382 LOG_WARNING("Error attempting to resubmit");
383 /* We do not set esysContext->state here but inherit the most recent
384 * state of the _async function. */
385 goto error_cleanup;
386 }
387 r = TSS2_ESYS_RC_TRY_AGAIN;
388 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
389 goto error_cleanup;
390 }
391 /* The following is the "regular error" handling. */
392 if (iesys_tpm_error(r)) {
393 LOG_WARNING("Received TPM Error");
394 esysContext->state = _ESYS_STATE_INIT;
395 goto error_cleanup;
396 } else if (r != TSS2_RC_SUCCESS) {
397 LOG_ERROR("Received a non-TPM Error");
398 esysContext->state = _ESYS_STATE_INTERNALERROR;
399 goto error_cleanup;
400 }
401
402 /*
403 * Now the verification of the response (hmac check) and if necessary the
404 * parameter decryption have to be done.
405 */
406 r = iesys_check_response(esysContext);
407 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
408 error_cleanup);
409
410 /*
411 * After the verification of the response we call the complete function
412 * to deliver the result.
413 */
414 r = Tss2_Sys_CreatePrimary_Complete(esysContext->sys,
415 &objectHandleNode->rsrc.handle,
416 loutPublic,
417 (creationData != NULL) ? *creationData
418 : NULL,
419 (creationHash != NULL) ? *creationHash
420 : NULL,
421 (creationTicket != NULL)
422 ? *creationTicket : NULL, &name);
423 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
424 "Received error from SAPI unmarshaling" ,
425 error_cleanup);
426
427
428 /* Check name and outPublic for consistency */
429 if (!iesys_compare_name(loutPublic, &name))
430 goto_error(r, TSS2_ESYS_RC_MALFORMED_RESPONSE,
431 "in Public name not equal name in response", error_cleanup);
432
433 /* Update the meta data of the ESYS_TR object */
434 objectHandleNode->auth = (*esysContext->in.CreatePrimary.inSensitive).sensitive.userAuth;
435 objectHandleNode->rsrc.name = name;
436 objectHandleNode->rsrc.rsrcType = IESYSC_KEY_RSRC;
437 objectHandleNode->rsrc.misc.rsrc_key_pub = *loutPublic;
438 if (outPublic != NULL)
439 *outPublic = loutPublic;
440 else
441 SAFE_FREE(loutPublic);
442
443 esysContext->state = _ESYS_STATE_INIT;
444
445 return TSS2_RC_SUCCESS;
446
447 error_cleanup:
448 Esys_TR_Close(esysContext, objectHandle);
449 SAFE_FREE(loutPublic);
450 if (creationData != NULL)
451 SAFE_FREE(*creationData);
452 if (creationHash != NULL)
453 SAFE_FREE(*creationHash);
454 if (creationTicket != NULL)
455 SAFE_FREE(*creationTicket);
456
457 return r;
458 }
459