blob: 8ba2cda0dc5dd32cbb54880283b347ce69dbb71a [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/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
48 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000049 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#ifndef MBEDTLS_X509_CSR_H
52#define MBEDTLS_X509_CSR_H
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059
60#include "x509.h"
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66/**
67 * \addtogroup x509_module
68 * \{ */
69
70/**
71 * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
72 * \{
73 */
74
75/**
76 * Certificate Signing Request (CSR) structure.
77 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078typedef struct mbedtls_x509_csr
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_x509_buf raw; /**< The raw CSR data (DER). */
81 mbedtls_x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020083 int version; /**< CSR version (1=v1). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). */
86 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_x509_buf sig_oid;
91 mbedtls_x509_buf sig;
92 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
93 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
94 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 +020095}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096mbedtls_x509_csr;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097
98/**
99 * Container for writing a CSR
100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101typedef struct mbedtls_x509write_csr
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_pk_context *key;
104 mbedtls_asn1_named_data *subject;
105 mbedtls_md_type_t md_alg;
106 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108mbedtls_x509write_csr;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111/**
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200112 * \brief Load a Certificate Signing Request (CSR) in DER format
113 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000114 * \note CSR attributes (if any) are currently silently ignored.
115 *
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200116 * \param csr CSR context to fill
117 * \param buf buffer holding the CRL data
118 * \param buflen size of the buffer
119 *
120 * \return 0 if successful, or a specific X509 error code
121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200123 const unsigned char *buf, size_t buflen );
124
125/**
126 * \brief Load a Certificate Signing Request (CSR), DER or PEM format
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000128 * \note See notes for \c mbedtls_x509_csr_parse_der()
129 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200130 * \param csr CSR context to fill
131 * \param buf buffer holding the CRL data
132 * \param buflen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200133 * (including the terminating null byte for PEM data)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200134 *
135 * \return 0 if successful, or a specific X509 or PEM error code
136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200140/**
141 * \brief Load a Certificate Signing Request (CSR)
142 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000143 * \note See notes for \c mbedtls_x509_csr_parse()
144 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145 * \param csr CSR context to fill
146 * \param path filename to read the CSR from
147 *
148 * \return 0 if successful, or a specific X509 or PEM error code
149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path );
151#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200152
153/**
154 * \brief Returns an informational string about the
155 * CSR.
156 *
157 * \param buf Buffer to write to
158 * \param size Maximum size of buffer
159 * \param prefix A line prefix
160 * \param csr The X509 CSR to represent
161 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200162 * \return The length of the string written (not including the
163 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
166 const mbedtls_x509_csr *csr );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200167
168/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200169 * \brief Initialize a CSR
170 *
171 * \param csr CSR to initialize
172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173void mbedtls_x509_csr_init( mbedtls_x509_csr *csr );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200174
175/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200176 * \brief Unallocate all CSR data
177 *
178 * \param csr CSR to free
179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180void mbedtls_x509_csr_free( mbedtls_x509_csr *csr );
181#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200182
183/* \} name */
184/* \} addtogroup x509_module */
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#if defined(MBEDTLS_X509_CSR_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200187/**
188 * \brief Initialize a CSR context
189 *
190 * \param ctx CSR context to initialize
191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200193
194/**
195 * \brief Set the subject name for a CSR
196 * Subject names should contain a comma-separated list
197 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000198 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200199 *
200 * \param ctx CSR context to use
201 * \param subject_name subject name to set
202 *
203 * \return 0 if subject name was parsed successfully, or
204 * a specific error code
205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100207 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200208
209/**
210 * \brief Set the key for a CSR (public key will be included,
211 * private key used to sign the CSR when writing it)
212 *
213 * \param ctx CSR context to use
214 * \param key Asymetric key to include
215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200217
218/**
219 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221 *
222 * \param ctx CSR context to use
223 * \param md_alg MD algorithm to use
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200226
227/**
228 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200230 *
231 * \param ctx CSR context to use
232 * \param key_usage key usage flags to set
233 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200234 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Andres Amaya Garcia64900342018-10-08 19:44:55 +0100235 *
236 * \note The <code>decipherOnly</code> flag from the Key Usage
237 * extension is represented by bit 8 (i.e.
238 * <code>0x8000</code>), which cannot typically be represented
239 * in an unsigned char. Therefore, the flag
240 * <code>decipherOnly</code> (i.e.
241 * #MBEDTLS_X509_KU_DECIPHER_ONLY) cannot be set using this
242 * function.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200245
246/**
247 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249 *
250 * \param ctx CSR context to use
251 * \param ns_cert_type Netscape Cert Type flags to set
252 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200253 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200256 unsigned char ns_cert_type );
257
258/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200259 * \brief Generic function to add to or replace an extension in the
260 * CSR
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261 *
262 * \param ctx CSR context to use
263 * \param oid OID of the extension
264 * \param oid_len length of the OID
265 * \param val value of the extension OCTET STRING
266 * \param val_len length of the value data
267 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200268 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271 const char *oid, size_t oid_len,
272 const unsigned char *val, size_t val_len );
273
274/**
275 * \brief Free the contents of a CSR context
276 *
277 * \param ctx CSR context to free
278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280
281/**
282 * \brief Write a CSR (Certificate Signing Request) to a
283 * DER structure
284 * Note: data is written at the end of the buffer! Use the
285 * return value to determine where you should start
286 * using the buffer
287 *
288 * \param ctx CSR to write away
289 * \param buf buffer to write to
290 * \param size size of the buffer
291 * \param f_rng RNG function (for signature, see note)
292 * \param p_rng RNG parameter
293 *
294 * \return length of data written if successful, or a specific
295 * error code
296 *
297 * \note f_rng may be NULL if RSA is used for signature and the
298 * signature is made offline (otherwise f_rng is desirable
299 * for countermeasures against timing attacks).
300 * ECDSA signatures always require a non-NULL f_rng.
301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200303 int (*f_rng)(void *, unsigned char *, size_t),
304 void *p_rng );
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307/**
308 * \brief Write a CSR (Certificate Signing Request) to a
309 * PEM string
310 *
311 * \param ctx CSR to write away
312 * \param buf buffer to write to
313 * \param size size of the buffer
314 * \param f_rng RNG function (for signature, see note)
315 * \param p_rng RNG parameter
316 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200317 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200318 *
319 * \note f_rng may be NULL if RSA is used for signature and the
320 * signature is made offline (otherwise f_rng is desirable
Andres AGcd153272016-10-04 12:06:50 +0100321 * for countermeasures against timing attacks).
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200322 * ECDSA signatures always require a non-NULL f_rng.
323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325 int (*f_rng)(void *, unsigned char *, size_t),
326 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#endif /* MBEDTLS_PEM_WRITE_C */
328#endif /* MBEDTLS_X509_CSR_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329
330#ifdef __cplusplus
331}
332#endif
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#endif /* mbedtls_x509_csr.h */