• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  PKCS#12 Personal Information Exchange Syntax
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  */
7 /*
8  *  The PKCS #12 Personal Information Exchange Syntax Standard v1.1
9  *
10  *  http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf
11  *  ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn
12  */
13 
14 #include "common.h"
15 
16 #if defined(MBEDTLS_PKCS12_C)
17 
18 #include "mbedtls/pkcs12.h"
19 #include "mbedtls/asn1.h"
20 #include "mbedtls/cipher.h"
21 #include "mbedtls/platform_util.h"
22 #include "mbedtls/error.h"
23 
24 #include <string.h>
25 
26 #if defined(MBEDTLS_ARC4_C)
27 #include "mbedtls/arc4.h"
28 #endif
29 
30 #if defined(MBEDTLS_DES_C)
31 #include "mbedtls/des.h"
32 #endif
33 
34 #if defined(MBEDTLS_ASN1_PARSE_C)
35 
pkcs12_parse_pbe_params(mbedtls_asn1_buf * params,mbedtls_asn1_buf * salt,int * iterations)36 static int pkcs12_parse_pbe_params(mbedtls_asn1_buf *params,
37                                    mbedtls_asn1_buf *salt, int *iterations)
38 {
39     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
40     unsigned char **p = &params->p;
41     const unsigned char *end = params->p + params->len;
42 
43     /*
44      *  pkcs-12PbeParams ::= SEQUENCE {
45      *    salt          OCTET STRING,
46      *    iterations    INTEGER
47      *  }
48      *
49      */
50     if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
51         return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
52                                  MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
53     }
54 
55     if ((ret = mbedtls_asn1_get_tag(p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
56         return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
57     }
58 
59     salt->p = *p;
60     *p += salt->len;
61 
62     if ((ret = mbedtls_asn1_get_int(p, end, iterations)) != 0) {
63         return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
64     }
65 
66     if (*p != end) {
67         return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
68                                  MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
69     }
70 
71     return 0;
72 }
73 
74 #define PKCS12_MAX_PWDLEN 128
75 
pkcs12_pbe_derive_key_iv(mbedtls_asn1_buf * pbe_params,mbedtls_md_type_t md_type,const unsigned char * pwd,size_t pwdlen,unsigned char * key,size_t keylen,unsigned char * iv,size_t ivlen)76 static int pkcs12_pbe_derive_key_iv(mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
77                                     const unsigned char *pwd,  size_t pwdlen,
78                                     unsigned char *key, size_t keylen,
79                                     unsigned char *iv,  size_t ivlen)
80 {
81     int ret, iterations = 0;
82     mbedtls_asn1_buf salt;
83     size_t i;
84     unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
85 
86     if (pwdlen > PKCS12_MAX_PWDLEN) {
87         return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
88     }
89 
90     memset(&salt, 0, sizeof(mbedtls_asn1_buf));
91     memset(&unipwd, 0, sizeof(unipwd));
92 
93     if ((ret = pkcs12_parse_pbe_params(pbe_params, &salt,
94                                        &iterations)) != 0) {
95         return ret;
96     }
97 
98     for (i = 0; i < pwdlen; i++) {
99         unipwd[i * 2 + 1] = pwd[i];
100     }
101 
102     if ((ret = mbedtls_pkcs12_derivation(key, keylen, unipwd, pwdlen * 2 + 2,
103                                          salt.p, salt.len, md_type,
104                                          MBEDTLS_PKCS12_DERIVE_KEY, iterations)) != 0) {
105         return ret;
106     }
107 
108     if (iv == NULL || ivlen == 0) {
109         return 0;
110     }
111 
112     if ((ret = mbedtls_pkcs12_derivation(iv, ivlen, unipwd, pwdlen * 2 + 2,
113                                          salt.p, salt.len, md_type,
114                                          MBEDTLS_PKCS12_DERIVE_IV, iterations)) != 0) {
115         return ret;
116     }
117     return 0;
118 }
119 
120 #undef PKCS12_MAX_PWDLEN
121 
mbedtls_pkcs12_pbe_sha1_rc4_128(mbedtls_asn1_buf * pbe_params,int mode,const unsigned char * pwd,size_t pwdlen,const unsigned char * data,size_t len,unsigned char * output)122 int mbedtls_pkcs12_pbe_sha1_rc4_128(mbedtls_asn1_buf *pbe_params, int mode,
123                                     const unsigned char *pwd,  size_t pwdlen,
124                                     const unsigned char *data, size_t len,
125                                     unsigned char *output)
126 {
127 #if !defined(MBEDTLS_ARC4_C)
128     ((void) pbe_params);
129     ((void) mode);
130     ((void) pwd);
131     ((void) pwdlen);
132     ((void) data);
133     ((void) len);
134     ((void) output);
135     return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
136 #else
137     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
138     unsigned char key[16];
139     mbedtls_arc4_context ctx;
140     ((void) mode);
141 
142     mbedtls_arc4_init(&ctx);
143 
144     if ((ret = pkcs12_pbe_derive_key_iv(pbe_params, MBEDTLS_MD_SHA1,
145                                         pwd, pwdlen,
146                                         key, 16, NULL, 0)) != 0) {
147         return ret;
148     }
149 
150     mbedtls_arc4_setup(&ctx, key, 16);
151     if ((ret = mbedtls_arc4_crypt(&ctx, len, data, output)) != 0) {
152         goto exit;
153     }
154 
155 exit:
156     mbedtls_platform_zeroize(key, sizeof(key));
157     mbedtls_arc4_free(&ctx);
158 
159     return ret;
160 #endif /* MBEDTLS_ARC4_C */
161 }
162 
163 #if !defined(MBEDTLS_CIPHER_PADDING_PKCS7)
164 int mbedtls_pkcs12_pbe_ext(mbedtls_asn1_buf *pbe_params, int mode,
165                            mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
166                            const unsigned char *pwd,  size_t pwdlen,
167                            const unsigned char *data, size_t len,
168                            unsigned char *output, size_t output_size,
169                            size_t *output_len);
170 #endif
171 
mbedtls_pkcs12_pbe(mbedtls_asn1_buf * pbe_params,int mode,mbedtls_cipher_type_t cipher_type,mbedtls_md_type_t md_type,const unsigned char * pwd,size_t pwdlen,const unsigned char * data,size_t len,unsigned char * output)172 int mbedtls_pkcs12_pbe(mbedtls_asn1_buf *pbe_params, int mode,
173                        mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
174                        const unsigned char *pwd,  size_t pwdlen,
175                        const unsigned char *data, size_t len,
176                        unsigned char *output)
177 {
178     size_t output_len = 0;
179 
180     /* We assume caller of the function is providing a big enough output buffer
181      * so we pass output_size as SIZE_MAX to pass checks, However, no guarantees
182      * for the output size actually being correct.
183      */
184     return mbedtls_pkcs12_pbe_ext(pbe_params, mode, cipher_type, md_type,
185                                   pwd, pwdlen, data, len, output, SIZE_MAX,
186                                   &output_len);
187 }
188 
mbedtls_pkcs12_pbe_ext(mbedtls_asn1_buf * pbe_params,int mode,mbedtls_cipher_type_t cipher_type,mbedtls_md_type_t md_type,const unsigned char * pwd,size_t pwdlen,const unsigned char * data,size_t len,unsigned char * output,size_t output_size,size_t * output_len)189 int mbedtls_pkcs12_pbe_ext(mbedtls_asn1_buf *pbe_params, int mode,
190                            mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
191                            const unsigned char *pwd,  size_t pwdlen,
192                            const unsigned char *data, size_t len,
193                            unsigned char *output, size_t output_size,
194                            size_t *output_len)
195 {
196     int ret, keylen = 0;
197     unsigned char key[32];
198     unsigned char iv[16];
199     const mbedtls_cipher_info_t *cipher_info;
200     mbedtls_cipher_context_t cipher_ctx;
201     size_t finish_olen = 0;
202     unsigned int padlen = 0;
203 
204     if (pwd == NULL && pwdlen != 0) {
205         return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
206     }
207 
208     cipher_info = mbedtls_cipher_info_from_type(cipher_type);
209     if (cipher_info == NULL) {
210         return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
211     }
212 
213     keylen = cipher_info->key_bitlen / 8;
214 
215     if (mode == MBEDTLS_PKCS12_PBE_DECRYPT) {
216         if (output_size < len) {
217             return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
218         }
219     }
220 
221     if (mode == MBEDTLS_PKCS12_PBE_ENCRYPT) {
222         padlen = cipher_info->block_size - (len % cipher_info->block_size);
223         if (output_size < (len + padlen)) {
224             return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
225         }
226     }
227 
228     if ((ret = pkcs12_pbe_derive_key_iv(pbe_params, md_type, pwd, pwdlen,
229                                         key, keylen,
230                                         iv, cipher_info->iv_size)) != 0) {
231         return ret;
232     }
233 
234     mbedtls_cipher_init(&cipher_ctx);
235 
236     if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
237         goto exit;
238     }
239 
240     if ((ret =
241              mbedtls_cipher_setkey(&cipher_ctx, key, 8 * keylen,
242                                    (mbedtls_operation_t) mode)) != 0) {
243         goto exit;
244     }
245 
246 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
247     /* PKCS12 uses CBC with PKCS7 padding */
248 
249     mbedtls_cipher_padding_t padding = MBEDTLS_PADDING_PKCS7;
250 #if !defined(MBEDTLS_CIPHER_PADDING_PKCS7)
251     /* For historical reasons, when decrypting, this function works when
252      * decrypting even when support for PKCS7 padding is disabled. In this
253      * case, it ignores the padding, and so will never report a
254      * password mismatch.
255      */
256     if (mode == MBEDTLS_PKCS12_PBE_DECRYPT) {
257         padding = MBEDTLS_PADDING_NONE;
258     }
259 #endif
260     if ((ret = mbedtls_cipher_set_padding_mode(&cipher_ctx, padding)) != 0) {
261         goto exit;
262     }
263 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
264 
265     if ((ret = mbedtls_cipher_set_iv(&cipher_ctx, iv, cipher_info->iv_size)) != 0) {
266         goto exit;
267     }
268 
269     if ((ret = mbedtls_cipher_reset(&cipher_ctx)) != 0) {
270         goto exit;
271     }
272 
273     if ((ret = mbedtls_cipher_update(&cipher_ctx, data, len,
274                                      output, output_len)) != 0) {
275         goto exit;
276     }
277 
278     if ((ret = mbedtls_cipher_finish(&cipher_ctx, output + (*output_len), &finish_olen)) != 0) {
279         ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
280     }
281 
282     *output_len += finish_olen;
283 
284 exit:
285     mbedtls_platform_zeroize(key, sizeof(key));
286     mbedtls_platform_zeroize(iv,  sizeof(iv));
287     mbedtls_cipher_free(&cipher_ctx);
288 
289     return ret;
290 }
291 
292 #endif /* MBEDTLS_ASN1_PARSE_C */
293 
pkcs12_fill_buffer(unsigned char * data,size_t data_len,const unsigned char * filler,size_t fill_len)294 static void pkcs12_fill_buffer(unsigned char *data, size_t data_len,
295                                const unsigned char *filler, size_t fill_len)
296 {
297     unsigned char *p = data;
298     size_t use_len;
299 
300     if (filler != NULL && fill_len != 0) {
301         while (data_len > 0) {
302             use_len = (data_len > fill_len) ? fill_len : data_len;
303             memcpy(p, filler, use_len);
304             p += use_len;
305             data_len -= use_len;
306         }
307     } else {
308         /* If either of the above are not true then clearly there is nothing
309          * that this function can do. The function should *not* be called
310          * under either of those circumstances, as you could end up with an
311          * incorrect output but for safety's sake, leaving the check in as
312          * otherwise we could end up with memory corruption.*/
313     }
314 }
315 
mbedtls_pkcs12_derivation(unsigned char * data,size_t datalen,const unsigned char * pwd,size_t pwdlen,const unsigned char * salt,size_t saltlen,mbedtls_md_type_t md_type,int id,int iterations)316 int mbedtls_pkcs12_derivation(unsigned char *data, size_t datalen,
317                               const unsigned char *pwd, size_t pwdlen,
318                               const unsigned char *salt, size_t saltlen,
319                               mbedtls_md_type_t md_type, int id, int iterations)
320 {
321     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
322     unsigned int j;
323 
324     unsigned char diversifier[128];
325     unsigned char salt_block[128], pwd_block[128], hash_block[128];
326     unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
327     unsigned char *p;
328     unsigned char c;
329     int           use_password = 0;
330     int           use_salt = 0;
331 
332     size_t hlen, use_len, v, i;
333 
334     const mbedtls_md_info_t *md_info;
335     mbedtls_md_context_t md_ctx;
336 
337     // This version only allows max of 64 bytes of password or salt
338     if (datalen > 128 || pwdlen > 64 || saltlen > 64) {
339         return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
340     }
341 
342     if (pwd == NULL && pwdlen != 0) {
343         return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
344     }
345 
346     if (salt == NULL && saltlen != 0) {
347         return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
348     }
349 
350     use_password = (pwd && pwdlen != 0);
351     use_salt = (salt && saltlen != 0);
352 
353     md_info = mbedtls_md_info_from_type(md_type);
354     if (md_info == NULL) {
355         return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
356     }
357 
358     mbedtls_md_init(&md_ctx);
359 
360     if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
361         return ret;
362     }
363     hlen = mbedtls_md_get_size(md_info);
364 
365     if (hlen <= 32) {
366         v = 64;
367     } else {
368         v = 128;
369     }
370 
371     memset(diversifier, (unsigned char) id, v);
372 
373     if (use_salt != 0) {
374         pkcs12_fill_buffer(salt_block, v, salt, saltlen);
375     }
376 
377     if (use_password != 0) {
378         pkcs12_fill_buffer(pwd_block,  v, pwd,  pwdlen);
379     }
380 
381     p = data;
382     while (datalen > 0) {
383         // Calculate hash( diversifier || salt_block || pwd_block )
384         if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
385             goto exit;
386         }
387 
388         if ((ret = mbedtls_md_update(&md_ctx, diversifier, v)) != 0) {
389             goto exit;
390         }
391 
392         if (use_salt != 0) {
393             if ((ret = mbedtls_md_update(&md_ctx, salt_block, v)) != 0) {
394                 goto exit;
395             }
396         }
397 
398         if (use_password != 0) {
399             if ((ret = mbedtls_md_update(&md_ctx, pwd_block, v)) != 0) {
400                 goto exit;
401             }
402         }
403 
404         if ((ret = mbedtls_md_finish(&md_ctx, hash_output)) != 0) {
405             goto exit;
406         }
407 
408         // Perform remaining ( iterations - 1 ) recursive hash calculations
409         for (i = 1; i < (size_t) iterations; i++) {
410             if ((ret = mbedtls_md(md_info, hash_output, hlen, hash_output)) != 0) {
411                 goto exit;
412             }
413         }
414 
415         use_len = (datalen > hlen) ? hlen : datalen;
416         memcpy(p, hash_output, use_len);
417         datalen -= use_len;
418         p += use_len;
419 
420         if (datalen == 0) {
421             break;
422         }
423 
424         // Concatenating copies of hash_output into hash_block (B)
425         pkcs12_fill_buffer(hash_block, v, hash_output, hlen);
426 
427         // B += 1
428         for (i = v; i > 0; i--) {
429             if (++hash_block[i - 1] != 0) {
430                 break;
431             }
432         }
433 
434         if (use_salt != 0) {
435             // salt_block += B
436             c = 0;
437             for (i = v; i > 0; i--) {
438                 j = salt_block[i - 1] + hash_block[i - 1] + c;
439                 c = MBEDTLS_BYTE_1(j);
440                 salt_block[i - 1] = MBEDTLS_BYTE_0(j);
441             }
442         }
443 
444         if (use_password != 0) {
445             // pwd_block  += B
446             c = 0;
447             for (i = v; i > 0; i--) {
448                 j = pwd_block[i - 1] + hash_block[i - 1] + c;
449                 c = MBEDTLS_BYTE_1(j);
450                 pwd_block[i - 1] = MBEDTLS_BYTE_0(j);
451             }
452         }
453     }
454 
455     ret = 0;
456 
457 exit:
458     mbedtls_platform_zeroize(salt_block, sizeof(salt_block));
459     mbedtls_platform_zeroize(pwd_block, sizeof(pwd_block));
460     mbedtls_platform_zeroize(hash_block, sizeof(hash_block));
461     mbedtls_platform_zeroize(hash_output, sizeof(hash_output));
462 
463     mbedtls_md_free(&md_ctx);
464 
465     return ret;
466 }
467 
468 #endif /* MBEDTLS_PKCS12_C */
469