blob: a3c28048e011e9f8d5346ca022983709c5efb301 [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
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é-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_X509_CSR_H
25#define MBEDTLS_X509_CSR_H
Paul Bakker7c6b2c32013-09-16 13:49:26 +020026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032
33#include "x509.h"
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39/**
40 * \addtogroup x509_module
41 * \{ */
42
43/**
44 * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
45 * \{
46 */
47
48/**
49 * Certificate Signing Request (CSR) structure.
50 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051typedef struct mbedtls_x509_csr
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 mbedtls_x509_buf raw; /**< The raw CSR data (DER). */
54 mbedtls_x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020056 int version; /**< CSR version (1=v1). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). */
59 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_x509_buf sig_oid;
64 mbedtls_x509_buf sig;
65 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
66 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
67 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 +020068}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069mbedtls_x509_csr;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070
71/**
72 * Container for writing a CSR
73 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074typedef struct mbedtls_x509write_csr
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_pk_context *key;
77 mbedtls_asn1_named_data *subject;
78 mbedtls_md_type_t md_alg;
79 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081mbedtls_x509write_csr;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084/**
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020085 * \brief Load a Certificate Signing Request (CSR) in DER format
86 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +000087 * \note CSR attributes (if any) are currently silently ignored.
88 *
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020089 * \param csr CSR context to fill
90 * \param buf buffer holding the CRL data
91 * \param buflen size of the buffer
92 *
93 * \return 0 if successful, or a specific X509 error code
94 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020096 const unsigned char *buf, size_t buflen );
97
98/**
99 * \brief Load a Certificate Signing Request (CSR), DER or PEM format
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000101 * \note See notes for \c mbedtls_x509_csr_parse_der()
102 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200103 * \param csr CSR context to fill
104 * \param buf buffer holding the CRL data
105 * \param buflen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200106 * (including the terminating null byte for PEM data)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107 *
108 * \return 0 if successful, or a specific X509 or PEM error code
109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113/**
114 * \brief Load a Certificate Signing Request (CSR)
115 *
Manuel Pégourié-Gonnard986bbf22016-02-24 14:36:05 +0000116 * \note See notes for \c mbedtls_x509_csr_parse()
117 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200118 * \param csr CSR context to fill
119 * \param path filename to read the CSR from
120 *
121 * \return 0 if successful, or a specific X509 or PEM error code
122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path );
124#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200125
126/**
127 * \brief Returns an informational string about the
128 * CSR.
129 *
130 * \param buf Buffer to write to
131 * \param size Maximum size of buffer
132 * \param prefix A line prefix
133 * \param csr The X509 CSR to represent
134 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200135 * \return The length of the string written (not including the
136 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
139 const mbedtls_x509_csr *csr );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200140
141/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200142 * \brief Initialize a CSR
143 *
144 * \param csr CSR to initialize
145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146void mbedtls_x509_csr_init( mbedtls_x509_csr *csr );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200147
148/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200149 * \brief Unallocate all CSR data
150 *
151 * \param csr CSR to free
152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153void mbedtls_x509_csr_free( mbedtls_x509_csr *csr );
154#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155
156/* \} name */
157/* \} addtogroup x509_module */
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_X509_CSR_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200160/**
161 * \brief Initialize a CSR context
162 *
163 * \param ctx CSR context to initialize
164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200166
167/**
168 * \brief Set the subject name for a CSR
169 * Subject names should contain a comma-separated list
170 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000171 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172 *
173 * \param ctx CSR context to use
174 * \param subject_name subject name to set
175 *
176 * \return 0 if subject name was parsed successfully, or
177 * a specific error code
178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100180 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200181
182/**
183 * \brief Set the key for a CSR (public key will be included,
184 * private key used to sign the CSR when writing it)
185 *
186 * \param ctx CSR context to use
187 * \param key Asymetric key to include
188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200190
191/**
192 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200194 *
195 * \param ctx CSR context to use
196 * \param md_alg MD algorithm to use
197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200199
200/**
201 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200203 *
204 * \param ctx CSR context to use
205 * \param key_usage key usage flags to set
206 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200207 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Andres Amaya Garciad8233f72018-10-08 19:44:55 +0100208 *
209 * \note The <code>decipherOnly</code> flag from the Key Usage
210 * extension is represented by bit 8 (i.e.
211 * <code>0x8000</code>), which cannot typically be represented
212 * in an unsigned char. Therefore, the flag
213 * <code>decipherOnly</code> (i.e.
214 * #MBEDTLS_X509_KU_DECIPHER_ONLY) cannot be set using this
215 * function.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200218
219/**
220 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200222 *
223 * \param ctx CSR context to use
224 * \param ns_cert_type Netscape Cert Type flags to set
225 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200226 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229 unsigned char ns_cert_type );
230
231/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200232 * \brief Generic function to add to or replace an extension in the
233 * CSR
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200234 *
235 * \param ctx CSR context to use
236 * \param oid OID of the extension
237 * \param oid_len length of the OID
238 * \param val value of the extension OCTET STRING
239 * \param val_len length of the value data
240 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200241 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200244 const char *oid, size_t oid_len,
245 const unsigned char *val, size_t val_len );
246
247/**
248 * \brief Free the contents of a CSR context
249 *
250 * \param ctx CSR context to free
251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200253
254/**
255 * \brief Write a CSR (Certificate Signing Request) to a
256 * DER structure
257 * Note: data is written at the end of the buffer! Use the
258 * return value to determine where you should start
259 * using the buffer
260 *
261 * \param ctx CSR to write away
262 * \param buf buffer to write to
263 * \param size size of the buffer
264 * \param f_rng RNG function (for signature, see note)
265 * \param p_rng RNG parameter
266 *
267 * \return length of data written if successful, or a specific
268 * error code
269 *
270 * \note f_rng may be NULL if RSA is used for signature and the
271 * signature is made offline (otherwise f_rng is desirable
272 * for countermeasures against timing attacks).
273 * ECDSA signatures always require a non-NULL f_rng.
274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200276 int (*f_rng)(void *, unsigned char *, size_t),
277 void *p_rng );
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280/**
281 * \brief Write a CSR (Certificate Signing Request) to a
282 * PEM string
283 *
284 * \param ctx CSR to write away
285 * \param buf buffer to write to
286 * \param size size of the buffer
287 * \param f_rng RNG function (for signature, see note)
288 * \param p_rng RNG parameter
289 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200290 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291 *
292 * \note f_rng may be NULL if RSA is used for signature and the
293 * signature is made offline (otherwise f_rng is desirable
Andres AGcd153272016-10-04 12:06:50 +0100294 * for countermeasures against timing attacks).
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200295 * ECDSA signatures always require a non-NULL f_rng.
296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200298 int (*f_rng)(void *, unsigned char *, size_t),
299 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#endif /* MBEDTLS_PEM_WRITE_C */
301#endif /* MBEDTLS_X509_CSR_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200302
303#ifdef __cplusplus
304}
305#endif
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#endif /* mbedtls_x509_csr.h */