xref: /optee_os/ta/pkcs11/src/pkcs11_attributes.c (revision b6ca7e5dd226f2c3691d798ab81d1cf35dfec33e)
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 /* Byte size of CKA_ID attribute when generated locally */
26 #define PKCS11_CKA_DEFAULT_SIZE		16
27 
28 static uint32_t pkcs11_func2ckfm(enum processing_func function)
29 {
30 	switch (function) {
31 	case PKCS11_FUNCTION_DIGEST:
32 		return PKCS11_CKFM_DIGEST;
33 	case PKCS11_FUNCTION_GENERATE:
34 		return PKCS11_CKFM_GENERATE;
35 	case PKCS11_FUNCTION_GENERATE_PAIR:
36 		return PKCS11_CKFM_GENERATE_KEY_PAIR;
37 	case PKCS11_FUNCTION_DERIVE:
38 		return PKCS11_CKFM_DERIVE;
39 	case PKCS11_FUNCTION_WRAP:
40 		return PKCS11_CKFM_WRAP;
41 	case PKCS11_FUNCTION_UNWRAP:
42 		return PKCS11_CKFM_UNWRAP;
43 	case PKCS11_FUNCTION_ENCRYPT:
44 		return PKCS11_CKFM_ENCRYPT;
45 	case PKCS11_FUNCTION_DECRYPT:
46 		return PKCS11_CKFM_DECRYPT;
47 	case PKCS11_FUNCTION_SIGN:
48 		return PKCS11_CKFM_SIGN;
49 	case PKCS11_FUNCTION_VERIFY:
50 		return PKCS11_CKFM_VERIFY;
51 	case PKCS11_FUNCTION_SIGN_RECOVER:
52 		return PKCS11_CKFM_SIGN_RECOVER;
53 	case PKCS11_FUNCTION_VERIFY_RECOVER:
54 		return PKCS11_CKFM_VERIFY_RECOVER;
55 	default:
56 		return 0;
57 	}
58 }
59 
60 enum pkcs11_rc
61 check_mechanism_against_processing(struct pkcs11_session *session,
62 				   enum pkcs11_mechanism_id mechanism_type,
63 				   enum processing_func function,
64 				   enum processing_step step)
65 {
66 	bool allowed = false;
67 
68 	switch (step) {
69 	case PKCS11_FUNC_STEP_INIT:
70 		switch (function) {
71 		case PKCS11_FUNCTION_IMPORT:
72 		case PKCS11_FUNCTION_COPY:
73 		case PKCS11_FUNCTION_MODIFY:
74 		case PKCS11_FUNCTION_DESTROY:
75 			return PKCS11_CKR_OK;
76 		default:
77 			break;
78 		}
79 		/*
80 		 * Check that the returned PKCS11_CKFM_* flag from
81 		 * pkcs11_func2ckfm() is among the ones from
82 		 * mechanism_supported_flags().
83 		 */
84 		allowed = mechanism_supported_flags(mechanism_type) &
85 			  pkcs11_func2ckfm(function);
86 		break;
87 
88 	case PKCS11_FUNC_STEP_ONESHOT:
89 		if (session->processing->always_authen &&
90 		    !session->processing->relogged)
91 			return PKCS11_CKR_USER_NOT_LOGGED_IN;
92 
93 		if (session->processing->updated) {
94 			EMSG("Cannot perform one-shot on updated processing");
95 			return PKCS11_CKR_OPERATION_ACTIVE;
96 		}
97 
98 		allowed = true;
99 		break;
100 
101 	case PKCS11_FUNC_STEP_UPDATE:
102 		if (session->processing->always_authen &&
103 		    !session->processing->relogged)
104 			return PKCS11_CKR_USER_NOT_LOGGED_IN;
105 
106 		allowed = !mechanism_is_one_shot_only(mechanism_type);
107 		break;
108 
109 	case PKCS11_FUNC_STEP_FINAL:
110 		if (session->processing->always_authen &&
111 		    !session->processing->relogged)
112 			return PKCS11_CKR_USER_NOT_LOGGED_IN;
113 
114 		return PKCS11_CKR_OK;
115 
116 	default:
117 		TEE_Panic(step);
118 		break;
119 	}
120 
121 	if (!allowed) {
122 		EMSG("Processing %#x/%s not permitted (%u/%u)",
123 		     (unsigned int)mechanism_type, id2str_proc(mechanism_type),
124 		     function, step);
125 		return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
126 	}
127 
128 	return PKCS11_CKR_OK;
129 }
130 
131 /*
132  * Object default boolean attributes as per PKCS#11
133  */
134 static uint8_t *pkcs11_object_default_boolprop(uint32_t attribute)
135 {
136 	static const uint8_t bool_true = 1;
137 	static const uint8_t bool_false;
138 
139 	switch (attribute) {
140 	/* As per PKCS#11 default value */
141 	case PKCS11_CKA_MODIFIABLE:
142 	case PKCS11_CKA_COPYABLE:
143 	case PKCS11_CKA_DESTROYABLE:
144 		return (uint8_t *)&bool_true;
145 	case PKCS11_CKA_TOKEN:
146 	case PKCS11_CKA_PRIVATE:
147 	case PKCS11_CKA_WRAP_WITH_TRUSTED:
148 	case PKCS11_CKA_ALWAYS_AUTHENTICATE:
149 	case PKCS11_CKA_SENSITIVE:
150 		return (uint8_t *)&bool_false;
151 	/* Token specific default value */
152 	case PKCS11_CKA_SIGN:
153 	case PKCS11_CKA_VERIFY:
154 	case PKCS11_CKA_DERIVE:
155 	case PKCS11_CKA_ENCRYPT:
156 	case PKCS11_CKA_DECRYPT:
157 	case PKCS11_CKA_SIGN_RECOVER:
158 	case PKCS11_CKA_VERIFY_RECOVER:
159 	case PKCS11_CKA_WRAP:
160 	case PKCS11_CKA_UNWRAP:
161 	case PKCS11_CKA_EXTRACTABLE:
162 	case PKCS11_CKA_TRUSTED:
163 		return (uint8_t *)&bool_false;
164 	default:
165 		DMSG("No default for boolprop attribute %#"PRIx32, attribute);
166 		return NULL;
167 	}
168 }
169 
170 /*
171  * Object expects several boolean attributes to be set to a default value
172  * or to a validate client configuration value. This function append the input
173  * attribute (id/size/value) in the serialized object.
174  */
175 static enum pkcs11_rc pkcs11_import_object_boolprop(struct obj_attrs **out,
176 						    struct obj_attrs *templ,
177 						    uint32_t attribute)
178 {
179 	enum pkcs11_rc rc = PKCS11_CKR_OK;
180 	uint8_t bbool = 0;
181 	uint32_t size = sizeof(uint8_t);
182 	void *attr = NULL;
183 
184 	rc = get_attribute(templ, attribute, &bbool, &size);
185 	if (rc) {
186 		if (rc != PKCS11_RV_NOT_FOUND)
187 			return rc;
188 		attr = pkcs11_object_default_boolprop(attribute);
189 		if (!attr)
190 			return PKCS11_CKR_TEMPLATE_INCOMPLETE;
191 	} else {
192 		attr = &bbool;
193 	}
194 
195 	/* Boolean attributes are 1byte in the ABI, no alignment issue */
196 	return add_attribute(out, attribute, attr, sizeof(uint8_t));
197 }
198 
199 static enum pkcs11_rc set_mandatory_boolprops(struct obj_attrs **out,
200 					      struct obj_attrs *temp,
201 					      uint32_t const *bp,
202 					      size_t bp_count)
203 {
204 	enum pkcs11_rc rc = PKCS11_CKR_OK;
205 	size_t n = 0;
206 
207 	for (n = 0; n < bp_count; n++) {
208 		rc = pkcs11_import_object_boolprop(out, temp, bp[n]);
209 		if (rc)
210 			return rc;
211 	}
212 
213 	return rc;
214 }
215 
216 static enum pkcs11_rc set_mandatory_attributes(struct obj_attrs **out,
217 					       struct obj_attrs *temp,
218 					       uint32_t const *attrs,
219 					       size_t attrs_count)
220 {
221 	enum pkcs11_rc rc = PKCS11_CKR_OK;
222 	size_t n = 0;
223 
224 	for (n = 0; n < attrs_count; n++) {
225 		uint32_t size = 0;
226 		void *value = NULL;
227 
228 		if (get_attribute_ptr(temp, attrs[n], &value, &size))
229 			return PKCS11_CKR_TEMPLATE_INCOMPLETE;
230 
231 		rc = add_attribute(out, attrs[n], value, size);
232 		if (rc)
233 			return rc;
234 	}
235 
236 	return rc;
237 }
238 
239 static enum pkcs11_rc get_default_value(enum pkcs11_attr_id id, void **value,
240 					uint32_t *size)
241 {
242 	/* should have been taken care of already */
243 	assert(!pkcs11_attr_is_boolean(id));
244 
245 	if (id == PKCS11_CKA_PUBLIC_KEY_INFO) {
246 		EMSG("Cannot provide default PUBLIC_KEY_INFO");
247 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
248 	}
249 
250 	/* All other attributes have an empty default value */
251 	*value = NULL;
252 	*size = 0;
253 	return PKCS11_CKR_OK;
254 }
255 
256 static enum pkcs11_rc set_optional_attributes_with_def(struct obj_attrs **out,
257 						       struct obj_attrs *temp,
258 						       uint32_t const *attrs,
259 						       size_t attrs_count,
260 						       bool default_to_null)
261 {
262 	enum pkcs11_rc rc = PKCS11_CKR_OK;
263 	size_t n = 0;
264 
265 	for (n = 0; n < attrs_count; n++) {
266 		uint32_t size = 0;
267 		void *value = NULL;
268 
269 		rc = get_attribute_ptr(temp, attrs[n], &value, &size);
270 		if (rc == PKCS11_RV_NOT_FOUND) {
271 			if (default_to_null) {
272 				rc = get_default_value(attrs[n], &value, &size);
273 			} else {
274 				rc = PKCS11_CKR_OK;
275 				continue;
276 			}
277 		}
278 		if (rc)
279 			return rc;
280 
281 		rc = add_attribute(out, attrs[n], value, size);
282 		if (rc)
283 			return rc;
284 	}
285 
286 	return rc;
287 }
288 
289 static enum pkcs11_rc set_attributes_opt_or_null(struct obj_attrs **out,
290 						 struct obj_attrs *temp,
291 						 uint32_t const *attrs,
292 						 size_t attrs_count)
293 {
294 	return set_optional_attributes_with_def(out, temp, attrs, attrs_count,
295 						true /* defaults to empty */);
296 }
297 
298 static enum pkcs11_rc set_optional_attributes(struct obj_attrs **out,
299 					      struct obj_attrs *temp,
300 					      uint32_t const *attrs,
301 					      size_t attrs_count)
302 {
303 	return set_optional_attributes_with_def(out, temp, attrs, attrs_count,
304 						false /* no default value */);
305 }
306 
307 /*
308  * Below are listed the mandated or optional expected attributes for
309  * PKCS#11 storage objects.
310  *
311  * Note: boolprops (mandated boolean attributes) PKCS11_CKA_ALWAYS_SENSITIVE,
312  * and PKCS11_CKA_NEVER_EXTRACTABLE are set by the token, not provided
313  * in the client template.
314  */
315 
316 /* PKCS#11 specification for any object (session/token) of the storage */
317 static const uint32_t any_object_boolprops[] = {
318 	PKCS11_CKA_TOKEN, PKCS11_CKA_PRIVATE,
319 	PKCS11_CKA_MODIFIABLE, PKCS11_CKA_COPYABLE, PKCS11_CKA_DESTROYABLE,
320 };
321 
322 static const uint32_t any_object_opt_or_null[] = {
323 	PKCS11_CKA_LABEL,
324 };
325 
326 /* PKCS#11 specification for raw data object (+any_object_xxx) */
327 const uint32_t raw_data_opt_or_null[] = {
328 	PKCS11_CKA_OBJECT_ID, PKCS11_CKA_APPLICATION, PKCS11_CKA_VALUE,
329 };
330 
331 /* PKCS#11 specification for any key object (+any_object_xxx) */
332 static const uint32_t any_key_boolprops[] = {
333 	PKCS11_CKA_DERIVE,
334 };
335 
336 static const uint32_t any_key_opt_or_null[] = {
337 	PKCS11_CKA_ID,
338 	PKCS11_CKA_START_DATE, PKCS11_CKA_END_DATE,
339 };
340 
341 static const uint32_t any_key_optional[] = {
342 	PKCS11_CKA_ALLOWED_MECHANISMS,
343 };
344 
345 /* PKCS#11 specification for any symmetric key (+any_key_xxx) */
346 static const uint32_t symm_key_boolprops[] = {
347 	PKCS11_CKA_ENCRYPT, PKCS11_CKA_DECRYPT,
348 	PKCS11_CKA_SIGN, PKCS11_CKA_VERIFY,
349 	PKCS11_CKA_WRAP, PKCS11_CKA_UNWRAP,
350 	PKCS11_CKA_SENSITIVE, PKCS11_CKA_EXTRACTABLE,
351 	PKCS11_CKA_WRAP_WITH_TRUSTED, PKCS11_CKA_TRUSTED,
352 };
353 
354 static const uint32_t symm_key_opt_or_null[] = {
355 	PKCS11_CKA_WRAP_TEMPLATE, PKCS11_CKA_UNWRAP_TEMPLATE,
356 	PKCS11_CKA_DERIVE_TEMPLATE,
357 	PKCS11_CKA_VALUE, PKCS11_CKA_VALUE_LEN,
358 };
359 
360 /* PKCS#11 specification for any asymmetric public key (+any_key_xxx) */
361 static const uint32_t public_key_boolprops[] = {
362 	PKCS11_CKA_ENCRYPT, PKCS11_CKA_VERIFY, PKCS11_CKA_VERIFY_RECOVER,
363 	PKCS11_CKA_WRAP,
364 	PKCS11_CKA_TRUSTED,
365 };
366 
367 static const uint32_t public_key_mandated[] = {
368 	PKCS11_CKA_SUBJECT
369 };
370 
371 static const uint32_t public_key_opt_or_null[] = {
372 	PKCS11_CKA_WRAP_TEMPLATE, PKCS11_CKA_PUBLIC_KEY_INFO,
373 };
374 
375 /* PKCS#11 specification for any asymmetric private key (+any_key_xxx) */
376 static const uint32_t private_key_boolprops[] = {
377 	PKCS11_CKA_DECRYPT, PKCS11_CKA_SIGN, PKCS11_CKA_SIGN_RECOVER,
378 	PKCS11_CKA_UNWRAP,
379 	PKCS11_CKA_SENSITIVE, PKCS11_CKA_EXTRACTABLE,
380 	PKCS11_CKA_WRAP_WITH_TRUSTED, PKCS11_CKA_ALWAYS_AUTHENTICATE,
381 };
382 
383 static const uint32_t private_key_mandated[] = {
384 	PKCS11_CKA_SUBJECT
385 };
386 
387 static const uint32_t private_key_opt_or_null[] = {
388 	PKCS11_CKA_UNWRAP_TEMPLATE, PKCS11_CKA_PUBLIC_KEY_INFO,
389 };
390 
391 /* PKCS#11 specification for any RSA key (+public/private_key_xxx) */
392 static const uint32_t rsa_public_key_mandated[] = {
393 	PKCS11_CKA_MODULUS_BITS,
394 };
395 
396 static const uint32_t rsa_public_key_opt_or_null[] = {
397 	PKCS11_CKA_MODULUS, PKCS11_CKA_PUBLIC_EXPONENT,
398 };
399 
400 static const uint32_t rsa_private_key_opt_or_null[] = {
401 	PKCS11_CKA_MODULUS, PKCS11_CKA_PUBLIC_EXPONENT,
402 	PKCS11_CKA_PRIVATE_EXPONENT,
403 	PKCS11_CKA_PRIME_1, PKCS11_CKA_PRIME_2,
404 	PKCS11_CKA_EXPONENT_1, PKCS11_CKA_EXPONENT_2, PKCS11_CKA_COEFFICIENT,
405 };
406 
407 /* PKCS#11 specification for any EC key (+public/private_key_xxx) */
408 static const uint32_t ec_public_key_mandated[] = {
409 	PKCS11_CKA_EC_PARAMS,
410 };
411 
412 static const uint32_t ec_public_key_opt_or_null[] = {
413 	PKCS11_CKA_EC_POINT,
414 };
415 
416 static const uint32_t ec_private_key_mandated[] = {
417 	PKCS11_CKA_EC_PARAMS,
418 };
419 
420 static const uint32_t ec_private_key_opt_or_null[] = {
421 	PKCS11_CKA_VALUE,
422 };
423 
424 static enum pkcs11_rc create_storage_attributes(struct obj_attrs **out,
425 						struct obj_attrs *temp)
426 {
427 	enum pkcs11_class_id class = PKCS11_CKO_UNDEFINED_ID;
428 	enum pkcs11_rc rc = PKCS11_CKR_OK;
429 
430 	rc = init_attributes_head(out);
431 	if (rc)
432 		return rc;
433 
434 	/* Object class is mandatory */
435 	class = get_class(temp);
436 	if (class == PKCS11_CKO_UNDEFINED_ID) {
437 		EMSG("Class attribute not found");
438 
439 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
440 	}
441 	rc = add_attribute(out, PKCS11_CKA_CLASS, &class, sizeof(uint32_t));
442 	if (rc)
443 		return rc;
444 
445 	rc = set_mandatory_boolprops(out, temp, any_object_boolprops,
446 				     ARRAY_SIZE(any_object_boolprops));
447 	if (rc)
448 		return rc;
449 
450 	return set_attributes_opt_or_null(out, temp, any_object_opt_or_null,
451 					  ARRAY_SIZE(any_object_opt_or_null));
452 }
453 
454 static enum pkcs11_rc create_genkey_attributes(struct obj_attrs **out,
455 					       struct obj_attrs *temp)
456 {
457 	uint32_t type = PKCS11_CKO_UNDEFINED_ID;
458 	enum pkcs11_rc rc = PKCS11_CKR_OK;
459 
460 	rc = create_storage_attributes(out, temp);
461 	if (rc)
462 		return rc;
463 
464 	type = get_key_type(temp);
465 	if (type == PKCS11_CKK_UNDEFINED_ID) {
466 		EMSG("Key type attribute not found");
467 
468 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
469 	}
470 	rc = add_attribute(out, PKCS11_CKA_KEY_TYPE, &type, sizeof(uint32_t));
471 	if (rc)
472 		return rc;
473 
474 	rc = set_mandatory_boolprops(out, temp, any_key_boolprops,
475 				     ARRAY_SIZE(any_key_boolprops));
476 	if (rc)
477 		return rc;
478 
479 	rc = set_attributes_opt_or_null(out, temp, any_key_opt_or_null,
480 					ARRAY_SIZE(any_key_opt_or_null));
481 	if (rc)
482 		return rc;
483 
484 	return set_optional_attributes(out, temp, any_key_optional,
485 				       ARRAY_SIZE(any_key_optional));
486 
487 }
488 
489 static enum pkcs11_rc create_symm_key_attributes(struct obj_attrs **out,
490 						 struct obj_attrs *temp)
491 {
492 	enum pkcs11_rc rc = PKCS11_CKR_OK;
493 
494 	assert(get_class(temp) == PKCS11_CKO_SECRET_KEY);
495 
496 	rc = create_genkey_attributes(out, temp);
497 	if (rc)
498 		return rc;
499 
500 	assert(get_class(*out) == PKCS11_CKO_SECRET_KEY);
501 
502 	switch (get_key_type(*out)) {
503 	case PKCS11_CKK_GENERIC_SECRET:
504 	case PKCS11_CKK_AES:
505 	case PKCS11_CKK_MD5_HMAC:
506 	case PKCS11_CKK_SHA_1_HMAC:
507 	case PKCS11_CKK_SHA256_HMAC:
508 	case PKCS11_CKK_SHA384_HMAC:
509 	case PKCS11_CKK_SHA512_HMAC:
510 	case PKCS11_CKK_SHA224_HMAC:
511 		break;
512 	default:
513 		EMSG("Invalid key type %#"PRIx32"/%s",
514 		     get_key_type(*out), id2str_key_type(get_key_type(*out)));
515 
516 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
517 	}
518 
519 	rc = set_mandatory_boolprops(out, temp, symm_key_boolprops,
520 				     ARRAY_SIZE(symm_key_boolprops));
521 	if (rc)
522 		return rc;
523 
524 	return set_attributes_opt_or_null(out, temp, symm_key_opt_or_null,
525 					  ARRAY_SIZE(symm_key_opt_or_null));
526 }
527 
528 static enum pkcs11_rc create_data_attributes(struct obj_attrs **out,
529 					     struct obj_attrs *temp)
530 {
531 	enum pkcs11_rc rc = PKCS11_CKR_OK;
532 
533 	assert(get_class(temp) == PKCS11_CKO_DATA);
534 
535 	rc = create_storage_attributes(out, temp);
536 	if (rc)
537 		return rc;
538 
539 	assert(get_class(*out) == PKCS11_CKO_DATA);
540 
541 	return set_attributes_opt_or_null(out, temp, raw_data_opt_or_null,
542 					  ARRAY_SIZE(raw_data_opt_or_null));
543 }
544 
545 static enum pkcs11_rc create_pub_key_attributes(struct obj_attrs **out,
546 						struct obj_attrs *temp)
547 {
548 	uint32_t const *mandated = NULL;
549 	uint32_t const *opt_or_null = NULL;
550 	size_t mandated_count = 0;
551 	size_t opt_or_null_count = 0;
552 	enum pkcs11_rc rc = PKCS11_CKR_OK;
553 
554 	assert(get_class(temp) == PKCS11_CKO_PUBLIC_KEY);
555 
556 	rc = create_genkey_attributes(out, temp);
557 	if (rc)
558 		return rc;
559 
560 	assert(get_class(*out) == PKCS11_CKO_PUBLIC_KEY);
561 
562 	rc = set_mandatory_boolprops(out, temp, public_key_boolprops,
563 				     ARRAY_SIZE(public_key_boolprops));
564 	if (rc)
565 		return rc;
566 
567 	rc = set_mandatory_attributes(out, temp, public_key_mandated,
568 				      ARRAY_SIZE(public_key_mandated));
569 	if (rc)
570 		return rc;
571 
572 	rc = set_attributes_opt_or_null(out, temp,
573 					public_key_opt_or_null,
574 					ARRAY_SIZE(public_key_opt_or_null));
575 	if (rc)
576 		return rc;
577 
578 	switch (get_key_type(*out)) {
579 	case PKCS11_CKK_RSA:
580 		mandated = rsa_public_key_mandated;
581 		opt_or_null = rsa_public_key_opt_or_null;
582 		mandated_count = ARRAY_SIZE(rsa_public_key_mandated);
583 		opt_or_null_count = ARRAY_SIZE(rsa_public_key_opt_or_null);
584 		break;
585 	case PKCS11_CKK_EC:
586 		mandated = ec_public_key_mandated;
587 		opt_or_null = ec_public_key_opt_or_null;
588 		mandated_count = ARRAY_SIZE(ec_public_key_mandated);
589 		opt_or_null_count = ARRAY_SIZE(ec_public_key_opt_or_null);
590 		break;
591 	default:
592 		EMSG("Invalid key type %#"PRIx32"/%s",
593 		     get_key_type(*out), id2str_key_type(get_key_type(*out)));
594 
595 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
596 	}
597 
598 	rc = set_mandatory_attributes(out, temp, mandated, mandated_count);
599 	if (rc)
600 		return rc;
601 
602 	return set_attributes_opt_or_null(out, temp, opt_or_null,
603 					  opt_or_null_count);
604 }
605 
606 static enum pkcs11_rc create_priv_key_attributes(struct obj_attrs **out,
607 						 struct obj_attrs *temp)
608 {
609 	uint32_t const *mandated = NULL;
610 	uint32_t const *opt_or_null = NULL;
611 	size_t mandated_count = 0;
612 	size_t opt_or_null_count = 0;
613 	enum pkcs11_rc rc = PKCS11_CKR_OK;
614 
615 	assert(get_class(temp) == PKCS11_CKO_PRIVATE_KEY);
616 
617 	rc = create_genkey_attributes(out, temp);
618 	if (rc)
619 		return rc;
620 
621 	assert(get_class(*out) == PKCS11_CKO_PRIVATE_KEY);
622 
623 	rc = set_mandatory_boolprops(out, temp, private_key_boolprops,
624 				     ARRAY_SIZE(private_key_boolprops));
625 	if (rc)
626 		return rc;
627 
628 	rc = set_mandatory_attributes(out, temp, private_key_mandated,
629 				      ARRAY_SIZE(private_key_mandated));
630 	if (rc)
631 		return rc;
632 
633 	rc = set_attributes_opt_or_null(out, temp, private_key_opt_or_null,
634 					ARRAY_SIZE(private_key_opt_or_null));
635 	if (rc)
636 		return rc;
637 
638 	switch (get_key_type(*out)) {
639 	case PKCS11_CKK_RSA:
640 		opt_or_null = rsa_private_key_opt_or_null;
641 		opt_or_null_count = ARRAY_SIZE(rsa_private_key_opt_or_null);
642 		break;
643 	case PKCS11_CKK_EC:
644 		mandated = ec_private_key_mandated;
645 		opt_or_null = ec_private_key_opt_or_null;
646 		mandated_count = ARRAY_SIZE(ec_private_key_mandated);
647 		opt_or_null_count = ARRAY_SIZE(ec_private_key_opt_or_null);
648 		break;
649 	default:
650 		EMSG("Invalid key type %#"PRIx32"/%s",
651 		     get_key_type(*out), id2str_key_type(get_key_type(*out)));
652 
653 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
654 	}
655 
656 	rc = set_mandatory_attributes(out, temp, mandated, mandated_count);
657 	if (rc)
658 		return rc;
659 
660 	return set_attributes_opt_or_null(out, temp, opt_or_null,
661 					  opt_or_null_count);
662 }
663 
664 /*
665  * Create an attribute list for a new object from a template and a parent
666  * object (optional) for an object generation function (generate, copy,
667  * derive...).
668  *
669  * PKCS#11 directives on the supplied template and expected return value:
670  * - template has an invalid attribute ID: ATTRIBUTE_TYPE_INVALID
671  * - template has an invalid value for an attribute: ATTRIBUTE_VALID_INVALID
672  * - template has value for a read-only attribute: ATTRIBUTE_READ_ONLY
673  * - template+default+parent => still miss an attribute: TEMPLATE_INCONSISTENT
674  *
675  * INFO on PKCS11_CMD_COPY_OBJECT:
676  * - parent PKCS11_CKA_COPYIABLE=false => return ACTION_PROHIBITED.
677  * - template can specify PKCS11_CKA_TOKEN, PKCS11_CKA_PRIVATE,
678  *   PKCS11_CKA_MODIFIABLE, PKCS11_CKA_DESTROYABLE.
679  * - SENSITIVE can change from false to true, not from true to false.
680  * - LOCAL is the parent LOCAL
681  */
682 enum pkcs11_rc
683 create_attributes_from_template(struct obj_attrs **out, void *template,
684 				size_t template_size,
685 				struct obj_attrs *parent __unused,
686 				enum processing_func function,
687 				enum pkcs11_mechanism_id mecha,
688 				enum pkcs11_class_id template_class __unused)
689 {
690 	struct obj_attrs *temp = NULL;
691 	struct obj_attrs *attrs = NULL;
692 	enum pkcs11_rc rc = PKCS11_CKR_OK;
693 	uint8_t local = 0;
694 	uint8_t always_sensitive = 0;
695 	uint8_t never_extract = 0;
696 	uint32_t class = PKCS11_UNDEFINED_ID;
697 	uint32_t type = PKCS11_UNDEFINED_ID;
698 	uint32_t mechanism_id = PKCS11_CKM_UNDEFINED_ID;
699 
700 #ifdef DEBUG	/* Sanity: check function argument */
701 	trace_attributes_from_api_head("template", template, template_size);
702 	switch (function) {
703 	case PKCS11_FUNCTION_GENERATE:
704 	case PKCS11_FUNCTION_IMPORT:
705 	case PKCS11_FUNCTION_MODIFY:
706 	case PKCS11_FUNCTION_COPY:
707 		break;
708 	default:
709 		TEE_Panic(TEE_ERROR_NOT_SUPPORTED);
710 	}
711 #endif
712 
713 	/*
714 	 * For PKCS11_FUNCTION_GENERATE, find the class and type
715 	 * based on the mechanism. These will be passed as hint
716 	 * sanitize_client_object() and added in temp if not
717 	 * already present
718 	 */
719 	if (function == PKCS11_FUNCTION_GENERATE) {
720 		switch (mecha) {
721 		case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
722 			class = PKCS11_CKO_SECRET_KEY;
723 			type = PKCS11_CKK_GENERIC_SECRET;
724 			break;
725 		case PKCS11_CKM_AES_KEY_GEN:
726 			class = PKCS11_CKO_SECRET_KEY;
727 			type = PKCS11_CKK_AES;
728 			break;
729 		default:
730 			TEE_Panic(TEE_ERROR_NOT_SUPPORTED);
731 		}
732 	}
733 
734 	/*
735 	 * Check and remove duplicates if any and create a new temporary
736 	 * template
737 	 */
738 	rc = sanitize_client_object(&temp, template, template_size, class,
739 				    type);
740 	if (rc)
741 		goto out;
742 
743 	/*
744 	 * For function type modify and copy return the created template
745 	 * from here. Rest of the code below is for creating objects
746 	 * or generating keys.
747 	 */
748 	switch (function) {
749 	case PKCS11_FUNCTION_MODIFY:
750 	case PKCS11_FUNCTION_COPY:
751 		*out = temp;
752 		return rc;
753 	default:
754 		break;
755 	}
756 
757 	/*
758 	 * Check if class and type in temp are consistent with the mechanism
759 	 */
760 	switch (mecha) {
761 	case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
762 		if (get_class(temp) != PKCS11_CKO_SECRET_KEY ||
763 		    get_key_type(temp) != PKCS11_CKK_GENERIC_SECRET) {
764 			rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
765 			goto out;
766 		}
767 		break;
768 	case PKCS11_CKM_AES_KEY_GEN:
769 		if (get_class(temp) != PKCS11_CKO_SECRET_KEY ||
770 		    get_key_type(temp) != PKCS11_CKK_AES) {
771 			rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
772 			goto out;
773 		}
774 		break;
775 	default:
776 		break;
777 	}
778 
779 	if (!sanitize_consistent_class_and_type(temp)) {
780 		EMSG("Inconsistent class/type");
781 		rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
782 		goto out;
783 	}
784 
785 	switch (get_class(temp)) {
786 	case PKCS11_CKO_DATA:
787 		rc = create_data_attributes(&attrs, temp);
788 		break;
789 	case PKCS11_CKO_SECRET_KEY:
790 		rc = create_symm_key_attributes(&attrs, temp);
791 		break;
792 	case PKCS11_CKO_PUBLIC_KEY:
793 		rc = create_pub_key_attributes(&attrs, temp);
794 		break;
795 	case PKCS11_CKO_PRIVATE_KEY:
796 		rc = create_priv_key_attributes(&attrs, temp);
797 		break;
798 	default:
799 		DMSG("Invalid object class %#"PRIx32"/%s",
800 		     get_class(temp), id2str_class(get_class(temp)));
801 
802 		rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
803 		break;
804 	}
805 	if (rc)
806 		goto out;
807 
808 	if (get_attribute_ptr(temp, PKCS11_CKA_LOCAL, NULL, NULL) !=
809 	    PKCS11_RV_NOT_FOUND) {
810 		rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
811 		goto out;
812 	}
813 
814 	if (get_attribute_ptr(temp, PKCS11_CKA_KEY_GEN_MECHANISM, NULL, NULL) !=
815 	    PKCS11_RV_NOT_FOUND) {
816 		rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
817 		goto out;
818 	}
819 
820 	switch (function) {
821 	case PKCS11_FUNCTION_GENERATE:
822 		local = PKCS11_TRUE;
823 		break;
824 	case PKCS11_FUNCTION_IMPORT:
825 	default:
826 		local = PKCS11_FALSE;
827 		break;
828 	}
829 	rc = add_attribute(&attrs, PKCS11_CKA_LOCAL, &local, sizeof(local));
830 	if (rc)
831 		goto out;
832 
833 	switch (get_class(attrs)) {
834 	case PKCS11_CKO_SECRET_KEY:
835 	case PKCS11_CKO_PRIVATE_KEY:
836 	case PKCS11_CKO_PUBLIC_KEY:
837 		always_sensitive = PKCS11_FALSE;
838 		never_extract = PKCS11_FALSE;
839 
840 		switch (function) {
841 		case PKCS11_FUNCTION_GENERATE:
842 			always_sensitive = get_bool(attrs,
843 						    PKCS11_CKA_SENSITIVE);
844 			never_extract = !get_bool(attrs,
845 						  PKCS11_CKA_EXTRACTABLE);
846 			break;
847 		default:
848 			break;
849 		}
850 
851 		rc = add_attribute(&attrs, PKCS11_CKA_ALWAYS_SENSITIVE,
852 				   &always_sensitive, sizeof(always_sensitive));
853 		if (rc)
854 			goto out;
855 
856 		rc = add_attribute(&attrs, PKCS11_CKA_NEVER_EXTRACTABLE,
857 				   &never_extract, sizeof(never_extract));
858 		if (rc)
859 			goto out;
860 
861 		/* Keys mandate attribute PKCS11_CKA_KEY_GEN_MECHANISM */
862 		if (local)
863 			mechanism_id = mecha;
864 		else
865 			mechanism_id = PKCS11_CK_UNAVAILABLE_INFORMATION;
866 
867 		rc = add_attribute(&attrs, PKCS11_CKA_KEY_GEN_MECHANISM,
868 				   &mechanism_id, sizeof(mechanism_id));
869 		if (rc)
870 			goto out;
871 		break;
872 
873 	default:
874 		break;
875 	}
876 
877 	*out = attrs;
878 
879 #ifdef DEBUG
880 	trace_attributes("object", attrs);
881 #endif
882 
883 out:
884 	TEE_Free(temp);
885 	if (rc)
886 		TEE_Free(attrs);
887 
888 	return rc;
889 }
890 
891 static enum pkcs11_rc check_attrs_misc_integrity(struct obj_attrs *head)
892 {
893 	if (get_bool(head, PKCS11_CKA_NEVER_EXTRACTABLE) &&
894 	    get_bool(head, PKCS11_CKA_EXTRACTABLE)) {
895 		DMSG("Never/Extractable attributes mismatch %d/%d",
896 		     get_bool(head, PKCS11_CKA_NEVER_EXTRACTABLE),
897 		     get_bool(head, PKCS11_CKA_EXTRACTABLE));
898 
899 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
900 	}
901 
902 	if (get_bool(head, PKCS11_CKA_ALWAYS_SENSITIVE) &&
903 	    !get_bool(head, PKCS11_CKA_SENSITIVE)) {
904 		DMSG("Sensitive/always attributes mismatch %d/%d",
905 		     get_bool(head, PKCS11_CKA_SENSITIVE),
906 		     get_bool(head, PKCS11_CKA_ALWAYS_SENSITIVE));
907 
908 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
909 	}
910 
911 	return PKCS11_CKR_OK;
912 }
913 
914 bool object_is_private(struct obj_attrs *head)
915 {
916 	if (get_class(head) == PKCS11_CKO_PRIVATE_KEY)
917 		return true;
918 
919 	if (get_bool(head, PKCS11_CKA_PRIVATE))
920 		return true;
921 
922 	return false;
923 }
924 
925 bool object_is_token(struct obj_attrs *head)
926 {
927 	return get_bool(head, PKCS11_CKA_TOKEN);
928 }
929 
930 bool object_is_modifiable(struct obj_attrs *head)
931 {
932 	return get_bool(head, PKCS11_CKA_MODIFIABLE);
933 }
934 
935 bool object_is_copyable(struct obj_attrs *head)
936 {
937 	return get_bool(head, PKCS11_CKA_COPYABLE);
938 }
939 
940 /*
941  * Check access to object against authentication to token
942  */
943 enum pkcs11_rc check_access_attrs_against_token(struct pkcs11_session *session,
944 						struct obj_attrs *head)
945 {
946 	bool private = true;
947 
948 	switch (get_class(head)) {
949 	case PKCS11_CKO_SECRET_KEY:
950 	case PKCS11_CKO_PUBLIC_KEY:
951 	case PKCS11_CKO_DATA:
952 		private = get_bool(head, PKCS11_CKA_PRIVATE);
953 		break;
954 	case PKCS11_CKO_PRIVATE_KEY:
955 		break;
956 	default:
957 		return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
958 	}
959 
960 	if (private && (pkcs11_session_is_public(session) ||
961 			pkcs11_session_is_so(session))) {
962 		DMSG("Private object access from a public or SO session");
963 
964 		return PKCS11_CKR_USER_NOT_LOGGED_IN;
965 	}
966 
967 	return PKCS11_CKR_OK;
968 }
969 
970 /*
971  * Check the attributes of a to-be-created object matches the token state
972  */
973 enum pkcs11_rc check_created_attrs_against_token(struct pkcs11_session *session,
974 						 struct obj_attrs *head)
975 {
976 	enum pkcs11_rc rc = PKCS11_CKR_OK;
977 
978 	rc = check_attrs_misc_integrity(head);
979 	if (rc)
980 		return rc;
981 
982 	if (get_bool(head, PKCS11_CKA_TRUSTED) &&
983 	    !pkcs11_session_is_so(session)) {
984 		DMSG("Can't create trusted object");
985 
986 		return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
987 	}
988 
989 	if (get_bool(head, PKCS11_CKA_TOKEN) &&
990 	    !pkcs11_session_is_read_write(session)) {
991 		DMSG("Can't create persistent object");
992 
993 		return PKCS11_CKR_SESSION_READ_ONLY;
994 	}
995 
996 	/*
997 	 * TODO: START_DATE and END_DATE: complies with current time?
998 	 */
999 	return PKCS11_CKR_OK;
1000 }
1001 
1002 #define DMSG_BAD_BBOOL(attr, proc, head)				\
1003 	do {								\
1004 		uint32_t __maybe_unused _attr = (attr);			\
1005 		uint8_t __maybe_unused _bvalue = 0;			\
1006 		enum pkcs11_rc __maybe_unused _rc = PKCS11_CKR_OK;	\
1007 									\
1008 		_rc = get_attribute((head), _attr, &_bvalue, NULL);	\
1009 		DMSG("%s issue for %s: %sfound, value %"PRIu8,		\
1010 		     id2str_attr(_attr), id2str_proc((proc)),		\
1011 		     _rc ? "not " : "", _bvalue);			\
1012 	} while (0)
1013 
1014 static bool __maybe_unused check_attr_bval(uint32_t proc_id __maybe_unused,
1015 					   struct obj_attrs *head,
1016 					   uint32_t attribute, bool val)
1017 {
1018 	uint8_t bbool = 0;
1019 	uint32_t sz = sizeof(bbool);
1020 
1021 	if (!get_attribute(head, attribute, &bbool, &sz) && !!bbool == val)
1022 		return true;
1023 
1024 	DMSG_BAD_BBOOL(attribute, proc_id, head);
1025 	return false;
1026 }
1027 
1028 /*
1029  * Check the attributes of a new secret match the processing/mechanism
1030  * used to create it.
1031  *
1032  * @proc_id - PKCS11_CKM_xxx
1033  * @head - head of the attributes of the to-be-created object.
1034  */
1035 enum pkcs11_rc check_created_attrs_against_processing(uint32_t proc_id,
1036 						      struct obj_attrs *head)
1037 {
1038 	/*
1039 	 * Processings that do not create secrets are not expected to call
1040 	 * this function which would panic.
1041 	 */
1042 	switch (proc_id) {
1043 	case PKCS11_PROCESSING_IMPORT:
1044 		assert(check_attr_bval(proc_id, head, PKCS11_CKA_LOCAL, false));
1045 		break;
1046 	case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
1047 	case PKCS11_CKM_AES_KEY_GEN:
1048 		assert(check_attr_bval(proc_id, head, PKCS11_CKA_LOCAL, true));
1049 		break;
1050 	default:
1051 		TEE_Panic(proc_id);
1052 		break;
1053 	}
1054 
1055 	switch (proc_id) {
1056 	case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
1057 		assert(get_key_type(head) == PKCS11_CKK_GENERIC_SECRET);
1058 		break;
1059 	case PKCS11_CKM_AES_KEY_GEN:
1060 		assert(get_key_type(head) == PKCS11_CKK_AES);
1061 		break;
1062 	case PKCS11_PROCESSING_IMPORT:
1063 	default:
1064 		break;
1065 	}
1066 
1067 	return PKCS11_CKR_OK;
1068 }
1069 
1070 static void get_key_min_max_sizes(enum pkcs11_key_type key_type,
1071 				  uint32_t *min_key_size,
1072 				  uint32_t *max_key_size)
1073 {
1074 	enum pkcs11_mechanism_id mechanism = PKCS11_CKM_UNDEFINED_ID;
1075 
1076 	switch (key_type) {
1077 	case PKCS11_CKK_GENERIC_SECRET:
1078 		mechanism = PKCS11_CKM_GENERIC_SECRET_KEY_GEN;
1079 		break;
1080 	case PKCS11_CKK_AES:
1081 		mechanism = PKCS11_CKM_AES_KEY_GEN;
1082 		break;
1083 	case PKCS11_CKK_MD5_HMAC:
1084 		mechanism = PKCS11_CKM_MD5_HMAC;
1085 		break;
1086 	case PKCS11_CKK_SHA_1_HMAC:
1087 		mechanism = PKCS11_CKM_SHA_1_HMAC;
1088 		break;
1089 	case PKCS11_CKK_SHA224_HMAC:
1090 		mechanism = PKCS11_CKM_SHA224_HMAC;
1091 		break;
1092 	case PKCS11_CKK_SHA256_HMAC:
1093 		mechanism = PKCS11_CKM_SHA256_HMAC;
1094 		break;
1095 	case PKCS11_CKK_SHA384_HMAC:
1096 		mechanism = PKCS11_CKM_SHA384_HMAC;
1097 		break;
1098 	case PKCS11_CKK_SHA512_HMAC:
1099 		mechanism = PKCS11_CKM_SHA512_HMAC;
1100 		break;
1101 	default:
1102 		TEE_Panic(key_type);
1103 		break;
1104 	}
1105 
1106 	mechanism_supported_key_sizes(mechanism, min_key_size,
1107 				      max_key_size);
1108 }
1109 
1110 enum pkcs11_rc check_created_attrs(struct obj_attrs *key1,
1111 				   struct obj_attrs *key2)
1112 {
1113 	enum pkcs11_rc rc = PKCS11_CKR_OK;
1114 	struct obj_attrs *secret = NULL;
1115 	uint32_t max_key_size = 0;
1116 	uint32_t min_key_size = 0;
1117 	uint32_t key_length = 0;
1118 
1119 	switch (get_class(key1)) {
1120 	case PKCS11_CKO_SECRET_KEY:
1121 		secret = key1;
1122 		break;
1123 	default:
1124 		return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID;
1125 	}
1126 
1127 	if (key2)
1128 		return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID;
1129 
1130 	if (secret) {
1131 		switch (get_key_type(secret)) {
1132 		case PKCS11_CKK_AES:
1133 		case PKCS11_CKK_GENERIC_SECRET:
1134 		case PKCS11_CKK_MD5_HMAC:
1135 		case PKCS11_CKK_SHA_1_HMAC:
1136 		case PKCS11_CKK_SHA224_HMAC:
1137 		case PKCS11_CKK_SHA256_HMAC:
1138 		case PKCS11_CKK_SHA384_HMAC:
1139 		case PKCS11_CKK_SHA512_HMAC:
1140 			break;
1141 		default:
1142 			return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1143 		}
1144 
1145 		/* Get key size */
1146 		rc = get_u32_attribute(secret, PKCS11_CKA_VALUE_LEN,
1147 				       &key_length);
1148 		if (rc)
1149 			return PKCS11_CKR_TEMPLATE_INCOMPLETE;
1150 	}
1151 
1152 	get_key_min_max_sizes(get_key_type(key1), &min_key_size, &max_key_size);
1153 	if (key_length < min_key_size || key_length > max_key_size) {
1154 		EMSG("Length %"PRIu32" vs range [%"PRIu32" %"PRIu32"]",
1155 		     key_length, min_key_size, max_key_size);
1156 
1157 		return PKCS11_CKR_KEY_SIZE_RANGE;
1158 	}
1159 
1160 	return PKCS11_CKR_OK;
1161 }
1162 
1163 /* Check processing ID against attribute ALLOWED_MECHANISMS if any */
1164 static bool parent_key_complies_allowed_processings(uint32_t proc_id,
1165 						    struct obj_attrs *head)
1166 {
1167 	char *attr = NULL;
1168 	uint32_t size = 0;
1169 	uint32_t proc = 0;
1170 	size_t count = 0;
1171 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
1172 
1173 	rc = get_attribute_ptr(head, PKCS11_CKA_ALLOWED_MECHANISMS,
1174 			       (void *)&attr, &size);
1175 	if (rc == PKCS11_RV_NOT_FOUND)
1176 		return true;
1177 	if (rc) {
1178 		EMSG("unexpected attributes state");
1179 		TEE_Panic(TEE_ERROR_BAD_STATE);
1180 	}
1181 
1182 	for (count = size / sizeof(uint32_t); count; count--) {
1183 		TEE_MemMove(&proc, attr, sizeof(uint32_t));
1184 		attr += sizeof(uint32_t);
1185 
1186 		if (proc == proc_id)
1187 			return true;
1188 	}
1189 
1190 	DMSG("can't find %s in allowed list", id2str_proc(proc_id));
1191 	return false;
1192 }
1193 
1194 static enum pkcs11_attr_id func_to_attr(enum processing_func func)
1195 {
1196 	switch (func) {
1197 	case PKCS11_FUNCTION_ENCRYPT:
1198 		return PKCS11_CKA_ENCRYPT;
1199 	case PKCS11_FUNCTION_DECRYPT:
1200 		return PKCS11_CKA_DECRYPT;
1201 	case PKCS11_FUNCTION_SIGN:
1202 		return PKCS11_CKA_SIGN;
1203 	case PKCS11_FUNCTION_VERIFY:
1204 		return PKCS11_CKA_VERIFY;
1205 	case PKCS11_FUNCTION_WRAP:
1206 		return PKCS11_CKA_WRAP;
1207 	case PKCS11_FUNCTION_UNWRAP:
1208 		return PKCS11_CKA_UNWRAP;
1209 	case PKCS11_FUNCTION_DERIVE:
1210 		return PKCS11_CKA_DERIVE;
1211 	default:
1212 		return PKCS11_CKA_UNDEFINED_ID;
1213 	}
1214 }
1215 
1216 enum pkcs11_rc
1217 check_parent_attrs_against_processing(enum pkcs11_mechanism_id proc_id,
1218 				      enum processing_func function,
1219 				      struct obj_attrs *head)
1220 {
1221 	enum pkcs11_class_id key_class = get_class(head);
1222 	enum pkcs11_key_type key_type = get_key_type(head);
1223 	enum pkcs11_attr_id attr = func_to_attr(function);
1224 
1225 	if (!get_bool(head, attr)) {
1226 		DMSG("%s not permitted", id2str_attr(attr));
1227 		return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1228 	}
1229 
1230 	/* Check processing complies with parent key family */
1231 	switch (proc_id) {
1232 	case PKCS11_CKM_AES_ECB:
1233 	case PKCS11_CKM_AES_CBC:
1234 	case PKCS11_CKM_AES_CBC_PAD:
1235 	case PKCS11_CKM_AES_CTS:
1236 	case PKCS11_CKM_AES_CTR:
1237 		if (key_class == PKCS11_CKO_SECRET_KEY &&
1238 		    key_type == PKCS11_CKK_AES)
1239 			break;
1240 
1241 		DMSG("%s invalid key %s/%s", id2str_proc(proc_id),
1242 		     id2str_class(key_class), id2str_key_type(key_type));
1243 
1244 		return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1245 
1246 	case PKCS11_CKM_MD5_HMAC:
1247 	case PKCS11_CKM_SHA_1_HMAC:
1248 	case PKCS11_CKM_SHA224_HMAC:
1249 	case PKCS11_CKM_SHA256_HMAC:
1250 	case PKCS11_CKM_SHA384_HMAC:
1251 	case PKCS11_CKM_SHA512_HMAC:
1252 		if (key_class != PKCS11_CKO_SECRET_KEY)
1253 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1254 
1255 		if (key_type == PKCS11_CKK_GENERIC_SECRET)
1256 			break;
1257 
1258 		switch (proc_id) {
1259 		case PKCS11_CKM_MD5_HMAC:
1260 			if (key_type == PKCS11_CKK_MD5_HMAC)
1261 				break;
1262 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1263 		case PKCS11_CKM_SHA_1_HMAC:
1264 			if (key_type == PKCS11_CKK_SHA_1_HMAC)
1265 				break;
1266 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1267 		case PKCS11_CKM_SHA224_HMAC:
1268 			if (key_type == PKCS11_CKK_SHA224_HMAC)
1269 				break;
1270 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1271 		case PKCS11_CKM_SHA256_HMAC:
1272 			if (key_type == PKCS11_CKK_SHA256_HMAC)
1273 				break;
1274 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1275 		case PKCS11_CKM_SHA384_HMAC:
1276 			if (key_type == PKCS11_CKK_SHA384_HMAC)
1277 				break;
1278 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1279 		case PKCS11_CKM_SHA512_HMAC:
1280 			if (key_type == PKCS11_CKK_SHA512_HMAC)
1281 				break;
1282 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1283 		default:
1284 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1285 		}
1286 		break;
1287 
1288 	default:
1289 		DMSG("Invalid processing %#"PRIx32"/%s", proc_id,
1290 		     id2str_proc(proc_id));
1291 
1292 		return PKCS11_CKR_MECHANISM_INVALID;
1293 	}
1294 
1295 	if (!parent_key_complies_allowed_processings(proc_id, head)) {
1296 		DMSG("Allowed mechanism failed");
1297 		return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1298 	}
1299 
1300 	return PKCS11_CKR_OK;
1301 }
1302 
1303 bool attribute_is_exportable(struct pkcs11_attribute_head *req_attr,
1304 			     struct pkcs11_object *obj)
1305 {
1306 	uint8_t boolval = 0;
1307 	uint32_t boolsize = 0;
1308 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
1309 	enum pkcs11_class_id key_class = get_class(obj->attributes);
1310 
1311 	if (key_class != PKCS11_CKO_SECRET_KEY &&
1312 	    key_class != PKCS11_CKO_PRIVATE_KEY)
1313 		return true;
1314 
1315 	switch (req_attr->id) {
1316 	case PKCS11_CKA_PRIVATE_EXPONENT:
1317 	case PKCS11_CKA_PRIME_1:
1318 	case PKCS11_CKA_PRIME_2:
1319 	case PKCS11_CKA_EXPONENT_1:
1320 	case PKCS11_CKA_EXPONENT_2:
1321 	case PKCS11_CKA_COEFFICIENT:
1322 	case PKCS11_CKA_VALUE:
1323 		boolsize = sizeof(boolval);
1324 		rc = get_attribute(obj->attributes, PKCS11_CKA_EXTRACTABLE,
1325 				   &boolval, &boolsize);
1326 		if (rc || boolval == PKCS11_FALSE)
1327 			return false;
1328 
1329 		boolsize = sizeof(boolval);
1330 		rc = get_attribute(obj->attributes, PKCS11_CKA_SENSITIVE,
1331 				   &boolval, &boolsize);
1332 		if (rc || boolval == PKCS11_TRUE)
1333 			return false;
1334 		break;
1335 	default:
1336 		break;
1337 	}
1338 
1339 	return true;
1340 }
1341 
1342 static bool attr_is_modifiable_any_key(struct pkcs11_attribute_head *attr)
1343 {
1344 	switch (attr->id) {
1345 	case PKCS11_CKA_ID:
1346 	case PKCS11_CKA_START_DATE:
1347 	case PKCS11_CKA_END_DATE:
1348 	case PKCS11_CKA_DERIVE:
1349 		return true;
1350 	default:
1351 		return false;
1352 	}
1353 }
1354 
1355 static bool attr_is_modifiable_secret_key(struct pkcs11_attribute_head *attr,
1356 					  struct pkcs11_session *session,
1357 					  struct pkcs11_object *obj)
1358 {
1359 	switch (attr->id) {
1360 	case PKCS11_CKA_ENCRYPT:
1361 	case PKCS11_CKA_DECRYPT:
1362 	case PKCS11_CKA_SIGN:
1363 	case PKCS11_CKA_VERIFY:
1364 	case PKCS11_CKA_WRAP:
1365 	case PKCS11_CKA_UNWRAP:
1366 		return true;
1367 	/* Can't be modified once set to CK_FALSE - 12 in Table 10 */
1368 	case PKCS11_CKA_EXTRACTABLE:
1369 		return get_bool(obj->attributes, attr->id);
1370 	/* Can't be modified once set to CK_TRUE - 11 in Table 10 */
1371 	case PKCS11_CKA_SENSITIVE:
1372 	case PKCS11_CKA_WRAP_WITH_TRUSTED:
1373 		return !get_bool(obj->attributes, attr->id);
1374 	/* Change in CKA_TRUSTED can only be done by SO */
1375 	case PKCS11_CKA_TRUSTED:
1376 		return pkcs11_session_is_so(session);
1377 	case PKCS11_CKA_NEVER_EXTRACTABLE:
1378 	case PKCS11_CKA_ALWAYS_SENSITIVE:
1379 		return false;
1380 	default:
1381 		return false;
1382 	}
1383 }
1384 
1385 static bool attr_is_modifiable_public_key(struct pkcs11_attribute_head *attr,
1386 					  struct pkcs11_session *session,
1387 					  struct pkcs11_object *obj __unused)
1388 {
1389 	switch (attr->id) {
1390 	case PKCS11_CKA_SUBJECT:
1391 	case PKCS11_CKA_ENCRYPT:
1392 	case PKCS11_CKA_VERIFY:
1393 	case PKCS11_CKA_VERIFY_RECOVER:
1394 	case PKCS11_CKA_WRAP:
1395 		return true;
1396 	case PKCS11_CKA_TRUSTED:
1397 		/* Change in CKA_TRUSTED can only be done by SO */
1398 		return pkcs11_session_is_so(session);
1399 	default:
1400 		return false;
1401 	}
1402 }
1403 
1404 static bool attr_is_modifiable_private_key(struct pkcs11_attribute_head *attr,
1405 					   struct pkcs11_session *sess __unused,
1406 					   struct pkcs11_object *obj)
1407 {
1408 	switch (attr->id) {
1409 	case PKCS11_CKA_SUBJECT:
1410 	case PKCS11_CKA_DECRYPT:
1411 	case PKCS11_CKA_SIGN:
1412 	case PKCS11_CKA_SIGN_RECOVER:
1413 	case PKCS11_CKA_UNWRAP:
1414 	/*
1415 	 * TBD: Revisit if we don't support PKCS11_CKA_PUBLIC_KEY_INFO
1416 	 * Specification mentions that if this attribute is
1417 	 * supplied as part of a template for C_CreateObject, C_CopyObject or
1418 	 * C_SetAttributeValue for a private key, the token MUST verify
1419 	 * correspondence between the private key data and the public key data
1420 	 * as supplied in CKA_PUBLIC_KEY_INFO. This needs to be
1421 	 * taken care of when this object type will be implemented
1422 	 */
1423 	case PKCS11_CKA_PUBLIC_KEY_INFO:
1424 		return true;
1425 	/* Can't be modified once set to CK_FALSE - 12 in Table 10 */
1426 	case PKCS11_CKA_EXTRACTABLE:
1427 		return get_bool(obj->attributes, attr->id);
1428 	/* Can't be modified once set to CK_TRUE - 11 in Table 10 */
1429 	case PKCS11_CKA_SENSITIVE:
1430 	case PKCS11_CKA_WRAP_WITH_TRUSTED:
1431 		return !get_bool(obj->attributes, attr->id);
1432 	case PKCS11_CKA_NEVER_EXTRACTABLE:
1433 	case PKCS11_CKA_ALWAYS_SENSITIVE:
1434 		return false;
1435 	default:
1436 		return false;
1437 	}
1438 }
1439 
1440 static bool attribute_is_modifiable(struct pkcs11_session *session,
1441 				    struct pkcs11_attribute_head *req_attr,
1442 				    struct pkcs11_object *obj,
1443 				    enum pkcs11_class_id class,
1444 				    enum processing_func function)
1445 {
1446 	/* Check modifiable attributes common to any object */
1447 	switch (req_attr->id) {
1448 	case PKCS11_CKA_LABEL:
1449 		return true;
1450 	case PKCS11_CKA_TOKEN:
1451 	case PKCS11_CKA_MODIFIABLE:
1452 	case PKCS11_CKA_DESTROYABLE:
1453 	case PKCS11_CKA_PRIVATE:
1454 		return function == PKCS11_FUNCTION_COPY;
1455 	case PKCS11_CKA_COPYABLE:
1456 		/*
1457 		 * Specification mentions that if the attribute value is false
1458 		 * it can't be set to true. Reading this we assume that it
1459 		 * should be possible to modify this attribute even though this
1460 		 * is not marked as modifiable in Table 10 if done in right
1461 		 * direction i.e from TRUE -> FALSE.
1462 		 */
1463 		return get_bool(obj->attributes, req_attr->id);
1464 	default:
1465 		break;
1466 	}
1467 
1468 	/* Attribute checking based on class type */
1469 	switch (class) {
1470 	case PKCS11_CKO_SECRET_KEY:
1471 	case PKCS11_CKO_PUBLIC_KEY:
1472 	case PKCS11_CKO_PRIVATE_KEY:
1473 		if (attr_is_modifiable_any_key(req_attr))
1474 			return true;
1475 		if (class == PKCS11_CKO_SECRET_KEY &&
1476 		    attr_is_modifiable_secret_key(req_attr, session, obj))
1477 			return true;
1478 		if (class == PKCS11_CKO_PUBLIC_KEY &&
1479 		    attr_is_modifiable_public_key(req_attr, session, obj))
1480 			return true;
1481 		if (class == PKCS11_CKO_PRIVATE_KEY &&
1482 		    attr_is_modifiable_private_key(req_attr, session, obj))
1483 			return true;
1484 		break;
1485 	case PKCS11_CKO_DATA:
1486 		/* None of the data object attributes are modifiable */
1487 		return false;
1488 	default:
1489 		break;
1490 	}
1491 
1492 	return false;
1493 }
1494 
1495 enum pkcs11_rc check_attrs_against_modification(struct pkcs11_session *session,
1496 						struct obj_attrs *head,
1497 						struct pkcs11_object *obj,
1498 						enum processing_func function)
1499 {
1500 	enum pkcs11_class_id class = PKCS11_CKO_UNDEFINED_ID;
1501 	char *cur = NULL;
1502 	char *end = NULL;
1503 	size_t len = 0;
1504 
1505 	class = get_class(obj->attributes);
1506 
1507 	cur = (char *)head + sizeof(struct obj_attrs);
1508 	end = cur + head->attrs_size;
1509 
1510 	for (; cur < end; cur += len) {
1511 		/* Structure aligned copy of the pkcs11_ref in the object */
1512 		struct pkcs11_attribute_head cli_ref = { };
1513 
1514 		TEE_MemMove(&cli_ref, cur, sizeof(cli_ref));
1515 		len = sizeof(cli_ref) + cli_ref.size;
1516 
1517 		/*
1518 		 * Check 1 - Check if attribute belongs to the object
1519 		 * The obj->attributes has all the attributes in
1520 		 * it which are allowed for an object.
1521 		 */
1522 		if (get_attribute_ptr(obj->attributes, cli_ref.id, NULL,
1523 				      NULL) == PKCS11_RV_NOT_FOUND)
1524 			return PKCS11_CKR_ATTRIBUTE_TYPE_INVALID;
1525 
1526 		/* Check 2 - Is attribute modifiable */
1527 		if (!attribute_is_modifiable(session, &cli_ref, obj, class,
1528 					     function))
1529 			return PKCS11_CKR_ATTRIBUTE_READ_ONLY;
1530 
1531 		/*
1532 		 * Checks for modification in PKCS11_CKA_TOKEN and
1533 		 * PKCS11_CKA_PRIVATE are required for PKCS11_FUNCTION_COPY
1534 		 * only, so skip them for PKCS11_FUNCTION_MODIFY.
1535 		 */
1536 		if (function == PKCS11_FUNCTION_MODIFY)
1537 			continue;
1538 
1539 		/*
1540 		 * An attempt to copy an object to a token will fail for
1541 		 * RO session
1542 		 */
1543 		if (cli_ref.id == PKCS11_CKA_TOKEN &&
1544 		    get_bool(head, PKCS11_CKA_TOKEN)) {
1545 			if (!pkcs11_session_is_read_write(session)) {
1546 				DMSG("Can't copy to token in a RO session");
1547 				return PKCS11_CKR_SESSION_READ_ONLY;
1548 			}
1549 		}
1550 
1551 		if (cli_ref.id == PKCS11_CKA_PRIVATE) {
1552 			bool parent_priv =
1553 				get_bool(obj->attributes, cli_ref.id);
1554 			bool obj_priv = get_bool(head, cli_ref.id);
1555 
1556 			/*
1557 			 * If PKCS11_CKA_PRIVATE is being set to TRUE from
1558 			 * FALSE, user has to be logged in
1559 			 */
1560 			if (!parent_priv && obj_priv) {
1561 				if ((pkcs11_session_is_public(session) ||
1562 				     pkcs11_session_is_so(session)))
1563 					return PKCS11_CKR_USER_NOT_LOGGED_IN;
1564 			}
1565 
1566 			/*
1567 			 * Restriction added - Even for Copy, do not allow
1568 			 * modification of CKA_PRIVATE from TRUE to FALSE
1569 			 */
1570 			if (parent_priv && !obj_priv)
1571 				return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1572 		}
1573 	}
1574 
1575 	return PKCS11_CKR_OK;
1576 }
1577