PKCS#5 v2 PBES2 support and use in PKCS#8 encrypted certificates

The error code POLARSSL_ERR_X509_PASSWORD_MISMATCH is now properly
returned in case of an encryption failure in the padding. The
POLARSSL_ERR_X509_PASSWORD_REQUIRED error code is only returned for PEM
formatted private keys as for DER formatted ones it is impossible to
distinguish if a DER blob is PKCS#8 encrypted or not.
(cherry picked from commit 1fd4321ba2016dfaff2b48c11f731fc9ccbd7ccf)

Conflicts:
	include/polarssl/error.h
	scripts/generate_errors.pl
This commit is contained in:
Paul Bakker 2013-06-24 19:28:55 +02:00
parent b0c19a4b3d
commit 28144decef
11 changed files with 376 additions and 10 deletions

View file

@ -63,6 +63,9 @@
#endif
#include "polarssl/dhm.h"
#include "polarssl/pkcs12.h"
#if defined(POLARSSL_PKCS5_C)
#include "polarssl/pkcs5.h"
#endif
#include <string.h>
#include <stdlib.h>
@ -2194,6 +2197,9 @@ static int x509parse_key_pkcs8_encrypted_der(
p = (unsigned char *) key;
end = p + keylen;
if( pwdlen == 0 )
return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
/*
* This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
*
@ -2277,6 +2283,19 @@ static int x509parse_key_pkcs8_encrypted_der(
return( ret );
}
}
#if defined(POLARSSL_PKCS5_C)
else if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
{
if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
p, len, buf ) ) != 0 )
{
if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
return( ret );
}
}
#endif /* POLARSSL_PKCS5_C */
else
return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
@ -2376,14 +2395,22 @@ int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
}
rsa_free( rsa );
if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
{
return( ret );
}
if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
return( 0 );
rsa_free( rsa );
if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
return( 0 );
rsa_free( rsa );
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
}