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

Annotation of /trunk/src/OEPassphraseCBDlg.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (hide annotations)
Fri Apr 7 10:46:41 2006 UTC (19 years, 1 month ago) by twoaday
File MIME type: text/plain
File size: 4795 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     * 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 twoaday 11
31 twoaday 1 /* Structure for the passphrase callback. */
32     struct pass_cb_s {
33     const char *uid_hint;
34     const char *passphrase_info;
35     char *pass;
36     HWND main_hwnd;
37     int cancel;
38     int prev_was_bad;
39     };
40    
41    
42     /* Extract public key algorithm from passwd info. */
43     const char*
44     get_pubkey_algo (const char *passphrase_info)
45     {
46     size_t pos = 16+1+16+1;
47    
48     /* AA6F7AB7DD3B4A21 E71D9BC8C93A8529 1 0 */
49     assert (strlen (passphrase_info) > pos);
50     switch (atol (passphrase_info+pos)) {
51     case 1: return "RSA";
52     case 17: return "DSA";
53     case 16: return "ELG";
54     }
55     return "???";
56     }
57    
58    
59     /* Passphrase callback dialog box procedure. */
60     BOOL CALLBACK
61     pass_cb_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
62     {
63     static pass_cb_t ctx;
64     char *info, keyid[8+2] = {0};
65     int n;
66    
67     switch (msg) {
68     case WM_INITDIALOG:
69     ctx = (pass_cb_t)lparam;
70     assert (ctx);
71     if (ctx->uid_hint && ctx->passphrase_info) {
72     char *utf8_uid = utf8_to_native (ctx->uid_hint+17);
73     memcpy (keyid, ctx->passphrase_info+8, 8);
74     info = xcalloc (1, strlen (utf8_uid) + strlen (keyid) + 32);
75     sprintf (info, _("%s\n%s key, ID %s"), utf8_uid,
76     get_pubkey_algo (ctx->passphrase_info), keyid);
77     SetDlgItemText (dlg, IDC_DECRYPT_MSG, info);
78     free_if_alloc (utf8_uid);
79     free_if_alloc (info);
80     }
81 twoaday 12 SetDlgItemText (dlg, IDOK, _("&OK"));
82     SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
83     SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO, _("Please enter your passphrase"));
84 twoaday 1 SendDlgItemMessage (dlg, IDC_DECRYPT_PWD, EM_SETPASSWORDCHAR,
85     (WPARAM)(UINT)'*', 0);
86 twoaday 12 SetWindowText (dlg, _("Decryption"));
87 twoaday 1 SetForegroundWindow (dlg);
88     break;
89    
90     case WM_COMMAND:
91     switch (LOWORD (wparam)) {
92     case IDCANCEL:
93     ctx->cancel = 1;
94     free_if_alloc (ctx->pass);
95     EndDialog (dlg, FALSE);
96     break;
97    
98     case IDOK:
99     if (ctx->prev_was_bad)
100     SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO,
101     _("Invalid passphrase; please enter your passphrase again"));
102     else
103     SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO,
104     _("Please enter your passphrase"));
105     n = SendDlgItemMessage (dlg, IDC_DECRYPT_PWD,
106     WM_GETTEXTLENGTH, 0, 0);
107     if (n < 1) {
108     MessageBox (dlg, _("Please enter your passphrase"),
109     _("GPG Plug-in Info"), MB_ICONINFORMATION|MB_OK);
110     return FALSE;
111     }
112     ctx->cancel = 0;
113     ctx->pass = xcalloc (1, n+2);
114     GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1);
115     EndDialog (dlg, TRUE);
116     break;
117     }
118     break;
119     }
120     return FALSE;
121     }
122    
123    
124     /* GPGME compatible passphrase callback. */
125     gpgme_error_t
126     passphrase_cb (void *hook,
127     const char *uid_hint,
128     const char *passphrase_info,
129     int prev_was_bad, int fd)
130     {
131     pass_cb_t cb = (pass_cb_t)hook;
132     HANDLE fp = (HANDLE)fd;
133     DWORD nwritten = 0;
134    
135     cb->prev_was_bad = prev_was_bad;
136     if (prev_was_bad && !cb->cancel) {
137     wipememory (cb->pass, strlen (cb->pass));
138     free_if_alloc (cb->pass);
139     }
140     if (!cb->pass && !cb->cancel) {
141     cb->uid_hint = uid_hint;
142     cb->passphrase_info = passphrase_info;
143     DialogBoxParam (mod_hinst_dll, (LPCTSTR)IDD_DECRYPT,
144     cb->main_hwnd, pass_cb_dlg_proc, (LPARAM)cb);
145     }
146    
147     if (cb->cancel)
148     WriteFile (fp, "\n", 1, &nwritten, NULL);
149     else if (cb->pass != NULL) {
150     WriteFile (fp, cb->pass, strlen (cb->pass), &nwritten, NULL);
151     WriteFile (fp, "\n", 1, &nwritten, NULL);
152     }
153     return 0;
154     }
155    
156    
157     /* Allocate new passphrase callback context. */
158     pass_cb_t
159     new_pass_cb (HWND main)
160     {
161     pass_cb_t cb_val;
162    
163     cb_val = xcalloc (1, sizeof *cb_val);
164     cb_val->main_hwnd = main;
165     return cb_val;
166     }
167    
168    
169    
170     /* Free passphrase callback context. */
171     void
172     free_pass_cb (pass_cb_t cb)
173     {
174     if (!cb)
175     return;
176     if (cb->pass) {
177     wipememory (cb->pass, strlen (cb->pass));
178     free_if_alloc (cb->pass);
179     }
180     free_if_alloc (cb);
181     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26