2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2011 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/>.
29 #include <glib/gi18n.h>
33 GCRY_THREAD_OPTION_PTHREAD_IMPL;
38 #include "ssl_certificate.h"
42 #include <libetpan/mailstream_ssl.h>
50 typedef struct _thread_data {
56 static int gnutls_client_cert_cb(gnutls_session session,
57 const gnutls_datum *req_ca_rdn, int nreqs,
58 const gnutls_pk_algorithm *sign_algos,
59 int sign_algos_length, gnutls_retr_st *st)
61 SSLClientCertHookData hookdata;
62 SockInfo *sockinfo = (SockInfo *)gnutls_session_get_ptr(session);
63 gnutls_certificate_type type = gnutls_certificate_type_get(session);
65 gnutls_x509_privkey key;
69 hookdata.account = sockinfo->account;
70 hookdata.cert_path = NULL;
71 hookdata.password = NULL;
72 hookdata.is_smtp = sockinfo->is_smtp;
73 hooks_invoke(SSLCERT_GET_CLIENT_CERT_HOOKLIST, &hookdata);
75 if (hookdata.cert_path == NULL)
78 sockinfo->client_crt = ssl_certificate_get_x509_from_pem_file(hookdata.cert_path);
79 sockinfo->client_key = ssl_certificate_get_pkey_from_pem_file(hookdata.cert_path);
80 if (!(sockinfo->client_crt && sockinfo->client_key)) {
81 /* try pkcs12 format */
82 ssl_certificate_get_x509_and_pkey_from_p12_file(hookdata.cert_path, hookdata.password,
84 sockinfo->client_crt = crt;
85 sockinfo->client_key = key;
88 if (type == GNUTLS_CRT_X509 && sockinfo->client_crt && sockinfo->client_key) {
91 st->cert.x509 = &(sockinfo->client_crt);
92 st->key.x509 = sockinfo->client_key;
99 const gchar *claws_ssl_get_cert_file(void)
101 const char *cert_files[]={
102 "/etc/pki/tls/certs/ca-bundle.crt",
103 "/etc/certs/ca-bundle.crt",
104 "/usr/share/ssl/certs/ca-bundle.crt",
105 "/etc/ssl/certs/ca-certificates.crt",
106 "/usr/local/ssl/certs/ca-bundle.crt",
107 "/etc/apache/ssl.crt/ca-bundle.crt",
108 "/usr/share/curl/curl-ca-bundle.crt",
109 "/usr/share/curl/curl-ca-bundle.crt",
110 "/usr/lib/ssl/cert.pem",
114 if (g_getenv("SSL_CERT_FILE"))
115 return g_getenv("SSL_CERT_FILE");
117 for (i = 0; cert_files[i]; i++) {
118 if (is_file_exist(cert_files[i]))
119 return cert_files[i];
123 return get_cert_file();
127 const gchar *claws_ssl_get_cert_dir(void)
129 const char *cert_dirs[]={
130 "/etc/pki/tls/certs",
132 "/usr/share/ssl/certs",
134 "/usr/local/ssl/certs",
135 "/etc/apache/ssl.crt",
137 "/usr/lib/ssl/certs",
141 if (g_getenv("SSL_CERT_DIR"))
142 return g_getenv("SSL_CERT_DIR");
144 for (i = 0; cert_dirs[i]; i++) {
145 if (is_dir_exist(cert_dirs[i]))
150 return "put_what_s_needed_here";
156 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
158 mailstream_gnutls_init_not_required();
160 gnutls_global_init();
165 gnutls_global_deinit();
169 static void *SSL_connect_thread(void *data)
171 thread_data *td = (thread_data *)data;
174 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
175 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
178 result = gnutls_handshake(td->ssl);
179 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
181 td->done = TRUE; /* let the caller thread join() */
182 return GINT_TO_POINTER(result);
186 static gint SSL_connect_nb(gnutls_session ssl)
190 thread_data *td = g_new0(thread_data, 1);
194 time_t start_time = time(NULL);
195 gboolean killed = FALSE;
200 /* try to create a thread to initialize the SSL connection,
201 * fallback to blocking method in case of problem
203 if (pthread_attr_init(&pta) != 0 ||
204 pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
205 pthread_create(&pt, &pta, SSL_connect_thread, td) != 0) {
207 result = gnutls_handshake(td->ssl);
208 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
211 debug_print("waiting for SSL_connect thread...\n");
213 /* don't let the interface freeze while waiting */
215 if (time(NULL) - start_time > 30) {
222 /* get the thread's return value and clean its resources */
223 pthread_join(pt, &res);
227 res = GINT_TO_POINTER(-1);
229 debug_print("SSL_connect thread returned %d\n",
230 GPOINTER_TO_INT(res));
232 return GPOINTER_TO_INT(res);
233 #else /* USE_PTHREAD */
235 result = gnutls_handshake(ssl);
236 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
240 gboolean ssl_init_socket(SockInfo *sockinfo)
242 return ssl_init_socket_with_method(sockinfo, SSL_METHOD_SSLv23);
245 gboolean ssl_init_socket_with_method(SockInfo *sockinfo, SSLMethod method)
247 gnutls_session session;
249 const gnutls_datum *raw_cert_list;
250 unsigned int raw_cert_list_length;
251 gnutls_x509_crt cert = NULL;
253 gnutls_certificate_credentials_t xcred;
255 if (gnutls_certificate_allocate_credentials (&xcred) != 0)
258 r = gnutls_init(&session, GNUTLS_CLIENT);
259 if (session == NULL || r != 0)
262 gnutls_transport_set_lowat (session, 0);
263 gnutls_set_default_priority(session);
265 gnutls_priority_set_direct(session, "EXPORT", NULL);
266 gnutls_record_disable_padding(session);
268 gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
270 if (claws_ssl_get_cert_file()) {
271 r = gnutls_certificate_set_x509_trust_file(xcred, claws_ssl_get_cert_file(), GNUTLS_X509_FMT_PEM);
273 g_warning("Can't read SSL_CERT_FILE %s: %s\n",
274 claws_ssl_get_cert_file(),
277 debug_print("Can't find SSL ca-certificates file\n");
279 gnutls_certificate_set_verify_flags (xcred, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
281 gnutls_transport_set_ptr(session, (gnutls_transport_ptr) sockinfo->sock);
282 gnutls_session_set_ptr(session, sockinfo);
283 gnutls_certificate_client_set_retrieve_function(xcred, gnutls_client_cert_cb);
285 gnutls_dh_set_prime_bits(session, 512);
287 if ((r = SSL_connect_nb(session)) < 0) {
288 g_warning("SSL connection failed (%s)", gnutls_strerror(r));
289 gnutls_certificate_free_credentials(xcred);
290 gnutls_deinit(session);
294 /* Get server's certificate (note: beware of dynamic allocation) */
295 raw_cert_list = gnutls_certificate_get_peers(session, &raw_cert_list_length);
298 || gnutls_certificate_type_get(session) != GNUTLS_CRT_X509
299 || (r = gnutls_x509_crt_init(&cert)) < 0
300 || (r = gnutls_x509_crt_import(cert, &raw_cert_list[0], GNUTLS_X509_FMT_DER)) < 0) {
301 g_warning("cert get failure: %d %s\n", r, gnutls_strerror(r));
302 gnutls_certificate_free_credentials(xcred);
303 gnutls_deinit(session);
307 r = gnutls_certificate_verify_peers2(session, &status);
309 if (!ssl_certificate_check(cert, status, sockinfo->hostname, sockinfo->port)) {
310 gnutls_x509_crt_deinit(cert);
311 gnutls_certificate_free_credentials(xcred);
312 gnutls_deinit(session);
316 gnutls_x509_crt_deinit(cert);
318 sockinfo->ssl = session;
319 sockinfo->xcred = xcred;
323 void ssl_done_socket(SockInfo *sockinfo)
325 if (sockinfo && sockinfo->ssl) {
326 gnutls_certificate_free_credentials(sockinfo->xcred);
327 gnutls_deinit(sockinfo->ssl);
328 if (sockinfo->client_crt)
329 gnutls_x509_crt_deinit(sockinfo->client_crt);
330 if (sockinfo->client_key)
331 gnutls_x509_privkey_deinit(sockinfo->client_key);
332 sockinfo->client_key = NULL;
333 sockinfo->client_crt = NULL;
334 sockinfo->ssl = NULL;
338 #endif /* USE_GNUTLS */