blob: 5de3cbd77fc2e7ab834ddd99087c823b974852b5 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
109#ifndef OPENSSL_HEADER_ERR_H
110#define OPENSSL_HEADER_ERR_H
111
Brian Smith83a82982015-04-09 16:21:10 -1000112#include <stdio.h>
113
Adam Langley95c29f32014-06-20 12:00:00 -0700114#include <openssl/base.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
116#if defined(__cplusplus)
117extern "C" {
118#endif
119
120
David Benjamin4512b792017-08-18 19:21:50 -0400121// Error queue handling functions.
122//
123// Errors in OpenSSL are generally signaled by the return value of a function.
124// When a function fails it may add an entry to a per-thread error queue,
125// which is managed by the functions in this header.
126//
127// Each error contains:
128// 1) The library (i.e. ec, pem, rsa) which created it.
129// 2) The file and line number of the call that added the error.
130// 3) A pointer to some error specific data, which may be NULL.
131//
132// The library identifier and reason code are packed in a uint32_t and there
133// exist various functions for unpacking it.
134//
135// The typical behaviour is that an error will occur deep in a call queue and
136// that code will push an error onto the error queue. As the error queue
137// unwinds, other functions will push their own errors. Thus, the "least
138// recent" error is the most specific and the other errors will provide a
139// backtrace of sorts.
Adam Langley95c29f32014-06-20 12:00:00 -0700140
141
David Benjamin4512b792017-08-18 19:21:50 -0400142// Startup and shutdown.
Adam Langley95c29f32014-06-20 12:00:00 -0700143
David Benjamin4512b792017-08-18 19:21:50 -0400144// ERR_load_BIO_strings does nothing.
145//
146// TODO(fork): remove. libjingle calls this.
Matt Braithwaite9febf192015-04-30 13:04:06 -0700147OPENSSL_EXPORT void ERR_load_BIO_strings(void);
148
David Benjamin4512b792017-08-18 19:21:50 -0400149// ERR_load_ERR_strings does nothing.
Matt Braithwaite9febf192015-04-30 13:04:06 -0700150OPENSSL_EXPORT void ERR_load_ERR_strings(void);
151
David Benjamin4512b792017-08-18 19:21:50 -0400152// ERR_load_crypto_strings does nothing.
David Benjaminc44d2f42014-08-20 16:24:00 -0400153OPENSSL_EXPORT void ERR_load_crypto_strings(void);
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjamin4512b792017-08-18 19:21:50 -0400155// ERR_free_strings does nothing.
David Benjaminc44d2f42014-08-20 16:24:00 -0400156OPENSSL_EXPORT void ERR_free_strings(void);
Adam Langley95c29f32014-06-20 12:00:00 -0700157
158
David Benjamin4512b792017-08-18 19:21:50 -0400159// Reading and formatting errors.
Adam Langley95c29f32014-06-20 12:00:00 -0700160
David Benjamin51776b02017-10-01 21:36:08 -0400161// ERR_GET_LIB returns the library code for the error. This is one of
162// the |ERR_LIB_*| values.
163#define ERR_GET_LIB(packed_error) ((int)(((packed_error) >> 24) & 0xff))
164
165// ERR_GET_REASON returns the reason code for the error. This is one of
166// library-specific |LIB_R_*| values where |LIB| is the library (see
167// |ERR_GET_LIB|). Note that reason codes are specific to the library.
168#define ERR_GET_REASON(packed_error) ((int)((packed_error) & 0xfff))
169
David Benjamin4512b792017-08-18 19:21:50 -0400170// ERR_get_error gets the packed error code for the least recent error and
171// removes that error from the queue. If there are no errors in the queue then
172// it returns zero.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700173OPENSSL_EXPORT uint32_t ERR_get_error(void);
Adam Langley95c29f32014-06-20 12:00:00 -0700174
David Benjamin4512b792017-08-18 19:21:50 -0400175// ERR_get_error_line acts like |ERR_get_error|, except that the file and line
176// number of the call that added the error are also returned.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700177OPENSSL_EXPORT uint32_t ERR_get_error_line(const char **file, int *line);
Adam Langley95c29f32014-06-20 12:00:00 -0700178
David Benjamina65c2522017-10-03 17:34:37 -0400179// ERR_FLAG_STRING means that the |data| member is a NUL-terminated string that
180// can be printed. This is always set if |data| is non-NULL.
181#define ERR_FLAG_STRING 1
182
David Benjamin4512b792017-08-18 19:21:50 -0400183// ERR_get_error_line_data acts like |ERR_get_error_line|, but also returns the
184// error-specific data pointer and flags. The flags are a bitwise-OR of
185// |ERR_FLAG_*| values. The error-specific data is owned by the error queue
186// and the pointer becomes invalid after the next call that affects the same
187// thread's error queue. If |*flags| contains |ERR_FLAG_STRING| then |*data| is
188// human-readable.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700189OPENSSL_EXPORT uint32_t ERR_get_error_line_data(const char **file, int *line,
Adam Langley5f1374e2014-10-01 15:21:01 -0700190 const char **data, int *flags);
Adam Langley95c29f32014-06-20 12:00:00 -0700191
David Benjamin4512b792017-08-18 19:21:50 -0400192// The "peek" functions act like the |ERR_get_error| functions, above, but they
193// do not remove the error from the queue.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700194OPENSSL_EXPORT uint32_t ERR_peek_error(void);
195OPENSSL_EXPORT uint32_t ERR_peek_error_line(const char **file, int *line);
196OPENSSL_EXPORT uint32_t ERR_peek_error_line_data(const char **file, int *line,
197 const char **data, int *flags);
Adam Langley95c29f32014-06-20 12:00:00 -0700198
David Benjamin4512b792017-08-18 19:21:50 -0400199// The "peek last" functions act like the "peek" functions, above, except that
200// they return the most recent error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700201OPENSSL_EXPORT uint32_t ERR_peek_last_error(void);
202OPENSSL_EXPORT uint32_t ERR_peek_last_error_line(const char **file, int *line);
David Benjamin5b082e82014-12-26 00:54:52 -0500203OPENSSL_EXPORT uint32_t ERR_peek_last_error_line_data(const char **file,
204 int *line,
205 const char **data,
206 int *flags);
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjamin4512b792017-08-18 19:21:50 -0400208// ERR_error_string_n generates a human-readable string representing
209// |packed_error| and places it at |buf|. It writes at most |len| bytes
210// (including the terminating NUL) and truncates the string if necessary. If
211// |len| is greater than zero then |buf| is always NUL terminated.
212//
213// The string will have the following format:
214//
215// error:[error code]:[library name]:OPENSSL_internal:[reason string]
216//
217// error code is an 8 digit hexadecimal number; library name and reason string
218// are ASCII text.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700219OPENSSL_EXPORT void ERR_error_string_n(uint32_t packed_error, char *buf,
220 size_t len);
Adam Langley95c29f32014-06-20 12:00:00 -0700221
David Benjamin4512b792017-08-18 19:21:50 -0400222// ERR_lib_error_string returns a string representation of the library that
223// generated |packed_error|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700224OPENSSL_EXPORT const char *ERR_lib_error_string(uint32_t packed_error);
Adam Langley95c29f32014-06-20 12:00:00 -0700225
David Benjamin4512b792017-08-18 19:21:50 -0400226// ERR_reason_error_string returns a string representation of the reason for
227// |packed_error|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700228OPENSSL_EXPORT const char *ERR_reason_error_string(uint32_t packed_error);
Adam Langley95c29f32014-06-20 12:00:00 -0700229
David Benjamin4512b792017-08-18 19:21:50 -0400230// ERR_print_errors_callback_t is the type of a function used by
231// |ERR_print_errors_cb|. It takes a pointer to a human readable string (and
232// its length) that describes an entry in the error queue. The |ctx| argument
233// is an opaque pointer given to |ERR_print_errors_cb|.
234//
235// It should return one on success or zero on error, which will stop the
236// iteration over the error queue.
Adam Langley95c29f32014-06-20 12:00:00 -0700237typedef int (*ERR_print_errors_callback_t)(const char *str, size_t len,
238 void *ctx);
239
David Benjaminbe90bf72017-09-05 13:21:09 -0400240// ERR_print_errors_cb clears the current thread's error queue, calling
241// |callback| with a string representation of each error, from the least recent
242// to the most recent error.
David Benjamin4512b792017-08-18 19:21:50 -0400243//
244// The string will have the following format (which differs from
245// |ERR_error_string|):
246//
David Benjaminbe90bf72017-09-05 13:21:09 -0400247// [thread id]:error:[error code]:[library name]:OPENSSL_internal:[reason string]:[file]:[line number]:[optional string data]
David Benjamin4512b792017-08-18 19:21:50 -0400248//
249// The callback can return one to continue the iteration or zero to stop it.
250// The |ctx| argument is an opaque value that is passed through to the
251// callback.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700252OPENSSL_EXPORT void ERR_print_errors_cb(ERR_print_errors_callback_t callback,
253 void *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700254
David Benjaminbe90bf72017-09-05 13:21:09 -0400255// ERR_print_errors_fp clears the current thread's error queue, printing each
256// error to |file|. See |ERR_print_errors_cb| for the format.
Brian Smith83a82982015-04-09 16:21:10 -1000257OPENSSL_EXPORT void ERR_print_errors_fp(FILE *file);
258
David Benjamin2079ee72015-09-15 01:34:37 -0400259
David Benjamin4512b792017-08-18 19:21:50 -0400260// Clearing errors.
Adam Langley95c29f32014-06-20 12:00:00 -0700261
David Benjamin4512b792017-08-18 19:21:50 -0400262// ERR_clear_error clears the error queue for the current thread.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700263OPENSSL_EXPORT void ERR_clear_error(void);
Adam Langley95c29f32014-06-20 12:00:00 -0700264
David Benjamin4512b792017-08-18 19:21:50 -0400265// ERR_remove_thread_state clears the error queue for the current thread if
266// |tid| is NULL. Otherwise it calls |assert(0)|, because it's no longer
267// possible to delete the error queue for other threads.
268//
269// Error queues are thread-local data and are deleted automatically. You do not
270// need to call this function. Use |ERR_clear_error|.
Adam Langley03d31ed2014-08-05 16:54:47 -0700271OPENSSL_EXPORT void ERR_remove_thread_state(const CRYPTO_THREADID *tid);
272
David Benjamin51776b02017-10-01 21:36:08 -0400273// ERR_set_mark "marks" the most recent error for use with |ERR_pop_to_mark|.
274// It returns one if an error was marked and zero if there are no errors.
275OPENSSL_EXPORT int ERR_set_mark(void);
276
277// ERR_pop_to_mark removes errors from the most recent to the least recent
278// until (and not including) a "marked" error. It returns zero if no marked
279// error was found (and thus all errors were removed) and one otherwise. Errors
280// are marked using |ERR_set_mark|.
281OPENSSL_EXPORT int ERR_pop_to_mark(void);
282
Adam Langley95c29f32014-06-20 12:00:00 -0700283
David Benjamin4512b792017-08-18 19:21:50 -0400284// Custom errors.
Adam Langley40426672014-07-01 16:48:28 -0700285
David Benjamin4512b792017-08-18 19:21:50 -0400286// ERR_get_next_error_library returns a value suitable for passing as the
287// |library| argument to |ERR_put_error|. This is intended for code that wishes
288// to push its own, non-standard errors to the error queue.
David Benjaminc44d2f42014-08-20 16:24:00 -0400289OPENSSL_EXPORT int ERR_get_next_error_library(void);
Adam Langley40426672014-07-01 16:48:28 -0700290
291
David Benjamin51776b02017-10-01 21:36:08 -0400292// Built-in library and reason codes.
293
294// The following values are built-in library codes.
295enum {
296 ERR_LIB_NONE = 1,
297 ERR_LIB_SYS,
298 ERR_LIB_BN,
299 ERR_LIB_RSA,
300 ERR_LIB_DH,
301 ERR_LIB_EVP,
302 ERR_LIB_BUF,
303 ERR_LIB_OBJ,
304 ERR_LIB_PEM,
305 ERR_LIB_DSA,
306 ERR_LIB_X509,
307 ERR_LIB_ASN1,
308 ERR_LIB_CONF,
309 ERR_LIB_CRYPTO,
310 ERR_LIB_EC,
311 ERR_LIB_SSL,
312 ERR_LIB_BIO,
313 ERR_LIB_PKCS7,
314 ERR_LIB_PKCS8,
315 ERR_LIB_X509V3,
316 ERR_LIB_RAND,
317 ERR_LIB_ENGINE,
318 ERR_LIB_OCSP,
319 ERR_LIB_UI,
320 ERR_LIB_COMP,
321 ERR_LIB_ECDSA,
322 ERR_LIB_ECDH,
323 ERR_LIB_HMAC,
324 ERR_LIB_DIGEST,
325 ERR_LIB_CIPHER,
326 ERR_LIB_HKDF,
327 ERR_LIB_USER,
328 ERR_NUM_LIBS
329};
330
331// The following reason codes used to denote an error occuring in another
332// library. They are sometimes used for a stack trace.
333#define ERR_R_SYS_LIB ERR_LIB_SYS
334#define ERR_R_BN_LIB ERR_LIB_BN
335#define ERR_R_RSA_LIB ERR_LIB_RSA
336#define ERR_R_DH_LIB ERR_LIB_DH
337#define ERR_R_EVP_LIB ERR_LIB_EVP
338#define ERR_R_BUF_LIB ERR_LIB_BUF
339#define ERR_R_OBJ_LIB ERR_LIB_OBJ
340#define ERR_R_PEM_LIB ERR_LIB_PEM
341#define ERR_R_DSA_LIB ERR_LIB_DSA
342#define ERR_R_X509_LIB ERR_LIB_X509
343#define ERR_R_ASN1_LIB ERR_LIB_ASN1
344#define ERR_R_CONF_LIB ERR_LIB_CONF
345#define ERR_R_CRYPTO_LIB ERR_LIB_CRYPTO
346#define ERR_R_EC_LIB ERR_LIB_EC
347#define ERR_R_SSL_LIB ERR_LIB_SSL
348#define ERR_R_BIO_LIB ERR_LIB_BIO
349#define ERR_R_PKCS7_LIB ERR_LIB_PKCS7
350#define ERR_R_PKCS8_LIB ERR_LIB_PKCS8
351#define ERR_R_X509V3_LIB ERR_LIB_X509V3
352#define ERR_R_RAND_LIB ERR_LIB_RAND
353#define ERR_R_DSO_LIB ERR_LIB_DSO
354#define ERR_R_ENGINE_LIB ERR_LIB_ENGINE
355#define ERR_R_OCSP_LIB ERR_LIB_OCSP
356#define ERR_R_UI_LIB ERR_LIB_UI
357#define ERR_R_COMP_LIB ERR_LIB_COMP
358#define ERR_R_ECDSA_LIB ERR_LIB_ECDSA
359#define ERR_R_ECDH_LIB ERR_LIB_ECDH
360#define ERR_R_STORE_LIB ERR_LIB_STORE
361#define ERR_R_FIPS_LIB ERR_LIB_FIPS
362#define ERR_R_CMS_LIB ERR_LIB_CMS
363#define ERR_R_TS_LIB ERR_LIB_TS
364#define ERR_R_HMAC_LIB ERR_LIB_HMAC
365#define ERR_R_JPAKE_LIB ERR_LIB_JPAKE
366#define ERR_R_USER_LIB ERR_LIB_USER
367#define ERR_R_DIGEST_LIB ERR_LIB_DIGEST
368#define ERR_R_CIPHER_LIB ERR_LIB_CIPHER
369#define ERR_R_HKDF_LIB ERR_LIB_HKDF
370
371// The following values are global reason codes. They may occur in any library.
372#define ERR_R_FATAL 64
373#define ERR_R_MALLOC_FAILURE (1 | ERR_R_FATAL)
374#define ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED (2 | ERR_R_FATAL)
375#define ERR_R_PASSED_NULL_PARAMETER (3 | ERR_R_FATAL)
376#define ERR_R_INTERNAL_ERROR (4 | ERR_R_FATAL)
377#define ERR_R_OVERFLOW (5 | ERR_R_FATAL)
378
379
David Benjamin4512b792017-08-18 19:21:50 -0400380// Deprecated functions.
Matt Braithwaite9b68e722015-05-06 13:57:29 -0700381
David Benjamin4512b792017-08-18 19:21:50 -0400382// ERR_remove_state calls |ERR_clear_error|.
Matt Braithwaite9b68e722015-05-06 13:57:29 -0700383OPENSSL_EXPORT void ERR_remove_state(unsigned long pid);
384
David Benjamin4512b792017-08-18 19:21:50 -0400385// ERR_func_error_string returns the string "OPENSSL_internal".
David Benjamin34248d42015-06-28 23:36:21 -0400386OPENSSL_EXPORT const char *ERR_func_error_string(uint32_t packed_error);
387
David Benjamin4512b792017-08-18 19:21:50 -0400388// ERR_error_string behaves like |ERR_error_string_n| but |len| is implicitly
389// |ERR_ERROR_STRING_BUF_LEN| and it returns |buf|. If |buf| is NULL, the error
390// string is placed in a static buffer which is returned. (The static buffer may
391// be overridden by concurrent calls in other threads so this form should not be
392// used.)
393//
394// Use |ERR_error_string_n| instead.
395//
396// TODO(fork): remove this function.
David Benjamin0d5bf8d2017-04-23 16:00:20 -0400397OPENSSL_EXPORT char *ERR_error_string(uint32_t packed_error, char *buf);
398#define ERR_ERROR_STRING_BUF_LEN 256
399
David Benjamin51776b02017-10-01 21:36:08 -0400400// ERR_GET_FUNC returns zero. BoringSSL errors do not report a function code.
401#define ERR_GET_FUNC(packed_error) 0
402
David Benjamina65c2522017-10-03 17:34:37 -0400403// ERR_TXT_STRING is provided for compatibility with code that assumes that
404// it's using OpenSSL.
405#define ERR_TXT_STRING ERR_FLAG_STRING
406
Matt Braithwaite9b68e722015-05-06 13:57:29 -0700407
David Benjamin4512b792017-08-18 19:21:50 -0400408// Private functions.
Adam Langley95c29f32014-06-20 12:00:00 -0700409
David Benjamin4512b792017-08-18 19:21:50 -0400410// ERR_clear_system_error clears the system's error value (i.e. errno).
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700411OPENSSL_EXPORT void ERR_clear_system_error(void);
Adam Langley95c29f32014-06-20 12:00:00 -0700412
David Benjamin4512b792017-08-18 19:21:50 -0400413// OPENSSL_PUT_ERROR is used by OpenSSL code to add an error to the error
414// queue.
David Benjamin3fc138e2015-10-28 18:03:21 -0400415#define OPENSSL_PUT_ERROR(library, reason) \
416 ERR_put_error(ERR_LIB_##library, 0, reason, __FILE__, __LINE__)
Adam Langley95c29f32014-06-20 12:00:00 -0700417
David Benjamin4512b792017-08-18 19:21:50 -0400418// OPENSSL_PUT_SYSTEM_ERROR is used by OpenSSL code to add an error from the
419// operating system to the error queue.
420// TODO(fork): include errno.
David Benjamin3fc138e2015-10-28 18:03:21 -0400421#define OPENSSL_PUT_SYSTEM_ERROR() \
422 ERR_put_error(ERR_LIB_SYS, 0, 0, __FILE__, __LINE__);
Adam Langley95c29f32014-06-20 12:00:00 -0700423
David Benjamin4512b792017-08-18 19:21:50 -0400424// ERR_put_error adds an error to the error queue, dropping the least recent
425// error if necessary for space reasons.
David Benjamin3fc138e2015-10-28 18:03:21 -0400426OPENSSL_EXPORT void ERR_put_error(int library, int unused, int reason,
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700427 const char *file, unsigned line);
Adam Langley95c29f32014-06-20 12:00:00 -0700428
David Benjamin4512b792017-08-18 19:21:50 -0400429// ERR_add_error_data takes a variable number (|count|) of const char*
430// pointers, concatenates them and sets the result as the data on the most
431// recent error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700432OPENSSL_EXPORT void ERR_add_error_data(unsigned count, ...);
Adam Langley95c29f32014-06-20 12:00:00 -0700433
David Benjamin4512b792017-08-18 19:21:50 -0400434// ERR_add_error_dataf takes a printf-style format and arguments, and sets the
435// result as the data on the most recent error.
Brian Smith8d3c43e2016-01-17 10:14:08 -1000436OPENSSL_EXPORT void ERR_add_error_dataf(const char *format, ...)
437 OPENSSL_PRINTF_FORMAT_FUNC(1, 2);
Adam Langley95c29f32014-06-20 12:00:00 -0700438
Adam Langley95c29f32014-06-20 12:00:00 -0700439struct err_error_st {
David Benjamin4512b792017-08-18 19:21:50 -0400440 // file contains the filename where the error occurred.
Adam Langley95c29f32014-06-20 12:00:00 -0700441 const char *file;
David Benjamine1c3dad2017-09-28 16:33:09 -0400442 // data contains a NUL-terminated string with optional data. It must be freed
443 // with |OPENSSL_free|.
Adam Langley95c29f32014-06-20 12:00:00 -0700444 char *data;
David Benjamin4512b792017-08-18 19:21:50 -0400445 // packed contains the error library and reason, as packed by ERR_PACK.
Adam Langley95c29f32014-06-20 12:00:00 -0700446 uint32_t packed;
David Benjamin4512b792017-08-18 19:21:50 -0400447 // line contains the line number where the error occurred.
Adam Langley95c29f32014-06-20 12:00:00 -0700448 uint16_t line;
David Benjamina65c2522017-10-03 17:34:37 -0400449 // mark indicates a reversion point in the queue. See |ERR_pop_to_mark|.
450 unsigned mark : 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700451};
452
David Benjamin4512b792017-08-18 19:21:50 -0400453// ERR_NUM_ERRORS is the limit of the number of errors in the queue.
Adam Langley95c29f32014-06-20 12:00:00 -0700454#define ERR_NUM_ERRORS 16
455
David Benjamin4512b792017-08-18 19:21:50 -0400456// err_state_st (aka |ERR_STATE|) contains the per-thread, error queue.
Adam Langley95c29f32014-06-20 12:00:00 -0700457typedef struct err_state_st {
David Benjamin4512b792017-08-18 19:21:50 -0400458 // errors contains the ERR_NUM_ERRORS most recent errors, organised as a ring
459 // buffer.
Adam Langley95c29f32014-06-20 12:00:00 -0700460 struct err_error_st errors[ERR_NUM_ERRORS];
David Benjamin4512b792017-08-18 19:21:50 -0400461 // top contains the index one past the most recent error. If |top| equals
462 // |bottom| then the queue is empty.
Adam Langley95c29f32014-06-20 12:00:00 -0700463 unsigned top;
David Benjamin4512b792017-08-18 19:21:50 -0400464 // bottom contains the index of the last error in the queue.
Adam Langley95c29f32014-06-20 12:00:00 -0700465 unsigned bottom;
Adam Langley5f1374e2014-10-01 15:21:01 -0700466
David Benjamin4512b792017-08-18 19:21:50 -0400467 // to_free, if not NULL, contains a pointer owned by this structure that was
468 // previously a |data| pointer of one of the elements of |errors|.
Adam Langley5f1374e2014-10-01 15:21:01 -0700469 void *to_free;
Adam Langley95c29f32014-06-20 12:00:00 -0700470} ERR_STATE;
471
David Benjamin34248d42015-06-28 23:36:21 -0400472#define ERR_PACK(lib, reason) \
David Benjaminb1133e92016-10-18 13:05:01 -0400473 (((((uint32_t)(lib)) & 0xff) << 24) | ((((uint32_t)(reason)) & 0xfff)))
Adam Langley95c29f32014-06-20 12:00:00 -0700474
David Benjamin4512b792017-08-18 19:21:50 -0400475// OPENSSL_DECLARE_ERROR_REASON is used by util/make_errors.h (which generates
476// the error defines) to recognise that an additional reason value is needed.
477// This is needed when the reason value is used outside of an
478// |OPENSSL_PUT_ERROR| macro. The resulting define will be
479// ${lib}_R_${reason}.
Adam Langley95c29f32014-06-20 12:00:00 -0700480#define OPENSSL_DECLARE_ERROR_REASON(lib, reason)
481
Adam Langley95c29f32014-06-20 12:00:00 -0700482
Adam Langley95c29f32014-06-20 12:00:00 -0700483#if defined(__cplusplus)
David Benjamin4512b792017-08-18 19:21:50 -0400484} // extern C
Adam Langley95c29f32014-06-20 12:00:00 -0700485#endif
486
David Benjamin4512b792017-08-18 19:21:50 -0400487#endif // OPENSSL_HEADER_ERR_H