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