xref: /optee_os/ta/pkcs11/src/pkcs11_attributes.c (revision 8fab4371e333936b8d965c1fa6f853774c2c0252)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2017-2020, Linaro Limited
4  */
5 
6 #include <assert.h>
7 #include <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 certificate object (+pkcs11_any_object_xxx) */
345 static const uint32_t pkcs11_certificate_mandated[] = {
346 	PKCS11_CKA_CERTIFICATE_TYPE,
347 };
348 
349 static const uint32_t pkcs11_certificate_boolprops[] = {
350 	PKCS11_CKA_TRUSTED,
351 };
352 
353 static const uint32_t pkcs11_certificate_optional[] = {
354 	PKCS11_CKA_CERTIFICATE_CATEGORY, PKCS11_CKA_CHECK_VALUE,
355 	PKCS11_CKA_START_DATE, PKCS11_CKA_END_DATE, PKCS11_CKA_PUBLIC_KEY_INFO,
356 };
357 
358 /*
359  * PKCS#11 specification for X.509 certificate object (+pkcs11_certificate_xxx)
360  */
361 static const uint32_t pkcs11_x509_certificate_mandated[] = {
362 	PKCS11_CKA_SUBJECT,
363 };
364 
365 static const uint32_t pkcs11_x509_certificate_optional[] = {
366 	PKCS11_CKA_ID, PKCS11_CKA_ISSUER, PKCS11_CKA_SERIAL_NUMBER,
367 	PKCS11_CKA_VALUE, PKCS11_CKA_URL,
368 	PKCS11_CKA_HASH_OF_SUBJECT_PUBLIC_KEY,
369 	PKCS11_CKA_HASH_OF_ISSUER_PUBLIC_KEY,
370 	PKCS11_CKA_JAVA_MIDP_SECURITY_DOMAIN, PKCS11_CKA_NAME_HASH_ALGORITHM,
371 };
372 
373 /* PKCS#11 specification for any key object (+any_object_xxx) */
374 static const uint32_t any_key_boolprops[] = {
375 	PKCS11_CKA_DERIVE,
376 };
377 
378 static const uint32_t any_key_opt_or_null[] = {
379 	PKCS11_CKA_ID,
380 	PKCS11_CKA_START_DATE, PKCS11_CKA_END_DATE,
381 };
382 
383 static const uint32_t any_key_optional[] = {
384 	PKCS11_CKA_ALLOWED_MECHANISMS,
385 };
386 
387 /* PKCS#11 specification for any symmetric key (+any_key_xxx) */
388 static const uint32_t symm_key_boolprops[] = {
389 	PKCS11_CKA_ENCRYPT, PKCS11_CKA_DECRYPT,
390 	PKCS11_CKA_SIGN, PKCS11_CKA_VERIFY,
391 	PKCS11_CKA_WRAP, PKCS11_CKA_UNWRAP,
392 	PKCS11_CKA_SENSITIVE, PKCS11_CKA_EXTRACTABLE,
393 	PKCS11_CKA_WRAP_WITH_TRUSTED, PKCS11_CKA_TRUSTED,
394 };
395 
396 static const uint32_t symm_key_opt_or_null[] = {
397 	PKCS11_CKA_WRAP_TEMPLATE, PKCS11_CKA_UNWRAP_TEMPLATE,
398 	PKCS11_CKA_DERIVE_TEMPLATE, PKCS11_CKA_VALUE,
399 };
400 
401 static const uint32_t symm_key_optional[] = {
402 	PKCS11_CKA_VALUE_LEN,
403 };
404 
405 /* PKCS#11 specification for any asymmetric public key (+any_key_xxx) */
406 static const uint32_t public_key_boolprops[] = {
407 	PKCS11_CKA_ENCRYPT, PKCS11_CKA_VERIFY, PKCS11_CKA_VERIFY_RECOVER,
408 	PKCS11_CKA_WRAP,
409 	PKCS11_CKA_TRUSTED,
410 };
411 
412 static const uint32_t public_key_mandated[] = {
413 };
414 
415 static const uint32_t public_key_opt_or_null[] = {
416 	PKCS11_CKA_SUBJECT, PKCS11_CKA_WRAP_TEMPLATE,
417 	PKCS11_CKA_PUBLIC_KEY_INFO,
418 };
419 
420 /* PKCS#11 specification for any asymmetric private key (+any_key_xxx) */
421 static const uint32_t private_key_boolprops[] = {
422 	PKCS11_CKA_DECRYPT, PKCS11_CKA_SIGN, PKCS11_CKA_SIGN_RECOVER,
423 	PKCS11_CKA_UNWRAP,
424 	PKCS11_CKA_SENSITIVE, PKCS11_CKA_EXTRACTABLE,
425 	PKCS11_CKA_WRAP_WITH_TRUSTED, PKCS11_CKA_ALWAYS_AUTHENTICATE,
426 };
427 
428 static const uint32_t private_key_mandated[] = {
429 };
430 
431 static const uint32_t private_key_opt_or_null[] = {
432 	PKCS11_CKA_SUBJECT, PKCS11_CKA_UNWRAP_TEMPLATE,
433 	PKCS11_CKA_PUBLIC_KEY_INFO,
434 };
435 
436 /* PKCS#11 specification for any RSA key (+public/private_key_xxx) */
437 static const uint32_t rsa_pub_key_gen_mand[] = {
438 	PKCS11_CKA_MODULUS_BITS,
439 };
440 
441 static const uint32_t rsa_pub_key_create_mand[] = {
442 	PKCS11_CKA_MODULUS, PKCS11_CKA_PUBLIC_EXPONENT,
443 };
444 
445 static const uint32_t rsa_pub_key_gen_opt_or_null[] = {
446 	PKCS11_CKA_PUBLIC_EXPONENT,
447 };
448 
449 static const uint32_t rsa_priv_key_opt_or_null[] = {
450 	PKCS11_CKA_MODULUS, PKCS11_CKA_PUBLIC_EXPONENT,
451 	PKCS11_CKA_PRIVATE_EXPONENT,
452 	PKCS11_CKA_PRIME_1, PKCS11_CKA_PRIME_2,
453 	PKCS11_CKA_EXPONENT_1, PKCS11_CKA_EXPONENT_2, PKCS11_CKA_COEFFICIENT,
454 };
455 
456 /* PKCS#11 specification for any EC key (+public/private_key_xxx) */
457 static const uint32_t ec_public_key_mandated[] = {
458 	PKCS11_CKA_EC_PARAMS,
459 };
460 
461 static const uint32_t ec_public_key_opt_or_null[] = {
462 	PKCS11_CKA_EC_POINT,
463 };
464 
465 static const uint32_t ec_private_key_mandated[] = {
466 };
467 
468 static const uint32_t ec_private_key_opt_or_null[] = {
469 	PKCS11_CKA_EC_PARAMS,
470 	PKCS11_CKA_VALUE,
471 };
472 
473 static const uint32_t eddsa_private_key_opt_or_null[] = {
474 	PKCS11_CKA_EC_PARAMS,
475 	PKCS11_CKA_VALUE,
476 	PKCS11_CKA_EC_POINT,
477 };
478 
479 static enum pkcs11_rc create_storage_attributes(struct obj_attrs **out,
480 						struct obj_attrs *temp)
481 {
482 	enum pkcs11_class_id class = PKCS11_CKO_UNDEFINED_ID;
483 	enum pkcs11_rc rc = PKCS11_CKR_OK;
484 
485 	rc = init_attributes_head(out);
486 	if (rc)
487 		return rc;
488 
489 	/* Object class is mandatory */
490 	class = get_class(temp);
491 	if (class == PKCS11_CKO_UNDEFINED_ID) {
492 		EMSG("Class attribute not found");
493 
494 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
495 	}
496 	rc = add_attribute(out, PKCS11_CKA_CLASS, &class, sizeof(uint32_t));
497 	if (rc)
498 		return rc;
499 
500 	rc = set_mandatory_boolprops(out, temp, any_object_boolprops,
501 				     ARRAY_SIZE(any_object_boolprops));
502 	if (rc)
503 		return rc;
504 
505 	return set_attributes_opt_or_null(out, temp, any_object_opt_or_null,
506 					  ARRAY_SIZE(any_object_opt_or_null));
507 }
508 
509 static enum pkcs11_rc create_genkey_attributes(struct obj_attrs **out,
510 					       struct obj_attrs *temp)
511 {
512 	uint32_t type = PKCS11_CKO_UNDEFINED_ID;
513 	enum pkcs11_rc rc = PKCS11_CKR_OK;
514 
515 	rc = create_storage_attributes(out, temp);
516 	if (rc)
517 		return rc;
518 
519 	type = get_key_type(temp);
520 	if (type == PKCS11_CKK_UNDEFINED_ID) {
521 		EMSG("Key type attribute not found");
522 
523 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
524 	}
525 	rc = add_attribute(out, PKCS11_CKA_KEY_TYPE, &type, sizeof(uint32_t));
526 	if (rc)
527 		return rc;
528 
529 	rc = set_mandatory_boolprops(out, temp, any_key_boolprops,
530 				     ARRAY_SIZE(any_key_boolprops));
531 	if (rc)
532 		return rc;
533 
534 	rc = set_attributes_opt_or_null(out, temp, any_key_opt_or_null,
535 					ARRAY_SIZE(any_key_opt_or_null));
536 	if (rc)
537 		return rc;
538 
539 	return set_optional_attributes(out, temp, any_key_optional,
540 				       ARRAY_SIZE(any_key_optional));
541 
542 }
543 
544 static enum pkcs11_rc create_symm_key_attributes(struct obj_attrs **out,
545 						 struct obj_attrs *temp)
546 {
547 	enum pkcs11_rc rc = PKCS11_CKR_OK;
548 
549 	assert(get_class(temp) == PKCS11_CKO_SECRET_KEY);
550 
551 	rc = create_genkey_attributes(out, temp);
552 	if (rc)
553 		return rc;
554 
555 	assert(get_class(*out) == PKCS11_CKO_SECRET_KEY);
556 
557 	switch (get_key_type(*out)) {
558 	case PKCS11_CKK_GENERIC_SECRET:
559 	case PKCS11_CKK_AES:
560 	case PKCS11_CKK_MD5_HMAC:
561 	case PKCS11_CKK_SHA_1_HMAC:
562 	case PKCS11_CKK_SHA256_HMAC:
563 	case PKCS11_CKK_SHA384_HMAC:
564 	case PKCS11_CKK_SHA512_HMAC:
565 	case PKCS11_CKK_SHA224_HMAC:
566 		break;
567 	default:
568 		EMSG("Invalid key type %#"PRIx32"/%s",
569 		     get_key_type(*out), id2str_key_type(get_key_type(*out)));
570 
571 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
572 	}
573 
574 	rc = set_mandatory_boolprops(out, temp, symm_key_boolprops,
575 				     ARRAY_SIZE(symm_key_boolprops));
576 	if (rc)
577 		return rc;
578 
579 	rc = set_attributes_opt_or_null(out, temp, symm_key_opt_or_null,
580 					ARRAY_SIZE(symm_key_opt_or_null));
581 	if (rc)
582 		return rc;
583 
584 	return set_optional_attributes(out, temp, symm_key_optional,
585 				       ARRAY_SIZE(symm_key_optional));
586 }
587 
588 static enum pkcs11_rc create_data_attributes(struct obj_attrs **out,
589 					     struct obj_attrs *temp)
590 {
591 	enum pkcs11_rc rc = PKCS11_CKR_OK;
592 
593 	assert(get_class(temp) == PKCS11_CKO_DATA);
594 
595 	rc = create_storage_attributes(out, temp);
596 	if (rc)
597 		return rc;
598 
599 	assert(get_class(*out) == PKCS11_CKO_DATA);
600 
601 	return set_attributes_opt_or_null(out, temp, raw_data_opt_or_null,
602 					  ARRAY_SIZE(raw_data_opt_or_null));
603 }
604 
605 static enum pkcs11_rc create_certificate_attributes(struct obj_attrs **out,
606 						    struct obj_attrs *temp)
607 {
608 	uint32_t const *mandated = NULL;
609 	uint32_t const *optional = NULL;
610 	size_t mandated_count = 0;
611 	size_t optional_count = 0;
612 	void *attr_value = NULL;
613 	uint32_t attr_size = 0;
614 	uint32_t default_cert_category =
615 		PKCS11_CK_CERTIFICATE_CATEGORY_UNSPECIFIED;
616 	uint32_t default_name_hash_alg = PKCS11_CKM_SHA_1;
617 	uint32_t cert_category = 0;
618 	enum pkcs11_rc rc = PKCS11_CKR_OK;
619 
620 	assert(get_class(temp) == PKCS11_CKO_CERTIFICATE);
621 
622 	rc = create_storage_attributes(out, temp);
623 	if (rc)
624 		return rc;
625 
626 	assert(get_class(*out) == PKCS11_CKO_CERTIFICATE);
627 
628 	rc = set_mandatory_boolprops(out, temp, pkcs11_certificate_boolprops,
629 				     ARRAY_SIZE(pkcs11_certificate_boolprops));
630 	if (rc)
631 		return rc;
632 
633 	rc = set_mandatory_attributes(out, temp, pkcs11_certificate_mandated,
634 				      ARRAY_SIZE(pkcs11_certificate_mandated));
635 	if (rc)
636 		return rc;
637 
638 	rc = set_optional_attributes(out, temp, pkcs11_certificate_optional,
639 				     ARRAY_SIZE(pkcs11_certificate_optional));
640 	if (rc)
641 		return rc;
642 
643 	switch (get_certificate_type(*out)) {
644 	case PKCS11_CKC_X_509:
645 		mandated = pkcs11_x509_certificate_mandated;
646 		optional = pkcs11_x509_certificate_optional;
647 		mandated_count = ARRAY_SIZE(pkcs11_x509_certificate_mandated);
648 		optional_count = ARRAY_SIZE(pkcs11_x509_certificate_optional);
649 		break;
650 	default:
651 		EMSG("Invalid certificate type %#"PRIx32"/%s",
652 		     get_certificate_type(*out),
653 		     id2str_certificate_type(get_certificate_type(*out)));
654 
655 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
656 	}
657 
658 	rc = set_mandatory_attributes(out, temp, mandated, mandated_count);
659 	if (rc)
660 		return rc;
661 
662 	rc = set_optional_attributes(out, temp, optional, optional_count);
663 	if (rc)
664 		return rc;
665 
666 	attr_size = 0;
667 	rc = get_attribute_ptr(*out, PKCS11_CKA_CERTIFICATE_CATEGORY,
668 			       &attr_value, &attr_size);
669 	if (rc == PKCS11_CKR_OK && attr_size == sizeof(cert_category)) {
670 		/* Sanitize certificate category */
671 		TEE_MemMove(&cert_category, attr_value, sizeof(cert_category));
672 
673 		switch (cert_category) {
674 		case PKCS11_CK_CERTIFICATE_CATEGORY_UNSPECIFIED:
675 		case PKCS11_CK_CERTIFICATE_CATEGORY_TOKEN_USER:
676 		case PKCS11_CK_CERTIFICATE_CATEGORY_AUTHORITY:
677 		case PKCS11_CK_CERTIFICATE_CATEGORY_OTHER_ENTITY:
678 			break;
679 		default:
680 			EMSG("Invalid certificate category %#"PRIx32,
681 			     cert_category);
682 
683 			return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID;
684 		}
685 	} else if (rc == PKCS11_RV_NOT_FOUND) {
686 		/* Set default category when missing */
687 		rc = set_attribute(out, PKCS11_CKA_CERTIFICATE_CATEGORY,
688 				   &default_cert_category,
689 				   sizeof(default_cert_category));
690 		if (rc)
691 			return rc;
692 	} else {
693 		/* All other cases are errors */
694 		EMSG("Invalid certificate category");
695 
696 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
697 	}
698 
699 	attr_size = 0;
700 	rc = get_attribute_ptr(*out, PKCS11_CKA_NAME_HASH_ALGORITHM, NULL,
701 			       &attr_size);
702 	if (rc == PKCS11_CKR_OK && attr_size == sizeof(uint32_t)) {
703 		/* We accept any algorithm what caller wanted to specify */
704 	} else if (rc == PKCS11_RV_NOT_FOUND) {
705 		/* Set default hash algorithm when missing */
706 		rc = set_attribute(out, PKCS11_CKA_NAME_HASH_ALGORITHM,
707 				   &default_name_hash_alg,
708 				   sizeof(default_name_hash_alg));
709 		if (rc)
710 			return rc;
711 	} else {
712 		/* All other cases are errors */
713 		EMSG("Invalid name hash algorithm");
714 
715 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
716 	}
717 
718 	return rc;
719 }
720 
721 static enum pkcs11_rc create_pub_key_attributes(struct obj_attrs **out,
722 						struct obj_attrs *temp,
723 						enum processing_func function)
724 {
725 	uint32_t const *mandated = NULL;
726 	uint32_t const *oon = NULL;
727 	size_t mandated_count = 0;
728 	size_t oon_count = 0;
729 	enum pkcs11_rc rc = PKCS11_CKR_OK;
730 
731 	assert(get_class(temp) == PKCS11_CKO_PUBLIC_KEY);
732 
733 	rc = create_genkey_attributes(out, temp);
734 	if (rc)
735 		return rc;
736 
737 	assert(get_class(*out) == PKCS11_CKO_PUBLIC_KEY);
738 
739 	rc = set_mandatory_boolprops(out, temp, public_key_boolprops,
740 				     ARRAY_SIZE(public_key_boolprops));
741 	if (rc)
742 		return rc;
743 
744 	rc = set_mandatory_attributes(out, temp, public_key_mandated,
745 				      ARRAY_SIZE(public_key_mandated));
746 	if (rc)
747 		return rc;
748 
749 	rc = set_attributes_opt_or_null(out, temp,
750 					public_key_opt_or_null,
751 					ARRAY_SIZE(public_key_opt_or_null));
752 	if (rc)
753 		return rc;
754 
755 	switch (get_key_type(*out)) {
756 	case PKCS11_CKK_RSA:
757 		switch (function) {
758 		case PKCS11_FUNCTION_GENERATE_PAIR:
759 			mandated = rsa_pub_key_gen_mand;
760 			oon = rsa_pub_key_gen_opt_or_null;
761 			mandated_count = ARRAY_SIZE(rsa_pub_key_gen_mand);
762 			oon_count = ARRAY_SIZE(rsa_pub_key_gen_opt_or_null);
763 			break;
764 		case PKCS11_FUNCTION_IMPORT:
765 			mandated = rsa_pub_key_create_mand;
766 			mandated_count = ARRAY_SIZE(rsa_pub_key_create_mand);
767 			break;
768 		default:
769 			EMSG("Unsupported function %#"PRIx32"/%s", function,
770 			     id2str_function(function));
771 
772 			return PKCS11_CKR_TEMPLATE_INCONSISTENT;
773 		}
774 		break;
775 	case PKCS11_CKK_EC:
776 	case PKCS11_CKK_EC_EDWARDS:
777 		mandated = ec_public_key_mandated;
778 		oon = ec_public_key_opt_or_null;
779 		mandated_count = ARRAY_SIZE(ec_public_key_mandated);
780 		oon_count = ARRAY_SIZE(ec_public_key_opt_or_null);
781 		break;
782 	default:
783 		EMSG("Invalid key type %#"PRIx32"/%s",
784 		     get_key_type(*out), id2str_key_type(get_key_type(*out)));
785 
786 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
787 	}
788 
789 	rc = set_mandatory_attributes(out, temp, mandated, mandated_count);
790 	if (rc)
791 		return rc;
792 
793 	return set_attributes_opt_or_null(out, temp, oon, oon_count);
794 }
795 
796 static enum pkcs11_rc create_priv_key_attributes(struct obj_attrs **out,
797 						 struct obj_attrs *temp)
798 {
799 	uint32_t const *mandated = NULL;
800 	uint32_t const *oon = NULL;
801 	size_t mandated_count = 0;
802 	size_t oon_count = 0;
803 	enum pkcs11_rc rc = PKCS11_CKR_OK;
804 
805 	assert(get_class(temp) == PKCS11_CKO_PRIVATE_KEY);
806 
807 	rc = create_genkey_attributes(out, temp);
808 	if (rc)
809 		return rc;
810 
811 	assert(get_class(*out) == PKCS11_CKO_PRIVATE_KEY);
812 
813 	rc = set_mandatory_boolprops(out, temp, private_key_boolprops,
814 				     ARRAY_SIZE(private_key_boolprops));
815 	if (rc)
816 		return rc;
817 
818 	rc = set_mandatory_attributes(out, temp, private_key_mandated,
819 				      ARRAY_SIZE(private_key_mandated));
820 	if (rc)
821 		return rc;
822 
823 	rc = set_attributes_opt_or_null(out, temp, private_key_opt_or_null,
824 					ARRAY_SIZE(private_key_opt_or_null));
825 	if (rc)
826 		return rc;
827 
828 	switch (get_key_type(*out)) {
829 	case PKCS11_CKK_RSA:
830 		oon = rsa_priv_key_opt_or_null;
831 		oon_count = ARRAY_SIZE(rsa_priv_key_opt_or_null);
832 		break;
833 	case PKCS11_CKK_EC:
834 		mandated = ec_private_key_mandated;
835 		oon = ec_private_key_opt_or_null;
836 		mandated_count = ARRAY_SIZE(ec_private_key_mandated);
837 		oon_count = ARRAY_SIZE(ec_private_key_opt_or_null);
838 		break;
839 	case PKCS11_CKK_EC_EDWARDS:
840 		mandated = ec_private_key_mandated;
841 		oon = eddsa_private_key_opt_or_null;
842 		mandated_count = ARRAY_SIZE(ec_private_key_mandated);
843 		oon_count = ARRAY_SIZE(eddsa_private_key_opt_or_null);
844 		break;
845 	default:
846 		EMSG("Invalid key type %#"PRIx32"/%s",
847 		     get_key_type(*out), id2str_key_type(get_key_type(*out)));
848 
849 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
850 	}
851 
852 	rc = set_mandatory_attributes(out, temp, mandated, mandated_count);
853 	if (rc)
854 		return rc;
855 
856 	return set_attributes_opt_or_null(out, temp, oon, oon_count);
857 }
858 
859 static enum pkcs11_rc
860 sanitize_symm_key_attributes(struct obj_attrs **temp,
861 			     enum processing_func function)
862 {
863 	enum pkcs11_rc rc = PKCS11_CKR_OK;
864 	uint32_t a_size = 0;
865 
866 	assert(get_class(*temp) == PKCS11_CKO_SECRET_KEY);
867 
868 	rc = get_attribute_ptr(*temp, PKCS11_CKA_VALUE, NULL, &a_size);
869 
870 	switch (get_key_type(*temp)) {
871 	case PKCS11_CKK_GENERIC_SECRET:
872 	case PKCS11_CKK_AES:
873 	case PKCS11_CKK_MD5_HMAC:
874 	case PKCS11_CKK_SHA_1_HMAC:
875 	case PKCS11_CKK_SHA256_HMAC:
876 	case PKCS11_CKK_SHA384_HMAC:
877 	case PKCS11_CKK_SHA512_HMAC:
878 	case PKCS11_CKK_SHA224_HMAC:
879 		switch (function) {
880 		case PKCS11_FUNCTION_IMPORT:
881 			/* CKA_VALUE is a mandatory with C_CreateObject */
882 			if (rc || a_size == 0)
883 				return PKCS11_CKR_TEMPLATE_INCONSISTENT;
884 
885 			if (get_attribute_ptr(*temp, PKCS11_CKA_VALUE_LEN, NULL,
886 					      NULL) != PKCS11_RV_NOT_FOUND)
887 				return PKCS11_CKR_TEMPLATE_INCONSISTENT;
888 
889 			return add_attribute(temp, PKCS11_CKA_VALUE_LEN,
890 					     &a_size, sizeof(uint32_t));
891 		case PKCS11_FUNCTION_GENERATE:
892 			if (rc != PKCS11_RV_NOT_FOUND)
893 				return PKCS11_CKR_TEMPLATE_INCONSISTENT;
894 			break;
895 		default:
896 			break;
897 		}
898 		break;
899 	default:
900 		EMSG("Invalid key type %#"PRIx32"/%s",
901 		     get_key_type(*temp), id2str_key_type(get_key_type(*temp)));
902 
903 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
904 	}
905 
906 	return PKCS11_CKR_OK;
907 }
908 
909 /*
910  * Create an attribute list for a new object from a template and a parent
911  * object (optional) for an object generation function (generate, copy,
912  * derive...).
913  *
914  * PKCS#11 directives on the supplied template and expected return value:
915  * - template has an invalid attribute ID: ATTRIBUTE_TYPE_INVALID
916  * - template has an invalid value for an attribute: ATTRIBUTE_VALID_INVALID
917  * - template has value for a read-only attribute: ATTRIBUTE_READ_ONLY
918  * - template+default+parent => still miss an attribute: TEMPLATE_INCONSISTENT
919  *
920  * INFO on PKCS11_CMD_COPY_OBJECT:
921  * - parent PKCS11_CKA_COPYIABLE=false => return ACTION_PROHIBITED.
922  * - template can specify PKCS11_CKA_TOKEN, PKCS11_CKA_PRIVATE,
923  *   PKCS11_CKA_MODIFIABLE, PKCS11_CKA_DESTROYABLE.
924  * - SENSITIVE can change from false to true, not from true to false.
925  * - LOCAL is the parent LOCAL
926  */
927 enum pkcs11_rc
928 create_attributes_from_template(struct obj_attrs **out, void *template,
929 				size_t template_size,
930 				struct obj_attrs *parent,
931 				enum processing_func function,
932 				enum pkcs11_mechanism_id mecha,
933 				enum pkcs11_class_id template_class)
934 {
935 	struct obj_attrs *temp = NULL;
936 	struct obj_attrs *attrs = NULL;
937 	enum pkcs11_rc rc = PKCS11_CKR_OK;
938 	uint8_t local = 0;
939 	uint8_t always_sensitive = 0;
940 	uint8_t never_extract = 0;
941 	uint8_t extractable = 0;
942 	uint32_t class = PKCS11_UNDEFINED_ID;
943 	uint32_t type = PKCS11_UNDEFINED_ID;
944 	uint32_t mechanism_id = PKCS11_CKM_UNDEFINED_ID;
945 	struct obj_attrs *req_attrs = NULL;
946 	uint32_t size = 0;
947 	uint32_t indirect_template = PKCS11_CKA_UNDEFINED_ID;
948 
949 #ifdef DEBUG	/* Sanity: check function argument */
950 	trace_attributes_from_api_head("template", template, template_size);
951 	switch (function) {
952 	case PKCS11_FUNCTION_GENERATE:
953 	case PKCS11_FUNCTION_GENERATE_PAIR:
954 	case PKCS11_FUNCTION_IMPORT:
955 	case PKCS11_FUNCTION_MODIFY:
956 	case PKCS11_FUNCTION_DERIVE:
957 	case PKCS11_FUNCTION_UNWRAP:
958 	case PKCS11_FUNCTION_COPY:
959 		break;
960 	default:
961 		TEE_Panic(TEE_ERROR_NOT_SUPPORTED);
962 	}
963 #endif
964 
965 	/*
966 	 * For PKCS11_FUNCTION_GENERATE, find the class and type
967 	 * based on the mechanism. These will be passed as hint
968 	 * sanitize_client_object() and added in temp if not
969 	 * already present
970 	 */
971 	if (function == PKCS11_FUNCTION_GENERATE) {
972 		switch (mecha) {
973 		case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
974 			class = PKCS11_CKO_SECRET_KEY;
975 			type = PKCS11_CKK_GENERIC_SECRET;
976 			break;
977 		case PKCS11_CKM_AES_KEY_GEN:
978 			class = PKCS11_CKO_SECRET_KEY;
979 			type = PKCS11_CKK_AES;
980 			break;
981 		default:
982 			TEE_Panic(TEE_ERROR_NOT_SUPPORTED);
983 		}
984 	}
985 
986 	/*
987 	 * For PKCS11_FUNCTION_GENERATE_PAIR, find the class and type
988 	 * based on the mechanism. These will be passed as hint
989 	 * sanitize_client_object() and added in temp if not
990 	 * already present
991 	 */
992 	if (function == PKCS11_FUNCTION_GENERATE_PAIR) {
993 		switch (mecha) {
994 		case PKCS11_CKM_EC_EDWARDS_KEY_PAIR_GEN:
995 			class = template_class;
996 			type = PKCS11_CKK_EDDSA;
997 			break;
998 		case PKCS11_CKM_EC_KEY_PAIR_GEN:
999 			class = template_class;
1000 			type = PKCS11_CKK_EC;
1001 			break;
1002 		case PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN:
1003 			class = template_class;
1004 			type = PKCS11_CKK_RSA;
1005 			break;
1006 		default:
1007 			TEE_Panic(TEE_ERROR_NOT_SUPPORTED);
1008 		}
1009 	}
1010 
1011 	/*
1012 	 * Check and remove duplicates if any and create a new temporary
1013 	 * template
1014 	 */
1015 	rc = sanitize_client_object(&temp, template, template_size, class,
1016 				    type);
1017 	if (rc)
1018 		goto out;
1019 
1020 	/*
1021 	 * For function type modify and copy return the created template
1022 	 * from here. Rest of the code below is for creating objects
1023 	 * or generating keys.
1024 	 */
1025 	switch (function) {
1026 	case PKCS11_FUNCTION_MODIFY:
1027 	case PKCS11_FUNCTION_COPY:
1028 		*out = temp;
1029 		return rc;
1030 	case PKCS11_FUNCTION_DERIVE:
1031 	case PKCS11_FUNCTION_UNWRAP:
1032 		if (function == PKCS11_FUNCTION_UNWRAP)
1033 			indirect_template = PKCS11_CKA_UNWRAP_TEMPLATE;
1034 		else
1035 			indirect_template = PKCS11_CKA_DERIVE_TEMPLATE;
1036 
1037 		rc = get_attribute_ptr(parent, indirect_template,
1038 				       (void *)&req_attrs, &size);
1039 		if (rc == PKCS11_CKR_OK && size != 0) {
1040 			rc = attributes_match_add_reference(&temp, req_attrs);
1041 			if (rc)
1042 				goto out;
1043 		}
1044 		break;
1045 	default:
1046 		break;
1047 	}
1048 
1049 	/*
1050 	 * Check if class and type in temp are consistent with the mechanism
1051 	 */
1052 	switch (mecha) {
1053 	case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
1054 		if (get_class(temp) != PKCS11_CKO_SECRET_KEY ||
1055 		    get_key_type(temp) != PKCS11_CKK_GENERIC_SECRET) {
1056 			rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
1057 			goto out;
1058 		}
1059 		break;
1060 	case PKCS11_CKM_AES_KEY_GEN:
1061 		if (get_class(temp) != PKCS11_CKO_SECRET_KEY ||
1062 		    get_key_type(temp) != PKCS11_CKK_AES) {
1063 			rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
1064 			goto out;
1065 		}
1066 		break;
1067 	case PKCS11_CKM_EC_KEY_PAIR_GEN:
1068 		if ((get_class(temp) != PKCS11_CKO_PUBLIC_KEY &&
1069 		     get_class(temp) != PKCS11_CKO_PRIVATE_KEY) ||
1070 		    get_key_type(temp) != PKCS11_CKK_EC) {
1071 			rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
1072 			goto out;
1073 		}
1074 		break;
1075 	case PKCS11_CKM_EC_EDWARDS_KEY_PAIR_GEN:
1076 		if ((get_class(temp) != PKCS11_CKO_PUBLIC_KEY &&
1077 		     get_class(temp) != PKCS11_CKO_PRIVATE_KEY) ||
1078 		    get_key_type(temp) != PKCS11_CKK_EC_EDWARDS) {
1079 			rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
1080 			goto out;
1081 		}
1082 		break;
1083 	case PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN:
1084 		if ((get_class(temp) != PKCS11_CKO_PUBLIC_KEY &&
1085 		     get_class(temp) != PKCS11_CKO_PRIVATE_KEY) ||
1086 		    get_key_type(temp) != PKCS11_CKK_RSA) {
1087 			rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
1088 			goto out;
1089 		}
1090 		break;
1091 	default:
1092 		break;
1093 	}
1094 
1095 	if (!sanitize_consistent_class_and_type(temp)) {
1096 		EMSG("Inconsistent class/type");
1097 		rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
1098 		goto out;
1099 	}
1100 
1101 	/*
1102 	 * TBD - Add a check to see if temp contains any attribute which
1103 	 * is not consistent with the object class or type and return error.
1104 	 * In current implementation such attributes are ignored and not
1105 	 * added to final object while PKCS#11 specification expects a
1106 	 * failure and an error code be returned.
1107 	 */
1108 
1109 	switch (get_class(temp)) {
1110 	case PKCS11_CKO_DATA:
1111 		rc = create_data_attributes(&attrs, temp);
1112 		break;
1113 	case PKCS11_CKO_CERTIFICATE:
1114 		rc = create_certificate_attributes(&attrs, temp);
1115 		break;
1116 	case PKCS11_CKO_SECRET_KEY:
1117 		rc = sanitize_symm_key_attributes(&temp, function);
1118 		if (rc)
1119 			goto out;
1120 		rc = create_symm_key_attributes(&attrs, temp);
1121 		break;
1122 	case PKCS11_CKO_PUBLIC_KEY:
1123 		rc = create_pub_key_attributes(&attrs, temp, function);
1124 		break;
1125 	case PKCS11_CKO_PRIVATE_KEY:
1126 		rc = create_priv_key_attributes(&attrs, temp);
1127 		break;
1128 	default:
1129 		DMSG("Invalid object class %#"PRIx32"/%s",
1130 		     get_class(temp), id2str_class(get_class(temp)));
1131 
1132 		rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
1133 		break;
1134 	}
1135 	if (rc)
1136 		goto out;
1137 
1138 	if (get_attribute_ptr(temp, PKCS11_CKA_LOCAL, NULL, NULL) !=
1139 	    PKCS11_RV_NOT_FOUND) {
1140 		rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
1141 		goto out;
1142 	}
1143 
1144 	if (get_attribute_ptr(temp, PKCS11_CKA_KEY_GEN_MECHANISM, NULL, NULL) !=
1145 	    PKCS11_RV_NOT_FOUND) {
1146 		rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
1147 		goto out;
1148 	}
1149 
1150 	switch (function) {
1151 	case PKCS11_FUNCTION_GENERATE:
1152 	case PKCS11_FUNCTION_GENERATE_PAIR:
1153 		local = PKCS11_TRUE;
1154 		break;
1155 	case PKCS11_FUNCTION_IMPORT:
1156 	case PKCS11_FUNCTION_DERIVE:
1157 	case PKCS11_FUNCTION_UNWRAP:
1158 	default:
1159 		local = PKCS11_FALSE;
1160 		break;
1161 	}
1162 	rc = add_attribute(&attrs, PKCS11_CKA_LOCAL, &local, sizeof(local));
1163 	if (rc)
1164 		goto out;
1165 
1166 	switch (get_class(attrs)) {
1167 	case PKCS11_CKO_SECRET_KEY:
1168 	case PKCS11_CKO_PRIVATE_KEY:
1169 	case PKCS11_CKO_PUBLIC_KEY:
1170 		always_sensitive = PKCS11_FALSE;
1171 		never_extract = PKCS11_FALSE;
1172 
1173 		switch (function) {
1174 		case PKCS11_FUNCTION_DERIVE:
1175 			always_sensitive =
1176 				get_bool(parent, PKCS11_CKA_ALWAYS_SENSITIVE) &&
1177 				get_bool(attrs, PKCS11_CKA_SENSITIVE);
1178 			never_extract =
1179 			       get_bool(parent, PKCS11_CKA_NEVER_EXTRACTABLE) &&
1180 			       !get_bool(attrs, PKCS11_CKA_EXTRACTABLE);
1181 			break;
1182 		case PKCS11_FUNCTION_UNWRAP:
1183 			always_sensitive = PKCS11_FALSE;
1184 			never_extract = PKCS11_FALSE;
1185 			extractable = PKCS11_TRUE;
1186 
1187 			/*
1188 			 * Check if template passed by user has CKA_EXTRACTABLE.
1189 			 * If not, by default value of CKA_EXTRACTABLE is set as
1190 			 * TRUE.
1191 			 */
1192 			if (get_attribute_ptr(temp, PKCS11_CKA_EXTRACTABLE,
1193 					      NULL,
1194 					      NULL) == PKCS11_RV_NOT_FOUND) {
1195 				rc = set_attribute(&attrs,
1196 						   PKCS11_CKA_EXTRACTABLE,
1197 						   &extractable,
1198 						   sizeof(extractable));
1199 				if (rc)
1200 					goto out;
1201 			}
1202 			break;
1203 		case PKCS11_FUNCTION_GENERATE:
1204 		case PKCS11_FUNCTION_GENERATE_PAIR:
1205 			always_sensitive = get_bool(attrs,
1206 						    PKCS11_CKA_SENSITIVE);
1207 			never_extract = !get_bool(attrs,
1208 						  PKCS11_CKA_EXTRACTABLE);
1209 			break;
1210 		default:
1211 			break;
1212 		}
1213 
1214 		rc = add_attribute(&attrs, PKCS11_CKA_ALWAYS_SENSITIVE,
1215 				   &always_sensitive, sizeof(always_sensitive));
1216 		if (rc)
1217 			goto out;
1218 
1219 		rc = add_attribute(&attrs, PKCS11_CKA_NEVER_EXTRACTABLE,
1220 				   &never_extract, sizeof(never_extract));
1221 		if (rc)
1222 			goto out;
1223 
1224 		/* Keys mandate attribute PKCS11_CKA_KEY_GEN_MECHANISM */
1225 		if (local)
1226 			mechanism_id = mecha;
1227 		else
1228 			mechanism_id = PKCS11_CK_UNAVAILABLE_INFORMATION;
1229 
1230 		rc = add_attribute(&attrs, PKCS11_CKA_KEY_GEN_MECHANISM,
1231 				   &mechanism_id, sizeof(mechanism_id));
1232 		if (rc)
1233 			goto out;
1234 		break;
1235 
1236 	default:
1237 		break;
1238 	}
1239 
1240 	*out = attrs;
1241 
1242 #ifdef DEBUG
1243 	trace_attributes("object", attrs);
1244 #endif
1245 
1246 out:
1247 	TEE_Free(temp);
1248 	if (rc)
1249 		TEE_Free(attrs);
1250 
1251 	return rc;
1252 }
1253 
1254 static enum pkcs11_rc check_attrs_misc_integrity(struct obj_attrs *head)
1255 {
1256 	if (get_bool(head, PKCS11_CKA_NEVER_EXTRACTABLE) &&
1257 	    get_bool(head, PKCS11_CKA_EXTRACTABLE)) {
1258 		DMSG("Never/Extractable attributes mismatch %d/%d",
1259 		     get_bool(head, PKCS11_CKA_NEVER_EXTRACTABLE),
1260 		     get_bool(head, PKCS11_CKA_EXTRACTABLE));
1261 
1262 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1263 	}
1264 
1265 	if (get_bool(head, PKCS11_CKA_ALWAYS_SENSITIVE) &&
1266 	    !get_bool(head, PKCS11_CKA_SENSITIVE)) {
1267 		DMSG("Sensitive/always attributes mismatch %d/%d",
1268 		     get_bool(head, PKCS11_CKA_SENSITIVE),
1269 		     get_bool(head, PKCS11_CKA_ALWAYS_SENSITIVE));
1270 
1271 		return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1272 	}
1273 
1274 	return PKCS11_CKR_OK;
1275 }
1276 
1277 bool object_is_private(struct obj_attrs *head)
1278 {
1279 	return get_bool(head, PKCS11_CKA_PRIVATE);
1280 }
1281 
1282 bool object_is_token(struct obj_attrs *head)
1283 {
1284 	return get_bool(head, PKCS11_CKA_TOKEN);
1285 }
1286 
1287 bool object_is_modifiable(struct obj_attrs *head)
1288 {
1289 	return get_bool(head, PKCS11_CKA_MODIFIABLE);
1290 }
1291 
1292 bool object_is_copyable(struct obj_attrs *head)
1293 {
1294 	return get_bool(head, PKCS11_CKA_COPYABLE);
1295 }
1296 
1297 /*
1298  * Check access to object against authentication to token
1299  */
1300 enum pkcs11_rc check_access_attrs_against_token(struct pkcs11_session *session,
1301 						struct obj_attrs *head)
1302 {
1303 	bool private = true;
1304 
1305 	switch (get_class(head)) {
1306 	case PKCS11_CKO_SECRET_KEY:
1307 	case PKCS11_CKO_PRIVATE_KEY:
1308 	case PKCS11_CKO_PUBLIC_KEY:
1309 	case PKCS11_CKO_DATA:
1310 	case PKCS11_CKO_CERTIFICATE:
1311 		private = object_is_private(head);
1312 		break;
1313 	default:
1314 		return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1315 	}
1316 
1317 	if (private && (pkcs11_session_is_public(session) ||
1318 			pkcs11_session_is_so(session))) {
1319 		DMSG("Private object access from a public or SO session");
1320 
1321 		return PKCS11_CKR_USER_NOT_LOGGED_IN;
1322 	}
1323 
1324 	return PKCS11_CKR_OK;
1325 }
1326 
1327 /*
1328  * Check the attributes of a to-be-created object matches the token state
1329  */
1330 enum pkcs11_rc check_created_attrs_against_token(struct pkcs11_session *session,
1331 						 struct obj_attrs *head)
1332 {
1333 	enum pkcs11_rc rc = PKCS11_CKR_OK;
1334 
1335 	rc = check_attrs_misc_integrity(head);
1336 	if (rc)
1337 		return rc;
1338 
1339 	if (get_bool(head, PKCS11_CKA_TRUSTED) &&
1340 	    !pkcs11_session_is_so(session)) {
1341 		DMSG("Can't create trusted object");
1342 
1343 		return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1344 	}
1345 
1346 	if (get_bool(head, PKCS11_CKA_TOKEN) &&
1347 	    !pkcs11_session_is_read_write(session)) {
1348 		DMSG("Can't create persistent object");
1349 
1350 		return PKCS11_CKR_SESSION_READ_ONLY;
1351 	}
1352 
1353 	/*
1354 	 * TODO: START_DATE and END_DATE: complies with current time?
1355 	 */
1356 	return PKCS11_CKR_OK;
1357 }
1358 
1359 #define DMSG_BAD_BBOOL(attr, proc, head)				\
1360 	do {								\
1361 		uint32_t __maybe_unused _attr = (attr);			\
1362 		uint8_t __maybe_unused _bvalue = 0;			\
1363 		enum pkcs11_rc __maybe_unused _rc = PKCS11_CKR_OK;	\
1364 									\
1365 		_rc = get_attribute((head), _attr, &_bvalue, NULL);	\
1366 		DMSG("%s issue for %s: %sfound, value %"PRIu8,		\
1367 		     id2str_attr(_attr), id2str_proc((proc)),		\
1368 		     _rc ? "not " : "", _bvalue);			\
1369 	} while (0)
1370 
1371 static bool __maybe_unused check_attr_bval(uint32_t proc_id __maybe_unused,
1372 					   struct obj_attrs *head,
1373 					   uint32_t attribute, bool val)
1374 {
1375 	uint8_t bbool = 0;
1376 	uint32_t sz = sizeof(bbool);
1377 
1378 	if (!get_attribute(head, attribute, &bbool, &sz) && !!bbool == val)
1379 		return true;
1380 
1381 	DMSG_BAD_BBOOL(attribute, proc_id, head);
1382 	return false;
1383 }
1384 
1385 /*
1386  * Check the attributes of a new secret match the processing/mechanism
1387  * used to create it.
1388  *
1389  * @proc_id - PKCS11_CKM_xxx
1390  * @head - head of the attributes of the to-be-created object.
1391  */
1392 enum pkcs11_rc check_created_attrs_against_processing(uint32_t proc_id,
1393 						      struct obj_attrs *head)
1394 {
1395 	/*
1396 	 * Processings that do not create secrets are not expected to call
1397 	 * this function which would panic.
1398 	 */
1399 	switch (proc_id) {
1400 	case PKCS11_PROCESSING_IMPORT:
1401 	case PKCS11_CKM_ECDH1_DERIVE:
1402 	case PKCS11_CKM_AES_ECB:
1403 	case PKCS11_CKM_AES_CBC:
1404 	case PKCS11_CKM_AES_ECB_ENCRYPT_DATA:
1405 	case PKCS11_CKM_AES_CBC_ENCRYPT_DATA:
1406 		assert(check_attr_bval(proc_id, head, PKCS11_CKA_LOCAL, false));
1407 		break;
1408 	case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
1409 	case PKCS11_CKM_AES_KEY_GEN:
1410 	case PKCS11_CKM_EC_EDWARDS_KEY_PAIR_GEN:
1411 	case PKCS11_CKM_EC_KEY_PAIR_GEN:
1412 	case PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN:
1413 		assert(check_attr_bval(proc_id, head, PKCS11_CKA_LOCAL, true));
1414 		break;
1415 	default:
1416 		TEE_Panic(proc_id);
1417 		break;
1418 	}
1419 
1420 	switch (proc_id) {
1421 	case PKCS11_CKM_GENERIC_SECRET_KEY_GEN:
1422 		assert(get_key_type(head) == PKCS11_CKK_GENERIC_SECRET);
1423 		break;
1424 	case PKCS11_CKM_AES_KEY_GEN:
1425 		assert(get_key_type(head) == PKCS11_CKK_AES);
1426 		break;
1427 	case PKCS11_CKM_EC_EDWARDS_KEY_PAIR_GEN:
1428 		assert(get_key_type(head) == PKCS11_CKK_EC_EDWARDS);
1429 		break;
1430 	case PKCS11_CKM_EC_KEY_PAIR_GEN:
1431 		assert(get_key_type(head) == PKCS11_CKK_EC);
1432 		break;
1433 	case PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN:
1434 		assert(get_key_type(head) == PKCS11_CKK_RSA);
1435 		break;
1436 	case PKCS11_PROCESSING_IMPORT:
1437 	case PKCS11_CKM_ECDH1_DERIVE:
1438 	default:
1439 		break;
1440 	}
1441 
1442 	return PKCS11_CKR_OK;
1443 }
1444 
1445 /* Return min and max key size supported for a key_type in bytes */
1446 static void get_key_min_max_sizes(enum pkcs11_key_type key_type,
1447 				  uint32_t *min_key_size,
1448 				  uint32_t *max_key_size)
1449 {
1450 	enum pkcs11_mechanism_id mechanism = PKCS11_CKM_UNDEFINED_ID;
1451 
1452 	switch (key_type) {
1453 	case PKCS11_CKK_GENERIC_SECRET:
1454 		mechanism = PKCS11_CKM_GENERIC_SECRET_KEY_GEN;
1455 		break;
1456 	case PKCS11_CKK_AES:
1457 		mechanism = PKCS11_CKM_AES_KEY_GEN;
1458 		break;
1459 	case PKCS11_CKK_MD5_HMAC:
1460 		mechanism = PKCS11_CKM_MD5_HMAC;
1461 		break;
1462 	case PKCS11_CKK_SHA_1_HMAC:
1463 		mechanism = PKCS11_CKM_SHA_1_HMAC;
1464 		break;
1465 	case PKCS11_CKK_SHA224_HMAC:
1466 		mechanism = PKCS11_CKM_SHA224_HMAC;
1467 		break;
1468 	case PKCS11_CKK_SHA256_HMAC:
1469 		mechanism = PKCS11_CKM_SHA256_HMAC;
1470 		break;
1471 	case PKCS11_CKK_SHA384_HMAC:
1472 		mechanism = PKCS11_CKM_SHA384_HMAC;
1473 		break;
1474 	case PKCS11_CKK_SHA512_HMAC:
1475 		mechanism = PKCS11_CKM_SHA512_HMAC;
1476 		break;
1477 	case PKCS11_CKK_EC:
1478 		mechanism = PKCS11_CKM_EC_KEY_PAIR_GEN;
1479 		break;
1480 	case PKCS11_CKK_EDDSA:
1481 		mechanism = PKCS11_CKM_EC_EDWARDS_KEY_PAIR_GEN;
1482 		break;
1483 	case PKCS11_CKK_RSA:
1484 		mechanism = PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN;
1485 		break;
1486 	default:
1487 		TEE_Panic(key_type);
1488 		break;
1489 	}
1490 
1491 	mechanism_supported_key_sizes_bytes(mechanism, min_key_size,
1492 					    max_key_size);
1493 }
1494 
1495 enum pkcs11_rc check_created_attrs(struct obj_attrs *key1,
1496 				   struct obj_attrs *key2)
1497 {
1498 	enum pkcs11_rc rc = PKCS11_CKR_OK;
1499 	struct obj_attrs *secret = NULL;
1500 	struct obj_attrs *private = NULL;
1501 	struct obj_attrs *public = NULL;
1502 	uint32_t max_key_size = 0;
1503 	uint32_t min_key_size = 0;
1504 	uint32_t key_length = 0;
1505 
1506 	switch (get_class(key1)) {
1507 	case PKCS11_CKO_SECRET_KEY:
1508 		secret = key1;
1509 		break;
1510 	case PKCS11_CKO_PUBLIC_KEY:
1511 		public = key1;
1512 		break;
1513 	case PKCS11_CKO_PRIVATE_KEY:
1514 		private = key1;
1515 		break;
1516 	default:
1517 		return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID;
1518 	}
1519 
1520 	if (key2) {
1521 		switch (get_class(key2)) {
1522 		case PKCS11_CKO_PUBLIC_KEY:
1523 			public = key2;
1524 			if (private == key1)
1525 				break;
1526 
1527 			return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1528 		case PKCS11_CKO_PRIVATE_KEY:
1529 			private = key2;
1530 			if (public == key1)
1531 				break;
1532 
1533 			return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1534 		default:
1535 			return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID;
1536 		}
1537 
1538 		if (get_key_type(private) != get_key_type(public))
1539 			return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1540 	}
1541 
1542 	if (secret) {
1543 		switch (get_key_type(secret)) {
1544 		case PKCS11_CKK_AES:
1545 		case PKCS11_CKK_GENERIC_SECRET:
1546 		case PKCS11_CKK_MD5_HMAC:
1547 		case PKCS11_CKK_SHA_1_HMAC:
1548 		case PKCS11_CKK_SHA224_HMAC:
1549 		case PKCS11_CKK_SHA256_HMAC:
1550 		case PKCS11_CKK_SHA384_HMAC:
1551 		case PKCS11_CKK_SHA512_HMAC:
1552 			break;
1553 		default:
1554 			return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1555 		}
1556 
1557 		/* Get key size */
1558 		rc = get_u32_attribute(secret, PKCS11_CKA_VALUE_LEN,
1559 				       &key_length);
1560 		if (rc)
1561 			return PKCS11_CKR_TEMPLATE_INCOMPLETE;
1562 	}
1563 	if (public) {
1564 		switch (get_key_type(public)) {
1565 		case PKCS11_CKK_RSA:
1566 			/* Get key size */
1567 			rc = get_u32_attribute(public, PKCS11_CKA_MODULUS_BITS,
1568 					       &key_length);
1569 			if (rc)
1570 				return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1571 			key_length = ROUNDUP(key_length, 8) / 8;
1572 			break;
1573 		case PKCS11_CKK_EC:
1574 		case PKCS11_CKK_EC_EDWARDS:
1575 			break;
1576 		default:
1577 			return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1578 		}
1579 	}
1580 	if (private) {
1581 		switch (get_key_type(private)) {
1582 		case PKCS11_CKK_RSA:
1583 		case PKCS11_CKK_EC:
1584 		case PKCS11_CKK_EC_EDWARDS:
1585 			break;
1586 		default:
1587 			return PKCS11_CKR_TEMPLATE_INCONSISTENT;
1588 		}
1589 	}
1590 
1591 	/*
1592 	 * Check key size for symmetric keys and RSA keys
1593 	 * EC is bound to domains, no need to check here.
1594 	 */
1595 	switch (get_key_type(key1)) {
1596 	case PKCS11_CKK_EC:
1597 	case PKCS11_CKK_EC_EDWARDS:
1598 		return PKCS11_CKR_OK;
1599 	default:
1600 		break;
1601 	}
1602 
1603 	get_key_min_max_sizes(get_key_type(key1), &min_key_size, &max_key_size);
1604 	if (key_length < min_key_size || key_length > max_key_size) {
1605 		EMSG("Length %"PRIu32" vs range [%"PRIu32" %"PRIu32"]",
1606 		     key_length, min_key_size, max_key_size);
1607 
1608 		return PKCS11_CKR_KEY_SIZE_RANGE;
1609 	}
1610 
1611 	if (secret && get_key_type(secret) == PKCS11_CKK_AES) {
1612 		if (key_length != 16 && key_length != 24 && key_length != 32)
1613 			return PKCS11_CKR_KEY_SIZE_RANGE;
1614 	}
1615 
1616 	return PKCS11_CKR_OK;
1617 }
1618 
1619 /* Check processing ID against attribute ALLOWED_MECHANISMS if any */
1620 static bool parent_key_complies_allowed_processings(uint32_t proc_id,
1621 						    struct obj_attrs *head)
1622 {
1623 	char *attr = NULL;
1624 	uint32_t size = 0;
1625 	uint32_t proc = 0;
1626 	size_t count = 0;
1627 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
1628 
1629 	rc = get_attribute_ptr(head, PKCS11_CKA_ALLOWED_MECHANISMS,
1630 			       (void *)&attr, &size);
1631 	if (rc == PKCS11_RV_NOT_FOUND)
1632 		return true;
1633 	if (rc) {
1634 		EMSG("unexpected attributes state");
1635 		TEE_Panic(TEE_ERROR_BAD_STATE);
1636 	}
1637 
1638 	for (count = size / sizeof(uint32_t); count; count--) {
1639 		TEE_MemMove(&proc, attr, sizeof(uint32_t));
1640 		attr += sizeof(uint32_t);
1641 
1642 		if (proc == proc_id)
1643 			return true;
1644 	}
1645 
1646 	DMSG("can't find %s in allowed list", id2str_proc(proc_id));
1647 	return false;
1648 }
1649 
1650 static enum pkcs11_attr_id func_to_attr(enum processing_func func)
1651 {
1652 	switch (func) {
1653 	case PKCS11_FUNCTION_ENCRYPT:
1654 		return PKCS11_CKA_ENCRYPT;
1655 	case PKCS11_FUNCTION_DECRYPT:
1656 		return PKCS11_CKA_DECRYPT;
1657 	case PKCS11_FUNCTION_SIGN:
1658 		return PKCS11_CKA_SIGN;
1659 	case PKCS11_FUNCTION_VERIFY:
1660 		return PKCS11_CKA_VERIFY;
1661 	case PKCS11_FUNCTION_WRAP:
1662 		return PKCS11_CKA_WRAP;
1663 	case PKCS11_FUNCTION_UNWRAP:
1664 		return PKCS11_CKA_UNWRAP;
1665 	case PKCS11_FUNCTION_DERIVE:
1666 		return PKCS11_CKA_DERIVE;
1667 	default:
1668 		return PKCS11_CKA_UNDEFINED_ID;
1669 	}
1670 }
1671 
1672 enum pkcs11_rc
1673 check_parent_attrs_against_processing(enum pkcs11_mechanism_id proc_id,
1674 				      enum processing_func function,
1675 				      struct obj_attrs *head)
1676 {
1677 	enum pkcs11_class_id key_class = get_class(head);
1678 	enum pkcs11_key_type key_type = get_key_type(head);
1679 	enum pkcs11_attr_id attr = func_to_attr(function);
1680 
1681 	if (!get_bool(head, attr)) {
1682 		DMSG("%s not permitted", id2str_attr(attr));
1683 		return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1684 	}
1685 
1686 	/* Check processing complies with parent key family */
1687 	switch (proc_id) {
1688 	case PKCS11_CKM_AES_ECB:
1689 	case PKCS11_CKM_AES_CBC:
1690 	case PKCS11_CKM_AES_CTS:
1691 	case PKCS11_CKM_AES_CTR:
1692 	case PKCS11_CKM_AES_CMAC:
1693 	case PKCS11_CKM_AES_CMAC_GENERAL:
1694 		if (key_class == PKCS11_CKO_SECRET_KEY &&
1695 		    key_type == PKCS11_CKK_AES)
1696 			break;
1697 
1698 		DMSG("%s invalid key %s/%s", id2str_proc(proc_id),
1699 		     id2str_class(key_class), id2str_key_type(key_type));
1700 
1701 		if (function == PKCS11_FUNCTION_WRAP)
1702 			return PKCS11_CKR_WRAPPING_KEY_TYPE_INCONSISTENT;
1703 		else if (function == PKCS11_FUNCTION_UNWRAP)
1704 			return PKCS11_CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT;
1705 		else
1706 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1707 
1708 	case PKCS11_CKM_AES_ECB_ENCRYPT_DATA:
1709 	case PKCS11_CKM_AES_CBC_ENCRYPT_DATA:
1710 		if (key_class != PKCS11_CKO_SECRET_KEY &&
1711 		    key_type != PKCS11_CKK_AES)
1712 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1713 
1714 		if (get_bool(head, PKCS11_CKA_ENCRYPT)) {
1715 			/*
1716 			 * Intentionally refuse to proceed despite
1717 			 * PKCS#11 specifications v2.40 and v3.0 not expecting
1718 			 * this behavior to avoid potential security issue
1719 			 * where keys derived by these mechanisms can be
1720 			 * revealed by doing data encryption using parent key.
1721 			 */
1722 			return PKCS11_CKR_FUNCTION_FAILED;
1723 		}
1724 
1725 		break;
1726 	case PKCS11_CKM_MD5_HMAC:
1727 	case PKCS11_CKM_SHA_1_HMAC:
1728 	case PKCS11_CKM_SHA224_HMAC:
1729 	case PKCS11_CKM_SHA256_HMAC:
1730 	case PKCS11_CKM_SHA384_HMAC:
1731 	case PKCS11_CKM_SHA512_HMAC:
1732 	case PKCS11_CKM_MD5_HMAC_GENERAL:
1733 	case PKCS11_CKM_SHA_1_HMAC_GENERAL:
1734 	case PKCS11_CKM_SHA224_HMAC_GENERAL:
1735 	case PKCS11_CKM_SHA256_HMAC_GENERAL:
1736 	case PKCS11_CKM_SHA384_HMAC_GENERAL:
1737 	case PKCS11_CKM_SHA512_HMAC_GENERAL:
1738 		if (key_class != PKCS11_CKO_SECRET_KEY)
1739 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1740 
1741 		if (key_type == PKCS11_CKK_GENERIC_SECRET)
1742 			break;
1743 
1744 		switch (proc_id) {
1745 		case PKCS11_CKM_MD5_HMAC:
1746 		case PKCS11_CKM_MD5_HMAC_GENERAL:
1747 			if (key_type == PKCS11_CKK_MD5_HMAC)
1748 				break;
1749 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1750 		case PKCS11_CKM_SHA_1_HMAC:
1751 		case PKCS11_CKM_SHA_1_HMAC_GENERAL:
1752 			if (key_type == PKCS11_CKK_SHA_1_HMAC)
1753 				break;
1754 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1755 		case PKCS11_CKM_SHA224_HMAC:
1756 		case PKCS11_CKM_SHA224_HMAC_GENERAL:
1757 			if (key_type == PKCS11_CKK_SHA224_HMAC)
1758 				break;
1759 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1760 		case PKCS11_CKM_SHA256_HMAC:
1761 		case PKCS11_CKM_SHA256_HMAC_GENERAL:
1762 			if (key_type == PKCS11_CKK_SHA256_HMAC)
1763 				break;
1764 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1765 		case PKCS11_CKM_SHA384_HMAC:
1766 		case PKCS11_CKM_SHA384_HMAC_GENERAL:
1767 			if (key_type == PKCS11_CKK_SHA384_HMAC)
1768 				break;
1769 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1770 		case PKCS11_CKM_SHA512_HMAC:
1771 		case PKCS11_CKM_SHA512_HMAC_GENERAL:
1772 			if (key_type == PKCS11_CKK_SHA512_HMAC)
1773 				break;
1774 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1775 		default:
1776 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1777 		}
1778 		break;
1779 
1780 	case PKCS11_CKM_EDDSA:
1781 		if (key_type != PKCS11_CKK_EC_EDWARDS) {
1782 			EMSG("Invalid key %s for mechanism %s",
1783 			     id2str_type(key_type, key_class),
1784 			     id2str_proc(proc_id));
1785 			return PKCS11_CKR_KEY_TYPE_INCONSISTENT;
1786 		}
1787 		if (key_class != PKCS11_CKO_PUBLIC_KEY &&
1788 		    key_class != PKCS11_CKO_PRIVATE_KEY) {
1789 			EMSG("Invalid key class for mechanism %s",
1790 			     id2str_proc(proc_id));
1791 
1792 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1793 		}
1794 		break;
1795 
1796 	case PKCS11_CKM_ECDSA:
1797 	case PKCS11_CKM_ECDSA_SHA1:
1798 	case PKCS11_CKM_ECDSA_SHA224:
1799 	case PKCS11_CKM_ECDSA_SHA256:
1800 	case PKCS11_CKM_ECDSA_SHA384:
1801 	case PKCS11_CKM_ECDSA_SHA512:
1802 	case PKCS11_CKM_ECDH1_DERIVE:
1803 		if (key_type != PKCS11_CKK_EC) {
1804 			EMSG("Invalid key %s for mechanism %s",
1805 			     id2str_type(key_type, key_class),
1806 			     id2str_proc(proc_id));
1807 
1808 			return PKCS11_CKR_KEY_TYPE_INCONSISTENT;
1809 		}
1810 		if (key_class != PKCS11_CKO_PUBLIC_KEY &&
1811 		    key_class != PKCS11_CKO_PRIVATE_KEY) {
1812 			EMSG("Invalid key class for mechanism %s",
1813 			     id2str_proc(proc_id));
1814 
1815 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1816 		}
1817 		break;
1818 	case PKCS11_CKM_RSA_PKCS:
1819 	case PKCS11_CKM_MD5_RSA_PKCS:
1820 	case PKCS11_CKM_SHA1_RSA_PKCS:
1821 	case PKCS11_CKM_SHA224_RSA_PKCS:
1822 	case PKCS11_CKM_SHA256_RSA_PKCS:
1823 	case PKCS11_CKM_SHA384_RSA_PKCS:
1824 	case PKCS11_CKM_SHA512_RSA_PKCS:
1825 	case PKCS11_CKM_RSA_PKCS_OAEP:
1826 	case PKCS11_CKM_RSA_PKCS_PSS:
1827 	case PKCS11_CKM_SHA1_RSA_PKCS_PSS:
1828 	case PKCS11_CKM_SHA224_RSA_PKCS_PSS:
1829 	case PKCS11_CKM_SHA256_RSA_PKCS_PSS:
1830 	case PKCS11_CKM_SHA384_RSA_PKCS_PSS:
1831 	case PKCS11_CKM_SHA512_RSA_PKCS_PSS:
1832 		if (key_type != PKCS11_CKK_RSA) {
1833 			EMSG("Invalid key %s for mechanism %s",
1834 			     id2str_type(key_type, key_class),
1835 			     id2str_proc(proc_id));
1836 
1837 			return PKCS11_CKR_KEY_TYPE_INCONSISTENT;
1838 		}
1839 		if (key_class != PKCS11_CKO_PUBLIC_KEY &&
1840 		    key_class != PKCS11_CKO_PRIVATE_KEY) {
1841 			EMSG("Invalid key class for mechanism %s",
1842 			     id2str_proc(proc_id));
1843 
1844 			return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1845 		}
1846 		break;
1847 	default:
1848 		DMSG("Invalid processing %#"PRIx32"/%s", proc_id,
1849 		     id2str_proc(proc_id));
1850 
1851 		return PKCS11_CKR_MECHANISM_INVALID;
1852 	}
1853 
1854 	if (!parent_key_complies_allowed_processings(proc_id, head)) {
1855 		DMSG("Allowed mechanism failed");
1856 		return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED;
1857 	}
1858 
1859 	return PKCS11_CKR_OK;
1860 }
1861 
1862 bool attribute_is_exportable(struct pkcs11_attribute_head *req_attr,
1863 			     struct pkcs11_object *obj)
1864 {
1865 	uint8_t boolval = 0;
1866 	uint32_t boolsize = 0;
1867 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
1868 	enum pkcs11_class_id key_class = get_class(obj->attributes);
1869 
1870 	if (key_class != PKCS11_CKO_SECRET_KEY &&
1871 	    key_class != PKCS11_CKO_PRIVATE_KEY)
1872 		return true;
1873 
1874 	switch (req_attr->id) {
1875 	case PKCS11_CKA_PRIVATE_EXPONENT:
1876 	case PKCS11_CKA_PRIME_1:
1877 	case PKCS11_CKA_PRIME_2:
1878 	case PKCS11_CKA_EXPONENT_1:
1879 	case PKCS11_CKA_EXPONENT_2:
1880 	case PKCS11_CKA_COEFFICIENT:
1881 	case PKCS11_CKA_VALUE:
1882 		boolsize = sizeof(boolval);
1883 		rc = get_attribute(obj->attributes, PKCS11_CKA_EXTRACTABLE,
1884 				   &boolval, &boolsize);
1885 		if (rc || boolval == PKCS11_FALSE)
1886 			return false;
1887 
1888 		boolsize = sizeof(boolval);
1889 		rc = get_attribute(obj->attributes, PKCS11_CKA_SENSITIVE,
1890 				   &boolval, &boolsize);
1891 		if (rc || boolval == PKCS11_TRUE)
1892 			return false;
1893 		break;
1894 	default:
1895 		break;
1896 	}
1897 
1898 	return true;
1899 }
1900 
1901 static bool attr_is_modifiable_any_key(struct pkcs11_attribute_head *attr)
1902 {
1903 	switch (attr->id) {
1904 	case PKCS11_CKA_ID:
1905 	case PKCS11_CKA_START_DATE:
1906 	case PKCS11_CKA_END_DATE:
1907 	case PKCS11_CKA_DERIVE:
1908 		return true;
1909 	default:
1910 		return false;
1911 	}
1912 }
1913 
1914 static bool attr_is_modifiable_secret_key(struct pkcs11_attribute_head *attr,
1915 					  struct pkcs11_session *session,
1916 					  struct pkcs11_object *obj)
1917 {
1918 	switch (attr->id) {
1919 	case PKCS11_CKA_ENCRYPT:
1920 	case PKCS11_CKA_DECRYPT:
1921 	case PKCS11_CKA_SIGN:
1922 	case PKCS11_CKA_VERIFY:
1923 	case PKCS11_CKA_WRAP:
1924 	case PKCS11_CKA_UNWRAP:
1925 		return true;
1926 	/* Can't be modified once set to CK_FALSE - 12 in Table 10 */
1927 	case PKCS11_CKA_EXTRACTABLE:
1928 		return get_bool(obj->attributes, attr->id);
1929 	/* Can't be modified once set to CK_TRUE - 11 in Table 10 */
1930 	case PKCS11_CKA_SENSITIVE:
1931 	case PKCS11_CKA_WRAP_WITH_TRUSTED:
1932 		return !get_bool(obj->attributes, attr->id);
1933 	/* Change in CKA_TRUSTED can only be done by SO */
1934 	case PKCS11_CKA_TRUSTED:
1935 		return pkcs11_session_is_so(session);
1936 	case PKCS11_CKA_NEVER_EXTRACTABLE:
1937 	case PKCS11_CKA_ALWAYS_SENSITIVE:
1938 		return false;
1939 	default:
1940 		return false;
1941 	}
1942 }
1943 
1944 static bool attr_is_modifiable_public_key(struct pkcs11_attribute_head *attr,
1945 					  struct pkcs11_session *session,
1946 					  struct pkcs11_object *obj __unused)
1947 {
1948 	switch (attr->id) {
1949 	case PKCS11_CKA_SUBJECT:
1950 	case PKCS11_CKA_ENCRYPT:
1951 	case PKCS11_CKA_VERIFY:
1952 	case PKCS11_CKA_VERIFY_RECOVER:
1953 	case PKCS11_CKA_WRAP:
1954 		return true;
1955 	case PKCS11_CKA_TRUSTED:
1956 		/* Change in CKA_TRUSTED can only be done by SO */
1957 		return pkcs11_session_is_so(session);
1958 	default:
1959 		return false;
1960 	}
1961 }
1962 
1963 static bool attr_is_modifiable_private_key(struct pkcs11_attribute_head *attr,
1964 					   struct pkcs11_session *sess __unused,
1965 					   struct pkcs11_object *obj)
1966 {
1967 	switch (attr->id) {
1968 	case PKCS11_CKA_SUBJECT:
1969 	case PKCS11_CKA_DECRYPT:
1970 	case PKCS11_CKA_SIGN:
1971 	case PKCS11_CKA_SIGN_RECOVER:
1972 	case PKCS11_CKA_UNWRAP:
1973 	/*
1974 	 * TBD: Revisit if we don't support PKCS11_CKA_PUBLIC_KEY_INFO
1975 	 * Specification mentions that if this attribute is
1976 	 * supplied as part of a template for C_CreateObject, C_CopyObject or
1977 	 * C_SetAttributeValue for a private key, the token MUST verify
1978 	 * correspondence between the private key data and the public key data
1979 	 * as supplied in CKA_PUBLIC_KEY_INFO. This needs to be
1980 	 * taken care of when this object type will be implemented
1981 	 */
1982 	case PKCS11_CKA_PUBLIC_KEY_INFO:
1983 		return true;
1984 	/* Can't be modified once set to CK_FALSE - 12 in Table 10 */
1985 	case PKCS11_CKA_EXTRACTABLE:
1986 		return get_bool(obj->attributes, attr->id);
1987 	/* Can't be modified once set to CK_TRUE - 11 in Table 10 */
1988 	case PKCS11_CKA_SENSITIVE:
1989 	case PKCS11_CKA_WRAP_WITH_TRUSTED:
1990 		return !get_bool(obj->attributes, attr->id);
1991 	case PKCS11_CKA_NEVER_EXTRACTABLE:
1992 	case PKCS11_CKA_ALWAYS_SENSITIVE:
1993 		return false;
1994 	default:
1995 		return false;
1996 	}
1997 }
1998 
1999 static bool attr_is_modifiable_certificate(struct pkcs11_attribute_head *attr,
2000 					   struct pkcs11_session *session,
2001 					   struct pkcs11_object *obj)
2002 {
2003 	uint8_t boolval = 0;
2004 	uint32_t boolsize = 0;
2005 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
2006 
2007 	/* Trusted certificates cannot be modified. */
2008 	rc = get_attribute(obj->attributes, PKCS11_CKA_TRUSTED,
2009 			   &boolval, &boolsize);
2010 	if (rc || boolval == PKCS11_TRUE)
2011 		return false;
2012 
2013 	/* Common certificate attributes */
2014 	switch (attr->id) {
2015 	case PKCS11_CKA_TRUSTED:
2016 		/*
2017 		 * The CKA_TRUSTED attribute cannot be set to CK_TRUE by an
2018 		 * application. It MUST be set by a token initialization
2019 		 * application or by the token’s SO.
2020 		 */
2021 		return pkcs11_session_is_so(session);
2022 	case PKCS11_CKA_CERTIFICATE_TYPE:
2023 	case PKCS11_CKA_CERTIFICATE_CATEGORY:
2024 		return false;
2025 	default:
2026 		break;
2027 	}
2028 
2029 	/* Certificate type specific attributes */
2030 	switch (get_certificate_type(obj->attributes)) {
2031 	case PKCS11_CKC_X_509:
2032 		/*
2033 		 * Only the CKA_ID, CKA_ISSUER, and CKA_SERIAL_NUMBER
2034 		 * attributes may be modified after the object is created.
2035 		 */
2036 		switch (attr->id) {
2037 		case PKCS11_CKA_ID:
2038 		case PKCS11_CKA_ISSUER:
2039 		case PKCS11_CKA_SERIAL_NUMBER:
2040 			return true;
2041 		default:
2042 			break;
2043 		}
2044 		break;
2045 	default:
2046 		/* Unsupported certificate type */
2047 		break;
2048 	}
2049 
2050 	return false;
2051 }
2052 
2053 static bool attribute_is_modifiable(struct pkcs11_session *session,
2054 				    struct pkcs11_attribute_head *req_attr,
2055 				    struct pkcs11_object *obj,
2056 				    enum pkcs11_class_id class,
2057 				    enum processing_func function)
2058 {
2059 	/* Check modifiable attributes common to any object */
2060 	switch (req_attr->id) {
2061 	case PKCS11_CKA_LABEL:
2062 		return true;
2063 	case PKCS11_CKA_TOKEN:
2064 	case PKCS11_CKA_MODIFIABLE:
2065 	case PKCS11_CKA_DESTROYABLE:
2066 	case PKCS11_CKA_PRIVATE:
2067 		return function == PKCS11_FUNCTION_COPY;
2068 	case PKCS11_CKA_COPYABLE:
2069 		/*
2070 		 * Specification mentions that if the attribute value is false
2071 		 * it can't be set to true. Reading this we assume that it
2072 		 * should be possible to modify this attribute even though this
2073 		 * is not marked as modifiable in Table 10 if done in right
2074 		 * direction i.e from TRUE -> FALSE.
2075 		 */
2076 		return get_bool(obj->attributes, req_attr->id);
2077 	default:
2078 		break;
2079 	}
2080 
2081 	/* Attribute checking based on class type */
2082 	switch (class) {
2083 	case PKCS11_CKO_SECRET_KEY:
2084 	case PKCS11_CKO_PUBLIC_KEY:
2085 	case PKCS11_CKO_PRIVATE_KEY:
2086 		if (attr_is_modifiable_any_key(req_attr))
2087 			return true;
2088 		if (class == PKCS11_CKO_SECRET_KEY &&
2089 		    attr_is_modifiable_secret_key(req_attr, session, obj))
2090 			return true;
2091 		if (class == PKCS11_CKO_PUBLIC_KEY &&
2092 		    attr_is_modifiable_public_key(req_attr, session, obj))
2093 			return true;
2094 		if (class == PKCS11_CKO_PRIVATE_KEY &&
2095 		    attr_is_modifiable_private_key(req_attr, session, obj))
2096 			return true;
2097 		break;
2098 	case PKCS11_CKO_DATA:
2099 		/* None of the data object attributes are modifiable */
2100 		return false;
2101 	case PKCS11_CKO_CERTIFICATE:
2102 		return attr_is_modifiable_certificate(req_attr, session, obj);
2103 	default:
2104 		break;
2105 	}
2106 
2107 	return false;
2108 }
2109 
2110 enum pkcs11_rc check_attrs_against_modification(struct pkcs11_session *session,
2111 						struct obj_attrs *head,
2112 						struct pkcs11_object *obj,
2113 						enum processing_func function)
2114 {
2115 	enum pkcs11_class_id class = PKCS11_CKO_UNDEFINED_ID;
2116 	char *cur = NULL;
2117 	char *end = NULL;
2118 	size_t len = 0;
2119 
2120 	class = get_class(obj->attributes);
2121 
2122 	cur = (char *)head + sizeof(struct obj_attrs);
2123 	end = cur + head->attrs_size;
2124 
2125 	for (; cur < end; cur += len) {
2126 		/* Structure aligned copy of the pkcs11_ref in the object */
2127 		struct pkcs11_attribute_head cli_ref = { };
2128 
2129 		TEE_MemMove(&cli_ref, cur, sizeof(cli_ref));
2130 		len = sizeof(cli_ref) + cli_ref.size;
2131 
2132 		/*
2133 		 * Check 1 - Check if attribute belongs to the object
2134 		 * The obj->attributes has all the attributes in
2135 		 * it which are allowed for an object.
2136 		 */
2137 		if (get_attribute_ptr(obj->attributes, cli_ref.id, NULL,
2138 				      NULL) == PKCS11_RV_NOT_FOUND)
2139 			return PKCS11_CKR_ATTRIBUTE_TYPE_INVALID;
2140 
2141 		/* Check 2 - Is attribute modifiable */
2142 		if (!attribute_is_modifiable(session, &cli_ref, obj, class,
2143 					     function))
2144 			return PKCS11_CKR_ATTRIBUTE_READ_ONLY;
2145 
2146 		/*
2147 		 * Checks for modification in PKCS11_CKA_TOKEN and
2148 		 * PKCS11_CKA_PRIVATE are required for PKCS11_FUNCTION_COPY
2149 		 * only, so skip them for PKCS11_FUNCTION_MODIFY.
2150 		 */
2151 		if (function == PKCS11_FUNCTION_MODIFY)
2152 			continue;
2153 
2154 		/*
2155 		 * An attempt to copy an object to a token will fail for
2156 		 * RO session
2157 		 */
2158 		if (cli_ref.id == PKCS11_CKA_TOKEN &&
2159 		    get_bool(head, PKCS11_CKA_TOKEN)) {
2160 			if (!pkcs11_session_is_read_write(session)) {
2161 				DMSG("Can't copy to token in a RO session");
2162 				return PKCS11_CKR_SESSION_READ_ONLY;
2163 			}
2164 		}
2165 
2166 		if (cli_ref.id == PKCS11_CKA_PRIVATE) {
2167 			bool parent_priv =
2168 				get_bool(obj->attributes, cli_ref.id);
2169 			bool obj_priv = get_bool(head, cli_ref.id);
2170 
2171 			/*
2172 			 * If PKCS11_CKA_PRIVATE is being set to TRUE from
2173 			 * FALSE, user has to be logged in
2174 			 */
2175 			if (!parent_priv && obj_priv) {
2176 				if ((pkcs11_session_is_public(session) ||
2177 				     pkcs11_session_is_so(session)))
2178 					return PKCS11_CKR_USER_NOT_LOGGED_IN;
2179 			}
2180 
2181 			/*
2182 			 * Restriction added - Even for Copy, do not allow
2183 			 * modification of CKA_PRIVATE from TRUE to FALSE
2184 			 */
2185 			if (parent_priv && !obj_priv)
2186 				return PKCS11_CKR_TEMPLATE_INCONSISTENT;
2187 		}
2188 	}
2189 
2190 	return PKCS11_CKR_OK;
2191 }
2192 
2193 static enum pkcs11_rc set_secret_key_data(struct obj_attrs **head, void *data,
2194 					  size_t key_size)
2195 {
2196 	uint32_t size = sizeof(uint32_t);
2197 	uint32_t key_length = 0;
2198 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
2199 
2200 	/* Get key size if present in template */
2201 	rc = get_attribute(*head, PKCS11_CKA_VALUE_LEN, &key_length, &size);
2202 	if (rc && rc != PKCS11_RV_NOT_FOUND)
2203 		return rc;
2204 
2205 	if (key_length) {
2206 		if (key_size < key_length)
2207 			return PKCS11_CKR_DATA_LEN_RANGE;
2208 	} else {
2209 		key_length = key_size;
2210 		rc = set_attribute(head, PKCS11_CKA_VALUE_LEN, &key_length,
2211 				   sizeof(uint32_t));
2212 		if (rc)
2213 			return rc;
2214 	}
2215 
2216 	/* Now we can check the VALUE_LEN field */
2217 	rc = check_created_attrs(*head, NULL);
2218 	if (rc)
2219 		return rc;
2220 
2221 	/* Remove the default empty value attribute if found */
2222 	rc = remove_empty_attribute(head, PKCS11_CKA_VALUE);
2223 	if (rc != PKCS11_CKR_OK && rc != PKCS11_RV_NOT_FOUND)
2224 		return PKCS11_CKR_GENERAL_ERROR;
2225 
2226 	return add_attribute(head, PKCS11_CKA_VALUE, data, key_length);
2227 }
2228 
2229 enum pkcs11_rc set_key_data(struct obj_attrs **head, void *data,
2230 			    size_t key_size)
2231 {
2232 	switch (get_class(*head)) {
2233 	case PKCS11_CKO_SECRET_KEY:
2234 		return set_secret_key_data(head, data, key_size);
2235 	default:
2236 		return PKCS11_CKR_GENERAL_ERROR;
2237 	}
2238 }
2239 
2240 enum pkcs11_rc get_key_data_to_wrap(struct obj_attrs *head, void **data,
2241 				    uint32_t *sz)
2242 {
2243 	switch (get_class(head)) {
2244 	case PKCS11_CKO_SECRET_KEY:
2245 		if (get_attribute_ptr(head, PKCS11_CKA_VALUE, data, sz))
2246 			return PKCS11_CKR_ARGUMENTS_BAD;
2247 		break;
2248 	default:
2249 		return PKCS11_CKR_GENERAL_ERROR;
2250 	}
2251 
2252 	return PKCS11_CKR_OK;
2253 }
2254 
2255 enum pkcs11_rc add_missing_attribute_id(struct obj_attrs **pub_head,
2256 					struct obj_attrs **priv_head)
2257 {
2258 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
2259 	void *id1 = NULL;
2260 	uint32_t id1_size = 0;
2261 	void *id2 = NULL;
2262 	uint32_t id2_size = 0;
2263 
2264 	assert(pub_head);
2265 	assert(priv_head);
2266 
2267 	rc = get_attribute_ptr(*pub_head, PKCS11_CKA_ID, &id1, &id1_size);
2268 	if (rc) {
2269 		if (rc != PKCS11_RV_NOT_FOUND)
2270 			return rc;
2271 		id1 = NULL;
2272 	} else if (!id1_size) {
2273 		id1 = NULL;
2274 	}
2275 
2276 	rc = get_attribute_ptr(*priv_head, PKCS11_CKA_ID, &id2, &id2_size);
2277 	if (rc) {
2278 		if (rc != PKCS11_RV_NOT_FOUND)
2279 			return rc;
2280 		id2 = NULL;
2281 	} else if (!id2_size) {
2282 		id2 = NULL;
2283 	}
2284 
2285 	/* Both have value -- let them be what caller has specified them */
2286 	if (id1 && id2)
2287 		return PKCS11_CKR_OK;
2288 
2289 	/* Both are empty -- leave empty values */
2290 	if (!id1 && !id2)
2291 		return PKCS11_CKR_OK;
2292 
2293 	/* Cross copy CKA_ID value */
2294 	if (id1)
2295 		return set_attribute(priv_head, PKCS11_CKA_ID, id1, id1_size);
2296 	else
2297 		return set_attribute(pub_head, PKCS11_CKA_ID, id2, id2_size);
2298 }
2299