blob: 5e8467fd477371a7e1c56353c71f43c0436cc685 [file] [log] [blame]
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02001/*
2 * PKCS#12 Personal Information Exchange Syntax
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020018 */
19/*
20 * The PKCS #12 Personal Information Exchange Syntax Standard v1.1
21 *
22 * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf
23 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn
24 */
25
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PKCS12_C)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020029
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020030# include "mbedtls/pkcs12.h"
31# include "mbedtls/asn1.h"
32# include "mbedtls/cipher.h"
33# include "mbedtls/platform_util.h"
34# include "mbedtls/error.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020035
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020036# include <string.h>
Rich Evans00ab4702015-02-06 13:43:58 +000037
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020038# if defined(MBEDTLS_DES_C)
39# include "mbedtls/des.h"
40# endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020041
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020042# if defined(MBEDTLS_ASN1_PARSE_C)
Hanno Becker8a89f9f2018-10-12 10:46:32 +010043
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020044static int pkcs12_parse_pbe_params(mbedtls_asn1_buf *params,
45 mbedtls_asn1_buf *salt,
46 int *iterations)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020047{
Janos Follath24eed8d2019-11-22 13:21:35 +000048 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020049 unsigned char **p = &params->p;
50 const unsigned char *end = params->p + params->len;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020051
52 /*
53 * pkcs-12PbeParams ::= SEQUENCE {
54 * salt OCTET STRING,
55 * iterations INTEGER
56 * }
57 *
58 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020059 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE))
60 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
61 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG));
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020062
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020063 if ((ret = mbedtls_asn1_get_tag(p, end, &salt->len,
64 MBEDTLS_ASN1_OCTET_STRING)) != 0)
65 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020066
67 salt->p = *p;
68 *p += salt->len;
69
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020070 if ((ret = mbedtls_asn1_get_int(p, end, iterations)) != 0)
71 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020072
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020073 if (*p != end)
74 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
75 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020076
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020077 return 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020078}
79
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020080# define PKCS12_MAX_PWDLEN 128
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020081
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020082static int pkcs12_pbe_derive_key_iv(mbedtls_asn1_buf *pbe_params,
83 mbedtls_md_type_t md_type,
84 const unsigned char *pwd,
85 size_t pwdlen,
86 unsigned char *key,
87 size_t keylen,
88 unsigned char *iv,
89 size_t ivlen)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020090{
Nicholas Wilson409401c2016-04-13 11:48:25 +010091 int ret, iterations = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_asn1_buf salt;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020093 size_t i;
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +020094 unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
95
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020096 if (pwdlen > PKCS12_MAX_PWDLEN)
97 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020098
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020099 memset(&salt, 0, sizeof(mbedtls_asn1_buf));
100 memset(&unipwd, 0, sizeof(unipwd));
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200101
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200102 if ((ret = pkcs12_parse_pbe_params(pbe_params, &salt, &iterations)) != 0)
103 return ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200104
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200105 for (i = 0; i < pwdlen; i++)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200106 unipwd[i * 2 + 1] = pwd[i];
107
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200108 if ((ret = mbedtls_pkcs12_derivation(
109 key, keylen, unipwd, pwdlen * 2 + 2, salt.p, salt.len, md_type,
110 MBEDTLS_PKCS12_DERIVE_KEY, iterations)) != 0) {
111 return ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200112 }
113
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200114 if (iv == NULL || ivlen == 0)
115 return 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200116
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200117 if ((ret = mbedtls_pkcs12_derivation(
118 iv, ivlen, unipwd, pwdlen * 2 + 2, salt.p, salt.len, md_type,
119 MBEDTLS_PKCS12_DERIVE_IV, iterations)) != 0) {
120 return ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200121 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200122 return 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200123}
124
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200125# undef PKCS12_MAX_PWDLEN
Manuel Pégourié-Gonnardd02a1da2015-09-28 18:34:48 +0200126
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200127int mbedtls_pkcs12_pbe(mbedtls_asn1_buf *pbe_params,
128 int mode,
129 mbedtls_cipher_type_t cipher_type,
130 mbedtls_md_type_t md_type,
131 const unsigned char *pwd,
132 size_t pwdlen,
133 const unsigned char *data,
134 size_t len,
135 unsigned char *output)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200136{
Paul Bakker38b50d72013-06-24 19:33:27 +0200137 int ret, keylen = 0;
138 unsigned char key[32];
139 unsigned char iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 const mbedtls_cipher_info_t *cipher_info;
141 mbedtls_cipher_context_t cipher_ctx;
Paul Bakker38b50d72013-06-24 19:33:27 +0200142 size_t olen = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200143
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200144 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
145 if (cipher_info == NULL)
146 return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
Paul Bakker38b50d72013-06-24 19:33:27 +0200147
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200148 keylen = cipher_info->key_bitlen / 8;
Paul Bakker38b50d72013-06-24 19:33:27 +0200149
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200150 if ((ret = pkcs12_pbe_derive_key_iv(pbe_params, md_type, pwd, pwdlen, key,
151 keylen, iv, cipher_info->iv_size)) !=
152 0) {
153 return ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200154 }
155
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200156 mbedtls_cipher_init(&cipher_ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200157
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200158 if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0)
Paul Bakkerbd552442013-07-03 14:44:40 +0200159 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200160
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200161 if ((ret = mbedtls_cipher_setkey(&cipher_ctx, key, 8 * keylen,
162 (mbedtls_operation_t)mode)) != 0)
Paul Bakkerbd552442013-07-03 14:44:40 +0200163 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200164
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200165 if ((ret = mbedtls_cipher_set_iv(&cipher_ctx, iv, cipher_info->iv_size)) !=
166 0)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200167 goto exit;
168
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200169 if ((ret = mbedtls_cipher_reset(&cipher_ctx)) != 0)
Paul Bakkerbd552442013-07-03 14:44:40 +0200170 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200171
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200172 if ((ret = mbedtls_cipher_update(&cipher_ctx, data, len, output, &olen)) !=
173 0) {
Paul Bakkerbd552442013-07-03 14:44:40 +0200174 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200175 }
176
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200177 if ((ret = mbedtls_cipher_finish(&cipher_ctx, output + olen, &olen)) != 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200179
Paul Bakkerbd552442013-07-03 14:44:40 +0200180exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200181 mbedtls_platform_zeroize(key, sizeof(key));
182 mbedtls_platform_zeroize(iv, sizeof(iv));
183 mbedtls_cipher_free(&cipher_ctx);
Paul Bakkerbd552442013-07-03 14:44:40 +0200184
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200185 return ret;
Paul Bakker531e2942013-06-24 19:23:12 +0200186}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200187
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200188# endif /* MBEDTLS_ASN1_PARSE_C */
Hanno Becker8a89f9f2018-10-12 10:46:32 +0100189
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200190static void pkcs12_fill_buffer(unsigned char *data,
191 size_t data_len,
192 const unsigned char *filler,
193 size_t fill_len)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200194{
195 unsigned char *p = data;
196 size_t use_len;
197
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200198 while (data_len > 0) {
199 use_len = (data_len > fill_len) ? fill_len : data_len;
200 memcpy(p, filler, use_len);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200201 p += use_len;
202 data_len -= use_len;
203 }
204}
205
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200206int mbedtls_pkcs12_derivation(unsigned char *data,
207 size_t datalen,
208 const unsigned char *pwd,
209 size_t pwdlen,
210 const unsigned char *salt,
211 size_t saltlen,
212 mbedtls_md_type_t md_type,
213 int id,
214 int iterations)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200215{
Janos Follath24eed8d2019-11-22 13:21:35 +0000216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200217 unsigned int j;
218
219 unsigned char diversifier[128];
220 unsigned char salt_block[128], pwd_block[128], hash_block[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200222 unsigned char *p;
223 unsigned char c;
224
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200225 size_t hlen, use_len, v, i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 const mbedtls_md_info_t *md_info;
228 mbedtls_md_context_t md_ctx;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200229
230 // This version only allows max of 64 bytes of password or salt
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200231 if (datalen > 128 || pwdlen > 64 || saltlen > 64)
232 return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200233
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200234 md_info = mbedtls_md_info_from_type(md_type);
235 if (md_info == NULL)
236 return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200237
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200238 mbedtls_md_init(&md_ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200239
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200240 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0)
241 return ret;
242 hlen = mbedtls_md_get_size(md_info);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200243
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200244 if (hlen <= 32)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200245 v = 64;
246 else
247 v = 128;
248
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200249 memset(diversifier, (unsigned char)id, v);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200250
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200251 pkcs12_fill_buffer(salt_block, v, salt, saltlen);
252 pkcs12_fill_buffer(pwd_block, v, pwd, pwdlen);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200253
254 p = data;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200255 while (datalen > 0) {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200256 // Calculate hash( diversifier || salt_block || pwd_block )
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200257 if ((ret = mbedtls_md_starts(&md_ctx)) != 0)
Paul Bakkerbd552442013-07-03 14:44:40 +0200258 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200259
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200260 if ((ret = mbedtls_md_update(&md_ctx, diversifier, v)) != 0)
Paul Bakkerbd552442013-07-03 14:44:40 +0200261 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200262
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200263 if ((ret = mbedtls_md_update(&md_ctx, salt_block, v)) != 0)
Paul Bakkerbd552442013-07-03 14:44:40 +0200264 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200265
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200266 if ((ret = mbedtls_md_update(&md_ctx, pwd_block, v)) != 0)
Paul Bakkerbd552442013-07-03 14:44:40 +0200267 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200268
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200269 if ((ret = mbedtls_md_finish(&md_ctx, hash_output)) != 0)
Paul Bakkerbd552442013-07-03 14:44:40 +0200270 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200271
272 // Perform remaining ( iterations - 1 ) recursive hash calculations
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200273 for (i = 1; i < (size_t)iterations; i++) {
274 if ((ret = mbedtls_md(md_info, hash_output, hlen, hash_output)) !=
275 0)
Paul Bakkerbd552442013-07-03 14:44:40 +0200276 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200277 }
278
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200279 use_len = (datalen > hlen) ? hlen : datalen;
280 memcpy(p, hash_output, use_len);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200281 datalen -= use_len;
282 p += use_len;
283
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200284 if (datalen == 0)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200285 break;
286
287 // Concatenating copies of hash_output into hash_block (B)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200288 pkcs12_fill_buffer(hash_block, v, hash_output, hlen);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200289
290 // B += 1
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200291 for (i = v; i > 0; i--)
292 if (++hash_block[i - 1] != 0)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200293 break;
294
295 // salt_block += B
296 c = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200297 for (i = v; i > 0; i--) {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200298 j = salt_block[i - 1] + hash_block[i - 1] + c;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200299 c = (unsigned char)(j >> 8);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200300 salt_block[i - 1] = j & 0xFF;
301 }
302
303 // pwd_block += B
304 c = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200305 for (i = v; i > 0; i--) {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200306 j = pwd_block[i - 1] + hash_block[i - 1] + c;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200307 c = (unsigned char)(j >> 8);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200308 pwd_block[i - 1] = j & 0xFF;
309 }
310 }
311
Paul Bakkerbd552442013-07-03 14:44:40 +0200312 ret = 0;
313
314exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200315 mbedtls_platform_zeroize(salt_block, sizeof(salt_block));
316 mbedtls_platform_zeroize(pwd_block, sizeof(pwd_block));
317 mbedtls_platform_zeroize(hash_block, sizeof(hash_block));
318 mbedtls_platform_zeroize(hash_output, sizeof(hash_output));
Paul Bakker91c301a2014-06-18 13:59:38 +0200319
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200320 mbedtls_md_free(&md_ctx);
Paul Bakkerbd552442013-07-03 14:44:40 +0200321
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200322 return ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200323}
324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#endif /* MBEDTLS_PKCS12_C */