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

Contents of /trunk/Src/wptCardEditCB.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (show annotations)
Sat Oct 8 10:43:08 2005 UTC (19 years, 4 months ago) by twoaday
File size: 7242 byte(s)
Bug fixes to correct some problems introduced by
the MyGPGME to GPGME port.

1 /* 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
22 #include <windows.h>
23 #include <stdio.h>
24 #include <malloc.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include "w32gpgme.h"
29 #include "wptCardEdit.h"
30 #include "wptCard.h"
31
32
33 #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
49 change_pin_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd)
50 {
51 GpgCardEdit *c = (GpgCardEdit *)opaque;
52 const char *s = "";
53
54 if (!c)
55 return gpg_error (GPG_ERR_EOF);
56
57 if (!strcmp (key, "cardctrl.insert_card_okay")) {
58 if (c->card_cb) {
59 s = c->card_cb (1, c->cb_value);
60 gpg_write (fd, s, strlen (s));
61 return 0;
62 }
63 }
64 if (!strcmp (key, "cardedit.prompt")) {
65 if (c->cnt++ == 0)
66 s = "admin";
67 else if (c->cnt++ == 1)
68 s = "passwd";
69 else
70 s = "quit";
71 gpg_write (fd, s, strlen (s));
72 return 0;
73 }
74 if (!strcmp (key, "cardutil.change_pin.menu")) {
75 if (c->cnt++ == 2) {
76 switch (c->type) {
77 case GPG_EDITCARD_CHUPIN: s = "1";
78 case GPG_EDITCARD_UNBPIN: s = "2";
79 case GPG_EDITCARD_CHAPIN: s = "3";
80 default: c->cnt = 0; s = "Q";
81 }
82 gpg_write (fd, s, strlen (s));
83 return 0;
84 }
85 else if (c->cnt > 0) {
86 c->cnt = 0;
87 gpg_write (fd, "Q", 1);
88 return 0;
89 }
90 }
91 if (c->type == GPG_EDITCARD_CHUPIN) {
92 if (!strcmp (key, "passphrase.pin.ask")) {
93 s = c->pin;
94 c->cnt++;
95 }
96 if (!strcmp (key, "passphrase.pin.new.ask")) {
97 s = c->pin_new;
98 c->cnt++;
99 }
100 if (!strcmp (key, "passphrase.pin.repeat")) {
101 s = c->pin_new;
102 c->cnt++;
103 }
104 gpg_write (fd, s, strlen (s));
105 }
106 else if (c->type == GPG_EDITCARD_CHAPIN) {
107 if (!strcmp (key, "passphrase.adminpin.ask")) {
108 s = c->pin;
109 c->cnt++;
110 }
111 if (!strcmp (key, "passphrase.adminpin.new.ask")) {
112 s = c->pin_new;
113 c->cnt++;
114 }
115 if (!strcmp (key, "passphrase.pin.repeat")) {
116 s = c->pin_new;
117 c->cnt++;
118 }
119 gpg_write (fd, s, strlen (s));
120 }
121 else if (c->type == GPG_EDITCARD_UNBPIN) {
122 /* todo */
123 }
124
125 return 0;
126 }
127
128
129 static gpgme_error_t
130 editcard_handler (void * opaque, gpgme_status_code_t code,
131 const char *key, int fd)
132 {
133 GpgCardEdit *c = (GpgCardEdit *)opaque;
134 const char *s = "";
135
136 if (!c)
137 return gpg_error (GPG_ERR_EOF);
138
139 if (!strcmp (key, "cardctrl.insert_card.okay")) {
140 if (c->card_cb) {
141 s = c->card_cb (1, c->cb_value);
142 gpg_write (fd, s, strlen (s));
143 return 0;
144 }
145 }
146
147 if (!strcmp (key, "cardedit.prompt")) {
148 if (c->cnt++ == 0) { /* first switch in the admin mode */
149 s = "admin";
150 gpg_write (fd, s, strlen (s));
151 return 0;
152 }
153 if (c->cnt == 1) {/* then run the send command */
154 c->cnt++;
155 switch (c->type) {
156 case GPG_EDITCARD_NAME: s = "name";
157 case GPG_EDITCARD_KEYURL: s = "url";
158 case GPG_EDITCARD_LOGIN: s = "login";
159 case GPG_EDITCARD_SEX: s = "sex";
160 case GPG_EDITCARD_LANG: s = "lang";
161 case GPG_EDITCARD_GENKEY: s = "generate";
162 default: s = "quit";
163 }
164 gpg_write (fd, s, strlen (s));
165 return 0;
166 }
167 else if (c->cnt >= 2) {/* done: send exit */
168 c->cnt = 0;
169 s = "quit";
170 gpg_write (fd, s, strlen (s));
171 return 0;
172 }
173 }
174 if (c->cnt > 0) {
175 if (!strcmp (key, "passphrase.adminpin.ask")) {
176 gpg_write (fd, c->admin_pin, strlen (c->admin_pin));
177 return 0;
178 }
179 if (!strcmp (key, "passphrase.pin.ask")) {
180 gpg_write (fd, c->pin, strlen (c->pin));
181 return 0;
182 }
183 }
184 switch (c->type) {
185 case GPG_EDITCARD_NAME:
186 if (!strcmp (key, "keygen.smartcard.surname")) {
187 s = save_write (c->edit.surname);
188 gpg_write (fd, s, strlen (s));
189 }
190 else if (!strcmp (key, "keygen.smartcard.givenname")) {
191 s = save_write (c->edit.givenname);
192 gpg_write (fd, s, strlen (s));
193 }
194 break;
195
196 case GPG_EDITCARD_KEYURL:
197 if (!strcmp (key, "cardedit.change_url")) {
198 s = save_write (c->edit.keyurl);
199 gpg_write (fd, s, strlen (s));
200 }
201 break;
202
203 case GPG_EDITCARD_LOGIN:
204 if (!strcmp (key, "cardedit.change_login")) {
205 s = save_write (c->edit.login);
206 gpg_write (fd, s, strlen (s));
207 }
208 break;
209
210 case GPG_EDITCARD_SEX:
211 if (!strcmp (key, "cardedit.change_sex")) {
212 static char buf[2];
213 if (c->edit.sex != 'M'
214 && c->edit.sex != 'F'
215 && c->edit.sex != ' ') {
216 buf[0] = ' '; buf[1] = 0;
217 }
218 else {
219 buf[0] = c->edit.sex; buf[1] = 0;
220 }
221 gpg_write (fd, buf, strlen (buf));
222 }
223 break;
224
225 case GPG_EDITCARD_LANG:
226 if (!strcmp (key, "cardedit.change_lang")) {
227 s = save_write (c->edit.lang);
228 gpg_write (fd, s, strlen (s));
229 }
230 break;
231
232 case GPG_EDITCARD_GENKEY:
233 if (!strcmp (key, "passphrase.enter"))
234 gpg_write (fd, c->keygen.pass, strlen (c->keygen.pass));
235 if (!strcmp (key, "cardedit.genkeys.backup_enc")) {
236 s = c->keygen.flags & GPG_CARDFLAG_BAKENC? "Y" : "N";
237 gpg_write (fd, s, strlen (s));
238 }
239 if (!strcmp (key, "cardedit.genkeys.replace_keys")) {
240 if (! (c->keygen.flags & GPG_CARDFLAG_REPLACE))
241 c->cancel = 1;
242 s = c->keygen.flags & GPG_CARDFLAG_REPLACE? "Y" : "N";
243 gpg_write (fd, s, strlen (s));
244 }
245 else if (!strcmp (key, "keygen.valid")) {
246 s = save_write (c->keygen.expdate);
247 gpg_write (fd, s, strlen (s));
248 }
249 else if (!strcmp (key, "keygen.name")) {
250 s = save_write (c->keygen.name);
251 gpg_write (fd, s, strlen (s));
252 }
253 else if (!strcmp (key, "keygen.email")) {
254 s = save_write (c->keygen.email);
255 gpg_write (fd, s, strlen (s));
256 }
257 else if (!strcmp (key, "keygen.comment")) {
258 s = save_write (c->keygen.comment);
259 gpg_write (fd, s, strlen (s));
260 }
261 break;
262
263 default:
264 break;
265 }
266
267 return 0;
268 }
269
270
271 gpgme_error_t
272 gpg_card_edit (gpgme_ctx_t ctx, GpgCardEdit *ce)
273 {
274 gpgme_error_t err;
275
276 if (ce->type > GPG_EDITCARD_CHPIN_ID)
277 err = gpgme_op_card_edit (ctx, NULL, change_pin_handler, ce, NULL);
278 else
279 err = gpgme_op_card_edit (ctx, NULL, editcard_handler, ce, NULL);
280 return err;
281 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26