Blame gio/gtlsclientconnection.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 "gtlsclientconnection.h"
Packit ae235b
#include "ginitable.h"
Packit ae235b
#include "gioenumtypes.h"
Packit ae235b
#include "gsocket.h"
Packit ae235b
#include "gsocketconnectable.h"
Packit ae235b
#include "gtlsbackend.h"
Packit ae235b
#include "gtlscertificate.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gtlsclientconnection
Packit ae235b
 * @short_description: TLS client-side connection
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * #GTlsClientConnection is the client-side subclass of
Packit ae235b
 * #GTlsConnection, representing a client-side TLS connection.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTlsClientConnection:
Packit ae235b
 *
Packit ae235b
 * Abstract base class for the backend-specific client connection
Packit ae235b
 * type.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
Packit ae235b
G_DEFINE_INTERFACE (GTlsClientConnection, g_tls_client_connection, G_TYPE_TLS_CONNECTION)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_client_connection_default_init (GTlsClientConnectionInterface *iface)
Packit ae235b
{
Packit ae235b
  /**
Packit ae235b
   * GTlsClientConnection:validation-flags:
Packit ae235b
   *
Packit ae235b
   * What steps to perform when validating a certificate received from
Packit ae235b
   * a server. Server certificates that fail to validate in all of the
Packit ae235b
   * ways indicated here will be rejected unless the application
Packit ae235b
   * overrides the default via #GTlsConnection::accept-certificate.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  g_object_interface_install_property (iface,
Packit ae235b
				       g_param_spec_flags ("validation-flags",
Packit ae235b
							   P_("Validation flags"),
Packit ae235b
							   P_("What certificate validation to perform"),
Packit ae235b
							   G_TYPE_TLS_CERTIFICATE_FLAGS,
Packit ae235b
							   G_TLS_CERTIFICATE_VALIDATE_ALL,
Packit ae235b
							   G_PARAM_READWRITE |
Packit ae235b
							   G_PARAM_CONSTRUCT |
Packit ae235b
							   G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GTlsClientConnection:server-identity:
Packit ae235b
   *
Packit ae235b
   * A #GSocketConnectable describing the identity of the server that
Packit ae235b
   * is expected on the other end of the connection.
Packit ae235b
   *
Packit ae235b
   * If the %G_TLS_CERTIFICATE_BAD_IDENTITY flag is set in
Packit ae235b
   * #GTlsClientConnection:validation-flags, this object will be used
Packit ae235b
   * to determine the expected identify of the remote end of the
Packit ae235b
   * connection; if #GTlsClientConnection:server-identity is not set,
Packit ae235b
   * or does not match the identity presented by the server, then the
Packit ae235b
   * %G_TLS_CERTIFICATE_BAD_IDENTITY validation will fail.
Packit ae235b
   *
Packit ae235b
   * In addition to its use in verifying the server certificate,
Packit ae235b
   * this is also used to give a hint to the server about what
Packit ae235b
   * certificate we expect, which is useful for servers that serve
Packit ae235b
   * virtual hosts.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  g_object_interface_install_property (iface,
Packit ae235b
				       g_param_spec_object ("server-identity",
Packit ae235b
							    P_("Server identity"),
Packit ae235b
							    P_("GSocketConnectable identifying the server"),
Packit ae235b
							    G_TYPE_SOCKET_CONNECTABLE,
Packit ae235b
							    G_PARAM_READWRITE |
Packit ae235b
							    G_PARAM_CONSTRUCT |
Packit ae235b
							    G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GTlsClientConnection:use-ssl3:
Packit ae235b
   *
Packit ae235b
   * If %TRUE, forces the connection to use a fallback version of TLS
Packit ae235b
   * or SSL, rather than trying to negotiate the best version of TLS
Packit ae235b
   * to use. This can be used when talking to servers that don't
Packit ae235b
   * implement version negotiation correctly and therefore refuse to
Packit ae235b
   * handshake at all with a modern TLS handshake.
Packit ae235b
   *
Packit ae235b
   * Despite the property name, the fallback version is usually not
Packit ae235b
   * SSL 3.0, because SSL 3.0 is generally disabled by the #GTlsBackend.
Packit ae235b
   * #GTlsClientConnection will use the next-highest available version
Packit ae235b
   * as the fallback version.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   *
Packit ae235b
   * Deprecated: 2.56: SSL 3.0 is insecure, and this property does not
Packit ae235b
   * generally enable or disable it, despite its name.
Packit ae235b
   */
Packit ae235b
  g_object_interface_install_property (iface,
Packit ae235b
				       g_param_spec_boolean ("use-ssl3",
Packit ae235b
							     P_("Use fallback"),
Packit ae235b
							     P_("Use fallback version of SSL/TLS rather than most recent version"),
Packit ae235b
							     FALSE,
Packit ae235b
							     G_PARAM_READWRITE |
Packit ae235b
							     G_PARAM_CONSTRUCT |
Packit ae235b
							     G_PARAM_STATIC_STRINGS |
Packit ae235b
							     G_PARAM_DEPRECATED));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GTlsClientConnection:accepted-cas: (type GLib.List) (element-type GLib.ByteArray)
Packit ae235b
   *
Packit ae235b
   * A list of the distinguished names of the Certificate Authorities
Packit ae235b
   * that the server will accept client certificates signed by. If the
Packit ae235b
   * server requests a client certificate during the handshake, then
Packit ae235b
   * this property will be set after the handshake completes.
Packit ae235b
   *
Packit ae235b
   * Each item in the list is a #GByteArray which contains the complete
Packit ae235b
   * subject DN of the certificate authority.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  g_object_interface_install_property (iface,
Packit ae235b
				       g_param_spec_pointer ("accepted-cas",
Packit ae235b
							     P_("Accepted CAs"),
Packit ae235b
							     P_("Distinguished names of the CAs the server accepts certificates from"),
Packit ae235b
							     G_PARAM_READABLE |
Packit ae235b
							     G_PARAM_STATIC_STRINGS));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_client_connection_new:
Packit ae235b
 * @base_io_stream: the #GIOStream to wrap
Packit ae235b
 * @server_identity: (nullable): the expected identity of the server
Packit ae235b
 * @error: #GError for error reporting, or %NULL to ignore.
Packit ae235b
 *
Packit ae235b
 * Creates a new #GTlsClientConnection wrapping @base_io_stream (which
Packit ae235b
 * must have pollable input and output streams) which is assumed to
Packit ae235b
 * communicate with the server identified by @server_identity.
Packit ae235b
 *
Packit ae235b
 * See the documentation for #GTlsConnection:base-io-stream for restrictions
Packit ae235b
 * on when application code can run operations on the @base_io_stream after
Packit ae235b
 * this function has returned.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full) (type GTlsClientConnection): the new
Packit ae235b
 * #GTlsClientConnection, or %NULL on error
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GIOStream *
Packit ae235b
g_tls_client_connection_new (GIOStream           *base_io_stream,
Packit ae235b
			     GSocketConnectable  *server_identity,
Packit ae235b
			     GError             **error)
Packit ae235b
{
Packit ae235b
  GObject *conn;
Packit ae235b
  GTlsBackend *backend;
Packit ae235b
Packit ae235b
  backend = g_tls_backend_get_default ();
Packit ae235b
  conn = g_initable_new (g_tls_backend_get_client_connection_type (backend),
Packit ae235b
			 NULL, error,
Packit ae235b
			 "base-io-stream", base_io_stream,
Packit ae235b
			 "server-identity", server_identity,
Packit ae235b
			 NULL);
Packit ae235b
  return G_IO_STREAM (conn);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_client_connection_get_validation_flags:
Packit ae235b
 * @conn: the #GTlsClientConnection
Packit ae235b
 *
Packit ae235b
 * Gets @conn's validation flags
Packit ae235b
 *
Packit ae235b
 * Returns: the validation flags
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GTlsCertificateFlags
Packit ae235b
g_tls_client_connection_get_validation_flags (GTlsClientConnection *conn)
Packit ae235b
{
Packit ae235b
  GTlsCertificateFlags flags = 0;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0);
Packit ae235b
Packit ae235b
  g_object_get (G_OBJECT (conn), "validation-flags", &flags, NULL);
Packit ae235b
  return flags;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_client_connection_set_validation_flags:
Packit ae235b
 * @conn: the #GTlsClientConnection
Packit ae235b
 * @flags: the #GTlsCertificateFlags to use
Packit ae235b
 *
Packit ae235b
 * Sets @conn's validation flags, to override the default set of
Packit ae235b
 * checks performed when validating a server certificate. By default,
Packit ae235b
 * %G_TLS_CERTIFICATE_VALIDATE_ALL is used.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_client_connection_set_validation_flags (GTlsClientConnection  *conn,
Packit ae235b
					      GTlsCertificateFlags   flags)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
Packit ae235b
Packit ae235b
  g_object_set (G_OBJECT (conn), "validation-flags", flags, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_client_connection_get_server_identity:
Packit ae235b
 * @conn: the #GTlsClientConnection
Packit ae235b
 *
Packit ae235b
 * Gets @conn's expected server identity
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): a #GSocketConnectable describing the
Packit ae235b
 * expected server identity, or %NULL if the expected identity is not
Packit ae235b
 * known.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GSocketConnectable *
Packit ae235b
g_tls_client_connection_get_server_identity (GTlsClientConnection *conn)
Packit ae235b
{
Packit ae235b
  GSocketConnectable *identity = NULL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0);
Packit ae235b
Packit ae235b
  g_object_get (G_OBJECT (conn), "server-identity", &identity, NULL);
Packit ae235b
  if (identity)
Packit ae235b
    g_object_unref (identity);
Packit ae235b
  return identity;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_client_connection_set_server_identity:
Packit ae235b
 * @conn: the #GTlsClientConnection
Packit ae235b
 * @identity: a #GSocketConnectable describing the expected server identity
Packit ae235b
 *
Packit ae235b
 * Sets @conn's expected server identity, which is used both to tell
Packit ae235b
 * servers on virtual hosts which certificate to present, and also
Packit ae235b
 * to let @conn know what name to look for in the certificate when
Packit ae235b
 * performing %G_TLS_CERTIFICATE_BAD_IDENTITY validation, if enabled.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_client_connection_set_server_identity (GTlsClientConnection *conn,
Packit ae235b
					     GSocketConnectable   *identity)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
Packit ae235b
Packit ae235b
  g_object_set (G_OBJECT (conn), "server-identity", identity, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_client_connection_get_use_ssl3:
Packit ae235b
 * @conn: the #GTlsClientConnection
Packit ae235b
 *
Packit ae235b
 * Gets whether @conn will force the lowest-supported TLS protocol
Packit ae235b
 * version rather than attempt to negotiate the highest mutually-
Packit ae235b
 * supported version of TLS; see g_tls_client_connection_set_use_ssl3().
Packit ae235b
 *
Packit ae235b
 * Returns: whether @conn will use the lowest-supported TLS protocol version
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.56: SSL 3.0 is insecure, and this function does not
Packit ae235b
 * actually indicate whether it is enabled.
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_tls_client_connection_get_use_ssl3 (GTlsClientConnection *conn)
Packit ae235b
{
Packit ae235b
  gboolean use_ssl3 = FALSE;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0);
Packit ae235b
Packit ae235b
  g_object_get (G_OBJECT (conn), "use-ssl3", &use_ssl3, NULL);
Packit ae235b
  return use_ssl3;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_client_connection_set_use_ssl3:
Packit ae235b
 * @conn: the #GTlsClientConnection
Packit ae235b
 * @use_ssl3: whether to use the lowest-supported protocol version
Packit ae235b
 *
Packit ae235b
 * If @use_ssl3 is %TRUE, this forces @conn to use the lowest-supported
Packit ae235b
 * TLS protocol version rather than trying to properly negotiate the
Packit ae235b
 * highest mutually-supported protocol version with the peer. This can
Packit ae235b
 * be used when talking to broken TLS servers that exhibit protocol
Packit ae235b
 * version intolerance.
Packit ae235b
 *
Packit ae235b
 * Be aware that SSL 3.0 is generally disabled by the #GTlsBackend, so
Packit ae235b
 * the lowest-supported protocol version is probably not SSL 3.0.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.56: SSL 3.0 is insecure, and this function does not
Packit ae235b
 * generally enable or disable it, despite its name.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_client_connection_set_use_ssl3 (GTlsClientConnection *conn,
Packit ae235b
				      gboolean              use_ssl3)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
Packit ae235b
Packit ae235b
  g_object_set (G_OBJECT (conn), "use-ssl3", use_ssl3, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_client_connection_get_accepted_cas:
Packit ae235b
 * @conn: the #GTlsClientConnection
Packit ae235b
 *
Packit ae235b
 * Gets the list of distinguished names of the Certificate Authorities
Packit ae235b
 * that the server will accept certificates from. This will be set
Packit ae235b
 * during the TLS handshake if the server requests a certificate.
Packit ae235b
 * Otherwise, it will be %NULL.
Packit ae235b
 *
Packit ae235b
 * Each item in the list is a #GByteArray which contains the complete
Packit ae235b
 * subject DN of the certificate authority.
Packit ae235b
 *
Packit ae235b
 * Returns: (element-type GByteArray) (transfer full): the list of
Packit ae235b
 * CA DNs. You should unref each element with g_byte_array_unref() and then
Packit ae235b
 * the free the list with g_list_free().
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GList *
Packit ae235b
g_tls_client_connection_get_accepted_cas (GTlsClientConnection *conn)
Packit ae235b
{
Packit ae235b
  GList *accepted_cas = NULL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), NULL);
Packit ae235b
Packit ae235b
  g_object_get (G_OBJECT (conn), "accepted-cas", &accepted_cas, NULL);
Packit ae235b
  return accepted_cas;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_client_connection_copy_session_state:
Packit ae235b
 * @conn: a #GTlsClientConnection
Packit ae235b
 * @source: a #GTlsClientConnection
Packit ae235b
 *
Packit ae235b
 * Copies session state from one connection to another. This is
Packit ae235b
 * not normally needed, but may be used when the same session
Packit ae235b
 * needs to be used between different endpoints as is required
Packit ae235b
 * by some protocols such as FTP over TLS. @source should have
Packit ae235b
 * already completed a handshake, and @conn should not have
Packit ae235b
 * completed a handshake.
Packit ae235b
 *
Packit ae235b
 * Since: 2.46
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_client_connection_copy_session_state (GTlsClientConnection *conn,
Packit ae235b
                                            GTlsClientConnection *source)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
Packit ae235b
  g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (source));
Packit ae235b
  g_return_if_fail (G_TLS_CLIENT_CONNECTION_GET_INTERFACE (conn)->copy_session_state != NULL);
Packit ae235b
Packit ae235b
  G_TLS_CLIENT_CONNECTION_GET_INTERFACE (conn)->copy_session_state (conn,
Packit ae235b
                                                                    source);
Packit ae235b
}