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
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
29 #define CONNECTION_CLOSE_TIMEOUT 15
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
);
39 tls_handshake_cb(GObject
*source
, GAsyncResult
*res
, gpointer user_data
)
41 PurpleSslConnection
*gsc
= user_data
;
44 if (!g_tls_connection_handshake_finish(G_TLS_CONNECTION(source
), res
,
46 if (g_error_matches(error
, G_IO_ERROR
, G_IO_ERROR_CANCELLED
)) {
47 /* Connection already closed/freed. Escape. */
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
);
54 /* Report any other errors as handshake failing */
55 emit_error(gsc
, PURPLE_SSL_HANDSHAKE_FAILED
);
58 purple_ssl_close(gsc
);
62 gsc
->connect_cb(gsc
->connect_cb_data
, gsc
, PURPLE_INPUT_READ
);
66 tls_connect(PurpleSslConnection
*gsc
)
69 GSocketConnection
*conn
;
70 GSocketConnectable
*identity
;
74 g_return_val_if_fail(gsc
->conn
== NULL
, FALSE
);
76 socket
= g_socket_new_from_fd(gsc
->fd
, &error
);
78 purple_debug_warning("sslconn",
79 "Error creating socket from fd (%u): %s",
80 gsc
->fd
, error
->message
);
81 g_clear_error(&error
);
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
,
91 g_object_unref(identity
);
94 if (tls_conn
== NULL
) {
95 purple_debug_warning("sslconn",
96 "Error creating TLS client connection: %s",
98 g_clear_error(&error
);
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
);
112 purple_ssl_connect_cb(gpointer data
, gint source
, const gchar
*error_message
)
114 PurpleSslConnection
*gsc
;
117 gsc
->connect_data
= NULL
;
121 emit_error(gsc
, PURPLE_SSL_CONNECT_FAILED
);
122 purple_ssl_close(gsc
);
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
,
139 return purple_ssl_connect_with_ssl_cn(account
, host
, port
, func
, error_func
,
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);
157 gsc
->host
= ssl_cn
? g_strdup(ssl_cn
) : g_strdup(host
);
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
)
173 return (PurpleSslConnection
*)gsc
;
177 recv_cb(GObject
*source
, gpointer data
)
179 PurpleSslConnection
*gsc
= data
;
181 gsc
->recv_cb(gsc
->recv_cb_data
, gsc
, PURPLE_INPUT_READ
);
187 purple_ssl_input_add(PurpleSslConnection
*gsc
, PurpleSslInputFunction func
,
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
;
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
);
211 purple_ssl_input_remove(PurpleSslConnection
*gsc
)
214 g_source_remove(gsc
->inpa
);
220 purple_ssl_strerror(PurpleSslErrorType 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");
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
,
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
;
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
;
265 connection_closed_cb(GObject
*stream
, GAsyncResult
*result
,
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
);
275 purple_debug_info("sslconn", "Connection close error: %s",
277 g_clear_error(&error
);
279 purple_debug_info("sslconn", "Connection closed.");
284 cleanup_cancellable_cb(gpointer data
, GObject
*where_the_object_was
)
286 g_object_unref(G_CANCELLABLE(data
));
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
);
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
;
313 cancellable
= g_cancellable_new();
314 g_object_weak_ref(G_OBJECT(gsc
->conn
), cleanup_cancellable_cb
,
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
);
332 purple_ssl_read(PurpleSslConnection
*gsc
, void *data
, size_t len
)
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
);
349 if (g_error_matches(error
, G_IO_ERROR
,
350 G_IO_ERROR_WOULD_BLOCK
)) {
354 g_clear_error(&error
);
361 purple_ssl_write(PurpleSslConnection
*gsc
, const void *data
, size_t len
)
363 GOutputStream
*output
;
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
);
378 if (g_error_matches(error
, G_IO_ERROR
,
379 G_IO_ERROR_WOULD_BLOCK
)) {
383 g_clear_error(&error
);
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
;