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 (base_rc(r) == TSS2_BASE_RC_TRY_AGAIN)
129 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
130 " => resubmitting command", r);
131 } while (base_rc(r) == 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 if (inPublic) {
211 r = iesys_hash_long_auth_values(
212 &esysContext->in.CreatePrimary.inSensitive->sensitive.userAuth,
213 inPublic->publicArea.nameAlg);
214 return_state_if_error(r, _ESYS_STATE_INIT, "Adapt auth value.");
215 }
216
217 /* Retrieve the metadata objects for provided handles */
218 r = esys_GetResourceObject(esysContext, primaryHandle, &primaryHandleNode);
219 return_state_if_error(r, _ESYS_STATE_INIT, "primaryHandle unknown.");
220
221 /* Initial invocation of SAPI to prepare the command buffer with parameters */
222 r = Tss2_Sys_CreatePrimary_Prepare(esysContext->sys,
223 (primaryHandleNode == NULL) ? TPM2_RH_NULL
224 : primaryHandleNode->rsrc.handle,
225 esysContext->in.CreatePrimary.inSensitive,
226 inPublic, outsideInfo,
227 creationPCR);
228 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
229
230 /* Calculate the cpHash Values */
231 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
232 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
233 if (primaryHandleNode != NULL)
234 iesys_compute_session_value(esysContext->session_tab[0],
235 &primaryHandleNode->rsrc.name, &primaryHandleNode->auth);
236 else
237 iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
238
239 iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
240 iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
241
242 /* Generate the auth values and set them in the SAPI command buffer */
243 r = iesys_gen_auths(esysContext, primaryHandleNode, NULL, NULL, &auths);
244 return_state_if_error(r, _ESYS_STATE_INIT,
245 "Error in computation of auth values");
246
247 esysContext->authsCount = auths.count;
248 if (auths.count > 0) {
249 r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
250 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
251 }
252
253 /* Trigger execution and finish the async invocation */
254 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
255 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
256 "Finish (Execute Async)");
257
258 esysContext->state = _ESYS_STATE_SENT;
259
260 return r;
261 }
262
263 /** Asynchronous finish function for TPM2_CreatePrimary
264 *
265 * This function returns the results of a TPM2_CreatePrimary command
266 * invoked via Esys_CreatePrimary_Finish. All non-simple output parameters
267 * are allocated by the function's implementation. NULL can be passed for every
268 * output parameter if the value is not required.
269 *
270 * @param[in,out] esysContext The ESYS_CONTEXT.
271 * @param[out] outPublic The public portion of the created object.
272 * (callee-allocated)
273 * @param[out] creationData Contains a TPMT_CREATION_DATA.
274 * (callee-allocated)
275 * @param[out] creationHash Digest of creationData using nameAlg of outPublic.
276 * (callee-allocated)
277 * @param[out] creationTicket Ticket used by TPM2_CertifyCreation() to validate
278 * that the creation data was produced by the TPM.
279 * (callee-allocated)
280 * @param[out] objectHandle ESYS_TR handle of ESYS resource for TPM2_HANDLE.
281 * @retval TSS2_RC_SUCCESS on success
282 * @retval ESYS_RC_SUCCESS if the function call was a success.
283 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
284 * pointers or required output handle references are NULL.
285 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
286 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
287 * internal operations or return parameters.
288 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
289 * operation already pending.
290 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
291 * TPM response is received.
292 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
293 * at least contain the tag, response length, and response code.
294 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
295 * not verify.
296 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
297 * @retval TSS2_RCs produced by lower layers of the software stack may be
298 * returned to the caller unaltered unless handled internally.
299 */
300 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)301 Esys_CreatePrimary_Finish(
302 ESYS_CONTEXT *esysContext, ESYS_TR *objectHandle,
303 TPM2B_PUBLIC **outPublic,
304 TPM2B_CREATION_DATA **creationData,
305 TPM2B_DIGEST **creationHash,
306 TPMT_TK_CREATION **creationTicket)
307 {
308 TPM2B_PUBLIC *loutPublic = NULL;
309 TSS2_RC r;
310 LOG_TRACE("context=%p, objectHandle=%p, outPublic=%p,"
311 "creationData=%p, creationHash=%p, creationTicket=%p",
312 esysContext, objectHandle, outPublic,
313 creationData, creationHash, creationTicket);
314
315 if (esysContext == NULL) {
316 LOG_ERROR("esyscontext is NULL.");
317 return TSS2_ESYS_RC_BAD_REFERENCE;
318 }
319
320 /* Check for correct sequence and set sequence to irregular for now */
321 if (esysContext->state != _ESYS_STATE_SENT &&
322 esysContext->state != _ESYS_STATE_RESUBMISSION) {
323 LOG_ERROR("Esys called in bad sequence.");
324 return TSS2_ESYS_RC_BAD_SEQUENCE;
325 }
326 esysContext->state = _ESYS_STATE_INTERNALERROR;
327 TPM2B_NAME name;
328 RSRC_NODE_T *objectHandleNode = NULL;
329
330 /* Initialize parameter to avoid unitialized usage */
331 if (creationHash != NULL)
332 *creationHash = NULL;
333 if (creationTicket != NULL)
334 *creationTicket = NULL;
335
336 /* Allocate memory for response parameters */
337 if (objectHandle == NULL) {
338 LOG_ERROR("Handle objectHandle may not be NULL");
339 return TSS2_ESYS_RC_BAD_REFERENCE;
340 }
341 *objectHandle = esysContext->esys_handle_cnt++;
342 r = esys_CreateResourceObject(esysContext, *objectHandle, &objectHandleNode);
343 if (r != TSS2_RC_SUCCESS)
344 return r;
345
346 loutPublic = calloc(sizeof(TPM2B_PUBLIC), 1);
347 if (loutPublic == NULL) {
348 goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
349 }
350 if (creationData != NULL) {
351 *creationData = calloc(sizeof(TPM2B_CREATION_DATA), 1);
352 if (*creationData == NULL) {
353 goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
354 }
355 }
356 if (creationHash != NULL) {
357 *creationHash = calloc(sizeof(TPM2B_DIGEST), 1);
358 if (*creationHash == NULL) {
359 goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
360 }
361 }
362 if (creationTicket != NULL) {
363 *creationTicket = calloc(sizeof(TPMT_TK_CREATION), 1);
364 if (*creationTicket == NULL) {
365 goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
366 }
367 }
368
369 /*Receive the TPM response and handle resubmissions if necessary. */
370 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
371 if (base_rc(r) == TSS2_BASE_RC_TRY_AGAIN) {
372 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
373 esysContext->state = _ESYS_STATE_SENT;
374 goto error_cleanup;
375 }
376 /* This block handle the resubmission of TPM commands given a certain set of
377 * TPM response codes. */
378 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
379 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
380 "resubmission: %" PRIx32, r);
381 if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
382 LOG_WARNING("Maximum number of (re)submissions has been reached.");
383 esysContext->state = _ESYS_STATE_INIT;
384 goto error_cleanup;
385 }
386 esysContext->state = _ESYS_STATE_RESUBMISSION;
387 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
388 if (r != TSS2_RC_SUCCESS) {
389 LOG_WARNING("Error attempting to resubmit");
390 /* We do not set esysContext->state here but inherit the most recent
391 * state of the _async function. */
392 goto error_cleanup;
393 }
394 r = TSS2_ESYS_RC_TRY_AGAIN;
395 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
396 goto error_cleanup;
397 }
398 /* The following is the "regular error" handling. */
399 if (iesys_tpm_error(r)) {
400 LOG_WARNING("Received TPM Error");
401 esysContext->state = _ESYS_STATE_INIT;
402 goto error_cleanup;
403 } else if (r != TSS2_RC_SUCCESS) {
404 LOG_ERROR("Received a non-TPM Error");
405 esysContext->state = _ESYS_STATE_INTERNALERROR;
406 goto error_cleanup;
407 }
408
409 /*
410 * Now the verification of the response (hmac check) and if necessary the
411 * parameter decryption have to be done.
412 */
413 r = iesys_check_response(esysContext);
414 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
415 error_cleanup);
416
417 /*
418 * After the verification of the response we call the complete function
419 * to deliver the result.
420 */
421 r = Tss2_Sys_CreatePrimary_Complete(esysContext->sys,
422 &objectHandleNode->rsrc.handle,
423 loutPublic,
424 (creationData != NULL) ? *creationData
425 : NULL,
426 (creationHash != NULL) ? *creationHash
427 : NULL,
428 (creationTicket != NULL)
429 ? *creationTicket : NULL, &name);
430 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
431 "Received error from SAPI unmarshaling" ,
432 error_cleanup);
433
434
435 /* Check name and outPublic for consistency */
436 if (!iesys_compare_name(loutPublic, &name))
437 goto_error(r, TSS2_ESYS_RC_MALFORMED_RESPONSE,
438 "in Public name not equal name in response", error_cleanup);
439
440 /* Update the meta data of the ESYS_TR object */
441 objectHandleNode->auth = (*esysContext->in.CreatePrimary.inSensitive).sensitive.userAuth;
442 objectHandleNode->rsrc.name = name;
443 objectHandleNode->rsrc.rsrcType = IESYSC_KEY_RSRC;
444 objectHandleNode->rsrc.misc.rsrc_key_pub = *loutPublic;
445 if (outPublic != NULL)
446 *outPublic = loutPublic;
447 else
448 SAFE_FREE(loutPublic);
449
450 esysContext->state = _ESYS_STATE_INIT;
451
452 return TSS2_RC_SUCCESS;
453
454 error_cleanup:
455 Esys_TR_Close(esysContext, objectHandle);
456 SAFE_FREE(loutPublic);
457 if (creationData != NULL)
458 SAFE_FREE(*creationData);
459 if (creationHash != NULL)
460 SAFE_FREE(*creationHash);
461 if (creationTicket != NULL)
462 SAFE_FREE(*creationTicket);
463
464 return r;
465 }
466