Blame gio/gtlsinteraction.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2011 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
 * Author: Stef Walter <stefw@collabora.co.uk>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include "gtlscertificate.h"
Packit ae235b
#include "gtlsconnection.h"
Packit ae235b
#include "gtlsinteraction.h"
Packit ae235b
#include "gtlspassword.h"
Packit ae235b
#include "gasyncresult.h"
Packit ae235b
#include "gcancellable.h"
Packit ae235b
#include "gtask.h"
Packit ae235b
#include "gioenumtypes.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gtlsinteraction
Packit ae235b
 * @short_description: Interaction with the user during TLS operations.
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * #GTlsInteraction provides a mechanism for the TLS connection and database
Packit ae235b
 * code to interact with the user. It can be used to ask the user for passwords.
Packit ae235b
 *
Packit ae235b
 * To use a #GTlsInteraction with a TLS connection use
Packit ae235b
 * g_tls_connection_set_interaction().
Packit ae235b
 *
Packit ae235b
 * Callers should instantiate a derived class that implements the various
Packit ae235b
 * interaction methods to show the required dialogs.
Packit ae235b
 *
Packit ae235b
 * Callers should use the 'invoke' functions like
Packit ae235b
 * g_tls_interaction_invoke_ask_password() to run interaction methods. These
Packit ae235b
 * functions make sure that the interaction is invoked in the main loop
Packit ae235b
 * and not in the current thread, if the current thread is not running the
Packit ae235b
 * main loop.
Packit ae235b
 *
Packit ae235b
 * Derived classes can choose to implement whichever interactions methods they'd
Packit ae235b
 * like to support by overriding those virtual methods in their class
Packit ae235b
 * initialization function. Any interactions not implemented will return
Packit ae235b
 * %G_TLS_INTERACTION_UNHANDLED. If a derived class implements an async method,
Packit ae235b
 * it must also implement the corresponding finish method.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTlsInteraction:
Packit ae235b
 *
Packit ae235b
 * An object representing interaction that the TLS connection and database
Packit ae235b
 * might have with the user.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTlsInteractionClass:
Packit ae235b
 * @ask_password: ask for a password synchronously. If the implementation
Packit ae235b
 *     returns %G_TLS_INTERACTION_HANDLED, then the password argument should
Packit ae235b
 *     have been filled in by using g_tls_password_set_value() or a similar
Packit ae235b
 *     function.
Packit ae235b
 * @ask_password_async: ask for a password asynchronously.
Packit ae235b
 * @ask_password_finish: complete operation to ask for a password asynchronously.
Packit ae235b
 *     If the implementation returns %G_TLS_INTERACTION_HANDLED, then the
Packit ae235b
 *     password argument of the async method should have been filled in by using
Packit ae235b
 *     g_tls_password_set_value() or a similar function.
Packit ae235b
 * @request_certificate: ask for a certificate synchronously. If the
Packit ae235b
 *     implementation returns %G_TLS_INTERACTION_HANDLED, then the connection
Packit ae235b
 *     argument should have been filled in by using
Packit ae235b
 *     g_tls_connection_set_certificate().
Packit ae235b
 * @request_certificate_async: ask for a certificate asynchronously.
Packit ae235b
 * @request_certificate_finish: complete operation to ask for a certificate
Packit ae235b
 *     asynchronously. If the implementation returns %G_TLS_INTERACTION_HANDLED,
Packit ae235b
 *     then the connection argument of the async method should have been
Packit ae235b
 *     filled in by using g_tls_connection_set_certificate().
Packit ae235b
 *
Packit ae235b
 * The class for #GTlsInteraction. Derived classes implement the various
Packit ae235b
 * virtual interaction methods to handle TLS interactions.
Packit ae235b
 *
Packit ae235b
 * Derived classes can choose to implement whichever interactions methods they'd
Packit ae235b
 * like to support by overriding those virtual methods in their class
Packit ae235b
 * initialization function. If a derived class implements an async method,
Packit ae235b
 * it must also implement the corresponding finish method.
Packit ae235b
 *
Packit ae235b
 * The synchronous interaction methods should implement to display modal dialogs,
Packit ae235b
 * and the asynchronous methods to display modeless dialogs.
Packit ae235b
 *
Packit ae235b
 * If the user cancels an interaction, then the result should be
Packit ae235b
 * %G_TLS_INTERACTION_FAILED and the error should be set with a domain of
Packit ae235b
 * %G_IO_ERROR and code of %G_IO_ERROR_CANCELLED.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
Packit ae235b
struct _GTlsInteractionPrivate {
Packit ae235b
  GMainContext *context;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_PRIVATE (GTlsInteraction, g_tls_interaction, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GMutex mutex;
Packit ae235b
Packit ae235b
  /* Input arguments */
Packit ae235b
  GTlsInteraction *interaction;
Packit ae235b
  GObject *argument;
Packit ae235b
  GCancellable *cancellable;
Packit ae235b
Packit ae235b
  /* Used when we're invoking async interactions */
Packit ae235b
  GAsyncReadyCallback callback;
Packit ae235b
  gpointer user_data;
Packit ae235b
Packit ae235b
  /* Used when we expect results */
Packit ae235b
  GTlsInteractionResult result;
Packit ae235b
  GError *error;
Packit ae235b
  gboolean complete;
Packit ae235b
  GCond cond;
Packit ae235b
} InvokeClosure;
Packit ae235b
Packit ae235b
static void
Packit ae235b
invoke_closure_free (gpointer data)
Packit ae235b
{
Packit ae235b
  InvokeClosure *closure = data;
Packit ae235b
  g_assert (closure);
Packit ae235b
  g_object_unref (closure->interaction);
Packit ae235b
  g_clear_object (&closure->argument);
Packit ae235b
  g_clear_object (&closure->cancellable);
Packit ae235b
  g_cond_clear (&closure->cond);
Packit ae235b
  g_mutex_clear (&closure->mutex);
Packit ae235b
  g_clear_error (&closure->error);
Packit ae235b
Packit ae235b
  /* Insurance that we've actually used these before freeing */
Packit ae235b
  g_assert (closure->callback == NULL);
Packit ae235b
  g_assert (closure->user_data == NULL);
Packit ae235b
Packit ae235b
  g_free (closure);
Packit ae235b
}
Packit ae235b
Packit ae235b
static InvokeClosure *
Packit ae235b
invoke_closure_new (GTlsInteraction *interaction,
Packit ae235b
                    GObject         *argument,
Packit ae235b
                    GCancellable    *cancellable)
Packit ae235b
{
Packit ae235b
  InvokeClosure *closure = g_new0 (InvokeClosure, 1);
Packit ae235b
  closure->interaction = g_object_ref (interaction);
Packit ae235b
  closure->argument = argument ? g_object_ref (argument) : NULL;
Packit ae235b
  closure->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
Packit ae235b
  g_mutex_init (&closure->mutex);
Packit ae235b
  g_cond_init (&closure->cond);
Packit ae235b
  closure->result = G_TLS_INTERACTION_UNHANDLED;
Packit ae235b
  return closure;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GTlsInteractionResult
Packit ae235b
invoke_closure_wait_and_free (InvokeClosure *closure,
Packit ae235b
                              GError       **error)
Packit ae235b
{
Packit ae235b
  GTlsInteractionResult result;
Packit ae235b
Packit ae235b
  g_mutex_lock (&closure->mutex);
Packit ae235b
Packit ae235b
  while (!closure->complete)
Packit ae235b
    g_cond_wait (&closure->cond, &closure->mutex);
Packit ae235b
Packit ae235b
  g_mutex_unlock (&closure->mutex);
Packit ae235b
Packit ae235b
  if (closure->error)
Packit ae235b
    {
Packit ae235b
      g_propagate_error (error, closure->error);
Packit ae235b
      closure->error = NULL;
Packit ae235b
    }
Packit ae235b
  result = closure->result;
Packit ae235b
Packit ae235b
  invoke_closure_free (closure);
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GTlsInteractionResult
Packit ae235b
invoke_closure_complete_and_free (GTlsInteraction *interaction,
Packit ae235b
                                  InvokeClosure *closure,
Packit ae235b
                                  GError **error)
Packit ae235b
{
Packit ae235b
  GTlsInteractionResult result;
Packit ae235b
  gboolean complete;
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Handle the case where we've been called from within the main context
Packit ae235b
   * or in the case where the main context is not running. This approximates
Packit ae235b
   * the behavior of a modal dialog.
Packit ae235b
   */
Packit ae235b
  if (g_main_context_acquire (interaction->priv->context))
Packit ae235b
    {
Packit ae235b
      for (;;)
Packit ae235b
        {
Packit ae235b
          g_mutex_lock (&closure->mutex);
Packit ae235b
          complete = closure->complete;
Packit ae235b
          g_mutex_unlock (&closure->mutex);
Packit ae235b
          if (complete)
Packit ae235b
            break;
Packit ae235b
          g_main_context_iteration (interaction->priv->context, TRUE);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_main_context_release (interaction->priv->context);
Packit ae235b
Packit ae235b
      if (closure->error)
Packit ae235b
        {
Packit ae235b
          g_propagate_error (error, closure->error);
Packit ae235b
          closure->error = NULL;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      result = closure->result;
Packit ae235b
      invoke_closure_free (closure);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Handle the case where we're in a different thread than the main
Packit ae235b
   * context and a main loop is running.
Packit ae235b
   */
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      result = invoke_closure_wait_and_free (closure, error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_interaction_init (GTlsInteraction *interaction)
Packit ae235b
{
Packit ae235b
  interaction->priv = g_tls_interaction_get_instance_private (interaction);
Packit ae235b
  interaction->priv->context = g_main_context_ref_thread_default ();
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_interaction_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GTlsInteraction *interaction = G_TLS_INTERACTION (object);
Packit ae235b
Packit ae235b
  g_main_context_unref (interaction->priv->context);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_tls_interaction_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_interaction_class_init (GTlsInteractionClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->finalize = g_tls_interaction_finalize;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
on_invoke_ask_password_sync (gpointer user_data)
Packit ae235b
{
Packit ae235b
  InvokeClosure *closure = user_data;
Packit ae235b
  GTlsInteractionClass *klass;
Packit ae235b
Packit ae235b
  g_mutex_lock (&closure->mutex);
Packit ae235b
Packit ae235b
  klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
Packit ae235b
  g_assert (klass->ask_password);
Packit ae235b
Packit ae235b
  closure->result = klass->ask_password (closure->interaction,
Packit ae235b
                                         G_TLS_PASSWORD (closure->argument),
Packit ae235b
                                         closure->cancellable,
Packit ae235b
                                         &closure->error);
Packit ae235b
Packit ae235b
  closure->complete = TRUE;
Packit ae235b
  g_cond_signal (&closure->cond);
Packit ae235b
  g_mutex_unlock (&closure->mutex);
Packit ae235b
Packit ae235b
  return FALSE; /* don't call again */
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
on_ask_password_complete (GObject      *source,
Packit ae235b
                          GAsyncResult *result,
Packit ae235b
                          gpointer      user_data)
Packit ae235b
{
Packit ae235b
  InvokeClosure *closure = user_data;
Packit ae235b
  GTlsInteractionClass *klass;
Packit ae235b
Packit ae235b
  g_mutex_lock (&closure->mutex);
Packit ae235b
Packit ae235b
  klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
Packit ae235b
  g_assert (klass->ask_password_finish);
Packit ae235b
Packit ae235b
  closure->result = klass->ask_password_finish (closure->interaction,
Packit ae235b
                                                result,
Packit ae235b
                                                &closure->error);
Packit ae235b
Packit ae235b
  closure->complete = TRUE;
Packit ae235b
  g_cond_signal (&closure->cond);
Packit ae235b
  g_mutex_unlock (&closure->mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
on_invoke_ask_password_async_as_sync (gpointer user_data)
Packit ae235b
{
Packit ae235b
  InvokeClosure *closure = user_data;
Packit ae235b
  GTlsInteractionClass *klass;
Packit ae235b
Packit ae235b
  g_mutex_lock (&closure->mutex);
Packit ae235b
Packit ae235b
  klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
Packit ae235b
  g_assert (klass->ask_password_async);
Packit ae235b
Packit ae235b
  klass->ask_password_async (closure->interaction,
Packit ae235b
                             G_TLS_PASSWORD (closure->argument),
Packit ae235b
                             closure->cancellable,
Packit ae235b
                             on_ask_password_complete,
Packit ae235b
                             closure);
Packit ae235b
Packit ae235b
  /* Note that we've used these */
Packit ae235b
  closure->callback = NULL;
Packit ae235b
  closure->user_data = NULL;
Packit ae235b
Packit ae235b
  g_mutex_unlock (&closure->mutex);
Packit ae235b
Packit ae235b
  return FALSE; /* don't call again */
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_interaction_invoke_ask_password:
Packit ae235b
 * @interaction: a #GTlsInteraction object
Packit ae235b
 * @password: a #GTlsPassword object
Packit ae235b
 * @cancellable: an optional #GCancellable cancellation object
Packit ae235b
 * @error: an optional location to place an error on failure
Packit ae235b
 *
Packit ae235b
 * Invoke the interaction to ask the user for a password. It invokes this
Packit ae235b
 * interaction in the main loop, specifically the #GMainContext returned by
Packit ae235b
 * g_main_context_get_thread_default() when the interaction is created. This
Packit ae235b
 * is called by called by #GTlsConnection or #GTlsDatabase to ask the user
Packit ae235b
 * for a password.
Packit ae235b
 *
Packit ae235b
 * Derived subclasses usually implement a password prompt, although they may
Packit ae235b
 * also choose to provide a password from elsewhere. The @password value will
Packit ae235b
 * be filled in and then @callback will be called. Alternatively the user may
Packit ae235b
 * abort this password request, which will usually abort the TLS connection.
Packit ae235b
 *
Packit ae235b
 * The implementation can either be a synchronous (eg: modal dialog) or an
Packit ae235b
 * asynchronous one (eg: modeless dialog). This function will take care of
Packit ae235b
 * calling which ever one correctly.
Packit ae235b
 *
Packit ae235b
 * If the interaction is cancelled by the cancellation object, or by the
Packit ae235b
 * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
Packit ae235b
 * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
Packit ae235b
 * not support immediate cancellation.
Packit ae235b
 *
Packit ae235b
 * Returns: The status of the ask password interaction.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
GTlsInteractionResult
Packit ae235b
g_tls_interaction_invoke_ask_password (GTlsInteraction    *interaction,
Packit ae235b
                                       GTlsPassword       *password,
Packit ae235b
                                       GCancellable       *cancellable,
Packit ae235b
                                       GError            **error)
Packit ae235b
{
Packit ae235b
  GTlsInteractionResult result;
Packit ae235b
  InvokeClosure *closure;
Packit ae235b
  GTlsInteractionClass *klass;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
Packit ae235b
  klass = G_TLS_INTERACTION_GET_CLASS (interaction);
Packit ae235b
Packit ae235b
  if (klass->ask_password)
Packit ae235b
    {
Packit ae235b
      closure = invoke_closure_new (interaction, G_OBJECT (password), cancellable);
Packit ae235b
      g_main_context_invoke (interaction->priv->context,
Packit ae235b
                             on_invoke_ask_password_sync, closure);
Packit ae235b
      result = invoke_closure_wait_and_free (closure, error);
Packit ae235b
    }
Packit ae235b
  else if (klass->ask_password_async)
Packit ae235b
    {
Packit ae235b
      g_return_val_if_fail (klass->ask_password_finish, G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
Packit ae235b
      closure = invoke_closure_new (interaction, G_OBJECT (password), cancellable);
Packit ae235b
      g_main_context_invoke (interaction->priv->context,
Packit ae235b
                             on_invoke_ask_password_async_as_sync, closure);
Packit ae235b
Packit ae235b
      result = invoke_closure_complete_and_free (interaction, closure, error);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      result = G_TLS_INTERACTION_UNHANDLED;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_interaction_ask_password:
Packit ae235b
 * @interaction: a #GTlsInteraction object
Packit ae235b
 * @password: a #GTlsPassword object
Packit ae235b
 * @cancellable: an optional #GCancellable cancellation object
Packit ae235b
 * @error: an optional location to place an error on failure
Packit ae235b
 *
Packit ae235b
 * Run synchronous interaction to ask the user for a password. In general,
Packit ae235b
 * g_tls_interaction_invoke_ask_password() should be used instead of this
Packit ae235b
 * function.
Packit ae235b
 *
Packit ae235b
 * Derived subclasses usually implement a password prompt, although they may
Packit ae235b
 * also choose to provide a password from elsewhere. The @password value will
Packit ae235b
 * be filled in and then @callback will be called. Alternatively the user may
Packit ae235b
 * abort this password request, which will usually abort the TLS connection.
Packit ae235b
 *
Packit ae235b
 * If the interaction is cancelled by the cancellation object, or by the
Packit ae235b
 * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
Packit ae235b
 * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
Packit ae235b
 * not support immediate cancellation.
Packit ae235b
 *
Packit ae235b
 * Returns: The status of the ask password interaction.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
GTlsInteractionResult
Packit ae235b
g_tls_interaction_ask_password (GTlsInteraction    *interaction,
Packit ae235b
                                GTlsPassword       *password,
Packit ae235b
                                GCancellable       *cancellable,
Packit ae235b
                                GError            **error)
Packit ae235b
{
Packit ae235b
  GTlsInteractionClass *klass;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
Packit ae235b
  klass = G_TLS_INTERACTION_GET_CLASS (interaction);
Packit ae235b
  if (klass->ask_password)
Packit ae235b
    return (klass->ask_password) (interaction, password, cancellable, error);
Packit ae235b
  else
Packit ae235b
    return G_TLS_INTERACTION_UNHANDLED;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_interaction_ask_password_async:
Packit ae235b
 * @interaction: a #GTlsInteraction object
Packit ae235b
 * @password: a #GTlsPassword object
Packit ae235b
 * @cancellable: an optional #GCancellable cancellation object
Packit ae235b
 * @callback: (nullable): will be called when the interaction completes
Packit ae235b
 * @user_data: (nullable): data to pass to the @callback
Packit ae235b
 *
Packit ae235b
 * Run asynchronous interaction to ask the user for a password. In general,
Packit ae235b
 * g_tls_interaction_invoke_ask_password() should be used instead of this
Packit ae235b
 * function.
Packit ae235b
 *
Packit ae235b
 * Derived subclasses usually implement a password prompt, although they may
Packit ae235b
 * also choose to provide a password from elsewhere. The @password value will
Packit ae235b
 * be filled in and then @callback will be called. Alternatively the user may
Packit ae235b
 * abort this password request, which will usually abort the TLS connection.
Packit ae235b
 *
Packit ae235b
 * If the interaction is cancelled by the cancellation object, or by the
Packit ae235b
 * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
Packit ae235b
 * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
Packit ae235b
 * not support immediate cancellation.
Packit ae235b
 *
Packit ae235b
 * Certain implementations may not support immediate cancellation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_interaction_ask_password_async (GTlsInteraction    *interaction,
Packit ae235b
                                      GTlsPassword       *password,
Packit ae235b
                                      GCancellable       *cancellable,
Packit ae235b
                                      GAsyncReadyCallback callback,
Packit ae235b
                                      gpointer            user_data)
Packit ae235b
{
Packit ae235b
  GTlsInteractionClass *klass;
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_TLS_INTERACTION (interaction));
Packit ae235b
  g_return_if_fail (G_IS_TLS_PASSWORD (password));
Packit ae235b
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
Packit ae235b
Packit ae235b
  klass = G_TLS_INTERACTION_GET_CLASS (interaction);
Packit ae235b
  if (klass->ask_password_async)
Packit ae235b
    {
Packit ae235b
      g_return_if_fail (klass->ask_password_finish);
Packit ae235b
      (klass->ask_password_async) (interaction, password, cancellable,
Packit ae235b
                                   callback, user_data);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      task = g_task_new (interaction, cancellable, callback, user_data);
Packit ae235b
      g_task_set_source_tag (task, g_tls_interaction_ask_password_async);
Packit ae235b
      g_task_return_int (task, G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
      g_object_unref (task);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_interaction_ask_password_finish:
Packit ae235b
 * @interaction: a #GTlsInteraction object
Packit ae235b
 * @result: the result passed to the callback
Packit ae235b
 * @error: an optional location to place an error on failure
Packit ae235b
 *
Packit ae235b
 * Complete an ask password user interaction request. This should be once
Packit ae235b
 * the g_tls_interaction_ask_password_async() completion callback is called.
Packit ae235b
 *
Packit ae235b
 * If %G_TLS_INTERACTION_HANDLED is returned, then the #GTlsPassword passed
Packit ae235b
 * to g_tls_interaction_ask_password() will have its password filled in.
Packit ae235b
 *
Packit ae235b
 * If the interaction is cancelled by the cancellation object, or by the
Packit ae235b
 * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
Packit ae235b
 * contains a %G_IO_ERROR_CANCELLED error code.
Packit ae235b
 *
Packit ae235b
 * Returns: The status of the ask password interaction.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
GTlsInteractionResult
Packit ae235b
g_tls_interaction_ask_password_finish (GTlsInteraction    *interaction,
Packit ae235b
                                       GAsyncResult       *result,
Packit ae235b
                                       GError            **error)
Packit ae235b
{
Packit ae235b
  GTlsInteractionClass *klass;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
Packit ae235b
  klass = G_TLS_INTERACTION_GET_CLASS (interaction);
Packit ae235b
  if (klass->ask_password_finish)
Packit ae235b
    {
Packit ae235b
      g_return_val_if_fail (klass->ask_password_async != NULL, G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
Packit ae235b
      return (klass->ask_password_finish) (interaction, result, error);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_return_val_if_fail (g_async_result_is_tagged (result, g_tls_interaction_ask_password_async), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
Packit ae235b
      return g_task_propagate_int (G_TASK (result), error);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
on_invoke_request_certificate_sync (gpointer user_data)
Packit ae235b
{
Packit ae235b
  InvokeClosure *closure = user_data;
Packit ae235b
  GTlsInteractionClass *klass;
Packit ae235b
Packit ae235b
  g_mutex_lock (&closure->mutex);
Packit ae235b
Packit ae235b
  klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
Packit ae235b
  g_assert (klass->request_certificate != NULL);
Packit ae235b
Packit ae235b
  closure->result = klass->request_certificate (closure->interaction,
Packit ae235b
                                                G_TLS_CONNECTION (closure->argument),
Packit ae235b
                                                0,
Packit ae235b
                                                closure->cancellable,
Packit ae235b
                                                &closure->error);
Packit ae235b
Packit ae235b
  closure->complete = TRUE;
Packit ae235b
  g_cond_signal (&closure->cond);
Packit ae235b
  g_mutex_unlock (&closure->mutex);
Packit ae235b
Packit ae235b
  return FALSE; /* don't call again */
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
on_request_certificate_complete (GObject      *source,
Packit ae235b
                                 GAsyncResult *result,
Packit ae235b
                                 gpointer      user_data)
Packit ae235b
{
Packit ae235b
  InvokeClosure *closure = user_data;
Packit ae235b
  GTlsInteractionClass *klass;
Packit ae235b
Packit ae235b
  g_mutex_lock (&closure->mutex);
Packit ae235b
Packit ae235b
  klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
Packit ae235b
  g_assert (klass->request_certificate_finish != NULL);
Packit ae235b
Packit ae235b
  closure->result = klass->request_certificate_finish (closure->interaction,
Packit ae235b
                                                       result, &closure->error);
Packit ae235b
Packit ae235b
  closure->complete = TRUE;
Packit ae235b
  g_cond_signal (&closure->cond);
Packit ae235b
  g_mutex_unlock (&closure->mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
on_invoke_request_certificate_async_as_sync (gpointer user_data)
Packit ae235b
{
Packit ae235b
  InvokeClosure *closure = user_data;
Packit ae235b
  GTlsInteractionClass *klass;
Packit ae235b
Packit ae235b
  g_mutex_lock (&closure->mutex);
Packit ae235b
Packit ae235b
  klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
Packit ae235b
  g_assert (klass->request_certificate_async);
Packit ae235b
Packit ae235b
  klass->request_certificate_async (closure->interaction,
Packit ae235b
                                    G_TLS_CONNECTION (closure->argument), 0,
Packit ae235b
                                    closure->cancellable,
Packit ae235b
                                    on_request_certificate_complete,
Packit ae235b
                                    closure);
Packit ae235b
Packit ae235b
  /* Note that we've used these */
Packit ae235b
  closure->callback = NULL;
Packit ae235b
  closure->user_data = NULL;
Packit ae235b
Packit ae235b
  g_mutex_unlock (&closure->mutex);
Packit ae235b
Packit ae235b
  return FALSE; /* don't call again */
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_interaction_invoke_request_certificate:
Packit ae235b
 * @interaction: a #GTlsInteraction object
Packit ae235b
 * @connection: a #GTlsConnection object
Packit ae235b
 * @flags: flags providing more information about the request
Packit ae235b
 * @cancellable: an optional #GCancellable cancellation object
Packit ae235b
 * @error: an optional location to place an error on failure
Packit ae235b
 *
Packit ae235b
 * Invoke the interaction to ask the user to choose a certificate to
Packit ae235b
 * use with the connection. It invokes this interaction in the main
Packit ae235b
 * loop, specifically the #GMainContext returned by
Packit ae235b
 * g_main_context_get_thread_default() when the interaction is
Packit ae235b
 * created. This is called by called by #GTlsConnection when the peer
Packit ae235b
 * requests a certificate during the handshake.
Packit ae235b
 *
Packit ae235b
 * Derived subclasses usually implement a certificate selector,
Packit ae235b
 * although they may also choose to provide a certificate from
Packit ae235b
 * elsewhere. Alternatively the user may abort this certificate
Packit ae235b
 * request, which may or may not abort the TLS connection.
Packit ae235b
 *
Packit ae235b
 * The implementation can either be a synchronous (eg: modal dialog) or an
Packit ae235b
 * asynchronous one (eg: modeless dialog). This function will take care of
Packit ae235b
 * calling which ever one correctly.
Packit ae235b
 *
Packit ae235b
 * If the interaction is cancelled by the cancellation object, or by the
Packit ae235b
 * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
Packit ae235b
 * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
Packit ae235b
 * not support immediate cancellation.
Packit ae235b
 *
Packit ae235b
 * Returns: The status of the certificate request interaction.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
GTlsInteractionResult
Packit ae235b
g_tls_interaction_invoke_request_certificate (GTlsInteraction    *interaction,
Packit ae235b
                                              GTlsConnection               *connection,
Packit ae235b
                                              GTlsCertificateRequestFlags   flags,
Packit ae235b
                                              GCancellable       *cancellable,
Packit ae235b
                                              GError            **error)
Packit ae235b
{
Packit ae235b
  GTlsInteractionResult result;
Packit ae235b
  InvokeClosure *closure;
Packit ae235b
  GTlsInteractionClass *klass;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CONNECTION (connection), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
Packit ae235b
  klass = G_TLS_INTERACTION_GET_CLASS (interaction);
Packit ae235b
Packit ae235b
  if (klass->request_certificate)
Packit ae235b
    {
Packit ae235b
      closure = invoke_closure_new (interaction, G_OBJECT (connection), cancellable);
Packit ae235b
      g_main_context_invoke (interaction->priv->context,
Packit ae235b
                             on_invoke_request_certificate_sync, closure);
Packit ae235b
      result = invoke_closure_wait_and_free (closure, error);
Packit ae235b
    }
Packit ae235b
  else if (klass->request_certificate_async)
Packit ae235b
    {
Packit ae235b
      g_return_val_if_fail (klass->request_certificate_finish, G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
Packit ae235b
      closure = invoke_closure_new (interaction, G_OBJECT (connection), cancellable);
Packit ae235b
      g_main_context_invoke (interaction->priv->context,
Packit ae235b
                             on_invoke_request_certificate_async_as_sync, closure);
Packit ae235b
Packit ae235b
      result = invoke_closure_complete_and_free (interaction, closure, error);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      result = G_TLS_INTERACTION_UNHANDLED;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_interaction_request_certificate:
Packit ae235b
 * @interaction: a #GTlsInteraction object
Packit ae235b
 * @connection: a #GTlsConnection object
Packit ae235b
 * @flags: flags providing more information about the request
Packit ae235b
 * @cancellable: an optional #GCancellable cancellation object
Packit ae235b
 * @error: an optional location to place an error on failure
Packit ae235b
 *
Packit ae235b
 * Run synchronous interaction to ask the user to choose a certificate to use
Packit ae235b
 * with the connection. In general, g_tls_interaction_invoke_request_certificate()
Packit ae235b
 * should be used instead of this function.
Packit ae235b
 *
Packit ae235b
 * Derived subclasses usually implement a certificate selector, although they may
Packit ae235b
 * also choose to provide a certificate from elsewhere. Alternatively the user may
Packit ae235b
 * abort this certificate request, which will usually abort the TLS connection.
Packit ae235b
 *
Packit ae235b
 * If %G_TLS_INTERACTION_HANDLED is returned, then the #GTlsConnection
Packit ae235b
 * passed to g_tls_interaction_request_certificate() will have had its
Packit ae235b
 * #GTlsConnection:certificate filled in.
Packit ae235b
 *
Packit ae235b
 * If the interaction is cancelled by the cancellation object, or by the
Packit ae235b
 * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
Packit ae235b
 * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
Packit ae235b
 * not support immediate cancellation.
Packit ae235b
 *
Packit ae235b
 * Returns: The status of the request certificate interaction.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
GTlsInteractionResult
Packit ae235b
g_tls_interaction_request_certificate (GTlsInteraction              *interaction,
Packit ae235b
                                       GTlsConnection               *connection,
Packit ae235b
                                       GTlsCertificateRequestFlags   flags,
Packit ae235b
                                       GCancellable                 *cancellable,
Packit ae235b
                                       GError                      **error)
Packit ae235b
{
Packit ae235b
  GTlsInteractionClass *klass;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CONNECTION (connection), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
Packit ae235b
  klass = G_TLS_INTERACTION_GET_CLASS (interaction);
Packit ae235b
  if (klass->request_certificate)
Packit ae235b
    return (klass->request_certificate) (interaction, connection, flags, cancellable, error);
Packit ae235b
  else
Packit ae235b
    return G_TLS_INTERACTION_UNHANDLED;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_interaction_request_certificate_async:
Packit ae235b
 * @interaction: a #GTlsInteraction object
Packit ae235b
 * @connection: a #GTlsConnection object
Packit ae235b
 * @flags: flags providing more information about the request
Packit ae235b
 * @cancellable: an optional #GCancellable cancellation object
Packit ae235b
 * @callback: (nullable): will be called when the interaction completes
Packit ae235b
 * @user_data: (nullable): data to pass to the @callback
Packit ae235b
 *
Packit ae235b
 * Run asynchronous interaction to ask the user for a certificate to use with
Packit ae235b
 * the connection. In general, g_tls_interaction_invoke_request_certificate() should
Packit ae235b
 * be used instead of this function.
Packit ae235b
 *
Packit ae235b
 * Derived subclasses usually implement a certificate selector, although they may
Packit ae235b
 * also choose to provide a certificate from elsewhere. @callback will be called
Packit ae235b
 * when the operation completes. Alternatively the user may abort this certificate
Packit ae235b
 * request, which will usually abort the TLS connection.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_interaction_request_certificate_async (GTlsInteraction              *interaction,
Packit ae235b
                                             GTlsConnection               *connection,
Packit ae235b
                                             GTlsCertificateRequestFlags   flags,
Packit ae235b
                                             GCancellable                 *cancellable,
Packit ae235b
                                             GAsyncReadyCallback           callback,
Packit ae235b
                                             gpointer                      user_data)
Packit ae235b
{
Packit ae235b
  GTlsInteractionClass *klass;
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_TLS_INTERACTION (interaction));
Packit ae235b
  g_return_if_fail (G_IS_TLS_CONNECTION (connection));
Packit ae235b
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
Packit ae235b
Packit ae235b
  klass = G_TLS_INTERACTION_GET_CLASS (interaction);
Packit ae235b
  if (klass->request_certificate_async)
Packit ae235b
    {
Packit ae235b
      g_return_if_fail (klass->request_certificate_finish);
Packit ae235b
      (klass->request_certificate_async) (interaction, connection, flags,
Packit ae235b
                                          cancellable, callback, user_data);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      task = g_task_new (interaction, cancellable, callback, user_data);
Packit ae235b
      g_task_set_source_tag (task, g_tls_interaction_request_certificate_async);
Packit ae235b
      g_task_return_int (task, G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
      g_object_unref (task);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_interaction_request_certificate_finish:
Packit ae235b
 * @interaction: a #GTlsInteraction object
Packit ae235b
 * @result: the result passed to the callback
Packit ae235b
 * @error: an optional location to place an error on failure
Packit ae235b
 *
Packit ae235b
 * Complete an request certificate user interaction request. This should be once
Packit ae235b
 * the g_tls_interaction_request_certificate_async() completion callback is called.
Packit ae235b
 *
Packit ae235b
 * If %G_TLS_INTERACTION_HANDLED is returned, then the #GTlsConnection
Packit ae235b
 * passed to g_tls_interaction_request_certificate_async() will have had its
Packit ae235b
 * #GTlsConnection:certificate filled in.
Packit ae235b
 *
Packit ae235b
 * If the interaction is cancelled by the cancellation object, or by the
Packit ae235b
 * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
Packit ae235b
 * contains a %G_IO_ERROR_CANCELLED error code.
Packit ae235b
 *
Packit ae235b
 * Returns: The status of the request certificate interaction.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
GTlsInteractionResult
Packit ae235b
g_tls_interaction_request_certificate_finish (GTlsInteraction    *interaction,
Packit ae235b
                                              GAsyncResult       *result,
Packit ae235b
                                              GError            **error)
Packit ae235b
{
Packit ae235b
  GTlsInteractionClass *klass;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
Packit ae235b
  klass = G_TLS_INTERACTION_GET_CLASS (interaction);
Packit ae235b
  if (klass->request_certificate_finish)
Packit ae235b
    {
Packit ae235b
      g_return_val_if_fail (klass->request_certificate_async != NULL, G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
Packit ae235b
      return (klass->request_certificate_finish) (interaction, result, error);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_return_val_if_fail (g_async_result_is_tagged (result, g_tls_interaction_request_certificate_async), G_TLS_INTERACTION_UNHANDLED);
Packit ae235b
Packit ae235b
      return g_task_propagate_int (G_TASK (result), error);
Packit ae235b
    }
Packit ae235b
}