diff --git a/include/mbedtls/oid.h b/include/mbedtls/oid.h index 65e626e11..342ef754e 100644 --- a/include/mbedtls/oid.h +++ b/include/mbedtls/oid.h @@ -167,6 +167,11 @@ #define MBEDTLS_OID_INIHIBIT_ANYPOLICY MBEDTLS_OID_ID_CE "\x36" /**< id-ce-inhibitAnyPolicy OBJECT IDENTIFIER ::= { id-ce 54 } */ #define MBEDTLS_OID_FRESHEST_CRL MBEDTLS_OID_ID_CE "\x2E" /**< id-ce-freshestCRL OBJECT IDENTIFIER ::= { id-ce 46 } */ +/* + * Certificate policies + */ +#define MBEDTLS_OID_ANY_POLICY MBEDTLS_OID_CERTIFICATE_POLICIES "\x00" /**< anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 } */ + /* * Netscape certificate extensions */ @@ -576,6 +581,16 @@ int mbedtls_oid_get_md_hmac( const mbedtls_asn1_buf *oid, mbedtls_md_type_t *md_ */ int mbedtls_oid_get_extended_key_usage( const mbedtls_asn1_buf *oid, const char **desc ); +/** + * \brief Translate certificate policies OID into description + * + * \param oid OID to use + * \param desc place to store string pointer + * + * \return 0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND + */ +int mbedtls_oid_get_certificate_policies( const mbedtls_asn1_buf *oid, const char **desc ); + /** * \brief Translate md_type into hash algorithm OID * diff --git a/library/oid.c b/library/oid.c index 294bbd661..4e10f8a26 100644 --- a/library/oid.c +++ b/library/oid.c @@ -296,6 +296,15 @@ static const mbedtls_oid_descriptor_t oid_ext_key_usage[] = FN_OID_TYPED_FROM_ASN1(mbedtls_oid_descriptor_t, ext_key_usage, oid_ext_key_usage) FN_OID_GET_ATTR1(mbedtls_oid_get_extended_key_usage, mbedtls_oid_descriptor_t, ext_key_usage, const char *, description) +static const mbedtls_oid_descriptor_t oid_certificate_policies[] = +{ + { ADD_LEN( MBEDTLS_OID_ANY_POLICY ), "anyPolicy", "Any Policy" }, + { NULL, 0, NULL, NULL }, +}; + +FN_OID_TYPED_FROM_ASN1(mbedtls_oid_descriptor_t, certificate_policies, oid_certificate_policies) +FN_OID_GET_ATTR1(mbedtls_oid_get_certificate_policies, mbedtls_oid_descriptor_t, certificate_policies, const char *, description) + #if defined(MBEDTLS_MD_C) /* * For SignatureAlgorithmIdentifier diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a8e7523e5..1b7ea78ca 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -116,6 +116,7 @@ add_test_suite(mdx) add_test_suite(memory_buffer_alloc) add_test_suite(mpi) add_test_suite(nist_kw) +add_test_suite(oid) add_test_suite(pem) add_test_suite(pkcs1_v15) add_test_suite(pkcs1_v21) diff --git a/tests/suites/test_suite_oid.data b/tests/suites/test_suite_oid.data new file mode 100644 index 000000000..759a01038 --- /dev/null +++ b/tests/suites/test_suite_oid.data @@ -0,0 +1,8 @@ +OID get Any Policy certificate policy +oid_get_certificate_policies:"551D2000":"Any Policy" + +OID get certificate policy invalid oid +oid_get_certificate_policies:"5533445566":"" + +OID get certificate policy wrong oid - id-ce-authorityKeyIdentifier +oid_get_certificate_policies:"551D23":"" diff --git a/tests/suites/test_suite_oid.function b/tests/suites/test_suite_oid.function new file mode 100644 index 000000000..e95e48d06 --- /dev/null +++ b/tests/suites/test_suite_oid.function @@ -0,0 +1,34 @@ +/* BEGIN_HEADER */ +#include "mbedtls/oid.h" +#include "mbedtls/asn1.h" +#include "mbedtls/asn1write.h" +#include "string.h" +/* END_HEADER */ + +/* BEGIN_DEPENDENCIES + * depends_on:MBEDTLS_OID_C + * END_DEPENDENCIES + */ + +/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C*/ +void oid_get_certificate_policies( data_t * oid, char * result_str ) +{ + mbedtls_asn1_buf asn1_buf = { 0, 0, NULL }; + int ret; + const char *desc; + + asn1_buf.tag = MBEDTLS_ASN1_OID; + asn1_buf.p = oid->x; + asn1_buf.len = oid->len; + + ret = mbedtls_oid_get_certificate_policies( &asn1_buf, &desc ); + if( strlen( result_str ) == 0 ) + { + TEST_ASSERT( ret == MBEDTLS_ERR_OID_NOT_FOUND ); + } + else + { + TEST_ASSERT( strcmp( ( char* )desc, result_str ) == 0 ); + } +} +/* END_CASE */