blob: d97e4a126ddebdc0501c345968b7de53ed9fc576 [file] [log] [blame]
Paul Bakkercf6e95d2013-06-12 13:18:15 +02001/*
2 * PKCS#12 Personal Information Exchange Syntax
3 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkercf6e95d2013-06-12 13:18:15 +02005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkercf6e95d2013-06-12 13:18:15 +02007 *
8 * 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
29#include "polarssl/config.h"
30
31#if defined(POLARSSL_PKCS12_C)
32
33#include "polarssl/pkcs12.h"
34#include "polarssl/asn1.h"
Paul Bakker14a222c2013-06-18 16:35:48 +020035#include "polarssl/cipher.h"
Paul Bakkercf6e95d2013-06-12 13:18:15 +020036
37#if defined(POLARSSL_ARC4_C)
38#include "polarssl/arc4.h"
39#endif
40
41#if defined(POLARSSL_DES_C)
42#include "polarssl/des.h"
43#endif
44
45static int pkcs12_parse_pbe_params( unsigned char **p,
46 const unsigned char *end,
47 asn1_buf *salt, int *iterations )
48{
49 int ret;
50 size_t len = 0;
51
52 /*
53 * pkcs-12PbeParams ::= SEQUENCE {
54 * salt OCTET STRING,
55 * iterations INTEGER
56 * }
57 *
58 */
59 if( ( ret = asn1_get_tag( p, end, &len,
60 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
61 {
62 return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
63 }
64
65 end = *p + len;
66
67 if( ( ret = asn1_get_tag( p, end, &salt->len, ASN1_OCTET_STRING ) ) != 0 )
68 return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
69
70 salt->p = *p;
71 *p += salt->len;
72
73 if( ( ret = asn1_get_int( p, end, iterations ) ) != 0 )
74 return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
75
76 if( *p != end )
77 return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT +
78 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
79
80 return( 0 );
81}
82
Paul Bakker14a222c2013-06-18 16:35:48 +020083static int pkcs12_pbe_derive_key_iv( asn1_buf *pbe_params, md_type_t md_type,
Paul Bakkercf6e95d2013-06-12 13:18:15 +020084 const unsigned char *pwd, size_t pwdlen,
85 unsigned char *key, size_t keylen,
86 unsigned char *iv, size_t ivlen )
87{
88 int ret, iterations;
89 asn1_buf salt;
90 size_t i;
91 unsigned char *p, *end;
92 unsigned char unipwd[258];
93
94 memset(&salt, 0, sizeof(asn1_buf));
95 memset(&unipwd, 0, sizeof(unipwd));
96
97 p = pbe_params->p;
98 end = p + pbe_params->len;
99
100 if( ( ret = pkcs12_parse_pbe_params( &p, end, &salt, &iterations ) ) != 0 )
101 return( ret );
102
103 for(i = 0; i < pwdlen; i++)
104 unipwd[i * 2 + 1] = pwd[i];
105
106 if( ( ret = pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
Paul Bakker14a222c2013-06-18 16:35:48 +0200107 salt.p, salt.len, md_type,
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200108 PKCS12_DERIVE_KEY, iterations ) ) != 0 )
109 {
110 return( ret );
111 }
112
113 if( iv == NULL || ivlen == 0 )
114 return( 0 );
115
116 if( ( ret = pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
Paul Bakker14a222c2013-06-18 16:35:48 +0200117 salt.p, salt.len, md_type,
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200118 PKCS12_DERIVE_IV, iterations ) ) != 0 )
119 {
120 return( ret );
121 }
122 return( 0 );
123}
124
125int pkcs12_pbe_sha1_rc4_128( asn1_buf *pbe_params, int mode,
126 const unsigned char *pwd, size_t pwdlen,
127 const unsigned char *data, size_t len,
128 unsigned char *output )
129{
130#if !defined(POLARSSL_ARC4_C)
131 ((void) pbe_params);
132 ((void) mode);
133 ((void) pwd);
134 ((void) pwdlen);
135 ((void) data);
136 ((void) len);
137 ((void) output);
138 return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE );
139#else
140 int ret;
141 unsigned char key[16];
142 arc4_context ctx;
143 ((void) mode);
144
Paul Bakker14a222c2013-06-18 16:35:48 +0200145 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, POLARSSL_MD_SHA1,
146 pwd, pwdlen,
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200147 key, 16, NULL, 0 ) ) != 0 )
148 {
149 return( ret );
150 }
151
152 arc4_setup( &ctx, key, 16 );
153 if( ( ret = arc4_crypt( &ctx, len, data, output ) ) != 0 )
154 return( ret );
155
156 return( 0 );
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200157#endif /* POLARSSL_ARC4_C */
Paul Bakker67812d32013-06-14 11:35:09 +0200158}
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200159
Paul Bakker14a222c2013-06-18 16:35:48 +0200160int pkcs12_pbe( asn1_buf *pbe_params, int mode,
161 cipher_type_t cipher_type, md_type_t md_type,
162 const unsigned char *pwd, size_t pwdlen,
163 const unsigned char *data, size_t len,
164 unsigned char *output )
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200165{
Paul Bakker14a222c2013-06-18 16:35:48 +0200166 int ret, keylen = 0;
167 unsigned char key[32];
168 unsigned char iv[16];
169 const cipher_info_t *cipher_info;
170 cipher_context_t cipher_ctx;
171 size_t olen = 0;
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200172
Paul Bakker14a222c2013-06-18 16:35:48 +0200173 cipher_info = cipher_info_from_type( cipher_type );
174 if( cipher_info == NULL )
175 return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE );
176
177 keylen = cipher_info->key_length / 8;
178
179 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
180 key, keylen,
181 iv, cipher_info->iv_size ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200182 {
183 return( ret );
184 }
185
Paul Bakker14a222c2013-06-18 16:35:48 +0200186 if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 )
187 return( ret );
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200188
Paul Bakker14a222c2013-06-18 16:35:48 +0200189 if( ( ret = cipher_setkey( &cipher_ctx, key, keylen, mode ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100190 goto cleanup;
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200191
Paul Bakker14a222c2013-06-18 16:35:48 +0200192 if( ( ret = cipher_reset( &cipher_ctx, iv ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100193 goto cleanup;
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200194
Paul Bakker14a222c2013-06-18 16:35:48 +0200195 if( ( ret = cipher_update( &cipher_ctx, data, len,
196 output, &olen ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200197 {
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100198 goto cleanup;
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200199 }
200
Paul Bakker14a222c2013-06-18 16:35:48 +0200201 if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100202 {
203 ret = POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH;
204 goto cleanup;
205 }
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200206
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100207cleanup:
208 cipher_free_ctx( &cipher_ctx );
209
210 return( ret );
Paul Bakker67812d32013-06-14 11:35:09 +0200211}
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200212
213static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
214 const unsigned char *filler, size_t fill_len )
215{
216 unsigned char *p = data;
217 size_t use_len;
218
219 while( data_len > 0 )
220 {
221 use_len = ( data_len > fill_len ) ? fill_len : data_len;
222 memcpy( p, filler, use_len );
223 p += use_len;
224 data_len -= use_len;
225 }
226}
227
228int pkcs12_derivation( unsigned char *data, size_t datalen,
229 const unsigned char *pwd, size_t pwdlen,
230 const unsigned char *salt, size_t saltlen,
231 md_type_t md_type, int id, int iterations )
232{
233 int ret, i;
234 unsigned int j;
235
236 unsigned char diversifier[128];
237 unsigned char salt_block[128], pwd_block[128], hash_block[128];
238 unsigned char hash_output[POLARSSL_MD_MAX_SIZE];
239 unsigned char *p;
240 unsigned char c;
241
242 size_t hlen, use_len, v;
243
244 const md_info_t *md_info;
245 md_context_t md_ctx;
246
247 // This version only allows max of 64 bytes of password or salt
248 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
249 return( POLARSSL_ERR_PKCS12_BAD_INPUT_DATA );
250
251 md_info = md_info_from_type( md_type );
252 if( md_info == NULL )
253 return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE );
254
255 if ( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
256 return( ret );
257 hlen = md_get_size( md_info );
258
259 if( hlen <= 32 )
260 v = 64;
261 else
262 v = 128;
263
264 memset( diversifier, (unsigned char) id, v );
265
266 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
267 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
268
269 p = data;
270 while( datalen > 0 )
271 {
272 // Calculate hash( diversifier || salt_block || pwd_block )
273 if( ( ret = md_starts( &md_ctx ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100274 goto cleanup;
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200275
276 if( ( ret = md_update( &md_ctx, diversifier, v ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100277 goto cleanup;
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200278
279 if( ( ret = md_update( &md_ctx, salt_block, v ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100280 goto cleanup;
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200281
282 if( ( ret = md_update( &md_ctx, pwd_block, v ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100283 goto cleanup;
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200284
285 if( ( ret = md_finish( &md_ctx, hash_output ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100286 goto cleanup;
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200287
288 // Perform remaining ( iterations - 1 ) recursive hash calculations
289 for( i = 1; i < iterations; i++ )
290 {
291 if( ( ret = md( md_info, hash_output, hlen, hash_output ) ) != 0 )
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100292 goto cleanup;
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200293 }
294
295 use_len = ( datalen > hlen ) ? hlen : datalen;
296 memcpy( p, hash_output, use_len );
297 datalen -= use_len;
298 p += use_len;
299
300 if( datalen == 0 )
301 break;
302
303 // Concatenating copies of hash_output into hash_block (B)
304 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
305
306 // B += 1
307 for( i = v; i > 0; i-- )
308 if( ++hash_block[i - 1] != 0 )
309 break;
310
311 // salt_block += B
312 c = 0;
313 for( i = v; i > 0; i-- )
314 {
315 j = salt_block[i - 1] + hash_block[i - 1] + c;
316 c = (unsigned char) (j >> 8);
317 salt_block[i - 1] = j & 0xFF;
318 }
319
320 // pwd_block += B
321 c = 0;
322 for( i = v; i > 0; i-- )
323 {
324 j = pwd_block[i - 1] + hash_block[i - 1] + c;
325 c = (unsigned char) (j >> 8);
326 pwd_block[i - 1] = j & 0xFF;
327 }
328 }
329
Manuel Pégourié-Gonnard1c022a62014-11-17 12:24:41 +0100330cleanup:
331 md_free_ctx( &md_ctx );
332
333 return( ret );
Paul Bakkercf6e95d2013-06-12 13:18:15 +0200334}
335
336#endif /* POLARSSL_PKCS12_C */