pk_set_type() cannot be used to reset key type

This commit is contained in:
Manuel Pégourié-Gonnard 2013-07-09 10:21:34 +02:00 committed by Paul Bakker
parent 0a64e8f1fd
commit 374e4b87d4
4 changed files with 24 additions and 2 deletions

View file

@ -28,6 +28,7 @@
#define POLARSSL_PK_H #define POLARSSL_PK_H
#define POLARSSL_ERR_PK_MALLOC_FAILED -0x2F80 /**< Memory alloation failed. */ #define POLARSSL_ERR_PK_MALLOC_FAILED -0x2F80 /**< Memory alloation failed. */
#define POLARSSL_ERR_PK_TYPE_MISMATCH -0x2F00 /**< Type mismatch, eg attempt to use a RSA key as EC, or to modify key type */
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -72,7 +73,12 @@ void pk_free( pk_context *ctx );
* \param ctx Context to initialize * \param ctx Context to initialize
* \param type Type of key * \param type Type of key
* *
* \return O on success, or POLARSSL_ERR_PK_MALLOC_FAILED * \note Once the type of a key has been set, it cannot be reset.
* If you want to do so, you need to use pk_free() first.
*
* \return O on success,
* POLARSSL_ERR_PK_MALLOC_FAILED on memory allocation fail,
* POLARSSL_ERR_PK_TYPE_MISMATCH on attempts to reset type.
*/ */
int pk_set_type( pk_context *ctx, pk_type_t type ); int pk_set_type( pk_context *ctx, pk_type_t type );

View file

@ -250,6 +250,8 @@ void polarssl_strerror( int ret, char *buf, size_t buflen )
#if defined(POLARSSL_PK_C) #if defined(POLARSSL_PK_C)
if( use_ret == -(POLARSSL_ERR_PK_MALLOC_FAILED) ) if( use_ret == -(POLARSSL_ERR_PK_MALLOC_FAILED) )
snprintf( buf, buflen, "PK - Memory alloation failed" ); snprintf( buf, buflen, "PK - Memory alloation failed" );
if( use_ret == -(POLARSSL_ERR_PK_TYPE_MISMATCH) )
snprintf( buf, buflen, "PK - Type mismatch, eg attempt to use a RSA key as EC, or to modify key type" );
#endif /* POLARSSL_PK_C */ #endif /* POLARSSL_PK_C */
#if defined(POLARSSL_PKCS12_C) #if defined(POLARSSL_PKCS12_C)

View file

@ -88,6 +88,12 @@ int pk_set_type( pk_context *ctx, pk_type_t type )
{ {
size_t size = 0; size_t size = 0;
if( ctx->type == type )
return( 0 );
if( ctx->type != POLARSSL_PK_NONE )
return( POLARSSL_ERR_PK_TYPE_MISMATCH );
switch( type ) switch( type )
{ {
#if defined(POLARSSL_RSA_C) #if defined(POLARSSL_RSA_C)
@ -104,7 +110,7 @@ int pk_set_type( pk_context *ctx, pk_type_t type )
#endif #endif
case POLARSSL_PK_NONE: case POLARSSL_PK_NONE:
; /* Should not happen */ ; /* Cannot happen, but the cmpiler doesn't know */
} }
if( ( ctx->data = malloc( size ) ) == NULL ) if( ( ctx->data = malloc( size ) ) == NULL )

View file

@ -3120,6 +3120,8 @@ int x509parse_key( pk_context *ctx,
return( 0 ); return( 0 );
} }
pk_free( ctx );
if ( ( ret = pk_set_type( ctx, POLARSSL_PK_ECKEY ) ) != 0 ) if ( ( ret = pk_set_type( ctx, POLARSSL_PK_ECKEY ) ) != 0 )
return( ret ); return( ret );
@ -3128,6 +3130,8 @@ int x509parse_key( pk_context *ctx,
return( 0 ); return( 0 );
} }
pk_free( ctx );
return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT ); return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
} }
@ -3145,12 +3149,16 @@ int x509parse_public_key( pk_context *ctx,
if( ( ret = x509parse_public_key_rsa( ctx->data, key, keylen ) ) == 0 ) if( ( ret = x509parse_public_key_rsa( ctx->data, key, keylen ) ) == 0 )
return( 0 ); return( 0 );
pk_free( ctx );
if ( ( ret = pk_set_type( ctx, POLARSSL_PK_ECKEY ) ) != 0 ) if ( ( ret = pk_set_type( ctx, POLARSSL_PK_ECKEY ) ) != 0 )
return( ret ); return( ret );
if( ( ret = x509parse_public_key_ec( ctx->data, key, keylen ) ) == 0 ) if( ( ret = x509parse_public_key_ec( ctx->data, key, keylen ) ) == 0 )
return( 0 ); return( 0 );
pk_free( ctx );
return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT ); return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
} }