xref: /optee_os/ta/pkcs11/src/processing.c (revision 8fab4371e333936b8d965c1fa6f853774c2c0252)
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 	pubkey_handle = 0;
559 	privkey_handle = 0;
560 
561 	DMSG("PKCS11 session %"PRIu32": create key pair %#"PRIx32"/%#"PRIx32,
562 	     session->handle, privkey_handle, pubkey_handle);
563 
564 out:
565 	if (pubkey_handle) {
566 		object = pkcs11_handle2object(pubkey_handle, session);
567 		if (!object)
568 			TEE_Panic(0);
569 		destroy_object(session, object, false);
570 	}
571 	TEE_Free(priv_head);
572 	TEE_Free(pub_head);
573 	TEE_Free(priv_template);
574 	TEE_Free(pub_template);
575 	TEE_Free(proc_params);
576 
577 	return rc;
578 }
579 
580 /*
581  * entry_processing_init - Generic entry for initializing a processing
582  *
583  * @client = client reference
584  * @ptype = Invocation parameter types
585  * @params = Invocation parameters reference
586  * @function - encrypt, decrypt, sign, verify, digest, ...
587  */
588 enum pkcs11_rc entry_processing_init(struct pkcs11_client *client,
589 				     uint32_t ptypes, TEE_Param *params,
590 				     enum processing_func function)
591 {
592 	const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
593 						TEE_PARAM_TYPE_NONE,
594 						TEE_PARAM_TYPE_NONE,
595 						TEE_PARAM_TYPE_NONE);
596 	TEE_Param *ctrl = params;
597 	enum pkcs11_rc rc = PKCS11_CKR_OK;
598 	struct serialargs ctrlargs = { };
599 	struct pkcs11_session *session = NULL;
600 	struct pkcs11_attribute_head *proc_params = NULL;
601 	uint32_t key_handle = 0;
602 	struct pkcs11_object *obj = NULL;
603 
604 	if (!client || ptypes != exp_pt)
605 		return PKCS11_CKR_ARGUMENTS_BAD;
606 
607 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
608 
609 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
610 	if (rc)
611 		return rc;
612 
613 	if (function != PKCS11_FUNCTION_DIGEST) {
614 		rc = serialargs_get(&ctrlargs, &key_handle, sizeof(uint32_t));
615 		if (rc)
616 			return rc;
617 	}
618 
619 	rc = serialargs_alloc_get_one_attribute(&ctrlargs, &proc_params);
620 	if (rc)
621 		return rc;
622 
623 	if (serialargs_remaining_bytes(&ctrlargs)) {
624 		rc = PKCS11_CKR_ARGUMENTS_BAD;
625 		goto out_free;
626 	}
627 
628 	rc = get_ready_session(session);
629 	if (rc)
630 		goto out_free;
631 
632 	if (function != PKCS11_FUNCTION_DIGEST) {
633 		obj = pkcs11_handle2object(key_handle, session);
634 		if (!obj) {
635 			rc = PKCS11_CKR_KEY_HANDLE_INVALID;
636 			goto out_free;
637 		}
638 	}
639 
640 	rc = set_processing_state(session, function, obj, NULL);
641 	if (rc)
642 		goto out;
643 
644 	rc = check_mechanism_against_processing(session, proc_params->id,
645 						function,
646 						PKCS11_FUNC_STEP_INIT);
647 	if (rc)
648 		goto out;
649 
650 	if (obj) {
651 		rc = check_parent_attrs_against_processing(proc_params->id,
652 							   function,
653 							   obj->attributes);
654 		if (rc)
655 			goto out;
656 
657 		rc = check_access_attrs_against_token(session,
658 						      obj->attributes);
659 		if (rc)
660 			goto out;
661 	}
662 
663 	if (processing_is_tee_symm(proc_params->id))
664 		rc = init_symm_operation(session, function, proc_params, obj);
665 	else if (processing_is_tee_asymm(proc_params->id))
666 		rc = init_asymm_operation(session, function, proc_params, obj);
667 	else if (processing_is_tee_digest(proc_params->id))
668 		rc = init_digest_operation(session, proc_params);
669 	else
670 		rc = PKCS11_CKR_MECHANISM_INVALID;
671 
672 	if (rc == PKCS11_CKR_OK) {
673 		session->processing->mecha_type = proc_params->id;
674 		DMSG("PKCS11 session %"PRIu32": init processing %s %s",
675 		     session->handle, id2str_proc(proc_params->id),
676 		     id2str_function(function));
677 	}
678 
679 out:
680 	if (rc)
681 		release_active_processing(session);
682 out_free:
683 	TEE_Free(proc_params);
684 
685 	return rc;
686 }
687 
688 /*
689  * entry_processing_step - Generic entry on active processing
690  *
691  * @client = client reference
692  * @ptype = Invocation parameter types
693  * @params = Invocation parameters reference
694  * @function - encrypt, decrypt, sign, verify, digest, ...
695  * @step - update, oneshot, final
696  */
697 enum pkcs11_rc entry_processing_step(struct pkcs11_client *client,
698 				     uint32_t ptypes, TEE_Param *params,
699 				     enum processing_func function,
700 				     enum processing_step step)
701 {
702 	TEE_Param *ctrl = params;
703 	enum pkcs11_rc rc = PKCS11_CKR_OK;
704 	struct serialargs ctrlargs = { };
705 	struct pkcs11_session *session = NULL;
706 	enum pkcs11_mechanism_id mecha_type = PKCS11_CKM_UNDEFINED_ID;
707 	uint32_t key_handle = 0;
708 	struct pkcs11_object *obj = NULL;
709 
710 	if (!client ||
711 	    TEE_PARAM_TYPE_GET(ptypes, 0) != TEE_PARAM_TYPE_MEMREF_INOUT)
712 		return PKCS11_CKR_ARGUMENTS_BAD;
713 
714 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
715 
716 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
717 	if (rc)
718 		return rc;
719 
720 	if (step == PKCS11_FUNC_STEP_UPDATE_KEY) {
721 		assert(function == PKCS11_FUNCTION_DIGEST);
722 
723 		rc = serialargs_get(&ctrlargs, &key_handle, sizeof(uint32_t));
724 		if (rc)
725 			return rc;
726 	}
727 
728 	if (serialargs_remaining_bytes(&ctrlargs))
729 		return PKCS11_CKR_ARGUMENTS_BAD;
730 
731 	rc = get_active_session(session, function);
732 	if (rc)
733 		return rc;
734 
735 	if (step == PKCS11_FUNC_STEP_UPDATE_KEY) {
736 		assert(function == PKCS11_FUNCTION_DIGEST);
737 
738 		obj = pkcs11_handle2object(key_handle, session);
739 		if (!obj) {
740 			rc = PKCS11_CKR_KEY_HANDLE_INVALID;
741 			goto out;
742 		}
743 
744 		rc = check_access_attrs_against_token(session,
745 						      obj->attributes);
746 		if (rc) {
747 			rc = PKCS11_CKR_KEY_HANDLE_INVALID;
748 			goto out;
749 		}
750 	}
751 
752 	mecha_type = session->processing->mecha_type;
753 	rc = check_mechanism_against_processing(session, mecha_type,
754 						function, step);
755 	if (rc)
756 		goto out;
757 
758 	if (processing_is_tee_symm(mecha_type))
759 		rc = step_symm_operation(session, function, step,
760 					 ptypes, params);
761 	else if (processing_is_tee_asymm(mecha_type))
762 		rc = step_asymm_operation(session, function, step,
763 					  ptypes, params);
764 	else if (processing_is_tee_digest(mecha_type))
765 		rc = step_digest_operation(session, step, obj, ptypes, params);
766 	else
767 		rc = PKCS11_CKR_MECHANISM_INVALID;
768 
769 	if (rc == PKCS11_CKR_OK && (step == PKCS11_FUNC_STEP_UPDATE ||
770 				    step == PKCS11_FUNC_STEP_UPDATE_KEY)) {
771 		session->processing->step = PKCS11_FUNC_STEP_UPDATE;
772 		DMSG("PKCS11 session%"PRIu32": processing %s %s",
773 		     session->handle, id2str_proc(mecha_type),
774 		     id2str_function(function));
775 	}
776 
777 	if (rc == PKCS11_CKR_BUFFER_TOO_SMALL &&
778 	    step == PKCS11_FUNC_STEP_ONESHOT)
779 		session->processing->step = PKCS11_FUNC_STEP_ONESHOT;
780 
781 	if (rc == PKCS11_CKR_BUFFER_TOO_SMALL && step == PKCS11_FUNC_STEP_FINAL)
782 		session->processing->step = PKCS11_FUNC_STEP_FINAL;
783 
784 out:
785 	switch (step) {
786 	case PKCS11_FUNC_STEP_UPDATE:
787 	case PKCS11_FUNC_STEP_UPDATE_KEY:
788 		if (rc != PKCS11_CKR_OK && rc != PKCS11_CKR_BUFFER_TOO_SMALL)
789 			release_active_processing(session);
790 		break;
791 	default:
792 		/* ONESHOT and FINAL terminates processing on success */
793 		if (rc != PKCS11_CKR_BUFFER_TOO_SMALL)
794 			release_active_processing(session);
795 		break;
796 	}
797 
798 	return rc;
799 }
800 
801 enum pkcs11_rc entry_processing_key(struct pkcs11_client *client,
802 				    uint32_t ptypes, TEE_Param *params,
803 				    enum processing_func function)
804 {
805 	TEE_Param *ctrl = params;
806 	TEE_Param *out = params + 2;
807 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
808 	struct serialargs ctrlargs = { };
809 	struct pkcs11_session *session = NULL;
810 	struct pkcs11_attribute_head *proc_params = NULL;
811 	struct pkcs11_object_head *template = NULL;
812 	uint32_t parent_handle = 0;
813 	uint32_t obj_handle = 0;
814 	struct pkcs11_object *parent = NULL;
815 	struct obj_attrs *head = NULL;
816 	size_t template_size = 0;
817 	void *in_buf = NULL;
818 	uint32_t in_size = 0;
819 	void *out_buf = NULL;
820 	uint32_t out_size = 0;
821 	enum processing_func operation = PKCS11_FUNCTION_UNKNOWN;
822 
823 	if (!client ||
824 	    TEE_PARAM_TYPE_GET(ptypes, 0) != TEE_PARAM_TYPE_MEMREF_INOUT ||
825 	    TEE_PARAM_TYPE_GET(ptypes, 2) != TEE_PARAM_TYPE_MEMREF_OUTPUT ||
826 	    out->memref.size != sizeof(obj_handle) ||
827 	    TEE_PARAM_TYPE_GET(ptypes, 3) != TEE_PARAM_TYPE_NONE)
828 		return PKCS11_CKR_ARGUMENTS_BAD;
829 
830 	switch (function) {
831 	case PKCS11_FUNCTION_UNWRAP:
832 		if (TEE_PARAM_TYPE_GET(ptypes, 1) !=
833 				TEE_PARAM_TYPE_MEMREF_INPUT)
834 			return PKCS11_CKR_ARGUMENTS_BAD;
835 
836 		in_buf = params[1].memref.buffer;
837 		in_size = params[1].memref.size;
838 		if (in_size && !in_buf)
839 			return PKCS11_CKR_ARGUMENTS_BAD;
840 
841 		/*
842 		 * Some unwrap mechanisms require encryption to be
843 		 * performed on the data passed in proc_params by parent
844 		 * key. Hence set operation as PKCS11_FUNCTION_DECRYPT
845 		 * to be used with init_symm_operation()
846 		 */
847 		operation = PKCS11_FUNCTION_DECRYPT;
848 		break;
849 	case PKCS11_FUNCTION_DERIVE:
850 		if (TEE_PARAM_TYPE_GET(ptypes, 1) != TEE_PARAM_TYPE_NONE)
851 			return PKCS11_CKR_ARGUMENTS_BAD;
852 
853 		/*
854 		 * Some derivation mechanism require encryption to be
855 		 * performed on the data passed in proc_params by parent
856 		 * key. Hence set operation as PKCS11_FUNCTION_ENCRYPT
857 		 * to be used with init_symm_operation()
858 		 */
859 		operation = PKCS11_FUNCTION_ENCRYPT;
860 		break;
861 	default:
862 		return PKCS11_CKR_ARGUMENTS_BAD;
863 	}
864 
865 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
866 
867 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
868 	if (rc)
869 		return rc;
870 
871 	rc = serialargs_get(&ctrlargs, &parent_handle, sizeof(uint32_t));
872 	if (rc)
873 		return rc;
874 
875 	rc = serialargs_alloc_get_one_attribute(&ctrlargs, &proc_params);
876 	if (rc)
877 		return rc;
878 
879 	rc = serialargs_alloc_get_attributes(&ctrlargs, &template);
880 	if (rc)
881 		goto out_free;
882 
883 	if (serialargs_remaining_bytes(&ctrlargs)) {
884 		rc = PKCS11_CKR_ARGUMENTS_BAD;
885 		goto out_free;
886 	}
887 
888 	/* Return error if processing already active */
889 	rc = get_ready_session(session);
890 	if (rc)
891 		goto out_free;
892 
893 	/* Check parent handle */
894 	parent = pkcs11_handle2object(parent_handle, session);
895 	if (!parent) {
896 		rc = PKCS11_CKR_KEY_HANDLE_INVALID;
897 		goto out_free;
898 	}
899 
900 	/* Check if mechanism can be used for derivation function */
901 	rc = check_mechanism_against_processing(session, proc_params->id,
902 						function,
903 						PKCS11_FUNC_STEP_INIT);
904 	if (rc)
905 		goto out_free;
906 
907 	/* Set the processing state to active */
908 	rc = set_processing_state(session, function, parent, NULL);
909 	if (rc)
910 		goto out_free;
911 
912 	/*
913 	 * Check if base/parent key has CKA_DERIVE set and its key type is
914 	 * compatible with the mechanism passed
915 	 */
916 	rc = check_parent_attrs_against_processing(proc_params->id, function,
917 						   parent->attributes);
918 	if (rc) {
919 		/*
920 		 * CKR_KEY_FUNCTION_NOT_PERMITTED is not in the list of errors
921 		 * specified with C_Derive/Unwrap() in the specification. So
922 		 * return the next most appropriate error.
923 		 */
924 		if (rc == PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED) {
925 			if (function == PKCS11_FUNCTION_UNWRAP)
926 				rc =
927 				  PKCS11_CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT;
928 			else
929 				rc = PKCS11_CKR_KEY_TYPE_INCONSISTENT;
930 		}
931 		goto out;
932 	}
933 
934 	/* Check access of base/parent key */
935 	rc = check_access_attrs_against_token(session, parent->attributes);
936 	if (rc)
937 		goto out;
938 
939 	template_size = sizeof(*template) + template->attrs_size;
940 	/*
941 	 * Prepare a clean initial state for the requested object attributes
942 	 * using base/parent key attributes. Free temporary template once done.
943 	 */
944 	rc = create_attributes_from_template(&head, template, template_size,
945 					     parent->attributes,
946 					     function,
947 					     proc_params->id,
948 					     PKCS11_CKO_UNDEFINED_ID);
949 	if (rc)
950 		goto out;
951 
952 	TEE_Free(template);
953 	template = NULL;
954 
955 	/* check_created_attrs() is called later once key size is known */
956 
957 	rc = check_created_attrs_against_processing(proc_params->id, head);
958 	if (rc)
959 		goto out;
960 
961 	rc = check_created_attrs_against_token(session, head);
962 	if (rc)
963 		goto out;
964 
965 	rc = check_access_attrs_against_token(session, head);
966 	if (rc)
967 		goto out;
968 
969 	if (processing_is_tee_symm(proc_params->id)) {
970 		rc = init_symm_operation(session, operation, proc_params,
971 					 parent);
972 		if (rc)
973 			goto out;
974 
975 		session->processing->mecha_type = proc_params->id;
976 
977 		switch (function) {
978 		case PKCS11_FUNCTION_DERIVE:
979 			rc = derive_key_by_symm_enc(session, &out_buf,
980 						    &out_size);
981 			break;
982 		case PKCS11_FUNCTION_UNWRAP:
983 			rc = unwrap_key_by_symm(session, in_buf, in_size,
984 						&out_buf, &out_size);
985 			break;
986 		default:
987 			TEE_Panic(function);
988 		}
989 		if (rc)
990 			goto out;
991 
992 	} else if (processing_is_tee_asymm(proc_params->id)) {
993 		assert(function == PKCS11_FUNCTION_DERIVE);
994 
995 		rc = init_asymm_operation(session, function, proc_params,
996 					  parent);
997 		if (rc)
998 			goto out;
999 
1000 		rc = do_asymm_derivation(session, proc_params, &head);
1001 		if (rc)
1002 			goto out;
1003 
1004 		goto done;
1005 	} else {
1006 		rc = PKCS11_CKR_MECHANISM_INVALID;
1007 		goto out;
1008 	}
1009 
1010 	rc = set_key_data(&head, out_buf, out_size);
1011 	if (rc)
1012 		goto out;
1013 
1014 done:
1015 	TEE_Free(out_buf);
1016 	out_buf = NULL;
1017 
1018 	TEE_Free(proc_params);
1019 	proc_params = NULL;
1020 
1021 	/*
1022 	 * Object is ready, register it and return a handle.
1023 	 */
1024 	rc = create_object(session, head, &obj_handle);
1025 	if (rc)
1026 		goto out;
1027 
1028 	/*
1029 	 * Now obj_handle (through the related struct pkcs11_object instance)
1030 	 * owns the serialized buffer that holds the object attributes.
1031 	 * We reset head to NULL as it is no more the buffer owner and would
1032 	 * be freed at function out.
1033 	 */
1034 	head = NULL;
1035 
1036 	TEE_MemMove(out->memref.buffer, &obj_handle, sizeof(obj_handle));
1037 	out->memref.size = sizeof(obj_handle);
1038 
1039 	DMSG("PKCS11 session %"PRIu32": derive secret %#"PRIx32,
1040 	     session->handle, obj_handle);
1041 
1042 out:
1043 	release_active_processing(session);
1044 out_free:
1045 	TEE_Free(proc_params);
1046 	TEE_Free(template);
1047 	TEE_Free(head);
1048 	TEE_Free(out_buf);
1049 
1050 	return rc;
1051 }
1052 
1053 enum pkcs11_rc entry_release_active_processing(struct pkcs11_client *client,
1054 					       uint32_t ptypes,
1055 					       TEE_Param *params)
1056 {
1057 	const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
1058 						TEE_PARAM_TYPE_NONE,
1059 						TEE_PARAM_TYPE_NONE,
1060 						TEE_PARAM_TYPE_NONE);
1061 	TEE_Param *ctrl = params;
1062 	enum pkcs11_rc rc = PKCS11_CKR_OK;
1063 	struct serialargs ctrlargs = { };
1064 	struct pkcs11_session *session = NULL;
1065 	enum processing_func function = PKCS11_FUNCTION_UNKNOWN;
1066 	uint32_t cmd = 0;
1067 
1068 	if (!client || ptypes != exp_pt)
1069 		return PKCS11_CKR_ARGUMENTS_BAD;
1070 
1071 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
1072 
1073 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
1074 	if (rc)
1075 		return rc;
1076 
1077 	rc = serialargs_get_u32(&ctrlargs, &cmd);
1078 
1079 	if (serialargs_remaining_bytes(&ctrlargs))
1080 		return PKCS11_CKR_ARGUMENTS_BAD;
1081 
1082 	function = func_for_cmd(cmd);
1083 	if (function == PKCS11_FUNCTION_UNKNOWN)
1084 		return PKCS11_CKR_ARGUMENTS_BAD;
1085 
1086 	rc = get_active_session(session, function);
1087 	if (rc)
1088 		return rc;
1089 
1090 	release_active_processing(session);
1091 
1092 	DMSG("PKCS11 session %"PRIu32": release processing", session->handle);
1093 
1094 	return PKCS11_CKR_OK;
1095 }
1096 
1097 enum pkcs11_rc entry_wrap_key(struct pkcs11_client *client,
1098 			      uint32_t ptypes, TEE_Param *params)
1099 {
1100 	const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
1101 						TEE_PARAM_TYPE_NONE,
1102 						TEE_PARAM_TYPE_MEMREF_OUTPUT,
1103 						TEE_PARAM_TYPE_NONE);
1104 	TEE_Param *ctrl = params;
1105 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
1106 	struct serialargs ctrlargs = { };
1107 	struct pkcs11_session *session = NULL;
1108 	struct pkcs11_attribute_head *proc_params = NULL;
1109 	struct pkcs11_object *wrapping_key = NULL;
1110 	struct pkcs11_object *key = NULL;
1111 	void *req_attrs = NULL;
1112 	uint32_t wrapping_key_handle = 0;
1113 	uint32_t key_handle = 0;
1114 	uint32_t size = 0;
1115 	void *key_data = NULL;
1116 	uint32_t key_sz = 0;
1117 	void *out_buf = params[2].memref.buffer;
1118 	uint32_t out_size = params[2].memref.size;
1119 	const enum processing_func function = PKCS11_FUNCTION_WRAP;
1120 
1121 	if (!client || ptypes != exp_pt ||
1122 	    (out_size && !out_buf))
1123 		return PKCS11_CKR_ARGUMENTS_BAD;
1124 
1125 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
1126 
1127 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
1128 	if (rc)
1129 		return rc;
1130 
1131 	rc = serialargs_get(&ctrlargs, &wrapping_key_handle, sizeof(uint32_t));
1132 	if (rc)
1133 		return rc;
1134 
1135 	rc = serialargs_get(&ctrlargs, &key_handle, sizeof(uint32_t));
1136 	if (rc)
1137 		return rc;
1138 
1139 	rc = serialargs_alloc_get_one_attribute(&ctrlargs, &proc_params);
1140 	if (rc)
1141 		return rc;
1142 
1143 	if (serialargs_remaining_bytes(&ctrlargs)) {
1144 		rc = PKCS11_CKR_ARGUMENTS_BAD;
1145 		goto out_free;
1146 	}
1147 
1148 	rc = get_ready_session(session);
1149 	if (rc)
1150 		goto out_free;
1151 
1152 	wrapping_key = pkcs11_handle2object(wrapping_key_handle, session);
1153 	if (!wrapping_key) {
1154 		rc = PKCS11_CKR_WRAPPING_KEY_HANDLE_INVALID;
1155 		goto out_free;
1156 	}
1157 
1158 	key = pkcs11_handle2object(key_handle, session);
1159 	if (!key) {
1160 		rc = PKCS11_CKR_KEY_HANDLE_INVALID;
1161 		goto out_free;
1162 	}
1163 
1164 	/*
1165 	 * The wrapping key and key to be wrapped shouldn't be same.
1166 	 * PKCS#11 spec doesn't explicitly state that but logically this isn't
1167 	 * a use case and also acts as an attack vector, so explicitly
1168 	 * disallow this.
1169 	 */
1170 	if (key == wrapping_key) {
1171 		rc = PKCS11_CKR_WRAPPING_KEY_HANDLE_INVALID;
1172 		goto out_free;
1173 	}
1174 
1175 	rc = set_processing_state(session, function, wrapping_key, NULL);
1176 	if (rc)
1177 		goto out_free;
1178 
1179 	/* Check if mechanism can be used for wrapping function */
1180 	rc = check_mechanism_against_processing(session, proc_params->id,
1181 						function,
1182 						PKCS11_FUNC_STEP_INIT);
1183 	if (rc)
1184 		goto out;
1185 
1186 	/*
1187 	 * Check if wrapping key has CKA_WRAP set and its key type is
1188 	 * compatible with the mechanism passed
1189 	 */
1190 	rc = check_parent_attrs_against_processing(proc_params->id, function,
1191 						   wrapping_key->attributes);
1192 	if (rc) {
1193 		/*
1194 		 * CKR_KEY_FUNCTION_NOT_PERMITTED is not in the list of errors
1195 		 * specified with C_Wrap() in the specification. So
1196 		 * return the next most appropriate error.
1197 		 */
1198 		if (rc == PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED)
1199 			rc = PKCS11_CKR_WRAPPING_KEY_TYPE_INCONSISTENT;
1200 
1201 		goto out;
1202 	}
1203 
1204 	/* Check access of wrapping key */
1205 	rc = check_access_attrs_against_token(session,
1206 					      wrapping_key->attributes);
1207 	if (rc)
1208 		goto out;
1209 
1210 	switch (get_class(key->attributes)) {
1211 	case PKCS11_CKO_SECRET_KEY:
1212 		break;
1213 	/* Key type not supported as yet */
1214 	case PKCS11_CKO_PRIVATE_KEY:
1215 	default:
1216 		rc = PKCS11_CKR_KEY_NOT_WRAPPABLE;
1217 		goto out;
1218 	}
1219 
1220 	/* Check if key to be wrapped is extractable */
1221 	if (!get_bool(key->attributes, PKCS11_CKA_EXTRACTABLE)) {
1222 		DMSG("Extractable property is false");
1223 		rc = PKCS11_CKR_KEY_UNEXTRACTABLE;
1224 		goto out;
1225 	}
1226 
1227 	if (get_bool(key->attributes, PKCS11_CKA_WRAP_WITH_TRUSTED) &&
1228 	    !get_bool(wrapping_key->attributes, PKCS11_CKA_TRUSTED)) {
1229 		DMSG("Wrap with trusted not satisfied");
1230 		rc = PKCS11_CKR_KEY_NOT_WRAPPABLE;
1231 		goto out;
1232 	}
1233 
1234 	rc = check_access_attrs_against_token(session, key->attributes);
1235 	if (rc)
1236 		goto out;
1237 
1238 	rc = get_attribute_ptr(wrapping_key->attributes,
1239 			       PKCS11_CKA_WRAP_TEMPLATE, &req_attrs, &size);
1240 	if (rc == PKCS11_CKR_OK && size != 0) {
1241 		if (!attributes_match_reference(key->attributes, req_attrs)) {
1242 			rc = PKCS11_CKR_KEY_HANDLE_INVALID;
1243 			goto out;
1244 		}
1245 	}
1246 
1247 	rc = get_key_data_to_wrap(key->attributes, &key_data, &key_sz);
1248 	if (rc)
1249 		goto out;
1250 
1251 	if (processing_is_tee_symm(proc_params->id)) {
1252 		rc = init_symm_operation(session, PKCS11_FUNCTION_ENCRYPT,
1253 					 proc_params, wrapping_key);
1254 		if (rc)
1255 			goto out;
1256 
1257 		session->processing->mecha_type = proc_params->id;
1258 
1259 		rc = wrap_data_by_symm_enc(session, key_data, key_sz, out_buf,
1260 					   &out_size);
1261 	} else {
1262 		rc = PKCS11_CKR_MECHANISM_INVALID;
1263 	}
1264 
1265 	if (rc == PKCS11_CKR_OK || rc == PKCS11_CKR_BUFFER_TOO_SMALL)
1266 		params[2].memref.size = out_size;
1267 
1268 out:
1269 	release_active_processing(session);
1270 out_free:
1271 	TEE_Free(proc_params);
1272 	return rc;
1273 }
1274