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