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