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