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