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

📄 ps_utils.c

📁 可信计算 TPM 很重要的应用底层接口封装中间层 IBM资深人员开发
💻 C
📖 第 1 页 / 共 2 页
字号:
}/* * add a new cache entry for a written key */TSS_RESULTcache_key(UINT32 offset, UINT16 flags,		TSS_UUID *uuid, TSS_UUID *parent_uuid,		UINT16 pub_data_size, UINT32 blob_size,		UINT32 vendor_data_size){	struct key_disk_cache *tmp;	pthread_mutex_lock(&disk_cache_lock);	tmp = key_disk_cache_head;	for (; tmp; tmp = tmp->next) {		/* reuse an invalidated key cache entry */		if (!(tmp->flags & CACHE_FLAG_VALID))			goto fill_cache_entry;	}	tmp = malloc(sizeof(struct key_disk_cache));	if (tmp == NULL) {		LogError("malloc of %zd bytes failed.", sizeof(struct key_disk_cache));		pthread_mutex_unlock(&disk_cache_lock);		return TCSERR(TSS_E_INTERNAL_ERROR);	}	tmp->next = key_disk_cache_head;	key_disk_cache_head = tmp;fill_cache_entry:	tmp->offset = offset;#ifdef TSS_DEBUG	if (offset == 0)		LogDebug("Storing key with file offset==0!!!");#endif	tmp->flags = flags;	tmp->blob_size = blob_size;	tmp->pub_data_size = pub_data_size;	tmp->vendor_data_size = vendor_data_size;	memcpy(&tmp->uuid, uuid, sizeof(TSS_UUID));	memcpy(&tmp->parent_uuid, parent_uuid, sizeof(TSS_UUID));	pthread_mutex_unlock(&disk_cache_lock);	return TSS_SUCCESS;}/* * read into the PS file and return the number of keys */intget_num_keys_in_file(int fd){	UINT32 num_keys;	int rc;	/* go to the number of keys */	rc = lseek(fd, NUM_KEYS_OFFSET, SEEK_SET);	if (rc == ((off_t) - 1)) {		LogError("lseek: %s", strerror(errno));		return 0;	}	rc = read(fd, &num_keys, sizeof(UINT32));	if (rc < 0) {		LogError("read of %zd bytes: %s", sizeof(UINT32), strerror(errno));		return 0;	} else if ((unsigned)rc < sizeof(UINT32)) {		num_keys = 0;	}	return num_keys;}/* * count the number of valid keys in the cache */intget_num_keys(){	int num_keys = 0;	struct key_disk_cache *tmp;	pthread_mutex_lock(&disk_cache_lock);	tmp = key_disk_cache_head;	for (; tmp; tmp = tmp->next) {		if (tmp->flags & CACHE_FLAG_VALID)			num_keys++;	}	pthread_mutex_unlock(&disk_cache_lock);	return num_keys;}/* * 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     ] * [...] * *//* * read the PS file pointed to by fd and create a cache based on it */intinit_disk_cache(int fd){	UINT32 num_keys = get_num_keys_in_file(fd);	UINT16 tmp_offset, i;	int rc = 0, offset;	struct key_disk_cache *tmp, *prev = NULL;	BYTE srk_blob[2048];	TCPA_KEY srk_key;#ifdef TSS_DEBUG	int valid_keys = 0;#endif	pthread_mutex_lock(&disk_cache_lock);	if (num_keys == 0) {		key_disk_cache_head = NULL;		pthread_mutex_unlock(&disk_cache_lock);		return 0;	} else {		key_disk_cache_head = tmp = calloc(1, sizeof(struct key_disk_cache));		if (tmp == NULL) {			LogError("malloc of %zd bytes failed.",						sizeof(struct key_disk_cache));			rc = -1;			goto err_exit;		}	}	/* 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, KEYS_OFFSET, SEEK_SET);	if (offset == ((off_t) - 1)) {		LogError("lseek: %s", strerror(errno));		rc = -1;		goto err_exit;	}	for (i=0; i<num_keys; i++) {		offset = lseek(fd, 0, SEEK_CUR);		if (offset == ((off_t) - 1)) {			LogError("lseek: %s", strerror(errno));			rc = -1;			goto err_exit;		}		tmp->offset = offset;#ifdef TSS_DEBUG		if (offset == 0)			LogDebug("Storing key with file offset==0!!!");#endif		/* read UUID */		if ((rc = read_data(fd, (void *)&tmp->uuid, sizeof(TSS_UUID)))) {			LogError("%s", __FUNCTION__);			goto err_exit;		}		/* read parent UUID */		if ((rc = read_data(fd, (void *)&tmp->parent_uuid, sizeof(TSS_UUID)))) {			LogError("%s", __FUNCTION__);			goto err_exit;		}		/* pub data size */		if ((rc = read_data(fd, &tmp->pub_data_size, sizeof(UINT16)))) {			LogError("%s", __FUNCTION__);			goto err_exit;		}		DBG_ASSERT(tmp->pub_data_size <= 2048 && tmp->pub_data_size > 0);		/* blob size */		if ((rc = read_data(fd, &tmp->blob_size, sizeof(UINT16)))) {			LogError("%s", __FUNCTION__);			goto err_exit;		}		DBG_ASSERT(tmp->blob_size <= 4096 && tmp->blob_size > 0);		/* vendor data size */		if ((rc = read_data(fd, &tmp->vendor_data_size, sizeof(UINT32)))) {			LogError("%s", __FUNCTION__);			goto err_exit;		}		/* cache flags */		if ((rc = read_data(fd, &tmp->flags, sizeof(UINT16)))) {			LogError("%s", __FUNCTION__);			goto err_exit;		}#ifdef TSS_DEBUG		if (tmp->flags & CACHE_FLAG_VALID)			valid_keys++;#endif		/* fast forward over the pub key */		offset = lseek(fd, tmp->pub_data_size, SEEK_CUR);		if (offset == ((off_t) - 1)) {			LogError("lseek: %s", strerror(errno));			rc = -1;			goto err_exit;		}		/* if this is the SRK, load it into memory, since its already loaded in		 * the chip */		if (!memcmp(&SRK_UUID, &tmp->uuid, sizeof(TSS_UUID))) {			/* read SRK blob from disk */			if ((rc = read_data(fd, srk_blob, tmp->blob_size))) {				LogError("%s", __FUNCTION__);				goto err_exit;			}			tmp_offset = 0;			if ((rc = UnloadBlob_KEY_PS(&tmp_offset, srk_blob, &srk_key)))				goto err_exit;			/* add to the mem cache */			if ((rc = mc_add_entry_srk(SRK_TPM_HANDLE, SRK_TPM_HANDLE,						   &srk_key))) {				LogError("Error adding SRK to mem cache.");				destroy_key_refs(&srk_key);				goto err_exit;			}			destroy_key_refs(&srk_key);		} else {			/* fast forward over the blob */			offset = lseek(fd, tmp->blob_size, SEEK_CUR);			if (offset == ((off_t) - 1)) {				LogError("lseek: %s", strerror(errno));				rc = -1;				goto err_exit;			}			/* fast forward over the vendor data */			offset = lseek(fd, tmp->vendor_data_size, SEEK_CUR);			if (offset == ((off_t) - 1)) {				LogError("lseek: %s", strerror(errno));				rc = -1;				goto err_exit;			}		}		tmp->next = calloc(1, sizeof(struct key_disk_cache));		if (tmp->next == NULL) {			LogError("malloc of %zd bytes failed.",					sizeof(struct key_disk_cache));			rc = -1;			goto err_exit;		}		prev = tmp;		tmp = tmp->next;	}	/* delete the dangling, unfilled cache entry */	free(tmp);	prev->next = NULL;	rc = 0;	LogDebug("%s: found %d valid key(s) on disk.\n", __FUNCTION__, valid_keys);err_exit:	pthread_mutex_unlock(&disk_cache_lock);	return rc;}intclose_disk_cache(int fd){	struct key_disk_cache *tmp, *tmp_next;	if (key_disk_cache_head == NULL)		return 0;	pthread_mutex_lock(&disk_cache_lock);	tmp = key_disk_cache_head;	do {		tmp_next = tmp->next;		free(tmp);		tmp = tmp_next;	} while (tmp);	pthread_mutex_unlock(&disk_cache_lock);	return 0;}

⌨️ 快捷键说明

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