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