Line data Source code
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 190 : 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 81 : gkm_secret_key_real_get_attribute (GkmObject *base, GkmSession *session, CK_ATTRIBUTE* attr)
50 : {
51 81 : GkmSecretKey *self = GKM_SECRET_KEY (base);
52 :
53 81 : switch (attr->type)
54 : {
55 54 : case CKA_CLASS:
56 54 : return gkm_attribute_set_ulong (attr, CKO_SECRET_KEY);
57 :
58 0 : 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 0 : return gkm_attribute_set_bool (attr, FALSE);
67 :
68 0 : case CKA_EXTRACTABLE:
69 0 : return gkm_attribute_set_bool (attr, TRUE);
70 :
71 0 : case CKA_ALWAYS_SENSITIVE:
72 0 : return gkm_attribute_set_bool (attr, FALSE);
73 :
74 0 : case CKA_NEVER_EXTRACTABLE:
75 0 : return gkm_attribute_set_bool (attr, FALSE);
76 :
77 0 : case CKA_WRAP_WITH_TRUSTED:
78 0 : return gkm_attribute_set_bool (attr, FALSE);
79 :
80 0 : case CKA_TRUSTED:
81 0 : return gkm_attribute_set_bool (attr, FALSE);
82 :
83 0 : case CKA_WRAP_TEMPLATE:
84 0 : gkm_debug ("CKR_ATTRIBUTE_TYPE_INVALID: no CKA_WRAP_TEMPLATE on key");
85 0 : return CKR_ATTRIBUTE_TYPE_INVALID;
86 :
87 0 : case CKA_UNWRAP_TEMPLATE:
88 0 : gkm_debug ("CKR_ATTRIBUTE_TYPE_INVALID: no CKA_UNWRAP_TEMPLATE on key");
89 0 : return CKR_ATTRIBUTE_TYPE_INVALID;
90 :
91 0 : case CKA_START_DATE:
92 : case CKA_END_DATE:
93 0 : return gkm_attribute_set_empty (attr);
94 :
95 0 : case CKA_LOCAL:
96 0 : return gkm_attribute_set_bool (attr, FALSE);
97 :
98 18 : case CKA_ID:
99 18 : return gkm_attribute_set_data (attr, self->pv->id, self->pv->n_id);
100 :
101 0 : case CKA_KEY_GEN_MECHANISM:
102 0 : return gkm_attribute_set_ulong (attr, CK_UNAVAILABLE_INFORMATION);
103 : };
104 :
105 9 : return GKM_OBJECT_CLASS (gkm_secret_key_parent_class)->get_attribute (base, session, attr);
106 : }
107 :
108 : static void
109 18 : gkm_secret_key_real_create_attributes (GkmObject *object, GkmSession *session, GkmTransaction *transaction,
110 : CK_ATTRIBUTE *attrs, CK_ULONG n_attrs)
111 : {
112 18 : GkmSecretKey *self = GKM_SECRET_KEY (object);
113 : CK_ATTRIBUTE_PTR id;
114 :
115 18 : if (!self->pv->n_id) {
116 18 : id = gkm_attributes_find (attrs, n_attrs, CKA_ID);
117 18 : if (id == NULL) {
118 18 : self->pv->id = NULL;
119 18 : self->pv->n_id = 0;
120 : } else {
121 0 : self->pv->id = g_memdup (id->pValue, id->ulValueLen);
122 0 : self->pv->n_id = id->ulValueLen;
123 0 : gkm_attribute_consume (id);
124 : }
125 : }
126 18 : }
127 :
128 : static void
129 18 : gkm_secret_key_init (GkmSecretKey *self)
130 : {
131 18 : self->pv = gkm_secret_key_get_instance_private (self);
132 18 : }
133 :
134 : static void
135 18 : gkm_secret_key_finalize (GObject *obj)
136 : {
137 18 : GkmSecretKey *self = GKM_SECRET_KEY (obj);
138 :
139 18 : g_free (self->pv->id);
140 18 : self->pv->id = NULL;
141 18 : self->pv->n_id = 0;
142 :
143 18 : G_OBJECT_CLASS (gkm_secret_key_parent_class)->finalize (obj);
144 18 : }
145 :
146 : static void
147 16 : gkm_secret_key_class_init (GkmSecretKeyClass *klass)
148 : {
149 16 : GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
150 16 : GkmObjectClass *gkm_class = GKM_OBJECT_CLASS (klass);
151 :
152 16 : gobject_class->finalize = gkm_secret_key_finalize;
153 :
154 16 : gkm_class->get_attribute = gkm_secret_key_real_get_attribute;
155 16 : gkm_class->create_attributes = gkm_secret_key_real_create_attributes;
156 16 : }
157 :
158 : /* -----------------------------------------------------------------------------
159 : * PUBLIC
160 : */
161 :
162 : gconstpointer
163 1 : gkm_secret_key_get_key_value (GkmSecretKey *self, gsize *n_value)
164 : {
165 1 : g_return_val_if_fail (GKM_IS_SECRET_KEY (self), NULL);
166 1 : g_return_val_if_fail (n_value, NULL);
167 :
168 : /* Check with the derived class */
169 1 : g_return_val_if_fail (GKM_SECRET_KEY_GET_CLASS (self)->get_key_value, NULL);
170 1 : return GKM_SECRET_KEY_GET_CLASS (self)->get_key_value (self, n_value);
171 : }
|