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

Legend:
Removed from v.23  
changed lines
  Added in v.47

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26