Blame gio/gdtlsclientconnection.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright © 2010 Red Hat, Inc
Packit ae235b
 * Copyright © 2015 Collabora, Ltd.
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 "gdtlsclientconnection.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:gdtlsclientconnection
Packit ae235b
 * @short_description: DTLS client-side connection
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * #GDtlsClientConnection is the client-side subclass of
Packit ae235b
 * #GDtlsConnection, representing a client-side DTLS connection.
Packit ae235b
 *
Packit ae235b
 * Since: 2.48
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GDtlsClientConnection:
Packit ae235b
 *
Packit ae235b
 * Abstract base class for the backend-specific client connection
Packit ae235b
 * type.
Packit ae235b
 *
Packit ae235b
 * Since: 2.48
Packit ae235b
 */
Packit ae235b
Packit ae235b
G_DEFINE_INTERFACE (GDtlsClientConnection, g_dtls_client_connection,
Packit ae235b
                    G_TYPE_DTLS_CONNECTION)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dtls_client_connection_default_init (GDtlsClientConnectionInterface *iface)
Packit ae235b
{
Packit ae235b
  /**
Packit ae235b
   * GDtlsClientConnection: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 #GDtlsConnection::accept-certificate.
Packit ae235b
   *
Packit ae235b
   * Since: 2.48
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
   * GDtlsClientConnection: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
   * #GDtlsClientConnection:validation-flags, this object will be used
Packit ae235b
   * to determine the expected identify of the remote end of the
Packit ae235b
   * connection; if #GDtlsClientConnection: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.48
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
   * GDtlsClientConnection: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.48
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_dtls_client_connection_new:
Packit ae235b
 * @base_socket: the #GDatagramBased 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 #GDtlsClientConnection wrapping @base_socket which is
Packit ae235b
 * assumed to communicate with the server identified by @server_identity.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full) (type GDtlsClientConnection): the new
Packit ae235b
 *   #GDtlsClientConnection, or %NULL on error
Packit ae235b
 *
Packit ae235b
 * Since: 2.48
Packit ae235b
 */
Packit ae235b
GDatagramBased *
Packit ae235b
g_dtls_client_connection_new (GDatagramBased      *base_socket,
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_dtls_client_connection_type (backend),
Packit ae235b
                         NULL, error,
Packit ae235b
                         "base-socket", base_socket,
Packit ae235b
                         "server-identity", server_identity,
Packit ae235b
                         NULL);
Packit ae235b
  return G_DATAGRAM_BASED (conn);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dtls_client_connection_get_validation_flags:
Packit ae235b
 * @conn: the #GDtlsClientConnection
Packit ae235b
 *
Packit ae235b
 * Gets @conn's validation flags
Packit ae235b
 *
Packit ae235b
 * Returns: the validation flags
Packit ae235b
 *
Packit ae235b
 * Since: 2.48
Packit ae235b
 */
Packit ae235b
GTlsCertificateFlags
Packit ae235b
g_dtls_client_connection_get_validation_flags (GDtlsClientConnection *conn)
Packit ae235b
{
Packit ae235b
  GTlsCertificateFlags flags = 0;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_DTLS_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_dtls_client_connection_set_validation_flags:
Packit ae235b
 * @conn: the #GDtlsClientConnection
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.48
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_dtls_client_connection_set_validation_flags (GDtlsClientConnection  *conn,
Packit ae235b
                                               GTlsCertificateFlags   flags)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_DTLS_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_dtls_client_connection_get_server_identity:
Packit ae235b
 * @conn: the #GDtlsClientConnection
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.48
Packit ae235b
 */
Packit ae235b
GSocketConnectable *
Packit ae235b
g_dtls_client_connection_get_server_identity (GDtlsClientConnection *conn)
Packit ae235b
{
Packit ae235b
  GSocketConnectable *identity = NULL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_DTLS_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_dtls_client_connection_set_server_identity:
Packit ae235b
 * @conn: the #GDtlsClientConnection
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.48
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_dtls_client_connection_set_server_identity (GDtlsClientConnection *conn,
Packit ae235b
                                              GSocketConnectable    *identity)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_DTLS_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_dtls_client_connection_get_accepted_cas:
Packit ae235b
 * @conn: the #GDtlsClientConnection
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.48
Packit ae235b
 */
Packit ae235b
GList *
Packit ae235b
g_dtls_client_connection_get_accepted_cas (GDtlsClientConnection *conn)
Packit ae235b
{
Packit ae235b
  GList *accepted_cas = NULL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_DTLS_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
}