blob: 7e3830087404df710d426755c5d19073ea7fd3ce [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 *
6 * Copyright (C) 2006-2013, Brainspark B.V.
7 *
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
30#include "config.h"
31
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 */
50typedef struct _x509_csr
51{
52 x509_buf raw; /**< The raw CSR data (DER). */
53 x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
54
55 int version;
56
57 x509_buf subject_raw; /**< The raw subject data (DER). */
58 x509_name subject; /**< The parsed subject data (named information object). */
59
60 pk_context pk; /**< Container for the public key context. */
61
62 x509_buf sig_oid;
63 x509_buf sig;
64 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
65 pk_type_t sig_pk /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. POLARSSL_PK_RSA */;
66}
67x509_csr;
68
69/**
70 * Container for writing a CSR
71 */
72typedef struct _x509write_csr
73{
74 pk_context *key;
75 asn1_named_data *subject;
76 md_type_t md_alg;
77 asn1_named_data *extensions;
78}
79x509write_csr;
80
81#if defined(POLARSSL_X509_CSR_PARSE_C)
82/**
83 * \brief Load a Certificate Signing Request (CSR)
84 *
85 * \param csr CSR context to fill
86 * \param buf buffer holding the CRL data
87 * \param buflen size of the buffer
88 *
89 * \return 0 if successful, or a specific X509 or PEM error code
90 */
Paul Bakkerddf26b42013-09-18 13:46:23 +020091int x509_csr_parse( x509_csr *csr, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092
93#if defined(POLARSSL_FS_IO)
94/**
95 * \brief Load a Certificate Signing Request (CSR)
96 *
97 * \param csr CSR context to fill
98 * \param path filename to read the CSR from
99 *
100 * \return 0 if successful, or a specific X509 or PEM error code
101 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200102int x509_csr_parse_file( x509_csr *csr, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200103#endif /* POLARSSL_FS_IO */
104
105/**
106 * \brief Returns an informational string about the
107 * CSR.
108 *
109 * \param buf Buffer to write to
110 * \param size Maximum size of buffer
111 * \param prefix A line prefix
112 * \param csr The X509 CSR to represent
113 *
114 * \return The amount of data written to the buffer, or -1 in
115 * case of an error.
116 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200117int x509_csr_info( char *buf, size_t size, const char *prefix,
118 const x509_csr *csr );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119
120/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200121 * \brief Initialize a CSR
122 *
123 * \param csr CSR to initialize
124 */
125void x509_csr_init( x509_csr *csr );
126
127/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200128 * \brief Unallocate all CSR data
129 *
130 * \param csr CSR to free
131 */
132void x509_csr_free( x509_csr *csr );
133#endif /* POLARSSL_X509_CSR_PARSE_C */
134
135/* \} name */
136/* \} addtogroup x509_module */
137
138#if defined(POLARSSL_X509_CSR_WRITE_C)
139/**
140 * \brief Initialize a CSR context
141 *
142 * \param ctx CSR context to initialize
143 */
144void x509write_csr_init( x509write_csr *ctx );
145
146/**
147 * \brief Set the subject name for a CSR
148 * Subject names should contain a comma-separated list
149 * of OID types and values:
150 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
151 *
152 * \param ctx CSR context to use
153 * \param subject_name subject name to set
154 *
155 * \return 0 if subject name was parsed successfully, or
156 * a specific error code
157 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100158int x509write_csr_set_subject_name( x509write_csr *ctx,
159 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200160
161/**
162 * \brief Set the key for a CSR (public key will be included,
163 * private key used to sign the CSR when writing it)
164 *
165 * \param ctx CSR context to use
166 * \param key Asymetric key to include
167 */
168void x509write_csr_set_key( x509write_csr *ctx, pk_context *key );
169
170/**
171 * \brief Set the MD algorithm to use for the signature
172 * (e.g. POLARSSL_MD_SHA1)
173 *
174 * \param ctx CSR context to use
175 * \param md_alg MD algorithm to use
176 */
177void x509write_csr_set_md_alg( x509write_csr *ctx, md_type_t md_alg );
178
179/**
180 * \brief Set the Key Usage Extension flags
181 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
182 *
183 * \param ctx CSR context to use
184 * \param key_usage key usage flags to set
185 *
186 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
187 */
188int x509write_csr_set_key_usage( x509write_csr *ctx, unsigned char key_usage );
189
190/**
191 * \brief Set the Netscape Cert Type flags
192 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
193 *
194 * \param ctx CSR context to use
195 * \param ns_cert_type Netscape Cert Type flags to set
196 *
197 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
198 */
199int x509write_csr_set_ns_cert_type( x509write_csr *ctx,
200 unsigned char ns_cert_type );
201
202/**
203 * \brief Generic function to add to or replace an extension in the CSR
204 *
205 * \param ctx CSR context to use
206 * \param oid OID of the extension
207 * \param oid_len length of the OID
208 * \param val value of the extension OCTET STRING
209 * \param val_len length of the value data
210 *
211 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
212 */
213int x509write_csr_set_extension( x509write_csr *ctx,
214 const char *oid, size_t oid_len,
215 const unsigned char *val, size_t val_len );
216
217/**
218 * \brief Free the contents of a CSR context
219 *
220 * \param ctx CSR context to free
221 */
222void x509write_csr_free( x509write_csr *ctx );
223
224/**
225 * \brief Write a CSR (Certificate Signing Request) to a
226 * DER structure
227 * Note: data is written at the end of the buffer! Use the
228 * return value to determine where you should start
229 * using the buffer
230 *
231 * \param ctx CSR to write away
232 * \param buf buffer to write to
233 * \param size size of the buffer
234 * \param f_rng RNG function (for signature, see note)
235 * \param p_rng RNG parameter
236 *
237 * \return length of data written if successful, or a specific
238 * error code
239 *
240 * \note f_rng may be NULL if RSA is used for signature and the
241 * signature is made offline (otherwise f_rng is desirable
242 * for countermeasures against timing attacks).
243 * ECDSA signatures always require a non-NULL f_rng.
244 */
245int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size,
246 int (*f_rng)(void *, unsigned char *, size_t),
247 void *p_rng );
248
249#if defined(POLARSSL_PEM_WRITE_C)
250/**
251 * \brief Write a CSR (Certificate Signing Request) to a
252 * PEM string
253 *
254 * \param ctx CSR to write away
255 * \param buf buffer to write to
256 * \param size size of the buffer
257 * \param f_rng RNG function (for signature, see note)
258 * \param p_rng RNG parameter
259 *
260 * \return 0 successful, or a specific error code
261 *
262 * \note f_rng may be NULL if RSA is used for signature and the
263 * signature is made offline (otherwise f_rng is desirable
264 * for couermeasures against timing attacks).
265 * ECDSA signatures always require a non-NULL f_rng.
266 */
267int x509write_csr_pem( x509write_csr *ctx, unsigned char *buf, size_t size,
268 int (*f_rng)(void *, unsigned char *, size_t),
269 void *p_rng );
270#endif /* POLARSSL_PEM_WRITE_C */
271#endif /* POLARSSL_X509_CSR_WRITE_C */
272
273#ifdef __cplusplus
274}
275#endif
276
277#endif /* x509_csr.h */