| 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_USER, |
| 257 | ERR_NUM_LIBS |
| 258 | }; |
| 259 |
| 260 | // The following reason codes used to denote an error occuring in another |
| 261 | // library. They are sometimes used for a stack trace. |
| 262 | #define ERR_R_SYS_LIB ERR_LIB_SYS |
| 263 | #define ERR_R_BN_LIB ERR_LIB_BN |
| 264 | #define ERR_R_RSA_LIB ERR_LIB_RSA |
| 265 | #define ERR_R_DH_LIB ERR_LIB_DH |
| 266 | #define ERR_R_EVP_LIB ERR_LIB_EVP |
| 267 | #define ERR_R_BUF_LIB ERR_LIB_BUF |
| 268 | #define ERR_R_OBJ_LIB ERR_LIB_OBJ |
| 269 | #define ERR_R_PEM_LIB ERR_LIB_PEM |
| 270 | #define ERR_R_DSA_LIB ERR_LIB_DSA |
| 271 | #define ERR_R_X509_LIB ERR_LIB_X509 |
| 272 | #define ERR_R_ASN1_LIB ERR_LIB_ASN1 |
| 273 | #define ERR_R_CONF_LIB ERR_LIB_CONF |
| 274 | #define ERR_R_CRYPTO_LIB ERR_LIB_CRYPTO |
| 275 | #define ERR_R_EC_LIB ERR_LIB_EC |
| 276 | #define ERR_R_SSL_LIB ERR_LIB_SSL |
| 277 | #define ERR_R_BIO_LIB ERR_LIB_BIO |
| 278 | #define ERR_R_PKCS7_LIB ERR_LIB_PKCS7 |
| 279 | #define ERR_R_PKCS8_LIB ERR_LIB_PKCS8 |
| 280 | #define ERR_R_X509V3_LIB ERR_LIB_X509V3 |
| 281 | #define ERR_R_RAND_LIB ERR_LIB_RAND |
| 282 | #define ERR_R_DSO_LIB ERR_LIB_DSO |
| 283 | #define ERR_R_ENGINE_LIB ERR_LIB_ENGINE |
| 284 | #define ERR_R_OCSP_LIB ERR_LIB_OCSP |
| 285 | #define ERR_R_UI_LIB ERR_LIB_UI |
| 286 | #define ERR_R_COMP_LIB ERR_LIB_COMP |
| 287 | #define ERR_R_ECDSA_LIB ERR_LIB_ECDSA |
| 288 | #define ERR_R_ECDH_LIB ERR_LIB_ECDH |
| 289 | #define ERR_R_STORE_LIB ERR_LIB_STORE |
| 290 | #define ERR_R_FIPS_LIB ERR_LIB_FIPS |
| 291 | #define ERR_R_CMS_LIB ERR_LIB_CMS |
| 292 | #define ERR_R_TS_LIB ERR_LIB_TS |
| 293 | #define ERR_R_HMAC_LIB ERR_LIB_HMAC |
| 294 | #define ERR_R_JPAKE_LIB ERR_LIB_JPAKE |
| 295 | #define ERR_R_USER_LIB ERR_LIB_USER |
| 296 | #define ERR_R_DIGEST_LIB ERR_LIB_DIGEST |
| 297 | #define ERR_R_CIPHER_LIB ERR_LIB_CIPHER |
| 298 | #define ERR_R_HKDF_LIB ERR_LIB_HKDF |
| 299 | #define ERR_R_TRUST_TOKEN_LIB ERR_LIB_TRUST_TOKEN |
| 300 |
| 301 | // The following values are global reason codes. They may occur in any library. |
| 302 | #define ERR_R_FATAL 64 |
| 303 | #define ERR_R_MALLOC_FAILURE (1 | ERR_R_FATAL) |
| 304 | #define ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED (2 | ERR_R_FATAL) |
| 305 | #define ERR_R_PASSED_NULL_PARAMETER (3 | ERR_R_FATAL) |
| 306 | #define ERR_R_INTERNAL_ERROR (4 | ERR_R_FATAL) |
| 307 | #define ERR_R_OVERFLOW (5 | ERR_R_FATAL) |
| 308 |
| 309 |
| 310 | // Deprecated functions. |
| 311 |
| 312 | // ERR_remove_state calls |ERR_clear_error|. |
| 313 | OPENSSL_EXPORT void ERR_remove_state(unsigned long pid); |
| 314 |
| 315 | // ERR_remove_thread_state clears the error queue for the current thread if |
| 316 | // |tid| is NULL. Otherwise it calls |assert(0)|, because it's no longer |
| 317 | // possible to delete the error queue for other threads. |
| 318 | // |
| 319 | // Use |ERR_clear_error| instead. Note error queues are deleted automatically on |
| 320 | // thread exit. You do not need to call this function to release memory. |
| 321 | OPENSSL_EXPORT void ERR_remove_thread_state(const CRYPTO_THREADID *tid); |
| 322 |
| 323 | // ERR_func_error_string returns the string "OPENSSL_internal". |
| 324 | OPENSSL_EXPORT const char *ERR_func_error_string(uint32_t packed_error); |
| 325 |
| 326 | // ERR_error_string behaves like |ERR_error_string_n| but |len| is implicitly |
| 327 | // |ERR_ERROR_STRING_BUF_LEN|. |
| 328 | // |
| 329 | // Additionally, if |buf| is NULL, the error string is placed in a static buffer |
| 330 | // which is returned. This is not thread-safe and only exists for backwards |
| 331 | // compatibility with legacy callers. The static buffer will be overridden by |
| 332 | // calls in other threads. |
| 333 | // |
| 334 | // Use |ERR_error_string_n| instead. |
| 335 | // |
| 336 | // TODO(fork): remove this function. |
| 337 | OPENSSL_EXPORT char *ERR_error_string(uint32_t packed_error, char *buf); |
| 338 | #define ERR_ERROR_STRING_BUF_LEN 120 |
| 339 |
| 340 | // ERR_GET_FUNC returns zero. BoringSSL errors do not report a function code. |
| 341 | OPENSSL_INLINE int ERR_GET_FUNC(uint32_t packed_error) { |
| 342 | (void)packed_error; |
| 343 | return 0; |
| 344 | } |
| 345 |
| 346 | // ERR_TXT_* are provided for compatibility with code that assumes that it's |
| 347 | // using OpenSSL. |
| 348 | #define ERR_TXT_STRING ERR_FLAG_STRING |
| 349 | #define ERR_TXT_MALLOCED ERR_FLAG_MALLOCED |
| 350 |
| 351 |
| 352 | // Private functions. |
| 353 |
| 354 | // ERR_clear_system_error clears the system's error value (i.e. errno). |
| 355 | OPENSSL_EXPORT void ERR_clear_system_error(void); |
| 356 |
| 357 | // OPENSSL_PUT_ERROR is used by OpenSSL code to add an error to the error |
| 358 | // queue. |
| 359 | #define OPENSSL_PUT_ERROR(library, reason) \ |
| 360 | ERR_put_error(ERR_LIB_##library, 0, reason, __FILE__, __LINE__) |
| 361 |
| 362 | // OPENSSL_PUT_SYSTEM_ERROR is used by OpenSSL code to add an error from the |
| 363 | // operating system to the error queue. |
| 364 | // TODO(fork): include errno. |
| 365 | #define OPENSSL_PUT_SYSTEM_ERROR() \ |
| 366 | ERR_put_error(ERR_LIB_SYS, 0, 0, __FILE__, __LINE__); |
| 367 |
| 368 | // ERR_put_error adds an error to the error queue, dropping the least recent |
| 369 | // error if necessary for space reasons. |
| 370 | OPENSSL_EXPORT void ERR_put_error(int library, int unused, int reason, |
| 371 | const char *file, unsigned line); |
| 372 |
| 373 | // ERR_add_error_data takes a variable number (|count|) of const char* |
| 374 | // pointers, concatenates them and sets the result as the data on the most |
| 375 | // recent error. |
| 376 | OPENSSL_EXPORT void ERR_add_error_data(unsigned count, ...); |
| 377 |
| 378 | // ERR_add_error_dataf takes a printf-style format and arguments, and sets the |
| 379 | // result as the data on the most recent error. |
| 380 | OPENSSL_EXPORT void ERR_add_error_dataf(const char *format, ...) |
| 381 | OPENSSL_PRINTF_FORMAT_FUNC(1, 2); |
| 382 |
| 383 | // ERR_set_error_data sets the data on the most recent error to |data|, which |
| 384 | // must be a NUL-terminated string. |flags| must contain |ERR_FLAG_STRING|. If |
| 385 | // |flags| contains |ERR_FLAG_MALLOCED|, this function takes ownership of |
| 386 | // |data|, which must have been allocated with |OPENSSL_malloc|. Otherwise, it |
| 387 | // saves a copy of |data|. |
| 388 | // |
| 389 | // Note this differs from OpenSSL which, when |ERR_FLAG_MALLOCED| is unset, |
| 390 | // saves the pointer as-is and requires it remain valid for the lifetime of the |
| 391 | // address space. |
| 392 | OPENSSL_EXPORT void ERR_set_error_data(char *data, int flags); |
| 393 |
| 394 | // ERR_NUM_ERRORS is one more than the limit of the number of errors in the |
| 395 | // queue. |
| 396 | #define ERR_NUM_ERRORS 16 |
| 397 |
| 398 | #define ERR_PACK(lib, reason) \ |
| 399 | (((((uint32_t)(lib)) & 0xff) << 24) | ((((uint32_t)(reason)) & 0xfff))) |
| 400 |
| 401 | // OPENSSL_DECLARE_ERROR_REASON is used by util/make_errors.h (which generates |
| 402 | // the error defines) to recognise that an additional reason value is needed. |
| 403 | // This is needed when the reason value is used outside of an |
| 404 | // |OPENSSL_PUT_ERROR| macro. The resulting define will be |
| 405 | // ${lib}_R_${reason}. |
| 406 | #define OPENSSL_DECLARE_ERROR_REASON(lib, reason) |
| 407 |
| 408 |
| 409 | #if defined(__cplusplus) |
| 410 | } // extern C |
| 411 | #endif |
| 412 |
| 413 | #endif // OPENSSL_HEADER_ERR_H |
| 414 |