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

Legend:
Removed from v.22  
changed lines
  Added in v.73

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26