2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 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>
34 #include "ssl_certificate.h"
37 #include <libetpan/mailstream_ssl.h>
45 typedef struct _thread_data {
52 static SSL_CTX *ssl_ctx;
58 /* Global system initialization*/
60 SSL_load_error_strings();
63 mailstream_openssl_init_not_required();
66 /* Create our context*/
67 meth = SSLv23_client_method();
68 ssl_ctx = SSL_CTX_new(meth);
70 /* Set default certificate paths */
71 SSL_CTX_set_default_verify_paths(ssl_ctx);
73 #if (OPENSSL_VERSION_NUMBER < 0x0090600fL)
74 SSL_CTX_set_verify_depth(ssl_ctx,1);
83 SSL_CTX_free(ssl_ctx);
87 static void *SSL_connect_thread(void *data)
89 thread_data *td = (thread_data *)data;
92 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
93 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
95 result = SSL_connect(td->ssl);
96 td->done = TRUE; /* let the caller thread join() */
97 return GINT_TO_POINTER(result);
101 static gint SSL_connect_nb(SSL *ssl)
103 #if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
104 thread_data *td = g_new0(thread_data, 1);
107 time_t start_time = time(NULL);
108 gboolean killed = FALSE;
113 /* try to create a thread to initialize the SSL connection,
114 * fallback to blocking method in case of problem
116 if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
117 SSL_connect_thread, td) != 0)
118 return SSL_connect(ssl);
120 debug_print("waiting for SSL_connect thread...\n");
122 /* don't let the interface freeze while waiting */
124 if (time(NULL) - start_time > 30) {
131 /* get the thread's return value and clean its resources */
132 pthread_join(pt, &res);
136 res = GINT_TO_POINTER(-1);
138 debug_print("SSL_connect thread returned %d\n",
139 GPOINTER_TO_INT(res));
141 return GPOINTER_TO_INT(res);
143 return SSL_connect(ssl);
147 gboolean ssl_init_socket(SockInfo *sockinfo)
149 return ssl_init_socket_with_method(sockinfo, SSL_METHOD_SSLv23);
152 gboolean ssl_init_socket_with_method(SockInfo *sockinfo, SSLMethod method)
157 ssl = SSL_new(ssl_ctx);
159 g_warning(_("Error creating ssl context\n"));
164 case SSL_METHOD_SSLv23:
165 debug_print("Setting SSLv23 client method\n");
166 SSL_set_ssl_method(ssl, SSLv23_client_method());
168 case SSL_METHOD_TLSv1:
169 debug_print("Setting TLSv1 client method\n");
170 SSL_set_ssl_method(ssl, TLSv1_client_method());
176 SSL_set_fd(ssl, sockinfo->sock);
177 if (SSL_connect_nb(ssl) == -1) {
178 g_warning(_("SSL connect failed (%s)\n"),
179 ERR_error_string(ERR_get_error(), NULL));
186 debug_print("SSL connection using %s\n", SSL_get_cipher(ssl));
188 /* Get server's certificate (note: beware of dynamic allocation) */
189 if ((server_cert = SSL_get_peer_certificate(ssl)) == NULL) {
190 debug_print("server_cert is NULL ! this _should_not_ happen !\n");
196 if (!ssl_certificate_check(server_cert, sockinfo->canonical_name, sockinfo->hostname, sockinfo->port)) {
197 X509_free(server_cert);
203 X509_free(server_cert);
209 void ssl_done_socket(SockInfo *sockinfo)
211 if (sockinfo && sockinfo->ssl) {
212 SSL_free(sockinfo->ssl);
213 sockinfo->ssl = NULL;
217 #endif /* USE_OPENSSL */