Debian Codegraph @

pkcs11/gkm/gkm-aes-key.c

raw
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 "pkcs11/pkcs11.h"
24 #include "pkcs11/pkcs11i.h"
25
26 #include "gkm-aes-mechanism.h"
27 #include "gkm-attributes.h"
28 #include "gkm-crypto.h"
29 #include "gkm-aes-key.h"
30 #include "gkm-session.h"
31 #include "gkm-transaction.h"
32 #include "gkm-util.h"
33
34 #include "egg/egg-secure-memory.h"
35
36 struct _GkmAesKey {
37 GkmSecretKey parent;
38 gpointer value;
39 gsize n_value;
40 };
41
42 G_DEFINE_TYPE (GkmAesKey, gkm_aes_key, GKM_TYPE_SECRET_KEY);
43
44 static const CK_MECHANISM_TYPE GKM_AES_MECHANISMS[] = {
45 CKM_AES_CBC_PAD,
46 CKM_G_HKDF_SHA256_DERIVE
47 };
48
49 EGG_SECURE_DECLARE (aes_key);
50
51 /* -----------------------------------------------------------------------------
52 * INTERNAL
53 */
54
55 static int
56 algorithm_for_length (gsize length)
57 {
58 switch (length) {
59 case 16:
60 return GCRY_CIPHER_AES128;
61 case 24:
62 return GCRY_CIPHER_AES192;
63 case 32:
64 return GCRY_CIPHER_AES256;
65 default:
66 return 0;
67 }
68 }
69
70 static CK_RV
71 attribute_set_check_value (GkmAesKey *self, CK_ATTRIBUTE *attr)
72 {
73 gcry_cipher_hd_t cih;
74 gcry_error_t gcry;
75 gpointer data;
76 CK_RV rv;
77
78 g_assert (GKM_IS_AES_KEY (self));
79 g_assert (attr);
80
81 /* Just asking for the length */
82 if (!attr->pValue) {
83 attr->ulValueLen = 3;
84 return CKR_OK;
85 }
86
87 cih = gkm_aes_key_get_cipher (self, GCRY_CIPHER_MODE_ECB);
88 if (cih == NULL)
89 return CKR_FUNCTION_FAILED;
90
91 /* Buffer of zeros */
92 data = g_malloc0 (self->n_value);
93
94 /* Encrypt it */
95 gcry = gcry_cipher_encrypt (cih, data, self->n_value, NULL, 0);
96 g_return_val_if_fail (gcry == 0, CKR_GENERAL_ERROR);
97
98 /* Use the first three bytes */
99 g_assert (self->n_value > 3);
100 rv = gkm_attribute_set_data (attr, data, 3);
101
102 gcry_cipher_close (cih);
103 g_free (data);
104
105 return rv;
106 }
107
108 static GkmObject*
109 factory_create_aes_key (GkmSession *session, GkmTransaction *transaction,
110 CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs)
111 {
112 GkmAesKey *key;
113 GkmManager *manager;
114 CK_ATTRIBUTE_PTR value;
115
116 value = gkm_attributes_find (attrs, n_attrs, CKA_VALUE);
117 if (value == NULL) {
118 gkm_transaction_fail (transaction, CKR_TEMPLATE_INCOMPLETE);
119 return NULL;
120 }
121
122 if (algorithm_for_length (value->ulValueLen) == 0) {
123 gkm_transaction_fail (transaction, CKR_TEMPLATE_INCONSISTENT);
124 return NULL;
125 }
126
127 manager = gkm_manager_for_template (attrs, n_attrs, session);
128 key = g_object_new (GKM_TYPE_AES_KEY,
129 "module", gkm_session_get_module (session),
130 "manager", manager,
131 NULL);
132
133 key->value = egg_secure_alloc (value->ulValueLen);
134 key->n_value = value->ulValueLen;
135 memcpy (key->value, value->pValue, key->n_value);
136
137 gkm_attribute_consume (value);
138
139 gkm_session_complete_object_creation (session, transaction, GKM_OBJECT (key),
140 TRUE, attrs, n_attrs);
141 return GKM_OBJECT (key);
142 }
143
144 /* -----------------------------------------------------------------------------
145 * OBJECT
146 */
147
148 static CK_RV
149 gkm_aes_key_get_attribute (GkmObject *base, GkmSession *session, CK_ATTRIBUTE *attr)
150 {
151 GkmAesKey *self = GKM_AES_KEY (base);
152
153 switch (attr->type)
154 {
155 case CKA_KEY_TYPE:
156 return gkm_attribute_set_ulong (attr, CKK_AES);
157
158 case CKA_DERIVE:
159 return gkm_attribute_set_bool (attr, CK_TRUE);
160
161 case CKA_UNWRAP:
162 case CKA_WRAP:
163 return gkm_attribute_set_bool (attr, CK_TRUE);
164
165 case CKA_VALUE:
166 return gkm_attribute_set_data (attr, self->value, self->n_value);
167
168 case CKA_VALUE_LEN:
169 return gkm_attribute_set_ulong (attr, self->n_value);
170
171 case CKA_CHECK_VALUE:
172 return attribute_set_check_value (self, attr);
173
174 case CKA_ALLOWED_MECHANISMS:
175 return gkm_attribute_set_data (attr, (CK_VOID_PTR)GKM_AES_MECHANISMS,
176 sizeof (GKM_AES_MECHANISMS));
177 };
178
179 return GKM_OBJECT_CLASS (gkm_aes_key_parent_class)->get_attribute (base, session, attr);
180 }
181
182 static gconstpointer
183 gkm_aes_key_get_key_value (GkmSecretKey *key, gsize *n_value)
184 {
185 GkmAesKey *self = GKM_AES_KEY (key);
186 *n_value = self->n_value;
187 return self->value;
188 }
189
190 static void
191 gkm_aes_key_init (GkmAesKey *self)
192 {
193
194 }
195
196 static void
197 gkm_aes_key_finalize (GObject *obj)
198 {
199 GkmAesKey *self = GKM_AES_KEY (obj);
200
201 if (self->value) {
202 egg_secure_clear (self->value, self->n_value);
203 egg_secure_free (self->value);
204 self->value = NULL;
205 self->n_value = 0;
206 }
207
208 G_OBJECT_CLASS (gkm_aes_key_parent_class)->finalize (obj);
209 }
210
211 static void
212 gkm_aes_key_class_init (GkmAesKeyClass *klass)
213 {
214 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
215 GkmObjectClass *gkm_class = GKM_OBJECT_CLASS (klass);
216 GkmSecretKeyClass *key_class = GKM_SECRET_KEY_CLASS (klass);
217
218 gkm_aes_key_parent_class = g_type_class_peek_parent (klass);
219
220 gobject_class->finalize = gkm_aes_key_finalize;
221
222 gkm_class->get_attribute = gkm_aes_key_get_attribute;
223
224 key_class->get_key_value = gkm_aes_key_get_key_value;
225 }
226
227 /* -----------------------------------------------------------------------------
228 * PUBLIC
229 */
230
231 GkmFactory*
232 gkm_aes_key_get_factory (void)
233 {
234 static CK_OBJECT_CLASS klass = CKO_SECRET_KEY;
235 static CK_KEY_TYPE type = CKK_AES;
236
237 static CK_ATTRIBUTE attributes[] = {
238 { CKA_CLASS, &klass, sizeof (klass) },
239 { CKA_KEY_TYPE, &type, sizeof (type) }
240 };
241
242 static GkmFactory factory = {
243 attributes,
244 G_N_ELEMENTS (attributes),
245 factory_create_aes_key
246 };
247
248 return &factory;
249 }
250
251 gsize
252 gkm_aes_key_get_block_size (GkmAesKey *self)
253 {
254 int algorithm;
255
256 g_return_val_if_fail (GKM_IS_AES_KEY (self), 0);
257
258 algorithm = algorithm_for_length (self->n_value);
259 g_return_val_if_fail (algorithm != 0, 0);
260
261 return self->n_value;
262 }
263
264 gcry_cipher_hd_t
265 gkm_aes_key_get_cipher (GkmAesKey *self, int mode)
266 {
267 gcry_cipher_hd_t cih;
268 gcry_error_t gcry;
269 int algorithm;
270
271 g_return_val_if_fail (GKM_IS_AES_KEY (self), NULL);
272
273 algorithm = algorithm_for_length (self->n_value);
274 g_return_val_if_fail (algorithm != 0, NULL);
275
276 gcry = gcry_cipher_open (&cih, algorithm, mode, 0);
277 if (gcry != 0) {
278 g_warning ("couldn't open %s cipher: %s",
279 gcry_cipher_algo_name (algorithm), gcry_strerror (gcry));
280 return NULL;
281 }
282
283 /* Setup the key */
284 gcry = gcry_cipher_setkey (cih, self->value, self->n_value);
285 g_return_val_if_fail (gcry == 0, NULL);
286
287 return cih;
288 }