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

Annotation of /trunk/Src/wptKeyPropsDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 273 - (hide annotations)
Fri Dec 8 10:22:17 2006 UTC (18 years, 2 months ago) by twoaday
File size: 10593 byte(s)


1 twoaday 256 /* wptKeyPropsDlg.cpp - WinPT key property dialog
2 twoaday 150 * Copyright (C) 2000, 2001, 2002, 2003, 2005, 2006 Timo Schulz
3 werner 36 *
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 werner 42 #ifdef HAVE_CONFIG_H
21     #include <config.h>
22     #endif
23    
24 werner 36 #include <windows.h>
25 twoaday 217 #include <assert.h>
26 werner 36
27 werner 47 #include "resource.h"
28 werner 36 #include "wptErrors.h"
29     #include "wptGPG.h"
30     #include "wptCommonCtl.h"
31     #include "wptContext.h" /* for passphrase_s */
32     #include "wptNLS.h"
33     #include "wptDlgs.h"
34     #include "wptTypes.h"
35     #include "wptKeylist.h"
36     #include "wptW32API.h"
37     #include "wptVersion.h"
38     #include "wptKeyEdit.h"
39    
40    
41     /* Check that the key is not expired or revoked. */
42     static int
43     do_check_key (gpgme_key_t key)
44     {
45 twoaday 273 int invalid;
46 twoaday 217
47     invalid = key->expired;
48     if (!invalid)
49     invalid = key->revoked;
50     return invalid;
51 werner 36 }
52    
53    
54     /* Convert a trust integer into a string representation. */
55     static const char*
56 twoaday 150 ownertrust_to_string (int val, bool is_keypair)
57 werner 36 {
58 twoaday 41 const char *inf;
59 werner 36 int id = val;
60 twoaday 41
61 werner 36 switch (id) {
62     case 1: inf = _("Don't know"); break;
63     case 2: inf = _("I do NOT trust"); break;
64     case 3: inf = _("I trust marginally"); break;
65     case 4: inf = _("I trust fully"); break;
66     case 5:
67 twoaday 150 case 6:
68     if (is_keypair)
69     inf = _("I trust ultimately (implicit)");
70     else
71     inf = _("I trust ultimately"); break;
72 werner 36 default:inf = _("Unknown"); break;
73     }
74    
75     return inf;
76     }
77    
78    
79     /* Generate a unique temp name for the photo which
80     depends on the dialog handle and return it. */
81 twoaday 226 const char*
82 werner 36 get_photo_tmpname (HWND dlg)
83     {
84 twoaday 174 static char buf[MAX_PATH+128+1];
85     char name[64];
86 werner 36
87 twoaday 226 _snprintf (name, DIM (name)-1, "winpt_photo_%08lX.tmp", (DWORD)dlg);
88 twoaday 211 get_temp_name (buf, DIM (buf)-1, name);
89 werner 36 return buf;
90     }
91    
92    
93 twoaday 129 static void
94     draw_nophoto_img (HWND dlg)
95     {
96     /*..
97     n = DrawText (hdc, "No Photo-ID", -1, &r, DT_LEFT);
98     ..*/
99     }
100    
101    
102 twoaday 226
103     /* Delete temporary photo file. */
104     void
105     key_unload_photo (HWND dlg)
106     {
107     DeleteFile (get_photo_tmpname (dlg));
108     }
109    
110    
111 werner 36 /* Load the photo from the key @key */
112 twoaday 226 int
113     key_load_photo (HWND dlg, gpgme_key_t key, gpgme_validity_t *r_valid)
114 werner 36 {
115     winpt_key_s k;
116 twoaday 256 FILE *fp;
117 werner 36 const BYTE *img;
118     DWORD imglen = 0;
119     int pos=0;
120    
121 twoaday 211 if (winpt_get_pubkey (key->subkeys->keyid, &k))
122     BUG (0);
123 werner 36 img = k.ext->attrib.d;
124     imglen = k.ext->attrib.len;
125 twoaday 217 if (img && !k.ext->attrib.validity)
126 werner 36 get_uat_validity (key->subkeys->keyid, &k.ext->attrib.validity);
127 twoaday 226 if (r_valid)
128     *r_valid = k.ext->attrib.validity;
129 werner 36
130 twoaday 129 if (!img || !imglen) {
131     draw_nophoto_img (dlg);
132 werner 36 return -1;
133 twoaday 129 }
134 twoaday 133
135 twoaday 256 fp = fopen (get_photo_tmpname (dlg), "wb");
136     if (fp) {
137 werner 36 pos += 16;
138 twoaday 256 fwrite (img + pos, 1, imglen - pos, fp);
139     fclose (fp);
140 werner 36 }
141     return 0;
142     }
143    
144    
145     /* Return string representation of the key validity. @key. */
146     static const char*
147     get_validity (gpgme_key_t key)
148     {
149 twoaday 211 if (key->expired)
150 werner 36 return _("Expired");
151 twoaday 211 if (key->revoked)
152 werner 36 return _("Revoked");
153 twoaday 211 if (key->disabled)
154 twoaday 41 return _("Disabled");
155 twoaday 225 if (key->invalid)
156     return _("Invalid");
157 werner 36 return get_key_trust2 (NULL, key->uids->validity, 0, 0);
158     }
159    
160    
161     /* Return the preferred sym. algorithm from @key as a string. */
162     static const char*
163     get_pref_cipher (winpt_key_t k)
164     {
165     if (k->is_v3)
166     return "IDEA";
167 twoaday 211 if (!k->ext || !k->ext->sym_prefs)
168 werner 36 return "3DES";
169     switch (*k->ext->sym_prefs) {
170     case 1: return "IDEA";
171     case 2: return "3DES";
172     case 3: return "CAST5";
173     case 4: return "Blowfish";
174 twoaday 211 case 7: return "AES128";
175     case 8: return "AES192";
176     case 9: return "AES256";
177 werner 36 case 10:return "Twofish";
178 twoaday 256 default:break;
179 werner 36 }
180     return "Unknown";
181     }
182    
183    
184     /* Return true if the key has designated revokers. */
185     static bool
186     check_for_desig_rev (gpgme_key_t key)
187     {
188 twoaday 225 winpt_key_s k;
189 twoaday 217
190 twoaday 225 memset (&k, 0, sizeof (k));
191     if (!winpt_get_pubkey (key->subkeys->keyid, &k))
192     return k.ext->gloflags.has_desig_rev? true : false;
193 werner 36 return false;
194     }
195    
196    
197     /* Print information (name) of the smart card. */
198     static const char*
199     get_card_type (winpt_key_t k)
200     {
201     static char buf[64];
202    
203 twoaday 181 if (!k->ext || !k->ext->card_type)
204 werner 36 return "";
205 twoaday 217 _snprintf (buf, DIM (buf)-1, _("Card-Type: %s\r\n"), k->ext->card_type);
206 werner 36 return buf;
207     }
208    
209    
210 twoaday 211 /* Return 1 if at least one user-ID is valid. */
211     static int
212 twoaday 217 key_is_valid (gpgme_key_t key)
213 twoaday 211 {
214     gpgme_user_id_t u;
215    
216 twoaday 217 for (u=key->uids; u; u=u->next) {
217 twoaday 211 if (u->validity >= GPGME_VALIDITY_MARGINAL)
218     return 1;
219     }
220     return 0;
221     }
222    
223    
224 twoaday 273 /* Return extended algorithm information. */
225     const char*
226     props_get_key_algo (gpgme_key_t key, int idx)
227     {
228     /* PGP calls the old RSAv3 keys 'RSA Legacy' and because this
229     is a good method to differ between OpenPGP v4 cert-only keys
230     and v3 RSA keys, we use the same notation. */
231     if (key->subkeys != NULL && strlen (key->subkeys->fpr) == 32)
232     return "RSA Legacy";
233     return get_key_algo (key, idx);
234     }
235 twoaday 211
236 twoaday 273
237 werner 36 /* Display the key information for key @k.
238     Return value: gpgme key on success. */
239     static void
240 twoaday 211 display_key_info (HWND dlg, winpt_key_t k)
241 werner 36 {
242 twoaday 181 gpgme_key_t key;
243 twoaday 211 struct winpt_key_s sk;
244 werner 36 char info[512];
245     const char *inf;
246 twoaday 181 DWORD created, expires;
247 werner 36
248 twoaday 211 gpg_keycache_update_attr (k->ext, KC_ATTR_PREFSYM, 0);
249 twoaday 217 memset (&sk, 0, sizeof (sk));
250 twoaday 181 if (k->key_pair && !winpt_get_seckey (k->keyid, &sk))
251     k->is_protected = sk.is_protected;
252 twoaday 211 key = k->ext->key;
253 twoaday 181 created = key->subkeys->timestamp;
254     expires = key->subkeys->expires;
255 werner 36 _snprintf (info, DIM (info)-1,
256     _("Type: %s\r\n"
257 twoaday 211 "Key ID: 0x%s\r\n"
258 werner 36 "Algorithm: %s\r\n"
259 twoaday 128 "Size: %s bits\r\n"
260 werner 36 "Created: %s\r\n"
261     "Expires: %s\r\n"
262     "Validity: %s\r\n"
263     "Cipher: %s\r\n"
264     "%s\r\n"),
265     get_key_type (key),
266     k->keyid,
267 twoaday 273 props_get_key_algo (key, 0),
268 werner 36 get_key_size (key, 0),
269     get_key_created (created),
270     get_key_expire_date (expires),
271     get_validity (key),
272 twoaday 211 get_pref_cipher (k),
273 twoaday 181 get_card_type (&sk));
274 werner 36
275     SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info);
276     SetDlgItemText (dlg, IDC_KEYPROPS_FPR, get_key_fpr (key));
277 twoaday 150 inf = ownertrust_to_string (key->owner_trust, k->key_pair);
278 werner 36 SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);
279     }
280    
281    
282 twoaday 217 /* Context to store associated data of the dialog. */
283     struct prop_info_s {
284     winpt_key_t key;
285     };
286    
287    
288     static void
289     on_init_dialog (HWND dlg, WPARAM wparam, LPARAM lparam)
290     {
291     gpgme_validity_t valid;
292    
293     winpt_key_t k = (winpt_key_t)lparam;
294     SetWindowText (dlg, _("Key Properties"));
295     SetDlgItemText (dlg, IDC_KEYPROPS_OT_CHANGE, _("&Change"));
296     SetDlgItemText (dlg, IDC_KEYPROPS_REVOKERS, _("&Revokers"));
297     SetDlgItemText (dlg, IDC_KEYPROPS_CHANGE_PWD, _("Change &Password"));
298     SetDlgItemText (dlg, IDC_KEYPROPS_OTINF, _("Ownertrust"));
299    
300     display_key_info (dlg, k);
301 twoaday 226 if (!key_load_photo (dlg, k->ctx, &valid)) {
302 twoaday 217 k->has_photo = 1;
303     if (valid < GPGME_VALIDITY_MARGINAL)
304     SetDlgItemText (dlg, IDC_KEYPROPS_IMGINF, _("Photo-ID not validated."));
305 twoaday 273 }
306 twoaday 217 if (k->key_pair)
307     EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_CHANGE_PWD), TRUE);
308     if (check_for_desig_rev (k->ctx))
309     EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_REVOKERS), TRUE);
310     if (do_check_key (k->ctx))
311     EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_OT_CHANGE), FALSE);
312     center_window (dlg, NULL);
313     SetForegroundWindow (dlg);
314     }
315    
316    
317 werner 36 /* Dialog box procedure to show the key properties. */
318     BOOL CALLBACK
319     keyprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
320     {
321 twoaday 217 struct prop_info_s *prop = NULL;
322 werner 36 const char *inf;
323     int rc;
324    
325 twoaday 217 if (msg != WM_INITDIALOG &&
326     (prop = (prop_info_s*)GetWindowLong (dlg, GWL_USERDATA)) == NULL)
327     return FALSE;
328    
329 werner 36 switch (msg) {
330     case WM_INITDIALOG:
331 twoaday 234 assert (lparam != 0);
332 twoaday 217 prop = new struct prop_info_s;
333     prop->key = (winpt_key_t)lparam;
334     SetWindowLong (dlg, GWL_USERDATA, (LONG)prop);
335     on_init_dialog (dlg, wparam, lparam);
336 werner 36 return TRUE;
337    
338     case WM_DESTROY:
339 twoaday 226 key_unload_photo (dlg);
340     delete prop;prop = NULL;
341 twoaday 217 SetWindowLong (dlg, GWL_USERDATA, 0);
342 werner 36 break;
343    
344     case WM_PAINT:
345 twoaday 226 /* Display the photo in the frame of the dialog @dlg.
346     The coordinates are fixed to (0,0). */
347     if (prop->key->has_photo) {
348     POINT p;
349     p.x = p.y = 0;
350     PTD_jpg_show (GetDlgItem (dlg, IDC_KEYPROPS_IMG),
351 twoaday 256 &p, get_photo_tmpname (dlg));
352 twoaday 226 }
353 werner 36 break;
354 twoaday 226
355 werner 36 case WM_COMMAND:
356     switch (LOWORD (wparam)) {
357     case IDOK:
358     EndDialog (dlg, TRUE);
359     return TRUE;
360 twoaday 170
361     case IDCANCEL:
362     EndDialog (dlg, FALSE);
363     return TRUE;
364 werner 36
365     case IDC_KEYPROPS_OT_CHANGE:
366 twoaday 217 if (!prop->key->key_pair && !key_is_valid (prop->key->ctx)) {
367 twoaday 150 rc = msg_box (dlg, _("This is a non-valid key.\n"
368 werner 36 "Modifying the ownertrust has no effect on such keys.\n\n"
369     "Do you really want to continue?"),
370 twoaday 150 _("WinPT Warning"), MB_ICONWARNING|MB_YESNO);
371 werner 36 if (rc == IDNO)
372     return TRUE;
373     }
374 twoaday 165 rc = dialog_box_param (glob_hinst,
375     (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST,
376     dlg, keyedit_ownertrust_dlg_proc,
377 twoaday 217 (LPARAM)prop->key, _("Change Ownertrust"),
378 twoaday 165 IDS_WINPT_KEYEDIT_OWNERTRUST);
379     if (rc == FALSE) /* Cancel */
380     return TRUE;
381 werner 36
382 twoaday 217 inf = ownertrust_to_string (prop->key->callback.new_val,
383     prop->key->key_pair);
384 werner 36 SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);
385     msg_box (dlg, _("Ownertrust successfully changed."),
386     _("GnuPG Status"), MB_OK);
387 twoaday 217 prop->key->update = 1;
388 werner 36 return TRUE;
389    
390     case IDC_KEYPROPS_CHANGE_PWD:
391 twoaday 217 keyedit_change_passwd (prop->key, dlg);
392 werner 36 return TRUE;
393    
394     case IDC_KEYPROPS_REVOKERS:
395 twoaday 217 prop->key->update = dialog_box_param (glob_hinst,
396     (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg,
397     key_revokers_dlg_proc, (LPARAM)prop->key,
398     _("Key Revokers"),
399     IDS_WINPT_KEY_REVOKERS);
400     UpdateWindow (dlg);
401 werner 36 break;
402     }
403     }
404    
405     return FALSE;
406     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26