1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2011 Collabora, Ltd.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Stef Walter <stefw@collabora.co.uk>
27 #include "gioenumtypes.h"
28 #include "gtlspassword.h"
33 * SECTION:gtlspassword
34 * @title: GTlsPassword
35 * @short_description: TLS Passwords for prompting
38 * Holds a password used in TLS.
44 * An abstract interface representing a password used in TLS. Often used in
45 * user interaction such as unlocking a key storage token.
58 struct _GTlsPasswordPrivate
62 GDestroyNotify destroy;
63 GTlsPasswordFlags flags;
68 G_DEFINE_TYPE_WITH_PRIVATE (GTlsPassword, g_tls_password, G_TYPE_OBJECT)
71 g_tls_password_init (GTlsPassword *password)
73 password->priv = g_tls_password_get_instance_private (password);
77 g_tls_password_real_get_value (GTlsPassword *password,
81 *length = password->priv->length;
82 return password->priv->value;
86 g_tls_password_real_set_value (GTlsPassword *password,
89 GDestroyNotify destroy)
91 if (password->priv->destroy)
92 (password->priv->destroy) (password->priv->value);
93 password->priv->destroy = NULL;
94 password->priv->value = NULL;
95 password->priv->length = 0;
98 length = strlen ((gchar*) value);
100 password->priv->value = value;
101 password->priv->length = length;
102 password->priv->destroy = destroy;
106 g_tls_password_real_get_default_warning (GTlsPassword *password)
108 GTlsPasswordFlags flags;
110 flags = g_tls_password_get_flags (password);
112 if (flags & G_TLS_PASSWORD_FINAL_TRY)
113 return _("This is the last chance to enter the password correctly before your access is locked out.");
114 if (flags & G_TLS_PASSWORD_MANY_TRIES)
115 /* Translators: This is not the 'This is the last chance' string. It is
116 * displayed when more than one attempt is allowed. */
117 return _("Several passwords entered have been incorrect, and your access will be locked out after further failures.");
118 if (flags & G_TLS_PASSWORD_RETRY)
119 return _("The password entered is incorrect.");
125 g_tls_password_get_property (GObject *object,
130 GTlsPassword *password = G_TLS_PASSWORD (object);
135 g_value_set_flags (value, g_tls_password_get_flags (password));
138 g_value_set_string (value, g_tls_password_get_warning (password));
140 case PROP_DESCRIPTION:
141 g_value_set_string (value, g_tls_password_get_description (password));
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150 g_tls_password_set_property (GObject *object,
155 GTlsPassword *password = G_TLS_PASSWORD (object);
160 g_tls_password_set_flags (password, g_value_get_flags (value));
163 g_tls_password_set_warning (password, g_value_get_string (value));
165 case PROP_DESCRIPTION:
166 g_tls_password_set_description (password, g_value_get_string (value));
169 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175 g_tls_password_finalize (GObject *object)
177 GTlsPassword *password = G_TLS_PASSWORD (object);
179 g_tls_password_real_set_value (password, NULL, 0, NULL);
180 g_free (password->priv->warning);
181 g_free (password->priv->description);
183 G_OBJECT_CLASS (g_tls_password_parent_class)->finalize (object);
187 g_tls_password_class_init (GTlsPasswordClass *klass)
189 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
191 klass->get_value = g_tls_password_real_get_value;
192 klass->set_value = g_tls_password_real_set_value;
193 klass->get_default_warning = g_tls_password_real_get_default_warning;
195 gobject_class->get_property = g_tls_password_get_property;
196 gobject_class->set_property = g_tls_password_set_property;
197 gobject_class->finalize = g_tls_password_finalize;
199 g_object_class_install_property (gobject_class, PROP_FLAGS,
200 g_param_spec_flags ("flags",
202 P_("Flags about the password"),
203 G_TYPE_TLS_PASSWORD_FLAGS,
206 G_PARAM_STATIC_STRINGS));
208 g_object_class_install_property (gobject_class, PROP_DESCRIPTION,
209 g_param_spec_string ("description",
211 P_("Description of what the password is for"),
214 G_PARAM_STATIC_STRINGS));
216 g_object_class_install_property (gobject_class, PROP_WARNING,
217 g_param_spec_string ("warning",
219 P_("Warning about the password"),
222 G_PARAM_STATIC_STRINGS));
227 * g_tls_password_new:
228 * @flags: the password flags
229 * @description: description of what the password is for
231 * Create a new #GTlsPassword object.
233 * Returns: (transfer full): The newly allocated password object
236 g_tls_password_new (GTlsPasswordFlags flags,
237 const gchar *description)
239 return g_object_new (G_TYPE_TLS_PASSWORD,
241 "description", description,
246 * g_tls_password_get_value: (virtual get_value)
247 * @password: a #GTlsPassword object
248 * @length: (optional): location to place the length of the password.
250 * Get the password value. If @length is not %NULL then it will be
251 * filled in with the length of the password value. (Note that the
252 * password value is not nul-terminated, so you can only pass %NULL
253 * for @length in contexts where you know the password will have a
254 * certain fixed length.)
256 * Returns: (array length=length): The password value (owned by the password object).
261 g_tls_password_get_value (GTlsPassword *password,
264 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
265 return G_TLS_PASSWORD_GET_CLASS (password)->get_value (password, length);
269 * g_tls_password_set_value:
270 * @password: a #GTlsPassword object
271 * @value: (array length=length): the new password value
272 * @length: the length of the password, or -1
274 * Set the value for this password. The @value will be copied by the password
277 * Specify the @length, for a non-nul-terminated password. Pass -1 as
278 * @length if using a nul-terminated password, and @length will be
279 * calculated automatically. (Note that the terminating nul is not
280 * considered part of the password in this case.)
285 g_tls_password_set_value (GTlsPassword *password,
289 g_return_if_fail (G_IS_TLS_PASSWORD (password));
293 /* FIXME: g_tls_password_set_value_full() doesn’t support unsigned gsize */
294 gsize length_unsigned = strlen ((gchar *) value);
295 g_return_if_fail (length_unsigned <= G_MAXSSIZE);
296 length = (gssize) length_unsigned;
299 g_tls_password_set_value_full (password, g_memdup2 (value, (gsize) length), length, g_free);
303 * g_tls_password_set_value_full:
304 * @password: a #GTlsPassword object
305 * @value: (array length=length): the value for the password
306 * @length: the length of the password, or -1
307 * @destroy: (nullable): a function to use to free the password.
309 * Provide the value for this password.
311 * The @value will be owned by the password object, and later freed using
312 * the @destroy function callback.
314 * Specify the @length, for a non-nul-terminated password. Pass -1 as
315 * @length if using a nul-terminated password, and @length will be
316 * calculated automatically. (Note that the terminating nul is not
317 * considered part of the password in this case.)
323 g_tls_password_set_value_full (GTlsPassword *password,
326 GDestroyNotify destroy)
328 g_return_if_fail (G_IS_TLS_PASSWORD (password));
329 G_TLS_PASSWORD_GET_CLASS (password)->set_value (password, value,
334 * g_tls_password_get_flags:
335 * @password: a #GTlsPassword object
337 * Get flags about the password.
339 * Returns: The flags about the password.
344 g_tls_password_get_flags (GTlsPassword *password)
346 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_PASSWORD_NONE);
347 return password->priv->flags;
351 * g_tls_password_set_flags:
352 * @password: a #GTlsPassword object
353 * @flags: The flags about the password
355 * Set flags about the password.
360 g_tls_password_set_flags (GTlsPassword *password,
361 GTlsPasswordFlags flags)
363 g_return_if_fail (G_IS_TLS_PASSWORD (password));
365 password->priv->flags = flags;
367 g_object_notify (G_OBJECT (password), "flags");
371 * g_tls_password_get_description:
372 * @password: a #GTlsPassword object
374 * Get a description string about what the password will be used for.
376 * Returns: The description of the password.
381 g_tls_password_get_description (GTlsPassword *password)
383 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
384 return password->priv->description;
388 * g_tls_password_set_description:
389 * @password: a #GTlsPassword object
390 * @description: The description of the password
392 * Set a description string about what the password will be used for.
397 g_tls_password_set_description (GTlsPassword *password,
398 const gchar *description)
402 g_return_if_fail (G_IS_TLS_PASSWORD (password));
404 copy = g_strdup (description);
405 g_free (password->priv->description);
406 password->priv->description = copy;
408 g_object_notify (G_OBJECT (password), "description");
412 * g_tls_password_get_warning:
413 * @password: a #GTlsPassword object
415 * Get a user readable translated warning. Usually this warning is a
416 * representation of the password flags returned from
417 * g_tls_password_get_flags().
419 * Returns: The warning.
424 g_tls_password_get_warning (GTlsPassword *password)
426 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
428 if (password->priv->warning == NULL)
429 return G_TLS_PASSWORD_GET_CLASS (password)->get_default_warning (password);
431 return password->priv->warning;
435 * g_tls_password_set_warning:
436 * @password: a #GTlsPassword object
437 * @warning: The user readable warning
439 * Set a user readable translated warning. Usually this warning is a
440 * representation of the password flags returned from
441 * g_tls_password_get_flags().
446 g_tls_password_set_warning (GTlsPassword *password,
447 const gchar *warning)
451 g_return_if_fail (G_IS_TLS_PASSWORD (password));
453 copy = g_strdup (warning);
454 g_free (password->priv->warning);
455 password->priv->warning = copy;
457 g_object_notify (G_OBJECT (password), "warning");