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