/[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 211 by twoaday, Sun May 7 12:36:48 2006 UTC revision 392 by twoaday, Sun Jan 15 18:05:11 2012 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-2003, 2005-2006, 2008 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
# Line 12  Line 12 
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.   * GNU General Public License for more details.
  *  
  * You should have received a copy of the GNU General Public License  
  * along with WinPT; if not, write to the Free Software Foundation,  
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA  
15   */   */
16  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
17  #include <config.h>  #include <config.h>
18  #endif  #endif
19    
20  #include <windows.h>  #include <windows.h>
21    #include <assert.h>
22    
23  #include "resource.h"  #include "resource.h"
24  #include "wptErrors.h"  #include "wptErrors.h"
# Line 35  Line 32 
32  #include "wptW32API.h"  #include "wptW32API.h"
33  #include "wptVersion.h"  #include "wptVersion.h"
34  #include "wptKeyEdit.h"  #include "wptKeyEdit.h"
35    #include "StringBuffer.h"
36    
37    
38    /* Context to store associated data of the dialog. */
39    struct prop_info_s {
40        winpt_key_t key;
41        char photo_file[MAX_PATH+128+1];
42        bool has_photo;
43    };
44    
45    
46  /* Check that the key is not expired or revoked. */  /* Check that the key is not expired or revoked. */
47  static int  static int
48  do_check_key (gpgme_key_t key)  do_check_key (gpgme_key_t key)
49  {  {
50      int okay = 0;      int invalid = key->expired;
51      okay = key->expired;      if (!invalid)
52      if (!okay)          invalid = key->revoked;
53          okay = key->revoked;      return invalid;
     return okay;  
54  }  }
55    
56    
# Line 73  ownertrust_to_string (int val, bool is_k Line 78  ownertrust_to_string (int val, bool is_k
78      return inf;      return inf;
79  }  }
80    
81    /* Generate a temporary name for the photo ID */
82  /* Generate a unique temp name for the photo which  int
83     depends on the dialog handle and return it. */  get_photo_tmpname (gpgme_key_t key, char *buf, size_t buflen)
 static const char*  
 get_photo_tmpname (HWND dlg)  
84  {  {
85      static char buf[MAX_PATH+128+1];      const char *fmt = "winpt_photo_%p.tmp";
86      char name[64];      char name[64];
87    
88      _snprintf (name, sizeof (name)-1, "winpt_photo_%08lX.tmp", (DWORD)dlg);      if (buflen < (MAX_PATH+strlen(fmt)+8+1))
89      get_temp_name (buf, DIM (buf)-1, name);          return WPTERR_GENERAL;
90      return buf;      _snprintf (name, DIM (name)-1, fmt, key);
91        get_temp_name (buf, buflen-1, name);
92        return 0;
93  }  }
94    
95    
 static void  
 draw_nophoto_img (HWND dlg)  
 {  
     /*..  
     n = DrawText (hdc, "No Photo-ID", -1, &r, DT_LEFT);  
     ..*/  
 }  
96    
97    
98  /* Load the photo from the key @key */  /* Load the photo from the key @key */
99  static int  int
100  keyprops_load_photo (HWND dlg, gpgme_key_t key, gpgme_validity_t *r_valid)  key_load_photo (winpt_key_t key,
101  {                  char *photo_file, size_t photo_file_size,
102      winpt_key_s k;                  gpgme_validity_t *r_valid)
103      FILE *f;  {
104      const BYTE *img;      const BYTE *img = key->ext->attrib.d;
105      DWORD imglen = 0;      DWORD imglen = key->ext->attrib.len;
106      int pos=0;      if (img && !key->ext->attrib.validity)
107            get_uat_validity (key->ctx->subkeys->keyid,
108      if (winpt_get_pubkey (key->subkeys->keyid, &k))                            &key->ext->attrib.validity);
109          BUG (0);      if (r_valid)
110      img = k.ext->attrib.d;          *r_valid = key->ext->attrib.validity;
     imglen = k.ext->attrib.len;  
     if (!k.ext->attrib.validity)  
         get_uat_validity (key->subkeys->keyid, &k.ext->attrib.validity);  
     *r_valid = k.ext->attrib.validity;  
111    
112      if (!img || !imglen) {      if (!img || imglen < 1)
113          draw_nophoto_img (dlg);          return -1;
114                
115        get_photo_tmpname (key->ctx, photo_file, photo_file_size);
116        FILE *fp = fopen (photo_file, "wb");
117        if (fp == NULL)
118          return -1;          return -1;
     }  
   
     f = fopen (get_photo_tmpname (dlg), "wb");  
     if (f) {  
         pos += 16;  
         fwrite (img + pos, 1, imglen - pos, f);  
         fclose (f);  
     }  
     return 0;  
 }  
   
   
 /* 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));  
119            
120        const int HEADER_OFF = 16;
121        fwrite (img + HEADER_OFF, 1, imglen - HEADER_OFF, fp);
122        fclose (fp);
123      return 0;      return 0;
124  }  }
125    
# Line 159  get_validity (gpgme_key_t key) Line 134  get_validity (gpgme_key_t key)
134          return _("Revoked");          return _("Revoked");
135      if (key->disabled)      if (key->disabled)
136          return _("Disabled");          return _("Disabled");
137        if (key->invalid)
138            return _("Invalid");
139      return get_key_trust2 (NULL, key->uids->validity, 0, 0);      return get_key_trust2 (NULL, key->uids->validity, 0, 0);
140  }  }
141    
142    
 /* Return the preferred sym. algorithm from @key as a string. */  
 static const char*  
 get_pref_cipher (winpt_key_t k)  
 {  
     if (k->is_v3)  
         return "IDEA";  
     if (!k->ext || !k->ext->sym_prefs)  
         return "3DES";  
     switch (*k->ext->sym_prefs) {  
     case 1: return "IDEA";  
     case 2: return "3DES";  
     case 3: return "CAST5";  
     case 4: return "Blowfish";  
     case 7: return "AES128";  
     case 8: return "AES192";  
     case 9: return "AES256";  
     case 10:return "Twofish";  
     }  
     return "Unknown";  
 }  
   
   
143  /* Return true if the key has designated revokers. */  /* Return true if the key has designated revokers. */
144  static bool  static bool
145  check_for_desig_rev (gpgme_key_t key)  check_for_desig_rev (gpgme_key_t key)
146  {  {
147      winpt_key_s k;      winpt_key_s k;
148    
149      memset (&k, 0, sizeof (k));      memset (&k, 0, sizeof (k));
150      if (!winpt_get_pubkey (key->subkeys->keyid, &k))      if (!winpt_get_pubkey (key->subkeys->keyid, &k))
151          return k.ext->gloflags.has_desig_rev? true : false;          return k.ext->gloflags.has_desig_rev? true : false;
# Line 205  get_card_type (winpt_key_t k) Line 161  get_card_type (winpt_key_t k)
161    
162      if (!k->ext || !k->ext->card_type)      if (!k->ext || !k->ext->card_type)
163          return "";          return "";
164      _snprintf (buf, sizeof (buf)-1, _("Card-Type: %s\r\n"), k->ext->card_type);      _snprintf (buf, DIM (buf)-1, _("Card-Type: %s\r\n"),
165                   k->ext->card_type);
166      return buf;      return buf;
167  }  }
168    
169    
170  /* Return 1 if at least one user-ID is valid. */  /* Return 1 if at least one user-ID is valid. */
171  static int  static int
172  key_is_valid (gpgme_key_t k)  key_is_valid (gpgme_key_t key)
173  {  {
174      gpgme_user_id_t u;      gpgme_user_id_t u;
175    
176      for (u=k->uids; u; u=u->next) {      for (u=key->uids; u; u=u->next) {
177          if (u->validity >= GPGME_VALIDITY_MARGINAL)          if (u->validity >= GPGME_VALIDITY_MARGINAL)
178              return 1;              return 1;
179      }      }
# Line 224  key_is_valid (gpgme_key_t k) Line 181  key_is_valid (gpgme_key_t k)
181  }  }
182    
183    
184    /* Return extended algorithm information. */
185    const char*
186    props_get_key_algo (gpgme_key_t key, int idx)
187    {
188        /* PGP calls the old RSAv3 keys 'RSA Legacy' and because this
189           is a good method to differ between OpenPGP v4 cert-only keys
190           and v3 RSA keys, we use the same notation. */
191        if (key->subkeys != NULL && strlen (key->subkeys->fpr) == 32)
192            return "RSA Legacy";
193        return get_key_algo (key, idx);
194    }
195    
196  /* Display the key information for key @k.  
197     Return value: gpgme key on success. */  /* Display the key information for key @k. */
198  static void  static void
199  display_key_info (HWND dlg, winpt_key_t k)  display_key_info (HWND dlg, winpt_key_t k)
200  {  {
# Line 237  display_key_info (HWND dlg, winpt_key_t Line 205  display_key_info (HWND dlg, winpt_key_t
205      DWORD created, expires;      DWORD created, expires;
206    
207      gpg_keycache_update_attr (k->ext, KC_ATTR_PREFSYM, 0);      gpg_keycache_update_attr (k->ext, KC_ATTR_PREFSYM, 0);
208      memset (&sk, 0, sizeof (sk));            memset (&sk, 0, sizeof (sk));
209      if (k->key_pair && !winpt_get_seckey (k->keyid, &sk))      if (k->key_pair && !winpt_get_seckey (k->keyid, &sk))
210          k->is_protected = sk.is_protected;          k->is_protected = sk.is_protected;
211      key = k->ext->key;      key = k->ext->key;
# Line 251  display_key_info (HWND dlg, winpt_key_t Line 219  display_key_info (HWND dlg, winpt_key_t
219                 "Created: %s\r\n"                 "Created: %s\r\n"
220                 "Expires: %s\r\n"                 "Expires: %s\r\n"
221                 "Validity: %s\r\n"                 "Validity: %s\r\n"
                "Cipher: %s\r\n"  
222                 "%s\r\n"),                 "%s\r\n"),
223                 get_key_type (key),                 get_key_type (key),
224                 k->keyid,                 k->keyid,
225                 get_key_algo (key, 0),                 props_get_key_algo (key, 0),
226                 get_key_size (key, 0),                 get_key_size (key, 0),
227                 get_key_created (created),                 get_key_created (created),
228                 get_key_expire_date (expires),                 get_key_expire_date (expires),
229                 get_validity (key),                 get_validity (key),
                get_pref_cipher (k),  
230                 get_card_type (&sk));                 get_card_type (&sk));
231    
232      SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info);      SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info);
# Line 270  display_key_info (HWND dlg, winpt_key_t Line 236  display_key_info (HWND dlg, winpt_key_t
236  }  }
237    
238    
239    
240    static void
241    on_init_dialog (HWND dlg, WPARAM wparam, LPARAM lparam)
242    {
243        winpt_key_t k = (winpt_key_t)lparam;
244        assert (k != NULL);
245    
246        StringBuffer s = _("Key Properties");
247        if (k->ext->key->revoked)
248            s = s + " - " + _("REVOKED KEY");
249        
250        SetWindowText (dlg, s.getBuffer());
251        SetDlgItemText (dlg, IDC_KEYPROPS_OT_CHANGE, _("&Change"));
252        SetDlgItemText (dlg, IDC_KEYPROPS_REVOKERS, _("&Revokers"));
253        SetDlgItemText (dlg, IDC_KEYPROPS_CHANGE_PWD, _("Change &Password"));
254        SetDlgItemText (dlg, IDC_KEYPROPS_OTINF, _("Ownertrust:"));
255        SetDlgItemText (dlg, IDC_KEYPROPS_FPRTXT, _("Fingerprint:"));
256        
257        display_key_info (dlg, k);
258        
259        if (k->key_pair)
260            EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_CHANGE_PWD), TRUE);
261        if (check_for_desig_rev (k->ctx))
262            EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_REVOKERS), TRUE);
263        if (do_check_key (k->ctx))
264            EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_OT_CHANGE), FALSE);
265        center_window (dlg, NULL);  
266        SetForegroundWindow (dlg);
267    }
268    
269    
270  /* Dialog box procedure to show the key properties. */  /* Dialog box procedure to show the key properties. */
271  BOOL CALLBACK  BOOL CALLBACK
272  keyprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  keyprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
273  {  {
274      static winpt_key_t k;      struct prop_info_s *prop = NULL;
     gpgme_validity_t valid;  
275      const char *inf;      const char *inf;
276        gpgme_validity_t valid;
277      int rc;      int rc;
278            
279      /* XXX: static variable (k) prevent that the dialog can      if (msg != WM_INITDIALOG) {
280              be opened twice. */          prop = (struct prop_info_s*)GetWindowLong (dlg, GWL_USERDATA);
281            if (prop == NULL)
282                return FALSE;
283        }
284    
285      switch (msg) {      switch (msg) {
286      case WM_INITDIALOG:      case WM_INITDIALOG:
287          if (!lparam)          assert (lparam != 0);
288              dlg_fatal_error (dlg, "Could not get dialog param!");          prop = new struct prop_info_s;
289          k = (winpt_key_t)lparam;          prop->key = (winpt_key_t)lparam;
290          SetWindowText (dlg, _("Key Properties"));          prop->has_photo = 0;
291          SetDlgItemText (dlg, IDC_KEYPROPS_OT_CHANGE, _("&Change"));          SetWindowLong (dlg, GWL_USERDATA, (LONG)prop);
292          SetDlgItemText (dlg, IDC_KEYPROPS_REVOKERS, _("&Revokers"));          on_init_dialog (dlg, wparam, lparam);
293          SetDlgItemText (dlg, IDC_KEYPROPS_CHANGE_PWD, _("Change &Password"));          if (!key_load_photo (prop->key,
294          SetDlgItemText (dlg, IDC_KEYPROPS_OTINF, _("Ownertrust"));                               prop->photo_file, DIM (prop->photo_file)-1,
295                                         &valid)) {
296          display_key_info (dlg, k);              prop->key->has_photo = 1;
297          if (!keyprops_load_photo (dlg, k->ctx, &valid)) {              prop->has_photo = true;
             k->has_photo = 1;    
298              if (valid < GPGME_VALIDITY_MARGINAL)              if (valid < GPGME_VALIDITY_MARGINAL)
299                  SetDlgItemText (dlg, IDC_KEYPROPS_IMGINF, _("Photo-ID not validated."));                  SetDlgItemText (dlg, IDC_KEYPROPS_IMGINF,
300                                    _("Photo-ID not validated."));
301          }          }
         if (k->key_pair)  
             EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_CHANGE_PWD), TRUE);  
         if (check_for_desig_rev (k->ctx))  
             EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_REVOKERS), TRUE);  
         if (k->ctx->revoked || k->ctx->expired)  
             EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_OT_CHANGE), FALSE);  
         center_window (dlg, NULL);  
         SetForegroundWindow (dlg);        
302          return TRUE;          return TRUE;
303    
304      case WM_DESTROY:      case WM_DESTROY:
305          remove (get_photo_tmpname (dlg));          if (prop->has_photo)
306                DeleteFile(prop->photo_file);
307            delete prop;prop = NULL;
308            SetWindowLong (dlg, GWL_USERDATA, 0);
309          break;          break;
310                
311      case WM_PAINT:      case WM_PAINT:
312          if (k->has_photo)          /* Display the photo in the frame of the dialog @dlg.
313              keyprops_show_photo (dlg);             The coordinates are fixed to (0,0). */
314            if (prop->has_photo) {
315                POINT p;
316                p.x = p.y = 0;
317                /* In case of errors we disable the flag to
318                   avoid an infinite loop. */
319                if (jpg_show (GetDlgItem (dlg, IDC_KEYPROPS_IMG),
320                              &p, prop->photo_file))
321                    prop->has_photo = false;
322            }
323          break;          break;
324    
     case WM_SYSCOMMAND:  
         if (LOWORD (wparam) == SC_CLOSE)  
             EndDialog (dlg, FALSE);  
         return FALSE;  
           
325      case WM_COMMAND:      case WM_COMMAND:
326          switch (LOWORD (wparam)) {          switch (LOWORD (wparam)) {
327          case IDOK:          case IDOK:
# Line 333  keyprops_dlg_proc (HWND dlg, UINT msg, W Line 333  keyprops_dlg_proc (HWND dlg, UINT msg, W
333              return TRUE;              return TRUE;
334                            
335          case IDC_KEYPROPS_OT_CHANGE:          case IDC_KEYPROPS_OT_CHANGE:
336              if (do_check_key (k->ctx)) {              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_is_valid (k->ctx)) {  
337                  rc = msg_box (dlg, _("This is a non-valid key.\n"                  rc = msg_box (dlg, _("This is a non-valid key.\n"
338                                       "Modifying the ownertrust has no effect on such keys.\n\n"                                       "Modifying the ownertrust has no effect on such keys.\n\n"
339                                       "Do you really want to continue?"),                                       "Do you really want to continue?"),
# Line 350  keyprops_dlg_proc (HWND dlg, UINT msg, W Line 344  keyprops_dlg_proc (HWND dlg, UINT msg, W
344              rc = dialog_box_param (glob_hinst,              rc = dialog_box_param (glob_hinst,
345                                     (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST,                                     (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST,
346                                      dlg, keyedit_ownertrust_dlg_proc,                                      dlg, keyedit_ownertrust_dlg_proc,
347                                     (LPARAM)k, _("Change Ownertrust"),                                     (LPARAM)prop->key, _("Change Ownertrust"),
348                                      IDS_WINPT_KEYEDIT_OWNERTRUST);                                      IDS_WINPT_KEYEDIT_OWNERTRUST);
349              if (rc == FALSE) /* Cancel */              if (rc == FALSE) /* Cancel */
350                  return TRUE;                  return TRUE;
351    
352              inf = ownertrust_to_string (k->callback.new_val, k->key_pair);              inf = ownertrust_to_string (prop->key->callback.new_val,
353                                            prop->key->key_pair);
354              SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);              SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);
355              msg_box (dlg, _("Ownertrust successfully changed."),              msg_box (dlg, _("Ownertrust successfully changed."),
356                       _("GnuPG Status"), MB_OK);                       _("GnuPG Status"), MB_OK);
357              k->update = 1;              prop->key->update = 1;
358              return TRUE;              return TRUE;
359                            
360          case IDC_KEYPROPS_CHANGE_PWD:          case IDC_KEYPROPS_CHANGE_PWD:
361              keyedit_change_passwd (k, dlg);                      keyedit_change_passwd (prop->key, dlg);        
362              return TRUE;              return TRUE;
363    
364          case IDC_KEYPROPS_REVOKERS:          case IDC_KEYPROPS_REVOKERS:
365              k->update = dialog_box_param (              prop->key->update = dialog_box_param (glob_hinst,
366                  glob_hinst, (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg,                                                    (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg,
367                  key_revokers_dlg_proc, (LPARAM)k,                                                    key_revokers_dlg_proc,
368                  _("Key Revokers"), IDS_WINPT_KEY_REVOKERS);                                                    (LPARAM)prop->key,
369                                                      _("Key Revokers"),
370                                                      IDS_WINPT_KEY_REVOKERS);
371                UpdateWindow (dlg);
372              break;              break;
373          }          }
374      }      }

Legend:
Removed from v.211  
changed lines
  Added in v.392

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26