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

Legend:
Removed from v.32  
changed lines
  Added in v.48

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26