tpm2_certify.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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_tool.h"
  7. #include "tpm2_alg_util.h"
  8. #include "tpm2_convert.h"
  9. #include "tpm2_options.h"
  10. typedef struct tpm_certify_ctx tpm_certify_ctx;
  11. #define MAX_AUX_SESSIONS 1 // two sessions provided by auth interface
  12. #define MAX_SESSIONS 3
  13. struct tpm_certify_ctx {
  14. /*
  15. * Inputs
  16. */
  17. struct {
  18. const char *ctx_path;
  19. const char *auth_str;
  20. tpm2_loaded_object object;
  21. } certified_key;
  22. struct {
  23. const char *ctx_path;
  24. const char *auth_str;
  25. tpm2_loaded_object object;
  26. } signing_key;
  27. TPMI_ALG_HASH halg;
  28. tpm2_convert_sig_fmt sig_fmt;
  29. TPMT_SIG_SCHEME scheme;
  30. /*
  31. * Outputs
  32. */
  33. struct {
  34. char *attest;
  35. char *sig;
  36. } file_path;
  37. TPM2B_ATTEST *certify_info;
  38. TPMT_SIGNATURE *signature;
  39. /*
  40. * Parameter hashes
  41. */
  42. const char *cp_hash_path;
  43. TPM2B_DIGEST cp_hash;
  44. const char *rp_hash_path;
  45. TPM2B_DIGEST rp_hash;
  46. bool is_command_dispatch;
  47. TPMI_ALG_HASH parameter_hash_algorithm;
  48. /*
  49. * Aux sessions
  50. */
  51. uint8_t aux_session_cnt;
  52. tpm2_session *aux_session[MAX_AUX_SESSIONS];
  53. const char *aux_session_path[MAX_AUX_SESSIONS];
  54. ESYS_TR aux_session_handle[MAX_AUX_SESSIONS];
  55. };
  56. static tpm_certify_ctx ctx = {
  57. .sig_fmt = signature_format_tss,
  58. .aux_session_handle[0] = ESYS_TR_NONE,
  59. .parameter_hash_algorithm = TPM2_ALG_ERROR,
  60. };
  61. static tool_rc certify(ESYS_CONTEXT *ectx) {
  62. TPM2B_DATA qualifying_data = {
  63. .size = 4,
  64. .buffer = { 0x00, 0xff, 0x55,0xaa },
  65. };
  66. /*
  67. * 1. TPM2_CC_<command> OR Retrieve cpHash
  68. */
  69. return tpm2_certify(ectx, &ctx.certified_key.object,
  70. &ctx.signing_key.object, &qualifying_data, &ctx.scheme,
  71. &ctx.certify_info, &ctx.signature, &ctx.cp_hash, &ctx.rp_hash,
  72. ctx.parameter_hash_algorithm, ctx.aux_session_handle[0]);
  73. }
  74. static tool_rc process_output(ESYS_CONTEXT *ectx) {
  75. UNUSED(ectx);
  76. /*
  77. * 1. Outputs that do not require TPM2_CC_<command> dispatch
  78. */
  79. bool is_file_op_success = true;
  80. if (ctx.cp_hash_path) {
  81. is_file_op_success = files_save_digest(&ctx.cp_hash, ctx.cp_hash_path);
  82. if (!is_file_op_success) {
  83. return tool_rc_general_error;
  84. }
  85. }
  86. if (!ctx.is_command_dispatch) {
  87. return tool_rc_success;
  88. }
  89. /*
  90. * 2. Outputs generated after TPM2_CC_<command> dispatch
  91. */
  92. /* serialization is safe here, since it's just a byte array */
  93. is_file_op_success = files_save_bytes_to_file(ctx.file_path.attest,
  94. ctx.certify_info->attestationData, ctx.certify_info->size);
  95. if (!is_file_op_success) {
  96. goto out;
  97. }
  98. is_file_op_success = tpm2_convert_sig_save(ctx.signature, ctx.sig_fmt,
  99. ctx.file_path.sig);
  100. if (!is_file_op_success) {
  101. goto out;
  102. }
  103. if (ctx.rp_hash_path) {
  104. is_file_op_success = files_save_digest(&ctx.rp_hash, ctx.rp_hash_path);
  105. }
  106. out:
  107. free(ctx.certify_info);
  108. free(ctx.signature);
  109. return is_file_op_success ? tool_rc_success : tool_rc_general_error;
  110. }
  111. static tool_rc get_key_type(ESYS_CONTEXT *ectx, ESYS_TR object_handle,
  112. TPMI_ALG_PUBLIC *type) {
  113. TPM2B_PUBLIC *out_public = NULL;
  114. tool_rc rc = tpm2_readpublic(ectx, object_handle, &out_public, NULL, NULL);
  115. if (rc != tool_rc_success) {
  116. return rc;
  117. }
  118. *type = out_public->publicArea.type;
  119. free(out_public);
  120. return tool_rc_success;
  121. }
  122. static tool_rc set_scheme(ESYS_CONTEXT *ectx, ESYS_TR key_handle,
  123. TPMI_ALG_HASH halg, TPMT_SIG_SCHEME *scheme) {
  124. TPM2_ALG_ID type;
  125. tool_rc rc = get_key_type(ectx, key_handle, &type);
  126. if (rc != tool_rc_success) {
  127. return rc;
  128. }
  129. switch (type) {
  130. case TPM2_ALG_RSA:
  131. scheme->scheme = TPM2_ALG_RSASSA;
  132. scheme->details.rsassa.hashAlg = halg;
  133. break;
  134. case TPM2_ALG_KEYEDHASH:
  135. scheme->scheme = TPM2_ALG_HMAC;
  136. scheme->details.hmac.hashAlg = halg;
  137. break;
  138. case TPM2_ALG_ECC:
  139. scheme->scheme = TPM2_ALG_ECDSA;
  140. scheme->details.ecdsa.hashAlg = halg;
  141. break;
  142. case TPM2_ALG_SYMCIPHER:
  143. default:
  144. LOG_ERR("Unknown key type, got: 0x%x", type);
  145. return tool_rc_general_error;
  146. }
  147. return tool_rc_success;
  148. }
  149. static tool_rc process_inputs(ESYS_CONTEXT *ectx) {
  150. /*
  151. * 1. Object and auth initializations
  152. */
  153. /*
  154. * 1.a Add the new-auth values to be set for the object.
  155. */
  156. /*
  157. * 1.b Add object names and their auth sessions
  158. */
  159. /* Object #1 */
  160. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.certified_key.ctx_path,
  161. ctx.certified_key.auth_str, &ctx.certified_key.object, false,
  162. TPM2_HANDLE_ALL_W_NV);
  163. if (rc != tool_rc_success) {
  164. return rc;
  165. }
  166. /* Object #2 */
  167. rc = tpm2_util_object_load_auth(ectx, ctx.signing_key.ctx_path,
  168. ctx.signing_key.auth_str, &ctx.signing_key.object, false,
  169. TPM2_HANDLE_ALL_W_NV);
  170. if (rc != tool_rc_success) {
  171. return rc;
  172. }
  173. /*
  174. * 2. Restore auxiliary sessions
  175. */
  176. rc = tpm2_util_aux_sessions_setup(ectx, ctx.aux_session_cnt,
  177. ctx.aux_session_path, ctx.aux_session_handle, ctx.aux_session);
  178. if (rc != tool_rc_success) {
  179. return rc;
  180. }
  181. /*
  182. * 3. Command specific initializations
  183. */
  184. rc = set_scheme(ectx, ctx.signing_key.object.tr_handle, ctx.halg,
  185. &ctx.scheme);
  186. if (rc != tool_rc_success) {
  187. LOG_ERR("No suitable signing scheme!");
  188. return rc;
  189. }
  190. /*
  191. * 4. Configuration for calculating the pHash
  192. */
  193. /*
  194. * 4.a Determine pHash length and alg
  195. */
  196. tpm2_session *all_sessions[MAX_SESSIONS] = {
  197. ctx.certified_key.object.session,
  198. ctx.signing_key.object.session,
  199. ctx.aux_session[0]
  200. };
  201. const char **cphash_path = ctx.cp_hash_path ? &ctx.cp_hash_path : 0;
  202. const char **rphash_path = ctx.rp_hash_path ? &ctx.rp_hash_path : 0;
  203. ctx.parameter_hash_algorithm = tpm2_util_calculate_phash_algorithm(ectx,
  204. cphash_path, &ctx.cp_hash, rphash_path, &ctx.rp_hash, all_sessions);
  205. /*
  206. * 4.b Determine if TPM2_CC_<command> is to be dispatched
  207. * !rphash && !cphash [Y]
  208. * !rphash && cphash [N]
  209. * rphash && !cphash [Y]
  210. * rphash && cphash [Y]
  211. */
  212. ctx.is_command_dispatch = (ctx.cp_hash_path && !ctx.rp_hash_path) ?
  213. false : true;
  214. return rc;
  215. }
  216. static tool_rc check_options(void) {
  217. if ((!ctx.certified_key.ctx_path) && (!ctx.signing_key.ctx_path)) {
  218. LOG_ERR("Must specify the object to be certified and the signing key.");
  219. return tool_rc_option_error;
  220. }
  221. if (ctx.cp_hash_path && !ctx.rp_hash_path &&
  222. (ctx.file_path.attest || ctx.file_path.sig)) {
  223. LOG_ERR("Cannot specify output options when calculating cpHash");
  224. return tool_rc_option_error;
  225. }
  226. return tool_rc_success;
  227. }
  228. static bool on_option(char key, char *value) {
  229. switch (key) {
  230. case 'c':
  231. ctx.certified_key.ctx_path = value;
  232. break;
  233. case 'C':
  234. ctx.signing_key.ctx_path = value;
  235. break;
  236. case 'P':
  237. ctx.certified_key.auth_str = value;
  238. break;
  239. case 'p':
  240. ctx.signing_key.auth_str = value;
  241. break;
  242. case 'g':
  243. ctx.halg = tpm2_alg_util_from_optarg(value, tpm2_alg_util_flags_hash);
  244. if (ctx.halg == TPM2_ALG_ERROR) {
  245. LOG_ERR("Could not format algorithm to number, got: \"%s\"", value);
  246. return false;
  247. }
  248. break;
  249. case 'o':
  250. ctx.file_path.attest = value;
  251. break;
  252. case 's':
  253. ctx.file_path.sig = value;
  254. break;
  255. case 0:
  256. ctx.cp_hash_path = value;
  257. break;
  258. case 1:
  259. ctx.rp_hash_path = value;
  260. break;
  261. case 'f':
  262. ctx.sig_fmt = tpm2_convert_sig_fmt_from_optarg(value);
  263. if (ctx.sig_fmt == signature_format_err) {
  264. return false;
  265. }
  266. break;
  267. case 'S':
  268. ctx.aux_session_path[ctx.aux_session_cnt] = value;
  269. if (ctx.aux_session_cnt < MAX_AUX_SESSIONS) {
  270. ctx.aux_session_cnt++;
  271. } else {
  272. LOG_ERR("Specify a max of 3 sessions");
  273. return false;
  274. }
  275. break;
  276. }
  277. return true;
  278. }
  279. static bool tpm2_tool_onstart(tpm2_options **opts) {
  280. const struct option topts[] = {
  281. { "certifiedkey-context", required_argument, NULL, 'c' },
  282. { "signingkey-context", required_argument, NULL, 'C' },
  283. { "certifiedkey-auth", required_argument, NULL, 'p' },
  284. { "signingkey-auth", required_argument, NULL, 'P' },
  285. { "hash-algorithm", required_argument, NULL, 'g' },
  286. { "attestation", required_argument, NULL, 'o' },
  287. { "signature", required_argument, NULL, 's' },
  288. { "format", required_argument, NULL, 'f' },
  289. { "cphash", required_argument, NULL, 0 },
  290. { "rphash", required_argument, NULL, 1 },
  291. { "session", required_argument, NULL, 'S' },
  292. };
  293. *opts = tpm2_options_new("P:p:g:o:s:c:C:f:S:", ARRAY_LEN(topts), topts,
  294. on_option, NULL, 0);
  295. return *opts != NULL;
  296. }
  297. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  298. UNUSED(flags);
  299. /*
  300. * 1. Process options
  301. */
  302. tool_rc rc = check_options();
  303. if (rc != tool_rc_success) {
  304. return rc;
  305. }
  306. /*
  307. * 2. Process inputs
  308. */
  309. rc = process_inputs(ectx);
  310. if (rc != tool_rc_success) {
  311. return rc;
  312. }
  313. /*
  314. * 3. TPM2_CC_<command> call
  315. */
  316. rc = certify(ectx);
  317. if (rc != tool_rc_success) {
  318. return rc;
  319. }
  320. /*
  321. * 4. Process outputs
  322. */
  323. return process_output(ectx);
  324. }
  325. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  326. UNUSED(ectx);
  327. /*
  328. * 1. Free objects
  329. */
  330. /*
  331. * 2. Close authorization sessions
  332. */
  333. tool_rc rc = tool_rc_success;
  334. tool_rc tmp_rc = tpm2_session_close(&ctx.signing_key.object.session);
  335. if (tmp_rc != tool_rc_success) {
  336. rc = tmp_rc;
  337. }
  338. tmp_rc = tpm2_session_close(&ctx.certified_key.object.session);
  339. if (tmp_rc != tool_rc_success) {
  340. rc = tmp_rc;
  341. }
  342. /*
  343. * 3. Close auxiliary sessions
  344. */
  345. size_t i = 0;
  346. for(i = 0; i < ctx.aux_session_cnt; i++) {
  347. if (ctx.aux_session_path[i]) {
  348. tmp_rc = tpm2_session_close(&ctx.aux_session[i]);
  349. if (tmp_rc != tool_rc_success) {
  350. rc = tmp_rc;
  351. }
  352. }
  353. }
  354. return rc;
  355. }
  356. // Register this tool with tpm2_tool.c
  357. TPM2_TOOL_REGISTER("certify", tpm2_tool_onstart, tpm2_tool_onrun,
  358. tpm2_tool_onstop, NULL)