minimal build
[platform/upstream/gcr.git] / gck / gck-password.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* gck-password.c - the GObject PKCS#11 wrapper library
3
4    Copyright (C) 2011 Collabora Ltd.
5
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.
10
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.
15
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.
20
21    Author: Stef Walter <stefw@collabora.co.uk>
22 */
23
24 #include "config.h"
25
26 #include "gck.h"
27 #include "gck-private.h"
28
29 #include "egg/egg-timegm.h"
30
31 #include <string.h>
32
33 /**
34  * SECTION:gck-password
35  * @title: GckPassword
36  * @short_description: Represents a password hich is requested of the user
37  *
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.
41  */
42
43 /**
44  * GckPassword:
45  *
46  * A #GTlsPasswordClass that contains information about the PKCS\#11 token
47  * or key the password is being requested for.
48  */
49
50 /**
51  * GckPasswordClass:
52  * @parent: parent class
53  *
54  * The class for #GTlsPassword.
55  */
56 enum {
57         PROP_0,
58         PROP_MODULE,
59         PROP_TOKEN,
60         PROP_KEY
61 };
62
63 struct _GckPasswordPrivate {
64         gboolean for_token;
65         gpointer token_or_key;
66 };
67
68 G_DEFINE_TYPE (GckPassword, gck_password, G_TYPE_TLS_PASSWORD);
69
70 static void
71 gck_password_init (GckPassword *self)
72 {
73         self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCK_TYPE_PASSWORD, GckPasswordPrivate);
74 }
75
76 static void
77 gck_password_constructed (GObject *obj)
78 {
79         GckPassword *self = GCK_PASSWORD (obj);
80
81         G_OBJECT_CLASS (gck_password_parent_class)->constructed (obj);
82
83         g_return_if_fail (GCK_IS_SLOT (self->pv->token_or_key) ||
84                           GCK_IS_OBJECT (self->pv->token_or_key));
85 }
86
87 static void
88 gck_password_get_property (GObject *obj,
89                            guint prop_id,
90                            GValue *value,
91                            GParamSpec *pspec)
92 {
93         GckPassword *self = GCK_PASSWORD (obj);
94
95         switch (prop_id) {
96         case PROP_MODULE:
97                 g_value_take_object (value, gck_password_get_module (self));
98                 break;
99         case PROP_TOKEN:
100                 g_value_take_object (value, gck_password_get_token (self));
101                 break;
102         case PROP_KEY:
103                 g_value_take_object (value, gck_password_get_key (self));
104                 break;
105         default:
106                 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
107                 break;
108         }
109 }
110
111 static void
112 gck_password_set_property (GObject *obj,
113                            guint prop_id,
114                            const GValue *value,
115                            GParamSpec *pspec)
116 {
117         GckPassword *self = GCK_PASSWORD (obj);
118         gpointer object;
119
120         /* All writes to data members below, happen only during construct phase */
121
122         switch (prop_id) {
123         case PROP_TOKEN:
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;
129                 }
130                 break;
131         case PROP_KEY:
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;
137                 }
138                 break;
139         default:
140                 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
141                 break;
142         }
143 }
144
145 static void
146 gck_password_finalize (GObject *obj)
147 {
148         GckPassword *self = GCK_PASSWORD (obj);
149
150         g_clear_object (&self->pv->token_or_key);
151
152         G_OBJECT_CLASS (gck_password_parent_class)->finalize (obj);
153 }
154
155 static void
156 gck_password_class_init (GckPasswordClass *klass)
157 {
158         GObjectClass *gobject_class = (GObjectClass*)klass;
159
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;
164
165         /**
166          * GckPassword:module:
167          *
168          * The PKCS\#11 module that is requesting the password
169          */
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));
173
174         /**
175          * GckPassword:token:
176          *
177          * The PKCS\#11 token the password is for, if this is set then
178          * the GckPassword:object property will be %NULL
179          */
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));
183
184         /**
185          * GckPassword:key:
186          *
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
189          */
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));
193
194         g_type_class_add_private (gobject_class, sizeof (GckPasswordPrivate));
195 }
196
197 /**
198  * gck_password_get_module:
199  * @self: the password object
200  *
201  * Get the PKCS\#11 module that is requesting the password.
202  *
203  * Returns: (transfer full): the module that is requesting the password, which
204  *          must be unreferenced after use
205  */
206 GckModule *
207 gck_password_get_module (GckPassword *self)
208 {
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);
212         else
213                 return gck_object_get_module (self->pv->token_or_key);
214 }
215
216 /**
217  * gck_password_get_token:
218  * @self: the password object
219  *
220  * If the password request is to unlock a PKCS\#11 token, then this is the
221  * slot containing that token.
222  *
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
225  */
226 GckSlot *
227 gck_password_get_token (GckPassword *self)
228 {
229         g_return_val_if_fail (GCK_IS_PASSWORD (self), NULL);
230         if (!self->pv->for_token)
231                 return NULL;
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);
234 }
235
236 /**
237  * gck_password_get_key:
238  * @self: the password object
239  *
240  * If the password request is to unlock a PKCS\#11 key, then this is the
241  * the object representing that key.
242  *
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
245  */
246 GckObject *
247 gck_password_get_key (GckPassword *self)
248 {
249         g_return_val_if_fail (GCK_IS_PASSWORD (self), NULL);
250         if (self->pv->for_token)
251                 return NULL;
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);
254 }