summaryrefslogtreecommitdiff
path: root/daemon/dbus/gkd-secret-secret.c
blob: 12bf62e69ef2734bd8f3431361e615c03a159930 (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
/*
 * gnome-keyring
 *
 * Copyright (C) 2008 Stefan Walter
 *
 * 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 License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see
 * <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gkd-secret-dispatch.h"
#include "gkd-secret-error.h"
#include "gkd-secret-secret.h"
#include "gkd-secret-service.h"
#include "gkd-secret-session.h"

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

#include <glib-object.h>

#include <string.h>

GkdSecretSecret *
gkd_secret_secret_new (GkdSecretSession *session,
		       gconstpointer parameter,
		       gsize n_parameter,
		       gconstpointer value,
		       gsize n_value)
{
	return gkd_secret_secret_new_take_memory (session,
						  g_memdup (parameter, n_parameter),
						  n_parameter,
						  g_memdup (value, n_value),
						  n_value);
}

static void
destroy_with_owned_memory (gpointer data)
{
	GkdSecretSecret *secret = data;
	g_free (secret->parameter);
	g_free (secret->value);
}

GkdSecretSecret*
gkd_secret_secret_new_take_memory (GkdSecretSession *session,
				   gpointer parameter, gsize n_parameter,
				   gpointer value, gsize n_value)
{
	GkdSecretSecret *secret;

	g_return_val_if_fail (GKD_SECRET_IS_SESSION (session), NULL);

	secret = g_slice_new0 (GkdSecretSecret);
	secret->session = g_object_ref (session);
	secret->parameter = parameter;
	secret->n_parameter = n_parameter;
	secret->value = value;
	secret->n_value = n_value;

	secret->destroy_func = destroy_with_owned_memory;
	secret->destroy_data = secret;

	return secret;
}

GkdSecretSecret*
gkd_secret_secret_parse (GkdSecretService *service,
			 const char *sender,
			 GVariant *variant,
			 GError **error)
{
	GkdSecretSecret *secret = NULL;
	GkdSecretSession *session;
	const char *parameter, *value, *path, *content_type;
	gsize n_parameter, n_value;
	GVariant *parameter_variant, *value_variant;

	g_return_val_if_fail (GKD_SECRET_IS_SERVICE (service), NULL);
	g_return_val_if_fail (variant, NULL);
	g_return_val_if_fail (sender, NULL);

	g_variant_get (variant, "(&o^&ay^&ay&s)", &path, NULL, NULL, &content_type);

	/* parameter */
	parameter_variant = g_variant_get_child_value (variant, 1);
	parameter = g_variant_get_fixed_array (parameter_variant, &n_parameter, sizeof (guint8));

	/* value */
	value_variant = g_variant_get_child_value (variant, 2);
	value = g_variant_get_fixed_array (value_variant, &n_value, sizeof (guint8));

	/* Try to lookup the session */
	session = gkd_secret_service_lookup_session (service, path, sender);
	if (session == NULL) {
		g_set_error_literal (error, GKD_SECRET_ERROR,
				     GKD_SECRET_ERROR_NO_SESSION,
				     "The session wrapping the secret does not exist");
		goto out;
	}

	secret = g_slice_new0 (GkdSecretSecret);
	secret->session = g_object_ref (session);
	secret->parameter = g_strndup (parameter, n_parameter);
	secret->n_parameter = n_parameter;
	secret->value = g_strndup (value, n_value);
	secret->n_value = n_value;

 out:
	g_variant_unref (parameter_variant);
	g_variant_unref (value_variant);

	return secret;
}

GVariant *
gkd_secret_secret_append (GkdSecretSecret *secret)
{
	GVariantBuilder builder;
	const gchar *content_type = "text/plain";
	const gchar *path;
	GVariant *parameter, *value;

	path = gkd_secret_dispatch_get_object_path (GKD_SECRET_DISPATCH (secret->session));
	g_return_val_if_fail (path, NULL);

	g_variant_builder_init (&builder, G_VARIANT_TYPE ("ay"));
	g_variant_builder_add (&builder, "y", secret->parameter);
	parameter = g_variant_builder_end (&builder);

	g_variant_builder_init (&builder, G_VARIANT_TYPE ("ay"));
	g_variant_builder_add (&builder, "y", secret->value);
	value = g_variant_builder_end (&builder);

	return g_variant_new ("(o@ay@ays)", path, parameter, value, content_type);
}

void
gkd_secret_secret_free (gpointer data)
{
	GkdSecretSecret *secret;

	if (!data)
		return;

	secret = data;

	/*
	 * These are not usually actual plain text secrets. However in
	 * the case that they are, we want to clear them from memory.
	 *
	 * This is not foolproof in any way. If they're plaintext, they would
	 * have been sent over DBus, and through all sorts of processes.
	 */

	egg_secure_clear (secret->parameter, secret->n_parameter);
	egg_secure_clear (secret->value, secret->n_value);

	g_object_unref (secret->session);

	/* Call the destructor of memory */
	if (secret->destroy_func)
		(secret->destroy_func) (secret->destroy_data);

	g_slice_free (GkdSecretSecret, secret);
}