Blame gio/gtlsconnection.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright © 2010 Red Hat, Inc
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include "glib.h"
Packit ae235b
Packit ae235b
#include "gtlsconnection.h"
Packit ae235b
#include "gcancellable.h"
Packit ae235b
#include "gioenumtypes.h"
Packit ae235b
#include "gsocket.h"
Packit ae235b
#include "gtlsbackend.h"
Packit ae235b
#include "gtlscertificate.h"
Packit ae235b
#include "gtlsclientconnection.h"
Packit ae235b
#include "gtlsdatabase.h"
Packit ae235b
#include "gtlsinteraction.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gtlsconnection
Packit ae235b
 * @short_description: TLS connection type
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * #GTlsConnection is the base TLS connection class type, which wraps
Packit ae235b
 * a #GIOStream and provides TLS encryption on top of it. Its
Packit ae235b
 * subclasses, #GTlsClientConnection and #GTlsServerConnection,
Packit ae235b
 * implement client-side and server-side TLS, respectively.
Packit ae235b
 *
Packit ae235b
 * For DTLS (Datagram TLS) support, see #GDtlsConnection.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTlsConnection:
Packit ae235b
 *
Packit ae235b
 * Abstract base class for the backend-specific #GTlsClientConnection
Packit ae235b
 * and #GTlsServerConnection types.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
Packit ae235b
G_DEFINE_ABSTRACT_TYPE (GTlsConnection, g_tls_connection, G_TYPE_IO_STREAM)
Packit ae235b
Packit ae235b
static void g_tls_connection_get_property (GObject    *object,
Packit ae235b
					   guint       prop_id,
Packit ae235b
					   GValue     *value,
Packit ae235b
					   GParamSpec *pspec);
Packit ae235b
static void g_tls_connection_set_property (GObject      *object,
Packit ae235b
					   guint         prop_id,
Packit ae235b
					   const GValue *value,
Packit ae235b
					   GParamSpec   *pspec);
Packit ae235b
Packit ae235b
enum {
Packit ae235b
  ACCEPT_CERTIFICATE,
Packit ae235b
Packit ae235b
  LAST_SIGNAL
Packit ae235b
};
Packit ae235b
Packit ae235b
static guint signals[LAST_SIGNAL] = { 0 };
Packit ae235b
Packit ae235b
enum {
Packit ae235b
  PROP_0,
Packit ae235b
  PROP_BASE_IO_STREAM,
Packit ae235b
  PROP_REQUIRE_CLOSE_NOTIFY,
Packit ae235b
  PROP_REHANDSHAKE_MODE,
Packit ae235b
  PROP_USE_SYSTEM_CERTDB,
Packit ae235b
  PROP_DATABASE,
Packit ae235b
  PROP_INTERACTION,
Packit ae235b
  PROP_CERTIFICATE,
Packit ae235b
  PROP_PEER_CERTIFICATE,
Packit ae235b
  PROP_PEER_CERTIFICATE_ERRORS
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_connection_class_init (GTlsConnectionClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->get_property = g_tls_connection_get_property;
Packit ae235b
  gobject_class->set_property = g_tls_connection_set_property;
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GTlsConnection:base-io-stream:
Packit ae235b
   *
Packit ae235b
   * The #GIOStream that the connection wraps. The connection holds a reference
Packit ae235b
   * to this stream, and may run operations on the stream from other threads
Packit ae235b
   * throughout its lifetime. Consequently, after the #GIOStream has been
Packit ae235b
   * constructed, application code may only run its own operations on this
Packit ae235b
   * stream when no #GIOStream operations are running.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_BASE_IO_STREAM,
Packit ae235b
				   g_param_spec_object ("base-io-stream",
Packit ae235b
							P_("Base IOStream"),
Packit ae235b
							P_("The GIOStream that the connection wraps"),
Packit ae235b
							G_TYPE_IO_STREAM,
Packit ae235b
							G_PARAM_READWRITE |
Packit ae235b
							G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
							G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GTlsConnection:use-system-certdb:
Packit ae235b
   *
Packit ae235b
   * Whether or not the system certificate database will be used to
Packit ae235b
   * verify peer certificates. See
Packit ae235b
   * g_tls_connection_set_use_system_certdb().
Packit ae235b
   *
Packit ae235b
   * Deprecated: 2.30: Use GTlsConnection:database instead
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_USE_SYSTEM_CERTDB,
Packit ae235b
				   g_param_spec_boolean ("use-system-certdb",
Packit ae235b
							 P_("Use system certificate database"),
Packit ae235b
							 P_("Whether to verify peer certificates against the system certificate database"),
Packit ae235b
							 TRUE,
Packit ae235b
							 G_PARAM_READWRITE |
Packit ae235b
							 G_PARAM_CONSTRUCT |
Packit ae235b
							 G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GTlsConnection:database:
Packit ae235b
   *
Packit ae235b
   * The certificate database to use when verifying this TLS connection.
Packit ae235b
   * If no certificate database is set, then the default database will be
Packit ae235b
   * used. See g_tls_backend_get_default_database().
Packit ae235b
   *
Packit ae235b
   * Since: 2.30
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_DATABASE,
Packit ae235b
				   g_param_spec_object ("database",
Packit ae235b
							 P_("Database"),
Packit ae235b
							 P_("Certificate database to use for looking up or verifying certificates"),
Packit ae235b
							 G_TYPE_TLS_DATABASE,
Packit ae235b
							 G_PARAM_READWRITE |
Packit ae235b
							 G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GTlsConnection:interaction:
Packit ae235b
   *
Packit ae235b
   * A #GTlsInteraction object to be used when the connection or certificate
Packit ae235b
   * database need to interact with the user. This will be used to prompt the
Packit ae235b
   * user for passwords where necessary.
Packit ae235b
   *
Packit ae235b
   * Since: 2.30
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_INTERACTION,
Packit ae235b
                                   g_param_spec_object ("interaction",
Packit ae235b
                                                        P_("Interaction"),
Packit ae235b
                                                        P_("Optional object for user interaction"),
Packit ae235b
                                                        G_TYPE_TLS_INTERACTION,
Packit ae235b
                                                        G_PARAM_READWRITE |
Packit ae235b
                                                        G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GTlsConnection:require-close-notify:
Packit ae235b
   *
Packit ae235b
   * Whether or not proper TLS close notification is required.
Packit ae235b
   * See g_tls_connection_set_require_close_notify().
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_REQUIRE_CLOSE_NOTIFY,
Packit ae235b
				   g_param_spec_boolean ("require-close-notify",
Packit ae235b
							 P_("Require close notify"),
Packit ae235b
							 P_("Whether to require proper TLS close notification"),
Packit ae235b
							 TRUE,
Packit ae235b
							 G_PARAM_READWRITE |
Packit ae235b
							 G_PARAM_CONSTRUCT |
Packit ae235b
							 G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GTlsConnection:rehandshake-mode:
Packit ae235b
   *
Packit ae235b
   * The rehandshaking mode. See
Packit ae235b
   * g_tls_connection_set_rehandshake_mode().
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_REHANDSHAKE_MODE,
Packit ae235b
				   g_param_spec_enum ("rehandshake-mode",
Packit ae235b
						      P_("Rehandshake mode"),
Packit ae235b
						      P_("When to allow rehandshaking"),
Packit ae235b
						      G_TYPE_TLS_REHANDSHAKE_MODE,
Packit ae235b
						      G_TLS_REHANDSHAKE_SAFELY,
Packit ae235b
						      G_PARAM_READWRITE |
Packit ae235b
						      G_PARAM_CONSTRUCT |
Packit ae235b
						      G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GTlsConnection:certificate:
Packit ae235b
   *
Packit ae235b
   * The connection's certificate; see
Packit ae235b
   * g_tls_connection_set_certificate().
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_CERTIFICATE,
Packit ae235b
				   g_param_spec_object ("certificate",
Packit ae235b
							P_("Certificate"),
Packit ae235b
							P_("The connection’s certificate"),
Packit ae235b
							G_TYPE_TLS_CERTIFICATE,
Packit ae235b
							G_PARAM_READWRITE |
Packit ae235b
							G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GTlsConnection:peer-certificate:
Packit ae235b
   *
Packit ae235b
   * The connection's peer's certificate, after the TLS handshake has
Packit ae235b
   * completed and the certificate has been accepted. Note in
Packit ae235b
   * particular that this is not yet set during the emission of
Packit ae235b
   * #GTlsConnection::accept-certificate.
Packit ae235b
   *
Packit ae235b
   * (You can watch for a #GObject::notify signal on this property to
Packit ae235b
   * detect when a handshake has occurred.)
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_PEER_CERTIFICATE,
Packit ae235b
				   g_param_spec_object ("peer-certificate",
Packit ae235b
							P_("Peer Certificate"),
Packit ae235b
							P_("The connection’s peer’s certificate"),
Packit ae235b
							G_TYPE_TLS_CERTIFICATE,
Packit ae235b
							G_PARAM_READABLE |
Packit ae235b
							G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GTlsConnection:peer-certificate-errors:
Packit ae235b
   *
Packit ae235b
   * The errors noticed-and-ignored while verifying
Packit ae235b
   * #GTlsConnection:peer-certificate. Normally this should be 0, but
Packit ae235b
   * it may not be if #GTlsClientConnection:validation-flags is not
Packit ae235b
   * %G_TLS_CERTIFICATE_VALIDATE_ALL, or if
Packit ae235b
   * #GTlsConnection::accept-certificate overrode the default
Packit ae235b
   * behavior.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_PEER_CERTIFICATE_ERRORS,
Packit ae235b
				   g_param_spec_flags ("peer-certificate-errors",
Packit ae235b
						       P_("Peer Certificate Errors"),
Packit ae235b
						       P_("Errors found with the peer’s certificate"),
Packit ae235b
						       G_TYPE_TLS_CERTIFICATE_FLAGS,
Packit ae235b
						       0,
Packit ae235b
						       G_PARAM_READABLE |
Packit ae235b
						       G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GTlsConnection::accept-certificate:
Packit ae235b
   * @conn: a #GTlsConnection
Packit ae235b
   * @peer_cert: the peer's #GTlsCertificate
Packit ae235b
   * @errors: the problems with @peer_cert.
Packit ae235b
   *
Packit ae235b
   * Emitted during the TLS handshake after the peer certificate has
Packit ae235b
   * been received. You can examine @peer_cert's certification path by
Packit ae235b
   * calling g_tls_certificate_get_issuer() on it.
Packit ae235b
   *
Packit ae235b
   * For a client-side connection, @peer_cert is the server's
Packit ae235b
   * certificate, and the signal will only be emitted if the
Packit ae235b
   * certificate was not acceptable according to @conn's
Packit ae235b
   * #GTlsClientConnection:validation_flags. If you would like the
Packit ae235b
   * certificate to be accepted despite @errors, return %TRUE from the
Packit ae235b
   * signal handler. Otherwise, if no handler accepts the certificate,
Packit ae235b
   * the handshake will fail with %G_TLS_ERROR_BAD_CERTIFICATE.
Packit ae235b
   *
Packit ae235b
   * For a server-side connection, @peer_cert is the certificate
Packit ae235b
   * presented by the client, if this was requested via the server's
Packit ae235b
   * #GTlsServerConnection:authentication_mode. On the server side,
Packit ae235b
   * the signal is always emitted when the client presents a
Packit ae235b
   * certificate, and the certificate will only be accepted if a
Packit ae235b
   * handler returns %TRUE.
Packit ae235b
   *
Packit ae235b
   * Note that if this signal is emitted as part of asynchronous I/O
Packit ae235b
   * in the main thread, then you should not attempt to interact with
Packit ae235b
   * the user before returning from the signal handler. If you want to
Packit ae235b
   * let the user decide whether or not to accept the certificate, you
Packit ae235b
   * would have to return %FALSE from the signal handler on the first
Packit ae235b
   * attempt, and then after the connection attempt returns a
Packit ae235b
   * %G_TLS_ERROR_HANDSHAKE, you can interact with the user, and if
Packit ae235b
   * the user decides to accept the certificate, remember that fact,
Packit ae235b
   * create a new connection, and return %TRUE from the signal handler
Packit ae235b
   * the next time.
Packit ae235b
   *
Packit ae235b
   * If you are doing I/O in another thread, you do not
Packit ae235b
   * need to worry about this, and can simply block in the signal
Packit ae235b
   * handler until the UI thread returns an answer.
Packit ae235b
   *
Packit ae235b
   * Returns: %TRUE to accept @peer_cert (which will also
Packit ae235b
   * immediately end the signal emission). %FALSE to allow the signal
Packit ae235b
   * emission to continue, which will cause the handshake to fail if
Packit ae235b
   * no one else overrides it.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  signals[ACCEPT_CERTIFICATE] =
Packit ae235b
    g_signal_new (I_("accept-certificate"),
Packit ae235b
		  G_TYPE_TLS_CONNECTION,
Packit ae235b
		  G_SIGNAL_RUN_LAST,
Packit ae235b
		  G_STRUCT_OFFSET (GTlsConnectionClass, accept_certificate),
Packit ae235b
		  g_signal_accumulator_true_handled, NULL,
Packit ae235b
		  NULL,
Packit ae235b
		  G_TYPE_BOOLEAN, 2,
Packit ae235b
		  G_TYPE_TLS_CERTIFICATE,
Packit ae235b
		  G_TYPE_TLS_CERTIFICATE_FLAGS);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_connection_init (GTlsConnection *conn)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_connection_get_property (GObject    *object,
Packit ae235b
			       guint       prop_id,
Packit ae235b
			       GValue     *value,
Packit ae235b
			       GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_connection_set_property (GObject      *object,
Packit ae235b
			       guint         prop_id,
Packit ae235b
			       const GValue *value,
Packit ae235b
			       GParamSpec   *pspec)
Packit ae235b
{
Packit ae235b
  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_set_use_system_certdb:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 * @use_system_certdb: whether to use the system certificate database
Packit ae235b
 *
Packit ae235b
 * Sets whether @conn uses the system certificate database to verify
Packit ae235b
 * peer certificates. This is %TRUE by default. If set to %FALSE, then
Packit ae235b
 * peer certificate validation will always set the
Packit ae235b
 * %G_TLS_CERTIFICATE_UNKNOWN_CA error (meaning
Packit ae235b
 * #GTlsConnection::accept-certificate will always be emitted on
Packit ae235b
 * client-side connections, unless that bit is not set in
Packit ae235b
 * #GTlsClientConnection:validation-flags).
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.30: Use g_tls_connection_set_database() instead
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_connection_set_use_system_certdb (GTlsConnection *conn,
Packit ae235b
					gboolean        use_system_certdb)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TLS_CONNECTION (conn));
Packit ae235b
Packit ae235b
  g_object_set (G_OBJECT (conn),
Packit ae235b
		"use-system-certdb", use_system_certdb,
Packit ae235b
		NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_get_use_system_certdb:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 *
Packit ae235b
 * Gets whether @conn uses the system certificate database to verify
Packit ae235b
 * peer certificates. See g_tls_connection_set_use_system_certdb().
Packit ae235b
 *
Packit ae235b
 * Returns: whether @conn uses the system certificate database
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.30: Use g_tls_connection_get_database() instead
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_tls_connection_get_use_system_certdb (GTlsConnection *conn)
Packit ae235b
{
Packit ae235b
  gboolean use_system_certdb;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), TRUE);
Packit ae235b
Packit ae235b
  g_object_get (G_OBJECT (conn),
Packit ae235b
		"use-system-certdb", &use_system_certdb,
Packit ae235b
		NULL);
Packit ae235b
  return use_system_certdb;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_set_database:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 * @database: a #GTlsDatabase
Packit ae235b
 *
Packit ae235b
 * Sets the certificate database that is used to verify peer certificates.
Packit ae235b
 * This is set to the default database by default. See
Packit ae235b
 * g_tls_backend_get_default_database(). If set to %NULL, then
Packit ae235b
 * peer certificate validation will always set the
Packit ae235b
 * %G_TLS_CERTIFICATE_UNKNOWN_CA error (meaning
Packit ae235b
 * #GTlsConnection::accept-certificate will always be emitted on
Packit ae235b
 * client-side connections, unless that bit is not set in
Packit ae235b
 * #GTlsClientConnection:validation-flags).
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_connection_set_database (GTlsConnection *conn,
Packit ae235b
                               GTlsDatabase   *database)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TLS_CONNECTION (conn));
Packit ae235b
  g_return_if_fail (database == NULL || G_IS_TLS_DATABASE (database));
Packit ae235b
Packit ae235b
  g_object_set (G_OBJECT (conn),
Packit ae235b
		"database", database,
Packit ae235b
		NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_get_database:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 *
Packit ae235b
 * Gets the certificate database that @conn uses to verify
Packit ae235b
 * peer certificates. See g_tls_connection_set_database().
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the certificate database that @conn uses or %NULL
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
GTlsDatabase*
Packit ae235b
g_tls_connection_get_database (GTlsConnection *conn)
Packit ae235b
{
Packit ae235b
  GTlsDatabase *database = NULL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
Packit ae235b
Packit ae235b
  g_object_get (G_OBJECT (conn),
Packit ae235b
		"database", &database,
Packit ae235b
		NULL);
Packit ae235b
  if (database)
Packit ae235b
    g_object_unref (database);
Packit ae235b
  return database;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_set_certificate:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 * @certificate: the certificate to use for @conn
Packit ae235b
 *
Packit ae235b
 * This sets the certificate that @conn will present to its peer
Packit ae235b
 * during the TLS handshake. For a #GTlsServerConnection, it is
Packit ae235b
 * mandatory to set this, and that will normally be done at construct
Packit ae235b
 * time.
Packit ae235b
 *
Packit ae235b
 * For a #GTlsClientConnection, this is optional. If a handshake fails
Packit ae235b
 * with %G_TLS_ERROR_CERTIFICATE_REQUIRED, that means that the server
Packit ae235b
 * requires a certificate, and if you try connecting again, you should
Packit ae235b
 * call this method first. You can call
Packit ae235b
 * g_tls_client_connection_get_accepted_cas() on the failed connection
Packit ae235b
 * to get a list of Certificate Authorities that the server will
Packit ae235b
 * accept certificates from.
Packit ae235b
 *
Packit ae235b
 * (It is also possible that a server will allow the connection with
Packit ae235b
 * or without a certificate; in that case, if you don't provide a
Packit ae235b
 * certificate, you can tell that the server requested one by the fact
Packit ae235b
 * that g_tls_client_connection_get_accepted_cas() will return
Packit ae235b
 * non-%NULL.)
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_connection_set_certificate (GTlsConnection  *conn,
Packit ae235b
				  GTlsCertificate *certificate)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TLS_CONNECTION (conn));
Packit ae235b
  g_return_if_fail (G_IS_TLS_CERTIFICATE (certificate));
Packit ae235b
Packit ae235b
  g_object_set (G_OBJECT (conn), "certificate", certificate, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_get_certificate:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 *
Packit ae235b
 * Gets @conn's certificate, as set by
Packit ae235b
 * g_tls_connection_set_certificate().
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): @conn's certificate, or %NULL
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GTlsCertificate *
Packit ae235b
g_tls_connection_get_certificate (GTlsConnection *conn)
Packit ae235b
{
Packit ae235b
  GTlsCertificate *certificate;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
Packit ae235b
Packit ae235b
  g_object_get (G_OBJECT (conn), "certificate", &certificate, NULL);
Packit ae235b
  if (certificate)
Packit ae235b
    g_object_unref (certificate);
Packit ae235b
Packit ae235b
  return certificate;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_set_interaction:
Packit ae235b
 * @conn: a connection
Packit ae235b
 * @interaction: (nullable): an interaction object, or %NULL
Packit ae235b
 *
Packit ae235b
 * Set the object that will be used to interact with the user. It will be used
Packit ae235b
 * for things like prompting the user for passwords.
Packit ae235b
 *
Packit ae235b
 * The @interaction argument will normally be a derived subclass of
Packit ae235b
 * #GTlsInteraction. %NULL can also be provided if no user interaction
Packit ae235b
 * should occur for this connection.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_connection_set_interaction (GTlsConnection       *conn,
Packit ae235b
                                  GTlsInteraction      *interaction)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TLS_CONNECTION (conn));
Packit ae235b
  g_return_if_fail (interaction == NULL || G_IS_TLS_INTERACTION (interaction));
Packit ae235b
Packit ae235b
  g_object_set (G_OBJECT (conn), "interaction", interaction, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_get_interaction:
Packit ae235b
 * @conn: a connection
Packit ae235b
 *
Packit ae235b
 * Get the object that will be used to interact with the user. It will be used
Packit ae235b
 * for things like prompting the user for passwords. If %NULL is returned, then
Packit ae235b
 * no user interaction will occur for this connection.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): The interaction object.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
GTlsInteraction *
Packit ae235b
g_tls_connection_get_interaction (GTlsConnection       *conn)
Packit ae235b
{
Packit ae235b
  GTlsInteraction *interaction = NULL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
Packit ae235b
Packit ae235b
  g_object_get (G_OBJECT (conn), "interaction", &interaction, NULL);
Packit ae235b
  if (interaction)
Packit ae235b
    g_object_unref (interaction);
Packit ae235b
Packit ae235b
  return interaction;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_get_peer_certificate:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 *
Packit ae235b
 * Gets @conn's peer's certificate after the handshake has completed.
Packit ae235b
 * (It is not set during the emission of
Packit ae235b
 * #GTlsConnection::accept-certificate.)
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): @conn's peer's certificate, or %NULL
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GTlsCertificate *
Packit ae235b
g_tls_connection_get_peer_certificate (GTlsConnection *conn)
Packit ae235b
{
Packit ae235b
  GTlsCertificate *peer_certificate;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
Packit ae235b
Packit ae235b
  g_object_get (G_OBJECT (conn), "peer-certificate", &peer_certificate, NULL);
Packit ae235b
  if (peer_certificate)
Packit ae235b
    g_object_unref (peer_certificate);
Packit ae235b
Packit ae235b
  return peer_certificate;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_get_peer_certificate_errors:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 *
Packit ae235b
 * Gets the errors associated with validating @conn's peer's
Packit ae235b
 * certificate, after the handshake has completed. (It is not set
Packit ae235b
 * during the emission of #GTlsConnection::accept-certificate.)
Packit ae235b
 *
Packit ae235b
 * Returns: @conn's peer's certificate errors
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GTlsCertificateFlags
Packit ae235b
g_tls_connection_get_peer_certificate_errors (GTlsConnection *conn)
Packit ae235b
{
Packit ae235b
  GTlsCertificateFlags errors;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), 0);
Packit ae235b
Packit ae235b
  g_object_get (G_OBJECT (conn), "peer-certificate-errors", &errors, NULL);
Packit ae235b
  return errors;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_set_require_close_notify:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 * @require_close_notify: whether or not to require close notification
Packit ae235b
 *
Packit ae235b
 * Sets whether or not @conn expects a proper TLS close notification
Packit ae235b
 * before the connection is closed. If this is %TRUE (the default),
Packit ae235b
 * then @conn will expect to receive a TLS close notification from its
Packit ae235b
 * peer before the connection is closed, and will return a
Packit ae235b
 * %G_TLS_ERROR_EOF error if the connection is closed without proper
Packit ae235b
 * notification (since this may indicate a network error, or
Packit ae235b
 * man-in-the-middle attack).
Packit ae235b
 *
Packit ae235b
 * In some protocols, the application will know whether or not the
Packit ae235b
 * connection was closed cleanly based on application-level data
Packit ae235b
 * (because the application-level data includes a length field, or is
Packit ae235b
 * somehow self-delimiting); in this case, the close notify is
Packit ae235b
 * redundant and sometimes omitted. (TLS 1.1 explicitly allows this;
Packit ae235b
 * in TLS 1.0 it is technically an error, but often done anyway.) You
Packit ae235b
 * can use g_tls_connection_set_require_close_notify() to tell @conn
Packit ae235b
 * to allow an "unannounced" connection close, in which case the close
Packit ae235b
 * will show up as a 0-length read, as in a non-TLS
Packit ae235b
 * #GSocketConnection, and it is up to the application to check that
Packit ae235b
 * the data has been fully received.
Packit ae235b
 *
Packit ae235b
 * Note that this only affects the behavior when the peer closes the
Packit ae235b
 * connection; when the application calls g_io_stream_close() itself
Packit ae235b
 * on @conn, this will send a close notification regardless of the
Packit ae235b
 * setting of this property. If you explicitly want to do an unclean
Packit ae235b
 * close, you can close @conn's #GTlsConnection:base-io-stream rather
Packit ae235b
 * than closing @conn itself, but note that this may only be done when no other
Packit ae235b
 * operations are pending on @conn or the base I/O stream.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_connection_set_require_close_notify (GTlsConnection *conn,
Packit ae235b
					   gboolean        require_close_notify)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TLS_CONNECTION (conn));
Packit ae235b
Packit ae235b
  g_object_set (G_OBJECT (conn),
Packit ae235b
		"require-close-notify", require_close_notify,
Packit ae235b
		NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_get_require_close_notify:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 *
Packit ae235b
 * Tests whether or not @conn expects a proper TLS close notification
Packit ae235b
 * when the connection is closed. See
Packit ae235b
 * g_tls_connection_set_require_close_notify() for details.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @conn requires a proper TLS close
Packit ae235b
 * notification.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_tls_connection_get_require_close_notify (GTlsConnection *conn)
Packit ae235b
{
Packit ae235b
  gboolean require_close_notify;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), TRUE);
Packit ae235b
Packit ae235b
  g_object_get (G_OBJECT (conn),
Packit ae235b
		"require-close-notify", &require_close_notify,
Packit ae235b
		NULL);
Packit ae235b
  return require_close_notify;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_set_rehandshake_mode:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 * @mode: the rehandshaking mode
Packit ae235b
 *
Packit ae235b
 * Sets how @conn behaves with respect to rehandshaking requests.
Packit ae235b
 *
Packit ae235b
 * %G_TLS_REHANDSHAKE_NEVER means that it will never agree to
Packit ae235b
 * rehandshake after the initial handshake is complete. (For a client,
Packit ae235b
 * this means it will refuse rehandshake requests from the server, and
Packit ae235b
 * for a server, this means it will close the connection with an error
Packit ae235b
 * if the client attempts to rehandshake.)
Packit ae235b
 *
Packit ae235b
 * %G_TLS_REHANDSHAKE_SAFELY means that the connection will allow a
Packit ae235b
 * rehandshake only if the other end of the connection supports the
Packit ae235b
 * TLS `renegotiation_info` extension. This is the default behavior,
Packit ae235b
 * but means that rehandshaking will not work against older
Packit ae235b
 * implementations that do not support that extension.
Packit ae235b
 *
Packit ae235b
 * %G_TLS_REHANDSHAKE_UNSAFELY means that the connection will allow
Packit ae235b
 * rehandshaking even without the `renegotiation_info` extension. On
Packit ae235b
 * the server side in particular, this is not recommended, since it
Packit ae235b
 * leaves the server open to certain attacks. However, this mode is
Packit ae235b
 * necessary if you need to allow renegotiation with older client
Packit ae235b
 * software.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_connection_set_rehandshake_mode (GTlsConnection       *conn,
Packit ae235b
				       GTlsRehandshakeMode   mode)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TLS_CONNECTION (conn));
Packit ae235b
Packit ae235b
  g_object_set (G_OBJECT (conn),
Packit ae235b
		"rehandshake-mode", mode,
Packit ae235b
		NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_get_rehandshake_mode:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 *
Packit ae235b
 * Gets @conn rehandshaking mode. See
Packit ae235b
 * g_tls_connection_set_rehandshake_mode() for details.
Packit ae235b
 *
Packit ae235b
 * Returns: @conn's rehandshaking mode
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GTlsRehandshakeMode
Packit ae235b
g_tls_connection_get_rehandshake_mode (GTlsConnection       *conn)
Packit ae235b
{
Packit ae235b
  GTlsRehandshakeMode mode;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), G_TLS_REHANDSHAKE_NEVER);
Packit ae235b
Packit ae235b
  g_object_get (G_OBJECT (conn),
Packit ae235b
		"rehandshake-mode", &mode,
Packit ae235b
		NULL);
Packit ae235b
  return mode;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_handshake:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 * @error: a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Attempts a TLS handshake on @conn.
Packit ae235b
 *
Packit ae235b
 * On the client side, it is never necessary to call this method;
Packit ae235b
 * although the connection needs to perform a handshake after
Packit ae235b
 * connecting (or after sending a "STARTTLS"-type command) and may
Packit ae235b
 * need to rehandshake later if the server requests it,
Packit ae235b
 * #GTlsConnection will handle this for you automatically when you try
Packit ae235b
 * to send or receive data on the connection. However, you can call
Packit ae235b
 * g_tls_connection_handshake() manually if you want to know for sure
Packit ae235b
 * whether the initial handshake succeeded or failed (as opposed to
Packit ae235b
 * just immediately trying to write to @conn's output stream, in which
Packit ae235b
 * case if it fails, it may not be possible to tell if it failed
Packit ae235b
 * before or after completing the handshake).
Packit ae235b
 *
Packit ae235b
 * Likewise, on the server side, although a handshake is necessary at
Packit ae235b
 * the beginning of the communication, you do not need to call this
Packit ae235b
 * function explicitly unless you want clearer error reporting.
Packit ae235b
 * However, you may call g_tls_connection_handshake() later on to
Packit ae235b
 * renegotiate parameters (encryption methods, etc) with the client.
Packit ae235b
 *
Packit ae235b
 * #GTlsConnection::accept_certificate may be emitted during the
Packit ae235b
 * handshake.
Packit ae235b
 *
Packit ae235b
 * Returns: success or failure
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_tls_connection_handshake (GTlsConnection   *conn,
Packit ae235b
			    GCancellable     *cancellable,
Packit ae235b
			    GError          **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), FALSE);
Packit ae235b
Packit ae235b
  return G_TLS_CONNECTION_GET_CLASS (conn)->handshake (conn, cancellable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_handshake_async:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 * @io_priority: the [I/O priority][io-priority] of the request
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 * @callback: callback to call when the handshake is complete
Packit ae235b
 * @user_data: the data to pass to the callback function
Packit ae235b
 *
Packit ae235b
 * Asynchronously performs a TLS handshake on @conn. See
Packit ae235b
 * g_tls_connection_handshake() for more information.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_connection_handshake_async (GTlsConnection       *conn,
Packit ae235b
				  int                   io_priority,
Packit ae235b
				  GCancellable         *cancellable,
Packit ae235b
				  GAsyncReadyCallback   callback,
Packit ae235b
				  gpointer              user_data)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TLS_CONNECTION (conn));
Packit ae235b
Packit ae235b
  G_TLS_CONNECTION_GET_CLASS (conn)->handshake_async (conn, io_priority,
Packit ae235b
						      cancellable,
Packit ae235b
						      callback, user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_handshake_finish:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 * @result: a #GAsyncResult.
Packit ae235b
 * @error: a #GError pointer, or %NULL
Packit ae235b
 *
Packit ae235b
 * Finish an asynchronous TLS handshake operation. See
Packit ae235b
 * g_tls_connection_handshake() for more information.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE on failure, in which
Packit ae235b
 * case @error will be set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_tls_connection_handshake_finish (GTlsConnection  *conn,
Packit ae235b
				   GAsyncResult    *result,
Packit ae235b
				   GError         **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), FALSE);
Packit ae235b
Packit ae235b
  return G_TLS_CONNECTION_GET_CLASS (conn)->handshake_finish (conn, result, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_error_quark:
Packit ae235b
 *
Packit ae235b
 * Gets the TLS error quark.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GQuark.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
G_DEFINE_QUARK (g-tls-error-quark, g_tls_error)
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_connection_emit_accept_certificate:
Packit ae235b
 * @conn: a #GTlsConnection
Packit ae235b
 * @peer_cert: the peer's #GTlsCertificate
Packit ae235b
 * @errors: the problems with @peer_cert
Packit ae235b
 *
Packit ae235b
 * Used by #GTlsConnection implementations to emit the
Packit ae235b
 * #GTlsConnection::accept-certificate signal.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if one of the signal handlers has returned
Packit ae235b
 *     %TRUE to accept @peer_cert
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_tls_connection_emit_accept_certificate (GTlsConnection       *conn,
Packit ae235b
					  GTlsCertificate      *peer_cert,
Packit ae235b
					  GTlsCertificateFlags  errors)
Packit ae235b
{
Packit ae235b
  gboolean accept = FALSE;
Packit ae235b
Packit ae235b
  g_signal_emit (conn, signals[ACCEPT_CERTIFICATE], 0,
Packit ae235b
		 peer_cert, errors, &accept);
Packit ae235b
  return accept;
Packit ae235b
}