Blame gio/gtlspassword.c

Packit 84794d
/* GIO - GLib Input, Output and Streaming Library
Packit 84794d
 *
Packit 84794d
 * Copyright (C) 2011 Collabora, Ltd.
Packit 84794d
 *
Packit 84794d
 * This library is free software; you can redistribute it and/or
Packit 84794d
 * modify it under the terms of the GNU Lesser General Public
Packit 84794d
 * License as published by the Free Software Foundation; either
Packit 84794d
 * version 2.1 of the License, or (at your option) any later version.
Packit 84794d
 *
Packit 84794d
 * This library is distributed in the hope that it will be useful,
Packit 84794d
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 84794d
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 84794d
 * Lesser General Public License for more details.
Packit 84794d
 *
Packit 84794d
 * You should have received a copy of the GNU Lesser General
Packit 84794d
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit 84794d
 *
Packit 84794d
 * Author: Stef Walter <stefw@collabora.co.uk>
Packit 84794d
 */
Packit 84794d
Packit 84794d
#include "config.h"
Packit 84794d
#include "glib.h"
Packit 84794d
#include "glibintl.h"
Packit 84794d
Packit 84794d
#include "gioenumtypes.h"
Packit 84794d
#include "gtlspassword.h"
Packit 84794d
Packit 84794d
#include <string.h>
Packit 84794d
Packit 84794d
/**
Packit 84794d
 * SECTION:gtlspassword
Packit 84794d
 * @title: GTlsPassword
Packit 84794d
 * @short_description: TLS Passwords for prompting
Packit 84794d
 * @include: gio/gio.h
Packit 84794d
 *
Packit 84794d
 * Holds a password used in TLS.
Packit 84794d
 */
Packit 84794d
Packit 84794d
/**
Packit 84794d
 * GTlsPassword:
Packit 84794d
 *
Packit 84794d
 * An abstract interface representing a password used in TLS. Often used in
Packit 84794d
 * user interaction such as unlocking a key storage token.
Packit 84794d
 *
Packit 84794d
 * Since: 2.30
Packit 84794d
 */
Packit 84794d
Packit 84794d
enum
Packit 84794d
{
Packit 84794d
  PROP_0,
Packit 84794d
  PROP_FLAGS,
Packit 84794d
  PROP_DESCRIPTION,
Packit 84794d
  PROP_WARNING
Packit 84794d
};
Packit 84794d
Packit 84794d
struct _GTlsPasswordPrivate
Packit 84794d
{
Packit 84794d
  guchar *value;
Packit 84794d
  gsize length;
Packit 84794d
  GDestroyNotify destroy;
Packit 84794d
  GTlsPasswordFlags flags;
Packit 84794d
  gchar *description;
Packit 84794d
  gchar *warning;
Packit 84794d
};
Packit 84794d
Packit 84794d
G_DEFINE_TYPE_WITH_PRIVATE (GTlsPassword, g_tls_password, G_TYPE_OBJECT)
Packit 84794d
Packit 84794d
static void
Packit 84794d
g_tls_password_init (GTlsPassword *password)
Packit 84794d
{
Packit 84794d
  password->priv = g_tls_password_get_instance_private (password);
Packit 84794d
}
Packit 84794d
Packit 84794d
static const guchar *
Packit 84794d
g_tls_password_real_get_value (GTlsPassword  *password,
Packit 84794d
                               gsize         *length)
Packit 84794d
{
Packit 84794d
  if (length)
Packit 84794d
    *length = password->priv->length;
Packit 84794d
  return password->priv->value;
Packit 84794d
}
Packit 84794d
Packit 84794d
static void
Packit 84794d
g_tls_password_real_set_value (GTlsPassword   *password,
Packit 84794d
                               guchar         *value,
Packit 84794d
                               gssize          length,
Packit 84794d
                               GDestroyNotify  destroy)
Packit 84794d
{
Packit 84794d
  if (password->priv->destroy)
Packit 84794d
      (password->priv->destroy) (password->priv->value);
Packit 84794d
  password->priv->destroy = NULL;
Packit 84794d
  password->priv->value = NULL;
Packit 84794d
  password->priv->length = 0;
Packit 84794d
Packit 84794d
  if (length < 0)
Packit 84794d
    length = strlen ((gchar*) value);
Packit 84794d
Packit 84794d
  password->priv->value = value;
Packit 84794d
  password->priv->length = length;
Packit 84794d
  password->priv->destroy = destroy;
Packit 84794d
}
Packit 84794d
Packit 84794d
static const gchar*
Packit 84794d
g_tls_password_real_get_default_warning (GTlsPassword  *password)
Packit 84794d
{
Packit 84794d
  GTlsPasswordFlags flags;
Packit 84794d
Packit 84794d
  flags = g_tls_password_get_flags (password);
Packit 84794d
Packit 84794d
  if (flags & G_TLS_PASSWORD_FINAL_TRY)
Packit 84794d
    return _("This is the last chance to enter the password correctly before your access is locked out.");
Packit 84794d
  if (flags & G_TLS_PASSWORD_MANY_TRIES)
Packit 84794d
    /* Translators: This is not the 'This is the last chance' string. It is
Packit 84794d
     * displayed when more than one attempt is allowed. */
Packit 84794d
    return _("Several passwords entered have been incorrect, and your access will be locked out after further failures.");
Packit 84794d
  if (flags & G_TLS_PASSWORD_RETRY)
Packit 84794d
    return _("The password entered is incorrect.");
Packit 84794d
Packit 84794d
  return NULL;
Packit 84794d
}
Packit 84794d
Packit 84794d
static void
Packit 84794d
g_tls_password_get_property (GObject    *object,
Packit 84794d
                             guint       prop_id,
Packit 84794d
                             GValue     *value,
Packit 84794d
                             GParamSpec *pspec)
Packit 84794d
{
Packit 84794d
  GTlsPassword *password = G_TLS_PASSWORD (object);
Packit 84794d
Packit 84794d
  switch (prop_id)
Packit 84794d
    {
Packit 84794d
    case PROP_FLAGS:
Packit 84794d
      g_value_set_flags (value, g_tls_password_get_flags (password));
Packit 84794d
      break;
Packit 84794d
    case PROP_WARNING:
Packit 84794d
      g_value_set_string (value, g_tls_password_get_warning (password));
Packit 84794d
      break;
Packit 84794d
    case PROP_DESCRIPTION:
Packit 84794d
      g_value_set_string (value, g_tls_password_get_description (password));
Packit 84794d
      break;
Packit 84794d
    default:
Packit 84794d
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 84794d
      break;
Packit 84794d
    }
Packit 84794d
}
Packit 84794d
Packit 84794d
static void
Packit 84794d
g_tls_password_set_property (GObject      *object,
Packit 84794d
                             guint         prop_id,
Packit 84794d
                             const GValue *value,
Packit 84794d
                             GParamSpec   *pspec)
Packit 84794d
{
Packit 84794d
  GTlsPassword *password = G_TLS_PASSWORD (object);
Packit 84794d
Packit 84794d
  switch (prop_id)
Packit 84794d
    {
Packit 84794d
    case PROP_FLAGS:
Packit 84794d
      g_tls_password_set_flags (password, g_value_get_flags (value));
Packit 84794d
      break;
Packit 84794d
    case PROP_WARNING:
Packit 84794d
      g_tls_password_set_warning (password, g_value_get_string (value));
Packit 84794d
      break;
Packit 84794d
    case PROP_DESCRIPTION:
Packit 84794d
      g_tls_password_set_description (password, g_value_get_string (value));
Packit 84794d
      break;
Packit 84794d
    default:
Packit 84794d
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 84794d
      break;
Packit 84794d
    }
Packit 84794d
}
Packit 84794d
Packit 84794d
static void
Packit 84794d
g_tls_password_finalize (GObject *object)
Packit 84794d
{
Packit 84794d
  GTlsPassword *password = G_TLS_PASSWORD (object);
Packit 84794d
Packit 84794d
  g_tls_password_real_set_value (password, NULL, 0, NULL);
Packit 84794d
  g_free (password->priv->warning);
Packit 84794d
  g_free (password->priv->description);
Packit 84794d
Packit 84794d
  G_OBJECT_CLASS (g_tls_password_parent_class)->finalize (object);
Packit 84794d
}
Packit 84794d
Packit 84794d
static void
Packit 84794d
g_tls_password_class_init (GTlsPasswordClass *klass)
Packit 84794d
{
Packit 84794d
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit 84794d
Packit 84794d
  klass->get_value = g_tls_password_real_get_value;
Packit 84794d
  klass->set_value = g_tls_password_real_set_value;
Packit 84794d
  klass->get_default_warning = g_tls_password_real_get_default_warning;
Packit 84794d
Packit 84794d
  gobject_class->get_property = g_tls_password_get_property;
Packit 84794d
  gobject_class->set_property = g_tls_password_set_property;
Packit 84794d
  gobject_class->finalize = g_tls_password_finalize;
Packit 84794d
Packit 84794d
  g_object_class_install_property (gobject_class, PROP_FLAGS,
Packit 84794d
				   g_param_spec_flags ("flags",
Packit 84794d
						       P_("Flags"),
Packit 84794d
						       P_("Flags about the password"),
Packit 84794d
						       G_TYPE_TLS_PASSWORD_FLAGS,
Packit 84794d
						       G_TLS_PASSWORD_NONE,
Packit 84794d
						       G_PARAM_READWRITE |
Packit 84794d
						       G_PARAM_STATIC_STRINGS));
Packit 84794d
Packit 84794d
  g_object_class_install_property (gobject_class, PROP_DESCRIPTION,
Packit 84794d
				   g_param_spec_string ("description",
Packit 84794d
							P_("Description"),
Packit 84794d
							P_("Description of what the password is for"),
Packit 84794d
							NULL,
Packit 84794d
							G_PARAM_READWRITE |
Packit 84794d
							G_PARAM_STATIC_STRINGS));
Packit 84794d
Packit 84794d
  g_object_class_install_property (gobject_class, PROP_WARNING,
Packit 84794d
				   g_param_spec_string ("warning",
Packit 84794d
							P_("Warning"),
Packit 84794d
							P_("Warning about the password"),
Packit 84794d
							NULL,
Packit 84794d
							G_PARAM_READWRITE |
Packit 84794d
							G_PARAM_STATIC_STRINGS));
Packit 84794d
Packit 84794d
}
Packit 84794d
Packit 84794d
/**
Packit 84794d
 * g_tls_password_new:
Packit 84794d
 * @flags: the password flags
Packit 84794d
 * @description: description of what the password is for
Packit 84794d
 *
Packit 84794d
 * Create a new #GTlsPassword object.
Packit 84794d
 *
Packit 84794d
 * Returns: (transfer full): The newly allocated password object
Packit 84794d
 */
Packit 84794d
GTlsPassword *
Packit 84794d
g_tls_password_new (GTlsPasswordFlags  flags,
Packit 84794d
                    const gchar       *description)
Packit 84794d
{
Packit 84794d
  return g_object_new (G_TYPE_TLS_PASSWORD,
Packit 84794d
                       "flags", flags,
Packit 84794d
                       "description", description,
Packit 84794d
                       NULL);
Packit 84794d
}
Packit 84794d
Packit 84794d
/**
Packit 84794d
 * g_tls_password_get_value:
Packit 84794d
 * @password: a #GTlsPassword object
Packit 84794d
 * @length: (nullable): location to place the length of the password.
Packit 84794d
 *
Packit 84794d
 * Get the password value. If @length is not %NULL then it will be
Packit 84794d
 * filled in with the length of the password value. (Note that the
Packit 84794d
 * password value is not nul-terminated, so you can only pass %NULL
Packit 84794d
 * for @length in contexts where you know the password will have a
Packit 84794d
 * certain fixed length.)
Packit 84794d
 *
Packit 84794d
 * Returns: The password value (owned by the password object).
Packit 84794d
 *
Packit 84794d
 * Since: 2.30
Packit 84794d
 */
Packit 84794d
const guchar *
Packit 84794d
g_tls_password_get_value (GTlsPassword  *password,
Packit 84794d
                          gsize         *length)
Packit 84794d
{
Packit 84794d
  g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
Packit 84794d
  return G_TLS_PASSWORD_GET_CLASS (password)->get_value (password, length);
Packit 84794d
}
Packit 84794d
Packit 84794d
/**
Packit 84794d
 * g_tls_password_set_value:
Packit 84794d
 * @password: a #GTlsPassword object
Packit 84794d
 * @value: (array length=length): the new password value
Packit 84794d
 * @length: the length of the password, or -1
Packit 84794d
 *
Packit 84794d
 * Set the value for this password. The @value will be copied by the password
Packit 84794d
 * object.
Packit 84794d
 *
Packit 84794d
 * Specify the @length, for a non-nul-terminated password. Pass -1 as
Packit 84794d
 * @length if using a nul-terminated password, and @length will be
Packit 84794d
 * calculated automatically. (Note that the terminating nul is not
Packit 84794d
 * considered part of the password in this case.)
Packit 84794d
 *
Packit 84794d
 * Since: 2.30
Packit 84794d
 */
Packit 84794d
void
Packit 84794d
g_tls_password_set_value (GTlsPassword  *password,
Packit 84794d
                          const guchar  *value,
Packit 84794d
                          gssize         length)
Packit 84794d
{
Packit 84794d
  g_return_if_fail (G_IS_TLS_PASSWORD (password));
Packit 84794d
Packit 84794d
  if (length < 0)
Packit 84794d
    length = strlen ((gchar *)value);
Packit 84794d
Packit 84794d
  g_tls_password_set_value_full (password, g_memdup (value, length), length, g_free);
Packit 84794d
}
Packit 84794d
Packit 84794d
/**
Packit 84794d
 * g_tls_password_set_value_full:
Packit 84794d
 * @password: a #GTlsPassword object
Packit 84794d
 * @value: (array length=length): the value for the password
Packit 84794d
 * @length: the length of the password, or -1
Packit 84794d
 * @destroy: (nullable): a function to use to free the password.
Packit 84794d
 *
Packit 84794d
 * Provide the value for this password.
Packit 84794d
 *
Packit 84794d
 * The @value will be owned by the password object, and later freed using
Packit 84794d
 * the @destroy function callback.
Packit 84794d
 *
Packit 84794d
 * Specify the @length, for a non-nul-terminated password. Pass -1 as
Packit 84794d
 * @length if using a nul-terminated password, and @length will be
Packit 84794d
 * calculated automatically. (Note that the terminating nul is not
Packit 84794d
 * considered part of the password in this case.)
Packit 84794d
 *
Packit 84794d
 * Virtual: set_value
Packit 84794d
 * Since: 2.30
Packit 84794d
 */
Packit 84794d
void
Packit 84794d
g_tls_password_set_value_full (GTlsPassword   *password,
Packit 84794d
                               guchar         *value,
Packit 84794d
                               gssize          length,
Packit 84794d
                               GDestroyNotify  destroy)
Packit 84794d
{
Packit 84794d
  g_return_if_fail (G_IS_TLS_PASSWORD (password));
Packit 84794d
  G_TLS_PASSWORD_GET_CLASS (password)->set_value (password, value,
Packit 84794d
                                                  length, destroy);
Packit 84794d
}
Packit 84794d
Packit 84794d
/**
Packit 84794d
 * g_tls_password_get_flags:
Packit 84794d
 * @password: a #GTlsPassword object
Packit 84794d
 *
Packit 84794d
 * Get flags about the password.
Packit 84794d
 *
Packit 84794d
 * Returns: The flags about the password.
Packit 84794d
 *
Packit 84794d
 * Since: 2.30
Packit 84794d
 */
Packit 84794d
GTlsPasswordFlags
Packit 84794d
g_tls_password_get_flags (GTlsPassword *password)
Packit 84794d
{
Packit 84794d
  g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_PASSWORD_NONE);
Packit 84794d
  return password->priv->flags;
Packit 84794d
}
Packit 84794d
Packit 84794d
/**
Packit 84794d
 * g_tls_password_set_flags:
Packit 84794d
 * @password: a #GTlsPassword object
Packit 84794d
 * @flags: The flags about the password
Packit 84794d
 *
Packit 84794d
 * Set flags about the password.
Packit 84794d
 *
Packit 84794d
 * Since: 2.30
Packit 84794d
 */
Packit 84794d
void
Packit 84794d
g_tls_password_set_flags (GTlsPassword      *password,
Packit 84794d
                          GTlsPasswordFlags  flags)
Packit 84794d
{
Packit 84794d
  g_return_if_fail (G_IS_TLS_PASSWORD (password));
Packit 84794d
Packit 84794d
  password->priv->flags = flags;
Packit 84794d
Packit 84794d
  g_object_notify (G_OBJECT (password), "flags");
Packit 84794d
}
Packit 84794d
Packit 84794d
/**
Packit 84794d
 * g_tls_password_get_description:
Packit 84794d
 * @password: a #GTlsPassword object
Packit 84794d
 *
Packit 84794d
 * Get a description string about what the password will be used for.
Packit 84794d
 *
Packit 84794d
 * Returns: The description of the password.
Packit 84794d
 *
Packit 84794d
 * Since: 2.30
Packit 84794d
 */
Packit 84794d
const gchar*
Packit 84794d
g_tls_password_get_description (GTlsPassword *password)
Packit 84794d
{
Packit 84794d
  g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
Packit 84794d
  return password->priv->description;
Packit 84794d
}
Packit 84794d
Packit 84794d
/**
Packit 84794d
 * g_tls_password_set_description:
Packit 84794d
 * @password: a #GTlsPassword object
Packit 84794d
 * @description: The description of the password
Packit 84794d
 *
Packit 84794d
 * Set a description string about what the password will be used for.
Packit 84794d
 *
Packit 84794d
 * Since: 2.30
Packit 84794d
 */
Packit 84794d
void
Packit 84794d
g_tls_password_set_description (GTlsPassword      *password,
Packit 84794d
                                const gchar       *description)
Packit 84794d
{
Packit 84794d
  gchar *copy;
Packit 84794d
Packit 84794d
  g_return_if_fail (G_IS_TLS_PASSWORD (password));
Packit 84794d
Packit 84794d
  copy = g_strdup (description);
Packit 84794d
  g_free (password->priv->description);
Packit 84794d
  password->priv->description = copy;
Packit 84794d
Packit 84794d
  g_object_notify (G_OBJECT (password), "description");
Packit 84794d
}
Packit 84794d
Packit 84794d
/**
Packit 84794d
 * g_tls_password_get_warning:
Packit 84794d
 * @password: a #GTlsPassword object
Packit 84794d
 *
Packit 84794d
 * Get a user readable translated warning. Usually this warning is a
Packit 84794d
 * representation of the password flags returned from
Packit 84794d
 * g_tls_password_get_flags().
Packit 84794d
 *
Packit 84794d
 * Returns: The warning.
Packit 84794d
 *
Packit 84794d
 * Since: 2.30
Packit 84794d
 */
Packit 84794d
const gchar *
Packit 84794d
g_tls_password_get_warning (GTlsPassword      *password)
Packit 84794d
{
Packit 84794d
  g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
Packit 84794d
Packit 84794d
  if (password->priv->warning == NULL)
Packit 84794d
    return G_TLS_PASSWORD_GET_CLASS (password)->get_default_warning (password);
Packit 84794d
Packit 84794d
  return password->priv->warning;
Packit 84794d
}
Packit 84794d
Packit 84794d
/**
Packit 84794d
 * g_tls_password_set_warning:
Packit 84794d
 * @password: a #GTlsPassword object
Packit 84794d
 * @warning: The user readable warning
Packit 84794d
 *
Packit 84794d
 * Set a user readable translated warning. Usually this warning is a
Packit 84794d
 * representation of the password flags returned from
Packit 84794d
 * g_tls_password_get_flags().
Packit 84794d
 *
Packit 84794d
 * Since: 2.30
Packit 84794d
 */
Packit 84794d
void
Packit 84794d
g_tls_password_set_warning (GTlsPassword      *password,
Packit 84794d
                            const gchar       *warning)
Packit 84794d
{
Packit 84794d
  gchar *copy;
Packit 84794d
Packit 84794d
  g_return_if_fail (G_IS_TLS_PASSWORD (password));
Packit 84794d
Packit 84794d
  copy = g_strdup (warning);
Packit 84794d
  g_free (password->priv->warning);
Packit 84794d
  password->priv->warning = copy;
Packit 84794d
Packit 84794d
  g_object_notify (G_OBJECT (password), "warning");
Packit 84794d
}