Macro-ized single and double attribute functions in OID database
This commit is contained in:
parent
bd51ad538d
commit
dd1150e846
165
library/oid.c
165
library/oid.c
@ -34,10 +34,55 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* Macro to generate an internal function for oid_XXX_from_asn1() (used by
|
||||
* the other functions)
|
||||
*/
|
||||
#define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST ) \
|
||||
static const TYPE_T * oid_ ## NAME ## _from_asn1( const asn1_buf *oid ) \
|
||||
{ return (const TYPE_T *) oid_descriptor_from_buf(LIST, sizeof(TYPE_T), oid->p, oid->len ); }
|
||||
|
||||
/*
|
||||
* Macro to generate a function for retrieving a single attribute from the
|
||||
* descriptor of an oid_descriptor_t wrapper.
|
||||
*/
|
||||
#define FN_OID_GET_DESCRIPTOR_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
|
||||
int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
|
||||
{ \
|
||||
const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
|
||||
if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
|
||||
*ATTR1 = data->descriptor.ATTR1; \
|
||||
return( 0 ); \
|
||||
}
|
||||
|
||||
/*
|
||||
* Macro to generate a function for retrieving a single attribute from an
|
||||
* oid_descriptor_t wrapper.
|
||||
*/
|
||||
#define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
|
||||
int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
|
||||
{ \
|
||||
const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
|
||||
if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
|
||||
*ATTR1 = data->ATTR1; \
|
||||
return( 0 ); \
|
||||
}
|
||||
|
||||
/*
|
||||
* Macro to generate a function for retrieving two attributes from an
|
||||
* oid_descriptor_t wrapper.
|
||||
*/
|
||||
#define FN_OID_GET_ATTR2(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1, \
|
||||
ATTR2_TYPE, ATTR2) \
|
||||
int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 ) \
|
||||
{ \
|
||||
const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
|
||||
if( data == NULL ) return ( POLARSSL_ERR_OID_NOT_FOUND ); \
|
||||
*ATTR1 = data->ATTR1; \
|
||||
*ATTR2 = data->ATTR2; \
|
||||
return( 0 ); \
|
||||
}
|
||||
|
||||
/*
|
||||
* Core generic function
|
||||
*/
|
||||
@ -110,19 +155,8 @@ static const oid_x520_attr_t oid_x520_attr_type[] =
|
||||
}
|
||||
};
|
||||
|
||||
FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type);
|
||||
|
||||
int oid_get_attr_short_name( const asn1_buf *oid, const char **short_name )
|
||||
{
|
||||
const oid_x520_attr_t *data = oid_x520_attr_from_asn1( oid );
|
||||
|
||||
if( data == NULL )
|
||||
return( POLARSSL_ERR_OID_NOT_FOUND );
|
||||
|
||||
*short_name = data->short_name;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
FN_OID_TYPED_FROM_ASN1(oid_x520_attr_t, x520_attr, oid_x520_attr_type);
|
||||
FN_OID_GET_ATTR1(oid_get_attr_short_name, oid_x520_attr_t, x520_attr, const char *, short_name);
|
||||
|
||||
#if defined(POLARSSL_X509_PARSE_C) || defined(POLARSSL_X509_WRITE_C)
|
||||
/*
|
||||
@ -161,19 +195,8 @@ static const oid_x509_ext_t oid_x509_ext[] =
|
||||
},
|
||||
};
|
||||
|
||||
FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext);
|
||||
|
||||
int oid_get_x509_ext_type( const asn1_buf *oid, int *ext_type )
|
||||
{
|
||||
const oid_x509_ext_t *data = oid_x509_ext_from_asn1( oid );
|
||||
|
||||
if( data == NULL )
|
||||
return( POLARSSL_ERR_OID_NOT_FOUND );
|
||||
|
||||
*ext_type = data->ext_type;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext);
|
||||
FN_OID_GET_ATTR1(oid_get_x509_ext_type, oid_x509_ext_t, x509_ext, int, ext_type);
|
||||
|
||||
static const oid_descriptor_t oid_ext_key_usage[] =
|
||||
{
|
||||
@ -186,19 +209,8 @@ static const oid_descriptor_t oid_ext_key_usage[] =
|
||||
{ NULL, NULL, NULL },
|
||||
};
|
||||
|
||||
FN_OID_TYPED_FROM_ASN1(oid_descriptor_t, ext_key_usage, oid_ext_key_usage);
|
||||
|
||||
int oid_get_extended_key_usage( const asn1_buf *oid, const char **desc )
|
||||
{
|
||||
const oid_descriptor_t *data = oid_ext_key_usage_from_asn1( oid );
|
||||
|
||||
if( data == NULL )
|
||||
return( POLARSSL_ERR_OID_NOT_FOUND );
|
||||
|
||||
*desc = data->description;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
FN_OID_TYPED_FROM_ASN1(oid_descriptor_t, ext_key_usage, oid_ext_key_usage);
|
||||
FN_OID_GET_ATTR1(oid_get_extended_key_usage, oid_descriptor_t, ext_key_usage, const char *, description);
|
||||
|
||||
#endif /* POLARSSL_X509_PARSE_C || POLARSSL_X509_WRITE_C */
|
||||
|
||||
@ -255,33 +267,9 @@ static const oid_sig_alg_t oid_sig_alg[] =
|
||||
},
|
||||
};
|
||||
|
||||
FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
|
||||
|
||||
int oid_get_sig_alg_desc( const asn1_buf *oid, const char **desc )
|
||||
{
|
||||
const oid_sig_alg_t *data = oid_sig_alg_from_asn1( oid );
|
||||
|
||||
if( data == NULL )
|
||||
return( POLARSSL_ERR_OID_NOT_FOUND );
|
||||
|
||||
*desc = data->descriptor.description;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
int oid_get_sig_alg( const asn1_buf *oid,
|
||||
md_type_t *md_alg, pk_type_t *pk_alg )
|
||||
{
|
||||
const oid_sig_alg_t *data = oid_sig_alg_from_asn1( oid );
|
||||
|
||||
if( data == NULL )
|
||||
return( POLARSSL_ERR_OID_NOT_FOUND );
|
||||
|
||||
*md_alg = data->md_alg;
|
||||
*pk_alg = data->pk_alg;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
FN_OID_TYPED_FROM_ASN1(oid_sig_alg_t, sig_alg, oid_sig_alg);
|
||||
FN_OID_GET_DESCRIPTOR_ATTR1(oid_get_sig_alg_desc, oid_sig_alg_t, sig_alg, const char *, description);
|
||||
FN_OID_GET_ATTR2(oid_get_sig_alg, oid_sig_alg_t, sig_alg, md_type_t, md_alg, pk_type_t, pk_alg);
|
||||
|
||||
int oid_get_oid_by_sig_alg( pk_type_t pk_alg, md_type_t md_alg,
|
||||
const char **oid_str )
|
||||
@ -323,19 +311,8 @@ static const oid_pk_alg_t oid_pk_alg[] =
|
||||
},
|
||||
};
|
||||
|
||||
FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
|
||||
|
||||
int oid_get_pk_alg( const asn1_buf *oid, pk_type_t *pk_alg )
|
||||
{
|
||||
const oid_pk_alg_t *data = oid_pk_alg_from_asn1( oid );
|
||||
|
||||
if( data == NULL )
|
||||
return( POLARSSL_ERR_OID_NOT_FOUND );
|
||||
|
||||
*pk_alg = data->pk_alg;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
FN_OID_TYPED_FROM_ASN1(oid_pk_alg_t, pk_alg, oid_pk_alg);
|
||||
FN_OID_GET_ATTR1(oid_get_pk_alg, oid_pk_alg_t, pk_alg, pk_type_t, pk_alg);
|
||||
|
||||
/*
|
||||
* For PKCS#5 PBES2 encryption algorithm
|
||||
@ -361,19 +338,8 @@ static const oid_cipher_alg_t oid_cipher_alg[] =
|
||||
},
|
||||
};
|
||||
|
||||
FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
|
||||
|
||||
int oid_get_cipher_alg( const asn1_buf *oid, cipher_type_t *cipher_alg )
|
||||
{
|
||||
const oid_cipher_alg_t *data = oid_cipher_alg_from_asn1( oid );
|
||||
|
||||
if( data == NULL )
|
||||
return( POLARSSL_ERR_OID_NOT_FOUND );
|
||||
|
||||
*cipher_alg = data->cipher_alg;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
FN_OID_TYPED_FROM_ASN1(oid_cipher_alg_t, cipher_alg, oid_cipher_alg);
|
||||
FN_OID_GET_ATTR1(oid_get_cipher_alg, oid_cipher_alg_t, cipher_alg, cipher_type_t, cipher_alg);
|
||||
|
||||
/*
|
||||
* For digestAlgorithm
|
||||
@ -427,19 +393,8 @@ static const oid_md_alg_t oid_md_alg[] =
|
||||
},
|
||||
};
|
||||
|
||||
FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
|
||||
|
||||
int oid_get_md_alg( const asn1_buf *oid, md_type_t *md_alg )
|
||||
{
|
||||
const oid_md_alg_t *data = oid_md_alg_from_asn1( oid );
|
||||
|
||||
if( data == NULL )
|
||||
return( POLARSSL_ERR_OID_NOT_FOUND );
|
||||
|
||||
*md_alg = data->md_alg;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
FN_OID_TYPED_FROM_ASN1(oid_md_alg_t, md_alg, oid_md_alg);
|
||||
FN_OID_GET_ATTR1(oid_get_md_alg, oid_md_alg_t, md_alg, md_type_t, md_alg);
|
||||
|
||||
int oid_get_oid_by_md( md_type_t md_alg, const char **oid_str )
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user