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 < 0x030400
61 #define DEFAULT_GNUTLS_PRIORITY "NORMAL:-VERS-SSL3.0"
63 #define DEFAULT_GNUTLS_PRIORITY "NORMAL"
66 #if GNUTLS_VERSION_NUMBER < 0x030000
67 /* GnuTLS 3.0 introduced new API for certificate callback,
68 * gnutls_certificate_set_retrieve_function2() */
70 #if GNUTLS_VERSION_NUMBER <= 0x020c00
71 static int gnutls_client_cert_cb(gnutls_session_t session,
72 const gnutls_datum_t *req_ca_rdn, int nreqs,
73 const gnutls_pk_algorithm_t *sign_algos,
74 int sign_algos_length, gnutls_retr_st *st)
76 static int gnutls_cert_cb(gnutls_session_t session,
77 const gnutls_datum_t *req_ca_rdn, int nreqs,
78 const gnutls_pk_algorithm_t *sign_algos,
79 int sign_algos_length, gnutls_retr2_st *st)
80 #endif /* GNUTLS_VERSION_NUMBER <= 0x020c00 */
82 SSLClientCertHookData hookdata;
83 SockInfo *sockinfo = (SockInfo *)gnutls_session_get_ptr(session);
84 gnutls_certificate_type_t type = gnutls_certificate_type_get(session);
85 gnutls_x509_crt_t crt;
86 gnutls_x509_privkey_t key;
90 hookdata.account = sockinfo->account;
91 hookdata.cert_path = NULL;
92 hookdata.password = NULL;
93 hookdata.is_smtp = sockinfo->is_smtp;
94 hooks_invoke(SSLCERT_GET_CLIENT_CERT_HOOKLIST, &hookdata);
96 if (hookdata.cert_path == NULL) {
97 g_free(hookdata.password);
101 sockinfo->client_crt = ssl_certificate_get_x509_from_pem_file(hookdata.cert_path);
102 sockinfo->client_key = ssl_certificate_get_pkey_from_pem_file(hookdata.cert_path);
103 if (!(sockinfo->client_crt && sockinfo->client_key)) {
104 /* try pkcs12 format */
105 ssl_certificate_get_x509_and_pkey_from_p12_file(hookdata.cert_path, hookdata.password,
107 sockinfo->client_crt = crt;
108 sockinfo->client_key = key;
111 if (type == GNUTLS_CRT_X509 && sockinfo->client_crt && sockinfo->client_key) {
113 #if GNUTLS_VERSION_NUMBER <= 0x020c00
118 st->cert.x509 = &(sockinfo->client_crt);
119 st->key.x509 = sockinfo->client_key;
121 g_free(hookdata.password);
124 g_free(hookdata.password);
128 #else /* GNUTLS_VERSION_NUMBER < 0x030000 */
130 static int gnutls_cert_cb(gnutls_session_t session,
131 const gnutls_datum_t *req_ca_rdn,
133 const gnutls_pk_algorithm_t *pk_algos,
135 gnutls_pcert_st **pcert,
136 unsigned int *pcert_length,
137 gnutls_privkey_t *privkey)
139 SSLClientCertHookData hookdata;
140 SockInfo *sockinfo = (SockInfo *)gnutls_session_get_ptr(session);
144 hookdata.account = sockinfo->account;
145 hookdata.cert_path = NULL;
146 hookdata.password = NULL;
147 hookdata.is_smtp = sockinfo->is_smtp;
148 hooks_invoke(SSLCERT_GET_CLIENT_CERT_HOOKLIST, &hookdata);
150 if (hookdata.cert_path == NULL) {
151 g_free(hookdata.password);
155 if ((r = gnutls_load_file(hookdata.cert_path, &tmp)) != 0) {
156 debug_print("couldn't load file '%s': %d\n",
157 hookdata.cert_path, r);
158 g_free(hookdata.password);
161 debug_print("trying to load client cert+key from file '%s'\n",
164 if ((r = gnutls_pcert_import_x509_raw(&sockinfo->client_crt, &tmp,
165 GNUTLS_X509_FMT_PEM, 0)) != 0) {
166 debug_print("couldn't import x509 cert from PEM file '%s': %d\n",
167 hookdata.cert_path, r);
168 g_free(hookdata.password);
171 debug_print("loaded client certificate...\n");
173 gnutls_privkey_init(&sockinfo->client_key);
174 if ((r = gnutls_privkey_import_x509_raw(sockinfo->client_key, &tmp,
175 GNUTLS_X509_FMT_PEM, hookdata.password, 0)) != 0) {
176 debug_print("couldn't import x509 pkey from PEM file '%s': %d\n",
177 hookdata.cert_path, r);
178 g_free(hookdata.password);
179 gnutls_privkey_deinit(sockinfo->client_key);
182 debug_print("loaded client private key...\n");
184 gnutls_free(tmp.data);
187 *pcert = &sockinfo->client_crt;
188 *privkey = sockinfo->client_key;
192 #endif /* GNUTLS_VERSION_NUMBER < 0x030000 */
194 const gchar *claws_ssl_get_cert_file(void)
197 const char *cert_files[]={
199 "/etc/pki/tls/certs/ca-bundle.crt",
200 "/etc/certs/ca-bundle.crt",
201 "/etc/ssl/ca-bundle.pem",
202 "/usr/share/ssl/certs/ca-bundle.crt",
203 "/etc/ssl/certs/ca-certificates.crt",
204 "/usr/local/ssl/certs/ca-bundle.crt",
205 "/etc/apache/ssl.crt/ca-bundle.crt",
206 "/usr/share/curl/curl-ca-bundle.crt",
207 "/usr/share/curl/curl-ca-bundle.crt",
208 "/usr/lib/ssl/cert.pem",
213 /* We honor this environment variable on all platforms. */
214 if (g_getenv("SSL_CERT_FILE"))
215 return g_getenv("SSL_CERT_FILE");
218 for (i = 0; cert_files[i]; i++) {
219 if (is_file_exist(cert_files[i]))
220 return cert_files[i];
224 return w32_get_cert_file();
228 const gchar *claws_ssl_get_cert_dir(void)
230 if (g_getenv("SSL_CERT_DIR"))
231 return g_getenv("SSL_CERT_DIR");
233 const char *cert_dirs[]={
234 "/etc/pki/tls/certs",
236 "/usr/share/ssl/certs",
238 "/usr/local/ssl/certs",
239 "/etc/apache/ssl.crt",
241 "/usr/lib/ssl/certs",
245 for (i = 0; cert_dirs[i]; i++) {
246 if (is_dir_exist(cert_dirs[i]))
257 #if GNUTLS_VERSION_NUMBER <= 0x020b00
258 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
261 mailstream_gnutls_init_not_required();
263 gnutls_global_init();
268 gnutls_global_deinit();
272 static void *SSL_connect_thread(void *data)
274 thread_data *td = (thread_data *)data;
277 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
278 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
281 result = gnutls_handshake(td->ssl);
282 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
284 td->done = TRUE; /* let the caller thread join() */
285 return GINT_TO_POINTER(result);
289 static gint SSL_connect_nb(gnutls_session_t ssl)
293 thread_data *td = g_new0(thread_data, 1);
296 time_t start_time = time(NULL);
297 gboolean killed = FALSE;
302 /* try to create a thread to initialize the SSL connection,
303 * fallback to blocking method in case of problem
305 if (pthread_create(&pt, NULL, SSL_connect_thread, td) != 0) {
307 result = gnutls_handshake(td->ssl);
308 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
311 debug_print("waiting for SSL_connect thread...\n");
313 /* don't let the interface freeze while waiting */
315 if (time(NULL) - start_time > 30) {
322 /* get the thread's return value and clean its resources */
323 pthread_join(pt, &res);
327 res = GINT_TO_POINTER(-1);
329 debug_print("SSL_connect thread returned %d\n",
330 GPOINTER_TO_INT(res));
332 return GPOINTER_TO_INT(res);
333 #else /* USE_PTHREAD */
335 result = gnutls_handshake(ssl);
336 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
340 gnutls_x509_crt_t *ssl_get_certificate_chain(gnutls_session_t session, gint *list_len)
342 const gnutls_datum_t *raw_cert_list;
343 gnutls_x509_crt_t *certs = NULL;
344 gboolean result = TRUE;
350 raw_cert_list = gnutls_certificate_get_peers(session, list_len);
352 if (raw_cert_list && gnutls_certificate_type_get(session) == GNUTLS_CRT_X509) {
358 certs = g_malloc(sizeof(gnutls_x509_crt_t) * (*list_len));
360 for(i = 0 ; i < (*list_len) ; i++) {
363 gnutls_x509_crt_init(&certs[i]);
364 r = gnutls_x509_crt_import(certs[i], &raw_cert_list[i], GNUTLS_X509_FMT_DER);
366 g_warning("cert get failure: %d %s", r, gnutls_strerror(r));
375 gnutls_x509_crt_deinit(certs[i]);
387 gboolean ssl_init_socket(SockInfo *sockinfo)
389 gnutls_session_t session;
391 unsigned int cert_list_length;
392 gnutls_x509_crt_t *certs = NULL;
393 gnutls_certificate_credentials_t xcred;
395 if (gnutls_certificate_allocate_credentials (&xcred) != 0)
398 r = gnutls_init(&session, GNUTLS_CLIENT);
399 if (session == NULL || r != 0)
402 if (sockinfo->gnutls_priority && strlen(sockinfo->gnutls_priority)) {
403 r = gnutls_priority_set_direct(session, sockinfo->gnutls_priority, NULL);
404 debug_print("Setting GnuTLS priority to %s, status = %d\n",
405 sockinfo->gnutls_priority, r);
408 gnutls_priority_set_direct(session, DEFAULT_GNUTLS_PRIORITY, NULL);
411 gnutls_record_disable_padding(session);
413 gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
415 if (claws_ssl_get_cert_file()) {
416 r = gnutls_certificate_set_x509_trust_file(xcred, claws_ssl_get_cert_file(), GNUTLS_X509_FMT_PEM);
418 g_warning("Can't read SSL_CERT_FILE '%s': %s",
419 claws_ssl_get_cert_file(),
422 debug_print("Can't find SSL ca-certificates file\n");
424 gnutls_certificate_set_verify_flags (xcred, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
426 gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) GINT_TO_POINTER(sockinfo->sock));
428 gnutls_session_set_ptr(session, sockinfo);
430 #if GNUTLS_VERSION_NUMBER < 0x030000
431 # if GNUTLS_VERSION_NUMBER <= 0x020c00
432 gnutls_certificate_client_set_retrieve_function(xcred, gnutls_client_cert_cb);
434 gnutls_certificate_set_retrieve_function(xcred, gnutls_cert_cb);
437 debug_print("setting certificate callback function\n");
438 gnutls_certificate_set_retrieve_function2(xcred, gnutls_cert_cb);
441 #if GNUTLS_VERSION_NUMBER < 0x030107
442 /* Starting from GnuTLS 3.1.7, minimal size of the DH prime is
443 * set by the priority string. By default ("NORMAL"), it is 1008
444 * as of GnuTLS 3.3.0. */
445 gnutls_dh_set_prime_bits(session, 1008);
448 if ((r = SSL_connect_nb(session)) < 0) {
449 g_warning("SSL/TLS connection failed (%s)", gnutls_strerror(r));
450 gnutls_certificate_free_credentials(xcred);
451 gnutls_deinit(session);
455 /* Get server's certificate (note: beware of dynamic allocation) */
456 certs = ssl_get_certificate_chain(session, &cert_list_length);
459 gnutls_certificate_free_credentials(xcred);
460 gnutls_deinit(session);
464 if (!ssl_certificate_check_chain(certs, cert_list_length, sockinfo->hostname, sockinfo->port,
465 sockinfo->ssl_cert_auto_accept)) {
466 for (i = 0; i < cert_list_length; i++)
467 gnutls_x509_crt_deinit(certs[i]);
469 gnutls_certificate_free_credentials(xcred);
470 gnutls_deinit(session);
474 for (i = 0; i < cert_list_length; i++)
475 gnutls_x509_crt_deinit(certs[i]);
478 sockinfo->ssl = session;
479 sockinfo->xcred = xcred;
483 void ssl_done_socket(SockInfo *sockinfo)
485 if (sockinfo && sockinfo->ssl) {
487 gnutls_certificate_free_credentials(sockinfo->xcred);
488 gnutls_deinit(sockinfo->ssl);
489 #if GNUTLS_VERSION_NUMBER < 0x030000
490 if (sockinfo->client_crt)
491 gnutls_x509_crt_deinit(sockinfo->client_crt);
492 if (sockinfo->client_key)
493 gnutls_x509_privkey_deinit(sockinfo->client_key);
494 sockinfo->client_key = NULL;
495 sockinfo->client_crt = NULL;
497 gnutls_pcert_deinit(&sockinfo->client_crt);
498 gnutls_privkey_deinit(sockinfo->client_key);
500 sockinfo->client_key = NULL;
501 sockinfo->xcred = NULL;
502 sockinfo->ssl = NULL;
506 #endif /* USE_GNUTLS */