blob: d534ad8196815ab0afd53e1e95b362662182ba18 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/**
2 * \file x509_crt.h
3 *
4 * \brief X.509 certificate parsing and writing
5 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02009 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +020010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24#ifndef POLARSSL_X509_CRT_H
25#define POLARSSL_X509_CRT_H
26
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032
33#include "x509.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034#include "x509_crl.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
36/**
37 * \addtogroup x509_module
38 * \{
39 */
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/**
46 * \name Structures and functions for parsing and writing X.509 certificates
47 * \{
48 */
49
50/**
51 * Container for an X.509 certificate. The certificate may be chained.
52 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +020053typedef struct _x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054{
55 x509_buf raw; /**< The raw certificate data (DER). */
56 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
57
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020058 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
60 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
61
62 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
63 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
64
65 x509_name issuer; /**< The parsed issuer data (named information object). */
66 x509_name subject; /**< The parsed subject data (named information object). */
67
68 x509_time valid_from; /**< Start time of certificate validity. */
69 x509_time valid_to; /**< End time of certificate validity. */
70
71 pk_context pk; /**< Container for the public key context. */
72
73 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
74 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020075 x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076 x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
77
78 int ext_types; /**< Bit string containing detected and parsed extensions */
79 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
80 int max_pathlen; /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. Path length is 1 higher than RFC 5280 'meaning', so 1+ */
81
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020082 unsigned char key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083
84 x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
85
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020086 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087
88 x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
89 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
90 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +020091 pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. POLARSSL_PK_RSA */
Paul Bakker6dade7c2014-06-12 22:02:14 +020092 void *sig_opts; /**< Signature options to be passed to pk_verify_ext(), e.g. for RSASSA-PSS */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093
Paul Bakkerc559c7a2013-09-18 14:13:26 +020094 struct _x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095}
Paul Bakkerc559c7a2013-09-18 14:13:26 +020096x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097
98#define X509_CRT_VERSION_1 0
99#define X509_CRT_VERSION_2 1
100#define X509_CRT_VERSION_3 2
101
102#define X509_RFC5280_MAX_SERIAL_LEN 32
103#define X509_RFC5280_UTC_TIME_LEN 15
104
105/**
106 * Container for writing a certificate (CRT)
107 */
108typedef struct _x509write_cert
109{
110 int version;
111 mpi serial;
112 pk_context *subject_key;
113 pk_context *issuer_key;
114 asn1_named_data *subject;
115 asn1_named_data *issuer;
116 md_type_t md_alg;
117 char not_before[X509_RFC5280_UTC_TIME_LEN + 1];
118 char not_after[X509_RFC5280_UTC_TIME_LEN + 1];
119 asn1_named_data *extensions;
120}
121x509write_cert;
122
123#if defined(POLARSSL_X509_CRT_PARSE_C)
124/**
125 * \brief Parse a single DER formatted certificate and add it
126 * to the chained list.
127 *
128 * \param chain points to the start of the chain
129 * \param buf buffer holding the certificate DER data
130 * \param buflen size of the buffer
131 *
132 * \return 0 if successful, or a specific X509 or PEM error code
133 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200134int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200135 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200136
137/**
138 * \brief Parse one or more certificates and add them
139 * to the chained list. Parses permissively. If some
140 * certificates can be parsed, the result is the number
141 * of failed certificates it encountered. If none complete
142 * correctly, the first error is returned.
143 *
144 * \param chain points to the start of the chain
145 * \param buf buffer holding the certificate data
146 * \param buflen size of the buffer
147 *
148 * \return 0 if all certificates parsed successfully, a positive number
149 * if partly successful or a specific X509 or PEM error code
150 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200151int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200152
153#if defined(POLARSSL_FS_IO)
154/**
155 * \brief Load one or more certificates and add them
156 * to the chained list. Parses permissively. If some
157 * certificates can be parsed, the result is the number
158 * of failed certificates it encountered. If none complete
159 * correctly, the first error is returned.
160 *
161 * \param chain points to the start of the chain
162 * \param path filename to read the certificates from
163 *
164 * \return 0 if all certificates parsed successfully, a positive number
165 * if partly successful or a specific X509 or PEM error code
166 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200167int x509_crt_parse_file( x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200168
169/**
170 * \brief Load one or more certificate files from a path and add them
171 * to the chained list. Parses permissively. If some
172 * certificates can be parsed, the result is the number
173 * of failed certificates it encountered. If none complete
174 * correctly, the first error is returned.
175 *
Manuel Pégourié-Gonnarde3339ce2013-11-28 17:16:41 +0100176 * \warning This function is NOT thread-safe unless
177 * POLARSSL_THREADING_PTHREADS is defined. If you're using an
178 * alternative threading implementation, you should either use
179 * this function only in the main thread, or mutex it.
180 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200181 * \param chain points to the start of the chain
182 * \param path directory / folder to read the certificate files from
183 *
184 * \return 0 if all certificates parsed successfully, a positive number
185 * if partly successful or a specific X509 or PEM error code
186 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200187int x509_crt_parse_path( x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200188#endif /* POLARSSL_FS_IO */
189
190/**
191 * \brief Returns an informational string about the
192 * certificate.
193 *
194 * \param buf Buffer to write to
195 * \param size Maximum size of buffer
196 * \param prefix A line prefix
197 * \param crt The X509 certificate to represent
198 *
199 * \return The amount of data written to the buffer, or -1 in
200 * case of an error.
201 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200202int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200203 const x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200204
205/**
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200206 * \brief Returns an informational string about the
207 * verification status of a certificate.
208 *
209 * \param buf Buffer to write to
210 * \param size Maximum size of buffer
211 * \param prefix A line prefix
212 * \param flags Verification flags created by x509_crt_verify()
213 *
214 * \return The amount of data written to the buffer, or -1 in
215 * case of an error.
216 */
217int x509_crt_verify_info( char *buf, size_t size, const char *prefix,
218 int flags );
219
220/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221 * \brief Verify the certificate signature
222 *
223 * The verify callback is a user-supplied callback that
224 * can clear / modify / add flags for a certificate. If set,
225 * the verification callback is called for each
226 * certificate in the chain (from the trust-ca down to the
227 * presented crt). The parameters for the callback are:
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200228 * (void *parameter, x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229 * int *flags). With the flags representing current flags for
230 * that specific certificate and the certificate depth from
231 * the bottom (Peer cert depth = 0).
232 *
233 * All flags left after returning from the callback
234 * are also returned to the application. The function should
Manuel Pégourié-Gonnardd0e75572017-07-10 11:31:01 +0200235 * return 0 for anything (including invalid certificates)
236 * other than fatal error, as a non-zero return code
237 * immediately aborts the verification process. For fatal
238 * errors, a specific error code should be used (different
239 * from POLARSSL_ERR_X509_CERT_VERIFY_FAILED which should not
240 * be returned at this point), or POLARSSL_ERR_X509_FATAL_ERROR
241 * can be used if no better code is available.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200242 *
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200243 * \note In case verification failed, the results can be displayed
244 * using \c x509_crt_verify_info()
245 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200246 * \param crt a certificate to be verified
247 * \param trust_ca the trusted CA chain
248 * \param ca_crl the CRL chain for trusted CA's
249 * \param cn expected Common Name (can be set to
250 * NULL if the CN must not be verified)
251 * \param flags result of the verification
252 * \param f_vrfy verification function
253 * \param p_vrfy verification parameter
254 *
Manuel Pégourié-Gonnardcfea3eb2017-07-10 11:45:47 +0200255 * \return 0 (and flags set to 0) if the chain was verified and valid,
256 * POLARSSL_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
257 * but found to be invalid, in which case *flags will have one
258 * or more BADCERT_XXX or POLARSSL_X509_BADCRL_XXX
259 * flags set, or another error (and flags set to 0xffffffff)
260 * in case of a fatal error encountered during the
261 * verification process.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200262 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200263int x509_crt_verify( x509_crt *crt,
264 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200265 x509_crl *ca_crl,
266 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200267 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200268 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200270#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
271/**
272 * \brief Check usage of certificate against keyUsage extension.
273 *
274 * \param crt Leaf certificate used.
275 * \param usage Intended usage(s) (eg KU_KEY_ENCIPHERMENT before using the
276 * certificate to perform an RSA key exchange).
277 *
278 * \return 0 is these uses of the certificate are allowed,
Paul Bakker02ff5ce2014-04-09 15:53:09 +0200279 * POLARSSL_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200280 * is present but does not contain all the bits set in the
281 * usage argument.
282 *
283 * \note You should only call this function on leaf certificates, on
284 * (intermediate) CAs the keyUsage extension is automatically
285 * checked by \c x509_crt_verify().
286 */
287int x509_crt_check_key_usage( const x509_crt *crt, int usage );
288#endif /* POLARSSL_X509_CHECK_KEY_USAGE) */
289
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200290#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
291/**
292 * \brief Check usage of certificate against extentedJeyUsage.
293 *
294 * \param crt Leaf certificate used.
295 * \param usage_oid Intended usage (eg OID_SERVER_AUTH or OID_CLIENT_AUTH).
296 * \param usage_len Length of usage_oid (eg given by OID_SIZE()).
297 *
298 * \return 0 is this use of the certificate is allowed,
299 * POLARSSL_ERR_X509_BAD_INPUT_DATA if not.
300 *
301 * \note Usually only makes sense on leaf certificates.
302 */
303int x509_crt_check_extended_key_usage( const x509_crt *crt,
304 const char *usage_oid,
305 size_t usage_len );
306#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE) */
307
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200308#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200309/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200310 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311 *
312 * \param crt a certificate to be verified
313 * \param crl the CRL to verify against
314 *
315 * \return 1 if the certificate is revoked, 0 otherwise
316 *
317 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200318int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200319#endif /* POLARSSL_X509_CRL_PARSE_C */
320
321/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200322 * \brief Initialize a certificate (chain)
323 *
324 * \param crt Certificate chain to initialize
325 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200326void x509_crt_init( x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200327
328/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329 * \brief Unallocate all certificate data
330 *
331 * \param crt Certificate chain to free
332 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200333void x509_crt_free( x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200334#endif /* POLARSSL_X509_CRT_PARSE_C */
335
336/* \} name */
337/* \} addtogroup x509_module */
338
339#if defined(POLARSSL_X509_CRT_WRITE_C)
340/**
341 * \brief Initialize a CRT writing context
342 *
343 * \param ctx CRT context to initialize
344 */
345void x509write_crt_init( x509write_cert *ctx );
346
347/**
348 * \brief Set the verion for a Certificate
349 * Default: X509_CRT_VERSION_3
350 *
351 * \param ctx CRT context to use
352 * \param version version to set (X509_CRT_VERSION_1, X509_CRT_VERSION_2 or
353 * X509_CRT_VERSION_3)
354 */
355void x509write_crt_set_version( x509write_cert *ctx, int version );
356
357/**
358 * \brief Set the serial number for a Certificate.
359 *
360 * \param ctx CRT context to use
361 * \param serial serial number to set
362 *
363 * \return 0 if successful
364 */
365int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial );
366
367/**
368 * \brief Set the validity period for a Certificate
369 * Timestamps should be in string format for UTC timezone
370 * i.e. "YYYYMMDDhhmmss"
371 * e.g. "20131231235959" for December 31st 2013
372 * at 23:59:59
373 *
374 * \param ctx CRT context to use
375 * \param not_before not_before timestamp
376 * \param not_after not_after timestamp
377 *
378 * \return 0 if timestamp was parsed successfully, or
379 * a specific error code
380 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100381int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before,
382 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383
384/**
385 * \brief Set the issuer name for a Certificate
386 * Issuer names should contain a comma-separated list
387 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000388 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200389 *
390 * \param ctx CRT context to use
391 * \param issuer_name issuer name to set
392 *
393 * \return 0 if issuer name was parsed successfully, or
394 * a specific error code
395 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100396int x509write_crt_set_issuer_name( x509write_cert *ctx,
397 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398
399/**
400 * \brief Set the subject name for a Certificate
401 * Subject names should contain a comma-separated list
402 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000403 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404 *
405 * \param ctx CRT context to use
406 * \param subject_name subject name to set
407 *
408 * \return 0 if subject name was parsed successfully, or
409 * a specific error code
410 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100411int x509write_crt_set_subject_name( x509write_cert *ctx,
412 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413
414/**
415 * \brief Set the subject public key for the certificate
416 *
417 * \param ctx CRT context to use
418 * \param key public key to include
419 */
420void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key );
421
422/**
423 * \brief Set the issuer key used for signing the certificate
424 *
425 * \param ctx CRT context to use
426 * \param key private key to sign with
427 */
428void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key );
429
430/**
431 * \brief Set the MD algorithm to use for the signature
432 * (e.g. POLARSSL_MD_SHA1)
433 *
434 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100435 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436 */
437void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg );
438
439/**
440 * \brief Generic function to add to or replace an extension in the
441 * CRT
442 *
443 * \param ctx CRT context to use
444 * \param oid OID of the extension
445 * \param oid_len length of the OID
446 * \param critical if the extension is critical (per the RFC's definition)
447 * \param val value of the extension OCTET STRING
448 * \param val_len length of the value data
449 *
450 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
451 */
452int x509write_crt_set_extension( x509write_cert *ctx,
453 const char *oid, size_t oid_len,
454 int critical,
455 const unsigned char *val, size_t val_len );
456
457/**
458 * \brief Set the basicConstraints extension for a CRT
459 *
460 * \param ctx CRT context to use
461 * \param is_ca is this a CA certificate
462 * \param max_pathlen maximum length of certificate chains below this
463 * certificate (only for CA certificates, -1 is
464 * inlimited)
465 *
466 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
467 */
468int x509write_crt_set_basic_constraints( x509write_cert *ctx,
469 int is_ca, int max_pathlen );
470
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100471#if defined(POLARSSL_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472/**
473 * \brief Set the subjectKeyIdentifier extension for a CRT
474 * Requires that x509write_crt_set_subject_key() has been
475 * called before
476 *
477 * \param ctx CRT context to use
478 *
479 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
480 */
481int x509write_crt_set_subject_key_identifier( x509write_cert *ctx );
482
483/**
484 * \brief Set the authorityKeyIdentifier extension for a CRT
485 * Requires that x509write_crt_set_issuer_key() has been
486 * called before
487 *
488 * \param ctx CRT context to use
489 *
490 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
491 */
492int x509write_crt_set_authority_key_identifier( x509write_cert *ctx );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100493#endif /* POLARSSL_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494
495/**
496 * \brief Set the Key Usage Extension flags
497 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
498 *
499 * \param ctx CRT context to use
500 * \param key_usage key usage flags to set
501 *
502 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
503 */
504int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage );
505
506/**
507 * \brief Set the Netscape Cert Type flags
508 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
509 *
510 * \param ctx CRT context to use
511 * \param ns_cert_type Netscape Cert Type flags to set
512 *
513 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
514 */
515int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
516 unsigned char ns_cert_type );
517
518/**
519 * \brief Free the contents of a CRT write context
520 *
521 * \param ctx CRT context to free
522 */
523void x509write_crt_free( x509write_cert *ctx );
524
525/**
526 * \brief Write a built up certificate to a X509 DER structure
527 * Note: data is written at the end of the buffer! Use the
528 * return value to determine where you should start
529 * using the buffer
530 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100531 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532 * \param buf buffer to write to
533 * \param size size of the buffer
534 * \param f_rng RNG function (for signature, see note)
535 * \param p_rng RNG parameter
536 *
537 * \return length of data written if successful, or a specific
538 * error code
539 *
540 * \note f_rng may be NULL if RSA is used for signature and the
541 * signature is made offline (otherwise f_rng is desirable
542 * for countermeasures against timing attacks).
543 * ECDSA signatures always require a non-NULL f_rng.
544 */
545int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
546 int (*f_rng)(void *, unsigned char *, size_t),
547 void *p_rng );
548
549#if defined(POLARSSL_PEM_WRITE_C)
550/**
551 * \brief Write a built up certificate to a X509 PEM string
552 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100553 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554 * \param buf buffer to write to
555 * \param size size of the buffer
556 * \param f_rng RNG function (for signature, see note)
557 * \param p_rng RNG parameter
558 *
559 * \return 0 successful, or a specific error code
560 *
561 * \note f_rng may be NULL if RSA is used for signature and the
562 * signature is made offline (otherwise f_rng is desirable
563 * for countermeasures against timing attacks).
564 * ECDSA signatures always require a non-NULL f_rng.
565 */
566int x509write_crt_pem( x509write_cert *ctx, unsigned char *buf, size_t size,
567 int (*f_rng)(void *, unsigned char *, size_t),
568 void *p_rng );
569#endif /* POLARSSL_PEM_WRITE_C */
570#endif /* POLARSSL_X509_CRT_WRITE_C */
571
572#ifdef __cplusplus
573}
574#endif
575
576#endif /* x509_crt.h */