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

Legend:
Removed from v.24  
changed lines
  Added in v.41

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26