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().
47 * A secret value, like a password or other binary secret.
50 EGG_SECURE_DECLARE (secret_value);
56 GDestroyNotify destroy;
61 secret_value_get_type (void)
63 static gsize initialized = 0;
64 static GType type = 0;
66 if (g_once_init_enter (&initialized)) {
67 type = g_boxed_type_register_static ("SecretValue",
68 (GBoxedCopyFunc)secret_value_ref,
69 (GBoxedFreeFunc)secret_value_unref);
70 g_once_init_leave (&initialized, 1);
78 * @secret: the secret data
79 * @length: the length of the data
80 * @content_type: the content type of the data
82 * Create a #SecretValue for the secret data passed in. The secret data is
83 * copied into non-pageable 'secure' memory.
85 * If the length is less than zero, then @secret is assumed to be
88 * Returns: (transfer full): the new #SecretValue
91 secret_value_new (const gchar *secret,
93 const gchar *content_type)
97 g_return_val_if_fail (secret == NULL || length != 0, NULL);
98 g_return_val_if_fail (content_type, NULL);
101 length = strlen (secret);
103 copy = egg_secure_alloc (length + 1);
104 memcpy (copy, secret, length);
106 return secret_value_new_full (copy, length, content_type, egg_secure_free);
110 * secret_value_new_full:
111 * @secret: the secret data
112 * @length: the length of the data
113 * @content_type: the content type of the data
114 * @destroy: function to call to free the secret data
116 * Create a #SecretValue for the secret data passed in. The secret data is
117 * not copied, and will later be freed with the @destroy function.
119 * If the length is less than zero, then @secret is assumed to be
122 * Returns: (transfer full): the new #SecretValue
125 secret_value_new_full (gchar *secret,
127 const gchar *content_type,
128 GDestroyNotify destroy)
132 g_return_val_if_fail (secret == NULL || length != 0, NULL);
133 g_return_val_if_fail (content_type, NULL);
136 length = strlen (secret);
138 value = g_slice_new0 (SecretValue);
140 value->content_type = g_strdup (content_type);
141 value->destroy = destroy;
142 value->length = length;
143 value->secret = secret;
151 * @length: (out): the length of the secret
153 * Get the secret data in the #SecretValue. The value is not necessarily
154 * null-terminated unless it was created with secret_value_new() or a
155 * null-terminated string was passed to secret_value_new_full().
157 * Returns: (array length=length): the secret data
160 secret_value_get (SecretValue *value,
163 g_return_val_if_fail (value, NULL);
165 *length = value->length;
166 return value->secret;
170 * secret_value_get_content_type:
173 * Get the content type of the secret value, such as
174 * <literal>text/plain</literal>.
176 * Returns: the content type
179 secret_value_get_content_type (SecretValue *value)
181 g_return_val_if_fail (value, NULL);
182 return value->content_type;
187 * @value: value to reference
189 * Add another reference to the #SecretValue. For each reference
190 * secret_value_unref() should be called to unreference the value.
192 * Returns: (transfer full): the value
195 secret_value_ref (SecretValue *value)
197 g_return_val_if_fail (value, NULL);
198 g_atomic_int_inc (&value->refs);
203 * secret_value_unref:
204 * @value: (type Secret.Value) (allow-none): value to unreference
206 * Unreference a #SecretValue. When the last reference is gone, then
207 * the value will be freed.
210 secret_value_unref (gpointer value)
212 SecretValue *val = value;
214 g_return_if_fail (value != NULL);
216 if (g_atomic_int_dec_and_test (&val->refs)) {
217 g_free (val->content_type);
219 (val->destroy) (val->secret);
220 g_slice_free (SecretValue, val);
225 is_password_value (SecretValue *value)
227 if (value->content_type && g_str_equal (value->content_type, "text/plain"))
230 /* gnome-keyring-daemon used to return passwords like this, so support this, but validate */
231 if (!value->content_type || g_str_equal (value->content_type, "application/octet-stream"))
232 return g_utf8_validate (value->secret, value->length, NULL);
238 _secret_value_unref_to_password (SecretValue *value)
240 SecretValue *val = value;
243 g_return_val_if_fail (value != NULL, NULL);
245 if (!is_password_value (value)) {
246 secret_value_unref (value);
250 if (g_atomic_int_dec_and_test (&val->refs)) {
251 if (val->destroy == egg_secure_free) {
252 result = val->secret;
255 result = egg_secure_strndup (val->secret, val->length);
257 (val->destroy) (val->secret);
259 g_free (val->content_type);
260 g_slice_free (SecretValue, val);
263 result = egg_secure_strndup (val->secret, val->length);
270 _secret_value_unref_to_string (SecretValue *value)
272 SecretValue *val = value;
275 g_return_val_if_fail (value != NULL, NULL);
277 if (!is_password_value (value)) {
278 secret_value_unref (value);
282 if (g_atomic_int_dec_and_test (&val->refs)) {
283 if (val->destroy == g_free) {
284 result = val->secret;
287 result = g_strndup (val->secret, val->length);
289 (val->destroy) (val->secret);
291 g_free (val->content_type);
292 g_slice_free (SecretValue, val);
295 result = g_strndup (val->secret, val->length);