blob: 98c75bcff4ebfafd328aa87d1e4136bf21706a47 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002 * \file mbedtls_x509_csr.h
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
4 * \brief X.509 certificate signing request parsing and writing
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_X509_CSR_H
24#define MBEDTLS_X509_CSR_H
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031
32#include "x509.h"
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38/**
39 * \addtogroup x509_module
40 * \{ */
41
42/**
43 * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
44 * \{
45 */
46
47/**
48 * Certificate Signing Request (CSR) structure.
49 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050typedef struct mbedtls_x509_csr
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 mbedtls_x509_buf raw; /**< The raw CSR data (DER). */
53 mbedtls_x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020055 int version; /**< CSR version (1=v1). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). */
58 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_pk_context pk; /**< Container for the public key context. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_x509_buf sig_oid;
63 mbedtls_x509_buf sig;
64 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
65 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
66 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 +020067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068mbedtls_x509_csr;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069
70/**
71 * Container for writing a CSR
72 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073typedef struct mbedtls_x509write_csr
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_pk_context *key;
76 mbedtls_asn1_named_data *subject;
77 mbedtls_md_type_t md_alg;
78 mbedtls_asn1_named_data *extensions;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080mbedtls_x509write_csr;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083/**
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020084 * \brief Load a Certificate Signing Request (CSR) in DER format
85 *
86 * \param csr CSR context to fill
87 * \param buf buffer holding the CRL data
88 * \param buflen size of the buffer
89 *
90 * \return 0 if successful, or a specific X509 error code
91 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020093 const unsigned char *buf, size_t buflen );
94
95/**
96 * \brief Load a Certificate Signing Request (CSR), DER or PEM format
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097 *
98 * \param csr CSR context to fill
99 * \param buf buffer holding the CRL data
100 * \param buflen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200101 * (including the terminating null byte for PEM data)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102 *
103 * \return 0 if successful, or a specific X509 or PEM error code
104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200108/**
109 * \brief Load a Certificate Signing Request (CSR)
110 *
111 * \param csr CSR context to fill
112 * \param path filename to read the CSR from
113 *
114 * \return 0 if successful, or a specific X509 or PEM error code
115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path );
117#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200118
119/**
120 * \brief Returns an informational string about the
121 * CSR.
122 *
123 * \param buf Buffer to write to
124 * \param size Maximum size of buffer
125 * \param prefix A line prefix
126 * \param csr The X509 CSR to represent
127 *
Manuel Pégourié-Gonnarde244f9f2015-06-23 12:10:45 +0200128 * \return The length of the string written (not including the
129 * terminated nul byte), or a negative error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
132 const mbedtls_x509_csr *csr );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200133
134/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200135 * \brief Initialize a CSR
136 *
137 * \param csr CSR to initialize
138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139void mbedtls_x509_csr_init( mbedtls_x509_csr *csr );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200140
141/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200142 * \brief Unallocate all CSR data
143 *
144 * \param csr CSR to free
145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146void mbedtls_x509_csr_free( mbedtls_x509_csr *csr );
147#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200148
149/* \} name */
150/* \} addtogroup x509_module */
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#if defined(MBEDTLS_X509_CSR_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153/**
154 * \brief Initialize a CSR context
155 *
156 * \param ctx CSR context to initialize
157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200159
160/**
161 * \brief Set the subject name for a CSR
162 * Subject names should contain a comma-separated list
163 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000164 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200165 *
166 * \param ctx CSR context to use
167 * \param subject_name subject name to set
168 *
169 * \return 0 if subject name was parsed successfully, or
170 * a specific error code
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +0100173 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200174
175/**
176 * \brief Set the key for a CSR (public key will be included,
177 * private key used to sign the CSR when writing it)
178 *
179 * \param ctx CSR context to use
180 * \param key Asymetric key to include
181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200183
184/**
185 * \brief Set the MD algorithm to use for the signature
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 * (e.g. MBEDTLS_MD_SHA1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200187 *
188 * \param ctx CSR context to use
189 * \param md_alg MD algorithm to use
190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200192
193/**
194 * \brief Set the Key Usage Extension flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200196 *
197 * \param ctx CSR context to use
198 * \param key_usage key usage flags to set
199 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200200 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200203
204/**
205 * \brief Set the Netscape Cert Type flags
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200207 *
208 * \param ctx CSR context to use
209 * \param ns_cert_type Netscape Cert Type flags to set
210 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200211 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200214 unsigned char ns_cert_type );
215
216/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200217 * \brief Generic function to add to or replace an extension in the
218 * CSR
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219 *
220 * \param ctx CSR context to use
221 * \param oid OID of the extension
222 * \param oid_len length of the OID
223 * \param val value of the extension OCTET STRING
224 * \param val_len length of the value data
225 *
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200226 * \return 0 if successful, or a 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_extension( mbedtls_x509write_csr *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229 const char *oid, size_t oid_len,
230 const unsigned char *val, size_t val_len );
231
232/**
233 * \brief Free the contents of a CSR context
234 *
235 * \param ctx CSR context to free
236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200238
239/**
240 * \brief Write a CSR (Certificate Signing Request) to a
241 * DER structure
242 * Note: data is written at the end of the buffer! Use the
243 * return value to determine where you should start
244 * using the buffer
245 *
246 * \param ctx CSR to write away
247 * \param buf buffer to write to
248 * \param size size of the buffer
249 * \param f_rng RNG function (for signature, see note)
250 * \param p_rng RNG parameter
251 *
252 * \return length of data written if successful, or a specific
253 * error code
254 *
255 * \note f_rng may be NULL if RSA is used for signature and the
256 * signature is made offline (otherwise f_rng is desirable
257 * for countermeasures against timing attacks).
258 * ECDSA signatures always require a non-NULL f_rng.
259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261 int (*f_rng)(void *, unsigned char *, size_t),
262 void *p_rng );
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265/**
266 * \brief Write a CSR (Certificate Signing Request) to a
267 * PEM string
268 *
269 * \param ctx CSR to write away
270 * \param buf buffer to write to
271 * \param size size of the buffer
272 * \param f_rng RNG function (for signature, see note)
273 * \param p_rng RNG parameter
274 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200275 * \return 0 if successful, or a specific error code
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200276 *
277 * \note f_rng may be NULL if RSA is used for signature and the
278 * signature is made offline (otherwise f_rng is desirable
279 * for couermeasures against timing attacks).
280 * ECDSA signatures always require a non-NULL f_rng.
281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283 int (*f_rng)(void *, unsigned char *, size_t),
284 void *p_rng );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#endif /* MBEDTLS_PEM_WRITE_C */
286#endif /* MBEDTLS_X509_CSR_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287
288#ifdef __cplusplus
289}
290#endif
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#endif /* mbedtls_x509_csr.h */