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

Diff of /trunk/Src/wptKeyPropsDlg.cpp

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

revision 77 by twoaday, Mon Nov 14 15:01:01 2005 UTC revision 217 by twoaday, Mon May 22 14:21:39 2006 UTC
# Line 1  Line 1 
1  /* wptKeyPropsDlg.cpp - WinPT key properties dialog  /* wptKeyPropsDlg.cpp - WinPT key properties dialog
2   *      Copyright (C) 2000, 2001, 2002, 2003, 2005 Timo Schulz   *      Copyright (C) 2000, 2001, 2002, 2003, 2005, 2006 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
# Line 17  Line 17 
17   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
18   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19   */   */
   
20  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
21  #include <config.h>  #include <config.h>
22  #endif  #endif
23    
24  #include <windows.h>  #include <windows.h>
25    #include <assert.h>
26    
27  #include "resource.h"  #include "resource.h"
28  #include "wptErrors.h"  #include "wptErrors.h"
# Line 42  Line 42 
42  static int  static int
43  do_check_key (gpgme_key_t key)  do_check_key (gpgme_key_t key)
44  {  {
45      int okay = 0;      int invalid = 0;
46      okay = key->expired;  
47      if (!okay)      invalid = key->expired;
48          okay = key->revoked;      if (!invalid)
49      return okay;          invalid = key->revoked;
50        return invalid;
51  }  }
52    
53    
54  /* Convert a trust integer into a string representation. */  /* Convert a trust integer into a string representation. */
55  static const char*  static const char*
56  ownertrust_to_string (int val)  ownertrust_to_string (int val, bool is_keypair)
57  {  {
58      const char *inf;      const char *inf;
59      int id = val;      int id = val;
# Line 63  ownertrust_to_string (int val) Line 64  ownertrust_to_string (int val)
64      case 3: inf = _("I trust marginally"); break;      case 3: inf = _("I trust marginally"); break;
65      case 4: inf = _("I trust fully");      break;      case 4: inf = _("I trust fully");      break;
66      case 5:      case 5:
67      case 6: inf = _("I trust ultimately"); break;      case 6:
68            if (is_keypair)
69                inf = _("I trust ultimately (implicit)");
70            else
71                inf = _("I trust ultimately"); break;
72      default:inf = _("Unknown");            break;      default:inf = _("Unknown");            break;
73      }      }
74            
# Line 76  ownertrust_to_string (int val) Line 81  ownertrust_to_string (int val)
81  static const char*  static const char*
82  get_photo_tmpname (HWND dlg)  get_photo_tmpname (HWND dlg)
83  {  {
84      static char buf[64];      static char buf[MAX_PATH+128+1];
85        char name[64];
86    
87      _snprintf (buf, sizeof (buf)-1, "winpt_photo_%08lX.tmp", (DWORD)dlg);      _snprintf (name, sizeof (name)-1, "winpt_photo_%08lX.tmp", (DWORD)dlg);
88        get_temp_name (buf, DIM (buf)-1, name);
89      return buf;      return buf;
90  }  }
91    
92    
93    static void
94    draw_nophoto_img (HWND dlg)
95    {
96        /*..
97        n = DrawText (hdc, "No Photo-ID", -1, &r, DT_LEFT);
98        ..*/
99    }
100    
101    
102  /* Load the photo from the key @key */  /* Load the photo from the key @key */
103  static int  static int
104  keyprops_load_photo (HWND dlg, gpgme_key_t key, gpgme_validity_t *r_valid)  keyprops_load_photo (HWND dlg, gpgme_key_t key, gpgme_validity_t *r_valid)
# Line 93  keyprops_load_photo (HWND dlg, gpgme_key Line 109  keyprops_load_photo (HWND dlg, gpgme_key
109      DWORD imglen = 0;      DWORD imglen = 0;
110      int pos=0;      int pos=0;
111    
112      winpt_get_pubkey (key->subkeys->keyid, &k);      if (winpt_get_pubkey (key->subkeys->keyid, &k))
113            BUG (0);
114      img = k.ext->attrib.d;      img = k.ext->attrib.d;
115      imglen = k.ext->attrib.len;      imglen = k.ext->attrib.len;
116      if (!k.ext->attrib.validity)      if (img && !k.ext->attrib.validity)
117          get_uat_validity (key->subkeys->keyid, &k.ext->attrib.validity);          get_uat_validity (key->subkeys->keyid, &k.ext->attrib.validity);
118      *r_valid = k.ext->attrib.validity;      *r_valid = k.ext->attrib.validity;
119    
120      if (!img || !imglen)      if (!img || !imglen) {
121            draw_nophoto_img (dlg);
122          return -1;          return -1;
123        }
124    
125      f = fopen (get_photo_tmpname (dlg), "wb");      f = fopen (get_photo_tmpname (dlg), "wb");
126      if (f) {      if (f) {
         for (pos = 0; img[pos] != 0x10; pos++)  
                 ;  
127          pos += 16;          pos += 16;
128          fwrite (img + pos, 1, imglen - pos, f);          fwrite (img + pos, 1, imglen - pos, f);
         fwrite (img, 1, imglen, f);  
129          fclose (f);          fclose (f);
130      }      }
131      return 0;      return 0;
# Line 138  keyprops_show_photo (HWND dlg) Line 155  keyprops_show_photo (HWND dlg)
155  static const char*  static const char*
156  get_validity (gpgme_key_t key)  get_validity (gpgme_key_t key)
157  {  {
158      int val;      if (key->expired)
     val = key->expired;  
     if (val)  
159          return _("Expired");              return _("Expired");    
160      val = key->revoked;      if (key->revoked)
     if (val)  
161          return _("Revoked");          return _("Revoked");
162      val = key->disabled;      if (key->disabled)
     if (val)  
163          return _("Disabled");          return _("Disabled");
164      return get_key_trust2 (NULL, key->uids->validity, 0, 0);      return get_key_trust2 (NULL, key->uids->validity, 0, 0);
165  }  }
# Line 158  get_pref_cipher (winpt_key_t k) Line 171  get_pref_cipher (winpt_key_t k)
171  {  {
172      if (k->is_v3)      if (k->is_v3)
173          return "IDEA";          return "IDEA";
174      if (!k->ext->sym_prefs)      if (!k->ext || !k->ext->sym_prefs)
175          return "3DES";          return "3DES";
176      switch (*k->ext->sym_prefs) {      switch (*k->ext->sym_prefs) {
177      case 1: return "IDEA";      case 1: return "IDEA";
178      case 2: return "3DES";      case 2: return "3DES";
179      case 3: return "CAST5";      case 3: return "CAST5";
180      case 4: return "Blowfish";      case 4: return "Blowfish";
181      case 7:      case 7: return "AES128";
182      case 8:      case 8: return "AES192";
183      case 9: return "AES";      case 9: return "AES256";
184      case 10:return "Twofish";      case 10:return "Twofish";
185      }      }
186      return "Unknown";      return "Unknown";
# Line 178  get_pref_cipher (winpt_key_t k) Line 191  get_pref_cipher (winpt_key_t k)
191  static bool  static bool
192  check_for_desig_rev (gpgme_key_t key)  check_for_desig_rev (gpgme_key_t key)
193  {  {
194      winpt_key_s k;      winpt_key_s kk;
195      memset (&k, 0, sizeof (k));  
196      if (!winpt_get_pubkey (key->subkeys->keyid, &k))      memset (&kk, 0, sizeof (kk));
197          return k.ext->gloflags.has_desig_rev? true : false;      if (!winpt_get_pubkey (key->subkeys->keyid, &kk))
198            return kk.ext->gloflags.has_desig_rev? true : false;
199      return false;      return false;
200  }  }
201    
# Line 192  get_card_type (winpt_key_t k) Line 206  get_card_type (winpt_key_t k)
206  {      {    
207      static char buf[64];      static char buf[64];
208    
209      if (!k->ext->card_type)      if (!k->ext || !k->ext->card_type)
210          return "";          return "";
211      _snprintf (buf, sizeof (buf)-1, _("Card-Type: %s\r\n"), k->ext->card_type);      _snprintf (buf, DIM (buf)-1, _("Card-Type: %s\r\n"), k->ext->card_type);
212      return buf;      return buf;
213  }  }
214    
215    
216    /* Return 1 if at least one user-ID is valid. */
217    static int
218    key_is_valid (gpgme_key_t key)
219    {
220        gpgme_user_id_t u;
221    
222        for (u=key->uids; u; u=u->next) {
223            if (u->validity >= GPGME_VALIDITY_MARGINAL)
224                return 1;
225        }
226        return 0;
227    }
228    
229    
230    
231  /* Display the key information for key @k.  /* Display the key information for key @k.
232     Return value: gpgme key on success. */     Return value: gpgme key on success. */
233  static void  static void
234  display_key_info (HWND dlg, winpt_key_t k, gpgme_key_t *r_key)  display_key_info (HWND dlg, winpt_key_t k)
235  {  {
236      struct winpt_key_s k2;      gpgme_key_t key;
237      gpgme_key_t sk, key;      struct winpt_key_s sk;
238      char info[512];      char info[512];
239      const char *inf;      const char *inf;
240      u32 created, expires;          DWORD created, expires;
241    
242      memset (&k2, 0, sizeof (k2));            gpg_keycache_update_attr (k->ext, KC_ATTR_PREFSYM, 0);
243      if (k->key_pair)      memset (&sk, 0, sizeof (sk));
244          winpt_get_seckey (k->keyid, &k2);      if (k->key_pair && !winpt_get_seckey (k->keyid, &sk))
245      else              k->is_protected = sk.is_protected;
246          winpt_get_pubkey (k->keyid, &k2);      key = k->ext->key;
247      sk = k2.ctx;              created = key->subkeys->timestamp;
248      if (sk)      expires = key->subkeys->expires;
         k->is_protected = k2.is_protected;  
     if (get_pubkey (k->keyid, &key))  
         BUG (0);      
     created = key->subkeys->timestamp;    
     expires = key->subkeys->expires;      
249      _snprintf (info, DIM (info)-1,      _snprintf (info, DIM (info)-1,
250                 _("Type: %s\r\n"                 _("Type: %s\r\n"
251                 "Key ID: %s\r\n"                 "Key ID: 0x%s\r\n"
252                 "Algorithm: %s\r\n"                 "Algorithm: %s\r\n"
253                 "Size: %s\r\n"                 "Size: %s bits\r\n"
254                 "Created: %s\r\n"                 "Created: %s\r\n"
255                 "Expires: %s\r\n"                 "Expires: %s\r\n"
256                 "Validity: %s\r\n"                 "Validity: %s\r\n"
# Line 239  display_key_info (HWND dlg, winpt_key_t Line 263  display_key_info (HWND dlg, winpt_key_t
263                 get_key_created (created),                 get_key_created (created),
264                 get_key_expire_date (expires),                 get_key_expire_date (expires),
265                 get_validity (key),                 get_validity (key),
266                 get_pref_cipher (&k2),                 get_pref_cipher (k),
267                 get_card_type (&k2));                 get_card_type (&sk));
268    
269      SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info);      SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info);
270      SetDlgItemText (dlg, IDC_KEYPROPS_FPR, get_key_fpr (key));        SetDlgItemText (dlg, IDC_KEYPROPS_FPR, get_key_fpr (key));  
271      inf = ownertrust_to_string (key->owner_trust);      inf = ownertrust_to_string (key->owner_trust, k->key_pair);
272      SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);      SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);
273    }
274    
275    
276    /* Context to store associated data of the dialog. */
277    struct prop_info_s {
278        winpt_key_t key;
279    };
280    
281    
282      *r_key = key;  static void
283    on_init_dialog (HWND dlg, WPARAM wparam, LPARAM lparam)
284    {
285        gpgme_validity_t valid;
286    
287        winpt_key_t k = (winpt_key_t)lparam;
288        SetWindowText (dlg, _("Key Properties"));
289        SetDlgItemText (dlg, IDC_KEYPROPS_OT_CHANGE, _("&Change"));
290        SetDlgItemText (dlg, IDC_KEYPROPS_REVOKERS, _("&Revokers"));
291        SetDlgItemText (dlg, IDC_KEYPROPS_CHANGE_PWD, _("Change &Password"));
292        SetDlgItemText (dlg, IDC_KEYPROPS_OTINF, _("Ownertrust"));  
293        
294        display_key_info (dlg, k);
295        if (!keyprops_load_photo (dlg, k->ctx, &valid)) {
296            k->has_photo = 1;
297            if (valid < GPGME_VALIDITY_MARGINAL)
298                SetDlgItemText (dlg, IDC_KEYPROPS_IMGINF, _("Photo-ID not validated."));
299        }    
300        if (k->key_pair)
301            EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_CHANGE_PWD), TRUE);
302        if (check_for_desig_rev (k->ctx))
303            EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_REVOKERS), TRUE);
304        if (do_check_key (k->ctx))
305            EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_OT_CHANGE), FALSE);
306        center_window (dlg, NULL);  
307        SetForegroundWindow (dlg);
308  }  }
309    
310    
# Line 255  display_key_info (HWND dlg, winpt_key_t Line 312  display_key_info (HWND dlg, winpt_key_t
312  BOOL CALLBACK  BOOL CALLBACK
313  keyprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  keyprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
314  {  {
315      static winpt_key_t k;      struct prop_info_s *prop = NULL;
     static gpgme_key_t key;  
     gpgme_validity_t valid;  
     refresh_cache_s rcs = {0};  
316      const char *inf;      const char *inf;
317      int rc;      int rc;
318            
319      /* XXX: static variable (k) prevent that the dialog can      if (msg != WM_INITDIALOG &&
320              be opened twice. */          (prop = (prop_info_s*)GetWindowLong (dlg, GWL_USERDATA)) == NULL)
321            return FALSE;
322    
323      switch (msg) {      switch (msg) {
324      case WM_INITDIALOG:      case WM_INITDIALOG:
325          if (!lparam)          assert (lparam != NULL);
326              dlg_fatal_error (dlg, "Could not get dialog param!");          prop = new struct prop_info_s;
327          k = (winpt_key_t)lparam;          prop->key = (winpt_key_t)lparam;
328          SetWindowText (dlg, _("Key Properties"));          SetWindowLong (dlg, GWL_USERDATA, (LONG)prop);
329          SetDlgItemText (dlg, IDC_KEYPROPS_OT_CHANGE, _("&Change"));          on_init_dialog (dlg, wparam, lparam);
         SetDlgItemText (dlg, IDC_KEYPROPS_REVOKERS, _("&Revokers"));  
         SetDlgItemText (dlg, IDC_KEYPROPS_CHANGE_PWD, _("Change &Passwd"));  
         SetDlgItemText (dlg, IDC_KEYPROPS_OTINF, _("Ownertrust"));  
   
         display_key_info (dlg, k, &key);  
         if (!keyprops_load_photo (dlg, key, &valid)) {  
             k->has_photo = 1;    
             if (valid < GPGME_VALIDITY_MARGINAL)  
                 SetDlgItemText (dlg, IDC_KEYPROPS_IMGINF, _("Photo-ID not validated."));  
         }  
         if (k->key_pair)  
             EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_CHANGE_PWD), TRUE);  
         if (check_for_desig_rev (key))  
             EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_REVOKERS), TRUE);  
         center_window (dlg, NULL);  
         SetForegroundWindow (dlg);        
330          return TRUE;          return TRUE;
331    
332      case WM_DESTROY:      case WM_DESTROY:
333          remove (get_photo_tmpname (dlg));          remove (get_photo_tmpname (dlg));
334            delete prop;prop=NULL;
335            SetWindowLong (dlg, GWL_USERDATA, 0);
336          break;          break;
337                
338      case WM_PAINT:      case WM_PAINT:
339          if (k->has_photo)          if (prop->key->has_photo)
340              keyprops_show_photo (dlg);              keyprops_show_photo (dlg);
341          break;          break;
   
     case WM_SYSCOMMAND:  
         if (LOWORD (wparam) == SC_CLOSE)  
             EndDialog (dlg, TRUE);  
         return FALSE;  
342                    
343      case WM_COMMAND:      case WM_COMMAND:
344          switch (LOWORD (wparam)) {          switch (LOWORD (wparam)) {
345          case IDOK:          case IDOK:
346              EndDialog (dlg, TRUE);              EndDialog (dlg, TRUE);
347              return TRUE;              return TRUE;
348    
349            case IDCANCEL:
350                EndDialog (dlg, FALSE);
351                return TRUE;
352                            
353          case IDC_KEYPROPS_OT_CHANGE:          case IDC_KEYPROPS_OT_CHANGE:
354              if (do_check_key (key)) {              if (do_check_key (prop->key->ctx)) {
355                  msg_box (dlg, _("The status of this key is 'revoked' or 'expired'.\n"                  msg_box (dlg, _("The status of this key is 'revoked' or 'expired'.\n"
356                                  "You cannot change the ownertrust of such keys."),                                  "You cannot change the ownertrust of such keys."),
357                                  _("WinPT Warning"), MB_ERR);                                  _("WinPT Warning"), MB_ERR);
358                  return TRUE;                  return TRUE;
359              }              }
360              if( !k->key_pair && key->uids->validity < 3 ) {              if (!prop->key->key_pair && !key_is_valid (prop->key->ctx)) {
361                  rc = msg_box( dlg, _("This is a non-valid key.\n"                  rc = msg_box (dlg, _("This is a non-valid key.\n"
362                                       "Modifying the ownertrust has no effect on such keys.\n\n"                                       "Modifying the ownertrust has no effect on such keys.\n\n"
363                                       "Do you really want to continue?"),                                       "Do you really want to continue?"),
364                                       _("WinPT Warning"), MB_ICONWARNING|MB_YESNO );                                       _("WinPT Warning"), MB_ICONWARNING|MB_YESNO);
365                  if (rc == IDNO)                  if (rc == IDNO)
366                      return TRUE;                      return TRUE;
367              }              }
368                rc = dialog_box_param (glob_hinst,
369                                       (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST,
370                                        dlg, keyedit_ownertrust_dlg_proc,
371                                       (LPARAM)prop->key, _("Change Ownertrust"),
372                                        IDS_WINPT_KEYEDIT_OWNERTRUST);
373                if (rc == FALSE) /* Cancel */
374                    return TRUE;
375    
376              dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST,              inf = ownertrust_to_string (prop->key->callback.new_val,
377                                dlg, (DLGPROC)keyedit_ownertrust_dlg_proc,                                          prop->key->key_pair);
                               (LPARAM)k, _("Change Ownertrust"),  
                               IDS_WINPT_KEYEDIT_OWNERTRUST);  
             if (k->callback.new_val == -1) { /* Cancel */  
                 EndDialog (dlg, FALSE);  
                 break;  
             }  
   
             inf = ownertrust_to_string (k->callback.new_val);  
378              SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);              SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);
379              msg_box (dlg, _("Ownertrust successfully changed."),              msg_box (dlg, _("Ownertrust successfully changed."),
380                       _("GnuPG Status"), MB_OK);                       _("GnuPG Status"), MB_OK);
381                            prop->key->update = 1;
             /* XXX: modified ownertrust values can effect the entire  
                     WoT so we reload the cache. But this is very slow. */  
             memset (&rcs, 0, sizeof (rcs));  
             rcs.kr_reload = 1; rcs.kr_update = 1; /* reload only keylist */  
             DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, dlg,  
                             keycache_dlg_proc, (LPARAM)&rcs);  
382              return TRUE;              return TRUE;
383                            
384          case IDC_KEYPROPS_CHANGE_PWD:          case IDC_KEYPROPS_CHANGE_PWD:
385              keyedit_change_passwd (k, dlg);                      keyedit_change_passwd (prop->key, dlg);        
386              return TRUE;              return TRUE;
387    
388          case IDC_KEYPROPS_REVOKERS:          case IDC_KEYPROPS_REVOKERS:
389              dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg,              prop->key->update = dialog_box_param (glob_hinst,
390                                key_revokers_dlg_proc, (LPARAM)key,                                            (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg,
391                                _("Key Revokers"), IDS_WINPT_KEY_REVOKERS);                                            key_revokers_dlg_proc, (LPARAM)prop->key,
392                                              _("Key Revokers"),
393                                              IDS_WINPT_KEY_REVOKERS);
394                UpdateWindow (dlg);
395              break;              break;
396          }          }
397      }      }

Legend:
Removed from v.77  
changed lines
  Added in v.217

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26