1/*
2 * Copyright 1995-2019 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 HEADER_ERR_H
11# define HEADER_ERR_H
12
13# include <openssl/e_os2.h>
14
15# ifndef OPENSSL_NO_STDIO
16# include <stdio.h>
17# include <stdlib.h>
18# endif
19
20# include <openssl/ossl_typ.h>
21# include <openssl/bio.h>
22# include <openssl/lhash.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28# ifndef OPENSSL_NO_ERR
29# define ERR_PUT_error(a,b,c,d,e) ERR_put_error(a,b,c,d,e)
30# else
31# define ERR_PUT_error(a,b,c,d,e) ERR_put_error(a,b,c,NULL,0)
32# endif
33
34# include <errno.h>
35
36# define ERR_TXT_MALLOCED 0x01
37# define ERR_TXT_STRING 0x02
38
39# define ERR_FLAG_MARK 0x01
40# define ERR_FLAG_CLEAR 0x02
41
42# define ERR_NUM_ERRORS 16
43typedef struct err_state_st {
44 int err_flags[ERR_NUM_ERRORS];
45 unsigned long err_buffer[ERR_NUM_ERRORS];
46 char *err_data[ERR_NUM_ERRORS];
47 int err_data_flags[ERR_NUM_ERRORS];
48 const char *err_file[ERR_NUM_ERRORS];
49 int err_line[ERR_NUM_ERRORS];
50 int top, bottom;
51} ERR_STATE;
52
53/* library */
54# define ERR_LIB_NONE 1
55# define ERR_LIB_SYS 2
56# define ERR_LIB_BN 3
57# define ERR_LIB_RSA 4
58# define ERR_LIB_DH 5
59# define ERR_LIB_EVP 6
60# define ERR_LIB_BUF 7
61# define ERR_LIB_OBJ 8
62# define ERR_LIB_PEM 9
63# define ERR_LIB_DSA 10
64# define ERR_LIB_X509 11
65/* #define ERR_LIB_METH 12 */
66# define ERR_LIB_ASN1 13
67# define ERR_LIB_CONF 14
68# define ERR_LIB_CRYPTO 15
69# define ERR_LIB_EC 16
70# define ERR_LIB_SSL 20
71/* #define ERR_LIB_SSL23 21 */
72/* #define ERR_LIB_SSL2 22 */
73/* #define ERR_LIB_SSL3 23 */
74/* #define ERR_LIB_RSAREF 30 */
75/* #define ERR_LIB_PROXY 31 */
76# define ERR_LIB_BIO 32
77# define ERR_LIB_PKCS7 33
78# define ERR_LIB_X509V3 34
79# define ERR_LIB_PKCS12 35
80# define ERR_LIB_RAND 36
81# define ERR_LIB_DSO 37
82# define ERR_LIB_ENGINE 38
83# define ERR_LIB_OCSP 39
84# define ERR_LIB_UI 40
85# define ERR_LIB_COMP 41
86# define ERR_LIB_ECDSA 42
87# define ERR_LIB_ECDH 43
88# define ERR_LIB_OSSL_STORE 44
89# define ERR_LIB_FIPS 45
90# define ERR_LIB_CMS 46
91# define ERR_LIB_TS 47
92# define ERR_LIB_HMAC 48
93/* # define ERR_LIB_JPAKE 49 */
94# define ERR_LIB_CT 50
95# define ERR_LIB_ASYNC 51
96# define ERR_LIB_KDF 52
97# define ERR_LIB_SM2 53
98
99# define ERR_LIB_USER 128
100
101# define SYSerr(f,r) ERR_PUT_error(ERR_LIB_SYS,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
102# define BNerr(f,r) ERR_PUT_error(ERR_LIB_BN,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
103# define RSAerr(f,r) ERR_PUT_error(ERR_LIB_RSA,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
104# define DHerr(f,r) ERR_PUT_error(ERR_LIB_DH,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
105# define EVPerr(f,r) ERR_PUT_error(ERR_LIB_EVP,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
106# define BUFerr(f,r) ERR_PUT_error(ERR_LIB_BUF,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
107# define OBJerr(f,r) ERR_PUT_error(ERR_LIB_OBJ,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
108# define PEMerr(f,r) ERR_PUT_error(ERR_LIB_PEM,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
109# define DSAerr(f,r) ERR_PUT_error(ERR_LIB_DSA,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
110# define X509err(f,r) ERR_PUT_error(ERR_LIB_X509,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
111# define ASN1err(f,r) ERR_PUT_error(ERR_LIB_ASN1,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
112# define CONFerr(f,r) ERR_PUT_error(ERR_LIB_CONF,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
113# define CRYPTOerr(f,r) ERR_PUT_error(ERR_LIB_CRYPTO,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
114# define ECerr(f,r) ERR_PUT_error(ERR_LIB_EC,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
115# define SSLerr(f,r) ERR_PUT_error(ERR_LIB_SSL,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
116# define BIOerr(f,r) ERR_PUT_error(ERR_LIB_BIO,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
117# define PKCS7err(f,r) ERR_PUT_error(ERR_LIB_PKCS7,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
118# define X509V3err(f,r) ERR_PUT_error(ERR_LIB_X509V3,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
119# define PKCS12err(f,r) ERR_PUT_error(ERR_LIB_PKCS12,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
120# define RANDerr(f,r) ERR_PUT_error(ERR_LIB_RAND,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
121# define DSOerr(f,r) ERR_PUT_error(ERR_LIB_DSO,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
122# define ENGINEerr(f,r) ERR_PUT_error(ERR_LIB_ENGINE,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
123# define OCSPerr(f,r) ERR_PUT_error(ERR_LIB_OCSP,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
124# define UIerr(f,r) ERR_PUT_error(ERR_LIB_UI,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
125# define COMPerr(f,r) ERR_PUT_error(ERR_LIB_COMP,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
126# define ECDSAerr(f,r) ERR_PUT_error(ERR_LIB_ECDSA,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
127# define ECDHerr(f,r) ERR_PUT_error(ERR_LIB_ECDH,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
128# define OSSL_STOREerr(f,r) ERR_PUT_error(ERR_LIB_OSSL_STORE,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
129# define FIPSerr(f,r) ERR_PUT_error(ERR_LIB_FIPS,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
130# define CMSerr(f,r) ERR_PUT_error(ERR_LIB_CMS,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
131# define TSerr(f,r) ERR_PUT_error(ERR_LIB_TS,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
132# define HMACerr(f,r) ERR_PUT_error(ERR_LIB_HMAC,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
133# define CTerr(f,r) ERR_PUT_error(ERR_LIB_CT,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
134# define ASYNCerr(f,r) ERR_PUT_error(ERR_LIB_ASYNC,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
135# define KDFerr(f,r) ERR_PUT_error(ERR_LIB_KDF,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
136# define SM2err(f,r) ERR_PUT_error(ERR_LIB_SM2,(f),(r),OPENSSL_FILE,OPENSSL_LINE)
137
138# define ERR_PACK(l,f,r) ( \
139 (((unsigned int)(l) & 0x0FF) << 24L) | \
140 (((unsigned int)(f) & 0xFFF) << 12L) | \
141 (((unsigned int)(r) & 0xFFF) ) )
142# define ERR_GET_LIB(l) (int)(((l) >> 24L) & 0x0FFL)
143# define ERR_GET_FUNC(l) (int)(((l) >> 12L) & 0xFFFL)
144# define ERR_GET_REASON(l) (int)( (l) & 0xFFFL)
145# define ERR_FATAL_ERROR(l) (int)( (l) & ERR_R_FATAL)
146
147/* OS functions */
148# define SYS_F_FOPEN 1
149# define SYS_F_CONNECT 2
150# define SYS_F_GETSERVBYNAME 3
151# define SYS_F_SOCKET 4
152# define SYS_F_IOCTLSOCKET 5
153# define SYS_F_BIND 6
154# define SYS_F_LISTEN 7
155# define SYS_F_ACCEPT 8
156# define SYS_F_WSASTARTUP 9/* Winsock stuff */
157# define SYS_F_OPENDIR 10
158# define SYS_F_FREAD 11
159# define SYS_F_GETADDRINFO 12
160# define SYS_F_GETNAMEINFO 13
161# define SYS_F_SETSOCKOPT 14
162# define SYS_F_GETSOCKOPT 15
163# define SYS_F_GETSOCKNAME 16
164# define SYS_F_GETHOSTBYNAME 17
165# define SYS_F_FFLUSH 18
166# define SYS_F_OPEN 19
167# define SYS_F_CLOSE 20
168# define SYS_F_IOCTL 21
169# define SYS_F_STAT 22
170# define SYS_F_FCNTL 23
171# define SYS_F_FSTAT 24
172
173/* reasons */
174# define ERR_R_SYS_LIB ERR_LIB_SYS/* 2 */
175# define ERR_R_BN_LIB ERR_LIB_BN/* 3 */
176# define ERR_R_RSA_LIB ERR_LIB_RSA/* 4 */
177# define ERR_R_DH_LIB ERR_LIB_DH/* 5 */
178# define ERR_R_EVP_LIB ERR_LIB_EVP/* 6 */
179# define ERR_R_BUF_LIB ERR_LIB_BUF/* 7 */
180# define ERR_R_OBJ_LIB ERR_LIB_OBJ/* 8 */
181# define ERR_R_PEM_LIB ERR_LIB_PEM/* 9 */
182# define ERR_R_DSA_LIB ERR_LIB_DSA/* 10 */
183# define ERR_R_X509_LIB ERR_LIB_X509/* 11 */
184# define ERR_R_ASN1_LIB ERR_LIB_ASN1/* 13 */
185# define ERR_R_EC_LIB ERR_LIB_EC/* 16 */
186# define ERR_R_BIO_LIB ERR_LIB_BIO/* 32 */
187# define ERR_R_PKCS7_LIB ERR_LIB_PKCS7/* 33 */
188# define ERR_R_X509V3_LIB ERR_LIB_X509V3/* 34 */
189# define ERR_R_ENGINE_LIB ERR_LIB_ENGINE/* 38 */
190# define ERR_R_UI_LIB ERR_LIB_UI/* 40 */
191# define ERR_R_ECDSA_LIB ERR_LIB_ECDSA/* 42 */
192# define ERR_R_OSSL_STORE_LIB ERR_LIB_OSSL_STORE/* 44 */
193
194# define ERR_R_NESTED_ASN1_ERROR 58
195# define ERR_R_MISSING_ASN1_EOS 63
196
197/* fatal error */
198# define ERR_R_FATAL 64
199# define ERR_R_MALLOC_FAILURE (1|ERR_R_FATAL)
200# define ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED (2|ERR_R_FATAL)
201# define ERR_R_PASSED_NULL_PARAMETER (3|ERR_R_FATAL)
202# define ERR_R_INTERNAL_ERROR (4|ERR_R_FATAL)
203# define ERR_R_DISABLED (5|ERR_R_FATAL)
204# define ERR_R_INIT_FAIL (6|ERR_R_FATAL)
205# define ERR_R_PASSED_INVALID_ARGUMENT (7)
206# define ERR_R_OPERATION_FAIL (8|ERR_R_FATAL)
207
208/*
209 * 99 is the maximum possible ERR_R_... code, higher values are reserved for
210 * the individual libraries
211 */
212
213typedef struct ERR_string_data_st {
214 unsigned long error;
215 const char *string;
216} ERR_STRING_DATA;
217
218DEFINE_LHASH_OF(ERR_STRING_DATA);
219
220void ERR_put_error(int lib, int func, int reason, const char *file, int line);
221void ERR_set_error_data(char *data, int flags);
222
223unsigned long ERR_get_error(void);
224unsigned long ERR_get_error_line(const char **file, int *line);
225unsigned long ERR_get_error_line_data(const char **file, int *line,
226 const char **data, int *flags);
227unsigned long ERR_peek_error(void);
228unsigned long ERR_peek_error_line(const char **file, int *line);
229unsigned long ERR_peek_error_line_data(const char **file, int *line,
230 const char **data, int *flags);
231unsigned long ERR_peek_last_error(void);
232unsigned long ERR_peek_last_error_line(const char **file, int *line);
233unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
234 const char **data, int *flags);
235void ERR_clear_error(void);
236char *ERR_error_string(unsigned long e, char *buf);
237void ERR_error_string_n(unsigned long e, char *buf, size_t len);
238const char *ERR_lib_error_string(unsigned long e);
239const char *ERR_func_error_string(unsigned long e);
240const char *ERR_reason_error_string(unsigned long e);
241void ERR_print_errors_cb(int (*cb) (const char *str, size_t len, void *u),
242 void *u);
243# ifndef OPENSSL_NO_STDIO
244void ERR_print_errors_fp(FILE *fp);
245# endif
246void ERR_print_errors(BIO *bp);
247void ERR_add_error_data(int num, ...);
248void ERR_add_error_vdata(int num, va_list args);
249int ERR_load_strings(int lib, ERR_STRING_DATA *str);
250int ERR_load_strings_const(const ERR_STRING_DATA *str);
251int ERR_unload_strings(int lib, ERR_STRING_DATA *str);
252int ERR_load_ERR_strings(void);
253
254#if OPENSSL_API_COMPAT < 0x10100000L
255# define ERR_load_crypto_strings() \
256 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL)
257# define ERR_free_strings() while(0) continue
258#endif
259
260DEPRECATEDIN_1_1_0(void ERR_remove_thread_state(void *))
261DEPRECATEDIN_1_0_0(void ERR_remove_state(unsigned long pid))
262ERR_STATE *ERR_get_state(void);
263
264int ERR_get_next_error_library(void);
265
266int ERR_set_mark(void);
267int ERR_pop_to_mark(void);
268int ERR_clear_last_mark(void);
269
270#ifdef __cplusplus
271}
272#endif
273
274#endif
275