| 1 /* purple |
|
| 2 * |
|
| 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. |
|
| 6 * |
|
| 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. |
|
| 11 * |
|
| 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. |
|
| 16 * |
|
| 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 |
|
| 20 */ |
|
| 21 |
|
| 22 #include "internal.h" |
|
| 23 |
|
| 24 #include "debug.h" |
|
| 25 #include "plugins.h" |
|
| 26 #include "request.h" |
|
| 27 #include "sslconn.h" |
|
| 28 |
|
| 29 #define CONNECTION_CLOSE_TIMEOUT 15 |
|
| 30 |
|
| 31 static void |
|
| 32 emit_error(PurpleSslConnection *gsc, int error_code) |
|
| 33 { |
|
| 34 if (gsc->error_cb != NULL) |
|
| 35 gsc->error_cb(gsc, error_code, gsc->connect_cb_data); |
|
| 36 } |
|
| 37 |
|
| 38 static void |
|
| 39 tls_handshake_cb(GObject *source, GAsyncResult *res, gpointer user_data) |
|
| 40 { |
|
| 41 PurpleSslConnection *gsc = user_data; |
|
| 42 GError *error = NULL; |
|
| 43 |
|
| 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); |
|
| 56 } |
|
| 57 |
|
| 58 purple_ssl_close(gsc); |
|
| 59 return; |
|
| 60 } |
|
| 61 |
|
| 62 gsc->connect_cb(gsc->connect_cb_data, gsc, PURPLE_INPUT_READ); |
|
| 63 } |
|
| 64 |
|
| 65 static gboolean |
|
| 66 tls_connect(PurpleSslConnection *gsc) |
|
| 67 { |
|
| 68 GSocket *socket; |
|
| 69 GSocketConnection *conn; |
|
| 70 GSocketConnectable *identity; |
|
| 71 GIOStream *tls_conn; |
|
| 72 GError *error = NULL; |
|
| 73 |
|
| 74 g_return_val_if_fail(gsc->conn == NULL, FALSE); |
|
| 75 |
|
| 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; |
|
| 83 } |
|
| 84 |
|
| 85 conn = g_socket_connection_factory_create_connection(socket); |
|
| 86 g_object_unref(socket); |
|
| 87 |
|
| 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); |
|
| 93 |
|
| 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; |
|
| 100 } |
|
| 101 |
|
| 102 gsc->conn = G_TLS_CONNECTION(tls_conn); |
|
| 103 gsc->cancellable = g_cancellable_new(); |
|
| 104 |
|
| 105 g_tls_connection_handshake_async(gsc->conn, G_PRIORITY_DEFAULT, |
|
| 106 gsc->cancellable, tls_handshake_cb, gsc); |
|
| 107 |
|
| 108 return TRUE; |
|
| 109 } |
|
| 110 |
|
| 111 static void |
|
| 112 purple_ssl_connect_cb(gpointer data, gint source, const gchar *error_message) |
|
| 113 { |
|
| 114 PurpleSslConnection *gsc; |
|
| 115 |
|
| 116 gsc = data; |
|
| 117 gsc->connect_data = NULL; |
|
| 118 |
|
| 119 if (source < 0) |
|
| 120 { |
|
| 121 emit_error(gsc, PURPLE_SSL_CONNECT_FAILED); |
|
| 122 purple_ssl_close(gsc); |
|
| 123 return; |
|
| 124 } |
|
| 125 |
|
| 126 gsc->fd = source; |
|
| 127 |
|
| 128 if (!tls_connect(gsc)) { |
|
| 129 emit_error(gsc, PURPLE_SSL_CONNECT_FAILED); |
|
| 130 purple_ssl_close(gsc); |
|
| 131 } |
|
| 132 } |
|
| 133 |
|
| 134 PurpleSslConnection * |
|
| 135 purple_ssl_connect(PurpleAccount *account, const char *host, int port, |
|
| 136 PurpleSslInputFunction func, PurpleSslErrorFunction error_func, |
|
| 137 void *data) |
|
| 138 { |
|
| 139 return purple_ssl_connect_with_ssl_cn(account, host, port, func, error_func, |
|
| 140 NULL, data); |
|
| 141 } |
|
| 142 |
|
| 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) |
|
| 147 { |
|
| 148 PurpleSslConnection *gsc; |
|
| 149 |
|
| 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); |
|
| 153 |
|
| 154 gsc = g_new0(PurpleSslConnection, 1); |
|
| 155 |
|
| 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; |
|
| 162 |
|
| 163 gsc->connect_data = purple_proxy_connect(NULL, account, host, port, purple_ssl_connect_cb, gsc); |
|
| 164 |
|
| 165 if (gsc->connect_data == NULL) |
|
| 166 { |
|
| 167 g_free(gsc->host); |
|
| 168 g_free(gsc); |
|
| 169 |
|
| 170 return NULL; |
|
| 171 } |
|
| 172 |
|
| 173 return (PurpleSslConnection *)gsc; |
|
| 174 } |
|
| 175 |
|
| 176 static gboolean |
|
| 177 recv_cb(GObject *source, gpointer data) |
|
| 178 { |
|
| 179 PurpleSslConnection *gsc = data; |
|
| 180 |
|
| 181 gsc->recv_cb(gsc->recv_cb_data, gsc, PURPLE_INPUT_READ); |
|
| 182 |
|
| 183 return TRUE; |
|
| 184 } |
|
| 185 |
|
| 186 void |
|
| 187 purple_ssl_input_add(PurpleSslConnection *gsc, PurpleSslInputFunction func, |
|
| 188 void *data) |
|
| 189 { |
|
| 190 GInputStream *input; |
|
| 191 GSource *source; |
|
| 192 |
|
| 193 g_return_if_fail(func != NULL); |
|
| 194 g_return_if_fail(gsc->conn != NULL); |
|
| 195 |
|
| 196 purple_ssl_input_remove(gsc); |
|
| 197 |
|
| 198 gsc->recv_cb_data = data; |
|
| 199 gsc->recv_cb = func; |
|
| 200 |
|
| 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); |
|
| 208 } |
|
| 209 |
|
| 210 void |
|
| 211 purple_ssl_input_remove(PurpleSslConnection *gsc) |
|
| 212 { |
|
| 213 if (gsc->inpa > 0) { |
|
| 214 g_source_remove(gsc->inpa); |
|
| 215 gsc->inpa = 0; |
|
| 216 } |
|
| 217 } |
|
| 218 |
|
| 219 const gchar * |
|
| 220 purple_ssl_strerror(PurpleSslErrorType error) |
|
| 221 { |
|
| 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"); |
|
| 232 } |
|
| 233 } |
|
| 234 |
|
| 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) |
|
| 241 { |
|
| 242 PurpleSslConnection *gsc; |
|
| 243 |
|
| 244 g_return_val_if_fail(fd != -1, NULL); |
|
| 245 g_return_val_if_fail(func != NULL, NULL); |
|
| 246 |
|
| 247 gsc = g_new0(PurpleSslConnection, 1); |
|
| 248 |
|
| 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(); |
|
| 255 |
|
| 256 if (!tls_connect(gsc)) { |
|
| 257 emit_error(gsc, PURPLE_SSL_CONNECT_FAILED); |
|
| 258 g_clear_pointer(&gsc, purple_ssl_close); |
|
| 259 } |
|
| 260 |
|
| 261 return (PurpleSslConnection *)gsc; |
|
| 262 } |
|
| 263 |
|
| 264 static void |
|
| 265 connection_closed_cb(GObject *stream, GAsyncResult *result, |
|
| 266 gpointer timeout_id) |
|
| 267 { |
|
| 268 GError *error = NULL; |
|
| 269 |
|
| 270 g_source_remove(GPOINTER_TO_UINT(timeout_id)); |
|
| 271 |
|
| 272 g_io_stream_close_finish(G_IO_STREAM(stream), result, &error); |
|
| 273 |
|
| 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."); |
|
| 280 } |
|
| 281 } |
|
| 282 |
|
| 283 static void |
|
| 284 cleanup_cancellable_cb(gpointer data, GObject *where_the_object_was) |
|
| 285 { |
|
| 286 g_object_unref(G_CANCELLABLE(data)); |
|
| 287 } |
|
| 288 |
|
| 289 void |
|
| 290 purple_ssl_close(PurpleSslConnection *gsc) |
|
| 291 { |
|
| 292 g_return_if_fail(gsc != NULL); |
|
| 293 |
|
| 294 purple_request_close_with_handle(gsc); |
|
| 295 purple_notify_close_with_handle(gsc); |
|
| 296 |
|
| 297 if (gsc->connect_data != NULL) |
|
| 298 purple_proxy_connect_cancel(gsc->connect_data); |
|
| 299 |
|
| 300 if (gsc->inpa > 0) |
|
| 301 purple_input_remove(gsc->inpa); |
|
| 302 |
|
| 303 /* Stop any pending operations */ |
|
| 304 if (G_IS_CANCELLABLE(gsc->cancellable)) { |
|
| 305 g_cancellable_cancel(gsc->cancellable); |
|
| 306 g_clear_object(&gsc->cancellable); |
|
| 307 } |
|
| 308 |
|
| 309 if (gsc->conn != NULL) { |
|
| 310 GCancellable *cancellable; |
|
| 311 guint timer_id; |
|
| 312 |
|
| 313 cancellable = g_cancellable_new(); |
|
| 314 g_object_weak_ref(G_OBJECT(gsc->conn), cleanup_cancellable_cb, |
|
| 315 cancellable); |
|
| 316 |
|
| 317 timer_id = g_timeout_add_seconds(CONNECTION_CLOSE_TIMEOUT, |
|
| 318 (GSourceFunc)g_cancellable_cancel, cancellable); |
|
| 319 |
|
| 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); |
|
| 325 } |
|
| 326 |
|
| 327 g_free(gsc->host); |
|
| 328 g_free(gsc); |
|
| 329 } |
|
| 330 |
|
| 331 size_t |
|
| 332 purple_ssl_read(PurpleSslConnection *gsc, void *data, size_t len) |
|
| 333 { |
|
| 334 GInputStream *input; |
|
| 335 gssize outlen; |
|
| 336 GError *error = NULL; |
|
| 337 |
|
| 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); |
|
| 342 |
|
| 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); |
|
| 347 |
|
| 348 if (outlen < 0) { |
|
| 349 if (g_error_matches(error, G_IO_ERROR, |
|
| 350 G_IO_ERROR_WOULD_BLOCK)) { |
|
| 351 errno = EAGAIN; |
|
| 352 } |
|
| 353 |
|
| 354 g_clear_error(&error); |
|
| 355 } |
|
| 356 |
|
| 357 return outlen; |
|
| 358 } |
|
| 359 |
|
| 360 size_t |
|
| 361 purple_ssl_write(PurpleSslConnection *gsc, const void *data, size_t len) |
|
| 362 { |
|
| 363 GOutputStream *output; |
|
| 364 gssize outlen; |
|
| 365 GError *error = NULL; |
|
| 366 |
|
| 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); |
|
| 371 |
|
| 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); |
|
| 376 |
|
| 377 if (outlen < 0) { |
|
| 378 if (g_error_matches(error, G_IO_ERROR, |
|
| 379 G_IO_ERROR_WOULD_BLOCK)) { |
|
| 380 errno = EAGAIN; |
|
| 381 } |
|
| 382 |
|
| 383 g_clear_error(&error); |
|
| 384 } |
|
| 385 |
|
| 386 return outlen; |
|
| 387 } |
|
| 388 |
|
| 389 GList * |
|
| 390 purple_ssl_get_peer_certificates(PurpleSslConnection *gsc) |
|
| 391 { |
|
| 392 GTlsCertificate *certificate; |
|
| 393 |
|
| 394 g_return_val_if_fail(gsc != NULL, NULL); |
|
| 395 g_return_val_if_fail(gsc->conn != NULL, NULL); |
|
| 396 |
|
| 397 certificate = g_tls_connection_get_peer_certificate(gsc->conn); |
|
| 398 |
|
| 399 return certificate != NULL ? g_list_append(NULL, certificate) : NULL; |
|
| 400 } |
|
| 401 |
|