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

Diff of /trunk/Src/wptKeyEdit.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.26  
changed lines
  Added in v.119

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26