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

📄 tcsps.c

📁 TCG软件栈 linux系统上使用 为可信应用软件提供和 TPM通信的 接口 其网站上有很多关于使用此软件的测试用例
💻 C
📖 第 1 页 / 共 2 页
字号:
                }		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;		}		*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_RESULTpsfile_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);		/* 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);		/* 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_RESULTpsfile_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 */        if ((rc = write_data(fd, &pub_key_size, sizeof(UINT16)))) {		LogError("%s", __FUNCTION__);                goto done;	}	/* [UINT16   blob_size0      ] yes */        if ((rc = write_data(fd, &key_blob_size, sizeof(UINT16)))) {		LogError("%s", __FUNCTION__);                goto done;	}	/* [UINT32   vendor_data_size0 ] yes */        if ((rc = write_data(fd, &vendor_size, sizeof(UINT32)))) {		LogError("%s", __FUNCTION__);                goto done;	}	/* [UINT16   cache_flags0    ] yes */        if ((rc = write_data(fd, &cache_flags, sizeof(UINT16)))) {		LogError("%s", __FUNCTION__);                goto done;	}	/* [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_RESULTpsfile_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));	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--;	if ((result = write_data(fd, (void *)&num_keys, sizeof(UINT32)))) {		LogError("%s", __FUNCTION__);		return result;	}	return TSS_SUCCESS;}

⌨️ 快捷键说明

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