blob: b992dba22f649b444df0850783a9806722098755 [file] [log] [blame]
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02001/*
2 * PKCS#12 Personal Information Exchange Syntax
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02007 *
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The PKCS #12 Personal Information Exchange Syntax Standard v1.1
24 *
25 * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf
26 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn
27 */
28
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020030#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#else
32#include POLARSSL_CONFIG_FILE
33#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020034
35#if defined(POLARSSL_PKCS12_C)
36
37#include "polarssl/pkcs12.h"
38#include "polarssl/asn1.h"
Paul Bakker38b50d72013-06-24 19:33:27 +020039#include "polarssl/cipher.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020040
41#if defined(POLARSSL_ARC4_C)
42#include "polarssl/arc4.h"
43#endif
44
45#if defined(POLARSSL_DES_C)
46#include "polarssl/des.h"
47#endif
48
Paul Bakker91c301a2014-06-18 13:59:38 +020049/* Implementation that should never be optimized out by the compiler */
50static void polarssl_zeroize( void *v, size_t n ) {
51 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
52}
53
Paul Bakkerf8d018a2013-06-29 12:16:17 +020054static int pkcs12_parse_pbe_params( asn1_buf *params,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020055 asn1_buf *salt, int *iterations )
56{
57 int ret;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020058 unsigned char **p = &params->p;
59 const unsigned char *end = params->p + params->len;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020060
61 /*
62 * pkcs-12PbeParams ::= SEQUENCE {
63 * salt OCTET STRING,
64 * iterations INTEGER
65 * }
66 *
67 */
Paul Bakkerf8d018a2013-06-29 12:16:17 +020068 if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
69 return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT +
70 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020071
72 if( ( ret = asn1_get_tag( p, end, &salt->len, ASN1_OCTET_STRING ) ) != 0 )
73 return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
74
75 salt->p = *p;
76 *p += salt->len;
77
78 if( ( ret = asn1_get_int( p, end, iterations ) ) != 0 )
79 return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
80
81 if( *p != end )
82 return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT +
83 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
84
85 return( 0 );
86}
87
Paul Bakker38b50d72013-06-24 19:33:27 +020088static int pkcs12_pbe_derive_key_iv( asn1_buf *pbe_params, md_type_t md_type,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020089 const unsigned char *pwd, size_t pwdlen,
90 unsigned char *key, size_t keylen,
91 unsigned char *iv, size_t ivlen )
92{
93 int ret, iterations;
94 asn1_buf salt;
95 size_t i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020096 unsigned char unipwd[258];
97
Paul Bakker66d5d072014-06-17 16:39:18 +020098 memset( &salt, 0, sizeof(asn1_buf) );
99 memset( &unipwd, 0, sizeof(unipwd) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200100
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200101 if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
102 &iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200103 return( ret );
104
Paul Bakker66d5d072014-06-17 16:39:18 +0200105 for( i = 0; i < pwdlen; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200106 unipwd[i * 2 + 1] = pwd[i];
107
108 if( ( ret = pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200109 salt.p, salt.len, md_type,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200110 PKCS12_DERIVE_KEY, iterations ) ) != 0 )
111 {
112 return( ret );
113 }
114
115 if( iv == NULL || ivlen == 0 )
116 return( 0 );
117
118 if( ( ret = pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200119 salt.p, salt.len, md_type,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200120 PKCS12_DERIVE_IV, iterations ) ) != 0 )
121 {
122 return( ret );
123 }
124 return( 0 );
125}
126
127int pkcs12_pbe_sha1_rc4_128( asn1_buf *pbe_params, int mode,
128 const unsigned char *pwd, size_t pwdlen,
129 const unsigned char *data, size_t len,
130 unsigned char *output )
131{
132#if !defined(POLARSSL_ARC4_C)
133 ((void) pbe_params);
134 ((void) mode);
135 ((void) pwd);
136 ((void) pwdlen);
137 ((void) data);
138 ((void) len);
139 ((void) output);
140 return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE );
141#else
142 int ret;
143 unsigned char key[16];
144 arc4_context ctx;
145 ((void) mode);
146
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200147 arc4_init( &ctx );
148
Paul Bakker38b50d72013-06-24 19:33:27 +0200149 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, POLARSSL_MD_SHA1,
150 pwd, pwdlen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200151 key, 16, NULL, 0 ) ) != 0 )
152 {
153 return( ret );
154 }
155
156 arc4_setup( &ctx, key, 16 );
157 if( ( ret = arc4_crypt( &ctx, len, data, output ) ) != 0 )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200158 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200159
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200160exit:
161 polarssl_zeroize( key, sizeof( key ) );
162 arc4_free( &ctx );
163
164 return( ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200165#endif /* POLARSSL_ARC4_C */
Paul Bakker531e2942013-06-24 19:23:12 +0200166}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200167
Paul Bakker38b50d72013-06-24 19:33:27 +0200168int pkcs12_pbe( asn1_buf *pbe_params, int mode,
169 cipher_type_t cipher_type, md_type_t md_type,
170 const unsigned char *pwd, size_t pwdlen,
171 const unsigned char *data, size_t len,
172 unsigned char *output )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200173{
Paul Bakker38b50d72013-06-24 19:33:27 +0200174 int ret, keylen = 0;
175 unsigned char key[32];
176 unsigned char iv[16];
177 const cipher_info_t *cipher_info;
178 cipher_context_t cipher_ctx;
179 size_t olen = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200180
Paul Bakker38b50d72013-06-24 19:33:27 +0200181 cipher_info = cipher_info_from_type( cipher_type );
182 if( cipher_info == NULL )
183 return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE );
184
185 keylen = cipher_info->key_length / 8;
186
187 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
188 key, keylen,
189 iv, cipher_info->iv_size ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200190 {
191 return( ret );
192 }
193
Paul Bakker84bbeb52014-07-01 14:53:22 +0200194 cipher_init( &cipher_ctx );
195
Paul Bakker38b50d72013-06-24 19:33:27 +0200196 if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200197 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200198
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200199 if( ( ret = cipher_setkey( &cipher_ctx, key, 8 * keylen, mode ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200200 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200201
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200202 if( ( ret = cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200203 goto exit;
204
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200205 if( ( ret = cipher_reset( &cipher_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200206 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200207
Paul Bakker38b50d72013-06-24 19:33:27 +0200208 if( ( ret = cipher_update( &cipher_ctx, data, len,
209 output, &olen ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200210 {
Paul Bakkerbd552442013-07-03 14:44:40 +0200211 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200212 }
213
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200214 if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200215 ret = POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200216
Paul Bakkerbd552442013-07-03 14:44:40 +0200217exit:
Paul Bakker91c301a2014-06-18 13:59:38 +0200218 polarssl_zeroize( key, sizeof( key ) );
219 polarssl_zeroize( iv, sizeof( iv ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200220 cipher_free( &cipher_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200221
222 return( ret );
Paul Bakker531e2942013-06-24 19:23:12 +0200223}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200224
225static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
226 const unsigned char *filler, size_t fill_len )
227{
228 unsigned char *p = data;
229 size_t use_len;
230
231 while( data_len > 0 )
232 {
233 use_len = ( data_len > fill_len ) ? fill_len : data_len;
234 memcpy( p, filler, use_len );
235 p += use_len;
236 data_len -= use_len;
237 }
238}
239
240int pkcs12_derivation( unsigned char *data, size_t datalen,
241 const unsigned char *pwd, size_t pwdlen,
242 const unsigned char *salt, size_t saltlen,
243 md_type_t md_type, int id, int iterations )
244{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200245 int ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200246 unsigned int j;
247
248 unsigned char diversifier[128];
249 unsigned char salt_block[128], pwd_block[128], hash_block[128];
250 unsigned char hash_output[POLARSSL_MD_MAX_SIZE];
251 unsigned char *p;
252 unsigned char c;
253
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200254 size_t hlen, use_len, v, i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200255
256 const md_info_t *md_info;
257 md_context_t md_ctx;
258
259 // This version only allows max of 64 bytes of password or salt
260 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
261 return( POLARSSL_ERR_PKCS12_BAD_INPUT_DATA );
262
263 md_info = md_info_from_type( md_type );
264 if( md_info == NULL )
265 return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE );
266
Paul Bakker84bbeb52014-07-01 14:53:22 +0200267 md_init( &md_ctx );
268
Paul Bakker66d5d072014-06-17 16:39:18 +0200269 if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200270 return( ret );
271 hlen = md_get_size( md_info );
272
273 if( hlen <= 32 )
274 v = 64;
275 else
276 v = 128;
277
278 memset( diversifier, (unsigned char) id, v );
279
280 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
281 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
282
283 p = data;
284 while( datalen > 0 )
285 {
286 // Calculate hash( diversifier || salt_block || pwd_block )
287 if( ( ret = md_starts( &md_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200288 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200289
290 if( ( ret = md_update( &md_ctx, diversifier, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200291 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200292
293 if( ( ret = md_update( &md_ctx, salt_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200294 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200295
296 if( ( ret = md_update( &md_ctx, pwd_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200297 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200298
299 if( ( ret = md_finish( &md_ctx, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200300 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200301
302 // Perform remaining ( iterations - 1 ) recursive hash calculations
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200303 for( i = 1; i < (size_t) iterations; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200304 {
305 if( ( ret = md( md_info, hash_output, hlen, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200306 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200307 }
308
309 use_len = ( datalen > hlen ) ? hlen : datalen;
310 memcpy( p, hash_output, use_len );
311 datalen -= use_len;
312 p += use_len;
313
314 if( datalen == 0 )
315 break;
316
317 // Concatenating copies of hash_output into hash_block (B)
318 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
319
320 // B += 1
321 for( i = v; i > 0; i-- )
322 if( ++hash_block[i - 1] != 0 )
323 break;
324
325 // salt_block += B
326 c = 0;
327 for( i = v; i > 0; i-- )
328 {
329 j = salt_block[i - 1] + hash_block[i - 1] + c;
330 c = (unsigned char) (j >> 8);
331 salt_block[i - 1] = j & 0xFF;
332 }
333
334 // pwd_block += B
335 c = 0;
336 for( i = v; i > 0; i-- )
337 {
338 j = pwd_block[i - 1] + hash_block[i - 1] + c;
339 c = (unsigned char) (j >> 8);
340 pwd_block[i - 1] = j & 0xFF;
341 }
342 }
343
Paul Bakkerbd552442013-07-03 14:44:40 +0200344 ret = 0;
345
346exit:
Paul Bakker91c301a2014-06-18 13:59:38 +0200347 polarssl_zeroize( salt_block, sizeof( salt_block ) );
348 polarssl_zeroize( pwd_block, sizeof( pwd_block ) );
349 polarssl_zeroize( hash_block, sizeof( hash_block ) );
350 polarssl_zeroize( hash_output, sizeof( hash_output ) );
351
Paul Bakker84bbeb52014-07-01 14:53:22 +0200352 md_free( &md_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200353
354 return( ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200355}
356
357#endif /* POLARSSL_PKCS12_C */