168
|
1 /*****************************************************************************/
|
|
2 /* sslcommon.c - interface to OpenSSL */
|
|
3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */
|
|
4 /* */
|
|
5 /* This program is free software; you can redistribute it and/or modify */
|
|
6 /* it under the terms of the GNU General Public License as published by */
|
|
7 /* the Free Software Foundation; either version 2 of the License, or */
|
|
8 /* (at your option) any later version. */
|
|
9 /* */
|
|
10 /* This program is distributed in the hope that it will be useful, */
|
|
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
|
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
|
13 /* GNU General Public License for more details. */
|
|
14 /* */
|
|
15 /* You should have received a copy of the GNU General Public License */
|
|
16 /* along with this program; if not, write to the Free Software */
|
|
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */
|
|
18 /*****************************************************************************/
|
|
19
|
|
20 #include "gftp.h"
|
|
21
|
|
22 static const char cvsid[] = "$Id$";
|
|
23
|
|
24 /* Some of the functions in here was taken either entirely or partially from
|
|
25 * the O'Reilly book Network Security with OpenSSL */
|
|
26
|
169
|
27 #ifdef USE_SSL
|
|
28
|
174
|
29 static gftp_config_vars config_vars[] =
|
|
30 {
|
|
31 {"", N_("SSL Engine"), gftp_option_type_notebook, NULL, NULL, 0, NULL,
|
|
32 GFTP_PORT_GTK, NULL},
|
|
33
|
|
34 {"entropy_source", N_("SSL Entropy File:"),
|
|
35 gftp_option_type_text, "/dev/urandom", NULL, 0,
|
|
36 N_("SSL entropy file"), GFTP_PORT_ALL, 0},
|
175
|
37 {"entropy_len", N_("Entropy Seed Length:"),
|
|
38 gftp_option_type_int, GINT_TO_POINTER(1024), NULL, 0,
|
|
39 N_("The maximum number of bytes to seed the SSL engine with"),
|
|
40 GFTP_PORT_ALL, 0},
|
416
|
41 {"verify_ssl_peer", N_("Verify SSL Peer"),
|
|
42 gftp_option_type_checkbox, GINT_TO_POINTER(1), NULL, 0,
|
|
43 N_("Verify SSL Peer"), GFTP_PORT_ALL, NULL},
|
175
|
44
|
174
|
45 {NULL, NULL, 0, NULL, NULL, 0, NULL, 0, NULL}
|
|
46 };
|
|
47
|
199
|
48 static GMutex ** gftp_ssl_mutexes = NULL;
|
|
49 static volatile int gftp_ssl_initialized = 0;
|
168
|
50 static SSL_CTX * ctx = NULL;
|
|
51
|
199
|
52 struct CRYPTO_dynlock_value
|
|
53 {
|
|
54 GMutex * mutex;
|
|
55 };
|
|
56
|
168
|
57
|
174
|
58 void
|
|
59 ssl_register_module (void)
|
|
60 {
|
|
61 static volatile int module_registered = 0;
|
|
62
|
|
63 if (!module_registered)
|
|
64 {
|
|
65 gftp_register_config_vars (config_vars);
|
|
66 module_registered = 1;
|
|
67 }
|
|
68 }
|
|
69
|
|
70
|
175
|
71 static int
|
|
72 gftp_ssl_get_index (void)
|
|
73 {
|
|
74 static volatile int index = -1;
|
|
75
|
|
76 if (index < 0)
|
|
77 index = SSL_get_ex_new_index (0, gftp_version, NULL, NULL, NULL);
|
|
78
|
|
79 return index;
|
|
80 }
|
|
81
|
|
82
|
168
|
83 static int
|
|
84 gftp_ssl_verify_callback (int ok, X509_STORE_CTX *store)
|
|
85 {
|
175
|
86 char issuer[256], subject[256];
|
431
|
87 intptr_t verify_ssl_peer;
|
175
|
88 gftp_request * request;
|
|
89 SSL * ssl;
|
|
90
|
|
91 ssl = X509_STORE_CTX_get_ex_data (store, SSL_get_ex_data_X509_STORE_CTX_idx ());
|
|
92 request = SSL_get_ex_data (ssl, gftp_ssl_get_index ());
|
431
|
93 gftp_lookup_request_option (request, "verify_ssl_peer", &verify_ssl_peer);
|
|
94
|
|
95 if (!verify_ssl_peer)
|
|
96 ok = 1;
|
168
|
97
|
|
98 if (!ok)
|
|
99 {
|
|
100 X509 *cert = X509_STORE_CTX_get_current_cert (store);
|
|
101 int depth = X509_STORE_CTX_get_error_depth (store);
|
|
102 int err = X509_STORE_CTX_get_error (store);
|
|
103
|
175
|
104 X509_NAME_oneline (X509_get_issuer_name (cert), issuer, sizeof (issuer));
|
|
105 X509_NAME_oneline (X509_get_subject_name (cert), subject, sizeof (subject));
|
186
|
106 request->logging_function (gftp_logging_error, request,
|
|
107 _("Error with certificate at depth: %i\nIssuer = %s\nSubject = %s\nError %i:%s\n"),
|
175
|
108 depth, issuer, subject, err,
|
|
109 X509_verify_cert_error_string (err));
|
168
|
110 }
|
|
111
|
|
112 return ok;
|
|
113 }
|
|
114
|
|
115
|
|
116 static int
|
|
117 gftp_ssl_post_connection_check (gftp_request * request)
|
|
118 {
|
|
119 char data[256], *extstr;
|
|
120 int extcount, ok, i, j;
|
|
121 X509_EXTENSION *ext;
|
|
122 X509_NAME *subj;
|
|
123 X509 *cert;
|
|
124
|
|
125 ok = 0;
|
|
126 if (!(cert = SSL_get_peer_certificate (request->ssl)))
|
|
127 {
|
186
|
128 request->logging_function (gftp_logging_error, request,
|
168
|
129 _("Cannot get peer certificate\n"));
|
|
130 return (X509_V_ERR_APPLICATION_VERIFICATION);
|
|
131 }
|
|
132
|
|
133 if ((extcount = X509_get_ext_count (cert)) > 0)
|
|
134 {
|
|
135 for (i = 0; i < extcount; i++)
|
|
136 {
|
|
137 ext = X509_get_ext (cert, i);
|
|
138 extstr = (char *) OBJ_nid2sn (OBJ_obj2nid (X509_EXTENSION_get_object (ext)));
|
|
139
|
|
140 if (strcmp (extstr, "subjectAltName") == 0)
|
|
141 {
|
199
|
142 unsigned char *data;
|
|
143 STACK_OF(CONF_VALUE) *val;
|
|
144 CONF_VALUE *nval;
|
|
145 X509V3_EXT_METHOD *meth;
|
|
146 void *ext_str = NULL;
|
168
|
147
|
199
|
148 if (!(meth = X509V3_EXT_get (ext)))
|
|
149 break;
|
|
150
|
|
151 data = ext->value->data;
|
168
|
152
|
|
153 #if (OPENSSL_VERSION_NUMBER > 0x00907000L)
|
199
|
154 if (meth->it)
|
|
155 ext_str = ASN1_item_d2i (NULL, &data, ext->value->length,
|
|
156 ASN1_ITEM_ptr (meth->it));
|
|
157 else
|
|
158 ext_str = meth->d2i (NULL, &data, ext->value->length);
|
168
|
159 #else
|
199
|
160 ext_str = meth->d2i(NULL, &data, ext->value->length);
|
168
|
161 #endif
|
199
|
162 val = meth->i2v(meth, ext_str, NULL);
|
|
163
|
|
164 for (j = 0; j < sk_CONF_VALUE_num(val); j++)
|
|
165 {
|
|
166 nval = sk_CONF_VALUE_value (val, j);
|
|
167 if (strcmp (nval->name, "DNS") == 0 &&
|
|
168 strcmp (nval->value, request->hostname) == 0)
|
|
169 {
|
|
170 ok = 1;
|
|
171 break;
|
|
172 }
|
|
173 }
|
|
174 }
|
|
175
|
|
176 if (ok)
|
|
177 break;
|
|
178 }
|
168
|
179 }
|
215
|
180
|
199
|
181 if (!ok && (subj = X509_get_subject_name (cert)) &&
|
|
182 X509_NAME_get_text_by_NID (subj, NID_commonName, data, 256) > 0)
|
|
183 {
|
|
184 data[sizeof (data) - 1] = '\0';
|
|
185 if (strcasecmp (data, request->hostname) != 0)
|
|
186 {
|
|
187 request->logging_function (gftp_logging_error, request,
|
|
188 _("ERROR: The host in the SSL certificate (%s) does not match the host that we connected to (%s). Aborting connection.\n"),
|
|
189 data, request->hostname);
|
|
190 X509_free (cert);
|
|
191 return (X509_V_ERR_APPLICATION_VERIFICATION);
|
|
192 }
|
|
193 }
|
168
|
194
|
|
195 X509_free (cert);
|
199
|
196
|
168
|
197 return (SSL_get_verify_result(request->ssl));
|
|
198 }
|
|
199
|
|
200
|
199
|
201 static void
|
|
202 _gftp_ssl_locking_function (int mode, int n, const char * file, int line)
|
|
203 {
|
|
204 if (mode & CRYPTO_LOCK)
|
|
205 g_mutex_lock (gftp_ssl_mutexes[n]);
|
|
206 else
|
|
207 g_mutex_unlock (gftp_ssl_mutexes[n]);
|
|
208 }
|
|
209
|
|
210
|
|
211 static unsigned long
|
|
212 _gftp_ssl_id_function (void)
|
|
213 {
|
|
214 #if GLIB_MAJOR_VERSION > 1
|
|
215 return ((unsigned long) g_thread_self ());
|
|
216 #else
|
210
|
217 /* FIXME _ call pthread version. Once this is done, the #ifdef below can be
|
|
218 removed */
|
199
|
219 return (0);
|
|
220 #endif
|
|
221 }
|
|
222
|
|
223
|
|
224 static struct CRYPTO_dynlock_value *
|
|
225 _gftp_ssl_create_dyn_mutex (const char *file, int line)
|
|
226 {
|
|
227 struct CRYPTO_dynlock_value *value;
|
|
228
|
|
229 value = g_malloc (sizeof (*value));
|
|
230 value->mutex = g_mutex_new ();
|
|
231 return (value);
|
|
232 }
|
|
233
|
|
234
|
|
235 static void
|
|
236 _gftp_ssl_dyn_mutex_lock (int mode, struct CRYPTO_dynlock_value *l,
|
|
237 const char *file, int line)
|
|
238 {
|
|
239 if (mode & CRYPTO_LOCK)
|
|
240 g_mutex_lock (l->mutex);
|
|
241 else
|
|
242 g_mutex_unlock (l->mutex);
|
|
243 }
|
|
244
|
|
245
|
|
246 static void
|
|
247 _gftp_ssl_destroy_dyn_mutex (struct CRYPTO_dynlock_value *l,
|
|
248 const char *file, int line)
|
|
249 {
|
|
250 g_mutex_free (l->mutex);
|
|
251 g_free (l);
|
|
252 }
|
|
253
|
|
254
|
|
255 static void
|
|
256 _gftp_ssl_thread_setup (void)
|
|
257 {
|
|
258 int i;
|
|
259
|
215
|
260 #if G_MAJOR_VERSION == 1
|
210
|
261 /* Thread setup isn't supported in glib 1.2 yet */
|
|
262 return;
|
|
263 #endif
|
|
264
|
199
|
265 gftp_ssl_mutexes = g_malloc (CRYPTO_num_locks( ) * sizeof (*gftp_ssl_mutexes));
|
|
266
|
|
267 for (i = 0; i < CRYPTO_num_locks ( ); i++)
|
|
268 gftp_ssl_mutexes[i] = g_mutex_new ();
|
|
269
|
|
270 CRYPTO_set_id_callback (_gftp_ssl_id_function);
|
|
271 CRYPTO_set_locking_callback (_gftp_ssl_locking_function);
|
|
272 CRYPTO_set_dynlock_create_callback (_gftp_ssl_create_dyn_mutex);
|
|
273 CRYPTO_set_dynlock_lock_callback (_gftp_ssl_dyn_mutex_lock);
|
|
274 CRYPTO_set_dynlock_destroy_callback (_gftp_ssl_destroy_dyn_mutex);
|
|
275 }
|
|
276
|
|
277
|
168
|
278 int
|
|
279 gftp_ssl_startup (gftp_request * request)
|
|
280 {
|
431
|
281 intptr_t entropy_len;
|
174
|
282 char *entropy_source;
|
|
283
|
173
|
284 if (gftp_ssl_initialized)
|
|
285 return (0);
|
|
286
|
168
|
287 gftp_ssl_initialized = 1;
|
|
288
|
199
|
289 if (g_thread_supported ())
|
|
290 _gftp_ssl_thread_setup ();
|
|
291
|
168
|
292 if (!SSL_library_init ())
|
|
293 {
|
186
|
294 request->logging_function (gftp_logging_error, request,
|
460
|
295 _("Cannot initialize the OpenSSL library\n"));
|
168
|
296 return (GFTP_EFATAL);
|
|
297 }
|
|
298
|
|
299 SSL_load_error_strings ();
|
174
|
300
|
|
301 gftp_lookup_request_option (request, "entropy_source", &entropy_source);
|
175
|
302 gftp_lookup_request_option (request, "entropy_len", &entropy_len);
|
|
303 RAND_load_file (entropy_source, entropy_len);
|
168
|
304
|
|
305 ctx = SSL_CTX_new (SSLv23_method ());
|
|
306
|
|
307 if (SSL_CTX_set_default_verify_paths (ctx) != 1)
|
|
308 {
|
186
|
309 request->logging_function (gftp_logging_error, request,
|
168
|
310 _("Error loading default SSL certificates\n"));
|
173
|
311 return (GFTP_EFATAL);
|
168
|
312 }
|
|
313
|
431
|
314 SSL_CTX_set_verify (ctx, SSL_VERIFY_PEER, gftp_ssl_verify_callback);
|
|
315 SSL_CTX_set_verify_depth (ctx, 9);
|
416
|
316
|
168
|
317 SSL_CTX_set_options (ctx, SSL_OP_ALL|SSL_OP_NO_SSLv2);
|
|
318
|
|
319 if (SSL_CTX_set_cipher_list (ctx, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH") != 1)
|
|
320 {
|
186
|
321 request->logging_function (gftp_logging_error, request,
|
168
|
322 _("Error setting cipher list (no valid ciphers)\n"));
|
|
323 return (GFTP_EFATAL);
|
|
324 }
|
|
325
|
|
326 return (0);
|
|
327 }
|
|
328
|
|
329
|
|
330 int
|
|
331 gftp_ssl_session_setup (gftp_request * request)
|
|
332 {
|
416
|
333 intptr_t verify_ssl_peer;
|
168
|
334 BIO * bio;
|
|
335 long ret;
|
|
336
|
169
|
337 g_return_val_if_fail (request->datafd > 0, GFTP_EFATAL);
|
168
|
338
|
|
339 if (!gftp_ssl_initialized)
|
|
340 {
|
186
|
341 request->logging_function (gftp_logging_error, request,
|
168
|
342 _("Error: SSL engine was not initialized\n"));
|
215
|
343 gftp_disconnect (request);
|
168
|
344 return (GFTP_EFATAL);
|
|
345 }
|
|
346
|
199
|
347 /* FIXME - take this out. I need to find out how to do timeouts with the SSL
|
|
348 functions (a select() or poll() like function) */
|
|
349
|
|
350 if (gftp_fd_set_sockblocking (request, request->datafd, 0) < 0)
|
168
|
351 {
|
|
352 gftp_disconnect (request);
|
|
353 return (GFTP_ERETRYABLE);
|
|
354 }
|
|
355
|
|
356 if ((bio = BIO_new (BIO_s_socket ())) == NULL)
|
|
357 {
|
186
|
358 request->logging_function (gftp_logging_error, request,
|
168
|
359 _("Error setting up SSL connection (BIO object)\n"));
|
215
|
360 gftp_disconnect (request);
|
168
|
361 return (GFTP_EFATAL);
|
|
362 }
|
|
363
|
169
|
364 BIO_set_fd (bio, request->datafd, BIO_NOCLOSE);
|
168
|
365
|
|
366 if ((request->ssl = SSL_new (ctx)) == NULL)
|
|
367 {
|
186
|
368 request->logging_function (gftp_logging_error, request,
|
168
|
369 _("Error setting up SSL connection (SSL object)\n"));
|
215
|
370 gftp_disconnect (request);
|
168
|
371 return (GFTP_EFATAL);
|
|
372 }
|
|
373
|
|
374 SSL_set_bio (request->ssl, bio, bio);
|
175
|
375 SSL_set_ex_data (request->ssl, gftp_ssl_get_index (), request);
|
168
|
376
|
|
377 if (SSL_connect (request->ssl) <= 0)
|
215
|
378 {
|
|
379 gftp_disconnect (request);
|
|
380 return (GFTP_EFATAL);
|
|
381 }
|
168
|
382
|
416
|
383 gftp_lookup_request_option (request, "verify_ssl_peer", &verify_ssl_peer);
|
|
384
|
|
385 if (verify_ssl_peer &&
|
|
386 (ret = gftp_ssl_post_connection_check (request)) != X509_V_OK)
|
168
|
387 {
|
199
|
388 if (ret != X509_V_ERR_APPLICATION_VERIFICATION)
|
|
389 request->logging_function (gftp_logging_error, request,
|
|
390 _("Error with peer certificate: %s\n"),
|
|
391 X509_verify_cert_error_string (ret));
|
215
|
392 gftp_disconnect (request);
|
168
|
393 return (GFTP_EFATAL);
|
|
394 }
|
|
395
|
186
|
396 request->logging_function (gftp_logging_misc, request,
|
168
|
397 "SSL connection established using %s (%s)\n",
|
|
398 SSL_get_cipher_version (request->ssl),
|
|
399 SSL_get_cipher_name (request->ssl));
|
|
400
|
|
401 return (0);
|
|
402 }
|
|
403
|
|
404
|
|
405 ssize_t
|
|
406 gftp_ssl_read (gftp_request * request, void *ptr, size_t size, int fd)
|
|
407 {
|
|
408 ssize_t ret;
|
|
409 int err;
|
|
410
|
546
|
411 g_return_val_if_fail (request->ssl != NULL, GFTP_EFATAL);
|
|
412
|
168
|
413 if (!gftp_ssl_initialized)
|
|
414 {
|
186
|
415 request->logging_function (gftp_logging_error, request,
|
168
|
416 _("Error: SSL engine was not initialized\n"));
|
|
417 return (GFTP_EFATAL);
|
|
418 }
|
|
419
|
|
420 errno = 0;
|
|
421 ret = 0;
|
|
422 do
|
|
423 {
|
|
424 if ((ret = SSL_read (request->ssl, ptr, size)) < 0)
|
|
425 {
|
|
426 err = SSL_get_error (request->ssl, ret);
|
546
|
427 if (errno == EINTR || errno == EAGAIN)
|
168
|
428 {
|
215
|
429 if (request->cancel)
|
546
|
430 {
|
|
431 gftp_disconnect (request);
|
|
432 return (GFTP_ERETRYABLE);
|
|
433 }
|
|
434
|
|
435 continue;
|
|
436 }
|
168
|
437
|
215
|
438 request->logging_function (gftp_logging_error, request,
|
168
|
439 _("Error: Could not read from socket: %s\n"),
|
|
440 g_strerror (errno));
|
215
|
441 gftp_disconnect (request);
|
|
442
|
168
|
443 return (GFTP_ERETRYABLE);
|
|
444 }
|
546
|
445
|
|
446 break;
|
168
|
447 }
|
546
|
448 while (1);
|
168
|
449
|
|
450 return (ret);
|
|
451 }
|
|
452
|
|
453
|
|
454 ssize_t
|
|
455 gftp_ssl_write (gftp_request * request, const char *ptr, size_t size, int fd)
|
|
456 {
|
|
457 size_t ret, w_ret;
|
|
458
|
546
|
459 g_return_val_if_fail (request->ssl != NULL, GFTP_EFATAL);
|
|
460
|
168
|
461 if (!gftp_ssl_initialized)
|
|
462 {
|
186
|
463 request->logging_function (gftp_logging_error, request,
|
168
|
464 _("Error: SSL engine was not initialized\n"));
|
|
465 return (GFTP_EFATAL);
|
|
466 }
|
|
467
|
|
468 ret = 0;
|
|
469 do
|
|
470 {
|
|
471 w_ret = SSL_write (request->ssl, ptr, size);
|
|
472 if (w_ret <= 0)
|
|
473 {
|
546
|
474 if (errno == EINTR || errno == EAGAIN)
|
168
|
475 {
|
|
476 if (request != NULL && request->cancel)
|
546
|
477 {
|
|
478 gftp_disconnect (request);
|
|
479 return (GFTP_ERETRYABLE);
|
|
480 }
|
|
481
|
|
482 continue;
|
168
|
483 }
|
|
484
|
215
|
485 request->logging_function (gftp_logging_error, request,
|
168
|
486 _("Error: Could not write to socket: %s\n"),
|
|
487 g_strerror (errno));
|
215
|
488 gftp_disconnect (request);
|
|
489
|
168
|
490 return (GFTP_ERETRYABLE);
|
|
491 }
|
546
|
492
|
168
|
493 ptr += w_ret;
|
|
494 size -= w_ret;
|
|
495 ret += w_ret;
|
|
496 }
|
|
497 while (size > 0);
|
|
498
|
|
499 return (ret);
|
|
500 }
|
|
501
|
|
502 #endif
|