1 |
werner |
36 |
/* wptKeyEdit.cpp - GPG key edit abstraction |
2 |
twoaday |
170 |
* Copyright (C) 2005, 2006 Timo Schulz |
3 |
werner |
36 |
* Copyright (C) 2005 g10 Code GmbH |
4 |
|
|
* |
5 |
|
|
* This file is part of WinPT. |
6 |
|
|
* |
7 |
|
|
* WinPT is free software; you can redistribute it and/or |
8 |
|
|
* modify it under the terms of the GNU General Public License |
9 |
|
|
* as published by the Free Software Foundation; either version 2 |
10 |
|
|
* of the License, or (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* WinPT is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
|
|
* General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
#ifdef HAVE_CONFIG_H |
23 |
|
|
#include <config.h> |
24 |
|
|
#endif |
25 |
twoaday |
225 |
|
26 |
|
|
#include <windows.h> |
27 |
twoaday |
205 |
#include <time.h> |
28 |
werner |
36 |
|
29 |
|
|
#include "gpgme.h" |
30 |
|
|
#include "wptCommonCtl.h" |
31 |
|
|
#include "wptContext.h" |
32 |
|
|
#include "wptKeyEdit.h" |
33 |
|
|
#include "wptTypes.h" |
34 |
|
|
#include "wptW32API.h" |
35 |
|
|
#include "wptGPG.h" |
36 |
|
|
#include "wptErrors.h" |
37 |
twoaday |
205 |
#include "wptUTF8.h" |
38 |
werner |
36 |
|
39 |
|
|
|
40 |
|
|
/* Parse the colon status information of @line and store |
41 |
|
|
the information in @rev. |
42 |
|
|
Return value: 0 on success. */ |
43 |
|
|
static gpgme_error_t |
44 |
|
|
rev_key_colon_handler (gpg_desig_rev_t *rev, char *line) |
45 |
|
|
{ |
46 |
|
|
char *p, *pend; |
47 |
|
|
gpg_desig_rev_t r, t; |
48 |
|
|
int field = 0; |
49 |
|
|
|
50 |
|
|
if (!line || strlen (line) < 3) |
51 |
|
|
return gpg_error (GPG_ERR_EOF); |
52 |
|
|
if (strncmp (line, "rvk", 3)) |
53 |
|
|
return 0; /* skip this line. */ |
54 |
|
|
|
55 |
|
|
log_debug ("rev_key: line=%s\r\n", line); |
56 |
|
|
|
57 |
|
|
r = (gpg_desig_rev_t)calloc (1, sizeof *r); |
58 |
|
|
if (!r) |
59 |
|
|
return gpg_error (GPG_ERR_ENOMEM); |
60 |
|
|
if (!*rev) |
61 |
|
|
*rev = r; |
62 |
|
|
else { |
63 |
|
|
for (t=*rev; t->next; t=t->next) |
64 |
|
|
; |
65 |
|
|
t->next = r; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
p = strdup (line); |
69 |
|
|
if (!p) |
70 |
|
|
return gpg_error (GPG_ERR_ENOMEM); |
71 |
|
|
|
72 |
|
|
for (;;) { |
73 |
|
|
field++; |
74 |
|
|
pend = strsep (&p, ":"); |
75 |
|
|
if (pend == NULL) |
76 |
|
|
break; |
77 |
|
|
switch (field) { |
78 |
twoaday |
234 |
case 4: |
79 |
|
|
r->pubkey_algo = (gpgme_pubkey_algo_t)atol (pend); |
80 |
|
|
break; |
81 |
|
|
|
82 |
|
|
case 10: |
83 |
|
|
strncpy (r->fpr, pend, 40); |
84 |
|
|
r->fpr[40] = 0; |
85 |
|
|
break; |
86 |
werner |
36 |
} |
87 |
|
|
} |
88 |
|
|
if (p) |
89 |
|
|
free (p); |
90 |
|
|
return 0; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
|
94 |
|
|
/* Parse the colon data output of edit key from @line and |
95 |
|
|
store the information in the @inf context. |
96 |
|
|
Return value: 0 on success. */ |
97 |
|
|
static gpgme_error_t |
98 |
|
|
uid_inf_colon_handler (gpg_uid_info_t *inf, char *line) |
99 |
|
|
{ |
100 |
|
|
gpg_uid_info_t i, t; |
101 |
|
|
char *p, *pend; |
102 |
twoaday |
205 |
char *name; |
103 |
werner |
36 |
int field = 0, len = 0; |
104 |
|
|
|
105 |
|
|
if (!line || strlen (line) < 3 || strncmp (line, "uid", 3)) |
106 |
|
|
return gpg_error (GPG_ERR_EOF); |
107 |
|
|
|
108 |
|
|
i = (gpg_uid_info_t)calloc (1, sizeof *i); |
109 |
|
|
if (!i) |
110 |
|
|
return gpg_error (GPG_ERR_ENOMEM); |
111 |
|
|
if (!*inf) |
112 |
|
|
*inf = i; |
113 |
|
|
else { |
114 |
|
|
for (t=*inf; t->next; t=t->next) |
115 |
|
|
; |
116 |
|
|
t->next = i; |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
p = strdup (line); |
120 |
|
|
if (!p) |
121 |
|
|
return gpg_error (GPG_ERR_ENOMEM);; |
122 |
|
|
for (;;) { |
123 |
|
|
field++; |
124 |
|
|
pend = strsep (&p, ":"); |
125 |
|
|
if (pend == NULL) |
126 |
|
|
break; |
127 |
|
|
|
128 |
|
|
switch (field) { |
129 |
|
|
case 2: /* trust info */ |
130 |
|
|
break; |
131 |
|
|
|
132 |
|
|
case 10: /* user ID */ |
133 |
twoaday |
205 |
name = (char *)calloc (1, strlen (pend)+1); |
134 |
|
|
if (!name) |
135 |
werner |
36 |
return gpg_error (GPG_ERR_ENOMEM);; |
136 |
twoaday |
205 |
gpg_decode_c_string (pend, &name, strlen (pend)+ 1); |
137 |
|
|
i->name = utf8_to_native (name); |
138 |
|
|
safe_free (name); |
139 |
werner |
36 |
if (strchr (pend, '<') != NULL && strchr (pend, '>') != NULL) { |
140 |
|
|
int pos = strchr (i->name, '<')- i->name + 1; |
141 |
|
|
int end = strchr (i->name, '>') - i->name; |
142 |
|
|
i->email = (char*) calloc (1, end-pos+2); |
143 |
|
|
if (!i->email) |
144 |
|
|
return gpg_error (GPG_ERR_ENOMEM);; |
145 |
|
|
memcpy (i->email, i->name+pos, (end-pos)); |
146 |
|
|
} |
147 |
|
|
break; |
148 |
|
|
|
149 |
|
|
case 13: /* preferences */ |
150 |
|
|
if (strstr (pend, "mdc")) { |
151 |
twoaday |
234 |
const char *s = "no-ks-modify"; |
152 |
werner |
36 |
len = strlen (pend) - 4; /* ,mdc */ |
153 |
twoaday |
234 |
if (strstr (pend, s)) { |
154 |
werner |
36 |
i->flags.no_ks_modify = 1; |
155 |
twoaday |
234 |
len -= strlen (s)+1; /* ',' + s */ |
156 |
werner |
36 |
} |
157 |
|
|
i->prefs = (char*)calloc (1, len+1); |
158 |
|
|
if (!i->prefs) |
159 |
|
|
return gpg_error (GPG_ERR_ENOMEM); |
160 |
|
|
memcpy (i->prefs, pend, len); |
161 |
|
|
i->prefs[len] = '\0'; |
162 |
|
|
i->flags.mdc = 1; |
163 |
|
|
} |
164 |
|
|
else { |
165 |
|
|
i->prefs = strdup (pend); |
166 |
|
|
if (!i->prefs) |
167 |
|
|
return gpg_error (GPG_ERR_ENOMEM); |
168 |
|
|
i->flags.mdc = 0; |
169 |
|
|
} |
170 |
|
|
break; |
171 |
|
|
|
172 |
|
|
case 14: /* index/flags */ |
173 |
twoaday |
234 |
i->index = atoi (pend); |
174 |
werner |
36 |
if (strchr (pend, 'r')) |
175 |
|
|
i->flags.revoked = 1; |
176 |
|
|
if (strchr (pend, 'p')) |
177 |
|
|
i->flags.primary = 1; |
178 |
|
|
break; |
179 |
|
|
} |
180 |
|
|
} |
181 |
|
|
if (p) |
182 |
|
|
free (p); |
183 |
|
|
return 0; |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
|
187 |
|
|
/* Release the context in @inf. */ |
188 |
|
|
void |
189 |
|
|
gpg_uid_info_release (gpg_uid_info_t list) |
190 |
|
|
{ |
191 |
|
|
gpg_uid_info_t i; |
192 |
|
|
|
193 |
|
|
while (list) { |
194 |
|
|
i = list->next; |
195 |
|
|
if (list->name) { |
196 |
|
|
if (list->name) |
197 |
|
|
free (list->name); |
198 |
|
|
list->name = NULL; |
199 |
|
|
} |
200 |
|
|
if (list->prefs) { |
201 |
|
|
if (list->prefs) |
202 |
|
|
free (list->prefs); |
203 |
|
|
list->prefs = NULL; |
204 |
|
|
} |
205 |
|
|
free (list); |
206 |
|
|
list = i; |
207 |
|
|
} |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
|
211 |
|
|
/* Release the context in @rev. */ |
212 |
|
|
void |
213 |
|
|
gpg_desig_rev_release (gpg_desig_rev_t rev) |
214 |
|
|
{ |
215 |
|
|
gpg_desig_rev_t r; |
216 |
|
|
|
217 |
|
|
while (rev) { |
218 |
|
|
r = rev->next; |
219 |
|
|
free (rev); |
220 |
|
|
rev = r; |
221 |
|
|
} |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
|
225 |
|
|
static gpgme_error_t |
226 |
|
|
list2_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd) |
227 |
|
|
{ |
228 |
|
|
DWORD n; |
229 |
|
|
const char *s; |
230 |
|
|
|
231 |
|
|
if (!strcmp (key, "keyedit.prompt")) { |
232 |
|
|
s = "quit\n"; |
233 |
|
|
WriteFile ((HANDLE)fd, s, strlen (s), &n, NULL); |
234 |
|
|
} |
235 |
|
|
return 0; |
236 |
|
|
} |
237 |
|
|
|
238 |
twoaday |
170 |
|
239 |
werner |
36 |
/* Dummy handler to get the colon data and then quit. */ |
240 |
|
|
static gpgme_error_t |
241 |
|
|
list_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd) |
242 |
|
|
{ |
243 |
|
|
static int step=0; |
244 |
|
|
const char *s=""; |
245 |
|
|
DWORD n; |
246 |
|
|
|
247 |
|
|
if (!strcmp (key, "keyedit.prompt") && step == 0) { |
248 |
|
|
step = 1; |
249 |
|
|
s = "list\n"; |
250 |
|
|
WriteFile ((HANDLE)fd, s, strlen (s), &n, NULL); |
251 |
|
|
} |
252 |
|
|
else if (!strcmp (key, "keyedit.prompt") && step == 1) { |
253 |
|
|
step = 0; |
254 |
|
|
s = "quit\n"; |
255 |
|
|
WriteFile ((HANDLE)fd, s, strlen (s), &n, NULL); |
256 |
|
|
} |
257 |
|
|
|
258 |
|
|
return 0; |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
|
262 |
|
|
/* Return all designated revokers for this key. If no revoker |
263 |
|
|
was set, @r_rev is NULL. |
264 |
|
|
Return value: 0 on success. */ |
265 |
|
|
gpgme_error_t |
266 |
|
|
GpgKeyEdit::getDesignatedRevoker (gpg_desig_rev_t *r_rev) |
267 |
|
|
{ |
268 |
twoaday |
129 |
gpgme_data_t out = NULL; |
269 |
werner |
36 |
gpg_desig_rev_t rev = NULL; |
270 |
|
|
gpgme_error_t err; |
271 |
|
|
char buf[256]; |
272 |
|
|
|
273 |
|
|
if (!this->key) |
274 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
275 |
|
|
|
276 |
|
|
err = gpgme_data_new (&out); |
277 |
|
|
if (err) |
278 |
|
|
goto leave; |
279 |
|
|
|
280 |
|
|
err = gpgme_op_edit (ctx, key, list2_handler, NULL, out); |
281 |
|
|
if (err) |
282 |
|
|
goto leave; |
283 |
|
|
|
284 |
|
|
gpgme_data_rewind (out); |
285 |
|
|
while (gpg_data_readline (out, buf, sizeof (buf)-1) > 0) |
286 |
|
|
rev_key_colon_handler (&rev, buf); |
287 |
|
|
*r_rev = rev; |
288 |
|
|
|
289 |
|
|
leave: |
290 |
|
|
if (out) |
291 |
|
|
gpgme_data_release (out); |
292 |
|
|
if (err) { |
293 |
|
|
gpg_desig_rev_release (rev); |
294 |
|
|
*r_rev = NULL; |
295 |
|
|
} |
296 |
|
|
return err; |
297 |
|
|
} |
298 |
|
|
|
299 |
twoaday |
170 |
|
300 |
werner |
36 |
/* Retrieve all user ID information of the key set via setKey |
301 |
|
|
in @r_inf. The result also contains the user ID number which |
302 |
|
|
is needed to securely delete the user-ID. */ |
303 |
|
|
gpgme_error_t |
304 |
|
|
GpgKeyEdit::getUseridInfo (gpg_uid_info_t *r_uinf) |
305 |
|
|
{ |
306 |
|
|
gpgme_data_t out = NULL; |
307 |
|
|
gpgme_error_t err; |
308 |
|
|
gpg_uid_info_t inf = NULL; |
309 |
|
|
char buf[256]; |
310 |
|
|
|
311 |
|
|
if (!this->key) |
312 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
313 |
|
|
|
314 |
|
|
err = gpgme_data_new (&out); |
315 |
|
|
if (err) |
316 |
|
|
goto leave; |
317 |
|
|
|
318 |
|
|
err = gpgme_op_edit (ctx, key, list_handler, NULL, out); |
319 |
|
|
if (err) |
320 |
|
|
goto leave; |
321 |
|
|
|
322 |
|
|
gpgme_data_rewind (out); |
323 |
|
|
while (gpg_data_readline (out, buf, sizeof (buf) -1) > 0) |
324 |
|
|
uid_inf_colon_handler (&inf, buf); |
325 |
|
|
|
326 |
|
|
*r_uinf = inf; |
327 |
|
|
|
328 |
|
|
leave: |
329 |
|
|
if (out) |
330 |
|
|
gpgme_data_release (out); |
331 |
|
|
if (err) { |
332 |
|
|
gpg_uid_info_release (inf); |
333 |
|
|
*r_uinf = NULL; |
334 |
|
|
} |
335 |
|
|
return err; |
336 |
|
|
} |
337 |
|
|
|
338 |
|
|
|
339 |
twoaday |
170 |
/* Clear object. */ |
340 |
|
|
void |
341 |
|
|
GpgKeyEdit::clear (void) |
342 |
werner |
36 |
{ |
343 |
twoaday |
170 |
pass = NULL; |
344 |
werner |
36 |
name = NULL; |
345 |
|
|
cmt = NULL; |
346 |
|
|
email = NULL; |
347 |
twoaday |
170 |
type = 0; |
348 |
werner |
36 |
cnt = 0; |
349 |
|
|
cmd_sent = 0; |
350 |
|
|
resval = 0; |
351 |
twoaday |
170 |
uid_index = sig_index = key_index = -1; |
352 |
twoaday |
176 |
key_has_passwd = true; |
353 |
twoaday |
170 |
} |
354 |
|
|
|
355 |
|
|
|
356 |
|
|
/* Construct an object with the given key in @key. */ |
357 |
|
|
GpgKeyEdit::GpgKeyEdit (gpgme_key_t _key) |
358 |
|
|
{ |
359 |
|
|
clear (); |
360 |
|
|
this->key = _key; |
361 |
werner |
36 |
gpgme_new (&ctx); /* FIXME */ |
362 |
|
|
} |
363 |
|
|
|
364 |
|
|
/* Construct an object and fetch the key with the keyid @keyid. */ |
365 |
twoaday |
69 |
GpgKeyEdit::GpgKeyEdit (const char *_keyid) |
366 |
werner |
36 |
{ |
367 |
twoaday |
170 |
clear (); |
368 |
twoaday |
69 |
get_pubkey (_keyid, &this->key); |
369 |
twoaday |
170 |
gpgme_new (&ctx); /* FIXME */ |
370 |
werner |
36 |
} |
371 |
|
|
|
372 |
|
|
/* Delete the given object. */ |
373 |
|
|
GpgKeyEdit::~GpgKeyEdit (void) |
374 |
|
|
{ |
375 |
|
|
free_if_alloc (name); |
376 |
|
|
free_if_alloc (cmt); |
377 |
|
|
free_if_alloc (email); |
378 |
|
|
gpgme_release (ctx); |
379 |
|
|
} |
380 |
|
|
|
381 |
|
|
|
382 |
|
|
/* Reset the state of the object. */ |
383 |
|
|
void |
384 |
|
|
GpgKeyEdit::reset (void) |
385 |
|
|
{ |
386 |
|
|
cmd_sent = 0; |
387 |
twoaday |
129 |
cnt = 0; |
388 |
werner |
36 |
} |
389 |
|
|
|
390 |
|
|
|
391 |
|
|
/* Return true if type has a non-zero value. */ |
392 |
|
|
bool |
393 |
|
|
GpgKeyEdit::isValid (void) |
394 |
|
|
{ |
395 |
|
|
return type != 0; |
396 |
|
|
} |
397 |
|
|
|
398 |
|
|
|
399 |
|
|
/* Return the GPGME key. */ |
400 |
|
|
gpgme_key_t |
401 |
|
|
GpgKeyEdit::getKey (void) |
402 |
|
|
{ |
403 |
|
|
return key; |
404 |
|
|
} |
405 |
|
|
|
406 |
|
|
|
407 |
|
|
/* Set the GPGME callback to @cb. The hook value can be |
408 |
|
|
given in @cb_value. */ |
409 |
|
|
void |
410 |
|
|
GpgKeyEdit::setCallback (gpgme_progress_cb_t cb, void *cb_value) |
411 |
|
|
{ |
412 |
|
|
gpgme_set_progress_cb (ctx, cb, cb_value); |
413 |
|
|
} |
414 |
|
|
|
415 |
twoaday |
175 |
|
416 |
|
|
/* Clear the stored passphrase. */ |
417 |
|
|
void |
418 |
|
|
GpgKeyEdit::clearPassphrase (void) |
419 |
|
|
{ |
420 |
|
|
if (pass) |
421 |
|
|
pass = NULL; |
422 |
|
|
} |
423 |
|
|
|
424 |
|
|
|
425 |
twoaday |
176 |
|
426 |
|
|
/* Inidicate that a key is protected by a passphrase or not. */ |
427 |
|
|
void |
428 |
|
|
GpgKeyEdit::setNoPassphrase (bool val) |
429 |
|
|
{ |
430 |
|
|
key_has_passwd = !val; |
431 |
|
|
} |
432 |
|
|
|
433 |
werner |
36 |
/* Set the passphrase to @pass. */ |
434 |
|
|
void |
435 |
twoaday |
69 |
GpgKeyEdit::setPassphrase (const char *_pass) |
436 |
werner |
36 |
{ |
437 |
twoaday |
69 |
this->pass = _pass; |
438 |
werner |
36 |
} |
439 |
|
|
|
440 |
|
|
/* Set the current key to @key. */ |
441 |
|
|
void |
442 |
twoaday |
69 |
GpgKeyEdit::setKey (gpgme_key_t _key) |
443 |
werner |
36 |
{ |
444 |
twoaday |
69 |
this->key = _key; |
445 |
werner |
36 |
} |
446 |
|
|
|
447 |
|
|
/* Set the keyid of the destination key to @keyid. */ |
448 |
|
|
void |
449 |
twoaday |
69 |
GpgKeyEdit::setKeyID (const char *_keyid) |
450 |
werner |
36 |
{ |
451 |
twoaday |
69 |
if (!_keyid) |
452 |
werner |
36 |
return; |
453 |
twoaday |
69 |
get_pubkey (_keyid, &this->key); |
454 |
werner |
36 |
} |
455 |
|
|
|
456 |
|
|
|
457 |
|
|
/* Set the local user for the operation to @locusr. */ |
458 |
|
|
void |
459 |
|
|
GpgKeyEdit::setLocalUser (gpgme_key_t locusr) |
460 |
|
|
{ |
461 |
|
|
gpgme_signers_add (ctx, locusr); |
462 |
|
|
} |
463 |
|
|
|
464 |
|
|
/* Set the result of the operation to @val. */ |
465 |
|
|
void |
466 |
|
|
GpgKeyEdit::setResult (int val) |
467 |
|
|
{ |
468 |
|
|
resval |= val; |
469 |
|
|
} |
470 |
|
|
|
471 |
|
|
|
472 |
|
|
/* Return the result of the operation. */ |
473 |
|
|
int |
474 |
|
|
GpgKeyEdit::getResult(void) |
475 |
|
|
{ |
476 |
|
|
return resval; |
477 |
|
|
} |
478 |
|
|
|
479 |
|
|
|
480 |
twoaday |
176 |
/* Return the amount of days the key is valid. */ |
481 |
|
|
int |
482 |
|
|
GpgKeyEdit::getValidDays (void) |
483 |
|
|
{ |
484 |
|
|
return valid; |
485 |
|
|
} |
486 |
werner |
36 |
|
487 |
twoaday |
176 |
|
488 |
werner |
36 |
int |
489 |
|
|
GpgKeyEdit::getType (void) |
490 |
|
|
{ |
491 |
|
|
return type; |
492 |
|
|
} |
493 |
|
|
|
494 |
twoaday |
200 |
|
495 |
|
|
/* Add the notation data from @notation to the user ID |
496 |
|
|
with the index @_uid_idx. |
497 |
|
|
Return value: 0 on success. */ |
498 |
|
|
gpgme_error_t |
499 |
|
|
GpgKeyEdit::addNotation (int _uid_idx, const char *_notation) |
500 |
|
|
{ |
501 |
|
|
if (!key) |
502 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
503 |
|
|
if (key_has_passwd && !this->pass) |
504 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
505 |
|
|
|
506 |
|
|
type = GPG_EDITKEY_NOTATION; |
507 |
|
|
this->uid_index = _uid_idx; |
508 |
|
|
this->notation = (char*)_notation; |
509 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
510 |
|
|
} |
511 |
|
|
|
512 |
|
|
|
513 |
werner |
36 |
/* Sign the key stored in the object with the |
514 |
|
|
signing mode @mode and the signature class @sig_class. |
515 |
|
|
Return value: 0 on success. */ |
516 |
|
|
gpgme_error_t |
517 |
twoaday |
69 |
GpgKeyEdit::signKey (int mode, int _sig_class, const char *_exp_date) |
518 |
werner |
36 |
{ |
519 |
twoaday |
176 |
if (!this->key) |
520 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
521 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
522 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
523 |
werner |
36 |
|
524 |
|
|
type = mode; |
525 |
twoaday |
69 |
this->exp_date = _exp_date; |
526 |
|
|
this->sig_class = _sig_class; |
527 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
528 |
|
|
} |
529 |
|
|
|
530 |
twoaday |
170 |
|
531 |
|
|
/* Sign a single user-id with the index @_uid_index. |
532 |
|
|
All other parameters are equal to signKey(). |
533 |
|
|
Return value: 0 on success. */ |
534 |
|
|
gpgme_error_t |
535 |
|
|
GpgKeyEdit::signUserid (int _uid_idx, int mode, int _sig_class, |
536 |
|
|
const char *_exp_date) |
537 |
|
|
{ |
538 |
twoaday |
176 |
if (!this->key) |
539 |
twoaday |
170 |
return gpg_error (GPG_ERR_INV_OBJ); |
540 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
541 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
542 |
|
|
|
543 |
twoaday |
170 |
this->uid_index = _uid_idx; |
544 |
|
|
type = mode; |
545 |
|
|
this->exp_date = _exp_date; |
546 |
|
|
this->sig_class = _sig_class; |
547 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
548 |
|
|
} |
549 |
|
|
|
550 |
|
|
|
551 |
werner |
36 |
/* Set the ownertrust of the key stored in the object |
552 |
|
|
to the trust value @trust. |
553 |
|
|
Return value: 0 on success. */ |
554 |
|
|
gpgme_error_t |
555 |
|
|
GpgKeyEdit::setTrust (gpgme_validity_t trust) |
556 |
|
|
{ |
557 |
|
|
if (!this->key) |
558 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
559 |
|
|
|
560 |
|
|
type = GPG_EDITKEY_TRUST; |
561 |
|
|
this->trust_id = (int)trust; |
562 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
563 |
|
|
} |
564 |
|
|
|
565 |
|
|
/* Add a user ID to the given key with the @name as the |
566 |
|
|
name, @cmt as the comment (or NULL) and @email as the email. |
567 |
|
|
Return value: 0 on success. */ |
568 |
|
|
gpgme_error_t |
569 |
twoaday |
77 |
GpgKeyEdit::addUserid (const char *_name, const char *_cmt, const char *_email) |
570 |
werner |
36 |
{ |
571 |
twoaday |
176 |
if (!this->key) |
572 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
573 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
574 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
575 |
werner |
36 |
|
576 |
|
|
type = GPG_EDITKEY_ADDUID; |
577 |
|
|
free_if_alloc (this->name); |
578 |
twoaday |
77 |
this->name = m_strdup (_name); |
579 |
werner |
36 |
free_if_alloc (this->cmt); |
580 |
|
|
this->cmt = NULL; |
581 |
twoaday |
129 |
if (_cmt != NULL) |
582 |
twoaday |
77 |
this->cmt = m_strdup (_cmt); |
583 |
werner |
36 |
free_if_alloc (this->email); |
584 |
twoaday |
77 |
this->email = m_strdup (_email); |
585 |
werner |
36 |
if (!this->email || !this->name) |
586 |
|
|
BUG (NULL); |
587 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
588 |
|
|
} |
589 |
|
|
|
590 |
|
|
/* Delete the user-ID with the index @uid_index of the given key. |
591 |
|
|
Return value: 0 on success. */ |
592 |
|
|
gpgme_error_t |
593 |
twoaday |
69 |
GpgKeyEdit::delUserid (int _uid_index) |
594 |
werner |
36 |
{ |
595 |
|
|
if (!this->key) |
596 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
597 |
|
|
|
598 |
|
|
type = GPG_EDITKEY_DELUID; |
599 |
twoaday |
69 |
this->uid_index = _uid_index; |
600 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
601 |
|
|
} |
602 |
|
|
|
603 |
|
|
/* Delete the subkey with the index @key_index. |
604 |
|
|
Return value: 0 on success. */ |
605 |
|
|
gpgme_error_t |
606 |
twoaday |
69 |
GpgKeyEdit::delKey (int _key_index) |
607 |
werner |
36 |
{ |
608 |
|
|
if (!this->key) |
609 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
610 |
|
|
|
611 |
|
|
type = GPG_EDITKEY_DELKEY; |
612 |
twoaday |
69 |
this->key_index = _key_index; |
613 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
614 |
|
|
} |
615 |
|
|
|
616 |
|
|
/* Add a new subkey to the given key. |
617 |
|
|
The new key will have @pubkey_algo as the algorithm |
618 |
|
|
and a size of @pubkey_size bits. If valid > 0, the |
619 |
|
|
key expires in @valid days. |
620 |
|
|
Return value: 0 on success. */ |
621 |
|
|
gpgme_error_t |
622 |
twoaday |
77 |
GpgKeyEdit::addSubkey (gpgme_pubkey_algo_t _pubkey_algo, |
623 |
|
|
unsigned int _pubkey_size, long _valid) |
624 |
werner |
36 |
{ |
625 |
twoaday |
176 |
if (!this->key) |
626 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
627 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
628 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
629 |
werner |
36 |
|
630 |
|
|
type = GPG_EDITKEY_ADDKEY; |
631 |
twoaday |
77 |
this->pubkey_algo = _pubkey_algo; |
632 |
|
|
this->pubkey_size = _pubkey_size; |
633 |
|
|
this->valid = _valid; |
634 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
635 |
|
|
} |
636 |
|
|
|
637 |
|
|
/* Change the passphrase of the given key to @new_pass. |
638 |
|
|
If allow_empty != 0, it is allowed to provide an empty passphrase. |
639 |
|
|
Return value: 0 on success. */ |
640 |
|
|
gpgme_error_t |
641 |
twoaday |
69 |
GpgKeyEdit::changePassphrase (const char *_new_pass, int allow_empty) |
642 |
werner |
36 |
{ |
643 |
twoaday |
176 |
if (!this->key) |
644 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
645 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
646 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
647 |
werner |
36 |
|
648 |
|
|
type = GPG_EDITKEY_PASSWD; |
649 |
twoaday |
69 |
this->new_pass = _new_pass; |
650 |
werner |
36 |
this->flags = allow_empty? 1 : 0; |
651 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
652 |
|
|
} |
653 |
|
|
|
654 |
|
|
/* Set the primary user-ID of the given key to user-ID with |
655 |
|
|
the index @uid_index. |
656 |
|
|
Return value: 0 on success. */ |
657 |
|
|
gpgme_error_t |
658 |
twoaday |
69 |
GpgKeyEdit::setPrimaryUserid (int _uid_index) |
659 |
werner |
36 |
{ |
660 |
twoaday |
176 |
if (!this->key) |
661 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
662 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
663 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
664 |
werner |
36 |
|
665 |
|
|
type = GPG_EDITKEY_PRIMARY; |
666 |
twoaday |
69 |
this->uid_index = _uid_index; |
667 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
668 |
|
|
} |
669 |
|
|
|
670 |
|
|
/* Set the expire date of the subkey with the index @key_index. |
671 |
|
|
@exp_timestamp is used to calculate the days the key is valid. |
672 |
|
|
if @exp_days is true, exp_timestamp is already converted to days. |
673 |
|
|
Return value: 0 on success. */ |
674 |
|
|
gpgme_error_t |
675 |
twoaday |
129 |
GpgKeyEdit::setKeyExpireDate (int _key_index, long exp_timestamp, |
676 |
|
|
bool exp_days) |
677 |
werner |
36 |
{ |
678 |
twoaday |
176 |
if (!this->key) |
679 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
680 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
681 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
682 |
werner |
36 |
if (!exp_days && exp_timestamp > 0 && exp_timestamp < time (NULL)) |
683 |
|
|
return gpg_error (GPG_ERR_INV_ARG); |
684 |
|
|
|
685 |
|
|
type = GPG_EDITKEY_EXPIRE; |
686 |
|
|
if (!exp_days && exp_timestamp > 0) { |
687 |
|
|
valid = exp_timestamp - time (NULL); |
688 |
|
|
valid /= 86400; |
689 |
|
|
} |
690 |
|
|
else |
691 |
|
|
valid = exp_timestamp; |
692 |
twoaday |
69 |
this->key_index = _key_index; |
693 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
694 |
|
|
} |
695 |
|
|
|
696 |
|
|
/* Revoke the userid given by the index @uid_index. |
697 |
|
|
Return value: 0 on success. */ |
698 |
|
|
gpgme_error_t |
699 |
twoaday |
69 |
GpgKeyEdit::revokeUserid (int _uid_index) |
700 |
werner |
36 |
{ |
701 |
twoaday |
176 |
if (!this->key) |
702 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
703 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
704 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
705 |
werner |
36 |
|
706 |
|
|
type = GPG_EDITKEY_REVUID; |
707 |
twoaday |
69 |
this->uid_index = _uid_index; |
708 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
709 |
|
|
} |
710 |
|
|
|
711 |
|
|
|
712 |
|
|
/* Revoke a signature on the user-ID with the index @uid_index |
713 |
|
|
and the signature index @sig_index. |
714 |
|
|
Return value: 0 on success. */ |
715 |
|
|
gpgme_error_t |
716 |
twoaday |
69 |
GpgKeyEdit::revokeSignature (int _uid_index, int _sig_index) |
717 |
werner |
36 |
{ |
718 |
twoaday |
176 |
if (!this->key) |
719 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
720 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
721 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
722 |
werner |
36 |
|
723 |
|
|
type = GPG_EDITKEY_REVSIG; |
724 |
twoaday |
69 |
this->uid_index = _uid_index; |
725 |
|
|
this->sig_index = _sig_index; |
726 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
727 |
|
|
} |
728 |
|
|
|
729 |
|
|
|
730 |
|
|
/* Revoke the subkey with the index @key_index. Optionally |
731 |
|
|
a reason can be provided in @reason with a text to describe |
732 |
|
|
more details in @cmt. |
733 |
|
|
Return value: 0 on success. */ |
734 |
|
|
gpgme_error_t |
735 |
twoaday |
69 |
GpgKeyEdit::revokeSubkey (int _key_index, int _reason, const char *_cmt) |
736 |
werner |
36 |
{ |
737 |
twoaday |
176 |
if (!this->key) |
738 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
739 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
740 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
741 |
werner |
36 |
|
742 |
|
|
type = GPG_EDITKEY_REVKEY; |
743 |
twoaday |
69 |
this->key_index = _key_index; |
744 |
|
|
this->reason = _reason; |
745 |
werner |
36 |
free_if_alloc (this->cmt); |
746 |
|
|
this->cmt = NULL; |
747 |
twoaday |
69 |
if (_cmt) |
748 |
|
|
this->cmt = m_strdup (_cmt); |
749 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
750 |
|
|
} |
751 |
|
|
|
752 |
|
|
|
753 |
|
|
/* Add a designated revoker to the key. @uid stores |
754 |
|
|
the user-ID of the key who is allowed to be a |
755 |
|
|
designated revoker. |
756 |
|
|
Return value: 0 on success. */ |
757 |
|
|
gpgme_error_t |
758 |
|
|
GpgKeyEdit::addDesignatedRevoker (const char *uid) |
759 |
|
|
{ |
760 |
twoaday |
176 |
if (!this->key) |
761 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
762 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
763 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
764 |
werner |
36 |
|
765 |
|
|
type = GPG_EDITKEY_ADDREV; |
766 |
|
|
free_if_alloc (this->name); |
767 |
|
|
this->name = m_strdup (uid); |
768 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
769 |
|
|
} |
770 |
|
|
|
771 |
|
|
/* Add a photo-ID to the key. The JPG image is given |
772 |
|
|
in the file with the name @jpg_file. |
773 |
|
|
Return value: 0 on success. */ |
774 |
|
|
gpgme_error_t |
775 |
|
|
GpgKeyEdit::addPhotoid (const char *jpg_file) |
776 |
|
|
{ |
777 |
twoaday |
176 |
if (!this->key) |
778 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
779 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
780 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
781 |
werner |
36 |
|
782 |
|
|
type = GPG_EDITKEY_ADDPHOTO; |
783 |
|
|
this->url = jpg_file; |
784 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
785 |
|
|
} |
786 |
|
|
|
787 |
|
|
/* Enable the given key. */ |
788 |
|
|
gpgme_error_t |
789 |
|
|
GpgKeyEdit::enable (void) |
790 |
|
|
{ |
791 |
|
|
if (!this->key) |
792 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
793 |
|
|
type = GPG_EDITKEY_ENABLE; |
794 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
795 |
|
|
} |
796 |
|
|
|
797 |
|
|
/* Disable the given key. */ |
798 |
|
|
gpgme_error_t |
799 |
|
|
GpgKeyEdit::disable (void) |
800 |
|
|
{ |
801 |
|
|
if (!this->key) |
802 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
803 |
|
|
type = GPG_EDITKEY_DISABLE; |
804 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
805 |
|
|
} |
806 |
|
|
|
807 |
|
|
|
808 |
twoaday |
207 |
/* Remove unusable parts and all signatures from a key. */ |
809 |
|
|
gpgme_error_t |
810 |
|
|
GpgKeyEdit::minimizeKey (void) |
811 |
|
|
{ |
812 |
|
|
if (!this->key) |
813 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
814 |
|
|
type = GPG_EDITKEY_MINIMIZE; |
815 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
816 |
|
|
} |
817 |
|
|
|
818 |
|
|
|
819 |
twoaday |
119 |
/* Remove unusable parts from a key. */ |
820 |
|
|
gpgme_error_t |
821 |
|
|
GpgKeyEdit::cleanKey (void) |
822 |
|
|
{ |
823 |
|
|
if (!this->key) |
824 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
825 |
|
|
type = GPG_EDITKEY_CLEAN; |
826 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
827 |
|
|
} |
828 |
|
|
|
829 |
|
|
|
830 |
werner |
36 |
/* Update the user-ID preferences of the user-ID with the |
831 |
|
|
index @uid_index to the prefs given in @new_prefs. |
832 |
|
|
Return value: 0 on success. */ |
833 |
|
|
gpgme_error_t |
834 |
twoaday |
234 |
GpgKeyEdit::setUseridPreferences (int _uid_index, const char *_new_prefs) |
835 |
werner |
36 |
{ |
836 |
twoaday |
176 |
if (!this->key) |
837 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
838 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
839 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
840 |
twoaday |
234 |
type = GPG_EDITKEY_SETPREF; |
841 |
|
|
this->uid_index = _uid_index; |
842 |
|
|
this->new_prefs = _new_prefs; |
843 |
|
|
return gpg_editkey (this->ctx, this->key, this); |
844 |
werner |
36 |
} |
845 |
|
|
|
846 |
|
|
|
847 |
|
|
/* Delete a signature from the user-ID with the index @uid_index. |
848 |
|
|
The index of the signature is given in @sig_index. |
849 |
|
|
Return value: 0 on success. */ |
850 |
|
|
gpgme_error_t |
851 |
twoaday |
69 |
GpgKeyEdit::delUseridSignature (int _uid_index, int _sig_index) |
852 |
werner |
36 |
{ |
853 |
|
|
if (!this->key) |
854 |
|
|
return gpg_error (GPG_ERR_INV_OBJ); |
855 |
|
|
type = GPG_EDITKEY_DELSIG; |
856 |
twoaday |
69 |
this->uid_index = _uid_index; |
857 |
|
|
this->sig_index = _sig_index; |
858 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
859 |
|
|
} |
860 |
|
|
|
861 |
|
|
/* Set the preferred keyserver for the given key to @url. |
862 |
twoaday |
181 |
If @_uid_index is -1, set the keyserver for all user-ids. |
863 |
werner |
36 |
Return value: 0 on success. */ |
864 |
|
|
gpgme_error_t |
865 |
twoaday |
69 |
GpgKeyEdit::setPreferredKeyserver (int _uid_index, const char *_url) |
866 |
werner |
36 |
{ |
867 |
twoaday |
176 |
if (!this->key) |
868 |
werner |
36 |
return gpg_error (GPG_ERR_INV_OBJ); |
869 |
twoaday |
176 |
if (key_has_passwd && !this->pass) |
870 |
|
|
return gpg_error (GPG_ERR_INV_PASSPHRASE); |
871 |
|
|
if (!_url) |
872 |
werner |
36 |
return gpg_error (GPG_ERR_INV_ARG); |
873 |
twoaday |
176 |
|
874 |
werner |
36 |
type = GPG_EDITKEY_KEYSERV; |
875 |
twoaday |
69 |
this->url = _url; |
876 |
|
|
this->uid_index = _uid_index; |
877 |
werner |
36 |
return gpg_editkey (this->ctx, this->key, this); |
878 |
|
|
} |
879 |
twoaday |
170 |
|
880 |
|
|
|
881 |
|
|
/* Return the saved user-id index. */ |
882 |
|
|
int |
883 |
|
|
GpgKeyEdit::getUseridIndex (void) |
884 |
|
|
{ |
885 |
|
|
return uid_index; |
886 |
|
|
} |
887 |
|
|
|
888 |
|
|
|
889 |
|
|
/* Return the saved key index. */ |
890 |
|
|
int |
891 |
|
|
GpgKeyEdit::getKeyIndex (void) |
892 |
|
|
{ |
893 |
|
|
return key_index; |
894 |
|
|
} |
895 |
|
|
|
896 |
|
|
|
897 |
|
|
/* Return the saved sig index. */ |
898 |
|
|
int |
899 |
|
|
GpgKeyEdit::getSigIndex (void) |
900 |
|
|
{ |
901 |
|
|
return sig_index; |
902 |
|
|
} |