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