1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2017-2020, Linaro Limited 4 */ 5 6 #include <assert.h> 7 #include <inttypes.h> 8 #include <pkcs11_ta.h> 9 #include <stdlib.h> 10 #include <string_ext.h> 11 #include <tee_internal_api_extensions.h> 12 #include <tee_internal_api.h> 13 #include <trace.h> 14 #include <util.h> 15 16 #include "attributes.h" 17 #include "handle.h" 18 #include "pkcs11_attributes.h" 19 #include "pkcs11_helpers.h" 20 #include "pkcs11_token.h" 21 #include "sanitize_object.h" 22 #include "serializer.h" 23 #include "token_capabilities.h" 24 25 static uint32_t pkcs11_func2ckfm(enum processing_func function) 26 { 27 switch (function) { 28 case PKCS11_FUNCTION_DIGEST: 29 return PKCS11_CKFM_DIGEST; 30 case PKCS11_FUNCTION_GENERATE: 31 return PKCS11_CKFM_GENERATE; 32 case PKCS11_FUNCTION_GENERATE_PAIR: 33 return PKCS11_CKFM_GENERATE_KEY_PAIR; 34 case PKCS11_FUNCTION_DERIVE: 35 return PKCS11_CKFM_DERIVE; 36 case PKCS11_FUNCTION_WRAP: 37 return PKCS11_CKFM_WRAP; 38 case PKCS11_FUNCTION_UNWRAP: 39 return PKCS11_CKFM_UNWRAP; 40 case PKCS11_FUNCTION_ENCRYPT: 41 return PKCS11_CKFM_ENCRYPT; 42 case PKCS11_FUNCTION_DECRYPT: 43 return PKCS11_CKFM_DECRYPT; 44 case PKCS11_FUNCTION_SIGN: 45 return PKCS11_CKFM_SIGN; 46 case PKCS11_FUNCTION_VERIFY: 47 return PKCS11_CKFM_VERIFY; 48 case PKCS11_FUNCTION_SIGN_RECOVER: 49 return PKCS11_CKFM_SIGN_RECOVER; 50 case PKCS11_FUNCTION_VERIFY_RECOVER: 51 return PKCS11_CKFM_VERIFY_RECOVER; 52 default: 53 return 0; 54 } 55 } 56 57 enum pkcs11_rc 58 check_mechanism_against_processing(struct pkcs11_session *session, 59 enum pkcs11_mechanism_id mechanism_type, 60 enum processing_func function, 61 enum processing_step step) 62 { 63 bool allowed = false; 64 65 switch (step) { 66 case PKCS11_FUNC_STEP_INIT: 67 switch (function) { 68 case PKCS11_FUNCTION_IMPORT: 69 case PKCS11_FUNCTION_COPY: 70 case PKCS11_FUNCTION_MODIFY: 71 case PKCS11_FUNCTION_DESTROY: 72 return PKCS11_CKR_OK; 73 default: 74 break; 75 } 76 /* 77 * Check that the returned PKCS11_CKFM_* flag from 78 * pkcs11_func2ckfm() is among the ones from 79 * mechanism_supported_flags(). 80 */ 81 allowed = mechanism_supported_flags(mechanism_type) & 82 pkcs11_func2ckfm(function); 83 break; 84 85 case PKCS11_FUNC_STEP_ONESHOT: 86 if (session->processing->always_authen && 87 !session->processing->relogged) 88 return PKCS11_CKR_USER_NOT_LOGGED_IN; 89 90 if (session->processing->step == PKCS11_FUNC_STEP_UPDATE || 91 session->processing->step == PKCS11_FUNC_STEP_FINAL) { 92 EMSG("Cannot perform one-shot on active processing"); 93 return PKCS11_CKR_OPERATION_ACTIVE; 94 } 95 96 allowed = true; 97 break; 98 99 case PKCS11_FUNC_STEP_UPDATE: 100 if (session->processing->always_authen && 101 !session->processing->relogged) 102 return PKCS11_CKR_USER_NOT_LOGGED_IN; 103 104 if (session->processing->step == PKCS11_FUNC_STEP_ONESHOT || 105 session->processing->step == PKCS11_FUNC_STEP_FINAL) { 106 EMSG("Cannot perform update on finalized processing"); 107 return PKCS11_CKR_OPERATION_ACTIVE; 108 } 109 110 allowed = !mechanism_is_one_shot_only(mechanism_type); 111 break; 112 113 case PKCS11_FUNC_STEP_UPDATE_KEY: 114 assert(function == PKCS11_FUNCTION_DIGEST); 115 116 if (session->processing->always_authen && 117 !session->processing->relogged) 118 return PKCS11_CKR_USER_NOT_LOGGED_IN; 119 120 allowed = true; 121 break; 122 123 case PKCS11_FUNC_STEP_FINAL: 124 if (session->processing->always_authen && 125 !session->processing->relogged) 126 return PKCS11_CKR_USER_NOT_LOGGED_IN; 127 128 if (session->processing->step == PKCS11_FUNC_STEP_ONESHOT) { 129 EMSG("Cannot perform final on oneshot processing"); 130 return PKCS11_CKR_OPERATION_ACTIVE; 131 } 132 return PKCS11_CKR_OK; 133 134 default: 135 TEE_Panic(step); 136 break; 137 } 138 139 if (!allowed) { 140 EMSG("Processing %#x/%s not permitted (%u/%u)", 141 (unsigned int)mechanism_type, id2str_proc(mechanism_type), 142 function, step); 143 return PKCS11_CKR_MECHANISM_INVALID; 144 } 145 146 return PKCS11_CKR_OK; 147 } 148 149 /* 150 * Object default boolean attributes as per PKCS#11 151 */ 152 static uint8_t *pkcs11_object_default_boolprop(uint32_t attribute) 153 { 154 static const uint8_t bool_true = 1; 155 static const uint8_t bool_false; 156 157 switch (attribute) { 158 /* As per PKCS#11 default value */ 159 case PKCS11_CKA_MODIFIABLE: 160 case PKCS11_CKA_COPYABLE: 161 case PKCS11_CKA_DESTROYABLE: 162 return (uint8_t *)&bool_true; 163 case PKCS11_CKA_TOKEN: 164 case PKCS11_CKA_PRIVATE: 165 case PKCS11_CKA_WRAP_WITH_TRUSTED: 166 case PKCS11_CKA_ALWAYS_AUTHENTICATE: 167 case PKCS11_CKA_SENSITIVE: 168 return (uint8_t *)&bool_false; 169 /* Token specific default value */ 170 case PKCS11_CKA_SIGN: 171 case PKCS11_CKA_VERIFY: 172 case PKCS11_CKA_DERIVE: 173 case PKCS11_CKA_ENCRYPT: 174 case PKCS11_CKA_DECRYPT: 175 case PKCS11_CKA_SIGN_RECOVER: 176 case PKCS11_CKA_VERIFY_RECOVER: 177 case PKCS11_CKA_WRAP: 178 case PKCS11_CKA_UNWRAP: 179 case PKCS11_CKA_EXTRACTABLE: 180 case PKCS11_CKA_TRUSTED: 181 return (uint8_t *)&bool_false; 182 default: 183 DMSG("No default for boolprop attribute %#"PRIx32, attribute); 184 return NULL; 185 } 186 } 187 188 /* 189 * Object expects several boolean attributes to be set to a default value 190 * or to a validate client configuration value. This function append the input 191 * attribute (id/size/value) in the serialized object. 192 */ 193 static enum pkcs11_rc pkcs11_import_object_boolprop(struct obj_attrs **out, 194 struct obj_attrs *templ, 195 uint32_t attribute) 196 { 197 enum pkcs11_rc rc = PKCS11_CKR_OK; 198 uint8_t bbool = 0; 199 uint32_t size = sizeof(uint8_t); 200 void *attr = NULL; 201 202 rc = get_attribute(templ, attribute, &bbool, &size); 203 if (rc) { 204 if (rc != PKCS11_RV_NOT_FOUND) 205 return rc; 206 attr = pkcs11_object_default_boolprop(attribute); 207 if (!attr) 208 return PKCS11_CKR_TEMPLATE_INCOMPLETE; 209 } else { 210 attr = &bbool; 211 } 212 213 /* Boolean attributes are 1byte in the ABI, no alignment issue */ 214 return add_attribute(out, attribute, attr, sizeof(uint8_t)); 215 } 216 217 static enum pkcs11_rc set_mandatory_boolprops(struct obj_attrs **out, 218 struct obj_attrs *temp, 219 uint32_t const *bp, 220 size_t bp_count) 221 { 222 enum pkcs11_rc rc = PKCS11_CKR_OK; 223 size_t n = 0; 224 225 for (n = 0; n < bp_count; n++) { 226 rc = pkcs11_import_object_boolprop(out, temp, bp[n]); 227 if (rc) 228 return rc; 229 } 230 231 return rc; 232 } 233 234 static enum pkcs11_rc set_mandatory_attributes(struct obj_attrs **out, 235 struct obj_attrs *temp, 236 uint32_t const *attrs, 237 size_t attrs_count) 238 { 239 enum pkcs11_rc rc = PKCS11_CKR_OK; 240 size_t n = 0; 241 242 for (n = 0; n < attrs_count; n++) { 243 uint32_t size = 0; 244 void *value = NULL; 245 246 if (get_attribute_ptr(temp, attrs[n], &value, &size)) 247 return PKCS11_CKR_TEMPLATE_INCOMPLETE; 248 249 rc = add_attribute(out, attrs[n], value, size); 250 if (rc) 251 return rc; 252 } 253 254 return rc; 255 } 256 257 static enum pkcs11_rc get_default_value(enum pkcs11_attr_id id, void **value, 258 uint32_t *size) 259 { 260 /* should have been taken care of already */ 261 assert(!pkcs11_attr_is_boolean(id)); 262 263 /* All other attributes have an empty default value */ 264 *value = NULL; 265 *size = 0; 266 return PKCS11_CKR_OK; 267 } 268 269 static enum pkcs11_rc set_optional_attributes_with_def(struct obj_attrs **out, 270 struct obj_attrs *temp, 271 uint32_t const *attrs, 272 size_t attrs_count, 273 bool default_to_null) 274 { 275 enum pkcs11_rc rc = PKCS11_CKR_OK; 276 size_t n = 0; 277 278 for (n = 0; n < attrs_count; n++) { 279 uint32_t size = 0; 280 void *value = NULL; 281 282 rc = get_attribute_ptr(temp, attrs[n], &value, &size); 283 if (rc == PKCS11_RV_NOT_FOUND) { 284 if (default_to_null) { 285 rc = get_default_value(attrs[n], &value, &size); 286 } else { 287 rc = PKCS11_CKR_OK; 288 continue; 289 } 290 } 291 if (rc) 292 return rc; 293 294 rc = add_attribute(out, attrs[n], value, size); 295 if (rc) 296 return rc; 297 } 298 299 return rc; 300 } 301 302 static enum pkcs11_rc set_attributes_opt_or_null(struct obj_attrs **out, 303 struct obj_attrs *temp, 304 uint32_t const *attrs, 305 size_t attrs_count) 306 { 307 return set_optional_attributes_with_def(out, temp, attrs, attrs_count, 308 true /* defaults to empty */); 309 } 310 311 static enum pkcs11_rc set_optional_attributes(struct obj_attrs **out, 312 struct obj_attrs *temp, 313 uint32_t const *attrs, 314 size_t attrs_count) 315 { 316 return set_optional_attributes_with_def(out, temp, attrs, attrs_count, 317 false /* no default value */); 318 } 319 320 /* 321 * Below are listed the mandated or optional expected attributes for 322 * PKCS#11 storage objects. 323 * 324 * Note: boolprops (mandated boolean attributes) PKCS11_CKA_ALWAYS_SENSITIVE, 325 * and PKCS11_CKA_NEVER_EXTRACTABLE are set by the token, not provided 326 * in the client template. 327 */ 328 329 /* PKCS#11 specification for any object (session/token) of the storage */ 330 static const uint32_t any_object_boolprops[] = { 331 PKCS11_CKA_TOKEN, PKCS11_CKA_PRIVATE, 332 PKCS11_CKA_MODIFIABLE, PKCS11_CKA_COPYABLE, PKCS11_CKA_DESTROYABLE, 333 }; 334 335 static const uint32_t any_object_opt_or_null[] = { 336 PKCS11_CKA_LABEL, 337 }; 338 339 /* PKCS#11 specification for raw data object (+any_object_xxx) */ 340 const uint32_t raw_data_opt_or_null[] = { 341 PKCS11_CKA_OBJECT_ID, PKCS11_CKA_APPLICATION, PKCS11_CKA_VALUE, 342 }; 343 344 /* PKCS#11 specification for certificate object (+pkcs11_any_object_xxx) */ 345 static const uint32_t pkcs11_certificate_mandated[] = { 346 PKCS11_CKA_CERTIFICATE_TYPE, 347 }; 348 349 static const uint32_t pkcs11_certificate_boolprops[] = { 350 PKCS11_CKA_TRUSTED, 351 }; 352 353 static const uint32_t pkcs11_certificate_optional[] = { 354 PKCS11_CKA_CERTIFICATE_CATEGORY, PKCS11_CKA_CHECK_VALUE, 355 PKCS11_CKA_START_DATE, PKCS11_CKA_END_DATE, PKCS11_CKA_PUBLIC_KEY_INFO, 356 }; 357 358 /* 359 * PKCS#11 specification for X.509 certificate object (+pkcs11_certificate_xxx) 360 */ 361 static const uint32_t pkcs11_x509_certificate_mandated[] = { 362 PKCS11_CKA_SUBJECT, 363 }; 364 365 static const uint32_t pkcs11_x509_certificate_optional[] = { 366 PKCS11_CKA_ID, PKCS11_CKA_ISSUER, PKCS11_CKA_SERIAL_NUMBER, 367 PKCS11_CKA_VALUE, PKCS11_CKA_URL, 368 PKCS11_CKA_HASH_OF_SUBJECT_PUBLIC_KEY, 369 PKCS11_CKA_HASH_OF_ISSUER_PUBLIC_KEY, 370 PKCS11_CKA_JAVA_MIDP_SECURITY_DOMAIN, PKCS11_CKA_NAME_HASH_ALGORITHM, 371 }; 372 373 /* PKCS#11 specification for any key object (+any_object_xxx) */ 374 static const uint32_t any_key_boolprops[] = { 375 PKCS11_CKA_DERIVE, 376 }; 377 378 static const uint32_t any_key_opt_or_null[] = { 379 PKCS11_CKA_ID, 380 PKCS11_CKA_START_DATE, PKCS11_CKA_END_DATE, 381 }; 382 383 static const uint32_t any_key_optional[] = { 384 PKCS11_CKA_ALLOWED_MECHANISMS, 385 }; 386 387 /* PKCS#11 specification for any symmetric key (+any_key_xxx) */ 388 static const uint32_t symm_key_boolprops[] = { 389 PKCS11_CKA_ENCRYPT, PKCS11_CKA_DECRYPT, 390 PKCS11_CKA_SIGN, PKCS11_CKA_VERIFY, 391 PKCS11_CKA_WRAP, PKCS11_CKA_UNWRAP, 392 PKCS11_CKA_SENSITIVE, PKCS11_CKA_EXTRACTABLE, 393 PKCS11_CKA_WRAP_WITH_TRUSTED, PKCS11_CKA_TRUSTED, 394 }; 395 396 static const uint32_t symm_key_opt_or_null[] = { 397 PKCS11_CKA_WRAP_TEMPLATE, PKCS11_CKA_UNWRAP_TEMPLATE, 398 PKCS11_CKA_DERIVE_TEMPLATE, PKCS11_CKA_VALUE, 399 }; 400 401 static const uint32_t symm_key_optional[] = { 402 PKCS11_CKA_VALUE_LEN, 403 }; 404 405 /* PKCS#11 specification for any asymmetric public key (+any_key_xxx) */ 406 static const uint32_t public_key_boolprops[] = { 407 PKCS11_CKA_ENCRYPT, PKCS11_CKA_VERIFY, PKCS11_CKA_VERIFY_RECOVER, 408 PKCS11_CKA_WRAP, 409 PKCS11_CKA_TRUSTED, 410 }; 411 412 static const uint32_t public_key_mandated[] = { 413 }; 414 415 static const uint32_t public_key_opt_or_null[] = { 416 PKCS11_CKA_SUBJECT, PKCS11_CKA_WRAP_TEMPLATE, 417 PKCS11_CKA_PUBLIC_KEY_INFO, 418 }; 419 420 /* PKCS#11 specification for any asymmetric private key (+any_key_xxx) */ 421 static const uint32_t private_key_boolprops[] = { 422 PKCS11_CKA_DECRYPT, PKCS11_CKA_SIGN, PKCS11_CKA_SIGN_RECOVER, 423 PKCS11_CKA_UNWRAP, 424 PKCS11_CKA_SENSITIVE, PKCS11_CKA_EXTRACTABLE, 425 PKCS11_CKA_WRAP_WITH_TRUSTED, PKCS11_CKA_ALWAYS_AUTHENTICATE, 426 }; 427 428 static const uint32_t private_key_mandated[] = { 429 }; 430 431 static const uint32_t private_key_opt_or_null[] = { 432 PKCS11_CKA_SUBJECT, PKCS11_CKA_UNWRAP_TEMPLATE, 433 PKCS11_CKA_PUBLIC_KEY_INFO, 434 }; 435 436 /* PKCS#11 specification for any RSA key (+public/private_key_xxx) */ 437 static const uint32_t rsa_pub_key_gen_mand[] = { 438 PKCS11_CKA_MODULUS_BITS, 439 }; 440 441 static const uint32_t rsa_pub_key_create_mand[] = { 442 PKCS11_CKA_MODULUS, PKCS11_CKA_PUBLIC_EXPONENT, 443 }; 444 445 static const uint32_t rsa_pub_key_gen_opt_or_null[] = { 446 PKCS11_CKA_PUBLIC_EXPONENT, 447 }; 448 449 static const uint32_t rsa_priv_key_opt_or_null[] = { 450 PKCS11_CKA_MODULUS, PKCS11_CKA_PUBLIC_EXPONENT, 451 PKCS11_CKA_PRIVATE_EXPONENT, 452 PKCS11_CKA_PRIME_1, PKCS11_CKA_PRIME_2, 453 PKCS11_CKA_EXPONENT_1, PKCS11_CKA_EXPONENT_2, PKCS11_CKA_COEFFICIENT, 454 }; 455 456 /* PKCS#11 specification for any EC key (+public/private_key_xxx) */ 457 static const uint32_t ec_public_key_mandated[] = { 458 PKCS11_CKA_EC_PARAMS, 459 }; 460 461 static const uint32_t ec_public_key_opt_or_null[] = { 462 PKCS11_CKA_EC_POINT, 463 }; 464 465 static const uint32_t ec_private_key_mandated[] = { 466 }; 467 468 static const uint32_t ec_private_key_opt_or_null[] = { 469 PKCS11_CKA_EC_PARAMS, 470 PKCS11_CKA_VALUE, 471 }; 472 473 static enum pkcs11_rc create_storage_attributes(struct obj_attrs **out, 474 struct obj_attrs *temp) 475 { 476 enum pkcs11_class_id class = PKCS11_CKO_UNDEFINED_ID; 477 enum pkcs11_rc rc = PKCS11_CKR_OK; 478 479 rc = init_attributes_head(out); 480 if (rc) 481 return rc; 482 483 /* Object class is mandatory */ 484 class = get_class(temp); 485 if (class == PKCS11_CKO_UNDEFINED_ID) { 486 EMSG("Class attribute not found"); 487 488 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 489 } 490 rc = add_attribute(out, PKCS11_CKA_CLASS, &class, sizeof(uint32_t)); 491 if (rc) 492 return rc; 493 494 rc = set_mandatory_boolprops(out, temp, any_object_boolprops, 495 ARRAY_SIZE(any_object_boolprops)); 496 if (rc) 497 return rc; 498 499 return set_attributes_opt_or_null(out, temp, any_object_opt_or_null, 500 ARRAY_SIZE(any_object_opt_or_null)); 501 } 502 503 static enum pkcs11_rc create_genkey_attributes(struct obj_attrs **out, 504 struct obj_attrs *temp) 505 { 506 uint32_t type = PKCS11_CKO_UNDEFINED_ID; 507 enum pkcs11_rc rc = PKCS11_CKR_OK; 508 509 rc = create_storage_attributes(out, temp); 510 if (rc) 511 return rc; 512 513 type = get_key_type(temp); 514 if (type == PKCS11_CKK_UNDEFINED_ID) { 515 EMSG("Key type attribute not found"); 516 517 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 518 } 519 rc = add_attribute(out, PKCS11_CKA_KEY_TYPE, &type, sizeof(uint32_t)); 520 if (rc) 521 return rc; 522 523 rc = set_mandatory_boolprops(out, temp, any_key_boolprops, 524 ARRAY_SIZE(any_key_boolprops)); 525 if (rc) 526 return rc; 527 528 rc = set_attributes_opt_or_null(out, temp, any_key_opt_or_null, 529 ARRAY_SIZE(any_key_opt_or_null)); 530 if (rc) 531 return rc; 532 533 return set_optional_attributes(out, temp, any_key_optional, 534 ARRAY_SIZE(any_key_optional)); 535 536 } 537 538 static enum pkcs11_rc create_symm_key_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_SECRET_KEY); 544 545 rc = create_genkey_attributes(out, temp); 546 if (rc) 547 return rc; 548 549 assert(get_class(*out) == PKCS11_CKO_SECRET_KEY); 550 551 switch (get_key_type(*out)) { 552 case PKCS11_CKK_GENERIC_SECRET: 553 case PKCS11_CKK_AES: 554 case PKCS11_CKK_MD5_HMAC: 555 case PKCS11_CKK_SHA_1_HMAC: 556 case PKCS11_CKK_SHA256_HMAC: 557 case PKCS11_CKK_SHA384_HMAC: 558 case PKCS11_CKK_SHA512_HMAC: 559 case PKCS11_CKK_SHA224_HMAC: 560 break; 561 default: 562 EMSG("Invalid key type %#"PRIx32"/%s", 563 get_key_type(*out), id2str_key_type(get_key_type(*out))); 564 565 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 566 } 567 568 rc = set_mandatory_boolprops(out, temp, symm_key_boolprops, 569 ARRAY_SIZE(symm_key_boolprops)); 570 if (rc) 571 return rc; 572 573 rc = set_attributes_opt_or_null(out, temp, symm_key_opt_or_null, 574 ARRAY_SIZE(symm_key_opt_or_null)); 575 if (rc) 576 return rc; 577 578 return set_optional_attributes(out, temp, symm_key_optional, 579 ARRAY_SIZE(symm_key_optional)); 580 } 581 582 static enum pkcs11_rc create_data_attributes(struct obj_attrs **out, 583 struct obj_attrs *temp) 584 { 585 enum pkcs11_rc rc = PKCS11_CKR_OK; 586 587 assert(get_class(temp) == PKCS11_CKO_DATA); 588 589 rc = create_storage_attributes(out, temp); 590 if (rc) 591 return rc; 592 593 assert(get_class(*out) == PKCS11_CKO_DATA); 594 595 return set_attributes_opt_or_null(out, temp, raw_data_opt_or_null, 596 ARRAY_SIZE(raw_data_opt_or_null)); 597 } 598 599 static enum pkcs11_rc create_certificate_attributes(struct obj_attrs **out, 600 struct obj_attrs *temp) 601 { 602 uint32_t const *mandated = NULL; 603 uint32_t const *optional = NULL; 604 size_t mandated_count = 0; 605 size_t optional_count = 0; 606 void *attr_value = NULL; 607 uint32_t attr_size = 0; 608 uint32_t default_cert_category = 609 PKCS11_CK_CERTIFICATE_CATEGORY_UNSPECIFIED; 610 uint32_t default_name_hash_alg = PKCS11_CKM_SHA_1; 611 uint32_t cert_category = 0; 612 enum pkcs11_rc rc = PKCS11_CKR_OK; 613 614 assert(get_class(temp) == PKCS11_CKO_CERTIFICATE); 615 616 rc = create_storage_attributes(out, temp); 617 if (rc) 618 return rc; 619 620 assert(get_class(*out) == PKCS11_CKO_CERTIFICATE); 621 622 rc = set_mandatory_boolprops(out, temp, pkcs11_certificate_boolprops, 623 ARRAY_SIZE(pkcs11_certificate_boolprops)); 624 if (rc) 625 return rc; 626 627 rc = set_mandatory_attributes(out, temp, pkcs11_certificate_mandated, 628 ARRAY_SIZE(pkcs11_certificate_mandated)); 629 if (rc) 630 return rc; 631 632 rc = set_optional_attributes(out, temp, pkcs11_certificate_optional, 633 ARRAY_SIZE(pkcs11_certificate_optional)); 634 if (rc) 635 return rc; 636 637 switch (get_certificate_type(*out)) { 638 case PKCS11_CKC_X_509: 639 mandated = pkcs11_x509_certificate_mandated; 640 optional = pkcs11_x509_certificate_optional; 641 mandated_count = ARRAY_SIZE(pkcs11_x509_certificate_mandated); 642 optional_count = ARRAY_SIZE(pkcs11_x509_certificate_optional); 643 break; 644 default: 645 EMSG("Invalid certificate type %#"PRIx32"/%s", 646 get_certificate_type(*out), 647 id2str_certificate_type(get_certificate_type(*out))); 648 649 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 650 } 651 652 rc = set_mandatory_attributes(out, temp, mandated, mandated_count); 653 if (rc) 654 return rc; 655 656 rc = set_optional_attributes(out, temp, optional, optional_count); 657 if (rc) 658 return rc; 659 660 attr_size = 0; 661 rc = get_attribute_ptr(*out, PKCS11_CKA_CERTIFICATE_CATEGORY, 662 &attr_value, &attr_size); 663 if (rc == PKCS11_CKR_OK && attr_size == sizeof(cert_category)) { 664 /* Sanitize certificate category */ 665 TEE_MemMove(&cert_category, attr_value, sizeof(cert_category)); 666 667 switch (cert_category) { 668 case PKCS11_CK_CERTIFICATE_CATEGORY_UNSPECIFIED: 669 case PKCS11_CK_CERTIFICATE_CATEGORY_TOKEN_USER: 670 case PKCS11_CK_CERTIFICATE_CATEGORY_AUTHORITY: 671 case PKCS11_CK_CERTIFICATE_CATEGORY_OTHER_ENTITY: 672 break; 673 default: 674 EMSG("Invalid certificate category %#"PRIx32, 675 cert_category); 676 677 return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID; 678 } 679 } else if (rc == PKCS11_RV_NOT_FOUND) { 680 /* Set default category when missing */ 681 rc = set_attribute(out, PKCS11_CKA_CERTIFICATE_CATEGORY, 682 &default_cert_category, 683 sizeof(default_cert_category)); 684 if (rc) 685 return rc; 686 } else { 687 /* All other cases are errors */ 688 EMSG("Invalid certificate category"); 689 690 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 691 } 692 693 attr_size = 0; 694 rc = get_attribute_ptr(*out, PKCS11_CKA_NAME_HASH_ALGORITHM, NULL, 695 &attr_size); 696 if (rc == PKCS11_CKR_OK && attr_size == sizeof(uint32_t)) { 697 /* We accept any algorithm what caller wanted to specify */ 698 } else if (rc == PKCS11_RV_NOT_FOUND) { 699 /* Set default hash algorithm when missing */ 700 rc = set_attribute(out, PKCS11_CKA_NAME_HASH_ALGORITHM, 701 &default_name_hash_alg, 702 sizeof(default_name_hash_alg)); 703 if (rc) 704 return rc; 705 } else { 706 /* All other cases are errors */ 707 EMSG("Invalid name hash algorithm"); 708 709 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 710 } 711 712 return rc; 713 } 714 715 static enum pkcs11_rc create_pub_key_attributes(struct obj_attrs **out, 716 struct obj_attrs *temp, 717 enum processing_func function) 718 { 719 uint32_t const *mandated = NULL; 720 uint32_t const *oon = NULL; 721 size_t mandated_count = 0; 722 size_t oon_count = 0; 723 enum pkcs11_rc rc = PKCS11_CKR_OK; 724 725 assert(get_class(temp) == PKCS11_CKO_PUBLIC_KEY); 726 727 rc = create_genkey_attributes(out, temp); 728 if (rc) 729 return rc; 730 731 assert(get_class(*out) == PKCS11_CKO_PUBLIC_KEY); 732 733 rc = set_mandatory_boolprops(out, temp, public_key_boolprops, 734 ARRAY_SIZE(public_key_boolprops)); 735 if (rc) 736 return rc; 737 738 rc = set_mandatory_attributes(out, temp, public_key_mandated, 739 ARRAY_SIZE(public_key_mandated)); 740 if (rc) 741 return rc; 742 743 rc = set_attributes_opt_or_null(out, temp, 744 public_key_opt_or_null, 745 ARRAY_SIZE(public_key_opt_or_null)); 746 if (rc) 747 return rc; 748 749 switch (get_key_type(*out)) { 750 case PKCS11_CKK_RSA: 751 switch (function) { 752 case PKCS11_FUNCTION_GENERATE_PAIR: 753 mandated = rsa_pub_key_gen_mand; 754 oon = rsa_pub_key_gen_opt_or_null; 755 mandated_count = ARRAY_SIZE(rsa_pub_key_gen_mand); 756 oon_count = ARRAY_SIZE(rsa_pub_key_gen_opt_or_null); 757 break; 758 case PKCS11_FUNCTION_IMPORT: 759 mandated = rsa_pub_key_create_mand; 760 mandated_count = ARRAY_SIZE(rsa_pub_key_create_mand); 761 break; 762 default: 763 EMSG("Unsupported function %#"PRIx32"/%s", function, 764 id2str_function(function)); 765 766 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 767 } 768 break; 769 case PKCS11_CKK_EC: 770 mandated = ec_public_key_mandated; 771 oon = ec_public_key_opt_or_null; 772 mandated_count = ARRAY_SIZE(ec_public_key_mandated); 773 oon_count = ARRAY_SIZE(ec_public_key_opt_or_null); 774 break; 775 default: 776 EMSG("Invalid key type %#"PRIx32"/%s", 777 get_key_type(*out), id2str_key_type(get_key_type(*out))); 778 779 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 780 } 781 782 rc = set_mandatory_attributes(out, temp, mandated, mandated_count); 783 if (rc) 784 return rc; 785 786 return set_attributes_opt_or_null(out, temp, oon, oon_count); 787 } 788 789 static enum pkcs11_rc create_priv_key_attributes(struct obj_attrs **out, 790 struct obj_attrs *temp) 791 { 792 uint32_t const *mandated = NULL; 793 uint32_t const *oon = NULL; 794 size_t mandated_count = 0; 795 size_t oon_count = 0; 796 enum pkcs11_rc rc = PKCS11_CKR_OK; 797 798 assert(get_class(temp) == PKCS11_CKO_PRIVATE_KEY); 799 800 rc = create_genkey_attributes(out, temp); 801 if (rc) 802 return rc; 803 804 assert(get_class(*out) == PKCS11_CKO_PRIVATE_KEY); 805 806 rc = set_mandatory_boolprops(out, temp, private_key_boolprops, 807 ARRAY_SIZE(private_key_boolprops)); 808 if (rc) 809 return rc; 810 811 rc = set_mandatory_attributes(out, temp, private_key_mandated, 812 ARRAY_SIZE(private_key_mandated)); 813 if (rc) 814 return rc; 815 816 rc = set_attributes_opt_or_null(out, temp, private_key_opt_or_null, 817 ARRAY_SIZE(private_key_opt_or_null)); 818 if (rc) 819 return rc; 820 821 switch (get_key_type(*out)) { 822 case PKCS11_CKK_RSA: 823 oon = rsa_priv_key_opt_or_null; 824 oon_count = ARRAY_SIZE(rsa_priv_key_opt_or_null); 825 break; 826 case PKCS11_CKK_EC: 827 mandated = ec_private_key_mandated; 828 oon = ec_private_key_opt_or_null; 829 mandated_count = ARRAY_SIZE(ec_private_key_mandated); 830 oon_count = ARRAY_SIZE(ec_private_key_opt_or_null); 831 break; 832 default: 833 EMSG("Invalid key type %#"PRIx32"/%s", 834 get_key_type(*out), id2str_key_type(get_key_type(*out))); 835 836 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 837 } 838 839 rc = set_mandatory_attributes(out, temp, mandated, mandated_count); 840 if (rc) 841 return rc; 842 843 return set_attributes_opt_or_null(out, temp, oon, oon_count); 844 } 845 846 static enum pkcs11_rc 847 sanitize_symm_key_attributes(struct obj_attrs **temp, 848 enum processing_func function) 849 { 850 enum pkcs11_rc rc = PKCS11_CKR_OK; 851 uint32_t a_size = 0; 852 853 assert(get_class(*temp) == PKCS11_CKO_SECRET_KEY); 854 855 rc = get_attribute_ptr(*temp, PKCS11_CKA_VALUE, NULL, &a_size); 856 857 switch (get_key_type(*temp)) { 858 case PKCS11_CKK_GENERIC_SECRET: 859 case PKCS11_CKK_AES: 860 case PKCS11_CKK_MD5_HMAC: 861 case PKCS11_CKK_SHA_1_HMAC: 862 case PKCS11_CKK_SHA256_HMAC: 863 case PKCS11_CKK_SHA384_HMAC: 864 case PKCS11_CKK_SHA512_HMAC: 865 case PKCS11_CKK_SHA224_HMAC: 866 switch (function) { 867 case PKCS11_FUNCTION_IMPORT: 868 /* CKA_VALUE is a mandatory with C_CreateObject */ 869 if (rc || a_size == 0) 870 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 871 872 if (get_attribute_ptr(*temp, PKCS11_CKA_VALUE_LEN, NULL, 873 NULL) != PKCS11_RV_NOT_FOUND) 874 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 875 876 return add_attribute(temp, PKCS11_CKA_VALUE_LEN, 877 &a_size, sizeof(uint32_t)); 878 case PKCS11_FUNCTION_GENERATE: 879 if (rc != PKCS11_RV_NOT_FOUND) 880 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 881 break; 882 default: 883 break; 884 } 885 break; 886 default: 887 EMSG("Invalid key type %#"PRIx32"/%s", 888 get_key_type(*temp), id2str_key_type(get_key_type(*temp))); 889 890 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 891 } 892 893 return PKCS11_CKR_OK; 894 } 895 896 /* 897 * Create an attribute list for a new object from a template and a parent 898 * object (optional) for an object generation function (generate, copy, 899 * derive...). 900 * 901 * PKCS#11 directives on the supplied template and expected return value: 902 * - template has an invalid attribute ID: ATTRIBUTE_TYPE_INVALID 903 * - template has an invalid value for an attribute: ATTRIBUTE_VALID_INVALID 904 * - template has value for a read-only attribute: ATTRIBUTE_READ_ONLY 905 * - template+default+parent => still miss an attribute: TEMPLATE_INCONSISTENT 906 * 907 * INFO on PKCS11_CMD_COPY_OBJECT: 908 * - parent PKCS11_CKA_COPYIABLE=false => return ACTION_PROHIBITED. 909 * - template can specify PKCS11_CKA_TOKEN, PKCS11_CKA_PRIVATE, 910 * PKCS11_CKA_MODIFIABLE, PKCS11_CKA_DESTROYABLE. 911 * - SENSITIVE can change from false to true, not from true to false. 912 * - LOCAL is the parent LOCAL 913 */ 914 enum pkcs11_rc 915 create_attributes_from_template(struct obj_attrs **out, void *template, 916 size_t template_size, 917 struct obj_attrs *parent, 918 enum processing_func function, 919 enum pkcs11_mechanism_id mecha, 920 enum pkcs11_class_id template_class) 921 { 922 struct obj_attrs *temp = NULL; 923 struct obj_attrs *attrs = NULL; 924 enum pkcs11_rc rc = PKCS11_CKR_OK; 925 uint8_t local = 0; 926 uint8_t always_sensitive = 0; 927 uint8_t never_extract = 0; 928 uint8_t extractable = 0; 929 uint32_t class = PKCS11_UNDEFINED_ID; 930 uint32_t type = PKCS11_UNDEFINED_ID; 931 uint32_t mechanism_id = PKCS11_CKM_UNDEFINED_ID; 932 struct obj_attrs *req_attrs = NULL; 933 uint32_t size = 0; 934 uint32_t indirect_template = PKCS11_CKA_UNDEFINED_ID; 935 936 #ifdef DEBUG /* Sanity: check function argument */ 937 trace_attributes_from_api_head("template", template, template_size); 938 switch (function) { 939 case PKCS11_FUNCTION_GENERATE: 940 case PKCS11_FUNCTION_GENERATE_PAIR: 941 case PKCS11_FUNCTION_IMPORT: 942 case PKCS11_FUNCTION_MODIFY: 943 case PKCS11_FUNCTION_DERIVE: 944 case PKCS11_FUNCTION_UNWRAP: 945 case PKCS11_FUNCTION_COPY: 946 break; 947 default: 948 TEE_Panic(TEE_ERROR_NOT_SUPPORTED); 949 } 950 #endif 951 952 /* 953 * For PKCS11_FUNCTION_GENERATE, find the class and type 954 * based on the mechanism. These will be passed as hint 955 * sanitize_client_object() and added in temp if not 956 * already present 957 */ 958 if (function == PKCS11_FUNCTION_GENERATE) { 959 switch (mecha) { 960 case PKCS11_CKM_GENERIC_SECRET_KEY_GEN: 961 class = PKCS11_CKO_SECRET_KEY; 962 type = PKCS11_CKK_GENERIC_SECRET; 963 break; 964 case PKCS11_CKM_AES_KEY_GEN: 965 class = PKCS11_CKO_SECRET_KEY; 966 type = PKCS11_CKK_AES; 967 break; 968 default: 969 TEE_Panic(TEE_ERROR_NOT_SUPPORTED); 970 } 971 } 972 973 /* 974 * For PKCS11_FUNCTION_GENERATE_PAIR, find the class and type 975 * based on the mechanism. These will be passed as hint 976 * sanitize_client_object() and added in temp if not 977 * already present 978 */ 979 if (function == PKCS11_FUNCTION_GENERATE_PAIR) { 980 switch (mecha) { 981 case PKCS11_CKM_EC_KEY_PAIR_GEN: 982 class = template_class; 983 type = PKCS11_CKK_EC; 984 break; 985 case PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN: 986 class = template_class; 987 type = PKCS11_CKK_RSA; 988 break; 989 default: 990 TEE_Panic(TEE_ERROR_NOT_SUPPORTED); 991 } 992 } 993 994 /* 995 * Check and remove duplicates if any and create a new temporary 996 * template 997 */ 998 rc = sanitize_client_object(&temp, template, template_size, class, 999 type); 1000 if (rc) 1001 goto out; 1002 1003 /* 1004 * For function type modify and copy return the created template 1005 * from here. Rest of the code below is for creating objects 1006 * or generating keys. 1007 */ 1008 switch (function) { 1009 case PKCS11_FUNCTION_MODIFY: 1010 case PKCS11_FUNCTION_COPY: 1011 *out = temp; 1012 return rc; 1013 case PKCS11_FUNCTION_DERIVE: 1014 case PKCS11_FUNCTION_UNWRAP: 1015 if (function == PKCS11_FUNCTION_UNWRAP) 1016 indirect_template = PKCS11_CKA_UNWRAP_TEMPLATE; 1017 else 1018 indirect_template = PKCS11_CKA_DERIVE_TEMPLATE; 1019 1020 rc = get_attribute_ptr(parent, indirect_template, 1021 (void *)&req_attrs, &size); 1022 if (rc == PKCS11_CKR_OK && size != 0) { 1023 rc = attributes_match_add_reference(&temp, req_attrs); 1024 if (rc) 1025 goto out; 1026 } 1027 break; 1028 default: 1029 break; 1030 } 1031 1032 /* 1033 * Check if class and type in temp are consistent with the mechanism 1034 */ 1035 switch (mecha) { 1036 case PKCS11_CKM_GENERIC_SECRET_KEY_GEN: 1037 if (get_class(temp) != PKCS11_CKO_SECRET_KEY || 1038 get_key_type(temp) != PKCS11_CKK_GENERIC_SECRET) { 1039 rc = PKCS11_CKR_TEMPLATE_INCONSISTENT; 1040 goto out; 1041 } 1042 break; 1043 case PKCS11_CKM_AES_KEY_GEN: 1044 if (get_class(temp) != PKCS11_CKO_SECRET_KEY || 1045 get_key_type(temp) != PKCS11_CKK_AES) { 1046 rc = PKCS11_CKR_TEMPLATE_INCONSISTENT; 1047 goto out; 1048 } 1049 break; 1050 case PKCS11_CKM_EC_KEY_PAIR_GEN: 1051 if ((get_class(temp) != PKCS11_CKO_PUBLIC_KEY && 1052 get_class(temp) != PKCS11_CKO_PRIVATE_KEY) || 1053 get_key_type(temp) != PKCS11_CKK_EC) { 1054 rc = PKCS11_CKR_TEMPLATE_INCONSISTENT; 1055 goto out; 1056 } 1057 break; 1058 case PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN: 1059 if ((get_class(temp) != PKCS11_CKO_PUBLIC_KEY && 1060 get_class(temp) != PKCS11_CKO_PRIVATE_KEY) || 1061 get_key_type(temp) != PKCS11_CKK_RSA) { 1062 rc = PKCS11_CKR_TEMPLATE_INCONSISTENT; 1063 goto out; 1064 } 1065 break; 1066 default: 1067 break; 1068 } 1069 1070 if (!sanitize_consistent_class_and_type(temp)) { 1071 EMSG("Inconsistent class/type"); 1072 rc = PKCS11_CKR_TEMPLATE_INCONSISTENT; 1073 goto out; 1074 } 1075 1076 /* 1077 * TBD - Add a check to see if temp contains any attribute which 1078 * is not consistent with the object class or type and return error. 1079 * In current implementation such attributes are ignored and not 1080 * added to final object while PKCS#11 specification expects a 1081 * failure and an error code be returned. 1082 */ 1083 1084 switch (get_class(temp)) { 1085 case PKCS11_CKO_DATA: 1086 rc = create_data_attributes(&attrs, temp); 1087 break; 1088 case PKCS11_CKO_CERTIFICATE: 1089 rc = create_certificate_attributes(&attrs, temp); 1090 break; 1091 case PKCS11_CKO_SECRET_KEY: 1092 rc = sanitize_symm_key_attributes(&temp, function); 1093 if (rc) 1094 goto out; 1095 rc = create_symm_key_attributes(&attrs, temp); 1096 break; 1097 case PKCS11_CKO_PUBLIC_KEY: 1098 rc = create_pub_key_attributes(&attrs, temp, function); 1099 break; 1100 case PKCS11_CKO_PRIVATE_KEY: 1101 rc = create_priv_key_attributes(&attrs, temp); 1102 break; 1103 default: 1104 DMSG("Invalid object class %#"PRIx32"/%s", 1105 get_class(temp), id2str_class(get_class(temp))); 1106 1107 rc = PKCS11_CKR_TEMPLATE_INCONSISTENT; 1108 break; 1109 } 1110 if (rc) 1111 goto out; 1112 1113 if (get_attribute_ptr(temp, PKCS11_CKA_LOCAL, NULL, NULL) != 1114 PKCS11_RV_NOT_FOUND) { 1115 rc = PKCS11_CKR_TEMPLATE_INCONSISTENT; 1116 goto out; 1117 } 1118 1119 if (get_attribute_ptr(temp, PKCS11_CKA_KEY_GEN_MECHANISM, NULL, NULL) != 1120 PKCS11_RV_NOT_FOUND) { 1121 rc = PKCS11_CKR_TEMPLATE_INCONSISTENT; 1122 goto out; 1123 } 1124 1125 switch (function) { 1126 case PKCS11_FUNCTION_GENERATE: 1127 case PKCS11_FUNCTION_GENERATE_PAIR: 1128 local = PKCS11_TRUE; 1129 break; 1130 case PKCS11_FUNCTION_IMPORT: 1131 case PKCS11_FUNCTION_DERIVE: 1132 case PKCS11_FUNCTION_UNWRAP: 1133 default: 1134 local = PKCS11_FALSE; 1135 break; 1136 } 1137 rc = add_attribute(&attrs, PKCS11_CKA_LOCAL, &local, sizeof(local)); 1138 if (rc) 1139 goto out; 1140 1141 switch (get_class(attrs)) { 1142 case PKCS11_CKO_SECRET_KEY: 1143 case PKCS11_CKO_PRIVATE_KEY: 1144 case PKCS11_CKO_PUBLIC_KEY: 1145 always_sensitive = PKCS11_FALSE; 1146 never_extract = PKCS11_FALSE; 1147 1148 switch (function) { 1149 case PKCS11_FUNCTION_DERIVE: 1150 always_sensitive = 1151 get_bool(parent, PKCS11_CKA_ALWAYS_SENSITIVE) && 1152 get_bool(attrs, PKCS11_CKA_SENSITIVE); 1153 never_extract = 1154 get_bool(parent, PKCS11_CKA_NEVER_EXTRACTABLE) && 1155 !get_bool(attrs, PKCS11_CKA_EXTRACTABLE); 1156 break; 1157 case PKCS11_FUNCTION_UNWRAP: 1158 always_sensitive = PKCS11_FALSE; 1159 never_extract = PKCS11_FALSE; 1160 extractable = PKCS11_TRUE; 1161 1162 /* 1163 * Check if template passed by user has CKA_EXTRACTABLE. 1164 * If not, by default value of CKA_EXTRACTABLE is set as 1165 * TRUE. 1166 */ 1167 if (get_attribute_ptr(temp, PKCS11_CKA_EXTRACTABLE, 1168 NULL, 1169 NULL) == PKCS11_RV_NOT_FOUND) { 1170 rc = set_attribute(&attrs, 1171 PKCS11_CKA_EXTRACTABLE, 1172 &extractable, 1173 sizeof(extractable)); 1174 if (rc) 1175 goto out; 1176 } 1177 break; 1178 case PKCS11_FUNCTION_GENERATE: 1179 case PKCS11_FUNCTION_GENERATE_PAIR: 1180 always_sensitive = get_bool(attrs, 1181 PKCS11_CKA_SENSITIVE); 1182 never_extract = !get_bool(attrs, 1183 PKCS11_CKA_EXTRACTABLE); 1184 break; 1185 default: 1186 break; 1187 } 1188 1189 rc = add_attribute(&attrs, PKCS11_CKA_ALWAYS_SENSITIVE, 1190 &always_sensitive, sizeof(always_sensitive)); 1191 if (rc) 1192 goto out; 1193 1194 rc = add_attribute(&attrs, PKCS11_CKA_NEVER_EXTRACTABLE, 1195 &never_extract, sizeof(never_extract)); 1196 if (rc) 1197 goto out; 1198 1199 /* Keys mandate attribute PKCS11_CKA_KEY_GEN_MECHANISM */ 1200 if (local) 1201 mechanism_id = mecha; 1202 else 1203 mechanism_id = PKCS11_CK_UNAVAILABLE_INFORMATION; 1204 1205 rc = add_attribute(&attrs, PKCS11_CKA_KEY_GEN_MECHANISM, 1206 &mechanism_id, sizeof(mechanism_id)); 1207 if (rc) 1208 goto out; 1209 break; 1210 1211 default: 1212 break; 1213 } 1214 1215 *out = attrs; 1216 1217 #ifdef DEBUG 1218 trace_attributes("object", attrs); 1219 #endif 1220 1221 out: 1222 TEE_Free(temp); 1223 if (rc) 1224 TEE_Free(attrs); 1225 1226 return rc; 1227 } 1228 1229 static enum pkcs11_rc check_attrs_misc_integrity(struct obj_attrs *head) 1230 { 1231 if (get_bool(head, PKCS11_CKA_NEVER_EXTRACTABLE) && 1232 get_bool(head, PKCS11_CKA_EXTRACTABLE)) { 1233 DMSG("Never/Extractable attributes mismatch %d/%d", 1234 get_bool(head, PKCS11_CKA_NEVER_EXTRACTABLE), 1235 get_bool(head, PKCS11_CKA_EXTRACTABLE)); 1236 1237 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 1238 } 1239 1240 if (get_bool(head, PKCS11_CKA_ALWAYS_SENSITIVE) && 1241 !get_bool(head, PKCS11_CKA_SENSITIVE)) { 1242 DMSG("Sensitive/always attributes mismatch %d/%d", 1243 get_bool(head, PKCS11_CKA_SENSITIVE), 1244 get_bool(head, PKCS11_CKA_ALWAYS_SENSITIVE)); 1245 1246 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 1247 } 1248 1249 return PKCS11_CKR_OK; 1250 } 1251 1252 bool object_is_private(struct obj_attrs *head) 1253 { 1254 return get_bool(head, PKCS11_CKA_PRIVATE); 1255 } 1256 1257 bool object_is_token(struct obj_attrs *head) 1258 { 1259 return get_bool(head, PKCS11_CKA_TOKEN); 1260 } 1261 1262 bool object_is_modifiable(struct obj_attrs *head) 1263 { 1264 return get_bool(head, PKCS11_CKA_MODIFIABLE); 1265 } 1266 1267 bool object_is_copyable(struct obj_attrs *head) 1268 { 1269 return get_bool(head, PKCS11_CKA_COPYABLE); 1270 } 1271 1272 /* 1273 * Check access to object against authentication to token 1274 */ 1275 enum pkcs11_rc check_access_attrs_against_token(struct pkcs11_session *session, 1276 struct obj_attrs *head) 1277 { 1278 bool private = true; 1279 1280 switch (get_class(head)) { 1281 case PKCS11_CKO_SECRET_KEY: 1282 case PKCS11_CKO_PRIVATE_KEY: 1283 case PKCS11_CKO_PUBLIC_KEY: 1284 case PKCS11_CKO_DATA: 1285 case PKCS11_CKO_CERTIFICATE: 1286 private = object_is_private(head); 1287 break; 1288 default: 1289 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1290 } 1291 1292 if (private && (pkcs11_session_is_public(session) || 1293 pkcs11_session_is_so(session))) { 1294 DMSG("Private object access from a public or SO session"); 1295 1296 return PKCS11_CKR_USER_NOT_LOGGED_IN; 1297 } 1298 1299 return PKCS11_CKR_OK; 1300 } 1301 1302 /* 1303 * Check the attributes of a to-be-created object matches the token state 1304 */ 1305 enum pkcs11_rc check_created_attrs_against_token(struct pkcs11_session *session, 1306 struct obj_attrs *head) 1307 { 1308 enum pkcs11_rc rc = PKCS11_CKR_OK; 1309 1310 rc = check_attrs_misc_integrity(head); 1311 if (rc) 1312 return rc; 1313 1314 if (get_bool(head, PKCS11_CKA_TRUSTED) && 1315 !pkcs11_session_is_so(session)) { 1316 DMSG("Can't create trusted object"); 1317 1318 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1319 } 1320 1321 if (get_bool(head, PKCS11_CKA_TOKEN) && 1322 !pkcs11_session_is_read_write(session)) { 1323 DMSG("Can't create persistent object"); 1324 1325 return PKCS11_CKR_SESSION_READ_ONLY; 1326 } 1327 1328 /* 1329 * TODO: START_DATE and END_DATE: complies with current time? 1330 */ 1331 return PKCS11_CKR_OK; 1332 } 1333 1334 #define DMSG_BAD_BBOOL(attr, proc, head) \ 1335 do { \ 1336 uint32_t __maybe_unused _attr = (attr); \ 1337 uint8_t __maybe_unused _bvalue = 0; \ 1338 enum pkcs11_rc __maybe_unused _rc = PKCS11_CKR_OK; \ 1339 \ 1340 _rc = get_attribute((head), _attr, &_bvalue, NULL); \ 1341 DMSG("%s issue for %s: %sfound, value %"PRIu8, \ 1342 id2str_attr(_attr), id2str_proc((proc)), \ 1343 _rc ? "not " : "", _bvalue); \ 1344 } while (0) 1345 1346 static bool __maybe_unused check_attr_bval(uint32_t proc_id __maybe_unused, 1347 struct obj_attrs *head, 1348 uint32_t attribute, bool val) 1349 { 1350 uint8_t bbool = 0; 1351 uint32_t sz = sizeof(bbool); 1352 1353 if (!get_attribute(head, attribute, &bbool, &sz) && !!bbool == val) 1354 return true; 1355 1356 DMSG_BAD_BBOOL(attribute, proc_id, head); 1357 return false; 1358 } 1359 1360 /* 1361 * Check the attributes of a new secret match the processing/mechanism 1362 * used to create it. 1363 * 1364 * @proc_id - PKCS11_CKM_xxx 1365 * @head - head of the attributes of the to-be-created object. 1366 */ 1367 enum pkcs11_rc check_created_attrs_against_processing(uint32_t proc_id, 1368 struct obj_attrs *head) 1369 { 1370 /* 1371 * Processings that do not create secrets are not expected to call 1372 * this function which would panic. 1373 */ 1374 switch (proc_id) { 1375 case PKCS11_PROCESSING_IMPORT: 1376 case PKCS11_CKM_ECDH1_DERIVE: 1377 case PKCS11_CKM_AES_ECB: 1378 case PKCS11_CKM_AES_CBC: 1379 case PKCS11_CKM_AES_ECB_ENCRYPT_DATA: 1380 case PKCS11_CKM_AES_CBC_ENCRYPT_DATA: 1381 assert(check_attr_bval(proc_id, head, PKCS11_CKA_LOCAL, false)); 1382 break; 1383 case PKCS11_CKM_GENERIC_SECRET_KEY_GEN: 1384 case PKCS11_CKM_AES_KEY_GEN: 1385 case PKCS11_CKM_EC_KEY_PAIR_GEN: 1386 case PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN: 1387 assert(check_attr_bval(proc_id, head, PKCS11_CKA_LOCAL, true)); 1388 break; 1389 default: 1390 TEE_Panic(proc_id); 1391 break; 1392 } 1393 1394 switch (proc_id) { 1395 case PKCS11_CKM_GENERIC_SECRET_KEY_GEN: 1396 assert(get_key_type(head) == PKCS11_CKK_GENERIC_SECRET); 1397 break; 1398 case PKCS11_CKM_AES_KEY_GEN: 1399 assert(get_key_type(head) == PKCS11_CKK_AES); 1400 break; 1401 case PKCS11_CKM_EC_KEY_PAIR_GEN: 1402 assert(get_key_type(head) == PKCS11_CKK_EC); 1403 break; 1404 case PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN: 1405 assert(get_key_type(head) == PKCS11_CKK_RSA); 1406 break; 1407 case PKCS11_PROCESSING_IMPORT: 1408 case PKCS11_CKM_ECDH1_DERIVE: 1409 default: 1410 break; 1411 } 1412 1413 return PKCS11_CKR_OK; 1414 } 1415 1416 /* Return min and max key size supported for a key_type in bytes */ 1417 static void get_key_min_max_sizes(enum pkcs11_key_type key_type, 1418 uint32_t *min_key_size, 1419 uint32_t *max_key_size) 1420 { 1421 enum pkcs11_mechanism_id mechanism = PKCS11_CKM_UNDEFINED_ID; 1422 1423 switch (key_type) { 1424 case PKCS11_CKK_GENERIC_SECRET: 1425 mechanism = PKCS11_CKM_GENERIC_SECRET_KEY_GEN; 1426 break; 1427 case PKCS11_CKK_AES: 1428 mechanism = PKCS11_CKM_AES_KEY_GEN; 1429 break; 1430 case PKCS11_CKK_MD5_HMAC: 1431 mechanism = PKCS11_CKM_MD5_HMAC; 1432 break; 1433 case PKCS11_CKK_SHA_1_HMAC: 1434 mechanism = PKCS11_CKM_SHA_1_HMAC; 1435 break; 1436 case PKCS11_CKK_SHA224_HMAC: 1437 mechanism = PKCS11_CKM_SHA224_HMAC; 1438 break; 1439 case PKCS11_CKK_SHA256_HMAC: 1440 mechanism = PKCS11_CKM_SHA256_HMAC; 1441 break; 1442 case PKCS11_CKK_SHA384_HMAC: 1443 mechanism = PKCS11_CKM_SHA384_HMAC; 1444 break; 1445 case PKCS11_CKK_SHA512_HMAC: 1446 mechanism = PKCS11_CKM_SHA512_HMAC; 1447 break; 1448 case PKCS11_CKK_EC: 1449 mechanism = PKCS11_CKM_EC_KEY_PAIR_GEN; 1450 break; 1451 case PKCS11_CKK_RSA: 1452 mechanism = PKCS11_CKM_RSA_PKCS_KEY_PAIR_GEN; 1453 break; 1454 default: 1455 TEE_Panic(key_type); 1456 break; 1457 } 1458 1459 mechanism_supported_key_sizes_bytes(mechanism, min_key_size, 1460 max_key_size); 1461 } 1462 1463 enum pkcs11_rc check_created_attrs(struct obj_attrs *key1, 1464 struct obj_attrs *key2) 1465 { 1466 enum pkcs11_rc rc = PKCS11_CKR_OK; 1467 struct obj_attrs *secret = NULL; 1468 struct obj_attrs *private = NULL; 1469 struct obj_attrs *public = NULL; 1470 uint32_t max_key_size = 0; 1471 uint32_t min_key_size = 0; 1472 uint32_t key_length = 0; 1473 1474 switch (get_class(key1)) { 1475 case PKCS11_CKO_SECRET_KEY: 1476 secret = key1; 1477 break; 1478 case PKCS11_CKO_PUBLIC_KEY: 1479 public = key1; 1480 break; 1481 case PKCS11_CKO_PRIVATE_KEY: 1482 private = key1; 1483 break; 1484 default: 1485 return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID; 1486 } 1487 1488 if (key2) { 1489 switch (get_class(key2)) { 1490 case PKCS11_CKO_PUBLIC_KEY: 1491 public = key2; 1492 if (private == key1) 1493 break; 1494 1495 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 1496 case PKCS11_CKO_PRIVATE_KEY: 1497 private = key2; 1498 if (public == key1) 1499 break; 1500 1501 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 1502 default: 1503 return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID; 1504 } 1505 1506 if (get_key_type(private) != get_key_type(public)) 1507 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 1508 } 1509 1510 if (secret) { 1511 switch (get_key_type(secret)) { 1512 case PKCS11_CKK_AES: 1513 case PKCS11_CKK_GENERIC_SECRET: 1514 case PKCS11_CKK_MD5_HMAC: 1515 case PKCS11_CKK_SHA_1_HMAC: 1516 case PKCS11_CKK_SHA224_HMAC: 1517 case PKCS11_CKK_SHA256_HMAC: 1518 case PKCS11_CKK_SHA384_HMAC: 1519 case PKCS11_CKK_SHA512_HMAC: 1520 break; 1521 default: 1522 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 1523 } 1524 1525 /* Get key size */ 1526 rc = get_u32_attribute(secret, PKCS11_CKA_VALUE_LEN, 1527 &key_length); 1528 if (rc) 1529 return PKCS11_CKR_TEMPLATE_INCOMPLETE; 1530 } 1531 if (public) { 1532 switch (get_key_type(public)) { 1533 case PKCS11_CKK_RSA: 1534 /* Get key size */ 1535 rc = get_u32_attribute(public, PKCS11_CKA_MODULUS_BITS, 1536 &key_length); 1537 if (rc) 1538 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 1539 key_length = ROUNDUP(key_length, 8) / 8; 1540 break; 1541 case PKCS11_CKK_EC: 1542 break; 1543 default: 1544 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 1545 } 1546 } 1547 if (private) { 1548 switch (get_key_type(private)) { 1549 case PKCS11_CKK_RSA: 1550 case PKCS11_CKK_EC: 1551 break; 1552 default: 1553 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 1554 } 1555 } 1556 1557 /* 1558 * Check key size for symmetric keys and RSA keys 1559 * EC is bound to domains, no need to check here. 1560 */ 1561 switch (get_key_type(key1)) { 1562 case PKCS11_CKK_EC: 1563 return PKCS11_CKR_OK; 1564 default: 1565 break; 1566 } 1567 1568 get_key_min_max_sizes(get_key_type(key1), &min_key_size, &max_key_size); 1569 if (key_length < min_key_size || key_length > max_key_size) { 1570 EMSG("Length %"PRIu32" vs range [%"PRIu32" %"PRIu32"]", 1571 key_length, min_key_size, max_key_size); 1572 1573 return PKCS11_CKR_KEY_SIZE_RANGE; 1574 } 1575 1576 if (secret && get_key_type(secret) == PKCS11_CKK_AES) { 1577 if (key_length != 16 && key_length != 24 && key_length != 32) 1578 return PKCS11_CKR_KEY_SIZE_RANGE; 1579 } 1580 1581 return PKCS11_CKR_OK; 1582 } 1583 1584 /* Check processing ID against attribute ALLOWED_MECHANISMS if any */ 1585 static bool parent_key_complies_allowed_processings(uint32_t proc_id, 1586 struct obj_attrs *head) 1587 { 1588 char *attr = NULL; 1589 uint32_t size = 0; 1590 uint32_t proc = 0; 1591 size_t count = 0; 1592 enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR; 1593 1594 rc = get_attribute_ptr(head, PKCS11_CKA_ALLOWED_MECHANISMS, 1595 (void *)&attr, &size); 1596 if (rc == PKCS11_RV_NOT_FOUND) 1597 return true; 1598 if (rc) { 1599 EMSG("unexpected attributes state"); 1600 TEE_Panic(TEE_ERROR_BAD_STATE); 1601 } 1602 1603 for (count = size / sizeof(uint32_t); count; count--) { 1604 TEE_MemMove(&proc, attr, sizeof(uint32_t)); 1605 attr += sizeof(uint32_t); 1606 1607 if (proc == proc_id) 1608 return true; 1609 } 1610 1611 DMSG("can't find %s in allowed list", id2str_proc(proc_id)); 1612 return false; 1613 } 1614 1615 static enum pkcs11_attr_id func_to_attr(enum processing_func func) 1616 { 1617 switch (func) { 1618 case PKCS11_FUNCTION_ENCRYPT: 1619 return PKCS11_CKA_ENCRYPT; 1620 case PKCS11_FUNCTION_DECRYPT: 1621 return PKCS11_CKA_DECRYPT; 1622 case PKCS11_FUNCTION_SIGN: 1623 return PKCS11_CKA_SIGN; 1624 case PKCS11_FUNCTION_VERIFY: 1625 return PKCS11_CKA_VERIFY; 1626 case PKCS11_FUNCTION_WRAP: 1627 return PKCS11_CKA_WRAP; 1628 case PKCS11_FUNCTION_UNWRAP: 1629 return PKCS11_CKA_UNWRAP; 1630 case PKCS11_FUNCTION_DERIVE: 1631 return PKCS11_CKA_DERIVE; 1632 default: 1633 return PKCS11_CKA_UNDEFINED_ID; 1634 } 1635 } 1636 1637 enum pkcs11_rc 1638 check_parent_attrs_against_processing(enum pkcs11_mechanism_id proc_id, 1639 enum processing_func function, 1640 struct obj_attrs *head) 1641 { 1642 enum pkcs11_class_id key_class = get_class(head); 1643 enum pkcs11_key_type key_type = get_key_type(head); 1644 enum pkcs11_attr_id attr = func_to_attr(function); 1645 1646 if (!get_bool(head, attr)) { 1647 DMSG("%s not permitted", id2str_attr(attr)); 1648 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1649 } 1650 1651 /* Check processing complies with parent key family */ 1652 switch (proc_id) { 1653 case PKCS11_CKM_AES_ECB: 1654 case PKCS11_CKM_AES_CBC: 1655 case PKCS11_CKM_AES_CTS: 1656 case PKCS11_CKM_AES_CTR: 1657 case PKCS11_CKM_AES_CMAC: 1658 case PKCS11_CKM_AES_CMAC_GENERAL: 1659 if (key_class == PKCS11_CKO_SECRET_KEY && 1660 key_type == PKCS11_CKK_AES) 1661 break; 1662 1663 DMSG("%s invalid key %s/%s", id2str_proc(proc_id), 1664 id2str_class(key_class), id2str_key_type(key_type)); 1665 1666 if (function == PKCS11_FUNCTION_WRAP) 1667 return PKCS11_CKR_WRAPPING_KEY_TYPE_INCONSISTENT; 1668 else if (function == PKCS11_FUNCTION_UNWRAP) 1669 return PKCS11_CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT; 1670 else 1671 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1672 1673 case PKCS11_CKM_AES_ECB_ENCRYPT_DATA: 1674 case PKCS11_CKM_AES_CBC_ENCRYPT_DATA: 1675 if (key_class != PKCS11_CKO_SECRET_KEY && 1676 key_type != PKCS11_CKK_AES) 1677 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1678 1679 if (get_bool(head, PKCS11_CKA_ENCRYPT)) { 1680 /* 1681 * Intentionally refuse to proceed despite 1682 * PKCS#11 specifications v2.40 and v3.0 not expecting 1683 * this behavior to avoid potential security issue 1684 * where keys derived by these mechanisms can be 1685 * revealed by doing data encryption using parent key. 1686 */ 1687 return PKCS11_CKR_FUNCTION_FAILED; 1688 } 1689 1690 break; 1691 case PKCS11_CKM_MD5_HMAC: 1692 case PKCS11_CKM_SHA_1_HMAC: 1693 case PKCS11_CKM_SHA224_HMAC: 1694 case PKCS11_CKM_SHA256_HMAC: 1695 case PKCS11_CKM_SHA384_HMAC: 1696 case PKCS11_CKM_SHA512_HMAC: 1697 case PKCS11_CKM_MD5_HMAC_GENERAL: 1698 case PKCS11_CKM_SHA_1_HMAC_GENERAL: 1699 case PKCS11_CKM_SHA224_HMAC_GENERAL: 1700 case PKCS11_CKM_SHA256_HMAC_GENERAL: 1701 case PKCS11_CKM_SHA384_HMAC_GENERAL: 1702 case PKCS11_CKM_SHA512_HMAC_GENERAL: 1703 if (key_class != PKCS11_CKO_SECRET_KEY) 1704 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1705 1706 if (key_type == PKCS11_CKK_GENERIC_SECRET) 1707 break; 1708 1709 switch (proc_id) { 1710 case PKCS11_CKM_MD5_HMAC: 1711 case PKCS11_CKM_MD5_HMAC_GENERAL: 1712 if (key_type == PKCS11_CKK_MD5_HMAC) 1713 break; 1714 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1715 case PKCS11_CKM_SHA_1_HMAC: 1716 case PKCS11_CKM_SHA_1_HMAC_GENERAL: 1717 if (key_type == PKCS11_CKK_SHA_1_HMAC) 1718 break; 1719 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1720 case PKCS11_CKM_SHA224_HMAC: 1721 case PKCS11_CKM_SHA224_HMAC_GENERAL: 1722 if (key_type == PKCS11_CKK_SHA224_HMAC) 1723 break; 1724 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1725 case PKCS11_CKM_SHA256_HMAC: 1726 case PKCS11_CKM_SHA256_HMAC_GENERAL: 1727 if (key_type == PKCS11_CKK_SHA256_HMAC) 1728 break; 1729 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1730 case PKCS11_CKM_SHA384_HMAC: 1731 case PKCS11_CKM_SHA384_HMAC_GENERAL: 1732 if (key_type == PKCS11_CKK_SHA384_HMAC) 1733 break; 1734 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1735 case PKCS11_CKM_SHA512_HMAC: 1736 case PKCS11_CKM_SHA512_HMAC_GENERAL: 1737 if (key_type == PKCS11_CKK_SHA512_HMAC) 1738 break; 1739 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1740 default: 1741 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1742 } 1743 break; 1744 1745 case PKCS11_CKM_ECDSA: 1746 case PKCS11_CKM_ECDSA_SHA1: 1747 case PKCS11_CKM_ECDSA_SHA224: 1748 case PKCS11_CKM_ECDSA_SHA256: 1749 case PKCS11_CKM_ECDSA_SHA384: 1750 case PKCS11_CKM_ECDSA_SHA512: 1751 case PKCS11_CKM_ECDH1_DERIVE: 1752 if (key_type != PKCS11_CKK_EC) { 1753 EMSG("Invalid key %s for mechanism %s", 1754 id2str_type(key_type, key_class), 1755 id2str_proc(proc_id)); 1756 1757 return PKCS11_CKR_KEY_TYPE_INCONSISTENT; 1758 } 1759 if (key_class != PKCS11_CKO_PUBLIC_KEY && 1760 key_class != PKCS11_CKO_PRIVATE_KEY) { 1761 EMSG("Invalid key class for mechanism %s", 1762 id2str_proc(proc_id)); 1763 1764 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1765 } 1766 break; 1767 case PKCS11_CKM_RSA_PKCS: 1768 case PKCS11_CKM_MD5_RSA_PKCS: 1769 case PKCS11_CKM_SHA1_RSA_PKCS: 1770 case PKCS11_CKM_SHA224_RSA_PKCS: 1771 case PKCS11_CKM_SHA256_RSA_PKCS: 1772 case PKCS11_CKM_SHA384_RSA_PKCS: 1773 case PKCS11_CKM_SHA512_RSA_PKCS: 1774 case PKCS11_CKM_RSA_PKCS_OAEP: 1775 case PKCS11_CKM_RSA_PKCS_PSS: 1776 case PKCS11_CKM_SHA1_RSA_PKCS_PSS: 1777 case PKCS11_CKM_SHA224_RSA_PKCS_PSS: 1778 case PKCS11_CKM_SHA256_RSA_PKCS_PSS: 1779 case PKCS11_CKM_SHA384_RSA_PKCS_PSS: 1780 case PKCS11_CKM_SHA512_RSA_PKCS_PSS: 1781 if (key_type != PKCS11_CKK_RSA) { 1782 EMSG("Invalid key %s for mechanism %s", 1783 id2str_type(key_type, key_class), 1784 id2str_proc(proc_id)); 1785 1786 return PKCS11_CKR_KEY_TYPE_INCONSISTENT; 1787 } 1788 if (key_class != PKCS11_CKO_PUBLIC_KEY && 1789 key_class != PKCS11_CKO_PRIVATE_KEY) { 1790 EMSG("Invalid key class for mechanism %s", 1791 id2str_proc(proc_id)); 1792 1793 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1794 } 1795 break; 1796 default: 1797 DMSG("Invalid processing %#"PRIx32"/%s", proc_id, 1798 id2str_proc(proc_id)); 1799 1800 return PKCS11_CKR_MECHANISM_INVALID; 1801 } 1802 1803 if (!parent_key_complies_allowed_processings(proc_id, head)) { 1804 DMSG("Allowed mechanism failed"); 1805 return PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED; 1806 } 1807 1808 return PKCS11_CKR_OK; 1809 } 1810 1811 bool attribute_is_exportable(struct pkcs11_attribute_head *req_attr, 1812 struct pkcs11_object *obj) 1813 { 1814 uint8_t boolval = 0; 1815 uint32_t boolsize = 0; 1816 enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR; 1817 enum pkcs11_class_id key_class = get_class(obj->attributes); 1818 1819 if (key_class != PKCS11_CKO_SECRET_KEY && 1820 key_class != PKCS11_CKO_PRIVATE_KEY) 1821 return true; 1822 1823 switch (req_attr->id) { 1824 case PKCS11_CKA_PRIVATE_EXPONENT: 1825 case PKCS11_CKA_PRIME_1: 1826 case PKCS11_CKA_PRIME_2: 1827 case PKCS11_CKA_EXPONENT_1: 1828 case PKCS11_CKA_EXPONENT_2: 1829 case PKCS11_CKA_COEFFICIENT: 1830 case PKCS11_CKA_VALUE: 1831 boolsize = sizeof(boolval); 1832 rc = get_attribute(obj->attributes, PKCS11_CKA_EXTRACTABLE, 1833 &boolval, &boolsize); 1834 if (rc || boolval == PKCS11_FALSE) 1835 return false; 1836 1837 boolsize = sizeof(boolval); 1838 rc = get_attribute(obj->attributes, PKCS11_CKA_SENSITIVE, 1839 &boolval, &boolsize); 1840 if (rc || boolval == PKCS11_TRUE) 1841 return false; 1842 break; 1843 default: 1844 break; 1845 } 1846 1847 return true; 1848 } 1849 1850 static bool attr_is_modifiable_any_key(struct pkcs11_attribute_head *attr) 1851 { 1852 switch (attr->id) { 1853 case PKCS11_CKA_ID: 1854 case PKCS11_CKA_START_DATE: 1855 case PKCS11_CKA_END_DATE: 1856 case PKCS11_CKA_DERIVE: 1857 return true; 1858 default: 1859 return false; 1860 } 1861 } 1862 1863 static bool attr_is_modifiable_secret_key(struct pkcs11_attribute_head *attr, 1864 struct pkcs11_session *session, 1865 struct pkcs11_object *obj) 1866 { 1867 switch (attr->id) { 1868 case PKCS11_CKA_ENCRYPT: 1869 case PKCS11_CKA_DECRYPT: 1870 case PKCS11_CKA_SIGN: 1871 case PKCS11_CKA_VERIFY: 1872 case PKCS11_CKA_WRAP: 1873 case PKCS11_CKA_UNWRAP: 1874 return true; 1875 /* Can't be modified once set to CK_FALSE - 12 in Table 10 */ 1876 case PKCS11_CKA_EXTRACTABLE: 1877 return get_bool(obj->attributes, attr->id); 1878 /* Can't be modified once set to CK_TRUE - 11 in Table 10 */ 1879 case PKCS11_CKA_SENSITIVE: 1880 case PKCS11_CKA_WRAP_WITH_TRUSTED: 1881 return !get_bool(obj->attributes, attr->id); 1882 /* Change in CKA_TRUSTED can only be done by SO */ 1883 case PKCS11_CKA_TRUSTED: 1884 return pkcs11_session_is_so(session); 1885 case PKCS11_CKA_NEVER_EXTRACTABLE: 1886 case PKCS11_CKA_ALWAYS_SENSITIVE: 1887 return false; 1888 default: 1889 return false; 1890 } 1891 } 1892 1893 static bool attr_is_modifiable_public_key(struct pkcs11_attribute_head *attr, 1894 struct pkcs11_session *session, 1895 struct pkcs11_object *obj __unused) 1896 { 1897 switch (attr->id) { 1898 case PKCS11_CKA_SUBJECT: 1899 case PKCS11_CKA_ENCRYPT: 1900 case PKCS11_CKA_VERIFY: 1901 case PKCS11_CKA_VERIFY_RECOVER: 1902 case PKCS11_CKA_WRAP: 1903 return true; 1904 case PKCS11_CKA_TRUSTED: 1905 /* Change in CKA_TRUSTED can only be done by SO */ 1906 return pkcs11_session_is_so(session); 1907 default: 1908 return false; 1909 } 1910 } 1911 1912 static bool attr_is_modifiable_private_key(struct pkcs11_attribute_head *attr, 1913 struct pkcs11_session *sess __unused, 1914 struct pkcs11_object *obj) 1915 { 1916 switch (attr->id) { 1917 case PKCS11_CKA_SUBJECT: 1918 case PKCS11_CKA_DECRYPT: 1919 case PKCS11_CKA_SIGN: 1920 case PKCS11_CKA_SIGN_RECOVER: 1921 case PKCS11_CKA_UNWRAP: 1922 /* 1923 * TBD: Revisit if we don't support PKCS11_CKA_PUBLIC_KEY_INFO 1924 * Specification mentions that if this attribute is 1925 * supplied as part of a template for C_CreateObject, C_CopyObject or 1926 * C_SetAttributeValue for a private key, the token MUST verify 1927 * correspondence between the private key data and the public key data 1928 * as supplied in CKA_PUBLIC_KEY_INFO. This needs to be 1929 * taken care of when this object type will be implemented 1930 */ 1931 case PKCS11_CKA_PUBLIC_KEY_INFO: 1932 return true; 1933 /* Can't be modified once set to CK_FALSE - 12 in Table 10 */ 1934 case PKCS11_CKA_EXTRACTABLE: 1935 return get_bool(obj->attributes, attr->id); 1936 /* Can't be modified once set to CK_TRUE - 11 in Table 10 */ 1937 case PKCS11_CKA_SENSITIVE: 1938 case PKCS11_CKA_WRAP_WITH_TRUSTED: 1939 return !get_bool(obj->attributes, attr->id); 1940 case PKCS11_CKA_NEVER_EXTRACTABLE: 1941 case PKCS11_CKA_ALWAYS_SENSITIVE: 1942 return false; 1943 default: 1944 return false; 1945 } 1946 } 1947 1948 static bool attr_is_modifiable_certificate(struct pkcs11_attribute_head *attr, 1949 struct pkcs11_session *session, 1950 struct pkcs11_object *obj) 1951 { 1952 uint8_t boolval = 0; 1953 uint32_t boolsize = 0; 1954 enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR; 1955 1956 /* Trusted certificates cannot be modified. */ 1957 rc = get_attribute(obj->attributes, PKCS11_CKA_TRUSTED, 1958 &boolval, &boolsize); 1959 if (rc || boolval == PKCS11_TRUE) 1960 return false; 1961 1962 /* Common certificate attributes */ 1963 switch (attr->id) { 1964 case PKCS11_CKA_TRUSTED: 1965 /* 1966 * The CKA_TRUSTED attribute cannot be set to CK_TRUE by an 1967 * application. It MUST be set by a token initialization 1968 * application or by the token’s SO. 1969 */ 1970 return pkcs11_session_is_so(session); 1971 case PKCS11_CKA_CERTIFICATE_TYPE: 1972 case PKCS11_CKA_CERTIFICATE_CATEGORY: 1973 return false; 1974 default: 1975 break; 1976 } 1977 1978 /* Certificate type specific attributes */ 1979 switch (get_certificate_type(obj->attributes)) { 1980 case PKCS11_CKC_X_509: 1981 /* 1982 * Only the CKA_ID, CKA_ISSUER, and CKA_SERIAL_NUMBER 1983 * attributes may be modified after the object is created. 1984 */ 1985 switch (attr->id) { 1986 case PKCS11_CKA_ID: 1987 case PKCS11_CKA_ISSUER: 1988 case PKCS11_CKA_SERIAL_NUMBER: 1989 return true; 1990 default: 1991 break; 1992 } 1993 break; 1994 default: 1995 /* Unsupported certificate type */ 1996 break; 1997 } 1998 1999 return false; 2000 } 2001 2002 static bool attribute_is_modifiable(struct pkcs11_session *session, 2003 struct pkcs11_attribute_head *req_attr, 2004 struct pkcs11_object *obj, 2005 enum pkcs11_class_id class, 2006 enum processing_func function) 2007 { 2008 /* Check modifiable attributes common to any object */ 2009 switch (req_attr->id) { 2010 case PKCS11_CKA_LABEL: 2011 return true; 2012 case PKCS11_CKA_TOKEN: 2013 case PKCS11_CKA_MODIFIABLE: 2014 case PKCS11_CKA_DESTROYABLE: 2015 case PKCS11_CKA_PRIVATE: 2016 return function == PKCS11_FUNCTION_COPY; 2017 case PKCS11_CKA_COPYABLE: 2018 /* 2019 * Specification mentions that if the attribute value is false 2020 * it can't be set to true. Reading this we assume that it 2021 * should be possible to modify this attribute even though this 2022 * is not marked as modifiable in Table 10 if done in right 2023 * direction i.e from TRUE -> FALSE. 2024 */ 2025 return get_bool(obj->attributes, req_attr->id); 2026 default: 2027 break; 2028 } 2029 2030 /* Attribute checking based on class type */ 2031 switch (class) { 2032 case PKCS11_CKO_SECRET_KEY: 2033 case PKCS11_CKO_PUBLIC_KEY: 2034 case PKCS11_CKO_PRIVATE_KEY: 2035 if (attr_is_modifiable_any_key(req_attr)) 2036 return true; 2037 if (class == PKCS11_CKO_SECRET_KEY && 2038 attr_is_modifiable_secret_key(req_attr, session, obj)) 2039 return true; 2040 if (class == PKCS11_CKO_PUBLIC_KEY && 2041 attr_is_modifiable_public_key(req_attr, session, obj)) 2042 return true; 2043 if (class == PKCS11_CKO_PRIVATE_KEY && 2044 attr_is_modifiable_private_key(req_attr, session, obj)) 2045 return true; 2046 break; 2047 case PKCS11_CKO_DATA: 2048 /* None of the data object attributes are modifiable */ 2049 return false; 2050 case PKCS11_CKO_CERTIFICATE: 2051 return attr_is_modifiable_certificate(req_attr, session, obj); 2052 default: 2053 break; 2054 } 2055 2056 return false; 2057 } 2058 2059 enum pkcs11_rc check_attrs_against_modification(struct pkcs11_session *session, 2060 struct obj_attrs *head, 2061 struct pkcs11_object *obj, 2062 enum processing_func function) 2063 { 2064 enum pkcs11_class_id class = PKCS11_CKO_UNDEFINED_ID; 2065 char *cur = NULL; 2066 char *end = NULL; 2067 size_t len = 0; 2068 2069 class = get_class(obj->attributes); 2070 2071 cur = (char *)head + sizeof(struct obj_attrs); 2072 end = cur + head->attrs_size; 2073 2074 for (; cur < end; cur += len) { 2075 /* Structure aligned copy of the pkcs11_ref in the object */ 2076 struct pkcs11_attribute_head cli_ref = { }; 2077 2078 TEE_MemMove(&cli_ref, cur, sizeof(cli_ref)); 2079 len = sizeof(cli_ref) + cli_ref.size; 2080 2081 /* 2082 * Check 1 - Check if attribute belongs to the object 2083 * The obj->attributes has all the attributes in 2084 * it which are allowed for an object. 2085 */ 2086 if (get_attribute_ptr(obj->attributes, cli_ref.id, NULL, 2087 NULL) == PKCS11_RV_NOT_FOUND) 2088 return PKCS11_CKR_ATTRIBUTE_TYPE_INVALID; 2089 2090 /* Check 2 - Is attribute modifiable */ 2091 if (!attribute_is_modifiable(session, &cli_ref, obj, class, 2092 function)) 2093 return PKCS11_CKR_ATTRIBUTE_READ_ONLY; 2094 2095 /* 2096 * Checks for modification in PKCS11_CKA_TOKEN and 2097 * PKCS11_CKA_PRIVATE are required for PKCS11_FUNCTION_COPY 2098 * only, so skip them for PKCS11_FUNCTION_MODIFY. 2099 */ 2100 if (function == PKCS11_FUNCTION_MODIFY) 2101 continue; 2102 2103 /* 2104 * An attempt to copy an object to a token will fail for 2105 * RO session 2106 */ 2107 if (cli_ref.id == PKCS11_CKA_TOKEN && 2108 get_bool(head, PKCS11_CKA_TOKEN)) { 2109 if (!pkcs11_session_is_read_write(session)) { 2110 DMSG("Can't copy to token in a RO session"); 2111 return PKCS11_CKR_SESSION_READ_ONLY; 2112 } 2113 } 2114 2115 if (cli_ref.id == PKCS11_CKA_PRIVATE) { 2116 bool parent_priv = 2117 get_bool(obj->attributes, cli_ref.id); 2118 bool obj_priv = get_bool(head, cli_ref.id); 2119 2120 /* 2121 * If PKCS11_CKA_PRIVATE is being set to TRUE from 2122 * FALSE, user has to be logged in 2123 */ 2124 if (!parent_priv && obj_priv) { 2125 if ((pkcs11_session_is_public(session) || 2126 pkcs11_session_is_so(session))) 2127 return PKCS11_CKR_USER_NOT_LOGGED_IN; 2128 } 2129 2130 /* 2131 * Restriction added - Even for Copy, do not allow 2132 * modification of CKA_PRIVATE from TRUE to FALSE 2133 */ 2134 if (parent_priv && !obj_priv) 2135 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 2136 } 2137 } 2138 2139 return PKCS11_CKR_OK; 2140 } 2141 2142 static enum pkcs11_rc set_secret_key_data(struct obj_attrs **head, void *data, 2143 size_t key_size) 2144 { 2145 uint32_t size = sizeof(uint32_t); 2146 uint32_t key_length = 0; 2147 enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR; 2148 2149 /* Get key size if present in template */ 2150 rc = get_attribute(*head, PKCS11_CKA_VALUE_LEN, &key_length, &size); 2151 if (rc && rc != PKCS11_RV_NOT_FOUND) 2152 return rc; 2153 2154 if (key_length) { 2155 if (key_size < key_length) 2156 return PKCS11_CKR_DATA_LEN_RANGE; 2157 } else { 2158 key_length = key_size; 2159 rc = set_attribute(head, PKCS11_CKA_VALUE_LEN, &key_length, 2160 sizeof(uint32_t)); 2161 if (rc) 2162 return rc; 2163 } 2164 2165 /* Now we can check the VALUE_LEN field */ 2166 rc = check_created_attrs(*head, NULL); 2167 if (rc) 2168 return rc; 2169 2170 /* Remove the default empty value attribute if found */ 2171 rc = remove_empty_attribute(head, PKCS11_CKA_VALUE); 2172 if (rc != PKCS11_CKR_OK && rc != PKCS11_RV_NOT_FOUND) 2173 return PKCS11_CKR_GENERAL_ERROR; 2174 2175 return add_attribute(head, PKCS11_CKA_VALUE, data, key_length); 2176 } 2177 2178 enum pkcs11_rc set_key_data(struct obj_attrs **head, void *data, 2179 size_t key_size) 2180 { 2181 switch (get_class(*head)) { 2182 case PKCS11_CKO_SECRET_KEY: 2183 return set_secret_key_data(head, data, key_size); 2184 default: 2185 return PKCS11_CKR_GENERAL_ERROR; 2186 } 2187 } 2188 2189 enum pkcs11_rc get_key_data_to_wrap(struct obj_attrs *head, void **data, 2190 uint32_t *sz) 2191 { 2192 switch (get_class(head)) { 2193 case PKCS11_CKO_SECRET_KEY: 2194 if (get_attribute_ptr(head, PKCS11_CKA_VALUE, data, sz)) 2195 return PKCS11_CKR_ARGUMENTS_BAD; 2196 break; 2197 default: 2198 return PKCS11_CKR_GENERAL_ERROR; 2199 } 2200 2201 return PKCS11_CKR_OK; 2202 } 2203 2204 enum pkcs11_rc add_missing_attribute_id(struct obj_attrs **pub_head, 2205 struct obj_attrs **priv_head) 2206 { 2207 enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR; 2208 void *id1 = NULL; 2209 uint32_t id1_size = 0; 2210 void *id2 = NULL; 2211 uint32_t id2_size = 0; 2212 2213 assert(pub_head); 2214 assert(priv_head); 2215 2216 rc = get_attribute_ptr(*pub_head, PKCS11_CKA_ID, &id1, &id1_size); 2217 if (rc) { 2218 if (rc != PKCS11_RV_NOT_FOUND) 2219 return rc; 2220 id1 = NULL; 2221 } else if (!id1_size) { 2222 id1 = NULL; 2223 } 2224 2225 rc = get_attribute_ptr(*priv_head, PKCS11_CKA_ID, &id2, &id2_size); 2226 if (rc) { 2227 if (rc != PKCS11_RV_NOT_FOUND) 2228 return rc; 2229 id2 = NULL; 2230 } else if (!id2_size) { 2231 id2 = NULL; 2232 } 2233 2234 /* Both have value -- let them be what caller has specified them */ 2235 if (id1 && id2) 2236 return PKCS11_CKR_OK; 2237 2238 /* Both are empty -- leave empty values */ 2239 if (!id1 && !id2) 2240 return PKCS11_CKR_OK; 2241 2242 /* Cross copy CKA_ID value */ 2243 if (id1) 2244 return set_attribute(priv_head, PKCS11_CKA_ID, id1, id1_size); 2245 else 2246 return set_attribute(pub_head, PKCS11_CKA_ID, id2, id2_size); 2247 } 2248