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