1 |
/* wptCardEditCB.cpp - Card callbacks |
/* wptCardEditCB.cpp - Card callbacks |
2 |
* Copyright (C) 2003, 2004, 2005 Timo Schulz |
* Copyright (C) 2003, 2004, 2005 Timo Schulz |
3 |
|
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
6 |
* |
* |
15 |
* GNU General Public License for more details. |
* GNU General Public License for more details. |
16 |
* |
* |
17 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software Foundation, |
* along with this program; if not, gpg_write to the Free Software Foundation, |
19 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
20 |
*/ |
*/ |
21 |
|
|
22 |
|
#include <windows.h> |
23 |
#include <stdio.h> |
#include <stdio.h> |
24 |
#include <malloc.h> |
#include <malloc.h> |
25 |
#include <string.h> |
#include <string.h> |
26 |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <io.h> |
|
27 |
|
|
28 |
#include "w32gpgme.h" |
#include "w32gpgme.h" |
29 |
#include "wptCardEdit.h" |
#include "wptCardEdit.h" |
30 |
#include "wptCard.h" |
#include "wptCard.h" |
31 |
|
|
32 |
|
|
33 |
#define save_write(val) ((val)? (val) : "") |
#define save_write(val) ((val)? (val) : "") |
34 |
|
|
35 |
|
|
36 |
|
/* Wrapper to emulate gpg_write(). */ |
37 |
|
static int |
38 |
|
gpg_write (int fd, const void *buf, size_t buflen) |
39 |
|
{ |
40 |
|
HANDLE hd = (HANDLE)fd; |
41 |
|
DWORD n; |
42 |
|
|
43 |
|
WriteFile (hd, buf, buflen, &n, NULL); |
44 |
|
return n; |
45 |
|
} |
46 |
|
|
47 |
|
|
48 |
static gpgme_error_t |
static gpgme_error_t |
49 |
change_pin_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd) |
change_pin_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd) |
57 |
if (!strcmp (key, "cardctrl.insert_card_okay")) { |
if (!strcmp (key, "cardctrl.insert_card_okay")) { |
58 |
if (c->card_cb) { |
if (c->card_cb) { |
59 |
s = c->card_cb (1, c->cb_value); |
s = c->card_cb (1, c->cb_value); |
60 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
61 |
return 0; |
return 0; |
62 |
} |
} |
63 |
} |
} |
68 |
s = "passwd"; |
s = "passwd"; |
69 |
else |
else |
70 |
s = "quit"; |
s = "quit"; |
71 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
72 |
return 0; |
return 0; |
73 |
} |
} |
74 |
if (!strcmp (key, "cardutil.change_pin.menu")) { |
if (!strcmp (key, "cardutil.change_pin.menu")) { |
79 |
case GPG_EDITCARD_CHAPIN: s = "3"; |
case GPG_EDITCARD_CHAPIN: s = "3"; |
80 |
default: c->cnt = 0; s = "Q"; |
default: c->cnt = 0; s = "Q"; |
81 |
} |
} |
82 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
83 |
return 0; |
return 0; |
84 |
} |
} |
85 |
else if (c->cnt > 0) { |
else if (c->cnt > 0) { |
86 |
c->cnt = 0; |
c->cnt = 0; |
87 |
write (fd, "Q", 1); |
gpg_write (fd, "Q", 1); |
88 |
return 0; |
return 0; |
89 |
} |
} |
90 |
} |
} |
101 |
s = c->pin_new; |
s = c->pin_new; |
102 |
c->cnt++; |
c->cnt++; |
103 |
} |
} |
104 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
105 |
} |
} |
106 |
else if (c->type == GPG_EDITCARD_CHAPIN) { |
else if (c->type == GPG_EDITCARD_CHAPIN) { |
107 |
if (!strcmp (key, "passphrase.adminpin.ask")) { |
if (!strcmp (key, "passphrase.adminpin.ask")) { |
116 |
s = c->pin_new; |
s = c->pin_new; |
117 |
c->cnt++; |
c->cnt++; |
118 |
} |
} |
119 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
120 |
} |
} |
121 |
else if (c->type == GPG_EDITCARD_UNBPIN) { |
else if (c->type == GPG_EDITCARD_UNBPIN) { |
122 |
/* todo */ |
/* todo */ |
139 |
if (!strcmp (key, "cardctrl.insert_card.okay")) { |
if (!strcmp (key, "cardctrl.insert_card.okay")) { |
140 |
if (c->card_cb) { |
if (c->card_cb) { |
141 |
s = c->card_cb (1, c->cb_value); |
s = c->card_cb (1, c->cb_value); |
142 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
143 |
return 0; |
return 0; |
144 |
} |
} |
145 |
} |
} |
147 |
if (!strcmp (key, "cardedit.prompt")) { |
if (!strcmp (key, "cardedit.prompt")) { |
148 |
if (c->cnt++ == 0) { /* first switch in the admin mode */ |
if (c->cnt++ == 0) { /* first switch in the admin mode */ |
149 |
s = "admin"; |
s = "admin"; |
150 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
151 |
return 0; |
return 0; |
152 |
} |
} |
153 |
if (c->cnt == 1) {/* then run the send command */ |
if (c->cnt == 1) {/* then run the send command */ |
161 |
case GPG_EDITCARD_GENKEY: s = "generate"; |
case GPG_EDITCARD_GENKEY: s = "generate"; |
162 |
default: s = "quit"; |
default: s = "quit"; |
163 |
} |
} |
164 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
165 |
return 0; |
return 0; |
166 |
} |
} |
167 |
else if (c->cnt >= 2) {/* done: send exit */ |
else if (c->cnt >= 2) {/* done: send exit */ |
168 |
c->cnt = 0; |
c->cnt = 0; |
169 |
s = "quit"; |
s = "quit"; |
170 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
171 |
return 0; |
return 0; |
172 |
} |
} |
173 |
} |
} |
174 |
if (c->cnt > 0) { |
if (c->cnt > 0) { |
175 |
if (!strcmp (key, "passphrase.adminpin.ask")) { |
if (!strcmp (key, "passphrase.adminpin.ask")) { |
176 |
write (fd, c->admin_pin, strlen (c->admin_pin)); |
gpg_write (fd, c->admin_pin, strlen (c->admin_pin)); |
177 |
return 0; |
return 0; |
178 |
} |
} |
179 |
if (!strcmp (key, "passphrase.pin.ask")) { |
if (!strcmp (key, "passphrase.pin.ask")) { |
180 |
write (fd, c->pin, strlen (c->pin)); |
gpg_write (fd, c->pin, strlen (c->pin)); |
181 |
return 0; |
return 0; |
182 |
} |
} |
183 |
} |
} |
185 |
case GPG_EDITCARD_NAME: |
case GPG_EDITCARD_NAME: |
186 |
if (!strcmp (key, "keygen.smartcard.surname")) { |
if (!strcmp (key, "keygen.smartcard.surname")) { |
187 |
s = save_write (c->edit.surname); |
s = save_write (c->edit.surname); |
188 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
189 |
} |
} |
190 |
else if (!strcmp (key, "keygen.smartcard.givenname")) { |
else if (!strcmp (key, "keygen.smartcard.givenname")) { |
191 |
s = save_write (c->edit.givenname); |
s = save_write (c->edit.givenname); |
192 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
193 |
} |
} |
194 |
break; |
break; |
195 |
|
|
196 |
case GPG_EDITCARD_KEYURL: |
case GPG_EDITCARD_KEYURL: |
197 |
if (!strcmp (key, "cardedit.change_url")) { |
if (!strcmp (key, "cardedit.change_url")) { |
198 |
s = save_write (c->edit.keyurl); |
s = save_write (c->edit.keyurl); |
199 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
200 |
} |
} |
201 |
break; |
break; |
202 |
|
|
203 |
case GPG_EDITCARD_LOGIN: |
case GPG_EDITCARD_LOGIN: |
204 |
if (!strcmp (key, "cardedit.change_login")) { |
if (!strcmp (key, "cardedit.change_login")) { |
205 |
s = save_write (c->edit.login); |
s = save_write (c->edit.login); |
206 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
207 |
} |
} |
208 |
break; |
break; |
209 |
|
|
218 |
else { |
else { |
219 |
buf[0] = c->edit.sex; buf[1] = 0; |
buf[0] = c->edit.sex; buf[1] = 0; |
220 |
} |
} |
221 |
write (fd, buf, strlen (buf)); |
gpg_write (fd, buf, strlen (buf)); |
222 |
} |
} |
223 |
break; |
break; |
224 |
|
|
225 |
case GPG_EDITCARD_LANG: |
case GPG_EDITCARD_LANG: |
226 |
if (!strcmp (key, "cardedit.change_lang")) { |
if (!strcmp (key, "cardedit.change_lang")) { |
227 |
s = save_write (c->edit.lang); |
s = save_write (c->edit.lang); |
228 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
229 |
} |
} |
230 |
break; |
break; |
231 |
|
|
232 |
case GPG_EDITCARD_GENKEY: |
case GPG_EDITCARD_GENKEY: |
233 |
if (!strcmp (key, "passphrase.enter")) |
if (!strcmp (key, "passphrase.enter")) |
234 |
write (fd, c->keygen.pass, strlen (c->keygen.pass)); |
gpg_write (fd, c->keygen.pass, strlen (c->keygen.pass)); |
235 |
if (!strcmp (key, "cardedit.genkeys.backup_enc")) { |
if (!strcmp (key, "cardedit.genkeys.backup_enc")) { |
236 |
s = c->keygen.flags & GPG_CARDFLAG_BAKENC? "Y" : "N"; |
s = c->keygen.flags & GPG_CARDFLAG_BAKENC? "Y" : "N"; |
237 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
238 |
} |
} |
239 |
if (!strcmp (key, "cardedit.genkeys.replace_keys")) { |
if (!strcmp (key, "cardedit.genkeys.replace_keys")) { |
240 |
if (! (c->keygen.flags & GPG_CARDFLAG_REPLACE)) |
if (! (c->keygen.flags & GPG_CARDFLAG_REPLACE)) |
241 |
c->cancel = 1; |
c->cancel = 1; |
242 |
s = c->keygen.flags & GPG_CARDFLAG_REPLACE? "Y" : "N"; |
s = c->keygen.flags & GPG_CARDFLAG_REPLACE? "Y" : "N"; |
243 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
244 |
} |
} |
245 |
else if (!strcmp (key, "keygen.valid")) { |
else if (!strcmp (key, "keygen.valid")) { |
246 |
s = save_write (c->keygen.expdate); |
s = save_write (c->keygen.expdate); |
247 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
248 |
} |
} |
249 |
else if (!strcmp (key, "keygen.name")) { |
else if (!strcmp (key, "keygen.name")) { |
250 |
s = save_write (c->keygen.name); |
s = save_write (c->keygen.name); |
251 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
252 |
} |
} |
253 |
else if (!strcmp (key, "keygen.email")) { |
else if (!strcmp (key, "keygen.email")) { |
254 |
s = save_write (c->keygen.email); |
s = save_write (c->keygen.email); |
255 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
256 |
} |
} |
257 |
else if (!strcmp (key, "keygen.comment")) { |
else if (!strcmp (key, "keygen.comment")) { |
258 |
s = save_write (c->keygen.comment); |
s = save_write (c->keygen.comment); |
259 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
260 |
} |
} |
261 |
break; |
break; |
262 |
|
|