32 #if !defined(POLARSSL_CONFIG_FILE)
35 #include POLARSSL_CONFIG_FILE
38 #if defined(POLARSSL_PKCS12_C)
44 #if defined(POLARSSL_ARC4_C)
48 #if defined(POLARSSL_DES_C)
53 static void polarssl_zeroize(
void *v,
size_t n ) {
54 volatile unsigned char *p = v;
while( n-- ) *p++ = 0;
57 static int pkcs12_parse_pbe_params(
asn1_buf *params,
61 unsigned char **p = ¶ms->
p;
62 const unsigned char *end = params->
p + params->
len;
92 const unsigned char *pwd,
size_t pwdlen,
93 unsigned char *key,
size_t keylen,
94 unsigned char *iv,
size_t ivlen )
99 unsigned char unipwd[258];
101 memset( &salt, 0,
sizeof(
asn1_buf) );
102 memset( &unipwd, 0,
sizeof(unipwd) );
104 if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
105 &iterations ) ) != 0 )
108 for( i = 0; i < pwdlen; i++ )
109 unipwd[i * 2 + 1] = pwd[i];
112 salt.
p, salt.
len, md_type,
118 if( iv == NULL || ivlen == 0 )
122 salt.
p, salt.
len, md_type,
131 const unsigned char *pwd,
size_t pwdlen,
132 const unsigned char *data,
size_t len,
133 unsigned char *output )
135 #if !defined(POLARSSL_ARC4_C)
146 unsigned char key[16];
154 key, 16, NULL, 0 ) ) != 0 )
160 if( ( ret =
arc4_crypt( &ctx, len, data, output ) ) != 0 )
164 polarssl_zeroize( key,
sizeof( key ) );
173 const unsigned char *pwd,
size_t pwdlen,
174 const unsigned char *data,
size_t len,
175 unsigned char *output )
178 unsigned char key[32];
179 unsigned char iv[16];
185 if( cipher_info == NULL )
190 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
192 iv, cipher_info->
iv_size ) ) != 0 )
202 if( ( ret =
cipher_setkey( &cipher_ctx, key, 8 * keylen, mode ) ) != 0 )
212 output, &olen ) ) != 0 )
217 if( ( ret =
cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
221 polarssl_zeroize( key,
sizeof( key ) );
222 polarssl_zeroize( iv,
sizeof( iv ) );
228 static void pkcs12_fill_buffer(
unsigned char *data,
size_t data_len,
229 const unsigned char *filler,
size_t fill_len )
231 unsigned char *p = data;
234 while( data_len > 0 )
236 use_len = ( data_len > fill_len ) ? fill_len : data_len;
237 memcpy( p, filler, use_len );
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 )
251 unsigned char diversifier[128];
252 unsigned char salt_block[128], pwd_block[128], hash_block[128];
257 size_t hlen, use_len, v, i;
263 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
267 if( md_info == NULL )
272 if( ( ret =
md_init_ctx( &md_ctx, md_info ) ) != 0 )
281 memset( diversifier, (
unsigned char)
id, v );
283 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
284 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
290 if( ( ret =
md_starts( &md_ctx ) ) != 0 )
293 if( ( ret =
md_update( &md_ctx, diversifier, v ) ) != 0 )
296 if( ( ret =
md_update( &md_ctx, salt_block, v ) ) != 0 )
299 if( ( ret =
md_update( &md_ctx, pwd_block, v ) ) != 0 )
302 if( ( ret =
md_finish( &md_ctx, hash_output ) ) != 0 )
306 for( i = 1; i < (size_t) iterations; i++ )
308 if( ( ret =
md( md_info, hash_output, hlen, hash_output ) ) != 0 )
312 use_len = ( datalen > hlen ) ? hlen : datalen;
313 memcpy( p, hash_output, use_len );
321 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
324 for( i = v; i > 0; i-- )
325 if( ++hash_block[i - 1] != 0 )
330 for( i = v; i > 0; i-- )
332 j = salt_block[i - 1] + hash_block[i - 1] + c;
333 c = (
unsigned char) (j >> 8);
334 salt_block[i - 1] = j & 0xFF;
339 for( i = v; i > 0; i-- )
341 j = pwd_block[i - 1] + hash_block[i - 1] + c;
342 c = (
unsigned char) (j >> 8);
343 pwd_block[i - 1] = j & 0xFF;
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 ) );
#define POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE
Feature not available, e.g.
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.
int arc4_crypt(arc4_context *ctx, size_t length, const unsigned char *input, unsigned char *output)
ARC4 cipher function.
void arc4_init(arc4_context *ctx)
Initialize ARC4 context.
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.
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's...
void cipher_init(cipher_context_t *ctx)
Initialize a cipher_context (as NONE)
int md_starts(md_context_t *ctx)
Set-up the given context for a new message digest.
void md_init(md_context_t *ctx)
Initialize a md_context (as NONE)
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.
Configuration options (set of defines)
static unsigned char md_get_size(const md_info_t *md_info)
Returns the size of the message digest output.
#define POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT
PBE ASN.1 data not as expected.
void arc4_free(arc4_context *ctx)
Clear ARC4 context.
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
void md_free(md_context_t *ctx)
Free and clear the message-specific context of ctx.
unsigned int key_length
Cipher key length, in bits (default length for variable sized ciphers) (Includes parity bits for ciph...
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.
#define PKCS12_DERIVE_IV
initialization vector
int cipher_reset(cipher_context_t *ctx)
Finish preparation of the given context.
void cipher_free(cipher_context_t *ctx)
Free and clear the cipher-specific context of ctx.
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.
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.
#define POLARSSL_MD_MAX_SIZE
int asn1_get_tag(unsigned char **p, const unsigned char *end, size_t *len, int tag)
Get the tag and length of the tag.
#define PKCS12_DERIVE_KEY
encryption/decryption key
#define ASN1_OCTET_STRING
int md_finish(md_context_t *ctx, unsigned char *output)
Generic message digest final digest.
The ARCFOUR stream cipher.
Message digest information.
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.
#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG
ASN1 tag was of an unexpected value.
unsigned int iv_size
IV/NONCE size, in bytes.
PKCS#12 Personal Information Exchange Syntax.
Generic message digest context.