blob: 32befdb4cf7c74004861be84cd573b5558607836 [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 */
91int x509parse_csr( x509_csr *csr, const unsigned char *buf, size_t buflen );
92
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 */
102int x509parse_csrfile( x509_csr *csr, const char *path );
103#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 */
117int x509parse_csr_info( char *buf, size_t size, const char *prefix,
118 const x509_csr *csr );
119
120/**
121 * \brief Unallocate all CSR data
122 *
123 * \param csr CSR to free
124 */
125void x509_csr_free( x509_csr *csr );
126#endif /* POLARSSL_X509_CSR_PARSE_C */
127
128/* \} name */
129/* \} addtogroup x509_module */
130
131#if defined(POLARSSL_X509_CSR_WRITE_C)
132/**
133 * \brief Initialize a CSR context
134 *
135 * \param ctx CSR context to initialize
136 */
137void x509write_csr_init( x509write_csr *ctx );
138
139/**
140 * \brief Set the subject name for a CSR
141 * Subject names should contain a comma-separated list
142 * of OID types and values:
143 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
144 *
145 * \param ctx CSR context to use
146 * \param subject_name subject name to set
147 *
148 * \return 0 if subject name was parsed successfully, or
149 * a specific error code
150 */
151int x509write_csr_set_subject_name( x509write_csr *ctx, char *subject_name );
152
153/**
154 * \brief Set the key for a CSR (public key will be included,
155 * private key used to sign the CSR when writing it)
156 *
157 * \param ctx CSR context to use
158 * \param key Asymetric key to include
159 */
160void x509write_csr_set_key( x509write_csr *ctx, pk_context *key );
161
162/**
163 * \brief Set the MD algorithm to use for the signature
164 * (e.g. POLARSSL_MD_SHA1)
165 *
166 * \param ctx CSR context to use
167 * \param md_alg MD algorithm to use
168 */
169void x509write_csr_set_md_alg( x509write_csr *ctx, md_type_t md_alg );
170
171/**
172 * \brief Set the Key Usage Extension flags
173 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
174 *
175 * \param ctx CSR context to use
176 * \param key_usage key usage flags to set
177 *
178 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
179 */
180int x509write_csr_set_key_usage( x509write_csr *ctx, unsigned char key_usage );
181
182/**
183 * \brief Set the Netscape Cert Type flags
184 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
185 *
186 * \param ctx CSR context to use
187 * \param ns_cert_type Netscape Cert Type flags to set
188 *
189 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
190 */
191int x509write_csr_set_ns_cert_type( x509write_csr *ctx,
192 unsigned char ns_cert_type );
193
194/**
195 * \brief Generic function to add to or replace an extension in the CSR
196 *
197 * \param ctx CSR context to use
198 * \param oid OID of the extension
199 * \param oid_len length of the OID
200 * \param val value of the extension OCTET STRING
201 * \param val_len length of the value data
202 *
203 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
204 */
205int x509write_csr_set_extension( x509write_csr *ctx,
206 const char *oid, size_t oid_len,
207 const unsigned char *val, size_t val_len );
208
209/**
210 * \brief Free the contents of a CSR context
211 *
212 * \param ctx CSR context to free
213 */
214void x509write_csr_free( x509write_csr *ctx );
215
216/**
217 * \brief Write a CSR (Certificate Signing Request) to a
218 * DER structure
219 * Note: data is written at the end of the buffer! Use the
220 * return value to determine where you should start
221 * using the buffer
222 *
223 * \param ctx CSR to write away
224 * \param buf buffer to write to
225 * \param size size of the buffer
226 * \param f_rng RNG function (for signature, see note)
227 * \param p_rng RNG parameter
228 *
229 * \return length of data written if successful, or a specific
230 * error code
231 *
232 * \note f_rng may be NULL if RSA is used for signature and the
233 * signature is made offline (otherwise f_rng is desirable
234 * for countermeasures against timing attacks).
235 * ECDSA signatures always require a non-NULL f_rng.
236 */
237int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size,
238 int (*f_rng)(void *, unsigned char *, size_t),
239 void *p_rng );
240
241#if defined(POLARSSL_PEM_WRITE_C)
242/**
243 * \brief Write a CSR (Certificate Signing Request) to a
244 * PEM string
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 0 successful, or a specific error code
253 *
254 * \note f_rng may be NULL if RSA is used for signature and the
255 * signature is made offline (otherwise f_rng is desirable
256 * for couermeasures against timing attacks).
257 * ECDSA signatures always require a non-NULL f_rng.
258 */
259int x509write_csr_pem( x509write_csr *ctx, unsigned char *buf, size_t size,
260 int (*f_rng)(void *, unsigned char *, size_t),
261 void *p_rng );
262#endif /* POLARSSL_PEM_WRITE_C */
263#endif /* POLARSSL_X509_CSR_WRITE_C */
264
265#ifdef __cplusplus
266}
267#endif
268
269#endif /* x509_csr.h */