blob: 4fad932bf52e8c8d63b8c912cb62beb4da7acf19 [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é-Gonnard860b5162015-01-28 17:12:07 +00008 * This file is part of mbed TLS (https://polarssl.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"
34
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035#include "x509_crl.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036
37/**
38 * \addtogroup x509_module
39 * \{
40 */
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46/**
47 * \name Structures and functions for parsing and writing X.509 certificates
48 * \{
49 */
50
51/**
52 * Container for an X.509 certificate. The certificate may be chained.
53 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +020054typedef struct _x509_crt
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055{
56 x509_buf raw; /**< The raw certificate data (DER). */
57 x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
58
Manuel Pégourié-Gonnardf4e1b642014-06-19 11:39:46 +020059 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060 x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
61 x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
62
63 x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
64 x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
65
66 x509_name issuer; /**< The parsed issuer data (named information object). */
67 x509_name subject; /**< The parsed subject data (named information object). */
68
69 x509_time valid_from; /**< Start time of certificate validity. */
70 x509_time valid_to; /**< End time of certificate validity. */
71
72 pk_context pk; /**< Container for the public key context. */
73
74 x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
75 x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020076 x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077 x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
78
79 int ext_types; /**< Bit string containing detected and parsed extensions */
80 int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
81 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+ */
82
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020083 unsigned char key_usage; /**< Optional key usage extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
85 x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
86
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +020087 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088
89 x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
90 x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
91 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 +020092 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 +020093 void *sig_opts; /**< Signature options to be passed to pk_verify_ext(), e.g. for RSASSA-PSS */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094
Paul Bakkerc559c7a2013-09-18 14:13:26 +020095 struct _x509_crt *next; /**< Next certificate in the CA-chain. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096}
Paul Bakkerc559c7a2013-09-18 14:13:26 +020097x509_crt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098
99#define X509_CRT_VERSION_1 0
100#define X509_CRT_VERSION_2 1
101#define X509_CRT_VERSION_3 2
102
103#define X509_RFC5280_MAX_SERIAL_LEN 32
104#define X509_RFC5280_UTC_TIME_LEN 15
105
106/**
107 * Container for writing a certificate (CRT)
108 */
109typedef struct _x509write_cert
110{
111 int version;
112 mpi serial;
113 pk_context *subject_key;
114 pk_context *issuer_key;
115 asn1_named_data *subject;
116 asn1_named_data *issuer;
117 md_type_t md_alg;
118 char not_before[X509_RFC5280_UTC_TIME_LEN + 1];
119 char not_after[X509_RFC5280_UTC_TIME_LEN + 1];
120 asn1_named_data *extensions;
121}
122x509write_cert;
123
124#if defined(POLARSSL_X509_CRT_PARSE_C)
125/**
126 * \brief Parse a single DER formatted certificate and add it
127 * to the chained list.
128 *
129 * \param chain points to the start of the chain
130 * \param buf buffer holding the certificate DER data
131 * \param buflen size of the buffer
132 *
133 * \return 0 if successful, or a specific X509 or PEM error code
134 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200135int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200136 size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200137
138/**
139 * \brief Parse one or more certificates and add them
140 * to the chained list. Parses permissively. If some
141 * certificates can be parsed, the result is the number
142 * of failed certificates it encountered. If none complete
143 * correctly, the first error is returned.
144 *
145 * \param chain points to the start of the chain
146 * \param buf buffer holding the certificate data
147 * \param buflen size of the buffer
148 *
149 * \return 0 if all certificates parsed successfully, a positive number
150 * if partly successful or a specific X509 or PEM error code
151 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200152int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153
154#if defined(POLARSSL_FS_IO)
155/**
156 * \brief Load one or more certificates and add them
157 * to the chained list. Parses permissively. If some
158 * certificates can be parsed, the result is the number
159 * of failed certificates it encountered. If none complete
160 * correctly, the first error is returned.
161 *
162 * \param chain points to the start of the chain
163 * \param path filename to read the certificates from
164 *
165 * \return 0 if all certificates parsed successfully, a positive number
166 * if partly successful or a specific X509 or PEM error code
167 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200168int x509_crt_parse_file( x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200169
170/**
171 * \brief Load one or more certificate files from a path and add them
172 * to the chained list. Parses permissively. If some
173 * certificates can be parsed, the result is the number
174 * of failed certificates it encountered. If none complete
175 * correctly, the first error is returned.
176 *
Manuel Pégourié-Gonnarde3339ce2013-11-28 17:16:41 +0100177 * \warning This function is NOT thread-safe unless
178 * POLARSSL_THREADING_PTHREADS is defined. If you're using an
179 * alternative threading implementation, you should either use
180 * this function only in the main thread, or mutex it.
181 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200182 * \param chain points to the start of the chain
183 * \param path directory / folder to read the certificate files from
184 *
185 * \return 0 if all certificates parsed successfully, a positive number
186 * if partly successful or a specific X509 or PEM error code
187 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200188int x509_crt_parse_path( x509_crt *chain, const char *path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200189#endif /* POLARSSL_FS_IO */
190
191/**
192 * \brief Returns an informational string about the
193 * certificate.
194 *
195 * \param buf Buffer to write to
196 * \param size Maximum size of buffer
197 * \param prefix A line prefix
198 * \param crt The X509 certificate to represent
199 *
200 * \return The amount of data written to the buffer, or -1 in
201 * case of an error.
202 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200203int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200204 const x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200205
206/**
207 * \brief Verify the certificate signature
208 *
209 * The verify callback is a user-supplied callback that
210 * can clear / modify / add flags for a certificate. If set,
211 * the verification callback is called for each
212 * certificate in the chain (from the trust-ca down to the
213 * presented crt). The parameters for the callback are:
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200214 * (void *parameter, x509_crt *crt, int certificate_depth,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200215 * int *flags). With the flags representing current flags for
216 * that specific certificate and the certificate depth from
217 * the bottom (Peer cert depth = 0).
218 *
219 * All flags left after returning from the callback
220 * are also returned to the application. The function should
221 * return 0 for anything but a fatal error.
222 *
223 * \param crt a certificate to be verified
224 * \param trust_ca the trusted CA chain
225 * \param ca_crl the CRL chain for trusted CA's
226 * \param cn expected Common Name (can be set to
227 * NULL if the CN must not be verified)
228 * \param flags result of the verification
229 * \param f_vrfy verification function
230 * \param p_vrfy verification parameter
231 *
232 * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
233 * in which case *flags will have one or more of
234 * the following values set:
235 * BADCERT_EXPIRED --
236 * BADCERT_REVOKED --
237 * BADCERT_CN_MISMATCH --
238 * BADCERT_NOT_TRUSTED
239 * or another error in case of a fatal error encountered
240 * during the verification process.
241 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200242int x509_crt_verify( x509_crt *crt,
243 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200244 x509_crl *ca_crl,
245 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200246 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +0200247 void *p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200248
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200249#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
250/**
251 * \brief Check usage of certificate against keyUsage extension.
252 *
253 * \param crt Leaf certificate used.
254 * \param usage Intended usage(s) (eg KU_KEY_ENCIPHERMENT before using the
255 * certificate to perform an RSA key exchange).
256 *
257 * \return 0 is these uses of the certificate are allowed,
Paul Bakker02ff5ce2014-04-09 15:53:09 +0200258 * POLARSSL_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +0200259 * is present but does not contain all the bits set in the
260 * usage argument.
261 *
262 * \note You should only call this function on leaf certificates, on
263 * (intermediate) CAs the keyUsage extension is automatically
264 * checked by \c x509_crt_verify().
265 */
266int x509_crt_check_key_usage( const x509_crt *crt, int usage );
267#endif /* POLARSSL_X509_CHECK_KEY_USAGE) */
268
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +0200269#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
270/**
271 * \brief Check usage of certificate against extentedJeyUsage.
272 *
273 * \param crt Leaf certificate used.
274 * \param usage_oid Intended usage (eg OID_SERVER_AUTH or OID_CLIENT_AUTH).
275 * \param usage_len Length of usage_oid (eg given by OID_SIZE()).
276 *
277 * \return 0 is this use of the certificate is allowed,
278 * POLARSSL_ERR_X509_BAD_INPUT_DATA if not.
279 *
280 * \note Usually only makes sense on leaf certificates.
281 */
282int x509_crt_check_extended_key_usage( const x509_crt *crt,
283 const char *usage_oid,
284 size_t usage_len );
285#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE) */
286
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200287#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288/**
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +0200289 * \brief Verify the certificate revocation status
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290 *
291 * \param crt a certificate to be verified
292 * \param crl the CRL to verify against
293 *
294 * \return 1 if the certificate is revoked, 0 otherwise
295 *
296 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200297int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200298#endif /* POLARSSL_X509_CRL_PARSE_C */
299
300/**
Paul Bakker369d2eb2013-09-18 11:58:25 +0200301 * \brief Initialize a certificate (chain)
302 *
303 * \param crt Certificate chain to initialize
304 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200305void x509_crt_init( x509_crt *crt );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200306
307/**
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200308 * \brief Unallocate all certificate data
309 *
310 * \param crt Certificate chain to free
311 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200312void x509_crt_free( x509_crt *crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200313#endif /* POLARSSL_X509_CRT_PARSE_C */
314
315/* \} name */
316/* \} addtogroup x509_module */
317
318#if defined(POLARSSL_X509_CRT_WRITE_C)
319/**
320 * \brief Initialize a CRT writing context
321 *
322 * \param ctx CRT context to initialize
323 */
324void x509write_crt_init( x509write_cert *ctx );
325
326/**
327 * \brief Set the verion for a Certificate
328 * Default: X509_CRT_VERSION_3
329 *
330 * \param ctx CRT context to use
331 * \param version version to set (X509_CRT_VERSION_1, X509_CRT_VERSION_2 or
332 * X509_CRT_VERSION_3)
333 */
334void x509write_crt_set_version( x509write_cert *ctx, int version );
335
336/**
337 * \brief Set the serial number for a Certificate.
338 *
339 * \param ctx CRT context to use
340 * \param serial serial number to set
341 *
342 * \return 0 if successful
343 */
344int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial );
345
346/**
347 * \brief Set the validity period for a Certificate
348 * Timestamps should be in string format for UTC timezone
349 * i.e. "YYYYMMDDhhmmss"
350 * e.g. "20131231235959" for December 31st 2013
351 * at 23:59:59
352 *
353 * \param ctx CRT context to use
354 * \param not_before not_before timestamp
355 * \param not_after not_after timestamp
356 *
357 * \return 0 if timestamp was parsed successfully, or
358 * a specific error code
359 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100360int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before,
361 const char *not_after );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362
363/**
364 * \brief Set the issuer name for a Certificate
365 * Issuer names should contain a comma-separated list
366 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000367 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368 *
369 * \param ctx CRT context to use
370 * \param issuer_name issuer name to set
371 *
372 * \return 0 if issuer name was parsed successfully, or
373 * a specific error code
374 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100375int x509write_crt_set_issuer_name( x509write_cert *ctx,
376 const char *issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377
378/**
379 * \brief Set the subject name for a Certificate
380 * Subject names should contain a comma-separated list
381 * of OID types and values:
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000382 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383 *
384 * \param ctx CRT context to use
385 * \param subject_name subject name to set
386 *
387 * \return 0 if subject name was parsed successfully, or
388 * a specific error code
389 */
Paul Bakker50dc8502013-10-28 21:19:10 +0100390int x509write_crt_set_subject_name( x509write_cert *ctx,
391 const char *subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392
393/**
394 * \brief Set the subject public key for the certificate
395 *
396 * \param ctx CRT context to use
397 * \param key public key to include
398 */
399void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key );
400
401/**
402 * \brief Set the issuer key used for signing the certificate
403 *
404 * \param ctx CRT context to use
405 * \param key private key to sign with
406 */
407void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key );
408
409/**
410 * \brief Set the MD algorithm to use for the signature
411 * (e.g. POLARSSL_MD_SHA1)
412 *
413 * \param ctx CRT context to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100414 * \param md_alg MD algorithm to use
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415 */
416void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg );
417
418/**
419 * \brief Generic function to add to or replace an extension in the
420 * CRT
421 *
422 * \param ctx CRT context to use
423 * \param oid OID of the extension
424 * \param oid_len length of the OID
425 * \param critical if the extension is critical (per the RFC's definition)
426 * \param val value of the extension OCTET STRING
427 * \param val_len length of the value data
428 *
429 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
430 */
431int x509write_crt_set_extension( x509write_cert *ctx,
432 const char *oid, size_t oid_len,
433 int critical,
434 const unsigned char *val, size_t val_len );
435
436/**
437 * \brief Set the basicConstraints extension for a CRT
438 *
439 * \param ctx CRT context to use
440 * \param is_ca is this a CA certificate
441 * \param max_pathlen maximum length of certificate chains below this
442 * certificate (only for CA certificates, -1 is
443 * inlimited)
444 *
445 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
446 */
447int x509write_crt_set_basic_constraints( x509write_cert *ctx,
448 int is_ca, int max_pathlen );
449
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100450#if defined(POLARSSL_SHA1_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451/**
452 * \brief Set the subjectKeyIdentifier extension for a CRT
453 * Requires that x509write_crt_set_subject_key() has been
454 * called before
455 *
456 * \param ctx CRT context to use
457 *
458 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
459 */
460int x509write_crt_set_subject_key_identifier( x509write_cert *ctx );
461
462/**
463 * \brief Set the authorityKeyIdentifier extension for a CRT
464 * Requires that x509write_crt_set_issuer_key() has been
465 * called before
466 *
467 * \param ctx CRT context to use
468 *
469 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
470 */
471int x509write_crt_set_authority_key_identifier( x509write_cert *ctx );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100472#endif /* POLARSSL_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473
474/**
475 * \brief Set the Key Usage Extension flags
476 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
477 *
478 * \param ctx CRT context to use
479 * \param key_usage key usage flags to set
480 *
481 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
482 */
483int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage );
484
485/**
486 * \brief Set the Netscape Cert Type flags
487 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
488 *
489 * \param ctx CRT context to use
490 * \param ns_cert_type Netscape Cert Type flags to set
491 *
492 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
493 */
494int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
495 unsigned char ns_cert_type );
496
497/**
498 * \brief Free the contents of a CRT write context
499 *
500 * \param ctx CRT context to free
501 */
502void x509write_crt_free( x509write_cert *ctx );
503
504/**
505 * \brief Write a built up certificate to a X509 DER structure
506 * Note: data is written at the end of the buffer! Use the
507 * return value to determine where you should start
508 * using the buffer
509 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100510 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511 * \param buf buffer to write to
512 * \param size size of the buffer
513 * \param f_rng RNG function (for signature, see note)
514 * \param p_rng RNG parameter
515 *
516 * \return length of data written if successful, or a specific
517 * error code
518 *
519 * \note f_rng may be NULL if RSA is used for signature and the
520 * signature is made offline (otherwise f_rng is desirable
521 * for countermeasures against timing attacks).
522 * ECDSA signatures always require a non-NULL f_rng.
523 */
524int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
525 int (*f_rng)(void *, unsigned char *, size_t),
526 void *p_rng );
527
528#if defined(POLARSSL_PEM_WRITE_C)
529/**
530 * \brief Write a built up certificate to a X509 PEM string
531 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100532 * \param ctx certificate to write away
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533 * \param buf buffer to write to
534 * \param size size of the buffer
535 * \param f_rng RNG function (for signature, see note)
536 * \param p_rng RNG parameter
537 *
538 * \return 0 successful, or a specific 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_pem( x509write_cert *ctx, unsigned char *buf, size_t size,
546 int (*f_rng)(void *, unsigned char *, size_t),
547 void *p_rng );
548#endif /* POLARSSL_PEM_WRITE_C */
549#endif /* POLARSSL_X509_CRT_WRITE_C */
550
551#ifdef __cplusplus
552}
553#endif
554
555#endif /* x509_crt.h */