blob: 027f84a82d8a36bc707db2faef0c66e395c0735e [file] [log] [blame]
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02001/*
2 * PKCS#12 Personal Information Exchange Syntax
3 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The PKCS #12 Personal Information Exchange Syntax Standard v1.1
27 *
28 * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf
29 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn
30 */
31
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020033#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
35#include POLARSSL_CONFIG_FILE
36#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020037
38#if defined(POLARSSL_PKCS12_C)
39
40#include "polarssl/pkcs12.h"
41#include "polarssl/asn1.h"
Paul Bakker38b50d72013-06-24 19:33:27 +020042#include "polarssl/cipher.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020043
44#if defined(POLARSSL_ARC4_C)
45#include "polarssl/arc4.h"
46#endif
47
48#if defined(POLARSSL_DES_C)
49#include "polarssl/des.h"
50#endif
51
Paul Bakker91c301a2014-06-18 13:59:38 +020052/* Implementation that should never be optimized out by the compiler */
53static void polarssl_zeroize( void *v, size_t n ) {
54 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
55}
56
Paul Bakkerf8d018a2013-06-29 12:16:17 +020057static int pkcs12_parse_pbe_params( asn1_buf *params,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020058 asn1_buf *salt, int *iterations )
59{
60 int ret;
Paul Bakkerf8d018a2013-06-29 12:16:17 +020061 unsigned char **p = &params->p;
62 const unsigned char *end = params->p + params->len;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020063
64 /*
65 * pkcs-12PbeParams ::= SEQUENCE {
66 * salt OCTET STRING,
67 * iterations INTEGER
68 * }
69 *
70 */
Paul Bakkerf8d018a2013-06-29 12:16:17 +020071 if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
72 return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT +
73 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020074
75 if( ( ret = asn1_get_tag( p, end, &salt->len, ASN1_OCTET_STRING ) ) != 0 )
76 return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
77
78 salt->p = *p;
79 *p += salt->len;
80
81 if( ( ret = asn1_get_int( p, end, iterations ) ) != 0 )
82 return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
83
84 if( *p != end )
85 return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT +
86 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
87
88 return( 0 );
89}
90
Paul Bakker38b50d72013-06-24 19:33:27 +020091static int pkcs12_pbe_derive_key_iv( asn1_buf *pbe_params, md_type_t md_type,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020092 const unsigned char *pwd, size_t pwdlen,
93 unsigned char *key, size_t keylen,
94 unsigned char *iv, size_t ivlen )
95{
96 int ret, iterations;
97 asn1_buf salt;
98 size_t i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020099 unsigned char unipwd[258];
100
Paul Bakker66d5d072014-06-17 16:39:18 +0200101 memset( &salt, 0, sizeof(asn1_buf) );
102 memset( &unipwd, 0, sizeof(unipwd) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200103
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200104 if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
105 &iterations ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200106 return( ret );
107
Paul Bakker66d5d072014-06-17 16:39:18 +0200108 for( i = 0; i < pwdlen; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200109 unipwd[i * 2 + 1] = pwd[i];
110
111 if( ( ret = pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200112 salt.p, salt.len, md_type,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200113 PKCS12_DERIVE_KEY, iterations ) ) != 0 )
114 {
115 return( ret );
116 }
117
118 if( iv == NULL || ivlen == 0 )
119 return( 0 );
120
121 if( ( ret = pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
Paul Bakker38b50d72013-06-24 19:33:27 +0200122 salt.p, salt.len, md_type,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200123 PKCS12_DERIVE_IV, iterations ) ) != 0 )
124 {
125 return( ret );
126 }
127 return( 0 );
128}
129
130int pkcs12_pbe_sha1_rc4_128( asn1_buf *pbe_params, int mode,
131 const unsigned char *pwd, size_t pwdlen,
132 const unsigned char *data, size_t len,
133 unsigned char *output )
134{
135#if !defined(POLARSSL_ARC4_C)
136 ((void) pbe_params);
137 ((void) mode);
138 ((void) pwd);
139 ((void) pwdlen);
140 ((void) data);
141 ((void) len);
142 ((void) output);
143 return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE );
144#else
145 int ret;
146 unsigned char key[16];
147 arc4_context ctx;
148 ((void) mode);
149
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200150 arc4_init( &ctx );
151
Paul Bakker38b50d72013-06-24 19:33:27 +0200152 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, POLARSSL_MD_SHA1,
153 pwd, pwdlen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200154 key, 16, NULL, 0 ) ) != 0 )
155 {
156 return( ret );
157 }
158
159 arc4_setup( &ctx, key, 16 );
160 if( ( ret = arc4_crypt( &ctx, len, data, output ) ) != 0 )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200161 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200162
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200163exit:
164 polarssl_zeroize( key, sizeof( key ) );
165 arc4_free( &ctx );
166
167 return( ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200168#endif /* POLARSSL_ARC4_C */
Paul Bakker531e2942013-06-24 19:23:12 +0200169}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200170
Paul Bakker38b50d72013-06-24 19:33:27 +0200171int pkcs12_pbe( asn1_buf *pbe_params, int mode,
172 cipher_type_t cipher_type, md_type_t md_type,
173 const unsigned char *pwd, size_t pwdlen,
174 const unsigned char *data, size_t len,
175 unsigned char *output )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200176{
Paul Bakker38b50d72013-06-24 19:33:27 +0200177 int ret, keylen = 0;
178 unsigned char key[32];
179 unsigned char iv[16];
180 const cipher_info_t *cipher_info;
181 cipher_context_t cipher_ctx;
182 size_t olen = 0;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200183
Paul Bakker38b50d72013-06-24 19:33:27 +0200184 cipher_info = cipher_info_from_type( cipher_type );
185 if( cipher_info == NULL )
186 return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE );
187
188 keylen = cipher_info->key_length / 8;
189
190 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
191 key, keylen,
192 iv, cipher_info->iv_size ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200193 {
194 return( ret );
195 }
196
Paul Bakker38b50d72013-06-24 19:33:27 +0200197 if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200198 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200199
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200200 if( ( ret = cipher_setkey( &cipher_ctx, key, 8 * keylen, mode ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200201 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200202
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200203 if( ( ret = cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200204 goto exit;
205
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200206 if( ( ret = cipher_reset( &cipher_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200207 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200208
Paul Bakker38b50d72013-06-24 19:33:27 +0200209 if( ( ret = cipher_update( &cipher_ctx, data, len,
210 output, &olen ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200211 {
Paul Bakkerbd552442013-07-03 14:44:40 +0200212 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200213 }
214
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200215 if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200216 ret = POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200217
Paul Bakkerbd552442013-07-03 14:44:40 +0200218exit:
Paul Bakker91c301a2014-06-18 13:59:38 +0200219 polarssl_zeroize( key, sizeof( key ) );
220 polarssl_zeroize( iv, sizeof( iv ) );
Paul Bakkerbd552442013-07-03 14:44:40 +0200221 cipher_free_ctx( &cipher_ctx );
222
223 return( ret );
Paul Bakker531e2942013-06-24 19:23:12 +0200224}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200225
226static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
227 const unsigned char *filler, size_t fill_len )
228{
229 unsigned char *p = data;
230 size_t use_len;
231
232 while( data_len > 0 )
233 {
234 use_len = ( data_len > fill_len ) ? fill_len : data_len;
235 memcpy( p, filler, use_len );
236 p += use_len;
237 data_len -= use_len;
238 }
239}
240
241int pkcs12_derivation( unsigned char *data, size_t datalen,
242 const unsigned char *pwd, size_t pwdlen,
243 const unsigned char *salt, size_t saltlen,
244 md_type_t md_type, int id, int iterations )
245{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200246 int ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200247 unsigned int j;
248
249 unsigned char diversifier[128];
250 unsigned char salt_block[128], pwd_block[128], hash_block[128];
251 unsigned char hash_output[POLARSSL_MD_MAX_SIZE];
252 unsigned char *p;
253 unsigned char c;
254
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200255 size_t hlen, use_len, v, i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200256
257 const md_info_t *md_info;
258 md_context_t md_ctx;
259
260 // This version only allows max of 64 bytes of password or salt
261 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
262 return( POLARSSL_ERR_PKCS12_BAD_INPUT_DATA );
263
264 md_info = md_info_from_type( md_type );
265 if( md_info == NULL )
266 return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE );
267
Paul Bakker66d5d072014-06-17 16:39:18 +0200268 if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200269 return( ret );
270 hlen = md_get_size( md_info );
271
272 if( hlen <= 32 )
273 v = 64;
274 else
275 v = 128;
276
277 memset( diversifier, (unsigned char) id, v );
278
279 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
280 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
281
282 p = data;
283 while( datalen > 0 )
284 {
285 // Calculate hash( diversifier || salt_block || pwd_block )
286 if( ( ret = md_starts( &md_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200287 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200288
289 if( ( ret = md_update( &md_ctx, diversifier, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200290 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200291
292 if( ( ret = md_update( &md_ctx, salt_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200293 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200294
295 if( ( ret = md_update( &md_ctx, pwd_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200296 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200297
298 if( ( ret = md_finish( &md_ctx, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200299 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200300
301 // Perform remaining ( iterations - 1 ) recursive hash calculations
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200302 for( i = 1; i < (size_t) iterations; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200303 {
304 if( ( ret = md( md_info, hash_output, hlen, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200305 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200306 }
307
308 use_len = ( datalen > hlen ) ? hlen : datalen;
309 memcpy( p, hash_output, use_len );
310 datalen -= use_len;
311 p += use_len;
312
313 if( datalen == 0 )
314 break;
315
316 // Concatenating copies of hash_output into hash_block (B)
317 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
318
319 // B += 1
320 for( i = v; i > 0; i-- )
321 if( ++hash_block[i - 1] != 0 )
322 break;
323
324 // salt_block += B
325 c = 0;
326 for( i = v; i > 0; i-- )
327 {
328 j = salt_block[i - 1] + hash_block[i - 1] + c;
329 c = (unsigned char) (j >> 8);
330 salt_block[i - 1] = j & 0xFF;
331 }
332
333 // pwd_block += B
334 c = 0;
335 for( i = v; i > 0; i-- )
336 {
337 j = pwd_block[i - 1] + hash_block[i - 1] + c;
338 c = (unsigned char) (j >> 8);
339 pwd_block[i - 1] = j & 0xFF;
340 }
341 }
342
Paul Bakkerbd552442013-07-03 14:44:40 +0200343 ret = 0;
344
345exit:
Paul Bakker91c301a2014-06-18 13:59:38 +0200346 polarssl_zeroize( salt_block, sizeof( salt_block ) );
347 polarssl_zeroize( pwd_block, sizeof( pwd_block ) );
348 polarssl_zeroize( hash_block, sizeof( hash_block ) );
349 polarssl_zeroize( hash_output, sizeof( hash_output ) );
350
Paul Bakkerbd552442013-07-03 14:44:40 +0200351 md_free_ctx( &md_ctx );
352
353 return( ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200354}
355
356#endif /* POLARSSL_PKCS12_C */