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