]> git.proxmox.com Git - mirror_qemu.git/blob - hw/usb/ccid-card-passthru.c
Include qemu/module.h where needed, drop it from qemu-common.h
[mirror_qemu.git] / hw / usb / ccid-card-passthru.c
1 /*
2 * CCID Passthru Card Device emulation
3 *
4 * Copyright (c) 2011 Red Hat.
5 * Written by Alon Levy.
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2.1 or later.
8 * See the COPYING file in the top-level directory.
9 */
10
11 #include "qemu/osdep.h"
12 #include "qemu/units.h"
13 #include <libcacard.h>
14 #include "chardev/char-fe.h"
15 #include "qemu/error-report.h"
16 #include "qemu/module.h"
17 #include "qemu/sockets.h"
18 #include "ccid.h"
19 #include "qapi/error.h"
20
21 #define DPRINTF(card, lvl, fmt, ...) \
22 do { \
23 if (lvl <= card->debug) { \
24 printf("ccid-card-passthru: " fmt , ## __VA_ARGS__); \
25 } \
26 } while (0)
27
28 #define D_WARN 1
29 #define D_INFO 2
30 #define D_MORE_INFO 3
31 #define D_VERBOSE 4
32
33 /* TODO: do we still need this? */
34 static const uint8_t DEFAULT_ATR[] = {
35 /*
36 * From some example somewhere
37 * 0x3B, 0xB0, 0x18, 0x00, 0xD1, 0x81, 0x05, 0xB1, 0x40, 0x38, 0x1F, 0x03, 0x28
38 */
39
40 /* From an Athena smart card */
41 0x3B, 0xD5, 0x18, 0xFF, 0x80, 0x91, 0xFE, 0x1F, 0xC3, 0x80, 0x73, 0xC8, 0x21,
42 0x13, 0x08
43 };
44
45 #define VSCARD_IN_SIZE (64 * KiB)
46
47 /* maximum size of ATR - from 7816-3 */
48 #define MAX_ATR_SIZE 40
49
50 typedef struct PassthruState PassthruState;
51
52 struct PassthruState {
53 CCIDCardState base;
54 CharBackend cs;
55 uint8_t vscard_in_data[VSCARD_IN_SIZE];
56 uint32_t vscard_in_pos;
57 uint32_t vscard_in_hdr;
58 uint8_t atr[MAX_ATR_SIZE];
59 uint8_t atr_length;
60 uint8_t debug;
61 };
62
63 #define TYPE_CCID_PASSTHRU "ccid-card-passthru"
64 #define PASSTHRU_CCID_CARD(obj) \
65 OBJECT_CHECK(PassthruState, (obj), TYPE_CCID_PASSTHRU)
66
67 /*
68 * VSCard protocol over chardev
69 * This code should not depend on the card type.
70 */
71
72 static void ccid_card_vscard_send_msg(PassthruState *s,
73 VSCMsgType type, uint32_t reader_id,
74 const uint8_t *payload, uint32_t length)
75 {
76 VSCMsgHeader scr_msg_header;
77
78 scr_msg_header.type = htonl(type);
79 scr_msg_header.reader_id = htonl(reader_id);
80 scr_msg_header.length = htonl(length);
81 /* XXX this blocks entire thread. Rewrite to use
82 * qemu_chr_fe_write and background I/O callbacks */
83 qemu_chr_fe_write_all(&s->cs, (uint8_t *)&scr_msg_header,
84 sizeof(VSCMsgHeader));
85 qemu_chr_fe_write_all(&s->cs, payload, length);
86 }
87
88 static void ccid_card_vscard_send_apdu(PassthruState *s,
89 const uint8_t *apdu, uint32_t length)
90 {
91 ccid_card_vscard_send_msg(
92 s, VSC_APDU, VSCARD_MINIMAL_READER_ID, apdu, length);
93 }
94
95 static void ccid_card_vscard_send_error(PassthruState *s,
96 uint32_t reader_id, VSCErrorCode code)
97 {
98 VSCMsgError msg = {.code = htonl(code)};
99
100 ccid_card_vscard_send_msg(
101 s, VSC_Error, reader_id, (uint8_t *)&msg, sizeof(msg));
102 }
103
104 static void ccid_card_vscard_send_init(PassthruState *s)
105 {
106 VSCMsgInit msg = {
107 .version = htonl(VSCARD_VERSION),
108 .magic = VSCARD_MAGIC,
109 .capabilities = {0}
110 };
111
112 ccid_card_vscard_send_msg(s, VSC_Init, VSCARD_UNDEFINED_READER_ID,
113 (uint8_t *)&msg, sizeof(msg));
114 }
115
116 static int ccid_card_vscard_can_read(void *opaque)
117 {
118 PassthruState *card = opaque;
119
120 return VSCARD_IN_SIZE >= card->vscard_in_pos ?
121 VSCARD_IN_SIZE - card->vscard_in_pos : 0;
122 }
123
124 static void ccid_card_vscard_handle_init(
125 PassthruState *card, VSCMsgHeader *hdr, VSCMsgInit *init)
126 {
127 uint32_t *capabilities;
128 int num_capabilities;
129 int i;
130
131 capabilities = init->capabilities;
132 num_capabilities =
133 1 + ((hdr->length - sizeof(VSCMsgInit)) / sizeof(uint32_t));
134 init->version = ntohl(init->version);
135 for (i = 0 ; i < num_capabilities; ++i) {
136 capabilities[i] = ntohl(capabilities[i]);
137 }
138 if (init->magic != VSCARD_MAGIC) {
139 error_report("wrong magic");
140 /* we can't disconnect the chardev */
141 }
142 if (init->version != VSCARD_VERSION) {
143 DPRINTF(card, D_WARN,
144 "got version %d, have %d", init->version, VSCARD_VERSION);
145 }
146 /* future handling of capabilities, none exist atm */
147 ccid_card_vscard_send_init(card);
148 }
149
150 static int check_atr(PassthruState *card, uint8_t *data, int len)
151 {
152 int historical_length, opt_bytes;
153 int td_count = 0;
154 int td;
155
156 if (len < 2) {
157 return 0;
158 }
159 historical_length = data[1] & 0xf;
160 opt_bytes = 0;
161 if (data[0] != 0x3b && data[0] != 0x3f) {
162 DPRINTF(card, D_WARN, "atr's T0 is 0x%X, not in {0x3b, 0x3f}\n",
163 data[0]);
164 return 0;
165 }
166 td_count = 0;
167 td = data[1] >> 4;
168 while (td && td_count < 2 && opt_bytes + historical_length + 2 < len) {
169 td_count++;
170 if (td & 0x1) {
171 opt_bytes++;
172 }
173 if (td & 0x2) {
174 opt_bytes++;
175 }
176 if (td & 0x4) {
177 opt_bytes++;
178 }
179 if (td & 0x8) {
180 opt_bytes++;
181 td = data[opt_bytes + 2] >> 4;
182 }
183 }
184 if (len < 2 + historical_length + opt_bytes) {
185 DPRINTF(card, D_WARN,
186 "atr too short: len %d, but historical_len %d, T1 0x%X\n",
187 len, historical_length, data[1]);
188 return 0;
189 }
190 if (len > 2 + historical_length + opt_bytes) {
191 DPRINTF(card, D_WARN,
192 "atr too long: len %d, but hist/opt %d/%d, T1 0x%X\n",
193 len, historical_length, opt_bytes, data[1]);
194 /* let it through */
195 }
196 DPRINTF(card, D_VERBOSE,
197 "atr passes check: %d total length, %d historical, %d optional\n",
198 len, historical_length, opt_bytes);
199
200 return 1;
201 }
202
203 static void ccid_card_vscard_handle_message(PassthruState *card,
204 VSCMsgHeader *scr_msg_header)
205 {
206 uint8_t *data = (uint8_t *)&scr_msg_header[1];
207
208 switch (scr_msg_header->type) {
209 case VSC_ATR:
210 DPRINTF(card, D_INFO, "VSC_ATR %d\n", scr_msg_header->length);
211 if (scr_msg_header->length > MAX_ATR_SIZE) {
212 error_report("ATR size exceeds spec, ignoring");
213 ccid_card_vscard_send_error(card, scr_msg_header->reader_id,
214 VSC_GENERAL_ERROR);
215 break;
216 }
217 if (!check_atr(card, data, scr_msg_header->length)) {
218 error_report("ATR is inconsistent, ignoring");
219 ccid_card_vscard_send_error(card, scr_msg_header->reader_id,
220 VSC_GENERAL_ERROR);
221 break;
222 }
223 memcpy(card->atr, data, scr_msg_header->length);
224 card->atr_length = scr_msg_header->length;
225 ccid_card_card_inserted(&card->base);
226 ccid_card_vscard_send_error(card, scr_msg_header->reader_id,
227 VSC_SUCCESS);
228 break;
229 case VSC_APDU:
230 ccid_card_send_apdu_to_guest(
231 &card->base, data, scr_msg_header->length);
232 break;
233 case VSC_CardRemove:
234 DPRINTF(card, D_INFO, "VSC_CardRemove\n");
235 ccid_card_card_removed(&card->base);
236 ccid_card_vscard_send_error(card,
237 scr_msg_header->reader_id, VSC_SUCCESS);
238 break;
239 case VSC_Init:
240 ccid_card_vscard_handle_init(
241 card, scr_msg_header, (VSCMsgInit *)data);
242 break;
243 case VSC_Error:
244 ccid_card_card_error(&card->base, *(uint32_t *)data);
245 break;
246 case VSC_ReaderAdd:
247 if (ccid_card_ccid_attach(&card->base) < 0) {
248 ccid_card_vscard_send_error(card, VSCARD_UNDEFINED_READER_ID,
249 VSC_CANNOT_ADD_MORE_READERS);
250 } else {
251 ccid_card_vscard_send_error(card, VSCARD_MINIMAL_READER_ID,
252 VSC_SUCCESS);
253 }
254 break;
255 case VSC_ReaderRemove:
256 ccid_card_ccid_detach(&card->base);
257 ccid_card_vscard_send_error(card,
258 scr_msg_header->reader_id, VSC_SUCCESS);
259 break;
260 default:
261 printf("usb-ccid: chardev: unexpected message of type %X\n",
262 scr_msg_header->type);
263 ccid_card_vscard_send_error(card, scr_msg_header->reader_id,
264 VSC_GENERAL_ERROR);
265 }
266 }
267
268 static void ccid_card_vscard_drop_connection(PassthruState *card)
269 {
270 qemu_chr_fe_deinit(&card->cs, true);
271 card->vscard_in_pos = card->vscard_in_hdr = 0;
272 }
273
274 static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size)
275 {
276 PassthruState *card = opaque;
277 VSCMsgHeader *hdr;
278
279 if (card->vscard_in_pos + size > VSCARD_IN_SIZE) {
280 error_report("no room for data: pos %u + size %d > %" PRId64 "."
281 " dropping connection.",
282 card->vscard_in_pos, size, VSCARD_IN_SIZE);
283 ccid_card_vscard_drop_connection(card);
284 return;
285 }
286 assert(card->vscard_in_pos < VSCARD_IN_SIZE);
287 assert(card->vscard_in_hdr < VSCARD_IN_SIZE);
288 memcpy(card->vscard_in_data + card->vscard_in_pos, buf, size);
289 card->vscard_in_pos += size;
290 hdr = (VSCMsgHeader *)(card->vscard_in_data + card->vscard_in_hdr);
291
292 while ((card->vscard_in_pos - card->vscard_in_hdr >= sizeof(VSCMsgHeader))
293 &&(card->vscard_in_pos - card->vscard_in_hdr >=
294 sizeof(VSCMsgHeader) + ntohl(hdr->length))) {
295 hdr->reader_id = ntohl(hdr->reader_id);
296 hdr->length = ntohl(hdr->length);
297 hdr->type = ntohl(hdr->type);
298 ccid_card_vscard_handle_message(card, hdr);
299 card->vscard_in_hdr += hdr->length + sizeof(VSCMsgHeader);
300 hdr = (VSCMsgHeader *)(card->vscard_in_data + card->vscard_in_hdr);
301 }
302 if (card->vscard_in_hdr == card->vscard_in_pos) {
303 card->vscard_in_pos = card->vscard_in_hdr = 0;
304 }
305 }
306
307 static void ccid_card_vscard_event(void *opaque, int event)
308 {
309 PassthruState *card = opaque;
310
311 switch (event) {
312 case CHR_EVENT_BREAK:
313 card->vscard_in_pos = card->vscard_in_hdr = 0;
314 break;
315 case CHR_EVENT_OPENED:
316 DPRINTF(card, D_INFO, "%s: CHR_EVENT_OPENED\n", __func__);
317 break;
318 }
319 }
320
321 /* End VSCard handling */
322
323 static void passthru_apdu_from_guest(
324 CCIDCardState *base, const uint8_t *apdu, uint32_t len)
325 {
326 PassthruState *card = PASSTHRU_CCID_CARD(base);
327
328 if (!qemu_chr_fe_backend_connected(&card->cs)) {
329 printf("ccid-passthru: no chardev, discarding apdu length %d\n", len);
330 return;
331 }
332 ccid_card_vscard_send_apdu(card, apdu, len);
333 }
334
335 static const uint8_t *passthru_get_atr(CCIDCardState *base, uint32_t *len)
336 {
337 PassthruState *card = PASSTHRU_CCID_CARD(base);
338
339 *len = card->atr_length;
340 return card->atr;
341 }
342
343 static void passthru_realize(CCIDCardState *base, Error **errp)
344 {
345 PassthruState *card = PASSTHRU_CCID_CARD(base);
346
347 card->vscard_in_pos = 0;
348 card->vscard_in_hdr = 0;
349 if (qemu_chr_fe_backend_connected(&card->cs)) {
350 DPRINTF(card, D_INFO, "ccid-card-passthru: initing chardev");
351 qemu_chr_fe_set_handlers(&card->cs,
352 ccid_card_vscard_can_read,
353 ccid_card_vscard_read,
354 ccid_card_vscard_event, NULL, card, NULL, true);
355 ccid_card_vscard_send_init(card);
356 } else {
357 error_setg(errp, "missing chardev");
358 return;
359 }
360 card->debug = parse_debug_env("QEMU_CCID_PASSTHRU_DEBUG", D_VERBOSE,
361 card->debug);
362 assert(sizeof(DEFAULT_ATR) <= MAX_ATR_SIZE);
363 memcpy(card->atr, DEFAULT_ATR, sizeof(DEFAULT_ATR));
364 card->atr_length = sizeof(DEFAULT_ATR);
365 }
366
367 static VMStateDescription passthru_vmstate = {
368 .name = "ccid-card-passthru",
369 .version_id = 1,
370 .minimum_version_id = 1,
371 .fields = (VMStateField[]) {
372 VMSTATE_BUFFER(vscard_in_data, PassthruState),
373 VMSTATE_UINT32(vscard_in_pos, PassthruState),
374 VMSTATE_UINT32(vscard_in_hdr, PassthruState),
375 VMSTATE_BUFFER(atr, PassthruState),
376 VMSTATE_UINT8(atr_length, PassthruState),
377 VMSTATE_END_OF_LIST()
378 }
379 };
380
381 static Property passthru_card_properties[] = {
382 DEFINE_PROP_CHR("chardev", PassthruState, cs),
383 DEFINE_PROP_UINT8("debug", PassthruState, debug, 0),
384 DEFINE_PROP_END_OF_LIST(),
385 };
386
387 static void passthru_class_initfn(ObjectClass *klass, void *data)
388 {
389 DeviceClass *dc = DEVICE_CLASS(klass);
390 CCIDCardClass *cc = CCID_CARD_CLASS(klass);
391
392 cc->realize = passthru_realize;
393 cc->get_atr = passthru_get_atr;
394 cc->apdu_from_guest = passthru_apdu_from_guest;
395 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
396 dc->desc = "passthrough smartcard";
397 dc->vmsd = &passthru_vmstate;
398 dc->props = passthru_card_properties;
399 }
400
401 static const TypeInfo passthru_card_info = {
402 .name = TYPE_CCID_PASSTHRU,
403 .parent = TYPE_CCID_CARD,
404 .instance_size = sizeof(PassthruState),
405 .class_init = passthru_class_initfn,
406 };
407
408 static void ccid_card_passthru_register_types(void)
409 {
410 type_register_static(&passthru_card_info);
411 }
412
413 type_init(ccid_card_passthru_register_types)