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 OR GPL-2.0-or-later
9  *
10  *  This file is provided under the Apache License 2.0, or the
11  *  GNU General Public License v2.0 or later.
12  *
13  *  **********
14  *  Apache License 2.0:
15  *
16  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
17  *  not use this file except in compliance with the License.
18  *  You may obtain a copy of the License at
19  *
20  *  http://www.apache.org/licenses/LICENSE-2.0
21  *
22  *  Unless required by applicable law or agreed to in writing, software
23  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  *  See the License for the specific language governing permissions and
26  *  limitations under the License.
27  *
28  *  **********
29  *
30  *  **********
31  *  GNU General Public License v2.0 or later:
32  *
33  *  This program is free software; you can redistribute it and/or modify
34  *  it under the terms of the GNU General Public License as published by
35  *  the Free Software Foundation; either version 2 of the License, or
36  *  (at your option) any later version.
37  *
38  *  This program is distributed in the hope that it will be useful,
39  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
40  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41  *  GNU General Public License for more details.
42  *
43  *  You should have received a copy of the GNU General Public License along
44  *  with this program; if not, write to the Free Software Foundation, Inc.,
45  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46  *
47  *  **********
48  */
49 #ifndef MBEDTLS_X509_CSR_H
50 #define MBEDTLS_X509_CSR_H
51 
52 #if !defined(MBEDTLS_CONFIG_FILE)
53 #include "config.h"
54 #else
55 #include MBEDTLS_CONFIG_FILE
56 #endif
57 
58 #include "x509.h"
59 
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 
64 /**
65  * \addtogroup x509_module
66  * \{ */
67 
68 /**
69  * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
70  * \{
71  */
72 
73 /**
74  * Certificate Signing Request (CSR) structure.
75  */
76 typedef struct mbedtls_x509_csr
77 {
78     mbedtls_x509_buf raw;           /**< The raw CSR data (DER). */
79     mbedtls_x509_buf cri;           /**< The raw CertificateRequestInfo body (DER). */
80 
81     int version;            /**< CSR version (1=v1). */
82 
83     mbedtls_x509_buf  subject_raw;  /**< The raw subject data (DER). */
84     mbedtls_x509_name subject;      /**< The parsed subject data (named information object). */
85 
86     mbedtls_pk_context pk;          /**< Container for the public key context. */
87 
88     mbedtls_x509_buf sig_oid;
89     mbedtls_x509_buf sig;
90     mbedtls_md_type_t sig_md;       /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
91     mbedtls_pk_type_t sig_pk;       /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
92     void *sig_opts;         /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
93 }
94 mbedtls_x509_csr;
95 
96 /**
97  * Container for writing a CSR
98  */
99 typedef struct mbedtls_x509write_csr
100 {
101     mbedtls_pk_context *key;
102     mbedtls_asn1_named_data *subject;
103     mbedtls_md_type_t md_alg;
104     mbedtls_asn1_named_data *extensions;
105 }
106 mbedtls_x509write_csr;
107 
108 #if defined(MBEDTLS_X509_CSR_PARSE_C)
109 /**
110  * \brief          Load a Certificate Signing Request (CSR) in DER format
111  *
112  * \note           CSR attributes (if any) are currently silently ignored.
113  *
114  * \param csr      CSR context to fill
115  * \param buf      buffer holding the CRL data
116  * \param buflen   size of the buffer
117  *
118  * \return         0 if successful, or a specific X509 error code
119  */
120 int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
121                         const unsigned char *buf, size_t buflen );
122 
123 /**
124  * \brief          Load a Certificate Signing Request (CSR), DER or PEM format
125  *
126  * \note           See notes for \c mbedtls_x509_csr_parse_der()
127  *
128  * \param csr      CSR context to fill
129  * \param buf      buffer holding the CRL data
130  * \param buflen   size of the buffer
131  *                 (including the terminating null byte for PEM data)
132  *
133  * \return         0 if successful, or a specific X509 or PEM error code
134  */
135 int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen );
136 
137 #if defined(MBEDTLS_FS_IO)
138 /**
139  * \brief          Load a Certificate Signing Request (CSR)
140  *
141  * \note           See notes for \c mbedtls_x509_csr_parse()
142  *
143  * \param csr      CSR context to fill
144  * \param path     filename to read the CSR from
145  *
146  * \return         0 if successful, or a specific X509 or PEM error code
147  */
148 int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path );
149 #endif /* MBEDTLS_FS_IO */
150 
151 /**
152  * \brief          Returns an informational string about the
153  *                 CSR.
154  *
155  * \param buf      Buffer to write to
156  * \param size     Maximum size of buffer
157  * \param prefix   A line prefix
158  * \param csr      The X509 CSR to represent
159  *
160  * \return         The length of the string written (not including the
161  *                 terminated nul byte), or a negative error code.
162  */
163 int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
164                    const mbedtls_x509_csr *csr );
165 
166 /**
167  * \brief          Initialize a CSR
168  *
169  * \param csr      CSR to initialize
170  */
171 void mbedtls_x509_csr_init( mbedtls_x509_csr *csr );
172 
173 /**
174  * \brief          Unallocate all CSR data
175  *
176  * \param csr      CSR to free
177  */
178 void mbedtls_x509_csr_free( mbedtls_x509_csr *csr );
179 #endif /* MBEDTLS_X509_CSR_PARSE_C */
180 
181 /* \} name */
182 /* \} addtogroup x509_module */
183 
184 #if defined(MBEDTLS_X509_CSR_WRITE_C)
185 /**
186  * \brief           Initialize a CSR context
187  *
188  * \param ctx       CSR context to initialize
189  */
190 void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx );
191 
192 /**
193  * \brief           Set the subject name for a CSR
194  *                  Subject names should contain a comma-separated list
195  *                  of OID types and values:
196  *                  e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
197  *
198  * \param ctx           CSR context to use
199  * \param subject_name  subject name to set
200  *
201  * \return          0 if subject name was parsed successfully, or
202  *                  a specific error code
203  */
204 int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
205                                     const char *subject_name );
206 
207 /**
208  * \brief           Set the key for a CSR (public key will be included,
209  *                  private key used to sign the CSR when writing it)
210  *
211  * \param ctx       CSR context to use
212  * \param key       Asymetric key to include
213  */
214 void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key );
215 
216 /**
217  * \brief           Set the MD algorithm to use for the signature
218  *                  (e.g. MBEDTLS_MD_SHA1)
219  *
220  * \param ctx       CSR context to use
221  * \param md_alg    MD algorithm to use
222  */
223 void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg );
224 
225 /**
226  * \brief           Set the Key Usage Extension flags
227  *                  (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
228  *
229  * \param ctx       CSR context to use
230  * \param key_usage key usage flags to set
231  *
232  * \return          0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
233  *
234  * \note            The <code>decipherOnly</code> flag from the Key Usage
235  *                  extension is represented by bit 8 (i.e.
236  *                  <code>0x8000</code>), which cannot typically be represented
237  *                  in an unsigned char. Therefore, the flag
238  *                  <code>decipherOnly</code> (i.e.
239  *                  #MBEDTLS_X509_KU_DECIPHER_ONLY) cannot be set using this
240  *                  function.
241  */
242 int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage );
243 
244 /**
245  * \brief           Set the Netscape Cert Type flags
246  *                  (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
247  *
248  * \param ctx           CSR context to use
249  * \param ns_cert_type  Netscape Cert Type flags to set
250  *
251  * \return          0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
252  */
253 int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
254                                     unsigned char ns_cert_type );
255 
256 /**
257  * \brief           Generic function to add to or replace an extension in the
258  *                  CSR
259  *
260  * \param ctx       CSR context to use
261  * \param oid       OID of the extension
262  * \param oid_len   length of the OID
263  * \param val       value of the extension OCTET STRING
264  * \param val_len   length of the value data
265  *
266  * \return          0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
267  */
268 int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
269                                  const char *oid, size_t oid_len,
270                                  const unsigned char *val, size_t val_len );
271 
272 /**
273  * \brief           Free the contents of a CSR context
274  *
275  * \param ctx       CSR context to free
276  */
277 void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx );
278 
279 /**
280  * \brief           Write a CSR (Certificate Signing Request) to a
281  *                  DER structure
282  *                  Note: data is written at the end of the buffer! Use the
283  *                        return value to determine where you should start
284  *                        using the buffer
285  *
286  * \param ctx       CSR to write away
287  * \param buf       buffer to write to
288  * \param size      size of the buffer
289  * \param f_rng     RNG function (for signature, see note)
290  * \param p_rng     RNG parameter
291  *
292  * \return          length of data written if successful, or a specific
293  *                  error code
294  *
295  * \note            f_rng may be NULL if RSA is used for signature and the
296  *                  signature is made offline (otherwise f_rng is desirable
297  *                  for countermeasures against timing attacks).
298  *                  ECDSA signatures always require a non-NULL f_rng.
299  */
300 int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
301                        int (*f_rng)(void *, unsigned char *, size_t),
302                        void *p_rng );
303 
304 #if defined(MBEDTLS_PEM_WRITE_C)
305 /**
306  * \brief           Write a CSR (Certificate Signing Request) to a
307  *                  PEM string
308  *
309  * \param ctx       CSR to write away
310  * \param buf       buffer to write to
311  * \param size      size of the buffer
312  * \param f_rng     RNG function (for signature, see note)
313  * \param p_rng     RNG parameter
314  *
315  * \return          0 if successful, or a specific error code
316  *
317  * \note            f_rng may be NULL if RSA is used for signature and the
318  *                  signature is made offline (otherwise f_rng is desirable
319  *                  for countermeasures against timing attacks).
320  *                  ECDSA signatures always require a non-NULL f_rng.
321  */
322 int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
323                        int (*f_rng)(void *, unsigned char *, size_t),
324                        void *p_rng );
325 #endif /* MBEDTLS_PEM_WRITE_C */
326 #endif /* MBEDTLS_X509_CSR_WRITE_C */
327 
328 #ifdef __cplusplus
329 }
330 #endif
331 
332 #endif /* mbedtls_x509_csr.h */
333