1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 */ 5 #include <stdlib.h> 6 #include <string.h> 7 8 #include <tee_api.h> 9 #include <utee_syscalls.h> 10 #include "tee_api_private.h" 11 12 #define TEE_USAGE_DEFAULT 0xffffffff 13 14 void __utee_from_attr(struct utee_attribute *ua, const TEE_Attribute *attrs, 15 uint32_t attr_count) 16 { 17 size_t n; 18 19 for (n = 0; n < attr_count; n++) { 20 ua[n].attribute_id = attrs[n].attributeID; 21 if (attrs[n].attributeID & TEE_ATTR_FLAG_VALUE) { 22 ua[n].a = attrs[n].content.value.a; 23 ua[n].b = attrs[n].content.value.b; 24 } else { 25 ua[n].a = (uintptr_t)attrs[n].content.ref.buffer; 26 ua[n].b = attrs[n].content.ref.length; 27 } 28 } 29 } 30 31 void __utee_from_gp11_attr(struct utee_attribute *ua, 32 const __GP11_TEE_Attribute *attrs, 33 uint32_t attr_count) 34 { 35 size_t n = 0; 36 37 for (n = 0; n < attr_count; n++) { 38 ua[n].attribute_id = attrs[n].attributeID; 39 if (attrs[n].attributeID & TEE_ATTR_FLAG_VALUE) { 40 ua[n].a = attrs[n].content.value.a; 41 ua[n].b = attrs[n].content.value.b; 42 } else { 43 ua[n].a = (uintptr_t)attrs[n].content.ref.buffer; 44 ua[n].b = attrs[n].content.ref.length; 45 } 46 } 47 } 48 49 /* Data and Key Storage API - Generic Object Functions */ 50 /* 51 * Use of this function is deprecated 52 * new code SHOULD use the TEE_GetObjectInfo1 function instead 53 * These functions will be removed at some future major revision of 54 * this specification 55 */ 56 void TEE_GetObjectInfo(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo) 57 { 58 struct utee_object_info info = { }; 59 TEE_Result res = TEE_SUCCESS; 60 61 res = _utee_cryp_obj_get_info((unsigned long)object, &info); 62 63 if (res != TEE_SUCCESS) 64 TEE_Panic(res); 65 66 if (info.obj_type == TEE_TYPE_CORRUPTED_OBJECT) { 67 objectInfo->objectSize = 0; 68 objectInfo->maxObjectSize = 0; 69 objectInfo->objectUsage = 0; 70 objectInfo->dataSize = 0; 71 objectInfo->dataPosition = 0; 72 objectInfo->handleFlags = 0; 73 } else { 74 objectInfo->objectType = info.obj_type; 75 objectInfo->objectSize = info.obj_size; 76 objectInfo->maxObjectSize = info.max_obj_size; 77 objectInfo->objectUsage = info.obj_usage; 78 objectInfo->dataSize = info.data_size; 79 objectInfo->dataPosition = info.data_pos; 80 objectInfo->handleFlags = info.handle_flags; 81 } 82 } 83 84 void __GP11_TEE_GetObjectInfo(TEE_ObjectHandle object, 85 __GP11_TEE_ObjectInfo *objectInfo) 86 { 87 struct utee_object_info info = { }; 88 TEE_Result res = TEE_SUCCESS; 89 90 res = _utee_cryp_obj_get_info((unsigned long)object, &info); 91 92 if (res != TEE_SUCCESS) 93 TEE_Panic(res); 94 95 if (info.obj_type == TEE_TYPE_CORRUPTED_OBJECT) { 96 objectInfo->keySize = 0; 97 objectInfo->maxKeySize = 0; 98 objectInfo->objectUsage = 0; 99 objectInfo->dataSize = 0; 100 objectInfo->dataPosition = 0; 101 objectInfo->handleFlags = 0; 102 } else { 103 objectInfo->objectType = info.obj_type; 104 objectInfo->keySize = info.obj_size; 105 objectInfo->maxKeySize = info.max_obj_size; 106 objectInfo->objectUsage = info.obj_usage; 107 objectInfo->dataSize = info.data_size; 108 objectInfo->dataPosition = info.data_pos; 109 objectInfo->handleFlags = info.handle_flags; 110 } 111 } 112 113 TEE_Result TEE_GetObjectInfo1(TEE_ObjectHandle object, 114 TEE_ObjectInfo *objectInfo) 115 { 116 struct utee_object_info info = { }; 117 TEE_Result res = TEE_SUCCESS; 118 119 res = _utee_cryp_obj_get_info((unsigned long)object, &info); 120 121 if (res != TEE_SUCCESS && 122 res != TEE_ERROR_CORRUPT_OBJECT && 123 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 124 TEE_Panic(res); 125 126 objectInfo->objectType = info.obj_type; 127 objectInfo->objectSize = info.obj_size; 128 objectInfo->maxObjectSize = info.max_obj_size; 129 objectInfo->objectUsage = info.obj_usage; 130 objectInfo->dataSize = info.data_size; 131 objectInfo->dataPosition = info.data_pos; 132 objectInfo->handleFlags = info.handle_flags; 133 134 return res; 135 } 136 137 TEE_Result __GP11_TEE_GetObjectInfo1(TEE_ObjectHandle object, 138 __GP11_TEE_ObjectInfo *objectInfo) 139 { 140 struct utee_object_info info = { }; 141 TEE_Result res = TEE_SUCCESS; 142 143 res = _utee_cryp_obj_get_info((unsigned long)object, &info); 144 145 if (res != TEE_SUCCESS && 146 res != TEE_ERROR_CORRUPT_OBJECT && 147 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 148 TEE_Panic(res); 149 150 objectInfo->objectType = info.obj_type; 151 objectInfo->keySize = info.obj_size; 152 objectInfo->maxKeySize = info.max_obj_size; 153 objectInfo->objectUsage = info.obj_usage; 154 objectInfo->dataSize = info.data_size; 155 objectInfo->dataPosition = info.data_pos; 156 objectInfo->handleFlags = info.handle_flags; 157 158 return res; 159 } 160 161 /* 162 * Use of this function is deprecated 163 * new code SHOULD use the TEE_RestrictObjectUsage1 function instead 164 * These functions will be removed at some future major revision of 165 * this specification 166 */ 167 void TEE_RestrictObjectUsage(TEE_ObjectHandle object, uint32_t objectUsage) 168 { 169 struct utee_object_info info = { }; 170 TEE_Result res = TEE_SUCCESS; 171 172 res = _utee_cryp_obj_get_info((unsigned long)object, &info); 173 if (info.obj_type == TEE_TYPE_CORRUPTED_OBJECT) 174 return; 175 176 res = TEE_RestrictObjectUsage1(object, objectUsage); 177 178 if (res != TEE_SUCCESS) 179 TEE_Panic(res); 180 } 181 182 TEE_Result TEE_RestrictObjectUsage1(TEE_ObjectHandle object, uint32_t objectUsage) 183 { 184 TEE_Result res; 185 186 res = _utee_cryp_obj_restrict_usage((unsigned long)object, 187 objectUsage); 188 189 if (res != TEE_SUCCESS && 190 res != TEE_ERROR_CORRUPT_OBJECT && 191 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 192 TEE_Panic(res); 193 194 return res; 195 } 196 197 TEE_Result TEE_GetObjectBufferAttribute(TEE_ObjectHandle object, 198 uint32_t attributeID, void *buffer, 199 size_t *size) 200 { 201 struct utee_object_info info = { }; 202 TEE_Result res = TEE_SUCCESS; 203 uint64_t sz = 0; 204 205 __utee_check_inout_annotation(size, sizeof(*size)); 206 207 res = _utee_cryp_obj_get_info((unsigned long)object, &info); 208 if (res != TEE_SUCCESS) 209 goto exit; 210 211 /* This function only supports reference attributes */ 212 if ((attributeID & TEE_ATTR_FLAG_VALUE)) { 213 res = TEE_ERROR_BAD_PARAMETERS; 214 goto exit; 215 } 216 217 sz = *size; 218 res = _utee_cryp_obj_get_attr((unsigned long)object, attributeID, 219 buffer, &sz); 220 *size = sz; 221 222 exit: 223 if (res != TEE_SUCCESS && 224 res != TEE_ERROR_ITEM_NOT_FOUND && 225 res != TEE_ERROR_SHORT_BUFFER && 226 res != TEE_ERROR_CORRUPT_OBJECT && 227 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 228 TEE_Panic(res); 229 230 return res; 231 } 232 233 TEE_Result __GP11_TEE_GetObjectBufferAttribute(TEE_ObjectHandle object, 234 uint32_t attributeID, 235 void *buffer, uint32_t *size) 236 { 237 TEE_Result res = TEE_SUCCESS; 238 size_t l = 0; 239 240 __utee_check_inout_annotation(size, sizeof(*size)); 241 l = *size; 242 res = TEE_GetObjectBufferAttribute(object, attributeID, buffer, &l); 243 *size = l; 244 return res; 245 } 246 247 TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object, 248 uint32_t attributeID, uint32_t *a, 249 uint32_t *b) 250 { 251 struct utee_object_info info = { }; 252 TEE_Result res = TEE_SUCCESS; 253 uint32_t buf[2]; 254 uint64_t size = sizeof(buf); 255 256 if (a) 257 __utee_check_out_annotation(a, sizeof(*a)); 258 if (b) 259 __utee_check_out_annotation(b, sizeof(*b)); 260 261 res = _utee_cryp_obj_get_info((unsigned long)object, &info); 262 if (res != TEE_SUCCESS) 263 goto exit; 264 265 /* This function only supports value attributes */ 266 if (!(attributeID & TEE_ATTR_FLAG_VALUE)) { 267 res = TEE_ERROR_BAD_PARAMETERS; 268 goto exit; 269 } 270 271 res = _utee_cryp_obj_get_attr((unsigned long)object, attributeID, buf, 272 &size); 273 274 exit: 275 if (res != TEE_SUCCESS && 276 res != TEE_ERROR_ITEM_NOT_FOUND && 277 res != TEE_ERROR_CORRUPT_OBJECT && 278 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 279 TEE_Panic(res); 280 281 if (size != sizeof(buf)) 282 TEE_Panic(0); 283 284 if (res == TEE_SUCCESS) { 285 if (a) 286 *a = buf[0]; 287 if (b) 288 *b = buf[1]; 289 } 290 291 return res; 292 } 293 294 void TEE_CloseObject(TEE_ObjectHandle object) 295 { 296 TEE_Result res; 297 298 if (object == TEE_HANDLE_NULL) 299 return; 300 301 res = _utee_cryp_obj_close((unsigned long)object); 302 if (res != TEE_SUCCESS) 303 TEE_Panic(res); 304 } 305 306 /* Data and Key Storage API - Transient Object Functions */ 307 308 TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType, 309 uint32_t maxObjectSize, 310 TEE_ObjectHandle *object) 311 { 312 if (objectType == TEE_TYPE_DATA) 313 return TEE_ERROR_NOT_SUPPORTED; 314 315 return __GP11_TEE_AllocateTransientObject(objectType, maxObjectSize, 316 object); 317 } 318 319 TEE_Result __GP11_TEE_AllocateTransientObject(TEE_ObjectType objectType, 320 uint32_t maxKeySize, 321 TEE_ObjectHandle *object) 322 { 323 TEE_Result res; 324 uint32_t obj; 325 326 __utee_check_out_annotation(object, sizeof(*object)); 327 328 res = _utee_cryp_obj_alloc(objectType, maxKeySize, &obj); 329 330 if (res != TEE_SUCCESS && 331 res != TEE_ERROR_OUT_OF_MEMORY && 332 res != TEE_ERROR_NOT_SUPPORTED) 333 TEE_Panic(res); 334 335 if (res == TEE_SUCCESS) 336 *object = (TEE_ObjectHandle)(uintptr_t)obj; 337 338 return res; 339 } 340 341 void TEE_FreeTransientObject(TEE_ObjectHandle object) 342 { 343 struct utee_object_info info = { }; 344 TEE_Result res = TEE_SUCCESS; 345 346 if (object == TEE_HANDLE_NULL) 347 return; 348 349 res = _utee_cryp_obj_get_info((unsigned long)object, &info); 350 if (res != TEE_SUCCESS) 351 TEE_Panic(res); 352 353 if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 354 TEE_Panic(0); 355 356 res = _utee_cryp_obj_close((unsigned long)object); 357 if (res != TEE_SUCCESS) 358 TEE_Panic(res); 359 } 360 361 void TEE_ResetTransientObject(TEE_ObjectHandle object) 362 { 363 struct utee_object_info info = { }; 364 TEE_Result res = TEE_SUCCESS; 365 366 if (object == TEE_HANDLE_NULL) 367 return; 368 369 res = _utee_cryp_obj_get_info((unsigned long)object, &info); 370 if (res != TEE_SUCCESS) 371 TEE_Panic(res); 372 373 if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 374 TEE_Panic(0); 375 376 res = _utee_cryp_obj_reset((unsigned long)object); 377 if (res != TEE_SUCCESS) 378 TEE_Panic(res); 379 } 380 381 TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object, 382 const TEE_Attribute *attrs, 383 uint32_t attrCount) 384 { 385 struct utee_attribute ua[attrCount]; 386 struct utee_object_info info = { }; 387 TEE_Result res = TEE_SUCCESS; 388 389 __utee_check_attr_in_annotation(attrs, attrCount); 390 391 res = _utee_cryp_obj_get_info((unsigned long)object, &info); 392 if (res != TEE_SUCCESS) 393 TEE_Panic(res); 394 395 /* Must be a transient object */ 396 if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 397 TEE_Panic(0); 398 399 /* Must not be initialized already */ 400 if ((info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 401 TEE_Panic(0); 402 403 __utee_from_attr(ua, attrs, attrCount); 404 res = _utee_cryp_obj_populate((unsigned long)object, ua, attrCount); 405 if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 406 TEE_Panic(res); 407 return res; 408 } 409 410 TEE_Result __GP11_TEE_PopulateTransientObject(TEE_ObjectHandle object, 411 const __GP11_TEE_Attribute *attrs, 412 uint32_t attrCount) 413 { 414 struct utee_attribute ua[attrCount]; 415 struct utee_object_info info = { }; 416 TEE_Result res = TEE_SUCCESS; 417 418 __utee_check_gp11_attr_in_annotation(attrs, attrCount); 419 420 res = _utee_cryp_obj_get_info((unsigned long)object, &info); 421 if (res != TEE_SUCCESS) 422 TEE_Panic(res); 423 424 /* Must be a transient object */ 425 if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 426 TEE_Panic(0); 427 428 /* Must not be initialized already */ 429 if ((info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 430 TEE_Panic(0); 431 432 __utee_from_gp11_attr(ua, attrs, attrCount); 433 res = _utee_cryp_obj_populate((unsigned long)object, ua, attrCount); 434 if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 435 TEE_Panic(res); 436 return res; 437 } 438 439 void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID, 440 const void *buffer, size_t length) 441 { 442 __utee_check_out_annotation(attr, sizeof(*attr)); 443 444 if ((attributeID & TEE_ATTR_FLAG_VALUE) != 0) 445 TEE_Panic(0); 446 attr->attributeID = attributeID; 447 attr->content.ref.buffer = (void *)buffer; 448 attr->content.ref.length = length; 449 } 450 451 void __GP11_TEE_InitRefAttribute(__GP11_TEE_Attribute *attr, 452 uint32_t attributeID, 453 const void *buffer, uint32_t length) 454 { 455 __utee_check_out_annotation(attr, sizeof(*attr)); 456 457 if ((attributeID & TEE_ATTR_FLAG_VALUE) != 0) 458 TEE_Panic(0); 459 attr->attributeID = attributeID; 460 attr->content.ref.buffer = (void *)buffer; 461 attr->content.ref.length = length; 462 } 463 464 void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID, 465 uint32_t a, uint32_t b) 466 { 467 __utee_check_out_annotation(attr, sizeof(*attr)); 468 469 if ((attributeID & TEE_ATTR_FLAG_VALUE) == 0) 470 TEE_Panic(0); 471 attr->attributeID = attributeID; 472 attr->content.value.a = a; 473 attr->content.value.b = b; 474 } 475 476 void __GP11_TEE_InitValueAttribute(__GP11_TEE_Attribute *attr, 477 uint32_t attributeID, 478 uint32_t a, uint32_t b) 479 { 480 __utee_check_out_annotation(attr, sizeof(*attr)); 481 482 if ((attributeID & TEE_ATTR_FLAG_VALUE) == 0) 483 TEE_Panic(0); 484 attr->attributeID = attributeID; 485 attr->content.value.a = a; 486 attr->content.value.b = b; 487 } 488 489 /* 490 * Use of this function is deprecated 491 * new code SHOULD use the TEE_CopyObjectAttributes1 function instead 492 * These functions will be removed at some future major revision of 493 * this specification 494 */ 495 void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject, 496 TEE_ObjectHandle srcObject) 497 { 498 struct utee_object_info src_info = { }; 499 TEE_Result res = TEE_SUCCESS; 500 501 res = _utee_cryp_obj_get_info((unsigned long)srcObject, &src_info); 502 if (src_info.obj_type == TEE_TYPE_CORRUPTED_OBJECT) 503 return; 504 505 res = TEE_CopyObjectAttributes1(destObject, srcObject); 506 if (res != TEE_SUCCESS) 507 TEE_Panic(res); 508 } 509 510 TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject, 511 TEE_ObjectHandle srcObject) 512 { 513 struct utee_object_info dst_info = { }; 514 struct utee_object_info src_info = { }; 515 TEE_Result res = TEE_SUCCESS; 516 517 res = _utee_cryp_obj_get_info((unsigned long)destObject, &dst_info); 518 if (res != TEE_SUCCESS) 519 goto exit; 520 521 res = _utee_cryp_obj_get_info((unsigned long)srcObject, &src_info); 522 if (res != TEE_SUCCESS) 523 goto exit; 524 525 if (!(src_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED)) 526 TEE_Panic(0); 527 528 if ((dst_info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT)) 529 TEE_Panic(0); 530 531 if ((dst_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED)) 532 TEE_Panic(0); 533 534 res = _utee_cryp_obj_copy((unsigned long)destObject, 535 (unsigned long)srcObject); 536 537 exit: 538 if (res != TEE_SUCCESS && 539 res != TEE_ERROR_CORRUPT_OBJECT && 540 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 541 TEE_Panic(res); 542 543 return res; 544 } 545 546 TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize, 547 const TEE_Attribute *params, uint32_t paramCount) 548 { 549 TEE_Result res; 550 struct utee_attribute ua[paramCount]; 551 552 __utee_check_attr_in_annotation(params, paramCount); 553 554 __utee_from_attr(ua, params, paramCount); 555 res = _utee_cryp_obj_generate_key((unsigned long)object, keySize, 556 ua, paramCount); 557 558 if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 559 TEE_Panic(res); 560 561 return res; 562 } 563 564 TEE_Result __GP11_TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize, 565 const __GP11_TEE_Attribute *params, 566 uint32_t paramCount) 567 { 568 TEE_Result res = TEE_SUCCESS; 569 struct utee_attribute ua[paramCount]; 570 571 __utee_check_gp11_attr_in_annotation(params, paramCount); 572 573 __utee_from_gp11_attr(ua, params, paramCount); 574 res = _utee_cryp_obj_generate_key((unsigned long)object, keySize, 575 ua, paramCount); 576 577 if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 578 TEE_Panic(res); 579 580 return res; 581 } 582 583 /* Data and Key Storage API - Persistent Object Functions */ 584 585 TEE_Result TEE_OpenPersistentObject(uint32_t storageID, const void *objectID, 586 size_t objectIDLen, uint32_t flags, 587 TEE_ObjectHandle *object) 588 { 589 TEE_Result res; 590 uint32_t obj; 591 592 __utee_check_out_annotation(object, sizeof(*object)); 593 594 res = _utee_storage_obj_open(storageID, objectID, objectIDLen, flags, 595 &obj); 596 if (res == TEE_SUCCESS) 597 *object = (TEE_ObjectHandle)(uintptr_t)obj; 598 599 if (res != TEE_SUCCESS && 600 res != TEE_ERROR_ITEM_NOT_FOUND && 601 res != TEE_ERROR_ACCESS_CONFLICT && 602 res != TEE_ERROR_OUT_OF_MEMORY && 603 res != TEE_ERROR_CORRUPT_OBJECT && 604 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 605 TEE_Panic(res); 606 607 if (res != TEE_SUCCESS) 608 *object = TEE_HANDLE_NULL; 609 610 return res; 611 } 612 613 TEE_Result __GP11_TEE_OpenPersistentObject(uint32_t storageID, 614 const void *objectID, 615 uint32_t objectIDLen, uint32_t flags, 616 TEE_ObjectHandle *object) 617 { 618 return TEE_OpenPersistentObject(storageID, objectID, objectIDLen, 619 flags, object); 620 } 621 622 TEE_Result TEE_CreatePersistentObject(uint32_t storageID, const void *objectID, 623 uint32_t objectIDLen, uint32_t flags, 624 TEE_ObjectHandle attributes, 625 const void *initialData, 626 uint32_t initialDataLen, 627 TEE_ObjectHandle *object) 628 { 629 TEE_Result res; 630 uint32_t obj; 631 632 __utee_check_out_annotation(object, sizeof(*object)); 633 634 res = _utee_storage_obj_create(storageID, objectID, objectIDLen, flags, 635 (unsigned long)attributes, initialData, 636 initialDataLen, &obj); 637 638 if (res == TEE_SUCCESS) 639 *object = (TEE_ObjectHandle)(uintptr_t)obj; 640 641 if (res != TEE_SUCCESS && 642 res != TEE_ERROR_ITEM_NOT_FOUND && 643 res != TEE_ERROR_ACCESS_CONFLICT && 644 res != TEE_ERROR_OUT_OF_MEMORY && 645 res != TEE_ERROR_STORAGE_NO_SPACE && 646 res != TEE_ERROR_CORRUPT_OBJECT && 647 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 648 TEE_Panic(res); 649 650 if (res != TEE_SUCCESS) 651 *object = TEE_HANDLE_NULL; 652 653 return res; 654 } 655 656 /* 657 * Use of this function is deprecated 658 * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead 659 * These functions will be removed at some future major revision of 660 * this specification 661 */ 662 void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object) 663 { 664 TEE_Result res; 665 666 if (object == TEE_HANDLE_NULL) 667 return; 668 669 res = TEE_CloseAndDeletePersistentObject1(object); 670 671 if (res != TEE_SUCCESS) 672 TEE_Panic(0); 673 } 674 675 TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object) 676 { 677 TEE_Result res; 678 679 if (object == TEE_HANDLE_NULL) 680 return TEE_SUCCESS; 681 682 res = _utee_storage_obj_del((unsigned long)object); 683 684 if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 685 TEE_Panic(res); 686 687 return res; 688 } 689 690 691 TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object, 692 const void *newObjectID, 693 uint32_t newObjectIDLen) 694 { 695 TEE_Result res; 696 697 if (object == TEE_HANDLE_NULL) { 698 res = TEE_ERROR_ITEM_NOT_FOUND; 699 goto out; 700 } 701 702 res = _utee_storage_obj_rename((unsigned long)object, newObjectID, 703 newObjectIDLen); 704 705 out: 706 if (res != TEE_SUCCESS && 707 res != TEE_ERROR_ACCESS_CONFLICT && 708 res != TEE_ERROR_CORRUPT_OBJECT && 709 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 710 TEE_Panic(res); 711 712 return res; 713 } 714 715 TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle * 716 objectEnumerator) 717 { 718 TEE_Result res; 719 uint32_t oe; 720 721 __utee_check_out_annotation(objectEnumerator, 722 sizeof(*objectEnumerator)); 723 724 res = _utee_storage_alloc_enum(&oe); 725 726 if (res != TEE_SUCCESS) 727 oe = TEE_HANDLE_NULL; 728 729 *objectEnumerator = (TEE_ObjectEnumHandle)(uintptr_t)oe; 730 731 if (res != TEE_SUCCESS && 732 res != TEE_ERROR_ACCESS_CONFLICT) 733 TEE_Panic(res); 734 735 return res; 736 } 737 738 void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 739 { 740 TEE_Result res; 741 742 if (objectEnumerator == TEE_HANDLE_NULL) 743 return; 744 745 res = _utee_storage_free_enum((unsigned long)objectEnumerator); 746 747 if (res != TEE_SUCCESS) 748 TEE_Panic(res); 749 } 750 751 void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 752 { 753 TEE_Result res; 754 755 if (objectEnumerator == TEE_HANDLE_NULL) 756 return; 757 758 res = _utee_storage_reset_enum((unsigned long)objectEnumerator); 759 760 if (res != TEE_SUCCESS) 761 TEE_Panic(res); 762 } 763 764 TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle 765 objectEnumerator, 766 uint32_t storageID) 767 { 768 TEE_Result res; 769 770 res = _utee_storage_start_enum((unsigned long)objectEnumerator, 771 storageID); 772 773 if (res != TEE_SUCCESS && 774 res != TEE_ERROR_ITEM_NOT_FOUND && 775 res != TEE_ERROR_CORRUPT_OBJECT && 776 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 777 TEE_Panic(res); 778 779 return res; 780 } 781 782 TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator, 783 TEE_ObjectInfo *objectInfo, 784 void *objectID, uint32_t *objectIDLen) 785 { 786 struct utee_object_info info = { }; 787 TEE_Result res = TEE_SUCCESS; 788 uint64_t len = 0; 789 790 if (objectInfo) 791 __utee_check_out_annotation(objectInfo, sizeof(*objectInfo)); 792 __utee_check_out_annotation(objectIDLen, sizeof(*objectIDLen)); 793 794 if (!objectID) { 795 res = TEE_ERROR_BAD_PARAMETERS; 796 goto out; 797 } 798 799 len = *objectIDLen; 800 res = _utee_storage_next_enum((unsigned long)objectEnumerator, 801 &info, objectID, &len); 802 if (objectInfo) { 803 objectInfo->objectType = info.obj_type; 804 objectInfo->objectSize = info.obj_size; 805 objectInfo->maxObjectSize = info.max_obj_size; 806 objectInfo->objectUsage = info.obj_usage; 807 objectInfo->dataSize = info.data_size; 808 objectInfo->dataPosition = info.data_pos; 809 objectInfo->handleFlags = info.handle_flags; 810 } 811 *objectIDLen = len; 812 813 out: 814 if (res != TEE_SUCCESS && 815 res != TEE_ERROR_ITEM_NOT_FOUND && 816 res != TEE_ERROR_CORRUPT_OBJECT && 817 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 818 TEE_Panic(res); 819 820 return res; 821 } 822 823 TEE_Result 824 __GP11_TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator, 825 __GP11_TEE_ObjectInfo *objectInfo, 826 void *objectID, uint32_t *objectIDLen) 827 { 828 struct utee_object_info info = { }; 829 TEE_Result res = TEE_SUCCESS; 830 uint64_t len = 0; 831 832 if (objectInfo) 833 __utee_check_out_annotation(objectInfo, sizeof(*objectInfo)); 834 __utee_check_out_annotation(objectIDLen, sizeof(*objectIDLen)); 835 836 if (!objectID) { 837 res = TEE_ERROR_BAD_PARAMETERS; 838 goto out; 839 } 840 841 len = *objectIDLen; 842 res = _utee_storage_next_enum((unsigned long)objectEnumerator, 843 &info, objectID, &len); 844 if (objectInfo) { 845 objectInfo->objectType = info.obj_type; 846 objectInfo->keySize = info.obj_size; 847 objectInfo->maxKeySize = info.max_obj_size; 848 objectInfo->objectUsage = info.obj_usage; 849 objectInfo->dataSize = info.data_size; 850 objectInfo->dataPosition = info.data_pos; 851 objectInfo->handleFlags = info.handle_flags; 852 } 853 *objectIDLen = len; 854 855 out: 856 if (res != TEE_SUCCESS && 857 res != TEE_ERROR_ITEM_NOT_FOUND && 858 res != TEE_ERROR_CORRUPT_OBJECT && 859 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 860 TEE_Panic(res); 861 862 return res; 863 } 864 865 /* Data and Key Storage API - Data Stream Access Functions */ 866 867 TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer, 868 uint32_t size, uint32_t *count) 869 { 870 TEE_Result res; 871 uint64_t cnt64; 872 873 if (object == TEE_HANDLE_NULL) { 874 res = TEE_ERROR_BAD_PARAMETERS; 875 goto out; 876 } 877 __utee_check_out_annotation(count, sizeof(*count)); 878 879 cnt64 = *count; 880 res = _utee_storage_obj_read((unsigned long)object, buffer, size, 881 &cnt64); 882 *count = cnt64; 883 884 out: 885 if (res != TEE_SUCCESS && 886 res != TEE_ERROR_CORRUPT_OBJECT && 887 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 888 TEE_Panic(res); 889 890 return res; 891 } 892 893 TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, const void *buffer, 894 uint32_t size) 895 { 896 TEE_Result res; 897 898 if (object == TEE_HANDLE_NULL) { 899 res = TEE_ERROR_BAD_PARAMETERS; 900 goto out; 901 } 902 903 if (size > TEE_DATA_MAX_POSITION) { 904 res = TEE_ERROR_OVERFLOW; 905 goto out; 906 } 907 908 res = _utee_storage_obj_write((unsigned long)object, buffer, size); 909 910 out: 911 if (res != TEE_SUCCESS && 912 res != TEE_ERROR_STORAGE_NO_SPACE && 913 res != TEE_ERROR_OVERFLOW && 914 res != TEE_ERROR_CORRUPT_OBJECT && 915 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 916 TEE_Panic(res); 917 918 return res; 919 } 920 921 TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size) 922 { 923 TEE_Result res; 924 925 if (object == TEE_HANDLE_NULL) { 926 res = TEE_ERROR_BAD_PARAMETERS; 927 goto out; 928 } 929 930 res = _utee_storage_obj_trunc((unsigned long)object, size); 931 932 out: 933 if (res != TEE_SUCCESS && 934 res != TEE_ERROR_STORAGE_NO_SPACE && 935 res != TEE_ERROR_CORRUPT_OBJECT && 936 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 937 TEE_Panic(res); 938 939 return res; 940 } 941 942 TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset, 943 TEE_Whence whence) 944 { 945 struct utee_object_info info = { }; 946 TEE_Result res = TEE_SUCCESS; 947 948 if (object == TEE_HANDLE_NULL) { 949 res = TEE_ERROR_BAD_PARAMETERS; 950 goto out; 951 } 952 953 res = _utee_cryp_obj_get_info((unsigned long)object, &info); 954 if (res != TEE_SUCCESS) 955 goto out; 956 957 switch (whence) { 958 case TEE_DATA_SEEK_SET: 959 if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) { 960 res = TEE_ERROR_OVERFLOW; 961 goto out; 962 } 963 break; 964 case TEE_DATA_SEEK_CUR: 965 if (offset > 0 && 966 ((uint32_t)offset + info.data_pos > TEE_DATA_MAX_POSITION || 967 (uint32_t)offset + info.data_pos < info.data_pos)) { 968 res = TEE_ERROR_OVERFLOW; 969 goto out; 970 } 971 break; 972 case TEE_DATA_SEEK_END: 973 if (offset > 0 && 974 ((uint32_t)offset + info.data_size > 975 TEE_DATA_MAX_POSITION || 976 (uint32_t)offset + info.data_size < info.data_size)) { 977 res = TEE_ERROR_OVERFLOW; 978 goto out; 979 } 980 break; 981 default: 982 res = TEE_ERROR_ITEM_NOT_FOUND; 983 goto out; 984 } 985 986 res = _utee_storage_obj_seek((unsigned long)object, offset, whence); 987 988 out: 989 if (res != TEE_SUCCESS && 990 res != TEE_ERROR_OVERFLOW && 991 res != TEE_ERROR_CORRUPT_OBJECT && 992 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 993 TEE_Panic(res); 994 995 return res; 996 } 997