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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26