blob: 7816801fe4550c8b3ccc5f7c59529e3ffa9f76a2 [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. */
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +010060 x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061
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é-Gonnard1d0ca1a2015-03-27 16:50:00 +010082 unsigned int 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
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
89 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 +020090 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 +020091 void *sig_opts; /**< Signature options to be passed to pk_verify_ext(), e.g. for RSASSA-PSS */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020092
Paul Bakkerc559c7a2013-09-18 14:13:26 +020093 struct _x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094}
Paul Bakkerc559c7a2013-09-18 14:13:26 +020095x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096
97#define X509_CRT_VERSION_1 0
98#define X509_CRT_VERSION_2 1
99#define X509_CRT_VERSION_3 2
100
101#define X509_RFC5280_MAX_SERIAL_LEN 32
102#define X509_RFC5280_UTC_TIME_LEN 15
103
104/**
105 * Container for writing a certificate (CRT)
106 */
107typedef struct _x509write_cert
108{
109 int version;
110 mpi serial;
111 pk_context *subject_key;
112 pk_context *issuer_key;
113 asn1_named_data *subject;
114 asn1_named_data *issuer;
115 md_type_t md_alg;
116 char not_before[X509_RFC5280_UTC_TIME_LEN + 1];
117 char not_after[X509_RFC5280_UTC_TIME_LEN + 1];
118 asn1_named_data *extensions;
119}
120x509write_cert;
121
122#if defined(POLARSSL_X509_CRT_PARSE_C)
123/**
124 * \brief Parse a single DER formatted certificate and add it
125 * to the chained list.
126 *
127 * \param chain points to the start of the chain
128 * \param buf buffer holding the certificate DER data
129 * \param buflen size of the buffer
130 *
131 * \return 0 if successful, or a specific X509 or PEM error code
132 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200133int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200134 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135
136/**
137 * \brief Parse one or more certificates and add them
138 * to the chained list. Parses permissively. If some
139 * certificates can be parsed, the result is the number
140 * of failed certificates it encountered. If none complete
141 * correctly, the first error is returned.
142 *
143 * \param chain points to the start of the chain
144 * \param buf buffer holding the certificate data
145 * \param buflen size of the buffer
146 *
147 * \return 0 if all certificates parsed successfully, a positive number
148 * if partly successful or a specific X509 or PEM error code
149 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200150int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200151
152#if defined(POLARSSL_FS_IO)
153/**
154 * \brief Load one or more certificates and add them
155 * to the chained list. Parses permissively. If some
156 * certificates can be parsed, the result is the number
157 * of failed certificates it encountered. If none complete
158 * correctly, the first error is returned.
159 *
160 * \param chain points to the start of the chain
161 * \param path filename to read the certificates from
162 *
163 * \return 0 if all certificates parsed successfully, a positive number
164 * if partly successful or a specific X509 or PEM error code
165 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200166int x509_crt_parse_file( x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200167
168/**
169 * \brief Load one or more certificate files from a path and add them
170 * to the chained list. Parses permissively. If some
171 * certificates can be parsed, the result is the number
172 * of failed certificates it encountered. If none complete
173 * correctly, the first error is returned.
174 *
Manuel Pégourié-Gonnarde3339ce2013-11-28 17:16:41 +0100175 * \warning This function is NOT thread-safe unless
Manuel Pégourié-Gonnard9b669902015-03-06 16:52:46 +0000176 * POLARSSL_THREADING_PTHREAD is defined. If you're using an
Manuel Pégourié-Gonnarde3339ce2013-11-28 17:16:41 +0100177 * alternative threading implementation, you should either use
178 * this function only in the main thread, or mutex it.
179 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200180 * \param chain points to the start of the chain
181 * \param path directory / folder to read the certificate files from
182 *
183 * \return 0 if all certificates parsed successfully, a positive number
184 * if partly successful or a specific X509 or PEM error code
185 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200186int x509_crt_parse_path( x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200187#endif /* POLARSSL_FS_IO */
188
189/**
190 * \brief Returns an informational string about the
191 * certificate.
192 *
193 * \param buf Buffer to write to
194 * \param size Maximum size of buffer
195 * \param prefix A line prefix
196 * \param crt The X509 certificate to represent
197 *
198 * \return The amount of data written to the buffer, or -1 in
199 * case of an error.
200 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200201int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200202 const x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200203
204/**
205 * \brief Verify the certificate signature
206 *
207 * The verify callback is a user-supplied callback that
208 * can clear / modify / add flags for a certificate. If set,
209 * the verification callback is called for each
210 * certificate in the chain (from the trust-ca down to the
211 * presented crt). The parameters for the callback are:
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200212 * (void *parameter, x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200213 * int *flags). With the flags representing current flags for
214 * that specific certificate and the certificate depth from
215 * the bottom (Peer cert depth = 0).
216 *
217 * All flags left after returning from the callback
218 * are also returned to the application. The function should
219 * return 0 for anything but a fatal error.
220 *
221 * \param crt a certificate to be verified
222 * \param trust_ca the trusted CA chain
223 * \param ca_crl the CRL chain for trusted CA's
224 * \param cn expected Common Name (can be set to
225 * NULL if the CN must not be verified)
226 * \param flags result of the verification
227 * \param f_vrfy verification function
228 * \param p_vrfy verification parameter
229 *
230 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
231 * in which case *flags will have one or more of
232 * the following values set:
233 * BADCERT_EXPIRED --
234 * BADCERT_REVOKED --
235 * BADCERT_CN_MISMATCH --
236 * BADCERT_NOT_TRUSTED
237 * or another error in case of a fatal error encountered
238 * during the verification process.
239 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200240int x509_crt_verify( x509_crt *crt,
241 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200242 x509_crl *ca_crl,
243 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200244 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200245 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200246
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200247#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
248/**
249 * \brief Check usage of certificate against keyUsage extension.
250 *
251 * \param crt Leaf certificate used.
252 * \param usage Intended usage(s) (eg KU_KEY_ENCIPHERMENT before using the
253 * certificate to perform an RSA key exchange).
254 *
255 * \return 0 is these uses of the certificate are allowed,
Paul Bakker02ff5ce2014-04-09 15:53:09 +0200256 * POLARSSL_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200257 * is present but does not contain all the bits set in the
258 * usage argument.
259 *
260 * \note You should only call this function on leaf certificates, on
261 * (intermediate) CAs the keyUsage extension is automatically
262 * checked by \c x509_crt_verify().
263 */
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100264int x509_crt_check_key_usage( const x509_crt *crt, unsigned int usage );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200265#endif /* POLARSSL_X509_CHECK_KEY_USAGE) */
266
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200267#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
268/**
269 * \brief Check usage of certificate against extentedJeyUsage.
270 *
271 * \param crt Leaf certificate used.
272 * \param usage_oid Intended usage (eg OID_SERVER_AUTH or OID_CLIENT_AUTH).
273 * \param usage_len Length of usage_oid (eg given by OID_SIZE()).
274 *
275 * \return 0 is this use of the certificate is allowed,
276 * POLARSSL_ERR_X509_BAD_INPUT_DATA if not.
277 *
278 * \note Usually only makes sense on leaf certificates.
279 */
280int x509_crt_check_extended_key_usage( const x509_crt *crt,
281 const char *usage_oid,
282 size_t usage_len );
283#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE) */
284
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200285#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200287 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 *
289 * \param crt a certificate to be verified
290 * \param crl the CRL to verify against
291 *
292 * \return 1 if the certificate is revoked, 0 otherwise
293 *
294 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200295int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296#endif /* POLARSSL_X509_CRL_PARSE_C */
297
298/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200299 * \brief Initialize a certificate (chain)
300 *
301 * \param crt Certificate chain to initialize
302 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200303void x509_crt_init( x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200304
305/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200306 * \brief Unallocate all certificate data
307 *
308 * \param crt Certificate chain to free
309 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200310void x509_crt_free( x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311#endif /* POLARSSL_X509_CRT_PARSE_C */
312
313/* \} name */
314/* \} addtogroup x509_module */
315
316#if defined(POLARSSL_X509_CRT_WRITE_C)
317/**
318 * \brief Initialize a CRT writing context
319 *
320 * \param ctx CRT context to initialize
321 */
322void x509write_crt_init( x509write_cert *ctx );
323
324/**
325 * \brief Set the verion for a Certificate
326 * Default: X509_CRT_VERSION_3
327 *
328 * \param ctx CRT context to use
329 * \param version version to set (X509_CRT_VERSION_1, X509_CRT_VERSION_2 or
330 * X509_CRT_VERSION_3)
331 */
332void x509write_crt_set_version( x509write_cert *ctx, int version );
333
334/**
335 * \brief Set the serial number for a Certificate.
336 *
337 * \param ctx CRT context to use
338 * \param serial serial number to set
339 *
340 * \return 0 if successful
341 */
342int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial );
343
344/**
345 * \brief Set the validity period for a Certificate
346 * Timestamps should be in string format for UTC timezone
347 * i.e. "YYYYMMDDhhmmss"
348 * e.g. "20131231235959" for December 31st 2013
349 * at 23:59:59
350 *
351 * \param ctx CRT context to use
352 * \param not_before not_before timestamp
353 * \param not_after not_after timestamp
354 *
355 * \return 0 if timestamp was parsed successfully, or
356 * a specific error code
357 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100358int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before,
359 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360
361/**
362 * \brief Set the issuer name for a Certificate
363 * Issuer names should contain a comma-separated list
364 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000365 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366 *
367 * \param ctx CRT context to use
368 * \param issuer_name issuer name to set
369 *
370 * \return 0 if issuer name was parsed successfully, or
371 * a specific error code
372 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100373int x509write_crt_set_issuer_name( x509write_cert *ctx,
374 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375
376/**
377 * \brief Set the subject name for a Certificate
378 * Subject names should contain a comma-separated list
379 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000380 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381 *
382 * \param ctx CRT context to use
383 * \param subject_name subject name to set
384 *
385 * \return 0 if subject name was parsed successfully, or
386 * a specific error code
387 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100388int x509write_crt_set_subject_name( x509write_cert *ctx,
389 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390
391/**
392 * \brief Set the subject public key for the certificate
393 *
394 * \param ctx CRT context to use
395 * \param key public key to include
396 */
397void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key );
398
399/**
400 * \brief Set the issuer key used for signing the certificate
401 *
402 * \param ctx CRT context to use
403 * \param key private key to sign with
404 */
405void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key );
406
407/**
408 * \brief Set the MD algorithm to use for the signature
409 * (e.g. POLARSSL_MD_SHA1)
410 *
411 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100412 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413 */
414void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg );
415
416/**
417 * \brief Generic function to add to or replace an extension in the
418 * CRT
419 *
420 * \param ctx CRT context to use
421 * \param oid OID of the extension
422 * \param oid_len length of the OID
423 * \param critical if the extension is critical (per the RFC's definition)
424 * \param val value of the extension OCTET STRING
425 * \param val_len length of the value data
426 *
427 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
428 */
429int x509write_crt_set_extension( x509write_cert *ctx,
430 const char *oid, size_t oid_len,
431 int critical,
432 const unsigned char *val, size_t val_len );
433
434/**
435 * \brief Set the basicConstraints extension for a CRT
436 *
437 * \param ctx CRT context to use
438 * \param is_ca is this a CA certificate
439 * \param max_pathlen maximum length of certificate chains below this
440 * certificate (only for CA certificates, -1 is
441 * inlimited)
442 *
443 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
444 */
445int x509write_crt_set_basic_constraints( x509write_cert *ctx,
446 int is_ca, int max_pathlen );
447
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100448#if defined(POLARSSL_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449/**
450 * \brief Set the subjectKeyIdentifier extension for a CRT
451 * Requires that x509write_crt_set_subject_key() has been
452 * called before
453 *
454 * \param ctx CRT context to use
455 *
456 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
457 */
458int x509write_crt_set_subject_key_identifier( x509write_cert *ctx );
459
460/**
461 * \brief Set the authorityKeyIdentifier extension for a CRT
462 * Requires that x509write_crt_set_issuer_key() has been
463 * called before
464 *
465 * \param ctx CRT context to use
466 *
467 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
468 */
469int x509write_crt_set_authority_key_identifier( x509write_cert *ctx );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100470#endif /* POLARSSL_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471
472/**
473 * \brief Set the Key Usage Extension flags
474 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
475 *
476 * \param ctx CRT context to use
477 * \param key_usage key usage flags to set
478 *
479 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
480 */
481int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage );
482
483/**
484 * \brief Set the Netscape Cert Type flags
485 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
486 *
487 * \param ctx CRT context to use
488 * \param ns_cert_type Netscape Cert Type flags to set
489 *
490 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
491 */
492int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
493 unsigned char ns_cert_type );
494
495/**
496 * \brief Free the contents of a CRT write context
497 *
498 * \param ctx CRT context to free
499 */
500void x509write_crt_free( x509write_cert *ctx );
501
502/**
503 * \brief Write a built up certificate to a X509 DER structure
504 * Note: data is written at the end of the buffer! Use the
505 * return value to determine where you should start
506 * using the buffer
507 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100508 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509 * \param buf buffer to write to
510 * \param size size of the buffer
511 * \param f_rng RNG function (for signature, see note)
512 * \param p_rng RNG parameter
513 *
514 * \return length of data written if successful, or a specific
515 * error code
516 *
517 * \note f_rng may be NULL if RSA is used for signature and the
518 * signature is made offline (otherwise f_rng is desirable
519 * for countermeasures against timing attacks).
520 * ECDSA signatures always require a non-NULL f_rng.
521 */
522int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
523 int (*f_rng)(void *, unsigned char *, size_t),
524 void *p_rng );
525
526#if defined(POLARSSL_PEM_WRITE_C)
527/**
528 * \brief Write a built up certificate to a X509 PEM string
529 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100530 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531 * \param buf buffer to write to
532 * \param size size of the buffer
533 * \param f_rng RNG function (for signature, see note)
534 * \param p_rng RNG parameter
535 *
536 * \return 0 successful, or a specific 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_pem( x509write_cert *ctx, unsigned char *buf, size_t size,
544 int (*f_rng)(void *, unsigned char *, size_t),
545 void *p_rng );
546#endif /* POLARSSL_PEM_WRITE_C */
547#endif /* POLARSSL_X509_CRT_WRITE_C */
548
549#ifdef __cplusplus
550}
551#endif
552
553#endif /* x509_crt.h */