err.h (16095B)
1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef OPENSSL_HEADER_ERR_H 16 #define OPENSSL_HEADER_ERR_H 17 18 #include <stdio.h> 19 20 #include <openssl/base.h> // IWYU pragma: export 21 22 #if defined(__cplusplus) 23 extern "C" { 24 #endif 25 26 27 // Error queue handling functions. 28 // 29 // Errors in OpenSSL are generally signaled by the return value of a function. 30 // When a function fails it may add an entry to a per-thread error queue, 31 // which is managed by the functions in this header. 32 // 33 // Each error contains: 34 // 1) The library (i.e. ec, pem, rsa) which created it. 35 // 2) The file and line number of the call that added the error. 36 // 3) A pointer to some error specific data, which may be NULL. 37 // 38 // The library identifier and reason code are packed in a uint32_t and there 39 // exist various functions for unpacking it. 40 // 41 // The typical behaviour is that an error will occur deep in a call queue and 42 // that code will push an error onto the error queue. As the error queue 43 // unwinds, other functions will push their own errors. Thus, the "least 44 // recent" error is the most specific and the other errors will provide a 45 // backtrace of sorts. 46 47 48 // Startup and shutdown. 49 50 // ERR_load_BIO_strings does nothing. 51 // 52 // TODO(fork): remove. libjingle calls this. 53 OPENSSL_EXPORT void ERR_load_BIO_strings(void); 54 55 // ERR_load_ERR_strings does nothing. 56 OPENSSL_EXPORT void ERR_load_ERR_strings(void); 57 58 // ERR_load_crypto_strings does nothing. 59 OPENSSL_EXPORT void ERR_load_crypto_strings(void); 60 61 // ERR_load_RAND_strings does nothing. 62 OPENSSL_EXPORT void ERR_load_RAND_strings(void); 63 64 // ERR_free_strings does nothing. 65 OPENSSL_EXPORT void ERR_free_strings(void); 66 67 68 // Reading and formatting errors. 69 70 // ERR_GET_LIB returns the library code for the error. This is one of 71 // the |ERR_LIB_*| values. 72 OPENSSL_INLINE int ERR_GET_LIB(uint32_t packed_error) { 73 return (int)((packed_error >> 24) & 0xff); 74 } 75 76 // ERR_GET_REASON returns the reason code for the error. This is one of 77 // library-specific |LIB_R_*| values where |LIB| is the library (see 78 // |ERR_GET_LIB|). Note that reason codes are specific to the library. 79 OPENSSL_INLINE int ERR_GET_REASON(uint32_t packed_error) { 80 return (int)(packed_error & 0xfff); 81 } 82 83 // ERR_get_error gets the packed error code for the least recent error and 84 // removes that error from the queue. If there are no errors in the queue then 85 // it returns zero. 86 OPENSSL_EXPORT uint32_t ERR_get_error(void); 87 88 // ERR_get_error_line acts like |ERR_get_error|, except that the file and line 89 // number of the call that added the error are also returned. 90 OPENSSL_EXPORT uint32_t ERR_get_error_line(const char **file, int *line); 91 92 // ERR_FLAG_STRING means that the |data| member is a NUL-terminated string that 93 // can be printed. This is always set if |data| is non-NULL. 94 #define ERR_FLAG_STRING 1 95 96 // ERR_FLAG_MALLOCED is passed into |ERR_set_error_data| to indicate that |data| 97 // was allocated with |OPENSSL_malloc|. 98 // 99 // It is, separately, returned in |*flags| from |ERR_get_error_line_data| to 100 // indicate that |*data| has a non-static lifetime, but this lifetime is still 101 // managed by the library. The caller must not call |OPENSSL_free| or |free| on 102 // |data|. 103 #define ERR_FLAG_MALLOCED 2 104 105 // ERR_get_error_line_data acts like |ERR_get_error_line|, but also returns the 106 // error-specific data pointer and flags. The flags are a bitwise-OR of 107 // |ERR_FLAG_*| values. The error-specific data is owned by the error queue 108 // and the pointer becomes invalid after the next call that affects the same 109 // thread's error queue. If |*flags| contains |ERR_FLAG_STRING| then |*data| is 110 // human-readable. 111 OPENSSL_EXPORT uint32_t ERR_get_error_line_data(const char **file, int *line, 112 const char **data, int *flags); 113 114 // The "peek" functions act like the |ERR_get_error| functions, above, but they 115 // do not remove the error from the queue. 116 OPENSSL_EXPORT uint32_t ERR_peek_error(void); 117 OPENSSL_EXPORT uint32_t ERR_peek_error_line(const char **file, int *line); 118 OPENSSL_EXPORT uint32_t ERR_peek_error_line_data(const char **file, int *line, 119 const char **data, int *flags); 120 121 // The "peek last" functions act like the "peek" functions, above, except that 122 // they return the most recent error. 123 OPENSSL_EXPORT uint32_t ERR_peek_last_error(void); 124 OPENSSL_EXPORT uint32_t ERR_peek_last_error_line(const char **file, int *line); 125 OPENSSL_EXPORT uint32_t ERR_peek_last_error_line_data(const char **file, 126 int *line, 127 const char **data, 128 int *flags); 129 130 // ERR_error_string_n generates a human-readable string representing 131 // |packed_error|, places it at |buf|, and returns |buf|. It writes at most 132 // |len| bytes (including the terminating NUL) and truncates the string if 133 // necessary. If |len| is greater than zero then |buf| is always NUL terminated. 134 // 135 // The string will have the following format: 136 // 137 // error:[error code]:[library name]:OPENSSL_internal:[reason string] 138 // 139 // error code is an 8 digit hexadecimal number; library name and reason string 140 // are ASCII text. 141 OPENSSL_EXPORT char *ERR_error_string_n(uint32_t packed_error, char *buf, 142 size_t len); 143 144 // ERR_lib_error_string returns a string representation of the library that 145 // generated |packed_error|, or a placeholder string is the library is 146 // unrecognized. 147 OPENSSL_EXPORT const char *ERR_lib_error_string(uint32_t packed_error); 148 149 // ERR_reason_error_string returns a string representation of the reason for 150 // |packed_error|, or a placeholder string if the reason is unrecognized. 151 OPENSSL_EXPORT const char *ERR_reason_error_string(uint32_t packed_error); 152 153 // ERR_lib_symbol_name returns the symbol name of library that generated 154 // |packed_error|, or NULL if unrecognized. For example, an error from 155 // |ERR_LIB_EVP| would return "EVP". 156 OPENSSL_EXPORT const char *ERR_lib_symbol_name(uint32_t packed_error); 157 158 // ERR_reason_symbol_name returns the symbol name of the reason for 159 // |packed_error|, or NULL if unrecognized. For example, |ERR_R_INTERNAL_ERROR| 160 // would return "INTERNAL_ERROR". 161 // 162 // Errors from the |ERR_LIB_SYS| library are typically |errno| values and will 163 // return NULL. User-defined errors will also return NULL. 164 OPENSSL_EXPORT const char *ERR_reason_symbol_name(uint32_t packed_error); 165 166 // ERR_print_errors_callback_t is the type of a function used by 167 // |ERR_print_errors_cb|. It takes a pointer to a human readable string (and 168 // its length) that describes an entry in the error queue. The |ctx| argument 169 // is an opaque pointer given to |ERR_print_errors_cb|. 170 // 171 // It should return one on success or zero on error, which will stop the 172 // iteration over the error queue. 173 typedef int (*ERR_print_errors_callback_t)(const char *str, size_t len, 174 void *ctx); 175 176 // ERR_print_errors_cb clears the current thread's error queue, calling 177 // |callback| with a string representation of each error, from the least recent 178 // to the most recent error. 179 // 180 // The string will have the following format (which differs from 181 // |ERR_error_string|): 182 // 183 // [thread id]:error:[error code]:[library name]:OPENSSL_internal:[reason string]:[file]:[line number]:[optional string data] 184 // 185 // The callback can return one to continue the iteration or zero to stop it. 186 // The |ctx| argument is an opaque value that is passed through to the 187 // callback. 188 OPENSSL_EXPORT void ERR_print_errors_cb(ERR_print_errors_callback_t callback, 189 void *ctx); 190 191 // ERR_print_errors_fp clears the current thread's error queue, printing each 192 // error to |file|. See |ERR_print_errors_cb| for the format. 193 OPENSSL_EXPORT void ERR_print_errors_fp(FILE *file); 194 195 196 // Clearing errors. 197 198 // ERR_clear_error clears the error queue for the current thread. 199 OPENSSL_EXPORT void ERR_clear_error(void); 200 201 // ERR_set_mark "marks" the most recent error for use with |ERR_pop_to_mark|. 202 // It returns one if an error was marked and zero if there are no errors. 203 OPENSSL_EXPORT int ERR_set_mark(void); 204 205 // ERR_pop_to_mark removes errors from the most recent to the least recent 206 // until (and not including) a "marked" error. It returns zero if no marked 207 // error was found (and thus all errors were removed) and one otherwise. Errors 208 // are marked using |ERR_set_mark|. 209 OPENSSL_EXPORT int ERR_pop_to_mark(void); 210 211 212 // Custom errors. 213 214 // ERR_get_next_error_library returns a value suitable for passing as the 215 // |library| argument to |ERR_put_error|. This is intended for code that wishes 216 // to push its own, non-standard errors to the error queue. 217 OPENSSL_EXPORT int ERR_get_next_error_library(void); 218 219 220 // Built-in library and reason codes. 221 222 // The following values are built-in library codes. 223 enum { 224 ERR_LIB_NONE = 1, 225 ERR_LIB_SYS, 226 ERR_LIB_BN, 227 ERR_LIB_RSA, 228 ERR_LIB_DH, 229 ERR_LIB_EVP, 230 ERR_LIB_BUF, 231 ERR_LIB_OBJ, 232 ERR_LIB_PEM, 233 ERR_LIB_DSA, 234 ERR_LIB_X509, 235 ERR_LIB_ASN1, 236 ERR_LIB_CONF, 237 ERR_LIB_CRYPTO, 238 ERR_LIB_EC, 239 ERR_LIB_SSL, 240 ERR_LIB_BIO, 241 ERR_LIB_PKCS7, 242 ERR_LIB_PKCS8, 243 ERR_LIB_X509V3, 244 ERR_LIB_RAND, 245 ERR_LIB_ENGINE, 246 ERR_LIB_OCSP, 247 ERR_LIB_UI, 248 ERR_LIB_COMP, 249 ERR_LIB_ECDSA, 250 ERR_LIB_ECDH, 251 ERR_LIB_HMAC, 252 ERR_LIB_DIGEST, 253 ERR_LIB_CIPHER, 254 ERR_LIB_HKDF, 255 ERR_LIB_TRUST_TOKEN, 256 ERR_LIB_CMS, 257 ERR_LIB_USER, 258 ERR_NUM_LIBS 259 }; 260 261 // The following reason codes used to denote an error occuring in another 262 // library. They are sometimes used for a stack trace. 263 #define ERR_R_SYS_LIB ERR_LIB_SYS 264 #define ERR_R_BN_LIB ERR_LIB_BN 265 #define ERR_R_RSA_LIB ERR_LIB_RSA 266 #define ERR_R_DH_LIB ERR_LIB_DH 267 #define ERR_R_EVP_LIB ERR_LIB_EVP 268 #define ERR_R_BUF_LIB ERR_LIB_BUF 269 #define ERR_R_OBJ_LIB ERR_LIB_OBJ 270 #define ERR_R_PEM_LIB ERR_LIB_PEM 271 #define ERR_R_DSA_LIB ERR_LIB_DSA 272 #define ERR_R_X509_LIB ERR_LIB_X509 273 #define ERR_R_ASN1_LIB ERR_LIB_ASN1 274 #define ERR_R_CONF_LIB ERR_LIB_CONF 275 #define ERR_R_CRYPTO_LIB ERR_LIB_CRYPTO 276 #define ERR_R_EC_LIB ERR_LIB_EC 277 #define ERR_R_SSL_LIB ERR_LIB_SSL 278 #define ERR_R_BIO_LIB ERR_LIB_BIO 279 #define ERR_R_PKCS7_LIB ERR_LIB_PKCS7 280 #define ERR_R_PKCS8_LIB ERR_LIB_PKCS8 281 #define ERR_R_X509V3_LIB ERR_LIB_X509V3 282 #define ERR_R_RAND_LIB ERR_LIB_RAND 283 #define ERR_R_DSO_LIB ERR_LIB_DSO 284 #define ERR_R_ENGINE_LIB ERR_LIB_ENGINE 285 #define ERR_R_OCSP_LIB ERR_LIB_OCSP 286 #define ERR_R_UI_LIB ERR_LIB_UI 287 #define ERR_R_COMP_LIB ERR_LIB_COMP 288 #define ERR_R_ECDSA_LIB ERR_LIB_ECDSA 289 #define ERR_R_ECDH_LIB ERR_LIB_ECDH 290 #define ERR_R_STORE_LIB ERR_LIB_STORE 291 #define ERR_R_FIPS_LIB ERR_LIB_FIPS 292 #define ERR_R_CMS_LIB ERR_LIB_CMS 293 #define ERR_R_TS_LIB ERR_LIB_TS 294 #define ERR_R_HMAC_LIB ERR_LIB_HMAC 295 #define ERR_R_JPAKE_LIB ERR_LIB_JPAKE 296 #define ERR_R_USER_LIB ERR_LIB_USER 297 #define ERR_R_DIGEST_LIB ERR_LIB_DIGEST 298 #define ERR_R_CIPHER_LIB ERR_LIB_CIPHER 299 #define ERR_R_HKDF_LIB ERR_LIB_HKDF 300 #define ERR_R_TRUST_TOKEN_LIB ERR_LIB_TRUST_TOKEN 301 302 // The following values are global reason codes. They may occur in any library. 303 #define ERR_R_FATAL 64 304 #define ERR_R_MALLOC_FAILURE (1 | ERR_R_FATAL) 305 #define ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED (2 | ERR_R_FATAL) 306 #define ERR_R_PASSED_NULL_PARAMETER (3 | ERR_R_FATAL) 307 #define ERR_R_INTERNAL_ERROR (4 | ERR_R_FATAL) 308 #define ERR_R_OVERFLOW (5 | ERR_R_FATAL) 309 310 311 // Deprecated functions. 312 313 // ERR_remove_state calls |ERR_clear_error|. 314 OPENSSL_EXPORT void ERR_remove_state(unsigned long pid); 315 316 // ERR_remove_thread_state clears the error queue for the current thread if 317 // |tid| is NULL. Otherwise it calls |assert(0)|, because it's no longer 318 // possible to delete the error queue for other threads. 319 // 320 // Use |ERR_clear_error| instead. Note error queues are deleted automatically on 321 // thread exit. You do not need to call this function to release memory. 322 OPENSSL_EXPORT void ERR_remove_thread_state(const CRYPTO_THREADID *tid); 323 324 // ERR_func_error_string returns the string "OPENSSL_internal". 325 OPENSSL_EXPORT const char *ERR_func_error_string(uint32_t packed_error); 326 327 // ERR_error_string behaves like |ERR_error_string_n| but |len| is implicitly 328 // |ERR_ERROR_STRING_BUF_LEN|. 329 // 330 // Additionally, if |buf| is NULL, the error string is placed in a static buffer 331 // which is returned. This is not thread-safe and only exists for backwards 332 // compatibility with legacy callers. The static buffer will be overridden by 333 // calls in other threads. 334 // 335 // Use |ERR_error_string_n| instead. 336 // 337 // TODO(fork): remove this function. 338 OPENSSL_EXPORT char *ERR_error_string(uint32_t packed_error, char *buf); 339 #define ERR_ERROR_STRING_BUF_LEN 120 340 341 // ERR_GET_FUNC returns zero. BoringSSL errors do not report a function code. 342 OPENSSL_INLINE int ERR_GET_FUNC(uint32_t packed_error) { 343 (void)packed_error; 344 return 0; 345 } 346 347 // ERR_TXT_* are provided for compatibility with code that assumes that it's 348 // using OpenSSL. 349 #define ERR_TXT_STRING ERR_FLAG_STRING 350 #define ERR_TXT_MALLOCED ERR_FLAG_MALLOCED 351 352 353 // Private functions. 354 355 // ERR_clear_system_error clears the system's error value (i.e. errno). 356 OPENSSL_EXPORT void ERR_clear_system_error(void); 357 358 // OPENSSL_PUT_ERROR is used by OpenSSL code to add an error to the error 359 // queue. 360 #define OPENSSL_PUT_ERROR(library, reason) \ 361 ERR_put_error(ERR_LIB_##library, 0, reason, __FILE__, __LINE__) 362 363 // OPENSSL_PUT_SYSTEM_ERROR is used by OpenSSL code to add an error from the 364 // operating system to the error queue. 365 // TODO(fork): include errno. 366 #define OPENSSL_PUT_SYSTEM_ERROR() \ 367 ERR_put_error(ERR_LIB_SYS, 0, 0, __FILE__, __LINE__); 368 369 // ERR_put_error adds an error to the error queue, dropping the least recent 370 // error if necessary for space reasons. 371 OPENSSL_EXPORT void ERR_put_error(int library, int unused, int reason, 372 const char *file, unsigned line); 373 374 // ERR_add_error_data takes a variable number (|count|) of const char* 375 // pointers, concatenates them and sets the result as the data on the most 376 // recent error. 377 OPENSSL_EXPORT void ERR_add_error_data(unsigned count, ...); 378 379 // ERR_add_error_dataf takes a printf-style format and arguments, and sets the 380 // result as the data on the most recent error. 381 OPENSSL_EXPORT void ERR_add_error_dataf(const char *format, ...) 382 OPENSSL_PRINTF_FORMAT_FUNC(1, 2); 383 384 // ERR_set_error_data sets the data on the most recent error to |data|, which 385 // must be a NUL-terminated string. |flags| must contain |ERR_FLAG_STRING|. If 386 // |flags| contains |ERR_FLAG_MALLOCED|, this function takes ownership of 387 // |data|, which must have been allocated with |OPENSSL_malloc|. Otherwise, it 388 // saves a copy of |data|. 389 // 390 // Note this differs from OpenSSL which, when |ERR_FLAG_MALLOCED| is unset, 391 // saves the pointer as-is and requires it remain valid for the lifetime of the 392 // address space. 393 OPENSSL_EXPORT void ERR_set_error_data(char *data, int flags); 394 395 // ERR_NUM_ERRORS is one more than the limit of the number of errors in the 396 // queue. 397 #define ERR_NUM_ERRORS 16 398 399 #define ERR_PACK(lib, reason) \ 400 (((((uint32_t)(lib)) & 0xff) << 24) | ((((uint32_t)(reason)) & 0xfff))) 401 402 // OPENSSL_DECLARE_ERROR_REASON is used by util/make_errors.h (which generates 403 // the error defines) to recognise that an additional reason value is needed. 404 // This is needed when the reason value is used outside of an 405 // |OPENSSL_PUT_ERROR| macro. The resulting define will be 406 // ${lib}_R_${reason}. 407 #define OPENSSL_DECLARE_ERROR_REASON(lib, reason) 408 409 410 #if defined(__cplusplus) 411 } // extern C 412 #endif 413 414 #endif // OPENSSL_HEADER_ERR_H