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