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>
34 #if GNUTLS_VERSION_NUMBER <= 0x020b00
36 GCRY_THREAD_OPTION_PTHREAD_IMPL;
42 #include "ssl_certificate.h"
46 #include <libetpan/mailstream_ssl.h>
54 typedef struct _thread_data {
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)
65 SSLClientCertHookData hookdata;
66 SockInfo *sockinfo = (SockInfo *)gnutls_session_get_ptr(session);
67 gnutls_certificate_type type = gnutls_certificate_type_get(session);
69 gnutls_x509_privkey key;
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);
79 if (hookdata.cert_path == NULL)
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,
88 sockinfo->client_crt = crt;
89 sockinfo->client_key = key;
92 if (type == GNUTLS_CRT_X509 && sockinfo->client_crt && sockinfo->client_key) {
95 st->cert.x509 = &(sockinfo->client_crt);
96 st->key.x509 = sockinfo->client_key;
103 const gchar *claws_ssl_get_cert_file(void)
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",
118 if (g_getenv("SSL_CERT_FILE"))
119 return g_getenv("SSL_CERT_FILE");
121 for (i = 0; cert_files[i]; i++) {
122 if (is_file_exist(cert_files[i]))
123 return cert_files[i];
127 return get_cert_file();
131 const gchar *claws_ssl_get_cert_dir(void)
133 const char *cert_dirs[]={
134 "/etc/pki/tls/certs",
136 "/usr/share/ssl/certs",
138 "/usr/local/ssl/certs",
139 "/etc/apache/ssl.crt",
141 "/usr/lib/ssl/certs",
145 if (g_getenv("SSL_CERT_DIR"))
146 return g_getenv("SSL_CERT_DIR");
148 for (i = 0; cert_dirs[i]; i++) {
149 if (is_dir_exist(cert_dirs[i]))
154 return "put_what_s_needed_here";
160 #if GNUTLS_VERSION_NUMBER <= 0x020b00
161 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
164 mailstream_gnutls_init_not_required();
166 gnutls_global_init();
171 gnutls_global_deinit();
175 static void *SSL_connect_thread(void *data)
177 thread_data *td = (thread_data *)data;
180 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
181 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
184 result = gnutls_handshake(td->ssl);
185 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
187 td->done = TRUE; /* let the caller thread join() */
188 return GINT_TO_POINTER(result);
192 static gint SSL_connect_nb(gnutls_session ssl)
196 thread_data *td = g_new0(thread_data, 1);
200 time_t start_time = time(NULL);
201 gboolean killed = FALSE;
206 /* try to create a thread to initialize the SSL connection,
207 * fallback to blocking method in case of problem
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) {
213 result = gnutls_handshake(td->ssl);
214 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
217 debug_print("waiting for SSL_connect thread...\n");
219 /* don't let the interface freeze while waiting */
221 if (time(NULL) - start_time > 30) {
228 /* get the thread's return value and clean its resources */
229 pthread_join(pt, &res);
233 res = GINT_TO_POINTER(-1);
235 debug_print("SSL_connect thread returned %d\n",
236 GPOINTER_TO_INT(res));
238 return GPOINTER_TO_INT(res);
239 #else /* USE_PTHREAD */
241 result = gnutls_handshake(ssl);
242 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
246 gboolean ssl_init_socket(SockInfo *sockinfo)
248 return ssl_init_socket_with_method(sockinfo, SSL_METHOD_SSLv23);
251 gboolean ssl_init_socket_with_method(SockInfo *sockinfo, SSLMethod method)
253 gnutls_session session;
255 const gnutls_datum *raw_cert_list;
256 unsigned int raw_cert_list_length;
257 gnutls_x509_crt cert = NULL;
259 gnutls_certificate_credentials_t xcred;
261 if (gnutls_certificate_allocate_credentials (&xcred) != 0)
264 r = gnutls_init(&session, GNUTLS_CLIENT);
265 if (session == NULL || r != 0)
268 #if GNUTLS_VERSION_NUMBER < 0x030003
269 gnutls_transport_set_lowat (session, 0);
272 gnutls_priority_set_direct(session, "NORMAL:-VERS-TLS1.0:-VERS-TLS1.1:-VERS-TLS1.2", NULL);
274 gnutls_priority_set_direct(session, "NORMAL", NULL);
275 gnutls_record_disable_padding(session);
277 gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
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);
282 g_warning("Can't read SSL_CERT_FILE %s: %s\n",
283 claws_ssl_get_cert_file(),
286 debug_print("Can't find SSL ca-certificates file\n");
288 gnutls_certificate_set_verify_flags (xcred, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
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);
294 gnutls_dh_set_prime_bits(session, 512);
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);
303 /* Get server's certificate (note: beware of dynamic allocation) */
304 raw_cert_list = gnutls_certificate_get_peers(session, &raw_cert_list_length);
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);
316 r = gnutls_certificate_verify_peers2(session, &status);
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);
325 gnutls_x509_crt_deinit(cert);
327 sockinfo->ssl = session;
328 sockinfo->xcred = xcred;
332 void ssl_done_socket(SockInfo *sockinfo)
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;
347 #endif /* USE_GNUTLS */