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