1 |
twoaday |
2 |
/* keycache.c - Caching for the pub- and the secring |
2 |
|
|
* Copyright (C) 2001-2004 Timo Schulz |
3 |
|
|
* |
4 |
|
|
* This file is part of MyGPGME. |
5 |
|
|
* |
6 |
|
|
* MyGPGME is free software; you can redistribute it and/or modify |
7 |
|
|
* 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 |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* MyGPGME is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include <stdio.h> |
22 |
|
|
#include <string.h> |
23 |
|
|
|
24 |
|
|
#include "context.h" |
25 |
|
|
#include "ops.h" |
26 |
|
|
#include "key.h" |
27 |
|
|
#include "util.h" |
28 |
|
|
|
29 |
|
|
|
30 |
|
|
gpgme_error_t |
31 |
|
|
gpgme_keycache_new( gpgme_keycache_t *r_ctx ) |
32 |
|
|
{ |
33 |
|
|
gpgme_keycache_t ctx; |
34 |
|
|
|
35 |
|
|
if( !r_ctx ) |
36 |
|
|
return mk_error( Invalid_Value ); |
37 |
|
|
ctx = calloc( 1, sizeof *ctx ); |
38 |
|
|
if( !ctx ) |
39 |
|
|
return mk_error( Out_Of_Core ); |
40 |
|
|
ctx->secret = 0; |
41 |
|
|
ctx->pos = 0; |
42 |
|
|
*r_ctx = ctx; |
43 |
|
|
return 0; |
44 |
|
|
} /* gpgme_keycache_new */ |
45 |
|
|
|
46 |
|
|
|
47 |
|
|
void |
48 |
|
|
gpgme_keycache_release (gpgme_keycache_t ctx) |
49 |
|
|
{ |
50 |
|
|
struct keycache_s * c, * c2; |
51 |
|
|
|
52 |
|
|
if (!ctx) |
53 |
|
|
return; |
54 |
|
|
|
55 |
|
|
for( c = ctx->item; c; c = c2 ) { |
56 |
|
|
c2 = c->next; |
57 |
|
|
gpgme_key_release( c->key ); |
58 |
|
|
c->key = NULL; |
59 |
|
|
safe_free( c ); |
60 |
|
|
} |
61 |
|
|
safe_free( ctx ); |
62 |
|
|
} /* gpgme_keycache_release */ |
63 |
|
|
|
64 |
|
|
|
65 |
|
|
void |
66 |
|
|
gpgme_keycache_set_cb (gpgme_keycache_t ctx, |
67 |
|
|
void (*cb)(void *, const char *, int, unsigned, unsigned), |
68 |
|
|
void * cb_value1, int cb_value2) |
69 |
|
|
{ |
70 |
|
|
if (!ctx) |
71 |
|
|
return; |
72 |
|
|
ctx->cb = cb; |
73 |
|
|
ctx->cb_value = cb_value1; |
74 |
|
|
ctx->cb_value2 = cb_value2; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
|
78 |
|
|
int |
79 |
|
|
gpgme_keycache_add_key( gpgme_keycache_t ctx, gpgme_key_t key ) |
80 |
|
|
{ |
81 |
|
|
struct keycache_s * c, * n1; |
82 |
|
|
|
83 |
|
|
if( !ctx ) |
84 |
|
|
return mk_error(Invalid_Value); |
85 |
|
|
|
86 |
|
|
c = calloc( 1, sizeof *c ); |
87 |
|
|
if( !c ) |
88 |
|
|
return mk_error( Out_Of_Core ); |
89 |
|
|
c->key = key; |
90 |
|
|
if( !ctx->item ) |
91 |
|
|
ctx->item = c; |
92 |
|
|
else { |
93 |
|
|
for( n1 = ctx->item; n1 && n1->next; n1 = n1->next ) |
94 |
|
|
; |
95 |
|
|
n1->next = c; |
96 |
|
|
} |
97 |
|
|
return 0; |
98 |
|
|
} /* gpgme_keycache_add_key */ |
99 |
|
|
|
100 |
|
|
|
101 |
|
|
#define is_deleted_item(c) (((c)->flags & 0x01)) |
102 |
|
|
#define has_keyid_len(pattern) (\ |
103 |
|
|
strlen (pattern) == 8 || strlen (pattern) == 10 || \ |
104 |
|
|
strlen (pattern) == 16 || strlen (pattern) == 18) |
105 |
|
|
|
106 |
|
|
|
107 |
|
|
static gpgme_error_t |
108 |
|
|
keycache_find_key (gpgme_keycache_t ctx, const char * pattern, int flags, |
109 |
|
|
gpgme_key_t * r_key, struct keycache_s ** r_item) |
110 |
|
|
{ |
111 |
|
|
struct keycache_s * c; |
112 |
|
|
struct subkey_s * s; |
113 |
|
|
struct user_id_s * u; |
114 |
|
|
const char *kid; |
115 |
|
|
|
116 |
|
|
if (!ctx || !r_key) |
117 |
|
|
return mk_error (Invalid_Value); |
118 |
|
|
|
119 |
|
|
if (strstr (pattern, "0x")) |
120 |
|
|
pattern += 2; |
121 |
|
|
/* fixme: this is VERY slow, se we should use the GPGME original code */ |
122 |
|
|
for (c = ctx->item; c; c = c->next) { |
123 |
|
|
if (is_deleted_item (c)) |
124 |
|
|
continue; |
125 |
|
|
for (s = &c->key->keys; s; s = s->next) { |
126 |
|
|
for (u = c->key->uids; u; u = u->next) { |
127 |
|
|
if (u->name && memistr (u->name, strlen (u->name), pattern)) { |
128 |
|
|
if (r_item) |
129 |
|
|
*r_item = c; |
130 |
|
|
*r_key = flags? c->pubpart : c->key; |
131 |
|
|
return 0; |
132 |
|
|
} |
133 |
|
|
} |
134 |
|
|
if (has_keyid_len (pattern)) |
135 |
|
|
kid = s->keyid; |
136 |
|
|
else |
137 |
|
|
kid = s->fingerprint; |
138 |
|
|
|
139 |
|
|
if (kid && memistr (kid, strlen (kid), pattern)) { |
140 |
|
|
if (r_item) |
141 |
|
|
*r_item = c; |
142 |
|
|
*r_key = flags? c->pubpart : c->key; |
143 |
|
|
return 0; |
144 |
|
|
} |
145 |
|
|
} |
146 |
|
|
} |
147 |
|
|
*r_key = NULL; |
148 |
|
|
return mk_error (General_Error); |
149 |
|
|
} /* keycache_find_key */ |
150 |
|
|
|
151 |
|
|
|
152 |
|
|
gpgme_error_t |
153 |
|
|
gpgme_keycache_find_key (gpgme_keycache_t ctx, const char * pattern, |
154 |
|
|
int flags, gpgme_key_t * r_key) |
155 |
|
|
{ |
156 |
|
|
return keycache_find_key (ctx, pattern, flags, r_key, NULL); |
157 |
|
|
} /* gpgme_keycache_find_key */ |
158 |
|
|
|
159 |
|
|
|
160 |
|
|
gpgme_error_t |
161 |
|
|
gpgme_keycache_delete_key (gpgme_keycache_t ctx, const char * pattern) |
162 |
|
|
{ |
163 |
|
|
struct keycache_s * c = NULL; |
164 |
|
|
gpgme_key_t key; |
165 |
|
|
gpgme_error_t rc; |
166 |
|
|
|
167 |
|
|
if (!ctx) |
168 |
|
|
return mk_error (Invalid_Value); |
169 |
|
|
rc = keycache_find_key (ctx, pattern, 0, &key, &c); |
170 |
|
|
if (!rc) |
171 |
|
|
c->flags |= 1; |
172 |
|
|
return rc; |
173 |
|
|
} /* gpgme_keycache_delete_key */ |
174 |
|
|
|
175 |
|
|
|
176 |
|
|
gpgme_error_t |
177 |
|
|
gpgme_keycache_init( gpgme_keycache_t ctx, const char *pattern, int secret ) |
178 |
|
|
{ |
179 |
|
|
gpgme_error_t err; |
180 |
|
|
gpgme_ctx_t c; |
181 |
|
|
gpgme_key_t key; |
182 |
|
|
|
183 |
|
|
if (!ctx) |
184 |
|
|
return mk_error( Invalid_Value ); |
185 |
|
|
|
186 |
|
|
err = gpgme_new (&c); |
187 |
|
|
if (err) |
188 |
|
|
return err; |
189 |
|
|
if (ctx->cb) |
190 |
|
|
{ |
191 |
|
|
gpgme_control (c, GPGME_CTRL_CB_VAL, ctx->cb_value2); |
192 |
|
|
gpgme_set_progress_cb (c, ctx->cb, ctx->cb_value); |
193 |
|
|
} |
194 |
|
|
gpgme_control (c, GPGME_CTRL_LOGGING, 1); |
195 |
|
|
err = gpgme_op_keylist_start( c, pattern, secret ); |
196 |
|
|
while(!err) |
197 |
|
|
{ |
198 |
|
|
err = gpgme_op_keylist_next (c, &key); |
199 |
|
|
if (!err) |
200 |
|
|
err = gpgme_keycache_add_key (ctx, key); |
201 |
|
|
} |
202 |
|
|
if (err == GPGME_EOF) |
203 |
|
|
err = 0; |
204 |
|
|
if (gpgme_get_process_rc (c)) |
205 |
|
|
err = gpgme_check_logging (c); |
206 |
|
|
gpgme_release (c); |
207 |
|
|
return err; |
208 |
|
|
} /* gpgme_keycache_init */ |
209 |
|
|
|
210 |
|
|
|
211 |
|
|
gpgme_error_t |
212 |
|
|
gpgme_keycache_sync (gpgme_keycache_t pub, gpgme_keycache_t sec) |
213 |
|
|
{ |
214 |
|
|
struct keycache_s * c; |
215 |
|
|
const char * s; |
216 |
|
|
gpgme_key_t key; |
217 |
|
|
|
218 |
|
|
if (!pub || !sec) |
219 |
|
|
return mk_error (Invalid_Value); |
220 |
|
|
/* The GPG secret key listing does not contain much information |
221 |
|
|
so we add some information to the public key cache. */ |
222 |
|
|
for (c=sec->item; c; c=c->next) { |
223 |
|
|
s = gpgme_key_get_string_attr (c->key, GPGME_ATTR_KEYID, NULL, 0); |
224 |
|
|
if (!gpgme_keycache_find_key (pub, s, 0, &key)) { |
225 |
|
|
key->gloflags.is_protected = c->key->gloflags.is_protected; |
226 |
|
|
key->gloflags.divert_to_card = c->key->gloflags.divert_to_card; |
227 |
|
|
c->pubpart = key; |
228 |
|
|
} |
229 |
|
|
} |
230 |
|
|
return 0; |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
|
234 |
|
|
void |
235 |
|
|
gpgme_keycache_rewind (gpgme_keycache_t ctx) |
236 |
|
|
{ |
237 |
|
|
if (ctx) |
238 |
|
|
ctx->pos = 0; |
239 |
|
|
} /* gpgme_keycache_rewind */ |
240 |
|
|
|
241 |
|
|
|
242 |
|
|
int |
243 |
|
|
gpgme_keycache_count (gpgme_keycache_t ctx) |
244 |
|
|
{ |
245 |
|
|
struct keycache_s * c; |
246 |
|
|
int count = 0; |
247 |
|
|
|
248 |
|
|
if (!ctx) |
249 |
|
|
return 0; |
250 |
|
|
for (c = ctx->item; c; c = c->next) { |
251 |
|
|
if (is_deleted_item (c)) |
252 |
|
|
continue; |
253 |
|
|
count++; |
254 |
|
|
} |
255 |
|
|
return count; |
256 |
|
|
} /* gpgme_keycache_count */ |
257 |
|
|
|
258 |
|
|
|
259 |
|
|
gpgme_error_t |
260 |
|
|
gpgme_keycache_next_key (gpgme_keycache_t ctx, int flags, gpgme_key_t * r_key) |
261 |
|
|
{ |
262 |
|
|
if (!ctx || !r_key) |
263 |
|
|
return mk_error (Invalid_Value); |
264 |
|
|
|
265 |
|
|
if (!ctx->pos) |
266 |
|
|
ctx->tmp = ctx->item; |
267 |
|
|
|
268 |
|
|
if (!ctx->tmp || !ctx->tmp->key) { |
269 |
|
|
ctx->pos = 0; |
270 |
|
|
*r_key = NULL; |
271 |
|
|
return mk_error (Invalid_Value); |
272 |
|
|
} |
273 |
|
|
|
274 |
twoaday |
5 |
*r_key = flags? ctx->tmp->pubpart : ctx->tmp->key; |
275 |
twoaday |
2 |
|
276 |
|
|
ctx->tmp = ctx->tmp->next; |
277 |
|
|
ctx->pos++; |
278 |
|
|
|
279 |
|
|
return 0; |
280 |
|
|
} /* gpgme_keycache_next_key */ |