1 /*
2  * Copyright (C) 2000-2012 Free Software Foundation, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
8  * The GnuTLS is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>
20  *
21  */
22 
23 #ifndef GNUTLS_LIB_ERRORS_H
24 #define GNUTLS_LIB_ERRORS_H
25 
26 #include "gnutls_int.h"
27 #include <global.h>
28 #include <mpi.h>
29 #include <gnutls/x509.h>
30 
31 #ifdef __FILE__
32 #ifdef __LINE__
33 #define gnutls_assert() _gnutls_assert_log( "ASSERT: %s[%s]:%d\n", __FILE__,__func__,__LINE__);
34 #else
35 #define gnutls_assert()
36 #endif
37 #else				/* __FILE__ not defined */
38 #define gnutls_assert()
39 #endif
40 
41 int _gnutls_asn2err(int asn_err) __GNUTLS_CONST__;
42 
43 void _gnutls_log(int, const char *fmt, ...)
44 #ifdef __GNUC__
45     __attribute__ ((format(printf, 2, 3)));
46 #else
47 ;
48 #endif
49 
50 void _gnutls_audit_log(gnutls_session_t, const char *fmt, ...)
51 #ifdef __GNUC__
52     __attribute__ ((format(printf, 2, 3)));
53 #else
54 ;
55 #endif
56 
57 void _gnutls_mpi_log(const char *prefix, bigint_t a);
58 
59 #define _gnutls_cert_log(str, cert) \
60 	do { \
61 		if (unlikely(_gnutls_log_level >= 3 && cert != NULL)) { \
62 			gnutls_datum_t _cl_out; int _cl_ret; \
63 			_cl_ret = gnutls_x509_crt_print(cert, GNUTLS_CRT_PRINT_ONELINE, &_cl_out); \
64 			if (_cl_ret >= 0) { \
65 				_gnutls_log( 3, "%s: %s\n", str, _cl_out.data); \
66 				gnutls_free(_cl_out.data); \
67 	                } \
68 		} \
69         } while(0)
70 
71 #define _gnutls_dn_log(str, dn) \
72 	do { \
73 		if (unlikely(_gnutls_log_level >= 3)) { \
74 			gnutls_datum_t _cl_out; int _cl_ret; \
75 			_cl_ret = gnutls_x509_rdn_get2((dn), &_cl_out, 0); \
76 			if (_cl_ret >= 0) { \
77 				_gnutls_log( 3, "%s: %s\n", str, _cl_out.data); \
78 				gnutls_free(_cl_out.data); \
79 	                } \
80 		} \
81         } while(0)
82 
83 #define _gnutls_reason_log(str, status) \
84 	do { \
85 		if (unlikely(_gnutls_log_level >= 3)) { \
86 			gnutls_datum_t _cl_out; int _cl_ret; \
87 			_cl_ret = gnutls_certificate_verification_status_print(status, GNUTLS_CRT_X509, &_cl_out, 0); \
88 			if (_cl_ret >= 0) { \
89 				_gnutls_log( 3, "%s: %s\n", str, _cl_out.data); \
90 				gnutls_free(_cl_out.data); \
91 	                } \
92 		} \
93          } while(0)
94 
95 #ifdef C99_MACROS
96 #define LEVEL(l, ...) do { if (unlikely(_gnutls_log_level >= l)) \
97       _gnutls_log( l, __VA_ARGS__); } while(0)
98 
99 #define _gnutls_debug_log(...) LEVEL(2, __VA_ARGS__)
100 #define _gnutls_assert_log(...) LEVEL(3, __VA_ARGS__)
101 #define _gnutls_handshake_log(...) LEVEL(4, __VA_ARGS__)
102 #define _gnutls_record_log(...) LEVEL(5, __VA_ARGS__)
103 #define _gnutls_dtls_log(...) LEVEL(6, __VA_ARGS__)
104 
105 #define _gnutls_hard_log(...) LEVEL(9, __VA_ARGS__)
106 
107 #define _gnutls_read_log(...) LEVEL(10, __VA_ARGS__)
108 #define _gnutls_write_log(...) LEVEL(11, __VA_ARGS__)
109 #define _gnutls_io_log(...) LEVEL(12, __VA_ARGS__)
110 #define _gnutls_buffers_log(...) LEVEL(13, __VA_ARGS__)
111 #define _gnutls_no_log(...) LEVEL(INT_MAX, __VA_ARGS__)
112 #else
113 #define _gnutls_debug_log _gnutls_null_log
114 #define _gnutls_assert_log _gnutls_null_log
115 #define _gnutls_handshake_log _gnutls_null_log
116 #define _gnutls_io_log _gnutls_null_log
117 #define _gnutls_buffers_log _gnutls_null_log
118 #define _gnutls_hard_log _gnutls_null_log
119 #define _gnutls_record_log _gnutls_null_log
120 #define _gnutls_dtls_log _gnutls_null_log
121 #define _gnutls_read_log _gnutls_null_log
122 #define _gnutls_write_log _gnutls_null_log
123 #define _gnutls_no_log _gnutle_null_log
124 
125 void _gnutls_null_log(void *, ...);
126 
127 #endif				/* C99_MACROS */
128 
129 /* GCC won't inline this by itself and results in a "fatal warning"
130    otherwise. Making this a macro has been tried, but it interacts
131    badly with the do..while in the expansion. Welcome to the dark
132    side. */
133 static inline
134 #ifdef __GNUC__
135     __attribute__ ((always_inline))
136 #endif
gnutls_assert_val_int(int val,const char * file,const char * func,int line)137 int gnutls_assert_val_int(int val, const char *file, const char *func, int line)
138 {
139 	_gnutls_assert_log( "ASSERT: %s[%s]:%d\n", file,func,line);
140 	return val;
141 }
142 
143 #define gnutls_assert_val(x) gnutls_assert_val_int(x, __FILE__, __func__, __LINE__)
144 #define gnutls_assert_val_fatal(x) (((x)!=GNUTLS_E_AGAIN && (x)!=GNUTLS_E_INTERRUPTED)?gnutls_assert_val_int(x, __FILE__, __func__, __LINE__):(x))
145 
146 #endif /* GNUTLS_LIB_ERRORS_H */
147