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