Debian Codegraph @

pkcs11/gkm/gkm-sexp.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-sexp.h"
24
25 struct _GkmSexp {
26 gint refs;
27 gcry_sexp_t real;
28 };
29
30 GkmSexp*
31 gkm_sexp_new (gcry_sexp_t real)
32 {
33 GkmSexp *sexp;
34 g_return_val_if_fail (real, NULL);
35 sexp = g_slice_new0 (GkmSexp);
36 sexp->refs = 1;
37 sexp->real = real;
38 return sexp;
39 }
40
41 GkmSexp*
42 gkm_sexp_ref (GkmSexp *sexp)
43 {
44 g_return_val_if_fail (sexp, NULL);
45 ++(sexp->refs);
46 return sexp;
47 }
48
49 void
50 gkm_sexp_unref (gpointer data)
51 {
52 GkmSexp *sexp = data;
53 g_return_if_fail (sexp);
54 if (--(sexp->refs) == 0) {
55 g_assert (sexp->real);
56 gcry_sexp_release (sexp->real);
57 g_slice_free (GkmSexp, sexp);
58 }
59 }
60
61 gcry_sexp_t
62 gkm_sexp_get (GkmSexp *sexp)
63 {
64 g_return_val_if_fail (sexp, NULL);
65 g_return_val_if_fail (sexp->real, NULL);
66 return sexp->real;
67 }
68
69 GType
70 gkm_sexp_boxed_type (void)
71 {
72 static GType type = 0;
73 if (!type)
74 type = g_boxed_type_register_static ("GkmSexp",
75 (GBoxedCopyFunc)gkm_sexp_ref,
76 (GBoxedFreeFunc)gkm_sexp_unref);
77 return type;
78 }
79
80 #define PUBLIC_KEY "public-key"
81 #define PUBLIC_KEY_L 10
82 #define PRIVATE_KEY "private-key"
83 #define PRIVATE_KEY_L 11
84
85 gboolean
86 gkm_sexp_parse_key (gcry_sexp_t s_key, int *algorithm, gboolean *is_private,
87 gcry_sexp_t *numbers)
88 {
89 gboolean ret = FALSE;
90 gcry_sexp_t child = NULL;
91 gchar *str = NULL;
92 const gchar *data;
93 gsize n_data;
94 gboolean priv;
95 int algo;
96
97 data = gcry_sexp_nth_data (s_key, 0, &n_data);
98 if (!data)
99 goto done;
100
101 if (n_data == PUBLIC_KEY_L && strncmp (data, PUBLIC_KEY, PUBLIC_KEY_L) == 0)
102 priv = FALSE;
103 else if (n_data == PRIVATE_KEY_L && strncmp (data, PRIVATE_KEY, PRIVATE_KEY_L) == 0)
104 priv = TRUE;
105 else
106 goto done;
107
108 child = gcry_sexp_nth (s_key, 1);
109 if (!child)
110 goto done;
111
112 data = gcry_sexp_nth_data (child, 0, &n_data);
113 if (!data)
114 goto done;
115
116 str = g_alloca (n_data + 1);
117 memcpy (str, data, n_data);
118 str[n_data] = 0;
119
120 algo = gcry_pk_map_name (str);
121 if (!algo)
122 goto done;
123
124 /* Yay all done */
125 if (algorithm)
126 *algorithm = algo;
127 if (numbers) {
128 *numbers = child;
129 child = NULL;
130 }
131 if (is_private)
132 *is_private = priv;
133
134 ret = TRUE;
135
136 done:
137 gcry_sexp_release (child);
138 return ret;
139 }
140
141 static gcry_sexp_t
142 rsa_numbers_to_public (gcry_sexp_t rsa)
143 {
144 gcry_sexp_t pubkey = NULL;
145 gcry_mpi_t n, e;
146 gcry_error_t gcry;
147
148 n = e = NULL;
149
150 if (!gkm_sexp_extract_mpi (rsa, &n, "n", NULL) ||
151 !gkm_sexp_extract_mpi (rsa, &e, "e", NULL))
152 goto done;
153
154 gcry = gcry_sexp_build (&pubkey, NULL, "(public-key (rsa (n %m) (e %m)))",
155 n, e);
156 if (gcry)
157 goto done;
158 g_assert (pubkey);
159
160 done:
161 gcry_mpi_release (n);
162 gcry_mpi_release (e);
163
164 return pubkey;
165 }
166
167 static gcry_sexp_t
168 dsa_numbers_to_public (gcry_sexp_t dsa)
169 {
170 gcry_mpi_t p, q, g, y;
171 gcry_sexp_t pubkey = NULL;
172 gcry_error_t gcry;
173
174 p = q = g = y = NULL;
175
176 if (!gkm_sexp_extract_mpi (dsa, &p, "p", NULL) ||
177 !gkm_sexp_extract_mpi (dsa, &q, "q", NULL) ||
178 !gkm_sexp_extract_mpi (dsa, &g, "g", NULL) ||
179 !gkm_sexp_extract_mpi (dsa, &y, "y", NULL))
180 goto done;
181
182 gcry = gcry_sexp_build (&pubkey, NULL, "(public-key (dsa (p %m) (q %m) (g %m) (y %m)))",
183 p, q, g, y);
184 if (gcry)
185 goto done;
186 g_assert (pubkey);
187
188 done:
189 gcry_mpi_release (p);
190 gcry_mpi_release (q);
191 gcry_mpi_release (g);
192 gcry_mpi_release (y);
193
194 return pubkey;
195 }
196
197 static gcry_sexp_t
198 ecdsa_numbers_to_public (gcry_sexp_t ecdsa)
199 {
200 gchar *curve_name = NULL, *q = NULL;
201 gsize q_len;
202 gcry_sexp_t pubkey = NULL;
203 gcry_error_t gcry;
204
205 if (!gkm_sexp_extract_string (ecdsa, &curve_name, "curve", NULL) ||
206 !gkm_sexp_extract_buffer (ecdsa, &q, &q_len, "q", NULL))
207 goto done;
208
209 gcry = gcry_sexp_build (&pubkey, NULL, "(public-key (ecdsa (curve %s) (q %b)))",
210 curve_name, q_len, q);
211 if (gcry)
212 goto done;
213 g_assert (pubkey);
214
215 done:
216 g_free (curve_name);
217 g_free (q);
218
219 return pubkey;
220 }
221
222 gboolean
223 gkm_sexp_key_to_public (gcry_sexp_t privkey, gcry_sexp_t *pubkey)
224 {
225 gcry_sexp_t numbers;
226 int algorithm;
227
228 if (!gkm_sexp_parse_key (privkey, &algorithm, NULL, &numbers))
229 g_return_val_if_reached (FALSE);
230
231 switch (algorithm) {
232 case GCRY_PK_RSA:
233 *pubkey = rsa_numbers_to_public (numbers);
234 break;
235 case GCRY_PK_DSA:
236 *pubkey = dsa_numbers_to_public (numbers);
237 break;
238 case GCRY_PK_ECC:
239 *pubkey = ecdsa_numbers_to_public (numbers);
240 break;
241 default:
242 g_return_val_if_reached (FALSE);
243 }
244
245 gcry_sexp_release (numbers);
246 return *pubkey ? TRUE : FALSE;
247 }
248
249 gboolean
250 gkm_sexp_extract_mpi (gcry_sexp_t sexp, gcry_mpi_t *mpi, ...)
251 {
252 gcry_sexp_t at = NULL;
253 va_list va;
254
255 g_assert (sexp);
256 g_assert (mpi);
257
258 va_start (va, mpi);
259 at = gkm_sexp_get_childv (sexp, va);
260 va_end (va);
261
262 *mpi = NULL;
263 if (at)
264 *mpi = gcry_sexp_nth_mpi (at ? at : sexp, 1, GCRYMPI_FMT_USG);
265 if (at)
266 gcry_sexp_release (at);
267
268 return (*mpi) ? TRUE : FALSE;
269 }
270
271 /* ECDSA s-exp lists the curve name as a string */
272 gboolean
273 gkm_sexp_extract_string (gcry_sexp_t sexp, gchar **buf, ...)
274 {
275 gcry_sexp_t at = NULL;
276 va_list va;
277
278 g_assert (sexp);
279 g_assert (buf);
280
281 va_start (va, buf);
282 at = gkm_sexp_get_childv (sexp, va);
283 va_end (va);
284
285 *buf = NULL;
286 if (at) {
287 size_t len;
288 const char *data;
289
290 data = gcry_sexp_nth_data (at, 1, &len);
291 *buf = g_strndup (data, len);
292 gcry_sexp_release (at);
293 }
294
295 return (*buf) ? TRUE : FALSE;
296 }
297
298 gboolean
299 gkm_sexp_extract_buffer (gcry_sexp_t sexp, gchar **buf, gsize *bufsize, ...)
300 {
301 gcry_sexp_t at = NULL;
302 va_list va;
303
304 g_assert (sexp);
305 g_assert (buf);
306
307 va_start (va, bufsize);
308 at = gkm_sexp_get_childv (sexp, va);
309 va_end (va);
310
311 *buf = NULL;
312 if (at) {
313 size_t len;
314 const char *data;
315
316 data = gcry_sexp_nth_data (at, 1, &len);
317 *buf = g_memdup2 (data, len);
318 *bufsize = len;
319 gcry_sexp_release (at);
320 }
321
322 return (*buf) ? TRUE : FALSE;
323 }
324
325 gcry_sexp_t
326 gkm_sexp_get_childv (gcry_sexp_t sexp, va_list va)
327 {
328 gcry_sexp_t at = NULL;
329 gcry_sexp_t child;
330 const char *name;
331
332 g_assert (sexp);
333
334 for(;;) {
335 name = va_arg (va, const char*);
336 if (!name)
337 break;
338
339 child = gcry_sexp_find_token (at ? at : sexp, name, 0);
340 gcry_sexp_release (at);
341 at = child;
342 if (at == NULL)
343 break;
344 }
345
346 va_end (va);
347
348 return at;
349 }
350
351 void
352 gkm_sexp_dump (gcry_sexp_t sexp)
353 {
354 gsize len;
355 gchar *buf;
356
357 len = gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, NULL, 0);
358 buf = g_malloc (len);
359 gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, buf, len);
360 g_printerr ("%s", buf);
361 g_free (buf);
362 }