1 |
/* wptKeyCache.cpp- Caching for the pub- and the secring |
/* wptKeyCache.cpp- Caching for the pub- and the secring |
2 |
* Copyright (C) 2001-2005 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. |
35 |
#include "wptNLS.h" |
#include "wptNLS.h" |
36 |
#include "wptErrors.h" |
#include "wptErrors.h" |
37 |
#include "wptW32API.h" |
#include "wptW32API.h" |
38 |
|
#include "wptGPG.h" |
39 |
|
#include "wptTypes.h" |
40 |
|
#include "wptUTF8.h" |
41 |
|
|
42 |
|
|
43 |
|
/* Attribute list which holds the image data. */ |
44 |
|
struct attr_list_s { |
45 |
|
struct attr_list_s *next; |
46 |
|
char *fpr; /* fingerprint of the key */ |
47 |
|
unsigned char *d; /* actual JPEG data. */ |
48 |
|
unsigned long octets; /* length of the data. */ |
49 |
|
unsigned int flags; /* status of the attribute. */ |
50 |
|
}; |
51 |
|
typedef struct attr_list_s *attr_list_t; |
52 |
|
|
53 |
|
|
54 |
#if 0 |
/* Free attribute list @ctx. */ |
55 |
/* convert a binary buffer into its hex representation. */ |
void |
56 |
static void |
free_attr_list (attr_list_t ctx) |
|
buffer_to_string (char *dst, size_t dlen, const byte *buf, size_t nbytes) |
|
57 |
{ |
{ |
58 |
char dig[3]; |
attr_list_t n; |
59 |
size_t i; |
while (ctx) { |
60 |
|
n = ctx->next; |
61 |
memset (dst, 0, dlen); |
safe_free (ctx->fpr); |
62 |
for (i = 0; i < nbytes && dlen > 0; i++) { |
safe_free (ctx->d); |
63 |
sprintf (dig, "%02X", buf[i]); |
ctx = n; |
|
strcat (dst, dig); |
|
|
dlen -= 2; |
|
64 |
} |
} |
65 |
} |
} |
66 |
#endif |
|
67 |
|
/* Parse the attribute list in @fp and store it into @ctx. |
68 |
|
Return value: number of parsed items. */ |
69 |
|
int |
70 |
|
parse_attr_list (FILE *fp, const BYTE *data, DWORD datlen, attr_list_t *ctx) |
71 |
|
{ |
72 |
|
attr_list_t c, t; |
73 |
|
char buf[512], *p, *buffer; |
74 |
|
int pos, n=0; |
75 |
|
|
76 |
|
*ctx = NULL; |
77 |
|
while (fgets (buf, 511, fp)) { |
78 |
|
if (strstr (buf, "\r\n")) |
79 |
|
buf[strlen (buf)-2]=0; |
80 |
|
if (strstr (buf, "\n")) |
81 |
|
buf[strlen (buf)-1]=0; |
82 |
|
if (strlen (buf) < 2 || !strstr (buf, "ATTRIBUTE")) |
83 |
|
continue; |
84 |
|
buffer = buf+9+10; |
85 |
|
pos = 0; |
86 |
|
c = (attr_list_t)calloc (1, sizeof *c); |
87 |
|
if (!c) |
88 |
|
BUG (0); |
89 |
|
p = strtok (buffer, " "); |
90 |
|
while (p != NULL) { |
91 |
|
switch (pos) { |
92 |
|
case 0: |
93 |
|
c->fpr = strdup (p); |
94 |
|
break; |
95 |
|
|
96 |
|
case 1: |
97 |
|
c->octets = strtoul (p, NULL, 10); |
98 |
|
break; |
99 |
|
|
100 |
|
case 7: |
101 |
|
c->flags = strtoul (p, NULL, 10); |
102 |
|
break; |
103 |
|
|
104 |
|
default: |
105 |
|
break; |
106 |
|
} |
107 |
|
pos++; |
108 |
|
p = strtok (NULL, " "); |
109 |
|
} |
110 |
|
if (!*ctx) |
111 |
|
*ctx = c; |
112 |
|
else { |
113 |
|
for (t = *ctx; t->next; t=t->next) |
114 |
|
; |
115 |
|
t->next = c; |
116 |
|
} |
117 |
|
c->d = (unsigned char*)malloc (c->octets); |
118 |
|
if (!c->d) |
119 |
|
BUG (0); |
120 |
|
memcpy (c->d, data, c->octets); |
121 |
|
data += c->octets; |
122 |
|
datlen -= c->octets; |
123 |
|
n++; |
124 |
|
} |
125 |
|
/*assert (datlen == 0); */ |
126 |
|
return n; |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
|
static int |
131 |
|
parse_attr_data (const char *keyid, attr_list_t *list) |
132 |
|
{ |
133 |
|
gpgme_error_t err; |
134 |
|
FILE *tmp; |
135 |
|
BYTE *data; |
136 |
|
char *status, tmpnam[MAX_PATH+1]; |
137 |
|
DWORD ndata = 0; |
138 |
|
|
139 |
|
err = gpg_get_photoid_data (keyid, &status, &data, &ndata); |
140 |
|
if (err) |
141 |
|
return err; |
142 |
|
|
143 |
|
get_temp_name (tmpnam, MAX_PATH, NULL); |
144 |
|
tmp = fopen (tmpnam, "w+b"); |
145 |
|
if (ndata > 0 && tmp != NULL) { |
146 |
|
fwrite (status, 1, strlen (status), tmp); |
147 |
|
fflush (tmp); |
148 |
|
rewind (tmp); |
149 |
|
|
150 |
|
ndata = parse_attr_list (tmp, data, ndata, list); |
151 |
|
fclose (tmp); |
152 |
|
DeleteFile (tmpnam); |
153 |
|
} |
154 |
|
else |
155 |
|
*list = NULL; |
156 |
|
|
157 |
|
safe_free (status); |
158 |
|
safe_free (data); |
159 |
|
return ndata; |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
/* Parse the secret keyring and retrieve some additional information |
/* Parse the secret keyring and retrieve some additional information |
165 |
static void |
static void |
166 |
parse_secring (gpg_keycache_t cache, const char *kid, const char *secring) |
parse_secring (gpg_keycache_t cache, const char *kid, const char *secring) |
167 |
{ |
{ |
168 |
PACKET *pkt = (PACKET*)calloc (1, sizeof *pkt); |
PACKET *pkt; |
169 |
PKT_secret_key *sk; |
PKT_secret_key *sk; |
170 |
gpg_iobuf_t inp; |
gpg_iobuf_t inp; |
171 |
gpgme_error_t err; |
gpgme_error_t err; |
174 |
char keyid[16+1]; |
char keyid[16+1]; |
175 |
|
|
176 |
inp = gpg_iobuf_open (secring); |
inp = gpg_iobuf_open (secring); |
177 |
if (!inp) { |
if (!inp) |
|
safe_free (pkt); |
|
178 |
return; |
return; |
179 |
} |
|
180 |
gpg_iobuf_ioctl (inp, 3, 1, NULL); |
gpg_iobuf_ioctl (inp, 3, 1, NULL); |
181 |
|
pkt = (PACKET*)calloc (1, sizeof *pkt); |
182 |
|
if (!pkt) |
183 |
|
BUG (0); |
184 |
gpg_init_packet (pkt); |
gpg_init_packet (pkt); |
185 |
while (gpg_parse_packet (inp, pkt) != -1) { |
while (gpg_parse_packet (inp, pkt) != -1) { |
186 |
if (pkt->pkttype == PKT_SECRET_KEY) { |
if (pkt->pkttype == PKT_SECRET_KEY) { |
209 |
} |
} |
210 |
|
|
211 |
|
|
212 |
|
/* Update the photo image of a single key with the fingerprint |
213 |
|
@fpr. The @dat struct contains the new item data. */ |
214 |
|
static gpgme_error_t |
215 |
|
keycache_update_photo (gpg_keycache_t ctx, const char *fpr, attr_list_t dat) |
216 |
|
{ |
217 |
|
struct keycache_s *fnd = NULL; |
218 |
|
gpgme_key_t key; |
219 |
|
|
220 |
|
gpg_keycache_find_key2 (ctx, fpr, 0, &key, &fnd); |
221 |
|
if (!fnd) |
222 |
|
return gpg_error (GPG_ERR_NOT_FOUND); |
223 |
|
safe_free (fnd->attrib.d); |
224 |
|
fnd->attrib.flags = dat->flags; |
225 |
|
fnd->attrib.len = dat->octets; |
226 |
|
fnd->attrib.d = (unsigned char*)malloc (dat->octets); |
227 |
|
if (!fnd->attrib.d) |
228 |
|
BUG (0); |
229 |
|
memcpy (fnd->attrib.d, dat->d, dat->octets); |
230 |
|
return 0; |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
|
/* Update all photo images in the cache. */ |
235 |
|
static gpgme_error_t |
236 |
|
keycache_update_photos (gpg_keycache_t ctx) |
237 |
|
{ |
238 |
|
attr_list_t list=NULL, n; |
239 |
|
DWORD ndata; |
240 |
|
|
241 |
|
ndata = parse_attr_data (NULL, &list); |
242 |
|
if (ndata < 1) { |
243 |
|
free_attr_list (list); |
244 |
|
return 0; |
245 |
|
} |
246 |
|
|
247 |
|
for (n=list; n; n=n->next) |
248 |
|
keycache_update_photo (ctx, n->fpr, n); |
249 |
|
free_attr_list (list); |
250 |
|
return 0; |
251 |
|
} |
252 |
|
|
253 |
|
|
254 |
|
/* Store utf8 decoded user IDs in the code to avoid in-place decoding. */ |
255 |
|
static gpgme_error_t |
256 |
|
keycache_decode_uids (gpg_keycache_t ctx) |
257 |
|
{ |
258 |
|
struct native_uid_s *n, *t; |
259 |
|
struct keycache_s *c; |
260 |
|
gpgme_user_id_t u; |
261 |
|
|
262 |
|
for (c = ctx->item; c; c = c->next) { |
263 |
|
for (u = c->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 |
|
if (!c->uids) |
285 |
|
c->uids = n; |
286 |
|
else { |
287 |
|
for (t = c->uids; t->next; t=t->next) |
288 |
|
; |
289 |
|
t->next = n; |
290 |
|
} |
291 |
|
} |
292 |
|
} |
293 |
|
return 0; |
294 |
|
} |
295 |
|
|
296 |
|
|
297 |
|
static void |
298 |
|
free_native_uids (struct native_uid_s *n) |
299 |
|
{ |
300 |
|
struct native_uid_s *t; |
301 |
|
|
302 |
|
while (n != NULL) { |
303 |
|
t = n->next; |
304 |
|
if (n->malloced) { |
305 |
|
safe_free (n->uid); |
306 |
|
safe_free (n->name); |
307 |
|
safe_free (n->comment); |
308 |
|
safe_free (n->email); |
309 |
|
safe_free (n->uid); |
310 |
|
} |
311 |
|
safe_free (n); |
312 |
|
n = t; |
313 |
|
} |
314 |
|
} |
315 |
|
|
316 |
|
|
317 |
|
|
318 |
/* Merge the information from the keyrings into the key cache structure. */ |
/* Merge the information from the keyrings into the key cache structure. */ |
319 |
gpgme_error_t |
gpgme_error_t |
320 |
keycache_prepare2 (gpg_keycache_t ctx, const char *kid, |
keycache_prepare2 (gpg_keycache_t ctx, const char *kid, |
323 |
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
324 |
gpgme_key_t key = NULL; |
gpgme_key_t key = NULL; |
325 |
gpg_iobuf_t inp; |
gpg_iobuf_t inp; |
326 |
PACKET *pkt = (PACKET*)calloc (1, sizeof * pkt); |
PACKET *pkt; |
327 |
struct keycache_s *c; |
struct keycache_s *c; |
328 |
const byte *sym_prefs; |
const byte *sym_prefs; |
329 |
char keyid[16+1], *id = NULL; |
char keyid[16+1]; |
330 |
int key_seen = 0; |
int key_seen = 0; |
331 |
size_t nsym =0; |
size_t nsym =0; |
332 |
|
|
333 |
if (secring) { |
if (secring) { |
334 |
parse_secring (ctx, kid, secring); |
parse_secring (ctx, kid, secring); |
335 |
if (!pubring) { |
if (!pubring) |
|
safe_free(pkt); |
|
336 |
return 0; |
return 0; |
|
} |
|
337 |
} |
} |
338 |
inp = gpg_iobuf_open (pubring); |
inp = gpg_iobuf_open (pubring); |
339 |
if (!inp) { |
if (!inp) |
|
safe_free( pkt ); |
|
340 |
return gpg_error (GPG_ERR_KEYRING_OPEN); |
return gpg_error (GPG_ERR_KEYRING_OPEN); |
341 |
} |
gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */ |
|
gpg_iobuf_ioctl( inp, 3, 1, NULL ); /* disable cache */ |
|
342 |
|
|
343 |
gpg_init_packet( pkt ); |
pkt = (PACKET*)calloc (1, sizeof * pkt); |
344 |
|
if (!pkt) |
345 |
|
BUG (0); |
346 |
|
gpg_init_packet (pkt); |
347 |
while (gpg_parse_packet (inp, pkt) != -1) { |
while (gpg_parse_packet (inp, pkt) != -1) { |
348 |
if (pkt->pkttype == PKT_PUBLIC_KEY) { |
if (pkt->pkttype == PKT_PUBLIC_KEY) { |
349 |
strcpy (keyid, ""); |
strcpy (keyid, ""); |
350 |
key_seen = 1; |
key_seen = 1; |
351 |
} |
} |
352 |
|
|
353 |
if (pkt->pkttype == PKT_SIGNATURE && pkt->pkt.signature->sig_class == 0x1F) { |
if (pkt->pkttype == PKT_SIGNATURE && |
354 |
|
pkt->pkt.signature->sig_class == 0x1F) { |
355 |
if (pkt->pkt.signature->numrevkeys == 0) |
if (pkt->pkt.signature->numrevkeys == 0) |
356 |
goto next; |
goto next; |
357 |
_snprintf (keyid, sizeof keyid -1, "%08X", pkt->pkt.signature->keyid[1]); |
_snprintf (keyid, sizeof (keyid) -1, "%08X", |
358 |
|
pkt->pkt.signature->keyid[1]); |
359 |
if (kid && strcmp (kid, keyid) != 0) |
if (kid && strcmp (kid, keyid) != 0) |
360 |
goto next; |
goto next; |
361 |
err = gpg_keycache_find_key2 (ctx, keyid, 0, &key, &c); |
err = gpg_keycache_find_key2 (ctx, keyid, 0, &key, &c); |
364 |
c->gloflags.has_desig_rev = 1; |
c->gloflags.has_desig_rev = 1; |
365 |
} |
} |
366 |
if (pkt->pkttype == PKT_SIGNATURE && key_seen == 1 ) { |
if (pkt->pkttype == PKT_SIGNATURE && key_seen == 1 ) { |
367 |
sym_prefs=gpg_parse_sig_subpkt (pkt->pkt.signature->hashed, |
sym_prefs = gpg_parse_sig_subpkt (pkt->pkt.signature->hashed, |
368 |
SIGSUBPKT_PREF_SYM, &nsym); |
SIGSUBPKT_PREF_SYM, &nsym); |
369 |
if (sym_prefs == NULL) |
if (!sym_prefs) |
370 |
goto next; |
goto next; |
371 |
_snprintf (keyid, sizeof keyid - 1, "%08X", pkt->pkt.signature->keyid[1]); |
_snprintf (keyid, sizeof (keyid) - 1, "%08X", |
372 |
|
pkt->pkt.signature->keyid[1]); |
373 |
if (kid && strcmp (kid, keyid) != 0) |
if (kid && strcmp (kid, keyid) != 0) |
374 |
goto next; |
goto next; |
375 |
err = gpg_keycache_find_key2 (ctx, keyid, 0, &key, &c); |
err = gpg_keycache_find_key2 (ctx, keyid, 0, &key, &c); |
378 |
else if (nsym > 0) { |
else if (nsym > 0) { |
379 |
c->sym_prefs = (unsigned char*)calloc (1, nsym+1); |
c->sym_prefs = (unsigned char*)calloc (1, nsym+1); |
380 |
if (!c->sym_prefs) |
if (!c->sym_prefs) |
381 |
return gpg_error (GPG_ERR_ENOMEM); |
BUG (0); |
382 |
memcpy (c->sym_prefs, sym_prefs, nsym); |
memcpy (c->sym_prefs, sym_prefs, nsym); |
383 |
} |
} |
384 |
} |
} |
|
if (pkt->pkttype == PKT_USER_ID) { |
|
|
if (id) |
|
|
free (id); |
|
|
id = strdup (pkt->pkt.user_id->name); |
|
|
if (!id) { |
|
|
err = gpg_error (GPG_ERR_ENOMEM); |
|
|
goto fail; |
|
|
} |
|
|
} |
|
|
if ((pkt->pkttype == PKT_USER_ID || pkt->pkttype == PKT_ATTRIBUTE) |
|
|
&& pkt->pkt.user_id->attrib_data && key) { |
|
|
PKT_user_id *uid = pkt->pkt.user_id; |
|
|
c->attrib.used = 1; |
|
|
c->attrib.len = uid->attrib_len; |
|
|
c->attrib.d = (unsigned char*)calloc (1, uid->attrib_len + 1); |
|
|
if (!c->attrib.d) { |
|
|
err = gpg_error (GPG_ERR_ENOMEM); |
|
|
goto fail; |
|
|
} |
|
|
memcpy (c->attrib.d, uid->attrib_data, uid->attrib_len); |
|
|
key = NULL; |
|
|
c = NULL; |
|
|
} |
|
385 |
next: |
next: |
386 |
gpg_free_packet (pkt); |
gpg_free_packet (pkt); |
387 |
gpg_init_packet(pkt); |
gpg_init_packet(pkt); |
388 |
} |
} |
389 |
|
|
|
fail: |
|
|
safe_free (id); |
|
390 |
safe_free (pkt); |
safe_free (pkt); |
391 |
gpg_iobuf_close (inp); |
gpg_iobuf_close (inp); |
392 |
return err; |
return err; |
420 |
return gpg_error (GPG_ERR_INV_ARG); |
return gpg_error (GPG_ERR_INV_ARG); |
421 |
ctx = (gpg_keycache_t)calloc (1, sizeof *ctx); |
ctx = (gpg_keycache_t)calloc (1, sizeof *ctx); |
422 |
if (!ctx) |
if (!ctx) |
423 |
return gpg_error (GPG_ERR_ENOMEM); |
BUG (0); |
424 |
ctx->secret = 0; |
ctx->secret = 0; |
425 |
ctx->pos = 0; |
ctx->pos = 0; |
426 |
*r_ctx = ctx; |
*r_ctx = ctx; |
441 |
c2 = c->next; |
c2 = c->next; |
442 |
gpgme_key_release (c->key); |
gpgme_key_release (c->key); |
443 |
c->key = NULL; |
c->key = NULL; |
444 |
if (c->sym_prefs) |
safe_free (c->pref_keyserver); |
445 |
free (c->sym_prefs); |
safe_free (c->sym_prefs); |
446 |
c->sym_prefs = NULL; |
safe_free (c->attrib.d); |
447 |
if (c->attrib.d) |
safe_free (c->card_type); |
448 |
free (c->attrib.d); |
free_native_uids (c->uids); |
449 |
c->attrib.d = NULL; |
safe_free (c); |
|
if (c->card_type) |
|
|
free (c->card_type); |
|
|
free (c); |
|
450 |
} |
} |
451 |
if (ctx) |
safe_free (ctx); |
|
free (ctx); |
|
452 |
} |
} |
453 |
|
|
454 |
|
|
482 |
|
|
483 |
c = (struct keycache_s*)calloc (1, sizeof *c); |
c = (struct keycache_s*)calloc (1, sizeof *c); |
484 |
if (!c) |
if (!c) |
485 |
return gpg_error (GPG_ERR_ENOMEM); |
BUG (0); |
486 |
c->gloflags.is_protected = 1; /*default: assume protection. */ |
c->gloflags.is_protected = 1; /*default: assume protection. */ |
487 |
c->key = key; |
c->key = key; |
488 |
if (!ctx->item) |
if (!ctx->item) |
553 |
} |
} |
554 |
*r_key = NULL; |
*r_key = NULL; |
555 |
return gpg_error (GPG_ERR_INTERNAL); |
return gpg_error (GPG_ERR_INTERNAL); |
556 |
} /* keycache_find_key */ |
} |
557 |
|
|
558 |
|
|
559 |
gpgme_error_t |
gpgme_error_t |
564 |
} |
} |
565 |
|
|
566 |
|
|
567 |
|
/* Reload a photo image of a single key with the keyid @keyid. |
568 |
|
Return value: 0 on success. */ |
569 |
|
static gpgme_error_t |
570 |
|
keycache_reload_photo (gpg_keycache_t ctx, const char *keyid) |
571 |
|
{ |
572 |
|
attr_list_t list; |
573 |
|
|
574 |
|
if (parse_attr_data (keyid, &list) < 1) { |
575 |
|
free_attr_list (list); |
576 |
|
return 0; |
577 |
|
} |
578 |
|
keycache_update_photo (ctx, list->fpr, list); |
579 |
|
free_attr_list (list); |
580 |
|
return 0; |
581 |
|
} |
582 |
|
|
583 |
|
|
584 |
|
/* Return the next key which was updated. Before it is |
585 |
|
returned the update flag is cleared. |
586 |
|
@r_status is 1 for a new key and 2 for an updated key. |
587 |
|
Return value: 0 on success. */ |
588 |
|
gpgme_error_t |
589 |
|
gpg_keycache_next_updated_key (gpg_keycache_t ctx, |
590 |
|
struct keycache_s **r_obj, |
591 |
|
int *r_status) |
592 |
|
{ |
593 |
|
struct keycache_s *c; |
594 |
|
|
595 |
|
for (c = ctx->item; c; c = c->next) { |
596 |
|
if (c->flags != 0) { |
597 |
|
*r_status = c->flags; |
598 |
|
*r_obj = c; |
599 |
|
c->flags = 0; |
600 |
|
return 0; |
601 |
|
} |
602 |
|
} |
603 |
|
return gpg_error (GPG_ERR_NOT_FOUND); |
604 |
|
} |
605 |
|
|
606 |
|
|
607 |
|
|
608 |
gpgme_error_t |
gpgme_error_t |
609 |
gpg_keycache_update_key (gpg_keycache_t ctx, int is_sec, |
gpg_keycache_update_key (gpg_keycache_t ctx, int is_sec, |
610 |
void *opaque, const char *keyid) |
void *opaque, const char *keyid) |
618 |
err = gpgme_new (&gctx); |
err = gpgme_new (&gctx); |
619 |
if (err) |
if (err) |
620 |
return err; |
return err; |
621 |
|
gpgme_set_keylist_mode (gctx, GPGME_KEYLIST_MODE_SIGS); |
622 |
err = gpgme_get_key (gctx, keyid, &key, is_sec); |
err = gpgme_get_key (gctx, keyid, &key, is_sec); |
623 |
gpgme_release (gctx); |
gpgme_release (gctx); |
624 |
if (err) |
if (err) |
628 |
log_debug ("keycache update: keyid=%s %p\r\n", keyid, pub); |
log_debug ("keycache update: keyid=%s %p\r\n", keyid, pub); |
629 |
gpgme_key_release (fndkey); |
gpgme_key_release (fndkey); |
630 |
c->key = key; |
c->key = key; |
631 |
c->flags = 0; |
c->flags = KC_FLAG_UPD; |
632 |
if (is_sec && pub != NULL && |
if (is_sec && pub != NULL && |
633 |
!gpg_keycache_find_key (pub, keyid, 0, &fndkey)) { |
!gpg_keycache_find_key (pub, keyid, 0, &fndkey)) { |
634 |
log_debug ("keycache update: set public part %p\r\n", fndkey); |
log_debug ("keycache update: set public part %p\r\n", fndkey); |
635 |
c->pubpart->key = fndkey; |
c->pubpart->key = fndkey; |
636 |
} |
} |
637 |
|
/* XXX: this is also called for keys without a photo-id. */ |
638 |
|
keycache_reload_photo (ctx, keyid); |
639 |
} |
} |
640 |
else { |
else { |
641 |
log_debug ("keycache add: sync public part\r\n"); |
log_debug ("keycache add: sync public part\r\n"); |
651 |
c->gloflags.divert_to_card = c_new->gloflags.divert_to_card; |
c->gloflags.divert_to_card = c_new->gloflags.divert_to_card; |
652 |
} |
} |
653 |
} |
} |
654 |
|
if (c) |
655 |
|
c->flags = KC_FLAG_ADD; |
656 |
} |
} |
657 |
return 0; |
return 0; |
658 |
} |
} |
676 |
c = ctx->item; |
c = ctx->item; |
677 |
if (c->next == NULL) { |
if (c->next == NULL) { |
678 |
gpgme_key_release (itm->key); |
gpgme_key_release (itm->key); |
679 |
if (itm) |
safe_free (itm); |
|
free (itm); |
|
680 |
ctx->item = NULL; |
ctx->item = NULL; |
681 |
} |
} |
682 |
else { |
else { |
683 |
while (c && c->next != itm) |
for (; c != NULL; c = c->next) { |
684 |
c = c->next; |
if (c->next == itm) |
685 |
|
break; |
686 |
|
} |
687 |
|
assert (c != NULL); /* XXX: sometimes access violation. */ |
688 |
c->next = c->next->next; |
c->next = c->next->next; |
689 |
gpgme_key_release (itm->key); |
gpgme_key_release (itm->key); |
690 |
if (itm) |
safe_free (itm); |
|
free (itm); |
|
691 |
} |
} |
692 |
return 0; |
return 0; |
693 |
} |
} |
723 |
} |
} |
724 |
if (gpgme_err_code (err) == GPG_ERR_EOF) |
if (gpgme_err_code (err) == GPG_ERR_EOF) |
725 |
err = gpg_error (GPG_ERR_NO_ERROR); |
err = gpg_error (GPG_ERR_NO_ERROR); |
726 |
|
keycache_update_photos (ctx); |
727 |
|
keycache_decode_uids (ctx); |
728 |
/* XXX: make sure the progress dialog is closed. */ |
/* XXX: make sure the progress dialog is closed. */ |
729 |
gpgme_op_keylist_end (c); |
gpgme_op_keylist_end (c); |
730 |
gpgme_release (c); |
gpgme_release (c); |
752 |
} |
} |
753 |
|
|
754 |
|
|
755 |
|
static unsigned char* |
756 |
|
copy_uid_prefs (const unsigned char *prefs) |
757 |
|
{ |
758 |
|
unsigned char *p; |
759 |
|
size_t pos=0; |
760 |
|
|
761 |
|
while (prefs[pos] != 0) |
762 |
|
pos++; |
763 |
|
p = (unsigned char*)calloc (1, pos+1); |
764 |
|
if (!p) |
765 |
|
BUG (0); |
766 |
|
memcpy (p, prefs, pos); |
767 |
|
return p; |
768 |
|
} |
769 |
|
|
770 |
|
|
771 |
gpgme_error_t |
gpgme_error_t |
772 |
gpg_keycache_sync (gpg_keycache_t pub, gpg_keycache_t sec) |
gpg_keycache_sync (gpg_keycache_t pub, gpg_keycache_t sec) |
773 |
{ |
{ |
783 |
c_sec->gloflags.divert_to_card = c->gloflags.divert_to_card; |
c_sec->gloflags.divert_to_card = c->gloflags.divert_to_card; |
784 |
if (!c->gloflags.divert_to_card) |
if (!c->gloflags.divert_to_card) |
785 |
c->gloflags.divert_to_card = key_divert_to_card (key); |
c->gloflags.divert_to_card = key_divert_to_card (key); |
786 |
|
if (c_sec->sym_prefs) |
787 |
|
c->sym_prefs = copy_uid_prefs (c_sec->sym_prefs); |
788 |
c->pubpart = c_sec; |
c->pubpart = c_sec; |
789 |
c->pubpart->key = key; |
c->pubpart->key = key; |
790 |
} |
} |
833 |
*r_key = NULL; |
*r_key = NULL; |
834 |
return gpg_error (GPG_ERR_EOF); |
return gpg_error (GPG_ERR_EOF); |
835 |
} |
} |
836 |
|
if (ctx->tmp->flags != 0) |
837 |
|
ctx->tmp->flags = 0; /* reset the 'updated' status. */ |
838 |
|
/* it might be possible there is no public key. */ |
839 |
|
if (flags && ctx->tmp->pubpart == NULL) |
840 |
|
flags = 0; |
841 |
*r_key = flags? ctx->tmp->pubpart->key : ctx->tmp->key; |
*r_key = flags? ctx->tmp->pubpart->key : ctx->tmp->key; |
842 |
*c = ctx->tmp = ctx->tmp->next; |
*c = ctx->tmp; |
843 |
|
ctx->tmp = ctx->tmp->next; |
844 |
ctx->pos++; |
ctx->pos++; |
845 |
|
|
846 |
return 0; |
return 0; |
854 |
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) |
855 |
{ |
{ |
856 |
struct keycache_s *c=NULL; |
struct keycache_s *c=NULL; |
857 |
gpgme_error_t err = 0; |
gpgme_error_t err; |
858 |
|
|
859 |
err = keycache_next_key (ctx, flags, &c, r_key); |
err = keycache_next_key (ctx, flags, &c, r_key); |
860 |
return err; |
return err; |
861 |
} |
} |
862 |
|
|
863 |
|
gpgme_error_t |
864 |
|
gpg_keycache_next_key2 (gpg_keycache_t ctx, int flags, |
865 |
|
struct keycache_s **c, gpgme_key_t *r_key) |
866 |
|
{ |
867 |
|
return keycache_next_key (ctx, flags, c, r_key); |
868 |
|
} |
869 |
|
|
870 |
|
|
871 |
|
/* Search for a key with the pattern @pattern and mark |
872 |
|
this key as the default signing key if found. |
873 |
|
Return value: 0 on success. */ |
874 |
|
gpgme_error_t |
875 |
|
gpg_keycache_set_default_key (gpg_keycache_t ctx, |
876 |
|
const char *pattern) |
877 |
|
{ |
878 |
|
gpgme_error_t err; |
879 |
|
gpgme_key_t key; |
880 |
|
struct keycache_s *itm; |
881 |
|
|
882 |
|
err = gpg_keycache_find_key2 (ctx, pattern, 0, &key, &itm); |
883 |
|
if (err) |
884 |
|
return err; |
885 |
|
|
886 |
|
if (itm) |
887 |
|
itm->default_key = 1; |
888 |
|
return 0; |
889 |
|
} |
890 |
|
|
891 |
|
/* Return the default key from the cache. If no was |
892 |
|
marked before, NULL is returned in @r_key. |
893 |
|
Return value: 0 on success. */ |
894 |
|
gpgme_error_t |
895 |
|
gpg_keycache_get_default_key (gpg_keycache_t ctx, |
896 |
|
gpgme_key_t *r_key) |
897 |
|
{ |
898 |
|
struct keycache_s *itm; |
899 |
|
|
900 |
|
*r_key = NULL; |
901 |
|
for (itm = ctx->item; itm; itm = itm->next) { |
902 |
|
if (itm->default_key) { |
903 |
|
*r_key = itm->key; |
904 |
|
break; |
905 |
|
} |
906 |
|
} |
907 |
|
if (!*r_key) |
908 |
|
return gpgme_error (GPG_ERR_NOT_FOUND); |
909 |
|
return 0; |
910 |
|
} |
911 |
|
|
912 |
|
|
913 |
|
static gpgme_error_t |
914 |
|
decode_subpacket (const char *subpkt_data, int *type, |
915 |
|
char **out, WORD *outlen) |
916 |
|
{ |
917 |
|
char tmp[128], *p = tmp, *val; |
918 |
|
char *enc = NULL; |
919 |
|
size_t pos = 0, i=0; |
920 |
|
|
921 |
|
/* example: spk:24:1:21:http%3A//subkeys.pgp.de */ |
922 |
|
*outlen = 0; |
923 |
|
*out = NULL; |
924 |
|
|
925 |
|
if (strncmp (subpkt_data, "spk:", 4)) |
926 |
|
return gpg_error (GPG_ERR_NO_DATA); |
927 |
|
|
928 |
|
strncpy (tmp, subpkt_data, 62); |
929 |
|
val = strtok (tmp, ":"); |
930 |
|
while (val != NULL) { |
931 |
|
switch (pos++) { |
932 |
|
case 0: |
933 |
|
break; |
934 |
|
|
935 |
|
case 1: |
936 |
|
if (type) |
937 |
|
*type = atoi (val); |
938 |
|
break; |
939 |
|
|
940 |
|
case 2: |
941 |
|
break; |
942 |
|
|
943 |
|
case 3: |
944 |
|
*outlen = atoi (val); |
945 |
|
break; |
946 |
|
|
947 |
|
case 4: |
948 |
|
enc = strdup (val); |
949 |
|
break; |
950 |
|
} |
951 |
|
val = strtok (NULL, ":"); |
952 |
|
} |
953 |
|
if (!enc) |
954 |
|
return gpg_error (GPG_ERR_NO_DATA);; |
955 |
|
*out = (char*)calloc (1, strlen (enc)+1); |
956 |
|
if (!*out) |
957 |
|
BUG (0); |
958 |
|
for (pos = 0; pos < strlen (enc); pos++) { |
959 |
|
if (enc[pos] == '%' && enc[pos+1] == '%') |
960 |
|
(*out)[i++] = '%'; |
961 |
|
else if (enc[pos] == '%') { |
962 |
|
char temp[3]; |
963 |
|
temp[0] = enc[++pos]; |
964 |
|
temp[1] = enc[++pos]; |
965 |
|
temp[2] = 0; |
966 |
|
(*out)[i++] = (char)strtoul (temp, NULL, 16); |
967 |
|
} |
968 |
|
else |
969 |
|
(*out)[i++] = enc[pos]; |
970 |
|
} |
971 |
|
(*out)[i] = 0; |
972 |
|
safe_free (enc); |
973 |
|
return 0; |
974 |
|
} |
975 |
|
|
976 |
|
|
977 |
|
/* If the attribute given in @attr is not set in the |
978 |
|
key cache object, try to update it. */ |
979 |
|
gpgme_error_t |
980 |
|
gpg_keycache_update_attr (struct keycache_s *item, |
981 |
|
int attr, int force) |
982 |
|
{ |
983 |
|
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
984 |
|
char *val = NULL; |
985 |
|
WORD n = 0; |
986 |
|
|
987 |
|
switch (attr) { |
988 |
|
case KC_ATTR_PREFSYM: |
989 |
|
if (!force && item->sym_prefs) |
990 |
|
break; |
991 |
|
safe_free (item->sym_prefs); |
992 |
|
err = gpg_find_key_subpacket (item->key->subkeys->keyid+8, attr, &val); |
993 |
|
if (!err && val != NULL) |
994 |
|
err = decode_subpacket (val, NULL, (char**)&item->sym_prefs, &n); |
995 |
|
break; |
996 |
|
|
997 |
|
case KC_ATTR_PREFKSERV: |
998 |
|
if (!force && item->pref_keyserver) |
999 |
|
break; |
1000 |
|
safe_free (item->pref_keyserver); |
1001 |
|
err = gpg_find_key_subpacket (item->key->subkeys->keyid+8, attr, &val); |
1002 |
|
if (!err && val != NULL) |
1003 |
|
err = decode_subpacket (val, NULL, &item->pref_keyserver, &n); |
1004 |
|
break; |
1005 |
|
} |
1006 |
|
safe_free (val); |
1007 |
|
return err; |
1008 |
|
} |