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 |
|
|
|
22 |
twoaday |
24 |
#include <windows.h> |
23 |
twoaday |
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 |
twoaday |
24 |
|
33 |
twoaday |
23 |
#define save_write(val) ((val)? (val) : "") |
34 |
|
|
|
35 |
|
|
|
36 |
twoaday |
24 |
/* 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 |
twoaday |
23 |
|
43 |
twoaday |
24 |
WriteFile (hd, buf, buflen, &n, NULL); |
44 |
|
|
return n; |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
|
48 |
twoaday |
23 |
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 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
61 |
twoaday |
23 |
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 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
72 |
twoaday |
23 |
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 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
83 |
twoaday |
23 |
return 0; |
84 |
|
|
} |
85 |
|
|
else if (c->cnt > 0) { |
86 |
|
|
c->cnt = 0; |
87 |
twoaday |
24 |
gpg_write (fd, "Q", 1); |
88 |
twoaday |
23 |
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 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
105 |
twoaday |
23 |
} |
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 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
120 |
twoaday |
23 |
} |
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 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
143 |
twoaday |
23 |
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 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
151 |
twoaday |
23 |
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 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
165 |
twoaday |
23 |
return 0; |
166 |
|
|
} |
167 |
|
|
else if (c->cnt >= 2) {/* done: send exit */ |
168 |
|
|
c->cnt = 0; |
169 |
|
|
s = "quit"; |
170 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
171 |
twoaday |
23 |
return 0; |
172 |
|
|
} |
173 |
|
|
} |
174 |
|
|
if (c->cnt > 0) { |
175 |
|
|
if (!strcmp (key, "passphrase.adminpin.ask")) { |
176 |
twoaday |
24 |
gpg_write (fd, c->admin_pin, strlen (c->admin_pin)); |
177 |
twoaday |
23 |
return 0; |
178 |
|
|
} |
179 |
|
|
if (!strcmp (key, "passphrase.pin.ask")) { |
180 |
twoaday |
24 |
gpg_write (fd, c->pin, strlen (c->pin)); |
181 |
twoaday |
23 |
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 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
189 |
twoaday |
23 |
} |
190 |
|
|
else if (!strcmp (key, "keygen.smartcard.givenname")) { |
191 |
|
|
s = save_write (c->edit.givenname); |
192 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
193 |
twoaday |
23 |
} |
194 |
|
|
break; |
195 |
|
|
|
196 |
|
|
case GPG_EDITCARD_KEYURL: |
197 |
|
|
if (!strcmp (key, "cardedit.change_url")) { |
198 |
|
|
s = save_write (c->edit.keyurl); |
199 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
200 |
twoaday |
23 |
} |
201 |
|
|
break; |
202 |
|
|
|
203 |
|
|
case GPG_EDITCARD_LOGIN: |
204 |
|
|
if (!strcmp (key, "cardedit.change_login")) { |
205 |
|
|
s = save_write (c->edit.login); |
206 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
207 |
twoaday |
23 |
} |
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 |
twoaday |
24 |
gpg_write (fd, buf, strlen (buf)); |
222 |
twoaday |
23 |
} |
223 |
|
|
break; |
224 |
|
|
|
225 |
|
|
case GPG_EDITCARD_LANG: |
226 |
|
|
if (!strcmp (key, "cardedit.change_lang")) { |
227 |
|
|
s = save_write (c->edit.lang); |
228 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
229 |
twoaday |
23 |
} |
230 |
|
|
break; |
231 |
|
|
|
232 |
|
|
case GPG_EDITCARD_GENKEY: |
233 |
|
|
if (!strcmp (key, "passphrase.enter")) |
234 |
twoaday |
24 |
gpg_write (fd, c->keygen.pass, strlen (c->keygen.pass)); |
235 |
twoaday |
23 |
if (!strcmp (key, "cardedit.genkeys.backup_enc")) { |
236 |
|
|
s = c->keygen.flags & GPG_CARDFLAG_BAKENC? "Y" : "N"; |
237 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
238 |
twoaday |
23 |
} |
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 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
244 |
twoaday |
23 |
} |
245 |
|
|
else if (!strcmp (key, "keygen.valid")) { |
246 |
|
|
s = save_write (c->keygen.expdate); |
247 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
248 |
twoaday |
23 |
} |
249 |
|
|
else if (!strcmp (key, "keygen.name")) { |
250 |
|
|
s = save_write (c->keygen.name); |
251 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
252 |
twoaday |
23 |
} |
253 |
|
|
else if (!strcmp (key, "keygen.email")) { |
254 |
|
|
s = save_write (c->keygen.email); |
255 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
256 |
twoaday |
23 |
} |
257 |
|
|
else if (!strcmp (key, "keygen.comment")) { |
258 |
|
|
s = save_write (c->keygen.comment); |
259 |
twoaday |
24 |
gpg_write (fd, s, strlen (s)); |
260 |
twoaday |
23 |
} |
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 |
|
|
} |