forked from lucab/ntop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssl_utils.c
355 lines (290 loc) · 10.2 KB
/
ssl_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
*
* http://www.ntop.org
*
* Copyright (C) 1998-2012 Luca Deri <[email protected]>
*
* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*
*/
#include "ntop.h"
#ifdef HAVE_OPENSSL
#include <openssl/rand.h>
int verify_callback(int ok, X509_STORE_CTX *ctx);
void ntop_ssl_error_report(char * whyMe) {
unsigned long l;
char buf[200];
const char *file,*data;
int line,flags;
unsigned long es;
if(myGlobals.newSock != 0) {
if(SSL_get_error(getSSLsocket(myGlobals.newSock), -1) == SSL_ERROR_SSL)
return; /* Internale OpenSSL failure: can't do much */
}
es = CRYPTO_thread_id();
while ((l=ERR_get_error_line_data(&file,&line,&data,&flags)) != 0) {
ERR_error_string_n(l, buf, sizeof buf);
traceEvent(CONST_TRACE_INFO, "SSL(%s)ERROR [Thread %lu]: %s at %s(%d) %s",
whyMe,
es,
buf,
file,
line,
(flags&ERR_TXT_STRING)?data:"");
}
}
int init_ssl(void) {
int idx;
FILE* fd=NULL;
char buf [384];
SSL_METHOD *meth;
int s_server_session_id_context = 1; /* anything will do */
DIR* directoryPointer=NULL;
struct dirent *dp;
struct stat fStat;
myGlobals.sslInitialized = 0;
if(myGlobals.runningPref.sslPort == 0) {
traceEvent(CONST_TRACE_INFO, "SSL is present but https is disabled: use -W <https port> for enabling it");
return(0); /* The user decided NOT to use SSL */
}
memset(myGlobals.ssl, 0, sizeof(myGlobals.ssl));
traceEvent(CONST_TRACE_INFO, "SSL: Initializing...");
/*
* If necessary, initialize the prng for ssl...
*/
if (RAND_status() == 0) {
struct timeval TOD;
/*
* If we get here, we need to add some entropy, because it's not there by default
* and because we don't have egd running.
*
* Remember, this is ntop during startup, so we can't just use ntop counters...
*
* We need some stuff that's random from ntop user/instance to ntop user/instance
* and some stuff the user just can't affect.
*
* We have to be careful, as some things that might seem random, such as pid#
* aren't if ntop is started during boot. OTOP, if the user won't run egd, then
* well, there's only so much we're going to do...
*/
traceEvent(CONST_TRACE_INFO, "SSL_PRNG: Initializing.");
traceEvent(CONST_TRACE_NOISY, "SSL_PRNG: see http://www.openssl.org/support/faq.cgi#USER1.");
RAND_add(version, strlen(version), (double)4.0);
RAND_add(buildDate, strlen(buildDate), (double)4.0);
RAND_add(configure_parameters, strlen(configure_parameters), (double)4.0);
gettimeofday(&TOD, NULL);
safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf), "%d%u%u%x%x%x",
getpid(),
TOD.tv_sec,
TOD.tv_usec,
myGlobals.startedAs,
myGlobals.udpSvc,
myGlobals.tcpSvc );
RAND_add(buf, strlen(buf), (double)24.0);
directoryPointer = opendir(myGlobals.dbPath);
if (directoryPointer == NULL) {
traceEvent(CONST_TRACE_WARNING, "SSL_PRNG: Unable to find directory '%s' for additional randomness", myGlobals.dbPath);
} else {
while((dp = readdir(directoryPointer)) != NULL) {
if (dp->d_name[0] != '.') {
safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf), "%s/%s", myGlobals.dbPath, dp->d_name);
if (stat(buf, &fStat) == 0) {
RAND_add(&fStat, sizeof(fStat), (double)16.0);
}
}
}
closedir(directoryPointer);
}
if (RAND_status() == 0) {
traceEvent(CONST_TRACE_WARNING, "SSL_PRNG: Unsuccessfully initialized - https:// may not work.");
} else {
traceEvent(CONST_TRACE_INFO, "SSL_PRNG: Successfully initialized.");
}
} else {
traceEvent(CONST_TRACE_INFO, "SSL_PRNG: Automatically initialized!");
}
for(idx=0; myGlobals.configFileDirs[idx] != NULL; idx++) {
safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf), "%s/%s", myGlobals.configFileDirs[idx], CONST_SSL_CERTF_FILENAME);
revertSlashIfWIN32(buf, 0);
if((fd = fopen(buf, "rb")) != NULL)
break;
}
if(fd == NULL) {
traceEvent(CONST_TRACE_WARNING,
"SSL: Unable to find certificate '%s'. SSL support has been disabled",
CONST_SSL_CERTF_FILENAME);
return(-1);
} else
fclose(fd);
SSL_load_error_strings();
SSL_library_init();
OpenSSL_add_ssl_algorithms();
#if (OPENSSL_VERSION_NUMBER < 0x00905100l)
needs_openssl_095_or_later();
#endif
meth = (SSL_METHOD*)SSLv23_server_method();
myGlobals.ctx = SSL_CTX_new(meth);
if (!myGlobals.ctx) {
ntop_ssl_error_report("ssl_init-server_method");
return(2);
}
SSL_CTX_set_options(myGlobals.ctx, SSL_OP_ALL); /* Enable the work-arounds */
SSL_CTX_set_options(myGlobals.ctx, SSL_OP_NO_TLSv1);
if ((!SSL_CTX_load_verify_locations(myGlobals.ctx, NULL, NULL)) ||
(!SSL_CTX_set_default_verify_paths(myGlobals.ctx))) {
ntop_ssl_error_report("ssl_init-verify");
}
SSL_CTX_set_session_id_context(myGlobals.ctx,
(void*)&s_server_session_id_context,
sizeof s_server_session_id_context);
SSL_CTX_set_client_CA_list(myGlobals.ctx,SSL_load_client_CA_file(NULL));
if (SSL_CTX_use_certificate_file(myGlobals.ctx, buf, SSL_FILETYPE_PEM) <= 0) {
ntop_ssl_error_report("ssl_init-use_cert");
return(3);
}
if (SSL_CTX_use_PrivateKey_file(myGlobals.ctx, buf, SSL_FILETYPE_PEM) <= 0) {
ntop_ssl_error_report("ssl_init-use_pvtkey");
return(4);
}
if (!SSL_CTX_check_private_key(myGlobals.ctx)) {
traceEvent(CONST_TRACE_ERROR, "Private key does not match the certificate public key");
return(5);
}
myGlobals.sslInitialized=1;
traceEvent(CONST_TRACE_INFO, "SSL initialized successfully");
return(0);
}
/* ********************* */
static int init_ssl_connection(SSL *con) {
int i;
long verify_error;
if(!myGlobals.sslInitialized) return(0);
if ((i=SSL_accept(con)) <= 0) {
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "SSL_accept: %d", i);
#endif
if (BIO_sock_should_retry(i))
return(1);
verify_error=SSL_get_verify_result(con);
if (verify_error != X509_V_OK) {
traceEvent(CONST_TRACE_WARNING, "verify error:%s", X509_verify_cert_error_string(verify_error));
}
else
ntop_ssl_error_report("ssl_init_connection");
return(0);
}
#ifdef DEBUG
{
/* the following declarations are needed to put debug mode to work */
X509 *peer;
char *str, buf[BUFSIZ];
peer=SSL_get_peer_certificate(con);
if(peer != NULL) {
traceEvent(CONST_TRACE_INFO, "Client certificate");
X509_NAME_oneline(X509_get_subject_name(peer),buf,BUFSIZ);
traceEvent(CONST_TRACE_INFO, "subject=%s",buf);
X509_NAME_oneline(X509_get_issuer_name(peer),buf,BUFSIZ);
traceEvent(CONST_TRACE_INFO, "issuer=%s",buf);
X509_free(peer);
}
if (SSL_get_shared_ciphers(con,buf,BUFSIZ) != NULL)
traceEvent(CONST_TRACE_INFO, "Shared ciphers:%s",buf);
str=SSL_CIPHER_get_name(SSL_get_current_cipher(con));
traceEvent(CONST_TRACE_INFO, "CIPHER is %s",(str != NULL)?str:"(NONE)");
if (con->hit) traceEvent(CONST_TRACE_INFO, "Reused session-id");
if (SSL_ctrl(con,SSL_CTRL_GET_FLAGS,0,NULL) &
TLS1_FLAGS_TLS_PADDING_BUG)
traceEvent(CONST_TRACE_WARNING, "Peer has incorrect TLSv1 block padding");
}
#endif
return(1);
}
/* ********************* */
int accept_ssl_connection(int fd) {
int i;
if(!myGlobals.sslInitialized) return(-1);
for(i=0; i<MAX_SSL_CONNECTIONS; i++) {
if(myGlobals.ssl[i].ctx == NULL) {
myGlobals.ssl[i].ctx = SSL_new(myGlobals.ctx);
if (myGlobals.ssl[i].ctx==NULL)
exit (1);
SSL_clear(myGlobals.ssl[i].ctx);
SSL_set_fd(myGlobals.ssl[i].ctx, fd);
myGlobals.ssl[i].socketId = fd;
if(!SSL_is_init_finished(myGlobals.ssl[i].ctx))
init_ssl_connection(myGlobals.ssl[i].ctx);
break;
}
}
if(i<MAX_SSL_CONNECTIONS)
return 1;
else
return -1;
}
/* ********************* */
SSL* getSSLsocket(int fd) {
int i;
if(!myGlobals.sslInitialized) return(NULL);
for(i=0; i<MAX_SSL_CONNECTIONS; i++) {
if((myGlobals.ssl[i].ctx != NULL)
&& (myGlobals.ssl[i].socketId == fd)) {
return(myGlobals.ssl[i].ctx);
}
}
return(NULL);
}
/* ********************* */
int term_ssl_connection(int fd) {
int i, rc = 0;
if(!myGlobals.sslInitialized) return(0);
for(i=0; i<MAX_SSL_CONNECTIONS; i++) {
if((myGlobals.ssl[i].ctx != NULL)
&& (myGlobals.ssl[i].socketId == fd)) {
rc = close(myGlobals.ssl[i].socketId);
SSL_free(myGlobals.ssl[i].ctx);
myGlobals.ssl[i].ctx = NULL;
}
}
return(rc);
}
/* ********************* */
void term_ssl(void) {
int i;
if(!myGlobals.sslInitialized) return;
for(i=0; i<MAX_SSL_CONNECTIONS; i++) {
if(myGlobals.ssl[i].ctx != NULL) {
close(myGlobals.ssl[i].socketId);
SSL_free (myGlobals.ssl[i].ctx);
myGlobals.ssl[i].ctx = NULL;
}
}
}
#endif