blob: fe270078f9d3d6e3bd8eaa318e8b01989d1f037e [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/**
206 * \brief Verify the certificate signature
207 *
208 * The verify callback is a user-supplied callback that
209 * can clear / modify / add flags for a certificate. If set,
210 * the verification callback is called for each
211 * certificate in the chain (from the trust-ca down to the
212 * presented crt). The parameters for the callback are:
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200213 * (void *parameter, x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200214 * int *flags). With the flags representing current flags for
215 * that specific certificate and the certificate depth from
216 * the bottom (Peer cert depth = 0).
217 *
218 * All flags left after returning from the callback
219 * are also returned to the application. The function should
220 * return 0 for anything but a fatal error.
221 *
222 * \param crt a certificate to be verified
223 * \param trust_ca the trusted CA chain
224 * \param ca_crl the CRL chain for trusted CA's
225 * \param cn expected Common Name (can be set to
226 * NULL if the CN must not be verified)
227 * \param flags result of the verification
228 * \param f_vrfy verification function
229 * \param p_vrfy verification parameter
230 *
231 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
232 * in which case *flags will have one or more of
233 * the following values set:
234 * BADCERT_EXPIRED --
235 * BADCERT_REVOKED --
236 * BADCERT_CN_MISMATCH --
237 * BADCERT_NOT_TRUSTED
238 * or another error in case of a fatal error encountered
239 * during the verification process.
240 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200241int x509_crt_verify( x509_crt *crt,
242 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200243 x509_crl *ca_crl,
244 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200245 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200246 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200247
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200248#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
249/**
250 * \brief Check usage of certificate against keyUsage extension.
251 *
252 * \param crt Leaf certificate used.
253 * \param usage Intended usage(s) (eg KU_KEY_ENCIPHERMENT before using the
254 * certificate to perform an RSA key exchange).
255 *
256 * \return 0 is these uses of the certificate are allowed,
Paul Bakker02ff5ce2014-04-09 15:53:09 +0200257 * POLARSSL_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200258 * is present but does not contain all the bits set in the
259 * usage argument.
260 *
261 * \note You should only call this function on leaf certificates, on
262 * (intermediate) CAs the keyUsage extension is automatically
263 * checked by \c x509_crt_verify().
264 */
265int x509_crt_check_key_usage( const x509_crt *crt, int usage );
266#endif /* POLARSSL_X509_CHECK_KEY_USAGE) */
267
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200268#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
269/**
270 * \brief Check usage of certificate against extentedJeyUsage.
271 *
272 * \param crt Leaf certificate used.
273 * \param usage_oid Intended usage (eg OID_SERVER_AUTH or OID_CLIENT_AUTH).
274 * \param usage_len Length of usage_oid (eg given by OID_SIZE()).
275 *
276 * \return 0 is this use of the certificate is allowed,
277 * POLARSSL_ERR_X509_BAD_INPUT_DATA if not.
278 *
279 * \note Usually only makes sense on leaf certificates.
280 */
281int x509_crt_check_extended_key_usage( const x509_crt *crt,
282 const char *usage_oid,
283 size_t usage_len );
284#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE) */
285
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200286#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200288 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289 *
290 * \param crt a certificate to be verified
291 * \param crl the CRL to verify against
292 *
293 * \return 1 if the certificate is revoked, 0 otherwise
294 *
295 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200296int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200297#endif /* POLARSSL_X509_CRL_PARSE_C */
298
299/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200300 * \brief Initialize a certificate (chain)
301 *
302 * \param crt Certificate chain to initialize
303 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200304void x509_crt_init( x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200305
306/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307 * \brief Unallocate all certificate data
308 *
309 * \param crt Certificate chain to free
310 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200311void x509_crt_free( x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200312#endif /* POLARSSL_X509_CRT_PARSE_C */
313
314/* \} name */
315/* \} addtogroup x509_module */
316
317#if defined(POLARSSL_X509_CRT_WRITE_C)
318/**
319 * \brief Initialize a CRT writing context
320 *
321 * \param ctx CRT context to initialize
322 */
323void x509write_crt_init( x509write_cert *ctx );
324
325/**
326 * \brief Set the verion for a Certificate
327 * Default: X509_CRT_VERSION_3
328 *
329 * \param ctx CRT context to use
330 * \param version version to set (X509_CRT_VERSION_1, X509_CRT_VERSION_2 or
331 * X509_CRT_VERSION_3)
332 */
333void x509write_crt_set_version( x509write_cert *ctx, int version );
334
335/**
336 * \brief Set the serial number for a Certificate.
337 *
338 * \param ctx CRT context to use
339 * \param serial serial number to set
340 *
341 * \return 0 if successful
342 */
343int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial );
344
345/**
346 * \brief Set the validity period for a Certificate
347 * Timestamps should be in string format for UTC timezone
348 * i.e. "YYYYMMDDhhmmss"
349 * e.g. "20131231235959" for December 31st 2013
350 * at 23:59:59
351 *
352 * \param ctx CRT context to use
353 * \param not_before not_before timestamp
354 * \param not_after not_after timestamp
355 *
356 * \return 0 if timestamp was parsed successfully, or
357 * a specific error code
358 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100359int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before,
360 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361
362/**
363 * \brief Set the issuer name for a Certificate
364 * Issuer names should contain a comma-separated list
365 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000366 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367 *
368 * \param ctx CRT context to use
369 * \param issuer_name issuer name to set
370 *
371 * \return 0 if issuer name was parsed successfully, or
372 * a specific error code
373 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100374int x509write_crt_set_issuer_name( x509write_cert *ctx,
375 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376
377/**
378 * \brief Set the subject name for a Certificate
379 * Subject names should contain a comma-separated list
380 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000381 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382 *
383 * \param ctx CRT context to use
384 * \param subject_name subject name to set
385 *
386 * \return 0 if subject name was parsed successfully, or
387 * a specific error code
388 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100389int x509write_crt_set_subject_name( x509write_cert *ctx,
390 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391
392/**
393 * \brief Set the subject public key for the certificate
394 *
395 * \param ctx CRT context to use
396 * \param key public key to include
397 */
398void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key );
399
400/**
401 * \brief Set the issuer key used for signing the certificate
402 *
403 * \param ctx CRT context to use
404 * \param key private key to sign with
405 */
406void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key );
407
408/**
409 * \brief Set the MD algorithm to use for the signature
410 * (e.g. POLARSSL_MD_SHA1)
411 *
412 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100413 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414 */
415void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg );
416
417/**
418 * \brief Generic function to add to or replace an extension in the
419 * CRT
420 *
421 * \param ctx CRT context to use
422 * \param oid OID of the extension
423 * \param oid_len length of the OID
424 * \param critical if the extension is critical (per the RFC's definition)
425 * \param val value of the extension OCTET STRING
426 * \param val_len length of the value data
427 *
428 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
429 */
430int x509write_crt_set_extension( x509write_cert *ctx,
431 const char *oid, size_t oid_len,
432 int critical,
433 const unsigned char *val, size_t val_len );
434
435/**
436 * \brief Set the basicConstraints extension for a CRT
437 *
438 * \param ctx CRT context to use
439 * \param is_ca is this a CA certificate
440 * \param max_pathlen maximum length of certificate chains below this
441 * certificate (only for CA certificates, -1 is
442 * inlimited)
443 *
444 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
445 */
446int x509write_crt_set_basic_constraints( x509write_cert *ctx,
447 int is_ca, int max_pathlen );
448
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100449#if defined(POLARSSL_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450/**
451 * \brief Set the subjectKeyIdentifier extension for a CRT
452 * Requires that x509write_crt_set_subject_key() has been
453 * called before
454 *
455 * \param ctx CRT context to use
456 *
457 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
458 */
459int x509write_crt_set_subject_key_identifier( x509write_cert *ctx );
460
461/**
462 * \brief Set the authorityKeyIdentifier extension for a CRT
463 * Requires that x509write_crt_set_issuer_key() has been
464 * called before
465 *
466 * \param ctx CRT context to use
467 *
468 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
469 */
470int x509write_crt_set_authority_key_identifier( x509write_cert *ctx );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100471#endif /* POLARSSL_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
473/**
474 * \brief Set the Key Usage Extension flags
475 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
476 *
477 * \param ctx CRT context to use
478 * \param key_usage key usage flags to set
479 *
480 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
481 */
482int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage );
483
484/**
485 * \brief Set the Netscape Cert Type flags
486 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
487 *
488 * \param ctx CRT context to use
489 * \param ns_cert_type Netscape Cert Type flags to set
490 *
491 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
492 */
493int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
494 unsigned char ns_cert_type );
495
496/**
497 * \brief Free the contents of a CRT write context
498 *
499 * \param ctx CRT context to free
500 */
501void x509write_crt_free( x509write_cert *ctx );
502
503/**
504 * \brief Write a built up certificate to a X509 DER structure
505 * Note: data is written at the end of the buffer! Use the
506 * return value to determine where you should start
507 * using the buffer
508 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100509 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510 * \param buf buffer to write to
511 * \param size size of the buffer
512 * \param f_rng RNG function (for signature, see note)
513 * \param p_rng RNG parameter
514 *
515 * \return length of data written if successful, or a specific
516 * error code
517 *
518 * \note f_rng may be NULL if RSA is used for signature and the
519 * signature is made offline (otherwise f_rng is desirable
520 * for countermeasures against timing attacks).
521 * ECDSA signatures always require a non-NULL f_rng.
522 */
523int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
524 int (*f_rng)(void *, unsigned char *, size_t),
525 void *p_rng );
526
527#if defined(POLARSSL_PEM_WRITE_C)
528/**
529 * \brief Write a built up certificate to a X509 PEM string
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 0 successful, or a specific error code
538 *
539 * \note f_rng may be NULL if RSA is used for signature and the
540 * signature is made offline (otherwise f_rng is desirable
541 * for countermeasures against timing attacks).
542 * ECDSA signatures always require a non-NULL f_rng.
543 */
544int x509write_crt_pem( x509write_cert *ctx, unsigned char *buf, size_t size,
545 int (*f_rng)(void *, unsigned char *, size_t),
546 void *p_rng );
547#endif /* POLARSSL_PEM_WRITE_C */
548#endif /* POLARSSL_X509_CRT_WRITE_C */
549
550#ifdef __cplusplus
551}
552#endif
553
554#endif /* x509_crt.h */