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