blob: e90b582b3299527ab6d3379b68b9cca57f9e6e9e [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/**
2 * \file x509write.h
3 *
4 * \brief X509 buffer writing functionality
5 *
Paul Bakker407a0da2013-06-27 14:29:21 +02006 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerbdb912d2012-02-13 23:11:30 +00007 *
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_WRITE_H
28#define POLARSSL_X509_WRITE_H
29
Paul Bakkerbc956d92013-04-19 14:51:29 +020030#include "config.h"
31
Paul Bakkerfde42702013-08-25 14:47:27 +020032#include "x509.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000033
Paul Bakkerf6774662013-08-25 11:47:51 +020034/**
35 * \addtogroup x509_module
36 * \{
37 */
38
39/**
40 * \name X509 Write Error codes
41 * \{
42 */
Paul Bakker0e06c0f2013-08-25 11:21:30 +020043#define POLARSSL_ERR_X509WRITE_UNKNOWN_OID -0x5F80 /**< Requested OID is unknown. */
44#define POLARSSL_ERR_X509WRITE_BAD_INPUT_DATA -0x5F00 /**< Failed to allocate memory. */
45#define POLARSSL_ERR_X509WRITE_MALLOC_FAILED -0x5E80 /**< Failed to allocate memory. */
Paul Bakkerf6774662013-08-25 11:47:51 +020046/* \} name */
47/* \} addtogroup x509_module */
Paul Bakker8eabfc12013-08-25 10:18:25 +020048
Paul Bakker407a0da2013-06-27 14:29:21 +020049#ifdef __cplusplus
50extern "C" {
51#endif
52
Paul Bakkerf6774662013-08-25 11:47:51 +020053/**
54 * \addtogroup x509_module
55 * \{
56 */
57
58/**
59 * \name Structures for writing X.509 CSRs (Certificate Signing Request)
60 * \{
61 */
62
63/**
Paul Bakkerf6774662013-08-25 11:47:51 +020064 * Container for a CSR
65 */
Paul Bakkercd358032013-09-09 12:08:11 +020066typedef struct _x509write_csr
Paul Bakker8eabfc12013-08-25 10:18:25 +020067{
Manuel Pégourié-Gonnard5353a032013-09-11 12:14:26 +020068 pk_context *key;
Paul Bakker5f45e622013-09-09 12:02:36 +020069 asn1_named_data *subject;
Paul Bakker8eabfc12013-08-25 10:18:25 +020070 md_type_t md_alg;
Paul Bakkere5eae762013-08-26 12:05:14 +020071 asn1_named_data *extensions;
Paul Bakker8eabfc12013-08-25 10:18:25 +020072}
Paul Bakkercd358032013-09-09 12:08:11 +020073x509write_csr;
Paul Bakker8eabfc12013-08-25 10:18:25 +020074
Paul Bakker9397dcb2013-09-06 09:55:26 +020075#define X509_CRT_VERSION_1 0
76#define X509_CRT_VERSION_2 1
77#define X509_CRT_VERSION_3 2
78
79#define X509_RFC5280_MAX_SERIAL_LEN 32
80#define X509_RFC5280_UTC_TIME_LEN 15
81
82/**
83 * Container for writing a certificate (CRT)
84 */
85typedef struct _x509write_cert
86{
87 int version;
88 mpi serial;
Manuel Pégourié-Gonnard53c64252013-09-12 05:39:46 +020089 pk_context *subject_key;
90 pk_context *issuer_key;
Paul Bakker5f45e622013-09-09 12:02:36 +020091 asn1_named_data *subject;
92 asn1_named_data *issuer;
Paul Bakker9397dcb2013-09-06 09:55:26 +020093 md_type_t md_alg;
94 char not_before[X509_RFC5280_UTC_TIME_LEN + 1];
95 char not_after[X509_RFC5280_UTC_TIME_LEN + 1];
96 asn1_named_data *extensions;
97}
98x509write_cert;
Paul Bakker8eabfc12013-08-25 10:18:25 +020099
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200100/* \} name */
Paul Bakkerf6774662013-08-25 11:47:51 +0200101/* \} addtogroup x509_module */
102
103/**
104 * \brief Initialize a CSR context
105 *
106 * \param ctx CSR context to initialize
107 */
Paul Bakkercd358032013-09-09 12:08:11 +0200108void x509write_csr_init( x509write_csr *ctx );
Paul Bakkerf6774662013-08-25 11:47:51 +0200109
110/**
111 * \brief Set the subject name for a CSR
112 * Subject names should contain a comma-separated list
113 * of OID types and values:
114 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
115 *
116 * \param ctx CSR context to use
117 * \param subject_name subject name to set
118 *
119 * \return 0 if subject name was parsed successfully, or
120 * a specific error code
121 */
Paul Bakkercd358032013-09-09 12:08:11 +0200122int x509write_csr_set_subject_name( x509write_csr *ctx, char *subject_name );
Paul Bakkerf6774662013-08-25 11:47:51 +0200123
124/**
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200125 * \brief Set the key for a CSR (public key will be included,
Paul Bakkerf6774662013-08-25 11:47:51 +0200126 * private key used to sign the CSR when writing it)
127 *
128 * \param ctx CSR context to use
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200129 * \param key Asymetric key to include
Paul Bakkerf6774662013-08-25 11:47:51 +0200130 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200131void x509write_csr_set_key( x509write_csr *ctx, pk_context *key );
Paul Bakkerf6774662013-08-25 11:47:51 +0200132
133/**
134 * \brief Set the MD algorithm to use for the signature
135 * (e.g. POLARSSL_MD_SHA1)
136 *
137 * \param ctx CSR context to use
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200138 * \param md_alg MD algorithm to use
Paul Bakkerf6774662013-08-25 11:47:51 +0200139 */
Paul Bakkercd358032013-09-09 12:08:11 +0200140void x509write_csr_set_md_alg( x509write_csr *ctx, md_type_t md_alg );
Paul Bakkerf6774662013-08-25 11:47:51 +0200141
142/**
Paul Bakkerfde42702013-08-25 14:47:27 +0200143 * \brief Set the Key Usage Extension flags
144 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
145 *
146 * \param ctx CSR context to use
Paul Bakker1c0e5502013-08-26 13:41:01 +0200147 * \param key_usage key usage flags to set
Paul Bakkere5eae762013-08-26 12:05:14 +0200148 *
149 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
Paul Bakkerfde42702013-08-25 14:47:27 +0200150 */
Paul Bakkercd358032013-09-09 12:08:11 +0200151int x509write_csr_set_key_usage( x509write_csr *ctx, unsigned char key_usage );
Paul Bakkerfde42702013-08-25 14:47:27 +0200152
153/**
Paul Bakker1c0e5502013-08-26 13:41:01 +0200154 * \brief Set the Netscape Cert Type flags
155 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
156 *
157 * \param ctx CSR context to use
158 * \param ns_cert_type Netscape Cert Type flags to set
159 *
160 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
161 */
Paul Bakkercd358032013-09-09 12:08:11 +0200162int x509write_csr_set_ns_cert_type( x509write_csr *ctx,
163 unsigned char ns_cert_type );
Paul Bakker1c0e5502013-08-26 13:41:01 +0200164
165/**
166 * \brief Generic function to add to or replace an extension in the CSR
167 *
168 * \param ctx CSR context to use
169 * \param oid OID of the extension
170 * \param oid_len length of the OID
171 * \param val value of the extension OCTET STRING
172 * \param val_len length of the value data
173 *
174 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
175 */
Paul Bakkercd358032013-09-09 12:08:11 +0200176int x509write_csr_set_extension( x509write_csr *ctx,
Paul Bakker1c0e5502013-08-26 13:41:01 +0200177 const char *oid, size_t oid_len,
178 const unsigned char *val, size_t val_len );
179
180/**
Paul Bakkerf6774662013-08-25 11:47:51 +0200181 * \brief Free the contents of a CSR context
182 *
183 * \param ctx CSR context to free
184 */
Paul Bakkercd358032013-09-09 12:08:11 +0200185void x509write_csr_free( x509write_csr *ctx );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200186
Paul Bakkerf6774662013-08-25 11:47:51 +0200187/**
Paul Bakker9397dcb2013-09-06 09:55:26 +0200188 * \brief Initialize a CRT writing context
189 *
190 * \param ctx CRT context to initialize
191 */
192void x509write_crt_init( x509write_cert *ctx );
193
194/**
195 * \brief Set the verion for a Certificate
196 * Default: X509_CRT_VERSION_3
197 *
198 * \param ctx CRT context to use
199 * \param version version to set (X509_CRT_VERSION_1, X509_CRT_VERSION_2 or
200 * X509_CRT_VERSION_3)
201 */
202void x509write_crt_set_version( x509write_cert *ctx, int version );
203
204/**
205 * \brief Set the serial number for a Certificate.
206 *
207 * \param ctx CRT context to use
208 * \param serial serial number to set
209 *
210 * \return 0 if successful
211 */
212int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial );
213
214/**
215 * \brief Set the validity period for a Certificate
216 * Timestamps should be in string format for UTC timezone
217 * i.e. "YYYYMMDDhhmmss"
218 * e.g. "20131231235959" for December 31st 2013
219 * at 23:59:59
220 *
221 * \param ctx CRT context to use
222 * \param not_before not_before timestamp
223 * \param not_after not_after timestamp
224 *
225 * \return 0 if timestamp was parsed successfully, or
226 * a specific error code
227 */
228int x509write_crt_set_validity( x509write_cert *ctx, char *not_before,
229 char *not_after );
230
231/**
232 * \brief Set the issuer name for a Certificate
233 * Issuer names should contain a comma-separated list
234 * of OID types and values:
235 * e.g. "C=NL,O=Offspark,CN=PolarSSL CA"
236 *
237 * \param ctx CRT context to use
238 * \param issuer_name issuer name to set
239 *
240 * \return 0 if issuer name was parsed successfully, or
241 * a specific error code
242 */
243int x509write_crt_set_issuer_name( x509write_cert *ctx, char *issuer_name );
244
245/**
246 * \brief Set the subject name for a Certificate
247 * Subject names should contain a comma-separated list
248 * of OID types and values:
249 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
250 *
251 * \param ctx CRT context to use
252 * \param subject_name subject name to set
253 *
254 * \return 0 if subject name was parsed successfully, or
255 * a specific error code
256 */
257int x509write_crt_set_subject_name( x509write_cert *ctx, char *subject_name );
258
259/**
260 * \brief Set the subject public key for the certificate
261 *
262 * \param ctx CRT context to use
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200263 * \param key public key to include
Paul Bakker9397dcb2013-09-06 09:55:26 +0200264 */
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200265void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200266
267/**
268 * \brief Set the issuer key used for signing the certificate
269 *
270 * \param ctx CRT context to use
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200271 * \param key private key to sign with
Paul Bakker9397dcb2013-09-06 09:55:26 +0200272 */
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200273void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200274
275/**
276 * \brief Set the MD algorithm to use for the signature
277 * (e.g. POLARSSL_MD_SHA1)
278 *
279 * \param ctx CRT context to use
280 * \param md_ald MD algorithm to use
281 */
282void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg );
283
284/**
Paul Bakker15162a02013-09-06 19:27:21 +0200285 * \brief Generic function to add to or replace an extension in the
286 * CRT
287 *
288 * \param ctx CRT context to use
289 * \param oid OID of the extension
290 * \param oid_len length of the OID
291 * \param critical if the extension is critical (per the RFC's definition)
292 * \param val value of the extension OCTET STRING
293 * \param val_len length of the value data
294 *
295 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
296 */
297int x509write_crt_set_extension( x509write_cert *ctx,
298 const char *oid, size_t oid_len,
299 int critical,
300 const unsigned char *val, size_t val_len );
301
302/**
303 * \brief Set the basicConstraints extension for a CRT
304 *
305 * \param ctx CRT context to use
306 * \param is_ca is this a CA certificate
307 * \param max_pathlen maximum length of certificate chains below this
308 * certificate (only for CA certificates, -1 is
309 * inlimited)
310 *
311 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
312 */
313int x509write_crt_set_basic_constraints( x509write_cert *ctx,
314 int is_ca, int max_pathlen );
315
316/**
317 * \brief Set the subjectKeyIdentifier extension for a CRT
318 * Requires that x509write_crt_set_subject_key() has been
319 * called before
320 *
321 * \param ctx CRT context to use
322 *
323 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
324 */
325int x509write_crt_set_subject_key_identifier( x509write_cert *ctx );
326
327/**
328 * \brief Set the authorityKeyIdentifier extension for a CRT
329 * Requires that x509write_crt_set_issuer_key() has been
330 * called before
331 *
332 * \param ctx CRT context to use
333 *
334 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
335 */
336int x509write_crt_set_authority_key_identifier( x509write_cert *ctx );
337
338/**
Paul Bakker52be08c2013-09-09 12:37:54 +0200339 * \brief Set the Key Usage Extension flags
340 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
341 *
342 * \param ctx CRT context to use
343 * \param key_usage key usage flags to set
344 *
345 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
346 */
347int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage );
348
349/**
350 * \brief Set the Netscape Cert Type flags
351 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
352 *
353 * \param ctx CRT context to use
354 * \param ns_cert_type Netscape Cert Type flags to set
355 *
356 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
357 */
358int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
359 unsigned char ns_cert_type );
360
361/**
Paul Bakker9397dcb2013-09-06 09:55:26 +0200362 * \brief Free the contents of a CRT write context
363 *
364 * \param ctx CRT context to free
365 */
366void x509write_crt_free( x509write_cert *ctx );
367
368/**
369 * \brief Write a built up certificate to a X509 DER structure
Paul Bakkerf6774662013-08-25 11:47:51 +0200370 * Note: data is written at the end of the buffer! Use the
371 * return value to determine where you should start
372 * using the buffer
373 *
Paul Bakker9397dcb2013-09-06 09:55:26 +0200374 * \param crt certificate to write away
375 * \param buf buffer to write to
376 * \param size size of the buffer
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200377 * \param f_rng RNG function (for signature, see note)
378 * \param p_rng RNG parameter
Paul Bakker9397dcb2013-09-06 09:55:26 +0200379 *
380 * \return length of data written if successful, or a specific
381 * error code
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200382 *
383 * \note f_rng may be NULL if RSA is used for signature and the
384 * signature is made offline (otherwise f_rng is desirable
385 * for countermeasures against timing attacks).
386 * ECDSA signatures always require a non-NULL f_rng.
Paul Bakker9397dcb2013-09-06 09:55:26 +0200387 */
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200388int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
389 int (*f_rng)(void *, unsigned char *, size_t),
390 void *p_rng );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200391
392/**
Paul Bakkerf6774662013-08-25 11:47:51 +0200393 * \brief Write a CSR (Certificate Signing Request) to a
394 * DER structure
395 * Note: data is written at the end of the buffer! Use the
396 * return value to determine where you should start
397 * using the buffer
398 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200399 * \param ctx CSR to write away
Paul Bakkerf6774662013-08-25 11:47:51 +0200400 * \param buf buffer to write to
401 * \param size size of the buffer
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200402 * \param f_rng RNG function (for signature, see note)
403 * \param p_rng RNG parameter
Paul Bakkerf6774662013-08-25 11:47:51 +0200404 *
405 * \return length of data written if successful, or a specific
406 * error code
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200407 *
408 * \note f_rng may be NULL if RSA is used for signature and the
409 * signature is made offline (otherwise f_rng is desirable
410 * for countermeasures against timing attacks).
411 * ECDSA signatures always require a non-NULL f_rng.
Paul Bakkerf6774662013-08-25 11:47:51 +0200412 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200413int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size,
414 int (*f_rng)(void *, unsigned char *, size_t),
415 void *p_rng );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000416
Paul Bakker77e23fb2013-09-15 20:03:26 +0200417#if defined(POLARSSL_PEM_C)
Paul Bakker135f1e92013-08-26 16:54:13 +0200418/**
Paul Bakker9397dcb2013-09-06 09:55:26 +0200419 * \brief Write a built up certificate to a X509 PEM string
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200420 *
Paul Bakker9397dcb2013-09-06 09:55:26 +0200421 * \param crt certificate to write away
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200422 * \param buf buffer to write to
423 * \param size size of the buffer
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200424 * \param f_rng RNG function (for signature, see note)
425 * \param p_rng RNG parameter
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200426 *
427 * \return 0 successful, or a specific error code
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200428 *
429 * \note f_rng may be NULL if RSA is used for signature and the
430 * signature is made offline (otherwise f_rng is desirable
431 * for countermeasures against timing attacks).
432 * ECDSA signatures always require a non-NULL f_rng.
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200433 */
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200434int x509write_crt_pem( x509write_cert *ctx, unsigned char *buf, size_t size,
435 int (*f_rng)(void *, unsigned char *, size_t),
436 void *p_rng );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200437
438/**
Paul Bakker135f1e92013-08-26 16:54:13 +0200439 * \brief Write a CSR (Certificate Signing Request) to a
440 * PEM string
441 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200442 * \param ctx CSR to write away
Paul Bakker135f1e92013-08-26 16:54:13 +0200443 * \param buf buffer to write to
444 * \param size size of the buffer
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200445 * \param f_rng RNG function (for signature, see note)
446 * \param p_rng RNG parameter
Paul Bakker135f1e92013-08-26 16:54:13 +0200447 *
448 * \return 0 successful, or a specific error code
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200449 *
450 * \note f_rng may be NULL if RSA is used for signature and the
451 * signature is made offline (otherwise f_rng is desirable
452 * for couermeasures against timing attacks).
453 * ECDSA signatures always require a non-NULL f_rng.
Paul Bakker135f1e92013-08-26 16:54:13 +0200454 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200455int x509write_csr_pem( x509write_csr *ctx, unsigned char *buf, size_t size,
456 int (*f_rng)(void *, unsigned char *, size_t),
457 void *p_rng );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200458#endif /* POLARSSL_PEM_C */
Paul Bakker135f1e92013-08-26 16:54:13 +0200459
Paul Bakker407a0da2013-06-27 14:29:21 +0200460#ifdef __cplusplus
461}
462#endif
463
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000464#endif /* POLARSSL_X509_WRITE_H */