mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-12-23 19:35:31 +00:00
Change pk_set_type to pk_init_ctx for consistency
This commit is contained in:
parent
7e56de1671
commit
ab46694558
|
@ -147,31 +147,38 @@ typedef struct
|
||||||
void * pk_ctx; /**< Underlying public key context */
|
void * pk_ctx; /**< Underlying public key context */
|
||||||
} pk_context;
|
} pk_context;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Return information associated with the given PK type
|
||||||
|
*
|
||||||
|
* \param type PK type to search for.
|
||||||
|
*
|
||||||
|
* \return The PK info associated with the type or NULL if not found.
|
||||||
|
*/
|
||||||
|
const pk_info_t *pk_info_from_type( pk_type_t pk_type );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Initialize a pk_context (as NONE)
|
* \brief Initialize a pk_context (as NONE)
|
||||||
*/
|
*/
|
||||||
void pk_init( pk_context *ctx );
|
void pk_init( pk_context *ctx );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Initialize a PK context with the information given
|
||||||
|
* and allocates the type-specific PK subcontext.
|
||||||
|
*
|
||||||
|
* \param ctx Context to initialize. Must be empty (type NONE).
|
||||||
|
* \param info Information to use
|
||||||
|
*
|
||||||
|
* \return 0 on success,
|
||||||
|
* POLARSSL_ERR_PK_BAD_INPUT_DATA on invalid input,
|
||||||
|
* POLARSSL_ERR_PK_MALLOC_FAILED on allocation failure.
|
||||||
|
*/
|
||||||
|
int pk_init_ctx( pk_context *ctx, const pk_info_t *info );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Free a pk_context
|
* \brief Free a pk_context
|
||||||
*/
|
*/
|
||||||
void pk_free( pk_context *ctx );
|
void pk_free( pk_context *ctx );
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Set a pk_context to a given type
|
|
||||||
*
|
|
||||||
* \param ctx Context to initialize
|
|
||||||
* \param type Type of key
|
|
||||||
*
|
|
||||||
* \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 );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Get the size in bits of the underlying key
|
* \brief Get the size in bits of the underlying key
|
||||||
*
|
*
|
||||||
|
|
18
library/pk.c
18
library/pk.c
|
@ -67,7 +67,7 @@ void pk_free( pk_context *ctx )
|
||||||
/*
|
/*
|
||||||
* Get pk_info structure from type
|
* Get pk_info structure from type
|
||||||
*/
|
*/
|
||||||
static const pk_info_t * pk_info_from_type( pk_type_t pk_type )
|
const pk_info_t * pk_info_from_type( pk_type_t pk_type )
|
||||||
{
|
{
|
||||||
switch( pk_type ) {
|
switch( pk_type ) {
|
||||||
#if defined(POLARSSL_RSA_C)
|
#if defined(POLARSSL_RSA_C)
|
||||||
|
@ -90,21 +90,11 @@ static const pk_info_t * pk_info_from_type( pk_type_t pk_type )
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Set a pk_context to a given type
|
* Initialise context
|
||||||
*/
|
*/
|
||||||
int pk_set_type( pk_context *ctx, pk_type_t type )
|
int pk_init_ctx( pk_context *ctx, const pk_info_t *info )
|
||||||
{
|
{
|
||||||
const pk_info_t *info;
|
if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
|
||||||
|
|
||||||
if( ctx->pk_info != NULL )
|
|
||||||
{
|
|
||||||
if( ctx->pk_info->type == type )
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return( POLARSSL_ERR_PK_TYPE_MISMATCH );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( ( info = pk_info_from_type( type ) ) == NULL )
|
|
||||||
return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
|
return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
|
||||||
|
|
||||||
if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
|
if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
|
||||||
|
|
|
@ -570,6 +570,7 @@ static int x509_get_pubkey( unsigned char **p,
|
||||||
size_t len;
|
size_t len;
|
||||||
x509_buf alg_params;
|
x509_buf alg_params;
|
||||||
pk_type_t pk_alg = POLARSSL_PK_NONE;
|
pk_type_t pk_alg = POLARSSL_PK_NONE;
|
||||||
|
const pk_info_t *pk_info;
|
||||||
|
|
||||||
if( ( ret = asn1_get_tag( p, end, &len,
|
if( ( ret = asn1_get_tag( p, end, &len,
|
||||||
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
|
||||||
|
@ -589,7 +590,10 @@ static int x509_get_pubkey( unsigned char **p,
|
||||||
return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
|
return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
|
||||||
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
||||||
|
|
||||||
if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
|
if( ( pk_info = pk_info_from_type( pk_alg ) ) == NULL )
|
||||||
|
return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
|
||||||
|
|
||||||
|
if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 )
|
||||||
return( ret );
|
return( ret );
|
||||||
|
|
||||||
#if defined(POLARSSL_RSA_C)
|
#if defined(POLARSSL_RSA_C)
|
||||||
|
@ -2142,10 +2146,12 @@ int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
|
||||||
pk_context pk;
|
pk_context pk;
|
||||||
|
|
||||||
pk_init( &pk );
|
pk_init( &pk );
|
||||||
pk_set_type( &pk, POLARSSL_PK_RSA );
|
|
||||||
|
|
||||||
ret = x509parse_keyfile( &pk, path, pwd );
|
ret = x509parse_keyfile( &pk, path, pwd );
|
||||||
|
|
||||||
|
if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
|
||||||
|
ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
|
||||||
|
|
||||||
if( ret == 0 )
|
if( ret == 0 )
|
||||||
rsa_copy( rsa, pk_rsa( pk ) );
|
rsa_copy( rsa, pk_rsa( pk ) );
|
||||||
else
|
else
|
||||||
|
@ -2165,10 +2171,12 @@ int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
|
||||||
pk_context pk;
|
pk_context pk;
|
||||||
|
|
||||||
pk_init( &pk );
|
pk_init( &pk );
|
||||||
pk_set_type( &pk, POLARSSL_PK_RSA );
|
|
||||||
|
|
||||||
ret = x509parse_public_keyfile( &pk, path );
|
ret = x509parse_public_keyfile( &pk, path );
|
||||||
|
|
||||||
|
if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
|
||||||
|
ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
|
||||||
|
|
||||||
if( ret == 0 )
|
if( ret == 0 )
|
||||||
rsa_copy( rsa, pk_rsa( pk ) );
|
rsa_copy( rsa, pk_rsa( pk ) );
|
||||||
else
|
else
|
||||||
|
@ -2380,6 +2388,7 @@ static int x509parse_key_pkcs8_unencrypted_der(
|
||||||
unsigned char *p = (unsigned char *) key;
|
unsigned char *p = (unsigned char *) key;
|
||||||
unsigned char *end = p + keylen;
|
unsigned char *end = p + keylen;
|
||||||
pk_type_t pk_alg = POLARSSL_PK_NONE;
|
pk_type_t pk_alg = POLARSSL_PK_NONE;
|
||||||
|
const pk_info_t *pk_info;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
|
* This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
|
||||||
|
@ -2421,7 +2430,10 @@ static int x509parse_key_pkcs8_unencrypted_der(
|
||||||
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
|
return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
|
||||||
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
POLARSSL_ERR_ASN1_OUT_OF_DATA );
|
||||||
|
|
||||||
if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
|
if( ( pk_info = pk_info_from_type( pk_alg ) ) == NULL )
|
||||||
|
return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
|
||||||
|
|
||||||
|
if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 )
|
||||||
return( ret );
|
return( ret );
|
||||||
|
|
||||||
#if defined(POLARSSL_RSA_C)
|
#if defined(POLARSSL_RSA_C)
|
||||||
|
@ -2568,6 +2580,7 @@ int x509parse_key( pk_context *pk,
|
||||||
const unsigned char *pwd, size_t pwdlen )
|
const unsigned char *pwd, size_t pwdlen )
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
const pk_info_t *pk_info;
|
||||||
|
|
||||||
#if defined(POLARSSL_PEM_C)
|
#if defined(POLARSSL_PEM_C)
|
||||||
size_t len;
|
size_t len;
|
||||||
|
@ -2582,7 +2595,10 @@ int x509parse_key( pk_context *pk,
|
||||||
key, pwd, pwdlen, &len );
|
key, pwd, pwdlen, &len );
|
||||||
if( ret == 0 )
|
if( ret == 0 )
|
||||||
{
|
{
|
||||||
if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) != 0 ||
|
if( ( pk_info = pk_info_from_type( POLARSSL_PK_RSA ) ) == NULL )
|
||||||
|
return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
|
||||||
|
|
||||||
|
if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
|
||||||
( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
|
( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
|
||||||
pem.buf, pem.buflen ) ) != 0 )
|
pem.buf, pem.buflen ) ) != 0 )
|
||||||
{
|
{
|
||||||
|
@ -2607,7 +2623,10 @@ int x509parse_key( pk_context *pk,
|
||||||
key, pwd, pwdlen, &len );
|
key, pwd, pwdlen, &len );
|
||||||
if( ret == 0 )
|
if( ret == 0 )
|
||||||
{
|
{
|
||||||
if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) != 0 ||
|
if( ( pk_info = pk_info_from_type( POLARSSL_PK_ECKEY ) ) == NULL )
|
||||||
|
return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
|
||||||
|
|
||||||
|
if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
|
||||||
( ret = x509parse_key_sec1_der( pk_ec( *pk ),
|
( ret = x509parse_key_sec1_der( pk_ec( *pk ),
|
||||||
pem.buf, pem.buflen ) ) != 0 )
|
pem.buf, pem.buflen ) ) != 0 )
|
||||||
{
|
{
|
||||||
|
@ -2692,7 +2711,10 @@ int x509parse_key( pk_context *pk,
|
||||||
pk_free( pk );
|
pk_free( pk );
|
||||||
|
|
||||||
#if defined(POLARSSL_RSA_C)
|
#if defined(POLARSSL_RSA_C)
|
||||||
if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) == 0 &&
|
if( ( pk_info = pk_info_from_type( POLARSSL_PK_RSA ) ) == NULL )
|
||||||
|
return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
|
||||||
|
|
||||||
|
if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
|
||||||
( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
|
( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
|
||||||
{
|
{
|
||||||
return( 0 );
|
return( 0 );
|
||||||
|
@ -2702,7 +2724,10 @@ int x509parse_key( pk_context *pk,
|
||||||
#endif /* POLARSSL_RSA_C */
|
#endif /* POLARSSL_RSA_C */
|
||||||
|
|
||||||
#if defined(POLARSSL_ECP_C)
|
#if defined(POLARSSL_ECP_C)
|
||||||
if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) == 0 &&
|
if( ( pk_info = pk_info_from_type( POLARSSL_PK_ECKEY ) ) == NULL )
|
||||||
|
return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
|
||||||
|
|
||||||
|
if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
|
||||||
( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
|
( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
|
||||||
{
|
{
|
||||||
return( 0 );
|
return( 0 );
|
||||||
|
@ -2769,10 +2794,12 @@ int x509parse_key_rsa( rsa_context *rsa,
|
||||||
pk_context pk;
|
pk_context pk;
|
||||||
|
|
||||||
pk_init( &pk );
|
pk_init( &pk );
|
||||||
pk_set_type( &pk, POLARSSL_PK_RSA );
|
|
||||||
|
|
||||||
ret = x509parse_key( &pk, key, keylen, pwd, pwdlen );
|
ret = x509parse_key( &pk, key, keylen, pwd, pwdlen );
|
||||||
|
|
||||||
|
if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
|
||||||
|
ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
|
||||||
|
|
||||||
if( ret == 0 )
|
if( ret == 0 )
|
||||||
rsa_copy( rsa, pk_rsa( pk ) );
|
rsa_copy( rsa, pk_rsa( pk ) );
|
||||||
else
|
else
|
||||||
|
@ -2793,10 +2820,12 @@ int x509parse_public_key_rsa( rsa_context *rsa,
|
||||||
pk_context pk;
|
pk_context pk;
|
||||||
|
|
||||||
pk_init( &pk );
|
pk_init( &pk );
|
||||||
pk_set_type( &pk, POLARSSL_PK_RSA );
|
|
||||||
|
|
||||||
ret = x509parse_public_key( &pk, key, keylen );
|
ret = x509parse_public_key( &pk, key, keylen );
|
||||||
|
|
||||||
|
if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
|
||||||
|
ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
|
||||||
|
|
||||||
if( ret == 0 )
|
if( ret == 0 )
|
||||||
rsa_copy( rsa, pk_rsa( pk ) );
|
rsa_copy( rsa, pk_rsa( pk ) );
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in a new issue