e1f3e806532f294cf0711296303bcec6899cc74a
[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 #if GNUTLS_VERSION_NUMBER <= 0x020b00
35 #include <gcrypt.h>
36 GCRY_THREAD_OPTION_PTHREAD_IMPL;
37 #endif
38
39 #include "claws.h"
40 #include "utils.h"
41 #include "ssl.h"
42 #include "ssl_certificate.h"
43 #include "hooks.h"
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 ssl;
56         gboolean done;
57 } thread_data;
58 #endif
59
60 static int gnutls_client_cert_cb(gnutls_session session,
61                                const gnutls_datum *req_ca_rdn, int nreqs,
62                                const gnutls_pk_algorithm *sign_algos,
63                                int sign_algos_length, gnutls_retr_st *st)
64 {
65         SSLClientCertHookData hookdata;
66         SockInfo *sockinfo = (SockInfo *)gnutls_session_get_ptr(session);
67         gnutls_certificate_type type = gnutls_certificate_type_get(session);
68         gnutls_x509_crt crt;
69         gnutls_x509_privkey key;
70
71         st->ncerts = 0;
72
73         hookdata.account = sockinfo->account;
74         hookdata.cert_path = NULL;
75         hookdata.password = NULL;
76         hookdata.is_smtp = sockinfo->is_smtp;
77         hooks_invoke(SSLCERT_GET_CLIENT_CERT_HOOKLIST, &hookdata);      
78
79         if (hookdata.cert_path == NULL)
80                 return 0;
81
82         sockinfo->client_crt = ssl_certificate_get_x509_from_pem_file(hookdata.cert_path);
83         sockinfo->client_key = ssl_certificate_get_pkey_from_pem_file(hookdata.cert_path);
84         if (!(sockinfo->client_crt && sockinfo->client_key)) {
85                 /* try pkcs12 format */
86                 ssl_certificate_get_x509_and_pkey_from_p12_file(hookdata.cert_path, hookdata.password, 
87                         &crt, &key);
88                 sockinfo->client_crt = crt;
89                 sockinfo->client_key = key;
90         }
91
92         if (type == GNUTLS_CRT_X509 && sockinfo->client_crt && sockinfo->client_key) {
93                 st->ncerts = 1;
94                 st->type = type;
95                 st->cert.x509 = &(sockinfo->client_crt);
96                 st->key.x509 = sockinfo->client_key;
97                 st->deinit_all = 0;
98                 return 0;
99         }
100         return 0;
101 }
102
103 const gchar *claws_ssl_get_cert_file(void)
104 {
105         const char *cert_files[]={
106                 "/etc/pki/tls/certs/ca-bundle.crt",
107                 "/etc/certs/ca-bundle.crt",
108                 "/usr/share/ssl/certs/ca-bundle.crt",
109                 "/etc/ssl/certs/ca-certificates.crt",
110                 "/usr/local/ssl/certs/ca-bundle.crt",
111                 "/etc/apache/ssl.crt/ca-bundle.crt",
112                 "/usr/share/curl/curl-ca-bundle.crt",
113                 "/usr/share/curl/curl-ca-bundle.crt",
114                 "/usr/lib/ssl/cert.pem",
115                 NULL};
116         int i;
117         
118         if (g_getenv("SSL_CERT_FILE"))
119                 return g_getenv("SSL_CERT_FILE");
120 #ifndef G_OS_WIN32
121         for (i = 0; cert_files[i]; i++) {
122                 if (is_file_exist(cert_files[i]))
123                         return cert_files[i];
124         }
125         return NULL;
126 #else
127         return get_cert_file();
128 #endif
129 }
130
131 const gchar *claws_ssl_get_cert_dir(void)
132 {
133         const char *cert_dirs[]={
134                 "/etc/pki/tls/certs",
135                 "/etc/certs",
136                 "/usr/share/ssl/certs",
137                 "/etc/ssl/certs",
138                 "/usr/local/ssl/certs",
139                 "/etc/apache/ssl.crt",
140                 "/usr/share/curl",
141                 "/usr/lib/ssl/certs",
142                 NULL};
143         int i;
144         
145         if (g_getenv("SSL_CERT_DIR"))
146                 return g_getenv("SSL_CERT_DIR");
147 #ifndef G_OS_WIN32
148         for (i = 0; cert_dirs[i]; i++) {
149                 if (is_dir_exist(cert_dirs[i]))
150                         return cert_dirs[i];
151         }
152         return NULL;
153 #else
154         return "put_what_s_needed_here";
155 #endif
156 }
157
158 void ssl_init(void)
159 {
160 #if GNUTLS_VERSION_NUMBER <= 0x020b00
161         gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
162 #endif
163 #ifdef HAVE_LIBETPAN
164         mailstream_gnutls_init_not_required();
165 #endif  
166         gnutls_global_init();
167 }
168
169 void ssl_done(void)
170 {
171         gnutls_global_deinit();
172 }
173
174 #ifdef USE_PTHREAD
175 static void *SSL_connect_thread(void *data)
176 {
177         thread_data *td = (thread_data *)data;
178         int result = -1;
179
180         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
181         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
182
183         do {
184                 result = gnutls_handshake(td->ssl);
185         } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
186
187         td->done = TRUE; /* let the caller thread join() */
188         return GINT_TO_POINTER(result);
189 }
190 #endif
191
192 static gint SSL_connect_nb(gnutls_session ssl)
193 {
194         int result;
195 #ifdef USE_PTHREAD
196         thread_data *td = g_new0(thread_data, 1);
197         pthread_t pt;
198         pthread_attr_t pta;
199         void *res = NULL;
200         time_t start_time = time(NULL);
201         gboolean killed = FALSE;
202         
203         td->ssl  = ssl;
204         td->done = FALSE;
205         
206         /* try to create a thread to initialize the SSL connection,
207          * fallback to blocking method in case of problem 
208          */
209         if (pthread_attr_init(&pta) != 0 ||
210             pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
211             pthread_create(&pt, &pta, SSL_connect_thread, td) != 0) {
212                 do {
213                         result = gnutls_handshake(td->ssl);
214                 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
215                 return result;
216         }
217         debug_print("waiting for SSL_connect thread...\n");
218         while(!td->done) {
219                 /* don't let the interface freeze while waiting */
220                 claws_do_idle();
221                 if (time(NULL) - start_time > 30) {
222                         pthread_cancel(pt);
223                         td->done = TRUE;
224                         killed = TRUE;
225                 }
226         }
227
228         /* get the thread's return value and clean its resources */
229         pthread_join(pt, &res);
230         g_free(td);
231         
232         if (killed) {
233                 res = GINT_TO_POINTER(-1);
234         }
235         debug_print("SSL_connect thread returned %d\n", 
236                         GPOINTER_TO_INT(res));
237         
238         return GPOINTER_TO_INT(res);
239 #else /* USE_PTHREAD */
240         do {
241                 result = gnutls_handshake(ssl);
242         } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
243 #endif
244 }
245
246 gboolean ssl_init_socket(SockInfo *sockinfo)
247 {
248         return ssl_init_socket_with_method(sockinfo, SSL_METHOD_SSLv23);
249 }
250
251 gboolean ssl_init_socket_with_method(SockInfo *sockinfo, SSLMethod method)
252 {
253         gnutls_session session;
254         int r;
255         const gnutls_datum *raw_cert_list;
256         unsigned int raw_cert_list_length;
257         gnutls_x509_crt cert = NULL;
258         guint status;
259         gnutls_certificate_credentials_t xcred;
260
261         if (gnutls_certificate_allocate_credentials (&xcred) != 0)
262                 return FALSE;
263
264         r = gnutls_init(&session, GNUTLS_CLIENT);
265         if (session == NULL || r != 0)
266                 return FALSE;
267
268 #if GNUTLS_VERSION_NUMBER < 0x030003
269         gnutls_transport_set_lowat (session, 0); 
270 #endif
271         if (method == 0)
272                 gnutls_priority_set_direct(session, "NORMAL:-VERS-TLS1.0:-VERS-TLS1.1:-VERS-TLS1.2", NULL);
273         else
274                 gnutls_priority_set_direct(session, "NORMAL", NULL);
275         gnutls_record_disable_padding(session);
276
277         gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
278
279         if (claws_ssl_get_cert_file()) {
280                 r = gnutls_certificate_set_x509_trust_file(xcred, claws_ssl_get_cert_file(),  GNUTLS_X509_FMT_PEM);
281                 if (r < 0)
282                         g_warning("Can't read SSL_CERT_FILE %s: %s\n",
283                                 claws_ssl_get_cert_file(), 
284                                 gnutls_strerror(r));
285         } else {
286                 debug_print("Can't find SSL ca-certificates file\n");
287         }
288         gnutls_certificate_set_verify_flags (xcred, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
289
290         gnutls_transport_set_ptr(session, (gnutls_transport_ptr) sockinfo->sock);
291         gnutls_session_set_ptr(session, sockinfo);
292         gnutls_certificate_client_set_retrieve_function(xcred, gnutls_client_cert_cb);
293
294         gnutls_dh_set_prime_bits(session, 512);
295
296         if ((r = SSL_connect_nb(session)) < 0) {
297                 g_warning("SSL connection failed (%s)", gnutls_strerror(r));
298                 gnutls_certificate_free_credentials(xcred);
299                 gnutls_deinit(session);
300                 return FALSE;
301         }
302
303         /* Get server's certificate (note: beware of dynamic allocation) */
304         raw_cert_list = gnutls_certificate_get_peers(session, &raw_cert_list_length);
305
306         if (!raw_cert_list 
307         ||  gnutls_certificate_type_get(session) != GNUTLS_CRT_X509
308         ||  (r = gnutls_x509_crt_init(&cert)) < 0
309         ||  (r = gnutls_x509_crt_import(cert, &raw_cert_list[0], GNUTLS_X509_FMT_DER)) < 0) {
310                 g_warning("cert get failure: %d %s\n", r, gnutls_strerror(r));
311                 gnutls_certificate_free_credentials(xcred);
312                 gnutls_deinit(session);
313                 return FALSE;
314         }
315
316         r = gnutls_certificate_verify_peers2(session, &status);
317
318         if (!ssl_certificate_check(cert, status, sockinfo->hostname, sockinfo->port)) {
319                 gnutls_x509_crt_deinit(cert);
320                 gnutls_certificate_free_credentials(xcred);
321                 gnutls_deinit(session);
322                 return FALSE;
323         }
324
325         gnutls_x509_crt_deinit(cert);
326
327         sockinfo->ssl = session;
328         sockinfo->xcred = xcred;
329         return TRUE;
330 }
331
332 void ssl_done_socket(SockInfo *sockinfo)
333 {
334         if (sockinfo && sockinfo->ssl) {
335                 gnutls_certificate_free_credentials(sockinfo->xcred);
336                 gnutls_deinit(sockinfo->ssl);
337                 if (sockinfo->client_crt)
338                         gnutls_x509_crt_deinit(sockinfo->client_crt);
339                 if (sockinfo->client_key)
340                         gnutls_x509_privkey_deinit(sockinfo->client_key);
341                 sockinfo->client_key = NULL;
342                 sockinfo->client_crt = NULL;
343                 sockinfo->ssl = NULL;
344         }
345 }
346
347 #endif /* USE_GNUTLS */