xref: /optee_os/ta/pkcs11/src/processing.c (revision 013934d8921ec762a0febb6dde6e2a9854d04a99)
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_op_handle != TEE_HANDLE_NULL) {
108 		TEE_FreeOperation(session->processing->tee_op_handle);
109 		session->processing->tee_op_handle = TEE_HANDLE_NULL;
110 	}
111 
112 	TEE_Free(session->processing->extra_ctx);
113 
114 	TEE_Free(session->processing);
115 	session->processing = NULL;
116 }
117 
118 size_t get_object_key_bit_size(struct pkcs11_object *obj)
119 {
120 	uint32_t a_size = 0;
121 	struct obj_attrs *attrs = obj->attributes;
122 
123 	switch (get_key_type(attrs)) {
124 	case PKCS11_CKK_AES:
125 	case PKCS11_CKK_GENERIC_SECRET:
126 	case PKCS11_CKK_MD5_HMAC:
127 	case PKCS11_CKK_SHA_1_HMAC:
128 	case PKCS11_CKK_SHA224_HMAC:
129 	case PKCS11_CKK_SHA256_HMAC:
130 	case PKCS11_CKK_SHA384_HMAC:
131 	case PKCS11_CKK_SHA512_HMAC:
132 		if (get_attribute_ptr(attrs, PKCS11_CKA_VALUE, NULL, &a_size))
133 			return 0;
134 
135 		return a_size * 8;
136 	default:
137 		TEE_Panic(0);
138 		return 0;
139 	}
140 }
141 
142 static enum pkcs11_rc generate_random_key_value(struct obj_attrs **head)
143 {
144 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
145 	void *data = NULL;
146 	uint32_t data_size = 0;
147 	uint32_t value_len = 0;
148 	void *value = NULL;
149 
150 	if (!*head)
151 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
152 
153 	rc = get_attribute_ptr(*head, PKCS11_CKA_VALUE_LEN, &data, &data_size);
154 	if (rc || data_size != sizeof(uint32_t)) {
155 		DMSG("%s", rc ? "No attribute value_len found" :
156 		     "Invalid size for attribute VALUE_LEN");
157 
158 		return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID;
159 	}
160 	TEE_MemMove(&value_len, data, data_size);
161 
162 	/* Remove the default empty value attribute if found */
163 	rc = remove_empty_attribute(head, PKCS11_CKA_VALUE);
164 	if (rc != PKCS11_CKR_OK && rc != PKCS11_RV_NOT_FOUND)
165 		return PKCS11_CKR_GENERAL_ERROR;
166 
167 	value = TEE_Malloc(value_len, TEE_USER_MEM_HINT_NO_FILL_ZERO);
168 	if (!value)
169 		return PKCS11_CKR_DEVICE_MEMORY;
170 
171 	TEE_GenerateRandom(value, value_len);
172 
173 	rc = add_attribute(head, PKCS11_CKA_VALUE, value, value_len);
174 
175 	TEE_Free(value);
176 
177 	return rc;
178 }
179 
180 enum pkcs11_rc entry_generate_secret(struct pkcs11_client *client,
181 				     uint32_t ptypes, TEE_Param *params)
182 {
183 	const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
184 						TEE_PARAM_TYPE_NONE,
185 						TEE_PARAM_TYPE_MEMREF_OUTPUT,
186 						TEE_PARAM_TYPE_NONE);
187 	TEE_Param *ctrl = params;
188 	TEE_Param *out = params + 2;
189 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
190 	struct serialargs ctrlargs = { };
191 	struct pkcs11_session *session = NULL;
192 	struct pkcs11_attribute_head *proc_params = NULL;
193 	struct obj_attrs *head = NULL;
194 	struct pkcs11_object_head *template = NULL;
195 	size_t template_size = 0;
196 	uint32_t obj_handle = 0;
197 
198 	if (!client || ptypes != exp_pt ||
199 	    out->memref.size != sizeof(obj_handle))
200 		return PKCS11_CKR_ARGUMENTS_BAD;
201 
202 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
203 
204 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
205 	if (rc)
206 		return rc;
207 
208 	rc = serialargs_alloc_get_one_attribute(&ctrlargs, &proc_params);
209 	if (rc)
210 		goto out;
211 
212 	rc = serialargs_alloc_get_attributes(&ctrlargs, &template);
213 	if (rc)
214 		goto out;
215 
216 	if (serialargs_remaining_bytes(&ctrlargs)) {
217 		rc = PKCS11_CKR_ARGUMENTS_BAD;
218 		goto out;
219 	}
220 
221 	rc = get_ready_session(session);
222 	if (rc)
223 		goto out;
224 
225 	template_size = sizeof(*template) + template->attrs_size;
226 
227 	rc = check_mechanism_against_processing(session, proc_params->id,
228 						PKCS11_FUNCTION_GENERATE,
229 						PKCS11_FUNC_STEP_INIT);
230 	if (rc) {
231 		DMSG("Invalid mechanism %#"PRIx32": %#x", proc_params->id, rc);
232 		goto out;
233 	}
234 
235 	/*
236 	 * Prepare a clean initial state for the requested object attributes.
237 	 * Free temporary template once done.
238 	 */
239 	rc = create_attributes_from_template(&head, template, template_size,
240 					     NULL, PKCS11_FUNCTION_GENERATE,
241 					     proc_params->id,
242 					     PKCS11_CKO_UNDEFINED_ID);
243 	if (rc)
244 		goto out;
245 
246 	TEE_Free(template);
247 	template = NULL;
248 
249 	rc = check_created_attrs(head, NULL);
250 	if (rc)
251 		goto out;
252 
253 	rc = check_created_attrs_against_processing(proc_params->id, head);
254 	if (rc)
255 		goto out;
256 
257 	rc = check_created_attrs_against_token(session, head);
258 	if (rc)
259 		goto out;
260 
261 	/*
262 	 * Execute target processing and add value as attribute
263 	 * PKCS11_CKA_VALUE. Symm key generation: depends on target
264 	 * processing to be used.
265 	 */
266 	switch (proc_params->id) {
267 	case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
268 	case PKCS11_CKM_AES_KEY_GEN:
269 		/* Generate random of size specified by attribute VALUE_LEN */
270 		rc = generate_random_key_value(&head);
271 		if (rc)
272 			goto out;
273 		break;
274 
275 	default:
276 		rc = PKCS11_CKR_MECHANISM_INVALID;
277 		goto out;
278 	}
279 
280 	TEE_Free(proc_params);
281 	proc_params = NULL;
282 
283 	/*
284 	 * Object is ready, register it and return a handle.
285 	 */
286 	rc = create_object(session, head, &obj_handle);
287 	if (rc)
288 		goto out;
289 
290 	/*
291 	 * Now obj_handle (through the related struct pkcs11_object instance)
292 	 * owns the serialized buffer that holds the object attributes.
293 	 * We reset head to NULL as it is no more the buffer owner and would
294 	 * be freed at function out.
295 	 */
296 	head = NULL;
297 
298 	TEE_MemMove(out->memref.buffer, &obj_handle, sizeof(obj_handle));
299 	out->memref.size = sizeof(obj_handle);
300 
301 	DMSG("PKCS11 session %"PRIu32": generate secret %#"PRIx32,
302 	     session->handle, obj_handle);
303 
304 out:
305 	TEE_Free(proc_params);
306 	TEE_Free(template);
307 	TEE_Free(head);
308 
309 	return rc;
310 }
311 
312 enum pkcs11_rc alloc_get_tee_attribute_data(TEE_ObjectHandle tee_obj,
313 					    uint32_t attribute,
314 					    void **data, size_t *size)
315 {
316 	TEE_Result res = TEE_ERROR_GENERIC;
317 	void *ptr = NULL;
318 	uint32_t sz = 0;
319 
320 	res = TEE_GetObjectBufferAttribute(tee_obj, attribute, NULL, &sz);
321 	if (res != TEE_ERROR_SHORT_BUFFER)
322 		return PKCS11_CKR_FUNCTION_FAILED;
323 
324 	ptr = TEE_Malloc(sz, TEE_USER_MEM_HINT_NO_FILL_ZERO);
325 	if (!ptr)
326 		return PKCS11_CKR_DEVICE_MEMORY;
327 
328 	res = TEE_GetObjectBufferAttribute(tee_obj, attribute, ptr, &sz);
329 	if (res) {
330 		TEE_Free(ptr);
331 	} else {
332 		*data = ptr;
333 		*size = sz;
334 	}
335 
336 	return tee2pkcs_error(res);
337 }
338 
339 enum pkcs11_rc tee2pkcs_add_attribute(struct obj_attrs **head,
340 				      uint32_t pkcs11_id,
341 				      TEE_ObjectHandle tee_obj,
342 				      uint32_t tee_id)
343 {
344 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
345 	void *a_ptr = NULL;
346 	size_t a_size = 0;
347 
348 	rc = alloc_get_tee_attribute_data(tee_obj, tee_id, &a_ptr, &a_size);
349 	if (rc)
350 		goto out;
351 
352 	rc = add_attribute(head, pkcs11_id, a_ptr, a_size);
353 
354 	TEE_Free(a_ptr);
355 
356 out:
357 	if (rc)
358 		EMSG("Failed TEE attribute %#"PRIx32" for %#"PRIx32"/%s",
359 		     tee_id, pkcs11_id, id2str_attr(pkcs11_id));
360 	return rc;
361 }
362 
363 enum pkcs11_rc entry_generate_key_pair(struct pkcs11_client *client,
364 				       uint32_t ptypes, TEE_Param *params)
365 {
366 	const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
367 						TEE_PARAM_TYPE_NONE,
368 						TEE_PARAM_TYPE_MEMREF_OUTPUT,
369 						TEE_PARAM_TYPE_NONE);
370 	TEE_Param *ctrl = params;
371 	TEE_Param *out = params + 2;
372 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
373 	struct serialargs ctrlargs = { };
374 	struct pkcs11_session *session = NULL;
375 	struct pkcs11_attribute_head *proc_params = NULL;
376 	struct obj_attrs *pub_head = NULL;
377 	struct obj_attrs *priv_head = NULL;
378 	struct pkcs11_object_head *pub_template = NULL;
379 	struct pkcs11_object_head *priv_template = NULL;
380 	struct pkcs11_object *object = NULL;
381 	size_t pub_template_size = 0;
382 	size_t priv_template_size = 0;
383 	uint32_t pubkey_handle = 0;
384 	uint32_t privkey_handle = 0;
385 	uint32_t *hdl_ptr = NULL;
386 	size_t out_ref_size = sizeof(pubkey_handle) + sizeof(privkey_handle);
387 
388 	if (!client || ptypes != exp_pt || out->memref.size != out_ref_size)
389 		return PKCS11_CKR_ARGUMENTS_BAD;
390 
391 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
392 
393 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
394 	if (rc)
395 		return rc;
396 
397 	rc = serialargs_alloc_get_one_attribute(&ctrlargs, &proc_params);
398 	if (rc)
399 		goto out;
400 
401 	rc = serialargs_alloc_get_attributes(&ctrlargs, &pub_template);
402 	if (rc)
403 		goto out;
404 
405 	rc = serialargs_alloc_get_attributes(&ctrlargs, &priv_template);
406 	if (rc)
407 		goto out;
408 
409 	if (serialargs_remaining_bytes(&ctrlargs)) {
410 		rc = PKCS11_CKR_ARGUMENTS_BAD;
411 		goto out;
412 	}
413 
414 	rc = get_ready_session(session);
415 	if (rc)
416 		goto out;
417 
418 	rc = check_mechanism_against_processing(session, proc_params->id,
419 						PKCS11_FUNCTION_GENERATE_PAIR,
420 						PKCS11_FUNC_STEP_INIT);
421 	if (rc)
422 		goto out;
423 
424 	pub_template_size = sizeof(*pub_template) + pub_template->attrs_size;
425 
426 	rc = create_attributes_from_template(&pub_head, pub_template,
427 					     pub_template_size, NULL,
428 					     PKCS11_FUNCTION_GENERATE_PAIR,
429 					     proc_params->id,
430 					     PKCS11_CKO_PUBLIC_KEY);
431 	if (rc)
432 		goto out;
433 
434 	TEE_Free(pub_template);
435 	pub_template = NULL;
436 
437 	priv_template_size = sizeof(*priv_template) +
438 			     priv_template->attrs_size;
439 
440 	rc = create_attributes_from_template(&priv_head, priv_template,
441 					     priv_template_size, NULL,
442 					     PKCS11_FUNCTION_GENERATE_PAIR,
443 					     proc_params->id,
444 					     PKCS11_CKO_PRIVATE_KEY);
445 	if (rc)
446 		goto out;
447 
448 	TEE_Free(priv_template);
449 	priv_template = NULL;
450 
451 	/* Generate CKA_ID for keys if not specified by the templates */
452 	rc = add_missing_attribute_id(&pub_head, &priv_head);
453 	if (rc)
454 		goto out;
455 
456 	/* Check created object against processing and token state */
457 	rc = check_created_attrs(pub_head, priv_head);
458 	if (rc)
459 		goto out;
460 
461 	rc = check_created_attrs_against_processing(proc_params->id, pub_head);
462 	if (rc)
463 		goto out;
464 
465 	rc = check_created_attrs_against_processing(proc_params->id,
466 						    priv_head);
467 	if (rc)
468 		goto out;
469 
470 	rc = check_created_attrs_against_token(session, pub_head);
471 	if (rc)
472 		goto out;
473 
474 	rc = check_access_attrs_against_token(session, pub_head);
475 	if (rc)
476 		goto out;
477 
478 	rc = check_created_attrs_against_token(session, priv_head);
479 	if (rc)
480 		goto out;
481 
482 	rc = check_access_attrs_against_token(session, priv_head);
483 	if (rc)
484 		goto out;
485 
486 	/* Generate key pair */
487 	switch (proc_params->id) {
488 	default:
489 		rc = PKCS11_CKR_MECHANISM_INVALID;
490 		break;
491 	}
492 	if (rc)
493 		goto out;
494 
495 	TEE_Free(proc_params);
496 	proc_params = NULL;
497 
498 	/*
499 	 * Object is ready, register it and return a handle.
500 	 */
501 	rc = create_object(session, pub_head, &pubkey_handle);
502 	if (rc)
503 		goto out;
504 
505 	/*
506 	 * Now obj_handle (through the related struct pkcs11_object instance)
507 	 * owns the serialized buffer that holds the object attributes.
508 	 * We reset local pub_head to NULL to mark that ownership has been
509 	 * transferred.
510 	 */
511 	pub_head = NULL;
512 
513 	rc = create_object(session, priv_head, &privkey_handle);
514 	if (rc)
515 		goto out;
516 
517 	/* Ownership has been transferred so mark it with NULL */
518 	priv_head = NULL;
519 
520 	hdl_ptr = (uint32_t *)out->memref.buffer;
521 
522 	TEE_MemMove(hdl_ptr, &pubkey_handle, sizeof(pubkey_handle));
523 	TEE_MemMove(hdl_ptr + 1, &privkey_handle, sizeof(privkey_handle));
524 
525 	pubkey_handle = 0;
526 	privkey_handle = 0;
527 
528 	DMSG("PKCS11 session %"PRIu32": create key pair %#"PRIx32"/%#"PRIx32,
529 	     session->handle, privkey_handle, pubkey_handle);
530 
531 out:
532 	if (pubkey_handle) {
533 		object = pkcs11_handle2object(pubkey_handle, session);
534 		if (!object)
535 			TEE_Panic(0);
536 		destroy_object(session, object, false);
537 	}
538 	TEE_Free(priv_head);
539 	TEE_Free(pub_head);
540 	TEE_Free(priv_template);
541 	TEE_Free(pub_template);
542 	TEE_Free(proc_params);
543 
544 	return rc;
545 }
546 
547 /*
548  * entry_processing_init - Generic entry for initializing a processing
549  *
550  * @client = client reference
551  * @ptype = Invocation parameter types
552  * @params = Invocation parameters reference
553  * @function - encrypt, decrypt, sign, verify, digest, ...
554  */
555 enum pkcs11_rc entry_processing_init(struct pkcs11_client *client,
556 				     uint32_t ptypes, TEE_Param *params,
557 				     enum processing_func function)
558 {
559 	const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
560 						TEE_PARAM_TYPE_NONE,
561 						TEE_PARAM_TYPE_NONE,
562 						TEE_PARAM_TYPE_NONE);
563 	TEE_Param *ctrl = params;
564 	enum pkcs11_rc rc = PKCS11_CKR_OK;
565 	struct serialargs ctrlargs = { };
566 	struct pkcs11_session *session = NULL;
567 	struct pkcs11_attribute_head *proc_params = NULL;
568 	uint32_t key_handle = 0;
569 	struct pkcs11_object *obj = NULL;
570 
571 	if (!client || ptypes != exp_pt)
572 		return PKCS11_CKR_ARGUMENTS_BAD;
573 
574 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
575 
576 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
577 	if (rc)
578 		return rc;
579 
580 	if (function != PKCS11_FUNCTION_DIGEST) {
581 		rc = serialargs_get(&ctrlargs, &key_handle, sizeof(uint32_t));
582 		if (rc)
583 			return rc;
584 	}
585 
586 	rc = serialargs_alloc_get_one_attribute(&ctrlargs, &proc_params);
587 	if (rc)
588 		return rc;
589 
590 	if (serialargs_remaining_bytes(&ctrlargs)) {
591 		rc = PKCS11_CKR_ARGUMENTS_BAD;
592 		goto out;
593 	}
594 
595 	rc = get_ready_session(session);
596 	if (rc)
597 		goto out;
598 
599 	if (function != PKCS11_FUNCTION_DIGEST) {
600 		obj = pkcs11_handle2object(key_handle, session);
601 		if (!obj) {
602 			rc = PKCS11_CKR_KEY_HANDLE_INVALID;
603 			goto out;
604 		}
605 	}
606 
607 	rc = set_processing_state(session, function, obj, NULL);
608 	if (rc)
609 		goto out;
610 
611 	rc = check_mechanism_against_processing(session, proc_params->id,
612 						function,
613 						PKCS11_FUNC_STEP_INIT);
614 	if (rc)
615 		goto out;
616 
617 	if (obj) {
618 		rc = check_parent_attrs_against_processing(proc_params->id,
619 							   function,
620 							   obj->attributes);
621 		if (rc)
622 			goto out;
623 
624 		rc = check_access_attrs_against_token(session,
625 						      obj->attributes);
626 		if (rc)
627 			goto out;
628 	}
629 
630 	if (processing_is_tee_symm(proc_params->id))
631 		rc = init_symm_operation(session, function, proc_params, obj);
632 	else if (processing_is_tee_digest(proc_params->id))
633 		rc = init_digest_operation(session, proc_params);
634 	else
635 		rc = PKCS11_CKR_MECHANISM_INVALID;
636 
637 	if (rc == PKCS11_CKR_OK) {
638 		session->processing->mecha_type = proc_params->id;
639 		DMSG("PKCS11 session %"PRIu32": init processing %s %s",
640 		     session->handle, id2str_proc(proc_params->id),
641 		     id2str_function(function));
642 	}
643 
644 out:
645 	if (rc && session)
646 		release_active_processing(session);
647 
648 	TEE_Free(proc_params);
649 
650 	return rc;
651 }
652 
653 /*
654  * entry_processing_step - Generic entry on active processing
655  *
656  * @client = client reference
657  * @ptype = Invocation parameter types
658  * @params = Invocation parameters reference
659  * @function - encrypt, decrypt, sign, verify, digest, ...
660  * @step - update, oneshot, final
661  */
662 enum pkcs11_rc entry_processing_step(struct pkcs11_client *client,
663 				     uint32_t ptypes, TEE_Param *params,
664 				     enum processing_func function,
665 				     enum processing_step step)
666 {
667 	TEE_Param *ctrl = params;
668 	enum pkcs11_rc rc = PKCS11_CKR_OK;
669 	struct serialargs ctrlargs = { };
670 	struct pkcs11_session *session = NULL;
671 	enum pkcs11_mechanism_id mecha_type = PKCS11_CKM_UNDEFINED_ID;
672 	uint32_t key_handle = 0;
673 	struct pkcs11_object *obj = NULL;
674 
675 	if (!client ||
676 	    TEE_PARAM_TYPE_GET(ptypes, 0) != TEE_PARAM_TYPE_MEMREF_INOUT)
677 		return PKCS11_CKR_ARGUMENTS_BAD;
678 
679 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
680 
681 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
682 	if (rc)
683 		return rc;
684 
685 	if (step == PKCS11_FUNC_STEP_UPDATE_KEY) {
686 		assert(function == PKCS11_FUNCTION_DIGEST);
687 
688 		rc = serialargs_get(&ctrlargs, &key_handle, sizeof(uint32_t));
689 		if (rc)
690 			return rc;
691 	}
692 
693 	if (serialargs_remaining_bytes(&ctrlargs))
694 		return PKCS11_CKR_ARGUMENTS_BAD;
695 
696 	rc = get_active_session(session, function);
697 	if (rc)
698 		return rc;
699 
700 	if (step == PKCS11_FUNC_STEP_UPDATE_KEY) {
701 		assert(function == PKCS11_FUNCTION_DIGEST);
702 
703 		obj = pkcs11_handle2object(key_handle, session);
704 		if (!obj) {
705 			rc = PKCS11_CKR_KEY_HANDLE_INVALID;
706 			goto out;
707 		}
708 
709 		rc = check_access_attrs_against_token(session,
710 						      obj->attributes);
711 		if (rc) {
712 			rc = PKCS11_CKR_KEY_HANDLE_INVALID;
713 			goto out;
714 		}
715 	}
716 
717 	mecha_type = session->processing->mecha_type;
718 	rc = check_mechanism_against_processing(session, mecha_type,
719 						function, step);
720 	if (rc)
721 		goto out;
722 
723 	if (processing_is_tee_symm(mecha_type))
724 		rc = step_symm_operation(session, function, step,
725 					 ptypes, params);
726 	else if (processing_is_tee_digest(mecha_type))
727 		rc = step_digest_operation(session, step, obj, ptypes, params);
728 	else
729 		rc = PKCS11_CKR_MECHANISM_INVALID;
730 
731 	if (rc == PKCS11_CKR_OK && (step == PKCS11_FUNC_STEP_UPDATE ||
732 				    step == PKCS11_FUNC_STEP_UPDATE_KEY)) {
733 		session->processing->updated = true;
734 		DMSG("PKCS11 session%"PRIu32": processing %s %s",
735 		     session->handle, id2str_proc(mecha_type),
736 		     id2str_function(function));
737 	}
738 
739 out:
740 	switch (step) {
741 	case PKCS11_FUNC_STEP_UPDATE:
742 	case PKCS11_FUNC_STEP_UPDATE_KEY:
743 		if (rc != PKCS11_CKR_OK && rc != PKCS11_CKR_BUFFER_TOO_SMALL)
744 			release_active_processing(session);
745 		break;
746 	default:
747 		/* ONESHOT and FINAL terminates processing on success */
748 		if (rc != PKCS11_CKR_BUFFER_TOO_SMALL)
749 			release_active_processing(session);
750 		break;
751 	}
752 
753 	return rc;
754 }
755 
756 enum pkcs11_rc entry_processing_key(struct pkcs11_client *client,
757 				    uint32_t ptypes, TEE_Param *params,
758 				    enum processing_func function)
759 {
760 	const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
761 						TEE_PARAM_TYPE_NONE,
762 						TEE_PARAM_TYPE_MEMREF_OUTPUT,
763 						TEE_PARAM_TYPE_NONE);
764 	TEE_Param *ctrl = params;
765 	TEE_Param *out = params + 2;
766 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
767 	struct serialargs ctrlargs = { };
768 	struct pkcs11_session *session = NULL;
769 	struct pkcs11_attribute_head *proc_params = NULL;
770 	struct pkcs11_object_head *template = NULL;
771 	uint32_t parent_handle = 0;
772 	uint32_t obj_handle = 0;
773 	struct pkcs11_object *parent = NULL;
774 	struct obj_attrs *head = NULL;
775 	size_t template_size = 0;
776 	void *out_buf = NULL;
777 	uint32_t out_size = 0;
778 
779 	if (!client || ptypes != exp_pt ||
780 	    out->memref.size != sizeof(obj_handle))
781 		return PKCS11_CKR_ARGUMENTS_BAD;
782 
783 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
784 
785 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
786 	if (rc)
787 		return rc;
788 
789 	rc = serialargs_get(&ctrlargs, &parent_handle, sizeof(uint32_t));
790 	if (rc)
791 		return rc;
792 
793 	rc = serialargs_alloc_get_one_attribute(&ctrlargs, &proc_params);
794 	if (rc)
795 		goto out_free;
796 
797 	rc = serialargs_alloc_get_attributes(&ctrlargs, &template);
798 	if (rc)
799 		goto out_free;
800 
801 	if (serialargs_remaining_bytes(&ctrlargs)) {
802 		rc = PKCS11_CKR_ARGUMENTS_BAD;
803 		goto out_free;
804 	}
805 
806 	/* Return error if processing already active */
807 	rc = get_ready_session(session);
808 	if (rc)
809 		goto out_free;
810 
811 	/* Check parent handle */
812 	parent = pkcs11_handle2object(parent_handle, session);
813 	if (!parent) {
814 		rc = PKCS11_CKR_KEY_HANDLE_INVALID;
815 		goto out_free;
816 	}
817 
818 	/* Check if mechanism can be used for derivation function */
819 	rc = check_mechanism_against_processing(session, proc_params->id,
820 						function,
821 						PKCS11_FUNC_STEP_INIT);
822 	if (rc)
823 		goto out_free;
824 
825 	/* Set the processing state to active */
826 	rc = set_processing_state(session, function, parent, NULL);
827 	if (rc)
828 		goto out_free;
829 
830 	/*
831 	 * Check if base/parent key has CKA_DERIVE set and its key type is
832 	 * compatible with the mechanism passed
833 	 */
834 	rc = check_parent_attrs_against_processing(proc_params->id, function,
835 						   parent->attributes);
836 	if (rc) {
837 		/*
838 		 * CKR_KEY_FUNCTION_NOT_PERMITTED is not in the list of errors
839 		 * specified with C_Derive/Unwrap() in the specification. So
840 		 * return the next most appropriate error.
841 		 */
842 		if (rc == PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED)
843 			rc = PKCS11_CKR_KEY_TYPE_INCONSISTENT;
844 		goto out;
845 	}
846 
847 	/* Check access of base/parent key */
848 	rc = check_access_attrs_against_token(session, parent->attributes);
849 	if (rc)
850 		goto out;
851 
852 	template_size = sizeof(*template) + template->attrs_size;
853 	/*
854 	 * Prepare a clean initial state for the requested object attributes
855 	 * using base/parent key attributes. Free temporary template once done.
856 	 */
857 	rc = create_attributes_from_template(&head, template, template_size,
858 					     parent->attributes,
859 					     function,
860 					     proc_params->id,
861 					     PKCS11_CKO_UNDEFINED_ID);
862 	if (rc)
863 		goto out;
864 
865 	TEE_Free(template);
866 	template = NULL;
867 
868 	/* check_created_attrs() is called later once key size is known */
869 
870 	rc = check_created_attrs_against_processing(proc_params->id, head);
871 	if (rc)
872 		goto out;
873 
874 	rc = check_created_attrs_against_token(session, head);
875 	if (rc)
876 		goto out;
877 
878 	if (processing_is_tee_symm(proc_params->id)) {
879 		/*
880 		 * These derivation mechanism require encryption to be
881 		 * performed on the data passed in proc_params by parent
882 		 * key. Hence pass function as PKCS11_FUNCTION_ENCRYPT
883 		 * to init_symm_operation()
884 		 */
885 		rc = init_symm_operation(session, PKCS11_FUNCTION_ENCRYPT,
886 					 proc_params, parent);
887 		if (rc)
888 			goto out;
889 
890 		session->processing->mecha_type = proc_params->id;
891 
892 		rc = derive_key_by_symm_enc(session, &out_buf, &out_size);
893 		if (rc)
894 			goto out;
895 	} else {
896 		rc = PKCS11_CKR_MECHANISM_INVALID;
897 		goto out;
898 	}
899 
900 	rc = set_key_data(&head, out_buf, out_size);
901 	if (rc)
902 		goto out;
903 
904 	TEE_Free(out_buf);
905 	out_buf = NULL;
906 
907 	TEE_Free(proc_params);
908 	proc_params = NULL;
909 
910 	/*
911 	 * Object is ready, register it and return a handle.
912 	 */
913 	rc = create_object(session, head, &obj_handle);
914 	if (rc)
915 		goto out;
916 
917 	/*
918 	 * Now obj_handle (through the related struct pkcs11_object instance)
919 	 * owns the serialized buffer that holds the object attributes.
920 	 * We reset head to NULL as it is no more the buffer owner and would
921 	 * be freed at function out.
922 	 */
923 	head = NULL;
924 
925 	TEE_MemMove(out->memref.buffer, &obj_handle, sizeof(obj_handle));
926 	out->memref.size = sizeof(obj_handle);
927 
928 	DMSG("PKCS11 session %"PRIu32": derive secret %#"PRIx32,
929 	     session->handle, obj_handle);
930 
931 out:
932 	release_active_processing(session);
933 out_free:
934 	TEE_Free(proc_params);
935 	TEE_Free(template);
936 	TEE_Free(head);
937 	TEE_Free(out_buf);
938 
939 	return rc;
940 }
941 
942 enum pkcs11_rc entry_release_active_processing(struct pkcs11_client *client,
943 					       uint32_t ptypes,
944 					       TEE_Param *params)
945 {
946 	const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
947 						TEE_PARAM_TYPE_NONE,
948 						TEE_PARAM_TYPE_NONE,
949 						TEE_PARAM_TYPE_NONE);
950 	TEE_Param *ctrl = params;
951 	enum pkcs11_rc rc = PKCS11_CKR_OK;
952 	struct serialargs ctrlargs = { };
953 	struct pkcs11_session *session = NULL;
954 	enum processing_func function = PKCS11_FUNCTION_UNKNOWN;
955 	uint32_t cmd = 0;
956 
957 	if (!client || ptypes != exp_pt)
958 		return PKCS11_CKR_ARGUMENTS_BAD;
959 
960 	serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size);
961 
962 	rc = serialargs_get_session_from_handle(&ctrlargs, client, &session);
963 	if (rc)
964 		return rc;
965 
966 	rc = serialargs_get_u32(&ctrlargs, &cmd);
967 
968 	if (serialargs_remaining_bytes(&ctrlargs))
969 		return PKCS11_CKR_ARGUMENTS_BAD;
970 
971 	function = func_for_cmd(cmd);
972 	if (function == PKCS11_FUNCTION_UNKNOWN)
973 		return PKCS11_CKR_ARGUMENTS_BAD;
974 
975 	rc = get_active_session(session, function);
976 	if (rc)
977 		return rc;
978 
979 	release_active_processing(session);
980 
981 	DMSG("PKCS11 session %"PRIu32": release processing", session->handle);
982 
983 	return PKCS11_CKR_OK;
984 }
985