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