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

Annotation of /trunk/src/OEPassphraseCBDlg.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (hide annotations)
Mon Apr 3 17:10:15 2006 UTC (19 years, 1 month ago) by twoaday
File MIME type: text/plain
File size: 4591 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     SendDlgItemMessage (dlg, IDC_DECRYPT_PWD, EM_SETPASSWORDCHAR,
82     (WPARAM)(UINT)'*', 0);
83     SetForegroundWindow (dlg);
84     break;
85    
86     case WM_COMMAND:
87     switch (LOWORD (wparam)) {
88     case IDCANCEL:
89     ctx->cancel = 1;
90     free_if_alloc (ctx->pass);
91     EndDialog (dlg, FALSE);
92     break;
93    
94     case IDOK:
95     if (ctx->prev_was_bad)
96     SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO,
97     _("Invalid passphrase; please enter your passphrase again"));
98     else
99     SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO,
100     _("Please enter your passphrase"));
101     n = SendDlgItemMessage (dlg, IDC_DECRYPT_PWD,
102     WM_GETTEXTLENGTH, 0, 0);
103     if (n < 1) {
104     MessageBox (dlg, _("Please enter your passphrase"),
105     _("GPG Plug-in Info"), MB_ICONINFORMATION|MB_OK);
106     return FALSE;
107     }
108     ctx->cancel = 0;
109     ctx->pass = xcalloc (1, n+2);
110     GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1);
111     EndDialog (dlg, TRUE);
112     break;
113     }
114     break;
115     }
116     return FALSE;
117     }
118    
119    
120     /* GPGME compatible passphrase callback. */
121     gpgme_error_t
122     passphrase_cb (void *hook,
123     const char *uid_hint,
124     const char *passphrase_info,
125     int prev_was_bad, int fd)
126     {
127     pass_cb_t cb = (pass_cb_t)hook;
128     HANDLE fp = (HANDLE)fd;
129     DWORD nwritten = 0;
130    
131     cb->prev_was_bad = prev_was_bad;
132     if (prev_was_bad && !cb->cancel) {
133     wipememory (cb->pass, strlen (cb->pass));
134     free_if_alloc (cb->pass);
135     }
136     if (!cb->pass && !cb->cancel) {
137     cb->uid_hint = uid_hint;
138     cb->passphrase_info = passphrase_info;
139     DialogBoxParam (mod_hinst_dll, (LPCTSTR)IDD_DECRYPT,
140     cb->main_hwnd, pass_cb_dlg_proc, (LPARAM)cb);
141     }
142    
143     if (cb->cancel)
144     WriteFile (fp, "\n", 1, &nwritten, NULL);
145     else if (cb->pass != NULL) {
146     WriteFile (fp, cb->pass, strlen (cb->pass), &nwritten, NULL);
147     WriteFile (fp, "\n", 1, &nwritten, NULL);
148     }
149     return 0;
150     }
151    
152    
153     /* Allocate new passphrase callback context. */
154     pass_cb_t
155     new_pass_cb (HWND main)
156     {
157     pass_cb_t cb_val;
158    
159     cb_val = xcalloc (1, sizeof *cb_val);
160     cb_val->main_hwnd = main;
161     return cb_val;
162     }
163    
164    
165    
166     /* Free passphrase callback context. */
167     void
168     free_pass_cb (pass_cb_t cb)
169     {
170     if (!cb)
171     return;
172     if (cb->pass) {
173     wipememory (cb->pass, strlen (cb->pass));
174     free_if_alloc (cb->pass);
175     }
176     free_if_alloc (cb);
177     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26