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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include "ssl_certificate.h"
41 typedef struct _thread_data {
48 static SSL_CTX *ssl_ctx;
54 /* Global system initialization*/
56 SSL_load_error_strings();
58 /* Create our context*/
59 meth = SSLv23_client_method();
60 ssl_ctx = SSL_CTX_new(meth);
62 /* Set default certificate paths */
63 SSL_CTX_set_default_verify_paths(ssl_ctx);
65 #if (OPENSSL_VERSION_NUMBER < 0x0090600fL)
66 SSL_CTX_set_verify_depth(ssl_ctx,1);
75 SSL_CTX_free(ssl_ctx);
79 void *SSL_connect_thread(void *data)
81 thread_data *td = (thread_data *)data;
82 int result = SSL_connect(td->ssl);
83 td->done = TRUE; /* let the caller thread join() */
84 return GINT_TO_POINTER(result);
88 gint SSL_connect_nb(SSL *ssl)
91 thread_data *td = g_new0(thread_data, 1);
98 /* try to create a thread to initialize the SSL connection,
99 * fallback to blocking method in case of problem
101 if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
102 SSL_connect_thread, td) != 0)
103 return SSL_connect(ssl);
105 debug_print("waiting for SSL_connect thread...\n");
107 /* don't let the interface freeze while waiting */
111 /* get the thread's return value and clean its resources */
112 pthread_join(pt, &res);
115 debug_print("SSL_connect thread returned %d\n",
116 GPOINTER_TO_INT(res));
118 return GPOINTER_TO_INT(res);
120 return SSL_connect(ssl);
124 gboolean ssl_init_socket(SockInfo *sockinfo)
126 return ssl_init_socket_with_method(sockinfo, SSL_METHOD_SSLv23);
129 gboolean ssl_init_socket_with_method(SockInfo *sockinfo, SSLMethod method)
134 ssl = SSL_new(ssl_ctx);
136 g_warning(_("Error creating ssl context\n"));
141 case SSL_METHOD_SSLv23:
142 debug_print("Setting SSLv23 client method\n");
143 SSL_set_ssl_method(ssl, SSLv23_client_method());
145 case SSL_METHOD_TLSv1:
146 debug_print("Setting TLSv1 client method\n");
147 SSL_set_ssl_method(ssl, TLSv1_client_method());
153 SSL_set_fd(ssl, sockinfo->sock);
154 if (SSL_connect_nb(ssl) == -1) {
155 g_warning(_("SSL connect failed (%s)\n"),
156 ERR_error_string(ERR_get_error(), NULL));
163 debug_print("SSL connection using %s\n", SSL_get_cipher(ssl));
165 /* Get server's certificate (note: beware of dynamic allocation) */
166 if ((server_cert = SSL_get_peer_certificate(ssl)) == NULL) {
167 debug_print("server_cert is NULL ! this _should_not_ happen !\n");
173 if (!ssl_certificate_check(server_cert, sockinfo->hostname, sockinfo->port)) {
174 X509_free(server_cert);
180 X509_free(server_cert);
186 void ssl_done_socket(SockInfo *sockinfo)
189 SSL_free(sockinfo->ssl);
193 #endif /* USE_OPENSSL */