pem.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * \file pem.h
  3. *
  4. * \brief Privacy Enhanced Mail (PEM) decoding
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  9. */
  10. #ifndef MBEDTLS_PEM_H
  11. #define MBEDTLS_PEM_H
  12. #include "mbedtls/private_access.h"
  13. #include "mbedtls/build_info.h"
  14. #include <stddef.h>
  15. /**
  16. * \name PEM Error codes
  17. * These error codes are returned in case of errors reading the
  18. * PEM data.
  19. * \{
  20. */
  21. /** No PEM header or footer found. */
  22. #define MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT -0x1080
  23. /** PEM string is not as expected. */
  24. #define MBEDTLS_ERR_PEM_INVALID_DATA -0x1100
  25. /** Failed to allocate memory. */
  26. #define MBEDTLS_ERR_PEM_ALLOC_FAILED -0x1180
  27. /** RSA IV is not in hex-format. */
  28. #define MBEDTLS_ERR_PEM_INVALID_ENC_IV -0x1200
  29. /** Unsupported key encryption algorithm. */
  30. #define MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG -0x1280
  31. /** Private key password can't be empty. */
  32. #define MBEDTLS_ERR_PEM_PASSWORD_REQUIRED -0x1300
  33. /** Given private key password does not allow for correct decryption. */
  34. #define MBEDTLS_ERR_PEM_PASSWORD_MISMATCH -0x1380
  35. /** Unavailable feature, e.g. hashing/encryption combination. */
  36. #define MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE -0x1400
  37. /** Bad input parameters to function. */
  38. #define MBEDTLS_ERR_PEM_BAD_INPUT_DATA -0x1480
  39. /** \} name PEM Error codes */
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. #if defined(MBEDTLS_PEM_PARSE_C)
  44. /**
  45. * \brief PEM context structure
  46. */
  47. typedef struct mbedtls_pem_context {
  48. unsigned char *MBEDTLS_PRIVATE(buf); /*!< buffer for decoded data */
  49. size_t MBEDTLS_PRIVATE(buflen); /*!< length of the buffer */
  50. unsigned char *MBEDTLS_PRIVATE(info); /*!< buffer for extra header information */
  51. }
  52. mbedtls_pem_context;
  53. /**
  54. * \brief PEM context setup
  55. *
  56. * \param ctx context to be initialized
  57. */
  58. void mbedtls_pem_init(mbedtls_pem_context *ctx);
  59. /**
  60. * \brief Read a buffer for PEM information and store the resulting
  61. * data into the specified context buffers.
  62. *
  63. * \param ctx context to use
  64. * \param header header string to seek and expect
  65. * \param footer footer string to seek and expect
  66. * \param data source data to look in (must be nul-terminated)
  67. * \param pwd password for decryption (can be NULL)
  68. * \param pwdlen length of password
  69. * \param use_len destination for total length used from data buffer. It is
  70. * set after header is correctly read, so unless you get
  71. * MBEDTLS_ERR_PEM_BAD_INPUT_DATA or
  72. * MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT, use_len is
  73. * the length to skip.
  74. *
  75. * \note Attempts to check password correctness by verifying if
  76. * the decrypted text starts with an ASN.1 sequence of
  77. * appropriate length
  78. *
  79. * \note \c mbedtls_pem_free must be called on PEM context before
  80. * the PEM context can be reused in another call to
  81. * \c mbedtls_pem_read_buffer
  82. *
  83. * \return 0 on success, or a specific PEM error code
  84. */
  85. int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
  86. const unsigned char *data,
  87. const unsigned char *pwd,
  88. size_t pwdlen, size_t *use_len);
  89. /**
  90. * \brief Get the pointer to the decoded binary data in a PEM context.
  91. *
  92. * \param ctx PEM context to access.
  93. * \param buflen On success, this will contain the length of the binary data.
  94. * This must be a valid (non-null) pointer.
  95. *
  96. * \return A pointer to the decoded binary data.
  97. *
  98. * \note The returned pointer remains valid only until \p ctx is
  99. modified or freed.
  100. */
  101. static inline const unsigned char *mbedtls_pem_get_buffer(mbedtls_pem_context *ctx, size_t *buflen)
  102. {
  103. *buflen = ctx->MBEDTLS_PRIVATE(buflen);
  104. return ctx->MBEDTLS_PRIVATE(buf);
  105. }
  106. /**
  107. * \brief PEM context memory freeing
  108. *
  109. * \param ctx context to be freed
  110. */
  111. void mbedtls_pem_free(mbedtls_pem_context *ctx);
  112. #endif /* MBEDTLS_PEM_PARSE_C */
  113. #if defined(MBEDTLS_PEM_WRITE_C)
  114. /**
  115. * \brief Write a buffer of PEM information from a DER encoded
  116. * buffer.
  117. *
  118. * \param header The header string to write.
  119. * \param footer The footer string to write.
  120. * \param der_data The DER data to encode.
  121. * \param der_len The length of the DER data \p der_data in Bytes.
  122. * \param buf The buffer to write to.
  123. * \param buf_len The length of the output buffer \p buf in Bytes.
  124. * \param olen The address at which to store the total length written
  125. * or required (if \p buf_len is not enough).
  126. *
  127. * \note You may pass \c NULL for \p buf and \c 0 for \p buf_len
  128. * to request the length of the resulting PEM buffer in
  129. * `*olen`.
  130. *
  131. * \note This function may be called with overlapping \p der_data
  132. * and \p buf buffers.
  133. *
  134. * \return \c 0 on success.
  135. * \return #MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL if \p buf isn't large
  136. * enough to hold the PEM buffer. In this case, `*olen` holds
  137. * the required minimum size of \p buf.
  138. * \return Another PEM or BASE64 error code on other kinds of failure.
  139. */
  140. int mbedtls_pem_write_buffer(const char *header, const char *footer,
  141. const unsigned char *der_data, size_t der_len,
  142. unsigned char *buf, size_t buf_len, size_t *olen);
  143. #endif /* MBEDTLS_PEM_WRITE_C */
  144. #ifdef __cplusplus
  145. }
  146. #endif
  147. #endif /* pem.h */