1 |
/* keycache.c - Caching for the pub- and the secring |
/* keycache.c - Caching for the pub- and the secring |
2 |
* Copyright (C) 2001-2004 Timo Schulz |
* Copyright (C) 2001-2005 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of MyGPGME. |
* This file is part of MyGPGME. |
5 |
* |
* |
28 |
|
|
29 |
|
|
30 |
gpgme_error_t |
gpgme_error_t |
31 |
gpgme_keycache_new( gpgme_keycache_t *r_ctx ) |
gpgme_keycache_new (gpgme_keycache_t *r_ctx) |
32 |
{ |
{ |
33 |
gpgme_keycache_t ctx; |
gpgme_keycache_t ctx; |
34 |
|
|
35 |
if( !r_ctx ) |
if( !r_ctx ) |
36 |
return mk_error( Invalid_Value ); |
return mk_error (Invalid_Value); |
37 |
ctx = calloc( 1, sizeof *ctx ); |
ctx = calloc (1, sizeof *ctx); |
38 |
if( !ctx ) |
if (!ctx) |
39 |
return mk_error( Out_Of_Core ); |
return mk_error (Out_Of_Core); |
40 |
ctx->secret = 0; |
ctx->secret = 0; |
41 |
ctx->pos = 0; |
ctx->pos = 0; |
42 |
*r_ctx = ctx; |
*r_ctx = ctx; |
52 |
if (!ctx) |
if (!ctx) |
53 |
return; |
return; |
54 |
|
|
55 |
for( c = ctx->item; c; c = c2 ) { |
for (c = ctx->item; c; c = c2) { |
56 |
c2 = c->next; |
c2 = c->next; |
57 |
gpgme_key_release( c->key ); |
gpgme_key_release (c->key); |
58 |
c->key = NULL; |
c->key = NULL; |
59 |
safe_free( c ); |
safe_free (c); |
60 |
} |
} |
61 |
safe_free( ctx ); |
safe_free( ctx ); |
62 |
} /* gpgme_keycache_release */ |
} /* gpgme_keycache_release */ |
75 |
} |
} |
76 |
|
|
77 |
|
|
78 |
int |
gpgme_error_t |
79 |
gpgme_keycache_add_key( gpgme_keycache_t ctx, gpgme_key_t key ) |
gpgme_keycache_add_key (gpgme_keycache_t ctx, gpgme_key_t key, void **opaque) |
80 |
{ |
{ |
81 |
struct keycache_s * c, * n1; |
struct keycache_s * c, * n1; |
82 |
|
|
83 |
if( !ctx ) |
if (!ctx) |
84 |
return mk_error(Invalid_Value); |
return mk_error(Invalid_Value); |
85 |
|
|
86 |
c = calloc( 1, sizeof *c ); |
c = calloc (1, sizeof *c); |
87 |
if( !c ) |
if (!c) |
88 |
return mk_error( Out_Of_Core ); |
return mk_error (Out_Of_Core); |
89 |
c->key = key; |
c->key = key; |
90 |
if( !ctx->item ) |
if( !ctx->item ) |
91 |
ctx->item = c; |
ctx->item = c; |
94 |
; |
; |
95 |
n1->next = c; |
n1->next = c; |
96 |
} |
} |
97 |
|
if (opaque) |
98 |
|
*opaque = c; |
99 |
return 0; |
return 0; |
100 |
} /* gpgme_keycache_add_key */ |
} /* gpgme_keycache_add_key */ |
101 |
|
|
102 |
|
|
|
#define is_deleted_item(c) (((c)->flags & 0x01)) |
|
103 |
#define has_keyid_len(pattern) (\ |
#define has_keyid_len(pattern) (\ |
104 |
strlen (pattern) == 8 || strlen (pattern) == 10 || \ |
strlen (pattern) == 8 || strlen (pattern) == 10 || \ |
105 |
strlen (pattern) == 16 || strlen (pattern) == 18) |
strlen (pattern) == 16 || strlen (pattern) == 18) |
106 |
|
|
107 |
|
|
108 |
static gpgme_error_t |
gpgme_error_t |
109 |
keycache_find_key (gpgme_keycache_t ctx, const char * pattern, int flags, |
_gpgme_keycache_find_key2 (gpgme_keycache_t ctx, const char *pattern, int flags, |
110 |
gpgme_key_t * r_key, struct keycache_s ** r_item) |
gpgme_key_t *r_key, struct keycache_s **r_item) |
111 |
{ |
{ |
112 |
struct keycache_s * c; |
struct keycache_s *c; |
113 |
struct subkey_s * s; |
struct subkey_s *s; |
114 |
struct user_id_s * u; |
struct user_id_s *u; |
115 |
const char *kid; |
const char *kid; |
116 |
|
|
117 |
if (!ctx || !r_key) |
if (!ctx || !r_key) |
118 |
return mk_error (Invalid_Value); |
return mk_error (Invalid_Value); |
119 |
|
|
120 |
if (strstr (pattern, "0x")) |
if (strstr (pattern, "0x")) |
121 |
pattern += 2; |
pattern += 2; |
122 |
/* fixme: this is VERY slow, se we should use the GPGME original code */ |
/* XXX: this code is very slow, revamp it and use hash tables whenever |
123 |
|
it is possible. */ |
124 |
for (c = ctx->item; c; c = c->next) { |
for (c = ctx->item; c; c = c->next) { |
|
if (is_deleted_item (c)) |
|
|
continue; |
|
125 |
for (s = &c->key->keys; s; s = s->next) { |
for (s = &c->key->keys; s; s = s->next) { |
126 |
for (u = c->key->uids; u; u = u->next) { |
for (u = c->key->uids; u; u = u->next) { |
127 |
if (u->name && memistr (u->name, strlen (u->name), pattern)) { |
if (u->name && memistr (u->name, strlen (u->name), pattern)) { |
150 |
|
|
151 |
|
|
152 |
gpgme_error_t |
gpgme_error_t |
153 |
gpgme_keycache_find_key (gpgme_keycache_t ctx, const char * pattern, |
gpgme_keycache_find_key (gpgme_keycache_t ctx, const char *pattern, |
154 |
int flags, gpgme_key_t * r_key) |
int flags, gpgme_key_t *r_key) |
155 |
{ |
{ |
156 |
return keycache_find_key (ctx, pattern, flags, r_key, NULL); |
return _gpgme_keycache_find_key2 (ctx, pattern, flags, r_key, NULL); |
157 |
} /* gpgme_keycache_find_key */ |
} /* gpgme_keycache_find_key */ |
158 |
|
|
159 |
|
|
160 |
gpgme_error_t |
gpgme_error_t |
161 |
|
gpgme_keycache_update_key (gpgme_keycache_t ctx, int is_sec, |
162 |
|
void *opaque, const char *keyid) |
163 |
|
{ |
164 |
|
struct keycache_s *c = NULL; |
165 |
|
gpgme_key_t key=NULL, fndkey=NULL; |
166 |
|
gpgme_error_t err; |
167 |
|
gpgme_keycache_t pub = (gpgme_keycache_t)opaque; |
168 |
|
|
169 |
|
err = gpgme_op_keylist_getkey (is_sec, keyid, &key); |
170 |
|
if (err) |
171 |
|
return err; |
172 |
|
err = _gpgme_keycache_find_key2 (ctx, keyid, 0, &fndkey, &c); |
173 |
|
if (!err && c != NULL) { |
174 |
|
DEBUG2 ("keycache update: keyid=%s %p\r\n", keyid, pub); |
175 |
|
gpgme_key_release (fndkey); |
176 |
|
c->key = key; |
177 |
|
c->flags = 0; |
178 |
|
if (is_sec && pub != NULL && |
179 |
|
!gpgme_keycache_find_key (pub, keyid, 0, &fndkey)) { |
180 |
|
DEBUG1 ("keycache update: set public part %p\r\n", fndkey); |
181 |
|
c->pubpart = fndkey; |
182 |
|
} |
183 |
|
} |
184 |
|
else { |
185 |
|
if (is_sec && !_gpgme_keycache_find_key2 (pub, keyid, 0, &fndkey, &c)) { |
186 |
|
DEBUG0 ("keycache add: sync public part\r\n"); |
187 |
|
fndkey->gloflags.is_protected = c->key->gloflags.is_protected; |
188 |
|
fndkey->gloflags.divert_to_card = c->key->gloflags.divert_to_card; |
189 |
|
} |
190 |
|
gpgme_keycache_add_key (ctx, key, &c); |
191 |
|
if (c != NULL && is_sec) { |
192 |
|
DEBUG3 ("keycache add: keyid=%s %p %p\r\n", keyid, c, fndkey); |
193 |
|
c->pubpart = fndkey; |
194 |
|
} |
195 |
|
} |
196 |
|
return 0; |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
gpgme_error_t |
201 |
gpgme_keycache_delete_key (gpgme_keycache_t ctx, const char * pattern) |
gpgme_keycache_delete_key (gpgme_keycache_t ctx, const char * pattern) |
202 |
{ |
{ |
203 |
struct keycache_s * c = NULL; |
struct keycache_s *itm = NULL, *c; |
204 |
gpgme_key_t key; |
gpgme_key_t key; |
205 |
gpgme_error_t rc; |
gpgme_error_t rc; |
206 |
|
|
207 |
if (!ctx) |
if (!ctx) |
208 |
return mk_error (Invalid_Value); |
return mk_error (Invalid_Value); |
209 |
rc = keycache_find_key (ctx, pattern, 0, &key, &c); |
rc = _gpgme_keycache_find_key2 (ctx, pattern, 0, &key, &itm); |
210 |
if (!rc) |
if (rc) |
211 |
c->flags |= 1; |
return rc; |
212 |
return rc; |
|
213 |
|
c = ctx->item; |
214 |
|
if (c->next == NULL) { |
215 |
|
gpgme_key_release (itm->key); |
216 |
|
safe_free (itm); |
217 |
|
ctx->item = NULL; |
218 |
|
} |
219 |
|
else { |
220 |
|
while (c->next != itm) |
221 |
|
c = c->next; |
222 |
|
c->next = c->next->next; |
223 |
|
gpgme_key_release (itm->key); |
224 |
|
safe_free (itm); |
225 |
|
} |
226 |
|
return 0; |
227 |
} /* gpgme_keycache_delete_key */ |
} /* gpgme_keycache_delete_key */ |
228 |
|
|
229 |
|
|
230 |
gpgme_error_t |
gpgme_error_t |
231 |
gpgme_keycache_init( gpgme_keycache_t ctx, const char *pattern, int secret ) |
gpgme_keycache_init (gpgme_keycache_t ctx, const char *pattern, int secret) |
232 |
{ |
{ |
233 |
gpgme_error_t err; |
gpgme_error_t err; |
234 |
gpgme_ctx_t c; |
gpgme_ctx_t c; |
235 |
gpgme_key_t key; |
gpgme_key_t key; |
236 |
|
|
237 |
if (!ctx) |
if (!ctx) |
238 |
return mk_error( Invalid_Value ); |
return mk_error (Invalid_Value); |
239 |
|
|
240 |
err = gpgme_new (&c); |
err = gpgme_new (&c); |
241 |
if (err) |
if (err) |
242 |
return err; |
return err; |
243 |
if (ctx->cb) |
if (ctx->cb) { |
|
{ |
|
244 |
gpgme_control (c, GPGME_CTRL_CB_VAL, ctx->cb_value2); |
gpgme_control (c, GPGME_CTRL_CB_VAL, ctx->cb_value2); |
245 |
gpgme_set_progress_cb (c, ctx->cb, ctx->cb_value); |
gpgme_set_progress_cb (c, ctx->cb, ctx->cb_value); |
246 |
} |
} |
247 |
gpgme_control (c, GPGME_CTRL_LOGGING, 1); |
gpgme_control (c, GPGME_CTRL_LOGGING, 1); |
248 |
err = gpgme_op_keylist_start( c, pattern, secret ); |
err = gpgme_op_keylist_start (c, pattern, secret); |
249 |
while(!err) |
while(!err) { |
|
{ |
|
250 |
err = gpgme_op_keylist_next (c, &key); |
err = gpgme_op_keylist_next (c, &key); |
251 |
if (!err) |
if (!err) |
252 |
err = gpgme_keycache_add_key (ctx, key); |
err = gpgme_keycache_add_key (ctx, key, NULL); |
253 |
} |
} |
254 |
if (err == GPGME_EOF) |
if (err == GPGME_EOF) |
255 |
err = 0; |
err = 0; |
294 |
int |
int |
295 |
gpgme_keycache_count (gpgme_keycache_t ctx) |
gpgme_keycache_count (gpgme_keycache_t ctx) |
296 |
{ |
{ |
297 |
struct keycache_s * c; |
struct keycache_s *c; |
298 |
int count = 0; |
int count = 0; |
299 |
|
|
300 |
if (!ctx) |
if (!ctx) |
301 |
return 0; |
return 0; |
302 |
for (c = ctx->item; c; c = c->next) { |
for (c = ctx->item; c; c = c->next) |
|
if (is_deleted_item (c)) |
|
|
continue; |
|
303 |
count++; |
count++; |
|
} |
|
304 |
return count; |
return count; |
305 |
} /* gpgme_keycache_count */ |
} /* gpgme_keycache_count */ |
306 |
|
|
307 |
|
|
308 |
gpgme_error_t |
static gpgme_error_t |
309 |
gpgme_keycache_next_key (gpgme_keycache_t ctx, int flags, gpgme_key_t * r_key) |
keycache_next_key (gpgme_keycache_t ctx, int flags, |
310 |
|
struct keycache_s **c, gpgme_key_t * r_key) |
311 |
{ |
{ |
312 |
if (!ctx || !r_key) |
if (!ctx || !r_key) |
313 |
return mk_error (Invalid_Value); |
return mk_error (Invalid_Value); |
318 |
if (!ctx->tmp || !ctx->tmp->key) { |
if (!ctx->tmp || !ctx->tmp->key) { |
319 |
ctx->pos = 0; |
ctx->pos = 0; |
320 |
*r_key = NULL; |
*r_key = NULL; |
321 |
return mk_error (Invalid_Value); |
return mk_error (EOF); |
322 |
} |
} |
323 |
|
|
324 |
if (r_key) |
*r_key = flags? ctx->tmp->pubpart : ctx->tmp->key; |
325 |
*r_key = flags? ctx->tmp->pubpart : ctx->tmp->key; |
*c = ctx->tmp = ctx->tmp->next; |
|
|
|
|
ctx->tmp = ctx->tmp->next; |
|
326 |
ctx->pos++; |
ctx->pos++; |
327 |
|
|
328 |
return 0; |
return 0; |
329 |
} /* gpgme_keycache_next_key */ |
} /* gpgme_keycache_next_key */ |
330 |
|
|
331 |
|
|
332 |
|
gpgme_error_t |
333 |
|
gpgme_keycache_next_key (gpgme_keycache_t ctx, int flags, gpgme_key_t *r_key) |
334 |
|
{ |
335 |
|
struct keycache_s *c; |
336 |
|
gpgme_error_t err = 0; |
337 |
|
|
338 |
|
c = NULL; |
339 |
|
err = keycache_next_key (ctx, flags, &c, r_key); |
340 |
|
return err; |
341 |
|
} |