/[winpt]/trunk/Src/wptKeysignDlg.cpp
ViewVC logotype

Contents of /trunk/Src/wptKeysignDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 225 - (show annotations)
Tue Jun 6 13:37:59 2006 UTC (18 years, 8 months ago) by twoaday
File size: 12445 byte(s)


1 /* wptKeysignDlg.cpp - Key signing dialog
2 * Copyright (C) 2001-2006 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 #include <time.h>
27
28 #include "resource.h"
29 #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 #include "wptKeylist.h"
41 #include "wptKeyEdit.h"
42
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 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 if (winpt_get_seckey (key->subkeys->keyid, &k))
72 BUG (0);
73 _snprintf (buf, DIM (buf)-1-16, _("%d-bit %s key, ID 0x%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, defkey;
91 const char *s, *inf;
92 char *uid, *p;
93 int i = 0, n = 0, curr_sel = 0;
94 int len = 0;
95
96 sec = keycache_get_ctx (0);
97 gpg_keycache_get_default_key (sec, &defkey);
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 if (defkey && !strcmp (defkey->subkeys->keyid, pk->subkeys->keyid))
115 curr_sel = i;
116 uid = utf8_to_native (s);
117 inf = get_keyinfo (pk);
118 len = strlen (uid) + strlen (inf) + 8;
119 p = new char[len+1];
120 if (!p)
121 BUG (NULL);
122 _snprintf (p, len, "%s (%s)", uid, inf);
123 SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
124 CB_ADDSTRING, i, (LPARAM)(char *)p);
125 SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
126 CB_SETITEMDATA, i++, (LPARAM)(DWORD)pk);
127 free_if_alloc (p);
128 safe_free (uid);
129 n++;
130 }
131 SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
132 CB_SETCURSEL, (WPARAM)curr_sel, 0);
133 if (!n)
134 return -1;
135 return 0;
136 }
137
138
139 /* Check if the selected key is protected and en- or disable the
140 passphrase control. */
141 static void
142 do_check_protection (HWND dlg)
143 {
144 gpgme_key_t key;
145 struct winpt_key_s k;
146 int idx, protec;
147
148 idx = SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0);
149 key = (gpgme_key_t)SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
150 CB_GETITEMDATA, (WPARAM)idx, 0);
151 if (key) {
152 if (winpt_get_seckey (key->subkeys->keyid, &k))
153 BUG (0);
154 protec = k.is_protected;
155 if (!protec)
156 protec = k.ext->gloflags.divert_to_card;
157 EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE), protec? TRUE : FALSE);
158 }
159 }
160
161
162 /* Dialog box procedure to choose the signature class. */
163 BOOL CALLBACK
164 sig_class_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
165 {
166 int sig_class = 0;
167
168 switch (msg) {
169 case WM_INITDIALOG:
170 SetWindowText (dlg, _("Choose Signature Class"));
171 SetDlgItemText (dlg, IDC_SIGCLASS_TITLEINF,
172 _("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\"."));
173 SetDlgItemText (dlg, IDC_SIGCLASS_CLASS0,
174 _("(0) I will not answer (default)"));
175 SetDlgItemText (dlg, IDC_SIGCLASS_CLASS1,
176 _("(1) I have not checked at all."));
177 SetDlgItemText (dlg, IDC_SIGCLASS_CLASS2,
178 _("(2) I have done causal checking."));
179 SetDlgItemText (dlg, IDC_SIGCLASS_CLASS3,
180 _("(3) I have done very careful checkings."));
181 CheckDlgButton (dlg, IDC_SIGCLASS_CLASS0, BST_CHECKED);
182 SetForegroundWindow (dlg);
183 center_window (dlg, NULL);
184 return TRUE;
185
186 case WM_COMMAND:
187 switch (LOWORD (wparam)) {
188 case IDOK:
189 if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS0))
190 sig_class = 0;
191 else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS1))
192 sig_class = 1;
193 else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS2))
194 sig_class = 2;
195 else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS3))
196 sig_class = 3;
197 else
198 sig_class = 0;
199 EndDialog (dlg, sig_class);
200 return TRUE;
201
202 case IDCANCEL:
203 EndDialog (dlg, 0);
204 return TRUE;
205 }
206 break;
207 }
208
209 return FALSE;
210 }
211
212
213 /* Return the humand friendly expiration date of @key. */
214 static const char*
215 get_expire_date (gpgme_key_t key)
216 {
217 u32 u = key->subkeys->expires;
218 if (!u)
219 return _("never");
220 return get_key_expire_date (u);
221 }
222
223
224 /* Display photo of key @key in a separate window. */
225 static void
226 show_photo (winpt_key_t key)
227 {
228 /* XXX: fill it with life. */
229 }
230
231
232 /* Check if the given system time @st points to today. */
233 static int
234 date_is_today (SYSTEMTIME *st)
235 {
236 time_t t;
237 struct tm *tm;
238
239 t = time (NULL);
240 tm = localtime (&t);
241 if (st->wDay == tm->tm_mday &&
242 st->wYear == tm->tm_year+1900 &&
243 st->wMonth == tm->tm_mon+1)
244 return -1;
245 return 0;
246 }
247
248
249 /* Dialog box procedure to sign a key. */
250 BOOL CALLBACK
251 keysign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
252 {
253 static winpt_key_t key;
254 GpgKeyEdit *ke;
255 gpgme_error_t err;
256 gpgme_key_t k;
257 SYSTEMTIME st;
258 HWND h;
259 char keymsg[2048], pwd[256];
260 const char *keyid, *s;
261 int type, expires=0, idx;
262 int sig_class = 0;
263
264 switch (msg) {
265 case WM_INITDIALOG:
266 key = (winpt_key_t) lparam;
267 if (!key)
268 BUG (0);
269 _snprintf (keymsg, sizeof keymsg -1,
270 _("pub %d/%s created: %s expires: %s\n\n"
271 "Primary key fingerprint: %s\n\n"
272 "\t%s\n\n"
273 "\nAre you really sure that you want to sign this key with YOUR key?\n"),
274 key->ctx->subkeys->length,
275 key->ctx->subkeys->keyid+8,
276 get_key_created (key->ctx->subkeys->timestamp),
277 get_expire_date (key->ctx),
278 get_printable_fpr (key->ctx->subkeys->fpr),
279 key->ext->uids->uid);
280 s = key->ctx->subkeys->keyid;
281 if (do_fill_seckeylist (dlg, s)) {
282 msg_box (dlg, _("No valid secret key found."), _("Key Signing"), MB_ERR);
283 EndDialog (dlg, FALSE);
284 return TRUE;
285 }
286 do_check_protection (dlg);
287 SetDlgItemText (dlg, IDC_KEYSIGN_INFOS, keymsg);
288 SetDlgItemText (dlg, IDC_KEYSIGN_LOCAL, _("Sign local only (non exportable signature)"));
289 SetDlgItemText (dlg, IDC_KEYSIGN_EXPSIG, _("Signature expires on"));
290 SetDlgItemText (dlg, IDC_KEYSIGN_NREV, _("Sign non-revocably"));
291 SetDlgItemText (dlg, IDC_KEYSIGN_ASKLEVEL, _("&Ask for certification level"));
292 SetDlgItemText (dlg, IDC_KEYSIGN_PWDINF, _("Passphrase"));
293 SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
294 SetDlgItemText (dlg, IDC_KEYSIGN_SHOWIMG, _("&Show photo"));
295 SetDlgItemText (dlg, IDC_KEYSIGN_HIDE, _("&Hide Typing"));
296 SetWindowText (dlg, _("Key Signing"));
297 CheckDlgButton (dlg, IDC_KEYSIGN_LOCAL, BST_CHECKED);
298 CheckDlgButton (dlg, IDC_KEYSIGN_EXPSIG, BST_UNCHECKED);
299 CheckDlgButton (dlg, IDC_KEYSIGN_ASKLEVEL, BST_UNCHECKED);
300 EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), FALSE);
301 if (reg_prefs.expert == 0)
302 ShowWindow (GetDlgItem (dlg, IDC_KEYSIGN_NREV), SW_HIDE);
303 if (key->ext && key->ext->attrib.len > 0)
304 EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_SHOWIMG), TRUE);
305 if (!reg_prefs.gpg.ask_cert_level)
306 EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_ASKLEVEL), FALSE);
307 if (!reg_prefs.gpg.ask_cert_expire)
308 EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPSIG), FALSE);
309 CheckDlgButton (dlg, IDC_KEYSIGN_HIDE, BST_CHECKED);
310 SetForegroundWindow (dlg);
311 h = GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE);
312 SetFocus (h);
313 return FALSE;
314
315 case WM_COMMAND:
316 if (HIWORD (wparam) == CBN_SELCHANGE) {
317 do_check_protection (dlg);
318 break;
319 }
320 if (HIWORD (wparam) == BN_CLICKED &&
321 LOWORD (wparam) == IDC_KEYSIGN_EXPSIG) {
322 int enable = IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG);
323
324 EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), enable? TRUE : FALSE);
325 }
326 if (HIWORD (wparam) == BN_CLICKED &&
327 LOWORD (wparam) == IDC_KEYSIGN_HIDE) {
328 HWND hwnd = GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE);
329 int hide = IsDlgButtonChecked (dlg, IDC_KEYSIGN_HIDE);
330
331 SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
332 SetFocus (hwnd);
333 }
334
335 switch (LOWORD (wparam)) {
336 case IDOK:
337 if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_ASKLEVEL))
338 sig_class = dialog_box_param (glob_hinst,
339 (LPCSTR)IDD_WINPT_SIGCLASS, dlg,
340 sig_class_dlg_proc, (LPARAM)NULL,
341 _("Choose Signature Class"),
342 IDS_WINPT_SIGCLASS);
343 type = IsDlgButtonChecked (dlg, IDC_KEYSIGN_LOCAL);
344 if (type)
345 type = GPG_EDITKEY_LSIGN;
346 else
347 type = GPG_EDITKEY_SIGN;
348
349 if (reg_prefs.expert && IsDlgButtonChecked (dlg, IDC_KEYSIGN_NREV)) {
350 type = GPG_EDITKEY_NRSIGN;
351 if (type == GPG_EDITKEY_LSIGN)
352 type = GPG_EDITKEY_NRLSIGN;
353 }
354 if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG)) {
355 DateTime_GetSystemtime (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), &st);
356 if (date_is_today (&st)) {
357 msg_box (dlg, _("You cannot select today as the expiration date."),
358 _("Key Signing"), MB_INFO);
359 return TRUE;
360 }
361 else
362 expires = 1;
363 sprintf (keymsg, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay);
364 }
365
366 /* XXX: check for --ask-cert-level and --ask-cert-expire in the gpg.conf
367 if an advanced button is checked and offer to add it to the config
368 file. */
369
370 GetDlgItemText (dlg, IDC_KEYSIGN_PASSPHRASE, pwd, DIM (pwd)-1);
371 keyid = key->ctx->subkeys->keyid;
372 if (!keyid) {
373 wipememory (pwd, sizeof (pwd));
374 msg_box (dlg, _("Could not get Key ID from key."), _("Key Signing"), MB_ERR);
375 return TRUE;
376 }
377 ke = new GpgKeyEdit (keyid);
378 if (!ke)
379 BUG (NULL);
380 ke->setPassphrase (pwd);
381 idx = SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0);
382 k = (gpgme_key_t)SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
383 CB_GETITEMDATA, (WPARAM)idx, 0);
384 if (k)
385 ke->setLocalUser (k);
386
387 err = ke->signKey (type, sig_class, expires? keymsg : "0");
388 wipememory (pwd, sizeof (pwd));
389 if (err) {
390 delete ke;
391 msg_box (dlg, gpgme_strerror (err), _("Key Signing"), MB_ERR);
392 return TRUE;
393 }
394 if (ke->getResult () != 0) {
395 msg_box (dlg, _("This key is already signed by your key"),
396 _("Key Signing"), MB_INFO);
397 delete ke;
398 return TRUE;
399 }
400 else {
401 status_box (dlg, _("Key successfully signed."), _("Key Signing"));
402 key->update = 1;
403 }
404 delete ke;
405 EndDialog (dlg, TRUE);
406 return TRUE;
407
408 case IDCANCEL:
409 EndDialog (dlg, FALSE);
410 return TRUE;
411
412 case IDC_KEYSIGN_SHOWIMG:
413 show_photo (key);
414 return TRUE;
415 }
416 break;
417 }
418
419 return FALSE;
420 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26