1 /**
2  * \file x509_csr.h
3  *
4  * \brief X.509 certificate signing request parsing and writing
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_CSR_H
23 #define MBEDTLS_X509_CSR_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 X.509 Certificate Signing Requests (CSR)
40  * \{
41  */
42 
43 /**
44  * Certificate Signing Request (CSR) structure.
45  *
46  * Some fields of this structure are publicly readable. Do not modify
47  * them except via Mbed TLS library functions: the effect of modifying
48  * those fields or the data that those fields point to is unspecified.
49  */
50 typedef struct mbedtls_x509_csr {
51     mbedtls_x509_buf raw;           /**< The raw CSR data (DER). */
52     mbedtls_x509_buf cri;           /**< The raw CertificateRequestInfo body (DER). */
53 
54     int version;            /**< CSR version (1=v1). */
55 
56     mbedtls_x509_buf  subject_raw;  /**< The raw subject data (DER). */
57     mbedtls_x509_name subject;      /**< The parsed subject data (named information object). */
58 
59     mbedtls_pk_context pk;          /**< Container for the public key context. */
60 
61     unsigned int key_usage;     /**< Optional key usage extension value: See the values in x509.h */
62     unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
63     mbedtls_x509_sequence subject_alt_names;    /**< Optional list of raw entries of Subject Alternative Names extension (currently only dNSName and OtherName are listed). */
64 
65     int MBEDTLS_PRIVATE(ext_types);              /**< Bit string containing detected and parsed extensions */
66 
67     mbedtls_x509_buf sig_oid;
68     mbedtls_x509_buf MBEDTLS_PRIVATE(sig);
69     mbedtls_md_type_t MBEDTLS_PRIVATE(sig_md);       /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
70     mbedtls_pk_type_t MBEDTLS_PRIVATE(sig_pk);       /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
71     void *MBEDTLS_PRIVATE(sig_opts);         /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
72 }
73 mbedtls_x509_csr;
74 
75 /**
76  * Container for writing a CSR
77  */
78 typedef struct mbedtls_x509write_csr {
79     mbedtls_pk_context *MBEDTLS_PRIVATE(key);
80     mbedtls_asn1_named_data *MBEDTLS_PRIVATE(subject);
81     mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg);
82     mbedtls_asn1_named_data *MBEDTLS_PRIVATE(extensions);
83 }
84 mbedtls_x509write_csr;
85 
86 typedef struct mbedtls_x509_san_list {
87     mbedtls_x509_subject_alternative_name node;
88     struct mbedtls_x509_san_list *next;
89 }
90 mbedtls_x509_san_list;
91 
92 #if defined(MBEDTLS_X509_CSR_PARSE_C)
93 /**
94  * \brief          Load a Certificate Signing Request (CSR) in DER format
95  *
96  * \note           CSR attributes (if any) are currently silently ignored.
97  *
98  * \note           If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
99  *                 subsystem must have been initialized by calling
100  *                 psa_crypto_init() before calling this function.
101  *
102  * \param csr      CSR context to fill
103  * \param buf      buffer holding the CRL data
104  * \param buflen   size of the buffer
105  *
106  * \return         0 if successful, or a specific X509 error code
107  */
108 int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr,
109                                const unsigned char *buf, size_t buflen);
110 
111 /**
112  * \brief          Load a Certificate Signing Request (CSR), DER or PEM format
113  *
114  * \note           See notes for \c mbedtls_x509_csr_parse_der()
115  *
116  * \note           If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
117  *                 subsystem must have been initialized by calling
118  *                 psa_crypto_init() before calling this function.
119  *
120  * \param csr      CSR context to fill
121  * \param buf      buffer holding the CRL data
122  * \param buflen   size of the buffer
123  *                 (including the terminating null byte for PEM data)
124  *
125  * \return         0 if successful, or a specific X509 or PEM error code
126  */
127 int mbedtls_x509_csr_parse(mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen);
128 
129 #if defined(MBEDTLS_FS_IO)
130 /**
131  * \brief          Load a Certificate Signing Request (CSR)
132  *
133  * \note           See notes for \c mbedtls_x509_csr_parse()
134  *
135  * \param csr      CSR context to fill
136  * \param path     filename to read the CSR from
137  *
138  * \return         0 if successful, or a specific X509 or PEM error code
139  */
140 int mbedtls_x509_csr_parse_file(mbedtls_x509_csr *csr, const char *path);
141 #endif /* MBEDTLS_FS_IO */
142 
143 #if !defined(MBEDTLS_X509_REMOVE_INFO)
144 /**
145  * \brief          Returns an informational string about the
146  *                 CSR.
147  *
148  * \param buf      Buffer to write to
149  * \param size     Maximum size of buffer
150  * \param prefix   A line prefix
151  * \param csr      The X509 CSR to represent
152  *
153  * \return         The length of the string written (not including the
154  *                 terminated nul byte), or a negative error code.
155  */
156 int mbedtls_x509_csr_info(char *buf, size_t size, const char *prefix,
157                           const mbedtls_x509_csr *csr);
158 #endif /* !MBEDTLS_X509_REMOVE_INFO */
159 
160 /**
161  * \brief          Initialize a CSR
162  *
163  * \param csr      CSR to initialize
164  */
165 void mbedtls_x509_csr_init(mbedtls_x509_csr *csr);
166 
167 /**
168  * \brief          Unallocate all CSR data
169  *
170  * \param csr      CSR to free
171  */
172 void mbedtls_x509_csr_free(mbedtls_x509_csr *csr);
173 #endif /* MBEDTLS_X509_CSR_PARSE_C */
174 
175 /** \} name Structures and functions for X.509 Certificate Signing Requests (CSR) */
176 
177 #if defined(MBEDTLS_X509_CSR_WRITE_C)
178 /**
179  * \brief           Initialize a CSR context
180  *
181  * \param ctx       CSR context to initialize
182  */
183 void mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx);
184 
185 /**
186  * \brief           Set the subject name for a CSR
187  *                  Subject names should contain a comma-separated list
188  *                  of OID types and values:
189  *                  e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
190  *
191  * \param ctx           CSR context to use
192  * \param subject_name  subject name to set
193  *
194  * \return          0 if subject name was parsed successfully, or
195  *                  a specific error code
196  */
197 int mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx,
198                                            const char *subject_name);
199 
200 /**
201  * \brief           Set the key for a CSR (public key will be included,
202  *                  private key used to sign the CSR when writing it)
203  *
204  * \param ctx       CSR context to use
205  * \param key       Asymmetric key to include
206  */
207 void mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx, mbedtls_pk_context *key);
208 
209 /**
210  * \brief           Set the MD algorithm to use for the signature
211  *                  (e.g. MBEDTLS_MD_SHA1)
212  *
213  * \param ctx       CSR context to use
214  * \param md_alg    MD algorithm to use
215  */
216 void mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg);
217 
218 /**
219  * \brief           Set the Key Usage Extension flags
220  *                  (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
221  *
222  * \param ctx       CSR context to use
223  * \param key_usage key usage flags to set
224  *
225  * \return          0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
226  *
227  * \note            The <code>decipherOnly</code> flag from the Key Usage
228  *                  extension is represented by bit 8 (i.e.
229  *                  <code>0x8000</code>), which cannot typically be represented
230  *                  in an unsigned char. Therefore, the flag
231  *                  <code>decipherOnly</code> (i.e.
232  *                  #MBEDTLS_X509_KU_DECIPHER_ONLY) cannot be set using this
233  *                  function.
234  */
235 int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage);
236 
237 /**
238  * \brief           Set Subject Alternative Name
239  *
240  * \param ctx       CSR context to use
241  * \param san_list  List of SAN values
242  *
243  * \return          0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
244  *
245  * \note            Only "dnsName", "uniformResourceIdentifier" and "otherName",
246  *                  as defined in RFC 5280, are supported.
247  */
248 int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ctx,
249                                                        const mbedtls_x509_san_list *san_list);
250 
251 /**
252  * \brief           Set the Netscape Cert Type flags
253  *                  (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
254  *
255  * \param ctx           CSR context to use
256  * \param ns_cert_type  Netscape Cert Type flags to set
257  *
258  * \return          0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
259  */
260 int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
261                                            unsigned char ns_cert_type);
262 
263 /**
264  * \brief           Generic function to add to or replace an extension in the
265  *                  CSR
266  *
267  * \param ctx       CSR context to use
268  * \param oid       OID of the extension
269  * \param oid_len   length of the OID
270  * \param critical  Set to 1 to mark the extension as critical, 0 otherwise.
271  * \param val       value of the extension OCTET STRING
272  * \param val_len   length of the value data
273  *
274  * \return          0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
275  */
276 int mbedtls_x509write_csr_set_extension(mbedtls_x509write_csr *ctx,
277                                         const char *oid, size_t oid_len,
278                                         int critical,
279                                         const unsigned char *val, size_t val_len);
280 
281 /**
282  * \brief           Free the contents of a CSR context
283  *
284  * \param ctx       CSR context to free
285  */
286 void mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx);
287 
288 /**
289  * \brief           Write a CSR (Certificate Signing Request) to a
290  *                  DER structure
291  *                  Note: data is written at the end of the buffer! Use the
292  *                        return value to determine where you should start
293  *                        using the buffer
294  *
295  * \param ctx       CSR to write away
296  * \param buf       buffer to write to
297  * \param size      size of the buffer
298  * \param f_rng     RNG function. This must not be \c NULL.
299  * \param p_rng     RNG parameter
300  *
301  * \return          length of data written if successful, or a specific
302  *                  error code
303  *
304  * \note            \p f_rng is used for the signature operation.
305  */
306 int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
307                               int (*f_rng)(void *, unsigned char *, size_t),
308                               void *p_rng);
309 
310 #if defined(MBEDTLS_PEM_WRITE_C)
311 /**
312  * \brief           Write a CSR (Certificate Signing Request) to a
313  *                  PEM string
314  *
315  * \param ctx       CSR to write away
316  * \param buf       buffer to write to
317  * \param size      size of the buffer
318  * \param f_rng     RNG function. This must not be \c NULL.
319  * \param p_rng     RNG parameter
320  *
321  * \return          0 if successful, or a specific error code
322  *
323  * \note            \p f_rng is used for the signature operation.
324  */
325 int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
326                               int (*f_rng)(void *, unsigned char *, size_t),
327                               void *p_rng);
328 #endif /* MBEDTLS_PEM_WRITE_C */
329 #endif /* MBEDTLS_X509_CSR_WRITE_C */
330 
331 /** \} addtogroup x509_module */
332 
333 #ifdef __cplusplus
334 }
335 #endif
336 
337 #endif /* mbedtls_x509_csr.h */
338