1 /**
2  * \file x509_csr.h
3  *
4  * \brief X.509 certificate signing request parsing and writing
5  */
6 /*
7  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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  *  This file is part of mbed TLS (https://tls.mbed.org)
23  */
24 #ifndef MBEDTLS_X509_CSR_H
25 #define MBEDTLS_X509_CSR_H
26 
27 #if !defined(MBEDTLS_CONFIG_FILE)
28 #include "config.h"
29 #else
30 #include MBEDTLS_CONFIG_FILE
31 #endif
32 
33 #include "x509.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /**
40  * \addtogroup x509_module
41  * \{ */
42 
43 /**
44  * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
45  * \{
46  */
47 
48 /**
49  * Certificate Signing Request (CSR) structure.
50  */
51 typedef struct mbedtls_x509_csr
52 {
53     mbedtls_x509_buf raw;           /**< The raw CSR data (DER). */
54     mbedtls_x509_buf cri;           /**< The raw CertificateRequestInfo body (DER). */
55 
56     int version;            /**< CSR version (1=v1). */
57 
58     mbedtls_x509_buf  subject_raw;  /**< The raw subject data (DER). */
59     mbedtls_x509_name subject;      /**< The parsed subject data (named information object). */
60 
61     mbedtls_pk_context pk;          /**< Container for the public key context. */
62 
63     mbedtls_x509_buf sig_oid;
64     mbedtls_x509_buf sig;
65     mbedtls_md_type_t sig_md;       /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
66     mbedtls_pk_type_t sig_pk;       /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
67     void *sig_opts;         /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
68 }
69 mbedtls_x509_csr;
70 
71 /**
72  * Container for writing a CSR
73  */
74 typedef struct mbedtls_x509write_csr
75 {
76     mbedtls_pk_context *key;
77     mbedtls_asn1_named_data *subject;
78     mbedtls_md_type_t md_alg;
79     mbedtls_asn1_named_data *extensions;
80 }
81 mbedtls_x509write_csr;
82 
83 #if defined(MBEDTLS_X509_CSR_PARSE_C)
84 /**
85  * \brief          Load a Certificate Signing Request (CSR) in DER format
86  *
87  * \note           CSR attributes (if any) are currently silently ignored.
88  *
89  * \param csr      CSR context to fill
90  * \param buf      buffer holding the CRL data
91  * \param buflen   size of the buffer
92  *
93  * \return         0 if successful, or a specific X509 error code
94  */
95 int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
96                         const unsigned char *buf, size_t buflen );
97 
98 /**
99  * \brief          Load a Certificate Signing Request (CSR), DER or PEM format
100  *
101  * \note           See notes for \c mbedtls_x509_csr_parse_der()
102  *
103  * \param csr      CSR context to fill
104  * \param buf      buffer holding the CRL data
105  * \param buflen   size of the buffer
106  *                 (including the terminating null byte for PEM data)
107  *
108  * \return         0 if successful, or a specific X509 or PEM error code
109  */
110 int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen );
111 
112 #if defined(MBEDTLS_FS_IO)
113 /**
114  * \brief          Load a Certificate Signing Request (CSR)
115  *
116  * \note           See notes for \c mbedtls_x509_csr_parse()
117  *
118  * \param csr      CSR context to fill
119  * \param path     filename to read the CSR from
120  *
121  * \return         0 if successful, or a specific X509 or PEM error code
122  */
123 int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path );
124 #endif /* MBEDTLS_FS_IO */
125 
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 
141 /**
142  * \brief          Initialize a CSR
143  *
144  * \param csr      CSR to initialize
145  */
146 void mbedtls_x509_csr_init( mbedtls_x509_csr *csr );
147 
148 /**
149  * \brief          Unallocate all CSR data
150  *
151  * \param csr      CSR to free
152  */
153 void mbedtls_x509_csr_free( mbedtls_x509_csr *csr );
154 #endif /* MBEDTLS_X509_CSR_PARSE_C */
155 
156 /* \} name */
157 /* \} addtogroup x509_module */
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       Asymetric 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 int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage );
210 
211 /**
212  * \brief           Set the Netscape Cert Type flags
213  *                  (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
214  *
215  * \param ctx           CSR context to use
216  * \param ns_cert_type  Netscape Cert Type flags to set
217  *
218  * \return          0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
219  */
220 int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
221                                     unsigned char ns_cert_type );
222 
223 /**
224  * \brief           Generic function to add to or replace an extension in the
225  *                  CSR
226  *
227  * \param ctx       CSR context to use
228  * \param oid       OID of the extension
229  * \param oid_len   length of the OID
230  * \param val       value of the extension OCTET STRING
231  * \param val_len   length of the value data
232  *
233  * \return          0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
234  */
235 int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
236                                  const char *oid, size_t oid_len,
237                                  const unsigned char *val, size_t val_len );
238 
239 /**
240  * \brief           Free the contents of a CSR context
241  *
242  * \param ctx       CSR context to free
243  */
244 void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx );
245 
246 /**
247  * \brief           Write a CSR (Certificate Signing Request) to a
248  *                  DER structure
249  *                  Note: data is written at the end of the buffer! Use the
250  *                        return value to determine where you should start
251  *                        using the buffer
252  *
253  * \param ctx       CSR to write away
254  * \param buf       buffer to write to
255  * \param size      size of the buffer
256  * \param f_rng     RNG function (for signature, see note)
257  * \param p_rng     RNG parameter
258  *
259  * \return          length of data written if successful, or a specific
260  *                  error code
261  *
262  * \note            f_rng may be NULL if RSA is used for signature and the
263  *                  signature is made offline (otherwise f_rng is desirable
264  *                  for countermeasures against timing attacks).
265  *                  ECDSA signatures always require a non-NULL f_rng.
266  */
267 int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
268                        int (*f_rng)(void *, unsigned char *, size_t),
269                        void *p_rng );
270 
271 #if defined(MBEDTLS_PEM_WRITE_C)
272 /**
273  * \brief           Write a CSR (Certificate Signing Request) to a
274  *                  PEM string
275  *
276  * \param ctx       CSR to write away
277  * \param buf       buffer to write to
278  * \param size      size of the buffer
279  * \param f_rng     RNG function (for signature, see note)
280  * \param p_rng     RNG parameter
281  *
282  * \return          0 if successful, or a specific error code
283  *
284  * \note            f_rng may be NULL if RSA is used for signature and the
285  *                  signature is made offline (otherwise f_rng is desirable
286  *                  for countermeasures against timing attacks).
287  *                  ECDSA signatures always require a non-NULL f_rng.
288  */
289 int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
290                        int (*f_rng)(void *, unsigned char *, size_t),
291                        void *p_rng );
292 #endif /* MBEDTLS_PEM_WRITE_C */
293 #endif /* MBEDTLS_X509_CSR_WRITE_C */
294 
295 #ifdef __cplusplus
296 }
297 #endif
298 
299 #endif /* mbedtls_x509_csr.h */
300