3
* Licensed Materials - Property of IBM
5
* trousers - An open source TCG Software Stack
7
* (C) Copyright International Business Machines Corp. 2004-2006
18
#include <sys/types.h>
24
#include "trousers/tss.h"
25
#include "trousers/trousers.h"
26
#include "trousers_types.h"
28
#include "spi_utils.h"
32
static int user_ps_fd = -1;
33
static pthread_mutex_t user_ps_lock = PTHREAD_MUTEX_INITIALIZER;
36
* Determine the default path to the persistent storage file and create it if it doesn't exist.
39
get_user_ps_path(char **file)
41
TSS_RESULT result = TSPERR(TSS_E_INTERNAL_ERROR);
42
char *file_name = NULL, *home_dir = NULL;
43
struct passwd pw, *pwp;
45
char buf[PASSWD_BUFSIZE];
49
if ((file_name = getenv("TSS_USER_PS_FILE"))) {
50
*file = strdup(file_name);
51
return (*file) ? TSS_SUCCESS : TSPERR(TSS_E_OUTOFMEMORY);
58
rc = getpwent_r(&pw, buf, PASSWD_BUFSIZE, &pwp);
60
LogDebugFn("USER PS: Error getting path to home directory: getpwent_r: %s",
63
return TSPERR(TSS_E_INTERNAL_ERROR);
66
if (euid == pwp->pw_uid) {
67
home_dir = strdup(pwp->pw_dir);
74
return TSPERR(TSS_E_OUTOFMEMORY);
76
/* Tack on TSS_USER_PS_DIR and see if it exists */
77
rc = snprintf(buf, PASSWD_BUFSIZE, "%s/%s", home_dir, TSS_USER_PS_DIR);
78
if (rc == PASSWD_BUFSIZE) {
79
LogDebugFn("USER PS: Path to file too long! (> %d bytes)", PASSWD_BUFSIZE);
84
if ((rc = stat(buf, &stat_buf)) == -1) {
85
if (errno == ENOENT) {
87
/* Create the base directory, $HOME/.trousers */
88
if ((rc = mkdir(buf, 0700)) == -1) {
89
LogDebugFn("USER PS: Error creating dir: %s: %s", buf,
94
LogDebugFn("USER PS: Error stating dir: %s: %s", buf, strerror(errno));
99
/* Directory exists or has been created, return the path to the file */
100
rc = snprintf(buf, PASSWD_BUFSIZE, "%s/%s/%s", home_dir, TSS_USER_PS_DIR,
102
if (rc == PASSWD_BUFSIZE) {
103
LogDebugFn("USER PS: Path to file too long! (> %d bytes)", PASSWD_BUFSIZE);
107
result = (*file) ? TSS_SUCCESS : TSPERR(TSS_E_OUTOFMEMORY);
118
char *file_name = NULL;
120
pthread_mutex_lock(&user_ps_lock);
122
/* check the global file handle first. If it exists, lock it and return */
123
if (user_ps_fd != -1) {
124
if ((rc = flock(user_ps_fd, LOCK_EX))) {
125
LogDebug("USER PS: failed to lock file: %s", strerror(errno));
126
pthread_mutex_unlock(&user_ps_lock);
127
return TSPERR(TSS_E_INTERNAL_ERROR);
134
/* open and lock the file */
135
if ((result = get_user_ps_path(&file_name))) {
136
LogDebugFn("USER PS: error getting file path");
137
pthread_mutex_unlock(&user_ps_lock);
141
user_ps_fd = open(file_name, O_CREAT|O_RDWR, 0600);
142
if (user_ps_fd < 0) {
143
LogDebug("USER PS: open of %s failed: %s", file_name, strerror(errno));
145
pthread_mutex_unlock(&user_ps_lock);
146
return TSPERR(TSS_E_INTERNAL_ERROR);
149
if ((rc = flock(user_ps_fd, LOCK_EX))) {
150
LogDebug("USER PS: failed to get lock of %s: %s", file_name, strerror(errno));
154
pthread_mutex_unlock(&user_ps_lock);
155
return TSPERR(TSS_E_INTERNAL_ERROR);
170
/* release the file lock */
171
if ((rc = flock(fd, LOCK_UN))) {
172
LogDebug("USER PS: failed to unlock file: %s", strerror(errno));
176
pthread_mutex_unlock(&user_ps_lock);
185
pthread_mutex_unlock(&user_ps_lock);
189
psfile_is_key_registered(int fd, TSS_UUID *uuid, TSS_BOOL *answer)
192
struct key_disk_cache tmp;
194
if ((result = psfile_get_cache_entry_by_uuid(fd, uuid, &tmp)) == TSS_SUCCESS)
196
else if (result == TSPERR(TSS_E_PS_KEY_NOTFOUND))
205
psfile_get_parent_uuid_by_uuid(int fd, TSS_UUID *uuid, TSS_UUID *ret_uuid)
208
struct key_disk_cache tmp;
210
if ((result = psfile_get_cache_entry_by_uuid(fd, uuid, &tmp)))
213
memcpy(ret_uuid, &tmp.parent_uuid, sizeof(TSS_UUID));
219
psfile_get_parent_ps_type(int fd, TSS_UUID *uuid, UINT32 *type)
222
struct key_disk_cache tmp;
224
if ((result = psfile_get_cache_entry_by_uuid(fd, uuid, &tmp)))
227
if (tmp.flags & CACHE_FLAG_PARENT_PS_SYSTEM)
228
*type = TSS_PS_TYPE_SYSTEM;
230
*type = TSS_PS_TYPE_USER;
236
* return a key struct from PS given a uuid
239
psfile_get_key_by_uuid(int fd, TSS_UUID *uuid, BYTE *key)
244
struct key_disk_cache tmp;
247
if ((result = psfile_get_cache_entry_by_uuid(fd, uuid, &tmp)))
250
/* jump to the location of the key blob */
251
file_offset = TSSPS_BLOB_DATA_OFFSET(&tmp);
253
rc = lseek(fd, file_offset, SEEK_SET);
254
if (rc == ((off_t)-1)) {
255
LogDebugFn("lseek: %s", strerror(errno));
256
return TSPERR(TSS_E_INTERNAL_ERROR);
259
if ((rc = read_data(fd, buf, tmp.blob_size))) {
260
LogDebugFn("Blob read from disk failed.");
264
memcpy(key, buf, tmp.blob_size);
269
* return a key struct from PS given a public key
272
psfile_get_key_by_pub(int fd, TSS_UUID *uuid, UINT32 pub_size, BYTE *pub, BYTE *key)
277
struct key_disk_cache tmp;
280
if ((result = psfile_get_cache_entry_by_pub(fd, pub_size, pub, &tmp)))
283
/* jump to the location of the key blob */
284
file_offset = TSSPS_BLOB_DATA_OFFSET(&tmp);
286
rc = lseek(fd, file_offset, SEEK_SET);
287
if (rc == ((off_t)-1)) {
288
LogDebugFn("lseek: %s", strerror(errno));
289
return TSPERR(TSS_E_INTERNAL_ERROR);
292
if ((result = read_data(fd, buf, tmp.blob_size))) {
293
LogDebugFn("Blob read from disk failed.");
297
memcpy(key, buf, tmp.blob_size);
298
memcpy(uuid, &tmp.uuid, sizeof(TSS_UUID));
304
psfile_get_uuid_by_pub(int fd, UINT32 pub_size, BYTE *pub, TSS_UUID *uuid)
307
struct key_disk_cache tmp;
309
if ((result = psfile_get_cache_entry_by_pub(fd, pub_size, pub, &tmp)))
312
memcpy(uuid, &tmp.uuid, sizeof(TSS_UUID));
318
psfile_change_num_keys(int fd, BYTE increment)
324
rc = lseek(fd, TSSPS_NUM_KEYS_OFFSET, SEEK_SET);
325
if (rc == ((off_t)-1)) {
326
LogDebug("lseek: %s", strerror(errno));
327
return TSPERR(TSS_E_INTERNAL_ERROR);
330
rc = read(fd, &num_keys, sizeof(UINT32));
331
if (rc != sizeof(UINT32)) {
332
LogDebug("read of %zd bytes: %s", sizeof(UINT32), strerror(errno));
333
return TSPERR(TSS_E_INTERNAL_ERROR);
341
rc = lseek(fd, TSSPS_NUM_KEYS_OFFSET, SEEK_SET);
342
if (rc == ((off_t)-1)) {
343
LogDebug("lseek: %s", strerror(errno));
344
return TSPERR(TSS_E_INTERNAL_ERROR);
347
if ((result = write_data(fd, (void *)&num_keys, sizeof(UINT32)))) {
348
LogDebug("%s", __FUNCTION__);
355
/* Write the initial header (number of keys and PS version) to initialize a new file */
357
psfile_write_key_header(int fd)
363
rc = lseek(fd, TSSPS_VERSION_OFFSET, SEEK_SET);
364
if (rc == ((off_t)-1)) {
365
LogDebug("lseek: %s", strerror(errno));
366
return TSPERR(TSS_E_INTERNAL_ERROR);
370
if ((result = write_data(fd, &i, sizeof(BYTE)))) {
371
LogDebug("%s", __FUNCTION__);
375
rc = lseek(fd, TSSPS_NUM_KEYS_OFFSET, SEEK_SET);
376
if (rc == ((off_t)-1)) {
377
LogDebug("lseek: %s", strerror(errno));
378
return TSPERR(TSS_E_INTERNAL_ERROR);
382
if ((result = write_data(fd, &i, sizeof(UINT32)))) {
383
LogDebug("%s", __FUNCTION__);
395
* [BYTE PS version = '\1']
396
* [UINT32 num_keys_on_disk ]
397
* [TSS_UUID uuid0 ] yes
398
* [TSS_UUID uuid_parent0 ] yes
399
* [UINT16 pub_data_size0 ] yes
400
* [UINT16 blob_size0 ] yes
401
* [UINT32 vendor_data_size0] yes
402
* [UINT16 cache_flags0 ] yes
403
* [BYTE[] pub_data0 ]
405
* [BYTE[] vendor_data0 ]
410
psfile_write_key(int fd,
412
TSS_UUID *parent_uuid,
415
UINT16 key_blob_size)
420
UINT16 pub_key_size, cache_flags = 0;
422
struct stat stat_buf;
425
/* leaving the cache flag for parent ps type as 0 implies TSS_PS_TYPE_USER */
426
if (parent_ps == TSS_PS_TYPE_SYSTEM)
427
cache_flags |= CACHE_FLAG_PARENT_PS_SYSTEM;
429
if ((rc = fstat(fd, &stat_buf)) == -1) {
430
LogDebugFn("stat failed: %s", strerror(errno));
431
return TSPERR(TSS_E_INTERNAL_ERROR);
434
file_offset = stat_buf.st_size;
436
if (file_offset < (int)TSSPS_KEYS_OFFSET) {
437
if ((result = psfile_write_key_header(fd)))
439
file_offset = TSSPS_KEYS_OFFSET;
442
rc = lseek(fd, file_offset, SEEK_SET);
443
if (rc == ((off_t)-1)) {
444
LogDebug("lseek: %s", strerror(errno));
445
return TSPERR(TSS_E_INTERNAL_ERROR);
448
/* Unload the blob to get the public key */
450
if ((result = Trspi_UnloadBlob_KEY(&offset, key_blob, &key)))
453
pub_key_size = key.pubKey.keyLength;
455
/* [TSS_UUID uuid0 ] yes */
456
if ((result = write_data(fd, (void *)uuid, sizeof(TSS_UUID)))) {
457
LogDebug("%s", __FUNCTION__);
461
/* [TSS_UUID uuid_parent0 ] yes */
462
if ((result = write_data(fd, (void *)parent_uuid, sizeof(TSS_UUID)))) {
463
LogDebug("%s", __FUNCTION__);
467
/* [UINT16 pub_data_size0 ] yes */
468
if ((result = write_data(fd, &pub_key_size, sizeof(UINT16)))) {
469
LogDebug("%s", __FUNCTION__);
473
/* [UINT16 blob_size0 ] yes */
474
if ((result = write_data(fd, &key_blob_size, sizeof(UINT16)))) {
475
LogDebug("%s", __FUNCTION__);
479
/* [UINT32 vendor_data_size0 ] yes */
480
if ((result = write_data(fd, &zero, sizeof(UINT32)))) {
481
LogDebug("%s", __FUNCTION__);
485
/* [UINT16 cache_flags0 ] yes */
486
if ((result = write_data(fd, &cache_flags, sizeof(UINT16)))) {
487
LogDebug("%s", __FUNCTION__);
491
/* [BYTE[] pub_data0 ] no */
492
if ((result = write_data(fd, (void *)key.pubKey.key, pub_key_size))) {
493
LogDebug("%s", __FUNCTION__);
497
/* [BYTE[] blob0 ] no */
498
if ((result = write_data(fd, (void *)key_blob, key_blob_size))) {
499
LogDebug("%s", __FUNCTION__);
503
if ((result = psfile_change_num_keys(fd, INCREMENT))) {
504
LogDebug("%s", __FUNCTION__);
514
psfile_remove_key(int fd, TSS_UUID *uuid)
517
UINT32 head_offset = 0, tail_offset;
519
struct key_disk_cache c;
522
if ((result = psfile_get_cache_entry_by_uuid(fd, uuid, &c)))
525
/* head_offset is the offset the beginning of the key */
526
head_offset = TSSPS_UUID_OFFSET(&c);
528
/* tail_offset is the offset the beginning of the next key */
529
tail_offset = TSSPS_VENDOR_DATA_OFFSET(&c) + c.vendor_data_size;
531
rc = lseek(fd, tail_offset, SEEK_SET);
532
if (rc == ((off_t)-1)) {
533
LogDebug("lseek: %s", strerror(errno));
534
return TSPERR(TSS_E_INTERNAL_ERROR);
537
/* read in from tail, write out to head to fill the gap */
538
while ((rc = read(fd, buf, sizeof(buf))) > 0) {
542
/* set the file pointer to where we want to write */
543
rc = lseek(fd, head_offset, SEEK_SET);
544
if (rc == ((off_t)-1)) {
545
LogDebug("lseek: %s", strerror(errno));
546
return TSPERR(TSS_E_INTERNAL_ERROR);
550
if ((result = write_data(fd, (void *)buf, size))) {
551
LogDebug("%s", __FUNCTION__);
556
/* set the file pointer to where we want to read in the next
558
rc = lseek(fd, tail_offset, SEEK_SET);
559
if (rc == ((off_t)-1)) {
560
LogDebug("lseek: %s", strerror(errno));
561
return TSPERR(TSS_E_INTERNAL_ERROR);
566
LogDebug("read: %s", strerror(errno));
567
return TSPERR(TSS_E_INTERNAL_ERROR);
570
/* set the file pointer to where we want to write */
571
rc = lseek(fd, head_offset, SEEK_SET);
572
if (rc == ((off_t)-1)) {
573
LogDebug("lseek: %s", strerror(errno));
574
return TSPERR(TSS_E_INTERNAL_ERROR);
577
/* head_offset now contains a pointer to where we want to truncate the
578
* file. Zero out the old tail end of the file and truncate it. */
580
memset(buf, 0, sizeof(buf));
582
/* Zero out the old tail end of the file */
583
if ((result = write_data(fd, (void *)buf, tail_offset - head_offset))) {
584
LogDebug("%s", __FUNCTION__);
588
if ((rc = ftruncate(fd, head_offset)) < 0) {
589
LogDebug("ftruncate: %s", strerror(errno));
590
return TSPERR(TSS_E_INTERNAL_ERROR);
593
/* we succeeded in removing a key from the disk. Decrement the number
594
* of keys in the file */
595
if ((result = psfile_change_num_keys(fd, DECREMENT)))
602
psfile_get_all_cache_entries(int fd, UINT32 *size, struct key_disk_cache **c)
604
UINT32 i, num_keys = psfile_get_num_keys(fd);
607
struct key_disk_cache *tmp = NULL;
615
/* make sure the file pointer is where we expect, just after the number
616
* of keys on disk at the head of the file
618
offset = lseek(fd, TSSPS_KEYS_OFFSET, SEEK_SET);
619
if (offset == ((off_t)-1)) {
620
LogDebug("lseek: %s", strerror(errno));
621
return TSPERR(TSS_E_INTERNAL_ERROR);
624
if ((tmp = malloc(num_keys * sizeof(struct key_disk_cache))) == NULL) {
625
LogDebug("malloc of %zu bytes failed.", num_keys * sizeof(struct key_disk_cache));
626
return TSPERR(TSS_E_OUTOFMEMORY);
629
for (i = 0; i < num_keys; i++) {
630
offset = lseek(fd, 0, SEEK_CUR);
631
if (offset == ((off_t)-1)) {
632
LogDebug("lseek: %s", strerror(errno));
633
result = TSPERR(TSS_E_INTERNAL_ERROR);
636
tmp[i].offset = offset;
639
if ((result = read_data(fd, &tmp[i].uuid, sizeof(TSS_UUID)))) {
640
LogDebug("%s", __FUNCTION__);
644
/* read parent UUID */
645
if ((result = read_data(fd, &tmp[i].parent_uuid, sizeof(TSS_UUID)))) {
646
LogDebug("%s", __FUNCTION__);
651
if ((result = read_data(fd, &tmp[i].pub_data_size, sizeof(UINT16)))) {
652
LogDebug("%s", __FUNCTION__);
656
DBG_ASSERT(tmp[i].pub_data_size <= 2048);
659
if ((result = read_data(fd, &tmp[i].blob_size, sizeof(UINT16)))) {
660
LogDebug("%s", __FUNCTION__);
664
DBG_ASSERT(tmp[i].blob_size <= 4096);
666
/* vendor data size */
667
if ((result = read_data(fd, &tmp[i].vendor_data_size, sizeof(UINT32)))) {
668
LogDebug("%s", __FUNCTION__);
673
if ((result = read_data(fd, &tmp[i].flags, sizeof(UINT16)))) {
674
LogDebug("%s", __FUNCTION__);
678
/* fast forward over the pub key */
679
offset = lseek(fd, tmp[i].pub_data_size, SEEK_CUR);
680
if (offset == ((off_t)-1)) {
681
LogDebug("lseek: %s", strerror(errno));
682
result = TSPERR(TSS_E_INTERNAL_ERROR);
686
/* fast forward over the blob */
687
offset = lseek(fd, tmp[i].blob_size, SEEK_CUR);
688
if (offset == ((off_t)-1)) {
689
LogDebug("lseek: %s", strerror(errno));
690
result = TSPERR(TSS_E_INTERNAL_ERROR);
694
/* ignore vendor data for user ps */
708
copy_key_info(int fd, TSS_KM_KEYINFO *ki, struct key_disk_cache *c)
716
/* Set the file pointer to the offset that the key blob is at */
717
off = lseek(fd, TSSPS_BLOB_DATA_OFFSET(c), SEEK_SET);
718
if (off == ((off_t)-1)) {
719
LogDebug("lseek: %s", strerror(errno));
720
return TSPERR(TSS_E_INTERNAL_ERROR);
723
/* Read in the key blob */
724
if ((result = read_data(fd, (void *)blob, c->blob_size))) {
725
LogDebug("%s", __FUNCTION__);
729
/* Expand the blob into a useable form */
731
if ((result = Trspi_UnloadBlob_KEY(&offset, blob, &key)))
734
memcpy(&ki->versionInfo, &key.ver, sizeof(TSS_VERSION));
735
memcpy(&ki->keyUUID, &c->uuid, sizeof(TSS_UUID));
736
memcpy(&ki->parentKeyUUID, &c->parent_uuid, sizeof(TSS_UUID));
737
ki->bAuthDataUsage = key.authDataUsage;
745
psfile_get_registered_keys(int fd,
749
TSS_KM_KEYINFO **keys)
752
struct key_disk_cache *cache_entries;
753
UINT32 cache_size, i, j;
754
TSS_KM_KEYINFO *keyinfos = NULL;
757
if ((result = psfile_get_all_cache_entries(fd, &cache_size, &cache_entries)))
760
if (cache_size == 0) {
762
return TSPERR(TSS_E_PS_KEY_NOTFOUND);
775
/* Search for the requested UUID. When found, allocate new space for it, copy
776
* it in, then change the uuid to be searched for it its parent and start over. */
777
for (i = 0; i < cache_size; i++) {
778
if (!memcmp(&cache_entries[i].uuid, find_uuid, sizeof(TSS_UUID))) {
779
if (!(keyinfos = realloc(keyinfos,
780
(j+1) * sizeof(TSS_KM_KEYINFO)))) {
783
return TSPERR(TSS_E_OUTOFMEMORY);
785
memset(&keyinfos[j], 0, sizeof(TSS_KM_KEYINFO));
787
if ((result = copy_key_info(fd, &keyinfos[j], &cache_entries[i]))) {
793
find_uuid = &keyinfos[j].parentKeyUUID;
799
/* Searching for keys in the user PS will always lead us up to some key in the
800
* system PS. Return that key's uuid so that the upper layers can call down to TCS
801
* to search for it. */
802
memcpy(tcs_uuid, find_uuid, sizeof(TSS_UUID));
806
if ((keyinfos = calloc(cache_size, sizeof(TSS_KM_KEYINFO))) == NULL) {
807
LogDebug("malloc of %zu bytes failed.",
808
cache_size * sizeof(TSS_KM_KEYINFO));
810
return TSPERR(TSS_E_OUTOFMEMORY);
813
for (i = 0; i < cache_size; i++) {
814
if ((result = copy_key_info(fd, &keyinfos[i], &cache_entries[i]))) {
833
* read into the PS file and return the number of keys
836
psfile_get_num_keys(int fd)
841
/* go to the number of keys */
842
rc = lseek(fd, TSSPS_NUM_KEYS_OFFSET, SEEK_SET);
843
if (rc == ((off_t)-1)) {
844
LogDebug("lseek: %s", strerror(errno));
848
rc = read(fd, &num_keys, sizeof(UINT32));
850
LogDebug("read of %zd bytes: %s", sizeof(UINT32), strerror(errno));
852
} else if ((unsigned)rc < sizeof(UINT32)) {
864
* [BYTE PS version = '\1']
865
* [UINT32 num_keys_on_disk ]
866
* [TSS_UUID uuid0 ] yes
867
* [TSS_UUID uuid_parent0 ] yes
868
* [UINT16 pub_data_size0 ] yes
869
* [UINT16 blob_size0 ] yes
870
* [UINT32 vendor_data_size0] yes
871
* [UINT16 cache_flags0 ] yes
872
* [BYTE[] pub_data0 ]
874
* [BYTE[] vendor_data0 ]
879
psfile_get_cache_entry_by_uuid(int fd, TSS_UUID *uuid, struct key_disk_cache *c)
881
UINT32 i, num_keys = psfile_get_num_keys(fd);
887
return TSPERR(TSS_E_PS_KEY_NOTFOUND);
889
/* make sure the file pointer is where we expect, just after the number
890
* of keys on disk at the head of the file
892
offset = lseek(fd, TSSPS_KEYS_OFFSET, SEEK_SET);
893
if (offset == ((off_t)-1)) {
894
LogDebug("lseek: %s", strerror(errno));
895
return TSPERR(TSS_E_INTERNAL_ERROR);
898
for (i = 0; i < num_keys && !found; i++) {
899
offset = lseek(fd, 0, SEEK_CUR);
900
if (offset == ((off_t)-1)) {
901
LogDebug("lseek: %s", strerror(errno));
902
return TSPERR(TSS_E_INTERNAL_ERROR);
907
if ((result = read_data(fd, (void *)&c->uuid, sizeof(TSS_UUID)))) {
908
LogDebug("%s", __FUNCTION__);
912
if (!memcmp(&c->uuid, uuid, sizeof(TSS_UUID))) {
915
/* read parent UUID */
916
if ((result = read_data(fd, (void *)&c->parent_uuid, sizeof(TSS_UUID)))) {
917
LogDebug("%s", __FUNCTION__);
921
/* fast forward over the parent UUID */
922
offset = lseek(fd, sizeof(TSS_UUID), SEEK_CUR);
923
if (offset == ((off_t)-1)) {
924
LogDebug("lseek: %s", strerror(errno));
925
return TSPERR(TSS_E_INTERNAL_ERROR);
930
if ((result = read_data(fd, &c->pub_data_size, sizeof(UINT16)))) {
931
LogDebug("%s", __FUNCTION__);
935
DBG_ASSERT(c->pub_data_size <= 2048 && c->pub_data_size > 0);
938
if ((result = read_data(fd, &c->blob_size, sizeof(UINT16)))) {
939
LogDebug("%s", __FUNCTION__);
943
DBG_ASSERT(c->blob_size <= 4096 && c->blob_size > 0);
945
/* vendor data size */
946
if ((result = read_data(fd, &c->vendor_data_size, sizeof(UINT32)))) {
947
LogDebug("%s", __FUNCTION__);
952
if ((result = read_data(fd, &c->flags, sizeof(UINT16)))) {
953
LogDebug("%s", __FUNCTION__);
957
/* fast forward over the pub key */
958
offset = lseek(fd, c->pub_data_size, SEEK_CUR);
959
if (offset == ((off_t)-1)) {
960
LogDebug("lseek: %s", strerror(errno));
961
return TSPERR(TSS_E_INTERNAL_ERROR);
964
/* fast forward over the blob */
965
offset = lseek(fd, c->blob_size, SEEK_CUR);
966
if (offset == ((off_t)-1)) {
967
LogDebug("lseek: %s", strerror(errno));
968
return TSPERR(TSS_E_INTERNAL_ERROR);
971
/* ignore vendor data in user ps */
974
return found ? TSS_SUCCESS : TSPERR(TSS_E_PS_KEY_NOTFOUND);
978
psfile_get_cache_entry_by_pub(int fd, UINT32 pub_size, BYTE *pub, struct key_disk_cache *c)
981
UINT32 i, num_keys = psfile_get_num_keys(fd);
986
return TSPERR(TSS_E_PS_KEY_NOTFOUND);
988
/* make sure the file pointer is where we expect, just after the number
989
* of keys on disk at the head of the file
991
offset = lseek(fd, TSSPS_KEYS_OFFSET, SEEK_SET);
992
if (offset == ((off_t)-1)) {
993
LogDebug("lseek: %s", strerror(errno));
994
return TSPERR(TSS_E_INTERNAL_ERROR);
997
for (i = 0; i < num_keys; i++) {
998
offset = lseek(fd, 0, SEEK_CUR);
999
if (offset == ((off_t)-1)) {
1000
LogDebug("lseek: %s", strerror(errno));
1001
return TSPERR(TSS_E_INTERNAL_ERROR);
1006
if ((result = read_data(fd, (void *)&c->uuid, sizeof(TSS_UUID)))) {
1007
LogDebug("%s", __FUNCTION__);
1011
/* read parent UUID */
1012
if ((result = read_data(fd, (void *)&c->parent_uuid, sizeof(TSS_UUID)))) {
1013
LogDebug("%s", __FUNCTION__);
1018
if ((result = read_data(fd, &c->pub_data_size, sizeof(UINT16)))) {
1019
LogDebug("%s", __FUNCTION__);
1023
DBG_ASSERT(c->pub_data_size <= 2048 && c->pub_data_size > 0);
1026
if ((result = read_data(fd, &c->blob_size, sizeof(UINT16)))) {
1027
LogDebug("%s", __FUNCTION__);
1031
DBG_ASSERT(c->blob_size <= 4096 && c->blob_size > 0);
1033
/* vendor data size */
1034
if ((result = read_data(fd, &c->vendor_data_size, sizeof(UINT32)))) {
1035
LogDebug("%s", __FUNCTION__);
1040
if ((result = read_data(fd, &c->flags, sizeof(UINT16)))) {
1041
LogDebug("%s", __FUNCTION__);
1045
if (c->pub_data_size == pub_size) {
1046
/* read in the pub key */
1047
if ((result = read_data(fd, blob, c->pub_data_size))) {
1048
LogDebug("%s", __FUNCTION__);
1052
if (!memcmp(blob, pub, pub_size))
1056
/* fast forward over the blob */
1057
offset = lseek(fd, c->blob_size, SEEK_CUR);
1058
if (offset == ((off_t)-1)) {
1059
LogDebug("lseek: %s", strerror(errno));
1060
return TSPERR(TSS_E_INTERNAL_ERROR);
1063
/* ignore vendor data */