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