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