/[winpt]/trunk/Src/wptCardEditCB.cpp
ViewVC logotype

Annotation of /trunk/Src/wptCardEditCB.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26