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

Legend:
Removed from v.33  
changed lines
  Added in v.77

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26