tpm2_rsaencrypt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include "files.h"
  5. #include "log.h"
  6. #include "object.h"
  7. #include "tpm2.h"
  8. #include "tpm2_tool.h"
  9. #include "tpm2_alg_util.h"
  10. #include "tpm2_options.h"
  11. typedef struct tpm_rsaencrypt_ctx tpm_rsaencrypt_ctx;
  12. struct tpm_rsaencrypt_ctx {
  13. const char *context_arg;
  14. tpm2_loaded_object key_context;
  15. TPM2B_PUBLIC_KEY_RSA message;
  16. char *output_path;
  17. char *input_path;
  18. TPMT_RSA_DECRYPT scheme;
  19. TPM2B_DATA label;
  20. };
  21. static tpm_rsaencrypt_ctx ctx = {
  22. .context_arg = NULL,
  23. .scheme = { .scheme = TPM2_ALG_RSAES }
  24. };
  25. static tool_rc rsa_encrypt_and_save(ESYS_CONTEXT *context) {
  26. bool ret = false;
  27. TPM2B_PUBLIC_KEY_RSA *out_data = NULL;
  28. tool_rc rc = tpm2_rsa_encrypt(context, &ctx.key_context,
  29. &ctx.message, &ctx.scheme, &ctx.label, &out_data);
  30. if (rc != tool_rc_success) {
  31. return rc;
  32. }
  33. FILE *f = ctx.output_path ? fopen(ctx.output_path, "wb+") : stdout;
  34. if (!f) {
  35. goto out;
  36. }
  37. ret = files_write_bytes(f, out_data->buffer, out_data->size);
  38. if (f != stdout) {
  39. fclose(f);
  40. }
  41. out:
  42. free(out_data);
  43. return ret ? tool_rc_success : tool_rc_general_error;
  44. }
  45. static bool on_option(char key, char *value) {
  46. switch (key) {
  47. case 'c':
  48. ctx.context_arg = value;
  49. break;
  50. case 'o':
  51. ctx.output_path = value;
  52. break;
  53. case 's':
  54. ctx.scheme.scheme = tpm2_alg_util_from_optarg(value,
  55. tpm2_alg_util_flags_rsa_scheme);
  56. if (ctx.scheme.scheme == TPM2_ALG_ERROR) {
  57. LOG_ERR("Invalid scheme.");
  58. return false;
  59. }
  60. ctx.scheme.details.oaep.hashAlg = ctx.scheme.scheme == TPM2_ALG_OAEP ?
  61. TPM2_ALG_SHA256 : 0;
  62. break;
  63. case 'l':
  64. return tpm2_util_get_label(value, &ctx.label);
  65. }
  66. return true;
  67. }
  68. static bool on_args(int argc, char **argv) {
  69. if (argc > 1) {
  70. LOG_ERR("Only supports one input file, got: %d", argc);
  71. return false;
  72. }
  73. ctx.input_path = argv[0];
  74. return true;
  75. }
  76. static bool tpm2_tool_onstart(tpm2_options **opts) {
  77. static const struct option topts[] = {
  78. {"output", required_argument, NULL, 'o'},
  79. {"key-context", required_argument, NULL, 'c'},
  80. {"scheme", required_argument, NULL, 's'},
  81. {"label", required_argument, NULL, 'l'},
  82. };
  83. *opts = tpm2_options_new("o:c:s:l:", ARRAY_LEN(topts), topts, on_option,
  84. on_args, 0);
  85. return *opts != NULL;
  86. }
  87. static tool_rc init(ESYS_CONTEXT *context) {
  88. if (!ctx.context_arg) {
  89. LOG_ERR("Expected option c");
  90. return tool_rc_option_error;
  91. }
  92. ctx.message.size = BUFFER_SIZE(TPM2B_PUBLIC_KEY_RSA, buffer);
  93. bool result = files_load_bytes_from_buffer_or_file_or_stdin(NULL,
  94. ctx.input_path, &ctx.message.size, ctx.message.buffer);
  95. if (!result) {
  96. return tool_rc_general_error;
  97. }
  98. return tpm2_util_object_load(context, ctx.context_arg, &ctx.key_context,
  99. TPM2_HANDLE_ALL_W_NV);
  100. }
  101. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *context, tpm2_option_flags flags) {
  102. UNUSED(flags);
  103. tool_rc rc = init(context);
  104. if (rc != tool_rc_success) {
  105. return rc;
  106. }
  107. return rsa_encrypt_and_save(context);
  108. }
  109. // Register this tool with tpm2_tool.c
  110. TPM2_TOOL_REGISTER("rsaencrypt", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)