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