zephyr: Remove unused defines and headers.
[pidgin-git.git] / libpurple / sslconn.c
blob3719aa534ab1559173ef993bf4d2bad8673289cb
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #include "internal.h"
24 #include "debug.h"
25 #include "plugins.h"
26 #include "request.h"
27 #include "sslconn.h"
29 #define CONNECTION_CLOSE_TIMEOUT 15
31 static void
32 emit_error(PurpleSslConnection *gsc, int error_code)
34 if (gsc->error_cb != NULL)
35 gsc->error_cb(gsc, error_code, gsc->connect_cb_data);
38 static void
39 tls_handshake_cb(GObject *source, GAsyncResult *res, gpointer user_data)
41 PurpleSslConnection *gsc = user_data;
42 GError *error = NULL;
44 if (!g_tls_connection_handshake_finish(G_TLS_CONNECTION(source), res,
45 &error)) {
46 if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
47 /* Connection already closed/freed. Escape. */
48 return;
49 } else if (g_error_matches(error, G_TLS_ERROR,
50 G_TLS_ERROR_HANDSHAKE)) {
51 /* In Gio, a handshake error is because of the cert */
52 emit_error(gsc, PURPLE_SSL_CERTIFICATE_INVALID);
53 } else {
54 /* Report any other errors as handshake failing */
55 emit_error(gsc, PURPLE_SSL_HANDSHAKE_FAILED);
58 purple_ssl_close(gsc);
59 return;
62 gsc->connect_cb(gsc->connect_cb_data, gsc, PURPLE_INPUT_READ);
65 static gboolean
66 tls_connect(PurpleSslConnection *gsc)
68 GSocket *socket;
69 GSocketConnection *conn;
70 GSocketConnectable *identity;
71 GIOStream *tls_conn;
72 GError *error = NULL;
74 g_return_val_if_fail(gsc->conn == NULL, FALSE);
76 socket = g_socket_new_from_fd(gsc->fd, &error);
77 if (socket == NULL) {
78 purple_debug_warning("sslconn",
79 "Error creating socket from fd (%u): %s",
80 gsc->fd, error->message);
81 g_clear_error(&error);
82 return FALSE;
85 conn = g_socket_connection_factory_create_connection(socket);
86 g_object_unref(socket);
88 identity = g_network_address_new(gsc->host, gsc->port);
89 tls_conn = g_tls_client_connection_new(G_IO_STREAM(conn), identity,
90 &error);
91 g_object_unref(identity);
92 g_object_unref(conn);
94 if (tls_conn == NULL) {
95 purple_debug_warning("sslconn",
96 "Error creating TLS client connection: %s",
97 error->message);
98 g_clear_error(&error);
99 return FALSE;
102 gsc->conn = G_TLS_CONNECTION(tls_conn);
103 gsc->cancellable = g_cancellable_new();
105 g_tls_connection_handshake_async(gsc->conn, G_PRIORITY_DEFAULT,
106 gsc->cancellable, tls_handshake_cb, gsc);
108 return TRUE;
111 static void
112 purple_ssl_connect_cb(gpointer data, gint source, const gchar *error_message)
114 PurpleSslConnection *gsc;
116 gsc = data;
117 gsc->connect_data = NULL;
119 if (source < 0)
121 emit_error(gsc, PURPLE_SSL_CONNECT_FAILED);
122 purple_ssl_close(gsc);
123 return;
126 gsc->fd = source;
128 if (!tls_connect(gsc)) {
129 emit_error(gsc, PURPLE_SSL_CONNECT_FAILED);
130 purple_ssl_close(gsc);
134 PurpleSslConnection *
135 purple_ssl_connect(PurpleAccount *account, const char *host, int port,
136 PurpleSslInputFunction func, PurpleSslErrorFunction error_func,
137 void *data)
139 return purple_ssl_connect_with_ssl_cn(account, host, port, func, error_func,
140 NULL, data);
143 PurpleSslConnection *
144 purple_ssl_connect_with_ssl_cn(PurpleAccount *account, const char *host, int port,
145 PurpleSslInputFunction func, PurpleSslErrorFunction error_func,
146 const char *ssl_cn, void *data)
148 PurpleSslConnection *gsc;
150 g_return_val_if_fail(host != NULL, NULL);
151 g_return_val_if_fail(port != 0 && port != -1, NULL);
152 g_return_val_if_fail(func != NULL, NULL);
154 gsc = g_new0(PurpleSslConnection, 1);
156 gsc->fd = -1;
157 gsc->host = ssl_cn ? g_strdup(ssl_cn) : g_strdup(host);
158 gsc->port = port;
159 gsc->connect_cb_data = data;
160 gsc->connect_cb = func;
161 gsc->error_cb = error_func;
163 gsc->connect_data = purple_proxy_connect(NULL, account, host, port, purple_ssl_connect_cb, gsc);
165 if (gsc->connect_data == NULL)
167 g_free(gsc->host);
168 g_free(gsc);
170 return NULL;
173 return (PurpleSslConnection *)gsc;
176 static gboolean
177 recv_cb(GObject *source, gpointer data)
179 PurpleSslConnection *gsc = data;
181 gsc->recv_cb(gsc->recv_cb_data, gsc, PURPLE_INPUT_READ);
183 return TRUE;
186 void
187 purple_ssl_input_add(PurpleSslConnection *gsc, PurpleSslInputFunction func,
188 void *data)
190 GInputStream *input;
191 GSource *source;
193 g_return_if_fail(func != NULL);
194 g_return_if_fail(gsc->conn != NULL);
196 purple_ssl_input_remove(gsc);
198 gsc->recv_cb_data = data;
199 gsc->recv_cb = func;
201 input = g_io_stream_get_input_stream(G_IO_STREAM(gsc->conn));
202 /* Pass NULL for cancellable as we don't want it notified on cancel */
203 source = g_pollable_input_stream_create_source(
204 G_POLLABLE_INPUT_STREAM(input), NULL);
205 g_source_set_callback(source, (GSourceFunc)recv_cb, gsc, NULL);
206 gsc->inpa = g_source_attach(source, NULL);
207 g_source_unref(source);
210 void
211 purple_ssl_input_remove(PurpleSslConnection *gsc)
213 if (gsc->inpa > 0) {
214 g_source_remove(gsc->inpa);
215 gsc->inpa = 0;
219 const gchar *
220 purple_ssl_strerror(PurpleSslErrorType error)
222 switch(error) {
223 case PURPLE_SSL_CONNECT_FAILED:
224 return _("SSL Connection Failed");
225 case PURPLE_SSL_HANDSHAKE_FAILED:
226 return _("SSL Handshake Failed");
227 case PURPLE_SSL_CERTIFICATE_INVALID:
228 return _("SSL peer presented an invalid certificate");
229 default:
230 purple_debug_warning("sslconn", "Unknown SSL error code %d\n", error);
231 return _("Unknown SSL error");
235 PurpleSslConnection *
236 purple_ssl_connect_with_host_fd(PurpleAccount *account, int fd,
237 PurpleSslInputFunction func,
238 PurpleSslErrorFunction error_func,
239 const char *host,
240 void *data)
242 PurpleSslConnection *gsc;
244 g_return_val_if_fail(fd != -1, NULL);
245 g_return_val_if_fail(func != NULL, NULL);
247 gsc = g_new0(PurpleSslConnection, 1);
249 gsc->connect_cb_data = data;
250 gsc->connect_cb = func;
251 gsc->error_cb = error_func;
252 gsc->fd = fd;
253 gsc->host = g_strdup(host);
254 gsc->cancellable = g_cancellable_new();
256 if (!tls_connect(gsc)) {
257 emit_error(gsc, PURPLE_SSL_CONNECT_FAILED);
258 g_clear_pointer(&gsc, purple_ssl_close);
261 return (PurpleSslConnection *)gsc;
264 static void
265 connection_closed_cb(GObject *stream, GAsyncResult *result,
266 gpointer timeout_id)
268 GError *error = NULL;
270 g_source_remove(GPOINTER_TO_UINT(timeout_id));
272 g_io_stream_close_finish(G_IO_STREAM(stream), result, &error);
274 if (error) {
275 purple_debug_info("sslconn", "Connection close error: %s",
276 error->message);
277 g_clear_error(&error);
278 } else {
279 purple_debug_info("sslconn", "Connection closed.");
283 static void
284 cleanup_cancellable_cb(gpointer data, GObject *where_the_object_was)
286 g_object_unref(G_CANCELLABLE(data));
289 void
290 purple_ssl_close(PurpleSslConnection *gsc)
292 g_return_if_fail(gsc != NULL);
294 purple_request_close_with_handle(gsc);
295 purple_notify_close_with_handle(gsc);
297 if (gsc->connect_data != NULL)
298 purple_proxy_connect_cancel(gsc->connect_data);
300 if (gsc->inpa > 0)
301 purple_input_remove(gsc->inpa);
303 /* Stop any pending operations */
304 if (G_IS_CANCELLABLE(gsc->cancellable)) {
305 g_cancellable_cancel(gsc->cancellable);
306 g_clear_object(&gsc->cancellable);
309 if (gsc->conn != NULL) {
310 GCancellable *cancellable;
311 guint timer_id;
313 cancellable = g_cancellable_new();
314 g_object_weak_ref(G_OBJECT(gsc->conn), cleanup_cancellable_cb,
315 cancellable);
317 timer_id = g_timeout_add_seconds(CONNECTION_CLOSE_TIMEOUT,
318 (GSourceFunc)g_cancellable_cancel, cancellable);
320 g_io_stream_close_async(G_IO_STREAM(gsc->conn),
321 G_PRIORITY_DEFAULT, cancellable,
322 connection_closed_cb,
323 GUINT_TO_POINTER(timer_id));
324 g_clear_object(&gsc->conn);
327 g_free(gsc->host);
328 g_free(gsc);
331 size_t
332 purple_ssl_read(PurpleSslConnection *gsc, void *data, size_t len)
334 GInputStream *input;
335 gssize outlen;
336 GError *error = NULL;
338 g_return_val_if_fail(gsc != NULL, 0);
339 g_return_val_if_fail(data != NULL, 0);
340 g_return_val_if_fail(len > 0, 0);
341 g_return_val_if_fail(gsc->conn != NULL, 0);
343 input = g_io_stream_get_input_stream(G_IO_STREAM(gsc->conn));
344 outlen = g_pollable_input_stream_read_nonblocking(
345 G_POLLABLE_INPUT_STREAM(input), data, len,
346 gsc->cancellable, &error);
348 if (outlen < 0) {
349 if (g_error_matches(error, G_IO_ERROR,
350 G_IO_ERROR_WOULD_BLOCK)) {
351 errno = EAGAIN;
354 g_clear_error(&error);
357 return outlen;
360 size_t
361 purple_ssl_write(PurpleSslConnection *gsc, const void *data, size_t len)
363 GOutputStream *output;
364 gssize outlen;
365 GError *error = NULL;
367 g_return_val_if_fail(gsc != NULL, 0);
368 g_return_val_if_fail(data != NULL, 0);
369 g_return_val_if_fail(len > 0, 0);
370 g_return_val_if_fail(gsc->conn != NULL, 0);
372 output = g_io_stream_get_output_stream(G_IO_STREAM(gsc->conn));
373 outlen = g_pollable_output_stream_write_nonblocking(
374 G_POLLABLE_OUTPUT_STREAM(output), data, len,
375 gsc->cancellable, &error);
377 if (outlen < 0) {
378 if (g_error_matches(error, G_IO_ERROR,
379 G_IO_ERROR_WOULD_BLOCK)) {
380 errno = EAGAIN;
383 g_clear_error(&error);
386 return outlen;
389 GList *
390 purple_ssl_get_peer_certificates(PurpleSslConnection *gsc)
392 GTlsCertificate *certificate;
394 g_return_val_if_fail(gsc != NULL, NULL);
395 g_return_val_if_fail(gsc->conn != NULL, NULL);
397 certificate = g_tls_connection_get_peer_certificate(gsc->conn);
399 return certificate != NULL ? g_list_append(NULL, certificate) : NULL;