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