
#use wml::openssl-macros area=docs page=err

<title>Documents, err(3)</title>

<h1>err(3)</h1>

#use wml::imp::generic

{:
## What's this? [[s|(<STRONG>[^<].+?)</A>(</STRONG><DD>)|$1$2|sg]]
[[s|<P>\s+<P>|<P>|sg]]
[[s|<P>\s+</|</|sg]]
[[s|<DD>\s*<DT>|<DD>&nbsp;<DT>|sg]]
[[s|<DD>\s*</DL>|<DD>&nbsp;</DL>|sg]]
[[s|\[|&#91;|sg]]
[[s|\]|&#93;|sg]]

<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#NAME">NAME</A>
	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#ADDING_NEW_ERROR_CODES_TO_OPENSS">ADDING NEW ERROR CODES TO OPENSSL</A>
	<UL>

		<LI><A HREF="#Reporting_errors">Reporting errors</A>
		<LI><A HREF="#Adding_new_libraries">Adding new libraries</A>
	</UL>

	<LI><A HREF="#USING_ERROR_CODES_IN_EXTERNAL_LI">USING ERROR CODES IN EXTERNAL LIBRARIES</A>
	<LI><A HREF="#INTERNALS">INTERNALS</A>
	<LI><A HREF="#SEE_ALSO">SEE ALSO</A>
</UL>
<!-- INDEX END -->

<HR>
<P>
<HR>
<H1><A NAME="NAME">NAME</A></H1>
<P>
err - error codes

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/err.h&gt;
</PRE>
<PRE> unsigned long ERR_get_error(void);
 unsigned long ERR_peek_error(void);
 unsigned long ERR_get_error_line(const char **file, int *line);
 unsigned long ERR_peek_error_line(const char **file, int *line);
 unsigned long ERR_get_error_line_data(const char **file, int *line,
         const char **data, int *flags);
 unsigned long ERR_peek_error_line_data(const char **file, int *line,
         const char **data, int *flags);
</PRE>
<PRE> int ERR_GET_LIB(unsigned long e);
 int ERR_GET_FUNC(unsigned long e);
 int ERR_GET_REASON(unsigned long e);
</PRE>
<PRE> void ERR_clear_error(void);
</PRE>
<PRE> char *ERR_error_string(unsigned long e, char *buf);
 const char *ERR_lib_error_string(unsigned long e);
 const char *ERR_func_error_string(unsigned long e);
 const char *ERR_reason_error_string(unsigned long e);
</PRE>
<PRE> void ERR_print_errors(BIO *bp);
 void ERR_print_errors_fp(FILE *fp);
</PRE>
<PRE> void ERR_load_crypto_strings(void);
 void ERR_free_strings(void);
</PRE>
<PRE> void ERR_remove_state(unsigned long pid);
</PRE>
<PRE> void ERR_put_error(int lib, int func, int reason, const char *file,
         int line);
 void ERR_add_error_data(int num, ...);
</PRE>
<PRE> void ERR_load_strings(int lib,ERR_STRING_DATA str[]);
 unsigned long ERR_PACK(int lib, int func, int reason);
 int ERR_get_next_error_library(void);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
When a call to the OpenSSL library fails, this is usually signalled by the
return value, and an error code is stored in an error queue associated with
the current thread. The <STRONG>err</STRONG> library provides functions to obtain these error codes and textual error
messages.

</P>
<P>
The <A HREF="../crypto/ERR_get_error.html#">ERR_get_error(3)</A> manpage describes how to access error codes.

</P>
<P>
Error codes contain information about where the error occurred, and what
went wrong. <A HREF="../crypto/ERR_GET_LIB.html#">ERR_GET_LIB(3)</A> describes how to extract this information. A method to obtain
human-readable error messages is described in <A HREF="../crypto/ERR_error_string.html#">ERR_error_string(3)</A>.

</P>
<P>
<A HREF="../crypto/ERR_clear_error.html#">ERR_clear_error(3)</A> can be used to clear the error queue.

</P>
<P>
Note that <A HREF="../crypto/ERR_remove_state.html#">ERR_remove_state(3)</A> should be used to avoid memory leaks when threads are terminated.

</P>
<P>
<HR>
<H1><A NAME="ADDING_NEW_ERROR_CODES_TO_OPENSS">ADDING NEW ERROR CODES TO OPENSSL</A></H1>
<P>
See <A HREF="../crypto/ERR_put_error.html#">the ERR_put_error(3) manpage</A> if you want to record error codes in the OpenSSL error system from within
your application.

</P>
<P>
The remainder of this section is of interest only if you want to add new
error codes to OpenSSL or add error codes from external libraries.

</P>
<P>
<HR>
<H2><A NAME="Reporting_errors">Reporting errors</A></H2>
<P>
Each sub-library has a specific macro <CODE>XXXerr()</CODE> that is used to
report errors. Its first argument is a function code <STRONG>XXX_F_...</STRONG>, the second argument is a reason code <STRONG>XXX_R_...</STRONG>. Function codes are derived from the function names; reason codes consist
of textual error descriptions. For example, the function
<CODE>ssl23_read()</CODE> reports a ``handshake failure'' as follows:

</P>
<PRE> SSLerr(SSL_F_SSL23_READ, SSL_R_SSL_HANDSHAKE_FAILURE);
</PRE>
<P>
Function and reason codes should consist of upper case characters, numbers
and underscores only. The error file generation script translates function
codes into function names by looking in the header files for an appropriate
function name, if none is found it just uses the capitalized form such as
``SSL23_READ'' in the above example.

</P>
<P>
The trailing section of a reason code (after the ``_R_'') is translated
into lower case and underscores changed to spaces.

</P>
<P>
When you are using new function or reason codes, run <STRONG>make errors</STRONG>. The necessary <STRONG>#define</STRONG>s will then automatically be added to the sub-library's header file.

</P>
<P>
Although a library will normally report errors using its own specific
XXXerr macro, another library's macro can be used. This is normally only
done when a library wants to include ASN1 code which must use the
<CODE>ASN1err()</CODE> macro.

</P>
<P>
<HR>
<H2><A NAME="Adding_new_libraries">Adding new libraries</A></H2>
<P>
When adding a new sub-library to OpenSSL, assign it a library number
<STRONG>ERR_LIB_XXX</STRONG>, define a macro <CODE>XXXerr()</CODE> (both in <STRONG>err.h</STRONG>), add its name to <STRONG>ERR_str_libraries[]</STRONG> (in <STRONG>crypto/err/err.c</STRONG>), and add
<CODE>ERR_load_XXX_strings()</CODE> to the <CODE>ERR_load_crypto_strings()</CODE> function (in <STRONG>crypto/err/err_all.c</STRONG>). Finally, add an entry

</P>
<PRE> L      XXX     xxx.h   xxx_err.c
</PRE>
<P>
to <STRONG>crypto/err/openssl.ec</STRONG>, and add <STRONG>xxx_err.c</STRONG> to the Makefile. Running <STRONG>make errors</STRONG> will then generate a file <STRONG>xxx_err.c</STRONG>, and add all error codes used in the library to <STRONG>xxx.h</STRONG>.

</P>
<P>
Additionally the library include file must have a certain form. Typically
it will initially look like this:

</P>
<PRE> #ifndef HEADER_XXX_H
 \#define HEADER_XXX_H
</PRE>
<PRE> #ifdef __cplusplus
 extern &quot;C&quot; {
 \#endif
</PRE>
<PRE> /* Include files */
</PRE>
<PRE> #include &lt;openssl/bio.h&gt;
 \#include &lt;openssl/x509.h&gt;
</PRE>
<PRE> /* Macros, structures and function prototypes */
</PRE>
<PRE> /* BEGIN ERROR CODES */
</PRE>
<P>
The <STRONG>BEGIN ERROR CODES</STRONG> sequence is used by the error code generation script as the point to place
new error codes, any text after this point will be overwritten when <STRONG>make errors</STRONG> is run. The closing #endif etc will be automatically added by the script.

</P>
<P>
The generated C error code file <STRONG>xxx_err.c</STRONG> will load the header files <STRONG>stdio.h</STRONG>, <STRONG>openssl/err.h</STRONG> and <STRONG>openssl/xxx.h</STRONG> so the header file must load any additional header files containing any
definitions it uses.

</P>
<P>
<HR>
<H1><A NAME="USING_ERROR_CODES_IN_EXTERNAL_LI">USING ERROR CODES IN EXTERNAL LIBRARIES</A></H1>
<P>
It is also possible to use OpenSSL's error code scheme in external
libraries. The library needs to load its own codes and call the OpenSSL
error code insertion script <STRONG>mkerr.pl</STRONG> explicitly to add codes to the header file and generate the C error code
file. This will normally be done if the external library needs to generate
new ASN1 structures but it can also be used to add more general purpose
error code handling.

</P>
<P>
TBA more details

</P>
<P>
<HR>
<H1><A NAME="INTERNALS">INTERNALS</A></H1>
<P>
The error queues are stored in a hash table with one <STRONG>ERR_STATE</STRONG>
entry for each pid. <CODE>ERR_get_state()</CODE> returns the current
thread's
<STRONG>ERR_STATE</STRONG>. An <STRONG>ERR_STATE</STRONG> can hold up to <STRONG>ERR_NUM_ERRORS</STRONG> error codes. When more error codes are added, the old ones are overwritten,
on the assumption that the most recent errors are most important.

</P>
<P>
Error strings are also stored in hash table. The hash tables can be
obtained by calling <CODE>ERR_get_err_state_table(void)</CODE> and
<CODE>ERR_get_string_table(void)</CODE> respectively.

</P>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
<EM>CRYPTO_set_id_callback(3)</EM>,
<A HREF="../crypto/threads.html#">CRYPTO_set_locking_callback(3)</A>,
<A HREF="../crypto/ERR_get_error.html#">ERR_get_error(3)</A>,
<A HREF="../crypto/ERR_GET_LIB.html#">ERR_GET_LIB(3)</A>,
<A HREF="../crypto/ERR_clear_error.html#">ERR_clear_error(3)</A>,
<A HREF="../crypto/ERR_error_string.html#">ERR_error_string(3)</A>,
<A HREF="../crypto/ERR_print_errors.html#">ERR_print_errors(3)</A>,
<A HREF="../crypto/ERR_load_crypto_strings.html#">ERR_load_crypto_strings(3)</A>,
<A HREF="../crypto/ERR_remove_state.html#">ERR_remove_state(3)</A>,
<A HREF="../crypto/ERR_put_error.html#">ERR_put_error(3)</A>,
<A HREF="../crypto/ERR_load_strings.html#">ERR_load_strings(3)</A>,
<A HREF="../ssl/SSL_get_error.html#">SSL_get_error(3)</A>



</P>
:}

