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

Annotation of /trunk/Src/wptKeysignDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 278 - (hide annotations)
Mon Jan 15 22:02:04 2007 UTC (18 years, 1 month ago) by twoaday
File size: 13444 byte(s)
See ChangeLog.


1 werner 36 /* wptKeysignDlg.cpp - Key signing dialog
2 twoaday 170 * Copyright (C) 2001-2006 Timo Schulz
3 werner 36 *
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 werner 42 #ifdef HAVE_CONFIG_H
17     #include <config.h>
18     #endif
19    
20 werner 36 #include <windows.h>
21     #include <commctrl.h>
22 twoaday 181 #include <time.h>
23 twoaday 229 #include <assert.h>
24 werner 36
25 werner 47 #include "resource.h"
26 werner 36 #include "wptGPG.h"
27     #include "wptNLS.h"
28     #include "wptW32API.h"
29     #include "wptVersion.h"
30     #include "wptTypes.h"
31     #include "wptErrors.h"
32     #include "wptCommonCtl.h"
33     #include "wptContext.h"
34     #include "wptDlgs.h"
35     #include "wptUTF8.h"
36     #include "wptRegistry.h"
37 werner 47 #include "wptKeylist.h"
38 werner 36 #include "wptKeyEdit.h"
39 twoaday 278 #include "StringBuffer.h"
40 werner 36
41    
42 twoaday 226 void key_unload_photo (HWND dlg);
43     int key_load_photo (HWND dlg, gpgme_key_t key, gpgme_validity_t *r_valid);
44     const char* get_photo_tmpname (HWND dlg);
45    
46 werner 36 /* Return a beautified printable fingerprint of @fpr. */
47     static const char*
48     get_printable_fpr (const char *fpr)
49     {
50 twoaday 225 static char pfpr[64];
51 werner 36 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 twoaday 225 const char*
67 werner 36 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 twoaday 225 if (winpt_get_seckey (key->subkeys->keyid, &k))
74     BUG (0);
75     _snprintf (buf, DIM (buf)-1-16, _("%d-bit %s key, ID 0x%s"),
76     key->subkeys->length,
77     get_key_pubalgo (key->subkeys->pubkey_algo),
78     key->subkeys->keyid+8);
79 werner 36 if (k.ext->gloflags.divert_to_card)
80     strcat (buf, " (Card)");
81     return buf;
82     }
83    
84    
85     /* Fill the secret key combo-box with all entries from the cache.
86     @dlg is the handle to the combo-box. @keyid show which key to skip.
87     Return value: 0 on success. */
88     static int
89     do_fill_seckeylist (HWND dlg, const char *keyid)
90     {
91     gpg_keycache_t sec;
92 twoaday 167 gpgme_key_t pk, defkey;
93 twoaday 278 const char *s;
94     char *uid;
95 twoaday 167 int i = 0, n = 0, curr_sel = 0;
96 werner 36
97     sec = keycache_get_ctx (0);
98 twoaday 167 gpg_keycache_get_default_key (sec, &defkey);
99 werner 36 gpg_keycache_rewind (sec);
100     while (!gpg_keycache_next_key (sec, 1, &pk)) {
101 twoaday 278 StringBuffer p;
102    
103 werner 36 if (!pk)
104     continue;
105     s = pk->subkeys->keyid;
106     if (!strcmp (s, keyid))
107     continue;
108     /* skip all ElGamal sign+encrypt keys */
109     if (pk->subkeys->pubkey_algo == GPGME_PK_ELG)
110     continue;
111     /* make sure the public key is okay not: revoked, expired or disabled. */
112     if (pk->expired ||pk->revoked || pk->disabled)
113     continue;
114     s = pk->uids->name;
115     if (!s)
116     continue;
117 twoaday 167 if (defkey && !strcmp (defkey->subkeys->keyid, pk->subkeys->keyid))
118     curr_sel = i;
119 twoaday 278
120 twoaday 187 uid = utf8_to_native (s);
121 twoaday 278 p = uid;
122     p = p + " (" + get_keyinfo (pk) + ")";
123 twoaday 167 SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
124 twoaday 278 CB_ADDSTRING, i, (LPARAM)(char *)p.getBuffer ());
125 twoaday 167 SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
126     CB_SETITEMDATA, i++, (LPARAM)(DWORD)pk);
127 twoaday 225 safe_free (uid);
128 werner 36 n++;
129     }
130 twoaday 167 SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
131     CB_SETCURSEL, (WPARAM)curr_sel, 0);
132 werner 36 if (!n)
133     return -1;
134     return 0;
135     }
136    
137    
138     /* Check if the selected key is protected and en- or disable the
139     passphrase control. */
140     static void
141     do_check_protection (HWND dlg)
142     {
143     gpgme_key_t key;
144     struct winpt_key_s k;
145 twoaday 212 int idx, protec;
146 werner 36
147 twoaday 212 idx = SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0);
148     key = (gpgme_key_t)SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
149     CB_GETITEMDATA, (WPARAM)idx, 0);
150 werner 36 if (key) {
151 twoaday 225 if (winpt_get_seckey (key->subkeys->keyid, &k))
152     BUG (0);
153 werner 36 protec = k.is_protected;
154     if (!protec)
155     protec = k.ext->gloflags.divert_to_card;
156     EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE), protec? TRUE : FALSE);
157     }
158     }
159    
160    
161 twoaday 226 /* Show the photographic ID of the key in a separate dialog window. */
162     BOOL CALLBACK
163     showphoto_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
164     {
165     static winpt_key_t key;
166 twoaday 229 char buf[200];
167 twoaday 226
168     switch (msg) {
169     case WM_INITDIALOG:
170     key = (winpt_key_t)lparam;
171     if (!key)
172     BUG (0);
173     key_load_photo (dlg, key->ctx, NULL);
174 twoaday 271 _snprintf (buf, DIM (buf)-1, _("Photo of %s"), key->uid);
175 twoaday 226 SetWindowText (dlg, buf);
176     SetForegroundWindow (dlg);
177     break;
178    
179     case WM_PAINT:
180     if (key->ext && key->ext->attrib.len > 0) {
181     POINT p;
182     p.x = p.y = 0;
183     PTD_jpg_show (GetDlgItem (dlg, IDC_SHOWPHOTO_IMG),
184     &p, get_photo_tmpname (dlg));
185     }
186     break;
187    
188     case WM_DESTROY:
189     key_unload_photo (dlg);
190     break;
191    
192     case WM_COMMAND:
193     switch (LOWORD (wparam)) {
194     case IDCANCEL:
195     case IDOK:
196     EndDialog (dlg, TRUE);
197     break;
198     }
199     break;
200     }
201    
202     return FALSE;
203     }
204    
205    
206 werner 36 /* Dialog box procedure to choose the signature class. */
207     BOOL CALLBACK
208     sig_class_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
209     {
210 twoaday 229 HWND parent;
211 twoaday 278 int sig_class = 0;
212 twoaday 170
213 werner 36 switch (msg) {
214     case WM_INITDIALOG:
215 twoaday 229 parent = (HWND)lparam;
216 werner 36 SetWindowText (dlg, _("Choose Signature Class"));
217 twoaday 225 SetDlgItemText (dlg, IDC_SIGCLASS_TITLEINF,
218     _("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\"."));
219     SetDlgItemText (dlg, IDC_SIGCLASS_CLASS0,
220     _("(0) I will not answer (default)"));
221     SetDlgItemText (dlg, IDC_SIGCLASS_CLASS1,
222     _("(1) I have not checked at all."));
223     SetDlgItemText (dlg, IDC_SIGCLASS_CLASS2,
224     _("(2) I have done causal checking."));
225     SetDlgItemText (dlg, IDC_SIGCLASS_CLASS3,
226 twoaday 248 _("(3) I have done very careful checking."));
227 werner 36 CheckDlgButton (dlg, IDC_SIGCLASS_CLASS0, BST_CHECKED);
228     SetForegroundWindow (dlg);
229 twoaday 229 center_window (dlg, parent? parent : NULL);
230 werner 36 return TRUE;
231    
232     case WM_COMMAND:
233 twoaday 170 switch (LOWORD (wparam)) {
234 werner 36 case IDOK:
235     if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS0))
236 twoaday 170 sig_class = 0;
237 werner 36 else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS1))
238 twoaday 170 sig_class = 1;
239 werner 36 else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS2))
240 twoaday 170 sig_class = 2;
241 werner 36 else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS3))
242 twoaday 170 sig_class = 3;
243 werner 36 else
244 twoaday 170 sig_class = 0;
245     EndDialog (dlg, sig_class);
246 werner 36 return TRUE;
247 twoaday 170
248     case IDCANCEL:
249     EndDialog (dlg, 0);
250     return TRUE;
251 werner 36 }
252     break;
253     }
254    
255     return FALSE;
256     }
257    
258    
259     /* Return the humand friendly expiration date of @key. */
260     static const char*
261     get_expire_date (gpgme_key_t key)
262     {
263     u32 u = key->subkeys->expires;
264     if (!u)
265     return _("never");
266     return get_key_expire_date (u);
267     }
268    
269 twoaday 225
270 twoaday 181 /* Check if the given system time @st points to today. */
271     static int
272     date_is_today (SYSTEMTIME *st)
273     {
274     time_t t;
275     struct tm *tm;
276    
277     t = time (NULL);
278     tm = localtime (&t);
279     if (st->wDay == tm->tm_mday &&
280     st->wYear == tm->tm_year+1900 &&
281     st->wMonth == tm->tm_mon+1)
282     return -1;
283     return 0;
284     }
285    
286    
287 twoaday 255 /* Display all needed information about the key @key in the dialog @dlg. */
288     static void
289     display_keyinfo (HWND dlg, winpt_key_t key)
290     {
291 twoaday 278 StringBuffer kinf;
292 twoaday 255 struct native_uid_s *u;
293 twoaday 278 char tmp[256];
294     int len=0;
295    
296 twoaday 271 _snprintf (tmp, DIM (tmp) -1,
297 twoaday 256 _("pub %d/0x%s created: %s expires: %s\n\n"
298 twoaday 255 "Primary key fingerprint: %s\n\n"),
299     key->ctx->subkeys->length,
300     key->ctx->subkeys->keyid+8,
301     get_key_created (key->ctx->subkeys->timestamp),
302     get_expire_date (key->ctx),
303 twoaday 278 get_printable_fpr (key->ctx->subkeys->fpr));
304     kinf = tmp;
305 twoaday 256 for (len=0, u = key->ext->uids; u; u = u->next) {
306     if (u->revoked)
307     continue;
308     if (len++ > 2)
309 twoaday 255 break;
310 twoaday 278 kinf = kinf + "\t\"" + u->uid + "\"\n";
311 twoaday 255 }
312 twoaday 278 kinf = kinf + _("\n\nAre you really sure that you want to sign this key with YOUR key?\n");
313     SetDlgItemText (dlg, IDC_KEYSIGN_INFOS, kinf.getBuffer ());
314 twoaday 255 }
315    
316    
317 twoaday 278 /* Perform the actual procedure to sign the key. */
318     static BOOL
319     on_click_ok (HWND dlg, winpt_key_t key)
320 werner 36 {
321 twoaday 278 gpgme_key_t k;
322 werner 36 gpgme_error_t err;
323     SYSTEMTIME st;
324 twoaday 278 GpgKeyEdit ke;
325     int sig_class = 0;
326 twoaday 255 char keymsg[64], *pwd;
327 werner 36 int type, expires=0, idx;
328 twoaday 278
329     if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_ASKLEVEL))
330     sig_class = dialog_box_param (glob_hinst,
331     (LPCSTR)IDD_WINPT_SIGCLASS, dlg,
332     sig_class_dlg_proc, (LPARAM)dlg,
333     _("Choose Signature Class"),
334     IDS_WINPT_SIGCLASS);
335     type = IsDlgButtonChecked (dlg, IDC_KEYSIGN_LOCAL);
336     if (type)
337     type = GPG_EDITKEY_LSIGN;
338     else
339     type = GPG_EDITKEY_SIGN;
340    
341    
342     if (reg_prefs.expert && IsDlgButtonChecked (dlg, IDC_KEYSIGN_NREV)) {
343     type = GPG_EDITKEY_NRSIGN;
344     if (type == GPG_EDITKEY_LSIGN)
345     type = GPG_EDITKEY_NRLSIGN;
346     }
347    
348     if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG)) {
349     DateTime_GetSystemtime (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), &st);
350     if (date_is_today (&st)) {
351     msg_box (dlg, _("You cannot select today as the expiration date."),
352     _("Key Signing"), MB_INFO);
353     return TRUE;
354     }
355     else
356     expires = 1;
357     _snprintf (keymsg, DIM (keymsg)-1, "%04d-%02d-%02d",
358     st.wYear, st.wMonth, st.wDay);
359     }
360    
361     /* XXX: check for --ask-cert-level and --ask-cert-expire in the gpg.conf
362     if an advanced button is checked and offer to add it to the config
363     file. */
364     GetDlgItemText_utf8 (dlg, IDC_KEYSIGN_PASSPHRASE, &pwd);
365 werner 36
366 twoaday 278 assert (key->ctx->subkeys->keyid != NULL); /* should be always true. */
367     ke.setKeyID (key->ctx->subkeys->keyid);
368     ke.setPassphrase (pwd);
369     idx = SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0);
370     k = (gpgme_key_t)SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
371     CB_GETITEMDATA, (WPARAM)idx, 0);
372     if (k != NULL)
373     ke.setLocalUser (k);
374    
375     err = ke.signKey (type, sig_class, expires? keymsg : "0");
376     sfree_if_alloc (pwd);
377     if (err) {
378     msg_box (dlg, gpgme_strerror (err), _("Key Signing"), MB_ERR);
379     return TRUE;
380     }
381     else if (ke.getResult () != 0)
382     msg_box (dlg, _("This key is already signed by your key"),
383     _("Key Signing"), MB_INFO);
384     else {
385     status_box (dlg, _("Key successfully signed."), _("Key Signing"));
386     key->update = 1;
387     }
388     EndDialog (dlg, TRUE);
389     return TRUE;
390     }
391    
392    
393     /* Dialog box procedure to sign a key. */
394     BOOL CALLBACK
395     keysign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
396     {
397     static winpt_key_t key;
398     const char *s;
399    
400 twoaday 177 switch (msg) {
401 werner 36 case WM_INITDIALOG:
402     key = (winpt_key_t) lparam;
403 twoaday 225 if (!key)
404 twoaday 255 BUG (0);
405 werner 36 s = key->ctx->subkeys->keyid;
406     if (do_fill_seckeylist (dlg, s)) {
407     msg_box (dlg, _("No valid secret key found."), _("Key Signing"), MB_ERR);
408     EndDialog (dlg, FALSE);
409 twoaday 225 return TRUE;
410 werner 36 }
411 twoaday 176 do_check_protection (dlg);
412 twoaday 255 display_keyinfo (dlg, key);
413 werner 36 SetDlgItemText (dlg, IDC_KEYSIGN_LOCAL, _("Sign local only (non exportable signature)"));
414     SetDlgItemText (dlg, IDC_KEYSIGN_EXPSIG, _("Signature expires on"));
415     SetDlgItemText (dlg, IDC_KEYSIGN_NREV, _("Sign non-revocably"));
416     SetDlgItemText (dlg, IDC_KEYSIGN_ASKLEVEL, _("&Ask for certification level"));
417     SetDlgItemText (dlg, IDC_KEYSIGN_PWDINF, _("Passphrase"));
418 twoaday 88 SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
419     SetDlgItemText (dlg, IDC_KEYSIGN_SHOWIMG, _("&Show photo"));
420 twoaday 178 SetDlgItemText (dlg, IDC_KEYSIGN_HIDE, _("&Hide Typing"));
421 twoaday 225 SetWindowText (dlg, _("Key Signing"));
422 werner 36 CheckDlgButton (dlg, IDC_KEYSIGN_LOCAL, BST_CHECKED);
423     CheckDlgButton (dlg, IDC_KEYSIGN_EXPSIG, BST_UNCHECKED);
424     CheckDlgButton (dlg, IDC_KEYSIGN_ASKLEVEL, BST_UNCHECKED);
425     EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), FALSE);
426     if (reg_prefs.expert == 0)
427     ShowWindow (GetDlgItem (dlg, IDC_KEYSIGN_NREV), SW_HIDE);
428 twoaday 255 if (key->ext && key->ext->attrib.len == 0)
429     ShowWindow (GetDlgItem (dlg, IDC_KEYSIGN_SHOWIMG), SW_HIDE);
430 twoaday 41 if (!reg_prefs.gpg.ask_cert_level)
431     EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_ASKLEVEL), FALSE);
432 twoaday 181 if (!reg_prefs.gpg.ask_cert_expire)
433     EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPSIG), FALSE);
434 twoaday 177 CheckDlgButton (dlg, IDC_KEYSIGN_HIDE, BST_CHECKED);
435 werner 36 SetForegroundWindow (dlg);
436 twoaday 229 SetFocus (GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE));
437 werner 36 return FALSE;
438    
439     case WM_COMMAND:
440 twoaday 176 if (HIWORD (wparam) == CBN_SELCHANGE) {
441 werner 36 do_check_protection (dlg);
442     break;
443     }
444 twoaday 177 if (HIWORD (wparam) == BN_CLICKED &&
445     LOWORD (wparam) == IDC_KEYSIGN_EXPSIG) {
446 werner 36 int enable = IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG);
447 twoaday 225
448 werner 36 EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), enable? TRUE : FALSE);
449     }
450 twoaday 177 if (HIWORD (wparam) == BN_CLICKED &&
451     LOWORD (wparam) == IDC_KEYSIGN_HIDE) {
452     HWND hwnd = GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE);
453     int hide = IsDlgButtonChecked (dlg, IDC_KEYSIGN_HIDE);
454 twoaday 225
455 twoaday 177 SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
456     SetFocus (hwnd);
457     }
458 werner 36
459 twoaday 170 switch (LOWORD (wparam)) {
460 werner 36 case IDOK:
461 twoaday 278 return on_click_ok (dlg, key);
462 werner 36
463     case IDCANCEL:
464     EndDialog (dlg, FALSE);
465     return TRUE;
466    
467     case IDC_KEYSIGN_SHOWIMG:
468 twoaday 226 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SHOWPHOTO,
469     dlg, showphoto_dlg_proc, (LPARAM)key);
470 werner 36 return TRUE;
471     }
472     break;
473     }
474    
475     return FALSE;
476     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26