/[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 73 by twoaday, Tue Nov 8 07:15:13 2005 UTC revision 366 by twoaday, Tue Dec 6 12:32:08 2011 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 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 36  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    
57  /* Convert a trust integer into a string representation. */  /* Convert a trust integer into a string representation. */
58  static const char*  static const char*
59  ownertrust_to_string (int val)  ownertrust_to_string (int val, bool is_keypair)
60  {  {
61      const char *inf;      const char *inf;
62      int id = val;      int id = val;
# Line 63  ownertrust_to_string (int val) Line 67  ownertrust_to_string (int val)
67      case 3: inf = _("I trust marginally"); break;      case 3: inf = _("I trust marginally"); break;
68      case 4: inf = _("I trust fully");      break;      case 4: inf = _("I trust fully");      break;
69      case 5:      case 5:
70      case 6: inf = _("I trust ultimately"); break;      case 6:
71            if (is_keypair)
72                inf = _("I trust ultimately (implicit)");
73            else
74                inf = _("I trust ultimately"); break;
75      default:inf = _("Unknown");            break;      default:inf = _("Unknown");            break;
76      }      }
77            
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[64];      const char *fmt = "winpt_photo_%p.tmp";
86        char name[64];
87    
88      _snprintf (buf, sizeof (buf)-1, "winpt_photo_%08lX.tmp", (DWORD)dlg);      if (buflen < (MAX_PATH+strlen(fmt)+8+1))
89      return buf;          return WPTERR_GENERAL;
90        _snprintf (name, DIM (name)-1, fmt, key);
91        get_temp_name (buf, buflen-1, name);
92        return 0;
93  }  }
94    
95    
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                    gpgme_validity_t *r_valid)
103  {  {
104      winpt_key_s k;      const BYTE *img = key->ext->attrib.d;
105      FILE *f;      DWORD imglen = key->ext->attrib.len;
106      const BYTE *img;      if (img && !key->ext->attrib.validity)
107      DWORD imglen = 0;          get_uat_validity (key->ctx->subkeys->keyid,
108      int pos=0;                            &key->ext->attrib.validity);
109        if (r_valid)
110      winpt_get_pubkey (key->subkeys->keyid, &k);          *r_valid = key->ext->attrib.validity;
     img = k.ext->attrib.d;  
     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            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) {  
         for (pos = 0; img[pos] != 0x10; pos++)  
                 ;  
         pos += 16;  
         fwrite (img + pos, 1, imglen - pos, f);  
         fwrite (img, 1, imglen, 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 138  keyprops_show_photo (HWND dlg) Line 128  keyprops_show_photo (HWND dlg)
128  static const char*  static const char*
129  get_validity (gpgme_key_t key)  get_validity (gpgme_key_t key)
130  {  {
131      int val;      if (key->expired)
     val = key->expired;  
     if (val)  
132          return _("Expired");              return _("Expired");    
133      val = key->revoked;      if (key->revoked)
     if (val)  
134          return _("Revoked");          return _("Revoked");
135      val = key->disabled;      if (key->disabled)
     if (val)  
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->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:  
     case 8:  
     case 9: return "AES";  
     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 192  get_card_type (winpt_key_t k) Line 159  get_card_type (winpt_key_t k)
159  {      {    
160      static char buf[64];      static char buf[64];
161    
162      if (!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  /* Display the key information for key @k.  /* Return 1 if at least one user-ID is valid. */
171     Return value: gpgme key on success. */  static int
172    key_is_valid (gpgme_key_t key)
173    {
174        gpgme_user_id_t u;
175    
176        for (u=key->uids; u; u=u->next) {
177            if (u->validity >= GPGME_VALIDITY_MARGINAL)
178                return 1;
179        }
180        return 0;
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    
197    /* Display the key information for key @k. */
198  static void  static void
199  display_key_info (HWND dlg, winpt_key_t k, gpgme_key_t *r_key)  display_key_info (HWND dlg, winpt_key_t k)
200  {  {
201      struct winpt_key_s k2;      gpgme_key_t key;
202      gpgme_key_t sk, key;      struct winpt_key_s sk;
203      char info[512];      char info[512];
204      const char *inf;      const char *inf;
205      u32 created, expires;          DWORD created, expires;
206    
207      memset (&k2, 0, sizeof (k2));            gpg_keycache_update_attr (k->ext, KC_ATTR_PREFSYM, 0);
208      if (k->key_pair)      memset (&sk, 0, sizeof (sk));
209          winpt_get_seckey (k->keyid, &k2);      if (k->key_pair && !winpt_get_seckey (k->keyid, &sk))
210      else              k->is_protected = sk.is_protected;
211          winpt_get_pubkey (k->keyid, &k2);      key = k->ext->key;
212      sk = k2.ctx;              created = key->subkeys->timestamp;
213      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;      
214      _snprintf (info, DIM (info)-1,      _snprintf (info, DIM (info)-1,
215                 _("Type: %s\r\n"                 _("Type: %s\r\n"
216                 "Key ID: %s\r\n"                 "Key ID: 0x%s\r\n"
217                 "Algorithm: %s\r\n"                 "Algorithm: %s\r\n"
218                 "Size: %s\r\n"                 "Size: %s bits\r\n"
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),
230                 get_pref_cipher (&k2),                 get_card_type (&sk));
                get_card_type (&k2));  
231    
232      SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info);      SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info);
233      SetDlgItemText (dlg, IDC_KEYPROPS_FPR, get_key_fpr (key));        SetDlgItemText (dlg, IDC_KEYPROPS_FPR, get_key_fpr (key));  
234      inf = ownertrust_to_string (key->owner_trust);      inf = ownertrust_to_string (key->owner_trust, k->key_pair);
235      SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);      SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);
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      *r_key = key;      StringBuffer s = _("Key Properties");
247        if (k->ext->key->revoked)
248            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    
# Line 255  display_key_info (HWND dlg, winpt_key_t Line 271  display_key_info (HWND dlg, winpt_key_t
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;
     static gpgme_key_t key;  
     gpgme_validity_t valid;  
     refresh_cache_s rcs = {0};  
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          #ifndef LANG_DE          prop->has_photo = 0;
291          SetWindowText (dlg, _("Key Properties"));          SetWindowLong (dlg, GWL_USERDATA, (LONG)prop);
292          SetDlgItemText (dlg, IDC_KEYPROPS_OT_CHANGE, _("&Change"));          on_init_dialog (dlg, wparam, lparam);
293          SetDlgItemText (dlg, IDC_KEYPROPS_REVOKERS, _("&Revokers"));          if (!key_load_photo (prop->key,
294          SetDlgItemText (dlg, IDC_KEYPROPS_CHANGE_PWD, _("Change &Passwd"));                               prop->photo_file, DIM (prop->photo_file)-1,
295          SetDlgItemText (dlg, IDC_KEYPROPS_OTINF, _("Ownertrust"));                               &valid)) {
296          #endif                prop->key->has_photo = 1;
297                prop->has_photo = true;
         display_key_info (dlg, k, &key);  
         if (!keyprops_load_photo (dlg, key, &valid)) {  
             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 (key))  
             EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_REVOKERS), TRUE);  
         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, TRUE);  
         return FALSE;  
           
325      case WM_COMMAND:      case WM_COMMAND:
326          switch (LOWORD (wparam)) {          switch (LOWORD (wparam)) {
327          case IDOK:          case IDOK:
328              EndDialog (dlg, TRUE);              EndDialog (dlg, TRUE);
329              return TRUE;              return TRUE;
330    
331            case IDCANCEL:
332                EndDialog (dlg, FALSE);
333                return TRUE;
334                            
335          case IDC_KEYPROPS_OT_CHANGE:          case IDC_KEYPROPS_OT_CHANGE:
336              if (do_check_key (key)) {              if (!prop->key->key_pair && !key_is_valid (prop->key->ctx)) {
337                  msg_box (dlg, _("The status of this key is 'revoked' or 'expired'.\n"                  rc = msg_box (dlg, _("This is a non-valid key.\n"
                                 "You cannot change the ownertrust of such keys."),  
                                 _("WinPT Warning"), MB_ERR);  
                 return TRUE;  
             }  
             if( !k->key_pair && key->uids->validity < 3 ) {  
                 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?"),
340                                       _("WinPT Warning"), MB_ICONWARNING|MB_YESNO );                                       _("WinPT Warning"), MB_ICONWARNING|MB_YESNO);
341                  if (rc == IDNO)                  if (rc == IDNO)
342                      return TRUE;                      return TRUE;
343              }              }
344              //GetDlgItemText (dlg, IDC_KEYPROPS_OT, info, sizeof info -1);              rc = dialog_box_param (glob_hinst,
345              dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST,                                     (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST,
346                                dlg, (DLGPROC)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 (k->callback.new_val == -1) { /* Cancel */              if (rc == FALSE) /* Cancel */
350                  EndDialog (dlg, FALSE);                  return TRUE;
                 break;  
             }  
351    
352              inf = ownertrust_to_string (k->callback.new_val);              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                            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);  
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              dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg,              prop->key->update = dialog_box_param (glob_hinst,
366                                key_revokers_dlg_proc, (LPARAM)key,                                                    (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg,
367                                _("Key Revokers"), IDS_WINPT_KEY_REVOKERS);                                                    key_revokers_dlg_proc,
368                                                      (LPARAM)prop->key,
369                                                      _("Key Revokers"),
370                                                      IDS_WINPT_KEY_REVOKERS);
371                UpdateWindow (dlg);
372              break;              break;
373          }          }
374      }      }

Legend:
Removed from v.73  
changed lines
  Added in v.366

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26