1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*******************************************************************************
3 * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
4 * All rights reserved.
5 ******************************************************************************/
6
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include "tss2_mu.h"
12 #include "tss2_sys.h"
13 #include "tss2_esys.h"
14
15 #include "esys_types.h"
16 #include "esys_iutil.h"
17 #include "esys_mu.h"
18 #define LOGMODULE esys
19 #include "util/log.h"
20 #include "util/aux_util.h"
21
22 /** One-Call function for TPM2_RSA_Encrypt
23 *
24 * This function invokes the TPM2_RSA_Encrypt command in a one-call
25 * variant. This means the function will block until the TPM response is
26 * available. All input parameters are const. The memory for non-simple output
27 * parameters is allocated by the function implementation.
28 *
29 * @param[in,out] esysContext The ESYS_CONTEXT.
30 * @param[in] keyHandle Reference to public portion of RSA key to use for
31 * encryption.
32 * @param[in] shandle1 First session handle.
33 * @param[in] shandle2 Second session handle.
34 * @param[in] shandle3 Third session handle.
35 * @param[in] message Message to be encrypted.
36 * @param[in] inScheme TPM2_The padding scheme to use if scheme associated with
37 * keyHandle is TPM2_ALG_NULL.
38 * @param[in] label Optional label L to be associated with the message.
39 * @param[out] outData Encrypted output.
40 * (callee-allocated)
41 * @retval TSS2_RC_SUCCESS if the function call was a success.
42 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
43 * pointers or required output handle references are NULL.
44 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
45 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
46 * internal operations or return parameters.
47 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
48 * operation already pending.
49 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
50 * at least contain the tag, response length, and response code.
51 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
52 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
53 did not verify.
54 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
55 * the 'decrypt' attribute bit set.
56 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
57 * the 'encrypt' attribute bit set.
58 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
59 * to the ESYS_CONTEXT or are of the wrong type or if required
60 * ESYS_TR objects are ESYS_TR_NONE.
61 * @retval TSS2_RCs produced by lower layers of the software stack may be
62 * returned to the caller unaltered unless handled internally.
63 */
64 TSS2_RC
Esys_RSA_Encrypt(ESYS_CONTEXT * esysContext,ESYS_TR keyHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_PUBLIC_KEY_RSA * message,const TPMT_RSA_DECRYPT * inScheme,const TPM2B_DATA * label,TPM2B_PUBLIC_KEY_RSA ** outData)65 Esys_RSA_Encrypt(
66 ESYS_CONTEXT *esysContext,
67 ESYS_TR keyHandle,
68 ESYS_TR shandle1,
69 ESYS_TR shandle2,
70 ESYS_TR shandle3,
71 const TPM2B_PUBLIC_KEY_RSA *message,
72 const TPMT_RSA_DECRYPT *inScheme,
73 const TPM2B_DATA *label,
74 TPM2B_PUBLIC_KEY_RSA **outData)
75 {
76 TSS2_RC r;
77
78 r = Esys_RSA_Encrypt_Async(esysContext, keyHandle, shandle1, shandle2,
79 shandle3, message, inScheme, label);
80 return_if_error(r, "Error in async function");
81
82 /* Set the timeout to indefinite for now, since we want _Finish to block */
83 int32_t timeouttmp = esysContext->timeout;
84 esysContext->timeout = -1;
85 /*
86 * Now we call the finish function, until return code is not equal to
87 * from TSS2_BASE_RC_TRY_AGAIN.
88 * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
89 * have set the timeout to -1. This occurs for example if the TPM requests
90 * a retransmission of the command via TPM2_RC_YIELDED.
91 */
92 do {
93 r = Esys_RSA_Encrypt_Finish(esysContext, outData);
94 /* This is just debug information about the reattempt to finish the
95 command */
96 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
97 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
98 " => resubmitting command", r);
99 } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
100
101 /* Restore the timeout value to the original value */
102 esysContext->timeout = timeouttmp;
103 return_if_error(r, "Esys Finish");
104
105 return TSS2_RC_SUCCESS;
106 }
107
108 /** Asynchronous function for TPM2_RSA_Encrypt
109 *
110 * This function invokes the TPM2_RSA_Encrypt command in a asynchronous
111 * variant. This means the function will return as soon as the command has been
112 * sent downwards the stack to the TPM. All input parameters are const.
113 * In order to retrieve the TPM's response call Esys_RSA_Encrypt_Finish.
114 *
115 * @param[in,out] esysContext The ESYS_CONTEXT.
116 * @param[in] keyHandle Reference to public portion of RSA key to use for
117 * encryption.
118 * @param[in] shandle1 First session handle.
119 * @param[in] shandle2 Second session handle.
120 * @param[in] shandle3 Third session handle.
121 * @param[in] message Message to be encrypted.
122 * @param[in] inScheme TPM2_The padding scheme to use if scheme associated with
123 * keyHandle is TPM2_ALG_NULL.
124 * @param[in] label Optional label L to be associated with the message.
125 * @retval ESYS_RC_SUCCESS if the function call was a success.
126 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
127 * pointers or required output handle references are NULL.
128 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
129 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
130 * internal operations or return parameters.
131 * @retval TSS2_RCs produced by lower layers of the software stack may be
132 returned to the caller unaltered unless handled internally.
133 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
134 * the 'decrypt' attribute bit set.
135 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
136 * the 'encrypt' attribute bit set.
137 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
138 * to the ESYS_CONTEXT or are of the wrong type or if required
139 * ESYS_TR objects are ESYS_TR_NONE.
140 */
141 TSS2_RC
Esys_RSA_Encrypt_Async(ESYS_CONTEXT * esysContext,ESYS_TR keyHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_PUBLIC_KEY_RSA * message,const TPMT_RSA_DECRYPT * inScheme,const TPM2B_DATA * label)142 Esys_RSA_Encrypt_Async(
143 ESYS_CONTEXT *esysContext,
144 ESYS_TR keyHandle,
145 ESYS_TR shandle1,
146 ESYS_TR shandle2,
147 ESYS_TR shandle3,
148 const TPM2B_PUBLIC_KEY_RSA *message,
149 const TPMT_RSA_DECRYPT *inScheme,
150 const TPM2B_DATA *label)
151 {
152 TSS2_RC r;
153 LOG_TRACE("context=%p, keyHandle=%"PRIx32 ", message=%p,"
154 "inScheme=%p, label=%p",
155 esysContext, keyHandle, message, inScheme, label);
156 TSS2L_SYS_AUTH_COMMAND auths;
157 RSRC_NODE_T *keyHandleNode;
158
159 /* Check context, sequence correctness and set state to error for now */
160 if (esysContext == NULL) {
161 LOG_ERROR("esyscontext is NULL.");
162 return TSS2_ESYS_RC_BAD_REFERENCE;
163 }
164 r = iesys_check_sequence_async(esysContext);
165 if (r != TSS2_RC_SUCCESS)
166 return r;
167 esysContext->state = _ESYS_STATE_INTERNALERROR;
168
169 /* Check input parameters */
170 r = check_session_feasibility(shandle1, shandle2, shandle3, 0);
171 return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
172
173 /* Retrieve the metadata objects for provided handles */
174 r = esys_GetResourceObject(esysContext, keyHandle, &keyHandleNode);
175 return_state_if_error(r, _ESYS_STATE_INIT, "keyHandle unknown.");
176
177 /* Initial invocation of SAPI to prepare the command buffer with parameters */
178 r = Tss2_Sys_RSA_Encrypt_Prepare(esysContext->sys,
179 (keyHandleNode == NULL) ? TPM2_RH_NULL
180 : keyHandleNode->rsrc.handle, message,
181 inScheme, label);
182 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
183
184 /* Calculate the cpHash Values */
185 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
186 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
187 iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
188 iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
189 iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
190
191 /* Generate the auth values and set them in the SAPI command buffer */
192 r = iesys_gen_auths(esysContext, keyHandleNode, NULL, NULL, &auths);
193 return_state_if_error(r, _ESYS_STATE_INIT,
194 "Error in computation of auth values");
195
196 esysContext->authsCount = auths.count;
197 if (auths.count > 0) {
198 r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
199 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
200 }
201
202 /* Trigger execution and finish the async invocation */
203 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
204 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
205 "Finish (Execute Async)");
206
207 esysContext->state = _ESYS_STATE_SENT;
208
209 return r;
210 }
211
212 /** Asynchronous finish function for TPM2_RSA_Encrypt
213 *
214 * This function returns the results of a TPM2_RSA_Encrypt command
215 * invoked via Esys_RSA_Encrypt_Finish. All non-simple output parameters
216 * are allocated by the function's implementation. NULL can be passed for every
217 * output parameter if the value is not required.
218 *
219 * @param[in,out] esysContext The ESYS_CONTEXT.
220 * @param[out] outData Encrypted output.
221 * (callee-allocated)
222 * @retval TSS2_RC_SUCCESS on success
223 * @retval ESYS_RC_SUCCESS if the function call was a success.
224 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
225 * pointers or required output handle references are NULL.
226 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
227 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
228 * internal operations or return parameters.
229 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
230 * operation already pending.
231 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
232 * TPM response is received.
233 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
234 * at least contain the tag, response length, and response code.
235 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
236 * not verify.
237 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
238 * @retval TSS2_RCs produced by lower layers of the software stack may be
239 * returned to the caller unaltered unless handled internally.
240 */
241 TSS2_RC
Esys_RSA_Encrypt_Finish(ESYS_CONTEXT * esysContext,TPM2B_PUBLIC_KEY_RSA ** outData)242 Esys_RSA_Encrypt_Finish(
243 ESYS_CONTEXT *esysContext,
244 TPM2B_PUBLIC_KEY_RSA **outData)
245 {
246 TSS2_RC r;
247 LOG_TRACE("context=%p, outData=%p",
248 esysContext, outData);
249
250 if (esysContext == NULL) {
251 LOG_ERROR("esyscontext is NULL.");
252 return TSS2_ESYS_RC_BAD_REFERENCE;
253 }
254
255 /* Check for correct sequence and set sequence to irregular for now */
256 if (esysContext->state != _ESYS_STATE_SENT &&
257 esysContext->state != _ESYS_STATE_RESUBMISSION) {
258 LOG_ERROR("Esys called in bad sequence.");
259 return TSS2_ESYS_RC_BAD_SEQUENCE;
260 }
261 esysContext->state = _ESYS_STATE_INTERNALERROR;
262
263 /* Allocate memory for response parameters */
264 if (outData != NULL) {
265 *outData = calloc(sizeof(TPM2B_PUBLIC_KEY_RSA), 1);
266 if (*outData == NULL) {
267 return_error(TSS2_ESYS_RC_MEMORY, "Out of memory");
268 }
269 }
270
271 /*Receive the TPM response and handle resubmissions if necessary. */
272 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
273 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
274 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
275 esysContext->state = _ESYS_STATE_SENT;
276 goto error_cleanup;
277 }
278 /* This block handle the resubmission of TPM commands given a certain set of
279 * TPM response codes. */
280 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
281 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
282 "resubmission: %" PRIx32, r);
283 if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
284 LOG_WARNING("Maximum number of (re)submissions has been reached.");
285 esysContext->state = _ESYS_STATE_INIT;
286 goto error_cleanup;
287 }
288 esysContext->state = _ESYS_STATE_RESUBMISSION;
289 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
290 if (r != TSS2_RC_SUCCESS) {
291 LOG_WARNING("Error attempting to resubmit");
292 /* We do not set esysContext->state here but inherit the most recent
293 * state of the _async function. */
294 goto error_cleanup;
295 }
296 r = TSS2_ESYS_RC_TRY_AGAIN;
297 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
298 goto error_cleanup;
299 }
300 /* The following is the "regular error" handling. */
301 if (iesys_tpm_error(r)) {
302 LOG_WARNING("Received TPM Error");
303 esysContext->state = _ESYS_STATE_INIT;
304 goto error_cleanup;
305 } else if (r != TSS2_RC_SUCCESS) {
306 LOG_ERROR("Received a non-TPM Error");
307 esysContext->state = _ESYS_STATE_INTERNALERROR;
308 goto error_cleanup;
309 }
310
311 /*
312 * Now the verification of the response (hmac check) and if necessary the
313 * parameter decryption have to be done.
314 */
315 r = iesys_check_response(esysContext);
316 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
317 error_cleanup);
318
319 /*
320 * After the verification of the response we call the complete function
321 * to deliver the result.
322 */
323 r = Tss2_Sys_RSA_Encrypt_Complete(esysContext->sys,
324 (outData != NULL) ? *outData : NULL);
325 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
326 "Received error from SAPI unmarshaling" ,
327 error_cleanup);
328
329 esysContext->state = _ESYS_STATE_INIT;
330
331 return TSS2_RC_SUCCESS;
332
333 error_cleanup:
334 if (outData != NULL)
335 SAFE_FREE(*outData);
336
337 return r;
338 }
339