tpm2_rsadecrypt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_options.h"
  9. typedef struct tpm_rsadecrypt_ctx tpm_rsadecrypt_ctx;
  10. struct tpm_rsadecrypt_ctx {
  11. struct {
  12. const char *ctx_path;
  13. const char *auth_str;
  14. tpm2_loaded_object object;
  15. } key;
  16. TPM2B_DATA label;
  17. TPM2B_PUBLIC_KEY_RSA cipher_text;
  18. char *input_path;
  19. char *output_file_path;
  20. TPMT_RSA_DECRYPT scheme;
  21. char *cp_hash_path;
  22. };
  23. static tpm_rsadecrypt_ctx ctx = {
  24. .scheme = { .scheme = TPM2_ALG_RSAES }
  25. };
  26. static tool_rc rsa_decrypt_and_save(ESYS_CONTEXT *ectx) {
  27. TPM2B_PUBLIC_KEY_RSA *message = NULL;
  28. if (ctx.cp_hash_path) {
  29. TPM2B_DIGEST cp_hash = { .size = 0 };
  30. tool_rc rc = tpm2_rsa_decrypt(ectx, &ctx.key.object, &ctx.cipher_text,
  31. &ctx.scheme, &ctx.label, &message, &cp_hash);
  32. if (rc != tool_rc_success) {
  33. return rc;
  34. }
  35. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  36. if (!result) {
  37. rc = tool_rc_general_error;
  38. }
  39. return rc;
  40. }
  41. tool_rc rc = tpm2_rsa_decrypt(ectx, &ctx.key.object, &ctx.cipher_text,
  42. &ctx.scheme, &ctx.label, &message, NULL);
  43. if (rc != tool_rc_success) {
  44. return rc;
  45. }
  46. bool ret = false;
  47. FILE *f =
  48. ctx.output_file_path ? fopen(ctx.output_file_path, "wb+") : stdout;
  49. if (!f) {
  50. goto out;
  51. }
  52. ret = files_write_bytes(f, message->buffer, message->size);
  53. if (f != stdout) {
  54. fclose(f);
  55. }
  56. out:
  57. free(message);
  58. return ret ? tool_rc_success : tool_rc_general_error;
  59. }
  60. static bool on_option(char key, char *value) {
  61. switch (key) {
  62. case 'c':
  63. ctx.key.ctx_path = value;
  64. break;
  65. case 'p':
  66. ctx.key.auth_str = value;
  67. break;
  68. case 'o': {
  69. ctx.output_file_path = value;
  70. break;
  71. }
  72. case 's':
  73. ctx.scheme.scheme = tpm2_alg_util_from_optarg(value,
  74. tpm2_alg_util_flags_rsa_scheme);
  75. if (ctx.scheme.scheme == TPM2_ALG_ERROR) {
  76. LOG_ERR("Invalid scheme.");
  77. return false;
  78. }
  79. ctx.scheme.details.oaep.hashAlg = ctx.scheme.scheme == TPM2_ALG_OAEP ?
  80. TPM2_ALG_SHA256 : 0;
  81. break;
  82. case 0:
  83. ctx.cp_hash_path = value;
  84. break;
  85. case 'l':
  86. return tpm2_util_get_label(value, &ctx.label);
  87. }
  88. return true;
  89. }
  90. static bool on_args(int argc, char **argv) {
  91. if (argc > 1) {
  92. LOG_ERR("Only supports one input file, got: %d", argc);
  93. return false;
  94. }
  95. ctx.input_path = argv[0];
  96. return true;
  97. }
  98. static bool tpm2_tool_onstart(tpm2_options **opts) {
  99. static struct option topts[] = {
  100. { "auth", required_argument, NULL, 'p' },
  101. { "output", required_argument, NULL, 'o' },
  102. { "key-context", required_argument, NULL, 'c' },
  103. { "scheme", required_argument, NULL, 's' },
  104. { "label", required_argument, NULL, 'l' },
  105. { "cphash", required_argument, NULL, 0 },
  106. };
  107. *opts = tpm2_options_new("p:o:c:s:l:", ARRAY_LEN(topts), topts, on_option,
  108. on_args, 0);
  109. return *opts != NULL;
  110. }
  111. static tool_rc init(ESYS_CONTEXT *ectx) {
  112. if (!ctx.key.ctx_path) {
  113. LOG_ERR("Expected argument -c.");
  114. return tool_rc_option_error;
  115. }
  116. if (ctx.output_file_path && ctx.cp_hash_path) {
  117. LOG_ERR("Cannout decrypt when calculating cphash");
  118. return tool_rc_option_error;
  119. }
  120. ctx.cipher_text.size = BUFFER_SIZE(TPM2B_PUBLIC_KEY_RSA, buffer);
  121. bool result = files_load_bytes_from_buffer_or_file_or_stdin(NULL,
  122. ctx.input_path, &ctx.cipher_text.size, ctx.cipher_text.buffer);
  123. if (!result) {
  124. return tool_rc_general_error;
  125. }
  126. return tpm2_util_object_load_auth(ectx, ctx.key.ctx_path, ctx.key.auth_str,
  127. &ctx.key.object, false, TPM2_HANDLE_ALL_W_NV);
  128. }
  129. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  130. UNUSED(flags);
  131. tool_rc rc = init(ectx);
  132. if (rc != tool_rc_success) {
  133. return rc;
  134. }
  135. return rsa_decrypt_and_save(ectx);
  136. }
  137. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  138. UNUSED(ectx);
  139. return tpm2_session_close(&ctx.key.object.session);
  140. }
  141. // Register this tool with tpm2_tool.c
  142. TPM2_TOOL_REGISTER("rsadecrypt", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)