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 uint32_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 TEE_CreatePersistentObject(uint32_t storageID, const void *objectID, 614 uint32_t objectIDLen, uint32_t flags, 615 TEE_ObjectHandle attributes, 616 const void *initialData, 617 uint32_t initialDataLen, 618 TEE_ObjectHandle *object) 619 { 620 TEE_Result res; 621 uint32_t obj; 622 623 __utee_check_out_annotation(object, sizeof(*object)); 624 625 res = _utee_storage_obj_create(storageID, objectID, objectIDLen, flags, 626 (unsigned long)attributes, initialData, 627 initialDataLen, &obj); 628 629 if (res == TEE_SUCCESS) 630 *object = (TEE_ObjectHandle)(uintptr_t)obj; 631 632 if (res != TEE_SUCCESS && 633 res != TEE_ERROR_ITEM_NOT_FOUND && 634 res != TEE_ERROR_ACCESS_CONFLICT && 635 res != TEE_ERROR_OUT_OF_MEMORY && 636 res != TEE_ERROR_STORAGE_NO_SPACE && 637 res != TEE_ERROR_CORRUPT_OBJECT && 638 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 639 TEE_Panic(res); 640 641 if (res != TEE_SUCCESS) 642 *object = TEE_HANDLE_NULL; 643 644 return res; 645 } 646 647 /* 648 * Use of this function is deprecated 649 * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead 650 * These functions will be removed at some future major revision of 651 * this specification 652 */ 653 void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object) 654 { 655 TEE_Result res; 656 657 if (object == TEE_HANDLE_NULL) 658 return; 659 660 res = TEE_CloseAndDeletePersistentObject1(object); 661 662 if (res != TEE_SUCCESS) 663 TEE_Panic(0); 664 } 665 666 TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object) 667 { 668 TEE_Result res; 669 670 if (object == TEE_HANDLE_NULL) 671 return TEE_SUCCESS; 672 673 res = _utee_storage_obj_del((unsigned long)object); 674 675 if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 676 TEE_Panic(res); 677 678 return res; 679 } 680 681 682 TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object, 683 const void *newObjectID, 684 uint32_t newObjectIDLen) 685 { 686 TEE_Result res; 687 688 if (object == TEE_HANDLE_NULL) { 689 res = TEE_ERROR_ITEM_NOT_FOUND; 690 goto out; 691 } 692 693 res = _utee_storage_obj_rename((unsigned long)object, newObjectID, 694 newObjectIDLen); 695 696 out: 697 if (res != TEE_SUCCESS && 698 res != TEE_ERROR_ACCESS_CONFLICT && 699 res != TEE_ERROR_CORRUPT_OBJECT && 700 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 701 TEE_Panic(res); 702 703 return res; 704 } 705 706 TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle * 707 objectEnumerator) 708 { 709 TEE_Result res; 710 uint32_t oe; 711 712 __utee_check_out_annotation(objectEnumerator, 713 sizeof(*objectEnumerator)); 714 715 res = _utee_storage_alloc_enum(&oe); 716 717 if (res != TEE_SUCCESS) 718 oe = TEE_HANDLE_NULL; 719 720 *objectEnumerator = (TEE_ObjectEnumHandle)(uintptr_t)oe; 721 722 if (res != TEE_SUCCESS && 723 res != TEE_ERROR_ACCESS_CONFLICT) 724 TEE_Panic(res); 725 726 return res; 727 } 728 729 void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 730 { 731 TEE_Result res; 732 733 if (objectEnumerator == TEE_HANDLE_NULL) 734 return; 735 736 res = _utee_storage_free_enum((unsigned long)objectEnumerator); 737 738 if (res != TEE_SUCCESS) 739 TEE_Panic(res); 740 } 741 742 void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 743 { 744 TEE_Result res; 745 746 if (objectEnumerator == TEE_HANDLE_NULL) 747 return; 748 749 res = _utee_storage_reset_enum((unsigned long)objectEnumerator); 750 751 if (res != TEE_SUCCESS) 752 TEE_Panic(res); 753 } 754 755 TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle 756 objectEnumerator, 757 uint32_t storageID) 758 { 759 TEE_Result res; 760 761 res = _utee_storage_start_enum((unsigned long)objectEnumerator, 762 storageID); 763 764 if (res != TEE_SUCCESS && 765 res != TEE_ERROR_ITEM_NOT_FOUND && 766 res != TEE_ERROR_CORRUPT_OBJECT && 767 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 768 TEE_Panic(res); 769 770 return res; 771 } 772 773 TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator, 774 TEE_ObjectInfo *objectInfo, 775 void *objectID, uint32_t *objectIDLen) 776 { 777 struct utee_object_info info = { }; 778 TEE_Result res = TEE_SUCCESS; 779 uint64_t len = 0; 780 781 if (objectInfo) 782 __utee_check_out_annotation(objectInfo, sizeof(*objectInfo)); 783 __utee_check_out_annotation(objectIDLen, sizeof(*objectIDLen)); 784 785 if (!objectID) { 786 res = TEE_ERROR_BAD_PARAMETERS; 787 goto out; 788 } 789 790 len = *objectIDLen; 791 res = _utee_storage_next_enum((unsigned long)objectEnumerator, 792 &info, objectID, &len); 793 if (objectInfo) { 794 objectInfo->objectType = info.obj_type; 795 objectInfo->objectSize = info.obj_size; 796 objectInfo->maxObjectSize = info.max_obj_size; 797 objectInfo->objectUsage = info.obj_usage; 798 objectInfo->dataSize = info.data_size; 799 objectInfo->dataPosition = info.data_pos; 800 objectInfo->handleFlags = info.handle_flags; 801 } 802 *objectIDLen = len; 803 804 out: 805 if (res != TEE_SUCCESS && 806 res != TEE_ERROR_ITEM_NOT_FOUND && 807 res != TEE_ERROR_CORRUPT_OBJECT && 808 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 809 TEE_Panic(res); 810 811 return res; 812 } 813 814 TEE_Result 815 __GP11_TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator, 816 __GP11_TEE_ObjectInfo *objectInfo, 817 void *objectID, uint32_t *objectIDLen) 818 { 819 struct utee_object_info info = { }; 820 TEE_Result res = TEE_SUCCESS; 821 uint64_t len = 0; 822 823 if (objectInfo) 824 __utee_check_out_annotation(objectInfo, sizeof(*objectInfo)); 825 __utee_check_out_annotation(objectIDLen, sizeof(*objectIDLen)); 826 827 if (!objectID) { 828 res = TEE_ERROR_BAD_PARAMETERS; 829 goto out; 830 } 831 832 len = *objectIDLen; 833 res = _utee_storage_next_enum((unsigned long)objectEnumerator, 834 &info, objectID, &len); 835 if (objectInfo) { 836 objectInfo->objectType = info.obj_type; 837 objectInfo->keySize = info.obj_size; 838 objectInfo->maxKeySize = info.max_obj_size; 839 objectInfo->objectUsage = info.obj_usage; 840 objectInfo->dataSize = info.data_size; 841 objectInfo->dataPosition = info.data_pos; 842 objectInfo->handleFlags = info.handle_flags; 843 } 844 *objectIDLen = len; 845 846 out: 847 if (res != TEE_SUCCESS && 848 res != TEE_ERROR_ITEM_NOT_FOUND && 849 res != TEE_ERROR_CORRUPT_OBJECT && 850 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 851 TEE_Panic(res); 852 853 return res; 854 } 855 856 /* Data and Key Storage API - Data Stream Access Functions */ 857 858 TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer, 859 uint32_t size, uint32_t *count) 860 { 861 TEE_Result res; 862 uint64_t cnt64; 863 864 if (object == TEE_HANDLE_NULL) { 865 res = TEE_ERROR_BAD_PARAMETERS; 866 goto out; 867 } 868 __utee_check_out_annotation(count, sizeof(*count)); 869 870 cnt64 = *count; 871 res = _utee_storage_obj_read((unsigned long)object, buffer, size, 872 &cnt64); 873 *count = cnt64; 874 875 out: 876 if (res != TEE_SUCCESS && 877 res != TEE_ERROR_CORRUPT_OBJECT && 878 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 879 TEE_Panic(res); 880 881 return res; 882 } 883 884 TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, const void *buffer, 885 uint32_t size) 886 { 887 TEE_Result res; 888 889 if (object == TEE_HANDLE_NULL) { 890 res = TEE_ERROR_BAD_PARAMETERS; 891 goto out; 892 } 893 894 if (size > TEE_DATA_MAX_POSITION) { 895 res = TEE_ERROR_OVERFLOW; 896 goto out; 897 } 898 899 res = _utee_storage_obj_write((unsigned long)object, buffer, size); 900 901 out: 902 if (res != TEE_SUCCESS && 903 res != TEE_ERROR_STORAGE_NO_SPACE && 904 res != TEE_ERROR_OVERFLOW && 905 res != TEE_ERROR_CORRUPT_OBJECT && 906 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 907 TEE_Panic(res); 908 909 return res; 910 } 911 912 TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size) 913 { 914 TEE_Result res; 915 916 if (object == TEE_HANDLE_NULL) { 917 res = TEE_ERROR_BAD_PARAMETERS; 918 goto out; 919 } 920 921 res = _utee_storage_obj_trunc((unsigned long)object, size); 922 923 out: 924 if (res != TEE_SUCCESS && 925 res != TEE_ERROR_STORAGE_NO_SPACE && 926 res != TEE_ERROR_CORRUPT_OBJECT && 927 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 928 TEE_Panic(res); 929 930 return res; 931 } 932 933 TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset, 934 TEE_Whence whence) 935 { 936 struct utee_object_info info = { }; 937 TEE_Result res = TEE_SUCCESS; 938 939 if (object == TEE_HANDLE_NULL) { 940 res = TEE_ERROR_BAD_PARAMETERS; 941 goto out; 942 } 943 944 res = _utee_cryp_obj_get_info((unsigned long)object, &info); 945 if (res != TEE_SUCCESS) 946 goto out; 947 948 switch (whence) { 949 case TEE_DATA_SEEK_SET: 950 if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) { 951 res = TEE_ERROR_OVERFLOW; 952 goto out; 953 } 954 break; 955 case TEE_DATA_SEEK_CUR: 956 if (offset > 0 && 957 ((uint32_t)offset + info.data_pos > TEE_DATA_MAX_POSITION || 958 (uint32_t)offset + info.data_pos < info.data_pos)) { 959 res = TEE_ERROR_OVERFLOW; 960 goto out; 961 } 962 break; 963 case TEE_DATA_SEEK_END: 964 if (offset > 0 && 965 ((uint32_t)offset + info.data_size > 966 TEE_DATA_MAX_POSITION || 967 (uint32_t)offset + info.data_size < info.data_size)) { 968 res = TEE_ERROR_OVERFLOW; 969 goto out; 970 } 971 break; 972 default: 973 res = TEE_ERROR_ITEM_NOT_FOUND; 974 goto out; 975 } 976 977 res = _utee_storage_obj_seek((unsigned long)object, offset, whence); 978 979 out: 980 if (res != TEE_SUCCESS && 981 res != TEE_ERROR_OVERFLOW && 982 res != TEE_ERROR_CORRUPT_OBJECT && 983 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 984 TEE_Panic(res); 985 986 return res; 987 } 988