tpm2_createprimary.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <string.h>
  3. #include "files.h"
  4. #include "log.h"
  5. #include "pcr.h"
  6. #include "tpm2_tool.h"
  7. #include "tpm2_alg_util.h"
  8. #include "tpm2_auth_util.h"
  9. #include "tpm2_hierarchy.h"
  10. #include "tpm2_options.h"
  11. #define DEFAULT_ATTRS \
  12. TPMA_OBJECT_RESTRICTED|TPMA_OBJECT_DECRYPT \
  13. |TPMA_OBJECT_FIXEDTPM|TPMA_OBJECT_FIXEDPARENT \
  14. |TPMA_OBJECT_SENSITIVEDATAORIGIN|TPMA_OBJECT_USERWITHAUTH
  15. #define DEFAULT_PRIMARY_KEY_ALG "rsa2048:null:aes128cfb"
  16. typedef struct tpm_createprimary_ctx tpm_createprimary_ctx;
  17. struct tpm_createprimary_ctx {
  18. struct {
  19. char *auth_str;
  20. tpm2_session *session;
  21. } parent;
  22. tpm2_hierarchy_pdata objdata;
  23. char *context_file;
  24. char *unique_file;
  25. char *key_auth_str;
  26. char *creation_data_file;
  27. char *creation_ticket_file;
  28. char *creation_hash_file;
  29. char *outside_info_data;
  30. char *template_data_path;
  31. char *alg;
  32. char *halg;
  33. char *attrs;
  34. char *policy;
  35. char *cp_hash_path;
  36. };
  37. static tpm_createprimary_ctx ctx = {
  38. .alg = DEFAULT_PRIMARY_KEY_ALG,
  39. .objdata = {
  40. .in = {
  41. .sensitive = TPM2B_SENSITIVE_CREATE_EMPTY_INIT,
  42. .hierarchy = TPM2_RH_OWNER
  43. },
  44. },
  45. };
  46. static bool on_option(char key, char *value) {
  47. bool res;
  48. switch (key) {
  49. case 'C': {
  50. res = tpm2_util_handle_from_optarg(value, &ctx.objdata.in.hierarchy,
  51. TPM2_HANDLE_FLAGS_ALL_HIERACHIES);
  52. if (!res) {
  53. return false;
  54. }
  55. break;
  56. }
  57. case 'P':
  58. ctx.parent.auth_str = value;
  59. break;
  60. case 'p':
  61. ctx.key_auth_str = value;
  62. break;
  63. case 'g':
  64. ctx.halg = value;
  65. break;
  66. case 'G':
  67. ctx.alg = value;
  68. break;
  69. case 'c':
  70. ctx.context_file = value;
  71. break;
  72. case 'u':
  73. ctx.unique_file = value;
  74. break;
  75. case 'L':
  76. ctx.policy = value;
  77. break;
  78. case 'a':
  79. ctx.attrs = value;
  80. break;
  81. case 0:
  82. ctx.creation_data_file = value;
  83. break;
  84. case 1:
  85. ctx.template_data_path = value;
  86. break;
  87. case 't':
  88. ctx.creation_ticket_file = value;
  89. break;
  90. case 'd':
  91. ctx.creation_hash_file = value;
  92. break;
  93. case 'q':
  94. ctx.outside_info_data = value;
  95. break;
  96. case 'l':
  97. if (!pcr_parse_selections(value, &ctx.objdata.in.creation_pcr)) {
  98. LOG_ERR("Could not parse pcr selections, got: \"%s\"", value);
  99. return false;
  100. }
  101. break;
  102. case 2:
  103. ctx.cp_hash_path = value;
  104. break;
  105. /* no default */
  106. }
  107. return true;
  108. }
  109. static bool tpm2_tool_onstart(tpm2_options **opts) {
  110. const struct option topts[] = {
  111. { "hierarchy", required_argument, NULL, 'C' },
  112. { "hierarchy-auth", required_argument, NULL, 'P' },
  113. { "key-auth", required_argument, NULL, 'p' },
  114. { "hash-algorithm", required_argument, NULL, 'g' },
  115. { "key-algorithm", required_argument, NULL, 'G' },
  116. { "key-context", required_argument, NULL, 'c' },
  117. { "policy", required_argument, NULL, 'L' },
  118. { "attributes", required_argument, NULL, 'a' },
  119. { "unique-data", required_argument, NULL, 'u' },
  120. { "creation-data", required_argument, NULL, 0 },
  121. { "template-data", required_argument, NULL, 1 },
  122. { "creation-ticket",required_argument, NULL, 't' },
  123. { "creation-hash", required_argument, NULL, 'd' },
  124. { "outside-info", required_argument, NULL, 'q' },
  125. { "pcr-list", required_argument, NULL, 'l' },
  126. { "cphash", required_argument, NULL, 2 },
  127. };
  128. *opts = tpm2_options_new("C:P:p:g:G:c:L:a:u:t:d:q:l:", ARRAY_LEN(topts), topts,
  129. on_option, NULL, 0);
  130. return *opts != NULL;
  131. }
  132. static tool_rc validate_input_options(void) {
  133. if (ctx.cp_hash_path && (ctx.creation_data_file || ctx.creation_hash_file ||
  134. ctx.creation_ticket_file || ctx.context_file)) {
  135. LOG_ERR("Cannot generate outputs when calculating cpHash");
  136. return tool_rc_option_error;
  137. }
  138. return tool_rc_success;
  139. }
  140. static tool_rc process_inputs(ESYS_CONTEXT *ectx) {
  141. /* Parent/ Hierarchy Auth */
  142. tool_rc rc = tpm2_auth_util_from_optarg(ectx, ctx.parent.auth_str,
  143. &ctx.parent.session, false);
  144. if (rc != tool_rc_success) {
  145. LOG_ERR("Invalid parent key authorization");
  146. return rc;
  147. }
  148. /* Primary key auth */
  149. tpm2_session *tmp;
  150. rc = tpm2_auth_util_from_optarg(NULL, ctx.key_auth_str, &tmp, true);
  151. if (rc != tool_rc_success) {
  152. LOG_ERR("Invalid new key authorization");
  153. return rc;
  154. }
  155. const TPM2B_AUTH *auth = tpm2_session_get_auth_value(tmp);
  156. ctx.objdata.in.sensitive.sensitive.userAuth = *auth;
  157. tpm2_session_close(&tmp);
  158. /*
  159. * Initialize the public properties of the key
  160. */
  161. rc = tpm2_alg_util_public_init(ctx.alg, ctx.halg, ctx.attrs,
  162. ctx.policy, DEFAULT_ATTRS, &ctx.objdata.in.public);
  163. if (rc != tool_rc_success) {
  164. return rc;
  165. }
  166. /* Optional unique data */
  167. if (ctx.unique_file) {
  168. if (!strcmp(ctx.unique_file, "-")) {
  169. ctx.unique_file = 0;
  170. }
  171. rc = files_load_unique_data(ctx.unique_file, &ctx.objdata.in.public);
  172. if (rc != tool_rc_success) {
  173. return rc;
  174. }
  175. }
  176. /* Outside data is optional. If not specified default to 0 */
  177. if (ctx.outside_info_data) {
  178. ctx.objdata.in.outside_info.size = sizeof(ctx.objdata.in.outside_info.buffer);
  179. bool result = tpm2_util_bin_from_hex_or_file(ctx.outside_info_data,
  180. &ctx.objdata.in.outside_info.size,
  181. ctx.objdata.in.outside_info.buffer);
  182. if (!result) {
  183. return tool_rc_general_error;
  184. }
  185. }
  186. /*
  187. * Template saved regardless of execution of the TPM2_CC_CreatePrimary.
  188. *
  189. * TODO: Does this need to be part of the no_execute_only_process_params
  190. * along side processing cphash.
  191. *
  192. */
  193. if (ctx.template_data_path) {
  194. bool result = files_save_template(&ctx.objdata.in.public.publicArea,
  195. ctx.template_data_path);
  196. if (!result) {
  197. LOG_ERR("Could not save public template to file.");
  198. return tool_rc_general_error;
  199. }
  200. }
  201. return tool_rc_success;
  202. }
  203. static tool_rc no_execute_only_process_params(ESYS_CONTEXT *ectx) {
  204. TPM2B_DIGEST cp_hash = { .size = 0 };
  205. tool_rc rc = tpm2_hierarchy_create_primary(ectx, ctx.parent.session,
  206. &ctx.objdata, &cp_hash);
  207. if (rc == tool_rc_success) {
  208. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  209. if (!result) {
  210. LOG_ERR("Failed to save cp hash");
  211. rc = tool_rc_general_error;
  212. }
  213. }
  214. return rc;
  215. }
  216. static tool_rc process_outputs(ESYS_CONTEXT *ectx) {
  217. tpm2_util_public_to_yaml(ctx.objdata.out.public, NULL);
  218. tool_rc rc = ctx.context_file ? files_save_tpm_context_to_path(ectx,
  219. ctx.objdata.out.handle, ctx.context_file) : tool_rc_success;
  220. if (rc != tool_rc_success) {
  221. LOG_ERR("Failed saving object context.");
  222. return rc;
  223. }
  224. bool result = true;
  225. if (ctx.creation_data_file) {
  226. result = files_save_creation_data(ctx.objdata.out.creation.data,
  227. ctx.creation_data_file);
  228. }
  229. if (!result) {
  230. LOG_ERR("Failed saving creation data.");
  231. return tool_rc_general_error;
  232. }
  233. if (ctx.creation_ticket_file) {
  234. result = files_save_creation_ticket(ctx.objdata.out.creation.ticket,
  235. ctx.creation_ticket_file);
  236. }
  237. if (!result) {
  238. LOG_ERR("Failed saving creation ticket.");
  239. return tool_rc_general_error;
  240. }
  241. if (ctx.creation_hash_file) {
  242. result = files_save_digest(ctx.objdata.out.hash,
  243. ctx.creation_hash_file);
  244. }
  245. if (!result) {
  246. LOG_ERR("Failed saving creation hash.");
  247. return tool_rc_general_error;
  248. }
  249. return tool_rc_success;
  250. }
  251. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  252. UNUSED(flags);
  253. /* Validate input options */
  254. tool_rc rc = validate_input_options();
  255. if (rc != tool_rc_success) {
  256. return tool_rc_option_error;
  257. }
  258. /* Process inputs */
  259. rc = process_inputs(ectx);
  260. if (rc != tool_rc_success) {
  261. return rc;
  262. }
  263. /* Process & return uncoditionally if no execute paths are to be executed */
  264. if (ctx.cp_hash_path) { // One of the conditions for no execute
  265. return no_execute_only_process_params(ectx);
  266. }
  267. /* Dispatch TPM2_CC_CreatePrimary */
  268. rc = tpm2_hierarchy_create_primary(ectx, ctx.parent.session, &ctx.objdata,
  269. NULL);
  270. if (rc != tool_rc_success) {
  271. return rc;
  272. }
  273. /* Process outputs and return */
  274. return process_outputs(ectx);
  275. }
  276. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  277. UNUSED(ectx);
  278. return tpm2_session_close(&ctx.parent.session);
  279. }
  280. static void tpm2_tool_onexit(void) {
  281. tpm2_hierarchy_pdata_free(&ctx.objdata);
  282. }
  283. // Register this tool with tpm2_tool.c
  284. TPM2_TOOL_REGISTER("createprimary", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, tpm2_tool_onexit)