/[winpt]/trunk/Src/wptKeyEdit.cpp
ViewVC logotype

Annotation of /trunk/Src/wptKeyEdit.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 207 - (hide annotations)
Fri Apr 28 10:28:24 2006 UTC (18 years, 10 months ago) by twoaday
File size: 21005 byte(s)
Bug fixes and cleanups.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26