PolarSSL v1.3.3
pkcs12.c
Go to the documentation of this file.
1 /*
2  * PKCS#12 Personal Information Exchange Syntax
3  *
4  * Copyright (C) 2006-2013, 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 #include "polarssl/config.h"
33 
34 #if defined(POLARSSL_PKCS12_C)
35 
36 #include "polarssl/pkcs12.h"
37 #include "polarssl/asn1.h"
38 #include "polarssl/cipher.h"
39 
40 #if defined(POLARSSL_ARC4_C)
41 #include "polarssl/arc4.h"
42 #endif
43 
44 #if defined(POLARSSL_DES_C)
45 #include "polarssl/des.h"
46 #endif
47 
48 static int pkcs12_parse_pbe_params( asn1_buf *params,
49  asn1_buf *salt, int *iterations )
50 {
51  int ret;
52  unsigned char **p = &params->p;
53  const unsigned char *end = params->p + params->len;
54 
55  /*
56  * pkcs-12PbeParams ::= SEQUENCE {
57  * salt OCTET STRING,
58  * iterations INTEGER
59  * }
60  *
61  */
62  if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
65 
66  if( ( ret = asn1_get_tag( p, end, &salt->len, ASN1_OCTET_STRING ) ) != 0 )
68 
69  salt->p = *p;
70  *p += salt->len;
71 
72  if( ( ret = asn1_get_int( p, end, iterations ) ) != 0 )
74 
75  if( *p != end )
78 
79  return( 0 );
80 }
81 
82 static int pkcs12_pbe_derive_key_iv( asn1_buf *pbe_params, md_type_t md_type,
83  const unsigned char *pwd, size_t pwdlen,
84  unsigned char *key, size_t keylen,
85  unsigned char *iv, size_t ivlen )
86 {
87  int ret, iterations;
88  asn1_buf salt;
89  size_t i;
90  unsigned char unipwd[258];
91 
92  memset(&salt, 0, sizeof(asn1_buf));
93  memset(&unipwd, 0, sizeof(unipwd));
94 
95  if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt, &iterations ) ) != 0 )
96  return( ret );
97 
98  for(i = 0; i < pwdlen; i++)
99  unipwd[i * 2 + 1] = pwd[i];
100 
101  if( ( ret = pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
102  salt.p, salt.len, md_type,
103  PKCS12_DERIVE_KEY, iterations ) ) != 0 )
104  {
105  return( ret );
106  }
107 
108  if( iv == NULL || ivlen == 0 )
109  return( 0 );
110 
111  if( ( ret = pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
112  salt.p, salt.len, md_type,
113  PKCS12_DERIVE_IV, iterations ) ) != 0 )
114  {
115  return( ret );
116  }
117  return( 0 );
118 }
119 
120 int pkcs12_pbe_sha1_rc4_128( asn1_buf *pbe_params, int mode,
121  const unsigned char *pwd, size_t pwdlen,
122  const unsigned char *data, size_t len,
123  unsigned char *output )
124 {
125 #if !defined(POLARSSL_ARC4_C)
126  ((void) pbe_params);
127  ((void) mode);
128  ((void) pwd);
129  ((void) pwdlen);
130  ((void) data);
131  ((void) len);
132  ((void) output);
134 #else
135  int ret;
136  unsigned char key[16];
137  arc4_context ctx;
138  ((void) mode);
139 
140  if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, POLARSSL_MD_SHA1,
141  pwd, pwdlen,
142  key, 16, NULL, 0 ) ) != 0 )
143  {
144  return( ret );
145  }
146 
147  arc4_setup( &ctx, key, 16 );
148  if( ( ret = arc4_crypt( &ctx, len, data, output ) ) != 0 )
149  return( ret );
150 
151  return( 0 );
152 #endif /* POLARSSL_ARC4_C */
153 }
154 
155 int pkcs12_pbe( asn1_buf *pbe_params, int mode,
156  cipher_type_t cipher_type, md_type_t md_type,
157  const unsigned char *pwd, size_t pwdlen,
158  const unsigned char *data, size_t len,
159  unsigned char *output )
160 {
161  int ret, keylen = 0;
162  unsigned char key[32];
163  unsigned char iv[16];
164  const cipher_info_t *cipher_info;
165  cipher_context_t cipher_ctx;
166  size_t olen = 0;
167 
168  cipher_info = cipher_info_from_type( cipher_type );
169  if( cipher_info == NULL )
171 
172  keylen = cipher_info->key_length / 8;
173 
174  if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
175  key, keylen,
176  iv, cipher_info->iv_size ) ) != 0 )
177  {
178  return( ret );
179  }
180 
181  if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 )
182  goto exit;
183 
184  if( ( ret = cipher_setkey( &cipher_ctx, key, 8 * keylen, mode ) ) != 0 )
185  goto exit;
186 
187  if( ( ret = cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
188  goto exit;
189 
190  if( ( ret = cipher_reset( &cipher_ctx ) ) != 0 )
191  goto exit;
192 
193  if( ( ret = cipher_update( &cipher_ctx, data, len,
194  output, &olen ) ) != 0 )
195  {
196  goto exit;
197  }
198 
199  if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
201 
202 exit:
203  cipher_free_ctx( &cipher_ctx );
204 
205  return( ret );
206 }
207 
208 static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
209  const unsigned char *filler, size_t fill_len )
210 {
211  unsigned char *p = data;
212  size_t use_len;
213 
214  while( data_len > 0 )
215  {
216  use_len = ( data_len > fill_len ) ? fill_len : data_len;
217  memcpy( p, filler, use_len );
218  p += use_len;
219  data_len -= use_len;
220  }
221 }
222 
223 int pkcs12_derivation( unsigned char *data, size_t datalen,
224  const unsigned char *pwd, size_t pwdlen,
225  const unsigned char *salt, size_t saltlen,
226  md_type_t md_type, int id, int iterations )
227 {
228  int ret;
229  unsigned int j;
230 
231  unsigned char diversifier[128];
232  unsigned char salt_block[128], pwd_block[128], hash_block[128];
233  unsigned char hash_output[POLARSSL_MD_MAX_SIZE];
234  unsigned char *p;
235  unsigned char c;
236 
237  size_t hlen, use_len, v, i;
238 
239  const md_info_t *md_info;
240  md_context_t md_ctx;
241 
242  // This version only allows max of 64 bytes of password or salt
243  if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
245 
246  md_info = md_info_from_type( md_type );
247  if( md_info == NULL )
249 
250  if ( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 )
251  return( ret );
252  hlen = md_get_size( md_info );
253 
254  if( hlen <= 32 )
255  v = 64;
256  else
257  v = 128;
258 
259  memset( diversifier, (unsigned char) id, v );
260 
261  pkcs12_fill_buffer( salt_block, v, salt, saltlen );
262  pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
263 
264  p = data;
265  while( datalen > 0 )
266  {
267  // Calculate hash( diversifier || salt_block || pwd_block )
268  if( ( ret = md_starts( &md_ctx ) ) != 0 )
269  goto exit;
270 
271  if( ( ret = md_update( &md_ctx, diversifier, v ) ) != 0 )
272  goto exit;
273 
274  if( ( ret = md_update( &md_ctx, salt_block, v ) ) != 0 )
275  goto exit;
276 
277  if( ( ret = md_update( &md_ctx, pwd_block, v ) ) != 0 )
278  goto exit;
279 
280  if( ( ret = md_finish( &md_ctx, hash_output ) ) != 0 )
281  goto exit;
282 
283  // Perform remaining ( iterations - 1 ) recursive hash calculations
284  for( i = 1; i < (size_t) iterations; i++ )
285  {
286  if( ( ret = md( md_info, hash_output, hlen, hash_output ) ) != 0 )
287  goto exit;
288  }
289 
290  use_len = ( datalen > hlen ) ? hlen : datalen;
291  memcpy( p, hash_output, use_len );
292  datalen -= use_len;
293  p += use_len;
294 
295  if( datalen == 0 )
296  break;
297 
298  // Concatenating copies of hash_output into hash_block (B)
299  pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
300 
301  // B += 1
302  for( i = v; i > 0; i-- )
303  if( ++hash_block[i - 1] != 0 )
304  break;
305 
306  // salt_block += B
307  c = 0;
308  for( i = v; i > 0; i-- )
309  {
310  j = salt_block[i - 1] + hash_block[i - 1] + c;
311  c = (unsigned char) (j >> 8);
312  salt_block[i - 1] = j & 0xFF;
313  }
314 
315  // pwd_block += B
316  c = 0;
317  for( i = v; i > 0; i-- )
318  {
319  j = pwd_block[i - 1] + hash_block[i - 1] + c;
320  c = (unsigned char) (j >> 8);
321  pwd_block[i - 1] = j & 0xFF;
322  }
323  }
324 
325  ret = 0;
326 
327 exit:
328  md_free_ctx( &md_ctx );
329 
330  return( ret );
331 }
332 
333 #endif /* POLARSSL_PKCS12_C */
#define POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE
Feature not available, e.g.
Definition: pkcs12.h:37
int md(const md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
Output = message_digest( input buffer )
int cipher_finish(cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
Generic cipher context.
Definition: cipher.h:239
int arc4_crypt(arc4_context *ctx, size_t length, const unsigned char *input, unsigned char *output)
ARC4 cipher function.
void arc4_setup(arc4_context *ctx, const unsigned char *key, unsigned int keylen)
ARC4 key schedule.
#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH
Actual length differs from expected length.
Definition: asn1.h:53
int pkcs12_pbe(asn1_buf *pbe_params, int mode, cipher_type_t cipher_type, md_type_t md_type, const unsigned char *pwd, size_t pwdlen, const unsigned char *input, size_t len, unsigned char *output)
PKCS12 Password Based function (encryption / decryption) for cipher-based and md-based PBE&#39;s...
int md_starts(md_context_t *ctx)
Set-up the given context for a new message digest.
Cipher information.
Definition: cipher.h:207
const cipher_info_t * cipher_info_from_type(const cipher_type_t cipher_type)
Returns the cipher information structure associated with the given cipher type.
int asn1_get_int(unsigned char **p, const unsigned char *end, int *val)
Retrieve an integer ASN.1 tag and its value.
int md_init_ctx(md_context_t *ctx, const md_info_t *md_info)
Initialises and fills the message digest context structure with the appropriate values.
#define POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH
Given private key password does not allow for correct decryption.
Definition: pkcs12.h:39
#define ASN1_SEQUENCE
Definition: asn1.h:78
Configuration options (set of defines)
#define ASN1_CONSTRUCTED
Definition: asn1.h:88
static unsigned char md_get_size(const md_info_t *md_info)
Returns the size of the message digest output.
Definition: md.h:205
#define POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT
PBE ASN.1 data not as expected.
Definition: pkcs12.h:38
md_type_t
Definition: md.h:51
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
Generic ASN.1 parsing.
cipher_type_t
Definition: cipher.h:75
int cipher_free_ctx(cipher_context_t *ctx)
Free the cipher-specific context of ctx.
unsigned int key_length
Cipher key length, in bits (default length for variable sized ciphers) (Includes parity bits for ciph...
Definition: cipher.h:216
int cipher_set_iv(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len)
Set the initialization vector (IV) or nonce.
int cipher_update(cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
Generic cipher update function.
int pkcs12_derivation(unsigned char *data, size_t datalen, const unsigned char *pwd, size_t pwdlen, const unsigned char *salt, size_t saltlen, md_type_t md, int id, int iterations)
The PKCS#12 derivation function uses a password and a salt to produce pseudo-random bits for a partic...
unsigned char * p
ASN1 data, e.g.
Definition: asn1.h:120
#define PKCS12_DERIVE_IV
initialization vector
Definition: pkcs12.h:42
Generic cipher wrapper.
int tag
ASN1 type, e.g.
Definition: asn1.h:118
int cipher_reset(cipher_context_t *ctx)
Finish preparation of the given context.
DES block cipher.
int cipher_init_ctx(cipher_context_t *ctx, const cipher_info_t *cipher_info)
Initialises and fills the cipher context structure with the appropriate values.
int cipher_setkey(cipher_context_t *ctx, const unsigned char *key, int key_length, const operation_t operation)
Set the key to use with the given context.
Type-length-value structure that allows for ASN1 using DER.
Definition: asn1.h:116
int pkcs12_pbe_sha1_rc4_128(asn1_buf *pbe_params, int mode, const unsigned char *pwd, size_t pwdlen, const unsigned char *input, size_t len, unsigned char *output)
PKCS12 Password Based function (encryption / decryption) for pbeWithSHAAnd128BitRC4.
size_t len
ASN1 length, e.g.
Definition: asn1.h:119
#define POLARSSL_MD_MAX_SIZE
Definition: md.h:66
int asn1_get_tag(unsigned char **p, const unsigned char *end, size_t *len, int tag)
Get the tag and length of the tag.
ARC4 context structure.
Definition: arc4.h:45
#define PKCS12_DERIVE_KEY
encryption/decryption key
Definition: pkcs12.h:41
#define ASN1_OCTET_STRING
Definition: asn1.h:74
int md_finish(md_context_t *ctx, unsigned char *output)
Generic message digest final digest.
int md_free_ctx(md_context_t *ctx)
Free the message-specific context of ctx.
The ARCFOUR stream cipher.
Message digest information.
Definition: md.h:73
int md_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic message digest process buffer.
#define POLARSSL_ERR_PKCS12_BAD_INPUT_DATA
Bad input parameters to function.
Definition: pkcs12.h:36
#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG
ASN1 tag was of an unexpected value.
Definition: asn1.h:51
unsigned int iv_size
IV/NONCE size, in bytes.
Definition: cipher.h:223
PKCS#12 Personal Information Exchange Syntax.
Generic message digest context.
Definition: md.h:129