PolarSSL v1.3.8
pkcs12.c
Go to the documentation of this file.
1 /*
2  * PKCS#12 Personal Information Exchange Syntax
3  *
4  * Copyright (C) 2006-2014, Brainspark B.V.
5  *
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 
32 #if !defined(POLARSSL_CONFIG_FILE)
33 #include "polarssl/config.h"
34 #else
35 #include POLARSSL_CONFIG_FILE
36 #endif
37 
38 #if defined(POLARSSL_PKCS12_C)
39 
40 #include "polarssl/pkcs12.h"
41 #include "polarssl/asn1.h"
42 #include "polarssl/cipher.h"
43 
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 
52 /* Implementation that should never be optimized out by the compiler */
53 static void polarssl_zeroize( void *v, size_t n ) {
54  volatile unsigned char *p = v; while( n-- ) *p++ = 0;
55 }
56 
57 static int pkcs12_parse_pbe_params( asn1_buf *params,
58  asn1_buf *salt, int *iterations )
59 {
60  int ret;
61  unsigned char **p = &params->p;
62  const unsigned char *end = params->p + params->len;
63 
64  /*
65  * pkcs-12PbeParams ::= SEQUENCE {
66  * salt OCTET STRING,
67  * iterations INTEGER
68  * }
69  *
70  */
71  if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
74 
75  if( ( ret = asn1_get_tag( p, end, &salt->len, ASN1_OCTET_STRING ) ) != 0 )
77 
78  salt->p = *p;
79  *p += salt->len;
80 
81  if( ( ret = asn1_get_int( p, end, iterations ) ) != 0 )
83 
84  if( *p != end )
87 
88  return( 0 );
89 }
90 
91 static int pkcs12_pbe_derive_key_iv( asn1_buf *pbe_params, md_type_t md_type,
92  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;
99  unsigned char unipwd[258];
100 
101  memset( &salt, 0, sizeof(asn1_buf) );
102  memset( &unipwd, 0, sizeof(unipwd) );
103 
104  if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
105  &iterations ) ) != 0 )
106  return( ret );
107 
108  for( i = 0; i < pwdlen; i++ )
109  unipwd[i * 2 + 1] = pwd[i];
110 
111  if( ( ret = pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
112  salt.p, salt.len, md_type,
113  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,
122  salt.p, salt.len, md_type,
123  PKCS12_DERIVE_IV, iterations ) ) != 0 )
124  {
125  return( ret );
126  }
127  return( 0 );
128 }
129 
130 int 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);
144 #else
145  int ret;
146  unsigned char key[16];
147  arc4_context ctx;
148  ((void) mode);
149 
150  arc4_init( &ctx );
151 
152  if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, POLARSSL_MD_SHA1,
153  pwd, pwdlen,
154  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 )
161  goto exit;
162 
163 exit:
164  polarssl_zeroize( key, sizeof( key ) );
165  arc4_free( &ctx );
166 
167  return( ret );
168 #endif /* POLARSSL_ARC4_C */
169 }
170 
171 int 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 )
176 {
177  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;
183 
184  cipher_info = cipher_info_from_type( cipher_type );
185  if( cipher_info == NULL )
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 )
193  {
194  return( ret );
195  }
196 
197  cipher_init( &cipher_ctx );
198 
199  if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 )
200  goto exit;
201 
202  if( ( ret = cipher_setkey( &cipher_ctx, key, 8 * keylen, mode ) ) != 0 )
203  goto exit;
204 
205  if( ( ret = cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
206  goto exit;
207 
208  if( ( ret = cipher_reset( &cipher_ctx ) ) != 0 )
209  goto exit;
210 
211  if( ( ret = cipher_update( &cipher_ctx, data, len,
212  output, &olen ) ) != 0 )
213  {
214  goto exit;
215  }
216 
217  if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
219 
220 exit:
221  polarssl_zeroize( key, sizeof( key ) );
222  polarssl_zeroize( iv, sizeof( iv ) );
223  cipher_free( &cipher_ctx );
224 
225  return( ret );
226 }
227 
228 static 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 
243 int 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 {
248  int ret;
249  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 
257  size_t hlen, use_len, v, i;
258 
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 )
265 
266  md_info = md_info_from_type( md_type );
267  if( md_info == NULL )
269 
270  md_init( &md_ctx );
271 
272  if( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
273  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 )
291  goto exit;
292 
293  if( ( ret = md_update( &md_ctx, diversifier, v ) ) != 0 )
294  goto exit;
295 
296  if( ( ret = md_update( &md_ctx, salt_block, v ) ) != 0 )
297  goto exit;
298 
299  if( ( ret = md_update( &md_ctx, pwd_block, v ) ) != 0 )
300  goto exit;
301 
302  if( ( ret = md_finish( &md_ctx, hash_output ) ) != 0 )
303  goto exit;
304 
305  // Perform remaining ( iterations - 1 ) recursive hash calculations
306  for( i = 1; i < (size_t) iterations; i++ )
307  {
308  if( ( ret = md( md_info, hash_output, hlen, hash_output ) ) != 0 )
309  goto exit;
310  }
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 
347  ret = 0;
348 
349 exit:
350  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 
355  md_free( &md_ctx );
356 
357  return( ret );
358 }
359 
360 #endif /* POLARSSL_PKCS12_C */