[MBEDTLS]
[reactos.git] / reactos / sdk / include / reactos / libs / mbedtls / x509_crt.h
1 /**
2 * \file x509_crt.h
3 *
4 * \brief X.509 certificate parsing and writing
5 *
6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: GPL-2.0
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 * This file is part of mbed TLS (https://tls.mbed.org)
24 */
25 #ifndef MBEDTLS_X509_CRT_H
26 #define MBEDTLS_X509_CRT_H
27
28 #if !defined(MBEDTLS_CONFIG_FILE)
29 #include "config.h"
30 #else
31 #include MBEDTLS_CONFIG_FILE
32 #endif
33
34 #include "x509.h"
35 #include "x509_crl.h"
36
37 /**
38 * \addtogroup x509_module
39 * \{
40 */
41
42 #ifdef __cplusplus
43 extern "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 */
54 typedef struct mbedtls_x509_crt
55 {
56 mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
57 mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
58
59 int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
60 mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
61 mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
62
63 mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
64 mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
65
66 mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
67 mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
68
69 mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
70 mbedtls_x509_time valid_to; /**< End time of certificate validity. */
71
72 mbedtls_pk_context pk; /**< Container for the public key context. */
73
74 mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
75 mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
76 mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
77 mbedtls_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
83 unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
84
85 mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
86
87 unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
88
89 mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
90 mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
91 mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
92 void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
93
94 struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
95 }
96 mbedtls_x509_crt;
97
98 /**
99 * Build flag from an algorithm/curve identifier (pk, md, ecp)
100 * Since 0 is always XXX_NONE, ignore it.
101 */
102 #define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( id - 1 ) )
103
104 /**
105 * Security profile for certificate verification.
106 *
107 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
108 */
109 typedef struct
110 {
111 uint32_t allowed_mds; /**< MDs for signatures */
112 uint32_t allowed_pks; /**< PK algs for signatures */
113 uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
114 uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
115 }
116 mbedtls_x509_crt_profile;
117
118 #define MBEDTLS_X509_CRT_VERSION_1 0
119 #define MBEDTLS_X509_CRT_VERSION_2 1
120 #define MBEDTLS_X509_CRT_VERSION_3 2
121
122 #define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
123 #define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
124
125 #if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
126 #define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
127 #endif
128
129 /**
130 * Container for writing a certificate (CRT)
131 */
132 typedef struct mbedtls_x509write_cert
133 {
134 int version;
135 mbedtls_mpi serial;
136 mbedtls_pk_context *subject_key;
137 mbedtls_pk_context *issuer_key;
138 mbedtls_asn1_named_data *subject;
139 mbedtls_asn1_named_data *issuer;
140 mbedtls_md_type_t md_alg;
141 char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
142 char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
143 mbedtls_asn1_named_data *extensions;
144 }
145 mbedtls_x509write_cert;
146
147 #if defined(MBEDTLS_X509_CRT_PARSE_C)
148 /**
149 * Default security profile. Should provide a good balance between security
150 * and compatibility with current deployments.
151 */
152 extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
153
154 /**
155 * Expected next default profile. Recommended for new deployments.
156 * Currently targets a 128-bit security level, except for RSA-2048.
157 */
158 extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
159
160 /**
161 * NSA Suite B profile.
162 */
163 extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
164
165 /**
166 * \brief Parse a single DER formatted certificate and add it
167 * to the chained list.
168 *
169 * \param chain points to the start of the chain
170 * \param buf buffer holding the certificate DER data
171 * \param buflen size of the buffer
172 *
173 * \return 0 if successful, or a specific X509 or PEM error code
174 */
175 int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
176 size_t buflen );
177
178 /**
179 * \brief Parse one or more certificates and add them
180 * to the chained list. Parses permissively. If some
181 * certificates can be parsed, the result is the number
182 * of failed certificates it encountered. If none complete
183 * correctly, the first error is returned.
184 *
185 * \param chain points to the start of the chain
186 * \param buf buffer holding the certificate data in PEM or DER format
187 * \param buflen size of the buffer
188 * (including the terminating null byte for PEM data)
189 *
190 * \return 0 if all certificates parsed successfully, a positive number
191 * if partly successful or a specific X509 or PEM error code
192 */
193 int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
194
195 #if defined(MBEDTLS_FS_IO)
196 /**
197 * \brief Load one or more certificates and add them
198 * to the chained list. Parses permissively. If some
199 * certificates can be parsed, the result is the number
200 * of failed certificates it encountered. If none complete
201 * correctly, the first error is returned.
202 *
203 * \param chain points to the start of the chain
204 * \param path filename to read the certificates from
205 *
206 * \return 0 if all certificates parsed successfully, a positive number
207 * if partly successful or a specific X509 or PEM error code
208 */
209 int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
210
211 /**
212 * \brief Load one or more certificate files from a path and add them
213 * to the chained list. Parses permissively. If some
214 * certificates can be parsed, the result is the number
215 * of failed certificates it encountered. If none complete
216 * correctly, the first error is returned.
217 *
218 * \param chain points to the start of the chain
219 * \param path directory / folder to read the certificate files from
220 *
221 * \return 0 if all certificates parsed successfully, a positive number
222 * if partly successful or a specific X509 or PEM error code
223 */
224 int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
225 #endif /* MBEDTLS_FS_IO */
226
227 /**
228 * \brief Returns an informational string about the
229 * certificate.
230 *
231 * \param buf Buffer to write to
232 * \param size Maximum size of buffer
233 * \param prefix A line prefix
234 * \param crt The X509 certificate to represent
235 *
236 * \return The length of the string written (not including the
237 * terminated nul byte), or a negative error code.
238 */
239 int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
240 const mbedtls_x509_crt *crt );
241
242 /**
243 * \brief Returns an informational string about the
244 * verification status of a certificate.
245 *
246 * \param buf Buffer to write to
247 * \param size Maximum size of buffer
248 * \param prefix A line prefix
249 * \param flags Verification flags created by mbedtls_x509_crt_verify()
250 *
251 * \return The length of the string written (not including the
252 * terminated nul byte), or a negative error code.
253 */
254 int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
255 uint32_t flags );
256
257 /**
258 * \brief Verify the certificate signature
259 *
260 * The verify callback is a user-supplied callback that
261 * can clear / modify / add flags for a certificate. If set,
262 * the verification callback is called for each
263 * certificate in the chain (from the trust-ca down to the
264 * presented crt). The parameters for the callback are:
265 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
266 * int *flags). With the flags representing current flags for
267 * that specific certificate and the certificate depth from
268 * the bottom (Peer cert depth = 0).
269 *
270 * All flags left after returning from the callback
271 * are also returned to the application. The function should
272 * return 0 for anything (including invalid certificates)
273 * other than fatal error, as a non-zero return code
274 * immediately aborts the verification process. For fatal
275 * errors, a specific error code should be used (different
276 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
277 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
278 * can be used if no better code is available.
279 *
280 * \note In case verification failed, the results can be displayed
281 * using \c mbedtls_x509_crt_verify_info()
282 *
283 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
284 * default security profile.
285 *
286 * \note It is your responsibility to provide up-to-date CRLs for
287 * all trusted CAs. If no CRL is provided for the CA that was
288 * used to sign the certificate, CRL verification is skipped
289 * silently, that is *without* setting any flag.
290 *
291 * \param crt a certificate (chain) to be verified
292 * \param trust_ca the list of trusted CAs
293 * \param ca_crl the list of CRLs for trusted CAs (see note above)
294 * \param cn expected Common Name (can be set to
295 * NULL if the CN must not be verified)
296 * \param flags result of the verification
297 * \param f_vrfy verification function
298 * \param p_vrfy verification parameter
299 *
300 * \return 0 (and flags set to 0) if the chain was verified and valid,
301 * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
302 * but found to be invalid, in which case *flags will have one
303 * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
304 * flags set, or another error (and flags set to 0xffffffff)
305 * in case of a fatal error encountered during the
306 * verification process.
307 */
308 int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
309 mbedtls_x509_crt *trust_ca,
310 mbedtls_x509_crl *ca_crl,
311 const char *cn, uint32_t *flags,
312 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
313 void *p_vrfy );
314
315 /**
316 * \brief Verify the certificate signature according to profile
317 *
318 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
319 * security profile.
320 *
321 * \note The restrictions on keys (RSA minimum size, allowed curves
322 * for ECDSA) apply to all certificates: trusted root,
323 * intermediate CAs if any, and end entity certificate.
324 *
325 * \param crt a certificate (chain) to be verified
326 * \param trust_ca the list of trusted CAs
327 * \param ca_crl the list of CRLs for trusted CAs
328 * \param profile security profile for verification
329 * \param cn expected Common Name (can be set to
330 * NULL if the CN must not be verified)
331 * \param flags result of the verification
332 * \param f_vrfy verification function
333 * \param p_vrfy verification parameter
334 *
335 * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
336 * in which case *flags will have one or more
337 * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
338 * set,
339 * or another error in case of a fatal error encountered
340 * during the verification process.
341 */
342 int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
343 mbedtls_x509_crt *trust_ca,
344 mbedtls_x509_crl *ca_crl,
345 const mbedtls_x509_crt_profile *profile,
346 const char *cn, uint32_t *flags,
347 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
348 void *p_vrfy );
349
350 #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
351 /**
352 * \brief Check usage of certificate against keyUsage extension.
353 *
354 * \param crt Leaf certificate used.
355 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
356 * before using the certificate to perform an RSA key
357 * exchange).
358 *
359 * \note Except for decipherOnly and encipherOnly, a bit set in the
360 * usage argument means this bit MUST be set in the
361 * certificate. For decipherOnly and encipherOnly, it means
362 * that bit MAY be set.
363 *
364 * \return 0 is these uses of the certificate are allowed,
365 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
366 * is present but does not match the usage argument.
367 *
368 * \note You should only call this function on leaf certificates, on
369 * (intermediate) CAs the keyUsage extension is automatically
370 * checked by \c mbedtls_x509_crt_verify().
371 */
372 int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
373 unsigned int usage );
374 #endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
375
376 #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
377 /**
378 * \brief Check usage of certificate against extentedJeyUsage.
379 *
380 * \param crt Leaf certificate used.
381 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or MBEDTLS_OID_CLIENT_AUTH).
382 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
383 *
384 * \return 0 if this use of the certificate is allowed,
385 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
386 *
387 * \note Usually only makes sense on leaf certificates.
388 */
389 int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
390 const char *usage_oid,
391 size_t usage_len );
392 #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) */
393
394 #if defined(MBEDTLS_X509_CRL_PARSE_C)
395 /**
396 * \brief Verify the certificate revocation status
397 *
398 * \param crt a certificate to be verified
399 * \param crl the CRL to verify against
400 *
401 * \return 1 if the certificate is revoked, 0 otherwise
402 *
403 */
404 int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
405 #endif /* MBEDTLS_X509_CRL_PARSE_C */
406
407 /**
408 * \brief Initialize a certificate (chain)
409 *
410 * \param crt Certificate chain to initialize
411 */
412 void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
413
414 /**
415 * \brief Unallocate all certificate data
416 *
417 * \param crt Certificate chain to free
418 */
419 void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
420 #endif /* MBEDTLS_X509_CRT_PARSE_C */
421
422 /* \} name */
423 /* \} addtogroup x509_module */
424
425 #if defined(MBEDTLS_X509_CRT_WRITE_C)
426 /**
427 * \brief Initialize a CRT writing context
428 *
429 * \param ctx CRT context to initialize
430 */
431 void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
432
433 /**
434 * \brief Set the verion for a Certificate
435 * Default: MBEDTLS_X509_CRT_VERSION_3
436 *
437 * \param ctx CRT context to use
438 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
439 * MBEDTLS_X509_CRT_VERSION_3)
440 */
441 void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
442
443 /**
444 * \brief Set the serial number for a Certificate.
445 *
446 * \param ctx CRT context to use
447 * \param serial serial number to set
448 *
449 * \return 0 if successful
450 */
451 int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
452
453 /**
454 * \brief Set the validity period for a Certificate
455 * Timestamps should be in string format for UTC timezone
456 * i.e. "YYYYMMDDhhmmss"
457 * e.g. "20131231235959" for December 31st 2013
458 * at 23:59:59
459 *
460 * \param ctx CRT context to use
461 * \param not_before not_before timestamp
462 * \param not_after not_after timestamp
463 *
464 * \return 0 if timestamp was parsed successfully, or
465 * a specific error code
466 */
467 int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
468 const char *not_after );
469
470 /**
471 * \brief Set the issuer name for a Certificate
472 * Issuer names should contain a comma-separated list
473 * of OID types and values:
474 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
475 *
476 * \param ctx CRT context to use
477 * \param issuer_name issuer name to set
478 *
479 * \return 0 if issuer name was parsed successfully, or
480 * a specific error code
481 */
482 int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
483 const char *issuer_name );
484
485 /**
486 * \brief Set the subject name for a Certificate
487 * Subject names should contain a comma-separated list
488 * of OID types and values:
489 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
490 *
491 * \param ctx CRT context to use
492 * \param subject_name subject name to set
493 *
494 * \return 0 if subject name was parsed successfully, or
495 * a specific error code
496 */
497 int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
498 const char *subject_name );
499
500 /**
501 * \brief Set the subject public key for the certificate
502 *
503 * \param ctx CRT context to use
504 * \param key public key to include
505 */
506 void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
507
508 /**
509 * \brief Set the issuer key used for signing the certificate
510 *
511 * \param ctx CRT context to use
512 * \param key private key to sign with
513 */
514 void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
515
516 /**
517 * \brief Set the MD algorithm to use for the signature
518 * (e.g. MBEDTLS_MD_SHA1)
519 *
520 * \param ctx CRT context to use
521 * \param md_alg MD algorithm to use
522 */
523 void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
524
525 /**
526 * \brief Generic function to add to or replace an extension in the
527 * CRT
528 *
529 * \param ctx CRT context to use
530 * \param oid OID of the extension
531 * \param oid_len length of the OID
532 * \param critical if the extension is critical (per the RFC's definition)
533 * \param val value of the extension OCTET STRING
534 * \param val_len length of the value data
535 *
536 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
537 */
538 int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
539 const char *oid, size_t oid_len,
540 int critical,
541 const unsigned char *val, size_t val_len );
542
543 /**
544 * \brief Set the basicConstraints extension for a CRT
545 *
546 * \param ctx CRT context to use
547 * \param is_ca is this a CA certificate
548 * \param max_pathlen maximum length of certificate chains below this
549 * certificate (only for CA certificates, -1 is
550 * inlimited)
551 *
552 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
553 */
554 int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
555 int is_ca, int max_pathlen );
556
557 #if defined(MBEDTLS_SHA1_C)
558 /**
559 * \brief Set the subjectKeyIdentifier extension for a CRT
560 * Requires that mbedtls_x509write_crt_set_subject_key() has been
561 * called before
562 *
563 * \param ctx CRT context to use
564 *
565 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
566 */
567 int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
568
569 /**
570 * \brief Set the authorityKeyIdentifier extension for a CRT
571 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
572 * called before
573 *
574 * \param ctx CRT context to use
575 *
576 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
577 */
578 int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
579 #endif /* MBEDTLS_SHA1_C */
580
581 /**
582 * \brief Set the Key Usage Extension flags
583 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
584 *
585 * \param ctx CRT context to use
586 * \param key_usage key usage flags to set
587 *
588 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
589 */
590 int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
591 unsigned int key_usage );
592
593 /**
594 * \brief Set the Netscape Cert Type flags
595 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
596 *
597 * \param ctx CRT context to use
598 * \param ns_cert_type Netscape Cert Type flags to set
599 *
600 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
601 */
602 int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
603 unsigned char ns_cert_type );
604
605 /**
606 * \brief Free the contents of a CRT write context
607 *
608 * \param ctx CRT context to free
609 */
610 void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
611
612 /**
613 * \brief Write a built up certificate to a X509 DER structure
614 * Note: data is written at the end of the buffer! Use the
615 * return value to determine where you should start
616 * using the buffer
617 *
618 * \param ctx certificate to write away
619 * \param buf buffer to write to
620 * \param size size of the buffer
621 * \param f_rng RNG function (for signature, see note)
622 * \param p_rng RNG parameter
623 *
624 * \return length of data written if successful, or a specific
625 * error code
626 *
627 * \note f_rng may be NULL if RSA is used for signature and the
628 * signature is made offline (otherwise f_rng is desirable
629 * for countermeasures against timing attacks).
630 * ECDSA signatures always require a non-NULL f_rng.
631 */
632 int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
633 int (*f_rng)(void *, unsigned char *, size_t),
634 void *p_rng );
635
636 #if defined(MBEDTLS_PEM_WRITE_C)
637 /**
638 * \brief Write a built up certificate to a X509 PEM string
639 *
640 * \param ctx certificate to write away
641 * \param buf buffer to write to
642 * \param size size of the buffer
643 * \param f_rng RNG function (for signature, see note)
644 * \param p_rng RNG parameter
645 *
646 * \return 0 if successful, or a specific error code
647 *
648 * \note f_rng may be NULL if RSA is used for signature and the
649 * signature is made offline (otherwise f_rng is desirable
650 * for countermeasures against timing attacks).
651 * ECDSA signatures always require a non-NULL f_rng.
652 */
653 int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
654 int (*f_rng)(void *, unsigned char *, size_t),
655 void *p_rng );
656 #endif /* MBEDTLS_PEM_WRITE_C */
657 #endif /* MBEDTLS_X509_CRT_WRITE_C */
658
659 #ifdef __cplusplus
660 }
661 #endif
662
663 #endif /* mbedtls_x509_crt.h */