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