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