blob: af4d2a12d5f9ac65041d909bb97203c3af3c4b47 [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/**
64 * Container for CSR named objects
65 */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000066typedef struct _x509_req_name
67{
68 char oid[128];
69 char name[128];
70
71 struct _x509_req_name *next;
72}
73x509_req_name;
74
Paul Bakkerf6774662013-08-25 11:47:51 +020075/**
76 * Container for a CSR
77 */
Paul Bakker82e29452013-08-25 11:01:31 +020078typedef struct _x509_csr
Paul Bakker8eabfc12013-08-25 10:18:25 +020079{
80 rsa_context *rsa;
81 x509_req_name *subject;
82 md_type_t md_alg;
Paul Bakkere5eae762013-08-26 12:05:14 +020083 asn1_named_data *extensions;
Paul Bakker8eabfc12013-08-25 10:18:25 +020084}
Paul Bakker82e29452013-08-25 11:01:31 +020085x509_csr;
Paul Bakker8eabfc12013-08-25 10:18:25 +020086
Paul Bakker9397dcb2013-09-06 09:55:26 +020087#define X509_CRT_VERSION_1 0
88#define X509_CRT_VERSION_2 1
89#define X509_CRT_VERSION_3 2
90
91#define X509_RFC5280_MAX_SERIAL_LEN 32
92#define X509_RFC5280_UTC_TIME_LEN 15
93
94/**
95 * Container for writing a certificate (CRT)
96 */
97typedef struct _x509write_cert
98{
99 int version;
100 mpi serial;
101 rsa_context *subject_key;
102 rsa_context *issuer_key;
103 x509_req_name *subject;
104 x509_req_name *issuer;
105 md_type_t md_alg;
106 char not_before[X509_RFC5280_UTC_TIME_LEN + 1];
107 char not_after[X509_RFC5280_UTC_TIME_LEN + 1];
108 asn1_named_data *extensions;
109}
110x509write_cert;
111
Paul Bakkerf6774662013-08-25 11:47:51 +0200112/* \} addtogroup x509_module */
113
114/**
115 * \brief Initialize a CSR context
116 *
117 * \param ctx CSR context to initialize
118 */
Paul Bakker82e29452013-08-25 11:01:31 +0200119void x509write_csr_init( x509_csr *ctx );
Paul Bakkerf6774662013-08-25 11:47:51 +0200120
121/**
122 * \brief Set the subject name for a CSR
123 * Subject names should contain a comma-separated list
124 * of OID types and values:
125 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
126 *
127 * \param ctx CSR context to use
128 * \param subject_name subject name to set
129 *
130 * \return 0 if subject name was parsed successfully, or
131 * a specific error code
132 */
Paul Bakker82e29452013-08-25 11:01:31 +0200133int x509write_csr_set_subject_name( x509_csr *ctx, char *subject_name );
Paul Bakkerf6774662013-08-25 11:47:51 +0200134
135/**
136 * \brief Set the RSA key for a CSR (public key will be included,
137 * private key used to sign the CSR when writing it)
138 *
139 * \param ctx CSR context to use
140 * \param rsa RSA key to include
141 */
Paul Bakker82e29452013-08-25 11:01:31 +0200142void x509write_csr_set_rsa_key( x509_csr *ctx, rsa_context *rsa );
Paul Bakkerf6774662013-08-25 11:47:51 +0200143
144/**
145 * \brief Set the MD algorithm to use for the signature
146 * (e.g. POLARSSL_MD_SHA1)
147 *
148 * \param ctx CSR context to use
149 * \param md_ald MD algorithm to use
150 */
Paul Bakker82e29452013-08-25 11:01:31 +0200151void x509write_csr_set_md_alg( x509_csr *ctx, md_type_t md_alg );
Paul Bakkerf6774662013-08-25 11:47:51 +0200152
153/**
Paul Bakkerfde42702013-08-25 14:47:27 +0200154 * \brief Set the Key Usage Extension flags
155 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
156 *
157 * \param ctx CSR context to use
Paul Bakker1c0e5502013-08-26 13:41:01 +0200158 * \param key_usage key usage flags to set
Paul Bakkere5eae762013-08-26 12:05:14 +0200159 *
160 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
Paul Bakkerfde42702013-08-25 14:47:27 +0200161 */
Paul Bakkere5eae762013-08-26 12:05:14 +0200162int x509write_csr_set_key_usage( x509_csr *ctx, unsigned char key_usage );
Paul Bakkerfde42702013-08-25 14:47:27 +0200163
164/**
Paul Bakker1c0e5502013-08-26 13:41:01 +0200165 * \brief Set the Netscape Cert Type flags
166 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
167 *
168 * \param ctx CSR context to use
169 * \param ns_cert_type Netscape Cert Type flags to set
170 *
171 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
172 */
173int x509write_csr_set_ns_cert_type( x509_csr *ctx, unsigned char ns_cert_type );
174
175/**
176 * \brief Generic function to add to or replace an extension in the CSR
177 *
178 * \param ctx CSR context to use
179 * \param oid OID of the extension
180 * \param oid_len length of the OID
181 * \param val value of the extension OCTET STRING
182 * \param val_len length of the value data
183 *
184 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
185 */
186int x509write_csr_set_extension( x509_csr *ctx,
187 const char *oid, size_t oid_len,
188 const unsigned char *val, size_t val_len );
189
190/**
Paul Bakkerf6774662013-08-25 11:47:51 +0200191 * \brief Free the contents of a CSR context
192 *
193 * \param ctx CSR context to free
194 */
Paul Bakker82e29452013-08-25 11:01:31 +0200195void x509write_csr_free( x509_csr *ctx );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200196
Paul Bakkerf6774662013-08-25 11:47:51 +0200197/**
Paul Bakker9397dcb2013-09-06 09:55:26 +0200198 * \brief Initialize a CRT writing context
199 *
200 * \param ctx CRT context to initialize
201 */
202void x509write_crt_init( x509write_cert *ctx );
203
204/**
205 * \brief Set the verion for a Certificate
206 * Default: X509_CRT_VERSION_3
207 *
208 * \param ctx CRT context to use
209 * \param version version to set (X509_CRT_VERSION_1, X509_CRT_VERSION_2 or
210 * X509_CRT_VERSION_3)
211 */
212void x509write_crt_set_version( x509write_cert *ctx, int version );
213
214/**
215 * \brief Set the serial number for a Certificate.
216 *
217 * \param ctx CRT context to use
218 * \param serial serial number to set
219 *
220 * \return 0 if successful
221 */
222int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial );
223
224/**
225 * \brief Set the validity period for a Certificate
226 * Timestamps should be in string format for UTC timezone
227 * i.e. "YYYYMMDDhhmmss"
228 * e.g. "20131231235959" for December 31st 2013
229 * at 23:59:59
230 *
231 * \param ctx CRT context to use
232 * \param not_before not_before timestamp
233 * \param not_after not_after timestamp
234 *
235 * \return 0 if timestamp was parsed successfully, or
236 * a specific error code
237 */
238int x509write_crt_set_validity( x509write_cert *ctx, char *not_before,
239 char *not_after );
240
241/**
242 * \brief Set the issuer name for a Certificate
243 * Issuer names should contain a comma-separated list
244 * of OID types and values:
245 * e.g. "C=NL,O=Offspark,CN=PolarSSL CA"
246 *
247 * \param ctx CRT context to use
248 * \param issuer_name issuer name to set
249 *
250 * \return 0 if issuer name was parsed successfully, or
251 * a specific error code
252 */
253int x509write_crt_set_issuer_name( x509write_cert *ctx, char *issuer_name );
254
255/**
256 * \brief Set the subject name for a Certificate
257 * Subject names should contain a comma-separated list
258 * of OID types and values:
259 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
260 *
261 * \param ctx CRT context to use
262 * \param subject_name subject name to set
263 *
264 * \return 0 if subject name was parsed successfully, or
265 * a specific error code
266 */
267int x509write_crt_set_subject_name( x509write_cert *ctx, char *subject_name );
268
269/**
270 * \brief Set the subject public key for the certificate
271 *
272 * \param ctx CRT context to use
273 * \param rsa RSA public key to include
274 */
275void x509write_crt_set_subject_key( x509write_cert *ctx, rsa_context *rsa );
276
277/**
278 * \brief Set the issuer key used for signing the certificate
279 *
280 * \param ctx CRT context to use
281 * \param rsa RSA key to sign with
282 */
283void x509write_crt_set_issuer_key( x509write_cert *ctx, rsa_context *rsa );
284
285/**
286 * \brief Set the MD algorithm to use for the signature
287 * (e.g. POLARSSL_MD_SHA1)
288 *
289 * \param ctx CRT context to use
290 * \param md_ald MD algorithm to use
291 */
292void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg );
293
294/**
Paul Bakker15162a02013-09-06 19:27:21 +0200295 * \brief Generic function to add to or replace an extension in the
296 * CRT
297 *
298 * \param ctx CRT context to use
299 * \param oid OID of the extension
300 * \param oid_len length of the OID
301 * \param critical if the extension is critical (per the RFC's definition)
302 * \param val value of the extension OCTET STRING
303 * \param val_len length of the value data
304 *
305 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
306 */
307int x509write_crt_set_extension( x509write_cert *ctx,
308 const char *oid, size_t oid_len,
309 int critical,
310 const unsigned char *val, size_t val_len );
311
312/**
313 * \brief Set the basicConstraints extension for a CRT
314 *
315 * \param ctx CRT context to use
316 * \param is_ca is this a CA certificate
317 * \param max_pathlen maximum length of certificate chains below this
318 * certificate (only for CA certificates, -1 is
319 * inlimited)
320 *
321 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
322 */
323int x509write_crt_set_basic_constraints( x509write_cert *ctx,
324 int is_ca, int max_pathlen );
325
326/**
327 * \brief Set the subjectKeyIdentifier extension for a CRT
328 * Requires that x509write_crt_set_subject_key() has been
329 * called before
330 *
331 * \param ctx CRT context to use
332 *
333 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
334 */
335int x509write_crt_set_subject_key_identifier( x509write_cert *ctx );
336
337/**
338 * \brief Set the authorityKeyIdentifier extension for a CRT
339 * Requires that x509write_crt_set_issuer_key() has been
340 * called before
341 *
342 * \param ctx CRT context to use
343 *
344 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
345 */
346int x509write_crt_set_authority_key_identifier( x509write_cert *ctx );
347
348/**
Paul Bakker9397dcb2013-09-06 09:55:26 +0200349 * \brief Free the contents of a CRT write context
350 *
351 * \param ctx CRT context to free
352 */
353void x509write_crt_free( x509write_cert *ctx );
354
355/**
356 * \brief Write a built up certificate to a X509 DER structure
357 * Note: data is written at the end of the buffer! Use the
358 * return value to determine where you should start
359 * using the buffer
360 *
361 * \param crt certificate to write away
362 * \param buf buffer to write to
363 * \param size size of the buffer
364 *
365 * \return length of data written if successful, or a specific
366 * error code
367 */
368int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size );
369
370/**
Paul Bakkerf6774662013-08-25 11:47:51 +0200371 * \brief Write a RSA public key to a PKCS#1 DER structure
372 * Note: data is written at the end of the buffer! Use the
373 * return value to determine where you should start
374 * using the buffer
375 *
376 * \param rsa RSA to write away
377 * \param buf buffer to write to
378 * \param size size of the buffer
379 *
380 * \return length of data written if successful, or a specific
381 * error code
382 */
Paul Bakker82e29452013-08-25 11:01:31 +0200383int x509write_pubkey_der( rsa_context *rsa, unsigned char *buf, size_t size );
Paul Bakkerf6774662013-08-25 11:47:51 +0200384
385/**
386 * \brief Write a RSA key to a PKCS#1 DER structure
387 * Note: data is written at the end of the buffer! Use the
388 * return value to determine where you should start
389 * using the buffer
390 *
391 * \param rsa RSA to write away
392 * \param buf buffer to write to
393 * \param size size of the buffer
394 *
395 * \return length of data written if successful, or a specific
396 * error code
397 */
Paul Bakker82e29452013-08-25 11:01:31 +0200398int x509write_key_der( rsa_context *rsa, unsigned char *buf, size_t size );
Paul Bakkerf6774662013-08-25 11:47:51 +0200399
400/**
401 * \brief Write a CSR (Certificate Signing Request) to a
402 * DER structure
403 * Note: data is written at the end of the buffer! Use the
404 * return value to determine where you should start
405 * using the buffer
406 *
407 * \param rsa CSR to write away
408 * \param buf buffer to write to
409 * \param size size of the buffer
410 *
411 * \return length of data written if successful, or a specific
412 * error code
413 */
Paul Bakker82e29452013-08-25 11:01:31 +0200414int x509write_csr_der( x509_csr *ctx, unsigned char *buf, size_t size );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000415
Paul Bakker135f1e92013-08-26 16:54:13 +0200416#if defined(POLARSSL_BASE64_C)
417/**
Paul Bakker9397dcb2013-09-06 09:55:26 +0200418 * \brief Write a built up certificate to a X509 PEM string
419 *
420 * \param crt certificate to write away
421 * \param buf buffer to write to
422 * \param size size of the buffer
423 *
424 * \return 0 successful, or a specific error code
425 */
426int x509write_crt_pem( x509write_cert *ctx, unsigned char *buf, size_t size );
427
428/**
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200429 * \brief Write a RSA public key to a PKCS#1 PEM string
430 *
431 * \param rsa RSA to write away
432 * \param buf buffer to write to
433 * \param size size of the buffer
434 *
435 * \return 0 successful, or a specific error code
436 */
437int x509write_pubkey_pem( rsa_context *rsa, unsigned char *buf, size_t size );
438
439/**
440 * \brief Write a RSA key to a PKCS#1 PEM string
441 *
442 * \param rsa RSA 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_key_pem( rsa_context *rsa, unsigned char *buf, size_t size );
449
450/**
Paul Bakker135f1e92013-08-26 16:54:13 +0200451 * \brief Write a CSR (Certificate Signing Request) to a
452 * PEM string
453 *
454 * \param rsa CSR to write away
455 * \param buf buffer to write to
456 * \param size size of the buffer
457 *
458 * \return 0 successful, or a specific error code
459 */
460int x509write_csr_pem( x509_csr *ctx, unsigned char *buf, size_t size );
461#endif /* POLARSSL_BASE64_C */
462
Paul Bakker407a0da2013-06-27 14:29:21 +0200463#ifdef __cplusplus
464}
465#endif
466
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000467#endif /* POLARSSL_X509_WRITE_H */