tpm2_nvcertify.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include "files.h"
  4. #include "log.h"
  5. #include "tpm2.h"
  6. #include "tpm2_alg_util.h"
  7. #include "tpm2_convert.h"
  8. #include "tpm2_nv_util.h"
  9. #include "tpm2_tool.h"
  10. typedef struct tpm_nvcertify_ctx tpm_nvcertify_ctx;
  11. struct tpm_nvcertify_ctx {
  12. //Input
  13. struct {
  14. const char *ctx_path;
  15. const char *auth_str;
  16. tpm2_loaded_object object;
  17. } signing_key;
  18. struct {
  19. const char *ctx_path;
  20. const char *auth_str;
  21. tpm2_loaded_object object;
  22. } nvindex_authobj;
  23. TPM2_HANDLE nv_index;
  24. TPMI_ALG_HASH halg;
  25. TPMI_ALG_SIG_SCHEME sig_scheme;
  26. UINT16 size;
  27. UINT16 offset;
  28. const char *policy_qualifier_arg;
  29. //Output
  30. char *certify_info_path;
  31. char *signature_path;
  32. tpm2_convert_sig_fmt sig_format;
  33. char *cp_hash_path;
  34. };
  35. static tpm_nvcertify_ctx ctx = {
  36. .halg = TPM2_ALG_NULL,
  37. .sig_scheme = TPM2_ALG_NULL,
  38. };
  39. static bool set_digest_algorithm(char *value) {
  40. ctx.halg = tpm2_alg_util_from_optarg(value, tpm2_alg_util_flags_hash);
  41. if (ctx.halg == TPM2_ALG_ERROR) {
  42. LOG_ERR("Could not convert to number or lookup algorithm, got: "
  43. "\"%s\"", value);
  44. return false;
  45. }
  46. return true;
  47. }
  48. static bool set_signing_scheme(char *value) {
  49. ctx.sig_scheme = tpm2_alg_util_from_optarg(value, tpm2_alg_util_flags_sig);
  50. if (ctx.sig_scheme == TPM2_ALG_ERROR) {
  51. LOG_ERR("Unknown signing scheme, got: \"%s\"", value);
  52. return false;
  53. }
  54. return true;
  55. }
  56. static bool set_signature_format(char *value) {
  57. ctx.sig_format = tpm2_convert_sig_fmt_from_optarg(value);
  58. if (ctx.sig_format == signature_format_err) {
  59. return false;
  60. }
  61. return true;
  62. }
  63. static bool on_option(char key, char *value) {
  64. bool result = true;
  65. uint32_t input_value;
  66. switch (key) {
  67. case 'C':
  68. ctx.signing_key.ctx_path = value;
  69. break;
  70. case 'P':
  71. ctx.signing_key.auth_str = value;
  72. break;
  73. case 'c':
  74. ctx.nvindex_authobj.ctx_path = value;
  75. break;
  76. case 'p':
  77. ctx.nvindex_authobj.auth_str = value;
  78. break;
  79. case 'g':
  80. result = set_digest_algorithm(value);
  81. goto on_option_out;
  82. case 's':
  83. result = set_signing_scheme(value);
  84. goto on_option_out;
  85. case 'f':
  86. result = set_signature_format(value);
  87. goto on_option_out;
  88. case 'o':
  89. ctx.signature_path = value;
  90. break;
  91. case 'q':
  92. ctx.policy_qualifier_arg = value;
  93. break;
  94. case 0:
  95. result = tpm2_util_string_to_uint32(value, &input_value);
  96. if (!result) {
  97. LOG_ERR("Could not convert size to number, got: \"%s\"", value);
  98. return false;
  99. }
  100. if (input_value > UINT16_MAX) {
  101. LOG_ERR("Specified size is larger than that allowed by command");
  102. return false;
  103. } else {
  104. ctx.size = input_value;
  105. }
  106. break;
  107. case 1:
  108. result = tpm2_util_string_to_uint32(value, &input_value);
  109. if (!result) {
  110. LOG_ERR("Could not convert offset to number, got: \"%s\"", value);
  111. return false;
  112. }
  113. if (input_value > UINT16_MAX) {
  114. LOG_ERR("Specified offset is larger than that allowed by command");
  115. return false;
  116. } else {
  117. ctx.offset = input_value;
  118. }
  119. break;
  120. case 2:
  121. ctx.certify_info_path = value;
  122. break;
  123. case 3:
  124. ctx.cp_hash_path = value;
  125. break;
  126. }
  127. on_option_out:
  128. return result;
  129. }
  130. static bool on_arg(int argc, char **argv) {
  131. /*
  132. * If the user doesn't specify an authorization hierarchy use the index
  133. */
  134. if (!ctx.nvindex_authobj.ctx_path) {
  135. ctx.nvindex_authobj.ctx_path = argv[0];
  136. }
  137. return on_arg_nv_index(argc, argv, &ctx.nv_index);
  138. }
  139. static bool tpm2_tool_onstart(tpm2_options **opts) {
  140. static const struct option topts[] = {
  141. { "signingkey-context", required_argument, NULL, 'C' },
  142. { "signingkey-auth", required_argument, NULL, 'P' },
  143. { "nvauthobj-context", required_argument, NULL, 'c' },
  144. { "nvauthobj-auth", required_argument, NULL, 'p' },
  145. { "hash-algorithm", required_argument, NULL, 'g' },
  146. { "scheme", required_argument, NULL, 's' },
  147. { "format", required_argument, NULL, 'f' },
  148. { "signature", required_argument, NULL, 'o' },
  149. { "qualification", required_argument, NULL, 'q' },
  150. { "size", required_argument, NULL, 0 },
  151. { "offset", required_argument, NULL, 1 },
  152. { "attestation", required_argument, NULL, 2 },
  153. { "cphash", required_argument, NULL, 3 },
  154. };
  155. *opts = tpm2_options_new("C:P:c:p:g:s:f:o:q:", ARRAY_LEN(topts), topts,
  156. on_option, on_arg, 0);
  157. return *opts != NULL;
  158. }
  159. static bool is_input_options_args_valid(ESYS_CONTEXT *ectx) {
  160. if (!ctx.signing_key.ctx_path) {
  161. LOG_ERR("Must specify the signing key '-C'.");
  162. return false;
  163. }
  164. if (!ctx.signature_path) {
  165. LOG_ERR("Must specify the file path to save signature '-o'");
  166. return false;
  167. }
  168. if (!ctx.certify_info_path) {
  169. LOG_ERR("Must specify file path to save attestation '--attestation'");
  170. return false;
  171. }
  172. /*
  173. * Ensure that NV index is large enough for certifying data size at offset.
  174. */
  175. bool result = true;
  176. TPM2B_NV_PUBLIC *nv_public = NULL;
  177. tool_rc rc = tpm2_util_nv_read_public(ectx, ctx.nv_index, &nv_public);
  178. if (rc != tool_rc_success) {
  179. LOG_ERR("Failed to access NVRAM public area at index 0x%X",
  180. ctx.nv_index);
  181. result = false;
  182. goto is_input_options_args_valid_out;
  183. }
  184. if (ctx.offset + ctx.size > nv_public->nvPublic.dataSize) {
  185. LOG_ERR("Size to read at offset is bigger than nv index size");
  186. result = false;
  187. goto is_input_options_args_valid_out;
  188. }
  189. is_input_options_args_valid_out:
  190. free(nv_public);
  191. return result;
  192. }
  193. static tool_rc process_nvcertify_input(ESYS_CONTEXT *ectx,
  194. TPMT_SIG_SCHEME *in_scheme, TPM2B_DATA *policy_qualifier) {
  195. /*
  196. * Load signing key and auth
  197. */
  198. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.signing_key.ctx_path,
  199. ctx.signing_key.auth_str, &ctx.signing_key.object, false,
  200. TPM2_HANDLES_FLAGS_TRANSIENT|TPM2_HANDLES_FLAGS_PERSISTENT);
  201. if (rc != tool_rc_success) {
  202. LOG_ERR("Invalid signing key/ authorization.");
  203. return rc;
  204. }
  205. /*
  206. * Load NV index authorization object and auth
  207. */
  208. rc = tpm2_util_object_load_auth(ectx, ctx.nvindex_authobj.ctx_path,
  209. ctx.nvindex_authobj.auth_str, &ctx.nvindex_authobj.object,
  210. false, TPM2_HANDLE_ALL_W_NV);
  211. if (rc != tool_rc_success) {
  212. LOG_ERR("Invalid object specified for NV index authorization.");
  213. return rc;
  214. }
  215. /*
  216. * Set appropriate signature scheme for key type
  217. */
  218. rc = tpm2_alg_util_get_signature_scheme(ectx,
  219. ctx.signing_key.object.tr_handle, &ctx.halg, ctx.sig_scheme, in_scheme);
  220. if (rc != tool_rc_success) {
  221. LOG_ERR("bad signature scheme for key type!");
  222. return rc;
  223. }
  224. /*
  225. * Qualifier data is optional. If not specified default to 0
  226. */
  227. if (ctx.policy_qualifier_arg) {
  228. policy_qualifier->size = sizeof(policy_qualifier->buffer);
  229. bool result = tpm2_util_bin_from_hex_or_file(ctx.policy_qualifier_arg,
  230. &policy_qualifier->size,
  231. policy_qualifier->buffer);
  232. if (!result) {
  233. return tool_rc_general_error;
  234. }
  235. }
  236. return tool_rc_success;
  237. }
  238. static tool_rc process_nvcertify_output(TPMT_SIGNATURE *signature,
  239. TPM2B_ATTEST *certify_info) {
  240. bool result = tpm2_convert_sig_save(signature, ctx.sig_format,
  241. ctx.signature_path);
  242. if (!result) {
  243. LOG_ERR("Failed saving signature data.");
  244. return tool_rc_general_error;
  245. }
  246. result = files_save_bytes_to_file(ctx.certify_info_path,
  247. certify_info->attestationData, certify_info->size);
  248. if (!result) {
  249. LOG_ERR("Failed saving attestation data.");
  250. return tool_rc_general_error;
  251. }
  252. return tool_rc_success;
  253. }
  254. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  255. /* opts is unused, avoid compiler warning */
  256. UNUSED(flags);
  257. bool result = is_input_options_args_valid(ectx);
  258. if (!result) {
  259. return tool_rc_option_error;
  260. }
  261. //Input
  262. TPMT_SIG_SCHEME in_scheme;
  263. TPM2B_DATA policy_qualifier = TPM2B_EMPTY_INIT;
  264. tool_rc rc = process_nvcertify_input(ectx, &in_scheme, &policy_qualifier);
  265. if (rc != tool_rc_success) {
  266. return rc;
  267. }
  268. //ESAPI call
  269. TPMT_SIGNATURE *signature = NULL;
  270. TPM2B_ATTEST *certify_info = NULL;
  271. if (!ctx.cp_hash_path) {
  272. rc = tpm2_nvcertify(ectx, &ctx.signing_key.object,
  273. &ctx.nvindex_authobj.object, ctx.nv_index, ctx.offset, ctx.size,
  274. &in_scheme, &certify_info, &signature, &policy_qualifier, NULL);
  275. if (rc != tool_rc_success) {
  276. goto tpm2_tool_onrun_out;
  277. }
  278. //Output
  279. rc = process_nvcertify_output(signature, certify_info);
  280. goto tpm2_tool_onrun_out;
  281. }
  282. TPM2B_DIGEST cp_hash = { .size = 0 };
  283. rc = tpm2_nvcertify(ectx, &ctx.signing_key.object,
  284. &ctx.nvindex_authobj.object, ctx.nv_index, ctx.offset, ctx.size,
  285. &in_scheme, &certify_info, &signature, &policy_qualifier, &cp_hash);
  286. if (rc != tool_rc_success) {
  287. return rc;
  288. }
  289. result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  290. if (!result) {
  291. rc = tool_rc_general_error;
  292. }
  293. tpm2_tool_onrun_out:
  294. Esys_Free(signature);
  295. Esys_Free(certify_info);
  296. return rc;
  297. }
  298. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  299. UNUSED(ectx);
  300. tool_rc rc = tool_rc_success;
  301. if (!ctx.cp_hash_path) {
  302. tool_rc tmp_rc = tpm2_session_close(&ctx.signing_key.object.session);
  303. if (tmp_rc != tool_rc_success) {
  304. rc = tmp_rc;
  305. }
  306. tmp_rc = tpm2_session_close(&ctx.nvindex_authobj.object.session);
  307. if (tmp_rc != tool_rc_success) {
  308. rc = tmp_rc;
  309. }
  310. }
  311. return rc;
  312. }
  313. // Register this tool with tpm2_tool.c
  314. TPM2_TOOL_REGISTER("nvcertify", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)