1 /**
2  * \file x509_oid.h
3  *
4  * \brief Object Identifier (OID) database
5  */
6 /*
7  *  Copyright The Mbed TLS Contributors
8  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9  */
10 #ifndef MBEDTLS_X509_OID_H
11 #define MBEDTLS_X509_OID_H
12 #include "mbedtls/private_access.h"
13 
14 #include "mbedtls/asn1.h"
15 #include "mbedtls/pk.h"
16 #if defined(MBEDTLS_PK_HAVE_PRIVATE_HEADER)
17 #include <mbedtls/private/pk_private.h>
18 #endif /* MBEDTLS_PK_HAVE_PRIVATE_HEADER */
19 #include "mbedtls/x509.h"
20 
21 #include <stddef.h>
22 
23 #include "mbedtls/md.h"
24 
25 /*
26  * Maximum number of OID components allowed
27  */
28 #define MBEDTLS_OID_MAX_COMPONENTS              128
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /**
35  * \brief Base OID descriptor structure
36  */
37 typedef struct {
38     const char *MBEDTLS_PRIVATE(asn1);               /*!< OID ASN.1 representation       */
39     size_t MBEDTLS_PRIVATE(asn1_len);                /*!< length of asn1                 */
40 #if !defined(MBEDTLS_X509_REMOVE_INFO)
41     const char *MBEDTLS_PRIVATE(name);               /*!< official name (e.g. from RFC)  */
42     const char *MBEDTLS_PRIVATE(description);        /*!< human friendly description     */
43 #endif
44 } mbedtls_x509_oid_descriptor_t;
45 
46 #if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C)
47 #define MBEDTLS_X509_OID_HAVE_GET_X509_EXT_TYPE
48 /**
49  * \brief          Translate an X.509 extension OID into local values
50  *
51  * \param oid      OID to use
52  * \param ext_type place to store the extension type
53  *
54  * \return         0 if successful, or MBEDTLS_ERR_X509_UNKNOWN_OID
55  */
56 int mbedtls_x509_oid_get_x509_ext_type(const mbedtls_asn1_buf *oid, int *ext_type);
57 #endif /* MBEDTLS_X509_OID_HAVE_GET_X509_EXT_TYPE */
58 
59 #if defined(MBEDTLS_X509_USE_C)
60 /**
61  * \brief          Translate an X.509 attribute type OID into the short name
62  *                 (e.g. the OID for an X520 Common Name into "CN")
63  *
64  * \param oid      OID to use
65  * \param short_name    place to store the string pointer
66  *
67  * \return         0 if successful, or MBEDTLS_ERR_X509_UNKNOWN_OID
68  */
69 int mbedtls_x509_oid_get_attr_short_name(const mbedtls_asn1_buf *oid, const char **short_name);
70 #endif /* MBEDTLS_X509_USE_C */
71 
72 #if defined(MBEDTLS_X509_USE_C)
73 /**
74  * \brief          Translate SignatureAlgorithm OID into md_type and pk_type
75  *
76  * \param oid      OID to use
77  * \param md_alg   place to store message digest algorithm
78  * \param pk_alg   place to store public key algorithm
79  *
80  * \return         0 if successful, or MBEDTLS_ERR_X509_UNKNOWN_OID
81  */
82 int mbedtls_x509_oid_get_sig_alg(const mbedtls_asn1_buf *oid,
83                                  mbedtls_md_type_t *md_alg, mbedtls_pk_sigalg_t *pk_alg);
84 
85 #if !defined(MBEDTLS_X509_REMOVE_INFO)
86 /**
87  * \brief          Translate SignatureAlgorithm OID into description
88  *
89  * \param oid      OID to use
90  * \param desc     place to store string pointer
91  *
92  * \return         0 if successful, or MBEDTLS_ERR_X509_UNKNOWN_OID
93  */
94 int mbedtls_x509_oid_get_sig_alg_desc(const mbedtls_asn1_buf *oid, const char **desc);
95 #endif /* !MBEDTLS_X509_REMOVE_INFO */
96 #endif /* MBEDTLS_X509_USE_C */
97 
98 #if defined(MBEDTLS_X509_CRT_WRITE_C) || defined(MBEDTLS_X509_CSR_WRITE_C)
99 /**
100  * \brief          Translate md_type and pk_type into SignatureAlgorithm OID
101  *
102  * \param md_alg   message digest algorithm
103  * \param pk_alg   public key algorithm
104  * \param oid      place to store ASN.1 OID string pointer
105  * \param olen     length of the OID
106  *
107  * \return         0 if successful, or MBEDTLS_ERR_X509_UNKNOWN_OID
108  */
109 int mbedtls_x509_oid_get_oid_by_sig_alg(mbedtls_pk_sigalg_t pk_alg, mbedtls_md_type_t md_alg,
110                                         const char **oid, size_t *olen);
111 #endif /* MBEDTLS_X509_CRT_WRITE_C || MBEDTLS_X509_CSR_WRITE_C */
112 
113 #if (defined(MBEDTLS_X509_USE_C) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)) || \
114     defined(MBEDTLS_PKCS7_C)
115 #define MBEDTLS_X509_OID_HAVE_GET_MD_ALG
116 /**
117  * \brief          Translate hash algorithm OID into md_type
118  *
119  * \param oid      OID to use
120  * \param md_alg   place to store message digest algorithm
121  *
122  * \return         0 if successful, or MBEDTLS_ERR_X509_UNKNOWN_OID
123  */
124 int mbedtls_x509_oid_get_md_alg(const mbedtls_asn1_buf *oid, mbedtls_md_type_t *md_alg);
125 #endif /* MBEDTLS_X509_OID_HAVE_GET_MD_ALG */
126 
127 #if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
128 /**
129  * \brief          Translate Extended Key Usage OID into description
130  *
131  * \param oid      OID to use
132  * \param desc     place to store string pointer
133  *
134  * \return         0 if successful, or MBEDTLS_ERR_X509_UNKNOWN_OID
135  */
136 int mbedtls_x509_oid_get_extended_key_usage(const mbedtls_asn1_buf *oid, const char **desc);
137 
138 /**
139  * \brief          Translate certificate policies OID into description
140  *
141  * \param oid      OID to use
142  * \param desc     place to store string pointer
143  *
144  * \return         0 if successful, or MBEDTLS_ERR_X509_UNKNOWN_OID
145  */
146 int mbedtls_x509_oid_get_certificate_policies(const mbedtls_asn1_buf *oid, const char **desc);
147 #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_INFO */
148 
149 #ifdef __cplusplus
150 }
151 #endif
152 
153 #endif /* x509_oid.h */
154