77
77
(GBoxedFreeFunc)gck_sexp_unref);
81
#define PUBLIC_KEY "public-key"
82
#define PUBLIC_KEY_L 10
83
#define PRIVATE_KEY "private-key"
84
#define PRIVATE_KEY_L 11
87
gck_sexp_parse_key (gcry_sexp_t s_key, int *algorithm, gboolean *is_private,
91
gcry_sexp_t child = NULL;
98
data = gcry_sexp_nth_data (s_key, 0, &n_data);
102
if (n_data == PUBLIC_KEY_L && strncmp (data, PUBLIC_KEY, PUBLIC_KEY_L) == 0)
104
else if (n_data == PRIVATE_KEY_L && strncmp (data, PRIVATE_KEY, PRIVATE_KEY_L) == 0)
109
child = gcry_sexp_nth (s_key, 1);
113
data = gcry_sexp_nth_data (child, 0, &n_data);
117
str = g_alloca (n_data + 1);
118
memcpy (str, data, n_data);
121
algo = gcry_pk_map_name (str);
138
gcry_sexp_release (child);
143
rsa_numbers_to_public (gcry_sexp_t rsa)
145
gcry_sexp_t pubkey = NULL;
151
if (!gck_sexp_extract_mpi (rsa, &n, "n", NULL) ||
152
!gck_sexp_extract_mpi (rsa, &e, "e", NULL))
155
gcry = gcry_sexp_build (&pubkey, NULL, "(public-key (rsa (n %m) (e %m)))",
162
gcry_mpi_release (n);
163
gcry_mpi_release (e);
169
dsa_numbers_to_public (gcry_sexp_t dsa)
171
gcry_mpi_t p, q, g, y;
172
gcry_sexp_t pubkey = NULL;
175
p = q = g = y = NULL;
177
if (!gck_sexp_extract_mpi (dsa, &p, "p", NULL) ||
178
!gck_sexp_extract_mpi (dsa, &q, "q", NULL) ||
179
!gck_sexp_extract_mpi (dsa, &g, "g", NULL) ||
180
!gck_sexp_extract_mpi (dsa, &y, "y", NULL))
183
gcry = gcry_sexp_build (&pubkey, NULL, "(public-key (dsa (p %m) (q %m) (g %m) (y %m)))",
190
gcry_mpi_release (p);
191
gcry_mpi_release (q);
192
gcry_mpi_release (g);
193
gcry_mpi_release (y);
199
gck_sexp_key_to_public (gcry_sexp_t privkey, gcry_sexp_t *pubkey)
204
if (!gck_sexp_parse_key (privkey, &algorithm, NULL, &numbers))
205
g_return_val_if_reached (FALSE);
209
*pubkey = rsa_numbers_to_public (numbers);
212
*pubkey = dsa_numbers_to_public (numbers);
215
g_return_val_if_reached (FALSE);
218
gcry_sexp_release (numbers);
219
return *pubkey ? TRUE : FALSE;
223
gck_sexp_extract_mpi (gcry_sexp_t sexp, gcry_mpi_t *mpi, ...)
225
gcry_sexp_t at = NULL;
232
at = gck_sexp_get_childv (sexp, va);
237
*mpi = gcry_sexp_nth_mpi (at ? at : sexp, 1, GCRYMPI_FMT_USG);
239
gcry_sexp_release (at);
241
return (*mpi) ? TRUE : FALSE;
245
gck_sexp_get_childv (gcry_sexp_t sexp, va_list va)
247
gcry_sexp_t at = NULL;
255
name = va_arg (va, const char*);
259
child = gcry_sexp_find_token (at ? at : sexp, name, 0);
260
gcry_sexp_release (at);
272
gck_sexp_dump (gcry_sexp_t sexp)
277
len = gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, NULL, 0);
278
buf = g_malloc (len);
279
gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, buf, len);
280
g_printerr ("%s", buf);