⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcsps.c

📁 TCG软件栈 linux系统上使用 为可信应用软件提供和 TPM通信的 接口 其网站上有很多关于使用此软件的测试用例
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * 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>#include <assert.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);intget_file(){	int rc = 0;	/* check the global file handle first.  If it exists, lock it and return */	if (system_ps_fd != -1) {		if ((rc = flock(system_ps_fd, LOCK_EX))) {			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, 0600);	if (system_ps_fd < 0) {		LogError("system PS: open() of %s failed: %s",				tcsd_options.system_ps_file, strerror(errno));		return -1;	}	if ((rc = flock(system_ps_fd, LOCK_EX))) {		LogError("failed to get system PS lock of file %s: %s",				tcsd_options.system_ps_file, strerror(errno));		return -1;	}	return system_ps_fd;}intput_file(int fd){	int rc = 0;	/* release the file lock */	if ((rc = flock(fd, LOCK_UN))) {		LogError("failed to unlock system PS file: %s", strerror(errno));		return -1;	}	return rc;}voidclose_file(int fd){	close(fd);	system_ps_fd = -1;}TSS_RESULTpsfile_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_RESULTpsfile_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_RESULTpsfile_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_RESULTpsfile_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_RESULTpsfile_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_RESULTpsfile_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_RESULTpsfile_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);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -