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

Annotation of /trunk/src/OEPassphraseCBDlg.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (hide annotations)
Tue Dec 13 10:40:30 2011 UTC (13 years, 4 months ago) by twoaday
File MIME type: text/plain
File size: 7260 byte(s)
Commit code for backup purposes.


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26