1 /** 2 * \file x509.h 3 * 4 * \brief X.509 generic defines and structures 5 */ 6 /* 7 * Copyright The Mbed TLS Contributors 8 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 9 */ 10 #ifndef MBEDTLS_X509_H 11 #define MBEDTLS_X509_H 12 13 #if !defined(MBEDTLS_CONFIG_FILE) 14 #include "mbedtls/config.h" 15 #else 16 #include MBEDTLS_CONFIG_FILE 17 #endif 18 19 #include "mbedtls/asn1.h" 20 #include "mbedtls/pk.h" 21 22 #if defined(MBEDTLS_RSA_C) 23 #include "mbedtls/rsa.h" 24 #endif 25 26 /** 27 * \addtogroup x509_module 28 * \{ 29 */ 30 31 #if !defined(MBEDTLS_X509_MAX_INTERMEDIATE_CA) 32 /** 33 * Maximum number of intermediate CAs in a verification chain. 34 * That is, maximum length of the chain, excluding the end-entity certificate 35 * and the trusted root certificate. 36 * 37 * Set this to a low value to prevent an adversary from making you waste 38 * resources verifying an overlong certificate chain. 39 */ 40 #define MBEDTLS_X509_MAX_INTERMEDIATE_CA 8 41 #endif 42 43 /** 44 * \name X509 Error codes 45 * \{ 46 */ 47 /** Unavailable feature, e.g. RSA hashing/encryption combination. */ 48 #define MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE -0x2080 49 /** Requested OID is unknown. */ 50 #define MBEDTLS_ERR_X509_UNKNOWN_OID -0x2100 51 /** The CRT/CRL/CSR format is invalid, e.g. different type expected. */ 52 #define MBEDTLS_ERR_X509_INVALID_FORMAT -0x2180 53 /** The CRT/CRL/CSR version element is invalid. */ 54 #define MBEDTLS_ERR_X509_INVALID_VERSION -0x2200 55 /** The serial tag or value is invalid. */ 56 #define MBEDTLS_ERR_X509_INVALID_SERIAL -0x2280 57 /** The algorithm tag or value is invalid. */ 58 #define MBEDTLS_ERR_X509_INVALID_ALG -0x2300 59 /** The name tag or value is invalid. */ 60 #define MBEDTLS_ERR_X509_INVALID_NAME -0x2380 61 /** The date tag or value is invalid. */ 62 #define MBEDTLS_ERR_X509_INVALID_DATE -0x2400 63 /** The signature tag or value invalid. */ 64 #define MBEDTLS_ERR_X509_INVALID_SIGNATURE -0x2480 65 /** The extension tag or value is invalid. */ 66 #define MBEDTLS_ERR_X509_INVALID_EXTENSIONS -0x2500 67 /** CRT/CRL/CSR has an unsupported version number. */ 68 #define MBEDTLS_ERR_X509_UNKNOWN_VERSION -0x2580 69 /** Signature algorithm (oid) is unsupported. */ 70 #define MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG -0x2600 71 /** Signature algorithms do not match. (see \c ::mbedtls_x509_crt sig_oid) */ 72 #define MBEDTLS_ERR_X509_SIG_MISMATCH -0x2680 73 /** Certificate verification failed, e.g. CRL, CA or signature check failed. */ 74 #define MBEDTLS_ERR_X509_CERT_VERIFY_FAILED -0x2700 75 /** Format not recognized as DER or PEM. */ 76 #define MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT -0x2780 77 /** Input invalid. */ 78 #define MBEDTLS_ERR_X509_BAD_INPUT_DATA -0x2800 79 /** Allocation of memory failed. */ 80 #define MBEDTLS_ERR_X509_ALLOC_FAILED -0x2880 81 /** Read/write of file failed. */ 82 #define MBEDTLS_ERR_X509_FILE_IO_ERROR -0x2900 83 /** Destination buffer is too small. */ 84 #define MBEDTLS_ERR_X509_BUFFER_TOO_SMALL -0x2980 85 /** A fatal error occurred, eg the chain is too long or the vrfy callback failed. */ 86 #define MBEDTLS_ERR_X509_FATAL_ERROR -0x3000 87 /** \} name X509 Error codes */ 88 89 /** 90 * \name X509 Verify codes 91 * \{ 92 */ 93 /* Reminder: update x509_crt_verify_strings[] in library/x509_crt.c */ 94 #define MBEDTLS_X509_BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */ 95 #define MBEDTLS_X509_BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */ 96 #define MBEDTLS_X509_BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */ 97 #define MBEDTLS_X509_BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */ 98 #define MBEDTLS_X509_BADCRL_NOT_TRUSTED 0x10 /**< The CRL is not correctly signed by the trusted CA. */ 99 #define MBEDTLS_X509_BADCRL_EXPIRED 0x20 /**< The CRL is expired. */ 100 #define MBEDTLS_X509_BADCERT_MISSING 0x40 /**< Certificate was missing. */ 101 #define MBEDTLS_X509_BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */ 102 #define MBEDTLS_X509_BADCERT_OTHER 0x0100 /**< Other reason (can be used by verify callback) */ 103 #define MBEDTLS_X509_BADCERT_FUTURE 0x0200 /**< The certificate validity starts in the future. */ 104 #define MBEDTLS_X509_BADCRL_FUTURE 0x0400 /**< The CRL is from the future */ 105 #define MBEDTLS_X509_BADCERT_KEY_USAGE 0x0800 /**< Usage does not match the keyUsage extension. */ 106 #define MBEDTLS_X509_BADCERT_EXT_KEY_USAGE 0x1000 /**< Usage does not match the extendedKeyUsage extension. */ 107 #define MBEDTLS_X509_BADCERT_NS_CERT_TYPE 0x2000 /**< Usage does not match the nsCertType extension. */ 108 #define MBEDTLS_X509_BADCERT_BAD_MD 0x4000 /**< The certificate is signed with an unacceptable hash. */ 109 #define MBEDTLS_X509_BADCERT_BAD_PK 0x8000 /**< The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA). */ 110 #define MBEDTLS_X509_BADCERT_BAD_KEY 0x010000 /**< The certificate is signed with an unacceptable key (eg bad curve, RSA too short). */ 111 #define MBEDTLS_X509_BADCRL_BAD_MD 0x020000 /**< The CRL is signed with an unacceptable hash. */ 112 #define MBEDTLS_X509_BADCRL_BAD_PK 0x040000 /**< The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA). */ 113 #define MBEDTLS_X509_BADCRL_BAD_KEY 0x080000 /**< The CRL is signed with an unacceptable key (eg bad curve, RSA too short). */ 114 115 /** \} name X509 Verify codes */ 116 /** \} addtogroup x509_module */ 117 118 /* 119 * X.509 v3 Subject Alternative Name types. 120 * otherName [0] OtherName, 121 * rfc822Name [1] IA5String, 122 * dNSName [2] IA5String, 123 * x400Address [3] ORAddress, 124 * directoryName [4] Name, 125 * ediPartyName [5] EDIPartyName, 126 * uniformResourceIdentifier [6] IA5String, 127 * iPAddress [7] OCTET STRING, 128 * registeredID [8] OBJECT IDENTIFIER 129 */ 130 #define MBEDTLS_X509_SAN_OTHER_NAME 0 131 #define MBEDTLS_X509_SAN_RFC822_NAME 1 132 #define MBEDTLS_X509_SAN_DNS_NAME 2 133 #define MBEDTLS_X509_SAN_X400_ADDRESS_NAME 3 134 #define MBEDTLS_X509_SAN_DIRECTORY_NAME 4 135 #define MBEDTLS_X509_SAN_EDI_PARTY_NAME 5 136 #define MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER 6 137 #define MBEDTLS_X509_SAN_IP_ADDRESS 7 138 #define MBEDTLS_X509_SAN_REGISTERED_ID 8 139 140 /* 141 * X.509 v3 Key Usage Extension flags 142 * Reminder: update x509_info_key_usage() when adding new flags. 143 */ 144 #define MBEDTLS_X509_KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */ 145 #define MBEDTLS_X509_KU_NON_REPUDIATION (0x40) /* bit 1 */ 146 #define MBEDTLS_X509_KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */ 147 #define MBEDTLS_X509_KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */ 148 #define MBEDTLS_X509_KU_KEY_AGREEMENT (0x08) /* bit 4 */ 149 #define MBEDTLS_X509_KU_KEY_CERT_SIGN (0x04) /* bit 5 */ 150 #define MBEDTLS_X509_KU_CRL_SIGN (0x02) /* bit 6 */ 151 #define MBEDTLS_X509_KU_ENCIPHER_ONLY (0x01) /* bit 7 */ 152 #define MBEDTLS_X509_KU_DECIPHER_ONLY (0x8000) /* bit 8 */ 153 154 /* 155 * Netscape certificate types 156 * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html) 157 */ 158 159 #define MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */ 160 #define MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */ 161 #define MBEDTLS_X509_NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */ 162 #define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */ 163 #define MBEDTLS_X509_NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */ 164 #define MBEDTLS_X509_NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */ 165 #define MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */ 166 #define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */ 167 168 /* 169 * X.509 extension types 170 * 171 * Comments refer to the status for using certificates. Status can be 172 * different for writing certificates or reading CRLs or CSRs. 173 * 174 * Those are defined in oid.h as oid.c needs them in a data structure. Since 175 * these were previously defined here, let's have aliases for compatibility. 176 */ 177 #define MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER MBEDTLS_OID_X509_EXT_AUTHORITY_KEY_IDENTIFIER 178 #define MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER MBEDTLS_OID_X509_EXT_SUBJECT_KEY_IDENTIFIER 179 #define MBEDTLS_X509_EXT_KEY_USAGE MBEDTLS_OID_X509_EXT_KEY_USAGE 180 #define MBEDTLS_X509_EXT_CERTIFICATE_POLICIES MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES 181 #define MBEDTLS_X509_EXT_POLICY_MAPPINGS MBEDTLS_OID_X509_EXT_POLICY_MAPPINGS 182 #define MBEDTLS_X509_EXT_SUBJECT_ALT_NAME MBEDTLS_OID_X509_EXT_SUBJECT_ALT_NAME /* Supported (DNS) */ 183 #define MBEDTLS_X509_EXT_ISSUER_ALT_NAME MBEDTLS_OID_X509_EXT_ISSUER_ALT_NAME 184 #define MBEDTLS_X509_EXT_SUBJECT_DIRECTORY_ATTRS MBEDTLS_OID_X509_EXT_SUBJECT_DIRECTORY_ATTRS 185 #define MBEDTLS_X509_EXT_BASIC_CONSTRAINTS MBEDTLS_OID_X509_EXT_BASIC_CONSTRAINTS /* Supported */ 186 #define MBEDTLS_X509_EXT_NAME_CONSTRAINTS MBEDTLS_OID_X509_EXT_NAME_CONSTRAINTS 187 #define MBEDTLS_X509_EXT_POLICY_CONSTRAINTS MBEDTLS_OID_X509_EXT_POLICY_CONSTRAINTS 188 #define MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE MBEDTLS_OID_X509_EXT_EXTENDED_KEY_USAGE 189 #define MBEDTLS_X509_EXT_CRL_DISTRIBUTION_POINTS MBEDTLS_OID_X509_EXT_CRL_DISTRIBUTION_POINTS 190 #define MBEDTLS_X509_EXT_INIHIBIT_ANYPOLICY MBEDTLS_OID_X509_EXT_INIHIBIT_ANYPOLICY 191 #define MBEDTLS_X509_EXT_FRESHEST_CRL MBEDTLS_OID_X509_EXT_FRESHEST_CRL 192 #define MBEDTLS_X509_EXT_NS_CERT_TYPE MBEDTLS_OID_X509_EXT_NS_CERT_TYPE 193 194 /* 195 * Storage format identifiers 196 * Recognized formats: PEM and DER 197 */ 198 #define MBEDTLS_X509_FORMAT_DER 1 199 #define MBEDTLS_X509_FORMAT_PEM 2 200 201 #define MBEDTLS_X509_MAX_DN_NAME_SIZE 256 /**< Maximum value size of a DN entry */ 202 203 #ifdef __cplusplus 204 extern "C" { 205 #endif 206 207 /** 208 * \addtogroup x509_module 209 * \{ */ 210 211 /** 212 * \name Structures for parsing X.509 certificates, CRLs and CSRs 213 * \{ 214 */ 215 216 /** 217 * Type-length-value structure that allows for ASN1 using DER. 218 */ 219 typedef mbedtls_asn1_buf mbedtls_x509_buf; 220 221 /** 222 * Container for ASN1 bit strings. 223 */ 224 typedef mbedtls_asn1_bitstring mbedtls_x509_bitstring; 225 226 /** 227 * Container for ASN1 named information objects. 228 * It allows for Relative Distinguished Names (e.g. cn=localhost,ou=code,etc.). 229 */ 230 typedef mbedtls_asn1_named_data mbedtls_x509_name; 231 232 /** 233 * Container for a sequence of ASN.1 items 234 */ 235 typedef mbedtls_asn1_sequence mbedtls_x509_sequence; 236 237 /** Container for date and time (precision in seconds). */ 238 typedef struct mbedtls_x509_time { 239 int year, mon, day; /**< Date. */ 240 int hour, min, sec; /**< Time. */ 241 } 242 mbedtls_x509_time; 243 244 /** \} name Structures for parsing X.509 certificates, CRLs and CSRs */ 245 246 /** 247 * \brief Store the certificate DN in printable form into buf; 248 * no more than size characters will be written. 249 * 250 * \param buf Buffer to write to 251 * \param size Maximum size of buffer 252 * \param dn The X509 name to represent 253 * 254 * \return The length of the string written (not including the 255 * terminated nul byte), or a negative error code. 256 */ 257 int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn); 258 259 /** 260 * \brief Store the certificate serial in printable form into buf; 261 * no more than size characters will be written. 262 * 263 * \param buf Buffer to write to 264 * \param size Maximum size of buffer 265 * \param serial The X509 serial to represent 266 * 267 * \return The length of the string written (not including the 268 * terminated nul byte), or a negative error code. 269 */ 270 int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial); 271 272 /** 273 * \brief Check a given mbedtls_x509_time against the system time 274 * and tell if it's in the past. 275 * 276 * \note Intended usage is "if( is_past( valid_to ) ) ERROR". 277 * Hence the return value of 1 if on internal errors. 278 * 279 * \param to mbedtls_x509_time to check 280 * 281 * \return 1 if the given time is in the past or an error occurred, 282 * 0 otherwise. 283 */ 284 int mbedtls_x509_time_is_past(const mbedtls_x509_time *to); 285 286 /** 287 * \brief Check a given mbedtls_x509_time against the system time 288 * and tell if it's in the future. 289 * 290 * \note Intended usage is "if( is_future( valid_from ) ) ERROR". 291 * Hence the return value of 1 if on internal errors. 292 * 293 * \param from mbedtls_x509_time to check 294 * 295 * \return 1 if the given time is in the future or an error occurred, 296 * 0 otherwise. 297 */ 298 int mbedtls_x509_time_is_future(const mbedtls_x509_time *from); 299 300 /** \} addtogroup x509_module */ 301 302 #if defined(MBEDTLS_SELF_TEST) 303 304 /** 305 * \brief Checkup routine 306 * 307 * \return 0 if successful, or 1 if the test failed 308 */ 309 int mbedtls_x509_self_test(int verbose); 310 311 #endif /* MBEDTLS_SELF_TEST */ 312 313 /* 314 * Internal module functions. You probably do not want to use these unless you 315 * know you do. 316 */ 317 int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end, 318 mbedtls_x509_name *cur); 319 int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end, 320 mbedtls_x509_buf *alg); 321 int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end, 322 mbedtls_x509_buf *alg, mbedtls_x509_buf *params); 323 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) 324 int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params, 325 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md, 326 int *salt_len); 327 #endif 328 int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig); 329 int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params, 330 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg, 331 void **sig_opts); 332 int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end, 333 mbedtls_x509_time *t); 334 int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end, 335 mbedtls_x509_buf *serial); 336 int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end, 337 mbedtls_x509_buf *ext, int tag); 338 int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid, 339 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg, 340 const void *sig_opts); 341 int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name); 342 int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name); 343 int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len, 344 int critical, const unsigned char *val, 345 size_t val_len); 346 int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start, 347 mbedtls_asn1_named_data *first); 348 int mbedtls_x509_write_names(unsigned char **p, unsigned char *start, 349 mbedtls_asn1_named_data *first); 350 int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start, 351 const char *oid, size_t oid_len, 352 unsigned char *sig, size_t size, 353 mbedtls_pk_type_t pk_alg); 354 355 #define MBEDTLS_X509_SAFE_SNPRINTF \ 356 do { \ 357 if (ret < 0 || (size_t) ret >= n) \ 358 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; \ 359 \ 360 n -= (size_t) ret; \ 361 p += (size_t) ret; \ 362 } while (0) 363 364 #ifdef __cplusplus 365 } 366 #endif 367 368 #endif /* x509.h */ 369