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

Annotation of /trunk/Src/wptKeyPropsDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (hide annotations)
Tue Oct 25 07:46:20 2005 UTC (19 years, 4 months ago) by twoaday
File size: 10221 byte(s)
More bug fixes and cleanups.
See ChangeLog for details.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26