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().
41 * These functions have an unstable API and may change across versions. Use
42 * <literal>libsecret-unstable</literal> package to access them.
50 * A secret value, like a password or other binary secret.
53 EGG_SECURE_DECLARE (secret_value);
59 GDestroyNotify destroy;
64 secret_value_get_type (void)
66 static gsize initialized = 0;
67 static GType type = 0;
69 if (g_once_init_enter (&initialized)) {
70 type = g_boxed_type_register_static ("SecretValue",
71 (GBoxedCopyFunc)secret_value_ref,
72 (GBoxedFreeFunc)secret_value_unref);
73 g_once_init_leave (&initialized, 1);
81 * @secret: the secret data
82 * @length: the length of the data
83 * @content_type: the content type of the data
85 * Create a #SecretValue for the secret data passed in. The secret data is
86 * copied into non-pageable 'secure' memory.
88 * If the length is less than zero, then @secret is assumed to be
91 * Returns: (transfer full): the new #SecretValue
94 secret_value_new (const gchar *secret,
96 const gchar *content_type)
100 g_return_val_if_fail (secret == NULL || length != 0, NULL);
101 g_return_val_if_fail (content_type, NULL);
104 length = strlen (secret);
106 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 (secret == NULL || length != 0, NULL);
136 g_return_val_if_fail (content_type, NULL);
139 length = strlen (secret);
141 value = g_slice_new0 (SecretValue);
143 value->content_type = g_strdup (content_type);
144 value->destroy = destroy;
145 value->length = length;
146 value->secret = secret;
154 * @length: (out): the length of the secret
156 * Get the secret data in the #SecretValue. The value is not necessarily
157 * null-terminated unless it was created with secret_value_new() or a
158 * null-terminated string was passed to secret_value_new_full().
160 * Returns: (array length=length): the secret data
163 secret_value_get (SecretValue *value,
166 g_return_val_if_fail (value, NULL);
168 *length = value->length;
169 return value->secret;
173 * secret_value_get_content_type:
176 * Get the content type of the secret value, such as
177 * <literal>text/plain</literal>.
179 * Returns: the content type
182 secret_value_get_content_type (SecretValue *value)
184 g_return_val_if_fail (value, NULL);
185 return value->content_type;
190 * @value: value to reference
192 * Add another reference to the #SecretValue. For each reference
193 * secret_value_unref() should be called to unreference the value.
195 * Returns: (transfer full): the value
198 secret_value_ref (SecretValue *value)
200 g_return_val_if_fail (value, NULL);
201 g_atomic_int_inc (&value->refs);
206 * secret_value_unref:
207 * @value: (type SecretUnstable.Value) (allow-none): value to unreference
209 * Unreference a #SecretValue. When the last reference is gone, then
210 * the value will be freed.
213 secret_value_unref (gpointer value)
215 SecretValue *val = value;
217 g_return_if_fail (value != NULL);
219 if (g_atomic_int_dec_and_test (&val->refs)) {
220 g_free (val->content_type);
222 (val->destroy) (val->secret);
223 g_slice_free (SecretValue, val);
228 is_password_value (SecretValue *value)
230 if (value->content_type && g_str_equal (value->content_type, "text/plain"))
233 /* gnome-keyring-daemon used to return passwords like this, so support this, but validate */
234 if (!value->content_type || g_str_equal (value->content_type, "application/octet-stream"))
235 return g_utf8_validate (value->secret, value->length, NULL);
241 _secret_value_unref_to_password (SecretValue *value)
243 SecretValue *val = value;
246 g_return_val_if_fail (value != NULL, NULL);
248 if (!is_password_value (value)) {
249 secret_value_unref (value);
253 if (g_atomic_int_dec_and_test (&val->refs)) {
254 if (val->destroy == egg_secure_free) {
255 result = val->secret;
258 result = egg_secure_strndup (val->secret, val->length);
260 (val->destroy) (val->secret);
262 g_free (val->content_type);
263 g_slice_free (SecretValue, val);
266 result = egg_secure_strndup (val->secret, val->length);
273 _secret_value_unref_to_string (SecretValue *value)
275 SecretValue *val = value;
278 g_return_val_if_fail (value != NULL, NULL);
280 if (!is_password_value (value)) {
281 secret_value_unref (value);
285 if (g_atomic_int_dec_and_test (&val->refs)) {
286 if (val->destroy == g_free) {
287 result = val->secret;
290 result = g_strndup (val->secret, val->length);
292 (val->destroy) (val->secret);
294 g_free (val->content_type);
295 g_slice_free (SecretValue, val);
298 result = g_strndup (val->secret, val->length);