/[winpt]/trunk/Src/wptKeyPropsDlg.cpp
ViewVC logotype

Diff of /trunk/Src/wptKeyPropsDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26