tpm2_convert.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #ifndef CONVERSION_H
  3. #define CONVERSION_H
  4. #include <stdbool.h>
  5. #include <openssl/evp.h>
  6. #include <tss2/tss2_sys.h>
  7. typedef enum tpm2_convert_pubkey_fmt tpm2_convert_pubkey_fmt;
  8. enum tpm2_convert_pubkey_fmt {
  9. pubkey_format_tss,
  10. pubkey_format_pem,
  11. pubkey_format_der,
  12. pubkey_format_tpmt,
  13. pubkey_format_err
  14. };
  15. typedef enum tpm2_convert_sig_fmt tpm2_convert_sig_fmt;
  16. enum tpm2_convert_sig_fmt {
  17. signature_format_tss,
  18. signature_format_plain,
  19. signature_format_err
  20. };
  21. /**
  22. * Parses the given command line public key format option string and returns
  23. * the corresponding pubkey_format enum value.
  24. *
  25. * LOG_ERR is used to communicate errors.
  26. *
  27. * @return
  28. * On error pubkey_format_err is returned.
  29. */
  30. tpm2_convert_pubkey_fmt tpm2_convert_pubkey_fmt_from_optarg(const char *label);
  31. /**
  32. * Converts the given public key structure into the requested target format
  33. * and writes the result to the given file system path.
  34. *
  35. * LOG_ERR is used to communicate errors.
  36. */
  37. bool tpm2_convert_pubkey_save(TPM2B_PUBLIC *public,
  38. tpm2_convert_pubkey_fmt format, const char *path);
  39. /**
  40. * Parses the given command line signature format option string and returns
  41. * the corresponding signature_format enum value.
  42. *
  43. * LOG_ERR is used to communicate errors.
  44. *
  45. * @return
  46. * On error signature_format_err is returned.
  47. */
  48. tpm2_convert_sig_fmt tpm2_convert_sig_fmt_from_optarg(const char *label);
  49. /**
  50. * Converts the given signature data into the requested target format and
  51. * writes the result to the given file system path.
  52. *
  53. * LOG_ERR is used to communicate errors.
  54. */
  55. bool tpm2_convert_sig_save(TPMT_SIGNATURE *signature,
  56. tpm2_convert_sig_fmt format, const char *path);
  57. /**
  58. * Like tpm2_convert_save with the "plain" signature option.
  59. *
  60. * @param size
  61. * The size of the signature buffer.
  62. * @param signature
  63. * The signature to convert.
  64. * @return
  65. * NULL on error or a buffer of size bytes to be freed by the caller
  66. * via free(2).
  67. */
  68. UINT8 *tpm2_convert_sig(UINT16 *size, TPMT_SIGNATURE *signature);
  69. /**
  70. * Load a signature from path and convert the format
  71. * @param path
  72. * The path to load the signature from.
  73. * @param format
  74. * The tss signature format
  75. * @param sig_alg
  76. * The algorithm used for the signature. Only RSASSA (RSA PKCS1.5) signatures accepted.
  77. * @param halg
  78. * The hashing algorithm used.
  79. * @param signature
  80. * The signature structure to output too.
  81. * @return
  82. * true on success, false on error.
  83. */
  84. bool tpm2_convert_sig_load(const char *path, tpm2_convert_sig_fmt format,
  85. TPMI_ALG_SIG_SCHEME sig_alg, TPMI_ALG_HASH halg,
  86. TPMT_SIGNATURE *signature);
  87. /**
  88. * Given a file, loads up the plain format of the signature. Probing to determine
  89. * if its a TSS buffer (using libmu errors as the detector) or a plain OSSL style
  90. * signature.
  91. * into a buffer.
  92. * @param path
  93. * The file path containing the signature.
  94. * @param signature
  95. * The plain signature bytes.
  96. * @param halg:
  97. * If the signature scheme is *tss* also provide the hash algorithm, else
  98. * set it to TPM2_ALG_NULL.
  99. * @return
  100. * true on success, false on error.
  101. */
  102. bool tpm2_convert_sig_load_plain(const char *path,
  103. TPM2B_MAX_BUFFER *signature, TPMI_ALG_HASH *halg);
  104. bool tpm2_public_load_pkey(const char *path, EVP_PKEY **pkey);
  105. /**
  106. * Encode a binary buffer to a Base64-encoded String.
  107. * @param buffer
  108. * The binary buffer.
  109. * @param buffer_length:
  110. * The length of the binary buffer.
  111. * @param base64
  112. * The resulting Base64-encoded String.
  113. * @return
  114. * true on success, false on error.
  115. */
  116. bool tpm2_base64_encode(BYTE *buffer, size_t buffer_length, char *base64);
  117. /**
  118. * Decode a Base64-encoded String to a binary buffer.
  119. * @param base64
  120. * The Base64-encoded String.
  121. * @param buffer
  122. * The resulting binary buffer, valid on success.
  123. * @param buffer_length:
  124. * The length of the resulting binary buffer, valid on success.
  125. * @return
  126. * true on success, false on error.
  127. */
  128. bool tpm2_base64_decode(char *base64, BYTE *buffer, size_t *buffer_length);
  129. #endif /* CONVERSION_H */