xref: /optee_os/ta/pkcs11/src/pkcs11_attributes.c (revision 2391d619a7d07dbc2526a2a6b8ba01a44589becb)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2017-2020, Linaro Limited
4  */
5 
6 #include <assert.h>
7 #include <inttypes.h>
8 #include <pkcs11_ta.h>
9 #include <stdlib.h>
10 #include <string_ext.h>
11 #include <tee_internal_api_extensions.h>
12 #include <tee_internal_api.h>
13 #include <trace.h>
14 #include <util.h>
15 
16 #include "attributes.h"
17 #include "handle.h"
18 #include "pkcs11_attributes.h"
19 #include "pkcs11_helpers.h"
20 #include "pkcs11_token.h"
21 #include "sanitize_object.h"
22 #include "serializer.h"
23 #include "token_capabilities.h"
24 
25 static uint32_t pkcs11_func2ckfm(enum processing_func function)
26 {
27 	switch (function) {
28 	case PKCS11_FUNCTION_DIGEST:
29 		return PKCS11_CKFM_DIGEST;
30 	case PKCS11_FUNCTION_GENERATE:
31 		return PKCS11_CKFM_GENERATE;
32 	case PKCS11_FUNCTION_GENERATE_PAIR:
33 		return PKCS11_CKFM_GENERATE_KEY_PAIR;
34 	case PKCS11_FUNCTION_DERIVE:
35 		return PKCS11_CKFM_DERIVE;
36 	case PKCS11_FUNCTION_WRAP:
37 		return PKCS11_CKFM_WRAP;
38 	case PKCS11_FUNCTION_UNWRAP:
39 		return PKCS11_CKFM_UNWRAP;
40 	case PKCS11_FUNCTION_ENCRYPT:
41 		return PKCS11_CKFM_ENCRYPT;
42 	case PKCS11_FUNCTION_DECRYPT:
43 		return PKCS11_CKFM_DECRYPT;
44 	case PKCS11_FUNCTION_SIGN:
45 		return PKCS11_CKFM_SIGN;
46 	case PKCS11_FUNCTION_VERIFY:
47 		return PKCS11_CKFM_VERIFY;
48 	case PKCS11_FUNCTION_SIGN_RECOVER:
49 		return PKCS11_CKFM_SIGN_RECOVER;
50 	case PKCS11_FUNCTION_VERIFY_RECOVER:
51 		return PKCS11_CKFM_VERIFY_RECOVER;
52 	default:
53 		return 0;
54 	}
55 }
56 
57 enum pkcs11_rc
58 check_mechanism_against_processing(struct pkcs11_session *session,
59 				   enum pkcs11_mechanism_id mechanism_type,
60 				   enum processing_func function,
61 				   enum processing_step step)
62 {
63 	bool allowed = false;
64 
65 	switch (step) {
66 	case PKCS11_FUNC_STEP_INIT:
67 		switch (function) {
68 		case PKCS11_FUNCTION_IMPORT:
69 		case PKCS11_FUNCTION_COPY:
70 		case PKCS11_FUNCTION_MODIFY:
71 		case PKCS11_FUNCTION_DESTROY:
72 			return PKCS11_CKR_OK;
73 		default:
74 			break;
75 		}
76 		/*
77 		 * Check that the returned PKCS11_CKFM_* flag from
78 		 * pkcs11_func2ckfm() is among the ones from
79 		 * mechanism_supported_flags().
80 		 */
81 		allowed = mechanism_supported_flags(mechanism_type) &
82 			  pkcs11_func2ckfm(function);
83 		break;
84 
85 	case PKCS11_FUNC_STEP_ONESHOT:
86 		if (session->processing->always_authen &&
87 		    !session->processing->relogged)
88 			return PKCS11_CKR_USER_NOT_LOGGED_IN;
89 
90 		if (session->processing->step == PKCS11_FUNC_STEP_UPDATE ||
91 		    session->processing->step == PKCS11_FUNC_STEP_FINAL) {
92 			EMSG("Cannot perform one-shot on active processing");
93 			return PKCS11_CKR_OPERATION_ACTIVE;
94 		}
95 
96 		allowed = true;
97 		break;
98 
99 	case PKCS11_FUNC_STEP_UPDATE:
100 		if (session->processing->always_authen &&
101 		    !session->processing->relogged)
102 			return PKCS11_CKR_USER_NOT_LOGGED_IN;
103 
104 		if (session->processing->step == PKCS11_FUNC_STEP_ONESHOT ||
105 		    session->processing->step == PKCS11_FUNC_STEP_FINAL) {
106 			EMSG("Cannot perform update on finalized processing");
107 			return PKCS11_CKR_OPERATION_ACTIVE;
108 		}
109 
110 		allowed = !mechanism_is_one_shot_only(mechanism_type);
111 		break;
112 
113 	case PKCS11_FUNC_STEP_UPDATE_KEY:
114 		assert(function == PKCS11_FUNCTION_DIGEST);
115 
116 		if (session->processing->always_authen &&
117 		    !session->processing->relogged)
118 			return PKCS11_CKR_USER_NOT_LOGGED_IN;
119 
120 		allowed = true;
121 		break;
122 
123 	case PKCS11_FUNC_STEP_FINAL:
124 		if (session->processing->always_authen &&
125 		    !session->processing->relogged)
126 			return PKCS11_CKR_USER_NOT_LOGGED_IN;
127 
128 		if (session->processing->step == PKCS11_FUNC_STEP_ONESHOT) {
129 			EMSG("Cannot perform final on oneshot processing");
130 			return PKCS11_CKR_OPERATION_ACTIVE;
131 		}
132 		return PKCS11_CKR_OK;
133 
134 	default:
135 		TEE_Panic(step);
136 		break;
137 	}
138 
139 	if (!allowed) {
140 		EMSG("Processing %#x/%s not permitted (%u/%u)",
141 		     (unsigned int)mechanism_type, id2str_proc(mechanism_type),
142 		     function, step);
143 		return PKCS11_CKR_MECHANISM_INVALID;
144 	}
145 
146 	return PKCS11_CKR_OK;
147 }
148 
149 /*
150  * Object default boolean attributes as per PKCS#11
151  */
152 static uint8_t *pkcs11_object_default_boolprop(uint32_t attribute)
153 {
154 	static const uint8_t bool_true = 1;
155 	static const uint8_t bool_false;
156 
157 	switch (attribute) {
158 	/* As per PKCS#11 default value */
159 	case PKCS11_CKA_MODIFIABLE:
160 	case PKCS11_CKA_COPYABLE:
161 	case PKCS11_CKA_DESTROYABLE:
162 		return (uint8_t *)&bool_true;
163 	case PKCS11_CKA_TOKEN:
164 	case PKCS11_CKA_PRIVATE:
165 	case PKCS11_CKA_WRAP_WITH_TRUSTED:
166 	case PKCS11_CKA_ALWAYS_AUTHENTICATE:
167 	case PKCS11_CKA_SENSITIVE:
168 		return (uint8_t *)&bool_false;
169 	/* Token specific default value */
170 	case PKCS11_CKA_SIGN:
171 	case PKCS11_CKA_VERIFY:
172 	case PKCS11_CKA_DERIVE:
173 	case PKCS11_CKA_ENCRYPT:
174 	case PKCS11_CKA_DECRYPT:
175 	case PKCS11_CKA_SIGN_RECOVER:
176 	case PKCS11_CKA_VERIFY_RECOVER:
177 	case PKCS11_CKA_WRAP:
178 	case PKCS11_CKA_UNWRAP:
179 	case PKCS11_CKA_EXTRACTABLE:
180 	case PKCS11_CKA_TRUSTED:
181 		return (uint8_t *)&bool_false;
182 	default:
183 		DMSG("No default for boolprop attribute %#"PRIx32, attribute);
184 		return NULL;
185 	}
186 }
187 
188 /*
189  * Object expects several boolean attributes to be set to a default value
190  * or to a validate client configuration value. This function append the input
191  * attribute (id/size/value) in the serialized object.
192  */
193 static enum pkcs11_rc pkcs11_import_object_boolprop(struct obj_attrs **out,
194 						    struct obj_attrs *templ,
195 						    uint32_t attribute)
196 {
197 	enum pkcs11_rc rc = PKCS11_CKR_OK;
198 	uint8_t bbool = 0;
199 	uint32_t size = sizeof(uint8_t);
200 	void *attr = NULL;
201 
202 	rc = get_attribute(templ, attribute, &bbool, &size);
203 	if (rc) {
204 		if (rc != PKCS11_RV_NOT_FOUND)
205 			return rc;
206 		attr = pkcs11_object_default_boolprop(attribute);
207 		if (!attr)
208 			return PKCS11_CKR_TEMPLATE_INCOMPLETE;
209 	} else {
210 		attr = &bbool;
211 	}
212 
213 	/* Boolean attributes are 1byte in the ABI, no alignment issue */
214 	return add_attribute(out, attribute, attr, sizeof(uint8_t));
215 }
216 
217 static enum pkcs11_rc set_mandatory_boolprops(struct obj_attrs **out,
218 					      struct obj_attrs *temp,
219 					      uint32_t const *bp,
220 					      size_t bp_count)
221 {
222 	enum pkcs11_rc rc = PKCS11_CKR_OK;
223 	size_t n = 0;
224 
225 	for (n = 0; n < bp_count; n++) {
226 		rc = pkcs11_import_object_boolprop(out, temp, bp[n]);
227 		if (rc)
228 			return rc;
229 	}
230 
231 	return rc;
232 }
233 
234 static enum pkcs11_rc set_mandatory_attributes(struct obj_attrs **out,
235 					       struct obj_attrs *temp,
236 					       uint32_t const *attrs,
237 					       size_t attrs_count)
238 {
239 	enum pkcs11_rc rc = PKCS11_CKR_OK;
240 	size_t n = 0;
241 
242 	for (n = 0; n < attrs_count; n++) {
243 		uint32_t size = 0;
244 		void *value = NULL;
245 
246 		if (get_attribute_ptr(temp, attrs[n], &value, &size))
247 			return PKCS11_CKR_TEMPLATE_INCOMPLETE;
248 
249 		rc = add_attribute(out, attrs[n], value, size);
250 		if (rc)
251 			return rc;
252 	}
253 
254 	return rc;
255 }
256 
257 static enum pkcs11_rc get_default_value(enum pkcs11_attr_id id, void **value,
258 					uint32_t *size)
259 {
260 	/* should have been taken care of already */
261 	assert(!pkcs11_attr_is_boolean(id));
262 
263 	/* All other attributes have an empty default value */
264 	*value = NULL;
265 	*size = 0;
266 	return PKCS11_CKR_OK;
267 }
268 
269 static enum pkcs11_rc set_optional_attributes_with_def(struct obj_attrs **out,
270 						       struct obj_attrs *temp,
271 						       uint32_t const *attrs,
272 						       size_t attrs_count,
273 						       bool default_to_null)
274 {
275 	enum pkcs11_rc rc = PKCS11_CKR_OK;
276 	size_t n = 0;
277 
278 	for (n = 0; n < attrs_count; n++) {
279 		uint32_t size = 0;
280 		void *value = NULL;
281 
282 		rc = get_attribute_ptr(temp, attrs[n], &value, &size);
283 		if (rc == PKCS11_RV_NOT_FOUND) {
284 			if (default_to_null) {
285 				rc = get_default_value(attrs[n], &value, &size);
286 			} else {
287 				rc = PKCS11_CKR_OK;
288 				continue;
289 			}
290 		}
291 		if (rc)
292 			return rc;
293 
294 		rc = add_attribute(out, attrs[n], value, size);
295 		if (rc)
296 			return rc;
297 	}
298 
299 	return rc;
300 }
301 
302 static enum pkcs11_rc set_attributes_opt_or_null(struct obj_attrs **out,
303 						 struct obj_attrs *temp,
304 						 uint32_t const *attrs,
305 						 size_t attrs_count)
306 {
307 	return set_optional_attributes_with_def(out, temp, attrs, attrs_count,
308 						true /* defaults to empty */);
309 }
310 
311 static enum pkcs11_rc set_optional_attributes(struct obj_attrs **out,
312 					      struct obj_attrs *temp,
313 					      uint32_t const *attrs,
314 					      size_t attrs_count)
315 {
316 	return set_optional_attributes_with_def(out, temp, attrs, attrs_count,
317 						false /* no default value */);
318 }
319 
320 /*
321  * Below are listed the mandated or optional expected attributes for
322  * PKCS#11 storage objects.
323  *
324  * Note: boolprops (mandated boolean attributes) PKCS11_CKA_ALWAYS_SENSITIVE,
325  * and PKCS11_CKA_NEVER_EXTRACTABLE are set by the token, not provided
326  * in the client template.
327  */
328 
329 /* PKCS#11 specification for any object (session/token) of the storage */
330 static const uint32_t any_object_boolprops[] = {
331 	PKCS11_CKA_TOKEN, PKCS11_CKA_PRIVATE,
332 	PKCS11_CKA_MODIFIABLE, PKCS11_CKA_COPYABLE, PKCS11_CKA_DESTROYABLE,
333 };
334 
335 static const uint32_t any_object_opt_or_null[] = {
336 	PKCS11_CKA_LABEL,
337 };
338 
339 /* PKCS#11 specification for raw data object (+any_object_xxx) */
340 const uint32_t raw_data_opt_or_null[] = {
341 	PKCS11_CKA_OBJECT_ID, PKCS11_CKA_APPLICATION, PKCS11_CKA_VALUE,
342 };
343 
344 /* PKCS#11 specification for any key object (+any_object_xxx) */
345 static const uint32_t any_key_boolprops[] = {
346 	PKCS11_CKA_DERIVE,
347 };
348 
349 static const uint32_t any_key_opt_or_null[] = {
350 	PKCS11_CKA_ID,
351 	PKCS11_CKA_START_DATE, PKCS11_CKA_END_DATE,
352 };
353 
354 static const uint32_t any_key_optional[] = {
355 	PKCS11_CKA_ALLOWED_MECHANISMS,
356 };
357 
358 /* PKCS#11 specification for any symmetric key (+any_key_xxx) */
359 static const uint32_t symm_key_boolprops[] = {
360 	PKCS11_CKA_ENCRYPT, PKCS11_CKA_DECRYPT,
361 	PKCS11_CKA_SIGN, PKCS11_CKA_VERIFY,
362 	PKCS11_CKA_WRAP, PKCS11_CKA_UNWRAP,
363 	PKCS11_CKA_SENSITIVE, PKCS11_CKA_EXTRACTABLE,
364 	PKCS11_CKA_WRAP_WITH_TRUSTED, PKCS11_CKA_TRUSTED,
365 };
366 
367 static const uint32_t symm_key_opt_or_null[] = {
368 	PKCS11_CKA_WRAP_TEMPLATE, PKCS11_CKA_UNWRAP_TEMPLATE,
369 	PKCS11_CKA_DERIVE_TEMPLATE, PKCS11_CKA_VALUE,
370 };
371 
372 static const uint32_t symm_key_optional[] = {
373 	PKCS11_CKA_VALUE_LEN,
374 };
375 
376 /* PKCS#11 specification for any asymmetric public key (+any_key_xxx) */
377 static const uint32_t public_key_boolprops[] = {
378 	PKCS11_CKA_ENCRYPT, PKCS11_CKA_VERIFY, PKCS11_CKA_VERIFY_RECOVER,
379 	PKCS11_CKA_WRAP,
380 	PKCS11_CKA_TRUSTED,
381 };
382 
383 static const uint32_t public_key_mandated[] = {
384 };
385 
386 static const uint32_t public_key_opt_or_null[] = {
387 	PKCS11_CKA_SUBJECT, PKCS11_CKA_WRAP_TEMPLATE,
388 	PKCS11_CKA_PUBLIC_KEY_INFO,
389 };
390 
391 /* PKCS#11 specification for any asymmetric private key (+any_key_xxx) */
392 static const uint32_t private_key_boolprops[] = {
393 	PKCS11_CKA_DECRYPT, PKCS11_CKA_SIGN, PKCS11_CKA_SIGN_RECOVER,
394 	PKCS11_CKA_UNWRAP,
395 	PKCS11_CKA_SENSITIVE, PKCS11_CKA_EXTRACTABLE,
396 	PKCS11_CKA_WRAP_WITH_TRUSTED, PKCS11_CKA_ALWAYS_AUTHENTICATE,
397 };
398 
399 static const uint32_t private_key_mandated[] = {
400 };
401 
402 static const uint32_t private_key_opt_or_null[] = {
403 	PKCS11_CKA_SUBJECT, PKCS11_CKA_UNWRAP_TEMPLATE,
404 	PKCS11_CKA_PUBLIC_KEY_INFO,
405 };
406 
407 /* PKCS#11 specification for any RSA key (+public/private_key_xxx) */
408 static const uint32_t rsa_pub_key_gen_mand[] = {
409 	PKCS11_CKA_MODULUS_BITS,
410 };
411 
412 static const uint32_t rsa_pub_key_create_mand[] = {
413 	PKCS11_CKA_MODULUS, PKCS11_CKA_PUBLIC_EXPONENT,
414 };
415 
416 static const uint32_t rsa_pub_key_gen_opt_or_null[] = {
417 	PKCS11_CKA_PUBLIC_EXPONENT,
418 };
419 
420 static const uint32_t rsa_priv_key_opt_or_null[] = {
421 	PKCS11_CKA_MODULUS, PKCS11_CKA_PUBLIC_EXPONENT,
422 	PKCS11_CKA_PRIVATE_EXPONENT,
423 	PKCS11_CKA_PRIME_1, PKCS11_CKA_PRIME_2,
424 	PKCS11_CKA_EXPONENT_1, PKCS11_CKA_EXPONENT_2, PKCS11_CKA_COEFFICIENT,
425 };
426 
427 /* PKCS#11 specification for any EC key (+public/private_key_xxx) */
428 static const uint32_t ec_public_key_mandated[] = {
429 	PKCS11_CKA_EC_PARAMS,
430 };
431 
432 static const uint32_t ec_public_key_opt_or_null[] = {
433 	PKCS11_CKA_EC_POINT,
434 };
435 
436 static const uint32_t ec_private_key_mandated[] = {
437 };
438 
439 static const uint32_t ec_private_key_opt_or_null[] = {
440 	PKCS11_CKA_EC_PARAMS,
441 	PKCS11_CKA_VALUE,
442 };
443 
444 static enum pkcs11_rc create_storage_attributes(struct obj_attrs **out,
445 						struct obj_attrs *temp)
446 {
447 	enum pkcs11_class_id class = PKCS11_CKO_UNDEFINED_ID;
448 	enum pkcs11_rc rc = PKCS11_CKR_OK;
449 
450 	rc = init_attributes_head(out);
451 	if (rc)
452 		return rc;
453 
454 	/* Object class is mandatory */
455 	class = get_class(temp);
456 	if (class == PKCS11_CKO_UNDEFINED_ID) {
457 		EMSG("Class attribute not found");
458 
459 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
460 	}
461 	rc = add_attribute(out, PKCS11_CKA_CLASS, &class, sizeof(uint32_t));
462 	if (rc)
463 		return rc;
464 
465 	rc = set_mandatory_boolprops(out, temp, any_object_boolprops,
466 				     ARRAY_SIZE(any_object_boolprops));
467 	if (rc)
468 		return rc;
469 
470 	return set_attributes_opt_or_null(out, temp, any_object_opt_or_null,
471 					  ARRAY_SIZE(any_object_opt_or_null));
472 }
473 
474 static enum pkcs11_rc create_genkey_attributes(struct obj_attrs **out,
475 					       struct obj_attrs *temp)
476 {
477 	uint32_t type = PKCS11_CKO_UNDEFINED_ID;
478 	enum pkcs11_rc rc = PKCS11_CKR_OK;
479 
480 	rc = create_storage_attributes(out, temp);
481 	if (rc)
482 		return rc;
483 
484 	type = get_key_type(temp);
485 	if (type == PKCS11_CKK_UNDEFINED_ID) {
486 		EMSG("Key type attribute not found");
487 
488 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
489 	}
490 	rc = add_attribute(out, PKCS11_CKA_KEY_TYPE, &type, sizeof(uint32_t));
491 	if (rc)
492 		return rc;
493 
494 	rc = set_mandatory_boolprops(out, temp, any_key_boolprops,
495 				     ARRAY_SIZE(any_key_boolprops));
496 	if (rc)
497 		return rc;
498 
499 	rc = set_attributes_opt_or_null(out, temp, any_key_opt_or_null,
500 					ARRAY_SIZE(any_key_opt_or_null));
501 	if (rc)
502 		return rc;
503 
504 	return set_optional_attributes(out, temp, any_key_optional,
505 				       ARRAY_SIZE(any_key_optional));
506 
507 }
508 
509 static enum pkcs11_rc create_symm_key_attributes(struct obj_attrs **out,
510 						 struct obj_attrs *temp)
511 {
512 	enum pkcs11_rc rc = PKCS11_CKR_OK;
513 
514 	assert(get_class(temp) == PKCS11_CKO_SECRET_KEY);
515 
516 	rc = create_genkey_attributes(out, temp);
517 	if (rc)
518 		return rc;
519 
520 	assert(get_class(*out) == PKCS11_CKO_SECRET_KEY);
521 
522 	switch (get_key_type(*out)) {
523 	case PKCS11_CKK_GENERIC_SECRET:
524 	case PKCS11_CKK_AES:
525 	case PKCS11_CKK_MD5_HMAC:
526 	case PKCS11_CKK_SHA_1_HMAC:
527 	case PKCS11_CKK_SHA256_HMAC:
528 	case PKCS11_CKK_SHA384_HMAC:
529 	case PKCS11_CKK_SHA512_HMAC:
530 	case PKCS11_CKK_SHA224_HMAC:
531 		break;
532 	default:
533 		EMSG("Invalid key type %#"PRIx32"/%s",
534 		     get_key_type(*out), id2str_key_type(get_key_type(*out)));
535 
536 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
537 	}
538 
539 	rc = set_mandatory_boolprops(out, temp, symm_key_boolprops,
540 				     ARRAY_SIZE(symm_key_boolprops));
541 	if (rc)
542 		return rc;
543 
544 	rc = set_attributes_opt_or_null(out, temp, symm_key_opt_or_null,
545 					ARRAY_SIZE(symm_key_opt_or_null));
546 	if (rc)
547 		return rc;
548 
549 	return set_optional_attributes(out, temp, symm_key_optional,
550 				       ARRAY_SIZE(symm_key_optional));
551 }
552 
553 static enum pkcs11_rc create_data_attributes(struct obj_attrs **out,
554 					     struct obj_attrs *temp)
555 {
556 	enum pkcs11_rc rc = PKCS11_CKR_OK;
557 
558 	assert(get_class(temp) == PKCS11_CKO_DATA);
559 
560 	rc = create_storage_attributes(out, temp);
561 	if (rc)
562 		return rc;
563 
564 	assert(get_class(*out) == PKCS11_CKO_DATA);
565 
566 	return set_attributes_opt_or_null(out, temp, raw_data_opt_or_null,
567 					  ARRAY_SIZE(raw_data_opt_or_null));
568 }
569 
570 static enum pkcs11_rc create_pub_key_attributes(struct obj_attrs **out,
571 						struct obj_attrs *temp,
572 						enum processing_func function)
573 {
574 	uint32_t const *mandated = NULL;
575 	uint32_t const *oon = NULL;
576 	size_t mandated_count = 0;
577 	size_t oon_count = 0;
578 	enum pkcs11_rc rc = PKCS11_CKR_OK;
579 
580 	assert(get_class(temp) == PKCS11_CKO_PUBLIC_KEY);
581 
582 	rc = create_genkey_attributes(out, temp);
583 	if (rc)
584 		return rc;
585 
586 	assert(get_class(*out) == PKCS11_CKO_PUBLIC_KEY);
587 
588 	rc = set_mandatory_boolprops(out, temp, public_key_boolprops,
589 				     ARRAY_SIZE(public_key_boolprops));
590 	if (rc)
591 		return rc;
592 
593 	rc = set_mandatory_attributes(out, temp, public_key_mandated,
594 				      ARRAY_SIZE(public_key_mandated));
595 	if (rc)
596 		return rc;
597 
598 	rc = set_attributes_opt_or_null(out, temp,
599 					public_key_opt_or_null,
600 					ARRAY_SIZE(public_key_opt_or_null));
601 	if (rc)
602 		return rc;
603 
604 	switch (get_key_type(*out)) {
605 	case PKCS11_CKK_RSA:
606 		switch (function) {
607 		case PKCS11_FUNCTION_GENERATE_PAIR:
608 			mandated = rsa_pub_key_gen_mand;
609 			oon = rsa_pub_key_gen_opt_or_null;
610 			mandated_count = ARRAY_SIZE(rsa_pub_key_gen_mand);
611 			oon_count = ARRAY_SIZE(rsa_pub_key_gen_opt_or_null);
612 			break;
613 		case PKCS11_FUNCTION_IMPORT:
614 			mandated = rsa_pub_key_create_mand;
615 			mandated_count = ARRAY_SIZE(rsa_pub_key_create_mand);
616 			break;
617 		default:
618 			EMSG("Unsupported function %#"PRIx32"/%s", function,
619 			     id2str_function(function));
620 
621 			return PKCS11_CKR_TEMPLATE_INCONSISTENT;
622 		}
623 		break;
624 	case PKCS11_CKK_EC:
625 		mandated = ec_public_key_mandated;
626 		oon = ec_public_key_opt_or_null;
627 		mandated_count = ARRAY_SIZE(ec_public_key_mandated);
628 		oon_count = ARRAY_SIZE(ec_public_key_opt_or_null);
629 		break;
630 	default:
631 		EMSG("Invalid key type %#"PRIx32"/%s",
632 		     get_key_type(*out), id2str_key_type(get_key_type(*out)));
633 
634 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
635 	}
636 
637 	rc = set_mandatory_attributes(out, temp, mandated, mandated_count);
638 	if (rc)
639 		return rc;
640 
641 	return set_attributes_opt_or_null(out, temp, oon, oon_count);
642 }
643 
644 static enum pkcs11_rc create_priv_key_attributes(struct obj_attrs **out,
645 						 struct obj_attrs *temp)
646 {
647 	uint32_t const *mandated = NULL;
648 	uint32_t const *oon = NULL;
649 	size_t mandated_count = 0;
650 	size_t oon_count = 0;
651 	enum pkcs11_rc rc = PKCS11_CKR_OK;
652 
653 	assert(get_class(temp) == PKCS11_CKO_PRIVATE_KEY);
654 
655 	rc = create_genkey_attributes(out, temp);
656 	if (rc)
657 		return rc;
658 
659 	assert(get_class(*out) == PKCS11_CKO_PRIVATE_KEY);
660 
661 	rc = set_mandatory_boolprops(out, temp, private_key_boolprops,
662 				     ARRAY_SIZE(private_key_boolprops));
663 	if (rc)
664 		return rc;
665 
666 	rc = set_mandatory_attributes(out, temp, private_key_mandated,
667 				      ARRAY_SIZE(private_key_mandated));
668 	if (rc)
669 		return rc;
670 
671 	rc = set_attributes_opt_or_null(out, temp, private_key_opt_or_null,
672 					ARRAY_SIZE(private_key_opt_or_null));
673 	if (rc)
674 		return rc;
675 
676 	switch (get_key_type(*out)) {
677 	case PKCS11_CKK_RSA:
678 		oon = rsa_priv_key_opt_or_null;
679 		oon_count = ARRAY_SIZE(rsa_priv_key_opt_or_null);
680 		break;
681 	case PKCS11_CKK_EC:
682 		mandated = ec_private_key_mandated;
683 		oon = ec_private_key_opt_or_null;
684 		mandated_count = ARRAY_SIZE(ec_private_key_mandated);
685 		oon_count = ARRAY_SIZE(ec_private_key_opt_or_null);
686 		break;
687 	default:
688 		EMSG("Invalid key type %#"PRIx32"/%s",
689 		     get_key_type(*out), id2str_key_type(get_key_type(*out)));
690 
691 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
692 	}
693 
694 	rc = set_mandatory_attributes(out, temp, mandated, mandated_count);
695 	if (rc)
696 		return rc;
697 
698 	return set_attributes_opt_or_null(out, temp, oon, oon_count);
699 }
700 
701 static enum pkcs11_rc
702 sanitize_symm_key_attributes(struct obj_attrs **temp,
703 			     enum processing_func function)
704 {
705 	enum pkcs11_rc rc = PKCS11_CKR_OK;
706 	uint32_t a_size = 0;
707 
708 	assert(get_class(*temp) == PKCS11_CKO_SECRET_KEY);
709 
710 	rc = get_attribute_ptr(*temp, PKCS11_CKA_VALUE, NULL, &a_size);
711 
712 	switch (get_key_type(*temp)) {
713 	case PKCS11_CKK_GENERIC_SECRET:
714 	case PKCS11_CKK_AES:
715 	case PKCS11_CKK_MD5_HMAC:
716 	case PKCS11_CKK_SHA_1_HMAC:
717 	case PKCS11_CKK_SHA256_HMAC:
718 	case PKCS11_CKK_SHA384_HMAC:
719 	case PKCS11_CKK_SHA512_HMAC:
720 	case PKCS11_CKK_SHA224_HMAC:
721 		switch (function) {
722 		case PKCS11_FUNCTION_IMPORT:
723 			/* CKA_VALUE is a mandatory with C_CreateObject */
724 			if (rc || a_size == 0)
725 				return PKCS11_CKR_TEMPLATE_INCONSISTENT;
726 
727 			if (get_attribute_ptr(*temp, PKCS11_CKA_VALUE_LEN, NULL,
728 					      NULL) != PKCS11_RV_NOT_FOUND)
729 				return PKCS11_CKR_TEMPLATE_INCONSISTENT;
730 
731 			return add_attribute(temp, PKCS11_CKA_VALUE_LEN,
732 					     &a_size, sizeof(uint32_t));
733 		case PKCS11_FUNCTION_GENERATE:
734 			if (rc != PKCS11_RV_NOT_FOUND)
735 				return PKCS11_CKR_TEMPLATE_INCONSISTENT;
736 			break;
737 		default:
738 			break;
739 		}
740 		break;
741 	default:
742 		EMSG("Invalid key type %#"PRIx32"/%s",
743 		     get_key_type(*temp), id2str_key_type(get_key_type(*temp)));
744 
745 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
746 	}
747 
748 	return PKCS11_CKR_OK;
749 }
750 
751 /*
752  * Create an attribute list for a new object from a template and a parent
753  * object (optional) for an object generation function (generate, copy,
754  * derive...).
755  *
756  * PKCS#11 directives on the supplied template and expected return value:
757  * - template has an invalid attribute ID: ATTRIBUTE_TYPE_INVALID
758  * - template has an invalid value for an attribute: ATTRIBUTE_VALID_INVALID
759  * - template has value for a read-only attribute: ATTRIBUTE_READ_ONLY
760  * - template+default+parent => still miss an attribute: TEMPLATE_INCONSISTENT
761  *
762  * INFO on PKCS11_CMD_COPY_OBJECT:
763  * - parent PKCS11_CKA_COPYIABLE=false => return ACTION_PROHIBITED.
764  * - template can specify PKCS11_CKA_TOKEN, PKCS11_CKA_PRIVATE,
765  *   PKCS11_CKA_MODIFIABLE, PKCS11_CKA_DESTROYABLE.
766  * - SENSITIVE can change from false to true, not from true to false.
767  * - LOCAL is the parent LOCAL
768  */
769 enum pkcs11_rc
770 create_attributes_from_template(struct obj_attrs **out, void *template,
771 				size_t template_size,
772 				struct obj_attrs *parent,
773 				enum processing_func function,
774 				enum pkcs11_mechanism_id mecha,
775 				enum pkcs11_class_id template_class)
776 {
777 	struct obj_attrs *temp = NULL;
778 	struct obj_attrs *attrs = NULL;
779 	enum pkcs11_rc rc = PKCS11_CKR_OK;
780 	uint8_t local = 0;
781 	uint8_t always_sensitive = 0;
782 	uint8_t never_extract = 0;
783 	uint8_t extractable = 0;
784 	uint32_t class = PKCS11_UNDEFINED_ID;
785 	uint32_t type = PKCS11_UNDEFINED_ID;
786 	uint32_t mechanism_id = PKCS11_CKM_UNDEFINED_ID;
787 	struct obj_attrs *req_attrs = NULL;
788 	uint32_t size = 0;
789 	uint32_t indirect_template = PKCS11_CKA_UNDEFINED_ID;
790 
791 #ifdef DEBUG	/* Sanity: check function argument */
792 	trace_attributes_from_api_head("template", template, template_size);
793 	switch (function) {
794 	case PKCS11_FUNCTION_GENERATE:
795 	case PKCS11_FUNCTION_GENERATE_PAIR:
796 	case PKCS11_FUNCTION_IMPORT:
797 	case PKCS11_FUNCTION_MODIFY:
798 	case PKCS11_FUNCTION_DERIVE:
799 	case PKCS11_FUNCTION_UNWRAP:
800 	case PKCS11_FUNCTION_COPY:
801 		break;
802 	default:
803 		TEE_Panic(TEE_ERROR_NOT_SUPPORTED);
804 	}
805 #endif
806 
807 	/*
808 	 * For PKCS11_FUNCTION_GENERATE, find the class and type
809 	 * based on the mechanism. These will be passed as hint
810 	 * sanitize_client_object() and added in temp if not
811 	 * already present
812 	 */
813 	if (function == PKCS11_FUNCTION_GENERATE) {
814 		switch (mecha) {
815 		case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
816 			class = PKCS11_CKO_SECRET_KEY;
817 			type = PKCS11_CKK_GENERIC_SECRET;
818 			break;
819 		case PKCS11_CKM_AES_KEY_GEN:
820 			class = PKCS11_CKO_SECRET_KEY;
821 			type = PKCS11_CKK_AES;
822 			break;
823 		default:
824 			TEE_Panic(TEE_ERROR_NOT_SUPPORTED);
825 		}
826 	}
827 
828 	/*
829 	 * For PKCS11_FUNCTION_GENERATE_PAIR, find the class and type
830 	 * based on the mechanism. These will be passed as hint
831 	 * sanitize_client_object() and added in temp if not
832 	 * already present
833 	 */
834 	if (function == PKCS11_FUNCTION_GENERATE_PAIR) {
835 		switch (mecha) {
836 		case PKCS11_CKM_EC_KEY_PAIR_GEN:
837 			class = template_class;
838 			type = PKCS11_CKK_EC;
839 			break;
840 		case PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN:
841 			class = template_class;
842 			type = PKCS11_CKK_RSA;
843 			break;
844 		default:
845 			TEE_Panic(TEE_ERROR_NOT_SUPPORTED);
846 		}
847 	}
848 
849 	/*
850 	 * Check and remove duplicates if any and create a new temporary
851 	 * template
852 	 */
853 	rc = sanitize_client_object(&temp, template, template_size, class,
854 				    type);
855 	if (rc)
856 		goto out;
857 
858 	/*
859 	 * For function type modify and copy return the created template
860 	 * from here. Rest of the code below is for creating objects
861 	 * or generating keys.
862 	 */
863 	switch (function) {
864 	case PKCS11_FUNCTION_MODIFY:
865 	case PKCS11_FUNCTION_COPY:
866 		*out = temp;
867 		return rc;
868 	case PKCS11_FUNCTION_DERIVE:
869 	case PKCS11_FUNCTION_UNWRAP:
870 		if (function == PKCS11_FUNCTION_UNWRAP)
871 			indirect_template = PKCS11_CKA_UNWRAP_TEMPLATE;
872 		else
873 			indirect_template = PKCS11_CKA_DERIVE_TEMPLATE;
874 
875 		rc = get_attribute_ptr(parent, indirect_template,
876 				       (void *)&req_attrs, &size);
877 		if (rc == PKCS11_CKR_OK && size != 0) {
878 			rc = attributes_match_add_reference(&temp, req_attrs);
879 			if (rc)
880 				goto out;
881 		}
882 		break;
883 	default:
884 		break;
885 	}
886 
887 	/*
888 	 * Check if class and type in temp are consistent with the mechanism
889 	 */
890 	switch (mecha) {
891 	case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
892 		if (get_class(temp) != PKCS11_CKO_SECRET_KEY ||
893 		    get_key_type(temp) != PKCS11_CKK_GENERIC_SECRET) {
894 			rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
895 			goto out;
896 		}
897 		break;
898 	case PKCS11_CKM_AES_KEY_GEN:
899 		if (get_class(temp) != PKCS11_CKO_SECRET_KEY ||
900 		    get_key_type(temp) != PKCS11_CKK_AES) {
901 			rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
902 			goto out;
903 		}
904 		break;
905 	case PKCS11_CKM_EC_KEY_PAIR_GEN:
906 		if ((get_class(temp) != PKCS11_CKO_PUBLIC_KEY &&
907 		     get_class(temp) != PKCS11_CKO_PRIVATE_KEY) ||
908 		    get_key_type(temp) != PKCS11_CKK_EC) {
909 			rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
910 			goto out;
911 		}
912 		break;
913 	case PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN:
914 		if ((get_class(temp) != PKCS11_CKO_PUBLIC_KEY &&
915 		     get_class(temp) != PKCS11_CKO_PRIVATE_KEY) ||
916 		    get_key_type(temp) != PKCS11_CKK_RSA) {
917 			rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
918 			goto out;
919 		}
920 		break;
921 	default:
922 		break;
923 	}
924 
925 	if (!sanitize_consistent_class_and_type(temp)) {
926 		EMSG("Inconsistent class/type");
927 		rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
928 		goto out;
929 	}
930 
931 	/*
932 	 * TBD - Add a check to see if temp contains any attribute which
933 	 * is not consistent with the object class or type and return error.
934 	 * In current implementation such attributes are ignored and not
935 	 * added to final object while PKCS#11 specification expects a
936 	 * failure and an error code be returned.
937 	 */
938 
939 	switch (get_class(temp)) {
940 	case PKCS11_CKO_DATA:
941 		rc = create_data_attributes(&attrs, temp);
942 		break;
943 	case PKCS11_CKO_SECRET_KEY:
944 		rc = sanitize_symm_key_attributes(&temp, function);
945 		if (rc)
946 			goto out;
947 		rc = create_symm_key_attributes(&attrs, temp);
948 		break;
949 	case PKCS11_CKO_PUBLIC_KEY:
950 		rc = create_pub_key_attributes(&attrs, temp, function);
951 		break;
952 	case PKCS11_CKO_PRIVATE_KEY:
953 		rc = create_priv_key_attributes(&attrs, temp);
954 		break;
955 	default:
956 		DMSG("Invalid object class %#"PRIx32"/%s",
957 		     get_class(temp), id2str_class(get_class(temp)));
958 
959 		rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
960 		break;
961 	}
962 	if (rc)
963 		goto out;
964 
965 	if (get_attribute_ptr(temp, PKCS11_CKA_LOCAL, NULL, NULL) !=
966 	    PKCS11_RV_NOT_FOUND) {
967 		rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
968 		goto out;
969 	}
970 
971 	if (get_attribute_ptr(temp, PKCS11_CKA_KEY_GEN_MECHANISM, NULL, NULL) !=
972 	    PKCS11_RV_NOT_FOUND) {
973 		rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
974 		goto out;
975 	}
976 
977 	switch (function) {
978 	case PKCS11_FUNCTION_GENERATE:
979 	case PKCS11_FUNCTION_GENERATE_PAIR:
980 		local = PKCS11_TRUE;
981 		break;
982 	case PKCS11_FUNCTION_IMPORT:
983 	case PKCS11_FUNCTION_DERIVE:
984 	case PKCS11_FUNCTION_UNWRAP:
985 	default:
986 		local = PKCS11_FALSE;
987 		break;
988 	}
989 	rc = add_attribute(&attrs, PKCS11_CKA_LOCAL, &local, sizeof(local));
990 	if (rc)
991 		goto out;
992 
993 	switch (get_class(attrs)) {
994 	case PKCS11_CKO_SECRET_KEY:
995 	case PKCS11_CKO_PRIVATE_KEY:
996 	case PKCS11_CKO_PUBLIC_KEY:
997 		always_sensitive = PKCS11_FALSE;
998 		never_extract = PKCS11_FALSE;
999 
1000 		switch (function) {
1001 		case PKCS11_FUNCTION_DERIVE:
1002 			always_sensitive =
1003 				get_bool(parent, PKCS11_CKA_ALWAYS_SENSITIVE) &&
1004 				get_bool(attrs, PKCS11_CKA_SENSITIVE);
1005 			never_extract =
1006 			       get_bool(parent, PKCS11_CKA_NEVER_EXTRACTABLE) &&
1007 			       !get_bool(attrs, PKCS11_CKA_EXTRACTABLE);
1008 			break;
1009 		case PKCS11_FUNCTION_UNWRAP:
1010 			always_sensitive = PKCS11_FALSE;
1011 			never_extract = PKCS11_FALSE;
1012 			extractable = PKCS11_TRUE;
1013 
1014 			/*
1015 			 * Check if template passed by user has CKA_EXTRACTABLE.
1016 			 * If not, by default value of CKA_EXTRACTABLE is set as
1017 			 * TRUE.
1018 			 */
1019 			if (get_attribute_ptr(temp, PKCS11_CKA_EXTRACTABLE,
1020 					      NULL,
1021 					      NULL) == PKCS11_RV_NOT_FOUND) {
1022 				rc = set_attribute(&attrs,
1023 						   PKCS11_CKA_EXTRACTABLE,
1024 						   &extractable,
1025 						   sizeof(extractable));
1026 				if (rc)
1027 					goto out;
1028 			}
1029 			break;
1030 		case PKCS11_FUNCTION_GENERATE:
1031 		case PKCS11_FUNCTION_GENERATE_PAIR:
1032 			always_sensitive = get_bool(attrs,
1033 						    PKCS11_CKA_SENSITIVE);
1034 			never_extract = !get_bool(attrs,
1035 						  PKCS11_CKA_EXTRACTABLE);
1036 			break;
1037 		default:
1038 			break;
1039 		}
1040 
1041 		rc = add_attribute(&attrs, PKCS11_CKA_ALWAYS_SENSITIVE,
1042 				   &always_sensitive, sizeof(always_sensitive));
1043 		if (rc)
1044 			goto out;
1045 
1046 		rc = add_attribute(&attrs, PKCS11_CKA_NEVER_EXTRACTABLE,
1047 				   &never_extract, sizeof(never_extract));
1048 		if (rc)
1049 			goto out;
1050 
1051 		/* Keys mandate attribute PKCS11_CKA_KEY_GEN_MECHANISM */
1052 		if (local)
1053 			mechanism_id = mecha;
1054 		else
1055 			mechanism_id = PKCS11_CK_UNAVAILABLE_INFORMATION;
1056 
1057 		rc = add_attribute(&attrs, PKCS11_CKA_KEY_GEN_MECHANISM,
1058 				   &mechanism_id, sizeof(mechanism_id));
1059 		if (rc)
1060 			goto out;
1061 		break;
1062 
1063 	default:
1064 		break;
1065 	}
1066 
1067 	*out = attrs;
1068 
1069 #ifdef DEBUG
1070 	trace_attributes("object", attrs);
1071 #endif
1072 
1073 out:
1074 	TEE_Free(temp);
1075 	if (rc)
1076 		TEE_Free(attrs);
1077 
1078 	return rc;
1079 }
1080 
1081 static enum pkcs11_rc check_attrs_misc_integrity(struct obj_attrs *head)
1082 {
1083 	if (get_bool(head, PKCS11_CKA_NEVER_EXTRACTABLE) &&
1084 	    get_bool(head, PKCS11_CKA_EXTRACTABLE)) {
1085 		DMSG("Never/Extractable attributes mismatch %d/%d",
1086 		     get_bool(head, PKCS11_CKA_NEVER_EXTRACTABLE),
1087 		     get_bool(head, PKCS11_CKA_EXTRACTABLE));
1088 
1089 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1090 	}
1091 
1092 	if (get_bool(head, PKCS11_CKA_ALWAYS_SENSITIVE) &&
1093 	    !get_bool(head, PKCS11_CKA_SENSITIVE)) {
1094 		DMSG("Sensitive/always attributes mismatch %d/%d",
1095 		     get_bool(head, PKCS11_CKA_SENSITIVE),
1096 		     get_bool(head, PKCS11_CKA_ALWAYS_SENSITIVE));
1097 
1098 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1099 	}
1100 
1101 	return PKCS11_CKR_OK;
1102 }
1103 
1104 bool object_is_private(struct obj_attrs *head)
1105 {
1106 	return get_bool(head, PKCS11_CKA_PRIVATE);
1107 }
1108 
1109 bool object_is_token(struct obj_attrs *head)
1110 {
1111 	return get_bool(head, PKCS11_CKA_TOKEN);
1112 }
1113 
1114 bool object_is_modifiable(struct obj_attrs *head)
1115 {
1116 	return get_bool(head, PKCS11_CKA_MODIFIABLE);
1117 }
1118 
1119 bool object_is_copyable(struct obj_attrs *head)
1120 {
1121 	return get_bool(head, PKCS11_CKA_COPYABLE);
1122 }
1123 
1124 /*
1125  * Check access to object against authentication to token
1126  */
1127 enum pkcs11_rc check_access_attrs_against_token(struct pkcs11_session *session,
1128 						struct obj_attrs *head)
1129 {
1130 	bool private = true;
1131 
1132 	switch (get_class(head)) {
1133 	case PKCS11_CKO_SECRET_KEY:
1134 	case PKCS11_CKO_PRIVATE_KEY:
1135 	case PKCS11_CKO_PUBLIC_KEY:
1136 	case PKCS11_CKO_DATA:
1137 		private = object_is_private(head);
1138 		break;
1139 	default:
1140 		return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1141 	}
1142 
1143 	if (private && (pkcs11_session_is_public(session) ||
1144 			pkcs11_session_is_so(session))) {
1145 		DMSG("Private object access from a public or SO session");
1146 
1147 		return PKCS11_CKR_USER_NOT_LOGGED_IN;
1148 	}
1149 
1150 	return PKCS11_CKR_OK;
1151 }
1152 
1153 /*
1154  * Check the attributes of a to-be-created object matches the token state
1155  */
1156 enum pkcs11_rc check_created_attrs_against_token(struct pkcs11_session *session,
1157 						 struct obj_attrs *head)
1158 {
1159 	enum pkcs11_rc rc = PKCS11_CKR_OK;
1160 
1161 	rc = check_attrs_misc_integrity(head);
1162 	if (rc)
1163 		return rc;
1164 
1165 	if (get_bool(head, PKCS11_CKA_TRUSTED) &&
1166 	    !pkcs11_session_is_so(session)) {
1167 		DMSG("Can't create trusted object");
1168 
1169 		return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1170 	}
1171 
1172 	if (get_bool(head, PKCS11_CKA_TOKEN) &&
1173 	    !pkcs11_session_is_read_write(session)) {
1174 		DMSG("Can't create persistent object");
1175 
1176 		return PKCS11_CKR_SESSION_READ_ONLY;
1177 	}
1178 
1179 	/*
1180 	 * TODO: START_DATE and END_DATE: complies with current time?
1181 	 */
1182 	return PKCS11_CKR_OK;
1183 }
1184 
1185 #define DMSG_BAD_BBOOL(attr, proc, head)				\
1186 	do {								\
1187 		uint32_t __maybe_unused _attr = (attr);			\
1188 		uint8_t __maybe_unused _bvalue = 0;			\
1189 		enum pkcs11_rc __maybe_unused _rc = PKCS11_CKR_OK;	\
1190 									\
1191 		_rc = get_attribute((head), _attr, &_bvalue, NULL);	\
1192 		DMSG("%s issue for %s: %sfound, value %"PRIu8,		\
1193 		     id2str_attr(_attr), id2str_proc((proc)),		\
1194 		     _rc ? "not " : "", _bvalue);			\
1195 	} while (0)
1196 
1197 static bool __maybe_unused check_attr_bval(uint32_t proc_id __maybe_unused,
1198 					   struct obj_attrs *head,
1199 					   uint32_t attribute, bool val)
1200 {
1201 	uint8_t bbool = 0;
1202 	uint32_t sz = sizeof(bbool);
1203 
1204 	if (!get_attribute(head, attribute, &bbool, &sz) && !!bbool == val)
1205 		return true;
1206 
1207 	DMSG_BAD_BBOOL(attribute, proc_id, head);
1208 	return false;
1209 }
1210 
1211 /*
1212  * Check the attributes of a new secret match the processing/mechanism
1213  * used to create it.
1214  *
1215  * @proc_id - PKCS11_CKM_xxx
1216  * @head - head of the attributes of the to-be-created object.
1217  */
1218 enum pkcs11_rc check_created_attrs_against_processing(uint32_t proc_id,
1219 						      struct obj_attrs *head)
1220 {
1221 	/*
1222 	 * Processings that do not create secrets are not expected to call
1223 	 * this function which would panic.
1224 	 */
1225 	switch (proc_id) {
1226 	case PKCS11_PROCESSING_IMPORT:
1227 	case PKCS11_CKM_AES_ECB:
1228 	case PKCS11_CKM_AES_CBC:
1229 	case PKCS11_CKM_AES_ECB_ENCRYPT_DATA:
1230 	case PKCS11_CKM_AES_CBC_ENCRYPT_DATA:
1231 		assert(check_attr_bval(proc_id, head, PKCS11_CKA_LOCAL, false));
1232 		break;
1233 	case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
1234 	case PKCS11_CKM_AES_KEY_GEN:
1235 	case PKCS11_CKM_EC_KEY_PAIR_GEN:
1236 	case PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN:
1237 		assert(check_attr_bval(proc_id, head, PKCS11_CKA_LOCAL, true));
1238 		break;
1239 	default:
1240 		TEE_Panic(proc_id);
1241 		break;
1242 	}
1243 
1244 	switch (proc_id) {
1245 	case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
1246 		assert(get_key_type(head) == PKCS11_CKK_GENERIC_SECRET);
1247 		break;
1248 	case PKCS11_CKM_AES_KEY_GEN:
1249 		assert(get_key_type(head) == PKCS11_CKK_AES);
1250 		break;
1251 	case PKCS11_CKM_EC_KEY_PAIR_GEN:
1252 		assert(get_key_type(head) == PKCS11_CKK_EC);
1253 		break;
1254 	case PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN:
1255 		assert(get_key_type(head) == PKCS11_CKK_RSA);
1256 		break;
1257 	case PKCS11_PROCESSING_IMPORT:
1258 	default:
1259 		break;
1260 	}
1261 
1262 	return PKCS11_CKR_OK;
1263 }
1264 
1265 /* Return min and max key size supported for a key_type in bytes */
1266 static void get_key_min_max_sizes(enum pkcs11_key_type key_type,
1267 				  uint32_t *min_key_size,
1268 				  uint32_t *max_key_size)
1269 {
1270 	enum pkcs11_mechanism_id mechanism = PKCS11_CKM_UNDEFINED_ID;
1271 
1272 	switch (key_type) {
1273 	case PKCS11_CKK_GENERIC_SECRET:
1274 		mechanism = PKCS11_CKM_GENERIC_SECRET_KEY_GEN;
1275 		break;
1276 	case PKCS11_CKK_AES:
1277 		mechanism = PKCS11_CKM_AES_KEY_GEN;
1278 		break;
1279 	case PKCS11_CKK_MD5_HMAC:
1280 		mechanism = PKCS11_CKM_MD5_HMAC;
1281 		break;
1282 	case PKCS11_CKK_SHA_1_HMAC:
1283 		mechanism = PKCS11_CKM_SHA_1_HMAC;
1284 		break;
1285 	case PKCS11_CKK_SHA224_HMAC:
1286 		mechanism = PKCS11_CKM_SHA224_HMAC;
1287 		break;
1288 	case PKCS11_CKK_SHA256_HMAC:
1289 		mechanism = PKCS11_CKM_SHA256_HMAC;
1290 		break;
1291 	case PKCS11_CKK_SHA384_HMAC:
1292 		mechanism = PKCS11_CKM_SHA384_HMAC;
1293 		break;
1294 	case PKCS11_CKK_SHA512_HMAC:
1295 		mechanism = PKCS11_CKM_SHA512_HMAC;
1296 		break;
1297 	case PKCS11_CKK_EC:
1298 		mechanism = PKCS11_CKM_EC_KEY_PAIR_GEN;
1299 		break;
1300 	case PKCS11_CKK_RSA:
1301 		mechanism = PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN;
1302 		break;
1303 	default:
1304 		TEE_Panic(key_type);
1305 		break;
1306 	}
1307 
1308 	mechanism_supported_key_sizes_bytes(mechanism, min_key_size,
1309 					    max_key_size);
1310 }
1311 
1312 enum pkcs11_rc check_created_attrs(struct obj_attrs *key1,
1313 				   struct obj_attrs *key2)
1314 {
1315 	enum pkcs11_rc rc = PKCS11_CKR_OK;
1316 	struct obj_attrs *secret = NULL;
1317 	struct obj_attrs *private = NULL;
1318 	struct obj_attrs *public = NULL;
1319 	uint32_t max_key_size = 0;
1320 	uint32_t min_key_size = 0;
1321 	uint32_t key_length = 0;
1322 
1323 	switch (get_class(key1)) {
1324 	case PKCS11_CKO_SECRET_KEY:
1325 		secret = key1;
1326 		break;
1327 	case PKCS11_CKO_PUBLIC_KEY:
1328 		public = key1;
1329 		break;
1330 	case PKCS11_CKO_PRIVATE_KEY:
1331 		private = key1;
1332 		break;
1333 	default:
1334 		return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID;
1335 	}
1336 
1337 	if (key2) {
1338 		switch (get_class(key2)) {
1339 		case PKCS11_CKO_PUBLIC_KEY:
1340 			public = key2;
1341 			if (private == key1)
1342 				break;
1343 
1344 			return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1345 		case PKCS11_CKO_PRIVATE_KEY:
1346 			private = key2;
1347 			if (public == key1)
1348 				break;
1349 
1350 			return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1351 		default:
1352 			return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID;
1353 		}
1354 
1355 		if (get_key_type(private) != get_key_type(public))
1356 			return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1357 	}
1358 
1359 	if (secret) {
1360 		switch (get_key_type(secret)) {
1361 		case PKCS11_CKK_AES:
1362 		case PKCS11_CKK_GENERIC_SECRET:
1363 		case PKCS11_CKK_MD5_HMAC:
1364 		case PKCS11_CKK_SHA_1_HMAC:
1365 		case PKCS11_CKK_SHA224_HMAC:
1366 		case PKCS11_CKK_SHA256_HMAC:
1367 		case PKCS11_CKK_SHA384_HMAC:
1368 		case PKCS11_CKK_SHA512_HMAC:
1369 			break;
1370 		default:
1371 			return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1372 		}
1373 
1374 		/* Get key size */
1375 		rc = get_u32_attribute(secret, PKCS11_CKA_VALUE_LEN,
1376 				       &key_length);
1377 		if (rc)
1378 			return PKCS11_CKR_TEMPLATE_INCOMPLETE;
1379 	}
1380 	if (public) {
1381 		switch (get_key_type(public)) {
1382 		case PKCS11_CKK_RSA:
1383 			/* Get key size */
1384 			rc = get_u32_attribute(public, PKCS11_CKA_MODULUS_BITS,
1385 					       &key_length);
1386 			if (rc)
1387 				return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1388 			key_length = ROUNDUP(key_length, 8) / 8;
1389 			break;
1390 		case PKCS11_CKK_EC:
1391 			break;
1392 		default:
1393 			return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1394 		}
1395 	}
1396 	if (private) {
1397 		switch (get_key_type(private)) {
1398 		case PKCS11_CKK_RSA:
1399 		case PKCS11_CKK_EC:
1400 			break;
1401 		default:
1402 			return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1403 		}
1404 	}
1405 
1406 	/*
1407 	 * Check key size for symmetric keys and RSA keys
1408 	 * EC is bound to domains, no need to check here.
1409 	 */
1410 	switch (get_key_type(key1)) {
1411 	case PKCS11_CKK_EC:
1412 		return PKCS11_CKR_OK;
1413 	default:
1414 		break;
1415 	}
1416 
1417 	get_key_min_max_sizes(get_key_type(key1), &min_key_size, &max_key_size);
1418 	if (key_length < min_key_size || key_length > max_key_size) {
1419 		EMSG("Length %"PRIu32" vs range [%"PRIu32" %"PRIu32"]",
1420 		     key_length, min_key_size, max_key_size);
1421 
1422 		return PKCS11_CKR_KEY_SIZE_RANGE;
1423 	}
1424 
1425 	if (secret && get_key_type(secret) == PKCS11_CKK_AES) {
1426 		if (key_length != 16 && key_length != 24 && key_length != 32)
1427 			return PKCS11_CKR_KEY_SIZE_RANGE;
1428 	}
1429 
1430 	return PKCS11_CKR_OK;
1431 }
1432 
1433 /* Check processing ID against attribute ALLOWED_MECHANISMS if any */
1434 static bool parent_key_complies_allowed_processings(uint32_t proc_id,
1435 						    struct obj_attrs *head)
1436 {
1437 	char *attr = NULL;
1438 	uint32_t size = 0;
1439 	uint32_t proc = 0;
1440 	size_t count = 0;
1441 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
1442 
1443 	rc = get_attribute_ptr(head, PKCS11_CKA_ALLOWED_MECHANISMS,
1444 			       (void *)&attr, &size);
1445 	if (rc == PKCS11_RV_NOT_FOUND)
1446 		return true;
1447 	if (rc) {
1448 		EMSG("unexpected attributes state");
1449 		TEE_Panic(TEE_ERROR_BAD_STATE);
1450 	}
1451 
1452 	for (count = size / sizeof(uint32_t); count; count--) {
1453 		TEE_MemMove(&proc, attr, sizeof(uint32_t));
1454 		attr += sizeof(uint32_t);
1455 
1456 		if (proc == proc_id)
1457 			return true;
1458 	}
1459 
1460 	DMSG("can't find %s in allowed list", id2str_proc(proc_id));
1461 	return false;
1462 }
1463 
1464 static enum pkcs11_attr_id func_to_attr(enum processing_func func)
1465 {
1466 	switch (func) {
1467 	case PKCS11_FUNCTION_ENCRYPT:
1468 		return PKCS11_CKA_ENCRYPT;
1469 	case PKCS11_FUNCTION_DECRYPT:
1470 		return PKCS11_CKA_DECRYPT;
1471 	case PKCS11_FUNCTION_SIGN:
1472 		return PKCS11_CKA_SIGN;
1473 	case PKCS11_FUNCTION_VERIFY:
1474 		return PKCS11_CKA_VERIFY;
1475 	case PKCS11_FUNCTION_WRAP:
1476 		return PKCS11_CKA_WRAP;
1477 	case PKCS11_FUNCTION_UNWRAP:
1478 		return PKCS11_CKA_UNWRAP;
1479 	case PKCS11_FUNCTION_DERIVE:
1480 		return PKCS11_CKA_DERIVE;
1481 	default:
1482 		return PKCS11_CKA_UNDEFINED_ID;
1483 	}
1484 }
1485 
1486 enum pkcs11_rc
1487 check_parent_attrs_against_processing(enum pkcs11_mechanism_id proc_id,
1488 				      enum processing_func function,
1489 				      struct obj_attrs *head)
1490 {
1491 	enum pkcs11_class_id key_class = get_class(head);
1492 	enum pkcs11_key_type key_type = get_key_type(head);
1493 	enum pkcs11_attr_id attr = func_to_attr(function);
1494 
1495 	if (!get_bool(head, attr)) {
1496 		DMSG("%s not permitted", id2str_attr(attr));
1497 		return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1498 	}
1499 
1500 	/* Check processing complies with parent key family */
1501 	switch (proc_id) {
1502 	case PKCS11_CKM_AES_ECB:
1503 	case PKCS11_CKM_AES_CBC:
1504 	case PKCS11_CKM_AES_CBC_PAD:
1505 	case PKCS11_CKM_AES_CTS:
1506 	case PKCS11_CKM_AES_CTR:
1507 		if (key_class == PKCS11_CKO_SECRET_KEY &&
1508 		    key_type == PKCS11_CKK_AES)
1509 			break;
1510 
1511 		DMSG("%s invalid key %s/%s", id2str_proc(proc_id),
1512 		     id2str_class(key_class), id2str_key_type(key_type));
1513 
1514 		if (function == PKCS11_FUNCTION_WRAP)
1515 			return PKCS11_CKR_WRAPPING_KEY_TYPE_INCONSISTENT;
1516 		else if (function == PKCS11_FUNCTION_UNWRAP)
1517 			return PKCS11_CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT;
1518 		else
1519 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1520 
1521 	case PKCS11_CKM_AES_ECB_ENCRYPT_DATA:
1522 	case PKCS11_CKM_AES_CBC_ENCRYPT_DATA:
1523 		if (key_class != PKCS11_CKO_SECRET_KEY &&
1524 		    key_type != PKCS11_CKK_AES)
1525 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1526 
1527 		if (get_bool(head, PKCS11_CKA_ENCRYPT)) {
1528 			/*
1529 			 * Intentionally refuse to proceed despite
1530 			 * PKCS#11 specifications v2.40 and v3.0 not expecting
1531 			 * this behavior to avoid potential security issue
1532 			 * where keys derived by these mechanisms can be
1533 			 * revealed by doing data encryption using parent key.
1534 			 */
1535 			return PKCS11_CKR_FUNCTION_FAILED;
1536 		}
1537 
1538 		break;
1539 	case PKCS11_CKM_MD5_HMAC:
1540 	case PKCS11_CKM_SHA_1_HMAC:
1541 	case PKCS11_CKM_SHA224_HMAC:
1542 	case PKCS11_CKM_SHA256_HMAC:
1543 	case PKCS11_CKM_SHA384_HMAC:
1544 	case PKCS11_CKM_SHA512_HMAC:
1545 	case PKCS11_CKM_MD5_HMAC_GENERAL:
1546 	case PKCS11_CKM_SHA_1_HMAC_GENERAL:
1547 	case PKCS11_CKM_SHA224_HMAC_GENERAL:
1548 	case PKCS11_CKM_SHA256_HMAC_GENERAL:
1549 	case PKCS11_CKM_SHA384_HMAC_GENERAL:
1550 	case PKCS11_CKM_SHA512_HMAC_GENERAL:
1551 		if (key_class != PKCS11_CKO_SECRET_KEY)
1552 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1553 
1554 		if (key_type == PKCS11_CKK_GENERIC_SECRET)
1555 			break;
1556 
1557 		switch (proc_id) {
1558 		case PKCS11_CKM_MD5_HMAC:
1559 		case PKCS11_CKM_MD5_HMAC_GENERAL:
1560 			if (key_type == PKCS11_CKK_MD5_HMAC)
1561 				break;
1562 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1563 		case PKCS11_CKM_SHA_1_HMAC:
1564 		case PKCS11_CKM_SHA_1_HMAC_GENERAL:
1565 			if (key_type == PKCS11_CKK_SHA_1_HMAC)
1566 				break;
1567 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1568 		case PKCS11_CKM_SHA224_HMAC:
1569 		case PKCS11_CKM_SHA224_HMAC_GENERAL:
1570 			if (key_type == PKCS11_CKK_SHA224_HMAC)
1571 				break;
1572 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1573 		case PKCS11_CKM_SHA256_HMAC:
1574 		case PKCS11_CKM_SHA256_HMAC_GENERAL:
1575 			if (key_type == PKCS11_CKK_SHA256_HMAC)
1576 				break;
1577 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1578 		case PKCS11_CKM_SHA384_HMAC:
1579 		case PKCS11_CKM_SHA384_HMAC_GENERAL:
1580 			if (key_type == PKCS11_CKK_SHA384_HMAC)
1581 				break;
1582 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1583 		case PKCS11_CKM_SHA512_HMAC:
1584 		case PKCS11_CKM_SHA512_HMAC_GENERAL:
1585 			if (key_type == PKCS11_CKK_SHA512_HMAC)
1586 				break;
1587 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1588 		default:
1589 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1590 		}
1591 		break;
1592 
1593 	case PKCS11_CKM_ECDSA:
1594 	case PKCS11_CKM_ECDSA_SHA1:
1595 	case PKCS11_CKM_ECDSA_SHA224:
1596 	case PKCS11_CKM_ECDSA_SHA256:
1597 	case PKCS11_CKM_ECDSA_SHA384:
1598 	case PKCS11_CKM_ECDSA_SHA512:
1599 		if (key_type != PKCS11_CKK_EC) {
1600 			EMSG("Invalid key %s for mechanism %s",
1601 			     id2str_type(key_type, key_class),
1602 			     id2str_proc(proc_id));
1603 
1604 			return PKCS11_CKR_KEY_TYPE_INCONSISTENT;
1605 		}
1606 		if (key_class != PKCS11_CKO_PUBLIC_KEY &&
1607 		    key_class != PKCS11_CKO_PRIVATE_KEY) {
1608 			EMSG("Invalid key class for mechanism %s",
1609 			     id2str_proc(proc_id));
1610 
1611 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1612 		}
1613 		break;
1614 	case PKCS11_CKM_RSA_PKCS:
1615 	case PKCS11_CKM_MD5_RSA_PKCS:
1616 	case PKCS11_CKM_SHA1_RSA_PKCS:
1617 	case PKCS11_CKM_SHA224_RSA_PKCS:
1618 	case PKCS11_CKM_SHA256_RSA_PKCS:
1619 	case PKCS11_CKM_SHA384_RSA_PKCS:
1620 	case PKCS11_CKM_SHA512_RSA_PKCS:
1621 	case PKCS11_CKM_RSA_PKCS_OAEP:
1622 	case PKCS11_CKM_RSA_PKCS_PSS:
1623 	case PKCS11_CKM_SHA1_RSA_PKCS_PSS:
1624 	case PKCS11_CKM_SHA224_RSA_PKCS_PSS:
1625 	case PKCS11_CKM_SHA256_RSA_PKCS_PSS:
1626 	case PKCS11_CKM_SHA384_RSA_PKCS_PSS:
1627 	case PKCS11_CKM_SHA512_RSA_PKCS_PSS:
1628 		if (key_type != PKCS11_CKK_RSA) {
1629 			EMSG("Invalid key %s for mechanism %s",
1630 			     id2str_type(key_type, key_class),
1631 			     id2str_proc(proc_id));
1632 
1633 			return PKCS11_CKR_KEY_TYPE_INCONSISTENT;
1634 		}
1635 		if (key_class != PKCS11_CKO_PUBLIC_KEY &&
1636 		    key_class != PKCS11_CKO_PRIVATE_KEY) {
1637 			EMSG("Invalid key class for mechanism %s",
1638 			     id2str_proc(proc_id));
1639 
1640 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1641 		}
1642 		break;
1643 	default:
1644 		DMSG("Invalid processing %#"PRIx32"/%s", proc_id,
1645 		     id2str_proc(proc_id));
1646 
1647 		return PKCS11_CKR_MECHANISM_INVALID;
1648 	}
1649 
1650 	if (!parent_key_complies_allowed_processings(proc_id, head)) {
1651 		DMSG("Allowed mechanism failed");
1652 		return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1653 	}
1654 
1655 	return PKCS11_CKR_OK;
1656 }
1657 
1658 bool attribute_is_exportable(struct pkcs11_attribute_head *req_attr,
1659 			     struct pkcs11_object *obj)
1660 {
1661 	uint8_t boolval = 0;
1662 	uint32_t boolsize = 0;
1663 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
1664 	enum pkcs11_class_id key_class = get_class(obj->attributes);
1665 
1666 	if (key_class != PKCS11_CKO_SECRET_KEY &&
1667 	    key_class != PKCS11_CKO_PRIVATE_KEY)
1668 		return true;
1669 
1670 	switch (req_attr->id) {
1671 	case PKCS11_CKA_PRIVATE_EXPONENT:
1672 	case PKCS11_CKA_PRIME_1:
1673 	case PKCS11_CKA_PRIME_2:
1674 	case PKCS11_CKA_EXPONENT_1:
1675 	case PKCS11_CKA_EXPONENT_2:
1676 	case PKCS11_CKA_COEFFICIENT:
1677 	case PKCS11_CKA_VALUE:
1678 		boolsize = sizeof(boolval);
1679 		rc = get_attribute(obj->attributes, PKCS11_CKA_EXTRACTABLE,
1680 				   &boolval, &boolsize);
1681 		if (rc || boolval == PKCS11_FALSE)
1682 			return false;
1683 
1684 		boolsize = sizeof(boolval);
1685 		rc = get_attribute(obj->attributes, PKCS11_CKA_SENSITIVE,
1686 				   &boolval, &boolsize);
1687 		if (rc || boolval == PKCS11_TRUE)
1688 			return false;
1689 		break;
1690 	default:
1691 		break;
1692 	}
1693 
1694 	return true;
1695 }
1696 
1697 static bool attr_is_modifiable_any_key(struct pkcs11_attribute_head *attr)
1698 {
1699 	switch (attr->id) {
1700 	case PKCS11_CKA_ID:
1701 	case PKCS11_CKA_START_DATE:
1702 	case PKCS11_CKA_END_DATE:
1703 	case PKCS11_CKA_DERIVE:
1704 		return true;
1705 	default:
1706 		return false;
1707 	}
1708 }
1709 
1710 static bool attr_is_modifiable_secret_key(struct pkcs11_attribute_head *attr,
1711 					  struct pkcs11_session *session,
1712 					  struct pkcs11_object *obj)
1713 {
1714 	switch (attr->id) {
1715 	case PKCS11_CKA_ENCRYPT:
1716 	case PKCS11_CKA_DECRYPT:
1717 	case PKCS11_CKA_SIGN:
1718 	case PKCS11_CKA_VERIFY:
1719 	case PKCS11_CKA_WRAP:
1720 	case PKCS11_CKA_UNWRAP:
1721 		return true;
1722 	/* Can't be modified once set to CK_FALSE - 12 in Table 10 */
1723 	case PKCS11_CKA_EXTRACTABLE:
1724 		return get_bool(obj->attributes, attr->id);
1725 	/* Can't be modified once set to CK_TRUE - 11 in Table 10 */
1726 	case PKCS11_CKA_SENSITIVE:
1727 	case PKCS11_CKA_WRAP_WITH_TRUSTED:
1728 		return !get_bool(obj->attributes, attr->id);
1729 	/* Change in CKA_TRUSTED can only be done by SO */
1730 	case PKCS11_CKA_TRUSTED:
1731 		return pkcs11_session_is_so(session);
1732 	case PKCS11_CKA_NEVER_EXTRACTABLE:
1733 	case PKCS11_CKA_ALWAYS_SENSITIVE:
1734 		return false;
1735 	default:
1736 		return false;
1737 	}
1738 }
1739 
1740 static bool attr_is_modifiable_public_key(struct pkcs11_attribute_head *attr,
1741 					  struct pkcs11_session *session,
1742 					  struct pkcs11_object *obj __unused)
1743 {
1744 	switch (attr->id) {
1745 	case PKCS11_CKA_SUBJECT:
1746 	case PKCS11_CKA_ENCRYPT:
1747 	case PKCS11_CKA_VERIFY:
1748 	case PKCS11_CKA_VERIFY_RECOVER:
1749 	case PKCS11_CKA_WRAP:
1750 		return true;
1751 	case PKCS11_CKA_TRUSTED:
1752 		/* Change in CKA_TRUSTED can only be done by SO */
1753 		return pkcs11_session_is_so(session);
1754 	default:
1755 		return false;
1756 	}
1757 }
1758 
1759 static bool attr_is_modifiable_private_key(struct pkcs11_attribute_head *attr,
1760 					   struct pkcs11_session *sess __unused,
1761 					   struct pkcs11_object *obj)
1762 {
1763 	switch (attr->id) {
1764 	case PKCS11_CKA_SUBJECT:
1765 	case PKCS11_CKA_DECRYPT:
1766 	case PKCS11_CKA_SIGN:
1767 	case PKCS11_CKA_SIGN_RECOVER:
1768 	case PKCS11_CKA_UNWRAP:
1769 	/*
1770 	 * TBD: Revisit if we don't support PKCS11_CKA_PUBLIC_KEY_INFO
1771 	 * Specification mentions that if this attribute is
1772 	 * supplied as part of a template for C_CreateObject, C_CopyObject or
1773 	 * C_SetAttributeValue for a private key, the token MUST verify
1774 	 * correspondence between the private key data and the public key data
1775 	 * as supplied in CKA_PUBLIC_KEY_INFO. This needs to be
1776 	 * taken care of when this object type will be implemented
1777 	 */
1778 	case PKCS11_CKA_PUBLIC_KEY_INFO:
1779 		return true;
1780 	/* Can't be modified once set to CK_FALSE - 12 in Table 10 */
1781 	case PKCS11_CKA_EXTRACTABLE:
1782 		return get_bool(obj->attributes, attr->id);
1783 	/* Can't be modified once set to CK_TRUE - 11 in Table 10 */
1784 	case PKCS11_CKA_SENSITIVE:
1785 	case PKCS11_CKA_WRAP_WITH_TRUSTED:
1786 		return !get_bool(obj->attributes, attr->id);
1787 	case PKCS11_CKA_NEVER_EXTRACTABLE:
1788 	case PKCS11_CKA_ALWAYS_SENSITIVE:
1789 		return false;
1790 	default:
1791 		return false;
1792 	}
1793 }
1794 
1795 static bool attribute_is_modifiable(struct pkcs11_session *session,
1796 				    struct pkcs11_attribute_head *req_attr,
1797 				    struct pkcs11_object *obj,
1798 				    enum pkcs11_class_id class,
1799 				    enum processing_func function)
1800 {
1801 	/* Check modifiable attributes common to any object */
1802 	switch (req_attr->id) {
1803 	case PKCS11_CKA_LABEL:
1804 		return true;
1805 	case PKCS11_CKA_TOKEN:
1806 	case PKCS11_CKA_MODIFIABLE:
1807 	case PKCS11_CKA_DESTROYABLE:
1808 	case PKCS11_CKA_PRIVATE:
1809 		return function == PKCS11_FUNCTION_COPY;
1810 	case PKCS11_CKA_COPYABLE:
1811 		/*
1812 		 * Specification mentions that if the attribute value is false
1813 		 * it can't be set to true. Reading this we assume that it
1814 		 * should be possible to modify this attribute even though this
1815 		 * is not marked as modifiable in Table 10 if done in right
1816 		 * direction i.e from TRUE -> FALSE.
1817 		 */
1818 		return get_bool(obj->attributes, req_attr->id);
1819 	default:
1820 		break;
1821 	}
1822 
1823 	/* Attribute checking based on class type */
1824 	switch (class) {
1825 	case PKCS11_CKO_SECRET_KEY:
1826 	case PKCS11_CKO_PUBLIC_KEY:
1827 	case PKCS11_CKO_PRIVATE_KEY:
1828 		if (attr_is_modifiable_any_key(req_attr))
1829 			return true;
1830 		if (class == PKCS11_CKO_SECRET_KEY &&
1831 		    attr_is_modifiable_secret_key(req_attr, session, obj))
1832 			return true;
1833 		if (class == PKCS11_CKO_PUBLIC_KEY &&
1834 		    attr_is_modifiable_public_key(req_attr, session, obj))
1835 			return true;
1836 		if (class == PKCS11_CKO_PRIVATE_KEY &&
1837 		    attr_is_modifiable_private_key(req_attr, session, obj))
1838 			return true;
1839 		break;
1840 	case PKCS11_CKO_DATA:
1841 		/* None of the data object attributes are modifiable */
1842 		return false;
1843 	default:
1844 		break;
1845 	}
1846 
1847 	return false;
1848 }
1849 
1850 enum pkcs11_rc check_attrs_against_modification(struct pkcs11_session *session,
1851 						struct obj_attrs *head,
1852 						struct pkcs11_object *obj,
1853 						enum processing_func function)
1854 {
1855 	enum pkcs11_class_id class = PKCS11_CKO_UNDEFINED_ID;
1856 	char *cur = NULL;
1857 	char *end = NULL;
1858 	size_t len = 0;
1859 
1860 	class = get_class(obj->attributes);
1861 
1862 	cur = (char *)head + sizeof(struct obj_attrs);
1863 	end = cur + head->attrs_size;
1864 
1865 	for (; cur < end; cur += len) {
1866 		/* Structure aligned copy of the pkcs11_ref in the object */
1867 		struct pkcs11_attribute_head cli_ref = { };
1868 
1869 		TEE_MemMove(&cli_ref, cur, sizeof(cli_ref));
1870 		len = sizeof(cli_ref) + cli_ref.size;
1871 
1872 		/*
1873 		 * Check 1 - Check if attribute belongs to the object
1874 		 * The obj->attributes has all the attributes in
1875 		 * it which are allowed for an object.
1876 		 */
1877 		if (get_attribute_ptr(obj->attributes, cli_ref.id, NULL,
1878 				      NULL) == PKCS11_RV_NOT_FOUND)
1879 			return PKCS11_CKR_ATTRIBUTE_TYPE_INVALID;
1880 
1881 		/* Check 2 - Is attribute modifiable */
1882 		if (!attribute_is_modifiable(session, &cli_ref, obj, class,
1883 					     function))
1884 			return PKCS11_CKR_ATTRIBUTE_READ_ONLY;
1885 
1886 		/*
1887 		 * Checks for modification in PKCS11_CKA_TOKEN and
1888 		 * PKCS11_CKA_PRIVATE are required for PKCS11_FUNCTION_COPY
1889 		 * only, so skip them for PKCS11_FUNCTION_MODIFY.
1890 		 */
1891 		if (function == PKCS11_FUNCTION_MODIFY)
1892 			continue;
1893 
1894 		/*
1895 		 * An attempt to copy an object to a token will fail for
1896 		 * RO session
1897 		 */
1898 		if (cli_ref.id == PKCS11_CKA_TOKEN &&
1899 		    get_bool(head, PKCS11_CKA_TOKEN)) {
1900 			if (!pkcs11_session_is_read_write(session)) {
1901 				DMSG("Can't copy to token in a RO session");
1902 				return PKCS11_CKR_SESSION_READ_ONLY;
1903 			}
1904 		}
1905 
1906 		if (cli_ref.id == PKCS11_CKA_PRIVATE) {
1907 			bool parent_priv =
1908 				get_bool(obj->attributes, cli_ref.id);
1909 			bool obj_priv = get_bool(head, cli_ref.id);
1910 
1911 			/*
1912 			 * If PKCS11_CKA_PRIVATE is being set to TRUE from
1913 			 * FALSE, user has to be logged in
1914 			 */
1915 			if (!parent_priv && obj_priv) {
1916 				if ((pkcs11_session_is_public(session) ||
1917 				     pkcs11_session_is_so(session)))
1918 					return PKCS11_CKR_USER_NOT_LOGGED_IN;
1919 			}
1920 
1921 			/*
1922 			 * Restriction added - Even for Copy, do not allow
1923 			 * modification of CKA_PRIVATE from TRUE to FALSE
1924 			 */
1925 			if (parent_priv && !obj_priv)
1926 				return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1927 		}
1928 	}
1929 
1930 	return PKCS11_CKR_OK;
1931 }
1932 
1933 static enum pkcs11_rc set_secret_key_data(struct obj_attrs **head, void *data,
1934 					  size_t key_size)
1935 {
1936 	uint32_t size = sizeof(uint32_t);
1937 	uint32_t key_length = 0;
1938 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
1939 
1940 	/* Get key size if present in template */
1941 	rc = get_attribute(*head, PKCS11_CKA_VALUE_LEN, &key_length, &size);
1942 	if (rc && rc != PKCS11_RV_NOT_FOUND)
1943 		return rc;
1944 
1945 	if (key_length) {
1946 		if (key_size < key_length)
1947 			return PKCS11_CKR_DATA_LEN_RANGE;
1948 	} else {
1949 		key_length = key_size;
1950 		rc = set_attribute(head, PKCS11_CKA_VALUE_LEN, &key_length,
1951 				   sizeof(uint32_t));
1952 		if (rc)
1953 			return rc;
1954 	}
1955 
1956 	/* Now we can check the VALUE_LEN field */
1957 	rc = check_created_attrs(*head, NULL);
1958 	if (rc)
1959 		return rc;
1960 
1961 	/* Remove the default empty value attribute if found */
1962 	rc = remove_empty_attribute(head, PKCS11_CKA_VALUE);
1963 	if (rc != PKCS11_CKR_OK && rc != PKCS11_RV_NOT_FOUND)
1964 		return PKCS11_CKR_GENERAL_ERROR;
1965 
1966 	return add_attribute(head, PKCS11_CKA_VALUE, data, key_length);
1967 }
1968 
1969 enum pkcs11_rc set_key_data(struct obj_attrs **head, void *data,
1970 			    size_t key_size)
1971 {
1972 	switch (get_class(*head)) {
1973 	case PKCS11_CKO_SECRET_KEY:
1974 		return set_secret_key_data(head, data, key_size);
1975 	default:
1976 		return PKCS11_CKR_GENERAL_ERROR;
1977 	}
1978 }
1979 
1980 enum pkcs11_rc get_key_data_to_wrap(struct obj_attrs *head, void **data,
1981 				    uint32_t *sz)
1982 {
1983 	switch (get_class(head)) {
1984 	case PKCS11_CKO_SECRET_KEY:
1985 		if (get_attribute_ptr(head, PKCS11_CKA_VALUE, data, sz))
1986 			return PKCS11_CKR_ARGUMENTS_BAD;
1987 		break;
1988 	default:
1989 		return PKCS11_CKR_GENERAL_ERROR;
1990 	}
1991 
1992 	return PKCS11_CKR_OK;
1993 }
1994 
1995 enum pkcs11_rc add_missing_attribute_id(struct obj_attrs **pub_head,
1996 					struct obj_attrs **priv_head)
1997 {
1998 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
1999 	void *id1 = NULL;
2000 	uint32_t id1_size = 0;
2001 	void *id2 = NULL;
2002 	uint32_t id2_size = 0;
2003 
2004 	assert(pub_head);
2005 	assert(priv_head);
2006 
2007 	rc = get_attribute_ptr(*pub_head, PKCS11_CKA_ID, &id1, &id1_size);
2008 	if (rc) {
2009 		if (rc != PKCS11_RV_NOT_FOUND)
2010 			return rc;
2011 		id1 = NULL;
2012 	} else if (!id1_size) {
2013 		id1 = NULL;
2014 	}
2015 
2016 	rc = get_attribute_ptr(*priv_head, PKCS11_CKA_ID, &id2, &id2_size);
2017 	if (rc) {
2018 		if (rc != PKCS11_RV_NOT_FOUND)
2019 			return rc;
2020 		id2 = NULL;
2021 	} else if (!id2_size) {
2022 		id2 = NULL;
2023 	}
2024 
2025 	/* Both have value -- let them be what caller has specified them */
2026 	if (id1 && id2)
2027 		return PKCS11_CKR_OK;
2028 
2029 	/* Both are empty -- leave empty values */
2030 	if (!id1 && !id2)
2031 		return PKCS11_CKR_OK;
2032 
2033 	/* Cross copy CKA_ID value */
2034 	if (id1)
2035 		return set_attribute(priv_head, PKCS11_CKA_ID, id1, id1_size);
2036 	else
2037 		return set_attribute(pub_head, PKCS11_CKA_ID, id2, id2_size);
2038 }
2039