/[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 181 by twoaday, Tue Mar 14 11:01:22 2006 UTC revision 328 by twoaday, Fri Sep 25 16:07:38 2009 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 37  Line 34 
34  #include "wptKeyEdit.h"  #include "wptKeyEdit.h"
35    
36    
37    /* Context to store associated data of the dialog. */
38    struct prop_info_s {
39        winpt_key_t key;
40        char photo_file[MAX_PATH+128+1];
41        bool has_photo;
42    };
43    
44    
45  /* Check that the key is not expired or revoked. */  /* Check that the key is not expired or revoked. */
46  static int  static int
47  do_check_key (gpgme_key_t key)  do_check_key (gpgme_key_t key)
48  {  {
49      int okay = 0;      int invalid;
50      okay = key->expired;  
51      if (!okay)      invalid = key->expired;
52          okay = key->revoked;      if (!invalid)
53      return okay;          invalid = key->revoked;
54        return invalid;
55  }  }
56    
57    
# Line 74  ownertrust_to_string (int val, bool is_k Line 80  ownertrust_to_string (int val, bool is_k
80  }  }
81    
82    
83  /* Generate a unique temp name for the photo which  int
84     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)  
85  {  {
86      static char buf[MAX_PATH+128+1];      const char *fmt = "winpt_photo_%p.tmp";
87      char name[64];      char name[64];
88    
89      _snprintf (name, sizeof (name)-1, "winpt_photo_%08lX.tmp", (DWORD)dlg);      if (buflen < (MAX_PATH+strlen(fmt)+8+1))
90      get_temp_name (buf, DIM (buf), name);          return WPTERR_GENERAL;
91      return buf;      _snprintf (name, DIM (name)-1, fmt, key);
92        get_temp_name (buf, buflen-1, name);
93        return 0;
94  }  }
95    
96    
 static void  
 draw_nophoto_img (HWND dlg)  
 {  
     /*..  
     n = DrawText (hdc, "No Photo-ID", -1, &r, DT_LEFT);  
     ..*/  
 }  
97    
98    
99  /* Load the photo from the key @key */  /* Load the photo from the key @key */
100  static int  int
101  keyprops_load_photo (HWND dlg, gpgme_key_t key, gpgme_validity_t *r_valid)  key_load_photo (winpt_key_t key,
102                    char *photo_file, size_t photo_file_size,
103                    gpgme_validity_t *r_valid)
104  {  {
105      winpt_key_s k;      FILE *fp;
     FILE *f;  
106      const BYTE *img;      const BYTE *img;
107      DWORD imglen = 0;      DWORD imglen;
108      int pos=0;      
109        img = key->ext->attrib.d;
110      winpt_get_pubkey (key->subkeys->keyid, &k);      imglen = key->ext->attrib.len;
111      img = k.ext->attrib.d;      if (img && !key->ext->attrib.validity)
112      imglen = k.ext->attrib.len;          get_uat_validity (key->ctx->subkeys->keyid,
113      if (!k.ext->attrib.validity)                            &key->ext->attrib.validity);
114          get_uat_validity (key->subkeys->keyid, &k.ext->attrib.validity);      if (r_valid)
115      *r_valid = k.ext->attrib.validity;          *r_valid = key->ext->attrib.validity;
116    
117      if (!img || !imglen) {      if (!img || imglen < 1)
         draw_nophoto_img (dlg);  
118          return -1;          return -1;
119                
120        get_photo_tmpname (key->ctx, photo_file, photo_file_size);
121        fp = fopen (photo_file, "wb");
122        if (fp != NULL) {
123            const int pos = 16;
124            fwrite (img + pos, 1, imglen - pos, fp);
125            fclose (fp);
126            return 0;
127      }      }
   
     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);  
         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));  
128            
129      return 0;      return -1;    
130  }  }
131    
132    
# Line 154  keyprops_show_photo (HWND dlg) Line 134  keyprops_show_photo (HWND dlg)
134  static const char*  static const char*
135  get_validity (gpgme_key_t key)  get_validity (gpgme_key_t key)
136  {  {
137      int val;      if (key->expired)
     val = key->expired;  
     if (val)  
138          return _("Expired");              return _("Expired");    
139      val = key->revoked;      if (key->revoked)
     if (val)  
140          return _("Revoked");          return _("Revoked");
141      val = key->disabled;      if (key->disabled)
     if (val)  
142          return _("Disabled");          return _("Disabled");
143        if (key->invalid)
144            return _("Invalid");
145      return get_key_trust2 (NULL, key->uids->validity, 0, 0);      return get_key_trust2 (NULL, key->uids->validity, 0, 0);
146  }  }
147    
148    
 /* 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";  
 }  
   
   
149  /* Return true if the key has designated revokers. */  /* Return true if the key has designated revokers. */
150  static bool  static bool
151  check_for_desig_rev (gpgme_key_t key)  check_for_desig_rev (gpgme_key_t key)
152  {  {
153      winpt_key_s k;      winpt_key_s k;
154    
155      memset (&k, 0, sizeof (k));      memset (&k, 0, sizeof (k));
156      if (!winpt_get_pubkey (key->subkeys->keyid, &k))      if (!winpt_get_pubkey (key->subkeys->keyid, &k))
157          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 167  get_card_type (winpt_key_t k)
167    
168      if (!k->ext || !k->ext->card_type)      if (!k->ext || !k->ext->card_type)
169          return "";          return "";
170      _snprintf (buf, sizeof (buf)-1, _("Card-Type: %s\r\n"), k->ext->card_type);      _snprintf (buf, DIM (buf)-1, _("Card-Type: %s\r\n"),
171                   k->ext->card_type);
172      return buf;      return buf;
173  }  }
174    
175    
176  /* Display the key information for key @k.  /* Return 1 if at least one user-ID is valid. */
177     Return value: gpgme key on success. */  static int
178    key_is_valid (gpgme_key_t key)
179    {
180        gpgme_user_id_t u;
181    
182        for (u=key->uids; u; u=u->next) {
183            if (u->validity >= GPGME_VALIDITY_MARGINAL)
184                return 1;
185        }
186        return 0;
187    }
188    
189    
190    /* Return extended algorithm information. */
191    const char*
192    props_get_key_algo (gpgme_key_t key, int idx)
193    {
194        /* PGP calls the old RSAv3 keys 'RSA Legacy' and because this
195           is a good method to differ between OpenPGP v4 cert-only keys
196           and v3 RSA keys, we use the same notation. */
197        if (key->subkeys != NULL && strlen (key->subkeys->fpr) == 32)
198            return "RSA Legacy";
199        return get_key_algo (key, idx);
200    }
201    
202    
203    /* Display the key information for key @k. */
204  static void  static void
205  display_key_info (HWND dlg, winpt_key_t k, gpgme_key_t *r_key)  display_key_info (HWND dlg, winpt_key_t k)
206  {  {
207      gpgme_key_t key;      gpgme_key_t key;
208      struct winpt_key_s pk, sk;      struct winpt_key_s sk;
209      char info[512];      char info[512];
210      const char *inf;      const char *inf;
211      DWORD created, expires;      DWORD created, expires;
212    
213      memset (&pk, 0, sizeof (pk));      gpg_keycache_update_attr (k->ext, KC_ATTR_PREFSYM, 0);
214      if (winpt_get_pubkey (k->keyid, &pk))      memset (&sk, 0, sizeof (sk));
         BUG (0);  
     gpg_keycache_update_attr (pk.ext, KC_ATTR_PREFSYM, 0);  
     memset (&sk, 0, sizeof (sk));        
215      if (k->key_pair && !winpt_get_seckey (k->keyid, &sk))      if (k->key_pair && !winpt_get_seckey (k->keyid, &sk))
216          k->is_protected = sk.is_protected;          k->is_protected = sk.is_protected;
217      key = pk.ext->key;      key = k->ext->key;
218      created = key->subkeys->timestamp;      created = key->subkeys->timestamp;
219      expires = key->subkeys->expires;      expires = key->subkeys->expires;
220      _snprintf (info, DIM (info)-1,      _snprintf (info, DIM (info)-1,
221                 _("Type: %s\r\n"                 _("Type: %s\r\n"
222                 "Key ID: %s\r\n"                 "Key ID: 0x%s\r\n"
223                 "Algorithm: %s\r\n"                 "Algorithm: %s\r\n"
224                 "Size: %s bits\r\n"                 "Size: %s bits\r\n"
225                 "Created: %s\r\n"                 "Created: %s\r\n"
226                 "Expires: %s\r\n"                 "Expires: %s\r\n"
227                 "Validity: %s\r\n"                 "Validity: %s\r\n"
                "Cipher: %s\r\n"  
228                 "%s\r\n"),                 "%s\r\n"),
229                 get_key_type (key),                 get_key_type (key),
230                 k->keyid,                 k->keyid,
231                 get_key_algo (key, 0),                 props_get_key_algo (key, 0),
232                 get_key_size (key, 0),                 get_key_size (key, 0),
233                 get_key_created (created),                 get_key_created (created),
234                 get_key_expire_date (expires),                 get_key_expire_date (expires),
235                 get_validity (key),                 get_validity (key),
                get_pref_cipher (&pk),  
236                 get_card_type (&sk));                 get_card_type (&sk));
237    
238      SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info);      SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info);
239      SetDlgItemText (dlg, IDC_KEYPROPS_FPR, get_key_fpr (key));        SetDlgItemText (dlg, IDC_KEYPROPS_FPR, get_key_fpr (key));  
240      inf = ownertrust_to_string (key->owner_trust, k->key_pair);      inf = ownertrust_to_string (key->owner_trust, k->key_pair);
241      SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);      SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);
242    }
243    
244    
245      *r_key = key;  
246    static void
247    on_init_dialog (HWND dlg, WPARAM wparam, LPARAM lparam)
248    {
249        winpt_key_t k = (winpt_key_t)lparam;
250        assert (k != NULL);
251        SetWindowText (dlg, _("Key Properties"));
252        SetDlgItemText (dlg, IDC_KEYPROPS_OT_CHANGE, _("&Change"));
253        SetDlgItemText (dlg, IDC_KEYPROPS_REVOKERS, _("&Revokers"));
254        SetDlgItemText (dlg, IDC_KEYPROPS_CHANGE_PWD, _("Change &Password"));
255        SetDlgItemText (dlg, IDC_KEYPROPS_OTINF, _("Ownertrust:"));
256        SetDlgItemText (dlg, IDC_KEYPROPS_FPRTXT, _("Fingerprint:"));
257        
258        display_key_info (dlg, k);
259        
260        if (k->key_pair)
261            EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_CHANGE_PWD), TRUE);
262        if (check_for_desig_rev (k->ctx))
263            EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_REVOKERS), TRUE);
264        if (do_check_key (k->ctx))
265            EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_OT_CHANGE), FALSE);
266        center_window (dlg, NULL);  
267        SetForegroundWindow (dlg);
268  }  }
269    
270    
# Line 269  display_key_info (HWND dlg, winpt_key_t Line 272  display_key_info (HWND dlg, winpt_key_t
272  BOOL CALLBACK  BOOL CALLBACK
273  keyprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  keyprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
274  {  {
275      static winpt_key_t k;      struct prop_info_s *prop = NULL;
     static gpgme_key_t key;  
     gpgme_validity_t valid;  
276      const char *inf;      const char *inf;
277        gpgme_validity_t valid;
278      int rc;      int rc;
279            
280      /* XXX: static variable (k) prevent that the dialog can      if (msg != WM_INITDIALOG) {
281              be opened twice. */          prop = (struct prop_info_s*)GetWindowLong (dlg, GWL_USERDATA);
282            if (prop == NULL)
283                return FALSE;
284        }
285    
286      switch (msg) {      switch (msg) {
287      case WM_INITDIALOG:      case WM_INITDIALOG:
288          if (!lparam)          assert (lparam != 0);
289              dlg_fatal_error (dlg, "Could not get dialog param!");          prop = new struct prop_info_s;
290          k = (winpt_key_t)lparam;          prop->key = (winpt_key_t)lparam;
291          SetWindowText (dlg, _("Key Properties"));          prop->has_photo = 0;
292          SetDlgItemText (dlg, IDC_KEYPROPS_OT_CHANGE, _("&Change"));          SetWindowLong (dlg, GWL_USERDATA, (LONG)prop);
293          SetDlgItemText (dlg, IDC_KEYPROPS_REVOKERS, _("&Revokers"));          on_init_dialog (dlg, wparam, lparam);
294          SetDlgItemText (dlg, IDC_KEYPROPS_CHANGE_PWD, _("Change &Password"));          if (!key_load_photo (prop->key,
295          SetDlgItemText (dlg, IDC_KEYPROPS_OTINF, _("Ownertrust"));                               prop->photo_file, DIM (prop->photo_file)-1,
296                                         &valid)) {
297          display_key_info (dlg, k, &key);              prop->key->has_photo = 1;
298          if (!keyprops_load_photo (dlg, key, &valid)) {              prop->has_photo = true;
             k->has_photo = 1;    
299              if (valid < GPGME_VALIDITY_MARGINAL)              if (valid < GPGME_VALIDITY_MARGINAL)
300                  SetDlgItemText (dlg, IDC_KEYPROPS_IMGINF, _("Photo-ID not validated."));                  SetDlgItemText (dlg, IDC_KEYPROPS_IMGINF,
301                                    _("Photo-ID not validated."));
302          }          }
         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);        
303          return TRUE;          return TRUE;
304    
305      case WM_DESTROY:      case WM_DESTROY:
306          remove (get_photo_tmpname (dlg));          if (prop->has_photo)
307                DeleteFile(prop->photo_file);
308            delete prop;prop = NULL;
309            SetWindowLong (dlg, GWL_USERDATA, 0);
310          break;          break;
311                
312      case WM_PAINT:      case WM_PAINT:
313          if (k->has_photo)          /* Display the photo in the frame of the dialog @dlg.
314              keyprops_show_photo (dlg);             The coordinates are fixed to (0,0). */
315            if (prop->has_photo) {
316                POINT p;
317                p.x = p.y = 0;
318                /* In case of errors we disable the flag to
319                   avoid an infinite loop. */
320                if (jpg_show (GetDlgItem (dlg, IDC_KEYPROPS_IMG),
321                              &p, prop->photo_file))
322                    prop->has_photo = false;
323            }
324          break;          break;
325    
     case WM_SYSCOMMAND:  
         if (LOWORD (wparam) == SC_CLOSE)  
             EndDialog (dlg, FALSE);  
         return FALSE;  
           
326      case WM_COMMAND:      case WM_COMMAND:
327          switch (LOWORD (wparam)) {          switch (LOWORD (wparam)) {
328          case IDOK:          case IDOK:
# Line 329  keyprops_dlg_proc (HWND dlg, UINT msg, W Line 334  keyprops_dlg_proc (HWND dlg, UINT msg, W
334              return TRUE;              return TRUE;
335                            
336          case IDC_KEYPROPS_OT_CHANGE:          case IDC_KEYPROPS_OT_CHANGE:
337              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) {  
338                  rc = msg_box (dlg, _("This is a non-valid key.\n"                  rc = msg_box (dlg, _("This is a non-valid key.\n"
339                                       "Modifying the ownertrust has no effect on such keys.\n\n"                                       "Modifying the ownertrust has no effect on such keys.\n\n"
340                                       "Do you really want to continue?"),                                       "Do you really want to continue?"),
# Line 346  keyprops_dlg_proc (HWND dlg, UINT msg, W Line 345  keyprops_dlg_proc (HWND dlg, UINT msg, W
345              rc = dialog_box_param (glob_hinst,              rc = dialog_box_param (glob_hinst,
346                                     (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST,                                     (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST,
347                                      dlg, keyedit_ownertrust_dlg_proc,                                      dlg, keyedit_ownertrust_dlg_proc,
348                                     (LPARAM)k, _("Change Ownertrust"),                                     (LPARAM)prop->key, _("Change Ownertrust"),
349                                      IDS_WINPT_KEYEDIT_OWNERTRUST);                                      IDS_WINPT_KEYEDIT_OWNERTRUST);
350              if (rc == FALSE) /* Cancel */              if (rc == FALSE) /* Cancel */
351                  return TRUE;                  return TRUE;
352    
353              inf = ownertrust_to_string (k->callback.new_val, k->key_pair);              inf = ownertrust_to_string (prop->key->callback.new_val,
354                                            prop->key->key_pair);
355              SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);              SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);
356              msg_box (dlg, _("Ownertrust successfully changed."),              msg_box (dlg, _("Ownertrust successfully changed."),
357                       _("GnuPG Status"), MB_OK);                       _("GnuPG Status"), MB_OK);
358              k->update = 1;              prop->key->update = 1;
359              return TRUE;              return TRUE;
360                            
361          case IDC_KEYPROPS_CHANGE_PWD:          case IDC_KEYPROPS_CHANGE_PWD:
362              keyedit_change_passwd (k, dlg);                      keyedit_change_passwd (prop->key, dlg);        
363              return TRUE;              return TRUE;
364    
365          case IDC_KEYPROPS_REVOKERS:          case IDC_KEYPROPS_REVOKERS:
366              k->update = dialog_box_param (              prop->key->update = dialog_box_param (glob_hinst,
367                  glob_hinst, (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg,                                                    (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg,
368                  key_revokers_dlg_proc, (LPARAM)key,                                                    key_revokers_dlg_proc,
369                  _("Key Revokers"), IDS_WINPT_KEY_REVOKERS);                                                    (LPARAM)prop->key,
370                                                      _("Key Revokers"),
371                                                      IDS_WINPT_KEY_REVOKERS);
372                UpdateWindow (dlg);
373              break;              break;
374          }          }
375      }      }

Legend:
Removed from v.181  
changed lines
  Added in v.328

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26