/[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 119 by twoaday, Fri Dec 9 08:04:51 2005 UTC revision 225 by twoaday, Tue Jun 6 13:37:59 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 34  Line 34 
34  #include "wptW32API.h"  #include "wptW32API.h"
35  #include "wptGPG.h"  #include "wptGPG.h"
36  #include "wptErrors.h"  #include "wptErrors.h"
37    #include "wptUTF8.h"
38    
39    
40  /* 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 93  uid_inf_colon_handler (gpg_uid_info_t *i
93  {  {
94      gpg_uid_info_t i, t;      gpg_uid_info_t i, t;
95      char *p, *pend;      char *p, *pend;
96        char *name;
97      int field = 0, len = 0;      int field = 0, len = 0;
98    
99      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 124  uid_inf_colon_handler (gpg_uid_info_t *i
124              break;              break;
125    
126          case 10: /* user ID */          case 10: /* user ID */
127              i->name = (char *)calloc (1, strlen (pend)+1);              name = (char *)calloc (1, strlen (pend)+1);
128              if (!i->name)              if (!name)
129                  return gpg_error (GPG_ERR_ENOMEM);;                  return gpg_error (GPG_ERR_ENOMEM);;
130              gpg_decode_c_string (pend, &i->name, strlen (pend)+ 1);              gpg_decode_c_string (pend, &name, strlen (pend)+ 1);
131                i->name = utf8_to_native (name);
132                safe_free (name);
133              if (strchr (pend, '<') != NULL && strchr (pend, '>') != NULL) {              if (strchr (pend, '<') != NULL && strchr (pend, '>') != NULL) {
134                  int pos = strchr (i->name, '<')- i->name + 1;                  int pos = strchr (i->name, '<')- i->name + 1;
135                  int end = strchr (i->name, '>') - i->name;                  int end = strchr (i->name, '>') - i->name;
# Line 224  list2_handler (void *opaque, gpgme_statu Line 228  list2_handler (void *opaque, gpgme_statu
228      return 0;      return 0;
229  }  }
230    
231    
232  /* Dummy handler to get the colon data and then quit. */  /* Dummy handler to get the colon data and then quit. */
233  static gpgme_error_t  static gpgme_error_t
234  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 258  list_handler (void *opaque, gpgme_status
258  gpgme_error_t  gpgme_error_t
259  GpgKeyEdit::getDesignatedRevoker (gpg_desig_rev_t *r_rev)  GpgKeyEdit::getDesignatedRevoker (gpg_desig_rev_t *r_rev)
260  {  {
261      gpgme_data_t out=NULL;      gpgme_data_t out = NULL;
262      gpg_desig_rev_t rev = NULL;      gpg_desig_rev_t rev = NULL;
263      gpgme_error_t err;      gpgme_error_t err;
264      char buf[256];      char buf[256];
# Line 284  leave: Line 289  leave:
289      return err;      return err;
290  }  }
291    
292    
293  /* Retrieve all user ID information of the key set via setKey  /* Retrieve all user ID information of the key set via setKey
294     in @r_inf. The result also contains the user ID number which     in @r_inf. The result also contains the user ID number which
295     is needed to securely delete the user-ID. */     is needed to securely delete the user-ID. */
# Line 323  leave: Line 329  leave:
329  }  }
330    
331    
332  /* Construct an object with the given key in @key. */  /* Clear object. */
333  GpgKeyEdit::GpgKeyEdit (gpgme_key_t _key)  void
334    GpgKeyEdit::clear (void)
335  {  {
336      this->key = _key;      pass = NULL;    
     pass = NULL;  
     type = 0;  
337      name = NULL;      name = NULL;
338      cmt = NULL;      cmt = NULL;
339      email = NULL;      email = NULL;
340        type = 0;
341      cnt = 0;      cnt = 0;
342      cmd_sent = 0;      cmd_sent = 0;
343      resval = 0;      resval = 0;
344        uid_index = sig_index = key_index = -1;
345        key_has_passwd = true;
346    }
347    
348    
349    /* Construct an object with the given key in @key. */
350    GpgKeyEdit::GpgKeyEdit (gpgme_key_t _key)
351    {
352        clear ();
353        this->key = _key;
354      gpgme_new (&ctx); /* FIXME */      gpgme_new (&ctx); /* FIXME */
355  }  }
356    
357  /* Construct an object and fetch the key with the keyid @keyid. */  /* Construct an object and fetch the key with the keyid @keyid. */
358  GpgKeyEdit::GpgKeyEdit (const char *_keyid)  GpgKeyEdit::GpgKeyEdit (const char *_keyid)
359  {  {
360        clear ();
361      get_pubkey (_keyid, &this->key);      get_pubkey (_keyid, &this->key);
362      pass = NULL;      gpgme_new (&ctx); /* FIXME */    
     type = 0;  
     name = NULL;  
     cmt = NULL;  
     email = NULL;  
     cmd_sent = 0;  
     resval = 0;  
     gpgme_new (&ctx); /* FIXME */  
363  }  }
364    
365  /* Delete the given object. */  /* Delete the given object. */
# Line 367  void Line 377  void
377  GpgKeyEdit::reset (void)  GpgKeyEdit::reset (void)
378  {  {
379      cmd_sent = 0;      cmd_sent = 0;
380        cnt = 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 484  GpgKeyEdit::setTrust (gpgme_validity_t t Line 561  GpgKeyEdit::setTrust (gpgme_validity_t t
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);
# Line 536  gpgme_error_t Line 615  gpgme_error_t
615  GpgKeyEdit::addSubkey (gpgme_pubkey_algo_t _pubkey_algo,  GpgKeyEdit::addSubkey (gpgme_pubkey_algo_t _pubkey_algo,
616                         unsigned int _pubkey_size, 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;
# 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 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  /* Remove unusable parts from a key. */  /* Remove unusable parts from a key. */
813  gpgme_error_t  gpgme_error_t
814  GpgKeyEdit::cleanKey (void)  GpgKeyEdit::cleanKey (void)
# Line 709  GpgKeyEdit::cleanKey (void) Line 817  GpgKeyEdit::cleanKey (void)
817          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
818      type = GPG_EDITKEY_CLEAN;      type = GPG_EDITKEY_CLEAN;
819      return gpg_editkey (this->ctx, this->key, this);      return gpg_editkey (this->ctx, this->key, this);
       
820  }  }
821    
822    
# Line 719  GpgKeyEdit::cleanKey (void) Line 826  GpgKeyEdit::cleanKey (void)
826  gpgme_error_t  gpgme_error_t
827  GpgKeyEdit::setUseridPreferences (int _uid_index, const char *new_prefs)  GpgKeyEdit::setUseridPreferences (int _uid_index, const char *new_prefs)
828  {  {
829      if (!this->key || !this->pass)      if (!this->key)
830          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
831        if (key_has_passwd && !this->pass)
832            return gpg_error (GPG_ERR_INV_PASSPHRASE);
833      return 0;      return 0;
834  }  }
835    
# Line 740  GpgKeyEdit::delUseridSignature (int _uid Line 849  GpgKeyEdit::delUseridSignature (int _uid
849  }  }
850    
851  /* Set the preferred keyserver for the given key to @url.  /* Set the preferred keyserver for the given key to @url.
852       If @_uid_index is -1, set the keyserver for all user-ids.
853     Return value: 0 on success. */     Return value: 0 on success. */
854  gpgme_error_t  gpgme_error_t
855  GpgKeyEdit::setPreferredKeyserver (int _uid_index, const char *_url)  GpgKeyEdit::setPreferredKeyserver (int _uid_index, const char *_url)
856  {  {
857      if (!this->key || !this->pass)      if (!this->key)
858          return gpg_error (GPG_ERR_INV_OBJ);          return gpg_error (GPG_ERR_INV_OBJ);
859      if (!url)      if (key_has_passwd && !this->pass)
860            return gpg_error (GPG_ERR_INV_PASSPHRASE);
861        if (!_url)
862          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
863    
864      type = GPG_EDITKEY_KEYSERV;      type = GPG_EDITKEY_KEYSERV;
865      this->url = _url;      this->url = _url;
866      this->uid_index = _uid_index;      this->uid_index = _uid_index;
867      return gpg_editkey (this->ctx, this->key, this);      return gpg_editkey (this->ctx, this->key, this);
868  }  }
869    
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    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26