blob: 28ddedaae6fd1d48990db46e7630bc2d5af4211d [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 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27#ifndef POLARSSL_X509_CSR_H
28#define POLARSSL_X509_CSR_H
29
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
36#include "x509.h"
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/**
43 * \addtogroup x509_module
44 * \{ */
45
46/**
47 * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
48 * \{
49 */
50
51/**
52 * Certificate Signing Request (CSR) structure.
53 */
54typedef struct _x509_csr
55{
56 x509_buf raw; /**< The raw CSR data (DER). */
57 x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
58
59 int version;
60
61 x509_buf subject_raw; /**< The raw subject data (DER). */
62 x509_name subject; /**< The parsed subject data (named information object). */
63
64 pk_context pk; /**< Container for the public key context. */
65
66 x509_buf sig_oid;
67 x509_buf sig;
68 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 +020069 pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. POLARSSL_PK_RSA */
Manuel Pégourié-Gonnard39868ee2014-01-24 18:47:17 +010070#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +020071 void *sig_opts; /**< Signature options to be passed to pk_verify_ext(), eg for RSASSA-PSS */
Manuel Pégourié-Gonnard39868ee2014-01-24 18:47:17 +010072 x509_buf sig_params; /**< Parameters for the signature algorithm */
73#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074}
75x509_csr;
76
77/**
78 * Container for writing a CSR
79 */
80typedef struct _x509write_csr
81{
82 pk_context *key;
83 asn1_named_data *subject;
84 md_type_t md_alg;
85 asn1_named_data *extensions;
86}
87x509write_csr;
88
89#if defined(POLARSSL_X509_CSR_PARSE_C)
90/**
91 * \brief Load a Certificate Signing Request (CSR)
92 *
93 * \param csr CSR context to fill
94 * \param buf buffer holding the CRL data
95 * \param buflen size of the buffer
96 *
97 * \return 0 if successful, or a specific X509 or PEM error code
98 */
Paul Bakkerddf26b42013-09-18 13:46:23 +020099int x509_csr_parse( x509_csr *csr, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100
101#if defined(POLARSSL_FS_IO)
102/**
103 * \brief Load a Certificate Signing Request (CSR)
104 *
105 * \param csr CSR context to fill
106 * \param path filename to read the CSR from
107 *
108 * \return 0 if successful, or a specific X509 or PEM error code
109 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200110int x509_csr_parse_file( x509_csr *csr, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111#endif /* POLARSSL_FS_IO */
112
113/**
114 * \brief Returns an informational string about the
115 * CSR.
116 *
117 * \param buf Buffer to write to
118 * \param size Maximum size of buffer
119 * \param prefix A line prefix
120 * \param csr The X509 CSR to represent
121 *
122 * \return The amount of data written to the buffer, or -1 in
123 * case of an error.
124 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200125int x509_csr_info( char *buf, size_t size, const char *prefix,
126 const x509_csr *csr );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127
128/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200129 * \brief Initialize a CSR
130 *
131 * \param csr CSR to initialize
132 */
133void x509_csr_init( x509_csr *csr );
134
135/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200136 * \brief Unallocate all CSR data
137 *
138 * \param csr CSR to free
139 */
140void x509_csr_free( x509_csr *csr );
141#endif /* POLARSSL_X509_CSR_PARSE_C */
142
143/* \} name */
144/* \} addtogroup x509_module */
145
146#if defined(POLARSSL_X509_CSR_WRITE_C)
147/**
148 * \brief Initialize a CSR context
149 *
150 * \param ctx CSR context to initialize
151 */
152void x509write_csr_init( x509write_csr *ctx );
153
154/**
155 * \brief Set the subject name for a CSR
156 * Subject names should contain a comma-separated list
157 * of OID types and values:
158 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
159 *
160 * \param ctx CSR context to use
161 * \param subject_name subject name to set
162 *
163 * \return 0 if subject name was parsed successfully, or
164 * a specific error code
165 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100166int x509write_csr_set_subject_name( x509write_csr *ctx,
167 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200168
169/**
170 * \brief Set the key for a CSR (public key will be included,
171 * private key used to sign the CSR when writing it)
172 *
173 * \param ctx CSR context to use
174 * \param key Asymetric key to include
175 */
176void x509write_csr_set_key( x509write_csr *ctx, pk_context *key );
177
178/**
179 * \brief Set the MD algorithm to use for the signature
180 * (e.g. POLARSSL_MD_SHA1)
181 *
182 * \param ctx CSR context to use
183 * \param md_alg MD algorithm to use
184 */
185void x509write_csr_set_md_alg( x509write_csr *ctx, md_type_t md_alg );
186
187/**
188 * \brief Set the Key Usage Extension flags
189 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
190 *
191 * \param ctx CSR context to use
192 * \param key_usage key usage flags to set
193 *
194 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
195 */
196int x509write_csr_set_key_usage( x509write_csr *ctx, unsigned char key_usage );
197
198/**
199 * \brief Set the Netscape Cert Type flags
200 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
201 *
202 * \param ctx CSR context to use
203 * \param ns_cert_type Netscape Cert Type flags to set
204 *
205 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
206 */
207int x509write_csr_set_ns_cert_type( x509write_csr *ctx,
208 unsigned char ns_cert_type );
209
210/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200211 * \brief Generic function to add to or replace an extension in the
212 * CSR
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200213 *
214 * \param ctx CSR context to use
215 * \param oid OID of the extension
216 * \param oid_len length of the OID
217 * \param val value of the extension OCTET STRING
218 * \param val_len length of the value data
219 *
220 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
221 */
222int x509write_csr_set_extension( x509write_csr *ctx,
223 const char *oid, size_t oid_len,
224 const unsigned char *val, size_t val_len );
225
226/**
227 * \brief Free the contents of a CSR context
228 *
229 * \param ctx CSR context to free
230 */
231void x509write_csr_free( x509write_csr *ctx );
232
233/**
234 * \brief Write a CSR (Certificate Signing Request) to a
235 * DER structure
236 * Note: data is written at the end of the buffer! Use the
237 * return value to determine where you should start
238 * using the buffer
239 *
240 * \param ctx CSR to write away
241 * \param buf buffer to write to
242 * \param size size of the buffer
243 * \param f_rng RNG function (for signature, see note)
244 * \param p_rng RNG parameter
245 *
246 * \return length of data written if successful, or a specific
247 * error code
248 *
249 * \note f_rng may be NULL if RSA is used for signature and the
250 * signature is made offline (otherwise f_rng is desirable
251 * for countermeasures against timing attacks).
252 * ECDSA signatures always require a non-NULL f_rng.
253 */
254int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size,
255 int (*f_rng)(void *, unsigned char *, size_t),
256 void *p_rng );
257
258#if defined(POLARSSL_PEM_WRITE_C)
259/**
260 * \brief Write a CSR (Certificate Signing Request) to a
261 * PEM string
262 *
263 * \param ctx CSR to write away
264 * \param buf buffer to write to
265 * \param size size of the buffer
266 * \param f_rng RNG function (for signature, see note)
267 * \param p_rng RNG parameter
268 *
269 * \return 0 successful, or a specific error code
270 *
271 * \note f_rng may be NULL if RSA is used for signature and the
272 * signature is made offline (otherwise f_rng is desirable
273 * for couermeasures against timing attacks).
274 * ECDSA signatures always require a non-NULL f_rng.
275 */
276int x509write_csr_pem( x509write_csr *ctx, unsigned char *buf, size_t size,
277 int (*f_rng)(void *, unsigned char *, size_t),
278 void *p_rng );
279#endif /* POLARSSL_PEM_WRITE_C */
280#endif /* POLARSSL_X509_CSR_WRITE_C */
281
282#ifdef __cplusplus
283}
284#endif
285
286#endif /* x509_csr.h */