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