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

Contents of /trunk/src/OEPassphraseCBDlg.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 19 - (show annotations)
Sun Jun 4 10:12:47 2006 UTC (18 years, 11 months ago) by twoaday
File MIME type: text/plain
File size: 6977 byte(s)
Prepare new release.


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26