1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2009 Red Hat, Inc
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include <gnutls/gnutls.h>
26 #include <gnutls/x509.h>
28 #include "gtlsconnection-gnutls.h"
29 #include "gtlsbackend-gnutls.h"
30 #include "gtlscertificate-gnutls.h"
31 #include "gtlsinputstream-gnutls.h"
32 #include "gtlsoutputstream-gnutls.h"
33 #include "gtlsserverconnection-gnutls.h"
34 #include <glib/gi18n-lib.h>
36 static void g_tls_connection_gnutls_get_property (GObject *object,
40 static void g_tls_connection_gnutls_set_property (GObject *object,
44 static void g_tls_connection_gnutls_finalize (GObject *object);
46 static gboolean g_tls_connection_gnutls_handshake (GTlsConnection *connection,
47 GCancellable *cancellable,
49 static void g_tls_connection_gnutls_handshake_async (GTlsConnection *conn,
51 GCancellable *cancellable,
52 GAsyncReadyCallback callback,
54 static gboolean g_tls_connection_gnutls_handshake_finish (GTlsConnection *conn,
58 static GInputStream *g_tls_connection_gnutls_get_input_stream (GIOStream *stream);
59 static GOutputStream *g_tls_connection_gnutls_get_output_stream (GIOStream *stream);
61 static gboolean g_tls_connection_gnutls_close (GIOStream *stream,
62 GCancellable *cancellable,
64 static void g_tls_connection_gnutls_close_async (GIOStream *stream,
66 GCancellable *cancellable,
67 GAsyncReadyCallback callback,
69 static gboolean g_tls_connection_gnutls_close_finish (GIOStream *stream,
73 static ssize_t g_tls_connection_gnutls_push_func (gnutls_transport_ptr_t transport_data,
76 static ssize_t g_tls_connection_gnutls_pull_func (gnutls_transport_ptr_t transport_data,
80 static void g_tls_connection_gnutls_initable_iface_init (GInitableIface *iface);
81 static gboolean g_tls_connection_gnutls_initable_init (GInitable *initable,
82 GCancellable *cancellable,
85 static void g_tls_connection_gnutls_init_priorities (void);
87 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GTlsConnectionGnutls, g_tls_connection_gnutls, G_TYPE_TLS_CONNECTION,
88 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
89 g_tls_connection_gnutls_initable_iface_init);
90 g_tls_connection_gnutls_init_priorities ();
98 PROP_REQUIRE_CLOSE_NOTIFY,
99 PROP_REHANDSHAKE_MODE,
100 PROP_USE_SYSTEM_CERTDB
103 struct _GTlsConnectionGnutlsPrivate
105 GIOStream *base_io_stream;
106 GPollableInputStream *base_istream;
107 GPollableOutputStream *base_ostream;
110 gnutls_certificate_credentials creds;
111 gnutls_session session;
112 gboolean require_close_notify;
113 GTlsRehandshakeMode rehandshake_mode;
114 gboolean use_system_certdb;
115 gboolean need_handshake, handshaking, ever_handshaked;
118 GInputStream *tls_istream;
119 GOutputStream *tls_ostream;
122 GCancellable *cancellable;
123 gboolean blocking, eof;
124 GIOCondition internal_direction;
128 g_tls_connection_gnutls_class_init (GTlsConnectionGnutlsClass *klass)
130 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
131 GTlsConnectionClass *connection_class = G_TLS_CONNECTION_CLASS (klass);
132 GIOStreamClass *iostream_class = G_IO_STREAM_CLASS (klass);
134 g_type_class_add_private (klass, sizeof (GTlsConnectionGnutlsPrivate));
136 gobject_class->get_property = g_tls_connection_gnutls_get_property;
137 gobject_class->set_property = g_tls_connection_gnutls_set_property;
138 gobject_class->finalize = g_tls_connection_gnutls_finalize;
140 connection_class->handshake = g_tls_connection_gnutls_handshake;
141 connection_class->handshake_async = g_tls_connection_gnutls_handshake_async;
142 connection_class->handshake_finish = g_tls_connection_gnutls_handshake_finish;
144 iostream_class->get_input_stream = g_tls_connection_gnutls_get_input_stream;
145 iostream_class->get_output_stream = g_tls_connection_gnutls_get_output_stream;
146 iostream_class->close_fn = g_tls_connection_gnutls_close;
147 iostream_class->close_async = g_tls_connection_gnutls_close_async;
148 iostream_class->close_finish = g_tls_connection_gnutls_close_finish;
150 g_object_class_override_property (gobject_class, PROP_BASE_IO_STREAM, "base-io-stream");
151 g_object_class_override_property (gobject_class, PROP_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
152 g_object_class_override_property (gobject_class, PROP_REHANDSHAKE_MODE, "rehandshake-mode");
153 g_object_class_override_property (gobject_class, PROP_USE_SYSTEM_CERTDB, "use-system-certdb");
157 g_tls_connection_gnutls_initable_iface_init (GInitableIface *iface)
159 iface->init = g_tls_connection_gnutls_initable_init;
163 g_tls_connection_gnutls_init (GTlsConnectionGnutls *gnutls)
165 gnutls->priv = G_TYPE_INSTANCE_GET_PRIVATE (gnutls, G_TYPE_TLS_CONNECTION_GNUTLS, GTlsConnectionGnutlsPrivate);
167 gnutls_certificate_allocate_credentials (&gnutls->priv->creds);
168 gnutls_certificate_set_verify_flags (gnutls->priv->creds,
169 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
171 gnutls->priv->need_handshake = TRUE;
174 static gnutls_priority_t priorities[2][2];
177 g_tls_connection_gnutls_init_priorities (void)
179 /* First field is "ssl3 only", second is "allow unsafe rehandshaking" */
181 gnutls_priority_init (&priorities[FALSE][FALSE],
184 gnutls_priority_init (&priorities[TRUE][FALSE],
185 "NORMAL:!VERS-TLS1.2:!VERS-TLS1.1:!VERS-TLS1.0",
187 gnutls_priority_init (&priorities[FALSE][TRUE],
188 "NORMAL:%UNSAFE_RENEGOTIATION",
190 gnutls_priority_init (&priorities[TRUE][TRUE],
191 "NORMAL:!VERS-TLS1.2:!VERS-TLS1.1:!VERS-TLS1.0:%UNSAFE_RENEGOTIATION",
196 g_tls_connection_gnutls_set_handshake_priority (GTlsConnectionGnutls *gnutls)
198 gboolean use_ssl3, unsafe_rehandshake;
200 if (G_IS_TLS_CLIENT_CONNECTION (gnutls))
201 use_ssl3 = g_tls_client_connection_get_use_ssl3 (G_TLS_CLIENT_CONNECTION (gnutls));
202 unsafe_rehandshake = (gnutls->priv->rehandshake_mode == G_TLS_REHANDSHAKE_UNSAFELY);
203 gnutls_priority_set (gnutls->priv->session,
204 priorities[use_ssl3][unsafe_rehandshake]);
208 g_tls_connection_gnutls_initable_init (GInitable *initable,
209 GCancellable *cancellable,
212 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (initable);
215 g_return_val_if_fail (gnutls->priv->base_istream != NULL &&
216 gnutls->priv->base_ostream != NULL, FALSE);
218 /* Make sure gnutls->priv->session has been initialized (it may have
219 * already been initialized by a construct-time property setter).
221 g_tls_connection_gnutls_get_session (gnutls);
223 status = gnutls_credentials_set (gnutls->priv->session,
224 GNUTLS_CRD_CERTIFICATE,
225 gnutls->priv->creds);
228 g_set_error (error, G_TLS_ERROR, G_TLS_ERROR_MISC,
229 _("Could not create TLS connection: %s"),
230 gnutls_strerror (status));
234 gnutls_transport_set_push_function (gnutls->priv->session,
235 g_tls_connection_gnutls_push_func);
236 gnutls_transport_set_pull_function (gnutls->priv->session,
237 g_tls_connection_gnutls_pull_func);
238 gnutls_transport_set_ptr (gnutls->priv->session, gnutls);
240 gnutls->priv->tls_istream = g_tls_input_stream_gnutls_new (gnutls);
241 gnutls->priv->tls_ostream = g_tls_output_stream_gnutls_new (gnutls);
247 g_tls_connection_gnutls_finalize (GObject *object)
249 GTlsConnectionGnutls *connection = G_TLS_CONNECTION_GNUTLS (object);
251 if (connection->priv->base_io_stream)
252 g_object_unref (connection->priv->base_io_stream);
254 if (connection->priv->session)
255 gnutls_deinit (connection->priv->session);
257 if (connection->priv->tls_istream)
258 g_object_unref (connection->priv->tls_istream);
259 if (connection->priv->tls_ostream)
260 g_object_unref (connection->priv->tls_ostream);
262 if (connection->priv->creds)
263 gnutls_certificate_free_credentials (connection->priv->creds);
265 if (connection->priv->error)
266 g_error_free (connection->priv->error);
268 G_OBJECT_CLASS (g_tls_connection_gnutls_parent_class)->finalize (object);
272 g_tls_connection_gnutls_get_property (GObject *object,
277 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (object);
281 case PROP_BASE_IO_STREAM:
282 g_value_set_object (value, gnutls->priv->base_io_stream);
285 case PROP_REQUIRE_CLOSE_NOTIFY:
286 g_value_set_boolean (value, gnutls->priv->require_close_notify);
289 case PROP_REHANDSHAKE_MODE:
290 g_value_set_enum (value, gnutls->priv->rehandshake_mode);
293 case PROP_USE_SYSTEM_CERTDB:
294 g_value_set_boolean (value, gnutls->priv->use_system_certdb);
298 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
303 g_tls_connection_gnutls_set_property (GObject *object,
308 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (object);
309 GInputStream *istream;
310 GOutputStream *ostream;
314 case PROP_BASE_IO_STREAM:
315 if (gnutls->priv->base_io_stream)
317 g_object_unref (gnutls->priv->base_io_stream);
318 gnutls->priv->base_istream = NULL;
319 gnutls->priv->base_ostream = NULL;
321 gnutls->priv->base_io_stream = g_value_dup_object (value);
322 if (!gnutls->priv->base_io_stream)
325 istream = g_io_stream_get_input_stream (gnutls->priv->base_io_stream);
326 ostream = g_io_stream_get_output_stream (gnutls->priv->base_io_stream);
328 if (G_IS_POLLABLE_INPUT_STREAM (istream) &&
329 g_pollable_input_stream_can_poll (G_POLLABLE_INPUT_STREAM (istream)))
330 gnutls->priv->base_istream = G_POLLABLE_INPUT_STREAM (istream);
331 if (G_IS_POLLABLE_OUTPUT_STREAM (ostream) &&
332 g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (ostream)))
333 gnutls->priv->base_ostream = G_POLLABLE_OUTPUT_STREAM (ostream);
336 case PROP_REQUIRE_CLOSE_NOTIFY:
337 gnutls->priv->require_close_notify = g_value_get_boolean (value);
340 case PROP_REHANDSHAKE_MODE:
341 gnutls->priv->rehandshake_mode = g_value_get_enum (value);
344 case PROP_USE_SYSTEM_CERTDB:
345 gnutls->priv->use_system_certdb = g_value_get_boolean (value);
347 gnutls_certificate_free_cas (gnutls->priv->creds);
348 if (gnutls->priv->use_system_certdb)
350 gnutls_x509_crt_t *cas;
353 g_tls_backend_gnutls_get_system_ca_list_gnutls (&cas, &num_cas);
354 gnutls_certificate_set_x509_trust (gnutls->priv->creds, cas, num_cas);
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363 gnutls_certificate_credentials
364 g_tls_connection_gnutls_get_credentials (GTlsConnectionGnutls *gnutls)
366 return gnutls->priv->creds;
370 g_tls_connection_gnutls_get_session (GTlsConnectionGnutls *gnutls)
372 /* Ideally we would initialize gnutls->priv->session from
373 * g_tls_connection_gnutls_init(), but we can't tell if it's a
374 * client or server connection at that point... And
375 * g_tls_connection_gnutls_initiable_init() is too late, because
376 * construct-time property setters may need to modify it.
378 if (!gnutls->priv->session)
380 gboolean client = G_IS_TLS_CLIENT_CONNECTION (gnutls);
381 gnutls_init (&gnutls->priv->session, client ? GNUTLS_CLIENT : GNUTLS_SERVER);
384 return gnutls->priv->session;
388 g_tls_connection_gnutls_get_certificate (GTlsConnectionGnutls *gnutls,
391 GTlsCertificate *cert;
393 cert = g_tls_connection_get_certificate (G_TLS_CONNECTION (gnutls));
395 st->type = GNUTLS_CRT_X509;
398 GTlsCertificateGnutls *gnutlscert = G_TLS_CERTIFICATE_GNUTLS (cert);
401 st->cert.x509 = gnutls_malloc (sizeof (gnutls_x509_crt_t));
402 st->cert.x509[0] = g_tls_certificate_gnutls_copy_cert (gnutlscert);
403 st->key.x509 = g_tls_certificate_gnutls_copy_key (gnutlscert);
404 st->deinit_all = TRUE;
411 g_tls_connection_gnutls_validate_peer (GTlsConnectionGnutls *gnutls)
415 status = gnutls_certificate_verify_peers (gnutls->priv->session);
416 return g_tls_certificate_gnutls_convert_flags (status);
420 begin_gnutls_io (GTlsConnectionGnutls *gnutls,
422 GCancellable *cancellable)
424 gnutls->priv->blocking = blocking;
425 gnutls->priv->cancellable = cancellable;
426 gnutls->priv->internal_direction = 0;
427 g_clear_error (&gnutls->priv->error);
431 end_gnutls_io (GTlsConnectionGnutls *gnutls,
433 const char *generic_error,
436 gnutls->priv->cancellable = NULL;
440 g_clear_error (&gnutls->priv->error);
444 if (gnutls->priv->handshaking && !gnutls->priv->ever_handshaked)
446 if (g_error_matches (gnutls->priv->error, G_IO_ERROR, G_IO_ERROR_FAILED) ||
447 status == GNUTLS_E_UNEXPECTED_PACKET_LENGTH ||
448 status == GNUTLS_E_FATAL_ALERT_RECEIVED ||
449 status == GNUTLS_E_UNSUPPORTED_VERSION_PACKET)
451 g_clear_error (&gnutls->priv->error);
452 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_NOT_TLS,
453 _("Peer failed to perform TLS handshake"));
454 return GNUTLS_E_PULL_ERROR;
458 if (gnutls->priv->error)
460 if (g_error_matches (gnutls->priv->error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
461 status = GNUTLS_E_AGAIN;
462 g_propagate_error (error, gnutls->priv->error);
463 gnutls->priv->error = NULL;
466 else if (status == GNUTLS_E_REHANDSHAKE)
468 if (gnutls->priv->rehandshake_mode == G_TLS_REHANDSHAKE_NEVER)
470 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_MISC,
471 _("Peer requested illegal TLS rehandshake"));
472 return GNUTLS_E_PULL_ERROR;
475 gnutls->priv->need_handshake = TRUE;
478 else if (status == GNUTLS_E_UNEXPECTED_PACKET_LENGTH)
480 if (gnutls->priv->eof)
482 if (gnutls->priv->require_close_notify)
484 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_EOF,
485 _("TLS connection closed unexpectedly"));
493 g_set_error (error, G_TLS_ERROR, G_TLS_ERROR_MISC,
494 generic_error, gnutls_strerror (status));
498 #define BEGIN_GNUTLS_IO(gnutls, blocking, cancellable) \
499 begin_gnutls_io (gnutls, blocking, cancellable); \
502 #define END_GNUTLS_IO(gnutls, ret, errmsg, error) \
503 } while ((ret == GNUTLS_E_AGAIN || \
504 ret == GNUTLS_E_WARNING_ALERT_RECEIVED) && \
505 !gnutls->priv->error); \
506 ret = end_gnutls_io (gnutls, ret, errmsg, error)
509 g_tls_connection_gnutls_check (GTlsConnectionGnutls *gnutls,
510 GIOCondition condition)
512 if (gnutls->priv->handshaking || gnutls->priv->closing)
513 condition = gnutls->priv->internal_direction;
515 if (condition & G_IO_IN)
516 return g_pollable_input_stream_is_readable (gnutls->priv->base_istream);
518 return g_pollable_output_stream_is_writable (gnutls->priv->base_ostream);
524 GTlsConnectionGnutls *gnutls;
527 GSource *child_source;
528 GIOCondition base_direction;
529 GIOCondition current_direction;
530 } GTlsConnectionGnutlsSource;
533 gnutls_source_prepare (GSource *source,
541 gnutls_source_check (GSource *source)
547 gnutls_source_sync_child_source (GTlsConnectionGnutlsSource *gnutls_source,
548 GIOCondition direction)
550 GTlsConnectionGnutls *gnutls = gnutls_source->gnutls;
551 GSource *source = (GSource *)gnutls_source;
553 if (direction == gnutls_source->current_direction)
556 if (gnutls_source->child_source)
558 g_source_remove_child_source (source, gnutls_source->child_source);
559 g_source_unref (gnutls_source->child_source);
562 if (direction & G_IO_IN)
563 gnutls_source->child_source = g_pollable_input_stream_create_source (gnutls->priv->base_istream, NULL);
565 gnutls_source->child_source = g_pollable_output_stream_create_source (gnutls->priv->base_ostream, NULL);
567 g_source_set_dummy_callback (gnutls_source->child_source);
568 g_source_add_child_source (source, gnutls_source->child_source);
569 gnutls_source->current_direction = direction;
573 gnutls_source_dispatch (GSource *source,
574 GSourceFunc callback,
577 GPollableSourceFunc func = (GPollableSourceFunc)callback;
578 GTlsConnectionGnutlsSource *gnutls_source = (GTlsConnectionGnutlsSource *)source;
579 GTlsConnectionGnutls *gnutls = gnutls_source->gnutls;
582 ret = (*func) (gnutls_source->stream, user_data);
585 GIOCondition direction = gnutls->priv->internal_direction ? gnutls->priv->internal_direction : gnutls_source->base_direction;
587 gnutls_source_sync_child_source (gnutls_source, direction);
594 gnutls_source_finalize (GSource *source)
596 GTlsConnectionGnutlsSource *gnutls_source = (GTlsConnectionGnutlsSource *)source;
598 g_object_unref (gnutls_source->gnutls);
600 if (gnutls_source->child_source)
601 g_source_unref (gnutls_source->child_source);
604 static GSourceFuncs gnutls_source_funcs =
606 gnutls_source_prepare,
608 gnutls_source_dispatch,
609 gnutls_source_finalize
613 g_tls_connection_gnutls_create_source (GTlsConnectionGnutls *gnutls,
614 GIOCondition condition,
615 GCancellable *cancellable)
617 GSource *source, *cancellable_source;
618 GTlsConnectionGnutlsSource *gnutls_source;
620 source = g_source_new (&gnutls_source_funcs, sizeof (GTlsConnectionGnutlsSource));
621 g_source_set_name (source, "GTlsConnectionGnutlsSource");
622 gnutls_source = (GTlsConnectionGnutlsSource *)source;
623 gnutls_source->gnutls = g_object_ref (gnutls);
624 gnutls_source->base_direction = condition & (G_IO_IN | G_IO_OUT);
625 if (gnutls_source->base_direction == G_IO_IN)
626 gnutls_source->stream = G_OBJECT (gnutls->priv->tls_istream);
627 else if (gnutls_source->base_direction == G_IO_OUT)
628 gnutls_source->stream = G_OBJECT (gnutls->priv->tls_ostream);
631 gnutls_source->base_direction = gnutls->priv->internal_direction;
632 gnutls_source->stream = NULL;
634 gnutls_source_sync_child_source (gnutls_source, gnutls_source->base_direction);
638 cancellable_source = g_cancellable_source_new (cancellable);
639 g_source_set_dummy_callback (cancellable_source);
640 g_source_add_child_source (source, cancellable_source);
641 g_source_unref (cancellable_source);
648 set_gnutls_error (GTlsConnectionGnutls *gnutls, GIOCondition direction)
650 if (g_error_matches (gnutls->priv->error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
651 gnutls_transport_set_errno (gnutls->priv->session, EINTR);
652 else if (g_error_matches (gnutls->priv->error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
654 gnutls_transport_set_errno (gnutls->priv->session, EAGAIN);
655 gnutls->priv->internal_direction = direction;
658 gnutls_transport_set_errno (gnutls->priv->session, EIO);
662 g_tls_connection_gnutls_pull_func (gnutls_transport_ptr_t transport_data,
666 GTlsConnectionGnutls *gnutls = transport_data;
669 /* If gnutls->priv->error is non-%NULL when we're called, it means
670 * that an error previously occurred, but gnutls decided not to
671 * propagate it. So it's correct for us to just clear it. (Usually
672 * this means it ignored an EAGAIN after a short read, and now
673 * we'll return EAGAIN again, which it will obey this time.)
675 g_clear_error (&gnutls->priv->error);
677 if (gnutls->priv->blocking)
679 ret = g_input_stream_read (G_INPUT_STREAM (gnutls->priv->base_istream),
681 gnutls->priv->cancellable,
682 &gnutls->priv->error);
686 ret = g_pollable_input_stream_read_nonblocking (gnutls->priv->base_istream,
688 gnutls->priv->cancellable,
689 &gnutls->priv->error);
693 set_gnutls_error (gnutls, G_IO_IN);
695 gnutls->priv->eof = TRUE;
701 g_tls_connection_gnutls_push_func (gnutls_transport_ptr_t transport_data,
705 GTlsConnectionGnutls *gnutls = transport_data;
708 /* See comment in pull_func. */
709 g_clear_error (&gnutls->priv->error);
711 if (gnutls->priv->blocking)
713 ret = g_output_stream_write (G_OUTPUT_STREAM (gnutls->priv->base_ostream),
715 gnutls->priv->cancellable,
716 &gnutls->priv->error);
720 ret = g_pollable_output_stream_write_nonblocking (gnutls->priv->base_ostream,
722 gnutls->priv->cancellable,
723 &gnutls->priv->error);
726 set_gnutls_error (gnutls, G_IO_OUT);
732 handshake_internal (GTlsConnectionGnutls *gnutls,
734 GCancellable *cancellable,
739 if (G_IS_TLS_SERVER_CONNECTION_GNUTLS (gnutls) &&
740 gnutls->priv->ever_handshaked && !gnutls->priv->need_handshake)
742 BEGIN_GNUTLS_IO (gnutls, blocking, cancellable);
743 ret = gnutls_rehandshake (gnutls->priv->session);
744 END_GNUTLS_IO (gnutls, ret, _("Error performing TLS handshake: %s"), error);
750 g_tls_connection_gnutls_set_handshake_priority (gnutls);
752 gnutls->priv->handshaking = TRUE;
753 G_TLS_CONNECTION_GNUTLS_GET_CLASS (gnutls)->begin_handshake (gnutls);
755 BEGIN_GNUTLS_IO (gnutls, blocking, cancellable);
756 ret = gnutls_handshake (gnutls->priv->session);
757 END_GNUTLS_IO (gnutls, ret, _("Error performing TLS handshake: %s"), error);
759 if (ret == GNUTLS_E_AGAIN)
762 gnutls->priv->handshaking = FALSE;
763 gnutls->priv->need_handshake = FALSE;
764 gnutls->priv->ever_handshaked = TRUE;
767 gnutls_certificate_type_get (gnutls->priv->session) == GNUTLS_CRT_X509)
769 GTlsCertificate *chain, *cert;
770 const gnutls_datum_t *certs;
771 unsigned int num_certs;
774 certs = gnutls_certificate_get_peers (gnutls->priv->session, &num_certs);
778 for (i = num_certs - 1; i >= 0; i--)
780 cert = g_tls_certificate_gnutls_new (&certs[i], chain);
785 g_tls_connection_set_peer_certificate (G_TLS_CONNECTION (gnutls), chain);
788 g_tls_connection_set_peer_certificate (G_TLS_CONNECTION (gnutls), NULL);
790 return G_TLS_CONNECTION_GNUTLS_GET_CLASS (gnutls)->finish_handshake (gnutls, ret == 0, error);
794 handshake_in_progress_or_failed (GTlsConnectionGnutls *gnutls,
796 GCancellable *cancellable,
799 if (!(gnutls->priv->need_handshake || gnutls->priv->handshaking))
802 return !handshake_internal (gnutls, blocking, cancellable, error);
806 g_tls_connection_gnutls_handshake (GTlsConnection *conn,
807 GCancellable *cancellable,
810 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (conn);
812 return handshake_internal (gnutls, TRUE, cancellable, error);
816 g_tls_connection_gnutls_handshake_ready (GObject *pollable_stream,
819 GTlsConnectionGnutls *gnutls;
820 GSimpleAsyncResult *simple = user_data;
822 GError *error = NULL;
824 gnutls = G_TLS_CONNECTION_GNUTLS (g_async_result_get_source_object (G_ASYNC_RESULT (simple)));
825 g_object_unref (gnutls);
827 success = handshake_internal (gnutls, FALSE, NULL, &error);
828 if (!success && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
830 g_error_free (error);
836 g_simple_async_result_set_from_error (simple, error);
837 g_error_free (error);
840 g_simple_async_result_set_op_res_gboolean (simple, success);
841 g_simple_async_result_complete (simple);
842 g_object_unref (simple);
848 g_tls_connection_gnutls_handshake_async (GTlsConnection *conn,
850 GCancellable *cancellable,
851 GAsyncReadyCallback callback,
854 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (conn);
855 GSimpleAsyncResult *simple;
857 GError *error = NULL;
860 simple = g_simple_async_result_new (G_OBJECT (conn), callback, user_data,
861 g_tls_connection_gnutls_handshake_async);
862 success = handshake_internal (gnutls, FALSE, cancellable, &error);
865 g_simple_async_result_set_op_res_gboolean (simple, TRUE);
866 g_simple_async_result_complete_in_idle (simple);
867 g_object_unref (simple);
869 else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
871 g_simple_async_result_set_from_error (simple, error);
872 g_error_free (error);
873 g_simple_async_result_complete_in_idle (simple);
874 g_object_unref (simple);
877 source = g_tls_connection_gnutls_create_source (gnutls, 0, cancellable);
878 g_source_set_callback (source,
879 (GSourceFunc) g_tls_connection_gnutls_handshake_ready,
881 g_source_set_priority (source, io_priority);
882 g_source_attach (source, g_main_context_get_thread_default ());
883 g_source_unref (source);
887 g_tls_connection_gnutls_handshake_finish (GTlsConnection *conn,
888 GAsyncResult *result,
891 GSimpleAsyncResult *simple;
893 g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (conn), g_tls_connection_gnutls_handshake_async), FALSE);
895 simple = G_SIMPLE_ASYNC_RESULT (result);
897 if (g_simple_async_result_propagate_error (simple, error))
900 return g_simple_async_result_get_op_res_gboolean (simple);
904 g_tls_connection_gnutls_read (GTlsConnectionGnutls *gnutls,
908 GCancellable *cancellable,
914 if (handshake_in_progress_or_failed (gnutls, blocking, cancellable, error))
917 BEGIN_GNUTLS_IO (gnutls, blocking, cancellable);
918 ret = gnutls_record_recv (gnutls->priv->session, buffer, count);
919 END_GNUTLS_IO (gnutls, ret, _("Error reading data from TLS socket: %s"), error);
923 else if (ret == GNUTLS_E_REHANDSHAKE)
930 g_tls_connection_gnutls_write (GTlsConnectionGnutls *gnutls,
934 GCancellable *cancellable,
940 if (handshake_in_progress_or_failed (gnutls, blocking, cancellable, error))
943 BEGIN_GNUTLS_IO (gnutls, blocking, cancellable);
944 ret = gnutls_record_send (gnutls->priv->session, buffer, count);
945 END_GNUTLS_IO (gnutls, ret, _("Error writing data to TLS socket: %s"), error);
949 else if (ret == GNUTLS_E_REHANDSHAKE)
955 static GInputStream *
956 g_tls_connection_gnutls_get_input_stream (GIOStream *stream)
958 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (stream);
960 return gnutls->priv->tls_istream;
963 static GOutputStream *
964 g_tls_connection_gnutls_get_output_stream (GIOStream *stream)
966 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (stream);
968 return gnutls->priv->tls_ostream;
972 close_internal (GTlsConnectionGnutls *gnutls,
974 GCancellable *cancellable,
979 /* If we haven't finished the initial handshake yet, there's no
980 * reason to finish it just so we can close.
982 if (gnutls->priv->handshaking && !gnutls->priv->ever_handshaked)
985 if (handshake_in_progress_or_failed (gnutls, blocking, cancellable, error))
988 gnutls->priv->closing = TRUE;
989 BEGIN_GNUTLS_IO (gnutls, blocking, cancellable);
990 ret = gnutls_bye (gnutls->priv->session, GNUTLS_SHUT_WR);
991 END_GNUTLS_IO (gnutls, ret, _("Error performing TLS close: %s"), error);
992 if (ret == 0 || !error || !g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
993 gnutls->priv->closing = FALSE;
999 g_tls_connection_gnutls_close (GIOStream *stream,
1000 GCancellable *cancellable,
1003 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (stream);
1005 if (!close_internal (gnutls, TRUE, cancellable, error))
1007 return g_io_stream_close (gnutls->priv->base_io_stream,
1008 cancellable, error);
1012 GSimpleAsyncResult *simple;
1013 GCancellable *cancellable;
1018 close_base_stream_cb (GObject *base_stream,
1019 GAsyncResult *result,
1023 GError *error = NULL;
1024 AsyncCloseData *acd = user_data;
1026 success = g_io_stream_close_finish (G_IO_STREAM (base_stream),
1029 g_simple_async_result_set_op_res_gboolean (acd->simple, TRUE);
1032 g_simple_async_result_set_from_error (acd->simple, error);
1033 g_error_free (error);
1036 g_simple_async_result_complete (acd->simple);
1037 g_object_unref (acd->simple);
1038 if (acd->cancellable)
1039 g_object_unref (acd->cancellable);
1040 g_slice_free (AsyncCloseData, acd);
1044 g_tls_connection_gnutls_close_ready (GObject *pollable_stream,
1047 GTlsConnectionGnutls *gnutls;
1048 AsyncCloseData *acd = user_data;
1050 GError *error = NULL;
1052 gnutls = G_TLS_CONNECTION_GNUTLS (g_async_result_get_source_object (G_ASYNC_RESULT (acd->simple)));
1053 g_object_unref (gnutls);
1055 success = close_internal (gnutls, FALSE, NULL, &error);
1056 if (!success && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1058 g_error_free (error);
1064 g_simple_async_result_set_from_error (acd->simple, error);
1065 g_simple_async_result_complete (acd->simple);
1066 g_error_free (error);
1067 g_object_unref (acd->simple);
1068 if (acd->cancellable)
1069 g_object_unref (acd->cancellable);
1070 g_slice_free (AsyncCloseData, acd);
1074 g_io_stream_close_async (gnutls->priv->base_io_stream,
1075 acd->io_priority, acd->cancellable,
1076 close_base_stream_cb, acd);
1083 g_tls_connection_gnutls_close_async (GIOStream *stream,
1085 GCancellable *cancellable,
1086 GAsyncReadyCallback callback,
1089 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (stream);
1090 GSimpleAsyncResult *simple;
1092 GError *error = NULL;
1093 AsyncCloseData *acd;
1096 simple = g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
1097 g_tls_connection_gnutls_close_async);
1099 success = close_internal (gnutls, FALSE, cancellable, &error);
1100 if (error && !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1102 g_simple_async_result_set_from_error (simple, error);
1103 g_error_free (error);
1104 g_simple_async_result_complete_in_idle (simple);
1105 g_object_unref (simple);
1109 g_error_free (error);
1111 acd = g_slice_new (AsyncCloseData);
1112 acd->simple = simple;
1113 acd->cancellable = g_object_ref (cancellable);
1114 acd->io_priority = io_priority;
1118 g_io_stream_close_async (gnutls->priv->base_io_stream,
1119 io_priority, cancellable,
1120 close_base_stream_cb, acd);
1124 source = g_tls_connection_gnutls_create_source (gnutls, 0, acd->cancellable);
1125 g_source_set_callback (source,
1126 (GSourceFunc) g_tls_connection_gnutls_close_ready,
1128 g_source_set_priority (source, acd->io_priority);
1129 g_source_attach (source, g_main_context_get_thread_default ());
1130 g_source_unref (source);
1134 g_tls_connection_gnutls_close_finish (GIOStream *stream,
1135 GAsyncResult *result,
1138 GSimpleAsyncResult *simple;
1140 g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (stream), g_tls_connection_gnutls_close_async), FALSE);
1142 simple = G_SIMPLE_ASYNC_RESULT (result);
1144 if (g_simple_async_result_propagate_error (simple, error))
1147 return g_simple_async_result_get_op_res_gboolean (simple);