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