Debian Codegraph @

pkcs11/gkm/gkm-secret-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
25 #include "gkm-attributes.h"
26 #include "gkm-crypto.h"
27 #define DEBUG_FLAG GKM_DEBUG_OBJECT
28 #include "gkm-debug.h"
29 #include "gkm-secret-key.h"
30 #include "gkm-session.h"
31 #include "gkm-util.h"
32
33 struct _GkmSecretKeyPrivate {
34 gpointer id;
35 gsize n_id;
36 };
37
38 G_DEFINE_TYPE_WITH_PRIVATE (GkmSecretKey, gkm_secret_key, GKM_TYPE_OBJECT);
39
40 /* -----------------------------------------------------------------------------
41 * INTERNAL
42 */
43
44 /* -----------------------------------------------------------------------------
45 * PUBLIC_SECRET_KEY
46 */
47
48 static CK_RV
49 gkm_secret_key_real_get_attribute (GkmObject *base, GkmSession *session, CK_ATTRIBUTE* attr)
50 {
51 GkmSecretKey *self = GKM_SECRET_KEY (base);
52
53 switch (attr->type)
54 {
55 case CKA_CLASS:
56 return gkm_attribute_set_ulong (attr, CKO_SECRET_KEY);
57
58 case CKA_SENSITIVE:
59 case CKA_ENCRYPT:
60 case CKA_DECRYPT:
61 case CKA_SIGN:
62 case CKA_VERIFY:
63 case CKA_WRAP:
64 case CKA_UNWRAP:
65 case CKA_DERIVE:
66 return gkm_attribute_set_bool (attr, FALSE);
67
68 case CKA_EXTRACTABLE:
69 return gkm_attribute_set_bool (attr, TRUE);
70
71 case CKA_ALWAYS_SENSITIVE:
72 return gkm_attribute_set_bool (attr, FALSE);
73
74 case CKA_NEVER_EXTRACTABLE:
75 return gkm_attribute_set_bool (attr, FALSE);
76
77 case CKA_WRAP_WITH_TRUSTED:
78 return gkm_attribute_set_bool (attr, FALSE);
79
80 case CKA_TRUSTED:
81 return gkm_attribute_set_bool (attr, FALSE);
82
83 case CKA_WRAP_TEMPLATE:
84 gkm_debug ("CKR_ATTRIBUTE_TYPE_INVALID: no CKA_WRAP_TEMPLATE on key");
85 return CKR_ATTRIBUTE_TYPE_INVALID;
86
87 case CKA_UNWRAP_TEMPLATE:
88 gkm_debug ("CKR_ATTRIBUTE_TYPE_INVALID: no CKA_UNWRAP_TEMPLATE on key");
89 return CKR_ATTRIBUTE_TYPE_INVALID;
90
91 case CKA_START_DATE:
92 case CKA_END_DATE:
93 return gkm_attribute_set_empty (attr);
94
95 case CKA_LOCAL:
96 return gkm_attribute_set_bool (attr, FALSE);
97
98 case CKA_ID:
99 return gkm_attribute_set_data (attr, self->pv->id, self->pv->n_id);
100
101 case CKA_KEY_GEN_MECHANISM:
102 return gkm_attribute_set_ulong (attr, CK_UNAVAILABLE_INFORMATION);
103 };
104
105 return GKM_OBJECT_CLASS (gkm_secret_key_parent_class)->get_attribute (base, session, attr);
106 }
107
108 static void
109 gkm_secret_key_real_create_attributes (GkmObject *object, GkmSession *session, GkmTransaction *transaction,
110 CK_ATTRIBUTE *attrs, CK_ULONG n_attrs)
111 {
112 GkmSecretKey *self = GKM_SECRET_KEY (object);
113 CK_ATTRIBUTE_PTR id;
114
115 if (!self->pv->n_id) {
116 id = gkm_attributes_find (attrs, n_attrs, CKA_ID);
117 if (id == NULL) {
118 self->pv->id = NULL;
119 self->pv->n_id = 0;
120 } else {
121 self->pv->id = g_memdup2 (id->pValue, id->ulValueLen);
122 self->pv->n_id = id->ulValueLen;
123 gkm_attribute_consume (id);
124 }
125 }
126 }
127
128 static void
129 gkm_secret_key_init (GkmSecretKey *self)
130 {
131 self->pv = gkm_secret_key_get_instance_private (self);
132 }
133
134 static void
135 gkm_secret_key_finalize (GObject *obj)
136 {
137 GkmSecretKey *self = GKM_SECRET_KEY (obj);
138
139 g_free (self->pv->id);
140 self->pv->id = NULL;
141 self->pv->n_id = 0;
142
143 G_OBJECT_CLASS (gkm_secret_key_parent_class)->finalize (obj);
144 }
145
146 static void
147 gkm_secret_key_class_init (GkmSecretKeyClass *klass)
148 {
149 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
150 GkmObjectClass *gkm_class = GKM_OBJECT_CLASS (klass);
151
152 gobject_class->finalize = gkm_secret_key_finalize;
153
154 gkm_class->get_attribute = gkm_secret_key_real_get_attribute;
155 gkm_class->create_attributes = gkm_secret_key_real_create_attributes;
156 }
157
158 /* -----------------------------------------------------------------------------
159 * PUBLIC
160 */
161
162 gconstpointer
163 gkm_secret_key_get_key_value (GkmSecretKey *self, gsize *n_value)
164 {
165 g_return_val_if_fail (GKM_IS_SECRET_KEY (self), NULL);
166 g_return_val_if_fail (n_value, NULL);
167
168 /* Check with the derived class */
169 g_return_val_if_fail (GKM_SECRET_KEY_GET_CLASS (self)->get_key_value, NULL);
170 return GKM_SECRET_KEY_GET_CLASS (self)->get_key_value (self, n_value);
171 }