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

Contents of /trunk/Src/wptKeyPropsDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 225 - (show annotations)
Tue Jun 6 13:37:59 2006 UTC (18 years, 8 months ago) by twoaday
File size: 10465 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26