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