1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2011 Collabora, Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Stef Walter <stefw@collabora.co.uk>
25 #include "gioenumtypes.h"
26 #include "gtlspassword.h"
31 * SECTION:gtlspassword
32 * @title: GTlsPassword
33 * @short_description: TLS Passwords for prompting
36 * Holds a password used in TLS.
42 * An abstract interface representing a password used in TLS. Often used in
43 * user interaction such as unlocking a key storage token.
56 struct _GTlsPasswordPrivate
60 GDestroyNotify destroy
;
61 GTlsPasswordFlags flags
;
66 G_DEFINE_TYPE_WITH_PRIVATE (GTlsPassword
, g_tls_password
, G_TYPE_OBJECT
)
69 g_tls_password_init (GTlsPassword
*password
)
71 password
->priv
= g_tls_password_get_instance_private (password
);
75 g_tls_password_real_get_value (GTlsPassword
*password
,
79 *length
= password
->priv
->length
;
80 return password
->priv
->value
;
84 g_tls_password_real_set_value (GTlsPassword
*password
,
87 GDestroyNotify destroy
)
89 if (password
->priv
->destroy
)
90 (password
->priv
->destroy
) (password
->priv
->value
);
91 password
->priv
->destroy
= NULL
;
92 password
->priv
->value
= NULL
;
93 password
->priv
->length
= 0;
96 length
= strlen ((gchar
*) value
);
98 password
->priv
->value
= value
;
99 password
->priv
->length
= length
;
100 password
->priv
->destroy
= destroy
;
104 g_tls_password_real_get_default_warning (GTlsPassword
*password
)
106 GTlsPasswordFlags flags
;
108 flags
= g_tls_password_get_flags (password
);
110 if (flags
& G_TLS_PASSWORD_FINAL_TRY
)
111 return _("This is the last chance to enter the password correctly before your access is locked out.");
112 if (flags
& G_TLS_PASSWORD_MANY_TRIES
)
113 /* Translators: This is not the 'This is the last chance' string. It is
114 * displayed when more than one attempt is allowed. */
115 return _("Several passwords entered have been incorrect, and your access will be locked out after further failures.");
116 if (flags
& G_TLS_PASSWORD_RETRY
)
117 return _("The password entered is incorrect.");
123 g_tls_password_get_property (GObject
*object
,
128 GTlsPassword
*password
= G_TLS_PASSWORD (object
);
133 g_value_set_flags (value
, g_tls_password_get_flags (password
));
136 g_value_set_string (value
, g_tls_password_get_warning (password
));
138 case PROP_DESCRIPTION
:
139 g_value_set_string (value
, g_tls_password_get_description (password
));
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
148 g_tls_password_set_property (GObject
*object
,
153 GTlsPassword
*password
= G_TLS_PASSWORD (object
);
158 g_tls_password_set_flags (password
, g_value_get_flags (value
));
161 g_tls_password_set_warning (password
, g_value_get_string (value
));
163 case PROP_DESCRIPTION
:
164 g_tls_password_set_description (password
, g_value_get_string (value
));
167 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
173 g_tls_password_finalize (GObject
*object
)
175 GTlsPassword
*password
= G_TLS_PASSWORD (object
);
177 g_tls_password_real_set_value (password
, NULL
, 0, NULL
);
178 g_free (password
->priv
->warning
);
179 g_free (password
->priv
->description
);
181 G_OBJECT_CLASS (g_tls_password_parent_class
)->finalize (object
);
185 g_tls_password_class_init (GTlsPasswordClass
*klass
)
187 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
189 klass
->get_value
= g_tls_password_real_get_value
;
190 klass
->set_value
= g_tls_password_real_set_value
;
191 klass
->get_default_warning
= g_tls_password_real_get_default_warning
;
193 gobject_class
->get_property
= g_tls_password_get_property
;
194 gobject_class
->set_property
= g_tls_password_set_property
;
195 gobject_class
->finalize
= g_tls_password_finalize
;
197 g_object_class_install_property (gobject_class
, PROP_FLAGS
,
198 g_param_spec_flags ("flags",
200 P_("Flags about the password"),
201 G_TYPE_TLS_PASSWORD_FLAGS
,
204 G_PARAM_STATIC_STRINGS
));
206 g_object_class_install_property (gobject_class
, PROP_DESCRIPTION
,
207 g_param_spec_string ("description",
209 P_("Description of what the password is for"),
212 G_PARAM_STATIC_STRINGS
));
214 g_object_class_install_property (gobject_class
, PROP_WARNING
,
215 g_param_spec_string ("warning",
217 P_("Warning about the password"),
220 G_PARAM_STATIC_STRINGS
));
225 * g_tls_password_new:
226 * @flags: the password flags
227 * @description: description of what the password is for
229 * Create a new #GTlsPassword object.
231 * Returns: (transfer full): The newly allocated password object
234 g_tls_password_new (GTlsPasswordFlags flags
,
235 const gchar
*description
)
237 return g_object_new (G_TYPE_TLS_PASSWORD
,
239 "description", description
,
244 * g_tls_password_get_value:
245 * @password: a #GTlsPassword object
246 * @length: (nullable): location to place the length of the password.
248 * Get the password value. If @length is not %NULL then it will be
249 * filled in with the length of the password value. (Note that the
250 * password value is not nul-terminated, so you can only pass %NULL
251 * for @length in contexts where you know the password will have a
252 * certain fixed length.)
254 * Returns: The password value (owned by the password object).
259 g_tls_password_get_value (GTlsPassword
*password
,
262 g_return_val_if_fail (G_IS_TLS_PASSWORD (password
), NULL
);
263 return G_TLS_PASSWORD_GET_CLASS (password
)->get_value (password
, length
);
267 * g_tls_password_set_value:
268 * @password: a #GTlsPassword object
269 * @value: (array length=length): the new password value
270 * @length: the length of the password, or -1
272 * Set the value for this password. The @value will be copied by the password
275 * Specify the @length, for a non-nul-terminated password. Pass -1 as
276 * @length if using a nul-terminated password, and @length will be
277 * calculated automatically. (Note that the terminating nul is not
278 * considered part of the password in this case.)
283 g_tls_password_set_value (GTlsPassword
*password
,
287 g_return_if_fail (G_IS_TLS_PASSWORD (password
));
290 length
= strlen ((gchar
*)value
);
292 g_tls_password_set_value_full (password
, g_memdup (value
, length
), length
, g_free
);
296 * g_tls_password_set_value_full:
297 * @password: a #GTlsPassword object
298 * @value: (array length=length): the value for the password
299 * @length: the length of the password, or -1
300 * @destroy: (nullable): a function to use to free the password.
302 * Provide the value for this password.
304 * The @value will be owned by the password object, and later freed using
305 * the @destroy function callback.
307 * Specify the @length, for a non-nul-terminated password. Pass -1 as
308 * @length if using a nul-terminated password, and @length will be
309 * calculated automatically. (Note that the terminating nul is not
310 * considered part of the password in this case.)
316 g_tls_password_set_value_full (GTlsPassword
*password
,
319 GDestroyNotify destroy
)
321 g_return_if_fail (G_IS_TLS_PASSWORD (password
));
322 G_TLS_PASSWORD_GET_CLASS (password
)->set_value (password
, value
,
327 * g_tls_password_get_flags:
328 * @password: a #GTlsPassword object
330 * Get flags about the password.
332 * Returns: The flags about the password.
337 g_tls_password_get_flags (GTlsPassword
*password
)
339 g_return_val_if_fail (G_IS_TLS_PASSWORD (password
), G_TLS_PASSWORD_NONE
);
340 return password
->priv
->flags
;
344 * g_tls_password_set_flags:
345 * @password: a #GTlsPassword object
346 * @flags: The flags about the password
348 * Set flags about the password.
353 g_tls_password_set_flags (GTlsPassword
*password
,
354 GTlsPasswordFlags flags
)
356 g_return_if_fail (G_IS_TLS_PASSWORD (password
));
358 password
->priv
->flags
= flags
;
360 g_object_notify (G_OBJECT (password
), "flags");
364 * g_tls_password_get_description:
365 * @password: a #GTlsPassword object
367 * Get a description string about what the password will be used for.
369 * Returns: The description of the password.
374 g_tls_password_get_description (GTlsPassword
*password
)
376 g_return_val_if_fail (G_IS_TLS_PASSWORD (password
), NULL
);
377 return password
->priv
->description
;
381 * g_tls_password_set_description:
382 * @password: a #GTlsPassword object
383 * @description: The description of the password
385 * Set a description string about what the password will be used for.
390 g_tls_password_set_description (GTlsPassword
*password
,
391 const gchar
*description
)
395 g_return_if_fail (G_IS_TLS_PASSWORD (password
));
397 copy
= g_strdup (description
);
398 g_free (password
->priv
->description
);
399 password
->priv
->description
= copy
;
401 g_object_notify (G_OBJECT (password
), "description");
405 * g_tls_password_get_warning:
406 * @password: a #GTlsPassword object
408 * Get a user readable translated warning. Usually this warning is a
409 * representation of the password flags returned from
410 * g_tls_password_get_flags().
412 * Returns: The warning.
417 g_tls_password_get_warning (GTlsPassword
*password
)
419 g_return_val_if_fail (G_IS_TLS_PASSWORD (password
), NULL
);
421 if (password
->priv
->warning
== NULL
)
422 return G_TLS_PASSWORD_GET_CLASS (password
)->get_default_warning (password
);
424 return password
->priv
->warning
;
428 * g_tls_password_set_warning:
429 * @password: a #GTlsPassword object
430 * @warning: The user readable warning
432 * Set a user readable translated warning. Usually this warning is a
433 * representation of the password flags returned from
434 * g_tls_password_get_flags().
439 g_tls_password_set_warning (GTlsPassword
*password
,
440 const gchar
*warning
)
444 g_return_if_fail (G_IS_TLS_PASSWORD (password
));
446 copy
= g_strdup (warning
);
447 g_free (password
->priv
->warning
);
448 password
->priv
->warning
= copy
;
450 g_object_notify (G_OBJECT (password
), "warning");