blob: 0cf2edf1029841f9fdaf7c42c36582887818c510 [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 Bakker84bbeb52014-07-01 14:53:22 +0200197 cipher_init( &cipher_ctx );
198
Paul Bakker38b50d72013-06-24 19:33:27 +0200199 if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200200 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200201
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200202 if( ( ret = cipher_setkey( &cipher_ctx, key, 8 * keylen, mode ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200203 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200204
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200205 if( ( ret = cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200206 goto exit;
207
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200208 if( ( ret = cipher_reset( &cipher_ctx ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200209 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200210
Paul Bakker38b50d72013-06-24 19:33:27 +0200211 if( ( ret = cipher_update( &cipher_ctx, data, len,
212 output, &olen ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200213 {
Paul Bakkerbd552442013-07-03 14:44:40 +0200214 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200215 }
216
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200217 if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200218 ret = POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200219
Paul Bakkerbd552442013-07-03 14:44:40 +0200220exit:
Paul Bakker91c301a2014-06-18 13:59:38 +0200221 polarssl_zeroize( key, sizeof( key ) );
222 polarssl_zeroize( iv, sizeof( iv ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200223 cipher_free( &cipher_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200224
225 return( ret );
Paul Bakker531e2942013-06-24 19:23:12 +0200226}
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200227
228static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
229 const unsigned char *filler, size_t fill_len )
230{
231 unsigned char *p = data;
232 size_t use_len;
233
234 while( data_len > 0 )
235 {
236 use_len = ( data_len > fill_len ) ? fill_len : data_len;
237 memcpy( p, filler, use_len );
238 p += use_len;
239 data_len -= use_len;
240 }
241}
242
243int pkcs12_derivation( unsigned char *data, size_t datalen,
244 const unsigned char *pwd, size_t pwdlen,
245 const unsigned char *salt, size_t saltlen,
246 md_type_t md_type, int id, int iterations )
247{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200248 int ret;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200249 unsigned int j;
250
251 unsigned char diversifier[128];
252 unsigned char salt_block[128], pwd_block[128], hash_block[128];
253 unsigned char hash_output[POLARSSL_MD_MAX_SIZE];
254 unsigned char *p;
255 unsigned char c;
256
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200257 size_t hlen, use_len, v, i;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200258
259 const md_info_t *md_info;
260 md_context_t md_ctx;
261
262 // This version only allows max of 64 bytes of password or salt
263 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
264 return( POLARSSL_ERR_PKCS12_BAD_INPUT_DATA );
265
266 md_info = md_info_from_type( md_type );
267 if( md_info == NULL )
268 return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE );
269
Paul Bakker84bbeb52014-07-01 14:53:22 +0200270 md_init( &md_ctx );
271
Paul Bakker66d5d072014-06-17 16:39:18 +0200272 if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200273 return( ret );
274 hlen = md_get_size( md_info );
275
276 if( hlen <= 32 )
277 v = 64;
278 else
279 v = 128;
280
281 memset( diversifier, (unsigned char) id, v );
282
283 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
284 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
285
286 p = data;
287 while( datalen > 0 )
288 {
289 // Calculate hash( diversifier || salt_block || pwd_block )
290 if( ( ret = md_starts( &md_ctx ) ) != 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, diversifier, 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, salt_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_update( &md_ctx, pwd_block, v ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200300 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200301
302 if( ( ret = md_finish( &md_ctx, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200303 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200304
305 // Perform remaining ( iterations - 1 ) recursive hash calculations
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200306 for( i = 1; i < (size_t) iterations; i++ )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200307 {
308 if( ( ret = md( md_info, hash_output, hlen, hash_output ) ) != 0 )
Paul Bakkerbd552442013-07-03 14:44:40 +0200309 goto exit;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200310 }
311
312 use_len = ( datalen > hlen ) ? hlen : datalen;
313 memcpy( p, hash_output, use_len );
314 datalen -= use_len;
315 p += use_len;
316
317 if( datalen == 0 )
318 break;
319
320 // Concatenating copies of hash_output into hash_block (B)
321 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
322
323 // B += 1
324 for( i = v; i > 0; i-- )
325 if( ++hash_block[i - 1] != 0 )
326 break;
327
328 // salt_block += B
329 c = 0;
330 for( i = v; i > 0; i-- )
331 {
332 j = salt_block[i - 1] + hash_block[i - 1] + c;
333 c = (unsigned char) (j >> 8);
334 salt_block[i - 1] = j & 0xFF;
335 }
336
337 // pwd_block += B
338 c = 0;
339 for( i = v; i > 0; i-- )
340 {
341 j = pwd_block[i - 1] + hash_block[i - 1] + c;
342 c = (unsigned char) (j >> 8);
343 pwd_block[i - 1] = j & 0xFF;
344 }
345 }
346
Paul Bakkerbd552442013-07-03 14:44:40 +0200347 ret = 0;
348
349exit:
Paul Bakker91c301a2014-06-18 13:59:38 +0200350 polarssl_zeroize( salt_block, sizeof( salt_block ) );
351 polarssl_zeroize( pwd_block, sizeof( pwd_block ) );
352 polarssl_zeroize( hash_block, sizeof( hash_block ) );
353 polarssl_zeroize( hash_output, sizeof( hash_output ) );
354
Paul Bakker84bbeb52014-07-01 14:53:22 +0200355 md_free( &md_ctx );
Paul Bakkerbd552442013-07-03 14:44:40 +0200356
357 return( ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +0200358}
359
360#endif /* POLARSSL_PKCS12_C */