blob: 5dfb4213e8d3f2e89ff219427a961ded79ad42ec [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útia2947ac2020-08-19 16:37:36 +02007 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02008 * 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:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 *
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.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020028 * **********
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 * **********
Paul Bakker7c6b2c32013-09-16 13:49:26 +020048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#ifndef MBEDTLS_X509_CSR_H
50#define MBEDTLS_X509_CSR_H
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020054#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057
58#include "x509.h"
59
60#ifdef __cplusplus
61extern "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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076typedef struct mbedtls_x509_csr
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 mbedtls_x509_buf raw; /**< The raw CSR data (DER). */
79 mbedtls_x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020081 int version; /**< CSR version (1=v1). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). */
84 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 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 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094mbedtls_x509_csr;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095
96/**
97 * Container for writing a CSR
98 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099typedef struct mbedtls_x509write_csr
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_pk_context *key;
102 mbedtls_asn1_named_data *subject;
103 mbedtls_md_type_t md_alg;
104 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106mbedtls_x509write_csr;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109/**
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200110 * \brief Load a Certificate Signing Request (CSR) in DER format
111 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000112 * \note CSR attributes (if any) are currently silently ignored.
113 *
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200114 * \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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200121 const unsigned char *buf, size_t buflen );
122
123/**
124 * \brief Load a Certificate Signing Request (CSR), DER or PEM format
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200125 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000126 * \note See notes for \c mbedtls_x509_csr_parse_der()
127 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200128 * \param csr CSR context to fill
129 * \param buf buffer holding the CRL data
130 * \param buflen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200131 * (including the terminating null byte for PEM data)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200132 *
133 * \return 0 if successful, or a specific X509 or PEM error code
134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138/**
139 * \brief Load a Certificate Signing Request (CSR)
140 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000141 * \note See notes for \c mbedtls_x509_csr_parse()
142 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200143 * \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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path );
149#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200150
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 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200160 * \return The length of the string written (not including the
161 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
164 const mbedtls_x509_csr *csr );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200165
166/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200167 * \brief Initialize a CSR
168 *
169 * \param csr CSR to initialize
170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171void mbedtls_x509_csr_init( mbedtls_x509_csr *csr );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200172
173/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200174 * \brief Unallocate all CSR data
175 *
176 * \param csr CSR to free
177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178void mbedtls_x509_csr_free( mbedtls_x509_csr *csr );
179#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200180
181/* \} name */
182/* \} addtogroup x509_module */
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#if defined(MBEDTLS_X509_CSR_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200185/**
186 * \brief Initialize a CSR context
187 *
188 * \param ctx CSR context to initialize
189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200191
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:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000196 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200197 *
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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100205 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206
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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200215
216/**
217 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219 *
220 * \param ctx CSR context to use
221 * \param md_alg MD algorithm to use
222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200224
225/**
226 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200228 *
229 * \param ctx CSR context to use
230 * \param key_usage key usage flags to set
231 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200232 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Andres Amaya Garcia64900342018-10-08 19:44:55 +0100233 *
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.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200243
244/**
245 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200247 *
248 * \param ctx CSR context to use
249 * \param ns_cert_type Netscape Cert Type flags to set
250 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200251 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200254 unsigned char ns_cert_type );
255
256/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200257 * \brief Generic function to add to or replace an extension in the
258 * CSR
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200259 *
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 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200266 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278
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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200301 int (*f_rng)(void *, unsigned char *, size_t),
302 void *p_rng );
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200305/**
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 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200315 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200316 *
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
Andres AGcd153272016-10-04 12:06:50 +0100319 * for countermeasures against timing attacks).
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200320 * ECDSA signatures always require a non-NULL f_rng.
321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323 int (*f_rng)(void *, unsigned char *, size_t),
324 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#endif /* MBEDTLS_PEM_WRITE_C */
326#endif /* MBEDTLS_X509_CSR_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327
328#ifdef __cplusplus
329}
330#endif
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#endif /* mbedtls_x509_csr.h */