📄 tspps.c
字号:
if (num_keys == 0) { *size = 0; *c = NULL; return TSS_SUCCESS; } /* make sure the file pointer is where we expect, just after the number * of keys on disk at the head of the file */ offset = lseek(fd, TSSPS_KEYS_OFFSET, SEEK_SET); if (offset == ((off_t)-1)) { LogDebug("lseek: %s", strerror(errno)); return TSPERR(TSS_E_INTERNAL_ERROR); } if ((tmp = malloc(num_keys * sizeof(struct key_disk_cache))) == NULL) { LogDebug("malloc of %zu bytes failed.", num_keys * sizeof(struct key_disk_cache)); return TSPERR(TSS_E_OUTOFMEMORY); } for (i = 0; i < num_keys; i++) { offset = lseek(fd, 0, SEEK_CUR); if (offset == ((off_t)-1)) { LogDebug("lseek: %s", strerror(errno)); result = TSPERR(TSS_E_INTERNAL_ERROR); goto err_exit; } tmp[i].offset = offset; /* read UUID */ if ((result = read_data(fd, &tmp[i].uuid, sizeof(TSS_UUID)))) { LogDebug("%s", __FUNCTION__); goto err_exit; } /* read parent UUID */ if ((result = read_data(fd, &tmp[i].parent_uuid, sizeof(TSS_UUID)))) { LogDebug("%s", __FUNCTION__); goto err_exit; } /* pub data size */ if ((result = read_data(fd, &tmp[i].pub_data_size, sizeof(UINT16)))) { LogDebug("%s", __FUNCTION__); goto err_exit; } DBG_ASSERT(tmp[i].pub_data_size <= 2048); /* blob size */ if ((result = read_data(fd, &tmp[i].blob_size, sizeof(UINT16)))) { LogDebug("%s", __FUNCTION__); goto err_exit; } DBG_ASSERT(tmp[i].blob_size <= 4096); /* vendor data size */ if ((result = read_data(fd, &tmp[i].vendor_data_size, sizeof(UINT32)))) { LogDebug("%s", __FUNCTION__); goto err_exit; } /* cache flags */ if ((result = read_data(fd, &tmp[i].flags, sizeof(UINT16)))) { LogDebug("%s", __FUNCTION__); goto err_exit; } /* fast forward over the pub key */ offset = lseek(fd, tmp[i].pub_data_size, SEEK_CUR); if (offset == ((off_t)-1)) { LogDebug("lseek: %s", strerror(errno)); result = TSPERR(TSS_E_INTERNAL_ERROR); goto err_exit; } /* fast forward over the blob */ offset = lseek(fd, tmp[i].blob_size, SEEK_CUR); if (offset == ((off_t)-1)) { LogDebug("lseek: %s", strerror(errno)); result = TSPERR(TSS_E_INTERNAL_ERROR); goto err_exit; } /* ignore vendor data for user ps */ } *size = num_keys; *c = tmp; return TSS_SUCCESS;err_exit: free(tmp); return result;}TSS_RESULTcopy_key_info(int fd, TSS_KM_KEYINFO *ki, struct key_disk_cache *c){ TSS_KEY key; BYTE blob[4096]; UINT64 offset; TSS_RESULT result; off_t off; /* Set the file pointer to the offset that the key blob is at */ off = lseek(fd, TSSPS_BLOB_DATA_OFFSET(c), SEEK_SET); if (off == ((off_t)-1)) { LogDebug("lseek: %s", strerror(errno)); return TSPERR(TSS_E_INTERNAL_ERROR); } /* Read in the key blob */ if ((result = read_data(fd, (void *)blob, c->blob_size))) { LogDebug("%s", __FUNCTION__); return result; } /* Expand the blob into a useable form */ offset = 0; if ((result = UnloadBlob_TSS_KEY(&offset, blob, &key))) return result; if (key.hdr.key12.tag == TPM_TAG_KEY12) { ki->versionInfo.bMajor = TSS_SPEC_MAJOR; ki->versionInfo.bMinor = TSS_SPEC_MINOR; ki->versionInfo.bRevMajor = 0; ki->versionInfo.bRevMinor = 0; } else memcpy(&ki->versionInfo, &key.hdr.key11.ver, sizeof(TSS_VERSION)); memcpy(&ki->keyUUID, &c->uuid, sizeof(TSS_UUID)); memcpy(&ki->parentKeyUUID, &c->parent_uuid, sizeof(TSS_UUID)); ki->bAuthDataUsage = key.authDataUsage; free_key_refs(&key); return TSS_SUCCESS;}TSS_RESULTcopy_key_info2(int fd, TSS_KM_KEYINFO2 *ki, struct key_disk_cache *c){ TSS_KEY key; BYTE blob[4096]; UINT64 offset; TSS_RESULT result; off_t off; /* Set the file pointer to the offset that the key blob is at */ off = lseek(fd, TSSPS_BLOB_DATA_OFFSET(c), SEEK_SET); if (off == ((off_t)-1)) { LogDebug("lseek: %s", strerror(errno)); return TSPERR(TSS_E_INTERNAL_ERROR); } /* Read in the key blob */ if ((result = read_data(fd, (void *)blob, c->blob_size))) { LogDebug("%s", __FUNCTION__); return result; } /* Expand the blob into a useable form */ offset = 0; if ((result = UnloadBlob_TSS_KEY(&offset, blob, &key))) return result; if (key.hdr.key12.tag == TPM_TAG_KEY12) { ki->versionInfo.bMajor = TSS_SPEC_MAJOR; ki->versionInfo.bMinor = TSS_SPEC_MINOR; ki->versionInfo.bRevMajor = 0; ki->versionInfo.bRevMinor = 0; } else memcpy(&ki->versionInfo, &key.hdr.key11.ver, sizeof(TSS_VERSION)); memcpy(&ki->keyUUID, &c->uuid, sizeof(TSS_UUID)); memcpy(&ki->parentKeyUUID, &c->parent_uuid, sizeof(TSS_UUID)); /* CHECK: fill the two new fields of TSS_KM_KEYINFO2 */ ki->persistentStorageType = TSS_PS_TYPE_USER; ki->persistentStorageTypeParent = c->flags & CACHE_FLAG_PARENT_PS_SYSTEM ? TSS_PS_TYPE_SYSTEM : TSS_PS_TYPE_USER; ki->bAuthDataUsage = key.authDataUsage; free_key_refs(&key); return TSS_SUCCESS;}TSS_RESULTpsfile_get_registered_keys(int fd, TSS_UUID *uuid, TSS_UUID *tcs_uuid, UINT32 *size, TSS_KM_KEYINFO **keys){ TSS_RESULT result; struct key_disk_cache *cache_entries; UINT32 cache_size, i, j; TSS_KM_KEYINFO *keyinfos = NULL; TSS_UUID *find_uuid; if ((result = psfile_get_all_cache_entries(fd, &cache_size, &cache_entries))) return result; if (cache_size == 0) { if (uuid) return TSPERR(TSS_E_PS_KEY_NOTFOUND); else { *size = 0; *keys = NULL; return TSS_SUCCESS; } } if (uuid) { find_uuid = uuid; j = 0;restart_search: /* Search for the requested UUID. When found, allocate new space for it, copy * it in, then change the uuid to be searched for it its parent and start over. */ for (i = 0; i < cache_size; i++) { if (!memcmp(&cache_entries[i].uuid, find_uuid, sizeof(TSS_UUID))) { if (!(keyinfos = realloc(keyinfos, (j+1) * sizeof(TSS_KM_KEYINFO)))) { free(cache_entries); free(keyinfos); return TSPERR(TSS_E_OUTOFMEMORY); } memset(&keyinfos[j], 0, sizeof(TSS_KM_KEYINFO)); if ((result = copy_key_info(fd, &keyinfos[j], &cache_entries[i]))) { free(cache_entries); free(keyinfos); return result; } find_uuid = &keyinfos[j].parentKeyUUID; j++; goto restart_search; } } /* Searching for keys in the user PS will always lead us up to some key in the * system PS. Return that key's uuid so that the upper layers can call down to TCS * to search for it. */ memcpy(tcs_uuid, find_uuid, sizeof(TSS_UUID)); *size = j; } else { if ((keyinfos = calloc(cache_size, sizeof(TSS_KM_KEYINFO))) == NULL) { LogDebug("malloc of %zu bytes failed.", cache_size * sizeof(TSS_KM_KEYINFO)); free(cache_entries); return TSPERR(TSS_E_OUTOFMEMORY); } for (i = 0; i < cache_size; i++) { if ((result = copy_key_info(fd, &keyinfos[i], &cache_entries[i]))) { free(cache_entries); free(keyinfos); return result; } } *size = cache_size; } free(cache_entries); *keys = keyinfos; return TSS_SUCCESS;}TSS_RESULTpsfile_get_registered_keys2(int fd, TSS_UUID *uuid, TSS_UUID *tcs_uuid, UINT32 *size, TSS_KM_KEYINFO2 **keys){ TSS_RESULT result; struct key_disk_cache *cache_entries; UINT32 cache_size, i, j; TSS_KM_KEYINFO2 *keyinfos = NULL; TSS_UUID *find_uuid; if ((result = psfile_get_all_cache_entries(fd, &cache_size, &cache_entries))) return result; if (cache_size == 0) { if (uuid) return TSPERR(TSS_E_PS_KEY_NOTFOUND); else { *size = 0; *keys = NULL; return TSS_SUCCESS; } } if (uuid) { find_uuid = uuid; j = 0; restart_search: /* Search for the requested UUID. When found, allocate new space for it, copy * it in, then change the uuid to be searched for it its parent and start over. */ for (i = 0; i < cache_size; i++) { /*Return 0 if normal finish*/ if (!memcmp(&cache_entries[i].uuid, find_uuid, sizeof(TSS_UUID))) { if (!(keyinfos = realloc(keyinfos, (j+1) * sizeof(TSS_KM_KEYINFO2)))) { free(cache_entries); free(keyinfos); return TSPERR(TSS_E_OUTOFMEMORY); } /* Here the key UUID is found and needs to be copied for the array*/ /* Initializes the keyinfos with 0's*/ memset(&keyinfos[j], 0, sizeof(TSS_KM_KEYINFO2)); if ((result = copy_key_info2(fd, &keyinfos[j], &cache_entries[i]))) { free(cache_entries); free(keyinfos); return result; } find_uuid = &keyinfos[j].parentKeyUUID; j++; goto restart_search; } } /* Searching for keys in the user PS will always lead us up to some key in the * system PS. Return that key's uuid so that the upper layers can call down to TCS * to search for it. */ memcpy(tcs_uuid, find_uuid, sizeof(TSS_UUID)); *size = j; } else { if ((keyinfos = calloc(cache_size, sizeof(TSS_KM_KEYINFO2))) == NULL) { LogDebug("malloc of %zu bytes failed.", cache_size * sizeof(TSS_KM_KEYINFO2)); free(cache_entries); return TSPERR(TSS_E_OUTOFMEMORY); } for (i = 0; i < cache_size; i++) { if ((result = copy_key_info2(fd, &keyinfos[i], &cache_entries[i]))) { free(cache_entries); free(keyinfos); return result; } } *size = cache_size; } free(cache_entries); *keys = keyinfos; return TSS_SUCCESS;}/* * read into the PS file and return the number of keys */UINT32psfile_get_num_keys(int fd){ UINT32 num_keys; int rc; /* go to the number of keys */ rc = lseek(fd, TSSPS_NUM_KEYS_OFFSET, SEEK_SET); if (rc == ((off_t)-1)) { LogDebug("lseek: %s", strerror(errno)); return 0; } rc = read(fd, &num_keys, sizeof(UINT32)); if (rc < 0) { LogDebug("read of %zd bytes: %s", sizeof(UINT32), strerror(errno)); return 0; } else if ((unsigned)rc < sizeof(UINT32)) { num_keys = 0; } return num_keys;}/* * disk store format: * * TrouSerS 0.2.1+ * Version 1: cached? * [BYTE PS version = '\1'] * [UINT32 num_keys_on_disk ] * [TSS_UUID uuid0 ] yes * [TSS_UUID uuid_parent0 ] yes * [UINT16 pub_data_size0 ] yes * [UINT16 blob_size0 ] yes * [UINT32 vendor_data_size0] yes * [UINT16 cache_flags0 ] yes * [BYTE[] pub_data0 ] * [BYTE[] blob0 ] * [BYTE[] vendor_data0 ] * [...] * */TSS_RESULTpsfile_get_cache_entry_by_uuid(int fd, TSS_UUID *uuid, struct key_disk_cache *c){ UINT32 i, num_keys = psfile_get_num_keys(fd); int offset; TSS_RESULT result; BYTE found = 0; if (num_keys == 0) return TSPERR(TSS_E_PS_KEY_NOTFOUND); /* make sure the file pointer is where we expect, just after the number * of keys on disk at the head of the file */ offset = lseek(fd, TSSPS_KEYS_OFFSET, SEEK_SET); if (offset == ((off_t)-1)) { LogDebug("lseek: %s", strerror(errno)); return TSPERR(TSS_E_INTERNAL_ERROR); } for (i = 0; i < num_keys && !found; i++) { offset = lseek(fd, 0, SEEK_CUR); if (offset == ((off_t)-1)) { LogDebug("lseek: %s", strerror(errno)); return TSPERR(TSS_E_INTERNAL_ERROR); } c->offset = offset; /* read UUID */ if ((result = read_data(fd, (void *)&c->uuid, sizeof(TSS_UUID)))) { LogDebug("%s", __FUNCTION__); return result; } if (!memcmp(&c->uuid, uuid, sizeof(TSS_UUID))) { found = 1; /* read parent UUID */ if ((result = read_data(fd, (void *)&c->parent_uuid, sizeof(TSS_UUID)))) { LogDebug("%s", __FUNCTION__); return result; } } else { /* fast forward over the parent UUID */ offset = lseek(fd, sizeof(TSS_UUID), SEEK_CUR); if (offset == ((off_t)-1)) { LogDebug("lseek: %s", strerror(errno)); return TSPERR(TSS_E_INTERNAL_ERROR); } } /* pub data size */ if ((result = read_data(fd, &c->pub_data_size, sizeof(UINT16)))) { LogDebug("%s", __FUNCTION__); return result; } DBG_ASSERT(c->pub_data_size <= 2048 && c->pub_data_size > 0); /* blob size */ if ((result = read_data(fd, &c->blob_size, sizeof(UINT16)))) { LogDebug("%s", __FUNCTION__); return result; } DBG_ASSERT(c->blob_size <= 4096 && c->blob_size > 0); /* vendor data size */ if ((result = read_data(fd, &c->vendor_data_size, sizeof(UINT32)))) { LogDebug("%s", __FUNCTION__); return result; } /* cache flags */ if ((result = read_data(fd, &c->flags, sizeof(UINT16)))) { LogDebug("%s", __FUNCTION__); return result; } /* fast forward over the pub key */ offset = lseek(fd, c->pub_data_size, SEEK_CUR); if (offset == ((off_t)-1)) { LogDebug("lseek: %s", strerror(errno)); return TSPERR(TSS_E_INTERNAL_ERROR); } /* fast forward over the blob */ offset = lseek(fd, c->blob_size, SEEK_CUR); if (offset == ((off_t)-1)) { LogDebug("lseek: %s", strerror(errno)); return TSPERR(TSS_E_INTERNAL_ERROR); } /* ignore vendor data in user ps */ } return found ? TSS_SUCCESS : TSPERR(TSS_E_PS_KEY_NOTFOUND);}TSS_RESULTpsfile_get_cache_entry_by_pub(int fd, UINT32 pub_size, BYTE *pub, struct key_disk_cache *c){ BYTE blob[2048]; UINT32 i, num_keys = psfile_get_num_keys(fd); int offset; TSS_RESULT result; if (num_keys == 0) return TSPERR(TSS_E_PS_KEY_NOTFOUND); /* make sure the file pointer is where we expect, just after the number * of keys on disk at the head of the file */ offset = lseek(fd, TSSPS_KEYS_OFFSET, SEEK_SET); if (offset == ((off_t)-1)) { LogDebug("lseek: %s", strerror(errno)); return TSPERR(TSS_E_INTERNAL_ERROR); } for (i = 0; i < num_keys; i++) { offset = lseek(fd, 0, SEEK_CUR); if (offset == ((off_t)-1)) { LogDebug("lseek: %s", strerror(errno)); return TSPERR(TSS_E_INTERNAL_ERROR); } c->offset = offset; /* read UUID */ if ((result = read_data(fd, (void *)&c->uuid, sizeof(TSS_UUID)))) { LogDebug("%s", __FUNCTION__); return result; } /* read parent UUID */ if ((result = read_data(fd, (void *)&c->parent_uuid, sizeof(TSS_UUID)))) { LogDebug("%s", __FUNCTION__); return result; } /* pub data size */ if ((result = read_data(fd, &c->pub_data_size, sizeof(UINT16)))) { LogDebug("%s", __FUNCTION__); return result; } DBG_ASSERT(c->pub_data_size <= 2048 && c->pub_data_size > 0); /* blob size */ if ((result = read_data(fd, &c->blob_size, sizeof(UINT16)))) { LogDebug("%s", __FUNCTION__); return result; } DBG_ASSERT(c->blob_size <= 4096 && c->blob_size > 0); /* vendor data size */ if ((result = read_data(fd, &c->vendor_data_size, sizeof(UINT32)))) { LogDebug("%s", __FUNCTION__); return result; } /* cache flags */ if ((result = read_data(fd, &c->flags, sizeof(UINT16)))) { LogDebug("%s", __FUNCTION__); return result; } if (c->pub_data_size == pub_size) { /* read in the pub key */ if ((result = read_data(fd, blob, c->pub_data_size))) { LogDebug("%s", __FUNCTION__); return result; } if (!memcmp(blob, pub, pub_size)) break; } /* fast forward over the blob */ offset = lseek(fd, c->blob_size, SEEK_CUR); if (offset == ((off_t)-1)) { LogDebug("lseek: %s", strerror(errno)); return TSPERR(TSS_E_INTERNAL_ERROR); } /* ignore vendor data */ } return TSS_SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -