Peter Huewe | d5a36f6 | 2018-06-12 00:59:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: BSD-2 */ |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 2 | /******************************************************************************* |
| 3 | * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG |
| 4 | * All rights reserved. |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 5 | ******************************************************************************/ |
| 6 | |
Philip Tricca | 910f17c | 2018-03-15 12:38:37 -0700 | [diff] [blame] | 7 | #include "tss2_mu.h" |
| 8 | #include "tss2_sys.h" |
Philip Tricca | 8ffd3c4 | 2018-03-09 16:27:24 -0800 | [diff] [blame] | 9 | #include "tss2_esys.h" |
Juergen Repp | ed6e6e2 | 2018-03-19 17:34:32 +0100 | [diff] [blame] | 10 | |
| 11 | #include "esys_types.h" |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 12 | #include "esys_iutil.h" |
| 13 | #include "esys_mu.h" |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 14 | #define LOGMODULE esys |
Philip Tricca | a7c51ce | 2018-03-10 18:28:25 -0800 | [diff] [blame] | 15 | #include "util/log.h" |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 16 | |
Juergen Repp | add438d | 2018-04-09 10:00:19 +0200 | [diff] [blame] | 17 | /** Store command parameters inside the ESYS_CONTEXT for use during _Finish */ |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 18 | static void store_input_parameters ( |
| 19 | ESYS_CONTEXT *esysContext, |
| 20 | ESYS_TR oldParent, |
| 21 | ESYS_TR newParent, |
| 22 | const TPM2B_PRIVATE *inDuplicate, |
| 23 | const TPM2B_NAME *name, |
| 24 | const TPM2B_ENCRYPTED_SECRET *inSymSeed) |
| 25 | { |
| 26 | esysContext->in.Rewrap.oldParent = oldParent; |
| 27 | esysContext->in.Rewrap.newParent = newParent; |
| 28 | if (inDuplicate == NULL) { |
| 29 | esysContext->in.Rewrap.inDuplicate = NULL; |
| 30 | } else { |
| 31 | esysContext->in.Rewrap.inDuplicateData = *inDuplicate; |
| 32 | esysContext->in.Rewrap.inDuplicate = |
| 33 | &esysContext->in.Rewrap.inDuplicateData; |
| 34 | } |
| 35 | if (name == NULL) { |
| 36 | esysContext->in.Rewrap.name = NULL; |
| 37 | } else { |
| 38 | esysContext->in.Rewrap.nameData = *name; |
| 39 | esysContext->in.Rewrap.name = |
| 40 | &esysContext->in.Rewrap.nameData; |
| 41 | } |
| 42 | if (inSymSeed == NULL) { |
| 43 | esysContext->in.Rewrap.inSymSeed = NULL; |
| 44 | } else { |
| 45 | esysContext->in.Rewrap.inSymSeedData = *inSymSeed; |
| 46 | esysContext->in.Rewrap.inSymSeed = |
| 47 | &esysContext->in.Rewrap.inSymSeedData; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** One-Call function for TPM2_Rewrap |
| 52 | * |
| 53 | * This function invokes the TPM2_Rewrap command in a one-call |
| 54 | * variant. This means the function will block until the TPM response is |
| 55 | * available. All input parameters are const. The memory for non-simple output |
| 56 | * parameters is allocated by the function implementation. |
| 57 | * |
| 58 | * @param[in,out] esysContext The ESYS_CONTEXT. |
Juergen Repp | f7e5bd3 | 2018-04-26 11:11:32 +0200 | [diff] [blame] | 59 | * @param[in] oldParent Parent of object. |
| 60 | * @param[in] newParent New parent of the object. |
| 61 | * @param[in] shandle1 Session handle for authorization of oldParent |
| 62 | * @param[in] shandle2 Second session handle. |
| 63 | * @param[in] shandle3 Third session handle. |
| 64 | * @param[in] inDuplicate An object encrypted using symmetric key derived from |
| 65 | * inSymSeed. |
| 66 | * @param[in] name The Name of the object being rewrapped. |
| 67 | * @param[in] inSymSeed The seed for the symmetric key and HMAC key. |
| 68 | * @param[out] outDuplicate An object encrypted using symmetric key derived from |
| 69 | * outSymSeed. |
| 70 | * (callee-allocated) |
| 71 | * @param[out] outSymSeed Seed for a symmetric key protected by newParent |
| 72 | * asymmetric key. |
| 73 | * (callee-allocated) |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 74 | * @retval TSS2_RC_SUCCESS on success |
Juergen Repp | f7e5bd3 | 2018-04-26 11:11:32 +0200 | [diff] [blame] | 75 | * @retval ESYS_RC_SUCCESS if the function call was a success. |
| 76 | * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input |
| 77 | * pointers or required output handle references are NULL. |
| 78 | * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected. |
| 79 | * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for |
| 80 | * internal operations or return parameters. |
| 81 | * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous |
| 82 | * operation already pending. |
| 83 | * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not |
| 84 | * at least contain the tag, response length, and response code. |
| 85 | * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted. |
| 86 | * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has |
| 87 | * the 'decrypt' attribute bit set. |
| 88 | * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has |
| 89 | * the 'encrypt' attribute bit set. |
| 90 | * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown to the |
| 91 | * ESYS_CONTEXT or are of the wrong type or if required ESYS_TR objects |
| 92 | * are ESYS_TR_NONE. |
| 93 | * @retval TSS2_RCs produced by lower layers of the software stack may be |
| 94 | * returned to the caller unaltered unless handled internally. |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 95 | */ |
| 96 | TSS2_RC |
| 97 | Esys_Rewrap( |
| 98 | ESYS_CONTEXT *esysContext, |
| 99 | ESYS_TR oldParent, |
| 100 | ESYS_TR newParent, |
| 101 | ESYS_TR shandle1, |
| 102 | ESYS_TR shandle2, |
| 103 | ESYS_TR shandle3, |
| 104 | const TPM2B_PRIVATE *inDuplicate, |
| 105 | const TPM2B_NAME *name, |
| 106 | const TPM2B_ENCRYPTED_SECRET *inSymSeed, |
| 107 | TPM2B_PRIVATE **outDuplicate, |
| 108 | TPM2B_ENCRYPTED_SECRET **outSymSeed) |
| 109 | { |
Juergen Repp | 3b7b41b | 2018-03-19 15:58:04 +0100 | [diff] [blame] | 110 | TSS2_RC r; |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 111 | |
Juergen Repp | add438d | 2018-04-09 10:00:19 +0200 | [diff] [blame] | 112 | r = Esys_Rewrap_Async(esysContext, |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 113 | oldParent, |
| 114 | newParent, |
| 115 | shandle1, |
| 116 | shandle2, |
| 117 | shandle3, |
| 118 | inDuplicate, |
| 119 | name, |
| 120 | inSymSeed); |
| 121 | return_if_error(r, "Error in async function"); |
| 122 | |
Juergen Repp | add438d | 2018-04-09 10:00:19 +0200 | [diff] [blame] | 123 | /* Set the timeout to indefinite for now, since we want _Finish to block */ |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 124 | int32_t timeouttmp = esysContext->timeout; |
| 125 | esysContext->timeout = -1; |
| 126 | /* |
| 127 | * Now we call the finish function, until return code is not equal to |
| 128 | * from TSS2_BASE_RC_TRY_AGAIN. |
| 129 | * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we |
| 130 | * have set the timeout to -1. This occurs for example if the TPM requests |
| 131 | * a retransmission of the command via TPM2_RC_YIELDED. |
| 132 | */ |
| 133 | do { |
Juergen Repp | add438d | 2018-04-09 10:00:19 +0200 | [diff] [blame] | 134 | r = Esys_Rewrap_Finish(esysContext, |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 135 | outDuplicate, |
| 136 | outSymSeed); |
| 137 | /* This is just debug information about the reattempt to finish the |
| 138 | command */ |
| 139 | if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) |
| 140 | LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32 |
| 141 | " => resubmitting command", r); |
| 142 | } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN); |
| 143 | |
| 144 | /* Restore the timeout value to the original value */ |
| 145 | esysContext->timeout = timeouttmp; |
| 146 | return_if_error(r, "Esys Finish"); |
| 147 | |
| 148 | return TSS2_RC_SUCCESS; |
| 149 | } |
| 150 | |
| 151 | /** Asynchronous function for TPM2_Rewrap |
| 152 | * |
| 153 | * This function invokes the TPM2_Rewrap command in a asynchronous |
| 154 | * variant. This means the function will return as soon as the command has been |
| 155 | * sent downwards the stack to the TPM. All input parameters are const. |
Juergen Repp | add438d | 2018-04-09 10:00:19 +0200 | [diff] [blame] | 156 | * In order to retrieve the TPM's response call Esys_Rewrap_Finish. |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 157 | * |
| 158 | * @param[in,out] esysContext The ESYS_CONTEXT. |
Juergen Repp | f7e5bd3 | 2018-04-26 11:11:32 +0200 | [diff] [blame] | 159 | * @param[in] oldParent Parent of object. |
| 160 | * @param[in] newParent New parent of the object. |
| 161 | * @param[in] shandle1 Session handle for authorization of oldParent |
| 162 | * @param[in] shandle2 Second session handle. |
| 163 | * @param[in] shandle3 Third session handle. |
| 164 | * @param[in] inDuplicate An object encrypted using symmetric key derived from |
| 165 | * inSymSeed. |
| 166 | * @param[in] name The Name of the object being rewrapped. |
| 167 | * @param[in] inSymSeed The seed for the symmetric key and HMAC key. |
| 168 | * @retval ESYS_RC_SUCCESS if the function call was a success. |
| 169 | * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input |
| 170 | * pointers or required output handle references are NULL. |
| 171 | * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected. |
| 172 | * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for |
| 173 | * internal operations or return parameters. |
| 174 | * @retval TSS2_RCs produced by lower layers of the software stack may be |
| 175 | returned to the caller unaltered unless handled internally. |
| 176 | * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has |
| 177 | * the 'decrypt' attribute bit set. |
| 178 | * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has |
| 179 | * the 'encrypt' attribute bit set. |
| 180 | * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown to the |
| 181 | ESYS_CONTEXT or are of the wrong type or if required ESYS_TR objects |
| 182 | are ESYS_TR_NONE. |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 183 | */ |
| 184 | TSS2_RC |
Juergen Repp | add438d | 2018-04-09 10:00:19 +0200 | [diff] [blame] | 185 | Esys_Rewrap_Async( |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 186 | ESYS_CONTEXT *esysContext, |
| 187 | ESYS_TR oldParent, |
| 188 | ESYS_TR newParent, |
| 189 | ESYS_TR shandle1, |
| 190 | ESYS_TR shandle2, |
| 191 | ESYS_TR shandle3, |
| 192 | const TPM2B_PRIVATE *inDuplicate, |
| 193 | const TPM2B_NAME *name, |
| 194 | const TPM2B_ENCRYPTED_SECRET *inSymSeed) |
| 195 | { |
Juergen Repp | 3b7b41b | 2018-03-19 15:58:04 +0100 | [diff] [blame] | 196 | TSS2_RC r; |
Andreas Fuchs | 15bbb67 | 2018-03-27 13:21:13 +0200 | [diff] [blame] | 197 | LOG_TRACE("context=%p, oldParent=%"PRIx32 ", newParent=%"PRIx32 "," |
| 198 | "inDuplicate=%p, name=%p, inSymSeed=%p", |
| 199 | esysContext, oldParent, newParent, inDuplicate, name, |
| 200 | inSymSeed); |
Andreas Fuchs | c5a73eb | 2018-03-19 16:01:00 +0100 | [diff] [blame] | 201 | TSS2L_SYS_AUTH_COMMAND auths; |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 202 | RSRC_NODE_T *oldParentNode; |
| 203 | RSRC_NODE_T *newParentNode; |
| 204 | |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 205 | /* Check context, sequence correctness and set state to error for now */ |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 206 | if (esysContext == NULL) { |
| 207 | LOG_ERROR("esyscontext is NULL."); |
| 208 | return TSS2_ESYS_RC_BAD_REFERENCE; |
| 209 | } |
| 210 | r = iesys_check_sequence_async(esysContext); |
| 211 | if (r != TSS2_RC_SUCCESS) |
| 212 | return r; |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 213 | esysContext->state = _ESYS_STATE_INTERNALERROR; |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 214 | |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 215 | /* Check and store input parameters */ |
dantpm | f6ef247 | 2018-04-06 15:21:59 -0700 | [diff] [blame] | 216 | r = check_session_feasibility(shandle1, shandle2, shandle3, 1); |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 217 | return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage"); |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 218 | store_input_parameters(esysContext, oldParent, newParent, |
| 219 | inDuplicate, |
| 220 | name, |
| 221 | inSymSeed); |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 222 | |
| 223 | /* Retrieve the metadata objects for provided handles */ |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 224 | r = esys_GetResourceObject(esysContext, oldParent, &oldParentNode); |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 225 | return_state_if_error(r, _ESYS_STATE_INIT, "oldParent unknown."); |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 226 | r = esys_GetResourceObject(esysContext, newParent, &newParentNode); |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 227 | return_state_if_error(r, _ESYS_STATE_INIT, "newParent unknown."); |
| 228 | |
| 229 | /* Initial invocation of SAPI to prepare the command buffer with parameters */ |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 230 | r = Tss2_Sys_Rewrap_Prepare(esysContext->sys, |
| 231 | (oldParentNode == NULL) ? TPM2_RH_NULL : oldParentNode->rsrc.handle, |
| 232 | (newParentNode == NULL) ? TPM2_RH_NULL : newParentNode->rsrc.handle, |
| 233 | inDuplicate, |
| 234 | name, |
| 235 | inSymSeed); |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 236 | return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error."); |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 237 | |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 238 | /* Calculate the cpHash Values */ |
| 239 | r = init_session_tab(esysContext, shandle1, shandle2, shandle3); |
| 240 | return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources"); |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 241 | iesys_compute_session_value(esysContext->session_tab[0], |
| 242 | &oldParentNode->rsrc.name, &oldParentNode->auth); |
| 243 | iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL); |
| 244 | iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL); |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 245 | |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 246 | /* Generate the auth values and set them in the SAPI command buffer */ |
| 247 | r = iesys_gen_auths(esysContext, oldParentNode, newParentNode, NULL, &auths); |
| 248 | return_state_if_error(r, _ESYS_STATE_INIT, "Error in computation of auth values"); |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 249 | esysContext->authsCount = auths.count; |
| 250 | r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths); |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 251 | return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths"); |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 252 | |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 253 | /* Trigger execution and finish the async invocation */ |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 254 | r = Tss2_Sys_ExecuteAsync(esysContext->sys); |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 255 | return_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Finish (Execute Async)"); |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 256 | |
| 257 | esysContext->state = _ESYS_STATE_SENT; |
| 258 | |
| 259 | return r; |
| 260 | } |
| 261 | |
| 262 | /** Asynchronous finish function for TPM2_Rewrap |
| 263 | * |
| 264 | * This function returns the results of a TPM2_Rewrap command |
Juergen Repp | add438d | 2018-04-09 10:00:19 +0200 | [diff] [blame] | 265 | * invoked via Esys_Rewrap_Finish. All non-simple output parameters |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 266 | * are allocated by the function's implementation. NULL can be passed for every |
| 267 | * output parameter if the value is not required. |
| 268 | * |
| 269 | * @param[in,out] esysContext The ESYS_CONTEXT. |
Juergen Repp | f7e5bd3 | 2018-04-26 11:11:32 +0200 | [diff] [blame] | 270 | * @param[out] outDuplicate An object encrypted using symmetric key derived from |
| 271 | * outSymSeed. |
| 272 | * (callee-allocated) |
| 273 | * @param[out] outSymSeed Seed for a symmetric key protected by newParent |
| 274 | * asymmetric key. |
| 275 | * (callee-allocated) |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 276 | * @retval TSS2_RC_SUCCESS on success |
Juergen Repp | f7e5bd3 | 2018-04-26 11:11:32 +0200 | [diff] [blame] | 277 | * @retval ESYS_RC_SUCCESS if the function call was a success. |
| 278 | * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input |
| 279 | * pointers or required output handle references are NULL. |
| 280 | * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected. |
| 281 | * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for |
| 282 | * internal operations or return parameters. |
| 283 | * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous |
| 284 | * operation already pending. |
| 285 | * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the |
| 286 | * TPM response is received. |
| 287 | * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not |
| 288 | * at least contain the tag, response length, and response code. |
| 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. |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 292 | */ |
| 293 | TSS2_RC |
Juergen Repp | add438d | 2018-04-09 10:00:19 +0200 | [diff] [blame] | 294 | Esys_Rewrap_Finish( |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 295 | ESYS_CONTEXT *esysContext, |
| 296 | TPM2B_PRIVATE **outDuplicate, |
| 297 | TPM2B_ENCRYPTED_SECRET **outSymSeed) |
| 298 | { |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 299 | TSS2_RC r; |
Andreas Fuchs | 15bbb67 | 2018-03-27 13:21:13 +0200 | [diff] [blame] | 300 | LOG_TRACE("context=%p, outDuplicate=%p, outSymSeed=%p", |
| 301 | esysContext, outDuplicate, outSymSeed); |
| 302 | |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 303 | if (esysContext == NULL) { |
| 304 | LOG_ERROR("esyscontext is NULL."); |
| 305 | return TSS2_ESYS_RC_BAD_REFERENCE; |
| 306 | } |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 307 | |
| 308 | /* Check for correct sequence and set sequence to irregular for now */ |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 309 | if (esysContext->state != _ESYS_STATE_SENT) { |
| 310 | LOG_ERROR("Esys called in bad sequence."); |
| 311 | return TSS2_ESYS_RC_BAD_SEQUENCE; |
| 312 | } |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 313 | esysContext->state = _ESYS_STATE_INTERNALERROR; |
| 314 | |
| 315 | /* Allocate memory for response parameters */ |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 316 | if (outDuplicate != NULL) { |
| 317 | *outDuplicate = calloc(sizeof(TPM2B_PRIVATE), 1); |
| 318 | if (*outDuplicate == NULL) { |
| 319 | return_error(TSS2_ESYS_RC_MEMORY, "Out of memory"); |
| 320 | } |
| 321 | } |
| 322 | if (outSymSeed != NULL) { |
| 323 | *outSymSeed = calloc(sizeof(TPM2B_ENCRYPTED_SECRET), 1); |
| 324 | if (*outSymSeed == NULL) { |
| 325 | goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup); |
| 326 | } |
| 327 | } |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 328 | |
| 329 | /*Receive the TPM response and handle resubmissions if necessary. */ |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 330 | r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout); |
| 331 | if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) { |
| 332 | LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r); |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 333 | esysContext->state = _ESYS_STATE_SENT; |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 334 | goto error_cleanup; |
| 335 | } |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 336 | /* This block handle the resubmission of TPM commands given a certain set of |
| 337 | * TPM response codes. */ |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 338 | if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) { |
| 339 | LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a " |
| 340 | "resubmission: %" PRIx32, r); |
Andreas Fuchs | 631d7e6 | 2018-03-19 10:54:56 +0100 | [diff] [blame] | 341 | if (esysContext->submissionCount >= _ESYS_MAX_SUBMISSIONS) { |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 342 | LOG_WARNING("Maximum number of (re)submissions has been reached."); |
| 343 | esysContext->state = _ESYS_STATE_INIT; |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 344 | goto error_cleanup; |
| 345 | } |
| 346 | esysContext->state = _ESYS_STATE_RESUBMISSION; |
Juergen Repp | add438d | 2018-04-09 10:00:19 +0200 | [diff] [blame] | 347 | r = Esys_Rewrap_Async(esysContext, |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 348 | esysContext->in.Rewrap.oldParent, |
| 349 | esysContext->in.Rewrap.newParent, |
| 350 | esysContext->session_type[0], |
| 351 | esysContext->session_type[1], |
| 352 | esysContext->session_type[2], |
| 353 | esysContext->in.Rewrap.inDuplicate, |
| 354 | esysContext->in.Rewrap.name, |
| 355 | esysContext->in.Rewrap.inSymSeed); |
| 356 | if (r != TSS2_RC_SUCCESS) { |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 357 | LOG_WARNING("Error attempting to resubmit"); |
| 358 | /* We do not set esysContext->state here but inherit the most recent |
| 359 | * state of the _async function. */ |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 360 | goto error_cleanup; |
| 361 | } |
| 362 | r = TSS2_ESYS_RC_TRY_AGAIN; |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 363 | LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN."); |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 364 | goto error_cleanup; |
| 365 | } |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 366 | /* The following is the "regular error" handling. */ |
Juergen Repp | 033da11 | 2018-07-10 13:06:18 +0200 | [diff] [blame^] | 367 | if (iesys_tpm_error(r)) { |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 368 | LOG_WARNING("Received TPM Error"); |
| 369 | esysContext->state = _ESYS_STATE_INIT; |
| 370 | goto error_cleanup; |
| 371 | } else if (r != TSS2_RC_SUCCESS) { |
| 372 | LOG_ERROR("Received a non-TPM Error"); |
| 373 | esysContext->state = _ESYS_STATE_INTERNALERROR; |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 374 | goto error_cleanup; |
| 375 | } |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 376 | |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 377 | /* |
| 378 | * Now the verification of the response (hmac check) and if necessary the |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 379 | * parameter decryption have to be done. |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 380 | */ |
Juergen Repp | 109de7d | 2018-02-23 13:42:08 +0100 | [diff] [blame] | 381 | r = iesys_check_response(esysContext); |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 382 | goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response", |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 383 | error_cleanup); |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 384 | /* |
| 385 | * After the verification of the response we call the complete function |
| 386 | * to deliver the result. |
| 387 | */ |
| 388 | r = Tss2_Sys_Rewrap_Complete(esysContext->sys, |
| 389 | (outDuplicate != NULL) ? *outDuplicate : NULL, |
| 390 | (outSymSeed != NULL) ? *outSymSeed : NULL); |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 391 | goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Received error from SAPI" |
dantpm | f6ef247 | 2018-04-06 15:21:59 -0700 | [diff] [blame] | 392 | " unmarshaling" ,error_cleanup); |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 393 | esysContext->state = _ESYS_STATE_INIT; |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 394 | |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 395 | return TSS2_RC_SUCCESS; |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 396 | |
| 397 | error_cleanup: |
| 398 | if (outDuplicate != NULL) |
| 399 | SAFE_FREE(*outDuplicate); |
| 400 | if (outSymSeed != NULL) |
| 401 | SAFE_FREE(*outSymSeed); |
Andreas Fuchs | 4d9961e | 2018-03-20 15:51:48 +0100 | [diff] [blame] | 402 | |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 403 | return r; |
Philip Tricca | 910f17c | 2018-03-15 12:38:37 -0700 | [diff] [blame] | 404 | } |