~ubuntu-branches/ubuntu/natty/gnome-keyring/natty

« back to all changes in this revision

Viewing changes to pkcs11/gck/gck-secret.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-02-16 19:00:06 UTC
  • mfrom: (1.1.58 upstream)
  • Revision ID: james.westby@ubuntu.com-20100216190006-cqpnic4zxlkmmi0o
Tags: 2.29.90git20100218-0ubuntu1
Updated to a git snapshot version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * gnome-keyring
 
3
 * 
 
4
 * Copyright (C) 2008 Stefan Walter
 
5
 * 
 
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.
 
10
 *  
 
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.
 
15
 *  
 
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
 
19
 * 02111-1307, USA.  
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include "gck-secret.h"
 
25
 
 
26
#include "egg/egg-secure-memory.h"
 
27
 
 
28
#include <string.h>
 
29
 
 
30
struct _GckSecret {
 
31
        GObject parent;
 
32
        guchar *memory;
 
33
        gsize n_memory;
 
34
};
 
35
 
 
36
G_DEFINE_TYPE (GckSecret, gck_secret, G_TYPE_OBJECT);
 
37
 
 
38
/* -----------------------------------------------------------------------------
 
39
 * OBJECT 
 
40
 */
 
41
 
 
42
static void
 
43
gck_secret_init (GckSecret *self)
 
44
{
 
45
 
 
46
}
 
47
 
 
48
static void
 
49
gck_secret_dispose (GObject *obj)
 
50
{
 
51
        GckSecret *self = GCK_SECRET (obj);
 
52
        
 
53
        egg_secure_clear (self->memory, self->n_memory);
 
54
    
 
55
        G_OBJECT_CLASS (gck_secret_parent_class)->dispose (obj);
 
56
}
 
57
 
 
58
static void
 
59
gck_secret_finalize (GObject *obj)
 
60
{
 
61
        GckSecret *self = GCK_SECRET (obj);
 
62
 
 
63
        egg_secure_free (self->memory);
 
64
        self->memory = NULL;
 
65
        self->n_memory = 0;
 
66
 
 
67
        G_OBJECT_CLASS (gck_secret_parent_class)->finalize (obj);
 
68
}
 
69
 
 
70
static void
 
71
gck_secret_class_init (GckSecretClass *klass)
 
72
{
 
73
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
74
        gobject_class->dispose = gck_secret_dispose;
 
75
        gobject_class->finalize = gck_secret_finalize;
 
76
}
 
77
 
 
78
/* -----------------------------------------------------------------------------
 
79
 * PUBLIC 
 
80
 */
 
81
 
 
82
GckSecret*
 
83
gck_secret_new (const guchar *data, gssize n_data)
 
84
{
 
85
        GckSecret *secret = g_object_new (GCK_TYPE_SECRET, NULL);
 
86
        
 
87
        if (data) {
 
88
                if (n_data == -1) {
 
89
                        secret->memory = (guchar*)egg_secure_strdup ((const gchar*)data);
 
90
                        secret->n_memory = strlen ((const gchar*)data);
 
91
                } else {
 
92
                        secret->memory = egg_secure_alloc (n_data + 1);
 
93
                        memcpy (secret->memory, data, n_data);
 
94
                        secret->n_memory = n_data;
 
95
                }
 
96
        } else {
 
97
                secret->memory = NULL;
 
98
                secret->n_memory = 0;
 
99
        }
 
100
        
 
101
        return secret;
 
102
}
 
103
 
 
104
GckSecret*
 
105
gck_secret_new_from_login (CK_UTF8CHAR_PTR pin, CK_ULONG n_pin)
 
106
{
 
107
        if (n_pin == (CK_ULONG)-1)
 
108
                return gck_secret_new ((const guchar*)pin, -1);
 
109
        else
 
110
                return gck_secret_new ((const guchar*)pin, (gssize)n_pin);
 
111
}
 
112
 
 
113
GckSecret*
 
114
gck_secret_new_from_password (const gchar *password)
 
115
{
 
116
        return gck_secret_new ((const guchar*)password, -1);
 
117
}
 
118
 
 
119
const guchar*
 
120
gck_secret_get (GckSecret *self, gsize *n_data)
 
121
{
 
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;
 
125
        return self->memory;
 
126
}
 
127
 
 
128
const gchar*
 
129
gck_secret_get_password (GckSecret *self, gsize *n_data)
 
130
{
 
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;
 
135
}
 
136
 
 
137
gboolean
 
138
gck_secret_equal (GckSecret *self, GckSecret *other)
 
139
{
 
140
        g_return_val_if_fail (GCK_IS_SECRET (self), FALSE);
 
141
        g_return_val_if_fail (GCK_IS_SECRET (other), FALSE);
 
142
        if (self == other)
 
143
                return TRUE;
 
144
        return gck_secret_equals (self, other->memory, other->n_memory);
 
145
}
 
146
 
 
147
gboolean
 
148
gck_secret_equals (GckSecret *self, const guchar* pin, gssize n_pin)
 
149
{
 
150
        g_return_val_if_fail (GCK_IS_SECRET (self), FALSE);
 
151
        
 
152
        /* In case they're different somewhere */
 
153
        if (n_pin == (CK_ULONG)-1)
 
154
                n_pin = -1;
 
155
        
 
156
        if (n_pin == -1 && pin != NULL)
 
157
                n_pin = strlen ((const gchar*)pin);
 
158
 
 
159
        /* The same length */
 
160
        if (n_pin != self->n_memory)
 
161
                return FALSE;
 
162
 
 
163
        /* Two null passwords */
 
164
        if (!pin && !self->memory)
 
165
                return TRUE;
 
166
 
 
167
        /* For our purposes a null password equals an empty password */
 
168
        if (n_pin == 0)
 
169
                return TRUE;
 
170
 
 
171
        /* One null, one not null */
 
172
        if (!pin || !self->memory)
 
173
                return FALSE;
 
174
 
 
175
        /* Compare actual memory */
 
176
        return memcmp (pin, self->memory, n_pin) == 0;
 
177
}