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';
|
769
|
185 /* Check for wildcard CN (must begin with *.) */
|
|
186 if (strncmp (data, "*.", 2) == 0)
|
|
187 {
|
|
188 size_t hostname_len = strlen (data) - 1;
|
|
189 if (strlen (request->hostname) > hostname_len &&
|
|
190 strcasecmp (&(data[1]), &(request->hostname[strlen (request->hostname) - hostname_len])) == 0)
|
|
191 ok = 1;
|
|
192 }
|
|
193 else if (strcasecmp (data, request->hostname) == 0)
|
|
194 ok = 1;
|
|
195
|
|
196 if (!ok)
|
199
|
197 {
|
|
198 request->logging_function (gftp_logging_error, request,
|
|
199 _("ERROR: The host in the SSL certificate (%s) does not match the host that we connected to (%s). Aborting connection.\n"),
|
|
200 data, request->hostname);
|
|
201 X509_free (cert);
|
|
202 return (X509_V_ERR_APPLICATION_VERIFICATION);
|
|
203 }
|
|
204 }
|
168
|
205
|
|
206 X509_free (cert);
|
199
|
207
|
168
|
208 return (SSL_get_verify_result(request->ssl));
|
|
209 }
|
|
210
|
|
211
|
199
|
212 static void
|
|
213 _gftp_ssl_locking_function (int mode, int n, const char * file, int line)
|
|
214 {
|
|
215 if (mode & CRYPTO_LOCK)
|
|
216 g_mutex_lock (gftp_ssl_mutexes[n]);
|
|
217 else
|
|
218 g_mutex_unlock (gftp_ssl_mutexes[n]);
|
|
219 }
|
|
220
|
|
221
|
|
222 static unsigned long
|
|
223 _gftp_ssl_id_function (void)
|
|
224 {
|
|
225 #if GLIB_MAJOR_VERSION > 1
|
|
226 return ((unsigned long) g_thread_self ());
|
|
227 #else
|
210
|
228 /* FIXME _ call pthread version. Once this is done, the #ifdef below can be
|
|
229 removed */
|
199
|
230 return (0);
|
|
231 #endif
|
|
232 }
|
|
233
|
|
234
|
|
235 static struct CRYPTO_dynlock_value *
|
|
236 _gftp_ssl_create_dyn_mutex (const char *file, int line)
|
|
237 {
|
|
238 struct CRYPTO_dynlock_value *value;
|
|
239
|
|
240 value = g_malloc (sizeof (*value));
|
|
241 value->mutex = g_mutex_new ();
|
|
242 return (value);
|
|
243 }
|
|
244
|
|
245
|
|
246 static void
|
|
247 _gftp_ssl_dyn_mutex_lock (int mode, struct CRYPTO_dynlock_value *l,
|
|
248 const char *file, int line)
|
|
249 {
|
|
250 if (mode & CRYPTO_LOCK)
|
|
251 g_mutex_lock (l->mutex);
|
|
252 else
|
|
253 g_mutex_unlock (l->mutex);
|
|
254 }
|
|
255
|
|
256
|
|
257 static void
|
|
258 _gftp_ssl_destroy_dyn_mutex (struct CRYPTO_dynlock_value *l,
|
|
259 const char *file, int line)
|
|
260 {
|
|
261 g_mutex_free (l->mutex);
|
|
262 g_free (l);
|
|
263 }
|
|
264
|
|
265
|
|
266 static void
|
|
267 _gftp_ssl_thread_setup (void)
|
|
268 {
|
|
269 int i;
|
|
270
|
215
|
271 #if G_MAJOR_VERSION == 1
|
210
|
272 /* Thread setup isn't supported in glib 1.2 yet */
|
|
273 return;
|
|
274 #endif
|
|
275
|
199
|
276 gftp_ssl_mutexes = g_malloc (CRYPTO_num_locks( ) * sizeof (*gftp_ssl_mutexes));
|
|
277
|
|
278 for (i = 0; i < CRYPTO_num_locks ( ); i++)
|
|
279 gftp_ssl_mutexes[i] = g_mutex_new ();
|
|
280
|
|
281 CRYPTO_set_id_callback (_gftp_ssl_id_function);
|
|
282 CRYPTO_set_locking_callback (_gftp_ssl_locking_function);
|
|
283 CRYPTO_set_dynlock_create_callback (_gftp_ssl_create_dyn_mutex);
|
|
284 CRYPTO_set_dynlock_lock_callback (_gftp_ssl_dyn_mutex_lock);
|
|
285 CRYPTO_set_dynlock_destroy_callback (_gftp_ssl_destroy_dyn_mutex);
|
|
286 }
|
|
287
|
|
288
|
168
|
289 int
|
|
290 gftp_ssl_startup (gftp_request * request)
|
|
291 {
|
431
|
292 intptr_t entropy_len;
|
174
|
293 char *entropy_source;
|
|
294
|
173
|
295 if (gftp_ssl_initialized)
|
|
296 return (0);
|
|
297
|
168
|
298 gftp_ssl_initialized = 1;
|
|
299
|
199
|
300 if (g_thread_supported ())
|
|
301 _gftp_ssl_thread_setup ();
|
|
302
|
168
|
303 if (!SSL_library_init ())
|
|
304 {
|
186
|
305 request->logging_function (gftp_logging_error, request,
|
460
|
306 _("Cannot initialize the OpenSSL library\n"));
|
168
|
307 return (GFTP_EFATAL);
|
|
308 }
|
|
309
|
|
310 SSL_load_error_strings ();
|
174
|
311
|
|
312 gftp_lookup_request_option (request, "entropy_source", &entropy_source);
|
175
|
313 gftp_lookup_request_option (request, "entropy_len", &entropy_len);
|
|
314 RAND_load_file (entropy_source, entropy_len);
|
168
|
315
|
|
316 ctx = SSL_CTX_new (SSLv23_method ());
|
|
317
|
|
318 if (SSL_CTX_set_default_verify_paths (ctx) != 1)
|
|
319 {
|
186
|
320 request->logging_function (gftp_logging_error, request,
|
168
|
321 _("Error loading default SSL certificates\n"));
|
173
|
322 return (GFTP_EFATAL);
|
168
|
323 }
|
|
324
|
431
|
325 SSL_CTX_set_verify (ctx, SSL_VERIFY_PEER, gftp_ssl_verify_callback);
|
|
326 SSL_CTX_set_verify_depth (ctx, 9);
|
416
|
327
|
168
|
328 SSL_CTX_set_options (ctx, SSL_OP_ALL|SSL_OP_NO_SSLv2);
|
|
329
|
|
330 if (SSL_CTX_set_cipher_list (ctx, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH") != 1)
|
|
331 {
|
186
|
332 request->logging_function (gftp_logging_error, request,
|
168
|
333 _("Error setting cipher list (no valid ciphers)\n"));
|
|
334 return (GFTP_EFATAL);
|
|
335 }
|
|
336
|
|
337 return (0);
|
|
338 }
|
|
339
|
|
340
|
|
341 int
|
|
342 gftp_ssl_session_setup (gftp_request * request)
|
|
343 {
|
416
|
344 intptr_t verify_ssl_peer;
|
168
|
345 BIO * bio;
|
|
346 long ret;
|
|
347
|
169
|
348 g_return_val_if_fail (request->datafd > 0, GFTP_EFATAL);
|
168
|
349
|
|
350 if (!gftp_ssl_initialized)
|
|
351 {
|
186
|
352 request->logging_function (gftp_logging_error, request,
|
168
|
353 _("Error: SSL engine was not initialized\n"));
|
215
|
354 gftp_disconnect (request);
|
168
|
355 return (GFTP_EFATAL);
|
|
356 }
|
|
357
|
199
|
358 /* FIXME - take this out. I need to find out how to do timeouts with the SSL
|
|
359 functions (a select() or poll() like function) */
|
|
360
|
|
361 if (gftp_fd_set_sockblocking (request, request->datafd, 0) < 0)
|
168
|
362 {
|
|
363 gftp_disconnect (request);
|
|
364 return (GFTP_ERETRYABLE);
|
|
365 }
|
|
366
|
|
367 if ((bio = BIO_new (BIO_s_socket ())) == NULL)
|
|
368 {
|
186
|
369 request->logging_function (gftp_logging_error, request,
|
168
|
370 _("Error setting up SSL connection (BIO object)\n"));
|
215
|
371 gftp_disconnect (request);
|
168
|
372 return (GFTP_EFATAL);
|
|
373 }
|
|
374
|
169
|
375 BIO_set_fd (bio, request->datafd, BIO_NOCLOSE);
|
168
|
376
|
|
377 if ((request->ssl = SSL_new (ctx)) == NULL)
|
|
378 {
|
186
|
379 request->logging_function (gftp_logging_error, request,
|
168
|
380 _("Error setting up SSL connection (SSL object)\n"));
|
215
|
381 gftp_disconnect (request);
|
168
|
382 return (GFTP_EFATAL);
|
|
383 }
|
|
384
|
|
385 SSL_set_bio (request->ssl, bio, bio);
|
175
|
386 SSL_set_ex_data (request->ssl, gftp_ssl_get_index (), request);
|
168
|
387
|
|
388 if (SSL_connect (request->ssl) <= 0)
|
215
|
389 {
|
|
390 gftp_disconnect (request);
|
|
391 return (GFTP_EFATAL);
|
|
392 }
|
168
|
393
|
416
|
394 gftp_lookup_request_option (request, "verify_ssl_peer", &verify_ssl_peer);
|
|
395
|
|
396 if (verify_ssl_peer &&
|
|
397 (ret = gftp_ssl_post_connection_check (request)) != X509_V_OK)
|
168
|
398 {
|
199
|
399 if (ret != X509_V_ERR_APPLICATION_VERIFICATION)
|
|
400 request->logging_function (gftp_logging_error, request,
|
|
401 _("Error with peer certificate: %s\n"),
|
|
402 X509_verify_cert_error_string (ret));
|
215
|
403 gftp_disconnect (request);
|
168
|
404 return (GFTP_EFATAL);
|
|
405 }
|
|
406
|
186
|
407 request->logging_function (gftp_logging_misc, request,
|
168
|
408 "SSL connection established using %s (%s)\n",
|
|
409 SSL_get_cipher_version (request->ssl),
|
|
410 SSL_get_cipher_name (request->ssl));
|
|
411
|
|
412 return (0);
|
|
413 }
|
|
414
|
|
415
|
|
416 ssize_t
|
|
417 gftp_ssl_read (gftp_request * request, void *ptr, size_t size, int fd)
|
|
418 {
|
|
419 ssize_t ret;
|
|
420 int err;
|
|
421
|
546
|
422 g_return_val_if_fail (request->ssl != NULL, GFTP_EFATAL);
|
|
423
|
168
|
424 if (!gftp_ssl_initialized)
|
|
425 {
|
186
|
426 request->logging_function (gftp_logging_error, request,
|
168
|
427 _("Error: SSL engine was not initialized\n"));
|
|
428 return (GFTP_EFATAL);
|
|
429 }
|
|
430
|
|
431 errno = 0;
|
|
432 ret = 0;
|
|
433 do
|
|
434 {
|
|
435 if ((ret = SSL_read (request->ssl, ptr, size)) < 0)
|
|
436 {
|
|
437 err = SSL_get_error (request->ssl, ret);
|
546
|
438 if (errno == EINTR || errno == EAGAIN)
|
168
|
439 {
|
215
|
440 if (request->cancel)
|
546
|
441 {
|
|
442 gftp_disconnect (request);
|
|
443 return (GFTP_ERETRYABLE);
|
|
444 }
|
|
445
|
|
446 continue;
|
|
447 }
|
168
|
448
|
215
|
449 request->logging_function (gftp_logging_error, request,
|
168
|
450 _("Error: Could not read from socket: %s\n"),
|
|
451 g_strerror (errno));
|
215
|
452 gftp_disconnect (request);
|
|
453
|
168
|
454 return (GFTP_ERETRYABLE);
|
|
455 }
|
546
|
456
|
|
457 break;
|
168
|
458 }
|
546
|
459 while (1);
|
168
|
460
|
|
461 return (ret);
|
|
462 }
|
|
463
|
|
464
|
|
465 ssize_t
|
|
466 gftp_ssl_write (gftp_request * request, const char *ptr, size_t size, int fd)
|
|
467 {
|
|
468 size_t ret, w_ret;
|
|
469
|
546
|
470 g_return_val_if_fail (request->ssl != NULL, GFTP_EFATAL);
|
|
471
|
168
|
472 if (!gftp_ssl_initialized)
|
|
473 {
|
186
|
474 request->logging_function (gftp_logging_error, request,
|
168
|
475 _("Error: SSL engine was not initialized\n"));
|
|
476 return (GFTP_EFATAL);
|
|
477 }
|
|
478
|
|
479 ret = 0;
|
|
480 do
|
|
481 {
|
|
482 w_ret = SSL_write (request->ssl, ptr, size);
|
|
483 if (w_ret <= 0)
|
|
484 {
|
546
|
485 if (errno == EINTR || errno == EAGAIN)
|
168
|
486 {
|
|
487 if (request != NULL && request->cancel)
|
546
|
488 {
|
|
489 gftp_disconnect (request);
|
|
490 return (GFTP_ERETRYABLE);
|
|
491 }
|
|
492
|
|
493 continue;
|
168
|
494 }
|
|
495
|
215
|
496 request->logging_function (gftp_logging_error, request,
|
168
|
497 _("Error: Could not write to socket: %s\n"),
|
|
498 g_strerror (errno));
|
215
|
499 gftp_disconnect (request);
|
|
500
|
168
|
501 return (GFTP_ERETRYABLE);
|
|
502 }
|
546
|
503
|
168
|
504 ptr += w_ret;
|
|
505 size -= w_ret;
|
|
506 ret += w_ret;
|
|
507 }
|
|
508 while (size > 0);
|
|
509
|
|
510 return (ret);
|
|
511 }
|
|
512
|
|
513 #endif
|