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