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

Legend:
Removed from v.2  
changed lines
  Added in v.36

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26