blob: 55dc9b82b4ead87408677fc07823c59f41af2276 [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
Mateusz Starzyk846f0212021-05-19 19:44:07 +020024#include "mbedtls/private_access.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027
Jaeden Amero6609aef2019-07-04 20:01:14 +010028#include "mbedtls/x509.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020029
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34/**
35 * \addtogroup x509_module
36 * \{ */
37
38/**
39 * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
40 * \{
41 */
42
43/**
44 * Certificate Signing Request (CSR) structure.
45 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020046typedef struct mbedtls_x509_csr {
47 mbedtls_x509_buf MBEDTLS_PRIVATE(raw); /**< The raw CSR data (DER). */
48 mbedtls_x509_buf MBEDTLS_PRIVATE(cri); /**< The raw CertificateRequestInfo
49 body (DER). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020051 int MBEDTLS_PRIVATE(version); /**< CSR version (1=v1). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053 mbedtls_x509_buf MBEDTLS_PRIVATE(subject_raw); /**< The raw subject data
54 (DER). */
55 mbedtls_x509_name MBEDTLS_PRIVATE(subject); /**< The parsed subject data
56 (named information object).
57 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020059 mbedtls_pk_context MBEDTLS_PRIVATE(pk); /**< Container for the public key
60 context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061
Mateusz Starzyk846f0212021-05-19 19:44:07 +020062 mbedtls_x509_buf MBEDTLS_PRIVATE(sig_oid);
63 mbedtls_x509_buf MBEDTLS_PRIVATE(sig);
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020064 mbedtls_md_type_t MBEDTLS_PRIVATE(sig_md); /**< Internal representation of
65 the MD algorithm of the
66 signature algorithm, e.g.
67 MBEDTLS_MD_SHA256 */
68 mbedtls_pk_type_t MBEDTLS_PRIVATE(sig_pk); /**< Internal representation of
69 the Public Key algorithm of
70 the signature algorithm, e.g.
71 MBEDTLS_PK_RSA */
72 void *MBEDTLS_PRIVATE(sig_opts); /**< Signature options to be passed to
73 mbedtls_pk_verify_ext(), e.g. for
74 RSASSA-PSS */
75} mbedtls_x509_csr;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076
77/**
78 * Container for writing a CSR
79 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020080typedef struct mbedtls_x509write_csr {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020081 mbedtls_pk_context *MBEDTLS_PRIVATE(key);
82 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(subject);
83 mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg);
84 mbedtls_asn1_named_data *MBEDTLS_PRIVATE(extensions);
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020085} mbedtls_x509write_csr;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088/**
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020089 * \brief Load a Certificate Signing Request (CSR) in DER format
90 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +000091 * \note CSR attributes (if any) are currently silently ignored.
92 *
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020093 * \param csr CSR context to fill
94 * \param buf buffer holding the CRL data
95 * \param buflen size of the buffer
96 *
97 * \return 0 if successful, or a specific X509 error code
98 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020099int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr,
100 const unsigned char *buf,
101 size_t buflen);
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200102
103/**
104 * \brief Load a Certificate Signing Request (CSR), DER or PEM format
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000106 * \note See notes for \c mbedtls_x509_csr_parse_der()
107 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200108 * \param csr CSR context to fill
109 * \param buf buffer holding the CRL data
110 * \param buflen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200111 * (including the terminating null byte for PEM data)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112 *
113 * \return 0 if successful, or a specific X509 or PEM error code
114 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200115int mbedtls_x509_csr_parse(mbedtls_x509_csr *csr,
116 const unsigned char *buf,
117 size_t buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200118
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200119# if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120/**
121 * \brief Load a Certificate Signing Request (CSR)
122 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000123 * \note See notes for \c mbedtls_x509_csr_parse()
124 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200125 * \param csr CSR context to fill
126 * \param path filename to read the CSR from
127 *
128 * \return 0 if successful, or a specific X509 or PEM error code
129 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200130int mbedtls_x509_csr_parse_file(mbedtls_x509_csr *csr, const char *path);
131# endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200132
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200133# if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200134/**
135 * \brief Returns an informational string about the
136 * CSR.
137 *
138 * \param buf Buffer to write to
139 * \param size Maximum size of buffer
140 * \param prefix A line prefix
141 * \param csr The X509 CSR to represent
142 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200143 * \return The length of the string written (not including the
144 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200146int mbedtls_x509_csr_info(char *buf,
147 size_t size,
148 const char *prefix,
149 const mbedtls_x509_csr *csr);
150# endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200151
152/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200153 * \brief Initialize a CSR
154 *
155 * \param csr CSR to initialize
156 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200157void mbedtls_x509_csr_init(mbedtls_x509_csr *csr);
Paul Bakker369d2eb2013-09-18 11:58:25 +0200158
159/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200160 * \brief Unallocate all CSR data
161 *
162 * \param csr CSR to free
163 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200164void mbedtls_x509_csr_free(mbedtls_x509_csr *csr);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200166
167/* \} name */
168/* \} addtogroup x509_module */
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_X509_CSR_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200171/**
172 * \brief Initialize a CSR context
173 *
174 * \param ctx CSR context to initialize
175 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200176void mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200177
178/**
179 * \brief Set the subject name for a CSR
180 * Subject names should contain a comma-separated list
181 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000182 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200183 *
184 * \param ctx CSR context to use
185 * \param subject_name subject name to set
186 *
187 * \return 0 if subject name was parsed successfully, or
188 * a specific error code
189 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200190int mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx,
191 const char *subject_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200192
193/**
194 * \brief Set the key for a CSR (public key will be included,
195 * private key used to sign the CSR when writing it)
196 *
197 * \param ctx CSR context to use
198 * \param key Asymetric key to include
199 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200200void mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx,
201 mbedtls_pk_context *key);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200202
203/**
204 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206 *
207 * \param ctx CSR context to use
208 * \param md_alg MD algorithm to use
209 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200210void mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx,
211 mbedtls_md_type_t md_alg);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200212
213/**
214 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200216 *
217 * \param ctx CSR context to use
218 * \param key_usage key usage flags to set
219 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200220 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100221 *
222 * \note The <code>decipherOnly</code> flag from the Key Usage
223 * extension is represented by bit 8 (i.e.
224 * <code>0x8000</code>), which cannot typically be represented
225 * in an unsigned char. Therefore, the flag
226 * <code>decipherOnly</code> (i.e.
227 * #MBEDTLS_X509_KU_DECIPHER_ONLY) cannot be set using this
228 * function.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200230int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx,
231 unsigned char key_usage);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200232
233/**
234 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200236 *
237 * \param ctx CSR context to use
238 * \param ns_cert_type Netscape Cert Type flags to set
239 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200240 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200241 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200242int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
243 unsigned char ns_cert_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200244
245/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200246 * \brief Generic function to add to or replace an extension in the
247 * CSR
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200248 *
249 * \param ctx CSR context to use
250 * \param oid OID of the extension
251 * \param oid_len length of the OID
Christoph Reiter95273f42021-01-21 13:31:23 +0100252 * \param critical Set to 1 to mark the extension as critical, 0 otherwise.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200253 * \param val value of the extension OCTET STRING
254 * \param val_len length of the value data
255 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200256 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200257 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200258int mbedtls_x509write_csr_set_extension(mbedtls_x509write_csr *ctx,
259 const char *oid,
260 size_t oid_len,
261 int critical,
262 const unsigned char *val,
263 size_t val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200264
265/**
266 * \brief Free the contents of a CSR context
267 *
268 * \param ctx CSR context to free
269 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200270void mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271
272/**
273 * \brief Write a CSR (Certificate Signing Request) to a
274 * DER structure
275 * Note: data is written at the end of the buffer! Use the
276 * return value to determine where you should start
277 * using the buffer
278 *
279 * \param ctx CSR to write away
280 * \param buf buffer to write to
281 * \param size size of the buffer
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +0200282 * \param f_rng RNG function. This must not be \c NULL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283 * \param p_rng RNG parameter
284 *
285 * \return length of data written if successful, or a specific
286 * error code
287 *
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +0200288 * \note \p f_rng is used for the signature operation.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200290int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx,
291 unsigned char *buf,
292 size_t size,
293 int (*f_rng)(void *, unsigned char *, size_t),
294 void *p_rng);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200295
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200296# if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200297/**
298 * \brief Write a CSR (Certificate Signing Request) to a
299 * PEM string
300 *
301 * \param ctx CSR to write away
302 * \param buf buffer to write to
303 * \param size size of the buffer
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +0200304 * \param f_rng RNG function. This must not be \c NULL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200305 * \param p_rng RNG parameter
306 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200307 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200308 *
Manuel Pégourié-Gonnardc305b722021-06-15 11:29:26 +0200309 * \note \p f_rng is used for the signature operation.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200310 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200311int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx,
312 unsigned char *buf,
313 size_t size,
314 int (*f_rng)(void *, unsigned char *, size_t),
315 void *p_rng);
316# endif /* MBEDTLS_PEM_WRITE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#endif /* MBEDTLS_X509_CSR_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200318
319#ifdef __cplusplus
320}
321#endif
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#endif /* mbedtls_x509_csr.h */