1 /**
2  * \file x509_crl.h
3  *
4  * \brief X.509 certificate revocation list parsing
5  */
6 /*
7  *  Copyright The Mbed TLS Contributors
8  *  SPDX-License-Identifier: Apache-2.0
9  *
10  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
11  *  not use this file except in compliance with the License.
12  *  You may obtain a copy of the License at
13  *
14  *  http://www.apache.org/licenses/LICENSE-2.0
15  *
16  *  Unless required by applicable law or agreed to in writing, software
17  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  *  See the License for the specific language governing permissions and
20  *  limitations under the License.
21  */
22 #ifndef MBEDTLS_X509_CRL_H
23 #define MBEDTLS_X509_CRL_H
24 #include "mbedtls/private_access.h"
25 
26 #include "mbedtls/build_info.h"
27 
28 #include "mbedtls/x509.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /**
35  * \addtogroup x509_module
36  * \{ */
37 
38 /**
39  * \name Structures and functions for parsing CRLs
40  * \{
41  */
42 
43 /**
44  * Certificate revocation list entry.
45  * Contains the CA-specific serial numbers and revocation dates.
46  *
47  * Some fields of this structure are publicly readable. Do not modify
48  * them except via Mbed TLS library functions: the effect of modifying
49  * those fields or the data that those fields points to is unspecified.
50  */
51 typedef struct mbedtls_x509_crl_entry
52 {
53     /** Direct access to the whole entry inside the containing buffer. */
54     mbedtls_x509_buf raw;
55     /** The serial number of the revoked certificate. */
56     mbedtls_x509_buf serial;
57     /** The revocation date of this entry. */
58     mbedtls_x509_time revocation_date;
59     /** Direct access to the list of CRL entry extensions
60      * (an ASN.1 constructed sequence).
61      *
62      * If there are no extensions, `entry_ext.len == 0` and
63      * `entry_ext.p == NULL`. */
64     mbedtls_x509_buf entry_ext;
65 
66     /** Next element in the linked list of entries.
67      * \p NULL indicates the end of the list.
68      * Do not modify this field directly. */
69     struct mbedtls_x509_crl_entry *next;
70 }
71 mbedtls_x509_crl_entry;
72 
73 /**
74  * Certificate revocation list structure.
75  * Every CRL may have multiple entries.
76  */
77 typedef struct mbedtls_x509_crl
78 {
79     mbedtls_x509_buf raw;           /**< The raw certificate data (DER). */
80     mbedtls_x509_buf tbs;           /**< The raw certificate body (DER). The part that is To Be Signed. */
81 
82     int version;            /**< CRL version (1=v1, 2=v2) */
83     mbedtls_x509_buf sig_oid;       /**< CRL signature type identifier */
84 
85     mbedtls_x509_buf issuer_raw;    /**< The raw issuer data (DER). */
86 
87     mbedtls_x509_name issuer;       /**< The parsed issuer data (named information object). */
88 
89     mbedtls_x509_time this_update;
90     mbedtls_x509_time next_update;
91 
92     mbedtls_x509_crl_entry entry;   /**< The CRL entries containing the certificate revocation times for this CA. */
93 
94     mbedtls_x509_buf crl_ext;
95 
96     mbedtls_x509_buf MBEDTLS_PRIVATE(sig_oid2);
97     mbedtls_x509_buf MBEDTLS_PRIVATE(sig);
98     mbedtls_md_type_t MBEDTLS_PRIVATE(sig_md);           /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
99     mbedtls_pk_type_t MBEDTLS_PRIVATE(sig_pk);           /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
100     void *MBEDTLS_PRIVATE(sig_opts);             /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
101 
102     /** Next element in the linked list of CRL.
103      * \p NULL indicates the end of the list.
104      * Do not modify this field directly. */
105     struct mbedtls_x509_crl *next;
106 }
107 mbedtls_x509_crl;
108 
109 /**
110  * \brief          Parse a DER-encoded CRL and append it to the chained list
111  *
112  * \param chain    points to the start of the chain
113  * \param buf      buffer holding the CRL data in DER format
114  * \param buflen   size of the buffer
115  *                 (including the terminating null byte for PEM data)
116  *
117  * \return         0 if successful, or a specific X509 or PEM error code
118  */
119 int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain,
120                         const unsigned char *buf, size_t buflen );
121 /**
122  * \brief          Parse one or more CRLs and append them to the chained list
123  *
124  * \note           Multiple CRLs are accepted only if using PEM format
125  *
126  * \param chain    points to the start of the chain
127  * \param buf      buffer holding the CRL data in PEM or DER format
128  * \param buflen   size of the buffer
129  *                 (including the terminating null byte for PEM data)
130  *
131  * \return         0 if successful, or a specific X509 or PEM error code
132  */
133 int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen );
134 
135 #if defined(MBEDTLS_FS_IO)
136 /**
137  * \brief          Load one or more CRLs and append them to the chained list
138  *
139  * \note           Multiple CRLs are accepted only if using PEM format
140  *
141  * \param chain    points to the start of the chain
142  * \param path     filename to read the CRLs from (in PEM or DER encoding)
143  *
144  * \return         0 if successful, or a specific X509 or PEM error code
145  */
146 int mbedtls_x509_crl_parse_file( mbedtls_x509_crl *chain, const char *path );
147 #endif /* MBEDTLS_FS_IO */
148 
149 #if !defined(MBEDTLS_X509_REMOVE_INFO)
150 /**
151  * \brief          Returns an informational string about the CRL.
152  *
153  * \param buf      Buffer to write to
154  * \param size     Maximum size of buffer
155  * \param prefix   A line prefix
156  * \param crl      The X509 CRL to represent
157  *
158  * \return         The length of the string written (not including the
159  *                 terminated nul byte), or a negative error code.
160  */
161 int mbedtls_x509_crl_info( char *buf, size_t size, const char *prefix,
162                    const mbedtls_x509_crl *crl );
163 #endif /* !MBEDTLS_X509_REMOVE_INFO */
164 
165 /**
166  * \brief          Initialize a CRL (chain)
167  *
168  * \param crl      CRL chain to initialize
169  */
170 void mbedtls_x509_crl_init( mbedtls_x509_crl *crl );
171 
172 /**
173  * \brief          Unallocate all CRL data
174  *
175  * \param crl      CRL chain to free
176  */
177 void mbedtls_x509_crl_free( mbedtls_x509_crl *crl );
178 
179 /** \} name Structures and functions for parsing CRLs */
180 /** \} addtogroup x509_module */
181 
182 #ifdef __cplusplus
183 }
184 #endif
185 
186 #endif /* mbedtls_x509_crl.h */
187