1 /* libsecret - GLib wrapper for Secret Service
3 * Copyright 2011 Collabora Ltd.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 2.1 of the licence or (at
8 * your option) any later version.
10 * See the included COPYING file for more information.
12 * Author: Stef Walter <stefw@gnome.org>
17 #include "secret-private.h"
18 #include "secret-value.h"
20 #include "egg/egg-secure-memory.h"
25 * SECTION:secret-value
27 * @short_description: a value containing a secret
29 * A #SecretValue contains a password or other secret value.
31 * Use secret_value_get() to get the actual secret data, such as a password.
32 * The secret data is not necessarily null-terminated, unless the content type
35 * Each #SecretValue has a content type. For passwords, this is "text/plain".
36 * Use secret_value_get_content_type() to look at the content type.
38 * #SecretValue is reference counted and immutable. The secret data is only
39 * freed when all references have been released via secret_value_unref().
44 static gboolean is_password_value (SecretValue *value);
49 * A secret value, like a password or other binary secret.
52 EGG_SECURE_DECLARE (secret_value);
58 GDestroyNotify destroy;
63 secret_value_get_type (void)
65 static gsize initialized = 0;
66 static GType type = 0;
68 if (g_once_init_enter (&initialized)) {
69 type = g_boxed_type_register_static ("SecretValue",
70 (GBoxedCopyFunc)secret_value_ref,
71 (GBoxedFreeFunc)secret_value_unref);
72 g_once_init_leave (&initialized, 1);
80 * @secret: the secret data
81 * @length: the length of the data
82 * @content_type: the content type of the data
84 * Create a #SecretValue for the secret data passed in. The secret data is
85 * copied into non-pageable 'secure' memory.
87 * If the length is less than zero, then @secret is assumed to be
90 * Returns: (transfer full): the new #SecretValue
93 secret_value_new (const gchar *secret,
95 const gchar *content_type)
99 g_return_val_if_fail (length == 0 || secret != NULL, NULL);
100 g_return_val_if_fail (content_type, NULL);
103 length = strlen (secret);
105 copy = egg_secure_alloc (length + 1);
107 memcpy (copy, secret, length);
109 return secret_value_new_full (copy, length, content_type, egg_secure_free);
113 * secret_value_new_full:
114 * @secret: the secret data
115 * @length: the length of the data
116 * @content_type: the content type of the data
117 * @destroy: function to call to free the secret data
119 * Create a #SecretValue for the secret data passed in. The secret data is
120 * not copied, and will later be freed with the @destroy function.
122 * If the length is less than zero, then @secret is assumed to be
125 * Returns: (transfer full): the new #SecretValue
128 secret_value_new_full (gchar *secret,
130 const gchar *content_type,
131 GDestroyNotify destroy)
135 g_return_val_if_fail (content_type, NULL);
138 length = strlen (secret);
140 value = g_slice_new0 (SecretValue);
142 value->content_type = g_strdup (content_type);
143 value->destroy = destroy;
144 value->length = length;
145 value->secret = secret;
153 * @length: the length of the secret
155 * Get the secret data in the #SecretValue. The value is not necessarily
156 * null-terminated unless it was created with secret_value_new() or a
157 * null-terminated string was passed to secret_value_new_full().
159 * Returns: (array length=length) (element-type guint8): the secret data
162 secret_value_get (SecretValue *value,
165 g_return_val_if_fail (value, NULL);
167 *length = value->length;
168 return value->secret;
172 * secret_value_get_text:
175 * Get the secret data in the #SecretValue if it contains a textual
176 * value. The content type must be <literal>text/plain</literal>.
178 * Returns: (allow-none): the content type
181 secret_value_get_text (SecretValue *value)
183 g_return_val_if_fail (value, NULL);
185 if (!is_password_value (value))
188 return value->secret;
192 * secret_value_get_content_type:
195 * Get the content type of the secret value, such as
196 * <literal>text/plain</literal>.
198 * Returns: the content type
201 secret_value_get_content_type (SecretValue *value)
203 g_return_val_if_fail (value, NULL);
204 return value->content_type;
209 * @value: value to reference
211 * Add another reference to the #SecretValue. For each reference
212 * secret_value_unref() should be called to unreference the value.
214 * Returns: (transfer full): the value
217 secret_value_ref (SecretValue *value)
219 g_return_val_if_fail (value, NULL);
220 g_atomic_int_inc (&value->refs);
225 * secret_value_unref:
226 * @value: (type Secret.Value) (allow-none): value to unreference
228 * Unreference a #SecretValue. When the last reference is gone, then
229 * the value will be freed.
232 secret_value_unref (gpointer value)
234 SecretValue *val = value;
236 g_return_if_fail (value != NULL);
238 if (g_atomic_int_dec_and_test (&val->refs)) {
239 g_free (val->content_type);
241 (val->destroy) (val->secret);
242 g_slice_free (SecretValue, val);
247 is_password_value (SecretValue *value)
249 if (value->content_type && g_str_equal (value->content_type, "text/plain"))
252 /* gnome-keyring-daemon used to return passwords like this, so support this, but validate */
253 if (!value->content_type || g_str_equal (value->content_type, "application/octet-stream"))
254 return g_utf8_validate (value->secret, value->length, NULL);
260 _secret_value_unref_to_password (SecretValue *value)
262 SecretValue *val = value;
265 g_return_val_if_fail (value != NULL, NULL);
267 if (!is_password_value (value)) {
268 secret_value_unref (value);
272 if (g_atomic_int_dec_and_test (&val->refs)) {
273 if (val->destroy == egg_secure_free) {
274 result = val->secret;
277 result = egg_secure_strndup (val->secret, val->length);
279 (val->destroy) (val->secret);
281 g_free (val->content_type);
282 g_slice_free (SecretValue, val);
285 result = egg_secure_strndup (val->secret, val->length);
292 _secret_value_unref_to_string (SecretValue *value)
294 SecretValue *val = value;
297 g_return_val_if_fail (value != NULL, NULL);
299 if (!is_password_value (value)) {
300 secret_value_unref (value);
304 if (g_atomic_int_dec_and_test (&val->refs)) {
305 if (val->destroy == g_free) {
306 result = val->secret;
309 result = g_strndup (val->secret, val->length);
311 (val->destroy) (val->secret);
313 g_free (val->content_type);
314 g_slice_free (SecretValue, val);
317 result = g_strndup (val->secret, val->length);