/[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 174 by twoaday, Thu Feb 2 08:20:50 2006 UTC revision 273 by twoaday, Fri Dec 8 10:22:17 2006 UTC
# Line 1  Line 1 
1  /* wptKeyPropsDlg.cpp - WinPT key properties dialog  /* wptKeyPropsDlg.cpp - WinPT key property dialog
2   *      Copyright (C) 2000, 2001, 2002, 2003, 2005, 2006 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.
# Line 22  Line 22 
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 41  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;
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    
# Line 76  ownertrust_to_string (int val, bool is_k Line 78  ownertrust_to_string (int val, bool is_k
78    
79  /* Generate a unique temp name for the photo which  /* Generate a unique temp name for the photo which
80     depends on the dialog handle and return it. */     depends on the dialog handle and return it. */
81  static const char*  const char*
82  get_photo_tmpname (HWND dlg)  get_photo_tmpname (HWND dlg)
83  {  {
84      static char buf[MAX_PATH+128+1];      static char buf[MAX_PATH+128+1];
85      char name[64];      char name[64];
86    
87      _snprintf (name, sizeof (name)-1, "winpt_photo_%08lX.tmp", (DWORD)dlg);      _snprintf (name, DIM (name)-1, "winpt_photo_%08lX.tmp", (DWORD)dlg);
88      GetTempPath (sizeof (buf)-128, buf);      get_temp_name (buf, DIM (buf)-1, name);
     strcat (buf, name);  
   
89      return buf;      return buf;
90  }  }
91    
# Line 99  draw_nophoto_img (HWND dlg) Line 99  draw_nophoto_img (HWND dlg)
99  }  }
100    
101    
102    
103    /* Delete temporary photo file. */
104    void
105    key_unload_photo (HWND dlg)
106    {
107        DeleteFile (get_photo_tmpname (dlg));
108    }
109    
110    
111  /* Load the photo from the key @key */  /* Load the photo from the key @key */
112  static int  int
113  keyprops_load_photo (HWND dlg, gpgme_key_t key, gpgme_validity_t *r_valid)  key_load_photo (HWND dlg, gpgme_key_t key, gpgme_validity_t *r_valid)
114  {  {
115      winpt_key_s k;      winpt_key_s k;
116      FILE *f;      FILE *fp;
117      const BYTE *img;      const BYTE *img;
118      DWORD imglen = 0;      DWORD imglen = 0;
119      int pos=0;      int pos=0;
120    
121      winpt_get_pubkey (key->subkeys->keyid, &k);      if (winpt_get_pubkey (key->subkeys->keyid, &k))
122            BUG (0);
123      img = k.ext->attrib.d;      img = k.ext->attrib.d;
124      imglen = k.ext->attrib.len;      imglen = k.ext->attrib.len;
125      if (!k.ext->attrib.validity)      if (img && !k.ext->attrib.validity)
126          get_uat_validity (key->subkeys->keyid, &k.ext->attrib.validity);          get_uat_validity (key->subkeys->keyid, &k.ext->attrib.validity);
127      *r_valid = k.ext->attrib.validity;      if (r_valid)
128            *r_valid = k.ext->attrib.validity;
129    
130      if (!img || !imglen) {      if (!img || !imglen) {
131          draw_nophoto_img (dlg);          draw_nophoto_img (dlg);
132          return -1;          return -1;
133      }      }
134    
135      f = fopen (get_photo_tmpname (dlg), "wb");      fp = fopen (get_photo_tmpname (dlg), "wb");
136      if (f) {      if (fp) {
         //for (pos = 0; img[pos] != 0x10; pos++)  
         //      ;  
137          pos += 16;          pos += 16;
138          fwrite (img + pos, 1, imglen - pos, f);          fwrite (img + pos, 1, imglen - pos, fp);
139          fclose (f);          fclose (fp);
140      }      }
141      return 0;      return 0;
142  }  }
143    
144    
 /* Display the photo in the image control in the dialog @dlg. */  
 static int  
 keyprops_show_photo (HWND dlg)  
 {  
     RECT r;      
     POINT p;  
     HWND h;  
   
     h = GetDlgItem (dlg, IDC_KEYPROPS_IMG);  
     GetWindowRect (h, &r);  
     p.x = r.left + 5;  
     p.y = r.top - 2;  
     memset (&p, 0, sizeof (p));  
     PTD_jpg_show (h, &p, get_photo_tmpname (dlg));  
       
     return 0;  
 }  
   
   
145  /* Return string representation of the key validity. @key. */  /* Return string representation of the key validity. @key. */
146  static const char*  static const char*
147  get_validity (gpgme_key_t key)  get_validity (gpgme_key_t key)
148  {  {
149      int val;      if (key->expired)
     val = key->expired;  
     if (val)  
150          return _("Expired");              return _("Expired");    
151      val = key->revoked;      if (key->revoked)
     if (val)  
152          return _("Revoked");          return _("Revoked");
153      val = key->disabled;      if (key->disabled)
     if (val)  
154          return _("Disabled");          return _("Disabled");
155        if (key->invalid)
156            return _("Invalid");
157      return get_key_trust2 (NULL, key->uids->validity, 0, 0);      return get_key_trust2 (NULL, key->uids->validity, 0, 0);
158  }  }
159    
# Line 176  get_pref_cipher (winpt_key_t k) Line 164  get_pref_cipher (winpt_key_t k)
164  {  {
165      if (k->is_v3)      if (k->is_v3)
166          return "IDEA";          return "IDEA";
167      if (!k->ext->sym_prefs)      if (!k->ext || !k->ext->sym_prefs)
168          return "3DES";          return "3DES";
169      switch (*k->ext->sym_prefs) {      switch (*k->ext->sym_prefs) {
170      case 1: return "IDEA";      case 1: return "IDEA";
171      case 2: return "3DES";      case 2: return "3DES";
172      case 3: return "CAST5";      case 3: return "CAST5";
173      case 4: return "Blowfish";      case 4: return "Blowfish";
174      case 7:      case 7: return "AES128";
175      case 8:      case 8: return "AES192";
176      case 9: return "AES";      case 9: return "AES256";
177      case 10:return "Twofish";      case 10:return "Twofish";
178        default:break;
179      }      }
180      return "Unknown";      return "Unknown";
181  }  }
# Line 197  static bool Line 186  static bool
186  check_for_desig_rev (gpgme_key_t key)  check_for_desig_rev (gpgme_key_t key)
187  {  {
188      winpt_key_s k;      winpt_key_s k;
189    
190      memset (&k, 0, sizeof (k));      memset (&k, 0, sizeof (k));
191      if (!winpt_get_pubkey (key->subkeys->keyid, &k))      if (!winpt_get_pubkey (key->subkeys->keyid, &k))
192          return k.ext->gloflags.has_desig_rev? true : false;          return k.ext->gloflags.has_desig_rev? true : false;
# Line 210  get_card_type (winpt_key_t k) Line 200  get_card_type (winpt_key_t k)
200  {      {    
201      static char buf[64];      static char buf[64];
202    
203      if (!k->ext->card_type)      if (!k->ext || !k->ext->card_type)
204          return "";          return "";
205      _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);
206      return buf;      return buf;
207  }  }
208    
209    
210    /* Return 1 if at least one user-ID is valid. */
211    static int
212    key_is_valid (gpgme_key_t key)
213    {
214        gpgme_user_id_t u;
215    
216        for (u=key->uids; u; u=u->next) {
217            if (u->validity >= GPGME_VALIDITY_MARGINAL)
218                return 1;
219        }
220        return 0;
221    }
222    
223    
224    /* Return extended algorithm information. */
225    const char*
226    props_get_key_algo (gpgme_key_t key, int idx)
227    {
228        /* PGP calls the old RSAv3 keys 'RSA Legacy' and because this
229           is a good method to differ between OpenPGP v4 cert-only keys
230           and v3 RSA keys, we use the same notation. */
231        if (key->subkeys != NULL && strlen (key->subkeys->fpr) == 32)
232            return "RSA Legacy";
233        return get_key_algo (key, idx);
234    }
235    
236    
237  /* Display the key information for key @k.  /* Display the key information for key @k.
238     Return value: gpgme key on success. */     Return value: gpgme key on success. */
239  static void  static void
240  display_key_info (HWND dlg, winpt_key_t k, gpgme_key_t *r_key)  display_key_info (HWND dlg, winpt_key_t k)
241  {  {
242      struct winpt_key_s k2;      gpgme_key_t key;
243      gpgme_key_t sk, key;      struct winpt_key_s sk;
244      char info[512];      char info[512];
245      const char *inf;      const char *inf;
246      u32 created, expires;      DWORD created, expires;
247    
248      memset (&k2, 0, sizeof (k2));            gpg_keycache_update_attr (k->ext, KC_ATTR_PREFSYM, 0);
249      if (k->key_pair)      memset (&sk, 0, sizeof (sk));
250          winpt_get_seckey (k->keyid, &k2);      if (k->key_pair && !winpt_get_seckey (k->keyid, &sk))
251      else              k->is_protected = sk.is_protected;
252          winpt_get_pubkey (k->keyid, &k2);      key = k->ext->key;
253      sk = k2.ctx;      created = key->subkeys->timestamp;
254      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;      
255      _snprintf (info, DIM (info)-1,      _snprintf (info, DIM (info)-1,
256                 _("Type: %s\r\n"                 _("Type: %s\r\n"
257                 "Key ID: %s\r\n"                 "Key ID: 0x%s\r\n"
258                 "Algorithm: %s\r\n"                 "Algorithm: %s\r\n"
259                 "Size: %s bits\r\n"                 "Size: %s bits\r\n"
260                 "Created: %s\r\n"                 "Created: %s\r\n"
# Line 252  display_key_info (HWND dlg, winpt_key_t Line 264  display_key_info (HWND dlg, winpt_key_t
264                 "%s\r\n"),                 "%s\r\n"),
265                 get_key_type (key),                 get_key_type (key),
266                 k->keyid,                 k->keyid,
267                 get_key_algo (key, 0),                 props_get_key_algo (key, 0),
268                 get_key_size (key, 0),                 get_key_size (key, 0),
269                 get_key_created (created),                 get_key_created (created),
270                 get_key_expire_date (expires),                 get_key_expire_date (expires),
271                 get_validity (key),                 get_validity (key),
272                 get_pref_cipher (&k2),                 get_pref_cipher (k),
273                 get_card_type (&k2));                 get_card_type (&sk));
274    
275      SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info);      SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info);
276      SetDlgItemText (dlg, IDC_KEYPROPS_FPR, get_key_fpr (key));        SetDlgItemText (dlg, IDC_KEYPROPS_FPR, get_key_fpr (key));  
277      inf = ownertrust_to_string (key->owner_trust, k->key_pair);      inf = ownertrust_to_string (key->owner_trust, k->key_pair);
278      SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);      SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);
279    }
280    
281    
282    /* Context to store associated data of the dialog. */
283    struct prop_info_s {
284        winpt_key_t key;
285    };
286    
287      *r_key = key;  
288    static void
289    on_init_dialog (HWND dlg, WPARAM wparam, LPARAM lparam)
290    {
291        gpgme_validity_t valid;
292    
293        winpt_key_t k = (winpt_key_t)lparam;
294        SetWindowText (dlg, _("Key Properties"));
295        SetDlgItemText (dlg, IDC_KEYPROPS_OT_CHANGE, _("&Change"));
296        SetDlgItemText (dlg, IDC_KEYPROPS_REVOKERS, _("&Revokers"));
297        SetDlgItemText (dlg, IDC_KEYPROPS_CHANGE_PWD, _("Change &Password"));
298        SetDlgItemText (dlg, IDC_KEYPROPS_OTINF, _("Ownertrust"));  
299        
300        display_key_info (dlg, k);
301        if (!key_load_photo (dlg, k->ctx, &valid)) {
302            k->has_photo = 1;
303            if (valid < GPGME_VALIDITY_MARGINAL)
304                SetDlgItemText (dlg, IDC_KEYPROPS_IMGINF, _("Photo-ID not validated."));
305        }
306        if (k->key_pair)
307            EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_CHANGE_PWD), TRUE);
308        if (check_for_desig_rev (k->ctx))
309            EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_REVOKERS), TRUE);
310        if (do_check_key (k->ctx))
311            EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_OT_CHANGE), FALSE);
312        center_window (dlg, NULL);  
313        SetForegroundWindow (dlg);
314  }  }
315    
316    
# Line 273  display_key_info (HWND dlg, winpt_key_t Line 318  display_key_info (HWND dlg, winpt_key_t
318  BOOL CALLBACK  BOOL CALLBACK
319  keyprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  keyprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
320  {  {
321      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};  
322      const char *inf;      const char *inf;
     int cancel = 0;  
323      int rc;      int rc;
324            
325      /* XXX: static variable (k) prevent that the dialog can      if (msg != WM_INITDIALOG &&
326              be opened twice. */          (prop = (prop_info_s*)GetWindowLong (dlg, GWL_USERDATA)) == NULL)
327            return FALSE;
328    
329      switch (msg) {      switch (msg) {
330      case WM_INITDIALOG:      case WM_INITDIALOG:
331          if (!lparam)          assert (lparam != 0);
332              dlg_fatal_error (dlg, "Could not get dialog param!");          prop = new struct prop_info_s;
333          k = (winpt_key_t)lparam;          prop->key = (winpt_key_t)lparam;
334          SetWindowText (dlg, _("Key Properties"));          SetWindowLong (dlg, GWL_USERDATA, (LONG)prop);
335          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 &Password"));  
         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);  
         if (key->revoked || key->expired)  
             EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_OT_CHANGE), FALSE);  
         center_window (dlg, NULL);  
         SetForegroundWindow (dlg);        
336          return TRUE;          return TRUE;
337    
338      case WM_DESTROY:      case WM_DESTROY:
339          remove (get_photo_tmpname (dlg));          key_unload_photo (dlg);
340            delete prop;prop = NULL;
341            SetWindowLong (dlg, GWL_USERDATA, 0);
342          break;          break;
343                
344      case WM_PAINT:      case WM_PAINT:
345          if (k->has_photo)          /* Display the photo in the frame of the dialog @dlg.
346              keyprops_show_photo (dlg);             The coordinates are fixed to (0,0). */
347            if (prop->key->has_photo) {
348                POINT p;
349                p.x = p.y = 0;
350                PTD_jpg_show (GetDlgItem (dlg, IDC_KEYPROPS_IMG),
351                              &p, get_photo_tmpname (dlg));
352            }
353          break;          break;
354    
     case WM_SYSCOMMAND:  
         if (LOWORD (wparam) == SC_CLOSE)  
             EndDialog (dlg, FALSE);  
         return FALSE;  
           
355      case WM_COMMAND:      case WM_COMMAND:
356          switch (LOWORD (wparam)) {          switch (LOWORD (wparam)) {
357          case IDOK:          case IDOK:
# Line 335  keyprops_dlg_proc (HWND dlg, UINT msg, W Line 363  keyprops_dlg_proc (HWND dlg, UINT msg, W
363              return TRUE;              return TRUE;
364                            
365          case IDC_KEYPROPS_OT_CHANGE:          case IDC_KEYPROPS_OT_CHANGE:
366              if (do_check_key (key)) {              if (!prop->key->key_pair && !key_is_valid (prop->key->ctx)) {
                 msg_box (dlg, _("The status of this key is 'revoked' or 'expired'.\n"  
                                 "You cannot change the ownertrust of such keys."),  
                                 _("WinPT Warning"), MB_ERR);  
                 return TRUE;  
             }  
             if (!k->key_pair && key->uids->validity < 3) {  
367                  rc = msg_box (dlg, _("This is a non-valid key.\n"                  rc = msg_box (dlg, _("This is a non-valid key.\n"
368                                       "Modifying the ownertrust has no effect on such keys.\n\n"                                       "Modifying the ownertrust has no effect on such keys.\n\n"
369                                       "Do you really want to continue?"),                                       "Do you really want to continue?"),
# Line 352  keyprops_dlg_proc (HWND dlg, UINT msg, W Line 374  keyprops_dlg_proc (HWND dlg, UINT msg, W
374              rc = dialog_box_param (glob_hinst,              rc = dialog_box_param (glob_hinst,
375                                     (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST,                                     (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST,
376                                      dlg, keyedit_ownertrust_dlg_proc,                                      dlg, keyedit_ownertrust_dlg_proc,
377                                     (LPARAM)k, _("Change Ownertrust"),                                     (LPARAM)prop->key, _("Change Ownertrust"),
378                                      IDS_WINPT_KEYEDIT_OWNERTRUST);                                      IDS_WINPT_KEYEDIT_OWNERTRUST);
379              if (rc == FALSE) /* Cancel */              if (rc == FALSE) /* Cancel */
380                  return TRUE;                  return TRUE;
381    
382              inf = ownertrust_to_string (k->callback.new_val, k->key_pair);              inf = ownertrust_to_string (prop->key->callback.new_val,
383                                            prop->key->key_pair);
384              SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);              SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);
385              msg_box (dlg, _("Ownertrust successfully changed."),              msg_box (dlg, _("Ownertrust successfully changed."),
386                       _("GnuPG Status"), MB_OK);                       _("GnuPG Status"), MB_OK);
387              k->update = 1;              prop->key->update = 1;
388              return TRUE;              return TRUE;
389                            
390          case IDC_KEYPROPS_CHANGE_PWD:          case IDC_KEYPROPS_CHANGE_PWD:
391              keyedit_change_passwd (k, dlg);                      keyedit_change_passwd (prop->key, dlg);        
392              return TRUE;              return TRUE;
393    
394          case IDC_KEYPROPS_REVOKERS:          case IDC_KEYPROPS_REVOKERS:
395              k->update = dialog_box_param (              prop->key->update = dialog_box_param (glob_hinst,
396                  glob_hinst, (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg,                                            (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg,
397                  key_revokers_dlg_proc, (LPARAM)key,                                            key_revokers_dlg_proc, (LPARAM)prop->key,
398                  _("Key Revokers"), IDS_WINPT_KEY_REVOKERS);                                            _("Key Revokers"),
399                                              IDS_WINPT_KEY_REVOKERS);
400                UpdateWindow (dlg);
401              break;              break;
402          }          }
403      }      }

Legend:
Removed from v.174  
changed lines
  Added in v.273

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26