2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
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.
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.
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/>.
22 #include "claws-features.h"
30 #include <glib/gi18n.h>
37 #include "ssl_certificate.h"
40 #if GNUTLS_VERSION_NUMBER <= 0x020b00
42 GCRY_THREAD_OPTION_PTHREAD_IMPL;
46 #include <libetpan/mailstream_ssl.h>
54 typedef struct _thread_data {
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)
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)
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;
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);
86 if (hookdata.cert_path == NULL)
89 sockinfo->client_crt = ssl_certificate_get_x509_from_pem_file(hookdata.cert_path);
90 sockinfo->client_key = ssl_certificate_get_pkey_from_pem_file(hookdata.cert_path);
91 if (!(sockinfo->client_crt && sockinfo->client_key)) {
92 /* try pkcs12 format */
93 ssl_certificate_get_x509_and_pkey_from_p12_file(hookdata.cert_path, hookdata.password,
95 sockinfo->client_crt = crt;
96 sockinfo->client_key = key;
99 if (type == GNUTLS_CRT_X509 && sockinfo->client_crt && sockinfo->client_key) {
101 #if GNUTLS_VERSION_NUMBER <= 0x020c00
106 st->cert.x509 = &(sockinfo->client_crt);
107 st->key.x509 = sockinfo->client_key;
114 const gchar *claws_ssl_get_cert_file(void)
116 const char *cert_files[]={
117 "/etc/pki/tls/certs/ca-bundle.crt",
118 "/etc/certs/ca-bundle.crt",
119 "/etc/ssl/ca-bundle.pem",
120 "/usr/share/ssl/certs/ca-bundle.crt",
121 "/etc/ssl/certs/ca-certificates.crt",
122 "/usr/local/ssl/certs/ca-bundle.crt",
123 "/etc/apache/ssl.crt/ca-bundle.crt",
124 "/usr/share/curl/curl-ca-bundle.crt",
125 "/usr/share/curl/curl-ca-bundle.crt",
126 "/usr/lib/ssl/cert.pem",
130 if (g_getenv("SSL_CERT_FILE"))
131 return g_getenv("SSL_CERT_FILE");
133 for (i = 0; cert_files[i]; i++) {
134 if (is_file_exist(cert_files[i]))
135 return cert_files[i];
139 return get_cert_file();
143 const gchar *claws_ssl_get_cert_dir(void)
145 const char *cert_dirs[]={
146 "/etc/pki/tls/certs",
148 "/usr/share/ssl/certs",
150 "/usr/local/ssl/certs",
151 "/etc/apache/ssl.crt",
153 "/usr/lib/ssl/certs",
157 if (g_getenv("SSL_CERT_DIR"))
158 return g_getenv("SSL_CERT_DIR");
160 for (i = 0; cert_dirs[i]; i++) {
161 if (is_dir_exist(cert_dirs[i]))
172 #if GNUTLS_VERSION_NUMBER <= 0x020b00
173 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
176 mailstream_gnutls_init_not_required();
178 gnutls_global_init();
183 gnutls_global_deinit();
187 static void *SSL_connect_thread(void *data)
189 thread_data *td = (thread_data *)data;
192 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
193 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
196 result = gnutls_handshake(td->ssl);
197 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
199 td->done = TRUE; /* let the caller thread join() */
200 return GINT_TO_POINTER(result);
204 static gint SSL_connect_nb(gnutls_session_t ssl)
208 thread_data *td = g_new0(thread_data, 1);
212 time_t start_time = time(NULL);
213 gboolean killed = FALSE;
218 /* try to create a thread to initialize the SSL connection,
219 * fallback to blocking method in case of problem
221 if (pthread_attr_init(&pta) != 0 ||
222 pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
223 pthread_create(&pt, &pta, SSL_connect_thread, td) != 0) {
225 result = gnutls_handshake(td->ssl);
226 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
229 debug_print("waiting for SSL_connect thread...\n");
231 /* don't let the interface freeze while waiting */
233 if (time(NULL) - start_time > 30) {
240 /* get the thread's return value and clean its resources */
241 pthread_join(pt, &res);
245 res = GINT_TO_POINTER(-1);
247 debug_print("SSL_connect thread returned %d\n",
248 GPOINTER_TO_INT(res));
250 return GPOINTER_TO_INT(res);
251 #else /* USE_PTHREAD */
253 result = gnutls_handshake(ssl);
254 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
258 gboolean ssl_init_socket(SockInfo *sockinfo)
260 return ssl_init_socket_with_method(sockinfo, SSL_METHOD_SSLv23);
263 gnutls_x509_crt_t *ssl_get_certificate_chain(gnutls_session_t session, gint *list_len)
265 const gnutls_datum_t *raw_cert_list;
266 gnutls_x509_crt_t *certs = NULL;
267 gboolean result = TRUE;
273 raw_cert_list = gnutls_certificate_get_peers(session, list_len);
275 if (raw_cert_list && gnutls_certificate_type_get(session) == GNUTLS_CRT_X509) {
278 certs = g_malloc(sizeof(gnutls_x509_crt_t) * (*list_len));
280 for(i = 0 ; i < (*list_len) ; i++) {
283 gnutls_x509_crt_init(&certs[i]);
284 r = gnutls_x509_crt_import(certs[i], &raw_cert_list[i], GNUTLS_X509_FMT_DER);
286 g_warning("cert get failure: %d %s\n", r, gnutls_strerror(r));
295 gnutls_x509_crt_deinit(certs[i]);
307 gboolean ssl_init_socket_with_method(SockInfo *sockinfo, SSLMethod method)
309 gnutls_session_t session;
311 unsigned int cert_list_length;
312 gnutls_x509_crt_t *certs = NULL;
313 gnutls_certificate_credentials_t xcred;
315 if (gnutls_certificate_allocate_credentials (&xcred) != 0)
318 r = gnutls_init(&session, GNUTLS_CLIENT);
319 if (session == NULL || r != 0)
322 if (sockinfo->gnutls_priority && strlen(sockinfo->gnutls_priority)) {
323 r = gnutls_priority_set_direct(session, sockinfo->gnutls_priority, NULL);
324 debug_print("Setting GnuTLS priority to %s, status = %d\n",
325 sockinfo->gnutls_priority, r);
329 gnutls_priority_set_direct(session, "NORMAL:-VERS-TLS1.0:-VERS-TLS1.1:-VERS-TLS1.2", NULL);
331 gnutls_priority_set_direct(session, "NORMAL", NULL);
333 gnutls_record_disable_padding(session);
335 gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
337 if (claws_ssl_get_cert_file()) {
338 r = gnutls_certificate_set_x509_trust_file(xcred, claws_ssl_get_cert_file(), GNUTLS_X509_FMT_PEM);
340 g_warning("Can't read SSL_CERT_FILE %s: %s\n",
341 claws_ssl_get_cert_file(),
344 debug_print("Can't find SSL ca-certificates file\n");
346 gnutls_certificate_set_verify_flags (xcred, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
348 gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) GINT_TO_POINTER(sockinfo->sock));
349 gnutls_session_set_ptr(session, sockinfo);
350 #if GNUTLS_VERSION_NUMBER <= 0x020c00
351 gnutls_certificate_client_set_retrieve_function(xcred, gnutls_client_cert_cb);
353 gnutls_certificate_set_retrieve_function(xcred, gnutls_cert_cb);
356 gnutls_dh_set_prime_bits(session, 512);
358 if ((r = SSL_connect_nb(session)) < 0) {
359 g_warning("SSL connection failed (%s)", gnutls_strerror(r));
360 gnutls_certificate_free_credentials(xcred);
361 gnutls_deinit(session);
365 /* Get server's certificate (note: beware of dynamic allocation) */
366 certs = ssl_get_certificate_chain(session, &cert_list_length);
369 gnutls_certificate_free_credentials(xcred);
370 gnutls_deinit(session);
374 if (!ssl_certificate_check_chain(certs, cert_list_length, sockinfo->hostname, sockinfo->port,
375 sockinfo->ssl_cert_auto_accept)) {
376 for (i = 0; i < cert_list_length; i++)
377 gnutls_x509_crt_deinit(certs[i]);
379 gnutls_certificate_free_credentials(xcred);
380 gnutls_deinit(session);
384 for (i = 0; i < cert_list_length; i++)
385 gnutls_x509_crt_deinit(certs[i]);
388 sockinfo->ssl = session;
389 sockinfo->xcred = xcred;
393 void ssl_done_socket(SockInfo *sockinfo)
395 if (sockinfo && sockinfo->ssl) {
397 gnutls_certificate_free_credentials(sockinfo->xcred);
398 gnutls_deinit(sockinfo->ssl);
399 if (sockinfo->client_crt)
400 gnutls_x509_crt_deinit(sockinfo->client_crt);
401 if (sockinfo->client_key)
402 gnutls_x509_privkey_deinit(sockinfo->client_key);
403 sockinfo->client_key = NULL;
404 sockinfo->client_crt = NULL;
405 sockinfo->xcred = NULL;
406 sockinfo->ssl = NULL;
410 #endif /* USE_GNUTLS */