diff options
Diffstat (limited to 'src/gpg.c')
-rw-r--r-- | src/gpg.c | 56 |
1 files changed, 31 insertions, 25 deletions
@@ -2,7 +2,7 @@ | |||
2 | gpg.c | 2 | gpg.c |
3 | 3 | ||
4 | This file is part of GNU Anubis. | 4 | This file is part of GNU Anubis. |
5 | Copyright (C) 2001-2014 The Anubis Team. | 5 | Copyright (C) 2001-2024 The Anubis Team. |
6 | 6 | ||
7 | GNU Anubis is free software; you can redistribute it and/or modify it | 7 | GNU Anubis is free software; you can redistribute it and/or modify it |
8 | under the terms of the GNU General Public License as published by the | 8 | under the terms of the GNU General Public License as published by the |
@@ -21,12 +21,7 @@ | |||
21 | #include "headers.h" | 21 | #include "headers.h" |
22 | #include "extern.h" | 22 | #include "extern.h" |
23 | #include "rcfile.h" | 23 | #include "rcfile.h" |
24 | |||
25 | #ifdef HAVE_GPG | ||
26 | #include <gpgme.h> | 24 | #include <gpgme.h> |
27 | #define obstack_chunk_alloc malloc | ||
28 | #define obstack_chunk_free free | ||
29 | #include <obstack.h> | ||
30 | 25 | ||
31 | struct gpg_struct | 26 | struct gpg_struct |
32 | { | 27 | { |
@@ -217,12 +212,15 @@ gpg_sign (char *gpg_data) | |||
217 | } | 212 | } |
218 | 213 | ||
219 | static gpgme_key_t * | 214 | static gpgme_key_t * |
220 | create_key_array(gpgme_ctx_t ctx, struct obstack *stk) | 215 | create_key_array (gpgme_ctx_t ctx) |
221 | { | 216 | { |
222 | gpgme_key_t tmpkey; | 217 | gpgme_key_t tmpkey; |
223 | char *current_key; | 218 | char *current_key; |
224 | int i, j, len = strlen (gpg.encryption_keys); | 219 | int i, j, len = strlen (gpg.encryption_keys); |
225 | 220 | gpgme_key_t *keybase = NULL; | |
221 | size_t keycap = 0; | ||
222 | size_t keycnt = 0; | ||
223 | |||
226 | current_key = xmalloc (len+1); | 224 | current_key = xmalloc (len+1); |
227 | for (i = j = 0; i <= len; i++) | 225 | for (i = j = 0; i <= len; i++) |
228 | { | 226 | { |
@@ -237,8 +235,12 @@ create_key_array(gpgme_ctx_t ctx, struct obstack *stk) | |||
237 | if (err) | 235 | if (err) |
238 | break; | 236 | break; |
239 | 237 | ||
240 | gpgme_get_key (ctx, current_key, &tmpkey, 0); | 238 | err = gpgme_get_key (ctx, current_key, &tmpkey, 0); |
241 | obstack_grow (stk, &tmpkey, sizeof (tmpkey)); | 239 | if (err) |
240 | break; | ||
241 | if (keycap == keycnt) | ||
242 | keybase = x2nrealloc (keybase, &keycap, sizeof (keybase)); | ||
243 | keybase[keycnt++] = tmpkey; | ||
242 | if (options.termlevel == DEBUG) | 244 | if (options.termlevel == DEBUG) |
243 | { | 245 | { |
244 | gpgme_user_id_t uid; | 246 | gpgme_user_id_t uid; |
@@ -248,23 +250,34 @@ create_key_array(gpgme_ctx_t ctx, struct obstack *stk) | |||
248 | uid->uid, uid->name, uid->email); | 250 | uid->uid, uid->name, uid->email); |
249 | } | 251 | } |
250 | } | 252 | } |
251 | if (gpg_err_code (err) != GPG_ERR_EOF) | 253 | |
254 | switch (gpg_err_code (err)) | ||
252 | { | 255 | { |
256 | case GPG_ERR_EOF: | ||
257 | break; | ||
258 | |||
259 | case GPG_ERR_INV_VALUE: | ||
260 | fprintf (stderr, "key not found: %s\n", current_key); | ||
261 | exit (1); | ||
262 | |||
263 | default: | ||
253 | fprintf (stderr, "cannot list keys: %s\n", | 264 | fprintf (stderr, "cannot list keys: %s\n", |
254 | gpgme_strerror (err)); | 265 | gpgme_strerror (err)); |
255 | exit (1); | 266 | exit (1); |
256 | } | 267 | } |
257 | 268 | ||
258 | memset (current_key, 0, sizeof (current_key)); | 269 | memset (current_key, 0, j); |
259 | j = 0; | 270 | j = 0; |
260 | } | 271 | } |
261 | else | 272 | else |
262 | current_key[j++] = gpg.encryption_keys[i]; | 273 | current_key[j++] = gpg.encryption_keys[i]; |
263 | } | 274 | } |
264 | xfree (current_key); | 275 | xfree (current_key); |
265 | tmpkey = NULL; | 276 | |
266 | obstack_grow (stk, &tmpkey, sizeof (tmpkey)); | 277 | if (keycap == keycnt) |
267 | return obstack_finish (stk); | 278 | keybase = x2nrealloc (keybase, &keycap, sizeof (keybase)); |
279 | keybase[keycnt] = NULL; | ||
280 | return keybase; | ||
268 | } | 281 | } |
269 | 282 | ||
270 | static char * | 283 | static char * |
@@ -274,7 +287,6 @@ gpg_encrypt (char *gpg_data) | |||
274 | gpgme_data_t in, out; | 287 | gpgme_data_t in, out; |
275 | char *encrypted_data; | 288 | char *encrypted_data; |
276 | gpgme_key_t *keyptr; | 289 | gpgme_key_t *keyptr; |
277 | struct obstack stk; | ||
278 | gpgme_encrypt_result_t result; | 290 | gpgme_encrypt_result_t result; |
279 | 291 | ||
280 | fail_if_err (gpgme_new (&ctx)); | 292 | fail_if_err (gpgme_new (&ctx)); |
@@ -283,8 +295,7 @@ gpg_encrypt (char *gpg_data) | |||
283 | fail_if_err (gpgme_data_new_from_mem (&in, gpg_data, strlen (gpg_data), 0)); | 295 | fail_if_err (gpgme_data_new_from_mem (&in, gpg_data, strlen (gpg_data), 0)); |
284 | fail_if_err (gpgme_data_new (&out)); | 296 | fail_if_err (gpgme_data_new (&out)); |
285 | 297 | ||
286 | obstack_init (&stk); | 298 | keyptr = create_key_array (ctx); |
287 | keyptr = create_key_array (ctx, &stk); | ||
288 | 299 | ||
289 | fail_if_err (gpgme_op_encrypt (ctx, keyptr, GPGME_ENCRYPT_ALWAYS_TRUST, | 300 | fail_if_err (gpgme_op_encrypt (ctx, keyptr, GPGME_ENCRYPT_ALWAYS_TRUST, |
290 | in, out)); | 301 | in, out)); |
@@ -301,7 +312,6 @@ gpg_encrypt (char *gpg_data) | |||
301 | anubis_gpg_read (out, strlen (gpg_data), &encrypted_data); | 312 | anubis_gpg_read (out, strlen (gpg_data), &encrypted_data); |
302 | for (; *keyptr; keyptr++) | 313 | for (; *keyptr; keyptr++) |
303 | gpgme_key_unref (*keyptr); | 314 | gpgme_key_unref (*keyptr); |
304 | obstack_free (&stk, NULL); | ||
305 | 315 | ||
306 | gpgme_data_release (in); | 316 | gpgme_data_release (in); |
307 | gpgme_data_release (out); | 317 | gpgme_data_release (out); |
@@ -367,7 +377,6 @@ gpg_sign_encrypt (char *gpg_data) | |||
367 | char *p, *se_data; /* Signed-Encrypted Data */ | 377 | char *p, *se_data; /* Signed-Encrypted Data */ |
368 | gpgme_encrypt_result_t result; | 378 | gpgme_encrypt_result_t result; |
369 | gpgme_sign_result_t sign_result; | 379 | gpgme_sign_result_t sign_result; |
370 | struct obstack stk; | ||
371 | 380 | ||
372 | fail_if_err (gpgme_new (&ctx)); | 381 | fail_if_err (gpgme_new (&ctx)); |
373 | 382 | ||
@@ -401,8 +410,7 @@ gpg_sign_encrypt (char *gpg_data) | |||
401 | fail_if_err (gpgme_data_new_from_mem (&in, gpg_data, strlen (gpg_data), 0)); | 410 | fail_if_err (gpgme_data_new_from_mem (&in, gpg_data, strlen (gpg_data), 0)); |
402 | fail_if_err (gpgme_data_new (&out)); | 411 | fail_if_err (gpgme_data_new (&out)); |
403 | 412 | ||
404 | obstack_init (&stk); | 413 | keyptr = create_key_array (ctx); |
405 | keyptr = create_key_array (ctx, &stk); | ||
406 | fail_if_err (gpgme_op_encrypt_sign (ctx, keyptr, GPGME_ENCRYPT_ALWAYS_TRUST, | 414 | fail_if_err (gpgme_op_encrypt_sign (ctx, keyptr, GPGME_ENCRYPT_ALWAYS_TRUST, |
407 | in, out)); | 415 | in, out)); |
408 | result = gpgme_op_encrypt_result (ctx); | 416 | result = gpgme_op_encrypt_result (ctx); |
@@ -424,7 +432,7 @@ gpg_sign_encrypt (char *gpg_data) | |||
424 | 432 | ||
425 | for (; *keyptr; keyptr++) | 433 | for (; *keyptr; keyptr++) |
426 | gpgme_key_unref (*keyptr); | 434 | gpgme_key_unref (*keyptr); |
427 | obstack_free (&stk, NULL); | 435 | |
428 | gpgme_data_release (in); | 436 | gpgme_data_release (in); |
429 | gpgme_data_release (out); | 437 | gpgme_data_release (out); |
430 | gpgme_release (ctx); | 438 | gpgme_release (ctx); |
@@ -571,6 +579,4 @@ gpg_section_init (void) | |||
571 | rc_secdef_add_child (sp, &gpg_sect_child); | 579 | rc_secdef_add_child (sp, &gpg_sect_child); |
572 | } | 580 | } |
573 | 581 | ||
574 | #endif /* HAVE_GPG */ | ||
575 | |||
576 | /* EOF */ | 582 | /* EOF */ |