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 #if GNUTLS_VERSION_NUMBER <= 0x020b00
35 GCRY_THREAD_OPTION_PTHREAD_IMPL;
41 #include "ssl_certificate.h"
45 #include <libetpan/mailstream_ssl.h>
53 typedef struct _thread_data {
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)
64 SSLClientCertHookData hookdata;
65 SockInfo *sockinfo = (SockInfo *)gnutls_session_get_ptr(session);
66 gnutls_certificate_type type = gnutls_certificate_type_get(session);
68 gnutls_x509_privkey key;
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);
78 if (hookdata.cert_path == NULL)
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,
87 sockinfo->client_crt = crt;
88 sockinfo->client_key = key;
91 if (type == GNUTLS_CRT_X509 && sockinfo->client_crt && sockinfo->client_key) {
94 st->cert.x509 = &(sockinfo->client_crt);
95 st->key.x509 = sockinfo->client_key;
102 const gchar *claws_ssl_get_cert_file(void)
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",
117 if (g_getenv("SSL_CERT_FILE"))
118 return g_getenv("SSL_CERT_FILE");
120 for (i = 0; cert_files[i]; i++) {
121 if (is_file_exist(cert_files[i]))
122 return cert_files[i];
126 return get_cert_file();
130 const gchar *claws_ssl_get_cert_dir(void)
132 const char *cert_dirs[]={
133 "/etc/pki/tls/certs",
135 "/usr/share/ssl/certs",
137 "/usr/local/ssl/certs",
138 "/etc/apache/ssl.crt",
140 "/usr/lib/ssl/certs",
144 if (g_getenv("SSL_CERT_DIR"))
145 return g_getenv("SSL_CERT_DIR");
147 for (i = 0; cert_dirs[i]; i++) {
148 if (is_dir_exist(cert_dirs[i]))
153 return "put_what_s_needed_here";
159 #if GNUTLS_VERSION_NUMBER <= 0x020b00
160 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
163 mailstream_gnutls_init_not_required();
165 gnutls_global_init();
170 gnutls_global_deinit();
174 static void *SSL_connect_thread(void *data)
176 thread_data *td = (thread_data *)data;
179 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
180 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
183 result = gnutls_handshake(td->ssl);
184 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
186 td->done = TRUE; /* let the caller thread join() */
187 return GINT_TO_POINTER(result);
191 static gint SSL_connect_nb(gnutls_session ssl)
195 thread_data *td = g_new0(thread_data, 1);
199 time_t start_time = time(NULL);
200 gboolean killed = FALSE;
205 /* try to create a thread to initialize the SSL connection,
206 * fallback to blocking method in case of problem
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) {
212 result = gnutls_handshake(td->ssl);
213 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
216 debug_print("waiting for SSL_connect thread...\n");
218 /* don't let the interface freeze while waiting */
220 if (time(NULL) - start_time > 30) {
227 /* get the thread's return value and clean its resources */
228 pthread_join(pt, &res);
232 res = GINT_TO_POINTER(-1);
234 debug_print("SSL_connect thread returned %d\n",
235 GPOINTER_TO_INT(res));
237 return GPOINTER_TO_INT(res);
238 #else /* USE_PTHREAD */
240 result = gnutls_handshake(ssl);
241 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
245 gboolean ssl_init_socket(SockInfo *sockinfo)
247 return ssl_init_socket_with_method(sockinfo, SSL_METHOD_SSLv23);
250 gboolean ssl_init_socket_with_method(SockInfo *sockinfo, SSLMethod method)
252 gnutls_session session;
254 const gnutls_datum *raw_cert_list;
255 unsigned int raw_cert_list_length;
256 gnutls_x509_crt cert = NULL;
258 gnutls_certificate_credentials_t xcred;
260 if (gnutls_certificate_allocate_credentials (&xcred) != 0)
263 r = gnutls_init(&session, GNUTLS_CLIENT);
264 if (session == NULL || r != 0)
267 #if GNUTLS_VERSION_NUMBER < 0x030003
268 gnutls_transport_set_lowat (session, 0);
270 gnutls_priority_set_direct(session, "NORMAL", NULL);
271 gnutls_record_disable_padding(session);
273 gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
275 if (claws_ssl_get_cert_file()) {
276 r = gnutls_certificate_set_x509_trust_file(xcred, claws_ssl_get_cert_file(), GNUTLS_X509_FMT_PEM);
278 g_warning("Can't read SSL_CERT_FILE %s: %s\n",
279 claws_ssl_get_cert_file(),
282 debug_print("Can't find SSL ca-certificates file\n");
284 gnutls_certificate_set_verify_flags (xcred, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
286 gnutls_transport_set_ptr(session, (gnutls_transport_ptr) sockinfo->sock);
287 gnutls_session_set_ptr(session, sockinfo);
288 gnutls_certificate_client_set_retrieve_function(xcred, gnutls_client_cert_cb);
290 gnutls_dh_set_prime_bits(session, 512);
292 if ((r = SSL_connect_nb(session)) < 0) {
293 g_warning("SSL connection failed (%s)", gnutls_strerror(r));
294 gnutls_certificate_free_credentials(xcred);
295 gnutls_deinit(session);
299 /* Get server's certificate (note: beware of dynamic allocation) */
300 raw_cert_list = gnutls_certificate_get_peers(session, &raw_cert_list_length);
303 || gnutls_certificate_type_get(session) != GNUTLS_CRT_X509
304 || (r = gnutls_x509_crt_init(&cert)) < 0
305 || (r = gnutls_x509_crt_import(cert, &raw_cert_list[0], GNUTLS_X509_FMT_DER)) < 0) {
306 g_warning("cert get failure: %d %s\n", r, gnutls_strerror(r));
307 gnutls_certificate_free_credentials(xcred);
308 gnutls_deinit(session);
312 r = gnutls_certificate_verify_peers2(session, &status);
314 if (!ssl_certificate_check(cert, status, sockinfo->hostname, sockinfo->port)) {
315 gnutls_x509_crt_deinit(cert);
316 gnutls_certificate_free_credentials(xcred);
317 gnutls_deinit(session);
321 gnutls_x509_crt_deinit(cert);
323 sockinfo->ssl = session;
324 sockinfo->xcred = xcred;
328 void ssl_done_socket(SockInfo *sockinfo)
330 if (sockinfo && sockinfo->ssl) {
331 gnutls_certificate_free_credentials(sockinfo->xcred);
332 gnutls_deinit(sockinfo->ssl);
333 if (sockinfo->client_crt)
334 gnutls_x509_crt_deinit(sockinfo->client_crt);
335 if (sockinfo->client_key)
336 gnutls_x509_privkey_deinit(sockinfo->client_key);
337 sockinfo->client_key = NULL;
338 sockinfo->client_crt = NULL;
339 sockinfo->ssl = NULL;
343 #endif /* USE_GNUTLS */