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