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