Debian Codegraph @

pkcs11/gnome2-store/gkm-gnome2-public-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 "gkm-gnome2-public-key.h"
24
25 #include "gkm/gkm-attributes.h"
26 #include "gkm/gkm-data-der.h"
27 #include "gkm/gkm-factory.h"
28 #include "gkm/gkm-serializable.h"
29 #include "gkm/gkm-session.h"
30 #include "gkm/gkm-object.h"
31 #include "gkm/gkm-util.h"
32
33 #include <glib/gi18n.h>
34
35 struct _GkmGnome2PublicKey {
36 GkmPublicXsaKey parent;
37 };
38
39 static void gkm_gnome2_public_key_serializable (GkmSerializableIface *iface);
40
41 G_DEFINE_TYPE_EXTENDED (GkmGnome2PublicKey, gkm_gnome2_public_key, GKM_TYPE_PUBLIC_XSA_KEY, 0,
42 G_IMPLEMENT_INTERFACE (GKM_TYPE_SERIALIZABLE, gkm_gnome2_public_key_serializable));
43
44 /* -----------------------------------------------------------------------------
45 * INTERNAL
46 */
47
48 static GkmObject*
49 factory_create_public_key (GkmSession *session, GkmTransaction *transaction,
50 CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs)
51 {
52 GkmObject *object = NULL;
53 GkmSexp *sexp;
54
55 g_return_val_if_fail (attrs || !n_attrs, NULL);
56
57 sexp = gkm_public_xsa_key_create_sexp (session, transaction, attrs, n_attrs);
58 if (sexp != NULL) {
59 object = g_object_new (GKM_TYPE_GNOME2_PUBLIC_KEY, "base-sexp", sexp,
60 "module", gkm_session_get_module (session),
61 "manager", gkm_manager_for_template (attrs, n_attrs, session),
62 NULL);
63 gkm_sexp_unref (sexp);
64 gkm_session_complete_object_creation (session, transaction, object,
65 TRUE, attrs, n_attrs);
66 }
67
68 return object;
69 }
70
71 /* -----------------------------------------------------------------------------
72 * OBJECT
73 */
74
75 static void
76 gkm_gnome2_public_key_init (GkmGnome2PublicKey *self)
77 {
78
79 }
80
81 static void
82 gkm_gnome2_public_key_finalize (GObject *obj)
83 {
84 /* GkmGnome2PublicKey *self = GKM_GNOME2_PUBLIC_KEY (obj); */
85 G_OBJECT_CLASS (gkm_gnome2_public_key_parent_class)->finalize (obj);
86 }
87
88 static void
89 gkm_gnome2_public_key_set_property (GObject *obj, guint prop_id, const GValue *value,
90 GParamSpec *pspec)
91 {
92 switch (prop_id) {
93 default:
94 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
95 break;
96 }
97 }
98
99 static void
100 gkm_gnome2_public_key_get_property (GObject *obj, guint prop_id, GValue *value,
101 GParamSpec *pspec)
102 {
103 switch (prop_id) {
104 default:
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
106 break;
107 }
108 }
109
110 static void
111 gkm_gnome2_public_key_class_init (GkmGnome2PublicKeyClass *klass)
112 {
113 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114
115 gobject_class->finalize = gkm_gnome2_public_key_finalize;
116 gobject_class->set_property = gkm_gnome2_public_key_set_property;
117 gobject_class->get_property = gkm_gnome2_public_key_get_property;
118 }
119
120
121 static gboolean
122 gkm_gnome2_public_key_real_load (GkmSerializable *base,
123 GkmSecret *login,
124 GBytes *data)
125 {
126 GkmGnome2PublicKey *self = GKM_GNOME2_PUBLIC_KEY (base);
127 GkmDataResult res;
128 GkmSexp *wrapper;
129 gcry_sexp_t sexp;
130
131 if (g_bytes_get_size (data) == 0)
132 return FALSE;
133
134 res = gkm_data_der_read_public_key (data, &sexp);
135
136 switch (res) {
137 case GKM_DATA_LOCKED:
138 g_message ("public key is locked");
139 return FALSE;
140 case GKM_DATA_FAILURE:
141 g_message ("couldn't parse public key");
142 return FALSE;
143 case GKM_DATA_UNRECOGNIZED:
144 g_message ("invalid or unrecognized public key");
145 return FALSE;
146 case GKM_DATA_SUCCESS:
147 break;
148 default:
149 g_assert_not_reached();
150 }
151
152 wrapper = gkm_sexp_new (sexp);
153 gkm_sexp_key_set_base (GKM_SEXP_KEY (self), wrapper);
154 gkm_sexp_unref (wrapper);
155
156 return TRUE;
157 }
158
159 static GBytes *
160 gkm_gnome2_public_key_real_save (GkmSerializable *base, GkmSecret *login)
161 {
162 GkmGnome2PublicKey *self = GKM_GNOME2_PUBLIC_KEY (base);
163 GkmSexp *wrapper;
164
165 g_return_val_if_fail (GKM_IS_GNOME2_PUBLIC_KEY (self), FALSE);
166
167 wrapper = gkm_sexp_key_get_base (GKM_SEXP_KEY (self));
168 g_return_val_if_fail (wrapper, FALSE);
169
170 return gkm_data_der_write_public_key (gkm_sexp_get (wrapper));
171 }
172
173 static void
174 gkm_gnome2_public_key_serializable (GkmSerializableIface *iface)
175 {
176 iface->extension = ".pub";
177 iface->load = gkm_gnome2_public_key_real_load;
178 iface->save = gkm_gnome2_public_key_real_save;
179 }
180
181 /* -----------------------------------------------------------------------------
182 * PUBLIC
183 */
184
185 GkmFactory*
186 gkm_gnome2_public_key_get_factory (void)
187 {
188 static CK_OBJECT_CLASS klass = CKO_PUBLIC_KEY;
189 static CK_BBOOL token = CK_TRUE;
190
191 static CK_ATTRIBUTE attributes[] = {
192 { CKA_CLASS, &klass, sizeof (klass) },
193 { CKA_TOKEN, &token, sizeof (token) },
194 };
195
196 static GkmFactory factory = {
197 attributes,
198 G_N_ELEMENTS (attributes),
199 factory_create_public_key
200 };
201
202 return &factory;
203 }