1 |
twoaday |
23 |
/* wptCardEditCB.cpp - Card callbacks |
2 |
|
|
* Copyright (C) 2003, 2004, 2005 Timo Schulz |
3 |
twoaday |
24 |
* Copyright (C) 2005 g10 Code GmbH |
4 |
twoaday |
23 |
* |
5 |
|
|
* This file is part of WinPT. |
6 |
|
|
* |
7 |
|
|
* WinPT is free software; you can redistribute it and/or modify |
8 |
|
|
* it under the terms of the GNU General Public License as published by |
9 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
* (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* WinPT is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
* GNU General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
twoaday |
24 |
* along with this program; if not, gpg_write to the Free Software Foundation, |
19 |
twoaday |
23 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
20 |
|
|
*/ |
21 |
twoaday |
24 |
#include <windows.h> |
22 |
twoaday |
23 |
#include <stdio.h> |
23 |
|
|
#include <malloc.h> |
24 |
|
|
#include <string.h> |
25 |
|
|
#include <stdlib.h> |
26 |
|
|
|
27 |
twoaday |
25 |
#include "gpgme.h" |
28 |
twoaday |
26 |
#include "wptCard.h" |
29 |
twoaday |
23 |
#include "wptCardEdit.h" |
30 |
twoaday |
26 |
#include "wptErrors.h" |
31 |
twoaday |
23 |
|
32 |
|
|
#define save_write(val) ((val)? (val) : "") |
33 |
twoaday |
26 |
static const char *stat_key = ""; |
34 |
twoaday |
23 |
|
35 |
twoaday |
26 |
/* Wrapper to emulate write(). */ |
36 |
twoaday |
24 |
static int |
37 |
|
|
gpg_write (int fd, const void *buf, size_t buflen) |
38 |
|
|
{ |
39 |
|
|
HANDLE hd = (HANDLE)fd; |
40 |
|
|
DWORD n; |
41 |
twoaday |
23 |
|
42 |
twoaday |
26 |
MessageBox (NULL, (const char*)buf, stat_key, MB_OK); |
43 |
twoaday |
24 |
WriteFile (hd, buf, buflen, &n, NULL); |
44 |
twoaday |
26 |
WriteFile (hd, "\n", 1, &n, NULL); |
45 |
twoaday |
24 |
return n; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
|
49 |
twoaday |
26 |
/* Common card handler. Handle general card status events. */ |
50 |
twoaday |
23 |
static gpgme_error_t |
51 |
twoaday |
26 |
common_card_handler (GpgCardEdit *c, gpgme_status_code_t code, |
52 |
|
|
const char *key, int fd) |
53 |
twoaday |
23 |
{ |
54 |
twoaday |
26 |
const char *s; |
55 |
twoaday |
23 |
|
56 |
twoaday |
27 |
if (!strcmp (key, "cardctrl.insert_card.okay")) { |
57 |
twoaday |
23 |
if (c->card_cb) { |
58 |
twoaday |
28 |
s = c->card_cb (CARD_CTL_INSERT, c->cb_value); |
59 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
60 |
twoaday |
23 |
return 0; |
61 |
|
|
} |
62 |
|
|
} |
63 |
twoaday |
28 |
|
64 |
|
|
if (code == GPGME_STATUS_CARDCTRL && |
65 |
|
|
(*key == CARD_CTL_NO_CARD || *key == CARD_CTL_NO_READER)) |
66 |
twoaday |
26 |
c->setResult (GPG_CARDRES_NOCARD); |
67 |
|
|
|
68 |
|
|
/* This can happen for example if a wrong PIN were |
69 |
|
|
submitted. In such a case, we quit ASAP. */ |
70 |
|
|
if (code == GPGME_STATUS_SC_OP_FAILURE) { |
71 |
twoaday |
28 |
if (key && !strcmp (key, "2")) |
72 |
|
|
c->setResult (GPG_CARDRES_BAD_PIN); |
73 |
|
|
else if (key && !strcmp (key, "1")) |
74 |
|
|
c->setResult (GPG_CARDRES_CANCEL); |
75 |
twoaday |
26 |
s = "quit\n"; |
76 |
|
|
gpg_write (fd, s, strlen (s)); |
77 |
|
|
return 0; |
78 |
|
|
} |
79 |
|
|
return 0; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
|
83 |
|
|
/* handler for changing PINs of cards. */ |
84 |
|
|
static gpgme_error_t |
85 |
|
|
change_pin_handler (void *opaque, gpgme_status_code_t code, |
86 |
|
|
const char *key, int fd) |
87 |
|
|
{ |
88 |
|
|
GpgCardEdit *c = (GpgCardEdit *)opaque; |
89 |
|
|
const char *s = ""; |
90 |
twoaday |
28 |
int type; |
91 |
twoaday |
26 |
|
92 |
|
|
if (!c) |
93 |
|
|
return gpg_error (GPG_ERR_EOF); |
94 |
|
|
|
95 |
|
|
stat_key = key; /* XXX: debug code */ |
96 |
|
|
|
97 |
|
|
common_card_handler (c, code, key, fd); |
98 |
twoaday |
28 |
|
99 |
|
|
type = c->getType (); |
100 |
twoaday |
23 |
if (!strcmp (key, "cardedit.prompt")) { |
101 |
twoaday |
26 |
if (c->cnt == 0) { |
102 |
|
|
c->cnt++; |
103 |
twoaday |
23 |
s = "admin"; |
104 |
twoaday |
26 |
} |
105 |
|
|
else if (c->cnt == 1) { |
106 |
|
|
c->cnt++; |
107 |
twoaday |
23 |
s = "passwd"; |
108 |
twoaday |
26 |
} |
109 |
|
|
else { |
110 |
|
|
c->reset (); |
111 |
twoaday |
23 |
s = "quit"; |
112 |
twoaday |
26 |
} |
113 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
114 |
twoaday |
23 |
return 0; |
115 |
|
|
} |
116 |
|
|
if (!strcmp (key, "cardutil.change_pin.menu")) { |
117 |
twoaday |
26 |
if (c->cnt == 2) { |
118 |
|
|
c->cnt++; |
119 |
twoaday |
28 |
switch (type) { |
120 |
twoaday |
26 |
case GPG_EDITCARD_CHUPIN: s = "1"; break; |
121 |
|
|
case GPG_EDITCARD_UNBPIN: s = "2"; break; |
122 |
|
|
case GPG_EDITCARD_CHAPIN: s = "3"; break; |
123 |
|
|
default: s = "Q"; |
124 |
twoaday |
23 |
} |
125 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
126 |
twoaday |
23 |
return 0; |
127 |
|
|
} |
128 |
|
|
else if (c->cnt > 0) { |
129 |
twoaday |
24 |
gpg_write (fd, "Q", 1); |
130 |
twoaday |
23 |
return 0; |
131 |
|
|
} |
132 |
|
|
} |
133 |
twoaday |
28 |
if (type == GPG_EDITCARD_CHUPIN) { |
134 |
twoaday |
23 |
if (!strcmp (key, "passphrase.pin.ask")) { |
135 |
|
|
s = c->pin; |
136 |
|
|
c->cnt++; |
137 |
|
|
} |
138 |
|
|
if (!strcmp (key, "passphrase.pin.new.ask")) { |
139 |
|
|
s = c->pin_new; |
140 |
|
|
c->cnt++; |
141 |
|
|
} |
142 |
twoaday |
26 |
if (!strcmp (key, "passphrase.pin.repeat") || |
143 |
|
|
!strcmp (key, "passphrase.ask")) { |
144 |
twoaday |
23 |
s = c->pin_new; |
145 |
|
|
c->cnt++; |
146 |
|
|
} |
147 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
148 |
twoaday |
23 |
} |
149 |
twoaday |
28 |
else if (type == GPG_EDITCARD_CHAPIN) { |
150 |
twoaday |
23 |
if (!strcmp (key, "passphrase.adminpin.ask")) { |
151 |
twoaday |
26 |
s = c->admin_pin; |
152 |
twoaday |
23 |
c->cnt++; |
153 |
|
|
} |
154 |
|
|
if (!strcmp (key, "passphrase.adminpin.new.ask")) { |
155 |
|
|
s = c->pin_new; |
156 |
|
|
c->cnt++; |
157 |
|
|
} |
158 |
twoaday |
26 |
if (!strcmp (key, "passphrase.pin.repeat") || |
159 |
|
|
!strcmp (key, "passphrase.ask")) { |
160 |
twoaday |
23 |
s = c->pin_new; |
161 |
|
|
c->cnt++; |
162 |
|
|
} |
163 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
164 |
twoaday |
23 |
} |
165 |
twoaday |
28 |
else if (type == GPG_EDITCARD_UNBPIN) { |
166 |
twoaday |
23 |
/* todo */ |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
return 0; |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
|
173 |
twoaday |
26 |
/* edit card attributes handler. */ |
174 |
twoaday |
23 |
static gpgme_error_t |
175 |
|
|
editcard_handler (void * opaque, gpgme_status_code_t code, |
176 |
|
|
const char *key, int fd) |
177 |
|
|
{ |
178 |
|
|
GpgCardEdit *c = (GpgCardEdit *)opaque; |
179 |
|
|
const char *s = ""; |
180 |
twoaday |
28 |
int type; |
181 |
twoaday |
23 |
|
182 |
|
|
if (!c) |
183 |
|
|
return gpg_error (GPG_ERR_EOF); |
184 |
twoaday |
26 |
|
185 |
|
|
stat_key = key; |
186 |
|
|
|
187 |
|
|
common_card_handler (c, code, key, fd); |
188 |
|
|
|
189 |
twoaday |
28 |
type = c->getType (); |
190 |
twoaday |
26 |
if (!strcmp (key, "cardedit.prompt")) { |
191 |
|
|
if (c->getResult ()) { /* in case of an error we quit. */ |
192 |
|
|
c->cnt = 0; |
193 |
|
|
s = "quit"; |
194 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
195 |
twoaday |
23 |
return 0; |
196 |
|
|
} |
197 |
twoaday |
26 |
if (c->cnt == 0) { /* first switch in the admin mode */ |
198 |
|
|
c->cnt++; |
199 |
twoaday |
23 |
s = "admin"; |
200 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
201 |
twoaday |
23 |
return 0; |
202 |
|
|
} |
203 |
|
|
if (c->cnt == 1) {/* then run the send command */ |
204 |
|
|
c->cnt++; |
205 |
twoaday |
28 |
switch (type) { |
206 |
twoaday |
26 |
case GPG_EDITCARD_NAME: s = "name"; break; |
207 |
|
|
case GPG_EDITCARD_KEYURL: s = "url"; break; |
208 |
|
|
case GPG_EDITCARD_LOGIN: s = "login"; break; |
209 |
|
|
case GPG_EDITCARD_SEX: s = "sex"; break; |
210 |
|
|
case GPG_EDITCARD_LANG: s = "lang"; break; |
211 |
|
|
case GPG_EDITCARD_GENKEY: s = "generate"; break; |
212 |
twoaday |
23 |
default: s = "quit"; |
213 |
|
|
} |
214 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
215 |
twoaday |
23 |
return 0; |
216 |
|
|
} |
217 |
|
|
else if (c->cnt >= 2) {/* done: send exit */ |
218 |
twoaday |
26 |
c->reset (); |
219 |
twoaday |
23 |
s = "quit"; |
220 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
221 |
twoaday |
23 |
return 0; |
222 |
|
|
} |
223 |
|
|
} |
224 |
|
|
if (c->cnt > 0) { |
225 |
|
|
if (!strcmp (key, "passphrase.adminpin.ask")) { |
226 |
twoaday |
24 |
gpg_write (fd, c->admin_pin, strlen (c->admin_pin)); |
227 |
twoaday |
23 |
return 0; |
228 |
|
|
} |
229 |
|
|
if (!strcmp (key, "passphrase.pin.ask")) { |
230 |
twoaday |
24 |
gpg_write (fd, c->pin, strlen (c->pin)); |
231 |
twoaday |
23 |
return 0; |
232 |
|
|
} |
233 |
|
|
} |
234 |
twoaday |
28 |
switch (type) { |
235 |
twoaday |
23 |
case GPG_EDITCARD_NAME: |
236 |
|
|
if (!strcmp (key, "keygen.smartcard.surname")) { |
237 |
|
|
s = save_write (c->edit.surname); |
238 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
239 |
twoaday |
23 |
} |
240 |
|
|
else if (!strcmp (key, "keygen.smartcard.givenname")) { |
241 |
|
|
s = save_write (c->edit.givenname); |
242 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
243 |
twoaday |
23 |
} |
244 |
|
|
break; |
245 |
|
|
|
246 |
|
|
case GPG_EDITCARD_KEYURL: |
247 |
|
|
if (!strcmp (key, "cardedit.change_url")) { |
248 |
|
|
s = save_write (c->edit.keyurl); |
249 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
250 |
twoaday |
23 |
} |
251 |
|
|
break; |
252 |
|
|
|
253 |
|
|
case GPG_EDITCARD_LOGIN: |
254 |
|
|
if (!strcmp (key, "cardedit.change_login")) { |
255 |
|
|
s = save_write (c->edit.login); |
256 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
257 |
twoaday |
23 |
} |
258 |
|
|
break; |
259 |
|
|
|
260 |
|
|
case GPG_EDITCARD_SEX: |
261 |
|
|
if (!strcmp (key, "cardedit.change_sex")) { |
262 |
|
|
static char buf[2]; |
263 |
|
|
if (c->edit.sex != 'M' |
264 |
|
|
&& c->edit.sex != 'F' |
265 |
|
|
&& c->edit.sex != ' ') { |
266 |
|
|
buf[0] = ' '; buf[1] = 0; |
267 |
|
|
} |
268 |
|
|
else { |
269 |
twoaday |
26 |
buf[0] = c->edit.sex; |
270 |
|
|
buf[1] = 0; |
271 |
twoaday |
23 |
} |
272 |
twoaday |
24 |
gpg_write (fd, buf, strlen (buf)); |
273 |
twoaday |
23 |
} |
274 |
|
|
break; |
275 |
|
|
|
276 |
|
|
case GPG_EDITCARD_LANG: |
277 |
|
|
if (!strcmp (key, "cardedit.change_lang")) { |
278 |
|
|
s = save_write (c->edit.lang); |
279 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
280 |
twoaday |
23 |
} |
281 |
|
|
break; |
282 |
|
|
|
283 |
|
|
case GPG_EDITCARD_GENKEY: |
284 |
|
|
if (!strcmp (key, "passphrase.enter")) |
285 |
twoaday |
24 |
gpg_write (fd, c->keygen.pass, strlen (c->keygen.pass)); |
286 |
twoaday |
23 |
if (!strcmp (key, "cardedit.genkeys.backup_enc")) { |
287 |
|
|
s = c->keygen.flags & GPG_CARDFLAG_BAKENC? "Y" : "N"; |
288 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
289 |
twoaday |
23 |
} |
290 |
|
|
if (!strcmp (key, "cardedit.genkeys.replace_keys")) { |
291 |
|
|
if (! (c->keygen.flags & GPG_CARDFLAG_REPLACE)) |
292 |
|
|
c->cancel = 1; |
293 |
|
|
s = c->keygen.flags & GPG_CARDFLAG_REPLACE? "Y" : "N"; |
294 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
295 |
twoaday |
23 |
} |
296 |
|
|
else if (!strcmp (key, "keygen.valid")) { |
297 |
|
|
s = save_write (c->keygen.expdate); |
298 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
299 |
twoaday |
23 |
} |
300 |
|
|
else if (!strcmp (key, "keygen.name")) { |
301 |
|
|
s = save_write (c->keygen.name); |
302 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
303 |
twoaday |
23 |
} |
304 |
|
|
else if (!strcmp (key, "keygen.email")) { |
305 |
|
|
s = save_write (c->keygen.email); |
306 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
307 |
twoaday |
23 |
} |
308 |
|
|
else if (!strcmp (key, "keygen.comment")) { |
309 |
|
|
s = save_write (c->keygen.comment); |
310 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
311 |
twoaday |
23 |
} |
312 |
|
|
break; |
313 |
|
|
|
314 |
|
|
default: |
315 |
|
|
break; |
316 |
|
|
} |
317 |
|
|
|
318 |
|
|
return 0; |
319 |
|
|
} |
320 |
|
|
|
321 |
|
|
|
322 |
twoaday |
26 |
/* Check if a GPG status code occured which marks the |
323 |
|
|
current operation as failed. |
324 |
|
|
Return value: gpg error constant. */ |
325 |
|
|
static gpgme_error_t |
326 |
|
|
map_result (GpgCardEdit *ce) |
327 |
|
|
{ |
328 |
|
|
if (!ce->getResult ()) |
329 |
|
|
return gpg_error (GPG_ERR_NO_ERROR); |
330 |
|
|
if (ce->getResult () & GPG_CARDRES_NOCARD) |
331 |
twoaday |
27 |
return gpg_error (GPG_ERR_CARD_NOT_PRESENT); |
332 |
twoaday |
26 |
if (ce->getResult () & GPG_CARDRES_BAD_PIN) |
333 |
|
|
return gpg_error (GPG_ERR_BAD_PIN); |
334 |
|
|
return 0; |
335 |
|
|
} |
336 |
|
|
|
337 |
|
|
|
338 |
|
|
/* Wrapper around the gpgme card interface. |
339 |
|
|
@ctx: context to use. |
340 |
|
|
@ce: card edit context. |
341 |
|
|
Return value: 0 on success. */ |
342 |
twoaday |
23 |
gpgme_error_t |
343 |
|
|
gpg_card_edit (gpgme_ctx_t ctx, GpgCardEdit *ce) |
344 |
|
|
{ |
345 |
twoaday |
26 |
gpgme_data_t out; |
346 |
twoaday |
23 |
gpgme_error_t err; |
347 |
|
|
|
348 |
twoaday |
26 |
err = gpgme_data_new (&out); |
349 |
|
|
if (err) |
350 |
|
|
return err; |
351 |
|
|
|
352 |
twoaday |
28 |
if (ce->getType () > GPG_EDITCARD_CHPIN_ID) |
353 |
twoaday |
27 |
err = gpgme_op_card_edit (ctx, NULL, change_pin_handler, ce, out); |
354 |
twoaday |
23 |
else |
355 |
twoaday |
27 |
err = gpgme_op_card_edit (ctx, NULL, editcard_handler, ce, out); |
356 |
twoaday |
26 |
if (!err) |
357 |
|
|
err = map_result (ce); |
358 |
|
|
|
359 |
|
|
gpgme_data_release (out); |
360 |
twoaday |
23 |
return err; |
361 |
|
|
} |