1 |
/* wptKeyCache.cpp- Caching for the pub- and the secring |
/* wptKeyCache.cpp- Caching for the pub- and the secring |
2 |
* Copyright (C) 2001-2006 Timo Schulz |
* Copyright (C) 2001-2006 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of MyGPGME. |
* This file is part of WinPT. |
5 |
* |
* |
6 |
* MyGPGME is free software; you can redistribute it and/or modify |
* WinPT is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
* (at your option) any later version. |
10 |
* |
* |
11 |
* MyGPGME is distributed in the hope that it will be useful, |
* WinPT is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
* GNU General Public License for more details. |
37 |
#include "wptW32API.h" |
#include "wptW32API.h" |
38 |
#include "wptGPG.h" |
#include "wptGPG.h" |
39 |
#include "wptTypes.h" |
#include "wptTypes.h" |
40 |
|
#include "wptCommonCtl.h" |
41 |
|
#include "wptContext.h" |
42 |
|
#include "wptKeyEdit.h" |
43 |
|
#include "wptUTF8.h" |
44 |
|
|
45 |
|
|
46 |
/* Attribute list which holds the image data. */ |
/* Attribute list which holds the image data. */ |
61 |
attr_list_t n; |
attr_list_t n; |
62 |
while (ctx) { |
while (ctx) { |
63 |
n = ctx->next; |
n = ctx->next; |
64 |
free (ctx->fpr); |
safe_free (ctx->fpr); |
65 |
free (ctx->d); |
safe_free (ctx->d); |
66 |
ctx = n; |
ctx = n; |
67 |
} |
} |
68 |
} |
} |
254 |
} |
} |
255 |
|
|
256 |
|
|
257 |
|
static void |
258 |
|
keycache_decode_uid (struct keycache_s *ctx) |
259 |
|
{ |
260 |
|
gpgme_user_id_t u; |
261 |
|
struct native_uid_s *n, *t; |
262 |
|
|
263 |
|
for (u = ctx->key->uids; u; u = u->next) { |
264 |
|
n = (struct native_uid_s*)calloc (1, sizeof *n); |
265 |
|
if (!n) |
266 |
|
BUG (0); |
267 |
|
if (is_8bit_string (u->uid)) { |
268 |
|
n->malloced = 1; |
269 |
|
n->uid = utf8_to_native (u->uid); |
270 |
|
if (u->name != NULL) |
271 |
|
n->name = utf8_to_native (u->name); |
272 |
|
if (u->email != NULL) |
273 |
|
n->email = strdup (u->email); |
274 |
|
if (u->comment != NULL) |
275 |
|
n->comment = utf8_to_native (u->comment); |
276 |
|
} |
277 |
|
else { |
278 |
|
n->malloced = 0; |
279 |
|
n->uid = u->uid; |
280 |
|
n->name = u->name; |
281 |
|
n->comment = u->comment; |
282 |
|
n->email = u->email; |
283 |
|
} |
284 |
|
n->signatures = u->signatures; |
285 |
|
n->validity = u->validity; |
286 |
|
n->revoked = u->revoked; |
287 |
|
if (!ctx->uids) |
288 |
|
ctx->uids = n; |
289 |
|
else { |
290 |
|
for (t = ctx->uids; t->next; t=t->next) |
291 |
|
; |
292 |
|
t->next = n; |
293 |
|
} |
294 |
|
} |
295 |
|
} |
296 |
|
|
297 |
|
|
298 |
|
/* Store utf8 decoded user IDs in the code to avoid in-place decoding. */ |
299 |
|
static void |
300 |
|
keycache_decode_uids (gpg_keycache_t ctx) |
301 |
|
{ |
302 |
|
struct keycache_s *c; |
303 |
|
|
304 |
|
for (c = ctx->item; c; c = c->next) |
305 |
|
keycache_decode_uid (c); |
306 |
|
} |
307 |
|
|
308 |
|
|
309 |
|
static void |
310 |
|
free_native_uids (struct native_uid_s **r_n) |
311 |
|
{ |
312 |
|
struct native_uid_s *t; |
313 |
|
struct native_uid_s *n = *r_n; |
314 |
|
|
315 |
|
while (n != NULL) { |
316 |
|
t = n->next; |
317 |
|
if (n->malloced) { |
318 |
|
safe_free (n->uid); |
319 |
|
safe_free (n->name); |
320 |
|
safe_free (n->comment); |
321 |
|
safe_free (n->email); |
322 |
|
safe_free (n->uid); |
323 |
|
} |
324 |
|
safe_free (n); |
325 |
|
n = t; |
326 |
|
} |
327 |
|
*r_n = NULL; |
328 |
|
} |
329 |
|
|
330 |
|
|
331 |
|
|
332 |
/* Merge the information from the keyrings into the key cache structure. */ |
/* Merge the information from the keyrings into the key cache structure. */ |
333 |
gpgme_error_t |
static gpgme_error_t |
334 |
keycache_prepare2 (gpg_keycache_t ctx, const char *kid, |
keycache_prepare2 (gpg_keycache_t ctx, const char *kid, |
335 |
const char *pubring, const char *secring) |
const char *pubring, const char *secring) |
336 |
{ |
{ |
363 |
strcpy (keyid, ""); |
strcpy (keyid, ""); |
364 |
key_seen = 1; |
key_seen = 1; |
365 |
} |
} |
|
|
|
366 |
if (pkt->pkttype == PKT_SIGNATURE && |
if (pkt->pkttype == PKT_SIGNATURE && |
367 |
pkt->pkt.signature->sig_class == 0x1F) { |
pkt->pkt.signature->sig_class == 0x1F) { |
368 |
if (pkt->pkt.signature->numrevkeys == 0) |
if (pkt->pkt.signature->numrevkeys == 0) |
376 |
goto next; |
goto next; |
377 |
c->gloflags.has_desig_rev = 1; |
c->gloflags.has_desig_rev = 1; |
378 |
} |
} |
379 |
if (pkt->pkttype == PKT_SIGNATURE && key_seen == 1 ) { |
if (pkt->pkttype == PKT_SIGNATURE && key_seen == 1 && c != NULL) { |
380 |
|
if (c->sym_prefs) /* only use the prefs from the primary uid. */ |
381 |
|
goto next; |
382 |
sym_prefs = gpg_parse_sig_subpkt (pkt->pkt.signature->hashed, |
sym_prefs = gpg_parse_sig_subpkt (pkt->pkt.signature->hashed, |
383 |
SIGSUBPKT_PREF_SYM, &nsym); |
SIGSUBPKT_PREF_SYM, &nsym); |
384 |
if (!sym_prefs) |
if (!sym_prefs) |
456 |
c2 = c->next; |
c2 = c->next; |
457 |
gpgme_key_release (c->key); |
gpgme_key_release (c->key); |
458 |
c->key = NULL; |
c->key = NULL; |
459 |
|
if (c->rev != NULL) |
460 |
|
gpg_desig_rev_release (c->rev); |
461 |
|
c->rev = NULL; |
462 |
safe_free (c->pref_keyserver); |
safe_free (c->pref_keyserver); |
463 |
safe_free (c->sym_prefs); |
safe_free (c->sym_prefs); |
464 |
safe_free (c->attrib.d); |
safe_free (c->attrib.d); |
465 |
safe_free (c->card_type); |
safe_free (c->card_type); |
466 |
free (c); |
free_native_uids (&c->uids); |
467 |
|
safe_free (c); |
468 |
} |
} |
469 |
safe_free (ctx); |
safe_free (ctx); |
470 |
} |
} |
636 |
err = gpgme_new (&gctx); |
err = gpgme_new (&gctx); |
637 |
if (err) |
if (err) |
638 |
return err; |
return err; |
639 |
gpgme_set_keylist_mode (gctx, GPGME_KEYLIST_MODE_SIGS); |
gpgme_set_keylist_mode (gctx, GPGME_KEYLIST_MODE_SIGS/*|GPGME_KEYLIST_MODE_SIG_NOTATIONS*/); |
640 |
err = gpgme_get_key (gctx, keyid, &key, is_sec); |
err = gpgme_get_key (gctx, keyid, &key, is_sec); |
641 |
gpgme_release (gctx); |
gpgme_release (gctx); |
642 |
if (err) |
if (err) |
671 |
} |
} |
672 |
if (c) |
if (c) |
673 |
c->flags = KC_FLAG_ADD; |
c->flags = KC_FLAG_ADD; |
674 |
|
|
675 |
|
} |
676 |
|
|
677 |
|
/* refresh utf8 user ID list. */ |
678 |
|
if (c != NULL) { |
679 |
|
free_native_uids (&c->uids); |
680 |
|
keycache_decode_uid (c); |
681 |
} |
} |
682 |
|
|
683 |
return 0; |
return 0; |
684 |
} |
} |
685 |
|
|
737 |
if (err) |
if (err) |
738 |
return err; |
return err; |
739 |
|
|
740 |
gpgme_set_keylist_mode (c, GPGME_KEYLIST_MODE_SIGS); |
/* XXX: GPGME_KEYLIST_MODE_SIG_NOTATIONS causes an internal error! */ |
741 |
|
gpgme_set_keylist_mode (c, GPGME_KEYLIST_MODE_SIGS/*|GPGME_KEYLIST_MODE_SIG_NOTATIONS*/); |
742 |
err = gpgme_op_keylist_start (c, pattern, secret); |
err = gpgme_op_keylist_start (c, pattern, secret); |
743 |
while(!err) { |
while(!err) { |
744 |
err = gpgme_op_keylist_next (c, &key); |
err = gpgme_op_keylist_next (c, &key); |
751 |
if (gpgme_err_code (err) == GPG_ERR_EOF) |
if (gpgme_err_code (err) == GPG_ERR_EOF) |
752 |
err = gpg_error (GPG_ERR_NO_ERROR); |
err = gpg_error (GPG_ERR_NO_ERROR); |
753 |
keycache_update_photos (ctx); |
keycache_update_photos (ctx); |
754 |
|
keycache_decode_uids (ctx); |
755 |
/* XXX: make sure the progress dialog is closed. */ |
/* XXX: make sure the progress dialog is closed. */ |
756 |
gpgme_op_keylist_end (c); |
gpgme_op_keylist_end (c); |
757 |
gpgme_release (c); |
gpgme_release (c); |
866 |
if (flags && ctx->tmp->pubpart == NULL) |
if (flags && ctx->tmp->pubpart == NULL) |
867 |
flags = 0; |
flags = 0; |
868 |
*r_key = flags? ctx->tmp->pubpart->key : ctx->tmp->key; |
*r_key = flags? ctx->tmp->pubpart->key : ctx->tmp->key; |
869 |
*c = ctx->tmp = ctx->tmp->next; |
*c = ctx->tmp; |
870 |
|
ctx->tmp = ctx->tmp->next; |
871 |
ctx->pos++; |
ctx->pos++; |
872 |
|
|
873 |
return 0; |
return 0; |
881 |
gpg_keycache_next_key (gpg_keycache_t ctx, int flags, gpgme_key_t *r_key) |
gpg_keycache_next_key (gpg_keycache_t ctx, int flags, gpgme_key_t *r_key) |
882 |
{ |
{ |
883 |
struct keycache_s *c=NULL; |
struct keycache_s *c=NULL; |
884 |
gpgme_error_t err = 0; |
gpgme_error_t err; |
885 |
|
|
886 |
err = keycache_next_key (ctx, flags, &c, r_key); |
err = keycache_next_key (ctx, flags, &c, r_key); |
887 |
return err; |
return err; |
888 |
} |
} |
889 |
|
|
890 |
|
gpgme_error_t |
891 |
|
gpg_keycache_next_key2 (gpg_keycache_t ctx, int flags, |
892 |
|
struct keycache_s **c, gpgme_key_t *r_key) |
893 |
|
{ |
894 |
|
return keycache_next_key (ctx, flags, c, r_key); |
895 |
|
} |
896 |
|
|
897 |
|
|
898 |
/* Search for a key with the pattern @pattern and mark |
/* Search for a key with the pattern @pattern and mark |
899 |
this key as the default signing key if found. |
this key as the default signing key if found. |
941 |
decode_subpacket (const char *subpkt_data, int *type, |
decode_subpacket (const char *subpkt_data, int *type, |
942 |
char **out, WORD *outlen) |
char **out, WORD *outlen) |
943 |
{ |
{ |
944 |
char tmp[128], *p = tmp, *val; |
char tmp[128], *val; |
945 |
char *enc = NULL; |
char *enc = NULL; |
946 |
size_t pos = 0, i=0; |
size_t pos = 0, i=0; |
947 |
|
|
996 |
(*out)[i++] = enc[pos]; |
(*out)[i++] = enc[pos]; |
997 |
} |
} |
998 |
(*out)[i] = 0; |
(*out)[i] = 0; |
999 |
free (enc); |
safe_free (enc); |
1000 |
return 0; |
return 0; |
1001 |
} |
} |
1002 |
|
|
1033 |
safe_free (val); |
safe_free (val); |
1034 |
return err; |
return err; |
1035 |
} |
} |
|
|
|