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)
52 static int pkcs12_parse_pbe_params(
asn1_buf *params,
56 unsigned char **p = ¶ms->
p;
57 const unsigned char *end = params->
p + params->
len;
87 const unsigned char *pwd,
size_t pwdlen,
88 unsigned char *key,
size_t keylen,
89 unsigned char *iv,
size_t ivlen )
94 unsigned char unipwd[258];
97 memset(&unipwd, 0,
sizeof(unipwd));
99 if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
100 &iterations ) ) != 0 )
103 for(i = 0; i < pwdlen; i++)
104 unipwd[i * 2 + 1] = pwd[i];
107 salt.
p, salt.
len, md_type,
113 if( iv == NULL || ivlen == 0 )
117 salt.
p, salt.
len, md_type,
126 const unsigned char *pwd,
size_t pwdlen,
127 const unsigned char *data,
size_t len,
128 unsigned char *output )
130 #if !defined(POLARSSL_ARC4_C)
141 unsigned char key[16];
147 key, 16, NULL, 0 ) ) != 0 )
153 if( ( ret =
arc4_crypt( &ctx, len, data, output ) ) != 0 )
162 const unsigned char *pwd,
size_t pwdlen,
163 const unsigned char *data,
size_t len,
164 unsigned char *output )
167 unsigned char key[32];
168 unsigned char iv[16];
174 if( cipher_info == NULL )
179 if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
181 iv, cipher_info->
iv_size ) ) != 0 )
189 if( ( ret =
cipher_setkey( &cipher_ctx, key, 8 * keylen, mode ) ) != 0 )
199 output, &olen ) ) != 0 )
204 if( ( ret =
cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
213 static void pkcs12_fill_buffer(
unsigned char *data,
size_t data_len,
214 const unsigned char *filler,
size_t fill_len )
216 unsigned char *p = data;
219 while( data_len > 0 )
221 use_len = ( data_len > fill_len ) ? fill_len : data_len;
222 memcpy( p, filler, use_len );
229 const unsigned char *pwd,
size_t pwdlen,
230 const unsigned char *salt,
size_t saltlen,
231 md_type_t md_type,
int id,
int iterations )
236 unsigned char diversifier[128];
237 unsigned char salt_block[128], pwd_block[128], hash_block[128];
242 size_t hlen, use_len, v, i;
248 if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
252 if( md_info == NULL )
255 if ( ( ret =
md_init_ctx( &md_ctx, md_info ) ) != 0 )
264 memset( diversifier, (
unsigned char)
id, v );
266 pkcs12_fill_buffer( salt_block, v, salt, saltlen );
267 pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
273 if( ( ret =
md_starts( &md_ctx ) ) != 0 )
276 if( ( ret =
md_update( &md_ctx, diversifier, v ) ) != 0 )
279 if( ( ret =
md_update( &md_ctx, salt_block, v ) ) != 0 )
282 if( ( ret =
md_update( &md_ctx, pwd_block, v ) ) != 0 )
285 if( ( ret =
md_finish( &md_ctx, hash_output ) ) != 0 )
289 for( i = 1; i < (size_t) iterations; i++ )
291 if( ( ret =
md( md_info, hash_output, hlen, hash_output ) ) != 0 )
295 use_len = ( datalen > hlen ) ? hlen : datalen;
296 memcpy( p, hash_output, use_len );
304 pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
307 for( i = v; i > 0; i-- )
308 if( ++hash_block[i - 1] != 0 )
313 for( i = v; i > 0; i-- )
315 j = salt_block[i - 1] + hash_block[i - 1] + c;
316 c = (
unsigned char) (j >> 8);
317 salt_block[i - 1] = j & 0xFF;
322 for( i = v; i > 0; i-- )
324 j = pwd_block[i - 1] + hash_block[i - 1] + c;
325 c = (
unsigned char) (j >> 8);
326 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.