小鱼爱吃什么| 吃什么能减肥最快还能减全身| 二婚结婚需要什么证件| 什么食物含维生素b| 崇洋媚外是什么意思| 戊日是什么意思| 石蛋是什么| 吃什么能让肠道蠕动快| 胃炎吃什么中药| 荨麻疹是什么引起的| 格林巴利综合症是什么病| 女性尿道感染吃什么药| 解神是什么意思| 婧字五行属什么| aep是什么意思| 白子画什么时候爱上花千骨的| 太史慈姓什么| 破绽是什么意思| 屁股流血是什么原因| 头疼挂什么科室| 子宫内膜厚是什么意思| 什么是脂肪肝| 打碎碗是什么预兆| 胆水的成分是什么| 梦见自己得绝症了是什么预兆| 化疗什么意思| pad是什么设备| 人体缺钠会出现什么症状| 什么蛇没有毒| 人中上窄下宽代表什么| 什么是宫寒| 梦见狗死了是什么预兆| 肛门痒什么原因| 牙龈出血用什么牙膏| 十全十美是什么生肖| 梦见蚯蚓是什么预兆| 红楼梦为什么是四大名著之首| 胃有息肉的症状是什么| 喉咙痛有什么好办法| 相爱相杀是什么意思| 人吃什么才能长胖| 脾不统血吃什么中成药| 火镰是什么意思| 鸡婆是什么意思| 肝属于五行中的什么| 睾丸积液吃什么药| 梦到男孩子是什么意思| 人心是什么意思| 尿酸高是什么病| 床上放什么可以驱蟑螂| 前列腺增生吃什么药效果最好| 介错是什么意思| 炫耀是什么意思| 深褐色是什么颜色| 什么人不能吃茄子| 甲状腺与甲亢有什么区别| 葡萄补什么| 镜检红细胞是什么意思| 什么情况需要查凝血| 中医为什么不让睡凉席| 为什么精液是黄色的| 右下眼皮跳是什么预兆| 鸡痘用什么药效果好| 葡萄糖高是什么原因| 发烧感冒挂什么科室| 番茄是什么| 支原体培养及药敏是检查什么| 避孕套有什么牌子| 肾在五行中属什么| 做梦捡钱是什么预兆| c肽测定是什么意思| 切尔西是什么意思| 什么是收缩压和舒张压| 刘禹锡是什么朝代的| 舌根发黄是什么原因造成的| 杰瑞是什么品种的老鼠| 日成念什么| 什么的骆驼| 纵欲什么意思| 你喜欢我什么| 来大姨妈吃什么对身体好| 指甲有竖纹吃什么药| 炎症吃什么消炎药| 绿豆汤放什么糖最好| 奥美拉唑治什么胃病| 耳鸣是什么症状| fc是什么| 红薯的别名叫什么| 棺材中禁止放什么东西| hc是胎儿的什么| 南下是什么意思| 爱困总想睡觉什么原因| 阴虚有什么症状| 感冒有痰吃什么药| 7月14号是什么星座| 无花果吃了有什么好处| 1126是什么星座| 蓝莓什么时候吃最好| 浅表性胃炎吃什么药效果好| 维生素k是什么| co什么意思| 家里适合养什么鱼| 半夜鸡叫是什么征兆| 胃痉挛什么症状| 咬肌疼是什么原因| 女人消瘦应该检查什么| 降压药什么时间吃最好| 小孩口臭吃什么药效果最好| 中国民间为什么要吃腊八粥| 老师家访的目的是什么| 右眼皮上长痣代表什么| 肩膀痛挂什么科| 眼睛痒什么原因| 力什么神什么| 什么手机电池最耐用| 什么的街道| 脚底脱皮用什么药| 恍惚什么意思| 5月6日是什么星座| 妊娠高血压对胎儿有什么影响| 查高血压挂什么科| 父亲节应该送什么| 黄喉是什么部位| 377是什么| 五月一日是什么星座| 眼睛流眼泪用什么眼药水| 没有味觉是什么病| 枸杞泡水喝有什么好处| 阿华田是什么饮料| 唇红是什么原因| 猫咪黑下巴是什么原因| 557是什么意思| 耳鸣是什么原因引起的嗡嗡的响| 爱妃是什么意思| 峦读什么| 夜间胃痛是什么原因| 喝什么茶能降血压| ex是什么意思| 不什么一什么| au是什么货币| 皮囊炎用什么药膏| 左下腹疼痛挂什么科| 钾低了会出现什么症状| 癃闭是什么意思| 帝旺是什么意思| 白加黑是什么药| 产后吃什么水果好| 尹什么意思| 入伏天是什么意思| 十一月一号是什么星座| 老年人嘴唇发紫是什么原因| 舌头看什么科| 刑冲破害是什么意思| 杵状指见于什么病| 啷个是什么意思| 蚊子代表什么生肖| 为什么纯牛奶容易爆痘| 1976年出生属什么生肖| 麻疹是什么| 男人吃什么补肾壮阳效果最好| 越南有什么特产| 玛尼石是什么意思| 男士睾丸疼是什么原因| 等不到天黑烟火不会太完美什么歌| 窦房结内游走性心律是什么意思| 什么饼不能吃脑筋急转弯| 病入膏肓是什么意思| 114514是什么梗| 装模作样是什么生肖| 血沉高说明什么| 什么东西止血最快最好| 无创是什么| 舐犊是什么意思| 败血症是什么病| 角逐是什么意思| 春饼卷什么菜好吃| 蒙圈什么意思| 吃什么通血管| 肾萎缩是什么原因引起的| 杵状指常见于什么病| 李宁是什么运动员| zero是什么牌子| 甲沟炎属于什么科| alcon是什么牌子| 受益匪浅是什么意思| 喜欢绿色的女人是什么性格| 脉冲是什么| 马的贵人是什么生肖| 梦到僵尸是什么预兆| 梓是什么意思| 平均红细胞体积偏低是什么意思| 马弁是什么意思| 丝瓜只开花不结果是什么原因| 化石是什么| 包饺子剩下的面团能做什么| 什么是转基因食品| 酸枣仁配什么治疗失眠| 做梦梦到怀孕了是什么意思| 兴风作浪什么意思| 蒹葭苍苍是什么意思| 不食人间烟火是什么意思| 宫颈纳氏囊肿是什么意思严重吗| 什么是贫血| 阿司匹林什么时候吃| 手背有痣代表什么| 为什么会肚子痛| 脉搏弱是什么原因| 这次是我真的决定离开是什么歌| 咖喱是什么东西| 孕妇c反应蛋白高说明什么| 孩子容易出汗是什么原因| lotus是什么意思| 大肠杆菌是什么意思| 眼睛干痒用什么眼药水比较好| 巴宝莉属于什么档次| 客单价什么意思| 什么大| dos是什么| 无蔗糖是什么意思| 新生儿c反应蛋白高说明什么| 辰代表什么意思| 别开生面什么意思| 肾病综合症是什么病| 双肺纹理增强是什么意思| 扁桃体经常发炎是什么原因| 格斗和散打有什么区别| 桥本甲状腺炎有什么症状表现| 1992年属猴的是什么命| 叶酸什么牌子好| 鱼和熊掌不可兼得什么意思| 鱼扣是鱼的什么部位| 梦见蛇在家里是什么意思| 老人适合喝什么茶| 什么什么的阳光| 孔雀开屏是什么行为| 滴水不漏什么意思| 山药与什么食物相克| 什么流淌| a2是什么材质| 牙齿松动什么原因| 出水痘不能吃什么食物| 十月二十是什么星座| 脊柱疼是什么原因| 右脸有痣代表什么| 胃嗳气是什么症状| 什么都没有| 铜绿假单胞菌用什么抗生素| 金命是什么意思| 开场白是什么意思| 县公安局局长什么级别| 割包皮属于什么科| ufo是什么意思| 42岁属什么| 鲤鱼打挺是什么意思| 水银是什么| 嘶哑什么意思| 孔子孟子什么关系| 专升本需要考什么| 经信局是做什么的| 一什么春天| 低钾血症有什么症状| 3月6号是什么星座的| 什么是肺纤维化| 圣诞节送什么好| 百度
blob: 62909a4ff9a52f483f1c8f74abac7c243d9f5366 [file] [log] [blame] [edit]
/*
* Licensed Materials - Property of IBM
*
* trousers - An open source TCG Software Stack
*
* (C) Copyright International Business Machines Corp. 2004-2006
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#if defined (HAVE_BYTEORDER_H)
#include <sys/byteorder.h>
#elif defined (HTOLE_DEFINED)
#include <endian.h>
#define LE_16 htole16
#define LE_32 htole32
#define LE_64 htole64
#else
#define LE_16(x) (x)
#define LE_32(x) (x)
#define LE_64(x) (x)
#endif
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include "trousers/tss.h"
#include "trousers_types.h"
#include "tcsps.h"
#include "tcs_tsp.h"
#include "tcs_utils.h"
#include "capabilities.h"
#include "tcslog.h"
#include "tcsd_wrap.h"
#include "tcsd.h"
int system_ps_fd = -1;
MUTEX_DECLARE(disk_cache_lock);
static struct flock fl;
int
get_file()
{
int rc;
/* check the global file handle first. If it exists, lock it and return */
if (system_ps_fd != -1) {
int rc = 0;
fl.l_type = F_WRLCK;
if ((rc = fcntl(system_ps_fd, F_SETLKW, &fl))) {
LogError("failed to get system PS lock: %s", strerror(errno));
return -1;
}
return system_ps_fd;
}
/* open and lock the file */
system_ps_fd = open(tcsd_options.system_ps_file, O_CREAT|O_RDWR|O_NOFOLLOW, 0600);
if (system_ps_fd < 0) {
LogError("system PS: open() of %s failed: %s",
tcsd_options.system_ps_file, strerror(errno));
return -1;
}
fl.l_type = F_WRLCK;
if ((rc = fcntl(system_ps_fd, F_SETLKW, &fl))) {
LogError("failed to get system PS lock of file %s: %s",
tcsd_options.system_ps_file, strerror(errno));
return -1;
}
return system_ps_fd;
}
int
put_file(int fd)
{
int rc = 0;
/* release the file lock */
fl.l_type = F_UNLCK;
if ((rc = fcntl(fd, F_SETLKW, &fl))) {
LogError("failed to unlock system PS file: %s",
strerror(errno));
return -1;
}
return rc;
}
void
close_file(int fd)
{
close(fd);
system_ps_fd = -1;
}
TSS_RESULT
psfile_get_parent_uuid_by_uuid(int fd, TSS_UUID *uuid, TSS_UUID *ret_uuid)
{
int rc;
UINT32 file_offset = 0;
struct key_disk_cache *tmp;
MUTEX_LOCK(disk_cache_lock);
tmp = key_disk_cache_head;
while (tmp) {
if (memcmp(uuid, &tmp->uuid, sizeof(TSS_UUID)) || !(tmp->flags & CACHE_FLAG_VALID)) {
tmp = tmp->next;
continue;
}
/* jump to the location of the parent uuid */
file_offset = TSSPS_PARENT_UUID_OFFSET(tmp);
rc = lseek(fd, file_offset, SEEK_SET);
if (rc == ((off_t) - 1)) {
LogError("lseek: %s", strerror(errno));
MUTEX_UNLOCK(disk_cache_lock);
return -1;
}
if ((rc = read_data(fd, ret_uuid, sizeof(TSS_UUID)))) {
LogError("%s", __FUNCTION__);
MUTEX_UNLOCK(disk_cache_lock);
return rc;
}
MUTEX_UNLOCK(disk_cache_lock);
return TSS_SUCCESS;
}
MUTEX_UNLOCK(disk_cache_lock);
/* key not found */
return -2;
}
/*
* return a key blob from PS given a uuid
*/
TSS_RESULT
psfile_get_key_by_uuid(int fd, TSS_UUID *uuid, BYTE *ret_buffer, UINT16 *ret_buffer_size)
{
int rc;
UINT32 file_offset = 0;
struct key_disk_cache *tmp;
MUTEX_LOCK(disk_cache_lock);
tmp = key_disk_cache_head;
while (tmp) {
if (memcmp(uuid, &tmp->uuid, sizeof(TSS_UUID)) || !(tmp->flags & CACHE_FLAG_VALID)) {
tmp = tmp->next;
continue;
}
/* jump to the location of the key blob */
file_offset = TSSPS_BLOB_DATA_OFFSET(tmp);
rc = lseek(fd, file_offset, SEEK_SET);
if (rc == ((off_t) - 1)) {
LogError("lseek: %s", strerror(errno));
MUTEX_UNLOCK(disk_cache_lock);
return TCSERR(TSS_E_INTERNAL_ERROR);
}
/* we found the key; file ptr is pointing at the blob */
if (*ret_buffer_size < tmp->blob_size) {
/* not enough room */
MUTEX_UNLOCK(disk_cache_lock);
return TCSERR(TSS_E_FAIL);
}
if ((rc = read_data(fd, ret_buffer, tmp->blob_size))) {
LogError("%s", __FUNCTION__);
MUTEX_UNLOCK(disk_cache_lock);
return rc;
}
*ret_buffer_size = tmp->blob_size;
LogDebugUnrollKey(ret_buffer);
MUTEX_UNLOCK(disk_cache_lock);
return TSS_SUCCESS;
}
MUTEX_UNLOCK(disk_cache_lock);
/* key not found */
return TCSERR(TSS_E_FAIL);
}
/*
* return a key blob from PS given its cache entry. The disk cache must be
* locked by the caller.
*/
TSS_RESULT
psfile_get_key_by_cache_entry(int fd, struct key_disk_cache *c, BYTE *ret_buffer,
UINT16 *ret_buffer_size)
{
int rc;
UINT32 file_offset = 0;
/* jump to the location of the key blob */
file_offset = TSSPS_BLOB_DATA_OFFSET(c);
rc = lseek(fd, file_offset, SEEK_SET);
if (rc == ((off_t) - 1)) {
LogError("lseek: %s", strerror(errno));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
/* we found the key; file ptr is pointing at the blob */
if (*ret_buffer_size < c->blob_size) {
/* not enough room */
LogError("%s: Buf size too small. Needed %d bytes, passed %d", __FUNCTION__,
c->blob_size, *ret_buffer_size);
return TCSERR(TSS_E_INTERNAL_ERROR);
}
if ((rc = read_data(fd, ret_buffer, c->blob_size))) {
LogError("%s: error reading %d bytes", __FUNCTION__, c->blob_size);
return TCSERR(TSS_E_INTERNAL_ERROR);
}
*ret_buffer_size = c->blob_size;
return TSS_SUCCESS;
}
/*
* return the vendor data from PS given its cache entry. The disk cache must be
* locked by the caller.
*/
TSS_RESULT
psfile_get_vendor_data(int fd, struct key_disk_cache *c, UINT32 *size, BYTE **data)
{
int rc;
UINT32 file_offset;
/* jump to the location of the data */
file_offset = TSSPS_VENDOR_DATA_OFFSET(c);
rc = lseek(fd, file_offset, SEEK_SET);
if (rc == ((off_t) - 1)) {
LogError("lseek: %s", strerror(errno));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
if ((*data = malloc(c->vendor_data_size)) == NULL) {
LogError("malloc of %u bytes failed", c->vendor_data_size);
return TCSERR(TSS_E_OUTOFMEMORY);
}
if ((rc = read_data(fd, *data, c->vendor_data_size))) {
LogError("%s: error reading %u bytes", __FUNCTION__, c->vendor_data_size);
free(*data);
*data = NULL;
return TCSERR(TSS_E_INTERNAL_ERROR);
}
*size = c->vendor_data_size;
return TSS_SUCCESS;
}
TSS_RESULT
psfile_get_ps_type_by_uuid(int fd, TSS_UUID *uuid, UINT32 *ret_ps_type)
{
struct key_disk_cache *tmp;
MUTEX_LOCK(disk_cache_lock);
tmp = key_disk_cache_head;
while (tmp) {
if (memcmp(uuid, &tmp->uuid, sizeof(TSS_UUID)) ||
!(tmp->flags & CACHE_FLAG_VALID)) {
tmp = tmp->next;
continue;
}
if (tmp->flags & CACHE_FLAG_PARENT_PS_SYSTEM) {
*ret_ps_type = TSS_PS_TYPE_SYSTEM;
goto done;
} else
break;
}
*ret_ps_type = TSS_PS_TYPE_USER;
done:
MUTEX_UNLOCK(disk_cache_lock);
return TSS_SUCCESS;
}
TSS_RESULT
psfile_is_pub_registered(int fd, TCPA_STORE_PUBKEY *pub, TSS_BOOL *is_reg)
{
int rc;
UINT32 file_offset = 0;
struct key_disk_cache *tmp;
char tmp_buffer[2048];
MUTEX_LOCK(disk_cache_lock);
tmp = key_disk_cache_head;
while (tmp) {
/* if the key is of the wrong size or is invalid, try the next one */
if (pub->keyLength != tmp->pub_data_size || !(tmp->flags & CACHE_FLAG_VALID)) {
tmp = tmp->next;
continue;
}
/* we have a valid key with the same key size as the one we're looking for.
* grab the pub key data off disk and compare it. */
/* jump to the location of the public key */
file_offset = TSSPS_PUB_DATA_OFFSET(tmp);
rc = lseek(fd, file_offset, SEEK_SET);
if (rc == ((off_t) - 1)) {
LogError("lseek: %s", strerror(errno));
MUTEX_UNLOCK(disk_cache_lock);
return TCSERR(TSS_E_INTERNAL_ERROR);
}
DBG_ASSERT(tmp->pub_data_size < 2048);
/* read in the key */
if ((rc = read_data(fd, tmp_buffer, tmp->pub_data_size))) {
LogError("%s", __FUNCTION__);
MUTEX_UNLOCK(disk_cache_lock);
return rc;
}
/* do the compare */
if (memcmp(tmp_buffer, pub->key, tmp->pub_data_size)) {
tmp = tmp->next;
continue;
}
/* the key matches, copy the uuid out */
*is_reg = TRUE;
MUTEX_UNLOCK(disk_cache_lock);
return TSS_SUCCESS;
}
MUTEX_UNLOCK(disk_cache_lock);
/* key not found */
*is_reg = FALSE;
return TSS_SUCCESS;
}
TSS_RESULT
psfile_get_uuid_by_pub(int fd, TCPA_STORE_PUBKEY *pub, TSS_UUID **ret_uuid)
{
int rc;
UINT32 file_offset = 0;
struct key_disk_cache *tmp;
char tmp_buffer[2048];
MUTEX_LOCK(disk_cache_lock);
tmp = key_disk_cache_head;
while (tmp) {
/* if the key is of the wrong size or is invalid, try the next one */
if (pub->keyLength != tmp->pub_data_size || !(tmp->flags & CACHE_FLAG_VALID)) {
tmp = tmp->next;
continue;
}
/* we have a valid key with the same key size as the one we're looking for.
* grab the pub key data off disk and compare it. */
/* jump to the location of the public key */
file_offset = TSSPS_PUB_DATA_OFFSET(tmp);
rc = lseek(fd, file_offset, SEEK_SET);
if (rc == ((off_t) - 1)) {
LogError("lseek: %s", strerror(errno));
MUTEX_UNLOCK(disk_cache_lock);
return TCSERR(TSS_E_INTERNAL_ERROR);
}
DBG_ASSERT(tmp->pub_data_size < 2048);
if (tmp->pub_data_size > sizeof(tmp_buffer)) {
LogError("Source buffer size too big! Size: %d",
tmp->pub_data_size);
MUTEX_UNLOCK(disk_cache_lock);
return TCSERR(TSS_E_INTERNAL_ERROR);
}
/* read in the key */
if ((rc = read_data(fd, tmp_buffer, tmp->pub_data_size))) {
LogError("%s", __FUNCTION__);
MUTEX_UNLOCK(disk_cache_lock);
return rc;
}
/* do the compare */
if (memcmp(tmp_buffer, pub->key, tmp->pub_data_size)) {
tmp = tmp->next;
continue;
}
*ret_uuid = (TSS_UUID *)malloc(sizeof(TSS_UUID));
if (*ret_uuid == NULL) {
LogError("malloc of %zd bytes failed.", sizeof(TSS_UUID));
MUTEX_UNLOCK(disk_cache_lock);
return TCSERR(TSS_E_OUTOFMEMORY);
}
/* the key matches, copy the uuid out */
memcpy(*ret_uuid, &tmp->uuid, sizeof(TSS_UUID));
MUTEX_UNLOCK(disk_cache_lock);
return TSS_SUCCESS;
}
MUTEX_UNLOCK(disk_cache_lock);
/* key not found */
return TCSERR(TSS_E_PS_KEY_NOTFOUND);
}
TSS_RESULT
psfile_get_key_by_pub(int fd, TCPA_STORE_PUBKEY *pub, UINT32 *size, BYTE **ret_key)
{
int rc;
UINT32 file_offset = 0;
struct key_disk_cache *tmp;
BYTE tmp_buffer[4096];
MUTEX_LOCK(disk_cache_lock);
tmp = key_disk_cache_head;
while (tmp) {
/* if the key is of the wrong size or is invalid, try the next one */
if (pub->keyLength != tmp->pub_data_size || !(tmp->flags & CACHE_FLAG_VALID)) {
tmp = tmp->next;
continue;
}
/* we have a valid key with the same key size as the one we're looking for.
* grab the pub key data off disk and compare it. */
/* jump to the location of the public key */
file_offset = TSSPS_PUB_DATA_OFFSET(tmp);
rc = lseek(fd, file_offset, SEEK_SET);
if (rc == ((off_t) - 1)) {
LogError("lseek: %s", strerror(errno));
MUTEX_UNLOCK(disk_cache_lock);
return TCSERR(TSS_E_INTERNAL_ERROR);
}
DBG_ASSERT(tmp->pub_data_size < 2048);
if (tmp->pub_data_size > sizeof(tmp_buffer)) {
LogError("Source buffer size too big! Size: %d",
tmp->pub_data_size);
MUTEX_UNLOCK(disk_cache_lock);
return TCSERR(TSS_E_INTERNAL_ERROR);
}
/* read in the key */
if ((rc = read_data(fd, tmp_buffer, tmp->pub_data_size))) {
LogError("%s", __FUNCTION__);
MUTEX_UNLOCK(disk_cache_lock);
return rc;
}
/* do the compare */
if (memcmp(tmp_buffer, pub->key, tmp->pub_data_size)) {
tmp = tmp->next;
continue;
}
/* jump to the location of the key blob */
file_offset = TSSPS_BLOB_DATA_OFFSET(tmp);
rc = lseek(fd, file_offset, SEEK_SET);
if (rc == ((off_t) - 1)) {
LogError("lseek: %s", strerror(errno));
MUTEX_UNLOCK(disk_cache_lock);
return TCSERR(TSS_E_INTERNAL_ERROR);
}
DBG_ASSERT(tmp->blob_size < 4096);
if (tmp->blob_size > sizeof(tmp_buffer)) {
LogError("Blob size greater than 4096! Size: %d",
tmp->blob_size);
MUTEX_UNLOCK(disk_cache_lock);
return TCSERR(TSS_E_INTERNAL_ERROR);
}
/* read in the key blob */
if ((rc = read_data(fd, tmp_buffer, tmp->blob_size))) {
LogError("%s", __FUNCTION__);
MUTEX_UNLOCK(disk_cache_lock);
return rc;
}
*ret_key = malloc(tmp->blob_size);
if (*ret_key == NULL) {
LogError("malloc of %d bytes failed.", tmp->blob_size);
MUTEX_UNLOCK(disk_cache_lock);
return TCSERR(TSS_E_OUTOFMEMORY);
}
memcpy(*ret_key, tmp_buffer, tmp->blob_size);
*size = tmp->blob_size;
MUTEX_UNLOCK(disk_cache_lock);
return rc;
}
MUTEX_UNLOCK(disk_cache_lock);
/* key not found */
return -2;
}
/*
* disk store format:
*
* TrouSerS 0.2.0 and before:
* Version 0: cached?
* [UINT32 num_keys_on_disk]
* [TSS_UUID uuid0 ] yes
* [TSS_UUID uuid_parent0 ] yes
* [UINT16 pub_data_size0 ] yes
* [UINT16 blob_size0 ] yes
* [UINT16 cache_flags0 ] yes
* [BYTE[] pub_data0 ]
* [BYTE[] blob0 ]
* [...]
*
* 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_RESULT
psfile_write_key(int fd,
TSS_UUID *uuid,
TSS_UUID *parent_uuid,
UINT32 *parent_ps,
BYTE *vendor_data,
UINT32 vendor_size,
BYTE *key_blob,
UINT16 key_blob_size)
{
TSS_KEY key;
UINT16 pub_key_size, cache_flags = CACHE_FLAG_VALID;
UINT64 offset;
int rc = 0;
/* leaving the cache flag for parent ps type as 0 implies TSS_PS_TYPE_USER */
if (*parent_ps == TSS_PS_TYPE_SYSTEM)
cache_flags |= CACHE_FLAG_PARENT_PS_SYSTEM;
/* Unload the blob to get the public key */
offset = 0;
if ((rc = UnloadBlob_TSS_KEY(&offset, key_blob, &key)))
return rc;
pub_key_size = key.pubKey.keyLength;
if ((rc = write_key_init(fd, pub_key_size, key_blob_size, vendor_size)) < 0)
goto done;
/* offset now holds the number of bytes from the beginning of the file
* the key will be stored at
*/
offset = rc;
#ifdef TSS_DEBUG
if (offset == 0)
LogDebug("ERROR: key being written with offset 0!!");
#endif
/* [TSS_UUID uuid0 ] yes */
if ((rc = write_data(fd, (void *)uuid, sizeof(TSS_UUID)))) {
LogError("%s", __FUNCTION__);
goto done;
}
/* [TSS_UUID uuid_parent0 ] yes */
if ((rc = write_data(fd, (void *)parent_uuid, sizeof(TSS_UUID)))) {
LogError("%s", __FUNCTION__);
goto done;
}
/* [UINT16 pub_data_size0 ] yes */
pub_key_size = LE_16(pub_key_size);
if ((rc = write_data(fd, &pub_key_size, sizeof(UINT16)))) {
LogError("%s", __FUNCTION__);
goto done;
}
/* Swap it back for later */
pub_key_size = LE_16(pub_key_size);
/* [UINT16 blob_size0 ] yes */
key_blob_size = LE_16(key_blob_size);
if ((rc = write_data(fd, &key_blob_size, sizeof(UINT16)))) {
LogError("%s", __FUNCTION__);
goto done;
}
/* Swap it back for later */
key_blob_size = LE_16(key_blob_size);
/* [UINT32 vendor_data_size0 ] yes */
vendor_size = LE_32(vendor_size);
if ((rc = write_data(fd, &vendor_size, sizeof(UINT32)))) {
LogError("%s", __FUNCTION__);
goto done;
}
/* Swap it back for later */
vendor_size = LE_32(vendor_size);
/* [UINT16 cache_flags0 ] yes */
cache_flags = LE_16(cache_flags);
if ((rc = write_data(fd, &cache_flags, sizeof(UINT16)))) {
LogError("%s", __FUNCTION__);
goto done;
}
/* Swap it back for later */
cache_flags = LE_16(cache_flags);
/* [BYTE[] pub_data0 ] no */
if ((rc = write_data(fd, (void *)key.pubKey.key, pub_key_size))) {
LogError("%s", __FUNCTION__);
goto done;
}
/* [BYTE[] blob0 ] no */
if ((rc = write_data(fd, (void *)key_blob, key_blob_size))) {
LogError("%s", __FUNCTION__);
goto done;
}
/* [BYTE[] vendor_data0 ] no */
if (vendor_size > 0) {
if ((rc = write_data(fd, (void *)vendor_data, vendor_size))) {
LogError("%s", __FUNCTION__);
goto done;
}
}
if ((rc = cache_key((UINT32)offset, cache_flags, uuid, parent_uuid, pub_key_size,
key_blob_size, vendor_size)))
goto done;
done:
destroy_key_refs(&key);
return rc;
}
TSS_RESULT
psfile_remove_key(int fd, struct key_disk_cache *c)
{
TSS_RESULT result;
UINT32 head_offset = 0, tail_offset, num_keys;
BYTE buf[4096];
struct stat stat_buf;
int rc, size = 0;
if ((rc = fstat(fd, &stat_buf)) != 0) {
LogError("fstat: %s", strerror(errno));
return TSS_E_INTERNAL_ERROR;
}
/* head_offset is the offset the beginning of the key */
head_offset = TSSPS_UUID_OFFSET(c);
/* tail_offset is the offset the beginning of the next key */
tail_offset = TSSPS_VENDOR_DATA_OFFSET(c) + c->vendor_data_size;
rc = lseek(fd, tail_offset, SEEK_SET);
if (rc == ((off_t) - 1)) {
LogError("lseek: %s", strerror(errno));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
/* read in from tail, write out to head to fill the gap */
while ((rc = read(fd, buf, sizeof(buf))) > 0) {
size = rc;
tail_offset += size;
/* set the file pointer to where we want to write */
rc = lseek(fd, head_offset, SEEK_SET);
if (rc == ((off_t) - 1)) {
LogError("lseek: %s", strerror(errno));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
/* write the data */
if ((result = write_data(fd, (void *)buf, size))) {
LogError("%s", __FUNCTION__);
return result;
}
head_offset += size;
/* set the file pointer to where we want to read in the next
* loop */
rc = lseek(fd, tail_offset, SEEK_SET);
if (rc == ((off_t) - 1)) {
LogError("lseek: %s", strerror(errno));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
}
if (rc < 0) {
LogError("read: %s", strerror(errno));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
/* set the file pointer to where we want to write */
rc = lseek(fd, head_offset, SEEK_SET);
if (rc == ((off_t) - 1)) {
LogError("lseek: %s", strerror(errno));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
/* head_offset now contains a pointer to where we want to truncate the
* file. Zero out the old tail end of the file and truncate it. */
memset(buf, 0, sizeof(buf));
/* Zero out the old tail end of the file */
if ((result = write_data(fd, (void *)buf, tail_offset - head_offset))) {
LogError("%s", __FUNCTION__);
return result;
}
if ((rc = ftruncate(fd, head_offset)) < 0) {
LogError("ftruncate: %s", strerror(errno));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
/* we succeeded in removing a key from the disk. Decrement the number
* of keys in the file */
rc = lseek(fd, TSSPS_NUM_KEYS_OFFSET, SEEK_SET);
if (rc == ((off_t) - 1)) {
LogError("lseek: %s", strerror(errno));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
rc = read(fd, &num_keys, sizeof(UINT32));
num_keys = LE_32(num_keys);
if (rc != sizeof(UINT32)) {
LogError("read of %zd bytes: %s", sizeof(UINT32), strerror(errno));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
rc = lseek(fd, TSSPS_NUM_KEYS_OFFSET, SEEK_SET);
if (rc == ((off_t) - 1)) {
LogError("lseek: %s", strerror(errno));
return TCSERR(TSS_E_INTERNAL_ERROR);
}
/* decrement, then write back out to disk */
num_keys--;
num_keys = LE_32(num_keys);
if ((result = write_data(fd, (void *)&num_keys, sizeof(UINT32)))) {
LogError("%s", __FUNCTION__);
return result;
}
return TSS_SUCCESS;
}
jdv是什么牌子 黄皮肤适合什么颜色的衣服 奥沙利文为什么叫火箭 小乌龟吃什么食物 九零年属什么生肖
阴超可以检查出什么 顾字五行属什么 正月二十九是什么星座 腹胀是什么原因引起的 湿气重可以吃什么
arf是什么意思 什么是pp材质 denham是什么牌子 点天灯是什么意思 木行念什么
没意思是什么意思 被银环蛇咬了有什么症状 肛门下坠是什么原因 左眼皮一直跳什么预兆 复方甘草酸苷片治什么病
什么的口罩hcv8jop8ns1r.cn 舌苔腻是什么意思hcv9jop4ns6r.cn 病毒性感冒咳嗽吃什么药效果好hcv8jop2ns9r.cn 自我价值是什么意思hcv9jop3ns9r.cn 阴道炎吃什么药beikeqingting.com
结论是什么意思bjhyzcsm.com 何炅和谢娜是什么关系hebeidezhi.com 空调变频和定频有什么区别hcv8jop2ns0r.cn 后人是什么意思hcv8jop1ns9r.cn 口腔溃疡反反复复是什么原因qingzhougame.com
消炎药都有什么hcv9jop1ns4r.cn 多糖是什么hcv8jop4ns0r.cn 天冬氨酸氨基转移酶高是什么原因hcv8jop0ns4r.cn 肝小钙化灶是什么意思hcv8jop4ns2r.cn 眼睛干痒滴什么眼药水hcv8jop7ns9r.cn
常州冬至吃什么hcv9jop5ns0r.cn 缺碘有什么症状hkuteam.com 梦见梳头发是什么意思hcv8jop7ns0r.cn dunk是什么意思hcv7jop9ns5r.cn 龙凤呈祥是什么意思qingzhougame.com
百度