2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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_ssl_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 void *SSL_connect_thread(void *data)
89 thread_data *td = (thread_data *)data;
90 int result = SSL_connect(td->ssl);
91 td->done = TRUE; /* let the caller thread join() */
92 return GINT_TO_POINTER(result);
96 gint SSL_connect_nb(SSL *ssl)
98 #if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
99 thread_data *td = g_new0(thread_data, 1);
106 /* try to create a thread to initialize the SSL connection,
107 * fallback to blocking method in case of problem
109 if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
110 SSL_connect_thread, td) != 0)
111 return SSL_connect(ssl);
113 debug_print("waiting for SSL_connect thread...\n");
115 /* don't let the interface freeze while waiting */
119 /* get the thread's return value and clean its resources */
120 pthread_join(pt, &res);
123 debug_print("SSL_connect thread returned %d\n",
124 GPOINTER_TO_INT(res));
126 return GPOINTER_TO_INT(res);
128 return SSL_connect(ssl);
132 gboolean ssl_init_socket(SockInfo *sockinfo)
134 return ssl_init_socket_with_method(sockinfo, SSL_METHOD_SSLv23);
137 gboolean ssl_init_socket_with_method(SockInfo *sockinfo, SSLMethod method)
142 ssl = SSL_new(ssl_ctx);
144 g_warning(_("Error creating ssl context\n"));
149 case SSL_METHOD_SSLv23:
150 debug_print("Setting SSLv23 client method\n");
151 SSL_set_ssl_method(ssl, SSLv23_client_method());
153 case SSL_METHOD_TLSv1:
154 debug_print("Setting TLSv1 client method\n");
155 SSL_set_ssl_method(ssl, TLSv1_client_method());
161 SSL_set_fd(ssl, sockinfo->sock);
162 if (SSL_connect_nb(ssl) == -1) {
163 g_warning(_("SSL connect failed (%s)\n"),
164 ERR_error_string(ERR_get_error(), NULL));
171 debug_print("SSL connection using %s\n", SSL_get_cipher(ssl));
173 /* Get server's certificate (note: beware of dynamic allocation) */
174 if ((server_cert = SSL_get_peer_certificate(ssl)) == NULL) {
175 debug_print("server_cert is NULL ! this _should_not_ happen !\n");
181 if (!ssl_certificate_check(server_cert, sockinfo->hostname, sockinfo->port)) {
182 X509_free(server_cert);
188 X509_free(server_cert);
194 void ssl_done_socket(SockInfo *sockinfo)
197 SSL_free(sockinfo->ssl);
201 #endif /* USE_OPENSSL */