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