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