blob: 87f7681f29a7be05bee7ff7a9786faf692f987f4 [file] [log] [blame]
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02001/**
2 * \file pkcs12.h
3 *
4 * \brief PKCS#12 Personal Information Exchange Syntax
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_PKCS12_H
11#define MBEDTLS_PKCS12_H
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020012
Bence Szépkútic662b362021-05-27 11:25:03 +020013#include "mbedtls/build_info.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050014
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010015#include "mbedtls/md.h"
16#include "mbedtls/cipher.h"
17#include "mbedtls/asn1.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020018
Rich Evans00ab4702015-02-06 13:43:58 +000019#include <stddef.h>
20
Gilles Peskined2971572021-07-26 18:48:10 +020021/** Bad input parameters to function. */
22#define MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA -0x1F80
23/** Feature not available, e.g. unsupported encryption scheme. */
24#define MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE -0x1F00
25/** PBE ASN.1 data not as expected. */
26#define MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT -0x1E80
27/** Given private key password does not allow for correct decryption. */
28#define MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH -0x1E00
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#define MBEDTLS_PKCS12_DERIVE_KEY 1 /**< encryption/decryption key */
31#define MBEDTLS_PKCS12_DERIVE_IV 2 /**< initialization vector */
32#define MBEDTLS_PKCS12_DERIVE_MAC_KEY 3 /**< integrity / MAC key */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020033
Valerio Setti4577bda2023-12-01 16:51:24 +010034#define MBEDTLS_PKCS12_PBE_DECRYPT MBEDTLS_DECRYPT
35#define MBEDTLS_PKCS12_PBE_ENCRYPT MBEDTLS_ENCRYPT
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020036
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020037#ifdef __cplusplus
38extern "C" {
39#endif
40
Valerio Settia69e8722023-12-21 16:37:29 +010041#if defined(MBEDTLS_ASN1_PARSE_C) && defined(MBEDTLS_CIPHER_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050042
Waleed Elmelegyd5278962023-09-12 14:42:49 +010043#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020044/**
45 * \brief PKCS12 Password Based function (encryption / decryption)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 * for cipher-based and mbedtls_md-based PBE's
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020047 *
Waleed Elmelegy255db802023-09-04 15:11:22 +010048 * \note When encrypting, #MBEDTLS_CIPHER_PADDING_PKCS7 must
49 * be enabled at compile time.
50 *
Waleed Elmelegyd5278962023-09-12 14:42:49 +010051 * \deprecated This function is deprecated and will be removed in a
52 * future version of the library.
53 * Please use mbedtls_pkcs12_pbe_ext() instead.
54 *
Waleed Elmelegy255db802023-09-04 15:11:22 +010055 * \warning When decrypting:
56 * - if #MBEDTLS_CIPHER_PADDING_PKCS7 is enabled at compile
57 * time, this function validates the CBC padding and returns
58 * #MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH if the padding is
59 * invalid. Note that this can help active adversaries
60 * attempting to brute-forcing the password. Note also that
61 * there is no guarantee that an invalid password will be
62 * detected (the chances of a valid padding with a random
63 * password are about 1/255).
64 * - if #MBEDTLS_CIPHER_PADDING_PKCS7 is disabled at compile
65 * time, this function does not validate the CBC padding.
66 *
Paul Elliott853c0da2021-11-11 19:00:38 +000067 * \param pbe_params an ASN1 buffer containing the pkcs-12 PbeParams structure
Paul Elliottfb5fdb52021-11-18 12:39:10 +000068 * \param mode either #MBEDTLS_PKCS12_PBE_ENCRYPT or
69 * #MBEDTLS_PKCS12_PBE_DECRYPT
Paul Bakker38b50d72013-06-24 19:33:27 +020070 * \param cipher_type the cipher used
Paul Elliott853c0da2021-11-11 19:00:38 +000071 * \param md_type the mbedtls_md used
Paul Elliottfb5fdb52021-11-18 12:39:10 +000072 * \param pwd Latin1-encoded password used. This may only be \c NULL when
Paul Elliottad7e8a72021-11-30 15:37:49 +000073 * \p pwdlen is 0. No null terminator should be used.
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020074 * \param pwdlen length of the password (may be 0)
Waleed Elmelegye1cb35b2023-09-06 15:48:08 +010075 * \param data the input data
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020076 * \param len data length
Waleed Elmelegy255db802023-09-04 15:11:22 +010077 * \param output Output buffer.
78 * On success, it contains the encrypted or decrypted data,
79 * possibly followed by the CBC padding.
80 * On failure, the content is indeterminate.
81 * For decryption, there must be enough room for \p len
82 * bytes.
83 * For encryption, there must be enough room for
84 * \p len + 1 bytes, rounded up to the block size of
85 * the block cipher identified by \p pbe_params.
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020086 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 * \return 0 if successful, or a MBEDTLS_ERR_XXX code
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020088 */
Waleed Elmelegyd5278962023-09-12 14:42:49 +010089int MBEDTLS_DEPRECATED mbedtls_pkcs12_pbe(mbedtls_asn1_buf *pbe_params, int mode,
Waleed Elmelegy5e48cad2023-09-12 14:52:48 +010090 mbedtls_cipher_type_t cipher_type,
91 mbedtls_md_type_t md_type,
92 const unsigned char *pwd, size_t pwdlen,
93 const unsigned char *data, size_t len,
94 unsigned char *output);
Waleed Elmelegyd5278962023-09-12 14:42:49 +010095#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020096
Waleed Elmelegye1cb35b2023-09-06 15:48:08 +010097#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
98
99/**
100 * \brief PKCS12 Password Based function (encryption / decryption)
101 * for cipher-based and mbedtls_md-based PBE's
102 *
103 *
104 * \warning When decrypting:
105 * - This function validates the CBC padding and returns
106 * #MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH if the padding is
107 * invalid. Note that this can help active adversaries
108 * attempting to brute-forcing the password. Note also that
109 * there is no guarantee that an invalid password will be
110 * detected (the chances of a valid padding with a random
111 * password are about 1/255).
112 *
113 * \param pbe_params an ASN1 buffer containing the pkcs-12 PbeParams structure
114 * \param mode either #MBEDTLS_PKCS12_PBE_ENCRYPT or
115 * #MBEDTLS_PKCS12_PBE_DECRYPT
116 * \param cipher_type the cipher used
117 * \param md_type the mbedtls_md used
118 * \param pwd Latin1-encoded password used. This may only be \c NULL when
119 * \p pwdlen is 0. No null terminator should be used.
120 * \param pwdlen length of the password (may be 0)
121 * \param data the input data
122 * \param len data length
123 * \param output Output buffer.
124 * On success, it contains the encrypted or decrypted data,
125 * possibly followed by the CBC padding.
126 * On failure, the content is indeterminate.
127 * For decryption, there must be enough room for \p len
128 * bytes.
129 * For encryption, there must be enough room for
130 * \p len + 1 bytes, rounded up to the block size of
131 * the block cipher identified by \p pbe_params.
132 * \param output_size size of output buffer.
133 * This must be big enough to accommodate for output plus
134 * padding data.
135 * \param output_len On success, length of actual data written to the output buffer.
136 *
137 * \return 0 if successful, or a MBEDTLS_ERR_XXX code
138 */
139int mbedtls_pkcs12_pbe_ext(mbedtls_asn1_buf *pbe_params, int mode,
140 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
141 const unsigned char *pwd, size_t pwdlen,
142 const unsigned char *data, size_t len,
143 unsigned char *output, size_t output_size,
144 size_t *output_len);
145
146#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
147
Valerio Settia69e8722023-12-21 16:37:29 +0100148#endif /* MBEDTLS_ASN1_PARSE_C && MBEDTLS_CIPHER_C */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500149
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200150/**
151 * \brief The PKCS#12 derivation function uses a password and a salt
152 * to produce pseudo-random bits for a particular "purpose".
153 *
154 * Depending on the given id, this function can produce an
Paul Elliottfb5fdb52021-11-18 12:39:10 +0000155 * encryption/decryption key, an initialization vector or an
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200156 * integrity key.
157 *
158 * \param data buffer to store the derived data in
Paul Elliott853c0da2021-11-11 19:00:38 +0000159 * \param datalen length of buffer to fill
Paul Elliottfb5fdb52021-11-18 12:39:10 +0000160 * \param pwd The password to use. For compliance with PKCS#12 §B.1, this
161 * should be a BMPString, i.e. a Unicode string where each
162 * character is encoded as 2 bytes in big-endian order, with
163 * no byte order mark and with a null terminator (i.e. the
164 * last two bytes should be 0x00 0x00).
165 * \param pwdlen length of the password (may be 0).
Tom Cosgrove1797b052022-12-04 17:19:59 +0000166 * \param salt Salt buffer to use. This may only be \c NULL when
Paul Elliottad7e8a72021-11-30 15:37:49 +0000167 * \p saltlen is 0.
Paul Elliottfb5fdb52021-11-18 12:39:10 +0000168 * \param saltlen length of the salt (may be zero)
Paul Elliott853c0da2021-11-11 19:00:38 +0000169 * \param mbedtls_md mbedtls_md type to use during the derivation
170 * \param id id that describes the purpose (can be
Paul Elliottfb5fdb52021-11-18 12:39:10 +0000171 * #MBEDTLS_PKCS12_DERIVE_KEY, #MBEDTLS_PKCS12_DERIVE_IV or
172 * #MBEDTLS_PKCS12_DERIVE_MAC_KEY)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200173 * \param iterations number of iterations
174 *
175 * \return 0 if successful, or a MD, BIGNUM type error.
176 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100177int mbedtls_pkcs12_derivation(unsigned char *data, size_t datalen,
178 const unsigned char *pwd, size_t pwdlen,
179 const unsigned char *salt, size_t saltlen,
180 mbedtls_md_type_t mbedtls_md, int id, int iterations);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200181
182#ifdef __cplusplus
183}
184#endif
185
186#endif /* pkcs12.h */