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. |
36 |
#include "wptErrors.h" |
#include "wptErrors.h" |
37 |
#include "wptW32API.h" |
#include "wptW32API.h" |
38 |
#include "wptGPG.h" |
#include "wptGPG.h" |
39 |
|
#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. */ |
54 |
typedef struct attr_list_s *attr_list_t; |
typedef struct attr_list_s *attr_list_t; |
55 |
|
|
56 |
|
|
57 |
|
/* Free attribute list @ctx. */ |
58 |
void |
void |
59 |
free_attr_list (attr_list_t ctx) |
free_attr_list (attr_list_t ctx) |
60 |
{ |
{ |
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 |
} |
} |
87 |
buffer = buf+9+10; |
buffer = buf+9+10; |
88 |
pos = 0; |
pos = 0; |
89 |
c = (attr_list_t)calloc (1, sizeof *c); |
c = (attr_list_t)calloc (1, sizeof *c); |
90 |
|
if (!c) |
91 |
|
BUG (0); |
92 |
p = strtok (buffer, " "); |
p = strtok (buffer, " "); |
93 |
while (p != NULL) { |
while (p != NULL) { |
94 |
switch (pos) { |
switch (pos) { |
110 |
pos++; |
pos++; |
111 |
p = strtok (NULL, " "); |
p = strtok (NULL, " "); |
112 |
} |
} |
|
/*printf ("id=%s octets=%d flags=%d\n", c->fpr, c->octets, c->flags);*/ |
|
113 |
if (!*ctx) |
if (!*ctx) |
114 |
*ctx = c; |
*ctx = c; |
115 |
else { |
else { |
118 |
t->next = c; |
t->next = c; |
119 |
} |
} |
120 |
c->d = (unsigned char*)malloc (c->octets); |
c->d = (unsigned char*)malloc (c->octets); |
121 |
|
if (!c->d) |
122 |
|
BUG (0); |
123 |
memcpy (c->d, data, c->octets); |
memcpy (c->d, data, c->octets); |
124 |
data += c->octets; |
data += c->octets; |
125 |
datlen -= c->octets; |
datlen -= c->octets; |
130 |
} |
} |
131 |
|
|
132 |
|
|
|
/* Always use the $users temp folder. */ |
|
|
static FILE* |
|
|
w32_tmpfile (char *tmp, DWORD tmplen) |
|
|
{ |
|
|
char id[16]; |
|
|
|
|
|
if (!GetTempPath (tmplen-17, tmp)) |
|
|
return NULL; |
|
|
if (tmp[strlen (tmp)-1] != '\\') |
|
|
strcat (tmp, "\\"); |
|
|
_snprintf (id, sizeof (id)-1, "%lu", GetTickCount ()); |
|
|
strcat (tmp, id); |
|
|
return fopen (tmp, "w+b"); |
|
|
} |
|
|
|
|
|
|
|
133 |
static int |
static int |
134 |
parse_attr_data (const char *keyid, attr_list_t *list) |
parse_attr_data (const char *keyid, attr_list_t *list) |
135 |
{ |
{ |
136 |
gpgme_error_t err; |
gpgme_error_t err; |
137 |
FILE *tmp; |
FILE *tmp; |
|
char *status, tmpnam[MAX_PATH+1]; |
|
138 |
BYTE *data; |
BYTE *data; |
139 |
DWORD ndata; |
char *status, tmpnam[MAX_PATH+1]; |
140 |
|
DWORD ndata = 0; |
141 |
|
|
142 |
err = gpg_get_photoid_data (keyid, &status, &data, &ndata); |
err = gpg_get_photoid_data (keyid, &status, &data, &ndata); |
143 |
if (err) |
if (err) |
144 |
return err; |
return err; |
145 |
|
|
146 |
if (ndata > 0 && (tmp = w32_tmpfile (tmpnam, MAX_PATH)) != NULL) { |
get_temp_name (tmpnam, MAX_PATH, NULL); |
147 |
|
tmp = fopen (tmpnam, "w+b"); |
148 |
|
if (ndata > 0 && tmp != NULL) { |
149 |
fwrite (status, 1, strlen (status), tmp); |
fwrite (status, 1, strlen (status), tmp); |
150 |
fflush (tmp); |
fflush (tmp); |
151 |
rewind (tmp); |
rewind (tmp); |
182 |
|
|
183 |
gpg_iobuf_ioctl (inp, 3, 1, NULL); |
gpg_iobuf_ioctl (inp, 3, 1, NULL); |
184 |
pkt = (PACKET*)calloc (1, sizeof *pkt); |
pkt = (PACKET*)calloc (1, sizeof *pkt); |
185 |
|
if (!pkt) |
186 |
|
BUG (0); |
187 |
gpg_init_packet (pkt); |
gpg_init_packet (pkt); |
188 |
while (gpg_parse_packet (inp, pkt) != -1) { |
while (gpg_parse_packet (inp, pkt) != -1) { |
189 |
if (pkt->pkttype == PKT_SECRET_KEY) { |
if (pkt->pkttype == PKT_SECRET_KEY) { |
227 |
fnd->attrib.flags = dat->flags; |
fnd->attrib.flags = dat->flags; |
228 |
fnd->attrib.len = dat->octets; |
fnd->attrib.len = dat->octets; |
229 |
fnd->attrib.d = (unsigned char*)malloc (dat->octets); |
fnd->attrib.d = (unsigned char*)malloc (dat->octets); |
230 |
|
if (!fnd->attrib.d) |
231 |
|
BUG (0); |
232 |
memcpy (fnd->attrib.d, dat->d, dat->octets); |
memcpy (fnd->attrib.d, dat->d, dat->octets); |
233 |
return 0; |
return 0; |
234 |
} |
} |
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 |
{ |
{ |
355 |
gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */ |
gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */ |
356 |
|
|
357 |
pkt = (PACKET*)calloc (1, sizeof * pkt); |
pkt = (PACKET*)calloc (1, sizeof * pkt); |
358 |
|
if (!pkt) |
359 |
|
BUG (0); |
360 |
gpg_init_packet (pkt); |
gpg_init_packet (pkt); |
361 |
while (gpg_parse_packet (inp, pkt) != -1) { |
while (gpg_parse_packet (inp, pkt) != -1) { |
362 |
if (pkt->pkttype == PKT_PUBLIC_KEY) { |
if (pkt->pkttype == PKT_PUBLIC_KEY) { |
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) |
393 |
else if (nsym > 0) { |
else if (nsym > 0) { |
394 |
c->sym_prefs = (unsigned char*)calloc (1, nsym+1); |
c->sym_prefs = (unsigned char*)calloc (1, nsym+1); |
395 |
if (!c->sym_prefs) |
if (!c->sym_prefs) |
396 |
return gpg_error (GPG_ERR_ENOMEM); |
BUG (0); |
397 |
memcpy (c->sym_prefs, sym_prefs, nsym); |
memcpy (c->sym_prefs, sym_prefs, nsym); |
398 |
} |
} |
399 |
} |
} |
435 |
return gpg_error (GPG_ERR_INV_ARG); |
return gpg_error (GPG_ERR_INV_ARG); |
436 |
ctx = (gpg_keycache_t)calloc (1, sizeof *ctx); |
ctx = (gpg_keycache_t)calloc (1, sizeof *ctx); |
437 |
if (!ctx) |
if (!ctx) |
438 |
return gpg_error (GPG_ERR_ENOMEM); |
BUG (0); |
439 |
ctx->secret = 0; |
ctx->secret = 0; |
440 |
ctx->pos = 0; |
ctx->pos = 0; |
441 |
*r_ctx = ctx; |
*r_ctx = ctx; |
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); |
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 |
} |
} |
500 |
|
|
501 |
c = (struct keycache_s*)calloc (1, sizeof *c); |
c = (struct keycache_s*)calloc (1, sizeof *c); |
502 |
if (!c) |
if (!c) |
503 |
return gpg_error (GPG_ERR_ENOMEM); |
BUG (0); |
504 |
c->gloflags.is_protected = 1; /*default: assume protection. */ |
c->gloflags.is_protected = 1; /*default: assume protection. */ |
505 |
c->key = key; |
c->key = key; |
506 |
if (!ctx->item) |
if (!ctx->item) |
613 |
for (c = ctx->item; c; c = c->next) { |
for (c = ctx->item; c; c = c->next) { |
614 |
if (c->flags != 0) { |
if (c->flags != 0) { |
615 |
*r_status = c->flags; |
*r_status = c->flags; |
|
c->flags = 0; |
|
616 |
*r_obj = c; |
*r_obj = c; |
617 |
|
c->flags = 0; |
618 |
return 0; |
return 0; |
619 |
} |
} |
620 |
} |
} |
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 |
|
|
702 |
c = ctx->item; |
c = ctx->item; |
703 |
if (c->next == NULL) { |
if (c->next == NULL) { |
704 |
gpgme_key_release (itm->key); |
gpgme_key_release (itm->key); |
705 |
if (itm) |
safe_free (itm); |
|
free (itm); |
|
706 |
ctx->item = NULL; |
ctx->item = NULL; |
707 |
} |
} |
708 |
else { |
else { |
709 |
while (c && c->next != itm) |
for (; c != NULL; c = c->next) { |
710 |
c = c->next; |
if (c->next == itm) |
711 |
|
break; |
712 |
|
} |
713 |
|
assert (c != NULL); /* XXX: sometimes access violation. */ |
714 |
c->next = c->next->next; |
c->next = c->next->next; |
715 |
gpgme_key_release (itm->key); |
gpgme_key_release (itm->key); |
716 |
if (itm) |
safe_free (itm); |
|
free (itm); |
|
717 |
} |
} |
718 |
return 0; |
return 0; |
719 |
} |
} |
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); |
789 |
pos++; |
pos++; |
790 |
p = (unsigned char*)calloc (1, pos+1); |
p = (unsigned char*)calloc (1, pos+1); |
791 |
if (!p) |
if (!p) |
792 |
abort (); |
BUG (0); |
793 |
memcpy (p, prefs, pos); |
memcpy (p, prefs, pos); |
794 |
return p; |
return p; |
795 |
} |
} |
810 |
c_sec->gloflags.divert_to_card = c->gloflags.divert_to_card; |
c_sec->gloflags.divert_to_card = c->gloflags.divert_to_card; |
811 |
if (!c->gloflags.divert_to_card) |
if (!c->gloflags.divert_to_card) |
812 |
c->gloflags.divert_to_card = key_divert_to_card (key); |
c->gloflags.divert_to_card = key_divert_to_card (key); |
813 |
c->sym_prefs = copy_uid_prefs (c_sec->sym_prefs); |
if (c_sec->sym_prefs) |
814 |
|
c->sym_prefs = copy_uid_prefs (c_sec->sym_prefs); |
815 |
c->pubpart = c_sec; |
c->pubpart = c_sec; |
816 |
c->pubpart->key = key; |
c->pubpart->key = key; |
817 |
} |
} |
862 |
} |
} |
863 |
if (ctx->tmp->flags != 0) |
if (ctx->tmp->flags != 0) |
864 |
ctx->tmp->flags = 0; /* reset the 'updated' status. */ |
ctx->tmp->flags = 0; /* reset the 'updated' status. */ |
865 |
|
/* it might be possible there is no public key. */ |
866 |
|
if (flags && ctx->tmp->pubpart == NULL) |
867 |
|
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 |
899 |
|
this key as the default signing key if found. |
900 |
|
Return value: 0 on success. */ |
901 |
|
gpgme_error_t |
902 |
|
gpg_keycache_set_default_key (gpg_keycache_t ctx, |
903 |
|
const char *pattern) |
904 |
|
{ |
905 |
|
gpgme_error_t err; |
906 |
|
gpgme_key_t key; |
907 |
|
struct keycache_s *itm; |
908 |
|
|
909 |
|
err = gpg_keycache_find_key2 (ctx, pattern, 0, &key, &itm); |
910 |
|
if (err) |
911 |
|
return err; |
912 |
|
|
913 |
|
if (itm) |
914 |
|
itm->default_key = 1; |
915 |
|
return 0; |
916 |
|
} |
917 |
|
|
918 |
|
/* Return the default key from the cache. If no was |
919 |
|
marked before, NULL is returned in @r_key. |
920 |
|
Return value: 0 on success. */ |
921 |
|
gpgme_error_t |
922 |
|
gpg_keycache_get_default_key (gpg_keycache_t ctx, |
923 |
|
gpgme_key_t *r_key) |
924 |
|
{ |
925 |
|
struct keycache_s *itm; |
926 |
|
|
927 |
|
*r_key = NULL; |
928 |
|
for (itm = ctx->item; itm; itm = itm->next) { |
929 |
|
if (itm->default_key) { |
930 |
|
*r_key = itm->key; |
931 |
|
break; |
932 |
|
} |
933 |
|
} |
934 |
|
if (!*r_key) |
935 |
|
return gpgme_error (GPG_ERR_NOT_FOUND); |
936 |
|
return 0; |
937 |
|
} |
938 |
|
|
939 |
|
|
940 |
|
static gpgme_error_t |
941 |
|
decode_subpacket (const char *subpkt_data, int *type, |
942 |
|
char **out, WORD *outlen) |
943 |
|
{ |
944 |
|
char tmp[128], *val; |
945 |
|
char *enc = NULL; |
946 |
|
size_t pos = 0, i=0; |
947 |
|
|
948 |
|
/* example: spk:24:1:21:http%3A//subkeys.pgp.de */ |
949 |
|
*outlen = 0; |
950 |
|
*out = NULL; |
951 |
|
|
952 |
|
if (strncmp (subpkt_data, "spk:", 4)) |
953 |
|
return gpg_error (GPG_ERR_NO_DATA); |
954 |
|
|
955 |
|
strncpy (tmp, subpkt_data, 62); |
956 |
|
val = strtok (tmp, ":"); |
957 |
|
while (val != NULL) { |
958 |
|
switch (pos++) { |
959 |
|
case 0: |
960 |
|
break; |
961 |
|
|
962 |
|
case 1: |
963 |
|
if (type) |
964 |
|
*type = atoi (val); |
965 |
|
break; |
966 |
|
|
967 |
|
case 2: |
968 |
|
break; |
969 |
|
|
970 |
|
case 3: |
971 |
|
*outlen = atoi (val); |
972 |
|
break; |
973 |
|
|
974 |
|
case 4: |
975 |
|
enc = strdup (val); |
976 |
|
break; |
977 |
|
} |
978 |
|
val = strtok (NULL, ":"); |
979 |
|
} |
980 |
|
if (!enc) |
981 |
|
return gpg_error (GPG_ERR_NO_DATA);; |
982 |
|
*out = (char*)calloc (1, strlen (enc)+1); |
983 |
|
if (!*out) |
984 |
|
BUG (0); |
985 |
|
for (pos = 0; pos < strlen (enc); pos++) { |
986 |
|
if (enc[pos] == '%' && enc[pos+1] == '%') |
987 |
|
(*out)[i++] = '%'; |
988 |
|
else if (enc[pos] == '%') { |
989 |
|
char temp[3]; |
990 |
|
temp[0] = enc[++pos]; |
991 |
|
temp[1] = enc[++pos]; |
992 |
|
temp[2] = 0; |
993 |
|
(*out)[i++] = (char)strtoul (temp, NULL, 16); |
994 |
|
} |
995 |
|
else |
996 |
|
(*out)[i++] = enc[pos]; |
997 |
|
} |
998 |
|
(*out)[i] = 0; |
999 |
|
safe_free (enc); |
1000 |
|
return 0; |
1001 |
|
} |
1002 |
|
|
1003 |
|
|
1004 |
|
/* If the attribute given in @attr is not set in the |
1005 |
|
key cache object, try to update it. */ |
1006 |
|
gpgme_error_t |
1007 |
|
gpg_keycache_update_attr (struct keycache_s *item, |
1008 |
|
int attr, int force) |
1009 |
|
{ |
1010 |
|
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
1011 |
|
char *val = NULL; |
1012 |
|
WORD n = 0; |
1013 |
|
|
1014 |
|
switch (attr) { |
1015 |
|
case KC_ATTR_PREFSYM: |
1016 |
|
if (!force && item->sym_prefs) |
1017 |
|
break; |
1018 |
|
safe_free (item->sym_prefs); |
1019 |
|
err = gpg_find_key_subpacket (item->key->subkeys->keyid+8, attr, &val); |
1020 |
|
if (!err && val != NULL) |
1021 |
|
err = decode_subpacket (val, NULL, (char**)&item->sym_prefs, &n); |
1022 |
|
break; |
1023 |
|
|
1024 |
|
case KC_ATTR_PREFKSERV: |
1025 |
|
if (!force && item->pref_keyserver) |
1026 |
|
break; |
1027 |
|
safe_free (item->pref_keyserver); |
1028 |
|
err = gpg_find_key_subpacket (item->key->subkeys->keyid+8, attr, &val); |
1029 |
|
if (!err && val != NULL) |
1030 |
|
err = decode_subpacket (val, NULL, &item->pref_keyserver, &n); |
1031 |
|
break; |
1032 |
|
} |
1033 |
|
safe_free (val); |
1034 |
|
return err; |
1035 |
|
} |