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, 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_DERIVE: 707 case PKCS11_FUNCTION_COPY: 708 break; 709 default: 710 TEE_Panic(TEE_ERROR_NOT_SUPPORTED); 711 } 712 #endif 713 714 /* 715 * For PKCS11_FUNCTION_GENERATE, find the class and type 716 * based on the mechanism. These will be passed as hint 717 * sanitize_client_object() and added in temp if not 718 * already present 719 */ 720 if (function == PKCS11_FUNCTION_GENERATE) { 721 switch (mecha) { 722 case PKCS11_CKM_GENERIC_SECRET_KEY_GEN: 723 class = PKCS11_CKO_SECRET_KEY; 724 type = PKCS11_CKK_GENERIC_SECRET; 725 break; 726 case PKCS11_CKM_AES_KEY_GEN: 727 class = PKCS11_CKO_SECRET_KEY; 728 type = PKCS11_CKK_AES; 729 break; 730 default: 731 TEE_Panic(TEE_ERROR_NOT_SUPPORTED); 732 } 733 } 734 735 /* 736 * Check and remove duplicates if any and create a new temporary 737 * template 738 */ 739 rc = sanitize_client_object(&temp, template, template_size, class, 740 type); 741 if (rc) 742 goto out; 743 744 /* 745 * For function type modify and copy return the created template 746 * from here. Rest of the code below is for creating objects 747 * or generating keys. 748 */ 749 switch (function) { 750 case PKCS11_FUNCTION_MODIFY: 751 case PKCS11_FUNCTION_COPY: 752 *out = temp; 753 return rc; 754 default: 755 break; 756 } 757 758 /* 759 * Check if class and type in temp are consistent with the mechanism 760 */ 761 switch (mecha) { 762 case PKCS11_CKM_GENERIC_SECRET_KEY_GEN: 763 if (get_class(temp) != PKCS11_CKO_SECRET_KEY || 764 get_key_type(temp) != PKCS11_CKK_GENERIC_SECRET) { 765 rc = PKCS11_CKR_TEMPLATE_INCONSISTENT; 766 goto out; 767 } 768 break; 769 case PKCS11_CKM_AES_KEY_GEN: 770 if (get_class(temp) != PKCS11_CKO_SECRET_KEY || 771 get_key_type(temp) != PKCS11_CKK_AES) { 772 rc = PKCS11_CKR_TEMPLATE_INCONSISTENT; 773 goto out; 774 } 775 break; 776 default: 777 break; 778 } 779 780 if (!sanitize_consistent_class_and_type(temp)) { 781 EMSG("Inconsistent class/type"); 782 rc = PKCS11_CKR_TEMPLATE_INCONSISTENT; 783 goto out; 784 } 785 786 switch (get_class(temp)) { 787 case PKCS11_CKO_DATA: 788 rc = create_data_attributes(&attrs, temp); 789 break; 790 case PKCS11_CKO_SECRET_KEY: 791 rc = create_symm_key_attributes(&attrs, temp); 792 break; 793 case PKCS11_CKO_PUBLIC_KEY: 794 rc = create_pub_key_attributes(&attrs, temp); 795 break; 796 case PKCS11_CKO_PRIVATE_KEY: 797 rc = create_priv_key_attributes(&attrs, temp); 798 break; 799 default: 800 DMSG("Invalid object class %#"PRIx32"/%s", 801 get_class(temp), id2str_class(get_class(temp))); 802 803 rc = PKCS11_CKR_TEMPLATE_INCONSISTENT; 804 break; 805 } 806 if (rc) 807 goto out; 808 809 if (get_attribute_ptr(temp, PKCS11_CKA_LOCAL, NULL, NULL) != 810 PKCS11_RV_NOT_FOUND) { 811 rc = PKCS11_CKR_TEMPLATE_INCONSISTENT; 812 goto out; 813 } 814 815 if (get_attribute_ptr(temp, PKCS11_CKA_KEY_GEN_MECHANISM, NULL, NULL) != 816 PKCS11_RV_NOT_FOUND) { 817 rc = PKCS11_CKR_TEMPLATE_INCONSISTENT; 818 goto out; 819 } 820 821 switch (function) { 822 case PKCS11_FUNCTION_GENERATE: 823 local = PKCS11_TRUE; 824 break; 825 case PKCS11_FUNCTION_IMPORT: 826 case PKCS11_FUNCTION_DERIVE: 827 default: 828 local = PKCS11_FALSE; 829 break; 830 } 831 rc = add_attribute(&attrs, PKCS11_CKA_LOCAL, &local, sizeof(local)); 832 if (rc) 833 goto out; 834 835 switch (get_class(attrs)) { 836 case PKCS11_CKO_SECRET_KEY: 837 case PKCS11_CKO_PRIVATE_KEY: 838 case PKCS11_CKO_PUBLIC_KEY: 839 always_sensitive = PKCS11_FALSE; 840 never_extract = PKCS11_FALSE; 841 842 switch (function) { 843 case PKCS11_FUNCTION_DERIVE: 844 always_sensitive = 845 get_bool(parent, PKCS11_CKA_ALWAYS_SENSITIVE) && 846 get_bool(attrs, PKCS11_CKA_SENSITIVE); 847 never_extract = 848 get_bool(parent, PKCS11_CKA_NEVER_EXTRACTABLE) && 849 !get_bool(attrs, PKCS11_CKA_EXTRACTABLE); 850 break; 851 case PKCS11_FUNCTION_GENERATE: 852 always_sensitive = get_bool(attrs, 853 PKCS11_CKA_SENSITIVE); 854 never_extract = !get_bool(attrs, 855 PKCS11_CKA_EXTRACTABLE); 856 break; 857 default: 858 break; 859 } 860 861 rc = add_attribute(&attrs, PKCS11_CKA_ALWAYS_SENSITIVE, 862 &always_sensitive, sizeof(always_sensitive)); 863 if (rc) 864 goto out; 865 866 rc = add_attribute(&attrs, PKCS11_CKA_NEVER_EXTRACTABLE, 867 &never_extract, sizeof(never_extract)); 868 if (rc) 869 goto out; 870 871 /* Keys mandate attribute PKCS11_CKA_KEY_GEN_MECHANISM */ 872 if (local) 873 mechanism_id = mecha; 874 else 875 mechanism_id = PKCS11_CK_UNAVAILABLE_INFORMATION; 876 877 rc = add_attribute(&attrs, PKCS11_CKA_KEY_GEN_MECHANISM, 878 &mechanism_id, sizeof(mechanism_id)); 879 if (rc) 880 goto out; 881 break; 882 883 default: 884 break; 885 } 886 887 *out = attrs; 888 889 #ifdef DEBUG 890 trace_attributes("object", attrs); 891 #endif 892 893 out: 894 TEE_Free(temp); 895 if (rc) 896 TEE_Free(attrs); 897 898 return rc; 899 } 900 901 static enum pkcs11_rc check_attrs_misc_integrity(struct obj_attrs *head) 902 { 903 if (get_bool(head, PKCS11_CKA_NEVER_EXTRACTABLE) && 904 get_bool(head, PKCS11_CKA_EXTRACTABLE)) { 905 DMSG("Never/Extractable attributes mismatch %d/%d", 906 get_bool(head, PKCS11_CKA_NEVER_EXTRACTABLE), 907 get_bool(head, PKCS11_CKA_EXTRACTABLE)); 908 909 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 910 } 911 912 if (get_bool(head, PKCS11_CKA_ALWAYS_SENSITIVE) && 913 !get_bool(head, PKCS11_CKA_SENSITIVE)) { 914 DMSG("Sensitive/always attributes mismatch %d/%d", 915 get_bool(head, PKCS11_CKA_SENSITIVE), 916 get_bool(head, PKCS11_CKA_ALWAYS_SENSITIVE)); 917 918 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 919 } 920 921 return PKCS11_CKR_OK; 922 } 923 924 bool object_is_private(struct obj_attrs *head) 925 { 926 return get_bool(head, PKCS11_CKA_PRIVATE); 927 } 928 929 bool object_is_token(struct obj_attrs *head) 930 { 931 return get_bool(head, PKCS11_CKA_TOKEN); 932 } 933 934 bool object_is_modifiable(struct obj_attrs *head) 935 { 936 return get_bool(head, PKCS11_CKA_MODIFIABLE); 937 } 938 939 bool object_is_copyable(struct obj_attrs *head) 940 { 941 return get_bool(head, PKCS11_CKA_COPYABLE); 942 } 943 944 /* 945 * Check access to object against authentication to token 946 */ 947 enum pkcs11_rc check_access_attrs_against_token(struct pkcs11_session *session, 948 struct obj_attrs *head) 949 { 950 bool private = true; 951 952 switch (get_class(head)) { 953 case PKCS11_CKO_SECRET_KEY: 954 case PKCS11_CKO_PRIVATE_KEY: 955 case PKCS11_CKO_PUBLIC_KEY: 956 case PKCS11_CKO_DATA: 957 private = object_is_private(head); 958 break; 959 default: 960 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 961 } 962 963 if (private && (pkcs11_session_is_public(session) || 964 pkcs11_session_is_so(session))) { 965 DMSG("Private object access from a public or SO session"); 966 967 return PKCS11_CKR_USER_NOT_LOGGED_IN; 968 } 969 970 return PKCS11_CKR_OK; 971 } 972 973 /* 974 * Check the attributes of a to-be-created object matches the token state 975 */ 976 enum pkcs11_rc check_created_attrs_against_token(struct pkcs11_session *session, 977 struct obj_attrs *head) 978 { 979 enum pkcs11_rc rc = PKCS11_CKR_OK; 980 981 rc = check_attrs_misc_integrity(head); 982 if (rc) 983 return rc; 984 985 if (get_bool(head, PKCS11_CKA_TRUSTED) && 986 !pkcs11_session_is_so(session)) { 987 DMSG("Can't create trusted object"); 988 989 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 990 } 991 992 if (get_bool(head, PKCS11_CKA_TOKEN) && 993 !pkcs11_session_is_read_write(session)) { 994 DMSG("Can't create persistent object"); 995 996 return PKCS11_CKR_SESSION_READ_ONLY; 997 } 998 999 /* 1000 * TODO: START_DATE and END_DATE: complies with current time? 1001 */ 1002 return PKCS11_CKR_OK; 1003 } 1004 1005 #define DMSG_BAD_BBOOL(attr, proc, head) \ 1006 do { \ 1007 uint32_t __maybe_unused _attr = (attr); \ 1008 uint8_t __maybe_unused _bvalue = 0; \ 1009 enum pkcs11_rc __maybe_unused _rc = PKCS11_CKR_OK; \ 1010 \ 1011 _rc = get_attribute((head), _attr, &_bvalue, NULL); \ 1012 DMSG("%s issue for %s: %sfound, value %"PRIu8, \ 1013 id2str_attr(_attr), id2str_proc((proc)), \ 1014 _rc ? "not " : "", _bvalue); \ 1015 } while (0) 1016 1017 static bool __maybe_unused check_attr_bval(uint32_t proc_id __maybe_unused, 1018 struct obj_attrs *head, 1019 uint32_t attribute, bool val) 1020 { 1021 uint8_t bbool = 0; 1022 uint32_t sz = sizeof(bbool); 1023 1024 if (!get_attribute(head, attribute, &bbool, &sz) && !!bbool == val) 1025 return true; 1026 1027 DMSG_BAD_BBOOL(attribute, proc_id, head); 1028 return false; 1029 } 1030 1031 /* 1032 * Check the attributes of a new secret match the processing/mechanism 1033 * used to create it. 1034 * 1035 * @proc_id - PKCS11_CKM_xxx 1036 * @head - head of the attributes of the to-be-created object. 1037 */ 1038 enum pkcs11_rc check_created_attrs_against_processing(uint32_t proc_id, 1039 struct obj_attrs *head) 1040 { 1041 /* 1042 * Processings that do not create secrets are not expected to call 1043 * this function which would panic. 1044 */ 1045 switch (proc_id) { 1046 case PKCS11_PROCESSING_IMPORT: 1047 case PKCS11_CKM_AES_ECB_ENCRYPT_DATA: 1048 case PKCS11_CKM_AES_CBC_ENCRYPT_DATA: 1049 assert(check_attr_bval(proc_id, head, PKCS11_CKA_LOCAL, false)); 1050 break; 1051 case PKCS11_CKM_GENERIC_SECRET_KEY_GEN: 1052 case PKCS11_CKM_AES_KEY_GEN: 1053 assert(check_attr_bval(proc_id, head, PKCS11_CKA_LOCAL, true)); 1054 break; 1055 default: 1056 TEE_Panic(proc_id); 1057 break; 1058 } 1059 1060 switch (proc_id) { 1061 case PKCS11_CKM_GENERIC_SECRET_KEY_GEN: 1062 assert(get_key_type(head) == PKCS11_CKK_GENERIC_SECRET); 1063 break; 1064 case PKCS11_CKM_AES_KEY_GEN: 1065 assert(get_key_type(head) == PKCS11_CKK_AES); 1066 break; 1067 case PKCS11_PROCESSING_IMPORT: 1068 default: 1069 break; 1070 } 1071 1072 return PKCS11_CKR_OK; 1073 } 1074 1075 /* Return min and max key size supported for a key_type in bytes */ 1076 static void get_key_min_max_sizes(enum pkcs11_key_type key_type, 1077 uint32_t *min_key_size, 1078 uint32_t *max_key_size) 1079 { 1080 enum pkcs11_mechanism_id mechanism = PKCS11_CKM_UNDEFINED_ID; 1081 1082 switch (key_type) { 1083 case PKCS11_CKK_GENERIC_SECRET: 1084 mechanism = PKCS11_CKM_GENERIC_SECRET_KEY_GEN; 1085 break; 1086 case PKCS11_CKK_AES: 1087 mechanism = PKCS11_CKM_AES_KEY_GEN; 1088 break; 1089 case PKCS11_CKK_MD5_HMAC: 1090 mechanism = PKCS11_CKM_MD5_HMAC; 1091 break; 1092 case PKCS11_CKK_SHA_1_HMAC: 1093 mechanism = PKCS11_CKM_SHA_1_HMAC; 1094 break; 1095 case PKCS11_CKK_SHA224_HMAC: 1096 mechanism = PKCS11_CKM_SHA224_HMAC; 1097 break; 1098 case PKCS11_CKK_SHA256_HMAC: 1099 mechanism = PKCS11_CKM_SHA256_HMAC; 1100 break; 1101 case PKCS11_CKK_SHA384_HMAC: 1102 mechanism = PKCS11_CKM_SHA384_HMAC; 1103 break; 1104 case PKCS11_CKK_SHA512_HMAC: 1105 mechanism = PKCS11_CKM_SHA512_HMAC; 1106 break; 1107 default: 1108 TEE_Panic(key_type); 1109 break; 1110 } 1111 1112 mechanism_supported_key_sizes_bytes(mechanism, min_key_size, 1113 max_key_size); 1114 } 1115 1116 enum pkcs11_rc check_created_attrs(struct obj_attrs *key1, 1117 struct obj_attrs *key2) 1118 { 1119 enum pkcs11_rc rc = PKCS11_CKR_OK; 1120 struct obj_attrs *secret = NULL; 1121 uint32_t max_key_size = 0; 1122 uint32_t min_key_size = 0; 1123 uint32_t key_length = 0; 1124 1125 switch (get_class(key1)) { 1126 case PKCS11_CKO_SECRET_KEY: 1127 secret = key1; 1128 break; 1129 default: 1130 return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID; 1131 } 1132 1133 if (key2) 1134 return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID; 1135 1136 if (secret) { 1137 switch (get_key_type(secret)) { 1138 case PKCS11_CKK_AES: 1139 case PKCS11_CKK_GENERIC_SECRET: 1140 case PKCS11_CKK_MD5_HMAC: 1141 case PKCS11_CKK_SHA_1_HMAC: 1142 case PKCS11_CKK_SHA224_HMAC: 1143 case PKCS11_CKK_SHA256_HMAC: 1144 case PKCS11_CKK_SHA384_HMAC: 1145 case PKCS11_CKK_SHA512_HMAC: 1146 break; 1147 default: 1148 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 1149 } 1150 1151 /* Get key size */ 1152 rc = get_u32_attribute(secret, PKCS11_CKA_VALUE_LEN, 1153 &key_length); 1154 if (rc) 1155 return PKCS11_CKR_TEMPLATE_INCOMPLETE; 1156 } 1157 1158 get_key_min_max_sizes(get_key_type(key1), &min_key_size, &max_key_size); 1159 if (key_length < min_key_size || key_length > max_key_size) { 1160 EMSG("Length %"PRIu32" vs range [%"PRIu32" %"PRIu32"]", 1161 key_length, min_key_size, max_key_size); 1162 1163 return PKCS11_CKR_KEY_SIZE_RANGE; 1164 } 1165 1166 if (secret && get_key_type(secret) == PKCS11_CKK_AES) { 1167 if (key_length != 16 && key_length != 24 && key_length != 32) 1168 return PKCS11_CKR_KEY_SIZE_RANGE; 1169 } 1170 1171 return PKCS11_CKR_OK; 1172 } 1173 1174 /* Check processing ID against attribute ALLOWED_MECHANISMS if any */ 1175 static bool parent_key_complies_allowed_processings(uint32_t proc_id, 1176 struct obj_attrs *head) 1177 { 1178 char *attr = NULL; 1179 uint32_t size = 0; 1180 uint32_t proc = 0; 1181 size_t count = 0; 1182 enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR; 1183 1184 rc = get_attribute_ptr(head, PKCS11_CKA_ALLOWED_MECHANISMS, 1185 (void *)&attr, &size); 1186 if (rc == PKCS11_RV_NOT_FOUND) 1187 return true; 1188 if (rc) { 1189 EMSG("unexpected attributes state"); 1190 TEE_Panic(TEE_ERROR_BAD_STATE); 1191 } 1192 1193 for (count = size / sizeof(uint32_t); count; count--) { 1194 TEE_MemMove(&proc, attr, sizeof(uint32_t)); 1195 attr += sizeof(uint32_t); 1196 1197 if (proc == proc_id) 1198 return true; 1199 } 1200 1201 DMSG("can't find %s in allowed list", id2str_proc(proc_id)); 1202 return false; 1203 } 1204 1205 static enum pkcs11_attr_id func_to_attr(enum processing_func func) 1206 { 1207 switch (func) { 1208 case PKCS11_FUNCTION_ENCRYPT: 1209 return PKCS11_CKA_ENCRYPT; 1210 case PKCS11_FUNCTION_DECRYPT: 1211 return PKCS11_CKA_DECRYPT; 1212 case PKCS11_FUNCTION_SIGN: 1213 return PKCS11_CKA_SIGN; 1214 case PKCS11_FUNCTION_VERIFY: 1215 return PKCS11_CKA_VERIFY; 1216 case PKCS11_FUNCTION_WRAP: 1217 return PKCS11_CKA_WRAP; 1218 case PKCS11_FUNCTION_UNWRAP: 1219 return PKCS11_CKA_UNWRAP; 1220 case PKCS11_FUNCTION_DERIVE: 1221 return PKCS11_CKA_DERIVE; 1222 default: 1223 return PKCS11_CKA_UNDEFINED_ID; 1224 } 1225 } 1226 1227 enum pkcs11_rc 1228 check_parent_attrs_against_processing(enum pkcs11_mechanism_id proc_id, 1229 enum processing_func function, 1230 struct obj_attrs *head) 1231 { 1232 enum pkcs11_class_id key_class = get_class(head); 1233 enum pkcs11_key_type key_type = get_key_type(head); 1234 enum pkcs11_attr_id attr = func_to_attr(function); 1235 1236 if (!get_bool(head, attr)) { 1237 DMSG("%s not permitted", id2str_attr(attr)); 1238 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1239 } 1240 1241 /* Check processing complies with parent key family */ 1242 switch (proc_id) { 1243 case PKCS11_CKM_AES_ECB: 1244 case PKCS11_CKM_AES_CBC: 1245 case PKCS11_CKM_AES_CBC_PAD: 1246 case PKCS11_CKM_AES_CTS: 1247 case PKCS11_CKM_AES_CTR: 1248 case PKCS11_CKM_AES_ECB_ENCRYPT_DATA: 1249 case PKCS11_CKM_AES_CBC_ENCRYPT_DATA: 1250 if (key_class == PKCS11_CKO_SECRET_KEY && 1251 key_type == PKCS11_CKK_AES) 1252 break; 1253 1254 DMSG("%s invalid key %s/%s", id2str_proc(proc_id), 1255 id2str_class(key_class), id2str_key_type(key_type)); 1256 1257 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1258 1259 case PKCS11_CKM_MD5_HMAC: 1260 case PKCS11_CKM_SHA_1_HMAC: 1261 case PKCS11_CKM_SHA224_HMAC: 1262 case PKCS11_CKM_SHA256_HMAC: 1263 case PKCS11_CKM_SHA384_HMAC: 1264 case PKCS11_CKM_SHA512_HMAC: 1265 if (key_class != PKCS11_CKO_SECRET_KEY) 1266 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1267 1268 if (key_type == PKCS11_CKK_GENERIC_SECRET) 1269 break; 1270 1271 switch (proc_id) { 1272 case PKCS11_CKM_MD5_HMAC: 1273 if (key_type == PKCS11_CKK_MD5_HMAC) 1274 break; 1275 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1276 case PKCS11_CKM_SHA_1_HMAC: 1277 if (key_type == PKCS11_CKK_SHA_1_HMAC) 1278 break; 1279 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1280 case PKCS11_CKM_SHA224_HMAC: 1281 if (key_type == PKCS11_CKK_SHA224_HMAC) 1282 break; 1283 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1284 case PKCS11_CKM_SHA256_HMAC: 1285 if (key_type == PKCS11_CKK_SHA256_HMAC) 1286 break; 1287 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1288 case PKCS11_CKM_SHA384_HMAC: 1289 if (key_type == PKCS11_CKK_SHA384_HMAC) 1290 break; 1291 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1292 case PKCS11_CKM_SHA512_HMAC: 1293 if (key_type == PKCS11_CKK_SHA512_HMAC) 1294 break; 1295 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1296 default: 1297 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1298 } 1299 break; 1300 1301 default: 1302 DMSG("Invalid processing %#"PRIx32"/%s", proc_id, 1303 id2str_proc(proc_id)); 1304 1305 return PKCS11_CKR_MECHANISM_INVALID; 1306 } 1307 1308 if (!parent_key_complies_allowed_processings(proc_id, head)) { 1309 DMSG("Allowed mechanism failed"); 1310 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1311 } 1312 1313 return PKCS11_CKR_OK; 1314 } 1315 1316 bool attribute_is_exportable(struct pkcs11_attribute_head *req_attr, 1317 struct pkcs11_object *obj) 1318 { 1319 uint8_t boolval = 0; 1320 uint32_t boolsize = 0; 1321 enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR; 1322 enum pkcs11_class_id key_class = get_class(obj->attributes); 1323 1324 if (key_class != PKCS11_CKO_SECRET_KEY && 1325 key_class != PKCS11_CKO_PRIVATE_KEY) 1326 return true; 1327 1328 switch (req_attr->id) { 1329 case PKCS11_CKA_PRIVATE_EXPONENT: 1330 case PKCS11_CKA_PRIME_1: 1331 case PKCS11_CKA_PRIME_2: 1332 case PKCS11_CKA_EXPONENT_1: 1333 case PKCS11_CKA_EXPONENT_2: 1334 case PKCS11_CKA_COEFFICIENT: 1335 case PKCS11_CKA_VALUE: 1336 boolsize = sizeof(boolval); 1337 rc = get_attribute(obj->attributes, PKCS11_CKA_EXTRACTABLE, 1338 &boolval, &boolsize); 1339 if (rc || boolval == PKCS11_FALSE) 1340 return false; 1341 1342 boolsize = sizeof(boolval); 1343 rc = get_attribute(obj->attributes, PKCS11_CKA_SENSITIVE, 1344 &boolval, &boolsize); 1345 if (rc || boolval == PKCS11_TRUE) 1346 return false; 1347 break; 1348 default: 1349 break; 1350 } 1351 1352 return true; 1353 } 1354 1355 static bool attr_is_modifiable_any_key(struct pkcs11_attribute_head *attr) 1356 { 1357 switch (attr->id) { 1358 case PKCS11_CKA_ID: 1359 case PKCS11_CKA_START_DATE: 1360 case PKCS11_CKA_END_DATE: 1361 case PKCS11_CKA_DERIVE: 1362 return true; 1363 default: 1364 return false; 1365 } 1366 } 1367 1368 static bool attr_is_modifiable_secret_key(struct pkcs11_attribute_head *attr, 1369 struct pkcs11_session *session, 1370 struct pkcs11_object *obj) 1371 { 1372 switch (attr->id) { 1373 case PKCS11_CKA_ENCRYPT: 1374 case PKCS11_CKA_DECRYPT: 1375 case PKCS11_CKA_SIGN: 1376 case PKCS11_CKA_VERIFY: 1377 case PKCS11_CKA_WRAP: 1378 case PKCS11_CKA_UNWRAP: 1379 return true; 1380 /* Can't be modified once set to CK_FALSE - 12 in Table 10 */ 1381 case PKCS11_CKA_EXTRACTABLE: 1382 return get_bool(obj->attributes, attr->id); 1383 /* Can't be modified once set to CK_TRUE - 11 in Table 10 */ 1384 case PKCS11_CKA_SENSITIVE: 1385 case PKCS11_CKA_WRAP_WITH_TRUSTED: 1386 return !get_bool(obj->attributes, attr->id); 1387 /* Change in CKA_TRUSTED can only be done by SO */ 1388 case PKCS11_CKA_TRUSTED: 1389 return pkcs11_session_is_so(session); 1390 case PKCS11_CKA_NEVER_EXTRACTABLE: 1391 case PKCS11_CKA_ALWAYS_SENSITIVE: 1392 return false; 1393 default: 1394 return false; 1395 } 1396 } 1397 1398 static bool attr_is_modifiable_public_key(struct pkcs11_attribute_head *attr, 1399 struct pkcs11_session *session, 1400 struct pkcs11_object *obj __unused) 1401 { 1402 switch (attr->id) { 1403 case PKCS11_CKA_SUBJECT: 1404 case PKCS11_CKA_ENCRYPT: 1405 case PKCS11_CKA_VERIFY: 1406 case PKCS11_CKA_VERIFY_RECOVER: 1407 case PKCS11_CKA_WRAP: 1408 return true; 1409 case PKCS11_CKA_TRUSTED: 1410 /* Change in CKA_TRUSTED can only be done by SO */ 1411 return pkcs11_session_is_so(session); 1412 default: 1413 return false; 1414 } 1415 } 1416 1417 static bool attr_is_modifiable_private_key(struct pkcs11_attribute_head *attr, 1418 struct pkcs11_session *sess __unused, 1419 struct pkcs11_object *obj) 1420 { 1421 switch (attr->id) { 1422 case PKCS11_CKA_SUBJECT: 1423 case PKCS11_CKA_DECRYPT: 1424 case PKCS11_CKA_SIGN: 1425 case PKCS11_CKA_SIGN_RECOVER: 1426 case PKCS11_CKA_UNWRAP: 1427 /* 1428 * TBD: Revisit if we don't support PKCS11_CKA_PUBLIC_KEY_INFO 1429 * Specification mentions that if this attribute is 1430 * supplied as part of a template for C_CreateObject, C_CopyObject or 1431 * C_SetAttributeValue for a private key, the token MUST verify 1432 * correspondence between the private key data and the public key data 1433 * as supplied in CKA_PUBLIC_KEY_INFO. This needs to be 1434 * taken care of when this object type will be implemented 1435 */ 1436 case PKCS11_CKA_PUBLIC_KEY_INFO: 1437 return true; 1438 /* Can't be modified once set to CK_FALSE - 12 in Table 10 */ 1439 case PKCS11_CKA_EXTRACTABLE: 1440 return get_bool(obj->attributes, attr->id); 1441 /* Can't be modified once set to CK_TRUE - 11 in Table 10 */ 1442 case PKCS11_CKA_SENSITIVE: 1443 case PKCS11_CKA_WRAP_WITH_TRUSTED: 1444 return !get_bool(obj->attributes, attr->id); 1445 case PKCS11_CKA_NEVER_EXTRACTABLE: 1446 case PKCS11_CKA_ALWAYS_SENSITIVE: 1447 return false; 1448 default: 1449 return false; 1450 } 1451 } 1452 1453 static bool attribute_is_modifiable(struct pkcs11_session *session, 1454 struct pkcs11_attribute_head *req_attr, 1455 struct pkcs11_object *obj, 1456 enum pkcs11_class_id class, 1457 enum processing_func function) 1458 { 1459 /* Check modifiable attributes common to any object */ 1460 switch (req_attr->id) { 1461 case PKCS11_CKA_LABEL: 1462 return true; 1463 case PKCS11_CKA_TOKEN: 1464 case PKCS11_CKA_MODIFIABLE: 1465 case PKCS11_CKA_DESTROYABLE: 1466 case PKCS11_CKA_PRIVATE: 1467 return function == PKCS11_FUNCTION_COPY; 1468 case PKCS11_CKA_COPYABLE: 1469 /* 1470 * Specification mentions that if the attribute value is false 1471 * it can't be set to true. Reading this we assume that it 1472 * should be possible to modify this attribute even though this 1473 * is not marked as modifiable in Table 10 if done in right 1474 * direction i.e from TRUE -> FALSE. 1475 */ 1476 return get_bool(obj->attributes, req_attr->id); 1477 default: 1478 break; 1479 } 1480 1481 /* Attribute checking based on class type */ 1482 switch (class) { 1483 case PKCS11_CKO_SECRET_KEY: 1484 case PKCS11_CKO_PUBLIC_KEY: 1485 case PKCS11_CKO_PRIVATE_KEY: 1486 if (attr_is_modifiable_any_key(req_attr)) 1487 return true; 1488 if (class == PKCS11_CKO_SECRET_KEY && 1489 attr_is_modifiable_secret_key(req_attr, session, obj)) 1490 return true; 1491 if (class == PKCS11_CKO_PUBLIC_KEY && 1492 attr_is_modifiable_public_key(req_attr, session, obj)) 1493 return true; 1494 if (class == PKCS11_CKO_PRIVATE_KEY && 1495 attr_is_modifiable_private_key(req_attr, session, obj)) 1496 return true; 1497 break; 1498 case PKCS11_CKO_DATA: 1499 /* None of the data object attributes are modifiable */ 1500 return false; 1501 default: 1502 break; 1503 } 1504 1505 return false; 1506 } 1507 1508 enum pkcs11_rc check_attrs_against_modification(struct pkcs11_session *session, 1509 struct obj_attrs *head, 1510 struct pkcs11_object *obj, 1511 enum processing_func function) 1512 { 1513 enum pkcs11_class_id class = PKCS11_CKO_UNDEFINED_ID; 1514 char *cur = NULL; 1515 char *end = NULL; 1516 size_t len = 0; 1517 1518 class = get_class(obj->attributes); 1519 1520 cur = (char *)head + sizeof(struct obj_attrs); 1521 end = cur + head->attrs_size; 1522 1523 for (; cur < end; cur += len) { 1524 /* Structure aligned copy of the pkcs11_ref in the object */ 1525 struct pkcs11_attribute_head cli_ref = { }; 1526 1527 TEE_MemMove(&cli_ref, cur, sizeof(cli_ref)); 1528 len = sizeof(cli_ref) + cli_ref.size; 1529 1530 /* 1531 * Check 1 - Check if attribute belongs to the object 1532 * The obj->attributes has all the attributes in 1533 * it which are allowed for an object. 1534 */ 1535 if (get_attribute_ptr(obj->attributes, cli_ref.id, NULL, 1536 NULL) == PKCS11_RV_NOT_FOUND) 1537 return PKCS11_CKR_ATTRIBUTE_TYPE_INVALID; 1538 1539 /* Check 2 - Is attribute modifiable */ 1540 if (!attribute_is_modifiable(session, &cli_ref, obj, class, 1541 function)) 1542 return PKCS11_CKR_ATTRIBUTE_READ_ONLY; 1543 1544 /* 1545 * Checks for modification in PKCS11_CKA_TOKEN and 1546 * PKCS11_CKA_PRIVATE are required for PKCS11_FUNCTION_COPY 1547 * only, so skip them for PKCS11_FUNCTION_MODIFY. 1548 */ 1549 if (function == PKCS11_FUNCTION_MODIFY) 1550 continue; 1551 1552 /* 1553 * An attempt to copy an object to a token will fail for 1554 * RO session 1555 */ 1556 if (cli_ref.id == PKCS11_CKA_TOKEN && 1557 get_bool(head, PKCS11_CKA_TOKEN)) { 1558 if (!pkcs11_session_is_read_write(session)) { 1559 DMSG("Can't copy to token in a RO session"); 1560 return PKCS11_CKR_SESSION_READ_ONLY; 1561 } 1562 } 1563 1564 if (cli_ref.id == PKCS11_CKA_PRIVATE) { 1565 bool parent_priv = 1566 get_bool(obj->attributes, cli_ref.id); 1567 bool obj_priv = get_bool(head, cli_ref.id); 1568 1569 /* 1570 * If PKCS11_CKA_PRIVATE is being set to TRUE from 1571 * FALSE, user has to be logged in 1572 */ 1573 if (!parent_priv && obj_priv) { 1574 if ((pkcs11_session_is_public(session) || 1575 pkcs11_session_is_so(session))) 1576 return PKCS11_CKR_USER_NOT_LOGGED_IN; 1577 } 1578 1579 /* 1580 * Restriction added - Even for Copy, do not allow 1581 * modification of CKA_PRIVATE from TRUE to FALSE 1582 */ 1583 if (parent_priv && !obj_priv) 1584 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 1585 } 1586 } 1587 1588 return PKCS11_CKR_OK; 1589 } 1590