Merge remote-tracking branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gtlspassword.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2011 Collabora, Ltd.
4  *
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 of the License, or (at your option) any later version.
9  *
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.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Stef Walter <stefw@collabora.co.uk>
21  */
22
23 #include "config.h"
24 #include "glib.h"
25 #include "glibintl.h"
26
27 #include "gioenumtypes.h"
28 #include "gtlspassword.h"
29
30 #include <string.h>
31
32 /**
33  * SECTION:gtlspassword
34  * @title: GTlsPassword
35  * @short_description: TLS Passwords for prompting
36  * @include: gio/gio.h
37  *
38  * Holds a password used in TLS.
39  */
40
41 /**
42  * GTlsPassword:
43  *
44  * An abstract interface representing a password used in TLS. Often used in
45  * user interaction such as unlocking a key storage token.
46  *
47  * Since: 2.30
48  */
49
50 enum
51 {
52   PROP_0,
53   PROP_FLAGS,
54   PROP_DESCRIPTION,
55   PROP_WARNING
56 };
57
58 struct _GTlsPasswordPrivate
59 {
60   guchar *value;
61   gsize length;
62   GDestroyNotify destroy;
63   GTlsPasswordFlags flags;
64   gchar *description;
65   gchar *warning;
66 };
67
68 G_DEFINE_TYPE (GTlsPassword, g_tls_password, G_TYPE_OBJECT);
69
70 static void
71 g_tls_password_init (GTlsPassword *password)
72 {
73   password->priv = G_TYPE_INSTANCE_GET_PRIVATE (password, G_TYPE_TLS_PASSWORD,
74                                               GTlsPasswordPrivate);
75 }
76
77 static const guchar *
78 g_tls_password_real_get_value (GTlsPassword  *password,
79                                gsize         *length)
80 {
81   if (length)
82     *length = password->priv->length;
83   return password->priv->value;
84 }
85
86 static void
87 g_tls_password_real_set_value (GTlsPassword  *password,
88                                guchar        *value,
89                                gssize         length,
90                                GDestroyNotify destroy)
91 {
92   if (password->priv->destroy)
93       (password->priv->destroy) (password->priv->value);
94   password->priv->destroy = NULL;
95   password->priv->value = NULL;
96   password->priv->length = 0;
97
98   if (length < 0)
99     length = strlen ((gchar*) value);
100
101   password->priv->value = value;
102   password->priv->length = length;
103   password->priv->destroy = destroy;
104 }
105
106 static const gchar*
107 g_tls_password_real_get_default_warning (GTlsPassword  *password)
108 {
109   GTlsPasswordFlags flags;
110
111   flags = g_tls_password_get_flags (password);
112
113   if (flags & G_TLS_PASSWORD_FINAL_TRY)
114     return _("This is the last chance to enter the password correctly before your access is locked out.");
115   if (flags & G_TLS_PASSWORD_MANY_TRIES)
116     return _("Several password entered have been incorrect, and your access will be locked out after further failures.");
117   if (flags & G_TLS_PASSWORD_RETRY)
118     return _("The password entered is incorrect.");
119
120   return NULL;
121 }
122
123 static void
124 g_tls_password_get_property (GObject    *object,
125                              guint       prop_id,
126                              GValue     *value,
127                              GParamSpec *pspec)
128 {
129   GTlsPassword *password = G_TLS_PASSWORD (object);
130
131   switch (prop_id)
132     {
133     case PROP_FLAGS:
134       g_value_set_flags (value, g_tls_password_get_flags (password));
135       break;
136     case PROP_WARNING:
137       g_value_set_string (value, g_tls_password_get_warning (password));
138       break;
139     case PROP_DESCRIPTION:
140       g_value_set_string (value, g_tls_password_get_description (password));
141       break;
142     default:
143       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
144       break;
145     }
146 }
147
148 static void
149 g_tls_password_set_property (GObject    *object,
150                              guint       prop_id,
151                              const GValue     *value,
152                              GParamSpec *pspec)
153 {
154   GTlsPassword *password = G_TLS_PASSWORD (object);
155
156   switch (prop_id)
157     {
158     case PROP_FLAGS:
159       g_tls_password_set_flags (password, g_value_get_flags (value));
160       break;
161     case PROP_WARNING:
162       g_tls_password_set_warning (password, g_value_get_string (value));
163       break;
164     case PROP_DESCRIPTION:
165       g_tls_password_set_description (password, g_value_get_string (value));
166       break;
167     default:
168       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169       break;
170     }
171 }
172
173 static void
174 g_tls_password_finalize (GObject *object)
175 {
176   GTlsPassword *password = G_TLS_PASSWORD (object);
177
178   g_tls_password_real_set_value (password, NULL, 0, NULL);
179   g_free (password->priv->warning);
180   g_free (password->priv->description);
181
182   G_OBJECT_CLASS (g_tls_password_parent_class)->finalize (object);
183 }
184
185 static void
186 g_tls_password_class_init (GTlsPasswordClass *klass)
187 {
188   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
189
190   klass->get_value = g_tls_password_real_get_value;
191   klass->set_value = g_tls_password_real_set_value;
192   klass->get_default_warning = g_tls_password_real_get_default_warning;
193
194   gobject_class->get_property = g_tls_password_get_property;
195   gobject_class->set_property = g_tls_password_set_property;
196   gobject_class->finalize = g_tls_password_finalize;
197
198   g_type_class_add_private (klass, sizeof (GTlsPasswordPrivate));
199
200   g_object_class_install_property (gobject_class, PROP_FLAGS,
201              g_param_spec_flags ("flags", "Flags", "Flags about the password",
202                                  G_TYPE_TLS_PASSWORD_FLAGS, G_TLS_PASSWORD_NONE,
203                                  G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204
205   g_object_class_install_property (gobject_class, PROP_DESCRIPTION,
206              g_param_spec_string ("description", "Description", "Description of what the password is for",
207                                   "", G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
208
209   g_object_class_install_property (gobject_class, PROP_WARNING,
210              g_param_spec_string ("warning", "Warning", "Warning about the password",
211                                  "", G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212
213 }
214
215 /**
216  * g_tls_password_new:
217  * @flags: the password flags
218  * @description: description of what the password is for
219  *
220  * Create a new #GTlsPassword object.
221  *
222  * Returns: (transfer full): The newly allocated password object
223  */
224 GTlsPassword *
225 g_tls_password_new (GTlsPasswordFlags  flags,
226                     const gchar       *description)
227 {
228   return g_object_new (G_TYPE_TLS_PASSWORD,
229                        "flags", flags,
230                        "description", description,
231                        NULL);
232 }
233
234 /**
235  * g_tls_password_get_value:
236  * @password: a #GTlsPassword object
237  * @length: (allow-none): location to place the length of the password.
238  *
239  * Get the password value. If @length is not %NULL then it will be filled
240  * in with the length of the password value.
241  *
242  * Returns: The password value owned by the password object.
243  *
244  * Since: 2.30
245  */
246 const guchar *
247 g_tls_password_get_value (GTlsPassword  *password,
248                           gsize         *length)
249 {
250   g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
251   return G_TLS_PASSWORD_GET_CLASS (password)->get_value (password, length);
252 }
253
254 /**
255  * g_tls_password_set_value:
256  * @password: a #GTlsPassword object
257  * @value: the new password value
258  * @length: the length of the password, or -1
259  *
260  * Set the value for this password. The @value will be copied by the password
261  * object.
262  *
263  * Specify the @length, for a non-null-terminated password. Pass -1 as
264  * @length if using a null-terminated password, and @length will be calculated
265  * automatically.
266  *
267  * Since: 2.30
268  */
269 void
270 g_tls_password_set_value (GTlsPassword  *password,
271                           const guchar  *value,
272                           gssize         length)
273 {
274   g_return_if_fail (G_IS_TLS_PASSWORD (password));
275
276   if (length < 0)
277     length = strlen ((gchar *)value);
278
279   g_tls_password_set_value_full (password, g_memdup (value, length), length, g_free);
280 }
281
282 /**
283  * g_tls_password_set_value_full:
284  * @password: a #GTlsPassword object
285  * @value: the value for the password
286  * @length: the length of the password, or -1
287  * @destroy: (allow-none): a function to use to free the password.
288  *
289  * Provide the value for this password.
290  *
291  * The @value will be owned by the password object, and later freed using
292  * the @destroy function callback.
293  *
294  * Specify the @length, for a non-null-terminated password. Pass -1 as
295  * @length if using a null-terminated password, and @length will be calculated
296  * automatically.
297  *
298  * Virtual: set_value
299  * Since: 2.30
300  */
301 void
302 g_tls_password_set_value_full (GTlsPassword  *password,
303                                guchar        *value,
304                                gssize         length,
305                                GDestroyNotify destroy)
306 {
307   g_return_if_fail (G_IS_TLS_PASSWORD (password));
308   G_TLS_PASSWORD_GET_CLASS (password)->set_value (password, value,
309                                                   length, destroy);
310 }
311
312 /**
313  * g_tls_password_get_flags:
314  * @password: a #GTlsPassword object
315  *
316  * Get flags about the password.
317  *
318  * Return value: The flags about the password.
319  *
320  * Since: 2.30
321  */
322 GTlsPasswordFlags
323 g_tls_password_get_flags (GTlsPassword *password)
324 {
325   g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_PASSWORD_NONE);
326   return password->priv->flags;
327 }
328
329 /**
330  * g_tls_password_set_flags:
331  * @password: a #GTlsPassword object
332  * @flags: The flags about the password
333  *
334  * Set flags about the password.
335  *
336  * Since: 2.30
337  */
338 void
339 g_tls_password_set_flags (GTlsPassword      *password,
340                           GTlsPasswordFlags  flags)
341 {
342   g_return_if_fail (G_IS_TLS_PASSWORD (password));
343
344   password->priv->flags = flags;
345
346   g_object_notify (G_OBJECT (password), "flags");
347 }
348
349 /**
350  * g_tls_password_get_description:
351  * @password: a #GTlsPassword object
352  *
353  * Get a description string about what the password will be used for.
354  *
355  * Return value: The description of the password.
356  *
357  * Since: 2.30
358  */
359 const gchar*
360 g_tls_password_get_description (GTlsPassword *password)
361 {
362   g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
363   return password->priv->description;
364 }
365
366 /**
367  * g_tls_password_set_description:
368  * @password: a #GTlsPassword object
369  * @description: The description of the password
370  *
371  * Set a description string about what the password will be used for.
372  *
373  * Since: 2.30
374  */
375 void
376 g_tls_password_set_description (GTlsPassword      *password,
377                                 const gchar       *description)
378 {
379   gchar *copy;
380
381   g_return_if_fail (G_IS_TLS_PASSWORD (password));
382
383   copy = g_strdup (description);
384   g_free (password->priv->description);
385   password->priv->description = copy;
386
387   g_object_notify (G_OBJECT (password), "description");
388 }
389
390 /**
391  * g_tls_password_get_warning:
392  * @password: a #GTlsPassword object
393  *
394  * Get a user readable translated warning. Usually this warning is a
395  * representation of the password flags returned from
396  * g_tls_password_get_flags().
397  *
398  * Return value: The warning.
399  *
400  * Since: 2.30
401  */
402 const gchar *
403 g_tls_password_get_warning (GTlsPassword      *password)
404 {
405   g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
406
407   if (password->priv->warning == NULL)
408     return G_TLS_PASSWORD_GET_CLASS (password)->get_default_warning (password);
409
410   return password->priv->warning;
411 }
412
413 /**
414  * g_tls_password_set_warning:
415  * @password: a #GTlsPassword object
416  * @warning: The user readable warning
417  *
418  * Set a user readable translated warning. Usually this warning is a
419  * representation of the password flags returned from
420  * g_tls_password_get_flags().
421  *
422  * Since: 2.30
423  */
424 void
425 g_tls_password_set_warning (GTlsPassword      *password,
426                             const gchar       *warning)
427 {
428   gchar *copy;
429
430   g_return_if_fail (G_IS_TLS_PASSWORD (password));
431
432   copy = g_strdup (warning);
433   g_free (password->priv->warning);
434   password->priv->warning = copy;
435
436   g_object_notify (G_OBJECT (password), "warning");
437 }