Do not explicitly disable SSLv3 if GnuTLS does it already.
[claws.git] / src / common / ssl.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #ifdef USE_GNUTLS
26 #include "defs.h"
27
28 #include <stdlib.h>
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <errno.h>
32 #include <pthread.h>
33
34 #include "claws.h"
35 #include "utils.h"
36 #include "ssl.h"
37 #include "ssl_certificate.h"
38 #include "hooks.h"
39
40 #if GNUTLS_VERSION_NUMBER <= 0x020b00
41 #include <gcrypt.h>
42 GCRY_THREAD_OPTION_PTHREAD_IMPL;
43 #endif
44
45 #ifdef HAVE_LIBETPAN
46 #include <libetpan/mailstream_ssl.h>
47 #endif
48
49 #ifdef USE_PTHREAD
50 #include <pthread.h>
51 #endif
52
53 #ifdef USE_PTHREAD
54 typedef struct _thread_data {
55         gnutls_session_t ssl;
56         gboolean done;
57 } thread_data;
58 #endif
59
60 #if GNUTLS_VERSION_NUMBER <= 0x020c00
61 static int gnutls_client_cert_cb(gnutls_session_t session,
62                                const gnutls_datum_t *req_ca_rdn, int nreqs,
63                                const gnutls_pk_algorithm_t *sign_algos,
64                                int sign_algos_length, gnutls_retr_st *st)
65 #else
66 static int gnutls_cert_cb(gnutls_session_t session,
67                                const gnutls_datum_t *req_ca_rdn, int nreqs,
68                                const gnutls_pk_algorithm_t *sign_algos,
69                                int sign_algos_length, gnutls_retr2_st *st)
70 #endif
71 {
72         SSLClientCertHookData hookdata;
73         SockInfo *sockinfo = (SockInfo *)gnutls_session_get_ptr(session);
74         gnutls_certificate_type_t type = gnutls_certificate_type_get(session);
75         gnutls_x509_crt_t crt;
76         gnutls_x509_privkey_t key;
77
78         st->ncerts = 0;
79
80         hookdata.account = sockinfo->account;
81         hookdata.cert_path = NULL;
82         hookdata.password = NULL;
83         hookdata.is_smtp = sockinfo->is_smtp;
84         hooks_invoke(SSLCERT_GET_CLIENT_CERT_HOOKLIST, &hookdata);      
85
86         if (hookdata.cert_path == NULL) {
87                 g_free(hookdata.password);
88                 return 0;
89         }
90
91         sockinfo->client_crt = ssl_certificate_get_x509_from_pem_file(hookdata.cert_path);
92         sockinfo->client_key = ssl_certificate_get_pkey_from_pem_file(hookdata.cert_path);
93         if (!(sockinfo->client_crt && sockinfo->client_key)) {
94                 /* try pkcs12 format */
95                 ssl_certificate_get_x509_and_pkey_from_p12_file(hookdata.cert_path, hookdata.password, 
96                         &crt, &key);
97                 sockinfo->client_crt = crt;
98                 sockinfo->client_key = key;
99         }
100
101         if (type == GNUTLS_CRT_X509 && sockinfo->client_crt && sockinfo->client_key) {
102                 st->ncerts = 1;
103 #if GNUTLS_VERSION_NUMBER <= 0x020c00
104                 st->type = type;
105 #else
106                 st->key_type = type;
107 #endif
108                 st->cert.x509 = &(sockinfo->client_crt);
109                 st->key.x509 = sockinfo->client_key;
110                 st->deinit_all = 0;
111                 g_free(hookdata.password);
112                 return 0;
113         }
114         g_free(hookdata.password);
115         return 0;
116 }
117
118 const gchar *claws_ssl_get_cert_file(void)
119 {
120 #ifndef G_OS_WIN32
121         const char *cert_files[]={
122                 "/etc/pki/tls/certs/ca-bundle.crt",
123                 "/etc/certs/ca-bundle.crt",
124                 "/etc/ssl/ca-bundle.pem",
125                 "/usr/share/ssl/certs/ca-bundle.crt",
126                 "/etc/ssl/certs/ca-certificates.crt",
127                 "/usr/local/ssl/certs/ca-bundle.crt",
128                 "/etc/apache/ssl.crt/ca-bundle.crt",
129                 "/usr/share/curl/curl-ca-bundle.crt",
130                 "/usr/share/curl/curl-ca-bundle.crt",
131                 "/usr/lib/ssl/cert.pem",
132                 NULL};
133         int i;
134 #endif
135
136         /* We honor this environment variable on all platforms. */
137         if (g_getenv("SSL_CERT_FILE"))
138                 return g_getenv("SSL_CERT_FILE");
139
140 #ifndef G_OS_WIN32
141         for (i = 0; cert_files[i]; i++) {
142                 if (is_file_exist(cert_files[i]))
143                         return cert_files[i];
144         }
145         return NULL;
146 #else
147         return w32_get_cert_file();
148 #endif
149 }
150
151 const gchar *claws_ssl_get_cert_dir(void)
152 {
153         const char *cert_dirs[]={
154                 "/etc/pki/tls/certs",
155                 "/etc/certs",
156                 "/usr/share/ssl/certs",
157                 "/etc/ssl/certs",
158                 "/usr/local/ssl/certs",
159                 "/etc/apache/ssl.crt",
160                 "/usr/share/curl",
161                 "/usr/lib/ssl/certs",
162                 NULL};
163         int i;
164         
165         if (g_getenv("SSL_CERT_DIR"))
166                 return g_getenv("SSL_CERT_DIR");
167 #ifndef G_OS_WIN32
168         for (i = 0; cert_dirs[i]; i++) {
169                 if (is_dir_exist(cert_dirs[i]))
170                         return cert_dirs[i];
171         }
172         return NULL;
173 #else
174         return NULL;
175 #endif
176 }
177
178 void ssl_init(void)
179 {
180 #if GNUTLS_VERSION_NUMBER <= 0x020b00
181         gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
182 #endif
183 #ifdef HAVE_LIBETPAN
184         mailstream_gnutls_init_not_required();
185 #endif  
186         gnutls_global_init();
187 }
188
189 void ssl_done(void)
190 {
191         gnutls_global_deinit();
192 }
193
194 #ifdef USE_PTHREAD
195 static void *SSL_connect_thread(void *data)
196 {
197         thread_data *td = (thread_data *)data;
198         int result = -1;
199
200         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
201         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
202
203         do {
204                 result = gnutls_handshake(td->ssl);
205         } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
206
207         td->done = TRUE; /* let the caller thread join() */
208         return GINT_TO_POINTER(result);
209 }
210 #endif
211
212 static gint SSL_connect_nb(gnutls_session_t ssl)
213 {
214         int result;
215 #ifdef USE_PTHREAD
216         thread_data *td = g_new0(thread_data, 1);
217         pthread_t pt;
218         pthread_attr_t pta;
219         void *res = NULL;
220         time_t start_time = time(NULL);
221         gboolean killed = FALSE;
222         
223         td->ssl  = ssl;
224         td->done = FALSE;
225         
226         /* try to create a thread to initialize the SSL connection,
227          * fallback to blocking method in case of problem 
228          */
229         if (pthread_attr_init(&pta) != 0 ||
230             pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
231             pthread_create(&pt, &pta, SSL_connect_thread, td) != 0) {
232                 do {
233                         result = gnutls_handshake(td->ssl);
234                 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
235                 return result;
236         }
237         debug_print("waiting for SSL_connect thread...\n");
238         while(!td->done) {
239                 /* don't let the interface freeze while waiting */
240                 claws_do_idle();
241                 if (time(NULL) - start_time > 30) {
242                         pthread_cancel(pt);
243                         td->done = TRUE;
244                         killed = TRUE;
245                 }
246         }
247
248         /* get the thread's return value and clean its resources */
249         pthread_join(pt, &res);
250         g_free(td);
251         
252         if (killed) {
253                 res = GINT_TO_POINTER(-1);
254         }
255         debug_print("SSL_connect thread returned %d\n", 
256                         GPOINTER_TO_INT(res));
257         
258         return GPOINTER_TO_INT(res);
259 #else /* USE_PTHREAD */
260         do {
261                 result = gnutls_handshake(ssl);
262         } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
263 #endif
264 }
265
266 gnutls_x509_crt_t *ssl_get_certificate_chain(gnutls_session_t session, gint *list_len)
267 {
268         const gnutls_datum_t *raw_cert_list;
269         gnutls_x509_crt_t *certs = NULL;
270         gboolean result = TRUE;
271
272         *list_len = -1;
273         if (!session)
274                 return NULL;
275
276         raw_cert_list = gnutls_certificate_get_peers(session, list_len);
277
278         if (raw_cert_list && gnutls_certificate_type_get(session) == GNUTLS_CRT_X509) {
279                 int i = 0;
280
281                 if (*list_len > 128)
282                         *list_len = 128;
283
284                 certs = g_malloc(sizeof(gnutls_x509_crt_t) * (*list_len));
285
286                 for(i = 0 ; i < (*list_len) ; i++) {
287                         int r;
288
289                         gnutls_x509_crt_init(&certs[i]);
290                         r = gnutls_x509_crt_import(certs[i], &raw_cert_list[i], GNUTLS_X509_FMT_DER);
291                         if (r < 0) {
292                                 g_warning("cert get failure: %d %s", r, gnutls_strerror(r));
293
294                                 result = FALSE;
295                                 i--;
296                                 break;
297                         }
298                 }
299                 if (!result) {
300                         for (; i >= 0; i--)
301                                 gnutls_x509_crt_deinit(certs[i]);
302
303                         g_free(certs);
304                         *list_len = -1;
305
306                         return NULL;
307                 }
308         }
309
310         return certs;
311 }
312
313 gboolean ssl_init_socket(SockInfo *sockinfo)
314 {
315         gnutls_session_t session;
316         int r, i;
317         unsigned int cert_list_length;
318         gnutls_x509_crt_t *certs = NULL;
319         gnutls_certificate_credentials_t xcred;
320
321         if (gnutls_certificate_allocate_credentials (&xcred) != 0)
322                 return FALSE;
323
324         r = gnutls_init(&session, GNUTLS_CLIENT);
325         if (session == NULL || r != 0)
326                 return FALSE;
327
328         if (sockinfo->gnutls_priority && strlen(sockinfo->gnutls_priority)) {
329                 r = gnutls_priority_set_direct(session, sockinfo->gnutls_priority, NULL);
330                 debug_print("Setting GnuTLS priority to %s, status = %d\n",
331                             sockinfo->gnutls_priority, r);
332         }
333 #ifdef GNUTLS_VERSION_NUMBER < 0x030400
334         else {
335                 gnutls_priority_set_direct(session, "NORMAL:-VERS-SSL3.0", NULL);
336         }
337 #endif
338         gnutls_record_disable_padding(session);
339
340         gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
341
342         if (claws_ssl_get_cert_file()) {
343                 r = gnutls_certificate_set_x509_trust_file(xcred, claws_ssl_get_cert_file(),  GNUTLS_X509_FMT_PEM);
344                 if (r < 0)
345                         g_warning("Can't read SSL_CERT_FILE '%s': %s",
346                                 claws_ssl_get_cert_file(), 
347                                 gnutls_strerror(r));
348         } else {
349                 debug_print("Can't find SSL ca-certificates file\n");
350         }
351         gnutls_certificate_set_verify_flags (xcred, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
352
353         gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) GINT_TO_POINTER(sockinfo->sock));
354         gnutls_session_set_ptr(session, sockinfo);
355 #if GNUTLS_VERSION_NUMBER <= 0x020c00
356         gnutls_certificate_client_set_retrieve_function(xcred, gnutls_client_cert_cb);
357 #else
358         gnutls_certificate_set_retrieve_function(xcred, gnutls_cert_cb);
359 #endif
360
361         gnutls_dh_set_prime_bits(session, 512);
362
363         if ((r = SSL_connect_nb(session)) < 0) {
364                 g_warning("SSL connection failed (%s)", gnutls_strerror(r));
365                 gnutls_certificate_free_credentials(xcred);
366                 gnutls_deinit(session);
367                 return FALSE;
368         }
369
370         /* Get server's certificate (note: beware of dynamic allocation) */
371         certs = ssl_get_certificate_chain(session, &cert_list_length);
372
373         if (!certs) {
374                 gnutls_certificate_free_credentials(xcred);
375                 gnutls_deinit(session);
376                 return FALSE;
377         }
378
379         if (!ssl_certificate_check_chain(certs, cert_list_length, sockinfo->hostname, sockinfo->port,
380                                          sockinfo->ssl_cert_auto_accept)) {
381                 for (i = 0; i < cert_list_length; i++)
382                         gnutls_x509_crt_deinit(certs[i]);
383                 g_free(certs);
384                 gnutls_certificate_free_credentials(xcred);
385                 gnutls_deinit(session);
386                 return FALSE;
387         }
388
389         for (i = 0; i < cert_list_length; i++)
390                 gnutls_x509_crt_deinit(certs[i]);
391         g_free(certs);
392
393         sockinfo->ssl = session;
394         sockinfo->xcred = xcred;
395         return TRUE;
396 }
397
398 void ssl_done_socket(SockInfo *sockinfo)
399 {
400         if (sockinfo && sockinfo->ssl) {
401                 if (sockinfo->xcred)
402                         gnutls_certificate_free_credentials(sockinfo->xcred);
403                 gnutls_deinit(sockinfo->ssl);
404                 if (sockinfo->client_crt)
405                         gnutls_x509_crt_deinit(sockinfo->client_crt);
406                 if (sockinfo->client_key)
407                         gnutls_x509_privkey_deinit(sockinfo->client_key);
408                 sockinfo->client_key = NULL;
409                 sockinfo->client_crt = NULL;
410                 sockinfo->xcred = NULL;
411                 sockinfo->ssl = NULL;
412         }
413 }
414
415 #endif /* USE_GNUTLS */