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