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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26