pkcs12.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * \file pkcs12.h
  3. *
  4. * \brief PKCS#12 Personal Information Exchange Syntax
  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_PKCS12_H
  11. #define MBEDTLS_PKCS12_H
  12. #include "mbedtls/build_info.h"
  13. #include "mbedtls/md.h"
  14. #include "mbedtls/cipher.h"
  15. #include "mbedtls/asn1.h"
  16. #include <stddef.h>
  17. /** Bad input parameters to function. */
  18. #define MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA -0x1F80
  19. /** Feature not available, e.g. unsupported encryption scheme. */
  20. #define MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE -0x1F00
  21. /** PBE ASN.1 data not as expected. */
  22. #define MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT -0x1E80
  23. /** Given private key password does not allow for correct decryption. */
  24. #define MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH -0x1E00
  25. #define MBEDTLS_PKCS12_DERIVE_KEY 1 /**< encryption/decryption key */
  26. #define MBEDTLS_PKCS12_DERIVE_IV 2 /**< initialization vector */
  27. #define MBEDTLS_PKCS12_DERIVE_MAC_KEY 3 /**< integrity / MAC key */
  28. #define MBEDTLS_PKCS12_PBE_DECRYPT MBEDTLS_DECRYPT
  29. #define MBEDTLS_PKCS12_PBE_ENCRYPT MBEDTLS_ENCRYPT
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #if defined(MBEDTLS_ASN1_PARSE_C) && defined(MBEDTLS_CIPHER_C)
  34. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  35. /**
  36. * \brief PKCS12 Password Based function (encryption / decryption)
  37. * for cipher-based and mbedtls_md-based PBE's
  38. *
  39. * \note When encrypting, #MBEDTLS_CIPHER_PADDING_PKCS7 must
  40. * be enabled at compile time.
  41. *
  42. * \deprecated This function is deprecated and will be removed in a
  43. * future version of the library.
  44. * Please use mbedtls_pkcs12_pbe_ext() instead.
  45. *
  46. * \warning When decrypting:
  47. * - if #MBEDTLS_CIPHER_PADDING_PKCS7 is enabled at compile
  48. * time, this function validates the CBC padding and returns
  49. * #MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH if the padding is
  50. * invalid. Note that this can help active adversaries
  51. * attempting to brute-forcing the password. Note also that
  52. * there is no guarantee that an invalid password will be
  53. * detected (the chances of a valid padding with a random
  54. * password are about 1/255).
  55. * - if #MBEDTLS_CIPHER_PADDING_PKCS7 is disabled at compile
  56. * time, this function does not validate the CBC padding.
  57. *
  58. * \param pbe_params an ASN1 buffer containing the pkcs-12 PbeParams structure
  59. * \param mode either #MBEDTLS_PKCS12_PBE_ENCRYPT or
  60. * #MBEDTLS_PKCS12_PBE_DECRYPT
  61. * \param cipher_type the cipher used
  62. * \param md_type the mbedtls_md used
  63. * \param pwd Latin1-encoded password used. This may only be \c NULL when
  64. * \p pwdlen is 0. No null terminator should be used.
  65. * \param pwdlen length of the password (may be 0)
  66. * \param data the input data
  67. * \param len data length
  68. * \param output Output buffer.
  69. * On success, it contains the encrypted or decrypted data,
  70. * possibly followed by the CBC padding.
  71. * On failure, the content is indeterminate.
  72. * For decryption, there must be enough room for \p len
  73. * bytes.
  74. * For encryption, there must be enough room for
  75. * \p len + 1 bytes, rounded up to the block size of
  76. * the block cipher identified by \p pbe_params.
  77. *
  78. * \return 0 if successful, or a MBEDTLS_ERR_XXX code
  79. */
  80. int MBEDTLS_DEPRECATED mbedtls_pkcs12_pbe(mbedtls_asn1_buf *pbe_params, int mode,
  81. mbedtls_cipher_type_t cipher_type,
  82. mbedtls_md_type_t md_type,
  83. const unsigned char *pwd, size_t pwdlen,
  84. const unsigned char *data, size_t len,
  85. unsigned char *output);
  86. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  87. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  88. /**
  89. * \brief PKCS12 Password Based function (encryption / decryption)
  90. * for cipher-based and mbedtls_md-based PBE's
  91. *
  92. *
  93. * \warning When decrypting:
  94. * - This function validates the CBC padding and returns
  95. * #MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH if the padding is
  96. * invalid. Note that this can help active adversaries
  97. * attempting to brute-forcing the password. Note also that
  98. * there is no guarantee that an invalid password will be
  99. * detected (the chances of a valid padding with a random
  100. * password are about 1/255).
  101. *
  102. * \param pbe_params an ASN1 buffer containing the pkcs-12 PbeParams structure
  103. * \param mode either #MBEDTLS_PKCS12_PBE_ENCRYPT or
  104. * #MBEDTLS_PKCS12_PBE_DECRYPT
  105. * \param cipher_type the cipher used
  106. * \param md_type the mbedtls_md used
  107. * \param pwd Latin1-encoded password used. This may only be \c NULL when
  108. * \p pwdlen is 0. No null terminator should be used.
  109. * \param pwdlen length of the password (may be 0)
  110. * \param data the input data
  111. * \param len data length
  112. * \param output Output buffer.
  113. * On success, it contains the encrypted or decrypted data,
  114. * possibly followed by the CBC padding.
  115. * On failure, the content is indeterminate.
  116. * For decryption, there must be enough room for \p len
  117. * bytes.
  118. * For encryption, there must be enough room for
  119. * \p len + 1 bytes, rounded up to the block size of
  120. * the block cipher identified by \p pbe_params.
  121. * \param output_size size of output buffer.
  122. * This must be big enough to accommodate for output plus
  123. * padding data.
  124. * \param output_len On success, length of actual data written to the output buffer.
  125. *
  126. * \return 0 if successful, or a MBEDTLS_ERR_XXX code
  127. */
  128. int mbedtls_pkcs12_pbe_ext(mbedtls_asn1_buf *pbe_params, int mode,
  129. mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
  130. const unsigned char *pwd, size_t pwdlen,
  131. const unsigned char *data, size_t len,
  132. unsigned char *output, size_t output_size,
  133. size_t *output_len);
  134. #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
  135. #endif /* MBEDTLS_ASN1_PARSE_C && MBEDTLS_CIPHER_C */
  136. /**
  137. * \brief The PKCS#12 derivation function uses a password and a salt
  138. * to produce pseudo-random bits for a particular "purpose".
  139. *
  140. * Depending on the given id, this function can produce an
  141. * encryption/decryption key, an initialization vector or an
  142. * integrity key.
  143. *
  144. * \param data buffer to store the derived data in
  145. * \param datalen length of buffer to fill
  146. * \param pwd The password to use. For compliance with PKCS#12 §B.1, this
  147. * should be a BMPString, i.e. a Unicode string where each
  148. * character is encoded as 2 bytes in big-endian order, with
  149. * no byte order mark and with a null terminator (i.e. the
  150. * last two bytes should be 0x00 0x00).
  151. * \param pwdlen length of the password (may be 0).
  152. * \param salt Salt buffer to use. This may only be \c NULL when
  153. * \p saltlen is 0.
  154. * \param saltlen length of the salt (may be zero)
  155. * \param mbedtls_md mbedtls_md type to use during the derivation
  156. * \param id id that describes the purpose (can be
  157. * #MBEDTLS_PKCS12_DERIVE_KEY, #MBEDTLS_PKCS12_DERIVE_IV or
  158. * #MBEDTLS_PKCS12_DERIVE_MAC_KEY)
  159. * \param iterations number of iterations
  160. *
  161. * \return 0 if successful, or a MD, BIGNUM type error.
  162. */
  163. int mbedtls_pkcs12_derivation(unsigned char *data, size_t datalen,
  164. const unsigned char *pwd, size_t pwdlen,
  165. const unsigned char *salt, size_t saltlen,
  166. mbedtls_md_type_t mbedtls_md, int id, int iterations);
  167. #ifdef __cplusplus
  168. }
  169. #endif
  170. #endif /* pkcs12.h */