• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #ifndef OPENSSL_HEADER_ERR_H
11 #define OPENSSL_HEADER_ERR_H
12 
13 #include <stdio.h>
14 
15 #include <openssl/base.h>
16 
17 #if defined(__cplusplus)
18 extern "C" {
19 #endif
20 
21 
22 // Error queue handling functions.
23 //
24 // Errors in OpenSSL are generally signaled by the return value of a function.
25 // When a function fails it may add an entry to a per-thread error queue,
26 // which is managed by the functions in this header.
27 //
28 // Each error contains:
29 //   1) The library (i.e. ec, pem, rsa) which created it.
30 //   2) The file and line number of the call that added the error.
31 //   3) A pointer to some error specific data, which may be NULL.
32 //
33 // The library identifier and reason code are packed in a uint32_t and there
34 // exist various functions for unpacking it.
35 //
36 // The typical behaviour is that an error will occur deep in a call queue and
37 // that code will push an error onto the error queue. As the error queue
38 // unwinds, other functions will push their own errors. Thus, the "least
39 // recent" error is the most specific and the other errors will provide a
40 // backtrace of sorts.
41 
42 
43 // Startup and shutdown.
44 
45 // ERR_load_BIO_strings does nothing.
46 //
47 // TODO(fork): remove. libjingle calls this.
48 OPENSSL_EXPORT void ERR_load_BIO_strings(void);
49 
50 // ERR_load_ERR_strings does nothing.
51 OPENSSL_EXPORT void ERR_load_ERR_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.
ERR_GET_LIB(uint32_t packed_error)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.
ERR_GET_REASON(uint32_t packed_error)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_lib_symbol_name returns the symbol name of library that generated
149 // |packed_error|, or NULL if unrecognized. For example, an error from
150 // |ERR_LIB_EVP| would return "EVP".
151 OPENSSL_EXPORT const char *ERR_lib_symbol_name(uint32_t packed_error);
152 
153 // ERR_reason_symbol_name returns the symbol name of the reason for
154 // |packed_error|, or NULL if unrecognized. For example, |ERR_R_INTERNAL_ERROR|
155 // would return "INTERNAL_ERROR".
156 //
157 // Errors from the |ERR_LIB_SYS| library are typically |errno| values and will
158 // return NULL. User-defined errors will also return NULL.
159 OPENSSL_EXPORT const char *ERR_reason_symbol_name(uint32_t packed_error);
160 
161 // ERR_print_errors_callback_t is the type of a function used by
162 // |ERR_print_errors_cb|. It takes a pointer to a human readable string (and
163 // its length) that describes an entry in the error queue. The |ctx| argument
164 // is an opaque pointer given to |ERR_print_errors_cb|.
165 //
166 // It should return one on success or zero on error, which will stop the
167 // iteration over the error queue.
168 typedef int (*ERR_print_errors_callback_t)(const char *str, size_t len,
169                                            void *ctx);
170 
171 // ERR_print_errors_cb clears the current thread's error queue, calling
172 // |callback| with a string representation of each error, from the least recent
173 // to the most recent error.
174 //
175 // The string will have the following format (which differs from
176 // |ERR_error_string|):
177 //
178 //   [thread id]:error:[error code]:[library name]:OPENSSL_internal:[reason string]:[file]:[line number]:[optional string data]
179 //
180 // The callback can return one to continue the iteration or zero to stop it.
181 // The |ctx| argument is an opaque value that is passed through to the
182 // callback.
183 OPENSSL_EXPORT void ERR_print_errors_cb(ERR_print_errors_callback_t callback,
184                                         void *ctx);
185 
186 // ERR_print_errors_fp clears the current thread's error queue, printing each
187 // error to |file|. See |ERR_print_errors_cb| for the format.
188 OPENSSL_EXPORT void ERR_print_errors_fp(FILE *file);
189 
190 
191 // Clearing errors.
192 
193 // ERR_clear_error clears the error queue for the current thread.
194 OPENSSL_EXPORT void ERR_clear_error(void);
195 
196 // ERR_set_mark "marks" the most recent error for use with |ERR_pop_to_mark|.
197 // It returns one if an error was marked and zero if there are no errors.
198 OPENSSL_EXPORT int ERR_set_mark(void);
199 
200 // ERR_pop_to_mark removes errors from the most recent to the least recent
201 // until (and not including) a "marked" error. It returns zero if no marked
202 // error was found (and thus all errors were removed) and one otherwise. Errors
203 // are marked using |ERR_set_mark|.
204 OPENSSL_EXPORT int ERR_pop_to_mark(void);
205 
206 
207 // Custom errors.
208 
209 // ERR_get_next_error_library returns a value suitable for passing as the
210 // |library| argument to |ERR_put_error|. This is intended for code that wishes
211 // to push its own, non-standard errors to the error queue.
212 OPENSSL_EXPORT int ERR_get_next_error_library(void);
213 
214 
215 // Built-in library and reason codes.
216 
217 // The following values are built-in library codes.
218 enum {
219   ERR_LIB_NONE = 1,
220   ERR_LIB_SYS,
221   ERR_LIB_BN,
222   ERR_LIB_RSA,
223   ERR_LIB_DH,
224   ERR_LIB_EVP,
225   ERR_LIB_BUF,
226   ERR_LIB_OBJ,
227   ERR_LIB_PEM,
228   ERR_LIB_DSA,
229   ERR_LIB_X509,
230   ERR_LIB_ASN1,
231   ERR_LIB_CONF,
232   ERR_LIB_CRYPTO,
233   ERR_LIB_EC,
234   ERR_LIB_SSL,
235   ERR_LIB_BIO,
236   ERR_LIB_PKCS7,
237   ERR_LIB_PKCS8,
238   ERR_LIB_X509V3,
239   ERR_LIB_RAND,
240   ERR_LIB_ENGINE,
241   ERR_LIB_OCSP,
242   ERR_LIB_UI,
243   ERR_LIB_COMP,
244   ERR_LIB_ECDSA,
245   ERR_LIB_ECDH,
246   ERR_LIB_HMAC,
247   ERR_LIB_DIGEST,
248   ERR_LIB_CIPHER,
249   ERR_LIB_HKDF,
250   ERR_LIB_TRUST_TOKEN,
251   ERR_LIB_USER,
252   ERR_NUM_LIBS
253 };
254 
255 // The following reason codes used to denote an error occuring in another
256 // library. They are sometimes used for a stack trace.
257 #define ERR_R_SYS_LIB ERR_LIB_SYS
258 #define ERR_R_BN_LIB ERR_LIB_BN
259 #define ERR_R_RSA_LIB ERR_LIB_RSA
260 #define ERR_R_DH_LIB ERR_LIB_DH
261 #define ERR_R_EVP_LIB ERR_LIB_EVP
262 #define ERR_R_BUF_LIB ERR_LIB_BUF
263 #define ERR_R_OBJ_LIB ERR_LIB_OBJ
264 #define ERR_R_PEM_LIB ERR_LIB_PEM
265 #define ERR_R_DSA_LIB ERR_LIB_DSA
266 #define ERR_R_X509_LIB ERR_LIB_X509
267 #define ERR_R_ASN1_LIB ERR_LIB_ASN1
268 #define ERR_R_CONF_LIB ERR_LIB_CONF
269 #define ERR_R_CRYPTO_LIB ERR_LIB_CRYPTO
270 #define ERR_R_EC_LIB ERR_LIB_EC
271 #define ERR_R_SSL_LIB ERR_LIB_SSL
272 #define ERR_R_BIO_LIB ERR_LIB_BIO
273 #define ERR_R_PKCS7_LIB ERR_LIB_PKCS7
274 #define ERR_R_PKCS8_LIB ERR_LIB_PKCS8
275 #define ERR_R_X509V3_LIB ERR_LIB_X509V3
276 #define ERR_R_RAND_LIB ERR_LIB_RAND
277 #define ERR_R_DSO_LIB ERR_LIB_DSO
278 #define ERR_R_ENGINE_LIB ERR_LIB_ENGINE
279 #define ERR_R_OCSP_LIB ERR_LIB_OCSP
280 #define ERR_R_UI_LIB ERR_LIB_UI
281 #define ERR_R_COMP_LIB ERR_LIB_COMP
282 #define ERR_R_ECDSA_LIB ERR_LIB_ECDSA
283 #define ERR_R_ECDH_LIB ERR_LIB_ECDH
284 #define ERR_R_STORE_LIB ERR_LIB_STORE
285 #define ERR_R_FIPS_LIB ERR_LIB_FIPS
286 #define ERR_R_CMS_LIB ERR_LIB_CMS
287 #define ERR_R_TS_LIB ERR_LIB_TS
288 #define ERR_R_HMAC_LIB ERR_LIB_HMAC
289 #define ERR_R_JPAKE_LIB ERR_LIB_JPAKE
290 #define ERR_R_USER_LIB ERR_LIB_USER
291 #define ERR_R_DIGEST_LIB ERR_LIB_DIGEST
292 #define ERR_R_CIPHER_LIB ERR_LIB_CIPHER
293 #define ERR_R_HKDF_LIB ERR_LIB_HKDF
294 #define ERR_R_TRUST_TOKEN_LIB ERR_LIB_TRUST_TOKEN
295 
296 // The following values are global reason codes. They may occur in any library.
297 #define ERR_R_FATAL 64
298 #define ERR_R_MALLOC_FAILURE (1 | ERR_R_FATAL)
299 #define ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED (2 | ERR_R_FATAL)
300 #define ERR_R_PASSED_NULL_PARAMETER (3 | ERR_R_FATAL)
301 #define ERR_R_INTERNAL_ERROR (4 | ERR_R_FATAL)
302 #define ERR_R_OVERFLOW (5 | ERR_R_FATAL)
303 
304 
305 // Deprecated functions.
306 
307 // ERR_remove_state calls |ERR_clear_error|.
308 OPENSSL_EXPORT void ERR_remove_state(unsigned long pid);
309 
310 // ERR_remove_thread_state clears the error queue for the current thread if
311 // |tid| is NULL. Otherwise it calls |assert(0)|, because it's no longer
312 // possible to delete the error queue for other threads.
313 //
314 // Use |ERR_clear_error| instead. Note error queues are deleted automatically on
315 // thread exit. You do not need to call this function to release memory.
316 OPENSSL_EXPORT void ERR_remove_thread_state(const CRYPTO_THREADID *tid);
317 
318 // ERR_func_error_string returns the string "OPENSSL_internal".
319 OPENSSL_EXPORT const char *ERR_func_error_string(uint32_t packed_error);
320 
321 // ERR_error_string behaves like |ERR_error_string_n| but |len| is implicitly
322 // |ERR_ERROR_STRING_BUF_LEN|.
323 //
324 // Additionally, if |buf| is NULL, the error string is placed in a static buffer
325 // which is returned. This is not thread-safe and only exists for backwards
326 // compatibility with legacy callers. The static buffer will be overridden by
327 // calls in other threads.
328 //
329 // Use |ERR_error_string_n| instead.
330 //
331 // TODO(fork): remove this function.
332 OPENSSL_EXPORT char *ERR_error_string(uint32_t packed_error, char *buf);
333 #define ERR_ERROR_STRING_BUF_LEN 120
334 
335 // ERR_GET_FUNC returns zero. BoringSSL errors do not report a function code.
ERR_GET_FUNC(uint32_t packed_error)336 OPENSSL_INLINE int ERR_GET_FUNC(uint32_t packed_error) {
337   (void)packed_error;
338   return 0;
339 }
340 
341 // ERR_TXT_* are provided for compatibility with code that assumes that it's
342 // using OpenSSL.
343 #define ERR_TXT_STRING ERR_FLAG_STRING
344 #define ERR_TXT_MALLOCED ERR_FLAG_MALLOCED
345 
346 
347 // Private functions.
348 
349 // ERR_clear_system_error clears the system's error value (i.e. errno).
350 OPENSSL_EXPORT void ERR_clear_system_error(void);
351 
352 // OPENSSL_PUT_ERROR is used by OpenSSL code to add an error to the error
353 // queue.
354 #define OPENSSL_PUT_ERROR(library, reason) \
355   ERR_put_error(ERR_LIB_##library, 0, reason, __FILE__, __LINE__)
356 
357 // OPENSSL_PUT_SYSTEM_ERROR is used by OpenSSL code to add an error from the
358 // operating system to the error queue.
359 // TODO(fork): include errno.
360 #define OPENSSL_PUT_SYSTEM_ERROR() \
361   ERR_put_error(ERR_LIB_SYS, 0, 0, __FILE__, __LINE__);
362 
363 // ERR_put_error adds an error to the error queue, dropping the least recent
364 // error if necessary for space reasons.
365 OPENSSL_EXPORT void ERR_put_error(int library, int unused, int reason,
366                                   const char *file, unsigned line);
367 
368 // ERR_add_error_data takes a variable number (|count|) of const char*
369 // pointers, concatenates them and sets the result as the data on the most
370 // recent error.
371 OPENSSL_EXPORT void ERR_add_error_data(unsigned count, ...);
372 
373 // ERR_add_error_dataf takes a printf-style format and arguments, and sets the
374 // result as the data on the most recent error.
375 OPENSSL_EXPORT void ERR_add_error_dataf(const char *format, ...)
376     OPENSSL_PRINTF_FORMAT_FUNC(1, 2);
377 
378 // ERR_set_error_data sets the data on the most recent error to |data|, which
379 // must be a NUL-terminated string. |flags| must contain |ERR_FLAG_STRING|. If
380 // |flags| contains |ERR_FLAG_MALLOCED|, this function takes ownership of
381 // |data|, which must have been allocated with |OPENSSL_malloc|. Otherwise, it
382 // saves a copy of |data|.
383 //
384 // Note this differs from OpenSSL which, when |ERR_FLAG_MALLOCED| is unset,
385 // saves the pointer as-is and requires it remain valid for the lifetime of the
386 // address space.
387 OPENSSL_EXPORT void ERR_set_error_data(char *data, int flags);
388 
389 // ERR_NUM_ERRORS is one more than the limit of the number of errors in the
390 // queue.
391 #define ERR_NUM_ERRORS 16
392 
393 #define ERR_PACK(lib, reason)                                              \
394   (((((uint32_t)(lib)) & 0xff) << 24) | ((((uint32_t)(reason)) & 0xfff)))
395 
396 // OPENSSL_DECLARE_ERROR_REASON is used by util/make_errors.h (which generates
397 // the error defines) to recognise that an additional reason value is needed.
398 // This is needed when the reason value is used outside of an
399 // |OPENSSL_PUT_ERROR| macro. The resulting define will be
400 // ${lib}_R_${reason}.
401 #define OPENSSL_DECLARE_ERROR_REASON(lib, reason)
402 
403 
404 #if defined(__cplusplus)
405 }  // extern C
406 #endif
407 
408 #endif  // OPENSSL_HEADER_ERR_H
409