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 "gnutls-marshal.h"
35 #include <glib/gi18n-lib.h>
37 static void g_tls_connection_gnutls_get_property (GObject *object,
41 static void g_tls_connection_gnutls_set_property (GObject *object,
45 static void g_tls_connection_gnutls_finalize (GObject *object);
47 static gboolean g_tls_connection_gnutls_handshake (GTlsConnection *connection,
48 GCancellable *cancellable,
50 static void g_tls_connection_gnutls_handshake_async (GTlsConnection *conn,
52 GCancellable *cancellable,
53 GAsyncReadyCallback callback,
55 static gboolean g_tls_connection_gnutls_handshake_finish (GTlsConnection *conn,
59 static GInputStream *g_tls_connection_gnutls_get_input_stream (GIOStream *stream);
60 static GOutputStream *g_tls_connection_gnutls_get_output_stream (GIOStream *stream);
62 static gboolean g_tls_connection_gnutls_close (GIOStream *stream,
63 GCancellable *cancellable,
65 static void g_tls_connection_gnutls_close_async (GIOStream *stream,
67 GCancellable *cancellable,
68 GAsyncReadyCallback callback,
70 static gboolean g_tls_connection_gnutls_close_finish (GIOStream *stream,
74 static ssize_t g_tls_connection_gnutls_push_func (gnutls_transport_ptr_t transport_data,
77 static ssize_t g_tls_connection_gnutls_pull_func (gnutls_transport_ptr_t transport_data,
81 static void g_tls_connection_gnutls_initable_iface_init (GInitableIface *iface);
82 static gboolean g_tls_connection_gnutls_initable_init (GInitable *initable,
83 GCancellable *cancellable,
86 static void g_tls_connection_gnutls_init_priorities (void);
88 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GTlsConnectionGnutls, g_tls_connection_gnutls, G_TYPE_TLS_CONNECTION,
89 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
90 g_tls_connection_gnutls_initable_iface_init);
91 g_tls_connection_gnutls_init_priorities ();
99 PROP_REQUIRE_CLOSE_NOTIFY,
100 PROP_REHANDSHAKE_MODE,
101 PROP_USE_SYSTEM_CERTDB,
103 PROP_PEER_CERTIFICATE,
104 PROP_PEER_CERTIFICATE_ERRORS
107 struct _GTlsConnectionGnutlsPrivate
109 GIOStream *base_io_stream;
110 GPollableInputStream *base_istream;
111 GPollableOutputStream *base_ostream;
114 gnutls_certificate_credentials creds;
115 gnutls_session session;
117 GTlsCertificate *certificate, *peer_certificate;
118 GTlsCertificateFlags peer_certificate_errors;
119 gboolean require_close_notify;
120 GTlsRehandshakeMode rehandshake_mode;
121 gboolean use_system_certdb;
122 gboolean need_handshake, handshaking, ever_handshaked;
125 GInputStream *tls_istream;
126 GOutputStream *tls_ostream;
129 GCancellable *cancellable;
130 gboolean blocking, eof;
131 GIOCondition internal_direction;
135 g_tls_connection_gnutls_class_init (GTlsConnectionGnutlsClass *klass)
137 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
138 GTlsConnectionClass *connection_class = G_TLS_CONNECTION_CLASS (klass);
139 GIOStreamClass *iostream_class = G_IO_STREAM_CLASS (klass);
141 g_type_class_add_private (klass, sizeof (GTlsConnectionGnutlsPrivate));
143 gobject_class->get_property = g_tls_connection_gnutls_get_property;
144 gobject_class->set_property = g_tls_connection_gnutls_set_property;
145 gobject_class->finalize = g_tls_connection_gnutls_finalize;
147 connection_class->handshake = g_tls_connection_gnutls_handshake;
148 connection_class->handshake_async = g_tls_connection_gnutls_handshake_async;
149 connection_class->handshake_finish = g_tls_connection_gnutls_handshake_finish;
151 iostream_class->get_input_stream = g_tls_connection_gnutls_get_input_stream;
152 iostream_class->get_output_stream = g_tls_connection_gnutls_get_output_stream;
153 iostream_class->close_fn = g_tls_connection_gnutls_close;
154 iostream_class->close_async = g_tls_connection_gnutls_close_async;
155 iostream_class->close_finish = g_tls_connection_gnutls_close_finish;
157 g_object_class_override_property (gobject_class, PROP_BASE_IO_STREAM, "base-io-stream");
158 g_object_class_override_property (gobject_class, PROP_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
159 g_object_class_override_property (gobject_class, PROP_REHANDSHAKE_MODE, "rehandshake-mode");
160 g_object_class_override_property (gobject_class, PROP_USE_SYSTEM_CERTDB, "use-system-certdb");
161 g_object_class_override_property (gobject_class, PROP_CERTIFICATE, "certificate");
162 g_object_class_override_property (gobject_class, PROP_PEER_CERTIFICATE, "peer-certificate");
163 g_object_class_override_property (gobject_class, PROP_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
167 g_tls_connection_gnutls_initable_iface_init (GInitableIface *iface)
169 iface->init = g_tls_connection_gnutls_initable_init;
173 g_tls_connection_gnutls_init (GTlsConnectionGnutls *gnutls)
175 gnutls->priv = G_TYPE_INSTANCE_GET_PRIVATE (gnutls, G_TYPE_TLS_CONNECTION_GNUTLS, GTlsConnectionGnutlsPrivate);
177 gnutls_certificate_allocate_credentials (&gnutls->priv->creds);
178 gnutls_certificate_set_verify_flags (gnutls->priv->creds,
179 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
181 gnutls->priv->need_handshake = TRUE;
184 static gnutls_priority_t priorities[2][2];
187 g_tls_connection_gnutls_init_priorities (void)
189 /* First field is "ssl3 only", second is "allow unsafe rehandshaking" */
191 gnutls_priority_init (&priorities[FALSE][FALSE],
194 gnutls_priority_init (&priorities[TRUE][FALSE],
195 "NORMAL:!VERS-TLS1.2:!VERS-TLS1.1:!VERS-TLS1.0",
197 gnutls_priority_init (&priorities[FALSE][TRUE],
198 "NORMAL:%UNSAFE_RENEGOTIATION",
200 gnutls_priority_init (&priorities[TRUE][TRUE],
201 "NORMAL:!VERS-TLS1.2:!VERS-TLS1.1:!VERS-TLS1.0:%UNSAFE_RENEGOTIATION",
206 g_tls_connection_gnutls_set_handshake_priority (GTlsConnectionGnutls *gnutls)
208 gboolean use_ssl3, unsafe_rehandshake;
210 if (G_IS_TLS_CLIENT_CONNECTION (gnutls))
211 use_ssl3 = g_tls_client_connection_get_use_ssl3 (G_TLS_CLIENT_CONNECTION (gnutls));
214 unsafe_rehandshake = (gnutls->priv->rehandshake_mode == G_TLS_REHANDSHAKE_UNSAFELY);
215 gnutls_priority_set (gnutls->priv->session,
216 priorities[use_ssl3][unsafe_rehandshake]);
220 g_tls_connection_gnutls_initable_init (GInitable *initable,
221 GCancellable *cancellable,
224 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (initable);
227 g_return_val_if_fail (gnutls->priv->base_istream != NULL &&
228 gnutls->priv->base_ostream != NULL, FALSE);
230 /* Make sure gnutls->priv->session has been initialized (it may have
231 * already been initialized by a construct-time property setter).
233 g_tls_connection_gnutls_get_session (gnutls);
235 status = gnutls_credentials_set (gnutls->priv->session,
236 GNUTLS_CRD_CERTIFICATE,
237 gnutls->priv->creds);
240 g_set_error (error, G_TLS_ERROR, G_TLS_ERROR_MISC,
241 _("Could not create TLS connection: %s"),
242 gnutls_strerror (status));
246 gnutls_transport_set_push_function (gnutls->priv->session,
247 g_tls_connection_gnutls_push_func);
248 gnutls_transport_set_pull_function (gnutls->priv->session,
249 g_tls_connection_gnutls_pull_func);
250 gnutls_transport_set_ptr (gnutls->priv->session, gnutls);
252 gnutls->priv->tls_istream = g_tls_input_stream_gnutls_new (gnutls);
253 gnutls->priv->tls_ostream = g_tls_output_stream_gnutls_new (gnutls);
259 g_tls_connection_gnutls_finalize (GObject *object)
261 GTlsConnectionGnutls *connection = G_TLS_CONNECTION_GNUTLS (object);
263 if (connection->priv->base_io_stream)
264 g_object_unref (connection->priv->base_io_stream);
266 if (connection->priv->session)
267 gnutls_deinit (connection->priv->session);
269 if (connection->priv->tls_istream)
270 g_object_unref (connection->priv->tls_istream);
271 if (connection->priv->tls_ostream)
272 g_object_unref (connection->priv->tls_ostream);
274 if (connection->priv->creds)
275 gnutls_certificate_free_credentials (connection->priv->creds);
277 if (connection->priv->certificate)
278 g_object_unref (connection->priv->certificate);
279 if (connection->priv->peer_certificate)
280 g_object_unref (connection->priv->peer_certificate);
282 if (connection->priv->error)
283 g_error_free (connection->priv->error);
285 G_OBJECT_CLASS (g_tls_connection_gnutls_parent_class)->finalize (object);
289 g_tls_connection_gnutls_get_property (GObject *object,
294 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (object);
298 case PROP_BASE_IO_STREAM:
299 g_value_set_object (value, gnutls->priv->base_io_stream);
302 case PROP_REQUIRE_CLOSE_NOTIFY:
303 g_value_set_boolean (value, gnutls->priv->require_close_notify);
306 case PROP_REHANDSHAKE_MODE:
307 g_value_set_enum (value, gnutls->priv->rehandshake_mode);
310 case PROP_USE_SYSTEM_CERTDB:
311 g_value_set_boolean (value, gnutls->priv->use_system_certdb);
314 case PROP_CERTIFICATE:
315 g_value_set_object (value, gnutls->priv->certificate);
318 case PROP_PEER_CERTIFICATE:
319 g_value_set_object (value, gnutls->priv->peer_certificate);
322 case PROP_PEER_CERTIFICATE_ERRORS:
323 g_value_set_flags (value, gnutls->priv->peer_certificate_errors);
327 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
332 g_tls_connection_gnutls_set_property (GObject *object,
337 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (object);
338 GInputStream *istream;
339 GOutputStream *ostream;
343 case PROP_BASE_IO_STREAM:
344 if (gnutls->priv->base_io_stream)
346 g_object_unref (gnutls->priv->base_io_stream);
347 gnutls->priv->base_istream = NULL;
348 gnutls->priv->base_ostream = NULL;
350 gnutls->priv->base_io_stream = g_value_dup_object (value);
351 if (!gnutls->priv->base_io_stream)
354 istream = g_io_stream_get_input_stream (gnutls->priv->base_io_stream);
355 ostream = g_io_stream_get_output_stream (gnutls->priv->base_io_stream);
357 if (G_IS_POLLABLE_INPUT_STREAM (istream) &&
358 g_pollable_input_stream_can_poll (G_POLLABLE_INPUT_STREAM (istream)))
359 gnutls->priv->base_istream = G_POLLABLE_INPUT_STREAM (istream);
360 if (G_IS_POLLABLE_OUTPUT_STREAM (ostream) &&
361 g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (ostream)))
362 gnutls->priv->base_ostream = G_POLLABLE_OUTPUT_STREAM (ostream);
365 case PROP_REQUIRE_CLOSE_NOTIFY:
366 gnutls->priv->require_close_notify = g_value_get_boolean (value);
369 case PROP_REHANDSHAKE_MODE:
370 gnutls->priv->rehandshake_mode = g_value_get_enum (value);
373 case PROP_USE_SYSTEM_CERTDB:
374 gnutls->priv->use_system_certdb = g_value_get_boolean (value);
376 gnutls_certificate_free_cas (gnutls->priv->creds);
377 if (gnutls->priv->use_system_certdb)
379 gnutls_x509_crt_t *cas;
382 g_tls_backend_gnutls_get_system_ca_list_gnutls (&cas, &num_cas);
383 gnutls_certificate_set_x509_trust (gnutls->priv->creds, cas, num_cas);
387 case PROP_CERTIFICATE:
388 if (gnutls->priv->certificate)
389 g_object_unref (gnutls->priv->certificate);
390 gnutls->priv->certificate = g_value_dup_object (value);
394 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
398 gnutls_certificate_credentials
399 g_tls_connection_gnutls_get_credentials (GTlsConnectionGnutls *gnutls)
401 return gnutls->priv->creds;
405 g_tls_connection_gnutls_get_session (GTlsConnectionGnutls *gnutls)
407 /* Ideally we would initialize gnutls->priv->session from
408 * g_tls_connection_gnutls_init(), but we can't tell if it's a
409 * client or server connection at that point... And
410 * g_tls_connection_gnutls_initiable_init() is too late, because
411 * construct-time property setters may need to modify it.
413 if (!gnutls->priv->session)
415 gboolean client = G_IS_TLS_CLIENT_CONNECTION (gnutls);
416 gnutls_init (&gnutls->priv->session, client ? GNUTLS_CLIENT : GNUTLS_SERVER);
419 return gnutls->priv->session;
423 g_tls_connection_gnutls_get_certificate (GTlsConnectionGnutls *gnutls,
426 GTlsCertificate *cert;
428 cert = g_tls_connection_get_certificate (G_TLS_CONNECTION (gnutls));
430 st->type = GNUTLS_CRT_X509;
433 GTlsCertificateGnutls *gnutlscert = G_TLS_CERTIFICATE_GNUTLS (cert);
436 st->cert.x509 = gnutls_malloc (sizeof (gnutls_x509_crt_t));
437 st->cert.x509[0] = g_tls_certificate_gnutls_copy_cert (gnutlscert);
438 st->key.x509 = g_tls_certificate_gnutls_copy_key (gnutlscert);
439 st->deinit_all = TRUE;
446 begin_gnutls_io (GTlsConnectionGnutls *gnutls,
448 GCancellable *cancellable)
450 gnutls->priv->blocking = blocking;
451 gnutls->priv->cancellable = cancellable;
452 gnutls->priv->internal_direction = 0;
453 g_clear_error (&gnutls->priv->error);
457 end_gnutls_io (GTlsConnectionGnutls *gnutls,
459 const char *generic_error,
462 gnutls->priv->cancellable = NULL;
466 g_clear_error (&gnutls->priv->error);
470 if (gnutls->priv->handshaking && !gnutls->priv->ever_handshaked)
472 if (g_error_matches (gnutls->priv->error, G_IO_ERROR, G_IO_ERROR_FAILED) ||
473 status == GNUTLS_E_UNEXPECTED_PACKET_LENGTH ||
474 status == GNUTLS_E_FATAL_ALERT_RECEIVED ||
475 status == GNUTLS_E_UNSUPPORTED_VERSION_PACKET)
477 g_clear_error (&gnutls->priv->error);
478 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_NOT_TLS,
479 _("Peer failed to perform TLS handshake"));
480 return GNUTLS_E_PULL_ERROR;
484 if (gnutls->priv->error)
486 if (g_error_matches (gnutls->priv->error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
487 status = GNUTLS_E_AGAIN;
488 g_propagate_error (error, gnutls->priv->error);
489 gnutls->priv->error = NULL;
492 else if (status == GNUTLS_E_REHANDSHAKE)
494 if (gnutls->priv->rehandshake_mode == G_TLS_REHANDSHAKE_NEVER)
496 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_MISC,
497 _("Peer requested illegal TLS rehandshake"));
498 return GNUTLS_E_PULL_ERROR;
501 gnutls->priv->need_handshake = TRUE;
504 else if (status == GNUTLS_E_UNEXPECTED_PACKET_LENGTH)
506 if (gnutls->priv->eof)
508 if (gnutls->priv->require_close_notify)
510 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_EOF,
511 _("TLS connection closed unexpectedly"));
519 g_set_error (error, G_TLS_ERROR, G_TLS_ERROR_MISC,
520 generic_error, gnutls_strerror (status));
524 #define BEGIN_GNUTLS_IO(gnutls, blocking, cancellable) \
525 begin_gnutls_io (gnutls, blocking, cancellable); \
528 #define END_GNUTLS_IO(gnutls, ret, errmsg, error) \
529 } while ((ret == GNUTLS_E_AGAIN || \
530 ret == GNUTLS_E_WARNING_ALERT_RECEIVED) && \
531 !gnutls->priv->error); \
532 ret = end_gnutls_io (gnutls, ret, errmsg, error)
535 g_tls_connection_gnutls_check (GTlsConnectionGnutls *gnutls,
536 GIOCondition condition)
538 if (gnutls->priv->handshaking || gnutls->priv->closing)
539 condition = gnutls->priv->internal_direction;
541 if (condition & G_IO_IN)
542 return g_pollable_input_stream_is_readable (gnutls->priv->base_istream);
544 return g_pollable_output_stream_is_writable (gnutls->priv->base_ostream);
550 GTlsConnectionGnutls *gnutls;
553 GSource *child_source;
554 GIOCondition base_direction;
555 GIOCondition current_direction;
556 } GTlsConnectionGnutlsSource;
559 gnutls_source_prepare (GSource *source,
567 gnutls_source_check (GSource *source)
573 gnutls_source_sync_child_source (GTlsConnectionGnutlsSource *gnutls_source,
574 GIOCondition direction)
576 GTlsConnectionGnutls *gnutls = gnutls_source->gnutls;
577 GSource *source = (GSource *)gnutls_source;
579 if (direction == gnutls_source->current_direction)
582 if (gnutls_source->child_source)
584 g_source_remove_child_source (source, gnutls_source->child_source);
585 g_source_unref (gnutls_source->child_source);
588 if (direction & G_IO_IN)
589 gnutls_source->child_source = g_pollable_input_stream_create_source (gnutls->priv->base_istream, NULL);
591 gnutls_source->child_source = g_pollable_output_stream_create_source (gnutls->priv->base_ostream, NULL);
593 g_source_set_dummy_callback (gnutls_source->child_source);
594 g_source_add_child_source (source, gnutls_source->child_source);
595 gnutls_source->current_direction = direction;
599 gnutls_source_dispatch (GSource *source,
600 GSourceFunc callback,
603 GPollableSourceFunc func = (GPollableSourceFunc)callback;
604 GTlsConnectionGnutlsSource *gnutls_source = (GTlsConnectionGnutlsSource *)source;
605 GTlsConnectionGnutls *gnutls = gnutls_source->gnutls;
608 ret = (*func) (gnutls_source->stream, user_data);
611 GIOCondition direction = gnutls->priv->internal_direction ? gnutls->priv->internal_direction : gnutls_source->base_direction;
613 gnutls_source_sync_child_source (gnutls_source, direction);
620 gnutls_source_finalize (GSource *source)
622 GTlsConnectionGnutlsSource *gnutls_source = (GTlsConnectionGnutlsSource *)source;
624 g_object_unref (gnutls_source->gnutls);
626 if (gnutls_source->child_source)
627 g_source_unref (gnutls_source->child_source);
631 g_tls_connection_gnutls_source_closure_callback (GObject *stream,
634 GClosure *closure = data;
636 GValue param = { 0, };
637 GValue result_value = { 0, };
640 g_value_init (&result_value, G_TYPE_BOOLEAN);
642 g_value_init (¶m, G_TYPE_OBJECT);
643 g_value_set_object (¶m, stream);
645 g_closure_invoke (closure, &result_value, 1, ¶m, NULL);
647 result = g_value_get_boolean (&result_value);
648 g_value_unset (&result_value);
649 g_value_unset (¶m);
654 static GSourceFuncs gnutls_source_funcs =
656 gnutls_source_prepare,
658 gnutls_source_dispatch,
659 gnutls_source_finalize,
660 (GSourceFunc)g_tls_connection_gnutls_source_closure_callback,
661 (GSourceDummyMarshal)_gnutls_marshal_BOOLEAN__VOID,
665 g_tls_connection_gnutls_create_source (GTlsConnectionGnutls *gnutls,
666 GIOCondition condition,
667 GCancellable *cancellable)
669 GSource *source, *cancellable_source;
670 GTlsConnectionGnutlsSource *gnutls_source;
672 source = g_source_new (&gnutls_source_funcs, sizeof (GTlsConnectionGnutlsSource));
673 g_source_set_name (source, "GTlsConnectionGnutlsSource");
674 gnutls_source = (GTlsConnectionGnutlsSource *)source;
675 gnutls_source->gnutls = g_object_ref (gnutls);
676 gnutls_source->base_direction = condition & (G_IO_IN | G_IO_OUT);
677 if (gnutls_source->base_direction == G_IO_IN)
678 gnutls_source->stream = G_OBJECT (gnutls->priv->tls_istream);
679 else if (gnutls_source->base_direction == G_IO_OUT)
680 gnutls_source->stream = G_OBJECT (gnutls->priv->tls_ostream);
683 gnutls_source->base_direction = gnutls->priv->internal_direction;
684 gnutls_source->stream = NULL;
686 gnutls_source_sync_child_source (gnutls_source, gnutls_source->base_direction);
690 cancellable_source = g_cancellable_source_new (cancellable);
691 g_source_set_dummy_callback (cancellable_source);
692 g_source_add_child_source (source, cancellable_source);
693 g_source_unref (cancellable_source);
700 set_gnutls_error (GTlsConnectionGnutls *gnutls, GIOCondition direction)
702 if (g_error_matches (gnutls->priv->error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
703 gnutls_transport_set_errno (gnutls->priv->session, EINTR);
704 else if (g_error_matches (gnutls->priv->error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
706 gnutls_transport_set_errno (gnutls->priv->session, EAGAIN);
707 gnutls->priv->internal_direction = direction;
710 gnutls_transport_set_errno (gnutls->priv->session, EIO);
714 g_tls_connection_gnutls_pull_func (gnutls_transport_ptr_t transport_data,
718 GTlsConnectionGnutls *gnutls = transport_data;
721 /* If gnutls->priv->error is non-%NULL when we're called, it means
722 * that an error previously occurred, but gnutls decided not to
723 * propagate it. So it's correct for us to just clear it. (Usually
724 * this means it ignored an EAGAIN after a short read, and now
725 * we'll return EAGAIN again, which it will obey this time.)
727 g_clear_error (&gnutls->priv->error);
729 if (gnutls->priv->blocking)
731 ret = g_input_stream_read (G_INPUT_STREAM (gnutls->priv->base_istream),
733 gnutls->priv->cancellable,
734 &gnutls->priv->error);
738 ret = g_pollable_input_stream_read_nonblocking (gnutls->priv->base_istream,
740 gnutls->priv->cancellable,
741 &gnutls->priv->error);
745 set_gnutls_error (gnutls, G_IO_IN);
747 gnutls->priv->eof = TRUE;
753 g_tls_connection_gnutls_push_func (gnutls_transport_ptr_t transport_data,
757 GTlsConnectionGnutls *gnutls = transport_data;
760 /* See comment in pull_func. */
761 g_clear_error (&gnutls->priv->error);
763 if (gnutls->priv->blocking)
765 ret = g_output_stream_write (G_OUTPUT_STREAM (gnutls->priv->base_ostream),
767 gnutls->priv->cancellable,
768 &gnutls->priv->error);
772 ret = g_pollable_output_stream_write_nonblocking (gnutls->priv->base_ostream,
774 gnutls->priv->cancellable,
775 &gnutls->priv->error);
778 set_gnutls_error (gnutls, G_IO_OUT);
784 handshake_internal (GTlsConnectionGnutls *gnutls,
786 GCancellable *cancellable,
789 GTlsCertificate *peer_certificate;
790 GTlsCertificateFlags peer_certificate_errors;
793 if (G_IS_TLS_SERVER_CONNECTION_GNUTLS (gnutls) &&
794 gnutls->priv->ever_handshaked && !gnutls->priv->need_handshake)
796 BEGIN_GNUTLS_IO (gnutls, blocking, cancellable);
797 ret = gnutls_rehandshake (gnutls->priv->session);
798 END_GNUTLS_IO (gnutls, ret, _("Error performing TLS handshake: %s"), error);
804 if (!gnutls->priv->handshaking)
806 gnutls->priv->handshaking = TRUE;
808 if (gnutls->priv->peer_certificate)
810 g_object_unref (gnutls->priv->peer_certificate);
811 gnutls->priv->peer_certificate = NULL;
812 gnutls->priv->peer_certificate_errors = 0;
814 g_object_notify (G_OBJECT (gnutls), "peer-certificate");
815 g_object_notify (G_OBJECT (gnutls), "peer-certificate-errors");
818 g_tls_connection_gnutls_set_handshake_priority (gnutls);
819 G_TLS_CONNECTION_GNUTLS_GET_CLASS (gnutls)->begin_handshake (gnutls);
822 BEGIN_GNUTLS_IO (gnutls, blocking, cancellable);
823 ret = gnutls_handshake (gnutls->priv->session);
824 END_GNUTLS_IO (gnutls, ret, _("Error performing TLS handshake: %s"), error);
826 if (ret == GNUTLS_E_AGAIN)
829 gnutls->priv->handshaking = FALSE;
830 gnutls->priv->need_handshake = FALSE;
831 gnutls->priv->ever_handshaked = TRUE;
834 gnutls_certificate_type_get (gnutls->priv->session) == GNUTLS_CRT_X509)
836 GTlsCertificate *chain, *cert;
837 const gnutls_datum_t *certs;
838 unsigned int num_certs;
841 certs = gnutls_certificate_get_peers (gnutls->priv->session, &num_certs);
845 for (i = num_certs - 1; i >= 0; i--)
847 cert = g_tls_certificate_gnutls_new (&certs[i], chain);
852 peer_certificate = chain;
855 peer_certificate = NULL;
857 if (peer_certificate)
861 status = gnutls_certificate_verify_peers (gnutls->priv->session);
862 peer_certificate_errors = g_tls_certificate_gnutls_convert_flags (status);
863 if (peer_certificate_errors)
865 /* gnutls_certificate_verify_peers() bails out on the first
866 * error, which may be G_TLS_CERTIFICATE_UNKNOWN_CA, but the
867 * caller may be planning to check that part themselves. So
868 * call g_tls_certificate_verify() to get any other errors.
870 peer_certificate_errors |= g_tls_certificate_verify (peer_certificate, NULL, NULL);
873 if (!G_TLS_CONNECTION_GNUTLS_GET_CLASS (gnutls)->verify_peer (gnutls, peer_certificate, &peer_certificate_errors))
875 g_object_unref (peer_certificate);
876 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
877 _("Unacceptable TLS certificate"));
882 G_TLS_CONNECTION_GNUTLS_GET_CLASS (gnutls)->finish_handshake (gnutls, error);
886 gnutls->priv->peer_certificate = peer_certificate;
887 gnutls->priv->peer_certificate_errors = peer_certificate_errors;
889 g_object_notify (G_OBJECT (gnutls), "peer-certificate");
890 g_object_notify (G_OBJECT (gnutls), "peer-certificate-errors");
899 handshake_in_progress_or_failed (GTlsConnectionGnutls *gnutls,
901 GCancellable *cancellable,
904 if (!(gnutls->priv->need_handshake || gnutls->priv->handshaking))
907 return !handshake_internal (gnutls, blocking, cancellable, error);
911 g_tls_connection_gnutls_handshake (GTlsConnection *conn,
912 GCancellable *cancellable,
915 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (conn);
917 return handshake_internal (gnutls, TRUE, cancellable, error);
921 g_tls_connection_gnutls_handshake_ready (GObject *pollable_stream,
924 GTlsConnectionGnutls *gnutls;
925 GSimpleAsyncResult *simple = user_data;
927 GError *error = NULL;
929 gnutls = G_TLS_CONNECTION_GNUTLS (g_async_result_get_source_object (G_ASYNC_RESULT (simple)));
930 g_object_unref (gnutls);
932 success = handshake_internal (gnutls, FALSE, NULL, &error);
933 if (!success && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
935 g_error_free (error);
941 g_simple_async_result_set_from_error (simple, error);
942 g_error_free (error);
945 g_simple_async_result_set_op_res_gboolean (simple, success);
946 g_simple_async_result_complete (simple);
947 g_object_unref (simple);
953 g_tls_connection_gnutls_handshake_async (GTlsConnection *conn,
955 GCancellable *cancellable,
956 GAsyncReadyCallback callback,
959 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (conn);
960 GSimpleAsyncResult *simple;
962 GError *error = NULL;
965 simple = g_simple_async_result_new (G_OBJECT (conn), callback, user_data,
966 g_tls_connection_gnutls_handshake_async);
967 success = handshake_internal (gnutls, FALSE, cancellable, &error);
970 g_simple_async_result_set_op_res_gboolean (simple, TRUE);
971 g_simple_async_result_complete_in_idle (simple);
972 g_object_unref (simple);
974 else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
976 g_simple_async_result_set_from_error (simple, error);
977 g_error_free (error);
978 g_simple_async_result_complete_in_idle (simple);
979 g_object_unref (simple);
982 source = g_tls_connection_gnutls_create_source (gnutls, 0, cancellable);
983 g_source_set_callback (source,
984 (GSourceFunc) g_tls_connection_gnutls_handshake_ready,
986 g_source_set_priority (source, io_priority);
987 g_source_attach (source, g_main_context_get_thread_default ());
988 g_source_unref (source);
992 g_tls_connection_gnutls_handshake_finish (GTlsConnection *conn,
993 GAsyncResult *result,
996 GSimpleAsyncResult *simple;
998 g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (conn), g_tls_connection_gnutls_handshake_async), FALSE);
1000 simple = G_SIMPLE_ASYNC_RESULT (result);
1002 if (g_simple_async_result_propagate_error (simple, error))
1005 return g_simple_async_result_get_op_res_gboolean (simple);
1009 g_tls_connection_gnutls_read (GTlsConnectionGnutls *gnutls,
1013 GCancellable *cancellable,
1019 if (handshake_in_progress_or_failed (gnutls, blocking, cancellable, error))
1022 BEGIN_GNUTLS_IO (gnutls, blocking, cancellable);
1023 ret = gnutls_record_recv (gnutls->priv->session, buffer, count);
1024 END_GNUTLS_IO (gnutls, ret, _("Error reading data from TLS socket: %s"), error);
1028 else if (ret == GNUTLS_E_REHANDSHAKE)
1035 g_tls_connection_gnutls_write (GTlsConnectionGnutls *gnutls,
1039 GCancellable *cancellable,
1045 if (handshake_in_progress_or_failed (gnutls, blocking, cancellable, error))
1048 BEGIN_GNUTLS_IO (gnutls, blocking, cancellable);
1049 ret = gnutls_record_send (gnutls->priv->session, buffer, count);
1050 END_GNUTLS_IO (gnutls, ret, _("Error writing data to TLS socket: %s"), error);
1054 else if (ret == GNUTLS_E_REHANDSHAKE)
1060 static GInputStream *
1061 g_tls_connection_gnutls_get_input_stream (GIOStream *stream)
1063 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (stream);
1065 return gnutls->priv->tls_istream;
1068 static GOutputStream *
1069 g_tls_connection_gnutls_get_output_stream (GIOStream *stream)
1071 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (stream);
1073 return gnutls->priv->tls_ostream;
1077 close_internal (GTlsConnectionGnutls *gnutls,
1079 GCancellable *cancellable,
1084 /* If we haven't finished the initial handshake yet, there's no
1085 * reason to finish it just so we can close.
1087 if (gnutls->priv->handshaking && !gnutls->priv->ever_handshaked)
1090 if (handshake_in_progress_or_failed (gnutls, blocking, cancellable, error))
1093 gnutls->priv->closing = TRUE;
1094 BEGIN_GNUTLS_IO (gnutls, blocking, cancellable);
1095 ret = gnutls_bye (gnutls->priv->session, GNUTLS_SHUT_WR);
1096 END_GNUTLS_IO (gnutls, ret, _("Error performing TLS close: %s"), error);
1097 if (ret == 0 || !error || !g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1098 gnutls->priv->closing = FALSE;
1104 g_tls_connection_gnutls_close (GIOStream *stream,
1105 GCancellable *cancellable,
1108 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (stream);
1110 if (!close_internal (gnutls, TRUE, cancellable, error))
1112 return g_io_stream_close (gnutls->priv->base_io_stream,
1113 cancellable, error);
1117 GSimpleAsyncResult *simple;
1118 GCancellable *cancellable;
1123 close_base_stream_cb (GObject *base_stream,
1124 GAsyncResult *result,
1128 GError *error = NULL;
1129 AsyncCloseData *acd = user_data;
1131 success = g_io_stream_close_finish (G_IO_STREAM (base_stream),
1134 g_simple_async_result_set_op_res_gboolean (acd->simple, TRUE);
1137 g_simple_async_result_set_from_error (acd->simple, error);
1138 g_error_free (error);
1141 g_simple_async_result_complete (acd->simple);
1142 g_object_unref (acd->simple);
1143 if (acd->cancellable)
1144 g_object_unref (acd->cancellable);
1145 g_slice_free (AsyncCloseData, acd);
1149 g_tls_connection_gnutls_close_ready (GObject *pollable_stream,
1152 GTlsConnectionGnutls *gnutls;
1153 AsyncCloseData *acd = user_data;
1155 GError *error = NULL;
1157 gnutls = G_TLS_CONNECTION_GNUTLS (g_async_result_get_source_object (G_ASYNC_RESULT (acd->simple)));
1158 g_object_unref (gnutls);
1160 success = close_internal (gnutls, FALSE, NULL, &error);
1161 if (!success && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1163 g_error_free (error);
1169 g_simple_async_result_set_from_error (acd->simple, error);
1170 g_simple_async_result_complete (acd->simple);
1171 g_error_free (error);
1172 g_object_unref (acd->simple);
1173 if (acd->cancellable)
1174 g_object_unref (acd->cancellable);
1175 g_slice_free (AsyncCloseData, acd);
1179 g_io_stream_close_async (gnutls->priv->base_io_stream,
1180 acd->io_priority, acd->cancellable,
1181 close_base_stream_cb, acd);
1188 g_tls_connection_gnutls_close_async (GIOStream *stream,
1190 GCancellable *cancellable,
1191 GAsyncReadyCallback callback,
1194 GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (stream);
1195 GSimpleAsyncResult *simple;
1197 GError *error = NULL;
1198 AsyncCloseData *acd;
1201 simple = g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
1202 g_tls_connection_gnutls_close_async);
1204 success = close_internal (gnutls, FALSE, cancellable, &error);
1205 if (error && !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1207 g_simple_async_result_set_from_error (simple, error);
1208 g_error_free (error);
1209 g_simple_async_result_complete_in_idle (simple);
1210 g_object_unref (simple);
1214 g_error_free (error);
1216 acd = g_slice_new (AsyncCloseData);
1217 acd->simple = simple;
1218 acd->cancellable = g_object_ref (cancellable);
1219 acd->io_priority = io_priority;
1223 g_io_stream_close_async (gnutls->priv->base_io_stream,
1224 io_priority, cancellable,
1225 close_base_stream_cb, acd);
1229 source = g_tls_connection_gnutls_create_source (gnutls, 0, acd->cancellable);
1230 g_source_set_callback (source,
1231 (GSourceFunc) g_tls_connection_gnutls_close_ready,
1233 g_source_set_priority (source, acd->io_priority);
1234 g_source_attach (source, g_main_context_get_thread_default ());
1235 g_source_unref (source);
1239 g_tls_connection_gnutls_close_finish (GIOStream *stream,
1240 GAsyncResult *result,
1243 GSimpleAsyncResult *simple;
1245 g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (stream), g_tls_connection_gnutls_close_async), FALSE);
1247 simple = G_SIMPLE_ASYNC_RESULT (result);
1249 if (g_simple_async_result_propagate_error (simple, error))
1252 return g_simple_async_result_get_op_res_gboolean (simple);