Blame gio/gtlspassword.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
#include "glib.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
#include "gioenumtypes.h"
Packit ae235b
#include "gtlspassword.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gtlspassword
Packit ae235b
 * @title: GTlsPassword
Packit ae235b
 * @short_description: TLS Passwords for prompting
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * Holds a password used in TLS.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTlsPassword:
Packit ae235b
 *
Packit ae235b
 * An abstract interface representing a password used in TLS. Often used in
Packit ae235b
 * user interaction such as unlocking a key storage token.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  PROP_0,
Packit ae235b
  PROP_FLAGS,
Packit ae235b
  PROP_DESCRIPTION,
Packit ae235b
  PROP_WARNING
Packit ae235b
};
Packit ae235b
Packit ae235b
struct _GTlsPasswordPrivate
Packit ae235b
{
Packit ae235b
  guchar *value;
Packit ae235b
  gsize length;
Packit ae235b
  GDestroyNotify destroy;
Packit ae235b
  GTlsPasswordFlags flags;
Packit ae235b
  gchar *description;
Packit ae235b
  gchar *warning;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_PRIVATE (GTlsPassword, g_tls_password, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_password_init (GTlsPassword *password)
Packit ae235b
{
Packit ae235b
  password->priv = g_tls_password_get_instance_private (password);
Packit ae235b
}
Packit ae235b
Packit ae235b
static const guchar *
Packit ae235b
g_tls_password_real_get_value (GTlsPassword  *password,
Packit ae235b
                               gsize         *length)
Packit ae235b
{
Packit ae235b
  if (length)
Packit ae235b
    *length = password->priv->length;
Packit ae235b
  return password->priv->value;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_password_real_set_value (GTlsPassword   *password,
Packit ae235b
                               guchar         *value,
Packit ae235b
                               gssize          length,
Packit ae235b
                               GDestroyNotify  destroy)
Packit ae235b
{
Packit ae235b
  if (password->priv->destroy)
Packit ae235b
      (password->priv->destroy) (password->priv->value);
Packit ae235b
  password->priv->destroy = NULL;
Packit ae235b
  password->priv->value = NULL;
Packit ae235b
  password->priv->length = 0;
Packit ae235b
Packit ae235b
  if (length < 0)
Packit ae235b
    length = strlen ((gchar*) value);
Packit ae235b
Packit ae235b
  password->priv->value = value;
Packit ae235b
  password->priv->length = length;
Packit ae235b
  password->priv->destroy = destroy;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const gchar*
Packit ae235b
g_tls_password_real_get_default_warning (GTlsPassword  *password)
Packit ae235b
{
Packit ae235b
  GTlsPasswordFlags flags;
Packit ae235b
Packit ae235b
  flags = g_tls_password_get_flags (password);
Packit ae235b
Packit ae235b
  if (flags & G_TLS_PASSWORD_FINAL_TRY)
Packit ae235b
    return _("This is the last chance to enter the password correctly before your access is locked out.");
Packit ae235b
  if (flags & G_TLS_PASSWORD_MANY_TRIES)
Packit ae235b
    /* Translators: This is not the 'This is the last chance' string. It is
Packit ae235b
     * displayed when more than one attempt is allowed. */
Packit ae235b
    return _("Several passwords entered have been incorrect, and your access will be locked out after further failures.");
Packit ae235b
  if (flags & G_TLS_PASSWORD_RETRY)
Packit ae235b
    return _("The password entered is incorrect.");
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_password_get_property (GObject    *object,
Packit ae235b
                             guint       prop_id,
Packit ae235b
                             GValue     *value,
Packit ae235b
                             GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  GTlsPassword *password = G_TLS_PASSWORD (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_FLAGS:
Packit ae235b
      g_value_set_flags (value, g_tls_password_get_flags (password));
Packit ae235b
      break;
Packit ae235b
    case PROP_WARNING:
Packit ae235b
      g_value_set_string (value, g_tls_password_get_warning (password));
Packit ae235b
      break;
Packit ae235b
    case PROP_DESCRIPTION:
Packit ae235b
      g_value_set_string (value, g_tls_password_get_description (password));
Packit ae235b
      break;
Packit ae235b
    default:
Packit ae235b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_password_set_property (GObject      *object,
Packit ae235b
                             guint         prop_id,
Packit ae235b
                             const GValue *value,
Packit ae235b
                             GParamSpec   *pspec)
Packit ae235b
{
Packit ae235b
  GTlsPassword *password = G_TLS_PASSWORD (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_FLAGS:
Packit ae235b
      g_tls_password_set_flags (password, g_value_get_flags (value));
Packit ae235b
      break;
Packit ae235b
    case PROP_WARNING:
Packit ae235b
      g_tls_password_set_warning (password, g_value_get_string (value));
Packit ae235b
      break;
Packit ae235b
    case PROP_DESCRIPTION:
Packit ae235b
      g_tls_password_set_description (password, g_value_get_string (value));
Packit ae235b
      break;
Packit ae235b
    default:
Packit ae235b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_password_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GTlsPassword *password = G_TLS_PASSWORD (object);
Packit ae235b
Packit ae235b
  g_tls_password_real_set_value (password, NULL, 0, NULL);
Packit ae235b
  g_free (password->priv->warning);
Packit ae235b
  g_free (password->priv->description);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_tls_password_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_password_class_init (GTlsPasswordClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  klass->get_value = g_tls_password_real_get_value;
Packit ae235b
  klass->set_value = g_tls_password_real_set_value;
Packit ae235b
  klass->get_default_warning = g_tls_password_real_get_default_warning;
Packit ae235b
Packit ae235b
  gobject_class->get_property = g_tls_password_get_property;
Packit ae235b
  gobject_class->set_property = g_tls_password_set_property;
Packit ae235b
  gobject_class->finalize = g_tls_password_finalize;
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_FLAGS,
Packit ae235b
				   g_param_spec_flags ("flags",
Packit ae235b
						       P_("Flags"),
Packit ae235b
						       P_("Flags about the password"),
Packit ae235b
						       G_TYPE_TLS_PASSWORD_FLAGS,
Packit ae235b
						       G_TLS_PASSWORD_NONE,
Packit ae235b
						       G_PARAM_READWRITE |
Packit ae235b
						       G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_DESCRIPTION,
Packit ae235b
				   g_param_spec_string ("description",
Packit ae235b
							P_("Description"),
Packit ae235b
							P_("Description of what the password is for"),
Packit ae235b
							NULL,
Packit ae235b
							G_PARAM_READWRITE |
Packit ae235b
							G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_WARNING,
Packit ae235b
				   g_param_spec_string ("warning",
Packit ae235b
							P_("Warning"),
Packit ae235b
							P_("Warning about the password"),
Packit ae235b
							NULL,
Packit ae235b
							G_PARAM_READWRITE |
Packit ae235b
							G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_password_new:
Packit ae235b
 * @flags: the password flags
Packit ae235b
 * @description: description of what the password is for
Packit ae235b
 *
Packit ae235b
 * Create a new #GTlsPassword object.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): The newly allocated password object
Packit ae235b
 */
Packit ae235b
GTlsPassword *
Packit ae235b
g_tls_password_new (GTlsPasswordFlags  flags,
Packit ae235b
                    const gchar       *description)
Packit ae235b
{
Packit ae235b
  return g_object_new (G_TYPE_TLS_PASSWORD,
Packit ae235b
                       "flags", flags,
Packit ae235b
                       "description", description,
Packit ae235b
                       NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_password_get_value:
Packit ae235b
 * @password: a #GTlsPassword object
Packit ae235b
 * @length: (nullable): location to place the length of the password.
Packit ae235b
 *
Packit ae235b
 * Get the password value. If @length is not %NULL then it will be
Packit ae235b
 * filled in with the length of the password value. (Note that the
Packit ae235b
 * password value is not nul-terminated, so you can only pass %NULL
Packit ae235b
 * for @length in contexts where you know the password will have a
Packit ae235b
 * certain fixed length.)
Packit ae235b
 *
Packit ae235b
 * Returns: The password value (owned by the password object).
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
const guchar *
Packit ae235b
g_tls_password_get_value (GTlsPassword  *password,
Packit ae235b
                          gsize         *length)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
Packit ae235b
  return G_TLS_PASSWORD_GET_CLASS (password)->get_value (password, length);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_password_set_value:
Packit ae235b
 * @password: a #GTlsPassword object
Packit ae235b
 * @value: (array length=length): the new password value
Packit ae235b
 * @length: the length of the password, or -1
Packit ae235b
 *
Packit ae235b
 * Set the value for this password. The @value will be copied by the password
Packit ae235b
 * object.
Packit ae235b
 *
Packit ae235b
 * Specify the @length, for a non-nul-terminated password. Pass -1 as
Packit ae235b
 * @length if using a nul-terminated password, and @length will be
Packit ae235b
 * calculated automatically. (Note that the terminating nul is not
Packit ae235b
 * considered part of the password in this case.)
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_password_set_value (GTlsPassword  *password,
Packit ae235b
                          const guchar  *value,
Packit ae235b
                          gssize         length)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TLS_PASSWORD (password));
Packit ae235b
Packit ae235b
  if (length < 0)
Packit ae235b
    length = strlen ((gchar *)value);
Packit ae235b
Packit ae235b
  g_tls_password_set_value_full (password, g_memdup (value, length), length, g_free);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_password_set_value_full:
Packit ae235b
 * @password: a #GTlsPassword object
Packit ae235b
 * @value: (array length=length): the value for the password
Packit ae235b
 * @length: the length of the password, or -1
Packit ae235b
 * @destroy: (nullable): a function to use to free the password.
Packit ae235b
 *
Packit ae235b
 * Provide the value for this password.
Packit ae235b
 *
Packit ae235b
 * The @value will be owned by the password object, and later freed using
Packit ae235b
 * the @destroy function callback.
Packit ae235b
 *
Packit ae235b
 * Specify the @length, for a non-nul-terminated password. Pass -1 as
Packit ae235b
 * @length if using a nul-terminated password, and @length will be
Packit ae235b
 * calculated automatically. (Note that the terminating nul is not
Packit ae235b
 * considered part of the password in this case.)
Packit ae235b
 *
Packit ae235b
 * Virtual: set_value
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_password_set_value_full (GTlsPassword   *password,
Packit ae235b
                               guchar         *value,
Packit ae235b
                               gssize          length,
Packit ae235b
                               GDestroyNotify  destroy)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TLS_PASSWORD (password));
Packit ae235b
  G_TLS_PASSWORD_GET_CLASS (password)->set_value (password, value,
Packit ae235b
                                                  length, destroy);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_password_get_flags:
Packit ae235b
 * @password: a #GTlsPassword object
Packit ae235b
 *
Packit ae235b
 * Get flags about the password.
Packit ae235b
 *
Packit ae235b
 * Returns: The flags about the password.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
GTlsPasswordFlags
Packit ae235b
g_tls_password_get_flags (GTlsPassword *password)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_PASSWORD_NONE);
Packit ae235b
  return password->priv->flags;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_password_set_flags:
Packit ae235b
 * @password: a #GTlsPassword object
Packit ae235b
 * @flags: The flags about the password
Packit ae235b
 *
Packit ae235b
 * Set flags about the password.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_password_set_flags (GTlsPassword      *password,
Packit ae235b
                          GTlsPasswordFlags  flags)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TLS_PASSWORD (password));
Packit ae235b
Packit ae235b
  password->priv->flags = flags;
Packit ae235b
Packit ae235b
  g_object_notify (G_OBJECT (password), "flags");
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_password_get_description:
Packit ae235b
 * @password: a #GTlsPassword object
Packit ae235b
 *
Packit ae235b
 * Get a description string about what the password will be used for.
Packit ae235b
 *
Packit ae235b
 * Returns: The description of the password.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
const gchar*
Packit ae235b
g_tls_password_get_description (GTlsPassword *password)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
Packit ae235b
  return password->priv->description;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_password_set_description:
Packit ae235b
 * @password: a #GTlsPassword object
Packit ae235b
 * @description: The description of the password
Packit ae235b
 *
Packit ae235b
 * Set a description string about what the password will be used for.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_password_set_description (GTlsPassword      *password,
Packit ae235b
                                const gchar       *description)
Packit ae235b
{
Packit ae235b
  gchar *copy;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_TLS_PASSWORD (password));
Packit ae235b
Packit ae235b
  copy = g_strdup (description);
Packit ae235b
  g_free (password->priv->description);
Packit ae235b
  password->priv->description = copy;
Packit ae235b
Packit ae235b
  g_object_notify (G_OBJECT (password), "description");
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_password_get_warning:
Packit ae235b
 * @password: a #GTlsPassword object
Packit ae235b
 *
Packit ae235b
 * Get a user readable translated warning. Usually this warning is a
Packit ae235b
 * representation of the password flags returned from
Packit ae235b
 * g_tls_password_get_flags().
Packit ae235b
 *
Packit ae235b
 * Returns: The warning.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_tls_password_get_warning (GTlsPassword      *password)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
Packit ae235b
Packit ae235b
  if (password->priv->warning == NULL)
Packit ae235b
    return G_TLS_PASSWORD_GET_CLASS (password)->get_default_warning (password);
Packit ae235b
Packit ae235b
  return password->priv->warning;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_password_set_warning:
Packit ae235b
 * @password: a #GTlsPassword object
Packit ae235b
 * @warning: The user readable warning
Packit ae235b
 *
Packit ae235b
 * Set a user readable translated warning. Usually this warning is a
Packit ae235b
 * representation of the password flags returned from
Packit ae235b
 * g_tls_password_get_flags().
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_tls_password_set_warning (GTlsPassword      *password,
Packit ae235b
                            const gchar       *warning)
Packit ae235b
{
Packit ae235b
  gchar *copy;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_TLS_PASSWORD (password));
Packit ae235b
Packit ae235b
  copy = g_strdup (warning);
Packit ae235b
  g_free (password->priv->warning);
Packit ae235b
  password->priv->warning = copy;
Packit ae235b
Packit ae235b
  g_object_notify (G_OBJECT (password), "warning");
Packit ae235b
}