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