Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file pkcs12.h |
| 3 | * |
| 4 | * \brief PKCS#12 Personal Information Exchange Syntax |
| 5 | * |
| 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
| 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_PKCS12_H |
| 28 | #define POLARSSL_PKCS12_H |
| 29 | |
| 30 | #include <string.h> |
| 31 | |
| 32 | #include "md.h" |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame^] | 33 | #include "cipher.h" |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 34 | #include "asn1.h" |
| 35 | |
| 36 | #define POLARSSL_ERR_PKCS12_BAD_INPUT_DATA -0x1F80 /**< Bad input parameters to function. */ |
| 37 | #define POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE -0x1F00 /**< Feature not available, e.g. unsupported encryption scheme. */ |
| 38 | #define POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT -0x1E80 /**< PBE ASN.1 data not as expected. */ |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame^] | 39 | #define POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH -0x1E00 /**< Given private key password does not allow for correct decryption. */ |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 40 | |
| 41 | #define PKCS12_DERIVE_KEY 1 /*< encryption/decryption key */ |
| 42 | #define PKCS12_DERIVE_IV 2 /*< initialization vector */ |
| 43 | #define PKCS12_DERIVE_MAC_KEY 3 /*< integrity / MAC key */ |
| 44 | |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame^] | 45 | #define PKCS12_PBE_DECRYPT 0 |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 46 | #define PKCS12_PBE_ENCRYPT 1 |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * PKCS#12 PBE types |
| 50 | */ |
| 51 | #define OID_PKCS12 "\x2a\x86\x48\x86\xf7\x0d\x01\x0c" |
| 52 | #define OID_PKCS12_PBE_SHA1_RC4_128 OID_PKCS12 "\x01\x01" |
| 53 | #define OID_PKCS12_PBE_SHA1_DES3_EDE_CBC OID_PKCS12 "\x01\x03" |
| 54 | #define OID_PKCS12_PBE_SHA1_DES2_EDE_CBC OID_PKCS12 "\x01\x04" |
| 55 | |
| 56 | #ifdef __cplusplus |
| 57 | extern "C" { |
| 58 | #endif |
| 59 | |
| 60 | /** |
| 61 | * \brief PKCS12 Password Based function (encryption / decryption) |
| 62 | * for pbeWithSHAAnd128BitRC4 |
| 63 | * |
| 64 | * \param pbe_params an ASN1 buffer containing the pkcs-12PbeParams structure |
| 65 | * \param mode either PKCS12_PBE_ENCRYPT or PKCS12_PBE_DECRYPT |
| 66 | * \param pwd the password used (may be NULL if no password is used) |
| 67 | * \param pwdlen length of the password (may be 0) |
| 68 | * \param input the input data |
| 69 | * \param len data length |
| 70 | * \param output the output buffer |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame^] | 71 | * |
| 72 | * \return 0 if successful, or a PolarSSL error code |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 73 | */ |
| 74 | int pkcs12_pbe_sha1_rc4_128( asn1_buf *pbe_params, int mode, |
| 75 | const unsigned char *pwd, size_t pwdlen, |
| 76 | const unsigned char *input, size_t len, |
| 77 | unsigned char *output ); |
| 78 | |
| 79 | /** |
| 80 | * \brief PKCS12 Password Based function (encryption / decryption) |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame^] | 81 | * for cipher-based and md-based PBE's |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 82 | * |
| 83 | * \param pbe_params an ASN1 buffer containing the pkcs-12PbeParams structure |
| 84 | * \param mode either PKCS12_PBE_ENCRYPT or PKCS12_PBE_DECRYPT |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame^] | 85 | * \param cipher_type the cipher used |
| 86 | * \param md_type the md used |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 87 | * \param pwd the password used (may be NULL if no password is used) |
| 88 | * \param pwdlen length of the password (may be 0) |
| 89 | * \param input the input data |
| 90 | * \param len data length |
| 91 | * \param output the output buffer |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 92 | * |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame^] | 93 | * \return 0 if successful, or a PolarSSL error code |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 94 | */ |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame^] | 95 | int pkcs12_pbe( asn1_buf *pbe_params, int mode, |
| 96 | cipher_type_t cipher_type, md_type_t md_type, |
| 97 | const unsigned char *pwd, size_t pwdlen, |
| 98 | const unsigned char *input, size_t len, |
| 99 | unsigned char *output ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * \brief The PKCS#12 derivation function uses a password and a salt |
| 103 | * to produce pseudo-random bits for a particular "purpose". |
| 104 | * |
| 105 | * Depending on the given id, this function can produce an |
| 106 | * encryption/decryption key, an nitialization vector or an |
| 107 | * integrity key. |
| 108 | * |
| 109 | * \param data buffer to store the derived data in |
| 110 | * \param datalen length to fill |
| 111 | * \param pwd password to use (may be NULL if no password is used) |
| 112 | * \param pwdlen length of the password (may be 0) |
| 113 | * \param salt salt buffer to use |
| 114 | * \param saltlen length of the salt |
| 115 | * \param md md type to use during the derivation |
| 116 | * \param id id that describes the purpose (can be PKCS12_DERIVE_KEY, |
| 117 | * PKCS12_DERIVE_IV or PKCS12_DERIVE_MAC_KEY) |
| 118 | * \param iterations number of iterations |
| 119 | * |
| 120 | * \return 0 if successful, or a MD, BIGNUM type error. |
| 121 | */ |
| 122 | int pkcs12_derivation( unsigned char *data, size_t datalen, |
| 123 | const unsigned char *pwd, size_t pwdlen, |
| 124 | const unsigned char *salt, size_t saltlen, |
| 125 | md_type_t md, int id, int iterations ); |
| 126 | |
| 127 | #ifdef __cplusplus |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | #endif /* pkcs12.h */ |