1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
/* SPDX-License-Identifier: BSD-3-Clause */
#include <stdlib.h>
#include "files.h"
#include "log.h"
#include "tpm2_cc_util.h"
#include "tpm2_policy.h"
#include "tpm2_tool.h"
#define MAX_SESSIONS 3
typedef struct tpm2_policycommandcode_ctx tpm2_policycommandcode_ctx;
struct tpm2_policycommandcode_ctx {
/*
* Inputs
*/
const char *session_path;
TPM2_CC command_code;
tpm2_session *session;
/*
* Outputs
*/
const char *out_policy_dgst_path;
/*
* Parameter hashes
*/
const char *cp_hash_path;
TPM2B_DIGEST cp_hash;
bool is_command_dispatch;
TPMI_ALG_HASH parameter_hash_algorithm;
};
static tpm2_policycommandcode_ctx ctx = {
.parameter_hash_algorithm = TPM2_ALG_ERROR,
};
static tool_rc tpm2_policycommandcode_build(ESYS_CONTEXT *ectx) {
tool_rc rc = tpm2_policy_build_policycommandcode(ectx, ctx.session,
ctx.command_code, &ctx.cp_hash, ctx.parameter_hash_algorithm);
if (rc != tool_rc_success) {
LOG_ERR("Could not build TPM policy_command_code");
}
return rc;
}
static tool_rc process_outputs(ESYS_CONTEXT *ectx) {
UNUSED(ectx);
/*
* 1. Outputs that do not require TPM2_CC_<command> dispatch
*/
bool is_file_op_success = true;
if (ctx.cp_hash_path) {
is_file_op_success = files_save_digest(&ctx.cp_hash, ctx.cp_hash_path);
if (!is_file_op_success) {
return tool_rc_general_error;
}
}
tool_rc rc = tool_rc_success;
if (!ctx.is_command_dispatch) {
return rc;
}
/*
* 2. Outputs generated after TPM2_CC_<command> dispatch
*/
return tpm2_policy_tool_finish(ectx, ctx.session, ctx.out_policy_dgst_path);
}
static tool_rc process_inputs(ESYS_CONTEXT *ectx) {
UNUSED(ectx);
/*
* 1. Object and auth initializations
*/
/*
* 1.a Add the new-auth values to be set for the object.
*/
/*
* 1.b Add object names and their auth sessions
*/
tool_rc rc = tpm2_session_restore(ectx, ctx.session_path, false,
&ctx.session);
if (rc != tool_rc_success) {
return rc;
}
/*
* 2. Restore auxiliary sessions
*/
/*
* 3. Command specific initializations
*/
/*
* 4. Configuration for calculating the pHash
*/
/*
* 4.a Determine pHash length and alg
*/
tpm2_session *all_sessions[MAX_SESSIONS] = {
ctx.session,
0,
0
};
const char **cphash_path = ctx.cp_hash_path ? &ctx.cp_hash_path : 0;
ctx.parameter_hash_algorithm = tpm2_util_calculate_phash_algorithm(ectx,
cphash_path, &ctx.cp_hash, 0, 0, all_sessions);
/*
* 4.b Determine if TPM2_CC_<command> is to be dispatched
*/
ctx.is_command_dispatch = ctx.cp_hash_path ? false : true;
return tool_rc_success;
}
static tool_rc check_options(void) {
if (!ctx.session_path) {
LOG_ERR("Must specify -S session file.");
return tool_rc_option_error;
}
return tool_rc_success;
}
static bool on_arg(int argc, char **argv) {
if (argc > 1) {
LOG_ERR("Specify only the TPM2 command code.");
return false;
}
if (!argc) {
LOG_ERR("TPM2 command code must be specified.");
return false;
}
bool result = tpm2_cc_util_from_str(argv[0], &ctx.command_code);
if (!result) {
return false;
}
return true;
}
static bool on_option(char key, char *value) {
switch (key) {
case 'S':
ctx.session_path = value;
break;
case 'L':
ctx.out_policy_dgst_path = value;
break;
case 0:
ctx.cp_hash_path = value;
break;
}
return true;
}
static bool tpm2_tool_onstart(tpm2_options **opts) {
static struct option topts[] = {
{ "session", required_argument, NULL, 'S' },
{ "policy", required_argument, NULL, 'L' },
{ "cphash", required_argument, NULL, 0 },
};
*opts = tpm2_options_new("S:L:", ARRAY_LEN(topts), topts, on_option, on_arg,
0);
return *opts != NULL;
}
static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
UNUSED(flags);
/*
* 1. Process options
*/
tool_rc rc = check_options();
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 = tpm2_policycommandcode_build(ectx);
if (rc != tool_rc_success) {
return rc;
}
/*
* 4. Process outputs
*/
return process_outputs(ectx);
}
static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
UNUSED(ectx);
/*
* 1. Free objects
*/
/*
* 2. Close authorization sessions
*/
return tpm2_session_close(&ctx.session);
/*
* 3. Close auxiliary sessions
*/
}
// Register this tool with tpm2_tool.c
TPM2_TOOL_REGISTER("policycommandcode", tpm2_tool_onstart, tpm2_tool_onrun,
tpm2_tool_onstop, NULL)
|