/[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 28 by twoaday, Thu Oct 20 12:35:59 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   *   *
6   * WinPT is free software; you can redistribute it and/or modify   * WinPT is free software; you can redistribute it and/or modify
7   * it under the terms of the GNU General Public License as published by   * it under the terms of the GNU General Public License as published by
8   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
9   * (at your option) any later version.   * (at your option) any later version.
10   *   *
11   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
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.
15   *   */
16   * You should have received a copy of the GNU General Public License  #ifdef HAVE_CONFIG_H
17   * along with WinPT; if not, write to the Free Software Foundation,  #include <config.h>
18   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA  #endif
19   */  
20    #include <windows.h>
21  #include <windows.h>  #include <assert.h>
22    
23  #include "../resource.h"  #include "resource.h"
24  #include "wptErrors.h"  #include "wptErrors.h"
25  #include "wptGPG.h"  #include "wptGPG.h"
26  #include "wptCommonCtl.h"  #include "wptCommonCtl.h"
27  #include "wptContext.h" /* for passphrase_s */  #include "wptContext.h" /* for passphrase_s */
28  #include "wptNLS.h"  #include "wptNLS.h"
29  #include "wptDlgs.h"  #include "wptDlgs.h"
30  #include "wptTypes.h"  #include "wptTypes.h"
31  #include "wptKeylist.h"  #include "wptKeylist.h"
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  static void  
38  do_change_ownertrust (winpt_key_t k, const char *s)  /* Context to store associated data of the dialog. */
39  {  struct prop_info_s {
40      char ot[64];      winpt_key_t key;
41            char photo_file[MAX_PATH+128+1];
42      if( strstr( s, "ultimate" ) ) {      bool has_photo;
43          listview_get_item_text (k->callback.ctl, k->callback.idx, 5, ot, DIM (ot)-1);  };
44          strcpy (ot, "Ultimate");  
45          listview_add_sub_item (k->callback.ctl, k->callback.idx, 5, ot);  
46          /* fixme: If we switch back from Ultimate to a lower level */  /* Check that the key is not expired or revoked. */
47      }  static int
48  } /* do_change_ownertrust */  do_check_key (gpgme_key_t key)
49    {
50        int invalid = key->expired;
51  static int      if (!invalid)
52  do_check_key (gpgme_key_t key)          invalid = key->revoked;
53  {      return invalid;
54      int okay = 0;  }
55      okay = key->expired;  
56      if (!okay)  
57          okay = key->revoked;  /* Convert a trust integer into a string representation. */
58      return okay;  static const char*
59  } /* do_check_key */  ownertrust_to_string (int val, bool is_keypair)
60    {
61        const char *inf;
62  static const char*      int id = val;
63  ownertrust_to_string (int val)  
64  {      switch (id) {
65      const char * inf;      case 1: inf = _("Don't know");         break;
66      int id = val;      case 2: inf = _("I do NOT trust");     break;
67      switch (id) {      case 3: inf = _("I trust marginally"); break;
68      case 1: inf = _("Don't know");         break;      case 4: inf = _("I trust fully");      break;
69      case 2: inf = _("I do NOT trust");     break;      case 5:
70      case 3: inf = _("I trust marginally"); break;      case 6:
71      case 4: inf = _("I trust fully");      break;          if (is_keypair)
72      case 5:              inf = _("I trust ultimately (implicit)");
73      case 6: inf = _("I trust ultimately"); break;          else
74      default:inf = _("Unknown");            break;              inf = _("I trust ultimately"); break;
75      }      default:inf = _("Unknown");            break;
76            }
77      return inf;      
78  } /* ownertrust_to_string */      return inf;
79    }
80    
81  #define PHOTO_TMPNAME "winpt_temp_photo.jpg"  /* Generate a temporary name for the photo ID */
82    int
83  static int  get_photo_tmpname (gpgme_key_t key, char *buf, size_t buflen)
84  keyprops_show_photo (HWND dlg, gpgme_key_t key)  {
85  {      const char *fmt = "winpt_photo_%p.tmp";
86      RECT r;          char name[64];
87      POINT p;  
88      HWND h;      if (buflen < (MAX_PATH+strlen(fmt)+8+1))
89      const BYTE *img;          return WPTERR_GENERAL;
90      DWORD imglen = 0;      _snprintf (name, DIM (name)-1, fmt, key);
91      int pos=0;      get_temp_name (buf, buflen-1, name);
92      winpt_key_s k;      return 0;
93    }
94      winpt_get_pubkey (key->subkeys->keyid, &k);  
95      img = k.ext->attrib.d;  
96      imglen = k.ext->attrib.len;  
97    
98      if (!img || !imglen)  /* Load the photo from the key @key */
99          return -1;  int
100      FILE *f = fopen (PHOTO_TMPNAME, "wb");  key_load_photo (winpt_key_t key,
101      if (f) {                  char *photo_file, size_t photo_file_size,
102          for (pos = 0; img[pos] != 0x10; pos++)                  gpgme_validity_t *r_valid)
103                  ;  {
104          pos += 16;      const BYTE *img = key->ext->attrib.d;
105          fwrite (img + pos, 1, imglen - pos, f);      DWORD imglen = key->ext->attrib.len;
106          fwrite (img, 1, imglen, f);      if (img && !key->ext->attrib.validity)
107          fclose (f);          get_uat_validity (key->ctx->subkeys->keyid,
108      }                            &key->ext->attrib.validity);
109        if (r_valid)
110      h = GetDlgItem (dlg, IDC_KEYPROPS_IMG);          *r_valid = key->ext->attrib.validity;
111      GetWindowRect (h, &r);  
112      p.x = r.left + 5;      if (!img || imglen < 1)
113      p.y = r.top - 2;          return -1;
114      memset (&p, 0, sizeof (p));              
115      PTD_jpg_show (h, &p, PHOTO_TMPNAME);      get_photo_tmpname (key->ctx, photo_file, photo_file_size);
116      unlink (PHOTO_TMPNAME);      FILE *fp = fopen (photo_file, "wb");
117        if (fp == NULL)
118      return 0;          return -1;
119  }      
120        const int HEADER_OFF = 16;
121        fwrite (img + HEADER_OFF, 1, imglen - HEADER_OFF, fp);
122  /* Return string representation of the key validity. @key. */      fclose (fp);
123  static const char*      return 0;
124  get_validity (gpgme_key_t key)  }
125  {  
126      int val;  
127      val = key->expired;  /* Return string representation of the key validity. @key. */
128      if (val)  static const char*
129          return "Expired";        get_validity (gpgme_key_t key)
130      val = key->revoked;  {
131      if (val)      if (key->expired)
132          return "Revoked";          return _("Expired");    
133      return get_key_trust2 (NULL, key->uids->validity, 0, 0);      if (key->revoked)
134  }          return _("Revoked");
135        if (key->disabled)
136            return _("Disabled");
137  /* Return the preferred sym. algorithm from @key as a string. */      if (key->invalid)
138  static const char*          return _("Invalid");
139  get_pref_cipher (winpt_key_t k)      return get_key_trust2 (NULL, key->uids->validity, 0, 0);
140  {  }
141      const char *sym_prefs=NULL;      
142    
143      if (k->is_v3)  /* Return true if the key has designated revokers. */
144          return "IDEA";  static bool
145      if (!k->ext->sym_prefs)  check_for_desig_rev (gpgme_key_t key)
146          return "3DES";  {
147      switch (*k->ext->sym_prefs) {      winpt_key_s k;
148      case 1: return "IDEA";  
149      case 2: return "3DES";      memset (&k, 0, sizeof (k));
150      case 3: return "CAST5";      if (!winpt_get_pubkey (key->subkeys->keyid, &k))
151      case 4: return "Blowfish";          return k.ext->gloflags.has_desig_rev? true : false;
152      case 7:      return false;
153      case 8:  }
154      case 9: return "AES";  
155      case 10:return "Twofish";  
156      }  /* Print information (name) of the smart card. */
157      return "Unknown";  static const char*
158  }  get_card_type (winpt_key_t k)
159    {    
160        static char buf[64];
161  /* Return true if the key has designated revokers. */  
162  static bool      if (!k->ext || !k->ext->card_type)
163  check_for_desig_rev (gpgme_key_t key)          return "";
164  {      _snprintf (buf, DIM (buf)-1, _("Card-Type: %s\r\n"),
165      winpt_key_s k;                 k->ext->card_type);
166      memset (&k, 0, sizeof (k));      return buf;
167      if (!winpt_get_pubkey (key->subkeys->keyid, &k))  }
168          return k.ext->gloflags.has_desig_rev? true : false;  
169      return false;  
170  }  /* Return 1 if at least one user-ID is valid. */
171    static int
172    key_is_valid (gpgme_key_t key)
173  /* Print information (name) of the smart card. */  {
174  static const char*      gpgme_user_id_t u;
175  get_card_type (winpt_key_t k)  
176  {      for (u=key->uids; u; u=u->next) {
177      static char buf[64];          if (u->validity >= GPGME_VALIDITY_MARGINAL)
178                return 1;
179      if (!k->ext->card_type)      }
180          return "";      return 0;
181      _snprintf (buf, sizeof (buf)-1, "Card-Type: %s\r\n", k->ext->card_type);  }
182      return buf;  
183  }  
184    /* Return extended algorithm information. */
185    const char*
186  /* Dialog box procedure to show the key properties. */  props_get_key_algo (gpgme_key_t key, int idx)
187  BOOL CALLBACK  {
188  keyprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)      /* 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      static winpt_key_t k;         and v3 RSA keys, we use the same notation. */
191      static gpgme_key_t key, sk;      if (key->subkeys != NULL && strlen (key->subkeys->fpr) == 32)
192      static int photo_done = 0;          return "RSA Legacy";
193      struct winpt_key_s k2;      return get_key_algo (key, idx);
194      refresh_cache_s rcs = {0};  }
195      const char *inf;  
196      char info[512];  
197      u32 created, expires;  /* Display the key information for key @k. */
198      int ot, cancel = 0;  static void
199      int rc;  display_key_info (HWND dlg, winpt_key_t k)
200        {
201      switch (msg) {      gpgme_key_t key;
202      case WM_INITDIALOG:      struct winpt_key_s sk;
203          if (!lparam)      char info[512];
204              dlg_fatal_error (dlg, "Could not get dialog param!");      const char *inf;
205          k = (winpt_key_t)lparam;      DWORD created, expires;
206          #ifndef LANG_DE  
207          SetWindowText( dlg, _("Key Properties") );      gpg_keycache_update_attr (k->ext, KC_ATTR_PREFSYM, 0);
208          SetDlgItemText (dlg, IDC_KEYPROPS_OT_CHANGE, _("&Change"));      memset (&sk, 0, sizeof (sk));
209          SetDlgItemText (dlg, IDC_KEYPROPS_REVOKERS, _("&Revokers"));      if (k->key_pair && !winpt_get_seckey (k->keyid, &sk))
210          SetDlgItemText (dlg, IDC_KEYPROPS_CHANGE_PWD, _("Change &Passwd"));          k->is_protected = sk.is_protected;
211          #endif        key = k->ext->key;
212        created = key->subkeys->timestamp;
213          photo_done = 0;      expires = key->subkeys->expires;
214          memset (&k2, 0, sizeof (k2));      _snprintf (info, DIM (info)-1,
215          if (k->key_pair)                 _("Type: %s\r\n"
216              winpt_get_seckey (k->keyid, &k2);                 "Key ID: 0x%s\r\n"
217          else                 "Algorithm: %s\r\n"
218              winpt_get_pubkey (k->keyid, &k2);                 "Size: %s bits\r\n"
219          sk = k2.ctx;                 "Created: %s\r\n"
220          if (sk)                 "Expires: %s\r\n"
221              k->is_protected = k2.is_protected;                 "Validity: %s\r\n"
222          if (get_pubkey (k->keyid, &key))                 "%s\r\n"),
223              BUG (0);                 get_key_type (key),
224          created = key->subkeys->timestamp;                 k->keyid,
225          expires = key->subkeys->expires;                 props_get_key_algo (key, 0),
226          _snprintf (info, DIM (info)-1,                 get_key_size (key, 0),
227                    "Type: %s\r\n"                 get_key_created (created),
228                    "Key ID: %s\r\n"                 get_key_expire_date (expires),
229                    "Algorithm: %s\r\n"                 get_validity (key),
230                    "Size: %s\r\n"                 get_card_type (&sk));
231                    "Created: %s\r\n"  
232                    "Expires: %s\r\n"      SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info);
233                    "Validity: %s\r\n"      SetDlgItemText (dlg, IDC_KEYPROPS_FPR, get_key_fpr (key));  
234                    "Cipher: %s\r\n"      inf = ownertrust_to_string (key->owner_trust, k->key_pair);
235                    "%s\r\n",      SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);
236                    get_key_type (key),  }
237                    k->keyid,  
238                    get_key_algo (key, 0),  
239                    get_key_size (key, 0),  
240                    get_key_created (created),  static void
241                    get_key_expire_date (expires),  on_init_dialog (HWND dlg, WPARAM wparam, LPARAM lparam)
242                    get_validity (key),  {
243                    get_pref_cipher (&k2),      winpt_key_t k = (winpt_key_t)lparam;
244                    get_card_type (&k2));      assert (k != NULL);
245          SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info);  
246          SetDlgItemText (dlg, IDC_KEYPROPS_FPR, get_key_fpr (key));      StringBuffer s = _("Key Properties");
247          ot = gpgme_key_get_ulong_attr( key, GPGME_ATTR_OTRUST, NULL, 0 );                    if (k->ext->key->revoked)
248          inf = ownertrust_to_string (ot);          s += " - REVOKED KEY";
249          SetDlgItemText( dlg, IDC_KEYPROPS_OT, inf );      
250          if (k->key_pair)      SetWindowText (dlg, s.getBuffer());
251              EnableWindow( GetDlgItem( dlg, IDC_KEYPROPS_CHANGE_PWD ), TRUE );      SetDlgItemText (dlg, IDC_KEYPROPS_OT_CHANGE, _("&Change"));
252          if (check_for_desig_rev (key))      SetDlgItemText (dlg, IDC_KEYPROPS_REVOKERS, _("&Revokers"));
253              EnableWindow( GetDlgItem( dlg, IDC_KEYPROPS_REVOKERS ), TRUE );      SetDlgItemText (dlg, IDC_KEYPROPS_CHANGE_PWD, _("Change &Password"));
254          center_window (dlg, NULL);      SetDlgItemText (dlg, IDC_KEYPROPS_OTINF, _("Ownertrust:"));
255          SetForegroundWindow (dlg);            SetDlgItemText (dlg, IDC_KEYPROPS_FPRTXT, _("Fingerprint:"));
256          return TRUE;      
257              display_key_info (dlg, k);
258      case WM_PAINT:            
259          if (photo_done == 0) {      if (k->key_pair)
260              photo_done = 1;          EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_CHANGE_PWD), TRUE);
261              keyprops_show_photo (dlg, key);      if (check_for_desig_rev (k->ctx))
262          }          EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_REVOKERS), TRUE);
263          break;      if (do_check_key (k->ctx))
264            EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_OT_CHANGE), FALSE);
265      case WM_SYSCOMMAND:      center_window (dlg, NULL);  
266          if( LOWORD( wparam ) == SC_CLOSE )      SetForegroundWindow (dlg);
267              EndDialog( dlg, TRUE );  }
268          return FALSE;  
269            
270      case WM_COMMAND:  /* Dialog box procedure to show the key properties. */
271          switch( LOWORD( wparam ) ) {  BOOL CALLBACK
272          case IDOK:  keyprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
273              EndDialog( dlg, TRUE );  {
274              return TRUE;      struct prop_info_s *prop = NULL;
275                    const char *inf;
276          case IDC_KEYPROPS_OT_CHANGE:      gpgme_validity_t valid;
277              if( do_check_key( key ) ) {      int rc;
278                  msg_box( dlg, _("The status of this key is 'revoked' or 'expired'.\n"      
279                                  "You cannot change the ownertrust of such keys."),      if (msg != WM_INITDIALOG) {
280                                  _("WinPT Warning"), MB_ERR );          prop = (struct prop_info_s*)GetWindowLong (dlg, GWL_USERDATA);
281                  return FALSE;          if (prop == NULL)
282              }              return FALSE;
283              if( !k->key_pair && key->uids->validity < 3 ) {      }
284                  rc = msg_box( dlg, _("This is a non-valid key.\n"  
285                                       "Modifying the ownertrust has no effect on such keys.\n\n"      switch (msg) {
286                                       "Do you really want to continue?"),      case WM_INITDIALOG:
287                                       _("WinPT Warning"), MB_ICONWARNING|MB_YESNO );          assert (lparam != 0);
288                  if (rc == IDNO)          prop = new struct prop_info_s;
289                      return FALSE;          prop->key = (winpt_key_t)lparam;
290              }          prop->has_photo = 0;
291              GetDlgItemText( dlg, IDC_KEYPROPS_OT, info, sizeof info -1 );          SetWindowLong (dlg, GWL_USERDATA, (LONG)prop);
292            on_init_dialog (dlg, wparam, lparam);
293                        if (!key_load_photo (prop->key,
294              dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST,                               prop->photo_file, DIM (prop->photo_file)-1,
295                                dlg, (DLGPROC)keyedit_ownertrust_dlg_proc,                               &valid)) {
296                                (LPARAM)k, _("Change Ownertrust"),              prop->key->has_photo = 1;
297                                IDS_WINPT_KEYEDIT_OWNERTRUST);              prop->has_photo = true;
298              if (k->callback.new_val == -1) { /* Cancel */              if (valid < GPGME_VALIDITY_MARGINAL)
299                  EndDialog (dlg, FALSE);                  SetDlgItemText (dlg, IDC_KEYPROPS_IMGINF,
300                  break;                                  _("Photo-ID not validated."));
301              }          }
302            return TRUE;
303              inf = ownertrust_to_string (k->callback.new_val);  
304              do_change_ownertrust (k, inf);      case WM_DESTROY:
305              SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);          if (prop->has_photo)
306              msg_box (dlg, _("Ownertrust successfully changed."), _("GnuPG Status"), MB_OK);              DeleteFile(prop->photo_file);
307                        delete prop;prop = NULL;
308              /* reload only the keylist */          SetWindowLong (dlg, GWL_USERDATA, 0);
309              rcs.kr_reload = 1; rcs.kr_update = 1;          break;
310              rcs.tr_update = 0;        
311              DialogBoxParam( glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, dlg,      case WM_PAINT:
312                              keycache_dlg_proc, (LPARAM)&rcs );          /* Display the photo in the frame of the dialog @dlg.
313              /* XXX: old code!?             The coordinates are fixed to (0,0). */
314              get_seckey (k->keyid, &sk);          if (prop->has_photo) {
315              if (get_pubkey (k->keyid, &key))              POINT p;
316                  BUG (0);              p.x = p.y = 0;
317              */              /* In case of errors we disable the flag to
318              return TRUE;                 avoid an infinite loop. */
319                            if (jpg_show (GetDlgItem (dlg, IDC_KEYPROPS_IMG),
320          case IDC_KEYPROPS_CHANGE_PWD:                            &p, prop->photo_file))
321              keyedit_change_passwd (k, dlg);                          prop->has_photo = false;
322              return TRUE;          }
323            break;
324          case IDC_KEYPROPS_REVOKERS:          
325              dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg,      case WM_COMMAND:
326                                key_revokers_dlg_proc, (LPARAM)key, _("Key Revokers"),          switch (LOWORD (wparam)) {
327                                IDS_WINPT_KEY_REVOKERS);          case IDOK:
328              break;              EndDialog (dlg, TRUE);
329          }              return TRUE;
330      }  
331                case IDCANCEL:
332      return FALSE;              EndDialog (dlg, FALSE);
333  }              return TRUE;
334                
335            case IDC_KEYPROPS_OT_CHANGE:
336                if (!prop->key->key_pair && !key_is_valid (prop->key->ctx)) {
337                    rc = msg_box (dlg, _("This is a non-valid key.\n"
338                                         "Modifying the ownertrust has no effect on such keys.\n\n"
339                                         "Do you really want to continue?"),
340                                         _("WinPT Warning"), MB_ICONWARNING|MB_YESNO);
341                    if (rc == IDNO)
342                        return TRUE;
343                }
344                rc = dialog_box_param (glob_hinst,
345                                       (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST,
346                                        dlg, keyedit_ownertrust_dlg_proc,
347                                       (LPARAM)prop->key, _("Change Ownertrust"),
348                                        IDS_WINPT_KEYEDIT_OWNERTRUST);
349                if (rc == FALSE) /* Cancel */
350                    return TRUE;
351    
352                inf = ownertrust_to_string (prop->key->callback.new_val,
353                                            prop->key->key_pair);
354                SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);
355                msg_box (dlg, _("Ownertrust successfully changed."),
356                         _("GnuPG Status"), MB_OK);
357                prop->key->update = 1;
358                return TRUE;
359                
360            case IDC_KEYPROPS_CHANGE_PWD:
361                keyedit_change_passwd (prop->key, dlg);        
362                return TRUE;
363    
364            case IDC_KEYPROPS_REVOKERS:
365                prop->key->update = dialog_box_param (glob_hinst,
366                                                      (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg,
367                                                      key_revokers_dlg_proc,
368                                                      (LPARAM)prop->key,
369                                                      _("Key Revokers"),
370                                                      IDS_WINPT_KEY_REVOKERS);
371                UpdateWindow (dlg);
372                break;
373            }
374        }
375        
376        return FALSE;
377    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26