blob: 34f527b8e4bab1a90de45a98cccde8292a16dccc [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/**
2 * \file x509_csr.h
3 *
4 * \brief X.509 certificate signing request parsing and writing
5 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02009 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +020010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24#ifndef POLARSSL_X509_CSR_H
25#define POLARSSL_X509_CSR_H
26
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#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 */
51typedef struct _x509_csr
52{
53 x509_buf raw; /**< The raw CSR data (DER). */
54 x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
55
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020056 int version; /**< CSR version (1=v1). */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057
58 x509_buf subject_raw; /**< The raw subject data (DER). */
59 x509_name subject; /**< The parsed subject data (named information object). */
60
61 pk_context pk; /**< Container for the public key context. */
62
63 x509_buf sig_oid;
64 x509_buf sig;
65 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +020066 pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. POLARSSL_PK_RSA */
Paul Bakker6dade7c2014-06-12 22:02:14 +020067 void *sig_opts; /**< Signature options to be passed to pk_verify_ext(), e.g. for RSASSA-PSS */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068}
69x509_csr;
70
71/**
72 * Container for writing a CSR
73 */
74typedef struct _x509write_csr
75{
76 pk_context *key;
77 asn1_named_data *subject;
78 md_type_t md_alg;
79 asn1_named_data *extensions;
80}
81x509write_csr;
82
83#if defined(POLARSSL_X509_CSR_PARSE_C)
84/**
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020085 * \brief Load a Certificate Signing Request (CSR) in DER format
86 *
87 * \param csr CSR context to fill
88 * \param buf buffer holding the CRL data
89 * \param buflen size of the buffer
90 *
91 * \return 0 if successful, or a specific X509 error code
92 */
93int x509_csr_parse_der( x509_csr *csr,
94 const unsigned char *buf, size_t buflen );
95
96/**
97 * \brief Load a Certificate Signing Request (CSR), DER or PEM format
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098 *
99 * \param csr CSR context to fill
100 * \param buf buffer holding the CRL data
101 * \param buflen size of the buffer
102 *
103 * \return 0 if successful, or a specific X509 or PEM error code
104 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200105int x509_csr_parse( x509_csr *csr, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200106
107#if defined(POLARSSL_FS_IO)
108/**
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 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200116int x509_csr_parse_file( x509_csr *csr, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117#endif /* POLARSSL_FS_IO */
118
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é-Gonnardd77cd5d2014-06-13 11:13:15 +0200128 * \return The length of the string written (exluding the terminating
129 * null byte), or a negative value in case of an error.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200130 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200131int x509_csr_info( char *buf, size_t size, const char *prefix,
132 const 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 */
139void x509_csr_init( x509_csr *csr );
140
141/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200142 * \brief Unallocate all CSR data
143 *
144 * \param csr CSR to free
145 */
146void x509_csr_free( x509_csr *csr );
147#endif /* POLARSSL_X509_CSR_PARSE_C */
148
149/* \} name */
150/* \} addtogroup x509_module */
151
152#if defined(POLARSSL_X509_CSR_WRITE_C)
153/**
154 * \brief Initialize a CSR context
155 *
156 * \param ctx CSR context to initialize
157 */
158void x509write_csr_init( x509write_csr *ctx );
159
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 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100172int x509write_csr_set_subject_name( x509write_csr *ctx,
173 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 */
182void x509write_csr_set_key( x509write_csr *ctx, pk_context *key );
183
184/**
185 * \brief Set the MD algorithm to use for the signature
186 * (e.g. POLARSSL_MD_SHA1)
187 *
188 * \param ctx CSR context to use
189 * \param md_alg MD algorithm to use
190 */
191void x509write_csr_set_md_alg( x509write_csr *ctx, md_type_t md_alg );
192
193/**
194 * \brief Set the Key Usage Extension flags
195 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
196 *
197 * \param ctx CSR context to use
198 * \param key_usage key usage flags to set
199 *
200 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
201 */
202int x509write_csr_set_key_usage( x509write_csr *ctx, unsigned char key_usage );
203
204/**
205 * \brief Set the Netscape Cert Type flags
206 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
207 *
208 * \param ctx CSR context to use
209 * \param ns_cert_type Netscape Cert Type flags to set
210 *
211 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
212 */
213int x509write_csr_set_ns_cert_type( x509write_csr *ctx,
214 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 *
226 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
227 */
228int x509write_csr_set_extension( x509write_csr *ctx,
229 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 */
237void x509write_csr_free( x509write_csr *ctx );
238
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 */
260int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size,
261 int (*f_rng)(void *, unsigned char *, size_t),
262 void *p_rng );
263
264#if defined(POLARSSL_PEM_WRITE_C)
265/**
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 *
275 * \return 0 successful, or a specific error code
276 *
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 */
282int x509write_csr_pem( x509write_csr *ctx, unsigned char *buf, size_t size,
283 int (*f_rng)(void *, unsigned char *, size_t),
284 void *p_rng );
285#endif /* POLARSSL_PEM_WRITE_C */
286#endif /* POLARSSL_X509_CSR_WRITE_C */
287
288#ifdef __cplusplus
289}
290#endif
291
292#endif /* x509_csr.h */