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 return get_bool(head, PKCS11_CKA_PRIVATE); 917 } 918 919 bool object_is_token(struct obj_attrs *head) 920 { 921 return get_bool(head, PKCS11_CKA_TOKEN); 922 } 923 924 bool object_is_modifiable(struct obj_attrs *head) 925 { 926 return get_bool(head, PKCS11_CKA_MODIFIABLE); 927 } 928 929 bool object_is_copyable(struct obj_attrs *head) 930 { 931 return get_bool(head, PKCS11_CKA_COPYABLE); 932 } 933 934 /* 935 * Check access to object against authentication to token 936 */ 937 enum pkcs11_rc check_access_attrs_against_token(struct pkcs11_session *session, 938 struct obj_attrs *head) 939 { 940 bool private = true; 941 942 switch (get_class(head)) { 943 case PKCS11_CKO_SECRET_KEY: 944 case PKCS11_CKO_PRIVATE_KEY: 945 case PKCS11_CKO_PUBLIC_KEY: 946 case PKCS11_CKO_DATA: 947 private = object_is_private(head); 948 break; 949 default: 950 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 951 } 952 953 if (private && (pkcs11_session_is_public(session) || 954 pkcs11_session_is_so(session))) { 955 DMSG("Private object access from a public or SO session"); 956 957 return PKCS11_CKR_USER_NOT_LOGGED_IN; 958 } 959 960 return PKCS11_CKR_OK; 961 } 962 963 /* 964 * Check the attributes of a to-be-created object matches the token state 965 */ 966 enum pkcs11_rc check_created_attrs_against_token(struct pkcs11_session *session, 967 struct obj_attrs *head) 968 { 969 enum pkcs11_rc rc = PKCS11_CKR_OK; 970 971 rc = check_attrs_misc_integrity(head); 972 if (rc) 973 return rc; 974 975 if (get_bool(head, PKCS11_CKA_TRUSTED) && 976 !pkcs11_session_is_so(session)) { 977 DMSG("Can't create trusted object"); 978 979 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 980 } 981 982 if (get_bool(head, PKCS11_CKA_TOKEN) && 983 !pkcs11_session_is_read_write(session)) { 984 DMSG("Can't create persistent object"); 985 986 return PKCS11_CKR_SESSION_READ_ONLY; 987 } 988 989 /* 990 * TODO: START_DATE and END_DATE: complies with current time? 991 */ 992 return PKCS11_CKR_OK; 993 } 994 995 #define DMSG_BAD_BBOOL(attr, proc, head) \ 996 do { \ 997 uint32_t __maybe_unused _attr = (attr); \ 998 uint8_t __maybe_unused _bvalue = 0; \ 999 enum pkcs11_rc __maybe_unused _rc = PKCS11_CKR_OK; \ 1000 \ 1001 _rc = get_attribute((head), _attr, &_bvalue, NULL); \ 1002 DMSG("%s issue for %s: %sfound, value %"PRIu8, \ 1003 id2str_attr(_attr), id2str_proc((proc)), \ 1004 _rc ? "not " : "", _bvalue); \ 1005 } while (0) 1006 1007 static bool __maybe_unused check_attr_bval(uint32_t proc_id __maybe_unused, 1008 struct obj_attrs *head, 1009 uint32_t attribute, bool val) 1010 { 1011 uint8_t bbool = 0; 1012 uint32_t sz = sizeof(bbool); 1013 1014 if (!get_attribute(head, attribute, &bbool, &sz) && !!bbool == val) 1015 return true; 1016 1017 DMSG_BAD_BBOOL(attribute, proc_id, head); 1018 return false; 1019 } 1020 1021 /* 1022 * Check the attributes of a new secret match the processing/mechanism 1023 * used to create it. 1024 * 1025 * @proc_id - PKCS11_CKM_xxx 1026 * @head - head of the attributes of the to-be-created object. 1027 */ 1028 enum pkcs11_rc check_created_attrs_against_processing(uint32_t proc_id, 1029 struct obj_attrs *head) 1030 { 1031 /* 1032 * Processings that do not create secrets are not expected to call 1033 * this function which would panic. 1034 */ 1035 switch (proc_id) { 1036 case PKCS11_PROCESSING_IMPORT: 1037 assert(check_attr_bval(proc_id, head, PKCS11_CKA_LOCAL, false)); 1038 break; 1039 case PKCS11_CKM_GENERIC_SECRET_KEY_GEN: 1040 case PKCS11_CKM_AES_KEY_GEN: 1041 assert(check_attr_bval(proc_id, head, PKCS11_CKA_LOCAL, true)); 1042 break; 1043 default: 1044 TEE_Panic(proc_id); 1045 break; 1046 } 1047 1048 switch (proc_id) { 1049 case PKCS11_CKM_GENERIC_SECRET_KEY_GEN: 1050 assert(get_key_type(head) == PKCS11_CKK_GENERIC_SECRET); 1051 break; 1052 case PKCS11_CKM_AES_KEY_GEN: 1053 assert(get_key_type(head) == PKCS11_CKK_AES); 1054 break; 1055 case PKCS11_PROCESSING_IMPORT: 1056 default: 1057 break; 1058 } 1059 1060 return PKCS11_CKR_OK; 1061 } 1062 1063 /* Return min and max key size supported for a key_type in bytes */ 1064 static void get_key_min_max_sizes(enum pkcs11_key_type key_type, 1065 uint32_t *min_key_size, 1066 uint32_t *max_key_size) 1067 { 1068 enum pkcs11_mechanism_id mechanism = PKCS11_CKM_UNDEFINED_ID; 1069 1070 switch (key_type) { 1071 case PKCS11_CKK_GENERIC_SECRET: 1072 mechanism = PKCS11_CKM_GENERIC_SECRET_KEY_GEN; 1073 break; 1074 case PKCS11_CKK_AES: 1075 mechanism = PKCS11_CKM_AES_KEY_GEN; 1076 break; 1077 case PKCS11_CKK_MD5_HMAC: 1078 mechanism = PKCS11_CKM_MD5_HMAC; 1079 break; 1080 case PKCS11_CKK_SHA_1_HMAC: 1081 mechanism = PKCS11_CKM_SHA_1_HMAC; 1082 break; 1083 case PKCS11_CKK_SHA224_HMAC: 1084 mechanism = PKCS11_CKM_SHA224_HMAC; 1085 break; 1086 case PKCS11_CKK_SHA256_HMAC: 1087 mechanism = PKCS11_CKM_SHA256_HMAC; 1088 break; 1089 case PKCS11_CKK_SHA384_HMAC: 1090 mechanism = PKCS11_CKM_SHA384_HMAC; 1091 break; 1092 case PKCS11_CKK_SHA512_HMAC: 1093 mechanism = PKCS11_CKM_SHA512_HMAC; 1094 break; 1095 default: 1096 TEE_Panic(key_type); 1097 break; 1098 } 1099 1100 mechanism_supported_key_sizes_bytes(mechanism, min_key_size, 1101 max_key_size); 1102 } 1103 1104 enum pkcs11_rc check_created_attrs(struct obj_attrs *key1, 1105 struct obj_attrs *key2) 1106 { 1107 enum pkcs11_rc rc = PKCS11_CKR_OK; 1108 struct obj_attrs *secret = NULL; 1109 uint32_t max_key_size = 0; 1110 uint32_t min_key_size = 0; 1111 uint32_t key_length = 0; 1112 1113 switch (get_class(key1)) { 1114 case PKCS11_CKO_SECRET_KEY: 1115 secret = key1; 1116 break; 1117 default: 1118 return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID; 1119 } 1120 1121 if (key2) 1122 return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID; 1123 1124 if (secret) { 1125 switch (get_key_type(secret)) { 1126 case PKCS11_CKK_AES: 1127 case PKCS11_CKK_GENERIC_SECRET: 1128 case PKCS11_CKK_MD5_HMAC: 1129 case PKCS11_CKK_SHA_1_HMAC: 1130 case PKCS11_CKK_SHA224_HMAC: 1131 case PKCS11_CKK_SHA256_HMAC: 1132 case PKCS11_CKK_SHA384_HMAC: 1133 case PKCS11_CKK_SHA512_HMAC: 1134 break; 1135 default: 1136 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 1137 } 1138 1139 /* Get key size */ 1140 rc = get_u32_attribute(secret, PKCS11_CKA_VALUE_LEN, 1141 &key_length); 1142 if (rc) 1143 return PKCS11_CKR_TEMPLATE_INCOMPLETE; 1144 } 1145 1146 get_key_min_max_sizes(get_key_type(key1), &min_key_size, &max_key_size); 1147 if (key_length < min_key_size || key_length > max_key_size) { 1148 EMSG("Length %"PRIu32" vs range [%"PRIu32" %"PRIu32"]", 1149 key_length, min_key_size, max_key_size); 1150 1151 return PKCS11_CKR_KEY_SIZE_RANGE; 1152 } 1153 1154 if (secret && get_key_type(secret) == PKCS11_CKK_AES) { 1155 if (key_length != 16 && key_length != 24 && key_length != 32) 1156 return PKCS11_CKR_KEY_SIZE_RANGE; 1157 } 1158 1159 return PKCS11_CKR_OK; 1160 } 1161 1162 /* Check processing ID against attribute ALLOWED_MECHANISMS if any */ 1163 static bool parent_key_complies_allowed_processings(uint32_t proc_id, 1164 struct obj_attrs *head) 1165 { 1166 char *attr = NULL; 1167 uint32_t size = 0; 1168 uint32_t proc = 0; 1169 size_t count = 0; 1170 enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR; 1171 1172 rc = get_attribute_ptr(head, PKCS11_CKA_ALLOWED_MECHANISMS, 1173 (void *)&attr, &size); 1174 if (rc == PKCS11_RV_NOT_FOUND) 1175 return true; 1176 if (rc) { 1177 EMSG("unexpected attributes state"); 1178 TEE_Panic(TEE_ERROR_BAD_STATE); 1179 } 1180 1181 for (count = size / sizeof(uint32_t); count; count--) { 1182 TEE_MemMove(&proc, attr, sizeof(uint32_t)); 1183 attr += sizeof(uint32_t); 1184 1185 if (proc == proc_id) 1186 return true; 1187 } 1188 1189 DMSG("can't find %s in allowed list", id2str_proc(proc_id)); 1190 return false; 1191 } 1192 1193 static enum pkcs11_attr_id func_to_attr(enum processing_func func) 1194 { 1195 switch (func) { 1196 case PKCS11_FUNCTION_ENCRYPT: 1197 return PKCS11_CKA_ENCRYPT; 1198 case PKCS11_FUNCTION_DECRYPT: 1199 return PKCS11_CKA_DECRYPT; 1200 case PKCS11_FUNCTION_SIGN: 1201 return PKCS11_CKA_SIGN; 1202 case PKCS11_FUNCTION_VERIFY: 1203 return PKCS11_CKA_VERIFY; 1204 case PKCS11_FUNCTION_WRAP: 1205 return PKCS11_CKA_WRAP; 1206 case PKCS11_FUNCTION_UNWRAP: 1207 return PKCS11_CKA_UNWRAP; 1208 case PKCS11_FUNCTION_DERIVE: 1209 return PKCS11_CKA_DERIVE; 1210 default: 1211 return PKCS11_CKA_UNDEFINED_ID; 1212 } 1213 } 1214 1215 enum pkcs11_rc 1216 check_parent_attrs_against_processing(enum pkcs11_mechanism_id proc_id, 1217 enum processing_func function, 1218 struct obj_attrs *head) 1219 { 1220 enum pkcs11_class_id key_class = get_class(head); 1221 enum pkcs11_key_type key_type = get_key_type(head); 1222 enum pkcs11_attr_id attr = func_to_attr(function); 1223 1224 if (!get_bool(head, attr)) { 1225 DMSG("%s not permitted", id2str_attr(attr)); 1226 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1227 } 1228 1229 /* Check processing complies with parent key family */ 1230 switch (proc_id) { 1231 case PKCS11_CKM_AES_ECB: 1232 case PKCS11_CKM_AES_CBC: 1233 case PKCS11_CKM_AES_CBC_PAD: 1234 case PKCS11_CKM_AES_CTS: 1235 case PKCS11_CKM_AES_CTR: 1236 if (key_class == PKCS11_CKO_SECRET_KEY && 1237 key_type == PKCS11_CKK_AES) 1238 break; 1239 1240 DMSG("%s invalid key %s/%s", id2str_proc(proc_id), 1241 id2str_class(key_class), id2str_key_type(key_type)); 1242 1243 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1244 1245 case PKCS11_CKM_MD5_HMAC: 1246 case PKCS11_CKM_SHA_1_HMAC: 1247 case PKCS11_CKM_SHA224_HMAC: 1248 case PKCS11_CKM_SHA256_HMAC: 1249 case PKCS11_CKM_SHA384_HMAC: 1250 case PKCS11_CKM_SHA512_HMAC: 1251 if (key_class != PKCS11_CKO_SECRET_KEY) 1252 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1253 1254 if (key_type == PKCS11_CKK_GENERIC_SECRET) 1255 break; 1256 1257 switch (proc_id) { 1258 case PKCS11_CKM_MD5_HMAC: 1259 if (key_type == PKCS11_CKK_MD5_HMAC) 1260 break; 1261 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1262 case PKCS11_CKM_SHA_1_HMAC: 1263 if (key_type == PKCS11_CKK_SHA_1_HMAC) 1264 break; 1265 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1266 case PKCS11_CKM_SHA224_HMAC: 1267 if (key_type == PKCS11_CKK_SHA224_HMAC) 1268 break; 1269 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1270 case PKCS11_CKM_SHA256_HMAC: 1271 if (key_type == PKCS11_CKK_SHA256_HMAC) 1272 break; 1273 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1274 case PKCS11_CKM_SHA384_HMAC: 1275 if (key_type == PKCS11_CKK_SHA384_HMAC) 1276 break; 1277 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1278 case PKCS11_CKM_SHA512_HMAC: 1279 if (key_type == PKCS11_CKK_SHA512_HMAC) 1280 break; 1281 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1282 default: 1283 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1284 } 1285 break; 1286 1287 default: 1288 DMSG("Invalid processing %#"PRIx32"/%s", proc_id, 1289 id2str_proc(proc_id)); 1290 1291 return PKCS11_CKR_MECHANISM_INVALID; 1292 } 1293 1294 if (!parent_key_complies_allowed_processings(proc_id, head)) { 1295 DMSG("Allowed mechanism failed"); 1296 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1297 } 1298 1299 return PKCS11_CKR_OK; 1300 } 1301 1302 bool attribute_is_exportable(struct pkcs11_attribute_head *req_attr, 1303 struct pkcs11_object *obj) 1304 { 1305 uint8_t boolval = 0; 1306 uint32_t boolsize = 0; 1307 enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR; 1308 enum pkcs11_class_id key_class = get_class(obj->attributes); 1309 1310 if (key_class != PKCS11_CKO_SECRET_KEY && 1311 key_class != PKCS11_CKO_PRIVATE_KEY) 1312 return true; 1313 1314 switch (req_attr->id) { 1315 case PKCS11_CKA_PRIVATE_EXPONENT: 1316 case PKCS11_CKA_PRIME_1: 1317 case PKCS11_CKA_PRIME_2: 1318 case PKCS11_CKA_EXPONENT_1: 1319 case PKCS11_CKA_EXPONENT_2: 1320 case PKCS11_CKA_COEFFICIENT: 1321 case PKCS11_CKA_VALUE: 1322 boolsize = sizeof(boolval); 1323 rc = get_attribute(obj->attributes, PKCS11_CKA_EXTRACTABLE, 1324 &boolval, &boolsize); 1325 if (rc || boolval == PKCS11_FALSE) 1326 return false; 1327 1328 boolsize = sizeof(boolval); 1329 rc = get_attribute(obj->attributes, PKCS11_CKA_SENSITIVE, 1330 &boolval, &boolsize); 1331 if (rc || boolval == PKCS11_TRUE) 1332 return false; 1333 break; 1334 default: 1335 break; 1336 } 1337 1338 return true; 1339 } 1340 1341 static bool attr_is_modifiable_any_key(struct pkcs11_attribute_head *attr) 1342 { 1343 switch (attr->id) { 1344 case PKCS11_CKA_ID: 1345 case PKCS11_CKA_START_DATE: 1346 case PKCS11_CKA_END_DATE: 1347 case PKCS11_CKA_DERIVE: 1348 return true; 1349 default: 1350 return false; 1351 } 1352 } 1353 1354 static bool attr_is_modifiable_secret_key(struct pkcs11_attribute_head *attr, 1355 struct pkcs11_session *session, 1356 struct pkcs11_object *obj) 1357 { 1358 switch (attr->id) { 1359 case PKCS11_CKA_ENCRYPT: 1360 case PKCS11_CKA_DECRYPT: 1361 case PKCS11_CKA_SIGN: 1362 case PKCS11_CKA_VERIFY: 1363 case PKCS11_CKA_WRAP: 1364 case PKCS11_CKA_UNWRAP: 1365 return true; 1366 /* Can't be modified once set to CK_FALSE - 12 in Table 10 */ 1367 case PKCS11_CKA_EXTRACTABLE: 1368 return get_bool(obj->attributes, attr->id); 1369 /* Can't be modified once set to CK_TRUE - 11 in Table 10 */ 1370 case PKCS11_CKA_SENSITIVE: 1371 case PKCS11_CKA_WRAP_WITH_TRUSTED: 1372 return !get_bool(obj->attributes, attr->id); 1373 /* Change in CKA_TRUSTED can only be done by SO */ 1374 case PKCS11_CKA_TRUSTED: 1375 return pkcs11_session_is_so(session); 1376 case PKCS11_CKA_NEVER_EXTRACTABLE: 1377 case PKCS11_CKA_ALWAYS_SENSITIVE: 1378 return false; 1379 default: 1380 return false; 1381 } 1382 } 1383 1384 static bool attr_is_modifiable_public_key(struct pkcs11_attribute_head *attr, 1385 struct pkcs11_session *session, 1386 struct pkcs11_object *obj __unused) 1387 { 1388 switch (attr->id) { 1389 case PKCS11_CKA_SUBJECT: 1390 case PKCS11_CKA_ENCRYPT: 1391 case PKCS11_CKA_VERIFY: 1392 case PKCS11_CKA_VERIFY_RECOVER: 1393 case PKCS11_CKA_WRAP: 1394 return true; 1395 case PKCS11_CKA_TRUSTED: 1396 /* Change in CKA_TRUSTED can only be done by SO */ 1397 return pkcs11_session_is_so(session); 1398 default: 1399 return false; 1400 } 1401 } 1402 1403 static bool attr_is_modifiable_private_key(struct pkcs11_attribute_head *attr, 1404 struct pkcs11_session *sess __unused, 1405 struct pkcs11_object *obj) 1406 { 1407 switch (attr->id) { 1408 case PKCS11_CKA_SUBJECT: 1409 case PKCS11_CKA_DECRYPT: 1410 case PKCS11_CKA_SIGN: 1411 case PKCS11_CKA_SIGN_RECOVER: 1412 case PKCS11_CKA_UNWRAP: 1413 /* 1414 * TBD: Revisit if we don't support PKCS11_CKA_PUBLIC_KEY_INFO 1415 * Specification mentions that if this attribute is 1416 * supplied as part of a template for C_CreateObject, C_CopyObject or 1417 * C_SetAttributeValue for a private key, the token MUST verify 1418 * correspondence between the private key data and the public key data 1419 * as supplied in CKA_PUBLIC_KEY_INFO. This needs to be 1420 * taken care of when this object type will be implemented 1421 */ 1422 case PKCS11_CKA_PUBLIC_KEY_INFO: 1423 return true; 1424 /* Can't be modified once set to CK_FALSE - 12 in Table 10 */ 1425 case PKCS11_CKA_EXTRACTABLE: 1426 return get_bool(obj->attributes, attr->id); 1427 /* Can't be modified once set to CK_TRUE - 11 in Table 10 */ 1428 case PKCS11_CKA_SENSITIVE: 1429 case PKCS11_CKA_WRAP_WITH_TRUSTED: 1430 return !get_bool(obj->attributes, attr->id); 1431 case PKCS11_CKA_NEVER_EXTRACTABLE: 1432 case PKCS11_CKA_ALWAYS_SENSITIVE: 1433 return false; 1434 default: 1435 return false; 1436 } 1437 } 1438 1439 static bool attribute_is_modifiable(struct pkcs11_session *session, 1440 struct pkcs11_attribute_head *req_attr, 1441 struct pkcs11_object *obj, 1442 enum pkcs11_class_id class, 1443 enum processing_func function) 1444 { 1445 /* Check modifiable attributes common to any object */ 1446 switch (req_attr->id) { 1447 case PKCS11_CKA_LABEL: 1448 return true; 1449 case PKCS11_CKA_TOKEN: 1450 case PKCS11_CKA_MODIFIABLE: 1451 case PKCS11_CKA_DESTROYABLE: 1452 case PKCS11_CKA_PRIVATE: 1453 return function == PKCS11_FUNCTION_COPY; 1454 case PKCS11_CKA_COPYABLE: 1455 /* 1456 * Specification mentions that if the attribute value is false 1457 * it can't be set to true. Reading this we assume that it 1458 * should be possible to modify this attribute even though this 1459 * is not marked as modifiable in Table 10 if done in right 1460 * direction i.e from TRUE -> FALSE. 1461 */ 1462 return get_bool(obj->attributes, req_attr->id); 1463 default: 1464 break; 1465 } 1466 1467 /* Attribute checking based on class type */ 1468 switch (class) { 1469 case PKCS11_CKO_SECRET_KEY: 1470 case PKCS11_CKO_PUBLIC_KEY: 1471 case PKCS11_CKO_PRIVATE_KEY: 1472 if (attr_is_modifiable_any_key(req_attr)) 1473 return true; 1474 if (class == PKCS11_CKO_SECRET_KEY && 1475 attr_is_modifiable_secret_key(req_attr, session, obj)) 1476 return true; 1477 if (class == PKCS11_CKO_PUBLIC_KEY && 1478 attr_is_modifiable_public_key(req_attr, session, obj)) 1479 return true; 1480 if (class == PKCS11_CKO_PRIVATE_KEY && 1481 attr_is_modifiable_private_key(req_attr, session, obj)) 1482 return true; 1483 break; 1484 case PKCS11_CKO_DATA: 1485 /* None of the data object attributes are modifiable */ 1486 return false; 1487 default: 1488 break; 1489 } 1490 1491 return false; 1492 } 1493 1494 enum pkcs11_rc check_attrs_against_modification(struct pkcs11_session *session, 1495 struct obj_attrs *head, 1496 struct pkcs11_object *obj, 1497 enum processing_func function) 1498 { 1499 enum pkcs11_class_id class = PKCS11_CKO_UNDEFINED_ID; 1500 char *cur = NULL; 1501 char *end = NULL; 1502 size_t len = 0; 1503 1504 class = get_class(obj->attributes); 1505 1506 cur = (char *)head + sizeof(struct obj_attrs); 1507 end = cur + head->attrs_size; 1508 1509 for (; cur < end; cur += len) { 1510 /* Structure aligned copy of the pkcs11_ref in the object */ 1511 struct pkcs11_attribute_head cli_ref = { }; 1512 1513 TEE_MemMove(&cli_ref, cur, sizeof(cli_ref)); 1514 len = sizeof(cli_ref) + cli_ref.size; 1515 1516 /* 1517 * Check 1 - Check if attribute belongs to the object 1518 * The obj->attributes has all the attributes in 1519 * it which are allowed for an object. 1520 */ 1521 if (get_attribute_ptr(obj->attributes, cli_ref.id, NULL, 1522 NULL) == PKCS11_RV_NOT_FOUND) 1523 return PKCS11_CKR_ATTRIBUTE_TYPE_INVALID; 1524 1525 /* Check 2 - Is attribute modifiable */ 1526 if (!attribute_is_modifiable(session, &cli_ref, obj, class, 1527 function)) 1528 return PKCS11_CKR_ATTRIBUTE_READ_ONLY; 1529 1530 /* 1531 * Checks for modification in PKCS11_CKA_TOKEN and 1532 * PKCS11_CKA_PRIVATE are required for PKCS11_FUNCTION_COPY 1533 * only, so skip them for PKCS11_FUNCTION_MODIFY. 1534 */ 1535 if (function == PKCS11_FUNCTION_MODIFY) 1536 continue; 1537 1538 /* 1539 * An attempt to copy an object to a token will fail for 1540 * RO session 1541 */ 1542 if (cli_ref.id == PKCS11_CKA_TOKEN && 1543 get_bool(head, PKCS11_CKA_TOKEN)) { 1544 if (!pkcs11_session_is_read_write(session)) { 1545 DMSG("Can't copy to token in a RO session"); 1546 return PKCS11_CKR_SESSION_READ_ONLY; 1547 } 1548 } 1549 1550 if (cli_ref.id == PKCS11_CKA_PRIVATE) { 1551 bool parent_priv = 1552 get_bool(obj->attributes, cli_ref.id); 1553 bool obj_priv = get_bool(head, cli_ref.id); 1554 1555 /* 1556 * If PKCS11_CKA_PRIVATE is being set to TRUE from 1557 * FALSE, user has to be logged in 1558 */ 1559 if (!parent_priv && obj_priv) { 1560 if ((pkcs11_session_is_public(session) || 1561 pkcs11_session_is_so(session))) 1562 return PKCS11_CKR_USER_NOT_LOGGED_IN; 1563 } 1564 1565 /* 1566 * Restriction added - Even for Copy, do not allow 1567 * modification of CKA_PRIVATE from TRUE to FALSE 1568 */ 1569 if (parent_priv && !obj_priv) 1570 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 1571 } 1572 } 1573 1574 return PKCS11_CKR_OK; 1575 } 1576