blob: 2e6fa0c7dba9379358cfa1a4fa6fafc6fbe51738 [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 Bakker82e29452013-08-25 11:01:31 +020066typedef struct _x509_csr
Paul Bakker8eabfc12013-08-25 10:18:25 +020067{
68 rsa_context *rsa;
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 Bakker82e29452013-08-25 11:01:31 +020073x509_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;
89 rsa_context *subject_key;
90 rsa_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;
99
Paul Bakkerf6774662013-08-25 11:47:51 +0200100/* \} addtogroup x509_module */
101
102/**
103 * \brief Initialize a CSR context
104 *
105 * \param ctx CSR context to initialize
106 */
Paul Bakker82e29452013-08-25 11:01:31 +0200107void x509write_csr_init( x509_csr *ctx );
Paul Bakkerf6774662013-08-25 11:47:51 +0200108
109/**
110 * \brief Set the subject name for a CSR
111 * Subject names should contain a comma-separated list
112 * of OID types and values:
113 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
114 *
115 * \param ctx CSR context to use
116 * \param subject_name subject name to set
117 *
118 * \return 0 if subject name was parsed successfully, or
119 * a specific error code
120 */
Paul Bakker82e29452013-08-25 11:01:31 +0200121int x509write_csr_set_subject_name( x509_csr *ctx, char *subject_name );
Paul Bakkerf6774662013-08-25 11:47:51 +0200122
123/**
124 * \brief Set the RSA key for a CSR (public key will be included,
125 * private key used to sign the CSR when writing it)
126 *
127 * \param ctx CSR context to use
128 * \param rsa RSA key to include
129 */
Paul Bakker82e29452013-08-25 11:01:31 +0200130void x509write_csr_set_rsa_key( x509_csr *ctx, rsa_context *rsa );
Paul Bakkerf6774662013-08-25 11:47:51 +0200131
132/**
133 * \brief Set the MD algorithm to use for the signature
134 * (e.g. POLARSSL_MD_SHA1)
135 *
136 * \param ctx CSR context to use
137 * \param md_ald MD algorithm to use
138 */
Paul Bakker82e29452013-08-25 11:01:31 +0200139void x509write_csr_set_md_alg( x509_csr *ctx, md_type_t md_alg );
Paul Bakkerf6774662013-08-25 11:47:51 +0200140
141/**
Paul Bakkerfde42702013-08-25 14:47:27 +0200142 * \brief Set the Key Usage Extension flags
143 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
144 *
145 * \param ctx CSR context to use
Paul Bakker1c0e5502013-08-26 13:41:01 +0200146 * \param key_usage key usage flags to set
Paul Bakkere5eae762013-08-26 12:05:14 +0200147 *
148 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
Paul Bakkerfde42702013-08-25 14:47:27 +0200149 */
Paul Bakkere5eae762013-08-26 12:05:14 +0200150int x509write_csr_set_key_usage( x509_csr *ctx, unsigned char key_usage );
Paul Bakkerfde42702013-08-25 14:47:27 +0200151
152/**
Paul Bakker1c0e5502013-08-26 13:41:01 +0200153 * \brief Set the Netscape Cert Type flags
154 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
155 *
156 * \param ctx CSR context to use
157 * \param ns_cert_type Netscape Cert Type flags to set
158 *
159 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
160 */
161int x509write_csr_set_ns_cert_type( x509_csr *ctx, unsigned char ns_cert_type );
162
163/**
164 * \brief Generic function to add to or replace an extension in the CSR
165 *
166 * \param ctx CSR context to use
167 * \param oid OID of the extension
168 * \param oid_len length of the OID
169 * \param val value of the extension OCTET STRING
170 * \param val_len length of the value data
171 *
172 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
173 */
174int x509write_csr_set_extension( x509_csr *ctx,
175 const char *oid, size_t oid_len,
176 const unsigned char *val, size_t val_len );
177
178/**
Paul Bakkerf6774662013-08-25 11:47:51 +0200179 * \brief Free the contents of a CSR context
180 *
181 * \param ctx CSR context to free
182 */
Paul Bakker82e29452013-08-25 11:01:31 +0200183void x509write_csr_free( x509_csr *ctx );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200184
Paul Bakkerf6774662013-08-25 11:47:51 +0200185/**
Paul Bakker9397dcb2013-09-06 09:55:26 +0200186 * \brief Initialize a CRT writing context
187 *
188 * \param ctx CRT context to initialize
189 */
190void x509write_crt_init( x509write_cert *ctx );
191
192/**
193 * \brief Set the verion for a Certificate
194 * Default: X509_CRT_VERSION_3
195 *
196 * \param ctx CRT context to use
197 * \param version version to set (X509_CRT_VERSION_1, X509_CRT_VERSION_2 or
198 * X509_CRT_VERSION_3)
199 */
200void x509write_crt_set_version( x509write_cert *ctx, int version );
201
202/**
203 * \brief Set the serial number for a Certificate.
204 *
205 * \param ctx CRT context to use
206 * \param serial serial number to set
207 *
208 * \return 0 if successful
209 */
210int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial );
211
212/**
213 * \brief Set the validity period for a Certificate
214 * Timestamps should be in string format for UTC timezone
215 * i.e. "YYYYMMDDhhmmss"
216 * e.g. "20131231235959" for December 31st 2013
217 * at 23:59:59
218 *
219 * \param ctx CRT context to use
220 * \param not_before not_before timestamp
221 * \param not_after not_after timestamp
222 *
223 * \return 0 if timestamp was parsed successfully, or
224 * a specific error code
225 */
226int x509write_crt_set_validity( x509write_cert *ctx, char *not_before,
227 char *not_after );
228
229/**
230 * \brief Set the issuer name for a Certificate
231 * Issuer names should contain a comma-separated list
232 * of OID types and values:
233 * e.g. "C=NL,O=Offspark,CN=PolarSSL CA"
234 *
235 * \param ctx CRT context to use
236 * \param issuer_name issuer name to set
237 *
238 * \return 0 if issuer name was parsed successfully, or
239 * a specific error code
240 */
241int x509write_crt_set_issuer_name( x509write_cert *ctx, char *issuer_name );
242
243/**
244 * \brief Set the subject name for a Certificate
245 * Subject names should contain a comma-separated list
246 * of OID types and values:
247 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
248 *
249 * \param ctx CRT context to use
250 * \param subject_name subject name to set
251 *
252 * \return 0 if subject name was parsed successfully, or
253 * a specific error code
254 */
255int x509write_crt_set_subject_name( x509write_cert *ctx, char *subject_name );
256
257/**
258 * \brief Set the subject public key for the certificate
259 *
260 * \param ctx CRT context to use
261 * \param rsa RSA public key to include
262 */
263void x509write_crt_set_subject_key( x509write_cert *ctx, rsa_context *rsa );
264
265/**
266 * \brief Set the issuer key used for signing the certificate
267 *
268 * \param ctx CRT context to use
269 * \param rsa RSA key to sign with
270 */
271void x509write_crt_set_issuer_key( x509write_cert *ctx, rsa_context *rsa );
272
273/**
274 * \brief Set the MD algorithm to use for the signature
275 * (e.g. POLARSSL_MD_SHA1)
276 *
277 * \param ctx CRT context to use
278 * \param md_ald MD algorithm to use
279 */
280void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg );
281
282/**
Paul Bakker15162a02013-09-06 19:27:21 +0200283 * \brief Generic function to add to or replace an extension in the
284 * CRT
285 *
286 * \param ctx CRT context to use
287 * \param oid OID of the extension
288 * \param oid_len length of the OID
289 * \param critical if the extension is critical (per the RFC's definition)
290 * \param val value of the extension OCTET STRING
291 * \param val_len length of the value data
292 *
293 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
294 */
295int x509write_crt_set_extension( x509write_cert *ctx,
296 const char *oid, size_t oid_len,
297 int critical,
298 const unsigned char *val, size_t val_len );
299
300/**
301 * \brief Set the basicConstraints extension for a CRT
302 *
303 * \param ctx CRT context to use
304 * \param is_ca is this a CA certificate
305 * \param max_pathlen maximum length of certificate chains below this
306 * certificate (only for CA certificates, -1 is
307 * inlimited)
308 *
309 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
310 */
311int x509write_crt_set_basic_constraints( x509write_cert *ctx,
312 int is_ca, int max_pathlen );
313
314/**
315 * \brief Set the subjectKeyIdentifier extension for a CRT
316 * Requires that x509write_crt_set_subject_key() has been
317 * called before
318 *
319 * \param ctx CRT context to use
320 *
321 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
322 */
323int x509write_crt_set_subject_key_identifier( x509write_cert *ctx );
324
325/**
326 * \brief Set the authorityKeyIdentifier extension for a CRT
327 * Requires that x509write_crt_set_issuer_key() has been
328 * called before
329 *
330 * \param ctx CRT context to use
331 *
332 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
333 */
334int x509write_crt_set_authority_key_identifier( x509write_cert *ctx );
335
336/**
Paul Bakker9397dcb2013-09-06 09:55:26 +0200337 * \brief Free the contents of a CRT write context
338 *
339 * \param ctx CRT context to free
340 */
341void x509write_crt_free( x509write_cert *ctx );
342
343/**
344 * \brief Write a built up certificate to a X509 DER structure
345 * Note: data is written at the end of the buffer! Use the
346 * return value to determine where you should start
347 * using the buffer
348 *
349 * \param crt certificate to write away
350 * \param buf buffer to write to
351 * \param size size of the buffer
352 *
353 * \return length of data written if successful, or a specific
354 * error code
355 */
356int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size );
357
358/**
Paul Bakkerf6774662013-08-25 11:47:51 +0200359 * \brief Write a RSA public key to a PKCS#1 DER structure
360 * Note: data is written at the end of the buffer! Use the
361 * return value to determine where you should start
362 * using the buffer
363 *
364 * \param rsa RSA to write away
365 * \param buf buffer to write to
366 * \param size size of the buffer
367 *
368 * \return length of data written if successful, or a specific
369 * error code
370 */
Paul Bakker82e29452013-08-25 11:01:31 +0200371int x509write_pubkey_der( rsa_context *rsa, unsigned char *buf, size_t size );
Paul Bakkerf6774662013-08-25 11:47:51 +0200372
373/**
374 * \brief Write a RSA key to a PKCS#1 DER structure
375 * Note: data is written at the end of the buffer! Use the
376 * return value to determine where you should start
377 * using the buffer
378 *
379 * \param rsa RSA to write away
380 * \param buf buffer to write to
381 * \param size size of the buffer
382 *
383 * \return length of data written if successful, or a specific
384 * error code
385 */
Paul Bakker82e29452013-08-25 11:01:31 +0200386int x509write_key_der( rsa_context *rsa, unsigned char *buf, size_t size );
Paul Bakkerf6774662013-08-25 11:47:51 +0200387
388/**
389 * \brief Write a CSR (Certificate Signing Request) to a
390 * DER structure
391 * Note: data is written at the end of the buffer! Use the
392 * return value to determine where you should start
393 * using the buffer
394 *
395 * \param rsa CSR to write away
396 * \param buf buffer to write to
397 * \param size size of the buffer
398 *
399 * \return length of data written if successful, or a specific
400 * error code
401 */
Paul Bakker82e29452013-08-25 11:01:31 +0200402int x509write_csr_der( x509_csr *ctx, unsigned char *buf, size_t size );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000403
Paul Bakker135f1e92013-08-26 16:54:13 +0200404#if defined(POLARSSL_BASE64_C)
405/**
Paul Bakker9397dcb2013-09-06 09:55:26 +0200406 * \brief Write a built up certificate to a X509 PEM string
407 *
408 * \param crt certificate to write away
409 * \param buf buffer to write to
410 * \param size size of the buffer
411 *
412 * \return 0 successful, or a specific error code
413 */
414int x509write_crt_pem( x509write_cert *ctx, unsigned char *buf, size_t size );
415
416/**
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200417 * \brief Write a RSA public key to a PKCS#1 PEM string
418 *
419 * \param rsa RSA to write away
420 * \param buf buffer to write to
421 * \param size size of the buffer
422 *
423 * \return 0 successful, or a specific error code
424 */
425int x509write_pubkey_pem( rsa_context *rsa, unsigned char *buf, size_t size );
426
427/**
428 * \brief Write a RSA key to a PKCS#1 PEM string
429 *
430 * \param rsa RSA to write away
431 * \param buf buffer to write to
432 * \param size size of the buffer
433 *
434 * \return 0 successful, or a specific error code
435 */
436int x509write_key_pem( rsa_context *rsa, unsigned char *buf, size_t size );
437
438/**
Paul Bakker135f1e92013-08-26 16:54:13 +0200439 * \brief Write a CSR (Certificate Signing Request) to a
440 * PEM string
441 *
442 * \param rsa CSR to write away
443 * \param buf buffer to write to
444 * \param size size of the buffer
445 *
446 * \return 0 successful, or a specific error code
447 */
448int x509write_csr_pem( x509_csr *ctx, unsigned char *buf, size_t size );
449#endif /* POLARSSL_BASE64_C */
450
Paul Bakker407a0da2013-06-27 14:29:21 +0200451#ifdef __cplusplus
452}
453#endif
454
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000455#endif /* POLARSSL_X509_WRITE_H */