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