blob: eef7f3afb4af8707215422c69c3f720cf8248316 [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 *
255 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
Manuel Pégourié-Gonnard39a183a2015-04-17 16:14:32 +0200256 * in which case *flags will have one or more BADCERT_XXX or
257 * BADCRL_XXX flags set,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200258 * or another error in case of a fatal error encountered
259 * during the verification process.
260 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200261int x509_crt_verify( x509_crt *crt,
262 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200263 x509_crl *ca_crl,
264 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200265 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200266 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200267
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200268#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
269/**
270 * \brief Check usage of certificate against keyUsage extension.
271 *
272 * \param crt Leaf certificate used.
273 * \param usage Intended usage(s) (eg KU_KEY_ENCIPHERMENT before using the
274 * certificate to perform an RSA key exchange).
275 *
276 * \return 0 is these uses of the certificate are allowed,
Paul Bakker02ff5ce2014-04-09 15:53:09 +0200277 * POLARSSL_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200278 * is present but does not contain all the bits set in the
279 * usage argument.
280 *
281 * \note You should only call this function on leaf certificates, on
282 * (intermediate) CAs the keyUsage extension is automatically
283 * checked by \c x509_crt_verify().
284 */
285int x509_crt_check_key_usage( const x509_crt *crt, int usage );
286#endif /* POLARSSL_X509_CHECK_KEY_USAGE) */
287
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200288#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
289/**
290 * \brief Check usage of certificate against extentedJeyUsage.
291 *
292 * \param crt Leaf certificate used.
293 * \param usage_oid Intended usage (eg OID_SERVER_AUTH or OID_CLIENT_AUTH).
294 * \param usage_len Length of usage_oid (eg given by OID_SIZE()).
295 *
296 * \return 0 is this use of the certificate is allowed,
297 * POLARSSL_ERR_X509_BAD_INPUT_DATA if not.
298 *
299 * \note Usually only makes sense on leaf certificates.
300 */
301int x509_crt_check_extended_key_usage( const x509_crt *crt,
302 const char *usage_oid,
303 size_t usage_len );
304#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE) */
305
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200306#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200308 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200309 *
310 * \param crt a certificate to be verified
311 * \param crl the CRL to verify against
312 *
313 * \return 1 if the certificate is revoked, 0 otherwise
314 *
315 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200316int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200317#endif /* POLARSSL_X509_CRL_PARSE_C */
318
319/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200320 * \brief Initialize a certificate (chain)
321 *
322 * \param crt Certificate chain to initialize
323 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200324void x509_crt_init( x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200325
326/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327 * \brief Unallocate all certificate data
328 *
329 * \param crt Certificate chain to free
330 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200331void x509_crt_free( x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200332#endif /* POLARSSL_X509_CRT_PARSE_C */
333
334/* \} name */
335/* \} addtogroup x509_module */
336
337#if defined(POLARSSL_X509_CRT_WRITE_C)
338/**
339 * \brief Initialize a CRT writing context
340 *
341 * \param ctx CRT context to initialize
342 */
343void x509write_crt_init( x509write_cert *ctx );
344
345/**
346 * \brief Set the verion for a Certificate
347 * Default: X509_CRT_VERSION_3
348 *
349 * \param ctx CRT context to use
350 * \param version version to set (X509_CRT_VERSION_1, X509_CRT_VERSION_2 or
351 * X509_CRT_VERSION_3)
352 */
353void x509write_crt_set_version( x509write_cert *ctx, int version );
354
355/**
356 * \brief Set the serial number for a Certificate.
357 *
358 * \param ctx CRT context to use
359 * \param serial serial number to set
360 *
361 * \return 0 if successful
362 */
363int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial );
364
365/**
366 * \brief Set the validity period for a Certificate
367 * Timestamps should be in string format for UTC timezone
368 * i.e. "YYYYMMDDhhmmss"
369 * e.g. "20131231235959" for December 31st 2013
370 * at 23:59:59
371 *
372 * \param ctx CRT context to use
373 * \param not_before not_before timestamp
374 * \param not_after not_after timestamp
375 *
376 * \return 0 if timestamp was parsed successfully, or
377 * a specific error code
378 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100379int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before,
380 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381
382/**
383 * \brief Set the issuer name for a Certificate
384 * Issuer names should contain a comma-separated list
385 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000386 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387 *
388 * \param ctx CRT context to use
389 * \param issuer_name issuer name to set
390 *
391 * \return 0 if issuer name was parsed successfully, or
392 * a specific error code
393 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100394int x509write_crt_set_issuer_name( x509write_cert *ctx,
395 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396
397/**
398 * \brief Set the subject name for a Certificate
399 * Subject names should contain a comma-separated list
400 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000401 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402 *
403 * \param ctx CRT context to use
404 * \param subject_name subject name to set
405 *
406 * \return 0 if subject name was parsed successfully, or
407 * a specific error code
408 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100409int x509write_crt_set_subject_name( x509write_cert *ctx,
410 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411
412/**
413 * \brief Set the subject public key for the certificate
414 *
415 * \param ctx CRT context to use
416 * \param key public key to include
417 */
418void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key );
419
420/**
421 * \brief Set the issuer key used for signing the certificate
422 *
423 * \param ctx CRT context to use
424 * \param key private key to sign with
425 */
426void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key );
427
428/**
429 * \brief Set the MD algorithm to use for the signature
430 * (e.g. POLARSSL_MD_SHA1)
431 *
432 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100433 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434 */
435void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg );
436
437/**
438 * \brief Generic function to add to or replace an extension in the
439 * CRT
440 *
441 * \param ctx CRT context to use
442 * \param oid OID of the extension
443 * \param oid_len length of the OID
444 * \param critical if the extension is critical (per the RFC's definition)
445 * \param val value of the extension OCTET STRING
446 * \param val_len length of the value data
447 *
448 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
449 */
450int x509write_crt_set_extension( x509write_cert *ctx,
451 const char *oid, size_t oid_len,
452 int critical,
453 const unsigned char *val, size_t val_len );
454
455/**
456 * \brief Set the basicConstraints extension for a CRT
457 *
458 * \param ctx CRT context to use
459 * \param is_ca is this a CA certificate
460 * \param max_pathlen maximum length of certificate chains below this
461 * certificate (only for CA certificates, -1 is
462 * inlimited)
463 *
464 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
465 */
466int x509write_crt_set_basic_constraints( x509write_cert *ctx,
467 int is_ca, int max_pathlen );
468
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100469#if defined(POLARSSL_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470/**
471 * \brief Set the subjectKeyIdentifier extension for a CRT
472 * Requires that x509write_crt_set_subject_key() has been
473 * called before
474 *
475 * \param ctx CRT context to use
476 *
477 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
478 */
479int x509write_crt_set_subject_key_identifier( x509write_cert *ctx );
480
481/**
482 * \brief Set the authorityKeyIdentifier extension for a CRT
483 * Requires that x509write_crt_set_issuer_key() has been
484 * called before
485 *
486 * \param ctx CRT context to use
487 *
488 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
489 */
490int x509write_crt_set_authority_key_identifier( x509write_cert *ctx );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100491#endif /* POLARSSL_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492
493/**
494 * \brief Set the Key Usage Extension flags
495 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
496 *
497 * \param ctx CRT context to use
498 * \param key_usage key usage flags to set
499 *
500 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
501 */
502int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage );
503
504/**
505 * \brief Set the Netscape Cert Type flags
506 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
507 *
508 * \param ctx CRT context to use
509 * \param ns_cert_type Netscape Cert Type flags to set
510 *
511 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
512 */
513int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
514 unsigned char ns_cert_type );
515
516/**
517 * \brief Free the contents of a CRT write context
518 *
519 * \param ctx CRT context to free
520 */
521void x509write_crt_free( x509write_cert *ctx );
522
523/**
524 * \brief Write a built up certificate to a X509 DER structure
525 * Note: data is written at the end of the buffer! Use the
526 * return value to determine where you should start
527 * using the buffer
528 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100529 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530 * \param buf buffer to write to
531 * \param size size of the buffer
532 * \param f_rng RNG function (for signature, see note)
533 * \param p_rng RNG parameter
534 *
535 * \return length of data written if successful, or a specific
536 * error code
537 *
538 * \note f_rng may be NULL if RSA is used for signature and the
539 * signature is made offline (otherwise f_rng is desirable
540 * for countermeasures against timing attacks).
541 * ECDSA signatures always require a non-NULL f_rng.
542 */
543int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
544 int (*f_rng)(void *, unsigned char *, size_t),
545 void *p_rng );
546
547#if defined(POLARSSL_PEM_WRITE_C)
548/**
549 * \brief Write a built up certificate to a X509 PEM string
550 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100551 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552 * \param buf buffer to write to
553 * \param size size of the buffer
554 * \param f_rng RNG function (for signature, see note)
555 * \param p_rng RNG parameter
556 *
557 * \return 0 successful, or a specific error code
558 *
559 * \note f_rng may be NULL if RSA is used for signature and the
560 * signature is made offline (otherwise f_rng is desirable
561 * for countermeasures against timing attacks).
562 * ECDSA signatures always require a non-NULL f_rng.
563 */
564int x509write_crt_pem( x509write_cert *ctx, unsigned char *buf, size_t size,
565 int (*f_rng)(void *, unsigned char *, size_t),
566 void *p_rng );
567#endif /* POLARSSL_PEM_WRITE_C */
568#endif /* POLARSSL_X509_CRT_WRITE_C */
569
570#ifdef __cplusplus
571}
572#endif
573
574#endif /* x509_crt.h */