1 |
/* wptKeyPropsDlg.cpp - WinPT key properties dialog |
2 |
* Copyright (C) 2000, 2001, 2002, 2003, 2005 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 |
|
21 |
#include <windows.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 |
static void |
38 |
do_change_ownertrust (winpt_key_t k, const char *s) |
39 |
{ |
40 |
char ot[64]; |
41 |
|
42 |
if( strstr( s, "ultimate" ) ) { |
43 |
listview_get_item_text (k->callback.ctl, k->callback.idx, 5, ot, DIM (ot)-1); |
44 |
strcpy (ot, "Ultimate"); |
45 |
listview_add_sub_item (k->callback.ctl, k->callback.idx, 5, ot); |
46 |
/* fixme: If we switch back from Ultimate to a lower level */ |
47 |
} |
48 |
} /* do_change_ownertrust */ |
49 |
|
50 |
|
51 |
static int |
52 |
do_check_key (gpgme_key_t key) |
53 |
{ |
54 |
int okay = 0; |
55 |
okay = key->expired; |
56 |
if (!okay) |
57 |
okay = key->revoked; |
58 |
return okay; |
59 |
} /* do_check_key */ |
60 |
|
61 |
|
62 |
static const char* |
63 |
ownertrust_to_string (GpgKeyEdit *ctx, int val) |
64 |
{ |
65 |
const char * inf; |
66 |
int id; |
67 |
|
68 |
if (ctx) |
69 |
id = ctx->trust_id; |
70 |
else if (!ctx && val) |
71 |
id = val; |
72 |
switch (id) { |
73 |
case 1: inf = _("Don't know"); break; |
74 |
case 2: inf = _("I do NOT trust"); break; |
75 |
case 3: inf = _("I trust marginally"); break; |
76 |
case 4: inf = _("I trust fully"); break; |
77 |
case 5: |
78 |
case 6: inf = _("I trust ultimately"); break; |
79 |
default:inf = _("Unknown"); break; |
80 |
} |
81 |
|
82 |
return inf; |
83 |
} /* ownertrust_to_string */ |
84 |
|
85 |
|
86 |
#define PHOTO_TMPNAME "winpt_temp_photo.jpg" |
87 |
|
88 |
static int |
89 |
keyprops_show_photo (HWND dlg, gpgme_key_t key) |
90 |
{ |
91 |
RECT r; |
92 |
POINT p; |
93 |
HWND h; |
94 |
const BYTE *img; |
95 |
DWORD imglen = 0; |
96 |
int pos=0; |
97 |
winpt_key_s k; |
98 |
|
99 |
winpt_get_pubkey (key->subkeys->keyid, &k); |
100 |
img = k.ext->attrib.d; |
101 |
imglen = k.ext->attrib.len; |
102 |
|
103 |
if (!img || !imglen) |
104 |
return -1; |
105 |
FILE *f = fopen (PHOTO_TMPNAME, "wb"); |
106 |
if (f) { |
107 |
for (pos = 0; img[pos] != 0x10; pos++) |
108 |
; |
109 |
pos += 16; |
110 |
fwrite (img + pos, 1, imglen - pos, f); |
111 |
fwrite (img, 1, imglen, f); |
112 |
fclose (f); |
113 |
} |
114 |
|
115 |
h = GetDlgItem (dlg, IDC_KEYPROPS_IMG); |
116 |
GetWindowRect (h, &r); |
117 |
p.x = r.left + 5; |
118 |
p.y = r.top - 2; |
119 |
memset (&p, 0, sizeof (p)); |
120 |
PTD_jpg_show (h, &p, PHOTO_TMPNAME); |
121 |
unlink (PHOTO_TMPNAME); |
122 |
|
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 |
int val; |
132 |
val = key->expired; |
133 |
if (val) |
134 |
return "Expired"; |
135 |
val = key->revoked; |
136 |
if (val) |
137 |
return "Revoked"; |
138 |
return get_key_trust2 (NULL, key->uids->validity, 0, 0); |
139 |
} |
140 |
|
141 |
|
142 |
/* Return string representation of cipher @algo. */ |
143 |
static const char* |
144 |
get_pref_str_cipher (int algo) |
145 |
{ |
146 |
/* XXX: finish the code */ |
147 |
switch (algo) { |
148 |
case 1: return "IDEA"; |
149 |
case 2: return "3DES"; |
150 |
case 4: return "Blowfish"; |
151 |
case 7: |
152 |
case 8: |
153 |
case 9: return "AES"; |
154 |
case 10: return "Twofish"; |
155 |
} |
156 |
return "Unknown"; |
157 |
} |
158 |
|
159 |
|
160 |
/* Return the preferred sym. algorithm from @key. */ |
161 |
static const char* |
162 |
get_pref_cipher (gpgme_key_t key) |
163 |
{ |
164 |
const char *sym_prefs=NULL; |
165 |
winpt_key_s k; |
166 |
|
167 |
winpt_get_seckey (key->subkeys->keyid, &k); |
168 |
if (k.is_v3) |
169 |
return "IDEA"; |
170 |
if (!k.ext->sym_prefs) |
171 |
return "3DES"; |
172 |
return get_pref_str_cipher (*k.ext->sym_prefs); |
173 |
} |
174 |
|
175 |
|
176 |
/* Return true if the key has designated revokers. */ |
177 |
static bool |
178 |
check_for_desig_rev (gpgme_key_t key) |
179 |
{ |
180 |
winpt_key_s k; |
181 |
if (!winpt_get_pubkey (key->subkeys->keyid, &k)) |
182 |
return k.ext->gloflags.has_desig_rev? true : false; |
183 |
return false; |
184 |
} |
185 |
|
186 |
|
187 |
/* Dialog box procedure to show the key properties. */ |
188 |
BOOL CALLBACK |
189 |
keyprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
190 |
{ |
191 |
static winpt_key_t k; |
192 |
static gpgme_key_t key, sk; |
193 |
static int photo_done = 0; |
194 |
struct winpt_key_s k2; |
195 |
GpgKeyEdit *ek; |
196 |
refresh_cache_s rcs = {0}; |
197 |
const char *inf; |
198 |
char info[2048]; |
199 |
u32 created, expires; |
200 |
int ot, cancel = 0; |
201 |
int rc; |
202 |
|
203 |
switch (msg) { |
204 |
case WM_INITDIALOG: |
205 |
if (!lparam) |
206 |
dlg_fatal_error( dlg, "Could not get dialog param!" ); |
207 |
k = (winpt_key_t)lparam; |
208 |
#ifndef LANG_DE |
209 |
SetWindowText( dlg, _("Key Properties") ); |
210 |
SetDlgItemText (dlg, IDC_KEYPROPS_OT_CHANGE, _("&Change")); |
211 |
SetDlgItemText (dlg, IDC_KEYPROPS_REVOKERS, _("&Revokers")); |
212 |
SetDlgItemText (dlg, IDC_KEYPROPS_CHANGE_PWD, _("Change &Passwd")); |
213 |
#endif |
214 |
|
215 |
photo_done = 0; |
216 |
winpt_get_seckey (k->keyid, &k2); |
217 |
sk = k2.ctx; |
218 |
if (sk) |
219 |
k->is_protected = k2.is_protected; |
220 |
if (get_pubkey (k->keyid, &key)) |
221 |
BUG (0); |
222 |
created = key->subkeys->timestamp; |
223 |
expires = key->subkeys->expires; |
224 |
_snprintf (info, DIM (info)-1, |
225 |
"Type: %s\r\n" |
226 |
"Key ID: %s\r\n" |
227 |
"Algorithm: %s\r\n" |
228 |
"Size: %s\r\n" |
229 |
"Created: %s\r\n" |
230 |
"Expires: %s\r\n" |
231 |
"Validity: %s\r\n" |
232 |
"Cipher: %s\r\n", |
233 |
get_key_type (key), |
234 |
k->keyid, |
235 |
get_key_algo (key, 0), |
236 |
get_key_size (key, 0), |
237 |
get_key_created (created), |
238 |
get_key_expire_date (expires), |
239 |
get_validity (key), |
240 |
get_pref_cipher (key)); |
241 |
SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info); |
242 |
SetDlgItemText (dlg, IDC_KEYPROPS_FPR, get_key_fpr (key)); |
243 |
ot = gpgme_key_get_ulong_attr( key, GPGME_ATTR_OTRUST, NULL, 0 ); |
244 |
inf = ownertrust_to_string (NULL, ot); |
245 |
SetDlgItemText( dlg, IDC_KEYPROPS_OT, inf ); |
246 |
if (k->key_pair) |
247 |
EnableWindow( GetDlgItem( dlg, IDC_KEYPROPS_CHANGE_PWD ), TRUE ); |
248 |
if (check_for_desig_rev (key)) |
249 |
EnableWindow( GetDlgItem( dlg, IDC_KEYPROPS_REVOKERS ), TRUE ); |
250 |
center_window (dlg, NULL); |
251 |
SetForegroundWindow (dlg); |
252 |
return TRUE; |
253 |
|
254 |
case WM_PAINT: |
255 |
if (photo_done == 0) { |
256 |
photo_done = 1; |
257 |
keyprops_show_photo (dlg, key); |
258 |
} |
259 |
break; |
260 |
|
261 |
case WM_SYSCOMMAND: |
262 |
if( LOWORD( wparam ) == SC_CLOSE ) |
263 |
EndDialog( dlg, TRUE ); |
264 |
return FALSE; |
265 |
|
266 |
case WM_COMMAND: |
267 |
switch( LOWORD( wparam ) ) { |
268 |
case IDOK: |
269 |
EndDialog( dlg, TRUE ); |
270 |
return TRUE; |
271 |
|
272 |
case IDC_KEYPROPS_OT_CHANGE: |
273 |
if( do_check_key( key ) ) { |
274 |
msg_box( dlg, _("The status of this key is 'revoked' or 'expired'.\n" |
275 |
"You cannot change the ownertrust of such keys."), |
276 |
_("WinPT Warning"), MB_ERR ); |
277 |
return FALSE; |
278 |
} |
279 |
if( !k->key_pair && key->uids->validity < 3 ) { |
280 |
rc = msg_box( dlg, _("This is a non-valid key.\n" |
281 |
"Modifying the ownertrust has no effect on such keys.\n\n" |
282 |
"Do you really want to continue?"), |
283 |
_("WinPT Warning"), MB_ICONWARNING|MB_YESNO ); |
284 |
if (rc == IDNO) |
285 |
return FALSE; |
286 |
} |
287 |
GetDlgItemText( dlg, IDC_KEYPROPS_OT, info, sizeof info -1 ); |
288 |
|
289 |
ek = new GpgKeyEdit (k->keyid); |
290 |
dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST, |
291 |
dlg, (DLGPROC)keyedit_ownertrust_dlg_proc, |
292 |
(LPARAM)ek, _("Change Ownertrust"), |
293 |
IDS_WINPT_KEYEDIT_OWNERTRUST); |
294 |
|
295 |
inf = ownertrust_to_string (ek, 0); |
296 |
k->callback.new_val = ek->trust_id; |
297 |
do_change_ownertrust (k, inf); |
298 |
SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf); |
299 |
msg_box (dlg, _("Ownertrust successfully changed."), _("GnuPG Status"), MB_OK); |
300 |
|
301 |
/* reload only the keylist */ |
302 |
rcs.kr_reload = 1; rcs.kr_update = 1; |
303 |
rcs.tr_update = 0; |
304 |
DialogBoxParam( glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, dlg, |
305 |
keycache_dlg_proc, (LPARAM)&rcs ); |
306 |
get_seckey (k->keyid, &sk); |
307 |
if (get_pubkey (k->keyid, &key)) |
308 |
BUG (0); |
309 |
delete ek; |
310 |
return TRUE; |
311 |
|
312 |
case IDC_KEYPROPS_CHANGE_PWD: |
313 |
keyedit_change_passwd (k, dlg); |
314 |
return TRUE; |
315 |
|
316 |
case IDC_KEYPROPS_REVOKERS: |
317 |
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg, |
318 |
key_revokers_dlg_proc, (LPARAM)key, _("Key Revokers"), |
319 |
IDS_WINPT_KEY_REVOKERS); |
320 |
break; |
321 |
} |
322 |
} |
323 |
|
324 |
return FALSE; |
325 |
} |
326 |
|