blob: 97a9db44c75e3586681e57407f46a7c694758fbe [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker7c6b2c32013-09-16 13:49:26 +02009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_X509_CSR_H
11#define MBEDTLS_X509_CSR_H
Paul Bakker7c6b2c32013-09-16 13:49:26 +020012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amero6609aef2019-07-04 20:01:14 +010014#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020015#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020017#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018
Jaeden Amero6609aef2019-07-04 20:01:14 +010019#include "mbedtls/x509.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25/**
26 * \addtogroup x509_module
27 * \{ */
28
29/**
30 * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
31 * \{
32 */
33
34/**
35 * Certificate Signing Request (CSR) structure.
36 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010037typedef struct mbedtls_x509_csr {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038 mbedtls_x509_buf raw; /**< The raw CSR data (DER). */
39 mbedtls_x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020040
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020041 int version; /**< CSR version (1=v1). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). */
44 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_x509_buf sig_oid;
49 mbedtls_x509_buf sig;
50 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
51 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
52 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 +020053}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054mbedtls_x509_csr;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055
56/**
57 * Container for writing a CSR
58 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010059typedef struct mbedtls_x509write_csr {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_pk_context *key;
61 mbedtls_asn1_named_data *subject;
62 mbedtls_md_type_t md_alg;
63 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065mbedtls_x509write_csr;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068/**
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020069 * \brief Load a Certificate Signing Request (CSR) in DER format
70 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +000071 * \note CSR attributes (if any) are currently silently ignored.
72 *
Gilles Peskine07ae2082023-03-07 20:22:51 +010073 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
74 * subsystem must have been initialized by calling
75 * psa_crypto_init() before calling this function.
76 *
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020077 * \param csr CSR context to fill
78 * \param buf buffer holding the CRL data
79 * \param buflen size of the buffer
80 *
81 * \return 0 if successful, or a specific X509 error code
82 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr,
84 const unsigned char *buf, size_t buflen);
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020085
86/**
87 * \brief Load a Certificate Signing Request (CSR), DER or PEM format
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +000089 * \note See notes for \c mbedtls_x509_csr_parse_der()
90 *
Gilles Peskine07ae2082023-03-07 20:22:51 +010091 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
92 * subsystem must have been initialized by calling
93 * psa_crypto_init() before calling this function.
94 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095 * \param csr CSR context to fill
96 * \param buf buffer holding the CRL data
97 * \param buflen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020098 * (including the terminating null byte for PEM data)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099 *
100 * \return 0 if successful, or a specific X509 or PEM error code
101 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102int mbedtls_x509_csr_parse(mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105/**
106 * \brief Load a Certificate Signing Request (CSR)
107 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000108 * \note See notes for \c mbedtls_x509_csr_parse()
109 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110 * \param csr CSR context to fill
111 * \param path filename to read the CSR from
112 *
113 * \return 0 if successful, or a specific X509 or PEM error code
114 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115int mbedtls_x509_csr_parse_file(mbedtls_x509_csr *csr, const char *path);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117
118/**
119 * \brief Returns an informational string about the
120 * CSR.
121 *
122 * \param buf Buffer to write to
123 * \param size Maximum size of buffer
124 * \param prefix A line prefix
125 * \param csr The X509 CSR to represent
126 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200127 * \return The length of the string written (not including the
128 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200129 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130int mbedtls_x509_csr_info(char *buf, size_t size, const char *prefix,
131 const mbedtls_x509_csr *csr);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200132
133/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200134 * \brief Initialize a CSR
135 *
136 * \param csr CSR to initialize
137 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138void mbedtls_x509_csr_init(mbedtls_x509_csr *csr);
Paul Bakker369d2eb2013-09-18 11:58:25 +0200139
140/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200141 * \brief Unallocate all CSR data
142 *
143 * \param csr CSR to free
144 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145void mbedtls_x509_csr_free(mbedtls_x509_csr *csr);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147
Andrzej Kurek73afe272022-01-24 10:31:06 -0500148/** \} name Structures and functions for X.509 Certificate Signing Requests (CSR) */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#if defined(MBEDTLS_X509_CSR_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200151/**
152 * \brief Initialize a CSR context
153 *
154 * \param ctx CSR context to initialize
155 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156void mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200157
158/**
159 * \brief Set the subject name for a CSR
160 * Subject names should contain a comma-separated list
161 * of OID types and values:
Gilles Peskinef08ca832023-09-12 19:21:54 +0200162 * e.g. "C=UK,O=ARM,CN=Mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163 *
164 * \param ctx CSR context to use
165 * \param subject_name subject name to set
166 *
167 * \return 0 if subject name was parsed successfully, or
168 * a specific error code
169 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170int mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx,
171 const char *subject_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172
173/**
174 * \brief Set the key for a CSR (public key will be included,
175 * private key used to sign the CSR when writing it)
176 *
177 * \param ctx CSR context to use
Shaun Case0e7791f2021-12-20 21:14:10 -0800178 * \param key Asymmetric key to include
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200179 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180void mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx, mbedtls_pk_context *key);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200181
182/**
183 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200185 *
186 * \param ctx CSR context to use
187 * \param md_alg MD algorithm to use
188 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189void mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200190
191/**
192 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200194 *
195 * \param ctx CSR context to use
196 * \param key_usage key usage flags to set
197 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200198 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100199 *
200 * \note The <code>decipherOnly</code> flag from the Key Usage
201 * extension is represented by bit 8 (i.e.
202 * <code>0x8000</code>), which cannot typically be represented
203 * in an unsigned char. Therefore, the flag
204 * <code>decipherOnly</code> (i.e.
205 * #MBEDTLS_X509_KU_DECIPHER_ONLY) cannot be set using this
206 * function.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200207 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200209
210/**
211 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200213 *
214 * \param ctx CSR context to use
215 * \param ns_cert_type Netscape Cert Type flags to set
216 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200217 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200218 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
220 unsigned char ns_cert_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221
222/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200223 * \brief Generic function to add to or replace an extension in the
224 * CSR
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200225 *
226 * \param ctx CSR context to use
227 * \param oid OID of the extension
228 * \param oid_len length of the OID
229 * \param val value of the extension OCTET STRING
230 * \param val_len length of the value data
231 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200232 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200233 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234int mbedtls_x509write_csr_set_extension(mbedtls_x509write_csr *ctx,
235 const char *oid, size_t oid_len,
236 const unsigned char *val, size_t val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200237
238/**
239 * \brief Free the contents of a CSR context
240 *
241 * \param ctx CSR context to free
242 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243void mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200244
245/**
246 * \brief Write a CSR (Certificate Signing Request) to a
247 * DER structure
248 * Note: data is written at the end of the buffer! Use the
249 * return value to determine where you should start
250 * using the buffer
251 *
252 * \param ctx CSR to write away
253 * \param buf buffer to write to
254 * \param size size of the buffer
255 * \param f_rng RNG function (for signature, see note)
256 * \param p_rng RNG parameter
257 *
258 * \return length of data written if successful, or a specific
259 * error code
260 *
261 * \note f_rng may be NULL if RSA is used for signature and the
262 * signature is made offline (otherwise f_rng is desirable
263 * for countermeasures against timing attacks).
264 * ECDSA signatures always require a non-NULL f_rng.
265 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
267 int (*f_rng)(void *, unsigned char *, size_t),
268 void *p_rng);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271/**
272 * \brief Write a CSR (Certificate Signing Request) to a
273 * PEM string
274 *
275 * \param ctx CSR to write away
276 * \param buf buffer to write to
277 * \param size size of the buffer
278 * \param f_rng RNG function (for signature, see note)
279 * \param p_rng RNG parameter
280 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200281 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200282 *
283 * \note f_rng may be NULL if RSA is used for signature and the
284 * signature is made offline (otherwise f_rng is desirable
Andres AGcd153272016-10-04 12:06:50 +0100285 * for countermeasures against timing attacks).
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286 * ECDSA signatures always require a non-NULL f_rng.
287 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
289 int (*f_rng)(void *, unsigned char *, size_t),
290 void *p_rng);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#endif /* MBEDTLS_PEM_WRITE_C */
292#endif /* MBEDTLS_X509_CSR_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293
Andrzej Kurekff632d52022-01-24 10:32:00 -0500294/** \} addtogroup x509_module */
295
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296#ifdef __cplusplus
297}
298#endif
299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#endif /* mbedtls_x509_csr.h */