1 |
/* wptKeysignDlg.cpp - Key signing dialog |
2 |
* Copyright (C) 2001-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 <commctrl.h> |
26 |
|
27 |
#include "resource.h" |
28 |
#include "wptGPG.h" |
29 |
#include "wptNLS.h" |
30 |
#include "wptW32API.h" |
31 |
#include "wptVersion.h" |
32 |
#include "wptTypes.h" |
33 |
#include "wptErrors.h" |
34 |
#include "wptCommonCtl.h" |
35 |
#include "wptContext.h" |
36 |
#include "wptDlgs.h" |
37 |
#include "wptUTF8.h" |
38 |
#include "wptRegistry.h" |
39 |
#include "wptKeylist.h" |
40 |
#include "wptKeyEdit.h" |
41 |
|
42 |
|
43 |
/* Return a beautified printable fingerprint of @fpr. */ |
44 |
static const char* |
45 |
get_printable_fpr (const char *fpr) |
46 |
{ |
47 |
static char pfpr[64]; |
48 |
int pos = 0; |
49 |
size_t i; |
50 |
|
51 |
for (i = 0; i < strlen (fpr); i += 4) { |
52 |
pfpr[pos++] = fpr[i]; |
53 |
pfpr[pos++] = fpr[i+1]; |
54 |
pfpr[pos++] = fpr[i+2]; |
55 |
pfpr[pos++] = fpr[i+3]; |
56 |
pfpr[pos++] = ' '; |
57 |
} |
58 |
return pfpr; |
59 |
} |
60 |
|
61 |
|
62 |
/* Return human friendly information about the key @key. */ |
63 |
static const char* |
64 |
get_keyinfo (gpgme_key_t key) |
65 |
{ |
66 |
static char buf[64+16]; |
67 |
struct winpt_key_s k; |
68 |
|
69 |
memset (&k, 0, sizeof (k)); |
70 |
winpt_get_seckey (key->subkeys->keyid, &k); |
71 |
_snprintf (buf, DIM (buf)-1-16, "%d-bit %s key, ID %s", |
72 |
key->subkeys->length, |
73 |
get_key_pubalgo (key->subkeys->pubkey_algo), |
74 |
key->subkeys->keyid+8); |
75 |
if (k.ext->gloflags.divert_to_card) |
76 |
strcat (buf, " (Card)"); |
77 |
return buf; |
78 |
} |
79 |
|
80 |
|
81 |
/* Fill the secret key combo-box with all entries from the cache. |
82 |
@dlg is the handle to the combo-box. @keyid show which key to skip. |
83 |
Return value: 0 on success. */ |
84 |
static int |
85 |
do_fill_seckeylist (HWND dlg, const char *keyid) |
86 |
{ |
87 |
gpg_keycache_t sec; |
88 |
gpgme_key_t pk, defkey; |
89 |
const char *s; |
90 |
char *uid, *p; |
91 |
int i = 0, n = 0, curr_sel = 0; |
92 |
|
93 |
sec = keycache_get_ctx (0); |
94 |
if (!sec) |
95 |
BUG (0); |
96 |
gpg_keycache_get_default_key (sec, &defkey); |
97 |
gpg_keycache_rewind (sec); |
98 |
while (!gpg_keycache_next_key (sec, 1, &pk)) { |
99 |
if (!pk) |
100 |
continue; |
101 |
s = pk->subkeys->keyid; |
102 |
if (!strcmp (s, keyid)) |
103 |
continue; |
104 |
/* skip all ElGamal sign+encrypt keys */ |
105 |
if (pk->subkeys->pubkey_algo == GPGME_PK_ELG) |
106 |
continue; |
107 |
/* make sure the public key is okay not: revoked, expired or disabled. */ |
108 |
if (pk->expired ||pk->revoked || pk->disabled) |
109 |
continue; |
110 |
s = pk->uids->name; |
111 |
if (!s) |
112 |
continue; |
113 |
if (defkey && !strcmp (defkey->subkeys->keyid, pk->subkeys->keyid)) |
114 |
curr_sel = i; |
115 |
uid = utf8_to_wincp (s, strlen (s)); |
116 |
p = new char[strlen (uid) + 64]; |
117 |
if (!p) |
118 |
BUG (NULL); |
119 |
_snprintf (p, strlen (uid) + 63, "%s (%s)", uid, get_keyinfo (pk)); |
120 |
SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, |
121 |
CB_ADDSTRING, i, (LPARAM)(char *)p); |
122 |
SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, |
123 |
CB_SETITEMDATA, i++, (LPARAM)(DWORD)pk); |
124 |
free_if_alloc (p); |
125 |
free (uid); |
126 |
n++; |
127 |
} |
128 |
SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, |
129 |
CB_SETCURSEL, (WPARAM)curr_sel, 0); |
130 |
if (!n) |
131 |
return -1; |
132 |
return 0; |
133 |
} |
134 |
|
135 |
|
136 |
/* Check if the selected key is protected and en- or disable the |
137 |
passphrase control. */ |
138 |
static void |
139 |
do_check_protection (HWND dlg) |
140 |
{ |
141 |
int idx, protec; |
142 |
gpgme_key_t key; |
143 |
struct winpt_key_s k; |
144 |
|
145 |
idx = SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0 ); |
146 |
key = (gpgme_key_t)SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETITEMDATA, (WPARAM)idx, 0 ); |
147 |
if (key) { |
148 |
winpt_get_seckey (key->subkeys->keyid, &k); |
149 |
protec = k.is_protected; |
150 |
if (!protec) |
151 |
protec = k.ext->gloflags.divert_to_card; |
152 |
EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE), protec? TRUE : FALSE); |
153 |
} |
154 |
} |
155 |
|
156 |
|
157 |
/* Dialog box procedure to choose the signature class. */ |
158 |
BOOL CALLBACK |
159 |
sig_class_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
160 |
{ |
161 |
int sig_class = 0; |
162 |
|
163 |
switch (msg) { |
164 |
case WM_INITDIALOG: |
165 |
SetWindowText (dlg, _("Choose Signature Class")); |
166 |
SetDlgItemText (dlg, IDC_SIGCLASS_TITLEINF, _("How carefully have you verified the key you are about to sign actually belongs to the person? If you don't know what to anwser, use \"0\".")); |
167 |
SetDlgItemText (dlg, IDC_SIGCLASS_CLASS0, _("(0) I will not answer (default)")); |
168 |
SetDlgItemText (dlg, IDC_SIGCLASS_CLASS1, _("(1) I have not checked at all.")); |
169 |
SetDlgItemText (dlg, IDC_SIGCLASS_CLASS2, _("(2) I have done causal checking.")); |
170 |
SetDlgItemText (dlg, IDC_SIGCLASS_CLASS3, _("(3) I have done very careful checkings.")); |
171 |
CheckDlgButton (dlg, IDC_SIGCLASS_CLASS0, BST_CHECKED); |
172 |
SetForegroundWindow (dlg); |
173 |
center_window (dlg, NULL); |
174 |
return TRUE; |
175 |
|
176 |
case WM_COMMAND: |
177 |
switch (LOWORD (wparam)) { |
178 |
case IDOK: |
179 |
if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS0)) |
180 |
sig_class = 0; |
181 |
else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS1)) |
182 |
sig_class = 1; |
183 |
else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS2)) |
184 |
sig_class = 2; |
185 |
else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS3)) |
186 |
sig_class = 3; |
187 |
else |
188 |
sig_class = 0; |
189 |
EndDialog (dlg, sig_class); |
190 |
return TRUE; |
191 |
|
192 |
case IDCANCEL: |
193 |
EndDialog (dlg, 0); |
194 |
return TRUE; |
195 |
} |
196 |
break; |
197 |
} |
198 |
|
199 |
return FALSE; |
200 |
} |
201 |
|
202 |
|
203 |
/* Return the humand friendly expiration date of @key. */ |
204 |
static const char* |
205 |
get_expire_date (gpgme_key_t key) |
206 |
{ |
207 |
u32 u = key->subkeys->expires; |
208 |
if (!u) |
209 |
return _("never"); |
210 |
return get_key_expire_date (u); |
211 |
} |
212 |
|
213 |
/* Display photo of key @key in a separate window. */ |
214 |
static void |
215 |
show_photo (winpt_key_t key) |
216 |
{ |
217 |
/* XXX: fill it with life. */ |
218 |
} |
219 |
|
220 |
|
221 |
/* Dialog box procedure to sign a key. */ |
222 |
BOOL CALLBACK |
223 |
keysign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
224 |
{ |
225 |
static winpt_key_t key; |
226 |
GpgKeyEdit *ke; |
227 |
gpgme_error_t err; |
228 |
gpgme_key_t k; |
229 |
SYSTEMTIME st; |
230 |
HWND h; |
231 |
char keymsg[2048], pwd[256]; |
232 |
char *uid = NULL; |
233 |
const char *keyid, *s; |
234 |
int type, expires=0, idx; |
235 |
int sig_class = 0; |
236 |
|
237 |
switch ( msg ) { |
238 |
case WM_INITDIALOG: |
239 |
if (lparam == 0) |
240 |
dlg_fatal_error (dlg, "Could not get dialog param."); |
241 |
SetWindowText (dlg, _("Key Signing")); |
242 |
key = (winpt_key_t) lparam; |
243 |
s = key->ctx->uids->uid; |
244 |
uid = utf8_to_wincp (s, strlen (s)); |
245 |
_snprintf (keymsg, sizeof keymsg -1, |
246 |
_("pub %d/%s created: %s expires: %s\n\n" |
247 |
"Primary key fingerprint: %s\n\n" |
248 |
"\t%s\n\n" |
249 |
"\nAre you really sure that you want to sign this key with YOUR key?\n"), |
250 |
key->ctx->subkeys->length, |
251 |
key->ctx->subkeys->keyid+8, |
252 |
get_key_created (key->ctx->subkeys->timestamp), |
253 |
get_expire_date (key->ctx), |
254 |
get_printable_fpr (key->ctx->subkeys->fpr), |
255 |
uid); |
256 |
free (uid); |
257 |
s = key->ctx->subkeys->keyid; |
258 |
if (do_fill_seckeylist (dlg, s)) { |
259 |
msg_box (dlg, _("No valid secret key found."), _("Key Signing"), MB_ERR); |
260 |
EndDialog (dlg, FALSE); |
261 |
} |
262 |
SetDlgItemText (dlg, IDC_KEYSIGN_INFOS, keymsg); |
263 |
SetDlgItemText (dlg, IDC_KEYSIGN_LOCAL, _("Sign local only (non exportable signature)")); |
264 |
SetDlgItemText (dlg, IDC_KEYSIGN_EXPSIG, _("Signature expires on")); |
265 |
SetDlgItemText (dlg, IDC_KEYSIGN_NREV, _("Sign non-revocably")); |
266 |
SetDlgItemText (dlg, IDC_KEYSIGN_ASKLEVEL, _("&Ask for certification level")); |
267 |
SetDlgItemText (dlg, IDC_KEYSIGN_PWDINF, _("Passphrase")); |
268 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
269 |
SetDlgItemText (dlg, IDC_KEYSIGN_SHOWIMG, _("&Show photo")); |
270 |
CheckDlgButton (dlg, IDC_KEYSIGN_LOCAL, BST_CHECKED); |
271 |
CheckDlgButton (dlg, IDC_KEYSIGN_EXPSIG, BST_UNCHECKED); |
272 |
CheckDlgButton (dlg, IDC_KEYSIGN_ASKLEVEL, BST_UNCHECKED); |
273 |
EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), FALSE); |
274 |
if (reg_prefs.expert == 0) |
275 |
ShowWindow (GetDlgItem (dlg, IDC_KEYSIGN_NREV), SW_HIDE); |
276 |
if (key->ext && key->ext->attrib.len > 0) |
277 |
EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_SHOWIMG), TRUE); |
278 |
if (!reg_prefs.gpg.ask_cert_level) |
279 |
EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_ASKLEVEL), FALSE); |
280 |
SetForegroundWindow (dlg); |
281 |
h = GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE); |
282 |
SetFocus (h); |
283 |
return FALSE; |
284 |
|
285 |
case WM_SYSCOMMAND: |
286 |
if( LOWORD( wparam ) == SC_CLOSE ) { |
287 |
SetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, "" ); |
288 |
EndDialog( dlg, TRUE ); |
289 |
} |
290 |
return FALSE; |
291 |
|
292 |
case WM_COMMAND: |
293 |
if (HIWORD( wparam ) == CBN_SELCHANGE) { |
294 |
do_check_protection (dlg); |
295 |
break; |
296 |
} |
297 |
if (HIWORD (wparam) == BN_CLICKED && LOWORD (wparam) == IDC_KEYSIGN_EXPSIG) { |
298 |
int enable = IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG); |
299 |
EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), enable? TRUE : FALSE); |
300 |
} |
301 |
|
302 |
switch (LOWORD (wparam)) { |
303 |
case IDOK: |
304 |
if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_ASKLEVEL)) |
305 |
sig_class = dialog_box_param (glob_hinst, |
306 |
(LPCSTR)IDD_WINPT_SIGCLASS, dlg, |
307 |
sig_class_dlg_proc, (LPARAM)NULL, |
308 |
_("Choose Signature Class"), |
309 |
IDS_WINPT_SIGCLASS); |
310 |
type = IsDlgButtonChecked (dlg, IDC_KEYSIGN_LOCAL); |
311 |
if (type) |
312 |
type = GPG_EDITKEY_LSIGN; |
313 |
else |
314 |
type = GPG_EDITKEY_SIGN; |
315 |
|
316 |
if (reg_prefs.expert && IsDlgButtonChecked (dlg, IDC_KEYSIGN_NREV)) { |
317 |
type = GPG_EDITKEY_NRSIGN; |
318 |
if (type == GPG_EDITKEY_LSIGN) |
319 |
type = GPG_EDITKEY_NRLSIGN; |
320 |
} |
321 |
if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG)) { |
322 |
expires = 1; |
323 |
DateTime_GetSystemtime (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), &st); |
324 |
sprintf (keymsg, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay); |
325 |
} |
326 |
|
327 |
/* XXX: check for --ask-cert-level and --ask-cert-expire in the gpg.conf |
328 |
if an advanced button is checked and offer to add it to the config |
329 |
file. */ |
330 |
|
331 |
GetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, pwd, DIM (pwd)-1); |
332 |
keyid = key->ctx->subkeys->keyid; |
333 |
if( !keyid ) { |
334 |
msg_box( dlg, _("Could not get Key ID from key."), _("Key Signing"), MB_ERR ); |
335 |
return TRUE; |
336 |
} |
337 |
ke = new GpgKeyEdit (keyid); |
338 |
if (!ke) |
339 |
BUG (NULL); |
340 |
ke->setPassphrase (pwd); |
341 |
idx = SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0); |
342 |
k = (gpgme_key_t)SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, |
343 |
CB_GETITEMDATA, (WPARAM)idx, 0); |
344 |
if (k) |
345 |
ke->setLocalUser (k); |
346 |
|
347 |
err = ke->signKey (type, sig_class, expires? keymsg : "0"); |
348 |
wipememory (pwd, sizeof (pwd)); |
349 |
if (err) { |
350 |
delete ke; |
351 |
msg_box (dlg, gpgme_strerror (err), _("Key Signing"), MB_ERR); |
352 |
return TRUE; |
353 |
} |
354 |
if (ke->getResult () != 0) |
355 |
msg_box (dlg, _("This key is already signed by your key"), |
356 |
_("Key Signing"), MB_INFO); |
357 |
else { |
358 |
status_box (dlg, _("Key successfully signed."), _("Key Signing")); |
359 |
key->update = 1; |
360 |
} |
361 |
delete ke; |
362 |
EndDialog (dlg, TRUE); |
363 |
return TRUE; |
364 |
|
365 |
case IDCANCEL: |
366 |
EndDialog (dlg, FALSE); |
367 |
return TRUE; |
368 |
|
369 |
case IDC_KEYSIGN_SHOWIMG: |
370 |
show_photo (key); |
371 |
return TRUE; |
372 |
} |
373 |
break; |
374 |
} |
375 |
|
376 |
return FALSE; |
377 |
} |
378 |
|