1*2175Sjp161948=pod 2*2175Sjp161948 3*2175Sjp161948=head1 NAME 4*2175Sjp161948 5*2175Sjp161948err - error codes 6*2175Sjp161948 7*2175Sjp161948=head1 SYNOPSIS 8*2175Sjp161948 9*2175Sjp161948 #include <openssl/err.h> 10*2175Sjp161948 11*2175Sjp161948 unsigned long ERR_get_error(void); 12*2175Sjp161948 unsigned long ERR_peek_error(void); 13*2175Sjp161948 unsigned long ERR_get_error_line(const char **file, int *line); 14*2175Sjp161948 unsigned long ERR_peek_error_line(const char **file, int *line); 15*2175Sjp161948 unsigned long ERR_get_error_line_data(const char **file, int *line, 16*2175Sjp161948 const char **data, int *flags); 17*2175Sjp161948 unsigned long ERR_peek_error_line_data(const char **file, int *line, 18*2175Sjp161948 const char **data, int *flags); 19*2175Sjp161948 20*2175Sjp161948 int ERR_GET_LIB(unsigned long e); 21*2175Sjp161948 int ERR_GET_FUNC(unsigned long e); 22*2175Sjp161948 int ERR_GET_REASON(unsigned long e); 23*2175Sjp161948 24*2175Sjp161948 void ERR_clear_error(void); 25*2175Sjp161948 26*2175Sjp161948 char *ERR_error_string(unsigned long e, char *buf); 27*2175Sjp161948 const char *ERR_lib_error_string(unsigned long e); 28*2175Sjp161948 const char *ERR_func_error_string(unsigned long e); 29*2175Sjp161948 const char *ERR_reason_error_string(unsigned long e); 30*2175Sjp161948 31*2175Sjp161948 void ERR_print_errors(BIO *bp); 32*2175Sjp161948 void ERR_print_errors_fp(FILE *fp); 33*2175Sjp161948 34*2175Sjp161948 void ERR_load_crypto_strings(void); 35*2175Sjp161948 void ERR_free_strings(void); 36*2175Sjp161948 37*2175Sjp161948 void ERR_remove_state(unsigned long pid); 38*2175Sjp161948 39*2175Sjp161948 void ERR_put_error(int lib, int func, int reason, const char *file, 40*2175Sjp161948 int line); 41*2175Sjp161948 void ERR_add_error_data(int num, ...); 42*2175Sjp161948 43*2175Sjp161948 void ERR_load_strings(int lib,ERR_STRING_DATA str[]); 44*2175Sjp161948 unsigned long ERR_PACK(int lib, int func, int reason); 45*2175Sjp161948 int ERR_get_next_error_library(void); 46*2175Sjp161948 47*2175Sjp161948=head1 DESCRIPTION 48*2175Sjp161948 49*2175Sjp161948When a call to the OpenSSL library fails, this is usually signalled 50*2175Sjp161948by the return value, and an error code is stored in an error queue 51*2175Sjp161948associated with the current thread. The B<err> library provides 52*2175Sjp161948functions to obtain these error codes and textual error messages. 53*2175Sjp161948 54*2175Sjp161948The L<ERR_get_error(3)|ERR_get_error(3)> manpage describes how to 55*2175Sjp161948access error codes. 56*2175Sjp161948 57*2175Sjp161948Error codes contain information about where the error occurred, and 58*2175Sjp161948what went wrong. L<ERR_GET_LIB(3)|ERR_GET_LIB(3)> describes how to 59*2175Sjp161948extract this information. A method to obtain human-readable error 60*2175Sjp161948messages is described in L<ERR_error_string(3)|ERR_error_string(3)>. 61*2175Sjp161948 62*2175Sjp161948L<ERR_clear_error(3)|ERR_clear_error(3)> can be used to clear the 63*2175Sjp161948error queue. 64*2175Sjp161948 65*2175Sjp161948Note that L<ERR_remove_state(3)|ERR_remove_state(3)> should be used to 66*2175Sjp161948avoid memory leaks when threads are terminated. 67*2175Sjp161948 68*2175Sjp161948=head1 ADDING NEW ERROR CODES TO OPENSSL 69*2175Sjp161948 70*2175Sjp161948See L<ERR_put_error(3)> if you want to record error codes in the 71*2175Sjp161948OpenSSL error system from within your application. 72*2175Sjp161948 73*2175Sjp161948The remainder of this section is of interest only if you want to add 74*2175Sjp161948new error codes to OpenSSL or add error codes from external libraries. 75*2175Sjp161948 76*2175Sjp161948=head2 Reporting errors 77*2175Sjp161948 78*2175Sjp161948Each sub-library has a specific macro XXXerr() that is used to report 79*2175Sjp161948errors. Its first argument is a function code B<XXX_F_...>, the second 80*2175Sjp161948argument is a reason code B<XXX_R_...>. Function codes are derived 81*2175Sjp161948from the function names; reason codes consist of textual error 82*2175Sjp161948descriptions. For example, the function ssl23_read() reports a 83*2175Sjp161948"handshake failure" as follows: 84*2175Sjp161948 85*2175Sjp161948 SSLerr(SSL_F_SSL23_READ, SSL_R_SSL_HANDSHAKE_FAILURE); 86*2175Sjp161948 87*2175Sjp161948Function and reason codes should consist of upper case characters, 88*2175Sjp161948numbers and underscores only. The error file generation script translates 89*2175Sjp161948function codes into function names by looking in the header files 90*2175Sjp161948for an appropriate function name, if none is found it just uses 91*2175Sjp161948the capitalized form such as "SSL23_READ" in the above example. 92*2175Sjp161948 93*2175Sjp161948The trailing section of a reason code (after the "_R_") is translated 94*2175Sjp161948into lower case and underscores changed to spaces. 95*2175Sjp161948 96*2175Sjp161948When you are using new function or reason codes, run B<make errors>. 97*2175Sjp161948The necessary B<#define>s will then automatically be added to the 98*2175Sjp161948sub-library's header file. 99*2175Sjp161948 100*2175Sjp161948Although a library will normally report errors using its own specific 101*2175Sjp161948XXXerr macro, another library's macro can be used. This is normally 102*2175Sjp161948only done when a library wants to include ASN1 code which must use 103*2175Sjp161948the ASN1err() macro. 104*2175Sjp161948 105*2175Sjp161948=head2 Adding new libraries 106*2175Sjp161948 107*2175Sjp161948When adding a new sub-library to OpenSSL, assign it a library number 108*2175Sjp161948B<ERR_LIB_XXX>, define a macro XXXerr() (both in B<err.h>), add its 109*2175Sjp161948name to B<ERR_str_libraries[]> (in B<crypto/err/err.c>), and add 110*2175Sjp161948C<ERR_load_XXX_strings()> to the ERR_load_crypto_strings() function 111*2175Sjp161948(in B<crypto/err/err_all.c>). Finally, add an entry 112*2175Sjp161948 113*2175Sjp161948 L XXX xxx.h xxx_err.c 114*2175Sjp161948 115*2175Sjp161948to B<crypto/err/openssl.ec>, and add B<xxx_err.c> to the Makefile. 116*2175Sjp161948Running B<make errors> will then generate a file B<xxx_err.c>, and 117*2175Sjp161948add all error codes used in the library to B<xxx.h>. 118*2175Sjp161948 119*2175Sjp161948Additionally the library include file must have a certain form. 120*2175Sjp161948Typically it will initially look like this: 121*2175Sjp161948 122*2175Sjp161948 #ifndef HEADER_XXX_H 123*2175Sjp161948 #define HEADER_XXX_H 124*2175Sjp161948 125*2175Sjp161948 #ifdef __cplusplus 126*2175Sjp161948 extern "C" { 127*2175Sjp161948 #endif 128*2175Sjp161948 129*2175Sjp161948 /* Include files */ 130*2175Sjp161948 131*2175Sjp161948 #include <openssl/bio.h> 132*2175Sjp161948 #include <openssl/x509.h> 133*2175Sjp161948 134*2175Sjp161948 /* Macros, structures and function prototypes */ 135*2175Sjp161948 136*2175Sjp161948 137*2175Sjp161948 /* BEGIN ERROR CODES */ 138*2175Sjp161948 139*2175Sjp161948The B<BEGIN ERROR CODES> sequence is used by the error code 140*2175Sjp161948generation script as the point to place new error codes, any text 141*2175Sjp161948after this point will be overwritten when B<make errors> is run. 142*2175Sjp161948The closing #endif etc will be automatically added by the script. 143*2175Sjp161948 144*2175Sjp161948The generated C error code file B<xxx_err.c> will load the header 145*2175Sjp161948files B<stdio.h>, B<openssl/err.h> and B<openssl/xxx.h> so the 146*2175Sjp161948header file must load any additional header files containing any 147*2175Sjp161948definitions it uses. 148*2175Sjp161948 149*2175Sjp161948=head1 USING ERROR CODES IN EXTERNAL LIBRARIES 150*2175Sjp161948 151*2175Sjp161948It is also possible to use OpenSSL's error code scheme in external 152*2175Sjp161948libraries. The library needs to load its own codes and call the OpenSSL 153*2175Sjp161948error code insertion script B<mkerr.pl> explicitly to add codes to 154*2175Sjp161948the header file and generate the C error code file. This will normally 155*2175Sjp161948be done if the external library needs to generate new ASN1 structures 156*2175Sjp161948but it can also be used to add more general purpose error code handling. 157*2175Sjp161948 158*2175Sjp161948TBA more details 159*2175Sjp161948 160*2175Sjp161948=head1 INTERNALS 161*2175Sjp161948 162*2175Sjp161948The error queues are stored in a hash table with one B<ERR_STATE> 163*2175Sjp161948entry for each pid. ERR_get_state() returns the current thread's 164*2175Sjp161948B<ERR_STATE>. An B<ERR_STATE> can hold up to B<ERR_NUM_ERRORS> error 165*2175Sjp161948codes. When more error codes are added, the old ones are overwritten, 166*2175Sjp161948on the assumption that the most recent errors are most important. 167*2175Sjp161948 168*2175Sjp161948Error strings are also stored in hash table. The hash tables can 169*2175Sjp161948be obtained by calling ERR_get_err_state_table(void) and 170*2175Sjp161948ERR_get_string_table(void) respectively. 171*2175Sjp161948 172*2175Sjp161948=head1 SEE ALSO 173*2175Sjp161948 174*2175Sjp161948L<CRYPTO_set_id_callback(3)|CRYPTO_set_id_callback(3)>, 175*2175Sjp161948L<CRYPTO_set_locking_callback(3)|CRYPTO_set_locking_callback(3)>, 176*2175Sjp161948L<ERR_get_error(3)|ERR_get_error(3)>, 177*2175Sjp161948L<ERR_GET_LIB(3)|ERR_GET_LIB(3)>, 178*2175Sjp161948L<ERR_clear_error(3)|ERR_clear_error(3)>, 179*2175Sjp161948L<ERR_error_string(3)|ERR_error_string(3)>, 180*2175Sjp161948L<ERR_print_errors(3)|ERR_print_errors(3)>, 181*2175Sjp161948L<ERR_load_crypto_strings(3)|ERR_load_crypto_strings(3)>, 182*2175Sjp161948L<ERR_remove_state(3)|ERR_remove_state(3)>, 183*2175Sjp161948L<ERR_put_error(3)|ERR_put_error(3)>, 184*2175Sjp161948L<ERR_load_strings(3)|ERR_load_strings(3)>, 185*2175Sjp161948L<SSL_get_error(3)|SSL_get_error(3)> 186*2175Sjp161948 187*2175Sjp161948=cut 188