您现在的位置是:首页 >技术交流 >tpm2-tools源码分析之tpm2_evictcontrol.c(2)网站首页技术交流
tpm2-tools源码分析之tpm2_evictcontrol.c(2)
接前一篇文章:tpm2-tools源码分析之tpm2_evictcontrol.c(1)
本文对tpm2_evictcontrol.c中的tpm2_tool_onstart函数进行详细解析。
先再次贴出该函数源码:
static bool tpm2_tool_onstart(tpm2_options **opts) {
const struct option topts[] = {
{ "hierarchy", required_argument, 0, 'C' },
{ "auth", required_argument, 0, 'P' },
{ "object-context", required_argument, 0, 'c' },
{ "output", required_argument, 0, 'o' },
{ "cphash", required_argument, 0, 0 },
};
*opts = tpm2_options_new("C:P:c:o:", ARRAY_LEN(topts), topts, on_option,
on_arg, 0);
return *opts != 0;
}
tpm2_options结构的定义在tpm2-tools/lib/tpm2_options.h中,代码如下:
struct tpm2_options {
struct {
tpm2_option_handler on_opt;
tpm2_arg_handler on_arg;
} callbacks;
char *short_opts;
size_t len;
uint32_t flags;
struct option long_opts[];
};
typedef struct tpm2_options tpm2_options;
struct option的定义在/usr/include/bits/getopt_ext.h中,代码如下:
struct option
{
const char *name;
/* has_arg can't be an enum because some compilers complain about
type mismatches in all the code that assumes it is an int. */
int has_arg;
int *flag;
int val;
};
on_option函数的实现在同文件(tools/tpm2_evictcontrol.c)中,如下:
static bool on_option(char key, char *value) {
switch (key) {
case 'C':
ctx.auth_hierarchy.ctx_path = value;
break;
case 'P':
ctx.auth_hierarchy.auth_str = value;
break;
case 'c':
ctx.to_persist_key.ctx_path = value;
break;
case 'o':
ctx.output_arg = value;
break;
case 0:
ctx.cp_hash_path = value;
break;
}
return true;
}
要更好地理解这些选项乃至tpm2_tool_onstart函数的功能,需要与tpm2_evictcontrol命令的说明相结合来看。tpm2_evictcontrol命令的详细说明参见:
tpm2-tools/tpm2_evictcontrol.1.md at master · tpm2-software/tpm2-tools · GitHub
下载了源码后,在tpm2-tools/man/tpm2_evictcontrol.1.md文件中。
其中的参数说明如下:
OPTIONS
-C, --hierarchy=OBJECT:
The authorization hierarchy used to authorize the commands. Defaults to the "owner" hierarchy. Supported options are: —— 用于命令授权的授权层级。默认为“owner层级”。支持的选项包括:
- o for TPM_RH_OWNER
- p for TPM_RH_PLATFORM
<num>
where a raw number can be used. —— <num> 可以使用的原始数字。-c, --object-context=OBJECT:
A context object specifier of a transient or persistent object. If OBJECT is a transient object it will be persisted, either to the handle specified by the argument or to first available vacant persistent handle. If the OBJECT is for a persistent object, then the object will be evicted from non-volatile memory. —— 一个临时的或持久的上下文对象的指示符。如果OBJECT是一个临时对象,它将被持久化,(持久化)到参数指定的句柄或者第一个可用的空闲持久句柄;如果OBJECT是一个持久性对象,则此对象将从非易失性存储器中移除。
-P, --auth=AUTH:
The authorization value for the hierarchy specified with -C. —— -C指定的层级的授权值。
-o, --output=FILE:
Optionally output a serialized object representing the persistent handle. If untampered, these files are safer to use then raw persistent handles. A raw persistent handle should be verified that the object it points to is as expected. —— 可选地输出表示持久句柄的序列化对象。如果未经处理,则使用这些文件比使用原始持久句柄更安全。应验证原始持久句柄所指向的对象是否符合预期。
--cphash=FILE
File path to record the hash of the command parameters. This is commonly termed as cpHash. NOTE: When this option is selected, The tool will not actually execute the command, it simply returns a cpHash. —— 记录命令参数的哈希值的文件路径。这通常被称为cpHash。注意:当此选项被选择,该工具不会实际执行命令,它只是返回一个cpHash。
ARGUMENT the command line argument specifies the persistent handle to save the transient object to. —— 命令行参数指定的用于保存临时性对象的持久性句柄。
tpm2_options_new函数属于公共代码,在tpm2-tools/lib/tpm2_options.c中,代码如下:
tpm2_options *tpm2_options_new(const char *short_opts, size_t len,
const struct option *long_opts, tpm2_option_handler on_opt,
tpm2_arg_handler on_arg, uint32_t flags) {
tpm2_options *opts = calloc(1, sizeof(*opts) + (sizeof(*long_opts) * len));
if (!opts) {
LOG_ERR("oom");
return NULL;
}
/*
* On NULL, just make it a zero length string so we don't have to keep
* checking it for NULL.
*/
if (!short_opts) {
short_opts = "";
}
opts->short_opts = strdup(short_opts);
if (!opts->short_opts) {
LOG_ERR("oom");
free(opts);
return NULL;
}
opts->callbacks.on_opt = on_opt;
opts->callbacks.on_arg = on_arg;
opts->len = len;
opts->flags = flags;
memcpy(opts->long_opts, long_opts, len * sizeof(*long_opts));
return opts;
}
tpm2_new_options函数很容易理解,其功能是基于tpm2_tool_onstart函数中的struct option topts构建tpm2_options实例(*opts)。
至此,tpm2_evictcontrol.c中的tpm2_tool_onstart函数就基本分析完了。