1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* gck-password.c - the GObject PKCS#11 wrapper library
4 Copyright (C) 2011 Collabora Ltd.
6 The Gnome Keyring Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The Gnome Keyring Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the Gnome Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Author: Stef Walter <stefw@collabora.co.uk>
27 #include "gck-private.h"
29 #include "egg/egg-timegm.h"
34 * SECTION:gck-password
36 * @short_description: Represents a password hich is requested of the user
38 * This is used in conjuction with GTlsInteraction. #GckPassword is a
39 * GTlsPassword which contains additional information about which PKCS\#11
40 * token or key the password is being requested for.
46 * A #GTlsPasswordClass that contains information about the PKCS\#11 token
47 * or key the password is being requested for.
52 * @parent: parent class
54 * The class for #GTlsPassword.
63 struct _GckPasswordPrivate {
65 gpointer token_or_key;
68 G_DEFINE_TYPE (GckPassword, gck_password, G_TYPE_TLS_PASSWORD);
71 gck_password_init (GckPassword *self)
73 self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCK_TYPE_PASSWORD, GckPasswordPrivate);
77 gck_password_constructed (GObject *obj)
79 GckPassword *self = GCK_PASSWORD (obj);
81 G_OBJECT_CLASS (gck_password_parent_class)->constructed (obj);
83 g_return_if_fail (GCK_IS_SLOT (self->pv->token_or_key) ||
84 GCK_IS_OBJECT (self->pv->token_or_key));
88 gck_password_get_property (GObject *obj,
93 GckPassword *self = GCK_PASSWORD (obj);
97 g_value_take_object (value, gck_password_get_module (self));
100 g_value_take_object (value, gck_password_get_token (self));
103 g_value_take_object (value, gck_password_get_key (self));
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
112 gck_password_set_property (GObject *obj,
117 GckPassword *self = GCK_PASSWORD (obj);
120 /* All writes to data members below, happen only during construct phase */
124 object = g_value_dup_object (value);
125 if (object != NULL) {
126 g_assert (self->pv->token_or_key == NULL);
127 self->pv->token_or_key = object;
128 self->pv->for_token = TRUE;
132 object = g_value_dup_object (value);
133 if (object != NULL) {
134 g_assert (self->pv->token_or_key == NULL);
135 self->pv->token_or_key = object;
136 self->pv->for_token = FALSE;
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
146 gck_password_finalize (GObject *obj)
148 GckPassword *self = GCK_PASSWORD (obj);
150 g_clear_object (&self->pv->token_or_key);
152 G_OBJECT_CLASS (gck_password_parent_class)->finalize (obj);
156 gck_password_class_init (GckPasswordClass *klass)
158 GObjectClass *gobject_class = (GObjectClass*)klass;
160 gobject_class->constructed = gck_password_constructed;
161 gobject_class->get_property = gck_password_get_property;
162 gobject_class->set_property = gck_password_set_property;
163 gobject_class->finalize = gck_password_finalize;
166 * GckPassword:module:
168 * The PKCS\#11 module that is requesting the password
170 g_object_class_install_property (gobject_class, PROP_MODULE,
171 g_param_spec_object ("module", "Module", "PKCS11 Module",
172 GCK_TYPE_MODULE, G_PARAM_READABLE));
177 * The PKCS\#11 token the password is for, if this is set then
178 * the GckPassword:object property will be %NULL
180 g_object_class_install_property (gobject_class, PROP_TOKEN,
181 g_param_spec_object ("token", "Token", "PKCS11 Token",
182 GCK_TYPE_SLOT, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
187 * The PKCS\#11 key that the password is being requested for. If this
188 * is set then the GckPassword:token property will be %NULL
190 g_object_class_install_property (gobject_class, PROP_KEY,
191 g_param_spec_object ("key", "Object", "PKCS11 Key Object",
192 GCK_TYPE_OBJECT, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
194 g_type_class_add_private (gobject_class, sizeof (GckPasswordPrivate));
198 * gck_password_get_module:
199 * @self: the password object
201 * Get the PKCS\#11 module that is requesting the password.
203 * Returns: (transfer full): the module that is requesting the password, which
204 * must be unreferenced after use
207 gck_password_get_module (GckPassword *self)
209 g_return_val_if_fail (GCK_IS_PASSWORD (self), NULL);
210 if (self->pv->for_token)
211 return gck_slot_get_module (self->pv->token_or_key);
213 return gck_object_get_module (self->pv->token_or_key);
217 * gck_password_get_token:
218 * @self: the password object
220 * If the password request is to unlock a PKCS\#11 token, then this is the
221 * slot containing that token.
223 * Returns: (transfer full): the slot that contains the token, or %NULL if not
224 * being requested for a token; must be unreferenced after use
227 gck_password_get_token (GckPassword *self)
229 g_return_val_if_fail (GCK_IS_PASSWORD (self), NULL);
230 if (!self->pv->for_token)
232 g_return_val_if_fail (GCK_IS_SLOT (self->pv->token_or_key), NULL);
233 return g_object_ref (self->pv->token_or_key);
237 * gck_password_get_key:
238 * @self: the password object
240 * If the password request is to unlock a PKCS\#11 key, then this is the
241 * the object representing that key.
243 * Returns: (transfer full): the password is for this key, or %NULL if not
244 * being requested for a key; must be unreferenced after use
247 gck_password_get_key (GckPassword *self)
249 g_return_val_if_fail (GCK_IS_PASSWORD (self), NULL);
250 if (self->pv->for_token)
252 g_return_val_if_fail (GCK_IS_OBJECT (self->pv->token_or_key), NULL);
253 return g_object_ref (self->pv->token_or_key);