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