/[gpgoe]/trunk/src/OEPassphraseCBDlg.c
ViewVC logotype

Annotation of /trunk/src/OEPassphraseCBDlg.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (hide annotations)
Sat Aug 18 10:55:14 2007 UTC (17 years, 8 months ago) by twoaday
File MIME type: text/plain
File size: 7217 byte(s)


1 twoaday 1 /* OEPassphraseDLG.cpp - GPGME Passphrase Callback
2     * Copyright (C) 2001, 2002, 2006 Timo Schulz
3     *
4     * This file is part of GPGOE.
5     *
6     * GPGOE is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU Lesser General Public License as published by
8     * the Free Software Foundation; either version 2.1 of the License, or
9     * (at your option) any later version.
10     *
11     * GPGOE 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    
17     #ifdef HAVE_CONFIG_H
18     #include <config.h>
19     #endif
20     #include <windows.h>
21     #include <assert.h>
22     #include "resource.h"
23     #include "gpgme.h"
24     #include "GPGOE.h"
25    
26 twoaday 11
27 twoaday 1 /* Structure for the passphrase callback. */
28     struct pass_cb_s {
29     const char *uid_hint;
30     const char *passphrase_info;
31 twoaday 16 char keyid[8+2];
32 twoaday 1 char *pass;
33 twoaday 16 HWND main_wnd;
34 twoaday 1 int cancel;
35     int prev_was_bad;
36     };
37    
38    
39 twoaday 23 /* Reference to the passphrase cache. */
40 twoaday 19 extern char gpgoe_pass_cache[HASH_BUCKETS][256];
41 twoaday 16
42    
43 twoaday 19 DWORD hash_string (const char *str_param);
44 twoaday 16
45 twoaday 19 /* Store the passphrase @pass in the hash table using the keyid @keyid
46     as the index. */
47 twoaday 16 static void
48 twoaday 19 passphrase_put (char ctx[HASH_BUCKETS][256], const char *keyid, const char *pass)
49 twoaday 16 {
50 twoaday 19 int pos = hash_string (keyid) % HASH_BUCKETS;
51 twoaday 23 int n, passlen;
52 twoaday 16
53 twoaday 23 /* We cannot store passphrase of that size. */
54     passlen = strlen (pass);
55     if (passlen > 255)
56     return;
57    
58     n = 0;
59 twoaday 19 while (n < HASH_BUCKETS) {
60 twoaday 23 if (strlen (ctx[(pos+n) % HASH_BUCKETS]) != 0) {
61     n++;
62     continue;
63 twoaday 19 }
64 twoaday 23 memcpy (ctx[(pos+n) % HASH_BUCKETS], keyid, strlen (keyid));
65     strncpy (ctx[(pos+n) % HASH_BUCKETS]+strlen (keyid), pass, passlen);
66     break;
67 twoaday 16 }
68     }
69    
70    
71 twoaday 19 /* Return the requested passphrase from the hash table @ctx
72     using the keyid @keyid as the index. Or NULL if there is
73     no stored passphrase. */
74 twoaday 16 static const char*
75 twoaday 19 passphrase_get (char ctx[HASH_BUCKETS][256], const char *keyid)
76 twoaday 16 {
77 twoaday 19 const char *item;
78     int pos = hash_string (keyid) % HASH_BUCKETS;
79 twoaday 23 int n;
80 twoaday 16
81 twoaday 23 n = 0;
82 twoaday 19 item = gpgoe_pass_cache[pos];
83 twoaday 23 while (n < HASH_BUCKETS) {
84 twoaday 19 item = ctx[(pos+n) % HASH_BUCKETS];
85 twoaday 23 if (!strncmp (item, keyid, strlen (keyid)))
86     break;
87 twoaday 19 n++;
88 twoaday 16 }
89 twoaday 23
90 twoaday 19 if (strlen (item) > 0 && !strncmp (item, keyid, strlen (keyid)))
91 twoaday 23 return item + strlen (keyid);
92     /* Return NULL to indicate no matching entry. */
93 twoaday 16 return NULL;
94     }
95    
96    
97 twoaday 1 /* Extract public key algorithm from passwd info. */
98     const char*
99     get_pubkey_algo (const char *passphrase_info)
100     {
101     size_t pos = 16+1+16+1;
102    
103     /* AA6F7AB7DD3B4A21 E71D9BC8C93A8529 1 0 */
104     assert (strlen (passphrase_info) > pos);
105     switch (atol (passphrase_info+pos)) {
106 twoaday 16 case 1: return "RSA";
107 twoaday 1 case 17: return "DSA";
108     case 16: return "ELG";
109 twoaday 23 default: break;
110 twoaday 1 }
111     return "???";
112     }
113    
114    
115     /* Passphrase callback dialog box procedure. */
116     BOOL CALLBACK
117     pass_cb_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
118     {
119     static pass_cb_t ctx;
120 twoaday 16 char *info;
121 twoaday 1 int n;
122    
123     switch (msg) {
124     case WM_INITDIALOG:
125     ctx = (pass_cb_t)lparam;
126 twoaday 23 assert (ctx != NULL);
127 twoaday 1 if (ctx->uid_hint && ctx->passphrase_info) {
128     char *utf8_uid = utf8_to_native (ctx->uid_hint+17);
129 twoaday 16 info = xcalloc (1, strlen (utf8_uid) + strlen (ctx->keyid) + 32);
130 twoaday 1 sprintf (info, _("%s\n%s key, ID %s"), utf8_uid,
131 twoaday 23 get_pubkey_algo (ctx->passphrase_info), ctx->keyid);
132 twoaday 1 SetDlgItemText (dlg, IDC_DECRYPT_MSG, info);
133     free_if_alloc (utf8_uid);
134     free_if_alloc (info);
135     }
136 twoaday 12 SetDlgItemText (dlg, IDOK, _("&OK"));
137     SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
138     SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO, _("Please enter your passphrase"));
139 twoaday 1 SendDlgItemMessage (dlg, IDC_DECRYPT_PWD, EM_SETPASSWORDCHAR,
140     (WPARAM)(UINT)'*', 0);
141 twoaday 16 CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED);
142 twoaday 18 SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide typing"));
143 twoaday 12 SetWindowText (dlg, _("Decryption"));
144 twoaday 1 SetForegroundWindow (dlg);
145 twoaday 16 center_window (dlg, ctx->main_wnd);
146 twoaday 1 break;
147    
148     case WM_COMMAND:
149 twoaday 16 if (HIWORD (wparam) == BN_CLICKED &&
150     LOWORD (wparam) == IDC_DECRYPT_HIDE) {
151     HWND hwnd;
152     hwnd = GetDlgItem (dlg, IDC_DECRYPT_PWD);
153     SendMessage (hwnd, EM_SETPASSWORDCHAR,
154     IsDlgButtonChecked (dlg, IDC_DECRYPT_HIDE)? '*' : 0, 0);
155     SetFocus (hwnd);
156     break;
157     }
158    
159 twoaday 1 switch (LOWORD (wparam)) {
160     case IDCANCEL:
161     ctx->cancel = 1;
162     free_if_alloc (ctx->pass);
163     EndDialog (dlg, FALSE);
164     break;
165    
166     case IDOK:
167     if (ctx->prev_was_bad)
168     SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO,
169     _("Invalid passphrase; please enter your passphrase again"));
170     else
171     SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO,
172     _("Please enter your passphrase"));
173 twoaday 16 n = SendDlgItemMessage (dlg, IDC_DECRYPT_PWD,
174 twoaday 1 WM_GETTEXTLENGTH, 0, 0);
175     if (n < 1) {
176     MessageBox (dlg, _("Please enter your passphrase"),
177     _("GPG Plug-in Info"), MB_ICONINFORMATION|MB_OK);
178     return FALSE;
179     }
180 twoaday 23 /* Make sure we reset the cancel flag. */
181 twoaday 1 ctx->cancel = 0;
182     ctx->pass = xcalloc (1, n+2);
183     GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1);
184 twoaday 16
185 twoaday 19 if (gpgoe_get_active_modes () & GPGOE_MODE_CACHEPASS)
186     passphrase_put (gpgoe_pass_cache, ctx->keyid, ctx->pass);
187 twoaday 1 EndDialog (dlg, TRUE);
188     break;
189     }
190     break;
191     }
192     return FALSE;
193     }
194    
195    
196     /* GPGME compatible passphrase callback. */
197     gpgme_error_t
198     passphrase_cb (void *hook,
199     const char *uid_hint,
200     const char *passphrase_info,
201     int prev_was_bad, int fd)
202     {
203     pass_cb_t cb = (pass_cb_t)hook;
204     HANDLE fp = (HANDLE)fd;
205 twoaday 16 const char *pass;
206 twoaday 23 DWORD nwritten;
207 twoaday 1
208 twoaday 23 assert (cb != NULL);
209 twoaday 1 cb->prev_was_bad = prev_was_bad;
210     if (prev_was_bad && !cb->cancel) {
211     wipememory (cb->pass, strlen (cb->pass));
212     free_if_alloc (cb->pass);
213     }
214     if (!cb->pass && !cb->cancel) {
215     cb->uid_hint = uid_hint;
216     cb->passphrase_info = passphrase_info;
217 twoaday 16 memset (cb->keyid, 0, sizeof (cb->keyid));
218     memcpy (cb->keyid, cb->passphrase_info+8, 8);
219    
220 twoaday 19 pass = passphrase_get (gpgoe_pass_cache, cb->keyid);
221     if (pass)
222 twoaday 16 cb->pass = xstrdup (pass);
223     else
224     DialogBoxParam (mod_hinst_dll, (LPCTSTR)IDD_DECRYPT,
225     cb->main_wnd, pass_cb_dlg_proc, (LPARAM)cb);
226 twoaday 1 }
227     if (cb->cancel)
228     WriteFile (fp, "\n", 1, &nwritten, NULL);
229     else if (cb->pass != NULL) {
230     WriteFile (fp, cb->pass, strlen (cb->pass), &nwritten, NULL);
231     WriteFile (fp, "\n", 1, &nwritten, NULL);
232     }
233     return 0;
234     }
235    
236    
237     /* Allocate new passphrase callback context. */
238     pass_cb_t
239     new_pass_cb (HWND main)
240     {
241     pass_cb_t cb_val;
242    
243     cb_val = xcalloc (1, sizeof *cb_val);
244 twoaday 16 cb_val->main_wnd = main;
245 twoaday 1 return cb_val;
246     }
247    
248    
249    
250     /* Free passphrase callback context. */
251     void
252     free_pass_cb (pass_cb_t cb)
253     {
254     if (!cb)
255     return;
256     if (cb->pass) {
257     wipememory (cb->pass, strlen (cb->pass));
258     free_if_alloc (cb->pass);
259     }
260     free_if_alloc (cb);
261     }
262 twoaday 16
263    
264 twoaday 23 /* Return 1 if the passphrase dialog were cancelled. */
265 twoaday 18 int
266     pass_cb_cancelled (pass_cb_t cb)
267     {
268     return cb->cancel;
269     }
270    
271    
272 twoaday 23 /* Reset the passphrase cache by overwritting each items with zeroes */
273 twoaday 16 void
274 twoaday 19 reset_pass_cache (void)
275 twoaday 16 {
276 twoaday 19 int i;
277    
278     for (i=0; i < HASH_BUCKETS; i++)
279 twoaday 23 wipememory (gpgoe_pass_cache[i], DIM (gpgoe_pass_cache[i]));
280 twoaday 16 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26