| 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, see |
| 18 |
* <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "gkm-secret.h" |
| 24 |
|
| 25 |
#include "egg/egg-secure-memory.h" |
| 26 |
|
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
struct _GkmSecret { |
| 30 |
GObject parent; |
| 31 |
guchar *memory; |
| 32 |
gsize n_memory; |
| 33 |
}; |
| 34 |
|
| 35 |
G_DEFINE_TYPE (GkmSecret, gkm_secret, G_TYPE_OBJECT); |
| 36 |
|
| 37 |
EGG_SECURE_DECLARE (secret); |
| 38 |
|
| 39 |
/* ----------------------------------------------------------------------------- |
| 40 |
* OBJECT |
| 41 |
*/ |
| 42 |
|
| 43 |
static void |
| 44 |
gkm_secret_init (GkmSecret *self) |
| 45 |
{ |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
static void |
| 50 |
gkm_secret_dispose (GObject *obj) |
| 51 |
{ |
| 52 |
GkmSecret *self = GKM_SECRET (obj); |
| 53 |
|
| 54 |
egg_secure_clear (self->memory, self->n_memory); |
| 55 |
|
| 56 |
G_OBJECT_CLASS (gkm_secret_parent_class)->dispose (obj); |
| 57 |
} |
| 58 |
|
| 59 |
static void |
| 60 |
gkm_secret_finalize (GObject *obj) |
| 61 |
{ |
| 62 |
GkmSecret *self = GKM_SECRET (obj); |
| 63 |
|
| 64 |
egg_secure_free (self->memory); |
| 65 |
self->memory = NULL; |
| 66 |
self->n_memory = 0; |
| 67 |
|
| 68 |
G_OBJECT_CLASS (gkm_secret_parent_class)->finalize (obj); |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
gkm_secret_class_init (GkmSecretClass *klass) |
| 73 |
{ |
| 74 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 75 |
gobject_class->dispose = gkm_secret_dispose; |
| 76 |
gobject_class->finalize = gkm_secret_finalize; |
| 77 |
} |
| 78 |
|
| 79 |
/* ----------------------------------------------------------------------------- |
| 80 |
* PUBLIC |
| 81 |
*/ |
| 82 |
|
| 83 |
GkmSecret* |
| 84 |
gkm_secret_new (const guchar *data, gssize n_data) |
| 85 |
{ |
| 86 |
GkmSecret *secret = g_object_new (GKM_TYPE_SECRET, NULL); |
| 87 |
|
| 88 |
if (data) { |
| 89 |
if (n_data == -1) { |
| 90 |
secret->memory = (guchar*)egg_secure_strdup ((const gchar*)data); |
| 91 |
secret->n_memory = strlen ((const gchar*)data); |
| 92 |
} else { |
| 93 |
secret->memory = egg_secure_alloc (n_data + 1); |
| 94 |
memcpy (secret->memory, data, n_data); |
| 95 |
secret->n_memory = n_data; |
| 96 |
} |
| 97 |
} else { |
| 98 |
secret->memory = NULL; |
| 99 |
secret->n_memory = 0; |
| 100 |
} |
| 101 |
|
| 102 |
return secret; |
| 103 |
} |
| 104 |
|
| 105 |
GkmSecret* |
| 106 |
gkm_secret_new_from_login (CK_UTF8CHAR_PTR pin, CK_ULONG n_pin) |
| 107 |
{ |
| 108 |
if (n_pin == (CK_ULONG)-1) |
| 109 |
return gkm_secret_new ((const guchar*)pin, -1); |
| 110 |
else |
| 111 |
return gkm_secret_new ((const guchar*)pin, (gssize)n_pin); |
| 112 |
} |
| 113 |
|
| 114 |
GkmSecret* |
| 115 |
gkm_secret_new_from_password (const gchar *password) |
| 116 |
{ |
| 117 |
return gkm_secret_new ((const guchar*)password, -1); |
| 118 |
} |
| 119 |
|
| 120 |
const guchar* |
| 121 |
gkm_secret_get (GkmSecret *self, gsize *n_data) |
| 122 |
{ |
| 123 |
g_return_val_if_fail (GKM_IS_SECRET (self), NULL); |
| 124 |
g_return_val_if_fail (n_data, NULL); |
| 125 |
*n_data = self->n_memory; |
| 126 |
return self->memory; |
| 127 |
} |
| 128 |
|
| 129 |
const gchar* |
| 130 |
gkm_secret_get_password (GkmSecret *self, gsize *n_data) |
| 131 |
{ |
| 132 |
g_return_val_if_fail (GKM_IS_SECRET (self), NULL); |
| 133 |
g_return_val_if_fail (n_data, NULL); |
| 134 |
*n_data = self->n_memory; |
| 135 |
return (gchar*)self->memory; |
| 136 |
} |
| 137 |
|
| 138 |
gboolean |
| 139 |
gkm_secret_equal (GkmSecret *self, GkmSecret *other) |
| 140 |
{ |
| 141 |
g_return_val_if_fail (GKM_IS_SECRET (self), FALSE); |
| 142 |
g_return_val_if_fail (GKM_IS_SECRET (other), FALSE); |
| 143 |
if (self == other) |
| 144 |
return TRUE; |
| 145 |
return gkm_secret_equals (self, other->memory, other->n_memory); |
| 146 |
} |
| 147 |
|
| 148 |
gboolean |
| 149 |
gkm_secret_equals (GkmSecret *self, const guchar* pin, gssize n_pin) |
| 150 |
{ |
| 151 |
g_return_val_if_fail (GKM_IS_SECRET (self), FALSE); |
| 152 |
|
| 153 |
/* In case they're different somewhere */ |
| 154 |
if (n_pin == (CK_ULONG)-1) |
| 155 |
n_pin = -1; |
| 156 |
|
| 157 |
if (n_pin == -1 && pin != NULL) |
| 158 |
n_pin = strlen ((const gchar*)pin); |
| 159 |
|
| 160 |
/* The same length */ |
| 161 |
if (n_pin != self->n_memory) |
| 162 |
return FALSE; |
| 163 |
|
| 164 |
/* Two null passwords */ |
| 165 |
if (!pin && !self->memory) |
| 166 |
return TRUE; |
| 167 |
|
| 168 |
/* For our purposes a null password equals an empty password */ |
| 169 |
if (n_pin == 0) |
| 170 |
return TRUE; |
| 171 |
|
| 172 |
/* One null, one not null */ |
| 173 |
if (!pin || !self->memory) |
| 174 |
return FALSE; |
| 175 |
|
| 176 |
/* Compare actual memory */ |
| 177 |
return memcmp (pin, self->memory, n_pin) == 0; |
| 178 |
} |
| 179 |
|
| 180 |
gboolean |
| 181 |
gkm_secret_is_trivially_weak (GkmSecret *self) |
| 182 |
{ |
| 183 |
return gkm_secret_equals (self, NULL, 0) || |
| 184 |
gkm_secret_equals (self, (const guchar*)"", 0); |
| 185 |
} |