blob: c418e8f243d3035c2b264e7c39f91f2d6d3bd8d8 [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/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 *
10 * This file is provided under the Apache License 2.0, or the
11 * GNU General Public License v2.0 or later.
12 *
13 * **********
14 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020027 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020028 * **********
29 *
30 * **********
31 * GNU General Public License v2.0 or later:
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 *
47 * **********
48 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000049 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#ifndef MBEDTLS_PKCS12_H
52#define MBEDTLS_PKCS12_H
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020053
Ron Eldor3625e562018-12-16 12:14:37 +020054#if !defined(MBEDTLS_CONFIG_FILE)
55#include "config.h"
56#else
57#include MBEDTLS_CONFIG_FILE
58#endif
59
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020060#include "md.h"
Paul Bakker38b50d72013-06-24 19:33:27 +020061#include "cipher.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020062#include "asn1.h"
63
Rich Evans00ab4702015-02-06 13:43:58 +000064#include <stddef.h>
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#define MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA -0x1F80 /**< Bad input parameters to function. */
67#define MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE -0x1F00 /**< Feature not available, e.g. unsupported encryption scheme. */
68#define MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT -0x1E80 /**< PBE ASN.1 data not as expected. */
69#define MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH -0x1E00 /**< Given private key password does not allow for correct decryption. */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#define MBEDTLS_PKCS12_DERIVE_KEY 1 /**< encryption/decryption key */
72#define MBEDTLS_PKCS12_DERIVE_IV 2 /**< initialization vector */
73#define MBEDTLS_PKCS12_DERIVE_MAC_KEY 3 /**< integrity / MAC key */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#define MBEDTLS_PKCS12_PBE_DECRYPT 0
76#define MBEDTLS_PKCS12_PBE_ENCRYPT 1
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020077
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020078#ifdef __cplusplus
79extern "C" {
80#endif
81
Hanno Beckerd30cd342018-10-12 10:46:32 +010082#if defined(MBEDTLS_ASN1_PARSE_C)
83
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020084/**
85 * \brief PKCS12 Password Based function (encryption / decryption)
86 * for pbeWithSHAAnd128BitRC4
87 *
88 * \param pbe_params an ASN1 buffer containing the pkcs-12PbeParams structure
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 * \param mode either MBEDTLS_PKCS12_PBE_ENCRYPT or MBEDTLS_PKCS12_PBE_DECRYPT
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020090 * \param pwd the password used (may be NULL if no password is used)
91 * \param pwdlen length of the password (may be 0)
92 * \param input the input data
93 * \param len data length
94 * \param output the output buffer
Paul Bakker38b50d72013-06-24 19:33:27 +020095 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 * \return 0 if successful, or a MBEDTLS_ERR_XXX code
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020099 const unsigned char *pwd, size_t pwdlen,
100 const unsigned char *input, size_t len,
101 unsigned char *output );
102
103/**
104 * \brief PKCS12 Password Based function (encryption / decryption)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 * for cipher-based and mbedtls_md-based PBE's
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200106 *
107 * \param pbe_params an ASN1 buffer containing the pkcs-12PbeParams structure
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 * \param mode either MBEDTLS_PKCS12_PBE_ENCRYPT or MBEDTLS_PKCS12_PBE_DECRYPT
Paul Bakker38b50d72013-06-24 19:33:27 +0200109 * \param cipher_type the cipher used
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 * \param md_type the mbedtls_md used
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200111 * \param pwd the password used (may be NULL if no password is used)
112 * \param pwdlen length of the password (may be 0)
113 * \param input the input data
114 * \param len data length
115 * \param output the output buffer
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200116 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 * \return 0 if successful, or a MBEDTLS_ERR_XXX code
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
120 mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
Paul Bakker38b50d72013-06-24 19:33:27 +0200121 const unsigned char *pwd, size_t pwdlen,
122 const unsigned char *input, size_t len,
123 unsigned char *output );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200124
Hanno Beckerd30cd342018-10-12 10:46:32 +0100125#endif /* MBEDTLS_ASN1_PARSE_C */
126
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200127/**
128 * \brief The PKCS#12 derivation function uses a password and a salt
129 * to produce pseudo-random bits for a particular "purpose".
130 *
131 * Depending on the given id, this function can produce an
132 * encryption/decryption key, an nitialization vector or an
133 * integrity key.
134 *
135 * \param data buffer to store the derived data in
136 * \param datalen length to fill
137 * \param pwd password to use (may be NULL if no password is used)
138 * \param pwdlen length of the password (may be 0)
139 * \param salt salt buffer to use
140 * \param saltlen length of the salt
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 * \param mbedtls_md mbedtls_md type to use during the derivation
142 * \param id id that describes the purpose (can be MBEDTLS_PKCS12_DERIVE_KEY,
143 * MBEDTLS_PKCS12_DERIVE_IV or MBEDTLS_PKCS12_DERIVE_MAC_KEY)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200144 * \param iterations number of iterations
145 *
146 * \return 0 if successful, or a MD, BIGNUM type error.
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200149 const unsigned char *pwd, size_t pwdlen,
150 const unsigned char *salt, size_t saltlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_md_type_t mbedtls_md, int id, int iterations );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200152
153#ifdef __cplusplus
154}
155#endif
156
157#endif /* pkcs12.h */