Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * PKCS#12 Personal Information Exchange Syntax |
| 3 | * |
Bence Szépkúti | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 45 | */ |
| 46 | /* |
| 47 | * The PKCS #12 Personal Information Exchange Syntax Standard v1.1 |
| 48 | * |
| 49 | * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf |
| 50 | * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn |
| 51 | */ |
| 52 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 54 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 55 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 57 | #endif |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 58 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_PKCS12_C) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 60 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 61 | #include "mbedtls/pkcs12.h" |
| 62 | #include "mbedtls/asn1.h" |
| 63 | #include "mbedtls/cipher.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 64 | #include "mbedtls/platform_util.h" |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 65 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 66 | #include <string.h> |
| 67 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 69 | #include "mbedtls/arc4.h" |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 70 | #endif |
| 71 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | #if defined(MBEDTLS_DES_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 73 | #include "mbedtls/des.h" |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 74 | #endif |
| 75 | |
Hanno Becker | 8a89f9f | 2018-10-12 10:46:32 +0100 | [diff] [blame] | 76 | #if defined(MBEDTLS_ASN1_PARSE_C) |
| 77 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 78 | static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params, |
| 79 | mbedtls_asn1_buf *salt, int *iterations ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 80 | { |
| 81 | int ret; |
Paul Bakker | f8d018a | 2013-06-29 12:16:17 +0200 | [diff] [blame] | 82 | unsigned char **p = ¶ms->p; |
| 83 | const unsigned char *end = params->p + params->len; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * pkcs-12PbeParams ::= SEQUENCE { |
| 87 | * salt OCTET STRING, |
| 88 | * iterations INTEGER |
| 89 | * } |
| 90 | * |
| 91 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) |
| 93 | return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + |
| 94 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 95 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 96 | if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) |
| 97 | return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 98 | |
| 99 | salt->p = *p; |
| 100 | *p += salt->len; |
| 101 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 ) |
| 103 | return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 104 | |
| 105 | if( *p != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + |
| 107 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 108 | |
| 109 | return( 0 ); |
| 110 | } |
| 111 | |
Manuel Pégourié-Gonnard | d02a1da | 2015-09-28 18:34:48 +0200 | [diff] [blame] | 112 | #define PKCS12_MAX_PWDLEN 128 |
| 113 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 114 | static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type, |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 115 | const unsigned char *pwd, size_t pwdlen, |
| 116 | unsigned char *key, size_t keylen, |
| 117 | unsigned char *iv, size_t ivlen ) |
| 118 | { |
Nicholas Wilson | 409401c | 2016-04-13 11:48:25 +0100 | [diff] [blame] | 119 | int ret, iterations = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 120 | mbedtls_asn1_buf salt; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 121 | size_t i; |
Manuel Pégourié-Gonnard | d02a1da | 2015-09-28 18:34:48 +0200 | [diff] [blame] | 122 | unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2]; |
| 123 | |
| 124 | if( pwdlen > PKCS12_MAX_PWDLEN ) |
| 125 | return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | memset( &salt, 0, sizeof(mbedtls_asn1_buf) ); |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 128 | memset( &unipwd, 0, sizeof(unipwd) ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 129 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 130 | if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt, |
| 131 | &iterations ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 132 | return( ret ); |
| 133 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 134 | for( i = 0; i < pwdlen; i++ ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 135 | unipwd[i * 2 + 1] = pwd[i]; |
| 136 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2, |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 138 | salt.p, salt.len, md_type, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 140 | { |
| 141 | return( ret ); |
| 142 | } |
| 143 | |
| 144 | if( iv == NULL || ivlen == 0 ) |
| 145 | return( 0 ); |
| 146 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2, |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 148 | salt.p, salt.len, md_type, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 150 | { |
| 151 | return( ret ); |
| 152 | } |
| 153 | return( 0 ); |
| 154 | } |
| 155 | |
Manuel Pégourié-Gonnard | d02a1da | 2015-09-28 18:34:48 +0200 | [diff] [blame] | 156 | #undef PKCS12_MAX_PWDLEN |
| 157 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode, |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 159 | const unsigned char *pwd, size_t pwdlen, |
| 160 | const unsigned char *data, size_t len, |
| 161 | unsigned char *output ) |
| 162 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | #if !defined(MBEDTLS_ARC4_C) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 164 | ((void) pbe_params); |
| 165 | ((void) mode); |
| 166 | ((void) pwd); |
| 167 | ((void) pwdlen); |
| 168 | ((void) data); |
| 169 | ((void) len); |
| 170 | ((void) output); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 171 | return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 172 | #else |
| 173 | int ret; |
| 174 | unsigned char key[16]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | mbedtls_arc4_context ctx; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 176 | ((void) mode); |
| 177 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | mbedtls_arc4_init( &ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 179 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 180 | if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, MBEDTLS_MD_SHA1, |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 181 | pwd, pwdlen, |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 182 | key, 16, NULL, 0 ) ) != 0 ) |
| 183 | { |
| 184 | return( ret ); |
| 185 | } |
| 186 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | mbedtls_arc4_setup( &ctx, key, 16 ); |
| 188 | if( ( ret = mbedtls_arc4_crypt( &ctx, len, data, output ) ) != 0 ) |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 189 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 190 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 191 | exit: |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 192 | mbedtls_platform_zeroize( key, sizeof( key ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 193 | mbedtls_arc4_free( &ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 194 | |
| 195 | return( ret ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | #endif /* MBEDTLS_ARC4_C */ |
Paul Bakker | 531e294 | 2013-06-24 19:23:12 +0200 | [diff] [blame] | 197 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 198 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode, |
| 200 | mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type, |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 201 | const unsigned char *pwd, size_t pwdlen, |
| 202 | const unsigned char *data, size_t len, |
| 203 | unsigned char *output ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 204 | { |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 205 | int ret, keylen = 0; |
| 206 | unsigned char key[32]; |
| 207 | unsigned char iv[16]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | const mbedtls_cipher_info_t *cipher_info; |
| 209 | mbedtls_cipher_context_t cipher_ctx; |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 210 | size_t olen = 0; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 211 | |
Paul Elliott | 4d44341 | 2021-11-11 19:00:38 +0000 | [diff] [blame] | 212 | if( pwd == NULL && pwdlen != 0 ) |
| 213 | return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA ); |
| 214 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | cipher_info = mbedtls_cipher_info_from_type( cipher_type ); |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 216 | if( cipher_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 217 | return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE ); |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 218 | |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 219 | keylen = cipher_info->key_bitlen / 8; |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 220 | |
| 221 | if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen, |
| 222 | key, keylen, |
| 223 | iv, cipher_info->iv_size ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 224 | { |
| 225 | return( ret ); |
| 226 | } |
| 227 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | mbedtls_cipher_init( &cipher_ctx ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 229 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 230 | if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 231 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 232 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 234 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 235 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 237 | goto exit; |
| 238 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 240 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 241 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 242 | if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len, |
Paul Bakker | 38b50d7 | 2013-06-24 19:33:27 +0200 | [diff] [blame] | 243 | output, &olen ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 244 | { |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 245 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 248 | if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 ) |
| 249 | ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 250 | |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 251 | exit: |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 252 | mbedtls_platform_zeroize( key, sizeof( key ) ); |
| 253 | mbedtls_platform_zeroize( iv, sizeof( iv ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | mbedtls_cipher_free( &cipher_ctx ); |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 255 | |
| 256 | return( ret ); |
Paul Bakker | 531e294 | 2013-06-24 19:23:12 +0200 | [diff] [blame] | 257 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 258 | |
Hanno Becker | 8a89f9f | 2018-10-12 10:46:32 +0100 | [diff] [blame] | 259 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
| 260 | |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 261 | static void pkcs12_fill_buffer( unsigned char *data, size_t data_len, |
| 262 | const unsigned char *filler, size_t fill_len ) |
| 263 | { |
| 264 | unsigned char *p = data; |
| 265 | size_t use_len; |
| 266 | |
Paul Elliott | 4d44341 | 2021-11-11 19:00:38 +0000 | [diff] [blame] | 267 | if( filler != NULL && fill_len != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 268 | { |
Paul Elliott | 4d44341 | 2021-11-11 19:00:38 +0000 | [diff] [blame] | 269 | while( data_len > 0 ) |
| 270 | { |
| 271 | use_len = ( data_len > fill_len ) ? fill_len : data_len; |
| 272 | memcpy( p, filler, use_len ); |
| 273 | p += use_len; |
| 274 | data_len -= use_len; |
| 275 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen, |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 280 | const unsigned char *pwd, size_t pwdlen, |
| 281 | const unsigned char *salt, size_t saltlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | mbedtls_md_type_t md_type, int id, int iterations ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 283 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 284 | int ret; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 285 | unsigned int j; |
| 286 | |
| 287 | unsigned char diversifier[128]; |
| 288 | unsigned char salt_block[128], pwd_block[128], hash_block[128]; |
Paul Elliott | 6b5707c | 2021-11-18 14:02:21 +0000 | [diff] [blame^] | 289 | unsigned char empty_string[2] = { 0, 0 }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 290 | unsigned char hash_output[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 291 | unsigned char *p; |
| 292 | unsigned char c; |
Paul Elliott | 6b5707c | 2021-11-18 14:02:21 +0000 | [diff] [blame^] | 293 | int use_password = 0; |
| 294 | int use_salt = 0; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 295 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 296 | size_t hlen, use_len, v, i; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 297 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 298 | const mbedtls_md_info_t *md_info; |
| 299 | mbedtls_md_context_t md_ctx; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 300 | |
| 301 | // This version only allows max of 64 bytes of password or salt |
| 302 | if( datalen > 128 || pwdlen > 64 || saltlen > 64 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 303 | return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 304 | |
Paul Elliott | 4d44341 | 2021-11-11 19:00:38 +0000 | [diff] [blame] | 305 | if( pwd == NULL && pwdlen != 0 ) |
| 306 | return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA ); |
| 307 | |
Paul Elliott | 6b5707c | 2021-11-18 14:02:21 +0000 | [diff] [blame^] | 308 | if( salt == NULL && saltlen != 0 ) |
| 309 | return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA ); |
| 310 | |
| 311 | use_password = ( pwd && pwdlen != 0 ); |
| 312 | use_salt = ( salt && saltlen != 0 ); |
| 313 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 314 | md_info = mbedtls_md_info_from_type( md_type ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 315 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 316 | return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 317 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 318 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 319 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 320 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 321 | return( ret ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 322 | hlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 323 | |
| 324 | if( hlen <= 32 ) |
| 325 | v = 64; |
| 326 | else |
| 327 | v = 128; |
| 328 | |
| 329 | memset( diversifier, (unsigned char) id, v ); |
| 330 | |
Paul Elliott | 6b5707c | 2021-11-18 14:02:21 +0000 | [diff] [blame^] | 331 | if( use_salt != 0 ) |
| 332 | { |
| 333 | pkcs12_fill_buffer( salt_block, v, salt, saltlen ); |
| 334 | } |
| 335 | |
| 336 | if( use_password != 0 ) |
| 337 | { |
| 338 | pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen ); |
| 339 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 340 | |
| 341 | p = data; |
| 342 | while( datalen > 0 ) |
| 343 | { |
| 344 | // Calculate hash( diversifier || salt_block || pwd_block ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 345 | if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 346 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 347 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 348 | if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 349 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 350 | |
Paul Elliott | 6b5707c | 2021-11-18 14:02:21 +0000 | [diff] [blame^] | 351 | if( use_salt != 0 ) |
| 352 | { |
| 353 | if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v )) != 0 ) |
| 354 | goto exit; |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | if( ( ret = mbedtls_md_update( &md_ctx, empty_string, |
| 359 | sizeof( empty_string ) )) != 0 ) |
| 360 | goto exit; |
| 361 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 362 | |
Paul Elliott | 6b5707c | 2021-11-18 14:02:21 +0000 | [diff] [blame^] | 363 | if( use_password != 0) |
| 364 | { |
| 365 | if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v )) != 0 ) |
| 366 | goto exit; |
| 367 | } |
| 368 | else |
| 369 | { |
| 370 | if( ( ret = mbedtls_md_update( &md_ctx, empty_string, |
| 371 | sizeof( empty_string ) )) != 0 ) |
| 372 | goto exit; |
| 373 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 374 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 375 | if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 376 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 377 | |
| 378 | // Perform remaining ( iterations - 1 ) recursive hash calculations |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 379 | for( i = 1; i < (size_t) iterations; i++ ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 380 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 381 | if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 ) |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 382 | goto exit; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | use_len = ( datalen > hlen ) ? hlen : datalen; |
| 386 | memcpy( p, hash_output, use_len ); |
| 387 | datalen -= use_len; |
| 388 | p += use_len; |
| 389 | |
| 390 | if( datalen == 0 ) |
| 391 | break; |
| 392 | |
| 393 | // Concatenating copies of hash_output into hash_block (B) |
| 394 | pkcs12_fill_buffer( hash_block, v, hash_output, hlen ); |
| 395 | |
| 396 | // B += 1 |
| 397 | for( i = v; i > 0; i-- ) |
| 398 | if( ++hash_block[i - 1] != 0 ) |
| 399 | break; |
| 400 | |
Paul Elliott | 6b5707c | 2021-11-18 14:02:21 +0000 | [diff] [blame^] | 401 | if( use_salt != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 402 | { |
Paul Elliott | 6b5707c | 2021-11-18 14:02:21 +0000 | [diff] [blame^] | 403 | // salt_block += B |
| 404 | c = 0; |
| 405 | for( i = v; i > 0; i-- ) |
| 406 | { |
| 407 | j = salt_block[i - 1] + hash_block[i - 1] + c; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 408 | c = (unsigned char) (j >> 8); |
| 409 | salt_block[i - 1] = j & 0xFF; |
Paul Elliott | 6b5707c | 2021-11-18 14:02:21 +0000 | [diff] [blame^] | 410 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 411 | } |
| 412 | |
Paul Elliott | 6b5707c | 2021-11-18 14:02:21 +0000 | [diff] [blame^] | 413 | if( use_password != 0 ) |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 414 | { |
Paul Elliott | 6b5707c | 2021-11-18 14:02:21 +0000 | [diff] [blame^] | 415 | // pwd_block += B |
| 416 | c = 0; |
| 417 | for( i = v; i > 0; i-- ) |
| 418 | { |
| 419 | j = pwd_block[i - 1] + hash_block[i - 1] + c; |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 420 | c = (unsigned char) (j >> 8); |
| 421 | pwd_block[i - 1] = j & 0xFF; |
Paul Elliott | 6b5707c | 2021-11-18 14:02:21 +0000 | [diff] [blame^] | 422 | } |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 426 | ret = 0; |
| 427 | |
| 428 | exit: |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 429 | mbedtls_platform_zeroize( salt_block, sizeof( salt_block ) ); |
| 430 | mbedtls_platform_zeroize( pwd_block, sizeof( pwd_block ) ); |
| 431 | mbedtls_platform_zeroize( hash_block, sizeof( hash_block ) ); |
| 432 | mbedtls_platform_zeroize( hash_output, sizeof( hash_output ) ); |
Paul Bakker | 91c301a | 2014-06-18 13:59:38 +0200 | [diff] [blame] | 433 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 434 | mbedtls_md_free( &md_ctx ); |
Paul Bakker | bd55244 | 2013-07-03 14:44:40 +0200 | [diff] [blame] | 435 | |
| 436 | return( ret ); |
Paul Bakker | f1f21fe | 2013-06-24 19:17:19 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 439 | #endif /* MBEDTLS_PKCS12_C */ |