Moved PKCS#12 PBE functions to cipher / md layer where possible
The 3-key and 2-key Triple DES PBE functions have been replaced with a
single pkcs12_pbe() function that handles both situations (and more).
In addition this allows for some PASSWORD_MISMATCH checking
(cherry picked from commit 14a222cef2
)
This commit is contained in:
parent
0e34235644
commit
38b50d73a1
6 changed files with 103 additions and 103 deletions
|
@ -248,6 +248,8 @@ void error_strerror( int ret, char *buf, size_t buflen )
|
|||
snprintf( buf, buflen, "PKCS12 - Feature not available, e.g. unsupported encryption scheme" );
|
||||
if( use_ret == -(POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT) )
|
||||
snprintf( buf, buflen, "PKCS12 - PBE ASN.1 data not as expected" );
|
||||
if( use_ret == -(POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH) )
|
||||
snprintf( buf, buflen, "PKCS12 - Given private key password does not allow for correct decryption" );
|
||||
#endif /* POLARSSL_PKCS12_C */
|
||||
|
||||
#if defined(POLARSSL_PKCS5_C)
|
||||
|
|
105
library/pkcs12.c
105
library/pkcs12.c
|
@ -35,6 +35,7 @@
|
|||
|
||||
#include "polarssl/pkcs12.h"
|
||||
#include "polarssl/asn1.h"
|
||||
#include "polarssl/cipher.h"
|
||||
|
||||
#if defined(POLARSSL_ARC4_C)
|
||||
#include "polarssl/arc4.h"
|
||||
|
@ -82,7 +83,7 @@ static int pkcs12_parse_pbe_params( unsigned char **p,
|
|||
return( 0 );
|
||||
}
|
||||
|
||||
static int pkcs12_pbe_derive_key_iv( asn1_buf *pbe_params,
|
||||
static int pkcs12_pbe_derive_key_iv( asn1_buf *pbe_params, md_type_t md_type,
|
||||
const unsigned char *pwd, size_t pwdlen,
|
||||
unsigned char *key, size_t keylen,
|
||||
unsigned char *iv, size_t ivlen )
|
||||
|
@ -106,7 +107,7 @@ static int pkcs12_pbe_derive_key_iv( asn1_buf *pbe_params,
|
|||
unipwd[i * 2 + 1] = pwd[i];
|
||||
|
||||
if( ( ret = pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
|
||||
salt.p, salt.len, POLARSSL_MD_SHA1,
|
||||
salt.p, salt.len, md_type,
|
||||
PKCS12_DERIVE_KEY, iterations ) ) != 0 )
|
||||
{
|
||||
return( ret );
|
||||
|
@ -116,7 +117,7 @@ static int pkcs12_pbe_derive_key_iv( asn1_buf *pbe_params,
|
|||
return( 0 );
|
||||
|
||||
if( ( ret = pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
|
||||
salt.p, salt.len, POLARSSL_MD_SHA1,
|
||||
salt.p, salt.len, md_type,
|
||||
PKCS12_DERIVE_IV, iterations ) ) != 0 )
|
||||
{
|
||||
return( ret );
|
||||
|
@ -144,7 +145,8 @@ int pkcs12_pbe_sha1_rc4_128( asn1_buf *pbe_params, int mode,
|
|||
arc4_context ctx;
|
||||
((void) mode);
|
||||
|
||||
if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, pwd, pwdlen,
|
||||
if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, POLARSSL_MD_SHA1,
|
||||
pwd, pwdlen,
|
||||
key, 16, NULL, 0 ) ) != 0 )
|
||||
{
|
||||
return( ret );
|
||||
|
@ -158,86 +160,51 @@ int pkcs12_pbe_sha1_rc4_128( asn1_buf *pbe_params, int mode,
|
|||
#endif /* POLARSSL_ARC4_C */
|
||||
}
|
||||
|
||||
int pkcs12_pbe_sha1_des2_ede_cbc( asn1_buf *pbe_params, int mode,
|
||||
const unsigned char *pwd, size_t pwdlen,
|
||||
const unsigned char *data, size_t len,
|
||||
unsigned char *output )
|
||||
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 *data, size_t len,
|
||||
unsigned char *output )
|
||||
{
|
||||
#if !defined(POLARSSL_DES_C)
|
||||
((void) pbe_params);
|
||||
((void) mode);
|
||||
((void) pwd);
|
||||
((void) pwdlen);
|
||||
((void) data);
|
||||
((void) len);
|
||||
((void) output);
|
||||
return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE );
|
||||
#else
|
||||
int ret;
|
||||
unsigned char key[16];
|
||||
unsigned char iv[8];
|
||||
des3_context ctx;
|
||||
int ret, keylen = 0;
|
||||
unsigned char key[32];
|
||||
unsigned char iv[16];
|
||||
const cipher_info_t *cipher_info;
|
||||
cipher_context_t cipher_ctx;
|
||||
size_t olen = 0;
|
||||
|
||||
if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, pwd, pwdlen,
|
||||
key, 16, iv, 8 ) ) != 0 )
|
||||
cipher_info = cipher_info_from_type( cipher_type );
|
||||
if( cipher_info == NULL )
|
||||
return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE );
|
||||
|
||||
keylen = cipher_info->key_length / 8;
|
||||
|
||||
if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
|
||||
key, keylen,
|
||||
iv, cipher_info->iv_size ) ) != 0 )
|
||||
{
|
||||
return( ret );
|
||||
}
|
||||
|
||||
if( mode == PKCS12_PBE_ENCRYPT )
|
||||
{
|
||||
des3_set2key_enc( &ctx, key );
|
||||
des3_crypt_cbc( &ctx, DES_ENCRYPT, len, iv, data, output );
|
||||
}
|
||||
else
|
||||
{
|
||||
des3_set2key_dec( &ctx, key );
|
||||
des3_crypt_cbc( &ctx, DES_DECRYPT, len, iv, data, output );
|
||||
}
|
||||
if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
return( 0 );
|
||||
#endif /* POLARSSL_DES_C */
|
||||
}
|
||||
if( ( ret = cipher_setkey( &cipher_ctx, key, keylen, mode ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
int pkcs12_pbe_sha1_des3_ede_cbc( asn1_buf *pbe_params, int mode,
|
||||
const unsigned char *pwd, size_t pwdlen,
|
||||
const unsigned char *data, size_t len,
|
||||
unsigned char *output )
|
||||
{
|
||||
#if !defined(POLARSSL_DES_C)
|
||||
((void) pbe_params);
|
||||
((void) mode);
|
||||
((void) pwd);
|
||||
((void) pwdlen);
|
||||
((void) data);
|
||||
((void) len);
|
||||
((void) output);
|
||||
return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE );
|
||||
#else
|
||||
int ret;
|
||||
unsigned char key[24];
|
||||
unsigned char iv[8];
|
||||
des3_context ctx;
|
||||
if( ( ret = cipher_reset( &cipher_ctx, iv ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, pwd, pwdlen,
|
||||
key, 24, iv, 8 ) ) != 0 )
|
||||
if( ( ret = cipher_update( &cipher_ctx, data, len,
|
||||
output, &olen ) ) != 0 )
|
||||
{
|
||||
return( ret );
|
||||
}
|
||||
|
||||
if( mode == PKCS12_PBE_ENCRYPT )
|
||||
{
|
||||
des3_set3key_enc( &ctx, key );
|
||||
des3_crypt_cbc( &ctx, DES_ENCRYPT, len, iv, data, output );
|
||||
}
|
||||
else
|
||||
{
|
||||
des3_set3key_dec( &ctx, key );
|
||||
des3_crypt_cbc( &ctx, DES_DECRYPT, len, iv, data, output );
|
||||
}
|
||||
if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
|
||||
return( POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH );
|
||||
|
||||
return( 0 );
|
||||
#endif /* POLARSSL_DES_C */
|
||||
}
|
||||
|
||||
static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
|
||||
|
|
|
@ -62,10 +62,12 @@
|
|||
#include "polarssl/sha4.h"
|
||||
#endif
|
||||
#include "polarssl/dhm.h"
|
||||
#include "polarssl/pkcs12.h"
|
||||
#if defined(POLARSSL_PKCS5_C)
|
||||
#include "polarssl/pkcs5.h"
|
||||
#endif
|
||||
#if defined(POLARSSL_PKCS12_C)
|
||||
#include "polarssl/pkcs12.h"
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -2253,23 +2255,28 @@ static int x509parse_key_pkcs8_encrypted_der(
|
|||
/*
|
||||
* Decrypt EncryptedData with appropriate PDE
|
||||
*/
|
||||
#if defined(POLARSSL_PKCS12_C)
|
||||
if( OID_CMP( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, &pbe_alg_oid ) )
|
||||
{
|
||||
if( ( ret = pkcs12_pbe_sha1_des3_ede_cbc( &pbe_params,
|
||||
PKCS12_PBE_DECRYPT,
|
||||
pwd, pwdlen,
|
||||
p, len, buf ) ) != 0 )
|
||||
if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
|
||||
POLARSSL_CIPHER_DES_EDE3_CBC, POLARSSL_MD_SHA1,
|
||||
pwd, pwdlen, p, len, buf ) ) != 0 )
|
||||
{
|
||||
if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
|
||||
return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
}
|
||||
else if( OID_CMP( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, &pbe_alg_oid ) )
|
||||
{
|
||||
if( ( ret = pkcs12_pbe_sha1_des2_ede_cbc( &pbe_params,
|
||||
PKCS12_PBE_DECRYPT,
|
||||
pwd, pwdlen,
|
||||
p, len, buf ) ) != 0 )
|
||||
if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
|
||||
POLARSSL_CIPHER_DES_EDE_CBC, POLARSSL_MD_SHA1,
|
||||
pwd, pwdlen, p, len, buf ) ) != 0 )
|
||||
{
|
||||
if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
|
||||
return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
}
|
||||
|
@ -2282,9 +2289,17 @@ static int x509parse_key_pkcs8_encrypted_der(
|
|||
{
|
||||
return( ret );
|
||||
}
|
||||
|
||||
// Best guess for password mismatch when using RC4. If first tag is
|
||||
// not ASN1_CONSTRUCTED | ASN1_SEQUENCE
|
||||
//
|
||||
if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
|
||||
return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
|
||||
}
|
||||
else
|
||||
#endif /* POLARSSL_PKCS12_C */
|
||||
#if defined(POLARSSL_PKCS5_C)
|
||||
else if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
|
||||
if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
|
||||
{
|
||||
if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
|
||||
p, len, buf ) ) != 0 )
|
||||
|
@ -2295,8 +2310,8 @@ static int x509parse_key_pkcs8_encrypted_der(
|
|||
return( ret );
|
||||
}
|
||||
}
|
||||
#endif /* POLARSSL_PKCS5_C */
|
||||
else
|
||||
#endif /* POLARSSL_PKCS5_C */
|
||||
return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
|
||||
|
||||
return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue