Sync from 0.9.12cvs55 HEAD (nonblocking SSL_connect)
[claws.git] / src / common / ssl.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #if USE_OPENSSL
25
26 #include "defs.h"
27
28 #include <glib.h>
29
30 #include "sylpheed.h"
31 #include "intl.h"
32 #include "utils.h"
33 #include "ssl.h"
34 #include "ssl_certificate.h"
35
36 #ifdef USE_PTHREAD
37 #include <pthread.h>
38 #endif
39
40 #ifdef USE_PTHREAD
41 typedef struct _thread_data {
42         SSL *ssl;
43         gboolean done;
44 } thread_data;
45 #endif
46
47
48 static SSL_CTX *ssl_ctx;
49
50 void ssl_init(void)
51 {
52         SSL_METHOD *meth;
53
54         /* Global system initialization*/
55         SSL_library_init();
56         SSL_load_error_strings();
57         
58         /* Create our context*/
59         meth = SSLv23_client_method();
60         ssl_ctx = SSL_CTX_new(meth);
61
62         /* Set default certificate paths */
63         SSL_CTX_set_default_verify_paths(ssl_ctx);
64         
65 #if (OPENSSL_VERSION_NUMBER < 0x0090600fL)
66         SSL_CTX_set_verify_depth(ssl_ctx,1);
67 #endif
68 }
69
70 void ssl_done(void)
71 {
72         if (!ssl_ctx)
73                 return;
74         
75         SSL_CTX_free(ssl_ctx);
76 }
77
78 #ifdef USE_PTHREAD
79 void *SSL_connect_thread(void *data)
80 {
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);
85 }
86 #endif
87
88 gint SSL_connect_nb(SSL *ssl)
89 {
90 #ifdef USE_PTHREAD
91         thread_data *td = g_new0(thread_data, 1);
92         pthread_t pt;
93         void *res = NULL;
94         
95         td->ssl  = ssl;
96         td->done = FALSE;
97         
98         /* try to create a thread to initialize the SSL connection,
99          * fallback to blocking method in case of problem 
100          */
101         if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE, 
102                         SSL_connect_thread, td) != 0)
103                 return SSL_connect(ssl);
104         
105         debug_print("waiting for SSL_connect thread...\n");
106         while(!td->done) {
107                 /* don't let the interface freeze while waiting */
108                 sylpheed_do_idle();
109         }
110
111         /* get the thread's return value and clean its resources */
112         pthread_join(pt, &res);
113         g_free(td);
114
115         debug_print("SSL_connect thread returned %d\n", 
116                         GPOINTER_TO_INT(res));
117         
118         return GPOINTER_TO_INT(res);
119 #else
120         return SSL_connect(ssl);
121 #endif
122 }
123
124 gboolean ssl_init_socket(SockInfo *sockinfo)
125 {
126         return ssl_init_socket_with_method(sockinfo, SSL_METHOD_SSLv23);
127 }
128
129 gboolean ssl_init_socket_with_method(SockInfo *sockinfo, SSLMethod method)
130 {
131         X509 *server_cert;
132         SSL *ssl;
133
134         ssl = SSL_new(ssl_ctx);
135         if (ssl == NULL) {
136                 g_warning(_("Error creating ssl context\n"));
137                 return FALSE;
138         }
139
140         switch (method) {
141         case SSL_METHOD_SSLv23:
142                 debug_print("Setting SSLv23 client method\n");
143                 SSL_set_ssl_method(ssl, SSLv23_client_method());
144                 break;
145         case SSL_METHOD_TLSv1:
146                 debug_print("Setting TLSv1 client method\n");
147                 SSL_set_ssl_method(ssl, TLSv1_client_method());
148                 break;
149         default:
150                 break;
151         }
152
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));
157                 SSL_free(ssl);
158                 return FALSE;
159         }
160
161         /* Get the cipher */
162
163         debug_print("SSL connection using %s\n", SSL_get_cipher(ssl));
164
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");
168                 SSL_free(ssl);
169                 return FALSE;
170         }
171
172
173         if (!ssl_certificate_check(server_cert, sockinfo->hostname, sockinfo->port)) {
174                 X509_free(server_cert);
175                 SSL_free(ssl);
176                 return FALSE;
177         }
178
179
180         X509_free(server_cert);
181         sockinfo->ssl = ssl;
182
183         return TRUE;
184 }
185
186 void ssl_done_socket(SockInfo *sockinfo)
187 {
188         if (sockinfo->ssl) {
189                 SSL_free(sockinfo->ssl);
190         }
191 }
192
193 #endif /* USE_OPENSSL */