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

Contents of /trunk/src/OEPassphraseCBDlg.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (show 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 /* 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
27 /* Structure for the passphrase callback. */
28 struct pass_cb_s {
29 const char *uid_hint;
30 const char *passphrase_info;
31 char keyid[8+2];
32 char *pass;
33 HWND main_wnd;
34 int cancel;
35 int prev_was_bad;
36 };
37
38
39 /* Reference to the passphrase cache. */
40 extern char gpgoe_pass_cache[HASH_BUCKETS][256];
41
42
43 DWORD hash_string (const char *str_param);
44
45 /* Store the passphrase @pass in the hash table using the keyid @keyid
46 as the index. */
47 static void
48 passphrase_put (char ctx[HASH_BUCKETS][256], const char *keyid, const char *pass)
49 {
50 int pos = hash_string (keyid) % HASH_BUCKETS;
51 int n, passlen;
52
53 /* We cannot store passphrase of that size. */
54 passlen = strlen (pass);
55 if (passlen > 255)
56 return;
57
58 n = 0;
59 while (n < HASH_BUCKETS) {
60 if (strlen (ctx[(pos+n) % HASH_BUCKETS]) != 0) {
61 n++;
62 continue;
63 }
64 memcpy (ctx[(pos+n) % HASH_BUCKETS], keyid, strlen (keyid));
65 strncpy (ctx[(pos+n) % HASH_BUCKETS]+strlen (keyid), pass, passlen);
66 break;
67 }
68 }
69
70
71 /* 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 static const char*
75 passphrase_get (char ctx[HASH_BUCKETS][256], const char *keyid)
76 {
77 const char *item;
78 int pos = hash_string (keyid) % HASH_BUCKETS;
79 int n;
80
81 n = 0;
82 item = gpgoe_pass_cache[pos];
83 while (n < HASH_BUCKETS) {
84 item = ctx[(pos+n) % HASH_BUCKETS];
85 if (!strncmp (item, keyid, strlen (keyid)))
86 break;
87 n++;
88 }
89
90 if (strlen (item) > 0 && !strncmp (item, keyid, strlen (keyid)))
91 return item + strlen (keyid);
92 /* Return NULL to indicate no matching entry. */
93 return NULL;
94 }
95
96
97 /* 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 case 1: return "RSA";
107 case 17: return "DSA";
108 case 16: return "ELG";
109 default: break;
110 }
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 char *info;
121 int n;
122
123 switch (msg) {
124 case WM_INITDIALOG:
125 ctx = (pass_cb_t)lparam;
126 assert (ctx != NULL);
127 if (ctx->uid_hint && ctx->passphrase_info) {
128 char *utf8_uid = utf8_to_native (ctx->uid_hint+17);
129 info = xcalloc (1, strlen (utf8_uid) + strlen (ctx->keyid) + 32);
130 sprintf (info, _("%s\n%s key, ID %s"), utf8_uid,
131 get_pubkey_algo (ctx->passphrase_info), ctx->keyid);
132 SetDlgItemText (dlg, IDC_DECRYPT_MSG, info);
133 free_if_alloc (utf8_uid);
134 free_if_alloc (info);
135 }
136 SetDlgItemText (dlg, IDOK, _("&OK"));
137 SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
138 SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO, _("Please enter your passphrase"));
139 SendDlgItemMessage (dlg, IDC_DECRYPT_PWD, EM_SETPASSWORDCHAR,
140 (WPARAM)(UINT)'*', 0);
141 CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED);
142 SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide typing"));
143 SetWindowText (dlg, _("Decryption"));
144 SetForegroundWindow (dlg);
145 center_window (dlg, ctx->main_wnd);
146 break;
147
148 case WM_COMMAND:
149 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 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 n = SendDlgItemMessage (dlg, IDC_DECRYPT_PWD,
174 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 /* Make sure we reset the cancel flag. */
181 ctx->cancel = 0;
182 ctx->pass = xcalloc (1, n+2);
183 GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1);
184
185 if (gpgoe_get_active_modes () & GPGOE_MODE_CACHEPASS)
186 passphrase_put (gpgoe_pass_cache, ctx->keyid, ctx->pass);
187 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 const char *pass;
206 DWORD nwritten;
207
208 assert (cb != NULL);
209 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 memset (cb->keyid, 0, sizeof (cb->keyid));
218 memcpy (cb->keyid, cb->passphrase_info+8, 8);
219
220 pass = passphrase_get (gpgoe_pass_cache, cb->keyid);
221 if (pass)
222 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 }
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 cb_val->main_wnd = main;
245 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
263
264 /* Return 1 if the passphrase dialog were cancelled. */
265 int
266 pass_cb_cancelled (pass_cb_t cb)
267 {
268 return cb->cancel;
269 }
270
271
272 /* Reset the passphrase cache by overwritting each items with zeroes */
273 void
274 reset_pass_cache (void)
275 {
276 int i;
277
278 for (i=0; i < HASH_BUCKETS; i++)
279 wipememory (gpgoe_pass_cache[i], DIM (gpgoe_pass_cache[i]));
280 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26