口臭严重是什么原因| cems是什么意思| 火烧是什么食物| 胖大海是什么东西| 眼疖子用什么药| 鸡蛋不能和什么食物一起吃| 默念是什么意思| 脖子上有结节挂什么科| 手足无措是什么意思| 什么头什么尾| 取环挂什么科室| 耳朵嗡嗡的响是什么原因| 眼睛痛用什么药| 什么是肠胃炎| 草包是什么意思| 什么人不适合做纹绣师| 表水是什么意思| 寄生虫吃什么药可以杀死| 萝莉控是什么意思| 文字属于五行属什么| 肠粘连吃什么药| 背水一战什么意思| 花儿为什么这样红简谱| 植物神经紊乱中医叫什么病| 可以组什么词语| 暴饮暴食容易得什么病| 尿出来很黄是什么原因| 众望所归是什么意思| 三皇五帝是什么时期| 神经性皮炎用什么药最好| 铁蛋白偏高是什么原因| 淀粉是什么| 梦到小男孩是什么意思| 李商隐号什么| 喝什么祛湿气效果最好| t1什么意思| 宫保鸡丁是什么菜系| 结婚10年是什么婚| 亚麻籽和什么相克| cognac是什么酒| 牙齿松动是什么原因引起的| 耳朵痒是什么预兆| 妇科病是什么| 脾肺两虚吃什么中成药| 女性下体长什么样| 斜杠青年什么意思| sunglasses什么意思| 胃痛打嗝什么原因| 什么是阴蒂| 徘徊是什么意思| 下加一笔是什么字| 益生菌对人体有什么好处| 逼格是什么意思| 可燃冰属于什么能源| 夏令时是什么| 深棕色是什么颜色| 白居易号什么居士| 肚脐眼左边疼是什么原因| 一什么骆驼| 什么的花纹| ds什么意思| 余事勿取什么意思| 痛风忌吃什么| 内疚是什么意思| 神经性皮炎用什么药膏| 女孩月经不规律是什么原因| 未属什么五行| 前列腺钙化有什么症状| 2019年是什么生肖| 豆角不能和什么一起吃| 切痣挂什么科| 梦见楼塌了是什么意思| 慢性浅表性胃炎伴糜烂吃什么药| 明月几时有的下一句是什么| 条的偏旁是什么| 一什么童话| 窘迫什么意思| 什么吹风机好用| 性激素六项什么时候查| 韩语欧巴是什么意思| 牙齿突然酸痛什么原因| 膝关节痛挂什么科| 强势是什么意思| 再生纤维素纤维是什么| 死间计划到底是什么| 诊疗是什么意思| 张靓颖什么星座| 血糖仪什么牌子好| 西红柿和番茄有什么区别| 看门神是什么生肖| 抗酸杆菌是什么意思| 身上长红痘痘是什么原因| 肾虚吃什么食物能补| 龙的本命佛是什么佛| 高的部首是什么| 十二月八号是什么星座| 蜂蜜和柠檬一起喝有什么作用| 月经期间吃什么食物最好| 两融是什么意思| 宝路华手表什么档次| 什么不绝| 安五行属性是什么| 做梦梦到拉屎是什么意思| 老人双脚浮肿是什么原因| 反流性食管炎挂什么科| 属虎的五行属什么| 月经吃什么水果| 女人做春梦预示着什么| 南海龙王叫什么| 大树像什么| 绽开什么| 为什么一来月经就拉肚子| 1965年属什么| 肺有问题会出现什么症状| 贫血的人来姨妈会有什么症状| 印度是什么教| 彩金是什么金| 履历是什么意思| 女生胸部发育到什么年龄| 一点是什么时辰| 血栓吃什么药| 青的五行属性是什么| 脑膜炎是什么原因引起的| 甲状腺是什么症状表现| 空调外机风扇不转是什么原因| 25是什么意思| 粉尘螨过敏是什么意思| 物色什么意思| 十月十六号是什么星座| 妈妈的弟弟的老婆叫什么| 手上的线分别代表什么图解| 腰突挂什么科| poa是什么意思| 什么情况要做支气管镜| 转注是什么意思| 病案号是什么意思| 什么是格林巴利综合症| 三头六臂指什么生肖| pab是什么意思| 蒸馏水敷脸有什么作用| 涉黑是什么意思| penis什么意思| 虎视眈眈是什么意思| 紊乱什么意思| 熬夜后吃什么恢复元气| 望闻问切的闻是什么意思| 18岁是什么年华| 阳痿早泄吃什么药好| pd990是什么金| 莲白是什么菜| flair是什么意思| 一什么摇篮| 什么什么不舍| 一个月一个亏念什么| 金先读什么| 脑震荡是什么症状| 24k镀金是什么意思| 什么情况下需要做宫腔镜| 什么是脱脂牛奶| 吃什么东西会误测怀孕| 腿上长痣代表什么| 风团是什么| 被舔下面什么感觉| 信阳毛尖属于什么茶| 梦见钓了好多鱼是什么意思| 什么是偏光眼镜| 新疆有什么民族| 芝士是什么味道| 小儿割包皮挂什么科| 艾滋病早期有什么症状| 什么是行政处罚| 养肝护肝吃什么药| 便潜血阳性什么意思| 刮痧的痧是什么东西| 末伏是什么意思| 什么是高危性行为| 四川人喜欢吃什么| 嘴巴长溃疡是什么原因| 欢五行属什么| 素肉是什么| 黄茶属于什么茶| 小寄居蟹吃什么| 乔迁是什么意思| 凌晨五点是什么时辰| 身心俱疲是什么意思| 白俄罗斯和俄罗斯有什么区别| 补充胶原蛋白吃什么最好| 一什么树干| 正常大便是什么颜色| 收缩压是什么| 病毒五项检查的是什么| 什么原因导致宫外孕| 肝不好挂什么科室| 吃什么升白细胞比较快| 三教九流什么意思| 银耳长在什么地方| 青青子衿什么意思| 家长里短是什么意思| 为什么相爱的人却不能在一起| 国潮是什么| 过敏应该挂什么科| 牙龈一直肿不消什么原因| 涟漪什么意思| 身上有白斑块是什么原因造成的| 起水泡痒是什么原因| 女性支原体感染有什么症状| 暗疾是什么意思| 星光是什么意思| 姜对头发有什么作用| 为什么一同房就有炎症| 小腿胀痛是什么原因| 今年16岁属什么| 喝完酒早上吃什么好| 十全十美是什么生肖| 痛风吃什么药效果最好| 一只脚面肿是什么原因| 右脚踝肿是什么原因引起的| 早上起床腰酸痛是什么原因| 晚上睡觉经常醒是什么原因| 虎头蜂泡酒有什么功效| 千人千面是什么意思| 吃什么水果对肝好| 曾毅玲花什么关系| 黑色的猫是什么品种| 两肺结节是什么意思| 语无伦次是什么意思| 铝中毒有什么症状| 圆圆的什么| 日本人什么时候投降的| 吃花生米有什么好处| 银耳是什么| 什么时间立秋| 糖尿病2型是什么意思| 什么是紫癜| 梅毒查血查什么项目| 变爻是什么意思| 嘴唇淡紫色是什么原因| 豆蔻是什么| 占卜是什么意思| 股票的量比是什么意思| fredperry是什么牌子| 丝瓜有什么营养| 眼睛痛吃什么药好得快| 登革热是什么病| 鸡翅木是什么木| 疏肝解郁是什么意思| 百丽鞋属于什么档次| 秋天有什么景物| 胆毛糙是什么原因| 磨牙是什么原因引起的如何治疗| 疖肿是什么样子的图片| 属蛇男和什么属相最配| 巴基斯坦用什么语言| 干眼症是什么| 左眼屈光不正是什么意思| 什么的叮咛| 狐臭是什么引起的| 类风湿不能吃什么东西| 沙漏是什么意思| 凝血五项是检查什么的| 太容易出汗是什么原因| silence是什么意思| 嗜酸性粒细胞偏高是什么意思| 为什么越睡越困| 百度
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;
}
西南方向是什么方位 处暑吃什么传统食物 3月17日什么星座 宫颈出血是什么原因 气胸是什么意思
星座上升是什么意思 俏皮话是什么意思 为什么会得风湿 3月12日什么星座 元宝是什么意思
血糖高一日三餐吃什么东西最适合 辞职是什么意思 男性尿路感染有什么症状 香蕉为什么是弯的 压床娃娃有什么讲究吗
内衣34是什么码 孩子发烧吃什么饭菜好 肝囊肿是什么原因造成的 儿童荨麻疹吃什么药 什么是生化妊娠
一喝酒就脸红是什么原因hcv8jop4ns0r.cn 火箭军是干什么的hcv7jop9ns9r.cn 心脏下边是什么器官hcv9jop4ns0r.cn 漂流需要准备什么东西bjcbxg.com 拍黄瓜是什么意思hcv9jop6ns3r.cn
落地成盒什么意思hcv8jop1ns8r.cn 狂躁症是什么hcv9jop7ns0r.cn 黑户什么意思hcv7jop6ns0r.cn 顾字五行属什么hcv9jop4ns5r.cn 2008年属鼠是什么命hcv9jop3ns6r.cn
烂尾是什么意思1949doufunao.com 桂花树施什么肥wzqsfys.com 做梦梦见马是什么意思hcv9jop0ns7r.cn 兔子爱吃什么hcv9jop1ns6r.cn 耳朵后面长痘痘是什么原因hcv9jop3ns1r.cn
au9999是什么意思hcv8jop0ns4r.cn 风热感冒吃什么消炎药kuyehao.com 十二朵玫瑰花代表什么意思hcv8jop4ns5r.cn 肝功能异常是什么意思travellingsim.com 什么是奇门遁甲hcv9jop0ns1r.cn
百度