blob: af3f226c86af31317e96daa10c01dc7629da7fdf [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 */
69 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)
71 x509_buf sig_params; /**< Parameters for the signature algorithm */
72#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073}
74x509_csr;
75
76/**
77 * Container for writing a CSR
78 */
79typedef struct _x509write_csr
80{
81 pk_context *key;
82 asn1_named_data *subject;
83 md_type_t md_alg;
84 asn1_named_data *extensions;
85}
86x509write_csr;
87
88#if defined(POLARSSL_X509_CSR_PARSE_C)
89/**
90 * \brief Load a Certificate Signing Request (CSR)
91 *
92 * \param csr CSR context to fill
93 * \param buf buffer holding the CRL data
94 * \param buflen size of the buffer
95 *
96 * \return 0 if successful, or a specific X509 or PEM error code
97 */
Paul Bakkerddf26b42013-09-18 13:46:23 +020098int x509_csr_parse( x509_csr *csr, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099
100#if defined(POLARSSL_FS_IO)
101/**
102 * \brief Load a Certificate Signing Request (CSR)
103 *
104 * \param csr CSR context to fill
105 * \param path filename to read the CSR from
106 *
107 * \return 0 if successful, or a specific X509 or PEM error code
108 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200109int x509_csr_parse_file( x509_csr *csr, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110#endif /* POLARSSL_FS_IO */
111
112/**
113 * \brief Returns an informational string about the
114 * CSR.
115 *
116 * \param buf Buffer to write to
117 * \param size Maximum size of buffer
118 * \param prefix A line prefix
119 * \param csr The X509 CSR to represent
120 *
121 * \return The amount of data written to the buffer, or -1 in
122 * case of an error.
123 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200124int x509_csr_info( char *buf, size_t size, const char *prefix,
125 const x509_csr *csr );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200126
127/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200128 * \brief Initialize a CSR
129 *
130 * \param csr CSR to initialize
131 */
132void x509_csr_init( x509_csr *csr );
133
134/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135 * \brief Unallocate all CSR data
136 *
137 * \param csr CSR to free
138 */
139void x509_csr_free( x509_csr *csr );
140#endif /* POLARSSL_X509_CSR_PARSE_C */
141
142/* \} name */
143/* \} addtogroup x509_module */
144
145#if defined(POLARSSL_X509_CSR_WRITE_C)
146/**
147 * \brief Initialize a CSR context
148 *
149 * \param ctx CSR context to initialize
150 */
151void x509write_csr_init( x509write_csr *ctx );
152
153/**
154 * \brief Set the subject name for a CSR
155 * Subject names should contain a comma-separated list
156 * of OID types and values:
157 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
158 *
159 * \param ctx CSR context to use
160 * \param subject_name subject name to set
161 *
162 * \return 0 if subject name was parsed successfully, or
163 * a specific error code
164 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100165int x509write_csr_set_subject_name( x509write_csr *ctx,
166 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200167
168/**
169 * \brief Set the key for a CSR (public key will be included,
170 * private key used to sign the CSR when writing it)
171 *
172 * \param ctx CSR context to use
173 * \param key Asymetric key to include
174 */
175void x509write_csr_set_key( x509write_csr *ctx, pk_context *key );
176
177/**
178 * \brief Set the MD algorithm to use for the signature
179 * (e.g. POLARSSL_MD_SHA1)
180 *
181 * \param ctx CSR context to use
182 * \param md_alg MD algorithm to use
183 */
184void x509write_csr_set_md_alg( x509write_csr *ctx, md_type_t md_alg );
185
186/**
187 * \brief Set the Key Usage Extension flags
188 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
189 *
190 * \param ctx CSR context to use
191 * \param key_usage key usage flags to set
192 *
193 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
194 */
195int x509write_csr_set_key_usage( x509write_csr *ctx, unsigned char key_usage );
196
197/**
198 * \brief Set the Netscape Cert Type flags
199 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
200 *
201 * \param ctx CSR context to use
202 * \param ns_cert_type Netscape Cert Type flags to set
203 *
204 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
205 */
206int x509write_csr_set_ns_cert_type( x509write_csr *ctx,
207 unsigned char ns_cert_type );
208
209/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200210 * \brief Generic function to add to or replace an extension in the
211 * CSR
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200212 *
213 * \param ctx CSR context to use
214 * \param oid OID of the extension
215 * \param oid_len length of the OID
216 * \param val value of the extension OCTET STRING
217 * \param val_len length of the value data
218 *
219 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
220 */
221int x509write_csr_set_extension( x509write_csr *ctx,
222 const char *oid, size_t oid_len,
223 const unsigned char *val, size_t val_len );
224
225/**
226 * \brief Free the contents of a CSR context
227 *
228 * \param ctx CSR context to free
229 */
230void x509write_csr_free( x509write_csr *ctx );
231
232/**
233 * \brief Write a CSR (Certificate Signing Request) to a
234 * DER structure
235 * Note: data is written at the end of the buffer! Use the
236 * return value to determine where you should start
237 * using the buffer
238 *
239 * \param ctx CSR to write away
240 * \param buf buffer to write to
241 * \param size size of the buffer
242 * \param f_rng RNG function (for signature, see note)
243 * \param p_rng RNG parameter
244 *
245 * \return length of data written if successful, or a specific
246 * error code
247 *
248 * \note f_rng may be NULL if RSA is used for signature and the
249 * signature is made offline (otherwise f_rng is desirable
250 * for countermeasures against timing attacks).
251 * ECDSA signatures always require a non-NULL f_rng.
252 */
253int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size,
254 int (*f_rng)(void *, unsigned char *, size_t),
255 void *p_rng );
256
257#if defined(POLARSSL_PEM_WRITE_C)
258/**
259 * \brief Write a CSR (Certificate Signing Request) to a
260 * PEM string
261 *
262 * \param ctx CSR to write away
263 * \param buf buffer to write to
264 * \param size size of the buffer
265 * \param f_rng RNG function (for signature, see note)
266 * \param p_rng RNG parameter
267 *
268 * \return 0 successful, or a specific 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 couermeasures against timing attacks).
273 * ECDSA signatures always require a non-NULL f_rng.
274 */
275int x509write_csr_pem( x509write_csr *ctx, unsigned char *buf, size_t size,
276 int (*f_rng)(void *, unsigned char *, size_t),
277 void *p_rng );
278#endif /* POLARSSL_PEM_WRITE_C */
279#endif /* POLARSSL_X509_CSR_WRITE_C */
280
281#ifdef __cplusplus
282}
283#endif
284
285#endif /* x509_csr.h */