tpm2-tools ソースコード解析の tpm2_nvundefine.c (1)

TPM 2.0 の tpm2_nvundefine コマンドに対応するソース ファイルは tpm2_nvundefine.c で、tpm2-tools/tools/ にあり、合計 495 行あります (バージョン 5.5)。

tpm2_nvundefine の機能は、不揮発性 (NV) インデックスを削除することです。tpm2_nvdefine で以前に定義された不揮発性 (NV) インデックスを削除します。

以下では、いくつかの記事の長さを利用して、tpm2_nvundefine コマンドと組み合わせた tpm2_nvundefine.c ファイルの詳細かつ完全な分析を実行します。

最初のコード部分を見てみましょう。

// Register this tool with tpm2_tool.c
TPM2_TOOL_REGISTER("nvundefine", tpm2_tool_onstart, tpm2_tool_onrun,
    tpm2_tool_onstop, NULL)

TPM2_TOOL_REGISTER はマクロ定義であり、tpm2-tools/tools/tpm2_tool.h 内のコードは次のとおりです。

#define TPM2_TOOL_REGISTER(tool_name,tool_onstart,tool_onrun,tool_onstop,tool_onexit) \
	static const tpm2_tool tool = { \
		.name		= tool_name, \
		.onstart	= tool_onstart, \
		.onrun		= tool_onrun, \
		.onstop		= tool_onstop, \
		.onexit		= tool_onexit, \
	}; \
	static void \
	__attribute__((__constructor__)) \
	__attribute__((__used__)) \
	_tpm2_tool_init(void) \
	{ \
		tpm2_tool_register(&tool); \
	}

このファイル tpm2_nvundefine.c では、tpm2_nvundefine コマンドのマクロ展開が次のようになっているとも言えます。

static const tpm2_tool tool = {
    .name		= "nvundefine",
	.onstart	= tpm2_tool_onstart,
	.onrun		= tpm2_tool_onrun,
	.onstop		= tpm2_tool_onstop,
	.onexit		= NULL,
};
static void
	__attribute__((__constructor__))
	__attribute__((__used__))
    _tpm2_tool_init(void)
{
		tpm2_tool_register(&tool);
}

tpm2_tool 構造体の定義は tpm2-tools/tools/tpm2_tool.h にもあり、コードは次のとおりです。

typedef struct {
	const char * name;
	tpm2_tool_onstart_t onstart;
	tpm2_tool_onrun_t onrun;
	tpm2_tool_onstop_t onstop;
	tpm2_tool_onexit_t onexit;
} tpm2_tool;

これに含まれる関連する関数ポインターは次のとおりです (同じファイルのすぐ上にあります)。

/**
 * An optional interface for tools to specify what options they support.
 * They are concatenated with main's options and passed to getopt_long.
 * @param opts
 *  The callee can choose to set *opts to a tpm_options pointer allocated
 *  via tpm2_options_new(). Setting *opts to NULL is not an error, and
 *  Indicates that no options are specified by the tool.
 *
 * @return
 *  True on success, false on error.
 */
typedef bool (*tpm2_tool_onstart_t)(tpm2_options **opts);
 
/**
 * This is the main interface for tools, after tcti and sapi/esapi initialization
 * are performed.
 * @param ectx
 *  The system/esapi api context.
 * @param flags
 *  Flags that tools may wish to respect.
 * @return
 *  A tool_rc indicating status.
 */
typedef tool_rc (*tpm2_tool_onrun_t)(ESYS_CONTEXT *ectx, tpm2_option_flags flags);
 
/**
 * Called after tpm2_tool_onrun() is invoked. ESAPI context is still valid during this call.
 * @param ectx
 *  The system/esapi api context.
 * @return
 *  A tool_rc indicating status.
 */
typedef tool_rc (*tpm2_tool_onstop_t)(ESYS_CONTEXT *ectx);
 
/**
 * Called when the tool is exiting, useful for cleanup.
 */
typedef void (*tpm2_tool_onexit_t)(void);

tpm2_tool_register 関数は tpm2-tools/tools/tpm2_tools.c に実装されており、コードは次のとおりです。

/*
 * Build a list of the TPM2 tools linked into this executable
 */
#ifndef TPM2_TOOLS_MAX
#define TPM2_TOOLS_MAX 1024
#endif
static const tpm2_tool *tools[TPM2_TOOLS_MAX];
static unsigned tool_count;
 
void tpm2_tool_register(const tpm2_tool *tool) {
 
    if (tool_count < TPM2_TOOLS_MAX) {
        tools[tool_count++] = tool;
    } else {
        LOG_ERR("Over tool count");
        abort();
    }
}

tpm2_nvundefine.c に戻って、特定の関数を確認します。

(1)tpm2_tool_onstart

tpm2_tool_onstart 関数のコードは次のとおりです。

static bool tpm2_tool_onstart(tpm2_options **opts) {
/*
 * The tool does both undefine and undefine space special and so the options
 * are interpreted accordingly.
 * 
 * Case NV_Undefine:
 *       1. 'C' and 'P' correspond to either TPM2_RH_OWNER or TPM2_RH_PLATFORM
 *       2. In this case, two aux sessions are allowed.
 * Case NV_UndefineSpaceSpecial:
 *       1. 'S' is for the NV-Index --> Object#1, Session#1.
 *       2. 'C' and 'P' is defaulted to TPM2_RH_PLATFORM --> Object#2, Session#2
 *       3. In this case, just one aux session is allowed.
 *       4. Additional option --with-policydelete is required with tcti=none as
 *          the NV index isn't available to read the attribute when only
 *          calculating cpHash and command is not dispatched.
 */
    const struct option topts[] = {
        { "hierarchy",          required_argument, NULL, 'C' },
        { "auth",               required_argument, NULL, 'P' },
        { "session",            required_argument, NULL, 'S' },
        { "cphash",             required_argument, NULL,  0  },
        { "rphash",             required_argument, NULL,  1  },
        { "with-policydelete",  no_argument,       NULL,  2  },
        { "name",               required_argument, NULL, 'n' },
    };

    *opts = tpm2_options_new("C:P:S:n:", ARRAY_LEN(topts), topts, on_option,
        on_arg, TPM2_OPTIONS_OPTIONAL_SAPI_AND_FAKE_TCTI);

    return *opts != NULL;
}

(2)tpm2_tool_onrun

tpm2_tool_onrun 関数のコードは次のとおりです。

static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {

    UNUSED(flags);

    /*
     * 1. Process options
     */
    tool_rc rc = check_options(ectx, flags);
    if (rc != tool_rc_success) {
        return rc;
    }

    /*
     * 2. Process inputs
     */
    rc = process_inputs(ectx);
    if (rc != tool_rc_success) {
        return rc;
    }

    /*
     * 3. TPM2_CC_<command> call
     */
    rc = nv_undefine(ectx);
    if (rc != tool_rc_success) {
        return rc;
    }

    /*
     * 4. Process outputs
     */
    return process_output(ectx);
}

(3)tpm2_tool_onstop

tpm2_tool_onstop 関数のコードは次のとおりです。

static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {

    UNUSED(ectx);
    /*
     * 1. Free objects
     */

    /*
     * 2. Close authorization sessions
     */
    tool_rc rc = tool_rc_success;
    tool_rc tmp_rc = tpm2_session_close(&ctx.policy_session.session);
    if (tmp_rc != tool_rc_success) {
        rc = tmp_rc;
    }

    tmp_rc = tpm2_session_close(&ctx.auth_hierarchy.object.session);
    if (tmp_rc != tool_rc_success) {
        rc = tmp_rc;
    }

    /*
     * 3. Close auxiliary sessions
     */
    size_t i = 0;
    for(i = 0; i < ctx.aux_session_cnt; i++) {
        if (ctx.aux_session_path[i]) {
            tmp_rc = tpm2_session_close(&ctx.aux_session[i]);
            if (tmp_rc != tool_rc_success) {
                rc = tmp_rc;
            }
        }
    }

    return rc;
}

後続の記事では、これらの機能の詳細な分析を提供します。

おすすめ

転載: blog.csdn.net/phmatthaus/article/details/130709932