xref: /optee_os/ta/pkcs11/src/processing.c (revision e5353ad98910f0cfc5cb867a51e6d6814b8a2e05)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2017-2020, Linaro Limited
4  */
5 
6 #include <assert.h>
7 #include <pkcs11_ta.h>
8 #include <string.h>
9 #include <tee_api_defines.h>
10 #include <tee_internal_api.h>
11 #include <tee_internal_api_extensions.h>
12 #include <util.h>
13 
14 #include "attributes.h"
15 #include "object.h"
16 #include "pkcs11_attributes.h"
17 #include "pkcs11_helpers.h"
18 #include "pkcs11_token.h"
19 #include "processing.h"
20 #include "serializer.h"
21 
22 static enum pkcs11_rc get_ready_session(struct pkcs11_session *session)
23 {
24 	if (session_is_active(session))
25 		return PKCS11_CKR_OPERATION_ACTIVE;
26 
27 	return PKCS11_CKR_OK;
28 }
29 
30 static enum processing_func func_for_cmd(enum pkcs11_ta_cmd cmd)
31 {
32 	switch (cmd) {
33 	case PKCS11_CMD_ENCRYPT_UPDATE:
34 	case PKCS11_CMD_ENCRYPT_ONESHOT:
35 	case PKCS11_CMD_ENCRYPT_FINAL:
36 		return PKCS11_FUNCTION_ENCRYPT;
37 	case PKCS11_CMD_DECRYPT_UPDATE:
38 	case PKCS11_CMD_DECRYPT_ONESHOT:
39 	case PKCS11_CMD_DECRYPT_FINAL:
40 		return PKCS11_FUNCTION_DECRYPT;
41 	case PKCS11_CMD_SIGN_ONESHOT:
42 	case PKCS11_CMD_SIGN_UPDATE:
43 	case PKCS11_CMD_SIGN_FINAL:
44 		return PKCS11_FUNCTION_SIGN;
45 	case PKCS11_CMD_VERIFY_ONESHOT:
46 	case PKCS11_CMD_VERIFY_UPDATE:
47 	case PKCS11_CMD_VERIFY_FINAL:
48 		return PKCS11_FUNCTION_VERIFY;
49 	case PKCS11_CMD_DIGEST_UPDATE:
50 	case PKCS11_CMD_DIGEST_KEY:
51 	case PKCS11_CMD_DIGEST_ONESHOT:
52 	case PKCS11_CMD_DIGEST_FINAL:
53 		return PKCS11_FUNCTION_DIGEST;
54 	default:
55 		return PKCS11_FUNCTION_UNKNOWN;
56 	}
57 }
58 
59 static bool func_matches_state(enum processing_func function,
60 			       enum pkcs11_proc_state state)
61 {
62 	switch (function) {
63 	case PKCS11_FUNCTION_ENCRYPT:
64 		return state == PKCS11_SESSION_ENCRYPTING ||
65 		       state == PKCS11_SESSION_DIGESTING_ENCRYPTING ||
66 		       state == PKCS11_SESSION_SIGNING_ENCRYPTING;
67 	case PKCS11_FUNCTION_DECRYPT:
68 		return state == PKCS11_SESSION_DECRYPTING ||
69 		       state == PKCS11_SESSION_DECRYPTING_DIGESTING ||
70 		       state == PKCS11_SESSION_DECRYPTING_VERIFYING;
71 	case PKCS11_FUNCTION_DIGEST:
72 		return state == PKCS11_SESSION_DIGESTING ||
73 		       state == PKCS11_SESSION_DIGESTING_ENCRYPTING;
74 	case PKCS11_FUNCTION_SIGN:
75 		return state == PKCS11_SESSION_SIGNING ||
76 		       state == PKCS11_SESSION_SIGNING_ENCRYPTING;
77 	case PKCS11_FUNCTION_VERIFY:
78 		return state == PKCS11_SESSION_VERIFYING ||
79 		       state == PKCS11_SESSION_DECRYPTING_VERIFYING;
80 	case PKCS11_FUNCTION_SIGN_RECOVER:
81 		return state == PKCS11_SESSION_SIGNING_RECOVER;
82 	case PKCS11_FUNCTION_VERIFY_RECOVER:
83 		return state == PKCS11_SESSION_SIGNING_RECOVER;
84 	default:
85 		TEE_Panic(function);
86 		return false;
87 	}
88 }
89 
90 static enum pkcs11_rc get_active_session(struct pkcs11_session *session,
91 					 enum processing_func function)
92 {
93 	enum pkcs11_rc rc = PKCS11_CKR_OPERATION_NOT_INITIALIZED;
94 
95 	if (session->processing &&
96 	    func_matches_state(function, session->processing->state))
97 		rc = PKCS11_CKR_OK;
98 
99 	return rc;
100 }
101 
102 void release_active_processing(struct pkcs11_session *session)
103 {
104 	if (!session->processing)
105 		return;
106 
107 	if (session->processing->tee_hash_op_handle != TEE_HANDLE_NULL) {
108 		TEE_FreeOperation(session->processing->tee_hash_op_handle);
109 		session->processing->tee_hash_op_handle = TEE_HANDLE_NULL;
110 		session->processing->tee_hash_algo = 0;
111 	}
112 
113 	if (session->processing->tee_op_handle != TEE_HANDLE_NULL) {
114 		TEE_FreeOperation(session->processing->tee_op_handle);
115 		session->processing->tee_op_handle = TEE_HANDLE_NULL;
116 	}
117 
118 	TEE_Free(session->processing->extra_ctx);
119 
120 	TEE_Free(session->processing);
121 	session->processing = NULL;
122 }
123 
124 size_t get_object_key_bit_size(struct pkcs11_object *obj)
125 {
126 	void *a_ptr = NULL;
127 	uint32_t a_size = 0;
128 	struct obj_attrs *attrs = obj->attributes;
129 
130 	switch (get_key_type(attrs)) {
131 	case PKCS11_CKK_AES:
132 	case PKCS11_CKK_GENERIC_SECRET:
133 	case PKCS11_CKK_MD5_HMAC:
134 	case PKCS11_CKK_SHA_1_HMAC:
135 	case PKCS11_CKK_SHA224_HMAC:
136 	case PKCS11_CKK_SHA256_HMAC:
137 	case PKCS11_CKK_SHA384_HMAC:
138 	case PKCS11_CKK_SHA512_HMAC:
139 		if (get_attribute_ptr(attrs, PKCS11_CKA_VALUE, NULL, &a_size))
140 			return 0;
141 
142 		return a_size * 8;
143 	case PKCS11_CKK_RSA:
144 		if (get_attribute_ptr(attrs, PKCS11_CKA_MODULUS, NULL, &a_size))
145 			return 0;
146 
147 		return a_size * 8;
148 	case PKCS11_CKK_EC:
149 		if (get_attribute_ptr(attrs, PKCS11_CKA_EC_PARAMS,
150 				      &a_ptr, &a_size) || !a_ptr)
151 			return 0;
152 
153 		return ec_params2tee_keysize(a_ptr, a_size);
154 	case PKCS11_CKK_EC_EDWARDS:
155 		if (get_attribute_ptr(attrs, PKCS11_CKA_EC_POINT, NULL,
156 				      &a_size))
157 			return 0;
158 
159 		return a_size * 8;
160 	default:
161 		TEE_Panic(0);
162 		return 0;
163 	}
164 }
165 
166 static enum pkcs11_rc generate_random_key_value(struct obj_attrs **head)
167 {
168 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
169 	void *data = NULL;
170 	uint32_t data_size = 0;
171 	uint32_t value_len = 0;
172 	void *value = NULL;
173 
174 	if (!*head)
175 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
176 
177 	rc = get_attribute_ptr(*head, PKCS11_CKA_VALUE_LEN, &data, &data_size);
178 	if (rc || data_size != sizeof(uint32_t)) {
179 		DMSG("%s", rc ? "No attribute value_len found" :
180 		     "Invalid size for attribute VALUE_LEN");
181 
182 		return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID;
183 	}
184 	TEE_MemMove(&value_len, data, data_size);
185 
186 	/* Remove the default empty value attribute if found */
187 	rc = remove_empty_attribute(head, PKCS11_CKA_VALUE);
188 	if (rc != PKCS11_CKR_OK && rc != PKCS11_RV_NOT_FOUND)
189 		return PKCS11_CKR_GENERAL_ERROR;
190 
191 	value = TEE_Malloc(value_len, TEE_USER_MEM_HINT_NO_FILL_ZERO);
192 	if (!value)
193 		return PKCS11_CKR_DEVICE_MEMORY;
194 
195 	TEE_GenerateRandom(value, value_len);
196 
197 	rc = add_attribute(head, PKCS11_CKA_VALUE, value, value_len);
198 
199 	TEE_Free(value);
200 
201 	return rc;
202 }
203 
204 enum pkcs11_rc entry_generate_secret(struct pkcs11_client *client,
205 				     uint32_t ptypes, TEE_Param *params)
206 {
207 	const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
208 						TEE_PARAM_TYPE_NONE,
209 						TEE_PARAM_TYPE_MEMREF_OUTPUT,
210 						TEE_PARAM_TYPE_NONE);
211 	TEE_Param *ctrl = params;
212 	TEE_Param *out = params + 2;
213 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
214 	struct serialargs ctrlargs = { };
215 	struct pkcs11_session *session = NULL;
216 	struct pkcs11_attribute_head *proc_params = NULL;
217 	struct obj_attrs *head = NULL;
218 	struct pkcs11_object_head *template = NULL;
219 	size_t template_size = 0;
220 	uint32_t obj_handle = 0;
221 
222 	if (!client || ptypes != exp_pt ||
223 	    out->memref.size != sizeof(obj_handle))
224 		return PKCS11_CKR_ARGUMENTS_BAD;
225 
226 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
227 
228 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
229 	if (rc)
230 		return rc;
231 
232 	rc = serialargs_alloc_get_one_attribute(&ctrlargs, &proc_params);
233 	if (rc)
234 		goto out;
235 
236 	rc = serialargs_alloc_get_attributes(&ctrlargs, &template);
237 	if (rc)
238 		goto out;
239 
240 	if (serialargs_remaining_bytes(&ctrlargs)) {
241 		rc = PKCS11_CKR_ARGUMENTS_BAD;
242 		goto out;
243 	}
244 
245 	rc = get_ready_session(session);
246 	if (rc)
247 		goto out;
248 
249 	template_size = sizeof(*template) + template->attrs_size;
250 
251 	rc = check_mechanism_against_processing(session, proc_params->id,
252 						PKCS11_FUNCTION_GENERATE,
253 						PKCS11_FUNC_STEP_INIT);
254 	if (rc) {
255 		DMSG("Invalid mechanism %#"PRIx32": %#x", proc_params->id, rc);
256 		goto out;
257 	}
258 
259 	/*
260 	 * Prepare a clean initial state for the requested object attributes.
261 	 * Free temporary template once done.
262 	 */
263 	rc = create_attributes_from_template(&head, template, template_size,
264 					     NULL, PKCS11_FUNCTION_GENERATE,
265 					     proc_params->id,
266 					     PKCS11_CKO_UNDEFINED_ID);
267 	if (rc)
268 		goto out;
269 
270 	TEE_Free(template);
271 	template = NULL;
272 
273 	rc = check_created_attrs(head, NULL);
274 	if (rc)
275 		goto out;
276 
277 	rc = check_created_attrs_against_processing(proc_params->id, head);
278 	if (rc)
279 		goto out;
280 
281 	rc = check_created_attrs_against_token(session, head);
282 	if (rc)
283 		goto out;
284 
285 	/*
286 	 * Execute target processing and add value as attribute
287 	 * PKCS11_CKA_VALUE. Symm key generation: depends on target
288 	 * processing to be used.
289 	 */
290 	switch (proc_params->id) {
291 	case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
292 	case PKCS11_CKM_AES_KEY_GEN:
293 		/* Generate random of size specified by attribute VALUE_LEN */
294 		rc = generate_random_key_value(&head);
295 		if (rc)
296 			goto out;
297 		break;
298 
299 	default:
300 		rc = PKCS11_CKR_MECHANISM_INVALID;
301 		goto out;
302 	}
303 
304 	TEE_Free(proc_params);
305 	proc_params = NULL;
306 
307 	/*
308 	 * Object is ready, register it and return a handle.
309 	 */
310 	rc = create_object(session, head, &obj_handle);
311 	if (rc)
312 		goto out;
313 
314 	/*
315 	 * Now obj_handle (through the related struct pkcs11_object instance)
316 	 * owns the serialized buffer that holds the object attributes.
317 	 * We reset head to NULL as it is no more the buffer owner and would
318 	 * be freed at function out.
319 	 */
320 	head = NULL;
321 
322 	TEE_MemMove(out->memref.buffer, &obj_handle, sizeof(obj_handle));
323 	out->memref.size = sizeof(obj_handle);
324 
325 	DMSG("PKCS11 session %"PRIu32": generate secret %#"PRIx32,
326 	     session->handle, obj_handle);
327 
328 out:
329 	TEE_Free(proc_params);
330 	TEE_Free(template);
331 	TEE_Free(head);
332 
333 	return rc;
334 }
335 
336 enum pkcs11_rc alloc_get_tee_attribute_data(TEE_ObjectHandle tee_obj,
337 					    uint32_t attribute,
338 					    void **data, size_t *size)
339 {
340 	TEE_Result res = TEE_ERROR_GENERIC;
341 	void *ptr = NULL;
342 	uint32_t sz = 0;
343 
344 	res = TEE_GetObjectBufferAttribute(tee_obj, attribute, NULL, &sz);
345 	if (res != TEE_ERROR_SHORT_BUFFER)
346 		return PKCS11_CKR_FUNCTION_FAILED;
347 
348 	ptr = TEE_Malloc(sz, TEE_USER_MEM_HINT_NO_FILL_ZERO);
349 	if (!ptr)
350 		return PKCS11_CKR_DEVICE_MEMORY;
351 
352 	res = TEE_GetObjectBufferAttribute(tee_obj, attribute, ptr, &sz);
353 	if (res) {
354 		TEE_Free(ptr);
355 	} else {
356 		*data = ptr;
357 		*size = sz;
358 	}
359 
360 	return tee2pkcs_error(res);
361 }
362 
363 enum pkcs11_rc tee2pkcs_add_attribute(struct obj_attrs **head,
364 				      uint32_t pkcs11_id,
365 				      TEE_ObjectHandle tee_obj,
366 				      uint32_t tee_id)
367 {
368 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
369 	void *a_ptr = NULL;
370 	size_t a_size = 0;
371 
372 	rc = alloc_get_tee_attribute_data(tee_obj, tee_id, &a_ptr, &a_size);
373 	if (rc)
374 		goto out;
375 
376 	rc = add_attribute(head, pkcs11_id, a_ptr, a_size);
377 
378 	TEE_Free(a_ptr);
379 
380 out:
381 	if (rc)
382 		EMSG("Failed TEE attribute %#"PRIx32" for %#"PRIx32"/%s",
383 		     tee_id, pkcs11_id, id2str_attr(pkcs11_id));
384 	return rc;
385 }
386 
387 enum pkcs11_rc entry_generate_key_pair(struct pkcs11_client *client,
388 				       uint32_t ptypes, TEE_Param *params)
389 {
390 	const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
391 						TEE_PARAM_TYPE_NONE,
392 						TEE_PARAM_TYPE_MEMREF_OUTPUT,
393 						TEE_PARAM_TYPE_NONE);
394 	TEE_Param *ctrl = params;
395 	TEE_Param *out = params + 2;
396 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
397 	struct serialargs ctrlargs = { };
398 	struct pkcs11_session *session = NULL;
399 	struct pkcs11_attribute_head *proc_params = NULL;
400 	struct obj_attrs *pub_head = NULL;
401 	struct obj_attrs *priv_head = NULL;
402 	struct pkcs11_object_head *pub_template = NULL;
403 	struct pkcs11_object_head *priv_template = NULL;
404 	struct pkcs11_object *object = NULL;
405 	size_t pub_template_size = 0;
406 	size_t priv_template_size = 0;
407 	uint32_t pubkey_handle = 0;
408 	uint32_t privkey_handle = 0;
409 	uint32_t *hdl_ptr = NULL;
410 	size_t out_ref_size = sizeof(pubkey_handle) + sizeof(privkey_handle);
411 
412 	if (!client || ptypes != exp_pt || out->memref.size != out_ref_size)
413 		return PKCS11_CKR_ARGUMENTS_BAD;
414 
415 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
416 
417 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
418 	if (rc)
419 		return rc;
420 
421 	rc = serialargs_alloc_get_one_attribute(&ctrlargs, &proc_params);
422 	if (rc)
423 		goto out;
424 
425 	rc = serialargs_alloc_get_attributes(&ctrlargs, &pub_template);
426 	if (rc)
427 		goto out;
428 
429 	rc = serialargs_alloc_get_attributes(&ctrlargs, &priv_template);
430 	if (rc)
431 		goto out;
432 
433 	if (serialargs_remaining_bytes(&ctrlargs)) {
434 		rc = PKCS11_CKR_ARGUMENTS_BAD;
435 		goto out;
436 	}
437 
438 	rc = get_ready_session(session);
439 	if (rc)
440 		goto out;
441 
442 	rc = check_mechanism_against_processing(session, proc_params->id,
443 						PKCS11_FUNCTION_GENERATE_PAIR,
444 						PKCS11_FUNC_STEP_INIT);
445 	if (rc)
446 		goto out;
447 
448 	pub_template_size = sizeof(*pub_template) + pub_template->attrs_size;
449 
450 	rc = create_attributes_from_template(&pub_head, pub_template,
451 					     pub_template_size, NULL,
452 					     PKCS11_FUNCTION_GENERATE_PAIR,
453 					     proc_params->id,
454 					     PKCS11_CKO_PUBLIC_KEY);
455 	if (rc)
456 		goto out;
457 
458 	TEE_Free(pub_template);
459 	pub_template = NULL;
460 
461 	priv_template_size = sizeof(*priv_template) +
462 			     priv_template->attrs_size;
463 
464 	rc = create_attributes_from_template(&priv_head, priv_template,
465 					     priv_template_size, NULL,
466 					     PKCS11_FUNCTION_GENERATE_PAIR,
467 					     proc_params->id,
468 					     PKCS11_CKO_PRIVATE_KEY);
469 	if (rc)
470 		goto out;
471 
472 	TEE_Free(priv_template);
473 	priv_template = NULL;
474 
475 	/* Generate CKA_ID for keys if not specified by the templates */
476 	rc = add_missing_attribute_id(&pub_head, &priv_head);
477 	if (rc)
478 		goto out;
479 
480 	/* Check created object against processing and token state */
481 	rc = check_created_attrs(pub_head, priv_head);
482 	if (rc)
483 		goto out;
484 
485 	rc = check_created_attrs_against_processing(proc_params->id, pub_head);
486 	if (rc)
487 		goto out;
488 
489 	rc = check_created_attrs_against_processing(proc_params->id,
490 						    priv_head);
491 	if (rc)
492 		goto out;
493 
494 	rc = check_created_attrs_against_token(session, pub_head);
495 	if (rc)
496 		goto out;
497 
498 	rc = check_access_attrs_against_token(session, pub_head);
499 	if (rc)
500 		goto out;
501 
502 	rc = check_created_attrs_against_token(session, priv_head);
503 	if (rc)
504 		goto out;
505 
506 	rc = check_access_attrs_against_token(session, priv_head);
507 	if (rc)
508 		goto out;
509 
510 	/* Generate key pair */
511 	switch (proc_params->id) {
512 	case PKCS11_CKM_EC_EDWARDS_KEY_PAIR_GEN:
513 		rc = generate_eddsa_keys(proc_params, &pub_head, &priv_head);
514 		break;
515 	case PKCS11_CKM_EC_KEY_PAIR_GEN:
516 		rc = generate_ec_keys(proc_params, &pub_head, &priv_head);
517 		break;
518 	case PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN:
519 		rc = generate_rsa_keys(proc_params, &pub_head, &priv_head);
520 		break;
521 	default:
522 		rc = PKCS11_CKR_MECHANISM_INVALID;
523 		break;
524 	}
525 	if (rc)
526 		goto out;
527 
528 	TEE_Free(proc_params);
529 	proc_params = NULL;
530 
531 	/*
532 	 * Object is ready, register it and return a handle.
533 	 */
534 	rc = create_object(session, pub_head, &pubkey_handle);
535 	if (rc)
536 		goto out;
537 
538 	/*
539 	 * Now obj_handle (through the related struct pkcs11_object instance)
540 	 * owns the serialized buffer that holds the object attributes.
541 	 * We reset local pub_head to NULL to mark that ownership has been
542 	 * transferred.
543 	 */
544 	pub_head = NULL;
545 
546 	rc = create_object(session, priv_head, &privkey_handle);
547 	if (rc)
548 		goto out;
549 
550 	/* Ownership has been transferred so mark it with NULL */
551 	priv_head = NULL;
552 
553 	hdl_ptr = (uint32_t *)out->memref.buffer;
554 
555 	TEE_MemMove(hdl_ptr, &pubkey_handle, sizeof(pubkey_handle));
556 	TEE_MemMove(hdl_ptr + 1, &privkey_handle, sizeof(privkey_handle));
557 
558 	DMSG("PKCS11 session %"PRIu32": create key pair %#"PRIx32"/%#"PRIx32,
559 	     session->handle, privkey_handle, pubkey_handle);
560 
561 	pubkey_handle = 0;
562 	privkey_handle = 0;
563 out:
564 	if (pubkey_handle) {
565 		object = pkcs11_handle2object(pubkey_handle, session);
566 		if (!object)
567 			TEE_Panic(0);
568 		destroy_object(session, object, false);
569 	}
570 	TEE_Free(priv_head);
571 	TEE_Free(pub_head);
572 	TEE_Free(priv_template);
573 	TEE_Free(pub_template);
574 	TEE_Free(proc_params);
575 
576 	return rc;
577 }
578 
579 /*
580  * entry_processing_init - Generic entry for initializing a processing
581  *
582  * @client = client reference
583  * @ptype = Invocation parameter types
584  * @params = Invocation parameters reference
585  * @function - encrypt, decrypt, sign, verify, digest, ...
586  */
587 enum pkcs11_rc entry_processing_init(struct pkcs11_client *client,
588 				     uint32_t ptypes, TEE_Param *params,
589 				     enum processing_func function)
590 {
591 	const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
592 						TEE_PARAM_TYPE_NONE,
593 						TEE_PARAM_TYPE_NONE,
594 						TEE_PARAM_TYPE_NONE);
595 	TEE_Param *ctrl = params;
596 	enum pkcs11_rc rc = PKCS11_CKR_OK;
597 	struct serialargs ctrlargs = { };
598 	struct pkcs11_session *session = NULL;
599 	struct pkcs11_attribute_head *proc_params = NULL;
600 	uint32_t key_handle = 0;
601 	struct pkcs11_object *obj = NULL;
602 
603 	if (!client || ptypes != exp_pt)
604 		return PKCS11_CKR_ARGUMENTS_BAD;
605 
606 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
607 
608 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
609 	if (rc)
610 		return rc;
611 
612 	if (function != PKCS11_FUNCTION_DIGEST) {
613 		rc = serialargs_get(&ctrlargs, &key_handle, sizeof(uint32_t));
614 		if (rc)
615 			return rc;
616 	}
617 
618 	rc = serialargs_alloc_get_one_attribute(&ctrlargs, &proc_params);
619 	if (rc)
620 		return rc;
621 
622 	if (serialargs_remaining_bytes(&ctrlargs)) {
623 		rc = PKCS11_CKR_ARGUMENTS_BAD;
624 		goto out_free;
625 	}
626 
627 	rc = get_ready_session(session);
628 	if (rc)
629 		goto out_free;
630 
631 	if (function != PKCS11_FUNCTION_DIGEST) {
632 		obj = pkcs11_handle2object(key_handle, session);
633 		if (!obj) {
634 			rc = PKCS11_CKR_KEY_HANDLE_INVALID;
635 			goto out_free;
636 		}
637 	}
638 
639 	rc = set_processing_state(session, function, obj, NULL);
640 	if (rc)
641 		goto out;
642 
643 	rc = check_mechanism_against_processing(session, proc_params->id,
644 						function,
645 						PKCS11_FUNC_STEP_INIT);
646 	if (rc)
647 		goto out;
648 
649 	if (obj) {
650 		rc = check_parent_attrs_against_processing(proc_params->id,
651 							   function,
652 							   obj->attributes);
653 		if (rc)
654 			goto out;
655 
656 		rc = check_access_attrs_against_token(session,
657 						      obj->attributes);
658 		if (rc)
659 			goto out;
660 	}
661 
662 	if (processing_is_tee_symm(proc_params->id))
663 		rc = init_symm_operation(session, function, proc_params, obj);
664 	else if (processing_is_tee_asymm(proc_params->id))
665 		rc = init_asymm_operation(session, function, proc_params, obj);
666 	else if (processing_is_tee_digest(proc_params->id))
667 		rc = init_digest_operation(session, proc_params);
668 	else
669 		rc = PKCS11_CKR_MECHANISM_INVALID;
670 
671 	if (rc == PKCS11_CKR_OK) {
672 		session->processing->mecha_type = proc_params->id;
673 		DMSG("PKCS11 session %"PRIu32": init processing %s %s",
674 		     session->handle, id2str_proc(proc_params->id),
675 		     id2str_function(function));
676 	}
677 
678 out:
679 	if (rc)
680 		release_active_processing(session);
681 out_free:
682 	TEE_Free(proc_params);
683 
684 	return rc;
685 }
686 
687 /*
688  * entry_processing_step - Generic entry on active processing
689  *
690  * @client = client reference
691  * @ptype = Invocation parameter types
692  * @params = Invocation parameters reference
693  * @function - encrypt, decrypt, sign, verify, digest, ...
694  * @step - update, oneshot, final
695  */
696 enum pkcs11_rc entry_processing_step(struct pkcs11_client *client,
697 				     uint32_t ptypes, TEE_Param *params,
698 				     enum processing_func function,
699 				     enum processing_step step)
700 {
701 	TEE_Param *ctrl = params;
702 	enum pkcs11_rc rc = PKCS11_CKR_OK;
703 	struct serialargs ctrlargs = { };
704 	struct pkcs11_session *session = NULL;
705 	enum pkcs11_mechanism_id mecha_type = PKCS11_CKM_UNDEFINED_ID;
706 	uint32_t key_handle = 0;
707 	struct pkcs11_object *obj = NULL;
708 
709 	if (!client ||
710 	    TEE_PARAM_TYPE_GET(ptypes, 0) != TEE_PARAM_TYPE_MEMREF_INOUT)
711 		return PKCS11_CKR_ARGUMENTS_BAD;
712 
713 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
714 
715 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
716 	if (rc)
717 		return rc;
718 
719 	if (step == PKCS11_FUNC_STEP_UPDATE_KEY) {
720 		assert(function == PKCS11_FUNCTION_DIGEST);
721 
722 		rc = serialargs_get(&ctrlargs, &key_handle, sizeof(uint32_t));
723 		if (rc)
724 			return rc;
725 	}
726 
727 	if (serialargs_remaining_bytes(&ctrlargs))
728 		return PKCS11_CKR_ARGUMENTS_BAD;
729 
730 	rc = get_active_session(session, function);
731 	if (rc)
732 		return rc;
733 
734 	if (step == PKCS11_FUNC_STEP_UPDATE_KEY) {
735 		assert(function == PKCS11_FUNCTION_DIGEST);
736 
737 		obj = pkcs11_handle2object(key_handle, session);
738 		if (!obj) {
739 			rc = PKCS11_CKR_KEY_HANDLE_INVALID;
740 			goto out;
741 		}
742 
743 		rc = check_access_attrs_against_token(session,
744 						      obj->attributes);
745 		if (rc) {
746 			rc = PKCS11_CKR_KEY_HANDLE_INVALID;
747 			goto out;
748 		}
749 	}
750 
751 	mecha_type = session->processing->mecha_type;
752 	rc = check_mechanism_against_processing(session, mecha_type,
753 						function, step);
754 	if (rc)
755 		goto out;
756 
757 	if (processing_is_tee_symm(mecha_type))
758 		rc = step_symm_operation(session, function, step,
759 					 ptypes, params);
760 	else if (processing_is_tee_asymm(mecha_type))
761 		rc = step_asymm_operation(session, function, step,
762 					  ptypes, params);
763 	else if (processing_is_tee_digest(mecha_type))
764 		rc = step_digest_operation(session, step, obj, ptypes, params);
765 	else
766 		rc = PKCS11_CKR_MECHANISM_INVALID;
767 
768 	if (rc == PKCS11_CKR_OK && (step == PKCS11_FUNC_STEP_UPDATE ||
769 				    step == PKCS11_FUNC_STEP_UPDATE_KEY)) {
770 		session->processing->step = PKCS11_FUNC_STEP_UPDATE;
771 		DMSG("PKCS11 session%"PRIu32": processing %s %s",
772 		     session->handle, id2str_proc(mecha_type),
773 		     id2str_function(function));
774 	}
775 
776 	if (rc == PKCS11_CKR_BUFFER_TOO_SMALL &&
777 	    step == PKCS11_FUNC_STEP_ONESHOT)
778 		session->processing->step = PKCS11_FUNC_STEP_ONESHOT;
779 
780 	if (rc == PKCS11_CKR_BUFFER_TOO_SMALL && step == PKCS11_FUNC_STEP_FINAL)
781 		session->processing->step = PKCS11_FUNC_STEP_FINAL;
782 
783 out:
784 	switch (step) {
785 	case PKCS11_FUNC_STEP_UPDATE:
786 	case PKCS11_FUNC_STEP_UPDATE_KEY:
787 		if (rc != PKCS11_CKR_OK && rc != PKCS11_CKR_BUFFER_TOO_SMALL)
788 			release_active_processing(session);
789 		break;
790 	default:
791 		/* ONESHOT and FINAL terminates processing on success */
792 		if (rc != PKCS11_CKR_BUFFER_TOO_SMALL)
793 			release_active_processing(session);
794 		break;
795 	}
796 
797 	return rc;
798 }
799 
800 enum pkcs11_rc entry_processing_key(struct pkcs11_client *client,
801 				    uint32_t ptypes, TEE_Param *params,
802 				    enum processing_func function)
803 {
804 	TEE_Param *ctrl = params;
805 	TEE_Param *out = params + 2;
806 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
807 	struct serialargs ctrlargs = { };
808 	struct pkcs11_session *session = NULL;
809 	struct pkcs11_attribute_head *proc_params = NULL;
810 	struct pkcs11_object_head *template = NULL;
811 	uint32_t parent_handle = 0;
812 	uint32_t obj_handle = 0;
813 	struct pkcs11_object *parent = NULL;
814 	struct obj_attrs *head = NULL;
815 	size_t template_size = 0;
816 	void *in_buf = NULL;
817 	uint32_t in_size = 0;
818 	void *out_buf = NULL;
819 	uint32_t out_size = 0;
820 	enum processing_func operation = PKCS11_FUNCTION_UNKNOWN;
821 
822 	if (!client ||
823 	    TEE_PARAM_TYPE_GET(ptypes, 0) != TEE_PARAM_TYPE_MEMREF_INOUT ||
824 	    TEE_PARAM_TYPE_GET(ptypes, 2) != TEE_PARAM_TYPE_MEMREF_OUTPUT ||
825 	    out->memref.size != sizeof(obj_handle) ||
826 	    TEE_PARAM_TYPE_GET(ptypes, 3) != TEE_PARAM_TYPE_NONE)
827 		return PKCS11_CKR_ARGUMENTS_BAD;
828 
829 	switch (function) {
830 	case PKCS11_FUNCTION_UNWRAP:
831 		if (TEE_PARAM_TYPE_GET(ptypes, 1) !=
832 				TEE_PARAM_TYPE_MEMREF_INPUT)
833 			return PKCS11_CKR_ARGUMENTS_BAD;
834 
835 		in_buf = params[1].memref.buffer;
836 		in_size = params[1].memref.size;
837 		if (in_size && !in_buf)
838 			return PKCS11_CKR_ARGUMENTS_BAD;
839 
840 		/*
841 		 * Some unwrap mechanisms require encryption to be
842 		 * performed on the data passed in proc_params by parent
843 		 * key. Hence set operation as PKCS11_FUNCTION_DECRYPT
844 		 * to be used with init_symm_operation()
845 		 */
846 		operation = PKCS11_FUNCTION_DECRYPT;
847 		break;
848 	case PKCS11_FUNCTION_DERIVE:
849 		if (TEE_PARAM_TYPE_GET(ptypes, 1) != TEE_PARAM_TYPE_NONE)
850 			return PKCS11_CKR_ARGUMENTS_BAD;
851 
852 		/*
853 		 * Some derivation mechanism require encryption to be
854 		 * performed on the data passed in proc_params by parent
855 		 * key. Hence set operation as PKCS11_FUNCTION_ENCRYPT
856 		 * to be used with init_symm_operation()
857 		 */
858 		operation = PKCS11_FUNCTION_ENCRYPT;
859 		break;
860 	default:
861 		return PKCS11_CKR_ARGUMENTS_BAD;
862 	}
863 
864 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
865 
866 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
867 	if (rc)
868 		return rc;
869 
870 	rc = serialargs_get(&ctrlargs, &parent_handle, sizeof(uint32_t));
871 	if (rc)
872 		return rc;
873 
874 	rc = serialargs_alloc_get_one_attribute(&ctrlargs, &proc_params);
875 	if (rc)
876 		return rc;
877 
878 	rc = serialargs_alloc_get_attributes(&ctrlargs, &template);
879 	if (rc)
880 		goto out_free;
881 
882 	if (serialargs_remaining_bytes(&ctrlargs)) {
883 		rc = PKCS11_CKR_ARGUMENTS_BAD;
884 		goto out_free;
885 	}
886 
887 	/* Return error if processing already active */
888 	rc = get_ready_session(session);
889 	if (rc)
890 		goto out_free;
891 
892 	/* Check parent handle */
893 	parent = pkcs11_handle2object(parent_handle, session);
894 	if (!parent) {
895 		rc = PKCS11_CKR_KEY_HANDLE_INVALID;
896 		goto out_free;
897 	}
898 
899 	/* Check if mechanism can be used for derivation function */
900 	rc = check_mechanism_against_processing(session, proc_params->id,
901 						function,
902 						PKCS11_FUNC_STEP_INIT);
903 	if (rc)
904 		goto out_free;
905 
906 	/* Set the processing state to active */
907 	rc = set_processing_state(session, function, parent, NULL);
908 	if (rc)
909 		goto out_free;
910 
911 	/*
912 	 * Check if base/parent key has CKA_DERIVE set and its key type is
913 	 * compatible with the mechanism passed
914 	 */
915 	rc = check_parent_attrs_against_processing(proc_params->id, function,
916 						   parent->attributes);
917 	if (rc) {
918 		/*
919 		 * CKR_KEY_FUNCTION_NOT_PERMITTED is not in the list of errors
920 		 * specified with C_Derive/Unwrap() in the specification. So
921 		 * return the next most appropriate error.
922 		 */
923 		if (rc == PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED) {
924 			if (function == PKCS11_FUNCTION_UNWRAP)
925 				rc =
926 				  PKCS11_CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT;
927 			else
928 				rc = PKCS11_CKR_KEY_TYPE_INCONSISTENT;
929 		}
930 		goto out;
931 	}
932 
933 	/* Check access of base/parent key */
934 	rc = check_access_attrs_against_token(session, parent->attributes);
935 	if (rc)
936 		goto out;
937 
938 	template_size = sizeof(*template) + template->attrs_size;
939 	/*
940 	 * Prepare a clean initial state for the requested object attributes
941 	 * using base/parent key attributes. Free temporary template once done.
942 	 */
943 	rc = create_attributes_from_template(&head, template, template_size,
944 					     parent->attributes,
945 					     function,
946 					     proc_params->id,
947 					     PKCS11_CKO_UNDEFINED_ID);
948 	if (rc)
949 		goto out;
950 
951 	TEE_Free(template);
952 	template = NULL;
953 
954 	/* check_created_attrs() is called later once key size is known */
955 
956 	rc = check_created_attrs_against_processing(proc_params->id, head);
957 	if (rc)
958 		goto out;
959 
960 	rc = check_created_attrs_against_token(session, head);
961 	if (rc)
962 		goto out;
963 
964 	rc = check_access_attrs_against_token(session, head);
965 	if (rc)
966 		goto out;
967 
968 	if (processing_is_tee_symm(proc_params->id)) {
969 		rc = init_symm_operation(session, operation, proc_params,
970 					 parent);
971 		if (rc)
972 			goto out;
973 
974 		session->processing->mecha_type = proc_params->id;
975 
976 		switch (function) {
977 		case PKCS11_FUNCTION_DERIVE:
978 			rc = derive_key_by_symm_enc(session, &out_buf,
979 						    &out_size);
980 			break;
981 		case PKCS11_FUNCTION_UNWRAP:
982 			rc = unwrap_key_by_symm(session, in_buf, in_size,
983 						&out_buf, &out_size);
984 			break;
985 		default:
986 			TEE_Panic(function);
987 		}
988 		if (rc)
989 			goto out;
990 
991 	} else if (processing_is_tee_asymm(proc_params->id)) {
992 		assert(function == PKCS11_FUNCTION_DERIVE);
993 
994 		rc = init_asymm_operation(session, function, proc_params,
995 					  parent);
996 		if (rc)
997 			goto out;
998 
999 		rc = do_asymm_derivation(session, proc_params, &head);
1000 		if (rc)
1001 			goto out;
1002 
1003 		goto done;
1004 	} else {
1005 		rc = PKCS11_CKR_MECHANISM_INVALID;
1006 		goto out;
1007 	}
1008 
1009 	rc = set_key_data(&head, out_buf, out_size);
1010 	if (rc)
1011 		goto out;
1012 
1013 done:
1014 	TEE_Free(out_buf);
1015 	out_buf = NULL;
1016 
1017 	TEE_Free(proc_params);
1018 	proc_params = NULL;
1019 
1020 	/*
1021 	 * Object is ready, register it and return a handle.
1022 	 */
1023 	rc = create_object(session, head, &obj_handle);
1024 	if (rc)
1025 		goto out;
1026 
1027 	/*
1028 	 * Now obj_handle (through the related struct pkcs11_object instance)
1029 	 * owns the serialized buffer that holds the object attributes.
1030 	 * We reset head to NULL as it is no more the buffer owner and would
1031 	 * be freed at function out.
1032 	 */
1033 	head = NULL;
1034 
1035 	TEE_MemMove(out->memref.buffer, &obj_handle, sizeof(obj_handle));
1036 	out->memref.size = sizeof(obj_handle);
1037 
1038 	DMSG("PKCS11 session %"PRIu32": derive secret %#"PRIx32,
1039 	     session->handle, obj_handle);
1040 
1041 out:
1042 	release_active_processing(session);
1043 out_free:
1044 	TEE_Free(proc_params);
1045 	TEE_Free(template);
1046 	TEE_Free(head);
1047 	TEE_Free(out_buf);
1048 
1049 	return rc;
1050 }
1051 
1052 enum pkcs11_rc entry_release_active_processing(struct pkcs11_client *client,
1053 					       uint32_t ptypes,
1054 					       TEE_Param *params)
1055 {
1056 	const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
1057 						TEE_PARAM_TYPE_NONE,
1058 						TEE_PARAM_TYPE_NONE,
1059 						TEE_PARAM_TYPE_NONE);
1060 	TEE_Param *ctrl = params;
1061 	enum pkcs11_rc rc = PKCS11_CKR_OK;
1062 	struct serialargs ctrlargs = { };
1063 	struct pkcs11_session *session = NULL;
1064 	enum processing_func function = PKCS11_FUNCTION_UNKNOWN;
1065 	uint32_t cmd = 0;
1066 
1067 	if (!client || ptypes != exp_pt)
1068 		return PKCS11_CKR_ARGUMENTS_BAD;
1069 
1070 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
1071 
1072 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
1073 	if (rc)
1074 		return rc;
1075 
1076 	rc = serialargs_get_u32(&ctrlargs, &cmd);
1077 
1078 	if (serialargs_remaining_bytes(&ctrlargs))
1079 		return PKCS11_CKR_ARGUMENTS_BAD;
1080 
1081 	function = func_for_cmd(cmd);
1082 	if (function == PKCS11_FUNCTION_UNKNOWN)
1083 		return PKCS11_CKR_ARGUMENTS_BAD;
1084 
1085 	rc = get_active_session(session, function);
1086 	if (rc)
1087 		return rc;
1088 
1089 	release_active_processing(session);
1090 
1091 	DMSG("PKCS11 session %"PRIu32": release processing", session->handle);
1092 
1093 	return PKCS11_CKR_OK;
1094 }
1095 
1096 enum pkcs11_rc entry_wrap_key(struct pkcs11_client *client,
1097 			      uint32_t ptypes, TEE_Param *params)
1098 {
1099 	const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
1100 						TEE_PARAM_TYPE_NONE,
1101 						TEE_PARAM_TYPE_MEMREF_OUTPUT,
1102 						TEE_PARAM_TYPE_NONE);
1103 	TEE_Param *ctrl = params;
1104 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
1105 	struct serialargs ctrlargs = { };
1106 	struct pkcs11_session *session = NULL;
1107 	struct pkcs11_attribute_head *proc_params = NULL;
1108 	struct pkcs11_object *wrapping_key = NULL;
1109 	struct pkcs11_object *key = NULL;
1110 	void *req_attrs = NULL;
1111 	uint32_t wrapping_key_handle = 0;
1112 	uint32_t key_handle = 0;
1113 	uint32_t size = 0;
1114 	void *key_data = NULL;
1115 	uint32_t key_sz = 0;
1116 	void *out_buf = params[2].memref.buffer;
1117 	uint32_t out_size = params[2].memref.size;
1118 	const enum processing_func function = PKCS11_FUNCTION_WRAP;
1119 
1120 	if (!client || ptypes != exp_pt ||
1121 	    (out_size && !out_buf))
1122 		return PKCS11_CKR_ARGUMENTS_BAD;
1123 
1124 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
1125 
1126 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
1127 	if (rc)
1128 		return rc;
1129 
1130 	rc = serialargs_get(&ctrlargs, &wrapping_key_handle, sizeof(uint32_t));
1131 	if (rc)
1132 		return rc;
1133 
1134 	rc = serialargs_get(&ctrlargs, &key_handle, sizeof(uint32_t));
1135 	if (rc)
1136 		return rc;
1137 
1138 	rc = serialargs_alloc_get_one_attribute(&ctrlargs, &proc_params);
1139 	if (rc)
1140 		return rc;
1141 
1142 	if (serialargs_remaining_bytes(&ctrlargs)) {
1143 		rc = PKCS11_CKR_ARGUMENTS_BAD;
1144 		goto out_free;
1145 	}
1146 
1147 	rc = get_ready_session(session);
1148 	if (rc)
1149 		goto out_free;
1150 
1151 	wrapping_key = pkcs11_handle2object(wrapping_key_handle, session);
1152 	if (!wrapping_key) {
1153 		rc = PKCS11_CKR_WRAPPING_KEY_HANDLE_INVALID;
1154 		goto out_free;
1155 	}
1156 
1157 	key = pkcs11_handle2object(key_handle, session);
1158 	if (!key) {
1159 		rc = PKCS11_CKR_KEY_HANDLE_INVALID;
1160 		goto out_free;
1161 	}
1162 
1163 	/*
1164 	 * The wrapping key and key to be wrapped shouldn't be same.
1165 	 * PKCS#11 spec doesn't explicitly state that but logically this isn't
1166 	 * a use case and also acts as an attack vector, so explicitly
1167 	 * disallow this.
1168 	 */
1169 	if (key == wrapping_key) {
1170 		rc = PKCS11_CKR_WRAPPING_KEY_HANDLE_INVALID;
1171 		goto out_free;
1172 	}
1173 
1174 	rc = set_processing_state(session, function, wrapping_key, NULL);
1175 	if (rc)
1176 		goto out_free;
1177 
1178 	/* Check if mechanism can be used for wrapping function */
1179 	rc = check_mechanism_against_processing(session, proc_params->id,
1180 						function,
1181 						PKCS11_FUNC_STEP_INIT);
1182 	if (rc)
1183 		goto out;
1184 
1185 	/*
1186 	 * Check if wrapping key has CKA_WRAP set and its key type is
1187 	 * compatible with the mechanism passed
1188 	 */
1189 	rc = check_parent_attrs_against_processing(proc_params->id, function,
1190 						   wrapping_key->attributes);
1191 	if (rc) {
1192 		/*
1193 		 * CKR_KEY_FUNCTION_NOT_PERMITTED is not in the list of errors
1194 		 * specified with C_Wrap() in the specification. So
1195 		 * return the next most appropriate error.
1196 		 */
1197 		if (rc == PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED)
1198 			rc = PKCS11_CKR_WRAPPING_KEY_TYPE_INCONSISTENT;
1199 
1200 		goto out;
1201 	}
1202 
1203 	/* Check access of wrapping key */
1204 	rc = check_access_attrs_against_token(session,
1205 					      wrapping_key->attributes);
1206 	if (rc)
1207 		goto out;
1208 
1209 	switch (get_class(key->attributes)) {
1210 	case PKCS11_CKO_SECRET_KEY:
1211 		break;
1212 	/* Key type not supported as yet */
1213 	case PKCS11_CKO_PRIVATE_KEY:
1214 	default:
1215 		rc = PKCS11_CKR_KEY_NOT_WRAPPABLE;
1216 		goto out;
1217 	}
1218 
1219 	/* Check if key to be wrapped is extractable */
1220 	if (!get_bool(key->attributes, PKCS11_CKA_EXTRACTABLE)) {
1221 		DMSG("Extractable property is false");
1222 		rc = PKCS11_CKR_KEY_UNEXTRACTABLE;
1223 		goto out;
1224 	}
1225 
1226 	if (get_bool(key->attributes, PKCS11_CKA_WRAP_WITH_TRUSTED) &&
1227 	    !get_bool(wrapping_key->attributes, PKCS11_CKA_TRUSTED)) {
1228 		DMSG("Wrap with trusted not satisfied");
1229 		rc = PKCS11_CKR_KEY_NOT_WRAPPABLE;
1230 		goto out;
1231 	}
1232 
1233 	rc = check_access_attrs_against_token(session, key->attributes);
1234 	if (rc)
1235 		goto out;
1236 
1237 	rc = get_attribute_ptr(wrapping_key->attributes,
1238 			       PKCS11_CKA_WRAP_TEMPLATE, &req_attrs, &size);
1239 	if (rc == PKCS11_CKR_OK && size != 0) {
1240 		if (!attributes_match_reference(key->attributes, req_attrs)) {
1241 			rc = PKCS11_CKR_KEY_HANDLE_INVALID;
1242 			goto out;
1243 		}
1244 	}
1245 
1246 	rc = get_key_data_to_wrap(key->attributes, &key_data, &key_sz);
1247 	if (rc)
1248 		goto out;
1249 
1250 	if (processing_is_tee_symm(proc_params->id)) {
1251 		rc = init_symm_operation(session, PKCS11_FUNCTION_ENCRYPT,
1252 					 proc_params, wrapping_key);
1253 		if (rc)
1254 			goto out;
1255 
1256 		session->processing->mecha_type = proc_params->id;
1257 
1258 		rc = wrap_data_by_symm_enc(session, key_data, key_sz, out_buf,
1259 					   &out_size);
1260 	} else {
1261 		rc = PKCS11_CKR_MECHANISM_INVALID;
1262 	}
1263 
1264 	if (rc == PKCS11_CKR_OK || rc == PKCS11_CKR_BUFFER_TOO_SMALL)
1265 		params[2].memref.size = out_size;
1266 
1267 out:
1268 	release_active_processing(session);
1269 out_free:
1270 	TEE_Free(proc_params);
1271 	return rc;
1272 }
1273