4 * \brief X.509 certificate signing request parsing and writing
6 * Copyright (C) 2006-2014, Brainspark B.V.
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
11 * All rights reserved.
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.
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.
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.
27 #ifndef POLARSSL_X509_CSR_H
28 #define POLARSSL_X509_CSR_H
30 #if !defined(POLARSSL_CONFIG_FILE)
33 #include POLARSSL_CONFIG_FILE
43 * \addtogroup x509_module
47 * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
52 * Certificate Signing Request (CSR) structure.
54 typedef struct _x509_csr
56 x509_buf raw
; /**< The raw CSR data (DER). */
57 x509_buf cri
; /**< The raw CertificateRequestInfo body (DER). */
61 x509_buf subject_raw
; /**< The raw subject data (DER). */
62 x509_name subject
; /**< The parsed subject data (named information object). */
64 pk_context pk
; /**< Container for the public key context. */
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 */;
74 * Container for writing a CSR
76 typedef struct _x509write_csr
79 asn1_named_data
*subject
;
81 asn1_named_data
*extensions
;
85 #if defined(POLARSSL_X509_CSR_PARSE_C)
87 * \brief Load a Certificate Signing Request (CSR)
89 * \param csr CSR context to fill
90 * \param buf buffer holding the CRL data
91 * \param buflen size of the buffer
93 * \return 0 if successful, or a specific X509 or PEM error code
95 int x509_csr_parse( x509_csr
*csr
, const unsigned char *buf
, size_t buflen
);
97 #if defined(POLARSSL_FS_IO)
99 * \brief Load a Certificate Signing Request (CSR)
101 * \param csr CSR context to fill
102 * \param path filename to read the CSR from
104 * \return 0 if successful, or a specific X509 or PEM error code
106 int x509_csr_parse_file( x509_csr
*csr
, const char *path
);
107 #endif /* POLARSSL_FS_IO */
110 * \brief Returns an informational string about the
113 * \param buf Buffer to write to
114 * \param size Maximum size of buffer
115 * \param prefix A line prefix
116 * \param csr The X509 CSR to represent
118 * \return The amount of data written to the buffer, or -1 in
121 int x509_csr_info( char *buf
, size_t size
, const char *prefix
,
122 const x509_csr
*csr
);
125 * \brief Initialize a CSR
127 * \param csr CSR to initialize
129 void x509_csr_init( x509_csr
*csr
);
132 * \brief Unallocate all CSR data
134 * \param csr CSR to free
136 void x509_csr_free( x509_csr
*csr
);
137 #endif /* POLARSSL_X509_CSR_PARSE_C */
140 /* \} addtogroup x509_module */
142 #if defined(POLARSSL_X509_CSR_WRITE_C)
144 * \brief Initialize a CSR context
146 * \param ctx CSR context to initialize
148 void x509write_csr_init( x509write_csr
*ctx
);
151 * \brief Set the subject name for a CSR
152 * Subject names should contain a comma-separated list
153 * of OID types and values:
154 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
156 * \param ctx CSR context to use
157 * \param subject_name subject name to set
159 * \return 0 if subject name was parsed successfully, or
160 * a specific error code
162 int x509write_csr_set_subject_name( x509write_csr
*ctx
,
163 const char *subject_name
);
166 * \brief Set the key for a CSR (public key will be included,
167 * private key used to sign the CSR when writing it)
169 * \param ctx CSR context to use
170 * \param key Asymetric key to include
172 void x509write_csr_set_key( x509write_csr
*ctx
, pk_context
*key
);
175 * \brief Set the MD algorithm to use for the signature
176 * (e.g. POLARSSL_MD_SHA1)
178 * \param ctx CSR context to use
179 * \param md_alg MD algorithm to use
181 void x509write_csr_set_md_alg( x509write_csr
*ctx
, md_type_t md_alg
);
184 * \brief Set the Key Usage Extension flags
185 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
187 * \param ctx CSR context to use
188 * \param key_usage key usage flags to set
190 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
192 int x509write_csr_set_key_usage( x509write_csr
*ctx
, unsigned char key_usage
);
195 * \brief Set the Netscape Cert Type flags
196 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
198 * \param ctx CSR context to use
199 * \param ns_cert_type Netscape Cert Type flags to set
201 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
203 int x509write_csr_set_ns_cert_type( x509write_csr
*ctx
,
204 unsigned char ns_cert_type
);
207 * \brief Generic function to add to or replace an extension in the
210 * \param ctx CSR context to use
211 * \param oid OID of the extension
212 * \param oid_len length of the OID
213 * \param val value of the extension OCTET STRING
214 * \param val_len length of the value data
216 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
218 int x509write_csr_set_extension( x509write_csr
*ctx
,
219 const char *oid
, size_t oid_len
,
220 const unsigned char *val
, size_t val_len
);
223 * \brief Free the contents of a CSR context
225 * \param ctx CSR context to free
227 void x509write_csr_free( x509write_csr
*ctx
);
230 * \brief Write a CSR (Certificate Signing Request) to a
232 * Note: data is written at the end of the buffer! Use the
233 * return value to determine where you should start
236 * \param ctx CSR to write away
237 * \param buf buffer to write to
238 * \param size size of the buffer
239 * \param f_rng RNG function (for signature, see note)
240 * \param p_rng RNG parameter
242 * \return length of data written if successful, or a specific
245 * \note f_rng may be NULL if RSA is used for signature and the
246 * signature is made offline (otherwise f_rng is desirable
247 * for countermeasures against timing attacks).
248 * ECDSA signatures always require a non-NULL f_rng.
250 int x509write_csr_der( x509write_csr
*ctx
, unsigned char *buf
, size_t size
,
251 int (*f_rng
)(void *, unsigned char *, size_t),
254 #if defined(POLARSSL_PEM_WRITE_C)
256 * \brief Write a CSR (Certificate Signing Request) to a
259 * \param ctx CSR to write away
260 * \param buf buffer to write to
261 * \param size size of the buffer
262 * \param f_rng RNG function (for signature, see note)
263 * \param p_rng RNG parameter
265 * \return 0 successful, or a specific error code
267 * \note f_rng may be NULL if RSA is used for signature and the
268 * signature is made offline (otherwise f_rng is desirable
269 * for couermeasures against timing attacks).
270 * ECDSA signatures always require a non-NULL f_rng.
272 int x509write_csr_pem( x509write_csr
*ctx
, unsigned char *buf
, size_t size
,
273 int (*f_rng
)(void *, unsigned char *, size_t),
275 #endif /* POLARSSL_PEM_WRITE_C */
276 #endif /* POLARSSL_X509_CSR_WRITE_C */
282 #endif /* x509_csr.h */