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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26