summaryrefslogtreecommitdiff
path: root/daemon/dbus/gkd-secret-util.c
blob: 29443f8c62ea1813919897377e90e83950f727f2 (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
/*
 * 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-util.h"

#include "pkcs11/pkcs11i.h"

#include <string.h>

/* -----------------------------------------------------------------------------
 * INTERNAL
 */

static gchar*
decode_object_identifier (const gchar* enc, gssize length)
{
	GString *result;

	g_assert (enc);

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

	result = g_string_sized_new (length);
	while (length > 0) {
		char ch = *(enc++);
		--length;

		/* Underscores get special handling */
		if (G_UNLIKELY (ch == '_' &&
				g_ascii_isxdigit(enc[0]) &&
				g_ascii_isxdigit (enc[1]))) {
			ch = (g_ascii_xdigit_value (enc[0]) * 16) +
			     (g_ascii_xdigit_value (enc[1]));
			enc += 2;
			length -= 2;
		}

		g_string_append_c (result, ch);
	}

	return g_string_free (result, FALSE);
}

gboolean
gkd_secret_util_parse_path (const gchar *path, gchar **collection, gchar **item)
{
	const gchar *pos;

	g_return_val_if_fail (path, FALSE);

	/* Make sure it starts with our prefix */
	if (g_str_has_prefix (path, SECRET_COLLECTION_PREFIX))
		path += strlen (SECRET_COLLECTION_PREFIX);
	else if (g_str_has_prefix (path, SECRET_ALIAS_PREFIX))
		path += strlen (SECRET_ALIAS_PREFIX);
	else
		return FALSE;

	/* Skip the path separator */
	if (path[0] != '/')
		return FALSE;
	++path;

	/* Make sure we have something */
	if (path[0] == '\0')
		return FALSE;

	pos = strchr (path, '/');

	/* No item, just a collection */
	if (pos == NULL) {
		if (collection)
			*collection = decode_object_identifier (path, -1);
		if (item)
			*item = NULL;
		return TRUE;
	}

	/* Make sure we have an item, and no further path bits */
	if (pos[1] == '\0' || strchr (pos + 1, '/'))
		return FALSE;

	if (collection)
		*collection = decode_object_identifier (path, pos - path);
	if (item)
		*item = decode_object_identifier (pos + 1, -1);
	return TRUE;
}

gchar*
gkd_secret_util_build_path (const gchar *base, gconstpointer identifier, gssize n_identifier)
{
	GString *result;
	const gchar *name;
	gsize length;

	g_assert (base);
	g_assert (base[0] == '/');
	g_assert (identifier);

	name = identifier;
	if (n_identifier < 0)
		length = strlen (name);
	else
		length = n_identifier;

	result = g_string_new (base);
	if (!g_str_has_suffix (base, "/"))
		g_string_append_c (result, '/');

	while (length > 0) {
		char ch = *(name++);
		--length;

		/* Normal characters can go right through */
		if (G_LIKELY ((ch >= 'A' && ch <= 'Z') ||
			      (ch >= 'a' && ch <= 'z') ||
			      (ch >= '0' && ch <= '9'))) {
			g_string_append_c (result, ch);

		/* Special characters are encoded with a _ */
		} else {
			g_string_append_printf (result, "_%02x", (unsigned int)(unsigned char)ch);
		}
	}

	return g_string_free (result, FALSE);
}