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

Diff of /trunk/Src/wptKeysignDlg.cpp

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

revision 22 by twoaday, Wed Aug 10 11:33:35 2005 UTC revision 25 by twoaday, Wed Oct 12 10:04:26 2005 UTC
# Line 34  Line 34 
34  #include "wptUTF8.h"  #include "wptUTF8.h"
35  #include "wptRegistry.h"  #include "wptRegistry.h"
36  #include "wptKeyList.h"  #include "wptKeyList.h"
37    #include "wptKeyEdit.h"
38    
39  static int sig_class_choice = 0;  static int sig_class_choice = 0;
40    
41  static const char *  /* Return a beautified printable fingerprint of @fpr. */
42  get_printable_fpr (const char * fpr)  static const char*
43    get_printable_fpr (const char *fpr)
44  {  {
45      static char pfpr[64];          static char pfpr[64];    
46      int pos = 0;      int pos = 0;
# Line 52  get_printable_fpr (const char * fpr) Line 54  get_printable_fpr (const char * fpr)
54          pfpr[pos++] = ' ';          pfpr[pos++] = ' ';
55      }      }
56      return pfpr;      return pfpr;
57  } /* get_printable_fpr */  }
58    
59    
60  static const char *  /* Return human friendly information about the key @key. */
61    static const char*
62  get_keyinfo (gpgme_key_t key)  get_keyinfo (gpgme_key_t key)
63  {  {
64      static char buf[64+16];      static char buf[64+16];
65          struct winpt_key_s k;
66    
67        memset (&k, 0, sizeof (k));
68        winpt_get_seckey (key->subkeys->keyid, &k);
69      _snprintf (buf, DIM (buf)-1-16, "%d-bit %s key, ID %s",      _snprintf (buf, DIM (buf)-1-16, "%d-bit %s key, ID %s",
70          gpgme_key_get_ulong_attr (key, GPGME_ATTR_LEN, NULL, 0),          key->subkeys->length,
71          gpgme_key_get_string_attr (key, GPGME_ATTR_ALGO, NULL, 0),          get_key_pubalgo (key->subkeys->pubkey_algo),
72          gpgme_key_get_string_attr (key, GPGME_ATTR_KEYID, NULL, 0) + 8 );          key->subkeys->keyid+8);
73      if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_DIVERT_CARD, NULL, 0))      if (k.ext->gloflags.divert_to_card)
74          strcat (buf, " (Card)");          strcat (buf, " (Card)");
75      return buf;      return buf;
76  } /* get_keyinfo */  }
77    
78    
79    /* Fill the secret key combo-box with all entries from the cache.
80       @dlg is the handle to the combo-box. @keyid show which key to skip.
81       Return value: 0 on success. */
82  static int  static int
83  do_fill_seckeylist (HWND dlg, const char * keyid)  do_fill_seckeylist (HWND dlg, const char *keyid)
84  {  {
85      gpgme_keycache_t sec;      gpg_keycache_t sec;
86      gpgme_key_t pk;      gpgme_key_t pk;
87      const char * s;      const char * s;
88      char * uid, * p;          char * uid, * p;    
# Line 82  do_fill_seckeylist (HWND dlg, const char Line 91  do_fill_seckeylist (HWND dlg, const char
91      sec = keycache_get_ctx (0);      sec = keycache_get_ctx (0);
92      if (!sec)      if (!sec)
93          BUG (0);          BUG (0);
94      gpgme_keycache_rewind (sec);      gpg_keycache_rewind (sec);
95      while (!gpgme_keycache_next_key (sec, 1, &pk)) {      while (!gpg_keycache_next_key (sec, 1, &pk)) {
96          if (!pk)          if (!pk)
97              continue;              continue;
98          s = gpgme_key_get_string_attr (pk, GPGME_ATTR_KEYID, NULL, 0);          s = pk->subkeys->keyid;
99          if (!strcmp (s, keyid))          if (!strcmp (s, keyid))
100              continue;              continue;
101          /* skip all ElGamal sign+encrypt keys */          /* skip all ElGamal sign+encrypt keys */
102          if( gpgme_key_get_ulong_attr( pk, GPGME_ATTR_ALGO, NULL, 0 )          if (pk->subkeys->pubkey_algo == GPGME_PK_ELG)
             == GPGME_PK_ELG_ES )  
103              continue;              continue;
104          /* make sure the public key is okay not: revoked, expired or disabled. */          /* make sure the public key is okay not: revoked, expired or disabled. */
105          if( gpgme_key_get_ulong_attr (pk, GPGME_ATTR_EXPIRE, NULL, 0)          if (pk->expired ||pk->revoked || pk->disabled)
             || gpgme_key_get_ulong_attr (pk, GPGME_ATTR_KEY_REVOKED, NULL, 0)  
             || gpgme_key_get_ulong_attr (pk, GPGME_ATTR_KEY_DISABLED, NULL, 0))  
106              continue;              continue;
107          s = gpgme_key_get_string_attr (pk, GPGME_ATTR_NAME, NULL, 0);          s = pk->uids->name;
108          if (!s)          if (!s)
109              continue;              continue;
110          uid = utf8_to_wincp (s, strlen (s));          uid = utf8_to_wincp (s, strlen (s));
111          p = new char[strlen( uid ) + 64];          p = new char[strlen (uid) + 64];
112          if (!p)          if (!p)
113              BUG( NULL );                      BUG (NULL);
114          _snprintf (p, strlen (uid) + 63, "%s (%s)", uid, get_keyinfo (pk));          _snprintf (p, strlen (uid) + 63, "%s (%s)", uid, get_keyinfo (pk));
115          SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_ADDSTRING, i, (LPARAM)(char *)p);          SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_ADDSTRING, i, (LPARAM)(char *)p);
116          SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_SETITEMDATA, i++, (LPARAM)(DWORD)pk);          SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_SETITEMDATA, i++, (LPARAM)(DWORD)pk);
# Line 116  do_fill_seckeylist (HWND dlg, const char Line 122  do_fill_seckeylist (HWND dlg, const char
122      if (!n)      if (!n)
123          return -1;          return -1;
124      return 0;      return 0;
125  } /* do_fill_seckeylist */  }
   
   
 static void  
 do_add_local_user (gpgme_ctx_t ctx, HWND dlg)  
 {  
     int idx;  
     const char * s;  
     gpgme_key_t key;  
   
     idx = SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0 );  
     key = (gpgme_key_t)SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETITEMDATA, (WPARAM)idx, 0 );  
     if( key && (s = gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, 0 )) )  
         gpgme_set_local_user( ctx, s );      
 } /* do_add_local_user */  
126    
127    
128    /* Check if the selected key is protected and en- or disable the
129       passphrase control. */
130  static void  static void
131  do_check_protection (HWND dlg)  do_check_protection (HWND dlg)
132  {  {
133      int idx, protec;      int idx, protec;
134      gpgme_key_t key;      gpgme_key_t key;
135        struct winpt_key_s k;
136    
137      idx = SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0 );      idx = SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0 );
138      key = (gpgme_key_t)SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETITEMDATA, (WPARAM)idx, 0 );      key = (gpgme_key_t)SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETITEMDATA, (WPARAM)idx, 0 );
139      if (key)      if (key) {
140      {          winpt_get_seckey (key->subkeys->keyid, &k);
141          protec = gpgme_key_get_ulong_attr (key, GPGME_ATTR_IS_PROTECTED, NULL, 0);          protec = k.is_protected;
142          if (!protec)          if (!protec)
143              protec = gpgme_key_get_ulong_attr (key, GPGME_ATTR_DIVERT_CARD, NULL, 0);              protec = k.ext->gloflags.divert_to_card;
144          EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE), protec? TRUE : FALSE);          EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE), protec? TRUE : FALSE);
145      }      }
146  } /* do_check_protection */  }
147    
148    
149    /* Dialog box procedure to choose the signature class. */
150  BOOL CALLBACK  BOOL CALLBACK
151  sig_class_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  sig_class_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
152  {  {
# Line 164  sig_class_dlg_proc (HWND dlg, UINT msg, Line 160  sig_class_dlg_proc (HWND dlg, UINT msg,
160          SetDlgItemText (dlg, IDC_SIGCLASS_CLASS3, _("(3) I have done very careful checkings."));          SetDlgItemText (dlg, IDC_SIGCLASS_CLASS3, _("(3) I have done very careful checkings."));
161          CheckDlgButton (dlg, IDC_SIGCLASS_CLASS0, BST_CHECKED);          CheckDlgButton (dlg, IDC_SIGCLASS_CLASS0, BST_CHECKED);
162          SetForegroundWindow (dlg);          SetForegroundWindow (dlg);
163          center_window (dlg);          center_window (dlg, NULL);
164          return TRUE;          return TRUE;
165    
166      case WM_COMMAND:      case WM_COMMAND:
# Line 181  sig_class_dlg_proc (HWND dlg, UINT msg, Line 177  sig_class_dlg_proc (HWND dlg, UINT msg,
177              else              else
178                  sig_class_choice = 0;                  sig_class_choice = 0;
179              EndDialog (dlg, TRUE);              EndDialog (dlg, TRUE);
180              break;              return TRUE;
181          }          }
182          break;          break;
183      }      }
184    
185      return FALSE;      return FALSE;
186  } /* sig_class_dlg_proc */  }
187    
188    
189    /* Return the humand friendly expiration date of @key. */
190  static const char*  static const char*
191  get_expire_date (gpgme_key_t key)  get_expire_date (gpgme_key_t key)
192  {  {
193      static char tmp[64];      static char tmp[64];
194    
195      u32 u = gpgme_key_get_ulong_attr (key, GPGME_ATTR_EXPIRES, NULL, 0);      u32 u = key->subkeys->expires;
196      if (!u)      if (!u)
197          return "never";          return "never";
198      return get_key_expire_date (u);      return get_key_expire_date (u);
199  }  }
200    
201    
202    /* Dialog box procedure to sign a key. */
203  BOOL CALLBACK  BOOL CALLBACK
204  keysign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  keysign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
205  {  {
206      static winpt_key_t key;      static winpt_key_t key;
207      gpgme_editkey_t ke;      GpgKeyEdit *ke;
     gpgme_ctx_t ctx;  
208      gpgme_error_t err;      gpgme_error_t err;
209        gpgme_key_t k;
210      SYSTEMTIME st;      SYSTEMTIME st;
211      HWND h;      HWND h;
212      char keymsg[4096], pwd[256], *uid = NULL;      char keymsg[4096], pwd[256], *uid = NULL;
213      const char *keyid, *s;      const char *keyid, *s;
214      u32 created;      u32 created;
215      int type, expires=0;      int type, expires=0, idx;
   
216            
217      switch ( msg )  {      switch ( msg )  {
218      case WM_INITDIALOG:      case WM_INITDIALOG:
219          if (lparam == NULL)          if (lparam == NULL)
220              dlg_fatal_error( dlg, "Could not get dialog param." );              dlg_fatal_error (dlg, "Could not get dialog param.");
221          #ifndef LANG_DE          #ifndef LANG_DE
222          SetWindowText( dlg, _("Key Signing") );          SetWindowText (dlg, _("Key Signing"));
223          #endif          #endif
224          key = (winpt_key_t) lparam;          key = (winpt_key_t) lparam;
225          created = gpgme_key_get_ulong_attr (key->ctx, GPGME_ATTR_CREATED, NULL, 0);          created = key->ctx->subkeys->timestamp;
226          s = gpgme_key_get_string_attr( key->ctx, GPGME_ATTR_USERID, NULL, 0 );          s = key->ctx->uids->uid;
227          if( s )          if (s)
228              uid = utf8_to_wincp (s, strlen (s));              uid = utf8_to_wincp (s, strlen (s));
229          _snprintf( keymsg, sizeof keymsg -1,          _snprintf (keymsg, sizeof keymsg -1,
230                     _("pub %d/%s created: %s    expires: %s\n\n"                     _("pub %d/%s  created: %s    expires: %s\n\n"
231                       "Primary key fingerprint: %s\n\n"                       "Primary key fingerprint: %s\n\n"
232                       "\t%s\n\n"                       "\t%s\n\n"
233                       "\nAre you really sure that you want to sign this key with YOUR key?\n"),                       "\nAre you really sure that you want to sign this key with YOUR key?\n"),
234                     gpgme_key_get_ulong_attr( key->ctx, GPGME_ATTR_LEN, NULL, 0 ),                     key->ctx->subkeys->length,
235                     gpgme_key_get_string_attr( key->ctx, GPGME_ATTR_KEYID, NULL, 0 )+8,                     key->ctx->subkeys->keyid+8,
236                     gpgme_key_expand_attr( GPGME_ATTR_CREATED, created ),                     get_key_created (key->ctx->subkeys->timestamp),
237                     get_expire_date (key->ctx),                     get_expire_date (key->ctx),
238                     get_printable_fpr( gpgme_key_get_string_attr( key->ctx, GPGME_ATTR_FPR, NULL, 0 ) ),                     get_printable_fpr (key->ctx->subkeys->fpr),
239                     uid );                     uid);
240          free (uid);          free (uid);
241          s = gpgme_key_get_string_attr (key->ctx, GPGME_ATTR_KEYID, NULL, 0);          s = key->ctx->subkeys->keyid;
242          if (do_fill_seckeylist (dlg, s)) {          if (do_fill_seckeylist (dlg, s)) {
243              msg_box( dlg, _("No valid secret key found."), _("Key Signing"), MB_ERR );              msg_box( dlg, _("No valid secret key found."), _("Key Signing"), MB_ERR );
244              EndDialog( dlg, FALSE );              EndDialog( dlg, FALSE );
245          }          }
246          SetDlgItemText( dlg, IDC_KEYSIGN_INFOS, keymsg );          SetDlgItemText (dlg, IDC_KEYSIGN_INFOS, keymsg);
247          #ifndef LANG_DE          #ifndef LANG_DE
248          SetDlgItemText (dlg, IDC_KEYSIGN_LOCAL, _("Sign local only (non exportable signature)"));          SetDlgItemText (dlg, IDC_KEYSIGN_LOCAL, _("Sign local only (non exportable signature)"));
249          SetDlgItemText (dlg, IDC_KEYSIGN_EXPSIG, _("Signature expires on"));          SetDlgItemText (dlg, IDC_KEYSIGN_EXPSIG, _("Signature expires on"));
# Line 292  keysign_dlg_proc (HWND dlg, UINT msg, WP Line 289  keysign_dlg_proc (HWND dlg, UINT msg, WP
289                                    _("Choose Signature Class"),  IDS_WINPT_SIGCLASS);                                    _("Choose Signature Class"),  IDS_WINPT_SIGCLASS);
290              type = IsDlgButtonChecked (dlg, IDC_KEYSIGN_LOCAL);              type = IsDlgButtonChecked (dlg, IDC_KEYSIGN_LOCAL);
291              if (type)              if (type)
292                  type = GPGME_EDITKEY_LSIGN;                  type = GPG_EDITKEY_LSIGN;
293              else              else
294                  type = GPGME_EDITKEY_SIGN;                  type = GPG_EDITKEY_SIGN;
295    
296              if (reg_prefs.expert && IsDlgButtonChecked (dlg, IDC_KEYSIGN_NREV)) {              if (reg_prefs.expert && IsDlgButtonChecked (dlg, IDC_KEYSIGN_NREV)) {
297                  type = GPGME_EDITKEY_NRSIGN;                  type = GPG_EDITKEY_NRSIGN;
298                  if (type == GPGME_EDITKEY_LSIGN)                  if (type == GPG_EDITKEY_LSIGN)
299                      type = GPGME_EDITKEY_NRLSIGN;                      type = GPG_EDITKEY_NRLSIGN;
300              }              }
301              if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG)) {              if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG)) {
302                  expires = 1;                  expires = 1;
# Line 307  keysign_dlg_proc (HWND dlg, UINT msg, WP Line 304  keysign_dlg_proc (HWND dlg, UINT msg, WP
304                  sprintf (keymsg, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay);                  sprintf (keymsg, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay);
305              }              }
306    
307                /* XXX: check for --ask-cert-level and --ask-cert-expire in the gpg.conf
308                        if an advanced button is checked and offer to add it to the config
309                        file. */
310    
311              GetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, pwd, DIM (pwd)-1);              GetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, pwd, DIM (pwd)-1);
312              keyid = gpgme_key_get_string_attr( key->ctx, GPGME_ATTR_KEYID, NULL, 0 );              keyid = key->ctx->subkeys->keyid;
313              if( !keyid ) {              if( !keyid ) {
314                  msg_box( dlg, _("Could not get Key ID from key."), _("Key Signing"), MB_ERR );                  msg_box( dlg, _("Could not get Key ID from key."), _("Key Signing"), MB_ERR );
315                  return FALSE;                  return TRUE;
316              }              }
317              err = gpgme_new( &ctx );              ke = new GpgKeyEdit (keyid);
318              if (err)              if (!ke)
319                  BUG (0);                  BUG (NULL);
320              do_add_local_user (ctx, dlg);              ke->setPassphrase (pwd);
321                idx = SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0);
322              err = gpgme_editkey_new (&ke);              k = (gpgme_key_t)SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
323              if (err)                                                   CB_GETITEMDATA, (WPARAM)idx, 0);
324                  BUG (0);              if (k)
325              err = gpgme_editkey_sign_set (ke, pwd, sig_class_choice, type,                  ke->setLocalUser (k);
326                                            expires? keymsg : NULL);  
327                err = ke->signKey (type, sig_class_choice, expires? keymsg : "0");
328                memset (&pwd, 0, sizeof pwd);          
329              if (err) {              if (err) {
330                    delete ke;
331                  msg_box (dlg, gpgme_strerror (err), _("Key Signing"), MB_ERR);                  msg_box (dlg, gpgme_strerror (err), _("Key Signing"), MB_ERR);
332                  gpgme_release (ctx);                  return TRUE;
                 gpgme_editkey_release (ke);  
                 return FALSE;  
333              }              }
334                                if (ke->getResultValue () != 0)
335              gpgme_set_edit_ctx (ctx, ke, type);                  msg_box (dlg, _("This key is already signed by your key"), _("Key Signing"), MB_INFO);
               
             err = gpgme_op_editkey( ctx, keyid );  
             memset( &pwd, 0, sizeof pwd );  
             if (err == GPGME_Conflict)  
                 Sleep (500); /* it seems the process is not really finished */  
             gpgme_release( ctx );  
             gpgme_editkey_release( ke );  
             if( err ) {  
                 if( err == GPGME_Conflict )  
                     msg_box( dlg, _("Key is already signed by your key."), _("Key Signing"), MB_INFO );  
                 else if( err == GPGME_Invalid_Mode )  
                     msg_box( dlg, _("Unusable secret key."), _("Key Signing"), MB_ERR );  
                 else  
                     msg_box( dlg, gpgme_strerror( err ), _("Key Signing"), MB_ERR );  
             }  
336              else {              else {
337                  status_box (dlg, _("Key successfully signed."), PGM_NAME);                  status_box (dlg, _("Key successfully signed."), PGM_NAME);
338                  key->update = 1;                  key->update = 1;
                 EndDialog (dlg, TRUE);  
339              }              }
340                delete ke;
341                EndDialog (dlg, TRUE);
342              return TRUE;              return TRUE;
343                            
344          case IDCANCEL:          case IDCANCEL:
345              EndDialog( dlg, FALSE );              EndDialog (dlg, FALSE);
346              return FALSE;              return TRUE;
347          }          }
348          break;          break;
349      }      }
350            
351      return FALSE;      return FALSE;
352  } /* keysign_dlg_proc */  }
353    

Legend:
Removed from v.22  
changed lines
  Added in v.25

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26