2 * Tester for VSCARD protocol, client side.
4 * Can be used with ccid-card-passthru.
6 * Copyright (c) 2011 Red Hat.
7 * Written by Alon Levy.
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
15 #include "qemu-common.h"
16 #include "qemu-thread.h"
17 #include "qemu_socket.h"
19 #include "vscard_common.h"
22 #include "vcard_emul.h"
35 for (i
= 0; i
< nSize
; i
++) {
36 printf("%02X ", arrBytes
[i
]);
43 printf("vscclient [-c <certname> .. -e <emul_args> -d <level>%s] "
47 printf(" -p use passthrough mode\n");
54 static QemuMutex write_lock
;
64 VSCMsgHeader mhHeader
;
66 qemu_mutex_lock(&write_lock
);
69 printf("sending type=%d id=%d, len =%d (0x%x)\n",
70 type
, reader_id
, length
, length
);
73 mhHeader
.type
= htonl(type
);
74 mhHeader
.reader_id
= 0;
75 mhHeader
.length
= htonl(length
);
76 rv
= write(sock
, &mhHeader
, sizeof(mhHeader
));
79 fprintf(stderr
, "write header error\n");
81 qemu_mutex_unlock(&write_lock
);
84 rv
= write(sock
, msg
, length
);
87 fprintf(stderr
, "write error\n");
89 qemu_mutex_unlock(&write_lock
);
92 qemu_mutex_unlock(&write_lock
);
97 static VReader
*pending_reader
;
98 static QemuMutex pending_reader_lock
;
99 static QemuCond pending_reader_condition
;
101 #define MAX_ATR_LEN 40
103 event_thread(void *arg
)
105 unsigned char atr
[MAX_ATR_LEN
];
106 int atr_len
= MAX_ATR_LEN
;
107 VEvent
*event
= NULL
;
108 unsigned int reader_id
;
112 const char *reader_name
;
114 event
= vevent_wait_next_vevent();
118 reader_id
= vreader_get_id(event
->reader
);
119 if (reader_id
== VSCARD_UNDEFINED_READER_ID
&&
120 event
->type
!= VEVENT_READER_INSERT
) {
121 /* ignore events from readers qemu has rejected */
122 /* if qemu is still deciding on this reader, wait to see if need to
123 * forward this event */
124 qemu_mutex_lock(&pending_reader_lock
);
125 if (!pending_reader
|| (pending_reader
!= event
->reader
)) {
126 /* wasn't for a pending reader, this reader has already been
127 * rejected by qemu */
128 qemu_mutex_unlock(&pending_reader_lock
);
129 vevent_delete(event
);
132 /* this reader hasn't been told it's status from qemu yet, wait for
134 while (pending_reader
!= NULL
) {
135 qemu_cond_wait(&pending_reader_condition
, &pending_reader_lock
);
137 qemu_mutex_unlock(&pending_reader_lock
);
138 /* now recheck the id */
139 reader_id
= vreader_get_id(event
->reader
);
140 if (reader_id
== VSCARD_UNDEFINED_READER_ID
) {
141 /* this reader was rejected */
142 vevent_delete(event
);
145 /* reader was accepted, now forward the event */
147 switch (event
->type
) {
148 case VEVENT_READER_INSERT
:
149 /* tell qemu to insert a new CCID reader */
150 /* wait until qemu has responded to our first reader insert
151 * before we send a second. That way we won't confuse the responses
153 qemu_mutex_lock(&pending_reader_lock
);
154 while (pending_reader
!= NULL
) {
155 qemu_cond_wait(&pending_reader_condition
, &pending_reader_lock
);
157 pending_reader
= vreader_reference(event
->reader
);
158 qemu_mutex_unlock(&pending_reader_lock
);
159 reader_name
= vreader_get_name(event
->reader
);
161 printf(" READER INSERT: %s\n", reader_name
);
163 send_msg(VSC_ReaderAdd
,
164 reader_id
, /* currerntly VSCARD_UNDEFINED_READER_ID */
165 NULL
, 0 /* TODO reader_name, strlen(reader_name) */);
167 case VEVENT_READER_REMOVE
:
168 /* future, tell qemu that an old CCID reader has been removed */
170 printf(" READER REMOVE: %d\n", reader_id
);
172 send_msg(VSC_ReaderRemove
, reader_id
, NULL
, 0);
174 case VEVENT_CARD_INSERT
:
175 /* get the ATR (intended as a response to a power on from the
177 atr_len
= MAX_ATR_LEN
;
178 vreader_power_on(event
->reader
, atr
, &atr_len
);
179 /* ATR call functions as a Card Insert event */
181 printf(" CARD INSERT %d: ", reader_id
);
182 print_byte_array(atr
, atr_len
);
184 send_msg(VSC_ATR
, reader_id
, atr
, atr_len
);
186 case VEVENT_CARD_REMOVE
:
189 printf(" CARD REMOVE %d:\n", reader_id
);
191 send_msg(VSC_CardRemove
, reader_id
, NULL
, 0);
196 vevent_delete(event
);
203 get_id_from_string(char *string
, unsigned int default_id
)
205 unsigned int id
= atoi(string
);
207 /* don't accidentally swith to zero because no numbers have been supplied */
208 if ((id
== 0) && *string
!= '0') {
219 VCardEmulError error
;
220 static unsigned int default_reader_id
;
221 unsigned int reader_id
;
222 VReader
*reader
= NULL
;
224 reader_id
= default_reader_id
;
225 string
= fgets(inbuf
, sizeof(inbuf
), stdin
);
226 if (string
!= NULL
) {
227 if (strncmp(string
, "exit", 4) == 0) {
228 /* remove all the readers */
229 VReaderList
*list
= vreader_get_reader_list();
230 VReaderListEntry
*reader_entry
;
231 printf("Active Readers:\n");
232 for (reader_entry
= vreader_list_get_first(list
); reader_entry
;
233 reader_entry
= vreader_list_get_next(reader_entry
)) {
234 VReader
*reader
= vreader_list_get_reader(reader_entry
);
235 vreader_id_t reader_id
;
236 reader_id
= vreader_get_id(reader
);
237 if (reader_id
== -1) {
240 /* be nice and signal card removal first (qemu probably should
242 if (vreader_card_is_present(reader
) == VREADER_OK
) {
243 send_msg(VSC_CardRemove
, reader_id
, NULL
, 0);
245 send_msg(VSC_ReaderRemove
, reader_id
, NULL
, 0);
248 } else if (strncmp(string
, "insert", 6) == 0) {
249 if (string
[6] == ' ') {
250 reader_id
= get_id_from_string(&string
[7], reader_id
);
252 reader
= vreader_get_reader_by_id(reader_id
);
253 if (reader
!= NULL
) {
254 error
= vcard_emul_force_card_insert(reader
);
255 printf("insert %s, returned %d\n",
256 reader
? vreader_get_name(reader
)
257 : "invalid reader", error
);
259 printf("no reader by id %d found\n", reader_id
);
261 } else if (strncmp(string
, "remove", 6) == 0) {
262 if (string
[6] == ' ') {
263 reader_id
= get_id_from_string(&string
[7], reader_id
);
265 reader
= vreader_get_reader_by_id(reader_id
);
266 if (reader
!= NULL
) {
267 error
= vcard_emul_force_card_remove(reader
);
268 printf("remove %s, returned %d\n",
269 reader
? vreader_get_name(reader
)
270 : "invalid reader", error
);
272 printf("no reader by id %d found\n", reader_id
);
274 } else if (strncmp(string
, "select", 6) == 0) {
275 if (string
[6] == ' ') {
276 reader_id
= get_id_from_string(&string
[7],
277 VSCARD_UNDEFINED_READER_ID
);
279 if (reader_id
!= VSCARD_UNDEFINED_READER_ID
) {
280 reader
= vreader_get_reader_by_id(reader_id
);
283 printf("Selecting reader %d, %s\n", reader_id
,
284 vreader_get_name(reader
));
285 default_reader_id
= reader_id
;
287 printf("Reader with id %d not found\n", reader_id
);
289 } else if (strncmp(string
, "debug", 5) == 0) {
290 if (string
[5] == ' ') {
291 verbose
= get_id_from_string(&string
[6], 0);
293 printf("debug level = %d\n", verbose
);
294 } else if (strncmp(string
, "list", 4) == 0) {
295 VReaderList
*list
= vreader_get_reader_list();
296 VReaderListEntry
*reader_entry
;
297 printf("Active Readers:\n");
298 for (reader_entry
= vreader_list_get_first(list
); reader_entry
;
299 reader_entry
= vreader_list_get_next(reader_entry
)) {
300 VReader
*reader
= vreader_list_get_reader(reader_entry
);
301 vreader_id_t reader_id
;
302 reader_id
= vreader_get_id(reader
);
303 if (reader_id
== -1) {
306 printf("%3d %s %s\n", reader_id
,
307 vreader_card_is_present(reader
) == VREADER_OK
?
308 "CARD_PRESENT" : " ",
309 vreader_get_name(reader
));
311 printf("Inactive Readers:\n");
312 for (reader_entry
= vreader_list_get_first(list
); reader_entry
;
313 reader_entry
= vreader_list_get_next(reader_entry
)) {
314 VReader
*reader
= vreader_list_get_reader(reader_entry
);
315 vreader_id_t reader_id
;
316 reader_id
= vreader_get_id(reader
);
317 if (reader_id
!= -1) {
321 printf("INA %s %s\n",
322 vreader_card_is_present(reader
) == VREADER_OK
?
323 "CARD_PRESENT" : " ",
324 vreader_get_name(reader
));
326 } else if (*string
!= 0) {
327 printf("valid commands:\n");
328 printf("insert [reader_id]\n");
329 printf("remove [reader_id]\n");
330 printf("select reader_id\n");
332 printf("debug [level]\n");
336 vreader_free(reader
);
342 #define APDUBufSize 270
344 /* just for ease of parsing command line arguments. */
345 #define MAX_CERTS 100
352 struct addrinfo hints
;
353 struct addrinfo
*server
;
356 sock
= qemu_socket(AF_INET
, SOCK_STREAM
, 0);
359 fprintf(stderr
, "Error opening socket!\n");
362 memset(&hints
, 0, sizeof(struct addrinfo
));
363 hints
.ai_family
= AF_UNSPEC
;
364 hints
.ai_socktype
= SOCK_STREAM
;
366 hints
.ai_protocol
= 0; /* Any protocol */
368 ret
= getaddrinfo(host
, port
, &hints
, &server
);
372 fprintf(stderr
, "getaddrinfo failed\n");
376 if (connect(sock
, server
->ai_addr
, server
->ai_addrlen
) < 0) {
378 fprintf(stderr
, "Could not connect\n");
382 printf("Connected (sizeof Header=%zd)!\n", sizeof(VSCMsgHeader
));
387 static int on_host_init(VSCMsgHeader
*mhHeader
, VSCMsgInit
*incoming
)
389 uint32_t *capabilities
= (incoming
->capabilities
);
390 int num_capabilities
=
391 1 + ((mhHeader
->length
- sizeof(VSCMsgInit
)) / sizeof(uint32_t));
396 incoming
->version
= ntohl(incoming
->version
);
397 if (incoming
->version
!= VSCARD_VERSION
) {
399 printf("warning: host has version %d, we have %d\n",
400 verbose
, VSCARD_VERSION
);
403 if (incoming
->magic
!= VSCARD_MAGIC
) {
404 printf("unexpected magic: got %d, expected %d\n",
405 incoming
->magic
, VSCARD_MAGIC
);
408 for (i
= 0 ; i
< num_capabilities
; ++i
) {
409 capabilities
[i
] = ntohl(capabilities
[i
]);
411 /* Future: check capabilities */
412 /* remove whatever reader might be left in qemu,
413 * in case of an unclean previous exit. */
414 send_msg(VSC_ReaderRemove
, VSCARD_MINIMAL_READER_ID
, NULL
, 0);
415 /* launch the event_thread. This will trigger reader adds for all the
416 * existing readers */
417 rv
= pthread_create(&thread_id
, NULL
, event_thread
, NULL
);
419 perror("pthread_create");
432 VSCMsgHeader mhHeader
;
433 VSCMsgError
*error_msg
;
438 uint8_t pbRecvBuffer
[APDUBufSize
];
439 uint8_t pbSendBuffer
[APDUBufSize
];
440 VReaderStatus reader_status
;
441 VReader
*reader
= NULL
;
442 VCardEmulOptions
*command_line_options
= NULL
;
444 char *cert_names
[MAX_CERTS
];
445 char *emul_args
= NULL
;
449 while ((c
= getopt(argc
, argv
, "c:e:pd:")) != -1) {
452 if (cert_count
>= MAX_CERTS
) {
453 printf("too many certificates (max = %d)\n", MAX_CERTS
);
456 cert_names
[cert_count
++] = optarg
;
466 verbose
= get_id_from_string(optarg
, 1);
471 if (argc
- optind
!= 2) {
476 if (cert_count
> 0) {
479 /* if we've given some -c options, we clearly we want do so some
480 * software emulation. add that emulation now. this is NSS Emulator
482 if (emul_args
== NULL
) {
483 emul_args
= (char *)"db=\"/etc/pki/nssdb\"";
485 #define SOFT_STRING ",soft=(,Virtual Reader,CAC,,"
486 /* 2 == close paren & null */
487 len
= strlen(emul_args
) + strlen(SOFT_STRING
) + 2;
488 for (i
= 0; i
< cert_count
; i
++) {
489 len
+= strlen(cert_names
[i
])+1; /* 1 == comma */
491 new_args
= g_malloc(len
);
492 strcpy(new_args
, emul_args
);
493 strcat(new_args
, SOFT_STRING
);
494 for (i
= 0; i
< cert_count
; i
++) {
495 strcat(new_args
, cert_names
[i
]);
496 strcat(new_args
, ",");
498 strcat(new_args
, ")");
499 emul_args
= new_args
;
502 command_line_options
= vcard_emul_options(emul_args
);
505 qemu_host
= strdup(argv
[argc
- 2]);
506 qemu_port
= strdup(argv
[argc
- 1]);
507 sock
= connect_to_qemu(qemu_host
, qemu_port
);
509 qemu_mutex_init(&write_lock
);
510 qemu_mutex_init(&pending_reader_lock
);
511 qemu_cond_init(&pending_reader_condition
);
513 vcard_emul_init(command_line_options
);
518 /* Send init message, Host responds (and then we send reader attachments) */
520 .version
= htonl(VSCARD_VERSION
),
521 .magic
= VSCARD_MAGIC
,
524 send_msg(VSC_Init
, mhHeader
.reader_id
, &init
, sizeof(init
));
533 /* waiting on input from the socket */
534 rv
= select(sock
+1, &fds
, NULL
, NULL
, NULL
);
540 if (FD_ISSET(1, &fds
)) {
543 if (!FD_ISSET(sock
, &fds
)) {
547 rv
= read(sock
, &mhHeader
, sizeof(mhHeader
));
548 if (rv
< sizeof(mhHeader
)) {
551 perror("header read error\n");
553 fprintf(stderr
, "header short read %d\n", rv
);
557 mhHeader
.type
= ntohl(mhHeader
.type
);
558 mhHeader
.reader_id
= ntohl(mhHeader
.reader_id
);
559 mhHeader
.length
= ntohl(mhHeader
.length
);
561 printf("Header: type=%d, reader_id=%d length=%d (0x%x)\n",
562 mhHeader
.type
, mhHeader
.reader_id
, mhHeader
.length
,
565 switch (mhHeader
.type
) {
570 rv
= read(sock
, pbSendBuffer
, mhHeader
.length
);
573 fprintf(stderr
, "Unexpected message of type 0x%X\n", mhHeader
.type
);
576 switch (mhHeader
.type
) {
580 fprintf(stderr
, "read error\n");
585 printf(" recv APDU: ");
586 print_byte_array(pbSendBuffer
, mhHeader
.length
);
588 /* Transmit received APDU */
589 dwSendLength
= mhHeader
.length
;
590 dwRecvLength
= sizeof(pbRecvBuffer
);
591 reader
= vreader_get_reader_by_id(mhHeader
.reader_id
);
592 reader_status
= vreader_xfr_bytes(reader
,
593 pbSendBuffer
, dwSendLength
,
594 pbRecvBuffer
, &dwRecvLength
);
595 if (reader_status
== VREADER_OK
) {
596 mhHeader
.length
= dwRecvLength
;
598 printf(" send response: ");
599 print_byte_array(pbRecvBuffer
, mhHeader
.length
);
601 send_msg(VSC_APDU
, mhHeader
.reader_id
,
602 pbRecvBuffer
, dwRecvLength
);
604 rv
= reader_status
; /* warning: not meaningful */
605 send_msg(VSC_Error
, mhHeader
.reader_id
, &rv
, sizeof(uint32_t));
607 vreader_free(reader
);
608 reader
= NULL
; /* we've freed it, don't use it by accident
612 /* TODO: actually flush */
613 send_msg(VSC_FlushComplete
, mhHeader
.reader_id
, NULL
, 0);
616 error_msg
= (VSCMsgError
*) pbSendBuffer
;
617 if (error_msg
->code
== VSC_SUCCESS
) {
618 qemu_mutex_lock(&pending_reader_lock
);
619 if (pending_reader
) {
620 vreader_set_id(pending_reader
, mhHeader
.reader_id
);
621 vreader_free(pending_reader
);
622 pending_reader
= NULL
;
623 qemu_cond_signal(&pending_reader_condition
);
625 qemu_mutex_unlock(&pending_reader_lock
);
628 printf("warning: qemu refused to add reader\n");
629 if (error_msg
->code
== VSC_CANNOT_ADD_MORE_READERS
) {
630 /* clear pending reader, qemu can't handle any more */
631 qemu_mutex_lock(&pending_reader_lock
);
632 if (pending_reader
) {
633 pending_reader
= NULL
;
634 /* make sure the event loop doesn't hang */
635 qemu_cond_signal(&pending_reader_condition
);
637 qemu_mutex_unlock(&pending_reader_lock
);
641 if (on_host_init(&mhHeader
, (VSCMsgInit
*)pbSendBuffer
) < 0) {