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

Contents of /trunk/Src/wptKeysignDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 204 - (show annotations)
Wed Apr 26 06:37:23 2006 UTC (18 years, 10 months ago) by twoaday
File size: 12559 byte(s)
2006-04-25  Timo Schulz  <ts@g10code.de>
                                                                                
        * wptFileManagerDlg.cpp (file_manager_dlg_proc): Handle IDCANCEL.
        * wptMDSumDlg.cpp (hash_selected_files): New.
        (mdsum_dlg_proc): Cleanups.
        * wptKeysignDlg.cpp (keysign_dlg_proc): Fix segv.
                                                                                
2006-04-23  Timo Schulz  <ts@g10code.de>
                                                                                
        * wptW32API.cpp (SetDlgItemText_utf8): New.
        (SetWindowText_utf8): New.
        (GetDlgItemText_utf8): New.
        * wptListView.cpp (listview_add_sub_item): Internal
        utf8 support.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26