4
* Copyright (C) 2008 Stefan Walter
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as
8
* published by the Free Software Foundation; either version 2.1 of
9
* the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24
#include "gck-secret.h"
26
#include "egg/egg-secure-memory.h"
36
G_DEFINE_TYPE (GckSecret, gck_secret, G_TYPE_OBJECT);
38
/* -----------------------------------------------------------------------------
43
gck_secret_init (GckSecret *self)
49
gck_secret_dispose (GObject *obj)
51
GckSecret *self = GCK_SECRET (obj);
53
egg_secure_clear (self->memory, self->n_memory);
55
G_OBJECT_CLASS (gck_secret_parent_class)->dispose (obj);
59
gck_secret_finalize (GObject *obj)
61
GckSecret *self = GCK_SECRET (obj);
63
egg_secure_free (self->memory);
67
G_OBJECT_CLASS (gck_secret_parent_class)->finalize (obj);
71
gck_secret_class_init (GckSecretClass *klass)
73
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
74
gobject_class->dispose = gck_secret_dispose;
75
gobject_class->finalize = gck_secret_finalize;
78
/* -----------------------------------------------------------------------------
83
gck_secret_new (const guchar *data, gssize n_data)
85
GckSecret *secret = g_object_new (GCK_TYPE_SECRET, NULL);
89
secret->memory = (guchar*)egg_secure_strdup ((const gchar*)data);
90
secret->n_memory = strlen ((const gchar*)data);
92
secret->memory = egg_secure_alloc (n_data + 1);
93
memcpy (secret->memory, data, n_data);
94
secret->n_memory = n_data;
97
secret->memory = NULL;
105
gck_secret_new_from_login (CK_UTF8CHAR_PTR pin, CK_ULONG n_pin)
107
if (n_pin == (CK_ULONG)-1)
108
return gck_secret_new ((const guchar*)pin, -1);
110
return gck_secret_new ((const guchar*)pin, (gssize)n_pin);
114
gck_secret_new_from_password (const gchar *password)
116
return gck_secret_new ((const guchar*)password, -1);
120
gck_secret_get (GckSecret *self, gsize *n_data)
122
g_return_val_if_fail (GCK_IS_SECRET (self), NULL);
123
g_return_val_if_fail (n_data, NULL);
124
*n_data = self->n_memory;
129
gck_secret_get_password (GckSecret *self, gsize *n_data)
131
g_return_val_if_fail (GCK_IS_SECRET (self), NULL);
132
g_return_val_if_fail (n_data, NULL);
133
*n_data = self->n_memory;
134
return (gchar*)self->memory;
138
gck_secret_equal (GckSecret *self, GckSecret *other)
140
g_return_val_if_fail (GCK_IS_SECRET (self), FALSE);
141
g_return_val_if_fail (GCK_IS_SECRET (other), FALSE);
144
return gck_secret_equals (self, other->memory, other->n_memory);
148
gck_secret_equals (GckSecret *self, const guchar* pin, gssize n_pin)
150
g_return_val_if_fail (GCK_IS_SECRET (self), FALSE);
152
/* In case they're different somewhere */
153
if (n_pin == (CK_ULONG)-1)
156
if (n_pin == -1 && pin != NULL)
157
n_pin = strlen ((const gchar*)pin);
159
/* The same length */
160
if (n_pin != self->n_memory)
163
/* Two null passwords */
164
if (!pin && !self->memory)
167
/* For our purposes a null password equals an empty password */
171
/* One null, one not null */
172
if (!pin || !self->memory)
175
/* Compare actual memory */
176
return memcmp (pin, self->memory, n_pin) == 0;