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