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