tpm2_geteccparameters.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include "files.h"
  3. #include "log.h"
  4. #include "tpm2.h"
  5. #include "tpm2_tool.h"
  6. #include "tpm2_alg_util.h"
  7. #include "tpm2_options.h"
  8. typedef struct tpm_geteccparameters_ctx tpm_geteccparameters_ctx;
  9. struct tpm_geteccparameters_ctx {
  10. TPMI_ECC_CURVE curve_id;
  11. const char *ecc_parameters_path;
  12. };
  13. static tpm_geteccparameters_ctx ctx = {
  14. .curve_id = TPM2_ECC_NONE,
  15. };
  16. static bool on_option(char key, char *value) {
  17. switch (key) {
  18. case 'o':
  19. ctx.ecc_parameters_path = value;
  20. break;
  21. };
  22. return true;
  23. }
  24. static bool on_args(int argc, char **argv) {
  25. if (argc > 1) {
  26. LOG_ERR("Specify a single argument for curveID");
  27. return false;
  28. }
  29. bool result = true;
  30. TPM2B_PUBLIC algorithm = { 0 };
  31. if (!tpm2_alg_util_handle_ext_alg(argv[0], &algorithm)) {
  32. result = false;
  33. }
  34. if (algorithm.publicArea.type != TPM2_ALG_ECC) {
  35. result = false;
  36. }
  37. if (algorithm.publicArea.parameters.eccDetail.curveID > TPM2_ECC_NIST_P521) {
  38. result = false;
  39. }
  40. if (!result) {
  41. LOG_ERR("Invalid/unsupported ECC curve: %s", argv[0]);
  42. return false;
  43. }
  44. ctx.curve_id = algorithm.publicArea.parameters.eccDetail.curveID;
  45. return true;
  46. }
  47. static bool tpm2_tool_onstart(tpm2_options **opts) {
  48. static struct option topts[] = {
  49. { "output", required_argument, NULL, 'o' },
  50. };
  51. *opts = tpm2_options_new("o:", ARRAY_LEN(topts), topts,
  52. on_option, on_args, 0);
  53. return *opts != NULL;
  54. }
  55. static tool_rc check_options(void) {
  56. if (!ctx.ecc_parameters_path) {
  57. LOG_ERR("Invalid path specified for saving the ECC parameters.");
  58. return tool_rc_option_error;
  59. }
  60. if (ctx.curve_id == TPM2_ECC_NONE) {
  61. LOG_ERR("Invalid/ unspecified ECC curve");
  62. return tool_rc_option_error;
  63. }
  64. return tool_rc_success;
  65. }
  66. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  67. UNUSED(flags);
  68. UNUSED(ectx);
  69. // Check input options and arguments
  70. tool_rc rc = check_options();
  71. if (rc != tool_rc_success) {
  72. return rc;
  73. }
  74. // ESAPI call
  75. TPMS_ALGORITHM_DETAIL_ECC *parameters;
  76. rc = tpm2_geteccparameters(ectx, ctx.curve_id, &parameters);
  77. if (rc != tool_rc_success) {
  78. return rc;
  79. }
  80. // Process outputs
  81. bool result = files_save_ecc_details(parameters, ctx.ecc_parameters_path);
  82. if (!result) {
  83. LOG_ERR("Failed to write out the ECC pub key");
  84. return tool_rc_general_error;
  85. }
  86. return tool_rc_success;
  87. }
  88. // Register this tool with tpm2_tool.c
  89. TPM2_TOOL_REGISTER("geteccparameters", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)