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