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 const char *cert_dirs[]={
231 "/etc/pki/tls/certs",
233 "/usr/share/ssl/certs",
235 "/usr/local/ssl/certs",
236 "/etc/apache/ssl.crt",
238 "/usr/lib/ssl/certs",
242 if (g_getenv("SSL_CERT_DIR"))
243 return g_getenv("SSL_CERT_DIR");
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);
297 time_t start_time = time(NULL);
298 gboolean killed = FALSE;
303 /* try to create a thread to initialize the SSL connection,
304 * fallback to blocking method in case of problem
306 if (pthread_attr_init(&pta) != 0 ||
307 pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
308 pthread_create(&pt, &pta, SSL_connect_thread, td) != 0) {
310 result = gnutls_handshake(td->ssl);
311 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
314 debug_print("waiting for SSL_connect thread...\n");
316 /* don't let the interface freeze while waiting */
318 if (time(NULL) - start_time > 30) {
325 /* get the thread's return value and clean its resources */
326 pthread_join(pt, &res);
330 res = GINT_TO_POINTER(-1);
332 debug_print("SSL_connect thread returned %d\n",
333 GPOINTER_TO_INT(res));
335 return GPOINTER_TO_INT(res);
336 #else /* USE_PTHREAD */
338 result = gnutls_handshake(ssl);
339 } while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
343 gnutls_x509_crt_t *ssl_get_certificate_chain(gnutls_session_t session, gint *list_len)
345 const gnutls_datum_t *raw_cert_list;
346 gnutls_x509_crt_t *certs = NULL;
347 gboolean result = TRUE;
353 raw_cert_list = gnutls_certificate_get_peers(session, list_len);
355 if (raw_cert_list && gnutls_certificate_type_get(session) == GNUTLS_CRT_X509) {
361 certs = g_malloc(sizeof(gnutls_x509_crt_t) * (*list_len));
363 for(i = 0 ; i < (*list_len) ; i++) {
366 gnutls_x509_crt_init(&certs[i]);
367 r = gnutls_x509_crt_import(certs[i], &raw_cert_list[i], GNUTLS_X509_FMT_DER);
369 g_warning("cert get failure: %d %s", r, gnutls_strerror(r));
378 gnutls_x509_crt_deinit(certs[i]);
390 gboolean ssl_init_socket(SockInfo *sockinfo)
392 gnutls_session_t session;
394 unsigned int cert_list_length;
395 gnutls_x509_crt_t *certs = NULL;
396 gnutls_certificate_credentials_t xcred;
398 if (gnutls_certificate_allocate_credentials (&xcred) != 0)
401 r = gnutls_init(&session, GNUTLS_CLIENT);
402 if (session == NULL || r != 0)
405 if (sockinfo->gnutls_priority && strlen(sockinfo->gnutls_priority)) {
406 r = gnutls_priority_set_direct(session, sockinfo->gnutls_priority, NULL);
407 debug_print("Setting GnuTLS priority to %s, status = %d\n",
408 sockinfo->gnutls_priority, r);
411 gnutls_priority_set_direct(session, DEFAULT_GNUTLS_PRIORITY, NULL);
414 gnutls_record_disable_padding(session);
416 gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
418 if (claws_ssl_get_cert_file()) {
419 r = gnutls_certificate_set_x509_trust_file(xcred, claws_ssl_get_cert_file(), GNUTLS_X509_FMT_PEM);
421 g_warning("Can't read SSL_CERT_FILE '%s': %s",
422 claws_ssl_get_cert_file(),
425 debug_print("Can't find SSL ca-certificates file\n");
427 gnutls_certificate_set_verify_flags (xcred, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
429 gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) GINT_TO_POINTER(sockinfo->sock));
431 gnutls_session_set_ptr(session, sockinfo);
433 #if GNUTLS_VERSION_NUMBER < 0x030000
434 # if GNUTLS_VERSION_NUMBER <= 0x020c00
435 gnutls_certificate_client_set_retrieve_function(xcred, gnutls_client_cert_cb);
437 gnutls_certificate_set_retrieve_function(xcred, gnutls_cert_cb);
440 debug_print("setting certificate callback function\n");
441 gnutls_certificate_set_retrieve_function2(xcred, gnutls_cert_cb);
444 #if GNUTLS_VERSION_NUMBER < 0x030107
445 /* Starting from GnuTLS 3.1.7, minimal size of the DH prime is
446 * set by the priority string. By default ("NORMAL"), it is 1008
447 * as of GnuTLS 3.3.0. */
448 gnutls_dh_set_prime_bits(session, 1008);
451 if ((r = SSL_connect_nb(session)) < 0) {
452 g_warning("SSL connection failed (%s)", gnutls_strerror(r));
453 gnutls_certificate_free_credentials(xcred);
454 gnutls_deinit(session);
458 /* Get server's certificate (note: beware of dynamic allocation) */
459 certs = ssl_get_certificate_chain(session, &cert_list_length);
462 gnutls_certificate_free_credentials(xcred);
463 gnutls_deinit(session);
467 if (!ssl_certificate_check_chain(certs, cert_list_length, sockinfo->hostname, sockinfo->port,
468 sockinfo->ssl_cert_auto_accept)) {
469 for (i = 0; i < cert_list_length; i++)
470 gnutls_x509_crt_deinit(certs[i]);
472 gnutls_certificate_free_credentials(xcred);
473 gnutls_deinit(session);
477 for (i = 0; i < cert_list_length; i++)
478 gnutls_x509_crt_deinit(certs[i]);
481 sockinfo->ssl = session;
482 sockinfo->xcred = xcred;
486 void ssl_done_socket(SockInfo *sockinfo)
488 if (sockinfo && sockinfo->ssl) {
490 gnutls_certificate_free_credentials(sockinfo->xcred);
491 gnutls_deinit(sockinfo->ssl);
492 #if GNUTLS_VERSION_NUMBER < 0x030000
493 if (sockinfo->client_crt)
494 gnutls_x509_crt_deinit(sockinfo->client_crt);
495 if (sockinfo->client_key)
496 gnutls_x509_privkey_deinit(sockinfo->client_key);
497 sockinfo->client_key = NULL;
498 sockinfo->client_crt = NULL;
500 gnutls_pcert_deinit(&sockinfo->client_crt);
501 gnutls_privkey_deinit(sockinfo->client_key);
503 sockinfo->client_key = NULL;
504 sockinfo->xcred = NULL;
505 sockinfo->ssl = NULL;
509 #endif /* USE_GNUTLS */