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

Annotation of /trunk/Src/wptPassphraseCB.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 204 - (hide annotations)
Wed Apr 26 06:37:23 2006 UTC (18 years, 10 months ago) by twoaday
File size: 13798 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 werner 36 /* wptPassphraseCB.cpp - GPGME Passphrase Callback
2 twoaday 204 * Copyright (C) 2001, 2002, 2003, 2005, 2006 Timo Schulz
3 werner 36 * Copyright (C) 2005 g10 Code GmbH
4     *
5     * This file is part of WinPT.
6     *
7     * WinPT is free software; you can redistribute it and/or
8     * modify it under the terms of the GNU General Public License
9     * as published by the Free Software Foundation; either version 2
10     * of the License, or (at your option) any later version.
11     *
12     * WinPT is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15     * General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with WinPT; if not, write to the Free Software Foundation,
19     * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20     */
21    
22     #ifdef HAVE_CONFIG_H
23     #include <config.h>
24     #endif
25    
26     #include <windows.h>
27     #include <ctype.h>
28    
29 werner 47 #include "resource.h"
30 werner 36 #include "wptNLS.h"
31     #include "wptW32API.h"
32     #include "wptVersion.h"
33     #include "wptGPG.h"
34     #include "wptCommonCtl.h"
35     #include "wptContext.h"
36     #include "wptDlgs.h"
37     #include "wptErrors.h"
38     #include "wptTypes.h"
39 werner 48 #include "wptKeylist.h"
40 werner 36 #include "wptAgent.h"
41     #include "wptRegistry.h"
42    
43     const char* get_symkey_algo (int algo);
44    
45 twoaday 181 #define item_ctrl_id(cmd) \
46 werner 36 ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_PWD : IDC_DECRYPT_SIGN_PWD)
47    
48     #define item_ctrl_id2(cmd) \
49     ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_HIDE : IDC_DECRYPT_SIGN_HIDE)
50    
51    
52 twoaday 204 void ListBox_AddString_utf8 (HWND lb, const char *txt);
53    
54 werner 36 /* Overwrite passphrase and free memory. */
55     static void
56     burn_passphrase (char **pwd)
57     {
58     char *pass = *pwd;
59     wipememory (pass, strlen (pass));
60     delete []pass;
61     *pwd = NULL;
62     }
63    
64    
65     /* Dialog procedure for the passphrase callback. */
66     static BOOL CALLBACK
67     passphrase_callback_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
68     {
69 twoaday 77 static passphrase_cb_s *c;
70     gpgme_decrypt_result_t res=NULL;
71     gpgme_sign_result_t res_sig=NULL;
72 werner 36 gpgme_key_t key;
73 twoaday 69 gpgme_recipient_t recip=NULL, r;
74     void *item;
75 werner 36 const char *id;
76     char *info;
77     int n;
78    
79     switch (msg) {
80 twoaday 181 case WM_ACTIVATE:
81     safe_edit_control_init (dlg, item_ctrl_id (c->gpg_cmd));
82     break;
83    
84     case WM_DESTROY:
85     safe_edit_control_free (dlg, item_ctrl_id (c->gpg_cmd));
86     break;
87    
88 werner 36 case WM_INITDIALOG:
89     c = (passphrase_cb_s *)lparam;
90     if (!c)
91     BUG (0);
92 twoaday 105 SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
93 twoaday 85 SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide Typing"));
94 werner 36 SetWindowText (dlg, c->title);
95     if (c->gpg_cmd == GPG_CMD_DECRYPT) {
96     SetDlgItemText (dlg, IDC_DECRYPT_LISTINF,
97     _("Encrypted with the following public key(s)"));
98     CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED);
99     }
100 twoaday 95 else if (c->gpg_cmd == GPG_CMD_SIGN) {
101     SetDlgItemText (dlg, IDC_DECRYPT_SIGN_HIDE, _("&Hide Typing"));
102 werner 36 CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED);
103 twoaday 95 }
104 twoaday 179 /* Because it depends on the order the keys are stored in the
105     keyring whether res->recipients is complete or not, we also
106     support that the recipients were externally extracted and then
107     we use this list. */
108 werner 36 if (c->recipients)
109     recip = c->recipients; /* recipients were already extracted. */
110     else {
111     res = gpgme_op_decrypt_result (c->gpg);
112     if (res && res->recipients)
113     recip = res->recipients;
114     }
115     if (recip != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) {
116 twoaday 179 for (r = recip; r; r = r->next) {
117 werner 36 get_pubkey (r->keyid, &key);
118     if (key) {
119 twoaday 160 gpgme_user_id_t u = key->uids;
120    
121     id = u->name;
122 werner 36 if (!id)
123     id = _("Invalid User ID");
124 twoaday 204 n = 32+strlen (id)+1+4+strlen (r->keyid)+1;
125 twoaday 160 if (u->email)
126     n += strlen (u->email)+1;
127     info = new char [n+1];
128 werner 36 if (!info)
129     BUG (NULL);
130 twoaday 160 if (!u->email || strlen (u->email) < 1)
131 twoaday 204 sprintf (info, "%s (%s, 0x%s)", id,
132 twoaday 160 get_key_pubalgo (r->pubkey_algo), r->keyid+8);
133     else
134 twoaday 204 sprintf (info, "%s <%s> (%s, 0x%s)", id, u->email,
135 twoaday 160 get_key_pubalgo (r->pubkey_algo), r->keyid+8);
136 werner 36 }
137     else {
138     info = new char [32 + strlen (r->keyid)+1 + 4];
139     if (!info)
140     BUG (NULL);
141     sprintf (info, _("Unknown key ID (%s, 0x%s)"),
142     get_key_pubalgo (r->pubkey_algo), r->keyid+8);
143     }
144 twoaday 204 ListBox_AddString_utf8 (GetDlgItem (dlg, IDC_DECRYPT_LIST), info);
145 werner 36 free_if_alloc (info);
146     }
147     }
148     else if (c->gpg_cmd == GPG_CMD_DECRYPT)
149     EnableWindow (GetDlgItem (dlg, IDC_DECRYPT_LIST), FALSE);
150     SetDlgItemText (dlg, c->gpg_cmd == GPG_CMD_DECRYPT?
151     IDC_DECRYPT_PWDINFO : IDC_DECRYPT_SIGN_PWDINFO,
152     c->bad_pwd? _("Bad passphrase; Enter passphrase again") :
153     _("Please enter your passphrase"));
154     if (c->gpg_cmd == GPG_CMD_DECRYPT) {
155     SetFocus (GetDlgItem (dlg, IDC_DECRYPT_PWD));
156     if (res && !res->recipients) {
157     const char *s = _("Symmetric encryption.\n"
158     "%s encrypted data.");
159     const char *alg = get_symkey_algo (c->sym.sym_algo);
160     info = new char[strlen (s) + strlen (alg) + 2];
161     if (!info)
162     BUG (NULL);
163     sprintf (info, s, alg);
164     SetDlgItemText (dlg, IDC_DECRYPT_MSG, info);
165     free_if_alloc (info);
166     }
167     else
168 twoaday 204 SetDlgItemText_utf8 (dlg, IDC_DECRYPT_MSG, c->info);
169 werner 36 }
170     else {
171 twoaday 204 SetFocus (GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD));
172     SetDlgItemText_utf8 (dlg, IDC_DECRYPT_SIGN_MSG, c->info);
173 werner 36 }
174     center_window (dlg, NULL);
175     SetForegroundWindow (dlg);
176     return FALSE;
177    
178     case WM_SYSCOMMAND:
179     if (LOWORD (wparam) == SC_CLOSE) {
180     SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), "");
181     c->cancel = 1;
182     EndDialog (dlg, TRUE);
183     }
184     break;
185    
186     case WM_COMMAND:
187     switch (HIWORD (wparam)) {
188     case BN_CLICKED:
189     if (LOWORD (wparam) == IDC_DECRYPT_HIDE
190     || LOWORD (wparam) == IDC_DECRYPT_SIGN_HIDE) {
191     HWND hwnd;
192     int hide = IsDlgButtonChecked (dlg, item_ctrl_id2 (c->gpg_cmd));
193     hwnd = GetDlgItem (dlg, item_ctrl_id (c->gpg_cmd));
194     SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
195     SetFocus (hwnd);
196     }
197     }
198    
199     switch (LOWORD (wparam)) {
200     case IDOK:
201     /* XXX: the item is even cached when the passphrase is not
202     correct, which means that the user needs to delete all
203     cached entries to continue. */
204     if (c->pwd)
205     burn_passphrase (&c->pwd);
206     n = item_get_text_length (dlg, item_ctrl_id (c->gpg_cmd));
207     if (!n) {
208     c->pwd = new char[2];
209     if (!c->pwd)
210     BUG (NULL);
211     strcpy (c->pwd, "");
212     }
213     else {
214     c->pwd = new char[n+2];
215     if (!c->pwd)
216     BUG (NULL);
217 twoaday 181 SafeGetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd),
218     c->pwd, n+1);
219 werner 36 }
220     res = gpgme_op_decrypt_result (c->gpg);
221     if (!res)
222 twoaday 77 res_sig = gpgme_op_sign_result (c->gpg);
223     if (reg_prefs.cache_time > 0 && !c->is_card &&
224     (res || res_sig)) {
225     if (agent_get_cache (c->keyid, &item))
226 werner 36 agent_unlock_cache_entry (&item);
227 twoaday 77 else
228 werner 36 agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time);
229     }
230     c->cancel = 0;
231     EndDialog (dlg, TRUE);
232     return TRUE;
233    
234     case IDCANCEL:
235     SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), "");
236     c->cancel = 1;
237     EndDialog (dlg, FALSE);
238     return FALSE;
239     }
240     break;
241     }
242    
243     return FALSE;
244     }
245    
246    
247     /* Extract the main keyid from @pass_info.
248     Return value: long main keyid or NULL for an error. */
249     static const char*
250     parse_gpg_keyid (const char *pass_info)
251     {
252     static char keyid[16+1];
253    
254     /* XXX: check for leading alpha-chars? */
255 twoaday 77 if (strlen (pass_info) < 16) {
256     log_debug ("parse_gpg_keyid: error '%s'\r\n", pass_info);
257 werner 36 return NULL;
258 twoaday 77 }
259 werner 36 /* the format of the desc buffer looks like this:
260     request_keyid[16] main_keyid[16] keytype[1] keylength[4]
261     we use the main keyid to use only one cache entry. */
262     strncpy (keyid, pass_info+17, 16);
263     keyid[16] = 0;
264     return keyid;
265     }
266    
267    
268     /* Parse the information in @uid_hint and @pass_info to generate
269     a input message for the user in @desc. */
270     static int
271     parse_gpg_description (const char *uid_hint, const char *pass_info,
272     char *desc, int size)
273     {
274     gpgme_pubkey_algo_t algo;
275     char usedkey[16+1];
276     char mainkey[16+1];
277 twoaday 204 char *p;
278 werner 36 int n=0;
279    
280 twoaday 69 algo = (gpgme_pubkey_algo_t)0;
281 werner 36 /* Each uid_hint contains a long key-ID so it is at least 16 bytes. */
282     if (strlen (uid_hint) < 17) {
283     *desc = 0;
284 twoaday 77 log_debug ("parse_gpg_description: error '%s'\r\n", uid_hint);
285 werner 36 return -1;
286     }
287    
288     while (p = strsep ((char**)&pass_info, " ")) {
289     switch (n++) {
290     case 0: strncpy (mainkey, p, 16); mainkey[16] = 0; break;
291     case 1: strncpy (usedkey, p, 16); usedkey[16] = 0; break;
292     case 2: algo = (gpgme_pubkey_algo_t)atol (p); break;
293     }
294     }
295     uid_hint += 16; /* skip keyid */
296     uid_hint += 1; /* space */
297    
298     if (strcmp (usedkey, mainkey))
299     _snprintf (desc, size-1,
300     _("You need a passphrase to unlock the secret key for\n"
301     "user: \"%s\"\n"
302     "%s key, ID %s (main key ID %s)\n"),
303 twoaday 204 uid_hint, get_key_pubalgo (algo), usedkey+8, mainkey+8);
304 werner 36 else if (!strcmp (usedkey, mainkey))
305     _snprintf (desc, size-1,
306     _("You need a passphrase to unlock the secret key for\n"
307     "user: \"%s\"\n"
308     "%s key, ID %s\n"),
309 twoaday 204 uid_hint, get_key_pubalgo (algo), usedkey+8);
310 werner 36 return 0;
311     }
312    
313    
314     /* Extract the serial number from the card ID @id and return it. */
315     const char*
316     extract_serial_no (const char *id)
317     {
318     static char buf[8];
319     char *p;
320    
321     p = strchr (id, '/');
322 twoaday 77 if (!p) {
323     log_debug ("extract_serial_no: error '%s'\r\n", id);
324 werner 36 return NULL;
325 twoaday 77 }
326 werner 36 strncpy (buf, id+(p-id)-6, 6);
327     return buf;
328     }
329    
330    
331     /* Passphrase callback with the ability to support caching. */
332     gpgme_error_t
333     passphrase_cb (void *hook, const char *uid_hint,
334     const char *passphrase_info,
335     int prev_was_bad, int fd)
336     {
337     passphrase_cb_s *c = (passphrase_cb_s*)hook;
338     HANDLE hd = (HANDLE)fd;
339     void *item;
340 twoaday 69 const char *keyid=NULL, *pass;
341 werner 36 DWORD n;
342 twoaday 77 int rc = 0;
343 werner 36
344 twoaday 77 if (!c) {
345     log_debug ("passphrase_cb: error '!c'\r\n");
346 werner 36 return gpg_error (GPG_ERR_INV_ARG);
347 twoaday 77 }
348 werner 36 c->bad_pwd = prev_was_bad? 1 : 0;
349     if (prev_was_bad && !c->cancel) {
350     if (c->pwd)
351     burn_passphrase (&c->pwd);
352     agent_del_cache (c->keyid);
353     c->pwd_init = 1;
354     }
355    
356     if (passphrase_info) {
357     if (strlen (passphrase_info) < 16 &&
358     !strstr (passphrase_info, "OPENPGP")) {
359     /* assume symetric encryption. */
360 twoaday 69 int pos=2;
361 werner 36 c->sym.sym_algo = atoi (passphrase_info);
362     if (c->sym.sym_algo > 9)
363 twoaday 69 pos++;
364 werner 36 /* XXX: be more strict. */
365 twoaday 69 c->sym.s2k_mode = atoi (passphrase_info+pos);
366     c->sym.s2k_hash = atoi (passphrase_info+pos+2);
367 werner 36 }
368    
369     keyid = parse_gpg_keyid (passphrase_info);
370 twoaday 77 pass = agent_get_cache (keyid+8, &item);
371 werner 36 if (pass) {
372     agent_unlock_cache_entry (&item);
373     c->pwd_init = 0;
374     if (!WriteFile (hd, pass, strlen (pass), &n, NULL))
375     log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
376     if (!WriteFile (hd, "\n", 1, &n, NULL))
377     log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
378     return 0;
379     }
380     }
381    
382     if (c->pwd_init) {
383     if (keyid && strlen (keyid) == 16)
384     strcpy (c->keyid, keyid+8);
385    
386     /* if @passphrase_info contains 'OPENPGP' we assume a smart card
387     has been used. */
388     if (strstr (passphrase_info, "OPENPGP")) {
389     const char *s=passphrase_info;
390     while (s && *s && *s != 'D')
391     s++;
392     _snprintf (c->info, sizeof c->info-1,
393     _("Please enter the PIN to unlock your secret card key\n"
394     "Card: %s"), extract_serial_no (s));
395     c->is_card = 1;
396     }
397     else if (uid_hint)
398     parse_gpg_description (uid_hint, passphrase_info,
399 twoaday 181 c->info, sizeof (c->info) - 1);
400 werner 36 if (c->gpg_cmd == GPG_CMD_DECRYPT) {
401     rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT,
402     (HWND)c->hwnd, passphrase_callback_proc,
403     (LPARAM)c);
404     }
405     else if (c->gpg_cmd == GPG_CMD_SIGN) {
406     rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT_SIGN,
407     (HWND)c->hwnd, passphrase_callback_proc,
408     (LPARAM)c);
409     }
410     if (rc == -1) {
411     if (!WriteFile (hd, "\n", 1, &n, NULL))
412     log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
413     return 0;
414     }
415     c->pwd_init = 0;
416     }
417     if (c->cancel) {
418     if (!WriteFile (hd, "\n", 1, &n, NULL))
419     log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
420     return 0;
421     }
422    
423 twoaday 77 if (!WriteFile (hd, c->pwd, strlen (c->pwd), &n, NULL))
424     log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
425     if (!WriteFile (hd, "\n", 1, &n, NULL))
426     log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
427 werner 36 return 0;
428     }
429    
430    
431     /* Initialize the given passphrase callback @cb with the
432     used gpgme context @ctx, the command @cmd and a title
433     @title for the dialog. */
434     void
435     set_gpg_passphrase_cb (passphrase_cb_s *cb, gpgme_ctx_t ctx,
436     int cmd, HWND hwnd, const char *title)
437     {
438     memset (cb, 0, sizeof *cb);
439     cb->gpg_cmd = cmd;
440     cb->bad_pwd = 0;
441     cb->is_card = 0;
442     cb->cancel = 0;
443     cb->hwnd = hwnd;
444     cb->pwd_init = 1;
445     free_if_alloc (cb->title);
446     cb->title = m_strdup (title);
447     if (!cb->title)
448     BUG (NULL);
449     gpgme_set_passphrase_cb (ctx, passphrase_cb, cb);
450     cb->gpg = ctx;
451     }
452    
453    
454     /* Release a passphrase callback @ctx. */
455     void
456     release_gpg_passphrase_cb (passphrase_cb_s *ctx)
457     {
458     gpgme_recipient_t r, n;
459    
460     if (!ctx)
461     return;
462     sfree_if_alloc (ctx->pwd);
463     free_if_alloc (ctx->title);
464     r = ctx->recipients;
465     while (r) {
466     n = r->next;
467     safe_free (r->keyid);
468     safe_free (r);
469     r = n;
470     }
471     }
472    
473    
474     /* Simple check to measure passphrase (@pass) quality.
475     Return value: 0 on success. */
476     int
477     check_passwd_quality (const char *pass, int strict)
478     {
479     int i, nd=0, nc=0, n;
480    
481     n = strlen (pass);
482     if (n < 8)
483     return -1;
484    
485     for (i=0; i < n; i++) {
486     if (isdigit (pass[i]))
487     nd++;
488     if (isalpha (pass[i]))
489     nc++;
490     }
491    
492     /* check that the passphrase contains letters and numbers. */
493     if (nd == n || nc == n)
494     return -1;
495    
496     return 0;
497     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26