blob: 6bea9e15237a1fd8bb65628f9c206f8ff05586da [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/**
Simon Butcher5b331b92016-01-03 16:14:14 +00002 * \file x509_csr.h
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
4 * \brief X.509 certificate signing request parsing and writing
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * 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.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#ifndef MBEDTLS_X509_CSR_H
23#define MBEDTLS_X509_CSR_H
Paul Bakker7c6b2c32013-09-16 13:49:26 +020024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amero6609aef2019-07-04 20:01:14 +010026#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030
Jaeden Amero6609aef2019-07-04 20:01:14 +010031#include "mbedtls/x509.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032
33#ifdef __cplusplus
34extern "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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049typedef struct mbedtls_x509_csr
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 mbedtls_x509_buf raw; /**< The raw CSR data (DER). */
52 mbedtls_x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020054 int version; /**< CSR version (1=v1). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). */
57 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 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 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067mbedtls_x509_csr;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068
69/**
70 * Container for writing a CSR
71 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072typedef struct mbedtls_x509write_csr
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_pk_context *key;
75 mbedtls_asn1_named_data *subject;
76 mbedtls_md_type_t md_alg;
77 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079mbedtls_x509write_csr;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082/**
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020083 * \brief Load a Certificate Signing Request (CSR) in DER format
84 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +000085 * \note CSR attributes (if any) are currently silently ignored.
86 *
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020087 * \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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020094 const unsigned char *buf, size_t buflen );
95
96/**
97 * \brief Load a Certificate Signing Request (CSR), DER or PEM format
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +000099 * \note See notes for \c mbedtls_x509_csr_parse_der()
100 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200101 * \param csr CSR context to fill
102 * \param buf buffer holding the CRL data
103 * \param buflen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200104 * (including the terminating null byte for PEM data)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105 *
106 * \return 0 if successful, or a specific X509 or PEM error code
107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111/**
112 * \brief Load a Certificate Signing Request (CSR)
113 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000114 * \note See notes for \c mbedtls_x509_csr_parse()
115 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200116 * \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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path );
122#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200123
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 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200133 * \return The length of the string written (not including the
134 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
137 const mbedtls_x509_csr *csr );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138
139/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200140 * \brief Initialize a CSR
141 *
142 * \param csr CSR to initialize
143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144void mbedtls_x509_csr_init( mbedtls_x509_csr *csr );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200145
146/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147 * \brief Unallocate all CSR data
148 *
149 * \param csr CSR to free
150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151void mbedtls_x509_csr_free( mbedtls_x509_csr *csr );
152#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153
Andrzej Kurek73afe272022-01-24 10:31:06 -0500154/** \} name Structures and functions for X.509 Certificate Signing Requests (CSR) */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_X509_CSR_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200157/**
158 * \brief Initialize a CSR context
159 *
160 * \param ctx CSR context to initialize
161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163
164/**
165 * \brief Set the subject name for a CSR
166 * Subject names should contain a comma-separated list
167 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000168 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200169 *
170 * \param ctx CSR context to use
171 * \param subject_name subject name to set
172 *
173 * \return 0 if subject name was parsed successfully, or
174 * a specific error code
175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100177 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200178
179/**
180 * \brief Set the key for a CSR (public key will be included,
181 * private key used to sign the CSR when writing it)
182 *
183 * \param ctx CSR context to use
184 * \param key Asymetric key to include
185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200187
188/**
189 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200191 *
192 * \param ctx CSR context to use
193 * \param md_alg MD algorithm to use
194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200196
197/**
198 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200200 *
201 * \param ctx CSR context to use
202 * \param key_usage key usage flags to set
203 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200204 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100205 *
206 * \note The <code>decipherOnly</code> flag from the Key Usage
207 * extension is represented by bit 8 (i.e.
208 * <code>0x8000</code>), which cannot typically be represented
209 * in an unsigned char. Therefore, the flag
210 * <code>decipherOnly</code> (i.e.
211 * #MBEDTLS_X509_KU_DECIPHER_ONLY) cannot be set using this
212 * function.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200215
216/**
217 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219 *
220 * \param ctx CSR context to use
221 * \param ns_cert_type Netscape Cert Type flags to set
222 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200223 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200226 unsigned char ns_cert_type );
227
228/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200229 * \brief Generic function to add to or replace an extension in the
230 * CSR
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200231 *
232 * \param ctx CSR context to use
233 * \param oid OID of the extension
234 * \param oid_len length of the OID
235 * \param val value of the extension OCTET STRING
236 * \param val_len length of the value data
237 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200238 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200241 const char *oid, size_t oid_len,
242 const unsigned char *val, size_t val_len );
243
244/**
245 * \brief Free the contents of a CSR context
246 *
247 * \param ctx CSR context to free
248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200250
251/**
252 * \brief Write a CSR (Certificate Signing Request) to a
253 * DER structure
254 * Note: data is written at the end of the buffer! Use the
255 * return value to determine where you should start
256 * using the buffer
257 *
258 * \param ctx CSR to write away
259 * \param buf buffer to write to
260 * \param size size of the buffer
261 * \param f_rng RNG function (for signature, see note)
262 * \param p_rng RNG parameter
263 *
264 * \return length of data written if successful, or a specific
265 * error code
266 *
267 * \note f_rng may be NULL if RSA is used for signature and the
268 * signature is made offline (otherwise f_rng is desirable
269 * for countermeasures against timing attacks).
270 * ECDSA signatures always require a non-NULL f_rng.
271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200273 int (*f_rng)(void *, unsigned char *, size_t),
274 void *p_rng );
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200277/**
278 * \brief Write a CSR (Certificate Signing Request) to a
279 * PEM string
280 *
281 * \param ctx CSR to write away
282 * \param buf buffer to write to
283 * \param size size of the buffer
284 * \param f_rng RNG function (for signature, see note)
285 * \param p_rng RNG parameter
286 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200287 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 *
289 * \note f_rng may be NULL if RSA is used for signature and the
290 * signature is made offline (otherwise f_rng is desirable
Andres AGcd153272016-10-04 12:06:50 +0100291 * for countermeasures against timing attacks).
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200292 * ECDSA signatures always require a non-NULL f_rng.
293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200295 int (*f_rng)(void *, unsigned char *, size_t),
296 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#endif /* MBEDTLS_PEM_WRITE_C */
298#endif /* MBEDTLS_X509_CSR_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200299
Andrzej Kurekff632d52022-01-24 10:32:00 -0500300/** \} addtogroup x509_module */
301
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200302#ifdef __cplusplus
303}
304#endif
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#endif /* mbedtls_x509_csr.h */