Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
x509.h
00001 /** 00002 * \file x509.h 00003 * 00004 * \brief X.509 generic defines and structures 00005 */ 00006 /* 00007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 00008 * SPDX-License-Identifier: Apache-2.0 00009 * 00010 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00011 * not use this file except in compliance with the License. 00012 * You may obtain a copy of the License at 00013 * 00014 * http://www.apache.org/licenses/LICENSE-2.0 00015 * 00016 * Unless required by applicable law or agreed to in writing, software 00017 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00018 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 * See the License for the specific language governing permissions and 00020 * limitations under the License. 00021 * 00022 * This file is part of mbed TLS (https://tls.mbed.org) 00023 */ 00024 #ifndef MBEDTLS_X509_H 00025 #define MBEDTLS_X509_H 00026 00027 #if !defined(MBEDTLS_CONFIG_FILE) 00028 #include "config.h" 00029 #else 00030 #include MBEDTLS_CONFIG_FILE 00031 #endif 00032 00033 #include "asn1.h" 00034 #include "pk.h" 00035 00036 #if defined(MBEDTLS_RSA_C) 00037 #include "rsa.h" 00038 #endif 00039 00040 /** 00041 * \addtogroup x509_module 00042 * \{ 00043 */ 00044 00045 #if !defined(MBEDTLS_X509_MAX_INTERMEDIATE_CA) 00046 /** 00047 * Maximum number of intermediate CAs in a verification chain. 00048 * That is, maximum length of the chain, excluding the end-entity certificate 00049 * and the trusted root certificate. 00050 * 00051 * Set this to a low value to prevent an adversary from making you waste 00052 * resources verifying an overlong certificate chain. 00053 */ 00054 #define MBEDTLS_X509_MAX_INTERMEDIATE_CA 8 00055 #endif 00056 00057 /** 00058 * \name X509 Error codes 00059 * \{ 00060 */ 00061 #define MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE -0x2080 /**< Unavailable feature, e.g. RSA hashing/encryption combination. */ 00062 #define MBEDTLS_ERR_X509_UNKNOWN_OID -0x2100 /**< Requested OID is unknown. */ 00063 #define MBEDTLS_ERR_X509_INVALID_FORMAT -0x2180 /**< The CRT/CRL/CSR format is invalid, e.g. different type expected. */ 00064 #define MBEDTLS_ERR_X509_INVALID_VERSION -0x2200 /**< The CRT/CRL/CSR version element is invalid. */ 00065 #define MBEDTLS_ERR_X509_INVALID_SERIAL -0x2280 /**< The serial tag or value is invalid. */ 00066 #define MBEDTLS_ERR_X509_INVALID_ALG -0x2300 /**< The algorithm tag or value is invalid. */ 00067 #define MBEDTLS_ERR_X509_INVALID_NAME -0x2380 /**< The name tag or value is invalid. */ 00068 #define MBEDTLS_ERR_X509_INVALID_DATE -0x2400 /**< The date tag or value is invalid. */ 00069 #define MBEDTLS_ERR_X509_INVALID_SIGNATURE -0x2480 /**< The signature tag or value invalid. */ 00070 #define MBEDTLS_ERR_X509_INVALID_EXTENSIONS -0x2500 /**< The extension tag or value is invalid. */ 00071 #define MBEDTLS_ERR_X509_UNKNOWN_VERSION -0x2580 /**< CRT/CRL/CSR has an unsupported version number. */ 00072 #define MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG -0x2600 /**< Signature algorithm (oid) is unsupported. */ 00073 #define MBEDTLS_ERR_X509_SIG_MISMATCH -0x2680 /**< Signature algorithms do not match. (see \c ::mbedtls_x509_crt sig_oid) */ 00074 #define MBEDTLS_ERR_X509_CERT_VERIFY_FAILED -0x2700 /**< Certificate verification failed, e.g. CRL, CA or signature check failed. */ 00075 #define MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT -0x2780 /**< Format not recognized as DER or PEM. */ 00076 #define MBEDTLS_ERR_X509_BAD_INPUT_DATA -0x2800 /**< Input invalid. */ 00077 #define MBEDTLS_ERR_X509_ALLOC_FAILED -0x2880 /**< Allocation of memory failed. */ 00078 #define MBEDTLS_ERR_X509_FILE_IO_ERROR -0x2900 /**< Read/write of file failed. */ 00079 #define MBEDTLS_ERR_X509_BUFFER_TOO_SMALL -0x2980 /**< Destination buffer is too small. */ 00080 #define MBEDTLS_ERR_X509_FATAL_ERROR -0x3000 /**< A fatal error occured, eg the chain is too long or the vrfy callback failed. */ 00081 /* \} name */ 00082 00083 /** 00084 * \name X509 Verify codes 00085 * \{ 00086 */ 00087 /* Reminder: update x509_crt_verify_strings[] in library/x509_crt.c */ 00088 #define MBEDTLS_X509_BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */ 00089 #define MBEDTLS_X509_BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */ 00090 #define MBEDTLS_X509_BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */ 00091 #define MBEDTLS_X509_BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */ 00092 #define MBEDTLS_X509_BADCRL_NOT_TRUSTED 0x10 /**< The CRL is not correctly signed by the trusted CA. */ 00093 #define MBEDTLS_X509_BADCRL_EXPIRED 0x20 /**< The CRL is expired. */ 00094 #define MBEDTLS_X509_BADCERT_MISSING 0x40 /**< Certificate was missing. */ 00095 #define MBEDTLS_X509_BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */ 00096 #define MBEDTLS_X509_BADCERT_OTHER 0x0100 /**< Other reason (can be used by verify callback) */ 00097 #define MBEDTLS_X509_BADCERT_FUTURE 0x0200 /**< The certificate validity starts in the future. */ 00098 #define MBEDTLS_X509_BADCRL_FUTURE 0x0400 /**< The CRL is from the future */ 00099 #define MBEDTLS_X509_BADCERT_KEY_USAGE 0x0800 /**< Usage does not match the keyUsage extension. */ 00100 #define MBEDTLS_X509_BADCERT_EXT_KEY_USAGE 0x1000 /**< Usage does not match the extendedKeyUsage extension. */ 00101 #define MBEDTLS_X509_BADCERT_NS_CERT_TYPE 0x2000 /**< Usage does not match the nsCertType extension. */ 00102 #define MBEDTLS_X509_BADCERT_BAD_MD 0x4000 /**< The certificate is signed with an unacceptable hash. */ 00103 #define MBEDTLS_X509_BADCERT_BAD_PK 0x8000 /**< The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA). */ 00104 #define MBEDTLS_X509_BADCERT_BAD_KEY 0x010000 /**< The certificate is signed with an unacceptable key (eg bad curve, RSA too short). */ 00105 #define MBEDTLS_X509_BADCRL_BAD_MD 0x020000 /**< The CRL is signed with an unacceptable hash. */ 00106 #define MBEDTLS_X509_BADCRL_BAD_PK 0x040000 /**< The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA). */ 00107 #define MBEDTLS_X509_BADCRL_BAD_KEY 0x080000 /**< The CRL is signed with an unacceptable key (eg bad curve, RSA too short). */ 00108 00109 /* \} name */ 00110 /* \} addtogroup x509_module */ 00111 00112 /* 00113 * X.509 v3 Key Usage Extension flags 00114 * Reminder: update x509_info_key_usage() when adding new flags. 00115 */ 00116 #define MBEDTLS_X509_KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */ 00117 #define MBEDTLS_X509_KU_NON_REPUDIATION (0x40) /* bit 1 */ 00118 #define MBEDTLS_X509_KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */ 00119 #define MBEDTLS_X509_KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */ 00120 #define MBEDTLS_X509_KU_KEY_AGREEMENT (0x08) /* bit 4 */ 00121 #define MBEDTLS_X509_KU_KEY_CERT_SIGN (0x04) /* bit 5 */ 00122 #define MBEDTLS_X509_KU_CRL_SIGN (0x02) /* bit 6 */ 00123 #define MBEDTLS_X509_KU_ENCIPHER_ONLY (0x01) /* bit 7 */ 00124 #define MBEDTLS_X509_KU_DECIPHER_ONLY (0x8000) /* bit 8 */ 00125 00126 /* 00127 * Netscape certificate types 00128 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html) 00129 */ 00130 00131 #define MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */ 00132 #define MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */ 00133 #define MBEDTLS_X509_NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */ 00134 #define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */ 00135 #define MBEDTLS_X509_NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */ 00136 #define MBEDTLS_X509_NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */ 00137 #define MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */ 00138 #define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */ 00139 00140 /* 00141 * X.509 extension types 00142 * 00143 * Comments refer to the status for using certificates. Status can be 00144 * different for writing certificates or reading CRLs or CSRs. 00145 */ 00146 #define MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0) 00147 #define MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER (1 << 1) 00148 #define MBEDTLS_X509_EXT_KEY_USAGE (1 << 2) 00149 #define MBEDTLS_X509_EXT_CERTIFICATE_POLICIES (1 << 3) 00150 #define MBEDTLS_X509_EXT_POLICY_MAPPINGS (1 << 4) 00151 #define MBEDTLS_X509_EXT_SUBJECT_ALT_NAME (1 << 5) /* Supported (DNS) */ 00152 #define MBEDTLS_X509_EXT_ISSUER_ALT_NAME (1 << 6) 00153 #define MBEDTLS_X509_EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7) 00154 #define MBEDTLS_X509_EXT_BASIC_CONSTRAINTS (1 << 8) /* Supported */ 00155 #define MBEDTLS_X509_EXT_NAME_CONSTRAINTS (1 << 9) 00156 #define MBEDTLS_X509_EXT_POLICY_CONSTRAINTS (1 << 10) 00157 #define MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE (1 << 11) 00158 #define MBEDTLS_X509_EXT_CRL_DISTRIBUTION_POINTS (1 << 12) 00159 #define MBEDTLS_X509_EXT_INIHIBIT_ANYPOLICY (1 << 13) 00160 #define MBEDTLS_X509_EXT_FRESHEST_CRL (1 << 14) 00161 00162 #define MBEDTLS_X509_EXT_NS_CERT_TYPE (1 << 16) 00163 00164 /* 00165 * Storage format identifiers 00166 * Recognized formats: PEM and DER 00167 */ 00168 #define MBEDTLS_X509_FORMAT_DER 1 00169 #define MBEDTLS_X509_FORMAT_PEM 2 00170 00171 #define MBEDTLS_X509_MAX_DN_NAME_SIZE 256 /**< Maximum value size of a DN entry */ 00172 00173 #ifdef __cplusplus 00174 extern "C" { 00175 #endif 00176 00177 /** 00178 * \addtogroup x509_module 00179 * \{ */ 00180 00181 /** 00182 * \name Structures for parsing X.509 certificates, CRLs and CSRs 00183 * \{ 00184 */ 00185 00186 /** 00187 * Type-length-value structure that allows for ASN1 using DER. 00188 */ 00189 typedef mbedtls_asn1_buf mbedtls_x509_buf; 00190 00191 /** 00192 * Container for ASN1 bit strings. 00193 */ 00194 typedef mbedtls_asn1_bitstring mbedtls_x509_bitstring; 00195 00196 /** 00197 * Container for ASN1 named information objects. 00198 * It allows for Relative Distinguished Names (e.g. cn=localhost,ou=code,etc.). 00199 */ 00200 typedef mbedtls_asn1_named_data mbedtls_x509_name; 00201 00202 /** 00203 * Container for a sequence of ASN.1 items 00204 */ 00205 typedef mbedtls_asn1_sequence mbedtls_x509_sequence; 00206 00207 /** Container for date and time (precision in seconds). */ 00208 typedef struct mbedtls_x509_time 00209 { 00210 int year, mon, day; /**< Date. */ 00211 int hour, min, sec; /**< Time. */ 00212 } 00213 mbedtls_x509_time; 00214 00215 /** \} name Structures for parsing X.509 certificates, CRLs and CSRs */ 00216 /** \} addtogroup x509_module */ 00217 00218 /** 00219 * \brief Store the certificate DN in printable form into buf; 00220 * no more than size characters will be written. 00221 * 00222 * \param buf Buffer to write to 00223 * \param size Maximum size of buffer 00224 * \param dn The X509 name to represent 00225 * 00226 * \return The length of the string written (not including the 00227 * terminated nul byte), or a negative error code. 00228 */ 00229 int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn ); 00230 00231 /** 00232 * \brief Store the certificate serial in printable form into buf; 00233 * no more than size characters will be written. 00234 * 00235 * \param buf Buffer to write to 00236 * \param size Maximum size of buffer 00237 * \param serial The X509 serial to represent 00238 * 00239 * \return The length of the string written (not including the 00240 * terminated nul byte), or a negative error code. 00241 */ 00242 int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial ); 00243 00244 /** 00245 * \brief Check a given mbedtls_x509_time against the system time 00246 * and tell if it's in the past. 00247 * 00248 * \note Intended usage is "if( is_past( valid_to ) ) ERROR". 00249 * Hence the return value of 1 if on internal errors. 00250 * 00251 * \param to mbedtls_x509_time to check 00252 * 00253 * \return 1 if the given time is in the past or an error occured, 00254 * 0 otherwise. 00255 */ 00256 int mbedtls_x509_time_is_past( const mbedtls_x509_time *to ); 00257 00258 /** 00259 * \brief Check a given mbedtls_x509_time against the system time 00260 * and tell if it's in the future. 00261 * 00262 * \note Intended usage is "if( is_future( valid_from ) ) ERROR". 00263 * Hence the return value of 1 if on internal errors. 00264 * 00265 * \param from mbedtls_x509_time to check 00266 * 00267 * \return 1 if the given time is in the future or an error occured, 00268 * 0 otherwise. 00269 */ 00270 int mbedtls_x509_time_is_future( const mbedtls_x509_time *from ); 00271 00272 /** 00273 * \brief Checkup routine 00274 * 00275 * \return 0 if successful, or 1 if the test failed 00276 */ 00277 int mbedtls_x509_self_test( int verbose ); 00278 00279 /* 00280 * Internal module functions. You probably do not want to use these unless you 00281 * know you do. 00282 */ 00283 int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end, 00284 mbedtls_x509_name *cur ); 00285 int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end, 00286 mbedtls_x509_buf *alg ); 00287 int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end, 00288 mbedtls_x509_buf *alg, mbedtls_x509_buf *params ); 00289 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) 00290 int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params, 00291 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md, 00292 int *salt_len ); 00293 #endif 00294 int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig ); 00295 int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params, 00296 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg, 00297 void **sig_opts ); 00298 int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end, 00299 mbedtls_x509_time *t ); 00300 int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end, 00301 mbedtls_x509_buf *serial ); 00302 int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end, 00303 mbedtls_x509_buf *ext, int tag ); 00304 int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid, 00305 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg, 00306 const void *sig_opts ); 00307 int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name ); 00308 int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name ); 00309 int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len, 00310 int critical, const unsigned char *val, 00311 size_t val_len ); 00312 int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start, 00313 mbedtls_asn1_named_data *first ); 00314 int mbedtls_x509_write_names( unsigned char **p, unsigned char *start, 00315 mbedtls_asn1_named_data *first ); 00316 int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start, 00317 const char *oid, size_t oid_len, 00318 unsigned char *sig, size_t size ); 00319 00320 #define MBEDTLS_X509_SAFE_SNPRINTF \ 00321 do { \ 00322 if( ret < 0 || (size_t) ret >= n ) \ 00323 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL ); \ 00324 \ 00325 n -= (size_t) ret; \ 00326 p += (size_t) ret; \ 00327 } while( 0 ) 00328 00329 #ifdef __cplusplus 00330 } 00331 #endif 00332 00333 #endif /* x509.h */
Generated on Tue Jul 12 2022 12:22:29 by
