/[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 69 by twoaday, Sat Nov 5 12:28:12 2005 UTC revision 205 by twoaday, Thu Apr 27 12:46:03 2006 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, 2006 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.
# Line 22  Line 22 
22  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
23  #include <config.h>  #include <config.h>
24  #endif  #endif
   
 #include <windows.h>  
25  #include <time.h>  #include <time.h>
26    #include <windows.h>
27    
28  #include "gpgme.h"  #include "gpgme.h"
29  #include "wptCommonCtl.h"  #include "wptCommonCtl.h"
# Line 34  Line 33 
33  #include "wptW32API.h"  #include "wptW32API.h"
34  #include "wptGPG.h"  #include "wptGPG.h"
35  #include "wptErrors.h"  #include "wptErrors.h"
36    #include "wptUTF8.h"
37    
38    
39  /* Parse the colon status information of @line and store  /* Parse the colon status information of @line and store
# Line 92  uid_inf_colon_handler (gpg_uid_info_t *i Line 92  uid_inf_colon_handler (gpg_uid_info_t *i
92  {  {
93      gpg_uid_info_t i, t;      gpg_uid_info_t i, t;
94      char *p, *pend;      char *p, *pend;
95        char *name;
96      int field = 0, len = 0;      int field = 0, len = 0;
97    
98      if (!line || strlen (line) < 3 || strncmp (line, "uid", 3))      if (!line || strlen (line) < 3 || strncmp (line, "uid", 3))
# Line 122  uid_inf_colon_handler (gpg_uid_info_t *i Line 123  uid_inf_colon_handler (gpg_uid_info_t *i
123              break;              break;
124    
125          case 10: /* user ID */          case 10: /* user ID */
126              i->name = (char *)calloc (1, strlen (pend)+1);              name = (char *)calloc (1, strlen (pend)+1);
127              if (!i->name)              if (!name)
128                  return gpg_error (GPG_ERR_ENOMEM);;                  return gpg_error (GPG_ERR_ENOMEM);;
129              gpg_decode_c_string (pend, &i->name, strlen (pend)+ 1);              gpg_decode_c_string (pend, &name, strlen (pend)+ 1);
130                i->name = utf8_to_native (name);
131                safe_free (name);
132              if (strchr (pend, '<') != NULL && strchr (pend, '>') != NULL) {              if (strchr (pend, '<') != NULL && strchr (pend, '>') != NULL) {
133                  int pos = strchr (i->name, '<')- i->name + 1;                  int pos = strchr (i->name, '<')- i->name + 1;
134                  int end = strchr (i->name, '>') - i->name;                  int end = strchr (i->name, '>') - i->name;
# Line 224  list2_handler (void *opaque, gpgme_statu Line 227  list2_handler (void *opaque, gpgme_statu
227      return 0;      return 0;
228  }  }
229    
230    
231  /* Dummy handler to get the colon data and then quit. */  /* Dummy handler to get the colon data and then quit. */
232  static gpgme_error_t  static gpgme_error_t
233  list_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd)  list_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd)
# Line 253  list_handler (void *opaque, gpgme_status Line 257  list_handler (void *opaque, gpgme_status
257  gpgme_error_t  gpgme_error_t
258  GpgKeyEdit::getDesignatedRevoker (gpg_desig_rev_t *r_rev)  GpgKeyEdit::getDesignatedRevoker (gpg_desig_rev_t *r_rev)
259  {  {
260      gpgme_data_t out=NULL;      gpgme_data_t out = NULL;
261      gpg_desig_rev_t rev = NULL;      gpg_desig_rev_t rev = NULL;
262      gpgme_error_t err;      gpgme_error_t err;
263      char buf[256];      char buf[256];
# Line 284  leave: Line 288  leave:
288      return err;      return err;
289  }  }
290    
291    
292  /* Retrieve all user ID information of the key set via setKey  /* Retrieve all user ID information of the key set via setKey
293     in @r_inf. The result also contains the user ID number which     in @r_inf. The result also contains the user ID number which
294     is needed to securely delete the user-ID. */     is needed to securely delete the user-ID. */
# Line 323  leave: Line 328  leave:
328  }  }
329    
330    
331  /* Construct an object with the given key in @key. */  /* Clear object. */
332  GpgKeyEdit::GpgKeyEdit (gpgme_key_t key)  void
333    GpgKeyEdit::clear (void)
334  {  {
335      this->key = key;      pass = NULL;    
     pass = NULL;  
     type = 0;  
336      name = NULL;      name = NULL;
337      cmt = NULL;      cmt = NULL;
338      email = NULL;      email = NULL;
339        type = 0;
340      cnt = 0;      cnt = 0;
341      cmd_sent = 0;      cmd_sent = 0;
342      resval = 0;      resval = 0;
343        uid_index = sig_index = key_index = -1;
344        key_has_passwd = true;
345    }
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      gpgme_new (&ctx); /* FIXME */      gpgme_new (&ctx); /* FIXME */
354  }  }
355    
356  /* Construct an object and fetch the key with the keyid @keyid. */  /* Construct an object and fetch the key with the keyid @keyid. */
357  GpgKeyEdit::GpgKeyEdit (const char *_keyid)  GpgKeyEdit::GpgKeyEdit (const char *_keyid)
358  {  {
359        clear ();
360      get_pubkey (_keyid, &this->key);      get_pubkey (_keyid, &this->key);
361      pass = NULL;      gpgme_new (&ctx); /* FIXME */    
     type = 0;  
     name = NULL;  
     cmt = NULL;  
     email = NULL;  
     cmd_sent = 0;  
     resval = 0;  
     gpgme_new (&ctx); /* FIXME */  
362  }  }
363    
364  /* Delete the given object. */  /* Delete the given object. */
# Line 367  void Line 376  void
376  GpgKeyEdit::reset (void)  GpgKeyEdit::reset (void)
377  {  {
378      cmd_sent = 0;      cmd_sent = 0;
379        cnt = 0;
380        //resval = 0;
381  }  }
382    
383    
# Line 394  GpgKeyEdit::setCallback (gpgme_progress_ Line 405  GpgKeyEdit::setCallback (gpgme_progress_
405      gpgme_set_progress_cb (ctx, cb, cb_value);      gpgme_set_progress_cb (ctx, cb, cb_value);
406  }  }
407    
408    
409    /* Clear the stored passphrase. */
410    void
411    GpgKeyEdit::clearPassphrase (void)
412    {
413        if (pass)
414            pass = NULL;
415    }
416    
417    
418    
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  /* Set the passphrase to @pass. */  /* Set the passphrase to @pass. */
427  void  void
428  GpgKeyEdit::setPassphrase (const char *_pass)  GpgKeyEdit::setPassphrase (const char *_pass)
# Line 441  GpgKeyEdit::getResult(void) Line 470  GpgKeyEdit::getResult(void)
470  }  }
471    
472    
473    /* Return the amount of days the key is valid. */
474    int
475    GpgKeyEdit::getValidDays (void)
476    {
477        return valid;
478    }
479    
480    
481  int  int
482  GpgKeyEdit::getType (void)  GpgKeyEdit::getType (void)
# Line 448  GpgKeyEdit::getType (void) Line 484  GpgKeyEdit::getType (void)
484      return type;      return type;
485  }  }
486    
487    
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  /* Sign the key stored in the object with the  /* Sign the key stored in the object with the
507     signing mode @mode and the signature class @sig_class.     signing mode @mode and the signature class @sig_class.
508     Return value: 0 on success. */     Return value: 0 on success. */
509  gpgme_error_t  gpgme_error_t
510  GpgKeyEdit::signKey (int mode, int _sig_class, const char *_exp_date)  GpgKeyEdit::signKey (int mode, int _sig_class, const char *_exp_date)
511  {  {
512      if (!this->key || !this->pass)      if (!this->key)
513            return gpg_error (GPG_ERR_INV_OBJ);
514        if (key_has_passwd && !this->pass)
515            return gpg_error (GPG_ERR_INV_PASSPHRASE);
516    
517        type = mode;
518        this->exp_date = _exp_date;
519        this->sig_class = _sig_class;
520        return gpg_editkey (this->ctx, this->key, this);
521    }
522    
523    
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        if (!this->key)
532          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
533        if (key_has_passwd && !this->pass)
534            return gpg_error (GPG_ERR_INV_PASSPHRASE);
535    
536        this->uid_index = _uid_idx;
537      type = mode;      type = mode;
538      this->exp_date = _exp_date;      this->exp_date = _exp_date;
539      this->sig_class = _sig_class;      this->sig_class = _sig_class;
# Line 482  GpgKeyEdit::setTrust (gpgme_validity_t t Line 559  GpgKeyEdit::setTrust (gpgme_validity_t t
559     name, @cmt as the comment (or NULL) and @email as the email.     name, @cmt as the comment (or NULL) and @email as the email.
560     Return value: 0 on success. */     Return value: 0 on success. */
561  gpgme_error_t  gpgme_error_t
562  GpgKeyEdit::addUserid (const char *name, const char *cmt, const char *email)  GpgKeyEdit::addUserid (const char *_name, const char *_cmt, const char *_email)
563  {  {
564      if (!this->key || !this->pass)      if (!this->key)
565          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
566        if (key_has_passwd && !this->pass)
567            return gpg_error (GPG_ERR_INV_PASSPHRASE);
568    
569      type = GPG_EDITKEY_ADDUID;      type = GPG_EDITKEY_ADDUID;
570      free_if_alloc (this->name);      free_if_alloc (this->name);
571      this->name = m_strdup (name);      this->name = m_strdup (_name);
572      free_if_alloc (this->cmt);      free_if_alloc (this->cmt);
573      this->cmt = NULL;      this->cmt = NULL;
574      if (cmt != NULL)      if (_cmt != NULL)
575          this->cmt = m_strdup (cmt);              this->cmt = m_strdup (_cmt);    
576      free_if_alloc (this->email);      free_if_alloc (this->email);
577      this->email = m_strdup (email);      this->email = m_strdup (_email);
578      if (!this->email || !this->name)      if (!this->email || !this->name)
579          BUG (NULL);          BUG (NULL);
580      return gpg_editkey (this->ctx, this->key, this);      return gpg_editkey (this->ctx, this->key, this);
# Line 533  GpgKeyEdit::delKey (int _key_index) Line 612  GpgKeyEdit::delKey (int _key_index)
612     key expires in @valid days.     key expires in @valid days.
613     Return value: 0 on success. */     Return value: 0 on success. */
614  gpgme_error_t  gpgme_error_t
615  GpgKeyEdit::addSubkey (gpgme_pubkey_algo_t pubkey_algo, unsigned int pubkey_size,  GpgKeyEdit::addSubkey (gpgme_pubkey_algo_t _pubkey_algo,
616                         long valid)                         unsigned int _pubkey_size, long _valid)
617  {  {
618      if (!this->key || !this->pass)      if (!this->key)
619          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
620        if (key_has_passwd && !this->pass)
621            return gpg_error (GPG_ERR_INV_PASSPHRASE);
622    
623      type = GPG_EDITKEY_ADDKEY;      type = GPG_EDITKEY_ADDKEY;
624      this->pubkey_algo = pubkey_algo;      this->pubkey_algo = _pubkey_algo;
625      this->pubkey_size = pubkey_size;      this->pubkey_size = _pubkey_size;
626      this->valid = valid;      this->valid = _valid;
627      return gpg_editkey (this->ctx, this->key, this);      return gpg_editkey (this->ctx, this->key, this);
628  }  }
629    
# Line 552  GpgKeyEdit::addSubkey (gpgme_pubkey_algo Line 633  GpgKeyEdit::addSubkey (gpgme_pubkey_algo
633  gpgme_error_t  gpgme_error_t
634  GpgKeyEdit::changePassphrase (const char *_new_pass, int allow_empty)  GpgKeyEdit::changePassphrase (const char *_new_pass, int allow_empty)
635  {  {
636      if (!this->key || !this->pass)      if (!this->key)
637          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
638        if (key_has_passwd && !this->pass)
639            return gpg_error (GPG_ERR_INV_PASSPHRASE);
640            
641      type = GPG_EDITKEY_PASSWD;      type = GPG_EDITKEY_PASSWD;
642      this->new_pass = _new_pass;      this->new_pass = _new_pass;
# Line 567  GpgKeyEdit::changePassphrase (const char Line 650  GpgKeyEdit::changePassphrase (const char
650  gpgme_error_t  gpgme_error_t
651  GpgKeyEdit::setPrimaryUserid (int _uid_index)  GpgKeyEdit::setPrimaryUserid (int _uid_index)
652  {  {
653      if (!this->key || !this->pass)      if (!this->key)
654          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
655        if (key_has_passwd && !this->pass)
656            return gpg_error (GPG_ERR_INV_PASSPHRASE);
657    
658      type = GPG_EDITKEY_PRIMARY;      type = GPG_EDITKEY_PRIMARY;
659      this->uid_index = _uid_index;      this->uid_index = _uid_index;
# Line 580  GpgKeyEdit::setPrimaryUserid (int _uid_i Line 665  GpgKeyEdit::setPrimaryUserid (int _uid_i
665     if @exp_days is true, exp_timestamp is already converted to days.     if @exp_days is true, exp_timestamp is already converted to days.
666     Return value: 0 on success. */     Return value: 0 on success. */
667  gpgme_error_t  gpgme_error_t
668  GpgKeyEdit::setKeyExpireDate (int _key_index,  GpgKeyEdit::setKeyExpireDate (int _key_index, long exp_timestamp,
669                                long exp_timestamp, bool exp_days)                                bool exp_days)
670  {  {
671      if (!this->key || !this->pass)      if (!this->key)
672          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
673        if (key_has_passwd && !this->pass)
674            return gpg_error (GPG_ERR_INV_PASSPHRASE);
675      if (!exp_days && exp_timestamp > 0 && exp_timestamp < time (NULL))      if (!exp_days && exp_timestamp > 0 && exp_timestamp < time (NULL))
676          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
677            
# Line 604  GpgKeyEdit::setKeyExpireDate (int _key_i Line 691  GpgKeyEdit::setKeyExpireDate (int _key_i
691  gpgme_error_t  gpgme_error_t
692  GpgKeyEdit::revokeUserid (int _uid_index)  GpgKeyEdit::revokeUserid (int _uid_index)
693  {  {
694      if (!this->key || !this->pass)      if (!this->key)
695          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
696        if (key_has_passwd && !this->pass)
697            return gpg_error (GPG_ERR_INV_PASSPHRASE);
698    
699      type = GPG_EDITKEY_REVUID;      type = GPG_EDITKEY_REVUID;
700      this->uid_index = _uid_index;      this->uid_index = _uid_index;
# Line 619  GpgKeyEdit::revokeUserid (int _uid_index Line 708  GpgKeyEdit::revokeUserid (int _uid_index
708  gpgme_error_t  gpgme_error_t
709  GpgKeyEdit::revokeSignature (int _uid_index, int _sig_index)  GpgKeyEdit::revokeSignature (int _uid_index, int _sig_index)
710  {  {
711      if (!this->key || !this->pass)      if (!this->key)
712          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
713        if (key_has_passwd && !this->pass)
714            return gpg_error (GPG_ERR_INV_PASSPHRASE);
715    
716      type = GPG_EDITKEY_REVSIG;      type = GPG_EDITKEY_REVSIG;
717      this->uid_index = _uid_index;      this->uid_index = _uid_index;
# Line 636  GpgKeyEdit::revokeSignature (int _uid_in Line 727  GpgKeyEdit::revokeSignature (int _uid_in
727  gpgme_error_t  gpgme_error_t
728  GpgKeyEdit::revokeSubkey (int _key_index, int _reason, const char *_cmt)  GpgKeyEdit::revokeSubkey (int _key_index, int _reason, const char *_cmt)
729  {  {
730      if (!this->key || !this->pass)      if (!this->key)
731          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
732        if (key_has_passwd && !this->pass)
733            return gpg_error (GPG_ERR_INV_PASSPHRASE);
734    
735      type = GPG_EDITKEY_REVKEY;      type = GPG_EDITKEY_REVKEY;
736      this->key_index = _key_index;      this->key_index = _key_index;
# Line 657  GpgKeyEdit::revokeSubkey (int _key_index Line 750  GpgKeyEdit::revokeSubkey (int _key_index
750  gpgme_error_t  gpgme_error_t
751  GpgKeyEdit::addDesignatedRevoker (const char *uid)  GpgKeyEdit::addDesignatedRevoker (const char *uid)
752  {  {
753      if (!this->key || !this->pass)      if (!this->key)
754          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
755        if (key_has_passwd && !this->pass)
756            return gpg_error (GPG_ERR_INV_PASSPHRASE);
757    
758      type = GPG_EDITKEY_ADDREV;      type = GPG_EDITKEY_ADDREV;
759      free_if_alloc (this->name);      free_if_alloc (this->name);
# Line 672  GpgKeyEdit::addDesignatedRevoker (const Line 767  GpgKeyEdit::addDesignatedRevoker (const
767  gpgme_error_t  gpgme_error_t
768  GpgKeyEdit::addPhotoid (const char *jpg_file)  GpgKeyEdit::addPhotoid (const char *jpg_file)
769  {  {
770      if (!this->key || !this->pass)      if (!this->key)
771          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
772        if (key_has_passwd && !this->pass)
773            return gpg_error (GPG_ERR_INV_PASSPHRASE);
774    
775      type = GPG_EDITKEY_ADDPHOTO;      type = GPG_EDITKEY_ADDPHOTO;
776      this->url = jpg_file;      this->url = jpg_file;
# Line 701  GpgKeyEdit::disable (void) Line 798  GpgKeyEdit::disable (void)
798  }  }
799    
800    
801    /* Remove unusable parts from a key. */
802    gpgme_error_t
803    GpgKeyEdit::cleanKey (void)
804    {
805        if (!this->key)
806            return gpg_error (GPG_ERR_INV_OBJ);
807        type = GPG_EDITKEY_CLEAN;
808        return gpg_editkey (this->ctx, this->key, this);
809    }
810    
811    
812  /* Update the user-ID preferences of the user-ID with the  /* Update the user-ID preferences of the user-ID with the
813     index @uid_index to the prefs given in @new_prefs.     index @uid_index to the prefs given in @new_prefs.
814     Return value: 0 on success. */     Return value: 0 on success. */
815  gpgme_error_t  gpgme_error_t
816  GpgKeyEdit::setUseridPreferences (int _uid_index, const char *new_prefs)  GpgKeyEdit::setUseridPreferences (int _uid_index, const char *new_prefs)
817  {  {
818      if (!this->key || !this->pass)      if (!this->key)
819          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
820        if (key_has_passwd && !this->pass)
821            return gpg_error (GPG_ERR_INV_PASSPHRASE);
822      return 0;      return 0;
823  }  }
824    
# Line 728  GpgKeyEdit::delUseridSignature (int _uid Line 838  GpgKeyEdit::delUseridSignature (int _uid
838  }  }
839    
840  /* Set the preferred keyserver for the given key to @url.  /* Set the preferred keyserver for the given key to @url.
841       If @_uid_index is -1, set the keyserver for all user-ids.
842     Return value: 0 on success. */     Return value: 0 on success. */
843  gpgme_error_t  gpgme_error_t
844  GpgKeyEdit::setPreferredKeyserver (int _uid_index, const char *_url)  GpgKeyEdit::setPreferredKeyserver (int _uid_index, const char *_url)
845  {  {
846      if (!this->key || !this->pass)      if (!this->key)
847          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
848      if (!url)      if (key_has_passwd && !this->pass)
849            return gpg_error (GPG_ERR_INV_PASSPHRASE);
850        if (!_url)
851          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
852    
853      type = GPG_EDITKEY_KEYSERV;      type = GPG_EDITKEY_KEYSERV;
854      this->url = _url;      this->url = _url;
855      this->uid_index = _uid_index;      this->uid_index = _uid_index;
856      return gpg_editkey (this->ctx, this->key, this);      return gpg_editkey (this->ctx, this->key, this);
857  }  }
858    
859    
860    /* Return the saved user-id index. */
861    int
862    GpgKeyEdit::getUseridIndex (void)
863    {
864        return uid_index;
865    }
866    
867    
868    /* Return the saved key index. */  
869    int
870    GpgKeyEdit::getKeyIndex (void)
871    {
872        return key_index;
873    }
874    
875    
876    /* Return the saved sig index. */
877    int
878    GpgKeyEdit::getSigIndex (void)
879    {
880        return sig_index;
881    }

Legend:
Removed from v.69  
changed lines
  Added in v.205

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26