summaryrefslogtreecommitdiff
path: root/gck/gck-password.c
blob: 4564fa530ff7a0b21790da730af2a0b1b34b032d (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gck-password.c - the GObject PKCS#11 wrapper library

   Copyright (C) 2011 Collabora Ltd.

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   see <http://www.gnu.org/licenses/>.

   Author: Stef Walter <stefw@collabora.co.uk>
*/

#include "config.h"

#include "gck.h"
#include "gck-private.h"

#include <string.h>

/**
 * GckPassword:
 *
 * Represents a password which is requested of the user.
 *
 * This is used in conjuction with [class@Gio.TlsInteraction]. `GckPassword` is
 * a [class@Gio.TlsPassword] which contains additional information about which
 * PKCS#11 token or key the password is being requested for.
 */

/**
 * GckPasswordClass:
 * @parent: parent class
 *
 * The class struct for [class@Password].
 */

enum {
	PROP_0,
	PROP_MODULE,
	PROP_TOKEN,
	PROP_KEY
};

struct _GckPassword {
	GTlsPassword parent_instance;

	gboolean for_token;
	gpointer token_or_key;
};

G_DEFINE_TYPE (GckPassword, gck_password, G_TYPE_TLS_PASSWORD);

static void
gck_password_init (GckPassword *self)
{
}

static void
gck_password_constructed (GObject *obj)
{
	GckPassword *self = GCK_PASSWORD (obj);

	G_OBJECT_CLASS (gck_password_parent_class)->constructed (obj);

	g_return_if_fail (GCK_IS_SLOT (self->token_or_key) ||
	                  GCK_IS_OBJECT (self->token_or_key));
}

static void
gck_password_get_property (GObject *obj,
                           guint prop_id,
                           GValue *value,
                           GParamSpec *pspec)
{
	GckPassword *self = GCK_PASSWORD (obj);

	switch (prop_id) {
	case PROP_MODULE:
		g_value_take_object (value, gck_password_get_module (self));
		break;
	case PROP_TOKEN:
		g_value_take_object (value, gck_password_get_token (self));
		break;
	case PROP_KEY:
		g_value_take_object (value, gck_password_get_key (self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
		break;
	}
}

static void
gck_password_set_property (GObject *obj,
                           guint prop_id,
                           const GValue *value,
                           GParamSpec *pspec)
{
	GckPassword *self = GCK_PASSWORD (obj);
	gpointer object;

	/* All writes to data members below, happen only during construct phase */

	switch (prop_id) {
	case PROP_TOKEN:
		object = g_value_dup_object (value);
		if (object != NULL) {
			g_assert (self->token_or_key == NULL);
			self->token_or_key = object;
			self->for_token = TRUE;
		}
		break;
	case PROP_KEY:
		object = g_value_dup_object (value);
		if (object != NULL) {
			g_assert (self->token_or_key == NULL);
			self->token_or_key = object;
			self->for_token = FALSE;
		}
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
		break;
	}
}

static void
gck_password_finalize (GObject *obj)
{
	GckPassword *self = GCK_PASSWORD (obj);

	g_clear_object (&self->token_or_key);

	G_OBJECT_CLASS (gck_password_parent_class)->finalize (obj);
}

static void
gck_password_class_init (GckPasswordClass *klass)
{
	GObjectClass *gobject_class = (GObjectClass*)klass;

	gobject_class->constructed = gck_password_constructed;
	gobject_class->get_property = gck_password_get_property;
	gobject_class->set_property = gck_password_set_property;
	gobject_class->finalize = gck_password_finalize;

	/**
	 * GckPassword:module:
	 *
	 * The PKCS#11 module that is requesting the password
	 */
	g_object_class_install_property (gobject_class, PROP_MODULE,
		g_param_spec_object ("module", "Module", "PKCS11 Module",
		                     GCK_TYPE_MODULE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

	/**
	 * GckPassword:token:
	 *
	 * The PKCS#11 token the password is for, if this is set then
	 * the GckPassword:object property will be %NULL
	 */
	g_object_class_install_property (gobject_class, PROP_TOKEN,
		g_param_spec_object ("token", "Token", "PKCS11 Token",
		                     GCK_TYPE_SLOT, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

	/**
	 * GckPassword:key:
	 *
	 * The PKCS#11 key that the password is being requested for. If this
	 * is set then the GckPassword:token property will be %NULL
	 */
	g_object_class_install_property (gobject_class, PROP_KEY,
		g_param_spec_object ("key", "Object", "PKCS11 Key Object",
		                     GCK_TYPE_OBJECT, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
}

/**
 * gck_password_get_module:
 * @self: the password object
 *
 * Get the PKCS#11 module that is requesting the password.
 *
 * Returns: (transfer full): the module that is requesting the password, which
 *          must be unreferenced after use
 */
GckModule *
gck_password_get_module (GckPassword *self)
{
	g_return_val_if_fail (GCK_IS_PASSWORD (self), NULL);
	if (self->for_token)
		return gck_slot_get_module (self->token_or_key);
	else
		return gck_object_get_module (self->token_or_key);
}

/**
 * gck_password_get_token:
 * @self: the password object
 *
 * If the password request is to unlock a PKCS#11 token, then this is the
 * slot containing that token.
 *
 * Returns: (transfer full): the slot that contains the token, or %NULL if not
 *          being requested for a token; must be unreferenced after use
 */
GckSlot *
gck_password_get_token (GckPassword *self)
{
	g_return_val_if_fail (GCK_IS_PASSWORD (self), NULL);
	if (!self->for_token)
		return NULL;
	g_return_val_if_fail (GCK_IS_SLOT (self->token_or_key), NULL);
	return g_object_ref (self->token_or_key);
}

/**
 * gck_password_get_key:
 * @self: the password object
 *
 * If the password request is to unlock a PKCS#11 key, then this is the
 * the object representing that key.
 *
 * Returns: (transfer full): the password is for this key, or %NULL if not
 *          being requested for a key; must be unreferenced after use
 */
GckObject *
gck_password_get_key (GckPassword *self)
{
	g_return_val_if_fail (GCK_IS_PASSWORD (self), NULL);
	if (self->for_token)
		return NULL;
	g_return_val_if_fail (GCK_IS_OBJECT (self->token_or_key), NULL);
	return g_object_ref (self->token_or_key);
}