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.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
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 "mbedtls/config.h" 00029 #else 00030 #include MBEDTLS_CONFIG_FILE 00031 #endif 00032 00033 #include "mbedtls/asn1.h" 00034 #include "mbedtls/pk.h" 00035 00036 #if defined(MBEDTLS_RSA_C) 00037 #include "mbedtls/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 occurred, 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 Subject Alternative Name types. 00114 * otherName [0] OtherName, 00115 * rfc822Name [1] IA5String, 00116 * dNSName [2] IA5String, 00117 * x400Address [3] ORAddress, 00118 * directoryName [4] Name, 00119 * ediPartyName [5] EDIPartyName, 00120 * uniformResourceIdentifier [6] IA5String, 00121 * iPAddress [7] OCTET STRING, 00122 * registeredID [8] OBJECT IDENTIFIER 00123 */ 00124 #define MBEDTLS_X509_SAN_OTHER_NAME 0 00125 #define MBEDTLS_X509_SAN_RFC822_NAME 1 00126 #define MBEDTLS_X509_SAN_DNS_NAME 2 00127 #define MBEDTLS_X509_SAN_X400_ADDRESS_NAME 3 00128 #define MBEDTLS_X509_SAN_DIRECTORY_NAME 4 00129 #define MBEDTLS_X509_SAN_EDI_PARTY_NAME 5 00130 #define MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER 6 00131 #define MBEDTLS_X509_SAN_IP_ADDRESS 7 00132 #define MBEDTLS_X509_SAN_REGISTERED_ID 8 00133 00134 /* 00135 * X.509 v3 Key Usage Extension flags 00136 * Reminder: update x509_info_key_usage() when adding new flags. 00137 */ 00138 #define MBEDTLS_X509_KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */ 00139 #define MBEDTLS_X509_KU_NON_REPUDIATION (0x40) /* bit 1 */ 00140 #define MBEDTLS_X509_KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */ 00141 #define MBEDTLS_X509_KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */ 00142 #define MBEDTLS_X509_KU_KEY_AGREEMENT (0x08) /* bit 4 */ 00143 #define MBEDTLS_X509_KU_KEY_CERT_SIGN (0x04) /* bit 5 */ 00144 #define MBEDTLS_X509_KU_CRL_SIGN (0x02) /* bit 6 */ 00145 #define MBEDTLS_X509_KU_ENCIPHER_ONLY (0x01) /* bit 7 */ 00146 #define MBEDTLS_X509_KU_DECIPHER_ONLY (0x8000) /* bit 8 */ 00147 00148 /* 00149 * Netscape certificate types 00150 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html) 00151 */ 00152 00153 #define MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */ 00154 #define MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */ 00155 #define MBEDTLS_X509_NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */ 00156 #define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */ 00157 #define MBEDTLS_X509_NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */ 00158 #define MBEDTLS_X509_NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */ 00159 #define MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */ 00160 #define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */ 00161 00162 /* 00163 * X.509 extension types 00164 * 00165 * Comments refer to the status for using certificates. Status can be 00166 * different for writing certificates or reading CRLs or CSRs. 00167 * 00168 * Those are defined in oid.h as oid.c needs them in a data structure. Since 00169 * these were previously defined here, let's have aliases for compatibility. 00170 */ 00171 #define MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER MBEDTLS_OID_X509_EXT_AUTHORITY_KEY_IDENTIFIER 00172 #define MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER MBEDTLS_OID_X509_EXT_SUBJECT_KEY_IDENTIFIER 00173 #define MBEDTLS_X509_EXT_KEY_USAGE MBEDTLS_OID_X509_EXT_KEY_USAGE 00174 #define MBEDTLS_X509_EXT_CERTIFICATE_POLICIES MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES 00175 #define MBEDTLS_X509_EXT_POLICY_MAPPINGS MBEDTLS_OID_X509_EXT_POLICY_MAPPINGS 00176 #define MBEDTLS_X509_EXT_SUBJECT_ALT_NAME MBEDTLS_OID_X509_EXT_SUBJECT_ALT_NAME /* Supported (DNS) */ 00177 #define MBEDTLS_X509_EXT_ISSUER_ALT_NAME MBEDTLS_OID_X509_EXT_ISSUER_ALT_NAME 00178 #define MBEDTLS_X509_EXT_SUBJECT_DIRECTORY_ATTRS MBEDTLS_OID_X509_EXT_SUBJECT_DIRECTORY_ATTRS 00179 #define MBEDTLS_X509_EXT_BASIC_CONSTRAINTS MBEDTLS_OID_X509_EXT_BASIC_CONSTRAINTS /* Supported */ 00180 #define MBEDTLS_X509_EXT_NAME_CONSTRAINTS MBEDTLS_OID_X509_EXT_NAME_CONSTRAINTS 00181 #define MBEDTLS_X509_EXT_POLICY_CONSTRAINTS MBEDTLS_OID_X509_EXT_POLICY_CONSTRAINTS 00182 #define MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE MBEDTLS_OID_X509_EXT_EXTENDED_KEY_USAGE 00183 #define MBEDTLS_X509_EXT_CRL_DISTRIBUTION_POINTS MBEDTLS_OID_X509_EXT_CRL_DISTRIBUTION_POINTS 00184 #define MBEDTLS_X509_EXT_INIHIBIT_ANYPOLICY MBEDTLS_OID_X509_EXT_INIHIBIT_ANYPOLICY 00185 #define MBEDTLS_X509_EXT_FRESHEST_CRL MBEDTLS_OID_X509_EXT_FRESHEST_CRL 00186 #define MBEDTLS_X509_EXT_NS_CERT_TYPE MBEDTLS_OID_X509_EXT_NS_CERT_TYPE 00187 00188 /* 00189 * Storage format identifiers 00190 * Recognized formats: PEM and DER 00191 */ 00192 #define MBEDTLS_X509_FORMAT_DER 1 00193 #define MBEDTLS_X509_FORMAT_PEM 2 00194 00195 #define MBEDTLS_X509_MAX_DN_NAME_SIZE 256 /**< Maximum value size of a DN entry */ 00196 00197 #ifdef __cplusplus 00198 extern "C" { 00199 #endif 00200 00201 /** 00202 * \addtogroup x509_module 00203 * \{ */ 00204 00205 /** 00206 * \name Structures for parsing X.509 certificates, CRLs and CSRs 00207 * \{ 00208 */ 00209 00210 /** 00211 * Type-length-value structure that allows for ASN1 using DER. 00212 */ 00213 typedef mbedtls_asn1_buf mbedtls_x509_buf; 00214 00215 /** 00216 * Container for ASN1 bit strings. 00217 */ 00218 typedef mbedtls_asn1_bitstring mbedtls_x509_bitstring; 00219 00220 /** 00221 * Container for ASN1 named information objects. 00222 * It allows for Relative Distinguished Names (e.g. cn=localhost,ou=code,etc.). 00223 */ 00224 typedef mbedtls_asn1_named_data mbedtls_x509_name; 00225 00226 /** 00227 * Container for a sequence of ASN.1 items 00228 */ 00229 typedef mbedtls_asn1_sequence mbedtls_x509_sequence; 00230 00231 /** Container for date and time (precision in seconds). */ 00232 typedef struct mbedtls_x509_time 00233 { 00234 int year, mon, day; /**< Date. */ 00235 int hour, min, sec; /**< Time. */ 00236 } 00237 mbedtls_x509_time; 00238 00239 /** \} name Structures for parsing X.509 certificates, CRLs and CSRs */ 00240 /** \} addtogroup x509_module */ 00241 00242 /** 00243 * \brief Store the certificate DN in printable form into buf; 00244 * no more than size characters will be written. 00245 * 00246 * \param buf Buffer to write to 00247 * \param size Maximum size of buffer 00248 * \param dn The X509 name to represent 00249 * 00250 * \return The length of the string written (not including the 00251 * terminated nul byte), or a negative error code. 00252 */ 00253 int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn ); 00254 00255 /** 00256 * \brief Store the certificate serial in printable form into buf; 00257 * no more than size characters will be written. 00258 * 00259 * \param buf Buffer to write to 00260 * \param size Maximum size of buffer 00261 * \param serial The X509 serial to represent 00262 * 00263 * \return The length of the string written (not including the 00264 * terminated nul byte), or a negative error code. 00265 */ 00266 int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial ); 00267 00268 /** 00269 * \brief Check a given mbedtls_x509_time against the system time 00270 * and tell if it's in the past. 00271 * 00272 * \note Intended usage is "if( is_past( valid_to ) ) ERROR". 00273 * Hence the return value of 1 if on internal errors. 00274 * 00275 * \param to mbedtls_x509_time to check 00276 * 00277 * \return 1 if the given time is in the past or an error occurred, 00278 * 0 otherwise. 00279 */ 00280 int mbedtls_x509_time_is_past( const mbedtls_x509_time *to ); 00281 00282 /** 00283 * \brief Check a given mbedtls_x509_time against the system time 00284 * and tell if it's in the future. 00285 * 00286 * \note Intended usage is "if( is_future( valid_from ) ) ERROR". 00287 * Hence the return value of 1 if on internal errors. 00288 * 00289 * \param from mbedtls_x509_time to check 00290 * 00291 * \return 1 if the given time is in the future or an error occurred, 00292 * 0 otherwise. 00293 */ 00294 int mbedtls_x509_time_is_future( const mbedtls_x509_time *from ); 00295 00296 #if defined(MBEDTLS_SELF_TEST) 00297 00298 /** 00299 * \brief Checkup routine 00300 * 00301 * \return 0 if successful, or 1 if the test failed 00302 */ 00303 int mbedtls_x509_self_test( int verbose ); 00304 00305 #endif /* MBEDTLS_SELF_TEST */ 00306 00307 /* 00308 * Internal module functions. You probably do not want to use these unless you 00309 * know you do. 00310 */ 00311 int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end, 00312 mbedtls_x509_name *cur ); 00313 int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end, 00314 mbedtls_x509_buf *alg ); 00315 int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end, 00316 mbedtls_x509_buf *alg, mbedtls_x509_buf *params ); 00317 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) 00318 int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params, 00319 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md, 00320 int *salt_len ); 00321 #endif 00322 int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig ); 00323 int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params, 00324 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg, 00325 void **sig_opts ); 00326 int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end, 00327 mbedtls_x509_time *t ); 00328 int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end, 00329 mbedtls_x509_buf *serial ); 00330 int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end, 00331 mbedtls_x509_buf *ext, int tag ); 00332 int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid, 00333 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg, 00334 const void *sig_opts ); 00335 int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name ); 00336 int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name ); 00337 int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len, 00338 int critical, const unsigned char *val, 00339 size_t val_len ); 00340 int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start, 00341 mbedtls_asn1_named_data *first ); 00342 int mbedtls_x509_write_names( unsigned char **p, unsigned char *start, 00343 mbedtls_asn1_named_data *first ); 00344 int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start, 00345 const char *oid, size_t oid_len, 00346 unsigned char *sig, size_t size ); 00347 00348 #define MBEDTLS_X509_SAFE_SNPRINTF \ 00349 do { \ 00350 if( ret < 0 || (size_t) ret >= n ) \ 00351 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL ); \ 00352 \ 00353 n -= (size_t) ret; \ 00354 p += (size_t) ret; \ 00355 } while( 0 ) 00356 00357 #ifdef __cplusplus 00358 } 00359 #endif 00360 00361 #endif /* x509.h */
Generated on Tue Jul 12 2022 13:55:04 by
