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 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 7 | * Copyright The Mbed TLS Contributors |
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 | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #ifndef MBEDTLS_X509_CRT_H |
| 23 | #define MBEDTLS_X509_CRT_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 24 | #include "mbedtls/private_access.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 25 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 26 | #include "mbedtls/build_info.h" |
Manuel Pégourié-Gonnard | e896705 | 2022-09-15 11:41:16 +0200 | [diff] [blame] | 27 | #include "mbedtls/legacy_or_psa.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 28 | |
Jaeden Amero | 6609aef | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 29 | #include "mbedtls/x509.h" |
| 30 | #include "mbedtls/x509_crl.h" |
| 31 | #include "mbedtls/bignum.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * \addtogroup x509_module |
| 35 | * \{ |
| 36 | */ |
| 37 | |
| 38 | #ifdef __cplusplus |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
| 42 | /** |
| 43 | * \name Structures and functions for parsing and writing X.509 certificates |
| 44 | * \{ |
| 45 | */ |
| 46 | |
| 47 | /** |
| 48 | * Container for an X.509 certificate. The certificate may be chained. |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +0200 | [diff] [blame] | 49 | * |
| 50 | * Some fields of this structure are publicly readable. Do not modify |
| 51 | * them except via Mbed TLS library functions: the effect of modifying |
| 52 | * those fields or the data that those fields points to is unspecified. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 53 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 54 | typedef struct mbedtls_x509_crt { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 55 | int MBEDTLS_PRIVATE(own_buffer); /**< Indicates if \c raw is owned |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | * by the structure or not. */ |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +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 | |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +0200 | [diff] [blame] | 60 | int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */ |
| 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 | |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +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 | |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +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 | |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +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 | |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +0200 | [diff] [blame] | 73 | mbedtls_x509_buf pk_raw; |
| 74 | mbedtls_pk_context pk; /**< Container for the public key context. */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 75 | |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +0200 | [diff] [blame] | 76 | mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */ |
| 77 | mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */ |
| 78 | mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */ |
| 79 | mbedtls_x509_sequence subject_alt_names; /**< Optional list of 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] | 80 | |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +0200 | [diff] [blame] | 81 | 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] | 82 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 83 | int MBEDTLS_PRIVATE(ext_types); /**< Bit string containing detected and parsed extensions */ |
| 84 | int MBEDTLS_PRIVATE(ca_istrue); /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */ |
| 85 | int MBEDTLS_PRIVATE(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+ */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 86 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 87 | unsigned int MBEDTLS_PRIVATE(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 | |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +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 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 91 | unsigned char MBEDTLS_PRIVATE(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 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 93 | mbedtls_x509_buf MBEDTLS_PRIVATE(sig); /**< Signature: hash of the tbs part signed with the private key. */ |
| 94 | mbedtls_md_type_t MBEDTLS_PRIVATE(sig_md); /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */ |
| 95 | mbedtls_pk_type_t MBEDTLS_PRIVATE(sig_pk); /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */ |
| 96 | void *MBEDTLS_PRIVATE(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 | |
Gilles Peskine | ca93995 | 2021-08-31 23:18:07 +0200 | [diff] [blame] | 98 | /** Next certificate in the linked list that constitutes the CA chain. |
| 99 | * \p NULL indicates the end of the list. |
| 100 | * Do not modify this field directly. */ |
| 101 | struct mbedtls_x509_crt *next; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 102 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | mbedtls_x509_crt; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 104 | |
Janos Follath | 5091bec | 2019-05-08 15:23:08 +0100 | [diff] [blame] | 105 | /** |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 106 | * From RFC 5280 section 4.2.1.6: |
| 107 | * OtherName ::= SEQUENCE { |
| 108 | * type-id OBJECT IDENTIFIER, |
| 109 | * value [0] EXPLICIT ANY DEFINED BY type-id } |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +0200 | [diff] [blame] | 110 | * |
| 111 | * Future versions of the library may add new fields to this structure or |
| 112 | * to its embedded union and structure. |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 113 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | typedef struct mbedtls_x509_san_other_name { |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 115 | /** |
Tom Cosgrove | 1e21144 | 2022-05-26 11:51:00 +0100 | [diff] [blame] | 116 | * The type_id is an OID as defined in RFC 5280. |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 117 | * To check the value of the type id, you should use |
| 118 | * \p MBEDTLS_OID_CMP with a known OID mbedtls_x509_buf. |
| 119 | */ |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +0200 | [diff] [blame] | 120 | mbedtls_x509_buf type_id; /**< The type id. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | union { |
Janos Follath | 5091bec | 2019-05-08 15:23:08 +0100 | [diff] [blame] | 122 | /** |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 123 | * From RFC 4108 section 5: |
| 124 | * HardwareModuleName ::= SEQUENCE { |
| 125 | * hwType OBJECT IDENTIFIER, |
| 126 | * hwSerialNum OCTET STRING } |
| 127 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 128 | struct { |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +0200 | [diff] [blame] | 129 | mbedtls_x509_buf oid; /**< The object identifier. */ |
| 130 | mbedtls_x509_buf val; /**< The named value. */ |
Ron Eldor | f05f594 | 2019-05-13 19:11:31 +0300 | [diff] [blame] | 131 | } |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +0200 | [diff] [blame] | 132 | hardware_module_name; |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 133 | } |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +0200 | [diff] [blame] | 134 | value; |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 135 | } |
| 136 | mbedtls_x509_san_other_name; |
| 137 | |
Janos Follath | 5091bec | 2019-05-08 15:23:08 +0100 | [diff] [blame] | 138 | /** |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +0200 | [diff] [blame] | 139 | * A structure for holding the parsed Subject Alternative Name, |
| 140 | * according to type. |
| 141 | * |
| 142 | * Future versions of the library may add new fields to this structure or |
| 143 | * to its embedded union and structure. |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 144 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 145 | typedef struct mbedtls_x509_subject_alternative_name { |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +0200 | [diff] [blame] | 146 | int type; /**< The SAN type, value of MBEDTLS_X509_SAN_XXX. */ |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 147 | union { |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +0200 | [diff] [blame] | 148 | mbedtls_x509_san_other_name other_name; /**< The otherName supported type. */ |
| 149 | mbedtls_x509_buf unstructured_name; /**< The buffer for the un constructed types. Only dnsName currently supported */ |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 150 | } |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +0200 | [diff] [blame] | 151 | san; /**< A union of the supported SAN types */ |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 152 | } |
| 153 | mbedtls_x509_subject_alternative_name; |
| 154 | |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 155 | /** |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 156 | * Build flag from an algorithm/curve identifier (pk, md, ecp) |
| 157 | * Since 0 is always XXX_NONE, ignore it. |
| 158 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 159 | #define MBEDTLS_X509_ID_FLAG(id) (1 << ((id) - 1)) |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 160 | |
| 161 | /** |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 162 | * Security profile for certificate verification. |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 163 | * |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 164 | * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG(). |
Manuel Pégourié-Gonnard | 55a7fb8 | 2021-06-17 10:39:39 +0200 | [diff] [blame] | 165 | * |
| 166 | * The fields of this structure are part of the public API and can be |
| 167 | * manipulated directly by applications. Future versions of the library may |
| 168 | * add extra fields or reorder existing fields. |
Manuel Pégourié-Gonnard | 9d4c2c4 | 2021-06-18 09:48:27 +0200 | [diff] [blame] | 169 | * |
| 170 | * You can create custom profiles by starting from a copy of |
| 171 | * an existing profile, such as mbedtls_x509_crt_profile_default or |
| 172 | * mbedtls_x509_ctr_profile_none and then tune it to your needs. |
| 173 | * |
| 174 | * For example to allow SHA-224 in addition to the default: |
| 175 | * |
| 176 | * mbedtls_x509_crt_profile my_profile = mbedtls_x509_crt_profile_default; |
| 177 | * my_profile.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ); |
| 178 | * |
| 179 | * Or to allow only RSA-3072+ with SHA-256: |
| 180 | * |
| 181 | * mbedtls_x509_crt_profile my_profile = mbedtls_x509_crt_profile_none; |
| 182 | * my_profile.allowed_mds = MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ); |
| 183 | * my_profile.allowed_pks = MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA ); |
| 184 | * my_profile.rsa_min_bitlen = 3072; |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 185 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 186 | typedef struct mbedtls_x509_crt_profile { |
Manuel Pégourié-Gonnard | 5314e08 | 2021-06-17 10:32:01 +0200 | [diff] [blame] | 187 | uint32_t allowed_mds; /**< MDs for signatures */ |
Hanno Becker | 2b9fb88 | 2018-10-11 11:36:29 +0100 | [diff] [blame] | 188 | uint32_t allowed_pks; /**< PK algs for public keys; |
Manuel Pégourié-Gonnard | 8ba99e7 | 2022-04-25 10:12:01 +0200 | [diff] [blame] | 189 | * this applies to all certificates |
Hanno Becker | 2b9fb88 | 2018-10-11 11:36:29 +0100 | [diff] [blame] | 190 | * in the provided chain. */ |
Manuel Pégourié-Gonnard | 5314e08 | 2021-06-17 10:32:01 +0200 | [diff] [blame] | 191 | uint32_t allowed_curves; /**< Elliptic curves for ECDSA */ |
| 192 | uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */ |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 193 | } |
| 194 | mbedtls_x509_crt_profile; |
| 195 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | #define MBEDTLS_X509_CRT_VERSION_1 0 |
| 197 | #define MBEDTLS_X509_CRT_VERSION_2 1 |
| 198 | #define MBEDTLS_X509_CRT_VERSION_3 2 |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 199 | |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 200 | #define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 20 |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | #define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15 |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | #if !defined(MBEDTLS_X509_MAX_FILE_PATH_LEN) |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 204 | #define MBEDTLS_X509_MAX_FILE_PATH_LEN 512 |
| 205 | #endif |
| 206 | |
Hanno Becker | 7ac83f9 | 2020-10-09 10:48:22 +0100 | [diff] [blame] | 207 | /* This macro unfolds to the concatenation of macro invocations |
| 208 | * X509_CRT_ERROR_INFO( error code, |
| 209 | * error code as string, |
| 210 | * human readable description ) |
| 211 | * where X509_CRT_ERROR_INFO is defined by the user. |
| 212 | * See x509_crt.c for an example of how to use this. */ |
| 213 | #define MBEDTLS_X509_CRT_ERROR_INFO_LIST \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 214 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_EXPIRED, \ |
| 215 | "MBEDTLS_X509_BADCERT_EXPIRED", \ |
| 216 | "The certificate validity has expired") \ |
| 217 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_REVOKED, \ |
| 218 | "MBEDTLS_X509_BADCERT_REVOKED", \ |
| 219 | "The certificate has been revoked (is on a CRL)") \ |
| 220 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_CN_MISMATCH, \ |
| 221 | "MBEDTLS_X509_BADCERT_CN_MISMATCH", \ |
| 222 | "The certificate Common Name (CN) does not match with the expected CN") \ |
| 223 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_NOT_TRUSTED, \ |
| 224 | "MBEDTLS_X509_BADCERT_NOT_TRUSTED", \ |
| 225 | "The certificate is not correctly signed by the trusted CA") \ |
| 226 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_NOT_TRUSTED, \ |
| 227 | "MBEDTLS_X509_BADCRL_NOT_TRUSTED", \ |
| 228 | "The CRL is not correctly signed by the trusted CA") \ |
| 229 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_EXPIRED, \ |
| 230 | "MBEDTLS_X509_BADCRL_EXPIRED", \ |
| 231 | "The CRL is expired") \ |
| 232 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_MISSING, \ |
| 233 | "MBEDTLS_X509_BADCERT_MISSING", \ |
| 234 | "Certificate was missing") \ |
| 235 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_SKIP_VERIFY, \ |
| 236 | "MBEDTLS_X509_BADCERT_SKIP_VERIFY", \ |
| 237 | "Certificate verification was skipped") \ |
| 238 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_OTHER, \ |
| 239 | "MBEDTLS_X509_BADCERT_OTHER", \ |
| 240 | "Other reason (can be used by verify callback)") \ |
| 241 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_FUTURE, \ |
| 242 | "MBEDTLS_X509_BADCERT_FUTURE", \ |
| 243 | "The certificate validity starts in the future") \ |
| 244 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_FUTURE, \ |
| 245 | "MBEDTLS_X509_BADCRL_FUTURE", \ |
| 246 | "The CRL is from the future") \ |
| 247 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_KEY_USAGE, \ |
| 248 | "MBEDTLS_X509_BADCERT_KEY_USAGE", \ |
| 249 | "Usage does not match the keyUsage extension") \ |
| 250 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, \ |
| 251 | "MBEDTLS_X509_BADCERT_EXT_KEY_USAGE", \ |
| 252 | "Usage does not match the extendedKeyUsage extension") \ |
| 253 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_NS_CERT_TYPE, \ |
| 254 | "MBEDTLS_X509_BADCERT_NS_CERT_TYPE", \ |
| 255 | "Usage does not match the nsCertType extension") \ |
| 256 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_BAD_MD, \ |
| 257 | "MBEDTLS_X509_BADCERT_BAD_MD", \ |
| 258 | "The certificate is signed with an unacceptable hash.") \ |
| 259 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_BAD_PK, \ |
| 260 | "MBEDTLS_X509_BADCERT_BAD_PK", \ |
| 261 | "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA).") \ |
| 262 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCERT_BAD_KEY, \ |
| 263 | "MBEDTLS_X509_BADCERT_BAD_KEY", \ |
| 264 | "The certificate is signed with an unacceptable key (eg bad curve, RSA too short).") \ |
| 265 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_BAD_MD, \ |
| 266 | "MBEDTLS_X509_BADCRL_BAD_MD", \ |
| 267 | "The CRL is signed with an unacceptable hash.") \ |
| 268 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_BAD_PK, \ |
| 269 | "MBEDTLS_X509_BADCRL_BAD_PK", \ |
| 270 | "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA).") \ |
| 271 | X509_CRT_ERROR_INFO(MBEDTLS_X509_BADCRL_BAD_KEY, \ |
| 272 | "MBEDTLS_X509_BADCRL_BAD_KEY", \ |
| 273 | "The CRL is signed with an unacceptable key (eg bad curve, RSA too short).") |
Hanno Becker | 7ac83f9 | 2020-10-09 10:48:22 +0100 | [diff] [blame] | 274 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 275 | /** |
| 276 | * Container for writing a certificate (CRT) |
| 277 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 278 | typedef struct mbedtls_x509write_cert { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 279 | int MBEDTLS_PRIVATE(version); |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 280 | unsigned char MBEDTLS_PRIVATE(serial)[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN]; |
| 281 | size_t MBEDTLS_PRIVATE(serial_len); |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 282 | mbedtls_pk_context *MBEDTLS_PRIVATE(subject_key); |
| 283 | mbedtls_pk_context *MBEDTLS_PRIVATE(issuer_key); |
| 284 | mbedtls_asn1_named_data *MBEDTLS_PRIVATE(subject); |
| 285 | mbedtls_asn1_named_data *MBEDTLS_PRIVATE(issuer); |
| 286 | mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg); |
| 287 | char MBEDTLS_PRIVATE(not_before)[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1]; |
| 288 | char MBEDTLS_PRIVATE(not_after)[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1]; |
| 289 | mbedtls_asn1_named_data *MBEDTLS_PRIVATE(extensions); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 290 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 291 | mbedtls_x509write_cert; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 292 | |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 293 | /** |
| 294 | * Item in a verification chain: cert and flags for it |
| 295 | */ |
| 296 | typedef struct { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 297 | mbedtls_x509_crt *MBEDTLS_PRIVATE(crt); |
| 298 | uint32_t MBEDTLS_PRIVATE(flags); |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 299 | } mbedtls_x509_crt_verify_chain_item; |
| 300 | |
| 301 | /** |
| 302 | * Max size of verification chain: end-entity + intermediates + trusted root |
| 303 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 304 | #define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2) |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 305 | |
| 306 | /** |
| 307 | * Verification chain as built by \c mbedtls_crt_verify_chain() |
| 308 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 309 | typedef struct { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 310 | mbedtls_x509_crt_verify_chain_item MBEDTLS_PRIVATE(items)[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE]; |
| 311 | unsigned MBEDTLS_PRIVATE(len); |
Hanno Becker | f53893b | 2019-03-28 13:45:55 +0000 | [diff] [blame] | 312 | |
| 313 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 314 | /* This stores the list of potential trusted signers obtained from |
| 315 | * the CA callback used for the CRT verification, if configured. |
| 316 | * We must track it somewhere because the callback passes its |
| 317 | * ownership to the caller. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 318 | mbedtls_x509_crt *MBEDTLS_PRIVATE(trust_ca_cb_result); |
Hanno Becker | f53893b | 2019-03-28 13:45:55 +0000 | [diff] [blame] | 319 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 320 | } mbedtls_x509_crt_verify_chain; |
| 321 | |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 322 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 323 | |
| 324 | /** |
| 325 | * \brief Context for resuming X.509 verify operations |
| 326 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 327 | typedef struct { |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 328 | /* for check_signature() */ |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 329 | mbedtls_pk_restart_ctx MBEDTLS_PRIVATE(pk); |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 330 | |
| 331 | /* for find_parent_in() */ |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 332 | mbedtls_x509_crt *MBEDTLS_PRIVATE(parent); /* non-null iff parent_in in progress */ |
| 333 | mbedtls_x509_crt *MBEDTLS_PRIVATE(fallback_parent); |
| 334 | int MBEDTLS_PRIVATE(fallback_signature_is_good); |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 335 | |
| 336 | /* for find_parent() */ |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 337 | int MBEDTLS_PRIVATE(parent_is_trusted); /* -1 if find_parent is not in progress */ |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 338 | |
| 339 | /* for verify_chain() */ |
Manuel Pégourié-Gonnard | daf0491 | 2017-08-23 12:32:19 +0200 | [diff] [blame] | 340 | enum { |
| 341 | x509_crt_rs_none, |
| 342 | x509_crt_rs_find_parent, |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 343 | } MBEDTLS_PRIVATE(in_progress); /* none if no operation is in progress */ |
| 344 | int MBEDTLS_PRIVATE(self_cnt); |
| 345 | mbedtls_x509_crt_verify_chain MBEDTLS_PRIVATE(ver_chain); |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 346 | |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 347 | } mbedtls_x509_crt_restart_ctx; |
| 348 | |
| 349 | #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 350 | |
| 351 | /* Now we can declare functions that take a pointer to that */ |
| 352 | typedef void mbedtls_x509_crt_restart_ctx; |
| 353 | |
| 354 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 355 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 356 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 357 | /** |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 358 | * Default security profile. Should provide a good balance between security |
| 359 | * and compatibility with current deployments. |
Gilles Peskine | ffb92da | 2021-06-02 00:03:26 +0200 | [diff] [blame] | 360 | * |
| 361 | * This profile permits: |
| 362 | * - SHA2 hashes with at least 256 bits: SHA-256, SHA-384, SHA-512. |
Gilles Peskine | 3995750 | 2021-06-17 23:17:52 +0200 | [diff] [blame] | 363 | * - Elliptic curves with 255 bits and above except secp256k1. |
Gilles Peskine | ffb92da | 2021-06-02 00:03:26 +0200 | [diff] [blame] | 364 | * - RSA with 2048 bits and above. |
| 365 | * |
| 366 | * New minor versions of Mbed TLS may extend this profile, for example if |
Gilles Peskine | 3995750 | 2021-06-17 23:17:52 +0200 | [diff] [blame] | 367 | * new algorithms are added to the library. New minor versions of Mbed TLS will |
Gilles Peskine | ffb92da | 2021-06-02 00:03:26 +0200 | [diff] [blame] | 368 | * not reduce this profile unless serious security concerns require it. |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 369 | */ |
| 370 | extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default; |
| 371 | |
| 372 | /** |
| 373 | * Expected next default profile. Recommended for new deployments. |
Gilles Peskine | ffb92da | 2021-06-02 00:03:26 +0200 | [diff] [blame] | 374 | * Currently targets a 128-bit security level, except for allowing RSA-2048. |
| 375 | * This profile may change at any time. |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 376 | */ |
| 377 | extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next; |
| 378 | |
| 379 | /** |
| 380 | * NSA Suite B profile. |
| 381 | */ |
| 382 | extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb; |
| 383 | |
| 384 | /** |
Manuel Pégourié-Gonnard | 9d4c2c4 | 2021-06-18 09:48:27 +0200 | [diff] [blame] | 385 | * Empty profile that allows nothing. Useful as a basis for constructing |
| 386 | * custom profiles. |
| 387 | */ |
| 388 | extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none; |
| 389 | |
| 390 | /** |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 391 | * \brief Parse a single DER formatted certificate and add it |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 392 | * to the end of the provided chained list. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 393 | * |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 394 | * \param chain The pointer to the start of the CRT chain to attach to. |
| 395 | * When parsing the first CRT in a chain, this should point |
| 396 | * to an instance of ::mbedtls_x509_crt initialized through |
| 397 | * mbedtls_x509_crt_init(). |
| 398 | * \param buf The buffer holding the DER encoded certificate. |
| 399 | * \param buflen The size in Bytes of \p buf. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 400 | * |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 401 | * \note This function makes an internal copy of the CRT buffer |
| 402 | * \p buf. In particular, \p buf may be destroyed or reused |
| 403 | * after this call returns. To avoid duplicating the CRT |
| 404 | * buffer (at the cost of stricter lifetime constraints), |
| 405 | * use mbedtls_x509_crt_parse_der_nocopy() instead. |
| 406 | * |
| 407 | * \return \c 0 if successful. |
| 408 | * \return A negative error code on failure. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 409 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 410 | int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain, |
| 411 | const unsigned char *buf, |
| 412 | size_t buflen); |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 413 | |
| 414 | /** |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 415 | * \brief The type of certificate extension callbacks. |
| 416 | * |
| 417 | * Callbacks of this type are passed to and used by the |
Nicola Di Lieto | fde98f7 | 2020-05-28 08:34:33 +0200 | [diff] [blame] | 418 | * mbedtls_x509_crt_parse_der_with_ext_cb() routine when |
Nicola Di Lieto | c84b1e6 | 2020-06-13 11:08:16 +0200 | [diff] [blame] | 419 | * it encounters either an unsupported extension or a |
| 420 | * "certificate policies" extension containing any |
| 421 | * unsupported certificate policies. |
Nicola Di Lieto | 511bc8c | 2020-06-23 00:15:28 +0200 | [diff] [blame] | 422 | * Future versions of the library may invoke the callback |
| 423 | * in other cases, if and when the need arises. |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 424 | * |
Nicola Di Lieto | 5659e7e | 2020-05-28 23:41:38 +0200 | [diff] [blame] | 425 | * \param p_ctx An opaque context passed to the callback. |
ndilieto | 6e24980 | 2020-05-28 06:17:38 +0000 | [diff] [blame] | 426 | * \param crt The certificate being parsed. |
| 427 | * \param oid The OID of the extension. |
| 428 | * \param critical Whether the extension is critical. |
Nicola Di Lieto | fae25a1 | 2020-05-28 08:55:08 +0200 | [diff] [blame] | 429 | * \param p Pointer to the start of the extension value |
ndilieto | 6e24980 | 2020-05-28 06:17:38 +0000 | [diff] [blame] | 430 | * (the content of the OCTET STRING). |
ndilieto | 6e24980 | 2020-05-28 06:17:38 +0000 | [diff] [blame] | 431 | * \param end End of extension value. |
Nicola Di Lieto | 565b52b | 2020-05-29 22:46:56 +0200 | [diff] [blame] | 432 | * |
| 433 | * \note The callback must fail and return a negative error code |
| 434 | * if it can not parse or does not support the extension. |
| 435 | * When the callback fails to parse a critical extension |
| 436 | * mbedtls_x509_crt_parse_der_with_ext_cb() also fails. |
| 437 | * When the callback fails to parse a non critical extension |
| 438 | * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips |
| 439 | * the extension and continues parsing. |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 440 | * |
| 441 | * \return \c 0 on success. |
| 442 | * \return A negative error code on failure. |
| 443 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | typedef int (*mbedtls_x509_crt_ext_cb_t)(void *p_ctx, |
| 445 | mbedtls_x509_crt const *crt, |
| 446 | mbedtls_x509_buf const *oid, |
| 447 | int critical, |
| 448 | const unsigned char *p, |
| 449 | const unsigned char *end); |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 450 | |
| 451 | /** |
Nicola Di Lieto | 4dbe567 | 2020-05-28 09:18:42 +0200 | [diff] [blame] | 452 | * \brief Parse a single DER formatted certificate and add it |
| 453 | * to the end of the provided chained list. |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 454 | * |
Nicola Di Lieto | 4dbe567 | 2020-05-28 09:18:42 +0200 | [diff] [blame] | 455 | * \param chain The pointer to the start of the CRT chain to attach to. |
| 456 | * When parsing the first CRT in a chain, this should point |
| 457 | * to an instance of ::mbedtls_x509_crt initialized through |
| 458 | * mbedtls_x509_crt_init(). |
| 459 | * \param buf The buffer holding the DER encoded certificate. |
| 460 | * \param buflen The size in Bytes of \p buf. |
| 461 | * \param make_copy When not zero this function makes an internal copy of the |
| 462 | * CRT buffer \p buf. In particular, \p buf may be destroyed |
| 463 | * or reused after this call returns. |
| 464 | * When zero this function avoids duplicating the CRT buffer |
| 465 | * by taking temporary ownership thereof until the CRT |
| 466 | * is destroyed (like mbedtls_x509_crt_parse_der_nocopy()) |
| 467 | * \param cb A callback invoked for every unsupported certificate |
| 468 | * extension. |
Nicola Di Lieto | 5659e7e | 2020-05-28 23:41:38 +0200 | [diff] [blame] | 469 | * \param p_ctx An opaque context passed to the callback. |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 470 | * |
Nicola Di Lieto | 4dbe567 | 2020-05-28 09:18:42 +0200 | [diff] [blame] | 471 | * \note This call is functionally equivalent to |
| 472 | * mbedtls_x509_crt_parse_der(), and/or |
| 473 | * mbedtls_x509_crt_parse_der_nocopy() |
| 474 | * but it calls the callback with every unsupported |
Nicola Di Lieto | c84b1e6 | 2020-06-13 11:08:16 +0200 | [diff] [blame] | 475 | * certificate extension and additionally the |
| 476 | * "certificate policies" extension if it contains any |
| 477 | * unsupported certificate policies. |
Nicola Di Lieto | 4dbe567 | 2020-05-28 09:18:42 +0200 | [diff] [blame] | 478 | * The callback must return a negative error code if it |
| 479 | * does not know how to handle such an extension. |
Nicola Di Lieto | 565b52b | 2020-05-29 22:46:56 +0200 | [diff] [blame] | 480 | * When the callback fails to parse a critical extension |
| 481 | * mbedtls_x509_crt_parse_der_with_ext_cb() also fails. |
| 482 | * When the callback fails to parse a non critical extension |
| 483 | * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips |
| 484 | * the extension and continues parsing. |
Nicola Di Lieto | 511bc8c | 2020-06-23 00:15:28 +0200 | [diff] [blame] | 485 | * Future versions of the library may invoke the callback |
| 486 | * in other cases, if and when the need arises. |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 487 | * |
Nicola Di Lieto | 4dbe567 | 2020-05-28 09:18:42 +0200 | [diff] [blame] | 488 | * \return \c 0 if successful. |
| 489 | * \return A negative error code on failure. |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 490 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain, |
| 492 | const unsigned char *buf, |
| 493 | size_t buflen, |
| 494 | int make_copy, |
| 495 | mbedtls_x509_crt_ext_cb_t cb, |
| 496 | void *p_ctx); |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 497 | |
| 498 | /** |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 499 | * \brief Parse a single DER formatted certificate and add it |
| 500 | * to the end of the provided chained list. This is a |
| 501 | * variant of mbedtls_x509_crt_parse_der() which takes |
| 502 | * temporary ownership of the CRT buffer until the CRT |
| 503 | * is destroyed. |
| 504 | * |
| 505 | * \param chain The pointer to the start of the CRT chain to attach to. |
| 506 | * When parsing the first CRT in a chain, this should point |
| 507 | * to an instance of ::mbedtls_x509_crt initialized through |
| 508 | * mbedtls_x509_crt_init(). |
| 509 | * \param buf The address of the readable buffer holding the DER encoded |
| 510 | * certificate to use. On success, this buffer must be |
Jan Bruckner | b33f6e5 | 2022-10-06 11:23:49 +0200 | [diff] [blame] | 511 | * retained and not be changed for the lifetime of the |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 512 | * CRT chain \p chain, that is, until \p chain is destroyed |
| 513 | * through a call to mbedtls_x509_crt_free(). |
| 514 | * \param buflen The size in Bytes of \p buf. |
| 515 | * |
| 516 | * \note This call is functionally equivalent to |
| 517 | * mbedtls_x509_crt_parse_der(), but it avoids creating a |
| 518 | * copy of the input buffer at the cost of stronger lifetime |
| 519 | * constraints. This is useful in constrained environments |
| 520 | * where duplication of the CRT cannot be tolerated. |
| 521 | * |
| 522 | * \return \c 0 if successful. |
| 523 | * \return A negative error code on failure. |
| 524 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain, |
| 526 | const unsigned char *buf, |
| 527 | size_t buflen); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 528 | |
| 529 | /** |
Hanno Becker | 89a9112 | 2018-08-23 16:14:00 +0100 | [diff] [blame] | 530 | * \brief Parse one DER-encoded or one or more concatenated PEM-encoded |
Hanno Becker | 65e619a | 2018-08-23 15:46:33 +0100 | [diff] [blame] | 531 | * certificates and add them to the chained list. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 532 | * |
Hanno Becker | e8658e2 | 2018-08-24 10:01:17 +0100 | [diff] [blame] | 533 | * For CRTs in PEM encoding, the function parses permissively: |
| 534 | * if at least one certificate can be parsed, the function |
Hanno Becker | 65e619a | 2018-08-23 15:46:33 +0100 | [diff] [blame] | 535 | * returns the number of certificates for which parsing failed |
| 536 | * (hence \c 0 if all certificates were parsed successfully). |
| 537 | * If no certificate could be parsed, the function returns |
| 538 | * the first (negative) error encountered during parsing. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 539 | * |
Hanno Becker | 65e619a | 2018-08-23 15:46:33 +0100 | [diff] [blame] | 540 | * PEM encoded certificates may be interleaved by other data |
| 541 | * such as human readable descriptions of their content, as |
| 542 | * long as the certificates are enclosed in the PEM specific |
| 543 | * '-----{BEGIN/END} CERTIFICATE-----' delimiters. |
| 544 | * |
| 545 | * \param chain The chain to which to add the parsed certificates. |
| 546 | * \param buf The buffer holding the certificate data in PEM or DER format. |
| 547 | * For certificates in PEM encoding, this may be a concatenation |
| 548 | * of multiple certificates; for DER encoding, the buffer must |
| 549 | * comprise exactly one certificate. |
| 550 | * \param buflen The size of \p buf, including the terminating \c NULL byte |
| 551 | * in case of PEM encoded data. |
| 552 | * |
| 553 | * \return \c 0 if all certificates were parsed successfully. |
| 554 | * \return The (positive) number of certificates that couldn't |
| 555 | * be parsed if parsing was partly successful (see above). |
Hanno Becker | 89a9112 | 2018-08-23 16:14:00 +0100 | [diff] [blame] | 556 | * \return A negative X509 or PEM error code otherwise. |
Hanno Becker | 65e619a | 2018-08-23 15:46:33 +0100 | [diff] [blame] | 557 | * |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 558 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 559 | 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] | 560 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 561 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 562 | /** |
| 563 | * \brief Load one or more certificates and add them |
| 564 | * to the chained list. Parses permissively. If some |
| 565 | * certificates can be parsed, the result is the number |
| 566 | * of failed certificates it encountered. If none complete |
| 567 | * correctly, the first error is returned. |
| 568 | * |
| 569 | * \param chain points to the start of the chain |
| 570 | * \param path filename to read the certificates from |
| 571 | * |
| 572 | * \return 0 if all certificates parsed successfully, a positive number |
| 573 | * if partly successful or a specific X509 or PEM error code |
| 574 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 575 | 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] | 576 | |
| 577 | /** |
| 578 | * \brief Load one or more certificate files from a path and add them |
| 579 | * to the chained list. Parses permissively. If some |
| 580 | * certificates can be parsed, the result is the number |
| 581 | * of failed certificates it encountered. If none complete |
| 582 | * correctly, the first error is returned. |
| 583 | * |
| 584 | * \param chain points to the start of the chain |
| 585 | * \param path directory / folder to read the certificate files from |
| 586 | * |
| 587 | * \return 0 if all certificates parsed successfully, a positive number |
| 588 | * if partly successful or a specific X509 or PEM error code |
| 589 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 590 | 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] | 591 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 592 | #endif /* MBEDTLS_FS_IO */ |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 593 | /** |
Ron Eldor | c8b5f3f | 2019-05-15 15:15:55 +0300 | [diff] [blame] | 594 | * \brief This function parses an item in the SubjectAlternativeNames |
| 595 | * extension. |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 596 | * |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 597 | * \param san_buf The buffer holding the raw data item of the subject |
| 598 | * alternative name. |
| 599 | * \param san The target structure to populate with the parsed presentation |
| 600 | * of the subject alternative name encoded in \p san_raw. |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 601 | * |
Ron Eldor | c8b5f3f | 2019-05-15 15:15:55 +0300 | [diff] [blame] | 602 | * \note Only "dnsName" and "otherName" of type hardware_module_name |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 603 | * as defined in RFC 4180 is supported. |
| 604 | * |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 605 | * \note This function should be called on a single raw data of |
Ron Eldor | cc45cd1 | 2019-05-15 10:20:09 +0300 | [diff] [blame] | 606 | * subject alternative name. For example, after successful |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 607 | * certificate parsing, one must iterate on every item in the |
Ron Eldor | c8b5f3f | 2019-05-15 15:15:55 +0300 | [diff] [blame] | 608 | * \p crt->subject_alt_names sequence, and pass it to |
| 609 | * this function. |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 610 | * |
Ron Eldor | c8b5f3f | 2019-05-15 15:15:55 +0300 | [diff] [blame] | 611 | * \warning The target structure contains pointers to the raw data of the |
Ron Eldor | cc45cd1 | 2019-05-15 10:20:09 +0300 | [diff] [blame] | 612 | * parsed certificate, and its lifetime is restricted by the |
| 613 | * lifetime of the certificate. |
| 614 | * |
Ron Eldor | 890819a | 2019-05-13 19:03:04 +0300 | [diff] [blame] | 615 | * \return \c 0 on success |
| 616 | * \return #MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE for an unsupported |
Ron Eldor | c8b5f3f | 2019-05-15 15:15:55 +0300 | [diff] [blame] | 617 | * SAN type. |
| 618 | * \return Another negative value for any other failure. |
Ron Eldor | b2dc3fa | 2019-03-21 13:40:13 +0200 | [diff] [blame] | 619 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 620 | int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf, |
| 621 | mbedtls_x509_subject_alternative_name *san); |
Peter Kolbus | 9a969b6 | 2018-12-11 13:55:56 -0600 | [diff] [blame] | 622 | |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 623 | #if !defined(MBEDTLS_X509_REMOVE_INFO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 624 | /** |
| 625 | * \brief Returns an informational string about the |
| 626 | * certificate. |
| 627 | * |
| 628 | * \param buf Buffer to write to |
| 629 | * \param size Maximum size of buffer |
| 630 | * \param prefix A line prefix |
| 631 | * \param crt The X509 certificate to represent |
| 632 | * |
Manuel Pégourié-Gonnard | e244f9f | 2015-06-23 12:10:45 +0200 | [diff] [blame] | 633 | * \return The length of the string written (not including the |
| 634 | * terminated nul byte), or a negative error code. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 635 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 636 | int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix, |
| 637 | const mbedtls_x509_crt *crt); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 638 | |
| 639 | /** |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 640 | * \brief Returns an informational string about the |
| 641 | * verification status of a certificate. |
| 642 | * |
| 643 | * \param buf Buffer to write to |
| 644 | * \param size Maximum size of buffer |
| 645 | * \param prefix A line prefix |
| 646 | * \param flags Verification flags created by mbedtls_x509_crt_verify() |
| 647 | * |
Manuel Pégourié-Gonnard | e244f9f | 2015-06-23 12:10:45 +0200 | [diff] [blame] | 648 | * \return The length of the string written (not including the |
| 649 | * terminated nul byte), or a negative error code. |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 650 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 651 | int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix, |
| 652 | uint32_t flags); |
Hanno Becker | 88c2bf3 | 2019-06-14 17:21:24 +0100 | [diff] [blame] | 653 | #endif /* !MBEDTLS_X509_REMOVE_INFO */ |
Manuel Pégourié-Gonnard | 39a183a | 2015-04-17 16:14:32 +0200 | [diff] [blame] | 654 | |
| 655 | /** |
Hanno Becker | 902451d | 2019-03-27 11:48:40 +0000 | [diff] [blame] | 656 | * \brief Verify a chain of certificates. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 657 | * |
| 658 | * The verify callback is a user-supplied callback that |
| 659 | * can clear / modify / add flags for a certificate. If set, |
| 660 | * the verification callback is called for each |
| 661 | * certificate in the chain (from the trust-ca down to the |
| 662 | * presented crt). The parameters for the callback are: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 663 | * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 664 | * int *flags). With the flags representing current flags for |
| 665 | * that specific certificate and the certificate depth from |
| 666 | * the bottom (Peer cert depth = 0). |
| 667 | * |
| 668 | * All flags left after returning from the callback |
| 669 | * are also returned to the application. The function should |
Manuel Pégourié-Gonnard | 31458a1 | 2017-06-26 10:11:49 +0200 | [diff] [blame] | 670 | * return 0 for anything (including invalid certificates) |
| 671 | * other than fatal error, as a non-zero return code |
| 672 | * immediately aborts the verification process. For fatal |
| 673 | * errors, a specific error code should be used (different |
| 674 | * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not |
| 675 | * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR |
| 676 | * can be used if no better code is available. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 677 | * |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 678 | * \note In case verification failed, the results can be displayed |
| 679 | * using \c mbedtls_x509_crt_verify_info() |
| 680 | * |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 681 | * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the |
| 682 | * default security profile. |
| 683 | * |
Manuel Pégourié-Gonnard | eeef947 | 2016-02-22 11:36:55 +0100 | [diff] [blame] | 684 | * \note It is your responsibility to provide up-to-date CRLs for |
| 685 | * all trusted CAs. If no CRL is provided for the CA that was |
| 686 | * used to sign the certificate, CRL verification is skipped |
| 687 | * silently, that is *without* setting any flag. |
| 688 | * |
Manuel Pégourié-Gonnard | 08eacec | 2017-10-18 14:20:24 +0200 | [diff] [blame] | 689 | * \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] | 690 | * (1) those of trusted root CAs, so that certificates |
| 691 | * chaining up to those CAs will be trusted, and (2) |
| 692 | * self-signed end-entity certificates to be trusted (for |
| 693 | * specific peers you know) - in that case, the self-signed |
Manuel Pégourié-Gonnard | 562df40 | 2017-08-08 18:09:14 +0200 | [diff] [blame] | 694 | * certificate doesn't need to have the CA bit set. |
Manuel Pégourié-Gonnard | a4a206e | 2017-06-21 09:35:44 +0200 | [diff] [blame] | 695 | * |
Hanno Becker | 902451d | 2019-03-27 11:48:40 +0000 | [diff] [blame] | 696 | * \param crt The certificate chain to be verified. |
| 697 | * \param trust_ca The list of trusted CAs. |
| 698 | * \param ca_crl The list of CRLs for trusted CAs. |
Manuel Pégourié-Gonnard | f58e5cc | 2020-07-24 10:31:37 +0200 | [diff] [blame] | 699 | * \param cn The expected Common Name. This will be checked to be |
| 700 | * present in the certificate's subjectAltNames extension or, |
| 701 | * if this extension is absent, as a CN component in its |
| 702 | * Subject name. Currently only DNS names are supported. This |
| 703 | * may be \c NULL if the CN need not be verified. |
Hanno Becker | 902451d | 2019-03-27 11:48:40 +0000 | [diff] [blame] | 704 | * \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] | 705 | * If the verification couldn't be completed, the flag value is |
| 706 | * set to (uint32_t) -1. |
Hanno Becker | 902451d | 2019-03-27 11:48:40 +0000 | [diff] [blame] | 707 | * \param f_vrfy The verification callback to use. See the documentation |
| 708 | * of mbedtls_x509_crt_verify() for more information. |
| 709 | * \param p_vrfy The context to be passed to \p f_vrfy. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 710 | * |
Hanno Becker | 902451d | 2019-03-27 11:48:40 +0000 | [diff] [blame] | 711 | * \return \c 0 if the chain is valid with respect to the |
| 712 | * passed CN, CAs, CRLs and security profile. |
| 713 | * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the |
| 714 | * certificate chain verification failed. In this case, |
| 715 | * \c *flags will have one or more |
| 716 | * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX |
| 717 | * flags set. |
| 718 | * \return Another negative error code in case of a fatal error |
| 719 | * encountered during the verification process. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 720 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 721 | int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt, |
| 722 | mbedtls_x509_crt *trust_ca, |
| 723 | mbedtls_x509_crl *ca_crl, |
| 724 | const char *cn, uint32_t *flags, |
| 725 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 726 | void *p_vrfy); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 727 | |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 728 | /** |
Hanno Becker | 902451d | 2019-03-27 11:48:40 +0000 | [diff] [blame] | 729 | * \brief Verify a chain of certificates with respect to |
| 730 | * a configurable security profile. |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 731 | * |
| 732 | * \note Same as \c mbedtls_x509_crt_verify(), but with explicit |
| 733 | * security profile. |
| 734 | * |
Manuel Pégourié-Gonnard | 27716cc | 2015-06-17 11:49:39 +0200 | [diff] [blame] | 735 | * \note The restrictions on keys (RSA minimum size, allowed curves |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 736 | * for ECDSA) apply to all certificates: trusted root, |
| 737 | * intermediate CAs if any, and end entity certificate. |
Manuel Pégourié-Gonnard | 27716cc | 2015-06-17 11:49:39 +0200 | [diff] [blame] | 738 | * |
Hanno Becker | 902451d | 2019-03-27 11:48:40 +0000 | [diff] [blame] | 739 | * \param crt The certificate chain to be verified. |
| 740 | * \param trust_ca The list of trusted CAs. |
| 741 | * \param ca_crl The list of CRLs for trusted CAs. |
| 742 | * \param profile The security profile to use for the verification. |
| 743 | * \param cn The expected Common Name. This may be \c NULL if the |
| 744 | * CN need not be verified. |
| 745 | * \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] | 746 | * If the verification couldn't be completed, the flag value is |
| 747 | * set to (uint32_t) -1. |
Hanno Becker | 902451d | 2019-03-27 11:48:40 +0000 | [diff] [blame] | 748 | * \param f_vrfy The verification callback to use. See the documentation |
| 749 | * of mbedtls_x509_crt_verify() for more information. |
| 750 | * \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] | 751 | * |
Hanno Becker | 902451d | 2019-03-27 11:48:40 +0000 | [diff] [blame] | 752 | * \return \c 0 if the chain is valid with respect to the |
| 753 | * passed CN, CAs, CRLs and security profile. |
| 754 | * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the |
| 755 | * certificate chain verification failed. In this case, |
| 756 | * \c *flags will have one or more |
| 757 | * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX |
| 758 | * flags set. |
| 759 | * \return Another negative error code in case of a fatal error |
| 760 | * encountered during the verification process. |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 761 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 762 | int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt, |
| 763 | mbedtls_x509_crt *trust_ca, |
| 764 | mbedtls_x509_crl *ca_crl, |
| 765 | const mbedtls_x509_crt_profile *profile, |
| 766 | const char *cn, uint32_t *flags, |
| 767 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 768 | void *p_vrfy); |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 769 | |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 770 | /** |
| 771 | * \brief Restartable version of \c mbedtls_crt_verify_with_profile() |
| 772 | * |
| 773 | * \note Performs the same job as \c mbedtls_crt_verify_with_profile() |
| 774 | * but can return early and restart according to the limit |
| 775 | * set with \c mbedtls_ecp_set_max_ops() to reduce blocking. |
| 776 | * |
Hanno Becker | 902451d | 2019-03-27 11:48:40 +0000 | [diff] [blame] | 777 | * \param crt The certificate chain to be verified. |
| 778 | * \param trust_ca The list of trusted CAs. |
| 779 | * \param ca_crl The list of CRLs for trusted CAs. |
| 780 | * \param profile The security profile to use for the verification. |
| 781 | * \param cn The expected Common Name. This may be \c NULL if the |
| 782 | * CN need not be verified. |
| 783 | * \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] | 784 | * If the verification couldn't be completed, the flag value is |
| 785 | * set to (uint32_t) -1. |
Hanno Becker | 902451d | 2019-03-27 11:48:40 +0000 | [diff] [blame] | 786 | * \param f_vrfy The verification callback to use. See the documentation |
| 787 | * of mbedtls_x509_crt_verify() for more information. |
| 788 | * \param p_vrfy The context to be passed to \p f_vrfy. |
| 789 | * \param rs_ctx The restart context to use. This may be set to \c NULL |
| 790 | * to disable restartable ECC. |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 791 | * |
| 792 | * \return See \c mbedtls_crt_verify_with_profile(), or |
Manuel Pégourié-Gonnard | 12e4a8b | 2018-09-12 10:55:15 +0200 | [diff] [blame] | 793 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 794 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
| 795 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 796 | int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt, |
| 797 | mbedtls_x509_crt *trust_ca, |
| 798 | mbedtls_x509_crl *ca_crl, |
| 799 | const mbedtls_x509_crt_profile *profile, |
| 800 | const char *cn, uint32_t *flags, |
| 801 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 802 | void *p_vrfy, |
| 803 | mbedtls_x509_crt_restart_ctx *rs_ctx); |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 804 | |
Hanno Becker | 5c8df78 | 2019-03-27 11:01:17 +0000 | [diff] [blame] | 805 | /** |
| 806 | * \brief The type of trusted certificate callbacks. |
| 807 | * |
| 808 | * Callbacks of this type are passed to and used by the CRT |
Jarno Lamsa | 31d9db6 | 2019-04-01 14:33:49 +0300 | [diff] [blame] | 809 | * verification routine mbedtls_x509_crt_verify_with_ca_cb() |
Hanno Becker | 5c8df78 | 2019-03-27 11:01:17 +0000 | [diff] [blame] | 810 | * when looking for trusted signers of a given certificate. |
| 811 | * |
| 812 | * On success, the callback returns a list of trusted |
| 813 | * certificates to be considered as potential signers |
| 814 | * for the input certificate. |
| 815 | * |
| 816 | * \param p_ctx An opaque context passed to the callback. |
| 817 | * \param child The certificate for which to search a potential signer. |
Jarno Lamsa | f49fedc | 2019-04-01 14:58:30 +0300 | [diff] [blame] | 818 | * This will point to a readable certificate. |
Hanno Becker | 5c8df78 | 2019-03-27 11:01:17 +0000 | [diff] [blame] | 819 | * \param candidate_cas The address at which to store the address of the first |
| 820 | * entry in the generated linked list of candidate signers. |
Jarno Lamsa | f49fedc | 2019-04-01 14:58:30 +0300 | [diff] [blame] | 821 | * This will not be \c NULL. |
Hanno Becker | 5c8df78 | 2019-03-27 11:01:17 +0000 | [diff] [blame] | 822 | * |
| 823 | * \note The callback must only return a non-zero value on a |
| 824 | * fatal error. If, in contrast, the search for a potential |
| 825 | * signer completes without a single candidate, the |
Jarno Lamsa | f49fedc | 2019-04-01 14:58:30 +0300 | [diff] [blame] | 826 | * callback must return \c 0 and set \c *candidate_cas |
Hanno Becker | 5c8df78 | 2019-03-27 11:01:17 +0000 | [diff] [blame] | 827 | * to \c NULL. |
| 828 | * |
| 829 | * \return \c 0 on success. In this case, \c *candidate_cas points |
| 830 | * to a heap-allocated linked list of instances of |
| 831 | * ::mbedtls_x509_crt, and ownership of this list is passed |
| 832 | * to the caller. |
| 833 | * \return A negative error code on failure. |
| 834 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 835 | typedef int (*mbedtls_x509_crt_ca_cb_t)(void *p_ctx, |
| 836 | mbedtls_x509_crt const *child, |
| 837 | mbedtls_x509_crt **candidate_cas); |
Hanno Becker | e15dae7 | 2019-03-28 13:55:38 +0000 | [diff] [blame] | 838 | |
| 839 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Hanno Becker | 5c8df78 | 2019-03-27 11:01:17 +0000 | [diff] [blame] | 840 | /** |
| 841 | * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which |
| 842 | * uses a callback to acquire the list of trusted CA |
| 843 | * certificates. |
| 844 | * |
| 845 | * \param crt The certificate chain to be verified. |
| 846 | * \param f_ca_cb The callback to be used to query for potential signers |
| 847 | * of a given child certificate. See the documentation of |
| 848 | * ::mbedtls_x509_crt_ca_cb_t for more information. |
| 849 | * \param p_ca_cb The opaque context to be passed to \p f_ca_cb. |
| 850 | * \param profile The security profile for the verification. |
| 851 | * \param cn The expected Common Name. This may be \c NULL if the |
| 852 | * CN need not be verified. |
| 853 | * \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] | 854 | * If the verification couldn't be completed, the flag value is |
| 855 | * set to (uint32_t) -1. |
Hanno Becker | 5c8df78 | 2019-03-27 11:01:17 +0000 | [diff] [blame] | 856 | * \param f_vrfy The verification callback to use. See the documentation |
| 857 | * of mbedtls_x509_crt_verify() for more information. |
| 858 | * \param p_vrfy The context to be passed to \p f_vrfy. |
| 859 | * |
| 860 | * \return See \c mbedtls_crt_verify_with_profile(). |
| 861 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 862 | int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt, |
| 863 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
| 864 | void *p_ca_cb, |
| 865 | const mbedtls_x509_crt_profile *profile, |
| 866 | const char *cn, uint32_t *flags, |
| 867 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 868 | void *p_vrfy); |
Hanno Becker | 5c8df78 | 2019-03-27 11:01:17 +0000 | [diff] [blame] | 869 | |
| 870 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 871 | |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 872 | /** |
| 873 | * \brief Check usage of certificate against keyUsage extension. |
| 874 | * |
| 875 | * \param crt Leaf certificate used. |
Manuel Pégourié-Gonnard | 655a964 | 2015-06-23 10:48:44 +0200 | [diff] [blame] | 876 | * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
| 877 | * before using the certificate to perform an RSA key |
| 878 | * exchange). |
| 879 | * |
| 880 | * \note Except for decipherOnly and encipherOnly, a bit set in the |
| 881 | * usage argument means this bit MUST be set in the |
| 882 | * certificate. For decipherOnly and encipherOnly, it means |
| 883 | * that bit MAY be set. |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 884 | * |
| 885 | * \return 0 is these uses of the certificate are allowed, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 886 | * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension |
Manuel Pégourié-Gonnard | 655a964 | 2015-06-23 10:48:44 +0200 | [diff] [blame] | 887 | * is present but does not match the usage argument. |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 888 | * |
| 889 | * \note You should only call this function on leaf certificates, on |
| 890 | * (intermediate) CAs the keyUsage extension is automatically |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 891 | * checked by \c mbedtls_x509_crt_verify(). |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 892 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 893 | int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt, |
| 894 | unsigned int usage); |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 895 | |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 896 | /** |
Andres Amaya Garcia | 5a6da63 | 2017-11-14 21:40:51 +0000 | [diff] [blame] | 897 | * \brief Check usage of certificate against extendedKeyUsage. |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 898 | * |
Andres Amaya Garcia | 5a6da63 | 2017-11-14 21:40:51 +0000 | [diff] [blame] | 899 | * \param crt Leaf certificate used. |
| 900 | * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or |
| 901 | * MBEDTLS_OID_CLIENT_AUTH). |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 902 | * \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] | 903 | * |
Andres Amaya Garcia | 5a6da63 | 2017-11-14 21:40:51 +0000 | [diff] [blame] | 904 | * \return 0 if this use of the certificate is allowed, |
| 905 | * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not. |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 906 | * |
Andres Amaya Garcia | 5a6da63 | 2017-11-14 21:40:51 +0000 | [diff] [blame] | 907 | * \note Usually only makes sense on leaf certificates. |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 908 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 909 | int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt, |
| 910 | const char *usage_oid, |
| 911 | size_t usage_len); |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 912 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 913 | #if defined(MBEDTLS_X509_CRL_PARSE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 914 | /** |
Manuel Pégourié-Gonnard | cbf3ef3 | 2013-09-23 12:20:02 +0200 | [diff] [blame] | 915 | * \brief Verify the certificate revocation status |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 916 | * |
| 917 | * \param crt a certificate to be verified |
| 918 | * \param crl the CRL to verify against |
| 919 | * |
| 920 | * \return 1 if the certificate is revoked, 0 otherwise |
| 921 | * |
| 922 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 923 | 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] | 924 | #endif /* MBEDTLS_X509_CRL_PARSE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 925 | |
| 926 | /** |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 927 | * \brief Initialize a certificate (chain) |
| 928 | * |
| 929 | * \param crt Certificate chain to initialize |
| 930 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 931 | void mbedtls_x509_crt_init(mbedtls_x509_crt *crt); |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 932 | |
| 933 | /** |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 934 | * \brief Unallocate all certificate data |
| 935 | * |
| 936 | * \param crt Certificate chain to free |
| 937 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 938 | void mbedtls_x509_crt_free(mbedtls_x509_crt *crt); |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 939 | |
| 940 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 941 | /** |
| 942 | * \brief Initialize a restart context |
| 943 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 944 | void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx); |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 945 | |
| 946 | /** |
| 947 | * \brief Free the components of a restart context |
| 948 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 949 | void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx); |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 950 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 951 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 952 | |
Thomas Daubney | a5f39e0 | 2022-06-06 15:42:32 +0100 | [diff] [blame] | 953 | /** |
| 954 | * \brief Query certificate for given extension type |
| 955 | * |
| 956 | * \param[in] ctx Certificate context to be queried, must not be \c NULL |
| 957 | * \param ext_type Extension type being queried for, must be a valid |
| 958 | * extension type. Must be one of the MBEDTLS_X509_EXT_XXX |
| 959 | * values |
| 960 | * |
| 961 | * \return 0 if the given extension type is not present, |
| 962 | * non-zero otherwise |
| 963 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 964 | static inline int mbedtls_x509_crt_has_ext_type(const mbedtls_x509_crt *ctx, |
| 965 | int ext_type) |
Thomas Daubney | a5f39e0 | 2022-06-06 15:42:32 +0100 | [diff] [blame] | 966 | { |
| 967 | return ctx->MBEDTLS_PRIVATE(ext_types) & ext_type; |
| 968 | } |
| 969 | |
Andrzej Kurek | 38d4fdd | 2021-12-28 16:22:52 +0100 | [diff] [blame] | 970 | /** \} name Structures and functions for parsing and writing X.509 certificates */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 972 | #if defined(MBEDTLS_X509_CRT_WRITE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 973 | /** |
| 974 | * \brief Initialize a CRT writing context |
| 975 | * |
| 976 | * \param ctx CRT context to initialize |
| 977 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 978 | void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 979 | |
| 980 | /** |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 981 | * \brief Set the version for a Certificate |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 982 | * Default: MBEDTLS_X509_CRT_VERSION_3 |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 983 | * |
| 984 | * \param ctx CRT context to use |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 985 | * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or |
| 986 | * MBEDTLS_X509_CRT_VERSION_3) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 987 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 988 | void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx, int version); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 989 | |
Valerio Setti | 5d164c4 | 2023-01-09 12:19:40 +0100 | [diff] [blame] | 990 | #if defined(MBEDTLS_BIGNUM_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 991 | /** |
| 992 | * \brief Set the serial number for a Certificate. |
| 993 | * |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 994 | * \deprecated This function is deprecated and will be removed in a |
| 995 | * future version of the library. Please use |
| 996 | * mbedtls_x509write_crt_set_serial_new() instead. |
| 997 | * |
| 998 | * \note Even though the MBEDTLS_BIGNUM_C guard looks redundant since |
| 999 | * X509 depends on PK and PK depends on BIGNUM, this emphasizes |
| 1000 | * a direct dependency between X509 and BIGNUM which is going |
| 1001 | * to be deprecated in the future. |
| 1002 | * |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1003 | * \param ctx CRT context to use |
| 1004 | * \param serial serial number to set |
| 1005 | * |
| 1006 | * \return 0 if successful |
| 1007 | */ |
Valerio Setti | 5d164c4 | 2023-01-09 12:19:40 +0100 | [diff] [blame] | 1008 | int MBEDTLS_DEPRECATED mbedtls_x509write_crt_set_serial( |
| 1009 | mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial); |
| 1010 | #endif // MBEDTLS_BIGNUM_C && !MBEDTLS_DEPRECATED_REMOVED |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 1011 | |
| 1012 | /** |
| 1013 | * \brief Set the serial number for a Certificate. |
| 1014 | * |
Valerio Setti | 746def5 | 2023-01-10 11:46:34 +0100 | [diff] [blame^] | 1015 | * \param ctx CRT context to use |
| 1016 | * \param serial A raw array of bytes containing the serial number in big |
| 1017 | * endian format |
| 1018 | * \param serial_len Length of the previous input buffer buffer |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 1019 | * |
| 1020 | * \return 0 if successful, or |
Valerio Setti | acf12fb | 2023-01-09 17:19:26 +0100 | [diff] [blame] | 1021 | * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the provided input buffer |
| 1022 | * is too big (longer than MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 1023 | */ |
| 1024 | int mbedtls_x509write_crt_set_serial_new(mbedtls_x509write_cert *ctx, |
Valerio Setti | 746def5 | 2023-01-10 11:46:34 +0100 | [diff] [blame^] | 1025 | unsigned char *serial, size_t serial_len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1026 | |
| 1027 | /** |
| 1028 | * \brief Set the validity period for a Certificate |
| 1029 | * Timestamps should be in string format for UTC timezone |
| 1030 | * i.e. "YYYYMMDDhhmmss" |
| 1031 | * e.g. "20131231235959" for December 31st 2013 |
| 1032 | * at 23:59:59 |
| 1033 | * |
| 1034 | * \param ctx CRT context to use |
| 1035 | * \param not_before not_before timestamp |
| 1036 | * \param not_after not_after timestamp |
| 1037 | * |
| 1038 | * \return 0 if timestamp was parsed successfully, or |
| 1039 | * a specific error code |
| 1040 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1041 | int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx, const char *not_before, |
| 1042 | const char *not_after); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1043 | |
| 1044 | /** |
| 1045 | * \brief Set the issuer name for a Certificate |
| 1046 | * Issuer names should contain a comma-separated list |
| 1047 | * of OID types and values: |
Manuel Pégourié-Gonnard | b4fe3cb | 2015-01-22 16:11:05 +0000 | [diff] [blame] | 1048 | * e.g. "C=UK,O=ARM,CN=mbed TLS CA" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1049 | * |
| 1050 | * \param ctx CRT context to use |
| 1051 | * \param issuer_name issuer name to set |
| 1052 | * |
| 1053 | * \return 0 if issuer name was parsed successfully, or |
| 1054 | * a specific error code |
| 1055 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1056 | int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx, |
| 1057 | const char *issuer_name); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1058 | |
| 1059 | /** |
| 1060 | * \brief Set the subject name for a Certificate |
| 1061 | * Subject names should contain a comma-separated list |
| 1062 | * of OID types and values: |
Manuel Pégourié-Gonnard | b4fe3cb | 2015-01-22 16:11:05 +0000 | [diff] [blame] | 1063 | * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1064 | * |
| 1065 | * \param ctx CRT context to use |
| 1066 | * \param subject_name subject name to set |
| 1067 | * |
| 1068 | * \return 0 if subject name was parsed successfully, or |
| 1069 | * a specific error code |
| 1070 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1071 | int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx, |
| 1072 | const char *subject_name); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1073 | |
| 1074 | /** |
| 1075 | * \brief Set the subject public key for the certificate |
| 1076 | * |
| 1077 | * \param ctx CRT context to use |
| 1078 | * \param key public key to include |
| 1079 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1080 | 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] | 1081 | |
| 1082 | /** |
| 1083 | * \brief Set the issuer key used for signing the certificate |
| 1084 | * |
| 1085 | * \param ctx CRT context to use |
| 1086 | * \param key private key to sign with |
| 1087 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1088 | 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] | 1089 | |
| 1090 | /** |
| 1091 | * \brief Set the MD algorithm to use for the signature |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1092 | * (e.g. MBEDTLS_MD_SHA1) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1093 | * |
| 1094 | * \param ctx CRT context to use |
Paul Bakker | a36d23e | 2013-12-30 17:57:27 +0100 | [diff] [blame] | 1095 | * \param md_alg MD algorithm to use |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1096 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1097 | 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] | 1098 | |
| 1099 | /** |
| 1100 | * \brief Generic function to add to or replace an extension in the |
| 1101 | * CRT |
| 1102 | * |
| 1103 | * \param ctx CRT context to use |
| 1104 | * \param oid OID of the extension |
| 1105 | * \param oid_len length of the OID |
| 1106 | * \param critical if the extension is critical (per the RFC's definition) |
| 1107 | * \param val value of the extension OCTET STRING |
| 1108 | * \param val_len length of the value data |
| 1109 | * |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1110 | * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1111 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1112 | int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx, |
| 1113 | const char *oid, size_t oid_len, |
| 1114 | int critical, |
| 1115 | const unsigned char *val, size_t val_len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1116 | |
| 1117 | /** |
| 1118 | * \brief Set the basicConstraints extension for a CRT |
| 1119 | * |
| 1120 | * \param ctx CRT context to use |
| 1121 | * \param is_ca is this a CA certificate |
| 1122 | * \param max_pathlen maximum length of certificate chains below this |
| 1123 | * certificate (only for CA certificates, -1 is |
Tom Cosgrove | 1e21144 | 2022-05-26 11:51:00 +0100 | [diff] [blame] | 1124 | * unlimited) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1125 | * |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1126 | * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1127 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1128 | int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx, |
| 1129 | int is_ca, int max_pathlen); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1130 | |
Manuel Pégourié-Gonnard | e896705 | 2022-09-15 11:41:16 +0200 | [diff] [blame] | 1131 | #if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_LOWLEVEL_OR_PSA) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1132 | /** |
| 1133 | * \brief Set the subjectKeyIdentifier extension for a CRT |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1134 | * Requires that mbedtls_x509write_crt_set_subject_key() has been |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1135 | * called before |
| 1136 | * |
| 1137 | * \param ctx CRT context to use |
| 1138 | * |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1139 | * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1140 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1141 | int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1142 | |
| 1143 | /** |
| 1144 | * \brief Set the authorityKeyIdentifier extension for a CRT |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1145 | * Requires that mbedtls_x509write_crt_set_issuer_key() has been |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1146 | * called before |
| 1147 | * |
| 1148 | * \param ctx CRT context to use |
| 1149 | * |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1150 | * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1151 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1152 | int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx); |
Manuel Pégourié-Gonnard | e896705 | 2022-09-15 11:41:16 +0200 | [diff] [blame] | 1153 | #endif /* MBEDTLS_HAS_ALG_SHA_1_VIA_LOWLEVEL_OR_PSA */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1154 | |
| 1155 | /** |
| 1156 | * \brief Set the Key Usage Extension flags |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1157 | * (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] | 1158 | * |
| 1159 | * \param ctx CRT context to use |
| 1160 | * \param key_usage key usage flags to set |
| 1161 | * |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1162 | * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1163 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1164 | int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx, |
| 1165 | unsigned int key_usage); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1166 | |
| 1167 | /** |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 1168 | * \brief Set the Extended Key Usage Extension |
| 1169 | * (e.g. MBEDTLS_OID_SERVER_AUTH) |
| 1170 | * |
| 1171 | * \param ctx CRT context to use |
| 1172 | * \param exts extended key usage extensions to set, a sequence of |
| 1173 | * MBEDTLS_ASN1_OID objects |
| 1174 | * |
| 1175 | * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED |
| 1176 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1177 | int mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert *ctx, |
| 1178 | const mbedtls_asn1_sequence *exts); |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 1179 | |
| 1180 | /** |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1181 | * \brief Set the Netscape Cert Type flags |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1182 | * (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] | 1183 | * |
| 1184 | * \param ctx CRT context to use |
| 1185 | * \param ns_cert_type Netscape Cert Type flags to set |
| 1186 | * |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1187 | * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1188 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1189 | int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx, |
| 1190 | unsigned char ns_cert_type); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1191 | |
| 1192 | /** |
| 1193 | * \brief Free the contents of a CRT write context |
| 1194 | * |
| 1195 | * \param ctx CRT context to free |
| 1196 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1197 | void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1198 | |
| 1199 | /** |
| 1200 | * \brief Write a built up certificate to a X509 DER structure |
| 1201 | * Note: data is written at the end of the buffer! Use the |
| 1202 | * return value to determine where you should start |
| 1203 | * using the buffer |
| 1204 | * |
Paul Bakker | a36d23e | 2013-12-30 17:57:27 +0100 | [diff] [blame] | 1205 | * \param ctx certificate to write away |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1206 | * \param buf buffer to write to |
| 1207 | * \param size size of the buffer |
Manuel Pégourié-Gonnard | c305b72 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 1208 | * \param f_rng RNG function. This must not be \c NULL. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1209 | * \param p_rng RNG parameter |
| 1210 | * |
| 1211 | * \return length of data written if successful, or a specific |
| 1212 | * error code |
| 1213 | * |
Manuel Pégourié-Gonnard | c305b72 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 1214 | * \note \p f_rng is used for the signature operation. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1215 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1216 | int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size, |
| 1217 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1218 | void *p_rng); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1219 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1220 | #if defined(MBEDTLS_PEM_WRITE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1221 | /** |
| 1222 | * \brief Write a built up certificate to a X509 PEM string |
| 1223 | * |
Paul Bakker | a36d23e | 2013-12-30 17:57:27 +0100 | [diff] [blame] | 1224 | * \param ctx certificate to write away |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1225 | * \param buf buffer to write to |
| 1226 | * \param size size of the buffer |
Manuel Pégourié-Gonnard | c305b72 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 1227 | * \param f_rng RNG function. This must not be \c NULL. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1228 | * \param p_rng RNG parameter |
| 1229 | * |
Manuel Pégourié-Gonnard | 81abefd | 2015-05-29 12:53:47 +0200 | [diff] [blame] | 1230 | * \return 0 if successful, or a specific error code |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1231 | * |
Manuel Pégourié-Gonnard | c305b72 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 1232 | * \note \p f_rng is used for the signature operation. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1233 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1234 | int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size, |
| 1235 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1236 | void *p_rng); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1237 | #endif /* MBEDTLS_PEM_WRITE_C */ |
| 1238 | #endif /* MBEDTLS_X509_CRT_WRITE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1239 | |
Andrzej Kurek | a0defed | 2021-12-30 12:33:31 +0100 | [diff] [blame] | 1240 | /** \} addtogroup x509_module */ |
| 1241 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1242 | #ifdef __cplusplus |
| 1243 | } |
| 1244 | #endif |
| 1245 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1246 | #endif /* mbedtls_x509_crt.h */ |