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 |
|
#include <windows.h> |
22 |
#include <stdio.h> |
#include <stdio.h> |
23 |
#include <malloc.h> |
#include <malloc.h> |
24 |
#include <string.h> |
#include <string.h> |
25 |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <io.h> |
|
26 |
|
|
27 |
#include "w32gpgme.h" |
#include "gpgme.h" |
|
#include "wptCardEdit.h" |
|
28 |
#include "wptCard.h" |
#include "wptCard.h" |
29 |
|
#include "wptCardEdit.h" |
30 |
|
#include "wptErrors.h" |
31 |
|
|
32 |
#define save_write(val) ((val)? (val) : "") |
#define save_write(val) ((val)? (val) : "") |
33 |
|
static const char *stat_key = ""; |
34 |
|
|
35 |
|
/* Wrapper to emulate write(). */ |
36 |
|
static int |
37 |
|
gpg_write (int fd, const void *buf, size_t buflen) |
38 |
|
{ |
39 |
|
HANDLE hd = (HANDLE)fd; |
40 |
|
DWORD n; |
41 |
|
|
42 |
|
MessageBox (NULL, (const char*)buf, stat_key, MB_OK); |
43 |
|
WriteFile (hd, buf, buflen, &n, NULL); |
44 |
|
WriteFile (hd, "\n", 1, &n, NULL); |
45 |
|
return n; |
46 |
|
} |
47 |
|
|
48 |
|
|
49 |
|
/* Common card handler. Handle general card status events. */ |
50 |
static gpgme_error_t |
static gpgme_error_t |
51 |
change_pin_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd) |
common_card_handler (GpgCardEdit *c, gpgme_status_code_t code, |
52 |
|
const char *key, int fd) |
53 |
{ |
{ |
54 |
GpgCardEdit *c = (GpgCardEdit *)opaque; |
const char *s; |
|
const char *s = ""; |
|
|
|
|
|
if (!c) |
|
|
return gpg_error (GPG_ERR_EOF); |
|
55 |
|
|
56 |
if (!strcmp (key, "cardctrl.insert_card_okay")) { |
if (!strcmp (key, "cardctrl.insert_card_okay")) { |
57 |
if (c->card_cb) { |
if (c->card_cb) { |
58 |
s = c->card_cb (1, c->cb_value); |
s = c->card_cb (1, c->cb_value); |
59 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
60 |
return 0; |
return 0; |
61 |
} |
} |
62 |
} |
} |
63 |
|
if (code == GPGME_STATUS_CARDCTRL && *key == '5') |
64 |
|
c->setResult (GPG_CARDRES_NOCARD); |
65 |
|
|
66 |
|
/* This can happen for example if a wrong PIN were |
67 |
|
submitted. In such a case, we quit ASAP. */ |
68 |
|
if (code == GPGME_STATUS_SC_OP_FAILURE) { |
69 |
|
c->setResult (GPG_CARDRES_BAD_PIN); |
70 |
|
s = "quit\n"; |
71 |
|
gpg_write (fd, s, strlen (s)); |
72 |
|
return 0; |
73 |
|
} |
74 |
|
return 0; |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
|
/* handler for changing PINs of cards. */ |
79 |
|
static gpgme_error_t |
80 |
|
change_pin_handler (void *opaque, gpgme_status_code_t code, |
81 |
|
const char *key, int fd) |
82 |
|
{ |
83 |
|
GpgCardEdit *c = (GpgCardEdit *)opaque; |
84 |
|
const char *s = ""; |
85 |
|
|
86 |
|
if (!c) |
87 |
|
return gpg_error (GPG_ERR_EOF); |
88 |
|
|
89 |
|
stat_key = key; /* XXX: debug code */ |
90 |
|
|
91 |
|
common_card_handler (c, code, key, fd); |
92 |
|
|
93 |
if (!strcmp (key, "cardedit.prompt")) { |
if (!strcmp (key, "cardedit.prompt")) { |
94 |
if (c->cnt++ == 0) |
if (c->cnt == 0) { |
95 |
|
c->cnt++; |
96 |
s = "admin"; |
s = "admin"; |
97 |
else if (c->cnt++ == 1) |
} |
98 |
|
else if (c->cnt == 1) { |
99 |
|
c->cnt++; |
100 |
s = "passwd"; |
s = "passwd"; |
101 |
else |
} |
102 |
|
else { |
103 |
|
c->reset (); |
104 |
s = "quit"; |
s = "quit"; |
105 |
write (fd, s, strlen (s)); |
} |
106 |
|
gpg_write (fd, s, strlen (s)); |
107 |
return 0; |
return 0; |
108 |
} |
} |
109 |
if (!strcmp (key, "cardutil.change_pin.menu")) { |
if (!strcmp (key, "cardutil.change_pin.menu")) { |
110 |
if (c->cnt++ == 2) { |
if (c->cnt == 2) { |
111 |
|
c->cnt++; |
112 |
switch (c->type) { |
switch (c->type) { |
113 |
case GPG_EDITCARD_CHUPIN: s = "1"; |
case GPG_EDITCARD_CHUPIN: s = "1"; break; |
114 |
case GPG_EDITCARD_UNBPIN: s = "2"; |
case GPG_EDITCARD_UNBPIN: s = "2"; break; |
115 |
case GPG_EDITCARD_CHAPIN: s = "3"; |
case GPG_EDITCARD_CHAPIN: s = "3"; break; |
116 |
default: c->cnt = 0; s = "Q"; |
default: s = "Q"; |
117 |
} |
} |
118 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
119 |
return 0; |
return 0; |
120 |
} |
} |
121 |
else if (c->cnt > 0) { |
else if (c->cnt > 0) { |
122 |
c->cnt = 0; |
gpg_write (fd, "Q", 1); |
|
write (fd, "Q", 1); |
|
123 |
return 0; |
return 0; |
124 |
} |
} |
125 |
} |
} |
132 |
s = c->pin_new; |
s = c->pin_new; |
133 |
c->cnt++; |
c->cnt++; |
134 |
} |
} |
135 |
if (!strcmp (key, "passphrase.pin.repeat")) { |
if (!strcmp (key, "passphrase.pin.repeat") || |
136 |
|
!strcmp (key, "passphrase.ask")) { |
137 |
s = c->pin_new; |
s = c->pin_new; |
138 |
c->cnt++; |
c->cnt++; |
139 |
} |
} |
140 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
141 |
} |
} |
142 |
else if (c->type == GPG_EDITCARD_CHAPIN) { |
else if (c->type == GPG_EDITCARD_CHAPIN) { |
143 |
if (!strcmp (key, "passphrase.adminpin.ask")) { |
if (!strcmp (key, "passphrase.adminpin.ask")) { |
144 |
s = c->pin; |
s = c->admin_pin; |
145 |
c->cnt++; |
c->cnt++; |
146 |
} |
} |
147 |
if (!strcmp (key, "passphrase.adminpin.new.ask")) { |
if (!strcmp (key, "passphrase.adminpin.new.ask")) { |
148 |
s = c->pin_new; |
s = c->pin_new; |
149 |
c->cnt++; |
c->cnt++; |
150 |
} |
} |
151 |
if (!strcmp (key, "passphrase.pin.repeat")) { |
if (!strcmp (key, "passphrase.pin.repeat") || |
152 |
|
!strcmp (key, "passphrase.ask")) { |
153 |
s = c->pin_new; |
s = c->pin_new; |
154 |
c->cnt++; |
c->cnt++; |
155 |
} |
} |
156 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
157 |
} |
} |
158 |
else if (c->type == GPG_EDITCARD_UNBPIN) { |
else if (c->type == GPG_EDITCARD_UNBPIN) { |
159 |
/* todo */ |
/* todo */ |
163 |
} |
} |
164 |
|
|
165 |
|
|
166 |
|
/* edit card attributes handler. */ |
167 |
static gpgme_error_t |
static gpgme_error_t |
168 |
editcard_handler (void * opaque, gpgme_status_code_t code, |
editcard_handler (void * opaque, gpgme_status_code_t code, |
169 |
const char *key, int fd) |
const char *key, int fd) |
173 |
|
|
174 |
if (!c) |
if (!c) |
175 |
return gpg_error (GPG_ERR_EOF); |
return gpg_error (GPG_ERR_EOF); |
176 |
|
|
177 |
if (!strcmp (key, "cardctrl.insert_card.okay")) { |
stat_key = key; |
178 |
if (c->card_cb) { |
|
179 |
s = c->card_cb (1, c->cb_value); |
common_card_handler (c, code, key, fd); |
|
write (fd, s, strlen (s)); |
|
|
return 0; |
|
|
} |
|
|
} |
|
180 |
|
|
181 |
if (!strcmp (key, "cardedit.prompt")) { |
if (!strcmp (key, "cardedit.prompt")) { |
182 |
if (c->cnt++ == 0) { /* first switch in the admin mode */ |
if (c->getResult ()) { /* in case of an error we quit. */ |
183 |
|
c->cnt = 0; |
184 |
|
s = "quit"; |
185 |
|
gpg_write (fd, s, strlen (s)); |
186 |
|
return 0; |
187 |
|
} |
188 |
|
if (c->cnt == 0) { /* first switch in the admin mode */ |
189 |
|
c->cnt++; |
190 |
s = "admin"; |
s = "admin"; |
191 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
192 |
return 0; |
return 0; |
193 |
} |
} |
194 |
if (c->cnt == 1) {/* then run the send command */ |
if (c->cnt == 1) {/* then run the send command */ |
195 |
c->cnt++; |
c->cnt++; |
196 |
switch (c->type) { |
switch (c->type) { |
197 |
case GPG_EDITCARD_NAME: s = "name"; |
case GPG_EDITCARD_NAME: s = "name"; break; |
198 |
case GPG_EDITCARD_KEYURL: s = "url"; |
case GPG_EDITCARD_KEYURL: s = "url"; break; |
199 |
case GPG_EDITCARD_LOGIN: s = "login"; |
case GPG_EDITCARD_LOGIN: s = "login"; break; |
200 |
case GPG_EDITCARD_SEX: s = "sex"; |
case GPG_EDITCARD_SEX: s = "sex"; break; |
201 |
case GPG_EDITCARD_LANG: s = "lang"; |
case GPG_EDITCARD_LANG: s = "lang"; break; |
202 |
case GPG_EDITCARD_GENKEY: s = "generate"; |
case GPG_EDITCARD_GENKEY: s = "generate"; break; |
203 |
default: s = "quit"; |
default: s = "quit"; |
204 |
} |
} |
205 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
206 |
return 0; |
return 0; |
207 |
} |
} |
208 |
else if (c->cnt >= 2) {/* done: send exit */ |
else if (c->cnt >= 2) {/* done: send exit */ |
209 |
c->cnt = 0; |
c->reset (); |
210 |
s = "quit"; |
s = "quit"; |
211 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
212 |
return 0; |
return 0; |
213 |
} |
} |
214 |
} |
} |
215 |
if (c->cnt > 0) { |
if (c->cnt > 0) { |
216 |
if (!strcmp (key, "passphrase.adminpin.ask")) { |
if (!strcmp (key, "passphrase.adminpin.ask")) { |
217 |
write (fd, c->admin_pin, strlen (c->admin_pin)); |
gpg_write (fd, c->admin_pin, strlen (c->admin_pin)); |
218 |
return 0; |
return 0; |
219 |
} |
} |
220 |
if (!strcmp (key, "passphrase.pin.ask")) { |
if (!strcmp (key, "passphrase.pin.ask")) { |
221 |
write (fd, c->pin, strlen (c->pin)); |
gpg_write (fd, c->pin, strlen (c->pin)); |
222 |
return 0; |
return 0; |
223 |
} |
} |
224 |
} |
} |
226 |
case GPG_EDITCARD_NAME: |
case GPG_EDITCARD_NAME: |
227 |
if (!strcmp (key, "keygen.smartcard.surname")) { |
if (!strcmp (key, "keygen.smartcard.surname")) { |
228 |
s = save_write (c->edit.surname); |
s = save_write (c->edit.surname); |
229 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
230 |
} |
} |
231 |
else if (!strcmp (key, "keygen.smartcard.givenname")) { |
else if (!strcmp (key, "keygen.smartcard.givenname")) { |
232 |
s = save_write (c->edit.givenname); |
s = save_write (c->edit.givenname); |
233 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
234 |
} |
} |
235 |
break; |
break; |
236 |
|
|
237 |
case GPG_EDITCARD_KEYURL: |
case GPG_EDITCARD_KEYURL: |
238 |
if (!strcmp (key, "cardedit.change_url")) { |
if (!strcmp (key, "cardedit.change_url")) { |
239 |
s = save_write (c->edit.keyurl); |
s = save_write (c->edit.keyurl); |
240 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
241 |
} |
} |
242 |
break; |
break; |
243 |
|
|
244 |
case GPG_EDITCARD_LOGIN: |
case GPG_EDITCARD_LOGIN: |
245 |
if (!strcmp (key, "cardedit.change_login")) { |
if (!strcmp (key, "cardedit.change_login")) { |
246 |
s = save_write (c->edit.login); |
s = save_write (c->edit.login); |
247 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
248 |
} |
} |
249 |
break; |
break; |
250 |
|
|
257 |
buf[0] = ' '; buf[1] = 0; |
buf[0] = ' '; buf[1] = 0; |
258 |
} |
} |
259 |
else { |
else { |
260 |
buf[0] = c->edit.sex; buf[1] = 0; |
buf[0] = c->edit.sex; |
261 |
|
buf[1] = 0; |
262 |
} |
} |
263 |
write (fd, buf, strlen (buf)); |
gpg_write (fd, buf, strlen (buf)); |
264 |
} |
} |
265 |
break; |
break; |
266 |
|
|
267 |
case GPG_EDITCARD_LANG: |
case GPG_EDITCARD_LANG: |
268 |
if (!strcmp (key, "cardedit.change_lang")) { |
if (!strcmp (key, "cardedit.change_lang")) { |
269 |
s = save_write (c->edit.lang); |
s = save_write (c->edit.lang); |
270 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
271 |
} |
} |
272 |
break; |
break; |
273 |
|
|
274 |
case GPG_EDITCARD_GENKEY: |
case GPG_EDITCARD_GENKEY: |
275 |
if (!strcmp (key, "passphrase.enter")) |
if (!strcmp (key, "passphrase.enter")) |
276 |
write (fd, c->keygen.pass, strlen (c->keygen.pass)); |
gpg_write (fd, c->keygen.pass, strlen (c->keygen.pass)); |
277 |
if (!strcmp (key, "cardedit.genkeys.backup_enc")) { |
if (!strcmp (key, "cardedit.genkeys.backup_enc")) { |
278 |
s = c->keygen.flags & GPG_CARDFLAG_BAKENC? "Y" : "N"; |
s = c->keygen.flags & GPG_CARDFLAG_BAKENC? "Y" : "N"; |
279 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
280 |
} |
} |
281 |
if (!strcmp (key, "cardedit.genkeys.replace_keys")) { |
if (!strcmp (key, "cardedit.genkeys.replace_keys")) { |
282 |
if (! (c->keygen.flags & GPG_CARDFLAG_REPLACE)) |
if (! (c->keygen.flags & GPG_CARDFLAG_REPLACE)) |
283 |
c->cancel = 1; |
c->cancel = 1; |
284 |
s = c->keygen.flags & GPG_CARDFLAG_REPLACE? "Y" : "N"; |
s = c->keygen.flags & GPG_CARDFLAG_REPLACE? "Y" : "N"; |
285 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
286 |
} |
} |
287 |
else if (!strcmp (key, "keygen.valid")) { |
else if (!strcmp (key, "keygen.valid")) { |
288 |
s = save_write (c->keygen.expdate); |
s = save_write (c->keygen.expdate); |
289 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
290 |
} |
} |
291 |
else if (!strcmp (key, "keygen.name")) { |
else if (!strcmp (key, "keygen.name")) { |
292 |
s = save_write (c->keygen.name); |
s = save_write (c->keygen.name); |
293 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
294 |
} |
} |
295 |
else if (!strcmp (key, "keygen.email")) { |
else if (!strcmp (key, "keygen.email")) { |
296 |
s = save_write (c->keygen.email); |
s = save_write (c->keygen.email); |
297 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
298 |
} |
} |
299 |
else if (!strcmp (key, "keygen.comment")) { |
else if (!strcmp (key, "keygen.comment")) { |
300 |
s = save_write (c->keygen.comment); |
s = save_write (c->keygen.comment); |
301 |
write (fd, s, strlen (s)); |
gpg_write (fd, s, strlen (s)); |
302 |
} |
} |
303 |
break; |
break; |
304 |
|
|
310 |
} |
} |
311 |
|
|
312 |
|
|
313 |
|
/* Check if a GPG status code occured which marks the |
314 |
|
current operation as failed. |
315 |
|
Return value: gpg error constant. */ |
316 |
|
static gpgme_error_t |
317 |
|
map_result (GpgCardEdit *ce) |
318 |
|
{ |
319 |
|
if (!ce->getResult ()) |
320 |
|
return gpg_error (GPG_ERR_NO_ERROR); |
321 |
|
if (ce->getResult () & GPG_CARDRES_NOCARD) |
322 |
|
return gpg_error (GPG_ERR_CARD_REMOVED); |
323 |
|
if (ce->getResult () & GPG_CARDRES_BAD_PIN) |
324 |
|
return gpg_error (GPG_ERR_BAD_PIN); |
325 |
|
return 0; |
326 |
|
} |
327 |
|
|
328 |
|
|
329 |
|
/* Wrapper around the gpgme card interface. |
330 |
|
@ctx: context to use. |
331 |
|
@ce: card edit context. |
332 |
|
Return value: 0 on success. */ |
333 |
gpgme_error_t |
gpgme_error_t |
334 |
gpg_card_edit (gpgme_ctx_t ctx, GpgCardEdit *ce) |
gpg_card_edit (gpgme_ctx_t ctx, GpgCardEdit *ce) |
335 |
{ |
{ |
336 |
|
gpgme_data_t out; |
337 |
|
gpgme_key_t key; |
338 |
gpgme_error_t err; |
gpgme_error_t err; |
339 |
|
|
340 |
|
err = gpgme_data_new (&out); |
341 |
|
if (err) |
342 |
|
return err; |
343 |
|
|
344 |
|
/* XXX: dummy key */ |
345 |
|
key = (gpgme_key_t)calloc (1, sizeof *key); |
346 |
|
key->subkeys = (gpgme_subkey_t)calloc (1, sizeof *key->subkeys); |
347 |
|
key->subkeys->fpr = strdup ("foo"); |
348 |
|
|
349 |
if (ce->type > GPG_EDITCARD_CHPIN_ID) |
if (ce->type > GPG_EDITCARD_CHPIN_ID) |
350 |
err = gpgme_op_card_edit (ctx, NULL, change_pin_handler, ce, NULL); |
err = gpgme_op_card_edit (ctx, key, change_pin_handler, ce, out); |
351 |
else |
else |
352 |
err = gpgme_op_card_edit (ctx, NULL, editcard_handler, ce, NULL); |
err = gpgme_op_card_edit (ctx, key, editcard_handler, ce, out); |
353 |
|
if (!err) |
354 |
|
err = map_result (ce); |
355 |
|
|
356 |
|
if (key) { /* XXX */ |
357 |
|
free (key->subkeys->keyid); |
358 |
|
free (key->subkeys); |
359 |
|
free (key); |
360 |
|
} |
361 |
|
gpgme_data_release (out); |
362 |
return err; |
return err; |
363 |
} |
} |