ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers x509_crt.h Source File

x509_crt.h

Go to the documentation of this file.
00001 /**
00002  * \file x509_crt.h
00003  *
00004  * \brief X.509 certificate parsing and writing
00005  *
00006  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00007  *  SPDX-License-Identifier: Apache-2.0
00008  *
00009  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00010  *  not use this file except in compliance with the License.
00011  *  You may obtain a copy of the License at
00012  *
00013  *  http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  *  Unless required by applicable law or agreed to in writing, software
00016  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00017  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  *  See the License for the specific language governing permissions and
00019  *  limitations under the License.
00020  *
00021  *  This file is part of mbed TLS (https://tls.mbed.org)
00022  */
00023 #ifndef MBEDTLS_X509_CRT_H
00024 #define MBEDTLS_X509_CRT_H
00025 
00026 #if !defined(MBEDTLS_CONFIG_FILE)
00027 #include "config.h"
00028 #else
00029 #include MBEDTLS_CONFIG_FILE
00030 #endif
00031 
00032 #include "x509.h"
00033 #include "x509_crl.h"
00034 
00035 /**
00036  * \addtogroup x509_module
00037  * \{
00038  */
00039 
00040 #ifdef __cplusplus
00041 extern "C" {
00042 #endif
00043 
00044 /**
00045  * \name Structures and functions for parsing and writing X.509 certificates
00046  * \{
00047  */
00048 
00049 /**
00050  * Container for an X.509 certificate. The certificate may be chained.
00051  */
00052 typedef struct mbedtls_x509_crt
00053 {
00054     mbedtls_x509_buf raw;               /**< The raw certificate data (DER). */
00055     mbedtls_x509_buf tbs;               /**< The raw certificate body (DER). The part that is To Be Signed. */
00056 
00057     int version;                /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
00058     mbedtls_x509_buf serial;            /**< Unique id for certificate issued by a specific CA. */
00059     mbedtls_x509_buf sig_oid;           /**< Signature algorithm, e.g. sha1RSA */
00060 
00061     mbedtls_x509_buf issuer_raw;        /**< The raw issuer data (DER). Used for quick comparison. */
00062     mbedtls_x509_buf subject_raw;       /**< The raw subject data (DER). Used for quick comparison. */
00063 
00064     mbedtls_x509_name issuer;           /**< The parsed issuer data (named information object). */
00065     mbedtls_x509_name subject;          /**< The parsed subject data (named information object). */
00066 
00067     mbedtls_x509_time valid_from;       /**< Start time of certificate validity. */
00068     mbedtls_x509_time valid_to;         /**< End time of certificate validity. */
00069 
00070     mbedtls_pk_context pk;              /**< Container for the public key context. */
00071 
00072     mbedtls_x509_buf issuer_id;         /**< Optional X.509 v2/v3 issuer unique identifier. */
00073     mbedtls_x509_buf subject_id;        /**< Optional X.509 v2/v3 subject unique identifier. */
00074     mbedtls_x509_buf v3_ext;            /**< Optional X.509 v3 extensions.  */
00075     mbedtls_x509_sequence subject_alt_names;    /**< Optional list of Subject Alternative Names (Only dNSName supported). */
00076 
00077     int ext_types;              /**< Bit string containing detected and parsed extensions */
00078     int ca_istrue;              /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
00079     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+ */
00080 
00081     unsigned int key_usage;     /**< Optional key usage extension value: See the values in x509.h */
00082 
00083     mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
00084 
00085     unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
00086 
00087     mbedtls_x509_buf sig;               /**< Signature: hash of the tbs part signed with the private key. */
00088     mbedtls_md_type_t sig_md;           /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
00089     mbedtls_pk_type_t sig_pk;           /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
00090     void *sig_opts;             /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
00091 
00092     struct mbedtls_x509_crt *next;     /**< Next certificate in the CA-chain. */
00093 }
00094 mbedtls_x509_crt;
00095 
00096 /**
00097  * Build flag from an algorithm/curve identifier (pk, md, ecp)
00098  * Since 0 is always XXX_NONE, ignore it.
00099  */
00100 #define MBEDTLS_X509_ID_FLAG( id )   ( 1 << ( id - 1 ) )
00101 
00102 /**
00103  * Security profile for certificate verification.
00104  *
00105  * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
00106  */
00107 typedef struct
00108 {
00109     uint32_t allowed_mds;       /**< MDs for signatures         */
00110     uint32_t allowed_pks;       /**< PK algs for signatures     */
00111     uint32_t allowed_curves;    /**< Elliptic curves for ECDSA  */
00112     uint32_t rsa_min_bitlen;    /**< Minimum size for RSA keys  */
00113 }
00114 mbedtls_x509_crt_profile;
00115 
00116 #define MBEDTLS_X509_CRT_VERSION_1              0
00117 #define MBEDTLS_X509_CRT_VERSION_2              1
00118 #define MBEDTLS_X509_CRT_VERSION_3              2
00119 
00120 #define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
00121 #define MBEDTLS_X509_RFC5280_UTC_TIME_LEN   15
00122 
00123 #if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
00124 #define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
00125 #endif
00126 
00127 /**
00128  * Container for writing a certificate (CRT)
00129  */
00130 typedef struct mbedtls_x509write_cert
00131 {
00132     int version;
00133     mbedtls_mpi serial;
00134     mbedtls_pk_context *subject_key;
00135     mbedtls_pk_context *issuer_key;
00136     mbedtls_asn1_named_data *subject;
00137     mbedtls_asn1_named_data *issuer;
00138     mbedtls_md_type_t md_alg;
00139     char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
00140     char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
00141     mbedtls_asn1_named_data *extensions;
00142 }
00143 mbedtls_x509write_cert;
00144 
00145 #if defined(MBEDTLS_X509_CRT_PARSE_C)
00146 /**
00147  * Default security profile. Should provide a good balance between security
00148  * and compatibility with current deployments.
00149  */
00150 extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
00151 
00152 /**
00153  * Expected next default profile. Recommended for new deployments.
00154  * Currently targets a 128-bit security level, except for RSA-2048.
00155  */
00156 extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
00157 
00158 /**
00159  * NSA Suite B profile.
00160  */
00161 extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
00162 
00163 /**
00164  * \brief          Parse a single DER formatted certificate and add it
00165  *                 to the chained list.
00166  *
00167  * \param chain    points to the start of the chain
00168  * \param buf      buffer holding the certificate DER data
00169  * \param buflen   size of the buffer
00170  *
00171  * \return         0 if successful, or a specific X509 or PEM error code
00172  */
00173 int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
00174                         size_t buflen );
00175 
00176 /**
00177  * \brief          Parse one or more certificates and add them
00178  *                 to the chained list. Parses permissively. If some
00179  *                 certificates can be parsed, the result is the number
00180  *                 of failed certificates it encountered. If none complete
00181  *                 correctly, the first error is returned.
00182  *
00183  * \param chain    points to the start of the chain
00184  * \param buf      buffer holding the certificate data in PEM or DER format
00185  * \param buflen   size of the buffer
00186  *                 (including the terminating null byte for PEM data)
00187  *
00188  * \return         0 if all certificates parsed successfully, a positive number
00189  *                 if partly successful or a specific X509 or PEM error code
00190  */
00191 int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
00192 
00193 #if defined(MBEDTLS_FS_IO)
00194 /**
00195  * \brief          Load one or more certificates and add them
00196  *                 to the chained list. Parses permissively. If some
00197  *                 certificates can be parsed, the result is the number
00198  *                 of failed certificates it encountered. If none complete
00199  *                 correctly, the first error is returned.
00200  *
00201  * \param chain    points to the start of the chain
00202  * \param path     filename to read the certificates from
00203  *
00204  * \return         0 if all certificates parsed successfully, a positive number
00205  *                 if partly successful or a specific X509 or PEM error code
00206  */
00207 int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
00208 
00209 /**
00210  * \brief          Load one or more certificate files from a path and add them
00211  *                 to the chained list. Parses permissively. If some
00212  *                 certificates can be parsed, the result is the number
00213  *                 of failed certificates it encountered. If none complete
00214  *                 correctly, the first error is returned.
00215  *
00216  * \param chain    points to the start of the chain
00217  * \param path     directory / folder to read the certificate files from
00218  *
00219  * \return         0 if all certificates parsed successfully, a positive number
00220  *                 if partly successful or a specific X509 or PEM error code
00221  */
00222 int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
00223 #endif /* MBEDTLS_FS_IO */
00224 
00225 /**
00226  * \brief          Returns an informational string about the
00227  *                 certificate.
00228  *
00229  * \param buf      Buffer to write to
00230  * \param size     Maximum size of buffer
00231  * \param prefix   A line prefix
00232  * \param crt      The X509 certificate to represent
00233  *
00234  * \return         The length of the string written (not including the
00235  *                 terminated nul byte), or a negative error code.
00236  */
00237 int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
00238                    const mbedtls_x509_crt *crt );
00239 
00240 /**
00241  * \brief          Returns an informational string about the
00242  *                 verification status of a certificate.
00243  *
00244  * \param buf      Buffer to write to
00245  * \param size     Maximum size of buffer
00246  * \param prefix   A line prefix
00247  * \param flags    Verification flags created by mbedtls_x509_crt_verify()
00248  *
00249  * \return         The length of the string written (not including the
00250  *                 terminated nul byte), or a negative error code.
00251  */
00252 int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
00253                           uint32_t flags );
00254 
00255 /**
00256  * \brief          Verify the certificate signature
00257  *
00258  *                 The verify callback is a user-supplied callback that
00259  *                 can clear / modify / add flags for a certificate. If set,
00260  *                 the verification callback is called for each
00261  *                 certificate in the chain (from the trust-ca down to the
00262  *                 presented crt). The parameters for the callback are:
00263  *                 (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
00264  *                 int *flags). With the flags representing current flags for
00265  *                 that specific certificate and the certificate depth from
00266  *                 the bottom (Peer cert depth = 0).
00267  *
00268  *                 All flags left after returning from the callback
00269  *                 are also returned to the application. The function should
00270  *                 return 0 for anything but a fatal error.
00271  *
00272  * \note           In case verification failed, the results can be displayed
00273  *                 using \c mbedtls_x509_crt_verify_info()
00274  *
00275  * \note           Same as \c mbedtls_x509_crt_verify_with_profile() with the
00276  *                 default security profile.
00277  *
00278  * \note           It is your responsibility to provide up-to-date CRLs for
00279  *                 all trusted CAs. If no CRL is provided for the CA that was
00280  *                 used to sign the certificate, CRL verification is skipped
00281  *                 silently, that is *without* setting any flag.
00282  *
00283  * \param crt      a certificate (chain) to be verified
00284  * \param trust_ca the list of trusted CAs
00285  * \param ca_crl   the list of CRLs for trusted CAs (see note above)
00286  * \param cn       expected Common Name (can be set to
00287  *                 NULL if the CN must not be verified)
00288  * \param flags    result of the verification
00289  * \param f_vrfy   verification function
00290  * \param p_vrfy   verification parameter
00291  *
00292  * \return         0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
00293  *                 in which case *flags will have one or more
00294  *                 MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
00295  *                 set,
00296  *                 or another error in case of a fatal error encountered
00297  *                 during the verification process.
00298  */
00299 int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
00300                      mbedtls_x509_crt *trust_ca,
00301                      mbedtls_x509_crl *ca_crl,
00302                      const char *cn, uint32_t *flags,
00303                      int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
00304                      void *p_vrfy );
00305 
00306 /**
00307  * \brief          Verify the certificate signature according to profile
00308  *
00309  * \note           Same as \c mbedtls_x509_crt_verify(), but with explicit
00310  *                 security profile.
00311  *
00312  * \note           The restrictions on keys (RSA minimum size, allowed curves
00313  *                 for ECDSA) apply to all certificates: trusted root,
00314  *                 intermediate CAs if any, and end entity certificate.
00315  *
00316  * \param crt      a certificate (chain) to be verified
00317  * \param trust_ca the list of trusted CAs
00318  * \param ca_crl   the list of CRLs for trusted CAs
00319  * \param profile  security profile for verification
00320  * \param cn       expected Common Name (can be set to
00321  *                 NULL if the CN must not be verified)
00322  * \param flags    result of the verification
00323  * \param f_vrfy   verification function
00324  * \param p_vrfy   verification parameter
00325  *
00326  * \return         0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
00327  *                 in which case *flags will have one or more
00328  *                 MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
00329  *                 set,
00330  *                 or another error in case of a fatal error encountered
00331  *                 during the verification process.
00332  */
00333 int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
00334                      mbedtls_x509_crt *trust_ca,
00335                      mbedtls_x509_crl *ca_crl,
00336                      const mbedtls_x509_crt_profile *profile,
00337                      const char *cn, uint32_t *flags,
00338                      int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
00339                      void *p_vrfy );
00340 
00341 #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
00342 /**
00343  * \brief          Check usage of certificate against keyUsage extension.
00344  *
00345  * \param crt      Leaf certificate used.
00346  * \param usage    Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
00347  *                 before using the certificate to perform an RSA key
00348  *                 exchange).
00349  *
00350  * \note           Except for decipherOnly and encipherOnly, a bit set in the
00351  *                 usage argument means this bit MUST be set in the
00352  *                 certificate. For decipherOnly and encipherOnly, it means
00353  *                 that bit MAY be set.
00354  *
00355  * \return         0 is these uses of the certificate are allowed,
00356  *                 MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
00357  *                 is present but does not match the usage argument.
00358  *
00359  * \note           You should only call this function on leaf certificates, on
00360  *                 (intermediate) CAs the keyUsage extension is automatically
00361  *                 checked by \c mbedtls_x509_crt_verify().
00362  */
00363 int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
00364                                       unsigned int usage );
00365 #endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
00366 
00367 #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
00368 /**
00369  * \brief          Check usage of certificate against extentedJeyUsage.
00370  *
00371  * \param crt      Leaf certificate used.
00372  * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or MBEDTLS_OID_CLIENT_AUTH).
00373  * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
00374  *
00375  * \return         0 if this use of the certificate is allowed,
00376  *                 MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
00377  *
00378  * \note           Usually only makes sense on leaf certificates.
00379  */
00380 int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
00381                                        const char *usage_oid,
00382                                        size_t usage_len );
00383 #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) */
00384 
00385 #if defined(MBEDTLS_X509_CRL_PARSE_C)
00386 /**
00387  * \brief          Verify the certificate revocation status
00388  *
00389  * \param crt      a certificate to be verified
00390  * \param crl      the CRL to verify against
00391  *
00392  * \return         1 if the certificate is revoked, 0 otherwise
00393  *
00394  */
00395 int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
00396 #endif /* MBEDTLS_X509_CRL_PARSE_C */
00397 
00398 /**
00399  * \brief          Initialize a certificate (chain)
00400  *
00401  * \param crt      Certificate chain to initialize
00402  */
00403 void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
00404 
00405 /**
00406  * \brief          Unallocate all certificate data
00407  *
00408  * \param crt      Certificate chain to free
00409  */
00410 void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
00411 #endif /* MBEDTLS_X509_CRT_PARSE_C */
00412 
00413 /* \} name */
00414 /* \} addtogroup x509_module */
00415 
00416 #if defined(MBEDTLS_X509_CRT_WRITE_C)
00417 /**
00418  * \brief           Initialize a CRT writing context
00419  *
00420  * \param ctx       CRT context to initialize
00421  */
00422 void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
00423 
00424 /**
00425  * \brief           Set the verion for a Certificate
00426  *                  Default: MBEDTLS_X509_CRT_VERSION_3
00427  *
00428  * \param ctx       CRT context to use
00429  * \param version   version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
00430  *                                  MBEDTLS_X509_CRT_VERSION_3)
00431  */
00432 void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
00433 
00434 /**
00435  * \brief           Set the serial number for a Certificate.
00436  *
00437  * \param ctx       CRT context to use
00438  * \param serial    serial number to set
00439  *
00440  * \return          0 if successful
00441  */
00442 int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
00443 
00444 /**
00445  * \brief           Set the validity period for a Certificate
00446  *                  Timestamps should be in string format for UTC timezone
00447  *                  i.e. "YYYYMMDDhhmmss"
00448  *                  e.g. "20131231235959" for December 31st 2013
00449  *                       at 23:59:59
00450  *
00451  * \param ctx       CRT context to use
00452  * \param not_before    not_before timestamp
00453  * \param not_after     not_after timestamp
00454  *
00455  * \return          0 if timestamp was parsed successfully, or
00456  *                  a specific error code
00457  */
00458 int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
00459                                 const char *not_after );
00460 
00461 /**
00462  * \brief           Set the issuer name for a Certificate
00463  *                  Issuer names should contain a comma-separated list
00464  *                  of OID types and values:
00465  *                  e.g. "C=UK,O=ARM,CN=mbed TLS CA"
00466  *
00467  * \param ctx           CRT context to use
00468  * \param issuer_name   issuer name to set
00469  *
00470  * \return          0 if issuer name was parsed successfully, or
00471  *                  a specific error code
00472  */
00473 int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
00474                                    const char *issuer_name );
00475 
00476 /**
00477  * \brief           Set the subject name for a Certificate
00478  *                  Subject names should contain a comma-separated list
00479  *                  of OID types and values:
00480  *                  e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
00481  *
00482  * \param ctx           CRT context to use
00483  * \param subject_name  subject name to set
00484  *
00485  * \return          0 if subject name was parsed successfully, or
00486  *                  a specific error code
00487  */
00488 int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
00489                                     const char *subject_name );
00490 
00491 /**
00492  * \brief           Set the subject public key for the certificate
00493  *
00494  * \param ctx       CRT context to use
00495  * \param key       public key to include
00496  */
00497 void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
00498 
00499 /**
00500  * \brief           Set the issuer key used for signing the certificate
00501  *
00502  * \param ctx       CRT context to use
00503  * \param key       private key to sign with
00504  */
00505 void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
00506 
00507 /**
00508  * \brief           Set the MD algorithm to use for the signature
00509  *                  (e.g. MBEDTLS_MD_SHA1)
00510  *
00511  * \param ctx       CRT context to use
00512  * \param md_alg    MD algorithm to use
00513  */
00514 void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
00515 
00516 /**
00517  * \brief           Generic function to add to or replace an extension in the
00518  *                  CRT
00519  *
00520  * \param ctx       CRT context to use
00521  * \param oid       OID of the extension
00522  * \param oid_len   length of the OID
00523  * \param critical  if the extension is critical (per the RFC's definition)
00524  * \param val       value of the extension OCTET STRING
00525  * \param val_len   length of the value data
00526  *
00527  * \return          0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
00528  */
00529 int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
00530                                  const char *oid, size_t oid_len,
00531                                  int critical,
00532                                  const unsigned char *val, size_t val_len );
00533 
00534 /**
00535  * \brief           Set the basicConstraints extension for a CRT
00536  *
00537  * \param ctx       CRT context to use
00538  * \param is_ca     is this a CA certificate
00539  * \param max_pathlen   maximum length of certificate chains below this
00540  *                      certificate (only for CA certificates, -1 is
00541  *                      inlimited)
00542  *
00543  * \return          0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
00544  */
00545 int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
00546                                          int is_ca, int max_pathlen );
00547 
00548 #if defined(MBEDTLS_SHA1_C)
00549 /**
00550  * \brief           Set the subjectKeyIdentifier extension for a CRT
00551  *                  Requires that mbedtls_x509write_crt_set_subject_key() has been
00552  *                  called before
00553  *
00554  * \param ctx       CRT context to use
00555  *
00556  * \return          0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
00557  */
00558 int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
00559 
00560 /**
00561  * \brief           Set the authorityKeyIdentifier extension for a CRT
00562  *                  Requires that mbedtls_x509write_crt_set_issuer_key() has been
00563  *                  called before
00564  *
00565  * \param ctx       CRT context to use
00566  *
00567  * \return          0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
00568  */
00569 int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
00570 #endif /* MBEDTLS_SHA1_C */
00571 
00572 /**
00573  * \brief           Set the Key Usage Extension flags
00574  *                  (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
00575  *
00576  * \param ctx       CRT context to use
00577  * \param key_usage key usage flags to set
00578  *
00579  * \return          0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
00580  */
00581 int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
00582                                          unsigned int key_usage );
00583 
00584 /**
00585  * \brief           Set the Netscape Cert Type flags
00586  *                  (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
00587  *
00588  * \param ctx           CRT context to use
00589  * \param ns_cert_type  Netscape Cert Type flags to set
00590  *
00591  * \return          0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
00592  */
00593 int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
00594                                     unsigned char ns_cert_type );
00595 
00596 /**
00597  * \brief           Free the contents of a CRT write context
00598  *
00599  * \param ctx       CRT context to free
00600  */
00601 void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
00602 
00603 /**
00604  * \brief           Write a built up certificate to a X509 DER structure
00605  *                  Note: data is written at the end of the buffer! Use the
00606  *                        return value to determine where you should start
00607  *                        using the buffer
00608  *
00609  * \param ctx       certificate to write away
00610  * \param buf       buffer to write to
00611  * \param size      size of the buffer
00612  * \param f_rng     RNG function (for signature, see note)
00613  * \param p_rng     RNG parameter
00614  *
00615  * \return          length of data written if successful, or a specific
00616  *                  error code
00617  *
00618  * \note            f_rng may be NULL if RSA is used for signature and the
00619  *                  signature is made offline (otherwise f_rng is desirable
00620  *                  for countermeasures against timing attacks).
00621  *                  ECDSA signatures always require a non-NULL f_rng.
00622  */
00623 int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
00624                        int (*f_rng)(void *, unsigned char *, size_t),
00625                        void *p_rng );
00626 
00627 #if defined(MBEDTLS_PEM_WRITE_C)
00628 /**
00629  * \brief           Write a built up certificate to a X509 PEM string
00630  *
00631  * \param ctx       certificate to write away
00632  * \param buf       buffer to write to
00633  * \param size      size of the buffer
00634  * \param f_rng     RNG function (for signature, see note)
00635  * \param p_rng     RNG parameter
00636  *
00637  * \return          0 if successful, or a specific error code
00638  *
00639  * \note            f_rng may be NULL if RSA is used for signature and the
00640  *                  signature is made offline (otherwise f_rng is desirable
00641  *                  for countermeasures against timing attacks).
00642  *                  ECDSA signatures always require a non-NULL f_rng.
00643  */
00644 int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
00645                        int (*f_rng)(void *, unsigned char *, size_t),
00646                        void *p_rng );
00647 #endif /* MBEDTLS_PEM_WRITE_C */
00648 #endif /* MBEDTLS_X509_CRT_WRITE_C */
00649 
00650 #ifdef __cplusplus
00651 }
00652 #endif
00653 
00654 #endif /* mbedtls_x509_crt.h */