34 #if defined(POLARSSL_PKCS12_C)
40 #if defined(POLARSSL_ARC4_C)
44 #if defined(POLARSSL_DES_C)
48 static int pkcs12_parse_pbe_params(
asn1_buf *params,
52 unsigned char **p = ¶ms->
p;
53 const unsigned char *end = params->
p + params->
len;
83 const unsigned char *pwd,
size_t pwdlen,
84 unsigned char *key,
size_t keylen,
85 unsigned char *iv,
size_t ivlen )
90 unsigned char unipwd[258];
93 memset(&unipwd, 0,
sizeof(unipwd));
95 if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt, &iterations ) ) != 0 )
98 for(i = 0; i < pwdlen; i++)
99 unipwd[i * 2 + 1] = pwd[i];
102 salt.
p, salt.
len, md_type,
108 if( iv == NULL || ivlen == 0 )
112 salt.
p, salt.
len, md_type,
121 const unsigned char *pwd,
size_t pwdlen,
122 const unsigned char *data,
size_t len,
123 unsigned char *output )
125 #if !defined(POLARSSL_ARC4_C)
136 unsigned char key[16];
142 key, 16, NULL, 0 ) ) != 0 )
148 if( ( ret =
arc4_crypt( &ctx, len, data, output ) ) != 0 )
157 const unsigned char *pwd,
size_t pwdlen,
158 const unsigned char *data,
size_t len,
159 unsigned char *output )
162 unsigned char key[32];
163 unsigned char iv[16];
169 if( cipher_info == NULL )
174 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
176 iv, cipher_info->
iv_size ) ) != 0 )
184 if( ( ret =
cipher_setkey( &cipher_ctx, key, 8 * keylen, mode ) ) != 0 )
194 output, &olen ) ) != 0 )
199 if( ( ret =
cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
208 static void pkcs12_fill_buffer(
unsigned char *data,
size_t data_len,
209 const unsigned char *filler,
size_t fill_len )
211 unsigned char *p = data;
214 while( data_len > 0 )
216 use_len = ( data_len > fill_len ) ? fill_len : data_len;
217 memcpy( p, filler, use_len );
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 )
231 unsigned char diversifier[128];
232 unsigned char salt_block[128], pwd_block[128], hash_block[128];
237 size_t hlen, use_len, v, i;
243 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
247 if( md_info == NULL )
250 if ( ( ret =
md_init_ctx( &md_ctx, md_info ) ) != 0 )
259 memset( diversifier, (
unsigned char)
id, v );
261 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
262 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
268 if( ( ret =
md_starts( &md_ctx ) ) != 0 )
271 if( ( ret =
md_update( &md_ctx, diversifier, v ) ) != 0 )
274 if( ( ret =
md_update( &md_ctx, salt_block, v ) ) != 0 )
277 if( ( ret =
md_update( &md_ctx, pwd_block, v ) ) != 0 )
280 if( ( ret =
md_finish( &md_ctx, hash_output ) ) != 0 )
284 for( i = 1; i < (size_t) iterations; i++ )
286 if( ( ret =
md( md_info, hash_output, hlen, hash_output ) ) != 0 )
290 use_len = ( datalen > hlen ) ? hlen : datalen;
291 memcpy( p, hash_output, use_len );
299 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
302 for( i = v; i > 0; i-- )
303 if( ++hash_block[i - 1] != 0 )
308 for( i = v; i > 0; i-- )
310 j = salt_block[i - 1] + hash_block[i - 1] + c;
311 c = (
unsigned char) (j >> 8);
312 salt_block[i - 1] = j & 0xFF;
317 for( i = v; i > 0; i-- )
319 j = pwd_block[i - 1] + hash_block[i - 1] + c;
320 c = (
unsigned char) (j >> 8);
321 pwd_block[i - 1] = j & 0xFF;
#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_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...
int md_starts(md_context_t *ctx)
Set-up the given context for a new message digest.
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.
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
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...
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.
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.
int md_free_ctx(md_context_t *ctx)
Free the message-specific context of ctx.
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.