1 |
twoaday |
256 |
/* wptKeyPropsDlg.cpp - WinPT key property dialog |
2 |
twoaday |
328 |
* Copyright (C) 2000-2003, 2005-2006, 2008 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 |
werner |
42 |
#ifdef HAVE_CONFIG_H |
17 |
|
|
#include <config.h> |
18 |
|
|
#endif |
19 |
|
|
|
20 |
werner |
36 |
#include <windows.h> |
21 |
twoaday |
217 |
#include <assert.h> |
22 |
werner |
36 |
|
23 |
werner |
47 |
#include "resource.h" |
24 |
werner |
36 |
#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 |
twoaday |
328 |
/* 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 |
werner |
36 |
/* Check that the key is not expired or revoked. */ |
46 |
|
|
static int |
47 |
|
|
do_check_key (gpgme_key_t key) |
48 |
|
|
{ |
49 |
twoaday |
273 |
int invalid; |
50 |
twoaday |
217 |
|
51 |
|
|
invalid = key->expired; |
52 |
|
|
if (!invalid) |
53 |
|
|
invalid = key->revoked; |
54 |
|
|
return invalid; |
55 |
werner |
36 |
} |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
/* Convert a trust integer into a string representation. */ |
59 |
|
|
static const char* |
60 |
twoaday |
150 |
ownertrust_to_string (int val, bool is_keypair) |
61 |
werner |
36 |
{ |
62 |
twoaday |
41 |
const char *inf; |
63 |
werner |
36 |
int id = val; |
64 |
twoaday |
41 |
|
65 |
werner |
36 |
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 |
twoaday |
150 |
case 6: |
72 |
|
|
if (is_keypair) |
73 |
|
|
inf = _("I trust ultimately (implicit)"); |
74 |
|
|
else |
75 |
|
|
inf = _("I trust ultimately"); break; |
76 |
werner |
36 |
default:inf = _("Unknown"); break; |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
return inf; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
|
83 |
twoaday |
328 |
int |
84 |
|
|
get_photo_tmpname (gpgme_key_t key, char *buf, size_t buflen) |
85 |
werner |
36 |
{ |
86 |
twoaday |
328 |
const char *fmt = "winpt_photo_%p.tmp"; |
87 |
twoaday |
174 |
char name[64]; |
88 |
werner |
36 |
|
89 |
twoaday |
328 |
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 |
werner |
36 |
} |
95 |
|
|
|
96 |
|
|
|
97 |
twoaday |
129 |
|
98 |
|
|
|
99 |
werner |
36 |
/* Load the photo from the key @key */ |
100 |
twoaday |
226 |
int |
101 |
twoaday |
328 |
key_load_photo (winpt_key_t key, |
102 |
|
|
char *photo_file, size_t photo_file_size, |
103 |
|
|
gpgme_validity_t *r_valid) |
104 |
werner |
36 |
{ |
105 |
twoaday |
256 |
FILE *fp; |
106 |
werner |
36 |
const BYTE *img; |
107 |
twoaday |
310 |
DWORD imglen; |
108 |
twoaday |
328 |
|
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 |
twoaday |
226 |
if (r_valid) |
115 |
twoaday |
328 |
*r_valid = key->ext->attrib.validity; |
116 |
werner |
36 |
|
117 |
twoaday |
328 |
if (!img || imglen < 1) |
118 |
werner |
36 |
return -1; |
119 |
twoaday |
328 |
|
120 |
|
|
get_photo_tmpname (key->ctx, photo_file, photo_file_size); |
121 |
|
|
fp = fopen (photo_file, "wb"); |
122 |
|
|
if (fp != NULL) { |
123 |
twoaday |
310 |
const int pos = 16; |
124 |
twoaday |
256 |
fwrite (img + pos, 1, imglen - pos, fp); |
125 |
|
|
fclose (fp); |
126 |
twoaday |
328 |
return 0; |
127 |
werner |
36 |
} |
128 |
twoaday |
328 |
|
129 |
|
|
return -1; |
130 |
werner |
36 |
} |
131 |
|
|
|
132 |
|
|
|
133 |
|
|
/* Return string representation of the key validity. @key. */ |
134 |
|
|
static const char* |
135 |
|
|
get_validity (gpgme_key_t key) |
136 |
|
|
{ |
137 |
twoaday |
211 |
if (key->expired) |
138 |
werner |
36 |
return _("Expired"); |
139 |
twoaday |
211 |
if (key->revoked) |
140 |
werner |
36 |
return _("Revoked"); |
141 |
twoaday |
211 |
if (key->disabled) |
142 |
twoaday |
41 |
return _("Disabled"); |
143 |
twoaday |
225 |
if (key->invalid) |
144 |
|
|
return _("Invalid"); |
145 |
werner |
36 |
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 |
twoaday |
225 |
winpt_key_s k; |
154 |
twoaday |
217 |
|
155 |
twoaday |
225 |
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 |
werner |
36 |
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 |
twoaday |
181 |
if (!k->ext || !k->ext->card_type) |
169 |
werner |
36 |
return ""; |
170 |
twoaday |
328 |
_snprintf (buf, DIM (buf)-1, _("Card-Type: %s\r\n"), |
171 |
|
|
k->ext->card_type); |
172 |
werner |
36 |
return buf; |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
|
176 |
twoaday |
211 |
/* Return 1 if at least one user-ID is valid. */ |
177 |
|
|
static int |
178 |
twoaday |
217 |
key_is_valid (gpgme_key_t key) |
179 |
twoaday |
211 |
{ |
180 |
|
|
gpgme_user_id_t u; |
181 |
|
|
|
182 |
twoaday |
217 |
for (u=key->uids; u; u=u->next) { |
183 |
twoaday |
211 |
if (u->validity >= GPGME_VALIDITY_MARGINAL) |
184 |
|
|
return 1; |
185 |
|
|
} |
186 |
|
|
return 0; |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
|
190 |
twoaday |
273 |
/* 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 |
twoaday |
211 |
|
202 |
twoaday |
273 |
|
203 |
twoaday |
328 |
/* Display the key information for key @k. */ |
204 |
werner |
36 |
static void |
205 |
twoaday |
211 |
display_key_info (HWND dlg, winpt_key_t k) |
206 |
werner |
36 |
{ |
207 |
twoaday |
181 |
gpgme_key_t key; |
208 |
twoaday |
211 |
struct winpt_key_s sk; |
209 |
werner |
36 |
char info[512]; |
210 |
|
|
const char *inf; |
211 |
twoaday |
181 |
DWORD created, expires; |
212 |
werner |
36 |
|
213 |
twoaday |
211 |
gpg_keycache_update_attr (k->ext, KC_ATTR_PREFSYM, 0); |
214 |
twoaday |
217 |
memset (&sk, 0, sizeof (sk)); |
215 |
twoaday |
181 |
if (k->key_pair && !winpt_get_seckey (k->keyid, &sk)) |
216 |
|
|
k->is_protected = sk.is_protected; |
217 |
twoaday |
211 |
key = k->ext->key; |
218 |
twoaday |
181 |
created = key->subkeys->timestamp; |
219 |
|
|
expires = key->subkeys->expires; |
220 |
werner |
36 |
_snprintf (info, DIM (info)-1, |
221 |
|
|
_("Type: %s\r\n" |
222 |
twoaday |
211 |
"Key ID: 0x%s\r\n" |
223 |
werner |
36 |
"Algorithm: %s\r\n" |
224 |
twoaday |
128 |
"Size: %s bits\r\n" |
225 |
werner |
36 |
"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 |
twoaday |
273 |
props_get_key_algo (key, 0), |
232 |
werner |
36 |
get_key_size (key, 0), |
233 |
|
|
get_key_created (created), |
234 |
|
|
get_key_expire_date (expires), |
235 |
|
|
get_validity (key), |
236 |
twoaday |
181 |
get_card_type (&sk)); |
237 |
werner |
36 |
|
238 |
|
|
SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info); |
239 |
|
|
SetDlgItemText (dlg, IDC_KEYPROPS_FPR, get_key_fpr (key)); |
240 |
twoaday |
150 |
inf = ownertrust_to_string (key->owner_trust, k->key_pair); |
241 |
werner |
36 |
SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf); |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
|
245 |
twoaday |
217 |
|
246 |
|
|
static void |
247 |
|
|
on_init_dialog (HWND dlg, WPARAM wparam, LPARAM lparam) |
248 |
|
|
{ |
249 |
|
|
winpt_key_t k = (winpt_key_t)lparam; |
250 |
twoaday |
310 |
assert (k != NULL); |
251 |
twoaday |
217 |
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 |
twoaday |
328 |
SetDlgItemText (dlg, IDC_KEYPROPS_OTINF, _("Ownertrust:")); |
256 |
|
|
SetDlgItemText (dlg, IDC_KEYPROPS_FPRTXT, _("Fingerprint:")); |
257 |
twoaday |
217 |
|
258 |
|
|
display_key_info (dlg, k); |
259 |
twoaday |
328 |
|
260 |
twoaday |
217 |
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 |
werner |
36 |
/* 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 |
twoaday |
217 |
struct prop_info_s *prop = NULL; |
276 |
werner |
36 |
const char *inf; |
277 |
twoaday |
328 |
gpgme_validity_t valid; |
278 |
werner |
36 |
int rc; |
279 |
|
|
|
280 |
twoaday |
328 |
if (msg != WM_INITDIALOG) { |
281 |
|
|
prop = (struct prop_info_s*)GetWindowLong (dlg, GWL_USERDATA); |
282 |
|
|
if (prop == NULL) |
283 |
|
|
return FALSE; |
284 |
|
|
} |
285 |
twoaday |
217 |
|
286 |
werner |
36 |
switch (msg) { |
287 |
|
|
case WM_INITDIALOG: |
288 |
twoaday |
234 |
assert (lparam != 0); |
289 |
twoaday |
217 |
prop = new struct prop_info_s; |
290 |
|
|
prop->key = (winpt_key_t)lparam; |
291 |
twoaday |
328 |
prop->has_photo = 0; |
292 |
twoaday |
217 |
SetWindowLong (dlg, GWL_USERDATA, (LONG)prop); |
293 |
|
|
on_init_dialog (dlg, wparam, lparam); |
294 |
twoaday |
328 |
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 |
werner |
36 |
return TRUE; |
304 |
|
|
|
305 |
|
|
case WM_DESTROY: |
306 |
twoaday |
328 |
if (prop->has_photo) |
307 |
|
|
DeleteFile(prop->photo_file); |
308 |
twoaday |
226 |
delete prop;prop = NULL; |
309 |
twoaday |
217 |
SetWindowLong (dlg, GWL_USERDATA, 0); |
310 |
werner |
36 |
break; |
311 |
|
|
|
312 |
|
|
case WM_PAINT: |
313 |
twoaday |
226 |
/* Display the photo in the frame of the dialog @dlg. |
314 |
|
|
The coordinates are fixed to (0,0). */ |
315 |
twoaday |
328 |
if (prop->has_photo) { |
316 |
twoaday |
226 |
POINT p; |
317 |
|
|
p.x = p.y = 0; |
318 |
twoaday |
328 |
/* 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 |
twoaday |
226 |
} |
324 |
werner |
36 |
break; |
325 |
twoaday |
226 |
|
326 |
werner |
36 |
case WM_COMMAND: |
327 |
|
|
switch (LOWORD (wparam)) { |
328 |
|
|
case IDOK: |
329 |
|
|
EndDialog (dlg, TRUE); |
330 |
|
|
return TRUE; |
331 |
twoaday |
170 |
|
332 |
|
|
case IDCANCEL: |
333 |
|
|
EndDialog (dlg, FALSE); |
334 |
|
|
return TRUE; |
335 |
werner |
36 |
|
336 |
|
|
case IDC_KEYPROPS_OT_CHANGE: |
337 |
twoaday |
217 |
if (!prop->key->key_pair && !key_is_valid (prop->key->ctx)) { |
338 |
twoaday |
150 |
rc = msg_box (dlg, _("This is a non-valid key.\n" |
339 |
werner |
36 |
"Modifying the ownertrust has no effect on such keys.\n\n" |
340 |
|
|
"Do you really want to continue?"), |
341 |
twoaday |
150 |
_("WinPT Warning"), MB_ICONWARNING|MB_YESNO); |
342 |
werner |
36 |
if (rc == IDNO) |
343 |
|
|
return TRUE; |
344 |
|
|
} |
345 |
twoaday |
165 |
rc = dialog_box_param (glob_hinst, |
346 |
|
|
(LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST, |
347 |
|
|
dlg, keyedit_ownertrust_dlg_proc, |
348 |
twoaday |
217 |
(LPARAM)prop->key, _("Change Ownertrust"), |
349 |
twoaday |
165 |
IDS_WINPT_KEYEDIT_OWNERTRUST); |
350 |
|
|
if (rc == FALSE) /* Cancel */ |
351 |
|
|
return TRUE; |
352 |
werner |
36 |
|
353 |
twoaday |
217 |
inf = ownertrust_to_string (prop->key->callback.new_val, |
354 |
|
|
prop->key->key_pair); |
355 |
werner |
36 |
SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf); |
356 |
|
|
msg_box (dlg, _("Ownertrust successfully changed."), |
357 |
|
|
_("GnuPG Status"), MB_OK); |
358 |
twoaday |
217 |
prop->key->update = 1; |
359 |
werner |
36 |
return TRUE; |
360 |
|
|
|
361 |
|
|
case IDC_KEYPROPS_CHANGE_PWD: |
362 |
twoaday |
217 |
keyedit_change_passwd (prop->key, dlg); |
363 |
werner |
36 |
return TRUE; |
364 |
|
|
|
365 |
|
|
case IDC_KEYPROPS_REVOKERS: |
366 |
twoaday |
217 |
prop->key->update = dialog_box_param (glob_hinst, |
367 |
twoaday |
328 |
(LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg, |
368 |
|
|
key_revokers_dlg_proc, |
369 |
|
|
(LPARAM)prop->key, |
370 |
|
|
_("Key Revokers"), |
371 |
|
|
IDS_WINPT_KEY_REVOKERS); |
372 |
twoaday |
217 |
UpdateWindow (dlg); |
373 |
werner |
36 |
break; |
374 |
|
|
} |
375 |
|
|
} |
376 |
|
|
|
377 |
|
|
return FALSE; |
378 |
|
|
} |