summaryrefslogtreecommitdiff
path: root/libsecret/secret-value.c
blob: 28322126a2db8dbef0918ad9d4e1e436d9928008 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* libsecret - GLib wrapper for Secret Service
 *
 * Copyright 2011 Collabora Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the licence or (at
 * your option) any later version.
 *
 * See the included COPYING file for more information.
 *
 * Author: Stef Walter <stefw@gnome.org>
 */

#include "config.h"

#include "secret-private.h"
#include "secret-value.h"

#include "egg/egg-secure-memory.h"

#include <string.h>

/**
 * SecretValue:
 *
 * A value containing a secret
 *
 * A #SecretValue contains a password or other secret value.
 *
 * Use [method@Value.get] to get the actual secret data, such as a password.
 * The secret data is not necessarily null-terminated, unless the content type
 * is "text/plain".
 *
 * Each #SecretValue has a content type. For passwords, this is `text/plain`.
 * Use [method@Value.get_content_type] to look at the content type.
 *
 * #SecretValue is reference counted and immutable. The secret data is only
 * freed when all references have been released via [method@Value.unref].
 *
 * Stability: Stable
 */

static gboolean     is_password_value    (SecretValue *value);

EGG_SECURE_DECLARE (secret_value);

struct _SecretValue {
	gint refs;
	gpointer secret;
	gsize length;
	GDestroyNotify destroy;
	gchar *content_type;
};

GType
secret_value_get_type (void)
{
	static gsize initialized = 0;
	static GType type = 0;

	if (g_once_init_enter (&initialized)) {
		type = g_boxed_type_register_static ("SecretValue",
		                                     (GBoxedCopyFunc)secret_value_ref,
		                                     (GBoxedFreeFunc)secret_value_unref);
		g_once_init_leave (&initialized, 1);
	}

	return type;
}

/**
 * secret_value_new:
 * @secret: the secret data
 * @length: the length of the data
 * @content_type: the content type of the data
 *
 * Create a #SecretValue for the secret data passed in.
 *
 * The secret data is copied into non-pageable 'secure' memory.
 *
 * If the length is less than zero, then @secret is assumed to be
 * null-terminated.
 *
 * Returns: (transfer full): the new #SecretValue
 */
SecretValue *
secret_value_new (const gchar *secret,
                  gssize length,
                  const gchar *content_type)
{
	gchar *copy;

	g_return_val_if_fail (length == 0 || secret != NULL, NULL);
	g_return_val_if_fail (content_type, NULL);

	if (length < 0)
		length = strlen (secret);

	copy = egg_secure_alloc (length + 1);
	if (secret)
		memcpy (copy, secret, length);
	copy[length] = 0;
	return secret_value_new_full (copy, length, content_type, egg_secure_free);
}

/**
 * secret_value_new_full:
 * @secret: the secret data
 * @length: the length of the data
 * @content_type: the content type of the data
 * @destroy: function to call to free the secret data
 *
 * Create a #SecretValue for the secret data passed in.
 *
 * The secret data is not copied, and will later be freed with the @destroy
 * function.
 *
 * If the length is less than zero, then @secret is assumed to be
 * null-terminated.
 *
 * Returns: (transfer full): the new #SecretValue
 */
SecretValue *
secret_value_new_full (gchar *secret,
                       gssize length,
                       const gchar *content_type,
                       GDestroyNotify destroy)
{
	SecretValue *value;

	g_return_val_if_fail (content_type, NULL);

	if (length < 0)
		length = strlen (secret);

	value = g_new0 (SecretValue, 1);
	value->refs = 1;
	value->content_type = g_strdup (content_type);
	value->destroy = destroy;
	value->length = length;
	value->secret = secret;

	return value;
}

/**
 * secret_value_get:
 * @value: the value
 * @length: the length of the secret
 *
 * Get the secret data in the #SecretValue.
 *
 * The value is not necessarily null-terminated unless it was created with
 * [ctor@Value.new] or a null-terminated string was passed to
 * [ctor@Value.new_full].
 *
 * Returns: (array length=length) (element-type guint8): the secret data
 */
const gchar *
secret_value_get (SecretValue *value,
                  gsize *length)
{
	g_return_val_if_fail (value, NULL);
	if (length)
		*length = value->length;
	return value->secret;
}

/**
 * secret_value_get_text:
 * @value: the value
 *
 * Get the secret data in the #SecretValue if it contains a textual
 * value.
 *
 * The content type must be `text/plain`.
 *
 * Returns: (nullable): the content type
 */
const gchar *
secret_value_get_text (SecretValue *value)
{
	g_return_val_if_fail (value, NULL);

	if (!is_password_value (value))
		return NULL;

	return value->secret;
}

/**
 * secret_value_get_content_type:
 * @value: the value
 *
 * Get the content type of the secret value, such as
 * `text/plain`.
 *
 * Returns: the content type
 */
const gchar *
secret_value_get_content_type (SecretValue *value)
{
	g_return_val_if_fail (value, NULL);
	return value->content_type;
}

/**
 * secret_value_ref:
 * @value: value to reference
 *
 * Add another reference to the #SecretValue.
 *
 * For each reference [method@Value.unref] should be called to unreference the
 * value.
 *
 * Returns: (transfer full): the value
 */
SecretValue *
secret_value_ref (SecretValue *value)
{
	g_return_val_if_fail (value, NULL);
	g_atomic_int_inc (&value->refs);
	return value;
}

/**
 * secret_value_unref:
 * @value: (type Secret.Value): value to unreference
 *
 * Unreference a #SecretValue.
 *
 * When the last reference is gone, then the value will be freed.
 */
void
secret_value_unref (gpointer value)
{
	SecretValue *val = value;

	g_return_if_fail (value != NULL);

	if (g_atomic_int_dec_and_test (&val->refs)) {
		g_free (val->content_type);
		if (val->destroy)
			(val->destroy) (val->secret);
		g_free (val);
	}
}

static gboolean
is_password_value (SecretValue *value)
{
	if (value->content_type && g_str_equal (value->content_type, "text/plain"))
		return TRUE;

	/* gnome-keyring-daemon used to return passwords like this, so support this, but validate */
	if (!value->content_type || g_str_equal (value->content_type, "application/octet-stream"))
		return g_utf8_validate (value->secret, value->length, NULL);

	return FALSE;
}

/**
 * secret_value_unref_to_password:
 * @value: the value
 * @length: (inout): the length of the secret
 *
 * Unreference a #SecretValue and steal the secret data in
 * #SecretValue as nonpageable memory.
 *
 * Returns: (transfer full): a new password string stored in nonpageable memory
 *   which must be freed with [func@password_free] when done
 *
 * Since: 0.19.0
 */
gchar *
secret_value_unref_to_password (SecretValue *value,
				gsize *length)
{
	SecretValue *val = value;
	gchar *result;

	g_return_val_if_fail (value != NULL, NULL);

	if (g_atomic_int_dec_and_test (&val->refs)) {
		if (val->destroy == egg_secure_free) {
			result = val->secret;
			if (length)
				*length = val->length;

		} else {
			result = egg_secure_strndup (val->secret, val->length);
			if (val->destroy)
				(val->destroy) (val->secret);
			if (length)
				*length = val->length;
		}
		g_free (val->content_type);
		g_free (val);

	} else {
		result = egg_secure_strndup (val->secret, val->length);
		if (length)
			*length = val->length;
	}

	return result;
}

gchar *
_secret_value_unref_to_password (SecretValue *value)
{
	g_return_val_if_fail (value != NULL, NULL);

	if (!is_password_value (value)) {
		secret_value_unref (value);
		return NULL;
	}

	return secret_value_unref_to_password (value, NULL);
}

gchar *
_secret_value_unref_to_string (SecretValue *value)
{
	SecretValue *val = value;
	gchar *result;

	g_return_val_if_fail (value != NULL, NULL);

	if (!is_password_value (value)) {
		secret_value_unref (value);
		return NULL;
	}

	if (g_atomic_int_dec_and_test (&val->refs)) {
		if (val->destroy == g_free) {
			result = val->secret;

		} else {
			result = g_strndup (val->secret, val->length);
			if (val->destroy)
				(val->destroy) (val->secret);
		}
		g_free (val->content_type);
		g_free (val);

	} else {
		result = g_strndup (val->secret, val->length);
	}

	return result;
}