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

« back to all changes in this revision

Viewing changes to pkcs11/gck/gck-login.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-login.h"
25
 
 
26
 
#include "egg/egg-secure-memory.h"
27
 
 
28
 
#include <string.h>
29
 
 
30
 
struct _GckLogin {
31
 
        GObject parent;
32
 
        gchar *password;
33
 
        gsize n_password;
34
 
};
35
 
 
36
 
G_DEFINE_TYPE (GckLogin, gck_login, G_TYPE_OBJECT);
37
 
 
38
 
/* -----------------------------------------------------------------------------
39
 
 * OBJECT 
40
 
 */
41
 
 
42
 
static void
43
 
gck_login_init (GckLogin *self)
44
 
{
45
 
 
46
 
}
47
 
 
48
 
static void
49
 
gck_login_dispose (GObject *obj)
50
 
{
51
 
        GckLogin *self = GCK_LOGIN (obj);
52
 
        
53
 
        egg_secure_strfree (self->password);
54
 
        self->password = NULL;
55
 
        self->n_password = 0;
56
 
    
57
 
        G_OBJECT_CLASS (gck_login_parent_class)->dispose (obj);
58
 
}
59
 
 
60
 
static void
61
 
gck_login_finalize (GObject *obj)
62
 
{
63
 
        GckLogin *self = GCK_LOGIN (obj);
64
 
        
65
 
        g_assert (!self->password);
66
 
        g_assert (!self->n_password);
67
 
 
68
 
        G_OBJECT_CLASS (gck_login_parent_class)->finalize (obj);
69
 
}
70
 
 
71
 
static void
72
 
gck_login_set_property (GObject *obj, guint prop_id, const GValue *value, 
73
 
                        GParamSpec *pspec)
74
 
{
75
 
        switch (prop_id) {
76
 
        default:
77
 
                G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
78
 
                break;
79
 
        }
80
 
}
81
 
 
82
 
static void
83
 
gck_login_get_property (GObject *obj, guint prop_id, GValue *value, 
84
 
                        GParamSpec *pspec)
85
 
{
86
 
        switch (prop_id) {
87
 
        default:
88
 
                G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
89
 
                break;
90
 
        }
91
 
}
92
 
 
93
 
static void
94
 
gck_login_class_init (GckLoginClass *klass)
95
 
{
96
 
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
97
 
    
98
 
        gobject_class->dispose = gck_login_dispose;
99
 
        gobject_class->finalize = gck_login_finalize;
100
 
        gobject_class->set_property = gck_login_set_property;
101
 
        gobject_class->get_property = gck_login_get_property;
102
 
}
103
 
 
104
 
/* -----------------------------------------------------------------------------
105
 
 * PUBLIC 
106
 
 */
107
 
 
108
 
GckLogin*
109
 
gck_login_new (CK_UTF8CHAR_PTR pin, CK_ULONG n_pin)
110
 
{
111
 
        GckLogin *login = g_object_new (GCK_TYPE_LOGIN, NULL);
112
 
        
113
 
        if (pin) {
114
 
                if (n_pin == (CK_ULONG)-1) {
115
 
                        login->password = egg_secure_strdup ((const gchar*)pin);
116
 
                        login->n_password = strlen (login->password);
117
 
                } else {
118
 
                        login->password = egg_secure_alloc (n_pin + 1);
119
 
                        memcpy (login->password, pin, n_pin);
120
 
                        login->n_password = n_pin;
121
 
                }
122
 
        } else {
123
 
                login->password = NULL;
124
 
                login->n_password = 0;
125
 
        }
126
 
        
127
 
        return login;
128
 
}
129
 
 
130
 
const gchar*
131
 
gck_login_get_password (GckLogin *self, gsize *n_password)
132
 
{
133
 
        g_return_val_if_fail (GCK_IS_LOGIN (self), NULL);
134
 
        g_return_val_if_fail (n_password, NULL);
135
 
        *n_password = self->n_password;
136
 
        return self->password;
137
 
}
138
 
 
139
 
gboolean
140
 
gck_login_equals (GckLogin *self, CK_UTF8CHAR_PTR pin, CK_ULONG n_pin)
141
 
{
142
 
        g_return_val_if_fail (GCK_IS_LOGIN (self), FALSE);
143
 
        
144
 
        if (n_pin == (CK_ULONG)-1 && pin != NULL)
145
 
                n_pin = strlen ((const gchar*)pin);
146
 
        
147
 
        if (n_pin != self->n_password)
148
 
                return FALSE;
149
 
        if (!pin && !self->password)
150
 
                return TRUE;
151
 
        if (!pin || !self->password)
152
 
                return FALSE;
153
 
        return memcmp (pin, self->password, n_pin) == 0;
154
 
}