usb-ccid
v20 cover usb-ccid
This patchset adds three new devices, usb-ccid, ccid-card-passthru and ccid-card-emulated, providing a CCID bus, a simple passthru protocol implementing card requiring a client, and a standalone emulated card.
It also introduces a new directory libcaccard with CAC card emulation, CAC is a type of ISO 7816 smart card.
Tree for pull: git://anongit.freedesktop.org/~alon/qemu usb_ccid.v20
v19->v20 changes:
* checkpatch.pl. Here are the remaining errors with explanation:
* ignored 5 macro errors of the type
"ERROR: Macros with complex values should be enclosed in parenthesis" because fixing them breaks current code, if it really bothers someone I can fix it.
* four of them are in libcacard/card_7816t.h:
/* give the subfields a unified look */
..
#define a_cla a_header->ah_cla /* class */
#define a_ins a_header->ah_ins /* instruction */
#define a_p1 a_header->ah_p1 /* parameter 1 */
#define a_p2 a_header->ah_p2 /* parameter 2 */
* and the fifth:
#4946: FILE: libcacard/vcardt.h:31:
+#define VCARD_ATR_PREFIX(size) 0x3b, 0x66+(size), 0x00, 0xff, \
+ 'V', 'C', 'A', 'R', 'D', '_'
* Ignored this warning since I couldn't figure it out, and it's a test file:
WARNING: externs should be avoided in .c files
#2343: FILE: libcacard/link_test.c:7:
+VCardStatus cac_card_init(const char *flags, VCard *card,
v18-v19 changes:
* more merges, down to a single digit number of patches.
* drop enumeration property, use string.
* rebased (trivial)
v17-v18 changes:
* merge vscard_common.h patches.
* actually provide a tree to pull.
v16-v17 changes:
* merged all the "v15->v16" patches
* merged some more wherever it was easy (all same file commits).
* added signed off by to first four patches
* ccid.h: added copyright, removed underscore in defines, and replaced non C89 comments
v15-v16 changes:
* split vscard_common introducing patch for ease of review
* sum of commit logs for the v15-v16 commits: (whitespace fixes
removed for space, see original commit messages in later patches)
* usb-ccid:
* fix abort on client answer after card remove
* enable migration
* remove side affect code from asserts
* return consistent self-powered state
* mask out reserved bits in ccid_set_parameters
* add missing abRFU in SetParameters (no affect on linux guest)
* vscard_common.h protocol change:
* VSCMsgInit capabilities and magic
* removed ReaderResponse, will use Error instead with code==VSC_SUCCESS.
* added Flush and FlushComplete, remove Reconnect.
* define VSCARD_MAGIC
* added error code VSC_SUCCESS.
* ccid-card-passthru
* return correct size
* return error instead of assert if client sent too large ATR
* don't assert if client sent too large a size, but add asserts for indices to buffer
* reset vscard_in indices on chardev disconnect
* handle init from client
* error if no chardev supplied
* use ntoh, hton
* eradicate reader_id_t
* remove Reconnect usage (removed from VSCARD protocol)
* send VSC_SUCCESS on card insert/remove and reader add/remove
* ccid-card-emulated
* fix error reporting in initfn
v14-v15 changes:
* add patch with --enable-smartcard and --disable-smartcard and only disable ccid-card-emulated if nss not found.
* add patch with description strings
* s/libcaccard/libcacard/ in docs/ccid.txt
v13-v14 changes:
- support device_del/device_add on ccid-card-* and usb-ccid
* usb-ccid:
* lose card reference when card device deleted
* check slot number and deny adding a slot if one is already added.
* ccid-card-*: use qdev_simple_unplug_cb in both emulated and passthru ccid cards, the exitfn already takes care of triggering card removal in the usb dev.
* libcacard:
* remove double include of config-host.mak
* add replay of card events to libcacard to support second and more emulation
* don't initialize more then once (doesn't support it right now, so one thread, NSS thread, is left when device_del is done)
* add VCARD_EMUL_INIT_ALREADY_INITED
* ccid-card-emulated:
* take correct mutexes on signaling to fix deadlocks on device_del
* allow card insertion/removal event without proper reader insertion event
v12-v13 changes:
* libcacard:
* fix Makefile clean to remove vscclient
* fix double include of config-host in Makefile
* usb-ccid: remove attach/detach logic, usb is always attached. Guest doesn't care if there is a reader attached with no card anyway.
* ccid-card-passthru: don't close chr_dev on removal, makes it possible to use device_del/device_add to create remove/insertion for debugging.
v11-v12 changes:
* fix out of tree build
v10-v11 changes:
* fix last patch that removed one of the doc files.
* updated flow table in docs/ccid.txt
v8-v10 changes:
* usb-ccid:
* add slot for future use (Gerd)
* ifdef ENABLE_MIGRATION for migration support on account of usb migration not being ready in general. (Gerd)
* verbosified commit messages. (Gerd)
* put libcacard docs in libcacard commit. (Gerd)
v8-v9 changes:
* Blue Swirl comments:
* white space fixes
* enabled by default, disabled only if missing nss
* forgotten fix from v8 (don't build libcacard.so)
* added a note about device being little endian
* library renamed from libcaccard to libcacard
* squashed both of libcacard patches, they touched different files anyway.
v7-v8 changes:
* Blue Swirl comments:
* usb-ccid: deannonymize some structs
* usb-ccid: coding style change - answer_t and bulk_in_t fixed
* usb-ccid: handle endianess conversion between guest and host
* usb-ccid: s/ccid_bulk_in_copy_out/ccid_bulk_in_copy_to_guest/
* ccid-card-emulated: fix segfault if backend not specified
* ccid-card-emulated: let last reader inserted win
* libcaccard: remove double vscard_common.h
v6->v7 changes:
* external libcaccard became internal directory libcaccard
* statically link object files into qemu
* produce libcaccard.so for usage by external projects
* applied coding style to new code (please check me)
- did not use the qemu options parsing for libcaccard, since it seems to draw large amounts of qemu code (monitor for instance).
v5->v6 changes:
* really remove static debug (I apologize for claiming to have done so before)
v4->v5 changes:
* rebased to latest
* remove static debug in card devices
* fix --enable-smartcard to link
* stall instead of assert when exceeding BULK_OUT_DATA_SIZE
* make ccid_reserve_recv_buf for too large len discard message, not exit
* make ccid_reserve_recv_buf return void*
* fix typo
* remove commented code in VMState
v3->v4:
* remove ccid field in CCIDBus
* remove static debug in bus
* add back docs
v2->v3:
* split into bus (usb-ccid.c, uses ccid.h) and card (ccid-card-passthru.c).
* removed documentation (being revised).
v1->v2:
* all QSIMPLEQ turned into fixed sized rings
* all allocated buffers turned into fixed size buffers
* added migration support
* added a message to tell client qemu has migrated to ip:port
* for lack of monitor commands ip:port are 0:0, which causes the updated vscclient to connect to one port higher on the same host. will add monitor commands in a separate patch. tested with current setup.
Alon Levy (6):
usb-ccid: add CCID bus
introduce libcacard/vscard_common.h
ccid: add passthru card device
ccid: add ccid-card-emulated device
ccid: add docs
ccid: configure: improve --enable-smartcard flags
Robert Relyea (1):
libcacard: initial commit
Makefile | 6 +- Makefile.objs | 7 + Makefile.target | 2 + configure | 60 ++ docs/ccid.txt | 135 +++++ docs/libcacard.txt | 483 +++++++++++++++ hw/ccid-card-emulated.c | 599 +++++++++++++++++++ hw/ccid-card-passthru.c | 337 +++++++++++ hw/ccid.h | 54 ++ hw/usb-ccid.c | 1391 +++++++++++++++++++++++++++++++++++++++++++ libcacard/Makefile | 14 + libcacard/cac.c | 411 +++++++++++++ libcacard/cac.h | 20 + libcacard/card_7816.c | 780 ++++++++++++++++++++++++ libcacard/card_7816.h | 60 ++ libcacard/card_7816t.h | 163 +++++ libcacard/event.c | 112 ++++ libcacard/eventt.h | 28 + libcacard/link_test.c | 20 + libcacard/mutex.h | 59 ++ libcacard/passthru.c | 612 +++++++++++++++++++ libcacard/passthru.h | 50 ++ libcacard/vcard.c | 350 +++++++++++ libcacard/vcard.h | 85 +++ libcacard/vcard_emul.h | 62 ++ libcacard/vcard_emul_nss.c | 1195 +++++++++++++++++++++++++++++++++++++ libcacard/vcard_emul_type.c | 60 ++ libcacard/vcard_emul_type.h | 29 + libcacard/vcardt.h | 66 ++ libcacard/vevent.h | 26 + libcacard/vreader.c | 526 ++++++++++++++++ libcacard/vreader.h | 54 ++ libcacard/vreadert.h | 23 + libcacard/vscard_common.h | 167 ++++++ libcacard/vscclient.c | 744 +++++++++++++++++++++++ 35 files changed, 8788 insertions(+), 2 deletions(-) create mode 100644 docs/ccid.txt create mode 100644 docs/libcacard.txt create mode 100644 hw/ccid-card-emulated.c create mode 100644 hw/ccid-card-passthru.c create mode 100644 hw/ccid.h create mode 100644 hw/usb-ccid.c create mode 100644 libcacard/Makefile create mode 100644 libcacard/cac.c create mode 100644 libcacard/cac.h create mode 100644 libcacard/card_7816.c create mode 100644 libcacard/card_7816.h create mode 100644 libcacard/card_7816t.h create mode 100644 libcacard/event.c create mode 100644 libcacard/eventt.h create mode 100644 libcacard/link_test.c create mode 100644 libcacard/mutex.h create mode 100644 libcacard/passthru.c create mode 100644 libcacard/passthru.h create mode 100644 libcacard/vcard.c create mode 100644 libcacard/vcard.h create mode 100644 libcacard/vcard_emul.h create mode 100644 libcacard/vcard_emul_nss.c create mode 100644 libcacard/vcard_emul_type.c create mode 100644 libcacard/vcard_emul_type.h create mode 100644 libcacard/vcardt.h create mode 100644 libcacard/vevent.h create mode 100644 libcacard/vreader.c create mode 100644 libcacard/vreader.h create mode 100644 libcacard/vreadert.h create mode 100644 libcacard/vscard_common.h create mode 100644 libcacard/vscclient.c
Signature · 1 line
1.7.4.1
PATCH v20 2/7 introduce libcacard/vscard_common.h
---
v19->v20 changes:
* checkpatch.pl
v15->v16 changes:
Protocol change:
* VSCMsgInit capabilities and magic
* removed ReaderResponse, will use Error instead with code==VSC_SUCCESS.
* adaded Flush and FlushComplete, remove Reconnect.
* define VSCARD_MAGIC
* added error code VSC_SUCCESS.
Fixes:
* update VSCMsgInit comment
* fix message type enum
* remove underscore from wrapping define
* update copyright
* updated comments.
* Header comment updated
* remove C++ style comment
* fix comment for VSCMsgError
* give names to enums in typedefs
--- libcacard/vscard_common.h | 167 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 167 insertions(+), 0 deletions(-) create mode 100644 libcacard/vscard_common.h
libcacard/vscard_common.h new file +167 -0
@@ -0,0 +1,167 @@ +/* Virtual Smart Card protocol definition +* +* This protocol is between a host using virtual smart card readers, +* and a client providing the smart cards, perhaps by emulating them or by +* access to real cards. +* +* Definitions for this protocol: +* Host - user of the card +* Client - owner of the card +* +* The current implementation passes the raw APDU's from 7816 and additionally +* contains messages to setup and teardown readers, handle insertion and +* removal of cards, negotiate the protocol via capabilities and provide +* for error responses. +* +* Copyright (c) 2011 Red Hat. +* +* This code is licensed under the LGPL. +*/ + +#ifndef VSCARD_COMMON_H +#define VSCARD_COMMON_H + +#include <stdint.h> + +#define VERSION_MAJOR_BITS 11 +#define VERSION_MIDDLE_BITS 11 +#define VERSION_MINOR_BITS 10 + +#define MAKE_VERSION (major, middle, minor) \ + ((major << (VERSION_MINOR_BITS + VERSION_MIDDLE_BITS)) \ + | (middle << VERSION_MINOR_BITS) \ + | (minor)) + +/** IMPORTANT NOTE on VERSION +* +* The version below MUST be changed whenever a change in this file is made. +* +* The last digit, the minor, is for bug fix changes only. +* +* The middle digit is for backward / forward compatible changes, updates +* to the existing messages, addition of fields. +* +* The major digit is for a breaking change of protocol, presumably +* something that cannot be accomodated with the existing protocol. +*/ + +#define VSCARD_VERSION MAKE_VERSION(0, 0, 2) + +typedef enum VSCMsgType { + VSC_Init =1 , + VSC_Error, + VSC_ReaderAdd, + VSC_ReaderRemove, +VSC_ATR , + VSC_CardRemove, +VSC_APDU , + VSC_Flush, + VSC_FlushComplete +}VSCMsgType ; + +typedef enum VSCErrorCode { +VSC_SUCCESS =0 , +VSC_GENERAL_ERROR =1 , +VSC_CANNOT_ADD_MORE_READERS , +VSC_CARD_ALREAY_INSERTED , +}VSCErrorCode ; + +#define VSCARD_UNDEFINED_READER_ID 0xffffffff +#define VSCARD_MINIMAL_READER_ID 0 + +#define VSCARD_MAGIC (*(uint32_t *)"VSCD") + +/* Header +* Each message starts with the header. +* type - message type +* reader_id - used by messages that are reader specific +* length - length of payload (not including header, i.e. zero for +* messages containing empty payloads) +*/ +typedef struct VSCMsgHeader { +uint32_t type; +uint32_t reader_id; +uint32_t length; +uint8_t data[0 ]; +}VSCMsgHeader ; + +/* VSCMsgInit Client <-> Host +* Client sends it on connection, with its own capabilities. +* Host replies with VSCMsgInit filling in its capabilities. +* +* It is not meant to be used for negotiation, i.e. sending more then +* once from any side, but could be used for that in the future. +* */ +typedef struct VSCMsgInit { +uint32_t magic; +uint32_t version; +uint32_t capabilities[1 ];/* receiver must check length, +array may grow in the future*/ +}VSCMsgInit ; + +/* VSCMsgError Client <-> Host +* This message is a response to any of: +* Reader Add +* Reader Remove +* Card Remove +* If the operation was successful then VSC_SUCCESS +* is returned, other wise a specific error code. +* */ +typedef struct VSCMsgError { +uint32_t code; +}VSCMsgError ; + +/* VSCMsgReaderAdd Client -> Host +* Host replies with allocated reader id in VSCMsgError with code==SUCCESS. +* +* name - name of the reader on client side, UTF-8 encoded. Only used +* for client presentation (may be translated to the device presented to the +* guest), protocol wise only reader_id is important. +* */ +typedef struct VSCMsgReaderAdd { +uint8_t name[0 ]; +}VSCMsgReaderAdd ; + +/* VSCMsgReaderRemove Client -> Host +* The client's reader has been removed. +* */ +typedef struct VSCMsgReaderRemove { +}VSCMsgReaderRemove ; + +/* VSCMsgATR Client -> Host +* Answer to reset. Sent for card insertion or card reset. The reset/insertion +* happens on the client side, they do not require any action from the host. +* */ +typedef struct VSCMsgATR { +uint8_t atr[0 ]; +}VSCMsgATR ; + +/* VSCMsgCardRemove Client -> Host +* The client card has been removed. +* */ +typedef struct VSCMsgCardRemove { +}VSCMsgCardRemove ; + +/* VSCMsgAPDU Client <-> Host +* Main reason of existance. Transfer a single APDU in either direction. +* */ +typedef struct VSCMsgAPDU { +uint8_t data[0 ]; +}VSCMsgAPDU ; + +/* VSCMsgFlush Host -> Client +* Request client to send a FlushComplete message when it is done +* servicing all outstanding APDUs +* */ +typedef struct VSCMsgFlush { +}VSCMsgFlush ; + +/* VSCMsgFlush Client -> Host +* Client response to Flush after all APDUs have been processed and +* responses sent. +* */ +typedef struct VSCMsgFlushComplete { +}VSCMsgFlushComplete ; + +#endif /* VSCARD_COMMON_H */ +
Signature · 1 line
1.7.4.1
PATCH v20 6/7 ccid: add docs
In reply to Alon LevyAdd documentation for the usb-ccid device and accompanying two card devices, ccid-card-emulated and ccid-card-passthru.
--- docs/ccid.txt | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 135 insertions(+), 0 deletions(-) create mode 100644 docs/ccid.txt
docs/ccid.txt new file +135 -0
@@ -0,0 +1,135 @@ +Qemu CCID Device Documentation. + +Contents +1. USB CCID device +2. Building +3. Using ccid-card-emulated with hardware +4. Using ccid-card-emulated with certificates +5. Using ccid-card-passthru with client side hardware +6. Using ccid-card-passthru with client side certificates +7. Passthrough protocol scenario +8. libcacard + +1. USB CCID device + +The USB CCID device is a USB device implementing the CCID specification, which +lets one connect smart card readers that implement the same spec. For more +information see the specification: + + Universal Serial Bus + Device Class: Smart Card + CCID + Specification for + Integrated Circuit(s) Cards Interface Devices + Revision 1.1 + April 22rd, 2005 + +Smartcard are used for authentication, single sign on, decryption in +public/private schemes and digital signatures. A smartcard reader on the client +cannot be used on a guest with simple usb passthrough since it will then not be +available on the client, possibly locking the computer when it is "removed". On +the other hand this device can let you use the smartcard on both the client and +the guest machine. It is also possible to have a completely virtual smart card +reader and smart card (i.e. not backed by a physical device) using this device. + +2. Building + +The cryptographic functions and access to the physical card is done via NSS. + +Installing NSS: + +In redhat/fedora: + yum install nss-devel +In ubuntu/debian: + apt-get install libnss3-dev + (not tested on ubuntu) + +Configuring and building: + ./configure --enable-smartcard && make + +3. Using ccid-card-emulated with hardware + +Assuming you have a working smartcard on the host with the current +user, using NSS, qemu acts as another NSS client using ccid-card-emulated: + + qemu -usb -device usb-ccid -device ccid-card-emualated + +4. Using ccid-card-emulated with certificates + +You must create the certificates. This is a one time process. We use NSS +certificates: + + certutil -d /etc/pki/nssdb -x -t "CT,CT,CT" -S -s "CN=cert1" -n cert1 + +Note: you must have exactly three certificates. + +Assuming the current user can access the certificates (use certutil -L to +verify), you can use the emulated card type with the certificates backend: + + qemu -usb -device usb-ccid -device ccid-card-emulated,backend=certificates,cert1=cert1,cert2=cert2,cert3=cert3 + +5. Using ccid-card-passthru with client side hardware + +on the host specify the ccid-card-passthru device with a suitable chardev: + + qemu -chardev socket,server,host=0.0.0.0,port=2001,id=ccid,nowait -usb -device usb-ccid -device ccid-card-passthru,chardev=ccid + +on the client run vscclient, built when you built the libcacard library: + libcacard/vscclient <qemu-host> 2001 + +6. Using ccid-card-passthru with client side certificates + +Run qemu as per #5, and run vscclient as follows: +(Note: vscclient command line interface is in a state of change) + + libcacard/vscclient -e "db=\"/etc/pki/nssdb\" use_hw=no soft=(,Test,CAC,,cert1,cert2,cert3)" <qemu-host> 2001 + +7. Passthrough protocol scenario + +This is a typical interchange of messages when using the passthru card device. +usb-ccid is a usb device. It defaults to an unattached usb device on startup. +usb-ccid expects a chardev and expects the protocol defined in +cac_card/vscard_common.h to be passed over that. +The usb-ccid device can be in one of three modes: + * detached + * attached with no card + * attached with card + +A typical interchange is: (the arrow shows who started each exchange, it can be client +originated or guest originated) + +client event | vscclient | passthru | usb-ccid | guest event +---------------------------------------------------------------------------------------------- + | VSC_Init | | | + | VSC_ReaderAdd | | attach | + | | | | sees new usb device. +card inserted -> | | | | + | VSC_ATR | insert | insert | see new card + | | | | + | VSC_APDU | VSC_APDU | | <- guest sends APDU +client<->physical | | | | +card APDU exchange| | | | +client response ->| VSC_APDU | VSC_APDU | | receive APDU response + ... + [APDU<->APDU repeats several times] + ... +card removed -> | | | | + | VSC_CardRemove | remove | remove | card removed + ... + [(card insert, apdu's, card remove) repeat] + ... +kill/quit | | | | + vscclient | | | | + | VSC_ReaderRemove | | detach | + | | | | usb device removed. + + +8. libcacard + +ccid-card-passthru and vscclient use libcacard as the card emulator. +libcacard implements a completely virtual CAC (DoD standard for smart cards) +compliant card and uses NSS to actually retrive certificates and do any +encryption using the backend (real reader + card or file backed certificates). + +For documentation of cac_card see README in libcacard subdirectory. +
Signature · 1 line
1.7.4.1
PATCH v20 4/7 libcacard: initial commit
In reply to Alon LevyFrom: Robert Relyea <[email protected]>
libcacard emulates a Common Access Card (CAC) which is a standard for smartcards. It is used by the emulated ccid card introduced in a following patch. Docs are available in docs/libcacard.txt
---
changes from v19->v20:
* checkpatch.pl
changes from v15->v16:
Build:
* don't erase self with distclean
* fix make clean after make distclean
* Makefile: make vscclient link quiet
Behavioral:
* vcard_emul_nss: load coolkey in more situations
* vscclient:
* use hton,ntoh
* send init on connect, only start vevent thread on response
* read payload after header check, before type switch
* remove Reconnect
* update for vscard_common changes, empty Flush implementation
Style/Whitespace:
* fix wrong variable usage
* remove unused variable
* use only C style comments
* add copyright header
* fix tabulation
--- Makefile | 6 +- Makefile.objs | 5 + Makefile.target | 2 + configure | 25 + docs/libcacard.txt | 483 +++++++++++++++++ libcacard/Makefile | 14 + libcacard/cac.c | 411 +++++++++++++++ libcacard/cac.h | 20 + libcacard/card_7816.c | 780 ++++++++++++++++++++++++++++ libcacard/card_7816.h | 60 +++ libcacard/card_7816t.h | 163 ++++++ libcacard/event.c | 112 ++++ libcacard/eventt.h | 28 + libcacard/link_test.c | 20 + libcacard/mutex.h | 59 +++ libcacard/passthru.c | 612 ++++++++++++++++++++++ libcacard/passthru.h | 50 ++ libcacard/vcard.c | 350 +++++++++++++ libcacard/vcard.h | 85 +++ libcacard/vcard_emul.h | 62 +++ libcacard/vcard_emul_nss.c | 1195 +++++++++++++++++++++++++++++++++++++++++++ libcacard/vcard_emul_type.c | 60 +++ libcacard/vcard_emul_type.h | 29 + libcacard/vcardt.h | 66 +++ libcacard/vevent.h | 26 + libcacard/vreader.c | 526 +++++++++++++++++++ libcacard/vreader.h | 54 ++ libcacard/vreadert.h | 23 + libcacard/vscclient.c | 744 +++++++++++++++++++++++++++ 29 files changed, 6068 insertions(+), 2 deletions(-) create mode 100644 docs/libcacard.txt create mode 100644 libcacard/Makefile create mode 100644 libcacard/cac.c create mode 100644 libcacard/cac.h create mode 100644 libcacard/card_7816.c create mode 100644 libcacard/card_7816.h create mode 100644 libcacard/card_7816t.h create mode 100644 libcacard/event.c create mode 100644 libcacard/eventt.h create mode 100644 libcacard/link_test.c create mode 100644 libcacard/mutex.h create mode 100644 libcacard/passthru.c create mode 100644 libcacard/passthru.h create mode 100644 libcacard/vcard.c create mode 100644 libcacard/vcard.h create mode 100644 libcacard/vcard_emul.h create mode 100644 libcacard/vcard_emul_nss.c create mode 100644 libcacard/vcard_emul_type.c create mode 100644 libcacard/vcard_emul_type.h create mode 100644 libcacard/vcardt.h create mode 100644 libcacard/vevent.h create mode 100644 libcacard/vreader.c create mode 100644 libcacard/vreader.h create mode 100644 libcacard/vreadert.h create mode 100644 libcacard/vscclient.c
Makefile +4 -2
@@ -173,6 +173,8 @@ check-qlist: check-qlist.o qlist.o qint.o $(CHECK_PROG_DEPS) check-qfloat: check-qfloat.o qfloat.o $(CHECK_PROG_DEPS) check-qjson: check-qjson.o qfloat.o qint.o qdict.o qstring.o qlist.o qbool.o qjson.o json-streamer.o json-lexer.o json-parser.o $(CHECK_PROG_DEPS) +QEMULIBS=libhw32 libhw64 libuser libdis libdis-user + clean: # avoid old build problems by removing potentially incorrect old files rm -f config.mak op-i386.h opc-i386.h gen-op-i386.h op-arm.h opc-arm.h gen-op-arm.h
@@ -184,7 +186,7 @@ clean: rm -f trace-dtrace.dtrace trace-dtrace.dtrace-timestamp rm -f trace-dtrace.h trace-dtrace.h-timestamp $(MAKE) -C tests clean - for d in $(ALL_SUBDIRS) libhw32 libhw64 libuser libdis libdis-user; do \ + for d in $(ALL_SUBDIRS) $(QEMULIBS) libcacard; do \ if test -d $$d; then $(MAKE) -C $$d $@ || exit 1; fi; \ rm -f $$d/qemu-options.def; \ done
@@ -195,7 +197,7 @@ distclean: clean rm -f roms/seabios/config.mak roms/vgabios/config.mak rm -f qemu-doc.info qemu-doc.aux qemu-doc.cp qemu-doc.dvi qemu-doc.fn qemu-doc.info qemu-doc.ky qemu-doc.log qemu-doc.pdf qemu-doc.pg qemu-doc.toc qemu-doc.tp qemu-doc.vr rm -f qemu-tech.info qemu-tech.aux qemu-tech.cp qemu-tech.dvi qemu-tech.fn qemu-tech.info qemu-tech.ky qemu-tech.log qemu-tech.pdf qemu-tech.pg qemu-tech.toc qemu-tech.tp qemu-tech.vr - for d in $(TARGET_DIRS) libhw32 libhw64 libuser libdis libdis-user; do \ + for d in $(TARGET_DIRS) $(QEMULIBS); do \ rm -rf $$d || exit 1 ; \ done
Makefile.objs +5 -0
@@ -317,6 +317,11 @@ user-obj-y += qemu-timer-common.o endif endif +###################################################################### +# smartcard + +libcacard-y = cac.o event.o passthru.o vcard.o vreader.o vcard_emul_nss.o vcard_emul_type.o card_7816.o + vl.o: QEMU_CFLAGS+=$(GPROF_CFLAGS) vl.o: QEMU_CFLAGS+=$(SDL_CFLAGS)
Makefile.target +2 -0
@@ -340,6 +340,8 @@ obj-y += $(addprefix $(HWDIR)/, $(hw-obj-y)) endif # CONFIG_SOFTMMU +obj-y += $(addprefix ../libcacard/, $(libcacard-$(CONFIG_SMARTCARD))) + obj-y += $(addprefix ../, $(trace-obj-y)) obj-$(CONFIG_GDBSTUB_XML) += gdbstub-xml.o
configure +25 -0
@@ -2291,6 +2291,19 @@ EOF fi fi +# check for libcacard for smartcard support +smartcard_cflags="-I\$(SRC_PATH)/libcacard" +libcacard_libs=$($pkgconfig --libs nss 2>/dev/null) +libcacard_cflags=$($pkgconfig --cflags nss) +# TODO - what's the minimal nss version we support? +if $pkgconfig --atleast-version=3.12.8 nss; then + smartcard="yes" + QEMU_CFLAGS="$QEMU_CFLAGS $smartcard_cflags $libcacard_cflags" + LIBS="$libcacard_libs $LIBS" +else + smartcard="no" +fi + ########################################## ##########################################
@@ -3152,6 +3165,11 @@ fi if test "$target_darwin_user" = "yes" ; then echo "CONFIG_DARWIN_USER=y" >> $config_target_mak fi +if test "$smartcard" = "yes" ; then + echo "subdir-$target: subdir-libcacard" >> $config_host_mak + echo "libcacard_libs=$libcacard_libs" >> $config_host_mak + echo "libcacard_cflags=$libcacard_cflags" >> $config_host_mak +fi list="" if test ! -z "$gdb_xml_files" ; then for x in $gdb_xml_files; do
@@ -3365,6 +3383,13 @@ for hwlib in 32 64; do echo "QEMU_CFLAGS+=-DTARGET_PHYS_ADDR_BITS=$hwlib" > $d/config.mak done +if [ $source_path != $workdir ]; then + # out of tree build + mkdir -p libcacard + rm -f libcacard/Makefile + ln -s $source_path/libcacard/Makefile libcacard/Makefile +fi + d=libuser mkdir -p $d symlink $source_path/Makefile.user $d/Makefile
docs/libcacard.txt new file +483 -0
483 lines not on this page · raw message
libcacard/Makefile new file +14 -0
@@ -0,0 +1,14 @@ +-include ../config-host.mak +-include $(SRC_PATH)/Makefile.objs +-include $(SRC_PATH)/rules.mak + +$(call set-vpath, $(SRC_PATH):$(SRC_PATH)/libcacard) + +vscclient: $(libcacard-y) vscclient.o + $(call quiet-command,$(CC) $(libcacard_libs) -o $@ $^," LINK $(TARGET_DIR)$@") + +all: vscclient + +clean: + rm -f *.o */*.o *.d */*.d *.a */*.a *~ */*~ vscclient +
libcacard/cac.c new file +411 -0
411 lines not on this page · raw message
libcacard/cac.h new file +20 -0
@@ -0,0 +1,20 @@ +/* +* defines the entry point for the cac card. Only used by cac.c anc +* vcard_emul_type.c +*/ +#ifndef CAC_H +#define CAC_H 1 +#include "vcard.h" +#include "vreader.h" +/* +* Initialize the cac card. This is the only public function in this file. All +* the rest are connected through function pointers. +*/ +VCardStatus cac_card_init (VReader *reader,VCard *card,const char *params, +unsigned char *const *cert,int cert_len[], +VCardKey *key[]/* adopt the keys*/ , +int cert_count); + +/* not yet implemented */ +VCardStatus cac_is_cac_card (VReader *reader); +#endif
libcacard/card_7816.c new file +780 -0
780 lines not on this page · raw message
libcacard/card_7816.h new file +60 -0
60 lines not on this page · raw message
libcacard/card_7816t.h new file +163 -0
163 lines not on this page · raw message
libcacard/event.c new file +112 -0
112 lines not on this page · raw message
libcacard/eventt.h new file +28 -0
@@ -0,0 +1,28 @@ +/* +* +*/ + +#ifndef EVENTT_H +#define EVENTT_H 1 +#include "vreadert.h" +#include "vcardt.h" + +typedef struct VEventStruct VEvent ; + +typedef enum { +VEVENT_READER_INSERT , +VEVENT_READER_REMOVE , +VEVENT_CARD_INSERT , +VEVENT_CARD_REMOVE , +VEVENT_LAST , +}VEventType ; + +struct VEventStruct { +VEvent *next; +VEventType type; +VReader *reader; +VCard *card; +}; +#endif + +
libcacard/link_test.c new file +20 -0
@@ -0,0 +1,20 @@ +/* +* +*/ +#include <stdio.h> +#include "vcard.h" + +VCardStatus cac_card_init (const char *flags,VCard *card, +const unsigned char *cert[], +int cert_len[],VCardKey *key[]/* adopt the keys*/ , +int cert_count); +/* +* this will crash... just test the linkage right now +*/ + +main(int argc, char **argv) +{ + VCard *card; /* no constructor yet */ +cac_card_init ("" , card,NULL ,0 ,NULL ,0 ); +} +
libcacard/mutex.h new file +59 -0
59 lines not on this page · raw message
libcacard/passthru.c new file +612 -0
612 lines not on this page · raw message
libcacard/passthru.h new file +50 -0
50 lines not on this page · raw message
libcacard/vcard.c new file +350 -0
102 lines not on this page · raw message
libcacard/vcard.h new file +85 -0
85 lines not on this page · raw message
libcacard/vcard_emul.h new file +62 -0
62 lines not on this page · raw message
libcacard/vcard_emul_nss.c new file +1195 -0
1,195 lines not on this page · raw message
libcacard/vcard_emul_type.c new file +60 -0
60 lines not on this page · raw message
libcacard/vcardt.h new file +66 -0
66 lines not on this page · raw message
libcacard/vreader.c new file +526 -0
526 lines not on this page · raw message
libcacard/vreader.h new file +54 -0
54 lines not on this page · raw message
libcacard/vscclient.c new file +744 -0
744 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
1.7.4.1
Part of the diff is not shown here. View the raw message.
PATCH v20 7/7 ccid: configure: improve --enable-smartcard flags
In reply to Alon Levy * add --enable-smartcard and --disable-smartcard flags
* let the nss check only disable building the ccid-card-emulated device
* report only if nss is found or not, not smartcard build inclusion
* don't link with NSS if --disable-smartcard-nss
--- Makefile.objs | 3 ++- Makefile.target | 2 +- configure | 55 ++++++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 45 insertions(+), 15 deletions(-)
Makefile.objs +2 -1
@@ -197,7 +197,8 @@ hw-obj-$(CONFIG_APM) += pm_smbus.o apm.o hw-obj-$(CONFIG_DMA) += dma.o hw-obj-$(CONFIG_HPET) += hpet.o hw-obj-$(CONFIG_APPLESMC) += applesmc.o -hw-obj-$(CONFIG_SMARTCARD) += usb-ccid.o ccid-card-passthru.o ccid-card-emulated.o +hw-obj-$(CONFIG_SMARTCARD) += usb-ccid.o ccid-card-passthru.o +hw-obj-$(CONFIG_SMARTCARD_NSS) += ccid-card-emulated.o # PPC devices hw-obj-$(CONFIG_OPENPIC) += openpic.o
Makefile.target +1 -1
@@ -340,7 +340,7 @@ obj-y += $(addprefix $(HWDIR)/, $(hw-obj-y)) endif # CONFIG_SOFTMMU -obj-y += $(addprefix ../libcacard/, $(libcacard-$(CONFIG_SMARTCARD))) +obj-y += $(addprefix ../libcacard/, $(libcacard-$(CONFIG_SMARTCARD_NSS))) obj-y += $(addprefix ../, $(trace-obj-y)) obj-$(CONFIG_GDBSTUB_XML) += gdbstub-xml.o
configure +42 -13
104 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v20 3/7 ccid: add passthru card device
In reply to Alon LevyThe passthru ccid card is a device sitting on the usb-ccid bus and using a chardevice to communicate with a remote device using the VSCard protocol defined in libcacard/vscard_common.h
Usage docs available in following patch in docs/ccid.txt
---
Changes from v19->v20:
* checkpatch.pl
Changes from v18->v19:
* add qdev.desc
* remove .qdev.unplug (no hot unplug support for ccid bus)
Changes from v16->v17:
* fix wrong cast when receiving VSC_Error
* ccid-card-passthru: force chardev user wakeup by sending Init see lengthy comment below.
Changes from v15->v16:
Behavioral changes:
* return correct size
* return error instead of assert if client sent too large ATR
* don't assert if client sent too large a size, but add asserts for indices to buffer
* reset vscard_in indices on chardev disconnect
* handle init from client
* error if no chardev supplied
* use ntoh, hton
* eradicate reader_id_t
* remove Reconnect usage (removed from VSCARD protocol)
* send VSC_SUCCESS on card insert/remove and reader add/remove
Style fixes:
* width of line fix
* update copyright
* remove old TODO's
* update file header comment
* use macros for debug levels
* c++ style comment replacement
* update copyright license
* fix ATR size comment
* fix whitespace in struct def
* fix DPRINTF prefix
* line width fix
ccid-card-passthru: force chardev user wakeup by sending Init
The problem: how to wakeup the user of the smartcard when the smartcard device is initialized?
Long term solution: have a callback interface. This was done via the deprecated so called chardev ioctl interface.
Short term solution: do a write. Specifically we write an Init message. And we change the client to send it's own Init message regardless of receiving this one. Additional Init messages will be regarded as acceptable, the first one received after connection establishment is the determining one wrt capabilities.
--- Makefile.objs | 2 +- hw/ccid-card-passthru.c | 337 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 338 insertions(+), 1 deletions(-) create mode 100644 hw/ccid-card-passthru.c
Makefile.objs +1 -1
@@ -197,7 +197,7 @@ hw-obj-$(CONFIG_APM) += pm_smbus.o apm.o hw-obj-$(CONFIG_DMA) += dma.o hw-obj-$(CONFIG_HPET) += hpet.o hw-obj-$(CONFIG_APPLESMC) += applesmc.o -hw-obj-$(CONFIG_SMARTCARD) += usb-ccid.o +hw-obj-$(CONFIG_SMARTCARD) += usb-ccid.o ccid-card-passthru.o # PPC devices hw-obj-$(CONFIG_OPENPIC) += openpic.o
hw/ccid-card-passthru.c new file +337 -0
337 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v20 5/7 ccid: add ccid-card-emulated device
In reply to Alon LevyThis devices uses libcacard (internal) to emulate a smartcard conforming to the CAC standard. It attaches to the usb-ccid bus. Usage instructions (example command lines) are in the following patch in docs/ccid.txt. It uses libcacard which uses nss, so it can work with both hw cards and certificates (files).
---
changes from v19->v20:
* checkpatch.pl
changes from v18->v19:
* add qdev.desc
* backend: drop the enumeration property, back to using a string one.
changes from v16->v17:
* use PROP_TYPE_ENUM for backend
changes from v15->v16:
* fix error reporting in initfn
* bump copyright year
* update copyright license
changes from v1:
* remove stale comments, use only c-style comments
* bugfix, forgot to set recv_len
* change reader name to 'Virtual Reader'
--- Makefile.objs | 2 +- hw/ccid-card-emulated.c | 599 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 600 insertions(+), 1 deletions(-) create mode 100644 hw/ccid-card-emulated.c
Makefile.objs +1 -1
@@ -197,7 +197,7 @@ hw-obj-$(CONFIG_APM) += pm_smbus.o apm.o hw-obj-$(CONFIG_DMA) += dma.o hw-obj-$(CONFIG_HPET) += hpet.o hw-obj-$(CONFIG_APPLESMC) += applesmc.o -hw-obj-$(CONFIG_SMARTCARD) += usb-ccid.o ccid-card-passthru.o +hw-obj-$(CONFIG_SMARTCARD) += usb-ccid.o ccid-card-passthru.o ccid-card-emulated.o # PPC devices hw-obj-$(CONFIG_OPENPIC) += openpic.o
hw/ccid-card-emulated.c new file +599 -0
599 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v20 1/7 usb-ccid: add CCID bus
In reply to Alon LevyA CCID device is a smart card reader. It is a USB device, defined at [1]. This patch introduces the usb-ccid device that is a ccid bus. Next patches will introduce two card types to use it, a passthru card and an emulated card.
[1] http://www.usb.org/developers/devclass_docs/DWG_Smart-Card_CCID_Rev110.
---
changes from v19->v20:
* checkpatch.pl
changes from v18->v19:
* merged: ccid.h: add copyright, fix define and remove non C89 comments
* add qdev.desc
changes from v15->v16:
Behavioral changes:
* fix abort on client answer after card remove
* enable migration
* remove side affect code from asserts
* return consistent self-powered state
* mask out reserved bits in ccid_set_parameters
* add missing abRFU in SetParameters (no affect on linux guest)
whitefixes / comments / consts defines:
* remove stale comment
* remove ccid_print_pending_answers if no DEBUG_CCID
* replace printf's with DPRINTF, remove DEBUG_CCID, add verbosity defines
* use error_report
* update copyright (most of the code is not original)
* reword known bug comment
* add missing closing quote in comment
* add missing whitespace on one line
* s/CCID_SetParameter/CCID_SetParameters/
* add comments
* use define for max packet size
Comment for "return consistent self-powered state":
the Configuration Descriptor bmAttributes claims we are self powered, but we were returning not self powered to USB_REQ_GET_STATUS control message.
In practice, this message is not sent by a linux 2.6.35.10-74.fc14.x86_64 guest (not tested on other guests), unless you issue lsusb -v as root (for example).
--- Makefile.objs | 1 + configure | 6 + hw/ccid.h | 54 +++ hw/usb-ccid.c | 1391 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 1452 insertions(+), 0 deletions(-) create mode 100644 hw/ccid.h create mode 100644 hw/usb-ccid.c
Makefile.objs +1 -0
@@ -197,6 +197,7 @@ hw-obj-$(CONFIG_APM) += pm_smbus.o apm.o hw-obj-$(CONFIG_DMA) += dma.o hw-obj-$(CONFIG_HPET) += hpet.o hw-obj-$(CONFIG_APPLESMC) += applesmc.o +hw-obj-$(CONFIG_SMARTCARD) += usb-ccid.o # PPC devices hw-obj-$(CONFIG_OPENPIC) += openpic.o
configure +6 -0
@@ -174,6 +174,7 @@ trace_backend="nop" trace_file="trace" spice="" rbd="" +smartcard="yes" # parse CC options first for opt do
@@ -2523,6 +2524,7 @@ echo "Trace output file $trace_file-<pid>" echo "spice support $spice" echo "rbd support $rbd" echo "xfsctl support $xfs" +echo "smartcard support $smartcard" if test $sdl_too_old = "yes"; then echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -2804,6 +2806,10 @@ if test "$spice" = "yes" ; then echo "CONFIG_SPICE=y" >> $config_host_mak fi +if test "$smartcard" = "yes" ; then + echo "CONFIG_SMARTCARD=y" >> $config_host_mak +fi + # XXX: suppress that if [ "$bsd" = "yes" ] ; then echo "CONFIG_BSD=y" >> $config_host_mak
hw/ccid.h new file +54 -0
54 lines not on this page · raw message
hw/usb-ccid.c new file +1391 -0
1,391 lines not on this page · raw message
Signature · 1 line
1.7.4.1
Ping.
Quoted 268 lines · Alon Levy
This patchset adds three new devices, usb-ccid, ccid-card-passthru and ccid-card-emulated, providing a CCID bus, a simple passthru protocol implementing card requiring a client, and a standalone emulated card.
It also introduces a new directory libcaccard with CAC card emulation, CAC is a type of ISO 7816 smart card.
Tree for pull:
git://anongit.freedesktop.org/~alon/qemuusb_ccid.v20
v19->v20changes:
* checkpatch.pl. Here are the remaining errors with explanation:
* ignored 5 macro errors of the type
"ERROR: Macros with complex values should be enclosed in parenthesis" because fixing them breaks current code, if it really bothers someone I can fix it.
* four of them are inlibcacard/card_7816t.h:/* give the subfields a unified look */ ..#define
a_claa_header->ah_cla/* class */
#definea_insa_header->ah_ins/* instruction */
#definea_p1a_header->ah_p1/* parameter 1 */
#definea_p2a_header->ah_p2/* parameter 2 */
* and the fifth:
#4946: FILE:libcacard/vcardt.h:31:(quoted diff) +2
-0+#define VCARD_ATR_PREFIX(size) 0x3b, 0x66+(size), 0x00, 0xff, \ + 'V', 'C', 'A', 'R', 'D', '_' * Ignored this warning since I couldn't figure it out, and it's a test file:WARNING: externs should be avoided in .c files #2343: FILE:
libcacard/link_test.c:7:
+VCardStatus cac_card_init(const char *flags, VCard *card,v18-v19 changes:
* more merges, down to a single digit number of patches.
* drop enumeration property, use string.
* rebased (trivial)v17-v18 changes:
* mergevscard_common.hpatches.
* actually provide a tree to pull.v16-v17 changes:
* merged all the "v15->v16" patches
* merged some more wherever it was easy (all same file commits).
* added signed off by to first four patches
*ccid.h: added copyright, removed underscore in defines, and replaced non C89 commentsv15-v16 changes:
* splitvscard_commonintroducing patch for ease of review
* sum of commit logs for the v15-v16 commits: (whitespace fixes
removed for space, see original commit messages in later patches)
* usb-ccid:
* fix abort on client answer after card remove
* enable migration
* remove side affect code from asserts
* return consistent self-powered state
* mask out reserved bits inccid_set_parameters
* add missing abRFU in SetParameters (no affect on linux guest)
*vscard_common.hprotocol change:
* VSCMsgInit capabilities and magic
* removed ReaderResponse, will use Error instead with code==VSC_SUCCESS.
* added Flush and FlushComplete, remove Reconnect.
* defineVSCARD_MAGIC
* added error codeVSC_SUCCESS.
* ccid-card-passthru
* return correct size
* return error instead of assert if client sent too large ATR
* don't assert if client sent too large a size, but add asserts for indices to buffer
* resetvscard_inindices on chardev disconnect
* handle init from client
* error if no chardev supplied
* use ntoh, hton
* eradicatereader_id_t
* remove Reconnect usage (removed from VSCARD protocol)
* sendVSC_SUCCESSon card insert/remove and reader add/remove
* ccid-card-emulated
* fix error reporting in initfnv14-v15 changes:
* add patch with --enable-smartcard and --disable-smartcard and only disable ccid-card-emulated if nss not found.
* add patch with description strings
* s/libcaccard/libcacard/ indocs/ccid.txtv13-v14 changes:
- support device_del/device_add on ccid-card-* and usb-ccid
* usb-ccid:
* lose card reference when card device deleted
* check slot number and deny adding a slot if one is already added.
* ccid-card-*: useqdev_simple_unplug_cbin both emulated and passthru ccid cards, the exitfn already takes care of triggering card removal in the usb dev.
* libcacard:
* remove double include of config-host.mak
* add replay of card events to libcacard to support second and more emulation
* don't initialize more then once (doesn't support it right now, so one thread, NSS thread, is left whendevice_delis done)
* addVCARD_EMUL_INIT_ALREADY_INITED
* ccid-card-emulated:
* take correct mutexes on signaling to fix deadlocks ondevice_del
* allow card insertion/removal event without proper reader insertion eventv12-v13 changes:
* libcacard:
* fixMakefileclean to remove vscclient
* fix double include of config-host inMakefile
* usb-ccid: remove attach/detach logic, usb is always attached. Guest doesn't care if there is a reader attached with no card anyway.
* ccid-card-passthru: don't closechr_devon removal, makes it possible to use device_del/device_add to create remove/insertion for debugging.v11-v12 changes:
* fix out of tree buildv10-v11 changes:
* fix last patch that removed one of the doc files.
* updated flow table indocs/ccid.txtv8-v10 changes:
* usb-ccid:
* add slot for future use (Gerd)
* ifdefENABLE_MIGRATIONfor migration support on account of usb migration not being ready in general. (Gerd)
* verbosified commit messages. (Gerd)
* put libcacard docs in libcacard commit. (Gerd)v8-v9 changes:
* Blue Swirl comments:
* white space fixes
* enabled by default, disabled only if missing nss
* forgotten fix from v8 (don't build libcacard.so)
* added a note about device being little endian
* library renamed from libcaccard to libcacard
* squashed both of libcacard patches, they touched different files anyway.v7-v8 changes:
* Blue Swirl comments:
* usb-ccid: deannonymize some structs
* usb-ccid: coding style change -answer_tandbulk_in_tfixed
* usb-ccid: handle endianess conversion between guest and host
* usb-ccid: s/ccid_bulk_in_copy_out/ccid_bulk_in_copy_to_guest/
* ccid-card-emulated: fix segfault if backend not specified
* ccid-card-emulated: let last reader inserted win
* libcaccard: remove doublevscard_common.h
v6->v7changes:
* external libcaccard became internal directory libcaccard
* statically link object files into qemu
* produce libcaccard.so for usage by external projects
* applied coding style to new code (please check me)
- did not use the qemu options parsing for libcaccard, since it seems to draw large amounts of qemu code (monitor for instance).
v5->v6changes:
* really remove static debug (I apologize for claiming to have done so before)
v4->v5changes:
* rebased to latest
* remove static debug in card devices
* fix --enable-smartcard to link
* stall instead of assert when exceedingBULK_OUT_DATA_SIZE
* makeccid_reserve_recv_buffor too large len discard message, not exit
* makeccid_reserve_recv_bufreturn void*
* fix typo
* remove commented code in VMState
v3->v4:
* remove ccid field in CCIDBus
* remove static debug in bus
* add back docs
v2->v3:
* split into bus (usb-ccid.c, usesccid.h) and card (ccid-card-passthru.c).
* removed documentation (being revised).
v1->v2:
* all QSIMPLEQ turned into fixed sized rings
* all allocated buffers turned into fixed size buffers
* added migration support
* added a message to tell client qemu has migrated to ip:port
* for lack of monitor commands ip:port are 0:0, which causes the updated vscclient to connect to one port higher on the same host. will add monitor commands in a separate patch. tested with current setup.Alon Levy (6):
usb-ccid: add CCID bus
introducelibcacard/vscard_common.h
ccid: add passthru card device
ccid: add ccid-card-emulated device
ccid: add docs
ccid: configure: improve --enable-smartcard flagsRobert Relyea (1):
libcacard: initial commitMakefile | 6 +- Makefile.objs | 7 + Makefile.target | 2 + configure | 60 ++ docs/ccid.txt | 135 +++++ docs/libcacard.txt | 483 +++++++++++++++ hw/ccid-card-emulated.c | 599 +++++++++++++++++++ hw/ccid-card-passthru.c | 337 +++++++++++ hw/ccid.h | 54 ++ hw/usb-ccid.c | 1391 +++++++++++++++++++++++++++++++++++++++++++ libcacard/Makefile | 14 + libcacard/cac.c | 411 +++++++++++++ libcacard/cac.h | 20 + libcacard/card_7816.c | 780 ++++++++++++++++++++++++ libcacard/card_7816.h | 60 ++ libcacard/card_7816t.h | 163 +++++ libcacard/event.c | 112 ++++ libcacard/eventt.h | 28 + libcacard/link_test.c | 20 + libcacard/mutex.h | 59 ++ libcacard/passthru.c | 612 +++++++++++++++++++ libcacard/passthru.h | 50 ++ libcacard/vcard.c | 350 +++++++++++ libcacard/vcard.h | 85 +++ libcacard/vcard_emul.h | 62 ++ libcacard/vcard_emul_nss.c | 1195 +++++++++++++++++++++++++++++++++++++ libcacard/vcard_emul_type.c | 60 ++ libcacard/vcard_emul_type.h | 29 + libcacard/vcardt.h | 66 ++ libcacard/vevent.h | 26 + libcacard/vreader.c | 526 ++++++++++++++++ libcacard/vreader.h | 54 ++ libcacard/vreadert.h | 23 + libcacard/vscard_common.h | 167 ++++++ libcacard/vscclient.c | 744 +++++++++++++++++++++++ 35 files changed, 8788 insertions(+), 2 deletions(-) create mode 100644 docs/ccid.txt create mode 100644 docs/libcacard.txt create mode 100644 hw/ccid-card-emulated.c create mode 100644 hw/ccid-card-passthru.c create mode 100644 hw/ccid.h create mode 100644 hw/usb-ccid.c create mode 100644 libcacard/Makefile create mode 100644 libcacard/cac.c create mode 100644 libcacard/cac.h create mode 100644 libcacard/card_7816.c create mode 100644 libcacard/card_7816.h create mode 100644 libcacard/card_7816t.h create mode 100644 libcacard/event.c create mode 100644 libcacard/eventt.h create mode 100644 libcacard/link_test.c create mode 100644 libcacard/mutex.h create mode 100644 libcacard/passthru.c create mode 100644 libcacard/passthru.h create mode 100644 libcacard/vcard.c create mode 100644 libcacard/vcard.h create mode 100644 libcacard/vcard_emul.h create mode 100644 libcacard/vcard_emul_nss.c create mode 100644 libcacard/vcard_emul_type.c create mode 100644 libcacard/vcard_emul_type.h create mode 100644 libcacard/vcardt.h create mode 100644 libcacard/vevent.h create mode 100644 libcacard/vreader.c create mode 100644 libcacard/vreader.h create mode 100644 libcacard/vreadert.h create mode 100644 libcacard/vscard_common.h create mode 100644 libcacard/vscclient.cSignature · 1 line
1.7.4.1
Re: [Qemu-devel] [PATCH 1/7] usb-ccid: add CCID bus
In reply to Alon Levyconfigure trace_backend="nop" · line 177
Quoted 9 lines · Alon Levy
configure +1
-0@@ -174,6 +174,7 @@ trace_backend="nop" trace_file="trace" spice="" rbd="" +smartcard="yes"
IMHO smartcard support shouldn't be enabled per default. The userbase is limited.
Quoted 15 lines
hw/ccid.h new file +9
-0@@ -0,0 +1,54 @@ +/* +* CCID Passthru Card Device emulation +* +* Copyright (c) 2011 Red Hat. +* Written by Alon Levy. +* +* This code is licenced under the GNU LGPL, version 2 or later. +*/ +
[snip]
hw/ccid.h line 27
Quoted 4 lines
hw/ccid.h new file +4
-0+ +/* callbacks to be used by the CCID device (hw/usb-ccid.c) to call +* into the smartcard device (hw/ccid-card-*.c) +*/
This is inconsistent with the comment above. Normally multi-line comments in QEMU are like this:
/*
* foo
* bar
*/
hw/ccid.h line 40
Quoted 13 lines
hw/ccid.h new file +13
-0+struct CCIDCardInfo { +DeviceInfo qdev; +void (*print)(Monitor *mon,CCIDCardState *card,int indent); +const uint8_t *(*get_atr)(CCIDCardState *card,uint32_t *len); +void (*apdu_from_guest)(CCIDCardState *card, +const uint8_t *apdu, +uint32_t len); +int (*exitfn)(CCIDCardState *card); +int (*initfn)(CCIDCardState *card); +}; + +/* API for smartcard calling the CCID device (used by hw/ccid-card-*.c) +*/
again here
hw/ccid.h line 50
Quoted 10 lines
hw/ccid.h new file +10
-0+void ccid_card_send_apdu_to_guest (CCIDCardState *card, +uint8_t *apdu, +uint32_t len); +void ccid_card_card_removed (CCIDCardState *card); +void ccid_card_card_inserted (CCIDCardState *card); +void ccid_card_card_error (CCIDCardState *card,uint64_t error); +void ccid_card_qdev_register (CCIDCardInfo *card); + +/* support guest visible insertion/removal of ccid devices based on actual +* devices connected/removed. Called by card implementation (passthru, local) */
and here
hw/usb-ccid.c line 63
Quoted 11 lines
hw/usb-ccid.c new file +6
-0+#define CCID_DEV_NAME "usb-ccid" + +/* The two options for variable sized buffers: +* make them constant size, for large enough constant, +* or handle the migration complexity - VMState doesn't handle this case. +* sizes are expected never to be exceeded, unless guest misbehaves. */
here again
[snip]
hw/usb-ccid.c line 91
Quoted 6 lines
hw/usb-ccid.c new file +6
-0+/* Using Gemplus Vendor and Product id +Effect on various drivers: +* usbccid.sys (winxp, others untested) is a class driver so it doesn't care. +* linux has a number of class drivers, but openct filters based on +vendor/product (/etc/openct.conf under fedora), hence Gemplus. +*/
Something went totally boink with the comments there!
Quoted 4 lines
hw/usb-ccid.c new file +4
-0+/* 6.2.6 RDR_to_PC_SlotStatus definitions */ +enum { +CLOCK_STATUS_RUNNING =0 , +/* 0 - Clock Running, 1 - Clock stopped in State L, 2 - H,
hw/usb-ccid.c line 179
Quoted 10 lines
hw/usb-ccid.c new file +10
-0+3 - unkonwn state. rest are RFU +*/ +}; + +typedef struct __attribute__ ((__packed__ ))CCID_Header { +uint8_t bMessageType; +uint32_t dwLength; +uint8_t bSlot; +uint8_t bSeq; +}CCID_Header ;
Is this header decided upon by the CCID spec or the code? It seems suboptimal to have a uint8 in front of a uint32 like that. Inefficient structure alignment :(
Quoted 4 lines
hw/usb-ccid.c new file +4
-0+ +/* 6.1.4 PC_to_RDR_XfrBlock */ +typedef struct __attribute__ ((__packed__)) CCID_XferBlock { + CCID_Header hdr;
hw/usb-ccid.c line 216
Quoted 10 lines
hw/usb-ccid.c new file +10
-0+uint8_t bBWI;/* Block Waiting Timeout */ +uint16_t wLevelParameter;/* XXX currently unused */ +uint8_t abData[0 ]; +} CCID_XferBlock; + +typedef struct __attribute__ ((__packed__ ))CCID_IccPowerOn { +CCID_Header hdr; +uint8_t bPowerSelect; +uint16_t abRFU; +}CCID_IccPowerOn ;
Same problem with the above two structs....
hw/usb-ccid.c line 228
Quoted 11 lines
hw/usb-ccid.c new file +11
-0+typedef struct __attribute__ ((__packed__ ))CCID_IccPowerOff { +CCID_Header hdr; +uint16_t abRFU; +}CCID_IccPowerOff ; + +typedef struct __attribute__ ((__packed__ ))CCID_SetParameters { +CCID_Header hdr; +uint8_t bProtocolNum; +uint16_t abRFU; +uint8_t abProtocolDataStructure[0 ]; +}CCID_SetParameters ;
and again.
Quoted 24 lines
hw/usb-ccid.c new file +24
-0+/** +* powered - defaults to true, changed by PowerOn/PowerOff messages +*/ +struct USBCCIDState { +USBDevice dev; +CCIDBus *bus; +CCIDCardState *card; +CCIDCardInfo *cardinfo;/* caching the info pointer */ +uint8_t debug; +BulkIn bulk_in_pending[BULK_IN_PENDING_NUM ];/* circular */ +uint32_t bulk_in_pending_start; +uint32_t bulk_in_pending_end;/* first free */ +uint32_t bulk_in_pending_num; +BulkIn *current_bulk_in; +uint8_t bulk_out_data[BULK_OUT_DATA_SIZE ]; +uint32_t bulk_out_pos; +uint8_t bmSlotICCState; +uint8_t powered; +uint8_t notify_slot_change; +uint64_t last_answer_error; +Answer pending_answers[PENDING_ANSWERS_NUM ]; +uint32_t pending_answers_start; +uint32_t pending_answers_end; +uint32_t pending_answers_num;
hw/usb-ccid.c line 291
Quoted 10 lines
hw/usb-ccid.c new file +10
-0+uint8_t bError; +uint8_t bmCommandStatus; +uint8_t bProtocolNum; +uint8_t abProtocolDataStructure[MAX_PROTOCOL_SIZE ]; +uint32_t ulProtocolDataStructureSize; +uint32_t state_vmstate; +uint8_t migration_state; +uint32_t migration_target_ip; +uint16_t migration_target_port; +};
Try to place the struct elements a little better so you don't end up with a lot of space wasted due to natural alignment by the compiler.
hw/usb-ccid.c line 539
Quoted 9 lines
hw/usb-ccid.c new file +9
-0+static void ccid_bulk_in_get (USBCCIDState *s) +{ +if (s->current_bulk_in !=NULL || s->bulk_in_pending_num ==0 ) { +return ; + } +assert (s->bulk_in_pending_num >0 ); + s->bulk_in_pending_num--; + s->current_bulk_in = &s->bulk_in_pending[ + (s->bulk_in_pending_start++) %BULK_IN_PENDING_NUM ];
That line break is really not good :( Either break it after the '=' or calculate the index outside the assignment statement.
hw/usb-ccid.c line 778
Quoted 3 lines
hw/usb-ccid.c new file +3
-0+static void ccid_write_data_block ( +USBCCIDState *s,uint8_t slot,uint8_t seq, +const uint8_t *data,uint32_t len)
Please fix this - keep some arguments on the first line, and align the following ones to match.
Quoted 3 lines
+/* handle a single
USB_TOKEN_OUT, return value returned to guest.
+ * 0 - all ok
+ *USB_RET_STALL- failed to handle packet */
another badly formatted comment
hw/usb-ccid.c line 1183
Quoted 12 lines
hw/usb-ccid.c new file +12
-0+void ccid_card_card_error (CCIDCardState *card,uint64_t error) +{ +USBCCIDState *s = +DO_UPCAST (USBCCIDState, dev.qdev, card->qdev.parent_bus->parent); + + s->bmCommandStatus =COMMAND_STATUS_FAILED ; + s->last_answer_error = error; +DPRINTF (s,1 ,"VSC_Error: %lX\n" , s->last_answer_error); +/* TODO: these error's should be more verbose and propogated to the guest. +* */ +/* we flush all pending answers on CardRemove message in ccid-card-passthru, +* so check that first to not trigger abort */
!!! there's more below.
Except for the mostly cosmetic stuff, it looks ok to me.
Cheers,
Jes
Re: [Qemu-devel] [PATCH 2/7] introduce libcacard/vscard_common.h
In reply to Alon LevyQuoted 14 lines · Alon Levy
libcacard/vscard_common.h new file +8
-0@@ -0,0 +1,167 @@ +/* Virtual Smart Card protocol definition +* +* This protocol is between a host using virtual smart card readers, +* and a client providing the smart cards, perhaps by emulating them or by +* access to real cards. +* +* Definitions for this protocol: +* Host - user of the card
libcacard/vscard_common.h line 18
Quoted 10 lines
libcacard/vscard_common.h new file +10
-0+* Client - owner of the card +* +* The current implementation passes the raw APDU's from 7816 and additionally +* contains messages to setup and teardown readers, handle insertion and +* removal of cards, negotiate the protocol via capabilities and provide +* for error responses. +* +* Copyright (c) 2011 Red Hat. +* +* This code is licensed under the LGPL.
Please be more specific on the license. Other code has the following:
* This work is licensed under the terms of the GNU LGPL, version 2.1 or
later.
* See the COPYING.LIB file in the top-level directory.
[snip]
libcacard/vscard_common.h line 94
Quoted 7 lines
libcacard/vscard_common.h new file +7
-0+/* VSCMsgInit Client <-> Host +* Client sends it on connection, with its own capabilities. +* Host replies with VSCMsgInit filling in its capabilities. +* +* It is not meant to be used for negotiation, i.e. sending more then +* once from any side, but could be used for that in the future. +* */
Looks like some automatic mangling of comments - happens in more than one place.
Except for the cosmetic stuff, looks ok.
Jes
Re: [Qemu-devel] [PATCH 3/7] ccid: add passthru card device
In reply to Alon LevyQuoted 8 lines · Alon Levy
The passthru ccid card is a device sitting on the usb-ccid bus and using a chardevice to communicate with a remote device using the VSCard protocol defined in
libcacard/vscard_common.hUsage docs available in following patch in
docs/ccid.txtSigned-off-by Alon Levy <[email protected]>
Except for the usual formatting bits, this one looks fine to me.
Cheers,
Jes
Re: [Qemu-devel] [PATCH 1/7] usb-ccid: add CCID bus
In reply to Jes SorensenQuoted 13 lines · Jes Sorensen
configure trace_backend="nop" · line 177
Quoted 9 lines · Alon Levy
configure +1
-0@@ -174,6 +174,7 @@ trace_backend="nop" trace_file="trace" spice="" rbd="" +smartcard="yes"IMHO smartcard support shouldn't be enabled per default. The userbase is limited.
Deciding based on importance/size of userbase is rather subjective. IMHO all features should be enabled by default, but if they depend on a 3rd party library that isn't installed on the build host, they should automatically disable themselves. eg
* [the default] - on, if external library is present, off otherwise
* --enable-<feature> - always on, raise error if external library is missing
* --disable-<feature> - always off,
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Re: [Qemu-devel] [PATCH 1/7] usb-ccid: add CCID bus
Quoted 13 lines · Daniel P. Berrange
Quoted 12 lines · Jes Sorensen
configure trace_backend="nop" · line 177
Quoted 9 lines · Alon Levy
configure +1
-0@@ -174,6 +174,7 @@ trace_backend="nop" trace_file="trace" spice="" rbd="" +smartcard="yes"IMHO smartcard support shouldn't be enabled per default. The userbase is limited.
Quoted 4 lines
Deciding based on importance/size of userbase is rather subjective. IMHO all features should be enabled by default, but if they depend on a 3rd party library that isn't installed on the build host, they should automatically disable themselves. eg
Yes, and this is the policy we've taken pretty consistently in QEMU.
Features that aren't enabled by default are subject to bit-rot.
Regards,
Anthony Liguori
Quoted 6 lines
* [the default] - on, if external library is present, off otherwise
* --enable-<feature> - always on, raise error if external library is missing
* --disable-<feature> - always off,Regards,
Daniel
Re: [Qemu-devel] [PATCH 2/7] introduce libcacard/vscard_common.h
In reply to Jes SorensenQuoted 22 lines · Jes Sorensen
Quoted 21 lines · Alon Levy
libcacard/vscard_common.h new file +15
-0@@ -0,0 +1,167 @@ +/* Virtual Smart Card protocol definition +* +* This protocol is between a host using virtual smart card readers, +* and a client providing the smart cards, perhaps by emulating them or by +* access to real cards. +* +* Definitions for this protocol: +* Host - user of the card +* Client - owner of the card +* +* The current implementation passes the raw APDU's from 7816 and additionally +* contains messages to setup and teardown readers, handle insertion and +* removal of cards, negotiate the protocol via capabilities and provide +* for error responses. +*
Quoted 9 lines
libcacard/vscard_common.h line 18
Quoted 3 lines
libcacard/vscard_common.h new file +3
-0+* Copyright (c) 2011 Red Hat. +* +* This code is licensed under the LGPL. Please be more specific on the license. Other code has the following:
* This work is licensed under the terms of the GNU LGPL, version 2.1 or
later.
* See the COPYING.LIB file in the top-level directory.
Yes, I have this ready on the v21 branch..
Quoted 17 lines
[snip]
Quoted 7 lines
+/* VSCMsgInit Client <-> Host
+ * Client sends it on connection, with its own capabilities.
+ * Host replies with VSCMsgInit filling in its capabilities.
+ *
+ * It is not meant to be used for negotiation, i.e. sending more then
+ * once from any side, but could be used for that in the future.
+ * */Looks like some automatic mangling of comments - happens in more than one place.
Except for the cosmetic stuff, looks ok.
Jes
Re: [Qemu-devel] [PATCH 2/7] introduce libcacard/vscard_common.h
In reply to Jes SorensenQuoted 40 lines · Jes Sorensen
Quoted 14 lines · Alon Levy
libcacard/vscard_common.h new file +8
-0@@ -0,0 +1,167 @@ +/* Virtual Smart Card protocol definition +* +* This protocol is between a host using virtual smart card readers, +* and a client providing the smart cards, perhaps by emulating them or by +* access to real cards. +* +* Definitions for this protocol: +* Host - user of the card libcacard/vscard_common.h line 18
Quoted 10 lines
libcacard/vscard_common.h new file +10
-0+* Client - owner of the card +* +* The current implementation passes the raw APDU's from 7816 and additionally +* contains messages to setup and teardown readers, handle insertion and +* removal of cards, negotiate the protocol via capabilities and provide +* for error responses. +* +* Copyright (c) 2011 Red Hat. +* +* This code is licensed under the LGPL. Please be more specific on the license. Other code has the following:
* This work is licensed under the terms of the GNU LGPL, version 2.1 or
later.
* See the COPYING.LIB file in the top-level directory.[snip]
libcacard/vscard_common.h line 94
Quoted 7 lines
libcacard/vscard_common.h new file +7
-0+/* VSCMsgInit Client <-> Host +* Client sends it on connection, with its own capabilities. +* Host replies with VSCMsgInit filling in its capabilities. +* +* It is not meant to be used for negotiation, i.e. sending more then +* once from any side, but could be used for that in the future. +* */
Quoted 2 lines
Looks like some automatic mangling of comments - happens in more than one place.
What, the repeated "capabilities" word at the end of two consecutive lines? that's on purpose.
Quoted 6 lines
Except for the cosmetic stuff, looks ok.
Jes
Re: [Qemu-devel] [PATCH 3/7] ccid: add passthru card device
In reply to Jes SorensenQuoted 11 lines · Jes Sorensen
Quoted 8 lines · Alon Levy
The passthru ccid card is a device sitting on the usb-ccid bus and using a chardevice to communicate with a remote device using the VSCard protocol defined in
libcacard/vscard_common.hUsage docs available in following patch in
docs/ccid.txtSigned-off-by Alon Levy <[email protected]>Except for the usual formatting bits, this one looks fine to me.
What exactly?
Quoted 4 lines
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
In reply to Alon Levylibcacard/cac.c line 27
Quoted 10 lines · Alon Levy
libcacard/cac.c new file +10
-0+/* private data for PKI applets */ +typedef struct CACPKIAppletDataStruct { +unsigned char *cert; +int cert_len; +unsigned char *cert_buffer; +int cert_buffer_len; +unsigned char *sign_buffer; +int sign_buffer_len; +VCardKey *key; +}CACPKIAppletData ;
Grouping the ints together would allow for better struct padding.
Quoted 3 lines
+/*
+ * resest the inter call state between applet selects
+ */
I presume it meant to say 'resets' ?
libcacard/event.c line 3
Quoted 9 lines
libcacard/event.c new file +3
-0@@ -0,0 +1,112 @@ +/* +* +*/
This comment is really spot on :)
libcacard/mutex.h
Quoted 5 lines
libcacard/mutex.h new file +0
-0
UH OH!
Quoted 29 lines
libcacard/mutex.h new file +29
-0+/* +* This header file provides a way of mapping windows and linux thread calls +* to a set of macros. Ideally this would be shared by whatever subsystem we +* link with. +*/ + +#ifndef _H_MUTEX +#define _H_MUTEX +#ifdef _WIN32 +#include <windows.h> +typedef CRITICAL_SECTION mutex_t ; +#define MUTEX_INIT (mutex) InitializeCriticalSection(&mutex) +#define MUTEX_LOCK (mutex) EnterCriticalSection(&mutex) +#define MUTEX_UNLOCK (mutex) LeaveCriticalSection(&mutex) +typedef CONDITION_VARIABLE condition_t ; +#define CONDITION_INIT (cond) InitializeConditionVariable(&cond) +#define CONDITION_WAIT (cond, mutex) \ + SleepConditionVariableCS(&cond, &mutex, INFINTE) +#define CONDITION_NOTIFY (cond) WakeConditionVariable(&cond) +typedef uint32_t thread_t ; +typedef HANDLE thread_status_t ; +#define THREAD_CREATE (tid, func, arg) \ + CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, arg, 0, &tid) +#define THREAD_SUCCESS (status) ((status) != NULL) +#else +#include <pthread.h> +typedef pthread_mutex_t mutex_t ; +#define MUTEX_INIT (mutex) pthread_mutex_init(&mutex, NULL) +#define MUTEX_LOCK (mutex) pthread_mutex_lock(&mutex)
libcacard/mutex.h line 57
Quoted 10 lines
libcacard/mutex.h new file +10
-0+#define MUTEX_UNLOCK (mutex) pthread_mutex_unlock(&mutex) +typedef pthread_cond_t condition_t ; +#define CONDITION_INIT (cond) pthread_cond_init(&cond, NULL) +#define CONDITION_WAIT (cond, mutex) pthread_cond_wait(&cond, &mutex) +#define CONDITION_NOTIFY (cond) pthread_cond_signal(&cond) +typedef pthread_t thread_t ; +typedef int thread_status_t ; +#define THREAD_CREATE (tid, func, arg) pthread_create(&tid, NULL, func, arg) +#define THREAD_SUCCESS (status) ((status) == 0) +#endif
NACK! This is no good, the code needs to be fixed to use QemuMutex from qemu-thread.h
In addition, a thing like a mutex feature should be in a separate patch,
not part of the code that uses it. However QEMU already has a mutex set
so this part needs to go.
libcacard/mutex.h
Quoted 7 lines
libcacard/mutex.h new file +7
-0+static VCardAppletPrivate * +passthru_new_applet_private (VReader *reader) +{ +VCardAppletPrivate *applet_private =NULL ; +LONG rv; + + applet_private = (VCardAppletPrivate *)malloc (sizeof (VCardAppletPrivate));
qemu_malloc()
libcacard/mutex.h
Quoted 3 lines
libcacard/mutex.h new file +3
-0+if (applet_private ==NULL ) { + goto fail; + }
and it never fails.
libcacard/mutex.h
Quoted 3 lines
libcacard/mutex.h new file +3
-0+if (new_reader_list_len != reader_list_len) { +/* update the list */ + new_reader_list = (char *)malloc (new_reader_list_len);
qemu_malloc() again.
Please grep through the full patch set and make sure you do not have any direct calls to malloc() or free().
libcacard/mutex.h
Quoted 4 lines
libcacard/mutex.h new file +4
-0+/* try resetting the pcsc_lite subsystem */ +SCardReleaseContext (global_context); + global_context =0 ;/* should close it */ +printf ("***** SCard failure %x\n" , rv);
error_report()
Quoted 5 lines
libcacard/vcard_emul_nss.c new file +0
-0
[snip]
libcacard/vcard_emul_nss.c line 45
Quoted 5 lines
libcacard/vcard_emul_nss.c new file +5
-0+struct VReaderEmulStruct { +PK11SlotInfo *slot; +VCardEmulType default_type; +char *type_params; +PRBool present;
What is PRBool and where does it come from?
Quoted 4 lines
libcacard/vcard_emul_nss.c new file +4
-0+void +vcard_emul_reset (VCard *card,VCardPower power) +{ +PK11SlotInfo *slot;
libcacard/vcard_emul_nss.c line 314
Quoted 10 lines
libcacard/vcard_emul_nss.c new file +10
-0+ +if (!nss_emul_init) { +return ; + } + +/* if we reset the card (either power on or power off), we loose our login +* state */ +/* TODO: we may also need to send insertion/removal events? */ + slot =vcard_emul_card_get_slot (card); + (void )PK11_Logout (slot);
We don't (void) cast calls like that in QEMU.
Quoted 9 lines
libcacard/vcard_emul_nss.c new file +9
-0+/* +* NSS needs the app to supply a password prompt. In our case the only time +* the password is supplied is as part of the Login APDU. The actual password +* is passed in the pw_arg in that case. In all other cases pw_arg should be +* NULL. +*/ +static char * +vcard_emul_get_password (PK11SlotInfo *slot,PRBool retries,void *pw_arg) +{
libcacard/vcard_emul_nss.c line 675
Quoted 10 lines
libcacard/vcard_emul_nss.c new file +10
-0+/* if it didn't work the first time, don't keep trying */ +if (retries) { +return NULL ; + } +/* we are looking up a password when we don't have one in hand */ +if (pw_arg ==NULL ) { +return NULL ; + } +/* TODO: we really should verify that were are using the right slot */ +return PORT_Strdup (pw_arg);
PORT_Strdup???
libcacard/vcard_emul_nss.c line 983
Quoted 11 lines
libcacard/vcard_emul_nss.c new file +11
-0+static const char * +find_token (const char *str,char token,char token_end) +{ +/* just do the blind simple thing */ +for (; *str; str++) { +if ((*str == token) || (*str == token_end)) { +break ; + } + } +return str; +}
What is wrong with strpbrk(3) ?
libcacard/vcard_emul_nss.c line 991
Quoted 7 lines
libcacard/vcard_emul_nss.c new file +7
-0+static const char * +strip (const char *str) +{ +for (; *str; str++) { +if ((*str !=' ' ) && (*str !='\n' ) && + (*str !='\t' ) && (*str !='\r' )) { +break ;
!isspace() ?
libcacard/vcard_emul_nss.c line 1062
Quoted 12 lines
libcacard/vcard_emul_nss.c new file +12
-0+static const char * +find_blank (const char *str) +{ +for (; *str; str++) { +if ((*str ==' ' ) || (*str =='\n' ) || + (*str =='\t' ) || (*str =='\r' )) { + +break ; + } + } +return str; +}
strpbrk(3) ?
Quoted 13 lines
libcacard/vcardt.h new file +7
-0@@ -0,0 +1,66 @@ +/* +* +*/ +#ifndef VCARDT_H +#define VCARDT_H 1 + +/*
libcacard/vcardt.h line 17
Quoted 10 lines
libcacard/vcardt.h new file +10
-0+* these should come from some common spice header file +*/ +#include <assert.h> +#ifndef ASSERT +#define ASSERT assert +#endif +#ifndef MIN +#define MIN (x, y) ((x) > (y) ? (y) : (x)) +#define MAX (x, y) ((x) > (y) ? (x) : (y)) +#endif
QEMU uses assert(), not ASSERT(). Please fix.
libcacard/vreader.c line 3
Quoted 9 lines
libcacard/vreader.c new file +3
-0@@ -0,0 +1,526 @@ +/* +* emulate the reader +*/
What is the license of this file?
Quoted 6 lines
libcacard/vreader.c new file +6
-0+#include "vcard.h" +#include "vcard_emul.h" +#include "card_7816.h" +#include "vreader.h" +#include "vevent.h" +
libcacard/vreader.c line 19
Quoted 10 lines
libcacard/vreader.c new file +10
-0+/* +* System includes +*/ +#include <stdlib.h> +#include <string.h> + +/* +* spice includes +*/ +#include "mutex.h"
No no no
libcacard/vreadert.h line 3
Quoted 9 lines
libcacard/vreadert.h new file +3
-0@@ -0,0 +1,23 @@ +/* +* +*/
Spot on!
General comment, this patch is *way* too big. It really should be a series of patches adding features one after another. The testing for cards ought to be separate for example.
Jes
Re: [Qemu-devel] [PATCH 5/7] ccid: add ccid-card-emulated device
In reply to Alon LevyQuoted 25 lines · Alon Levy
hw/ccid-card-emulated.c new file +19
-0@@ -0,0 +1,599 @@ +/* +* CCID Card Device. Emulated card. +* +* Copyright (c) 2011 Red Hat. +* Written by Alon Levy. +* +* This code is licenced under the GNU LGPL, version 2 or later. +*/ + +/* +* It can be used to provide access to the local hardware in a non exclusive +* way, or it can use certificates. It requires the usb-ccid bus. +* +* Usage 1: standard, mirror hardware reader+card: +* qemu .. -usb -device usb-ccid -device ccid-card-emulated +* +* Usage 2: use certificates, no hardware required +* one time: create the certificates: +* for i in 1 2 3; do
hw/ccid-card-emulated.c line 29
Quoted 10 lines
hw/ccid-card-emulated.c new file +10
-0+* certutil -d /etc/pki/nssdb -x -t "CT,CT,CT" -S -s "CN=user$i" -n user$i +* done +* qemu .. -usb -device usb-ccid \ +* -device ccid-card-emulated,cert1=user1,cert2=user2,cert3=user3 +* +* If you use a non default db for the certificates you can specify it using +* the db parameter. +*/ + +#include <pthread.h>
qemu-thread.h
hw/ccid-card-emulated.c line 78
Quoted 10 lines
hw/ccid-card-emulated.c new file +10
-0+static const char *emul_event_to_string (uint32_t emul_event) +{ +switch (emul_event) { +case EMUL_READER_INSERT :return "EMUL_READER_INSERT" ; +case EMUL_READER_REMOVE :return "EMUL_READER_REMOVE" ; +case EMUL_CARD_INSERT :return "EMUL_CARD_INSERT" ; +case EMUL_CARD_REMOVE :return "EMUL_CARD_REMOVE" ; +case EMUL_GUEST_APDU :return "EMUL_GUEST_APDU" ; +case EMUL_RESPONSE_APDU :return "EMUL_RESPONSE_APDU" ; +case EMUL_ERROR :return "EMUL_ERROR" ;
YUCK!
No multi statements on a single line!
Quoted 14 lines
hw/ccid-card-emulated.c new file +14
-0+#define MAX_ATR_SIZE 40 +struct EmulatedState { +CCIDCardState base; +uint8_t debug; +char *backend_str; +uint32_t backend; +char *cert1; +char *cert2; +char *cert3; +char *db; +uint8_t atr[MAX_ATR_SIZE ]; +uint8_t atr_length; + QSIMPLEQ_HEAD(event_list, EmulEvent) event_list; +pthread_mutex_t event_list_mutex;
hw/ccid-card-emulated.c line 126
Quoted 10 lines
hw/ccid-card-emulated.c new file +10
-0+VReader *reader; + QSIMPLEQ_HEAD(guest_apdu_list, EmulEvent) guest_apdu_list; +pthread_mutex_t vreader_mutex;/* and guest_apdu_list mutex */ +pthread_mutex_t handle_apdu_mutex; +pthread_cond_t handle_apdu_cond; +int pipe[2 ]; +int quit_apdu_thread; +pthread_mutex_t apdu_thread_quit_mutex; +pthread_cond_t apdu_thread_quit_cond; +};
Bad struct packing and wrong thread types.
hw/ccid-card-emulated.c line 166
Quoted 3 lines
hw/ccid-card-emulated.c new file +3
-0+static void emulated_push_type (EmulatedState *card,uint32_t type) +{ +EmulEvent *event = (EmulEvent *)malloc (sizeof (EmulEvent));
qemu_malloc()
hw/ccid-card-emulated.c line 175
Quoted 8 lines
hw/ccid-card-emulated.c new file +8
-0+assert (event); + event->p.gen.type = type; +emulated_push_event (card, event); +} + +static void emulated_push_error (EmulatedState *card,uint64_t code) +{ +EmulEvent *event = (EmulEvent *)malloc (sizeof (EmulEvent));
qemu_malloc()
hw/ccid-card-emulated.c line 186
Quoted 10 lines
hw/ccid-card-emulated.c new file +10
-0+assert (event); + event->p.error.type =EMUL_ERROR ; + event->p.error.code = code; +emulated_push_event (card, event); +} + +static void emulated_push_data_type (EmulatedState *card,uint32_t type, +const uint8_t *data,uint32_t len) +{ +EmulEvent *event = (EmulEvent *)malloc (sizeof (EmulEvent) + len);
qemu_malloc()
hw/ccid-card-emulated.c line 367
Quoted 10 lines
hw/ccid-card-emulated.c new file +10
-0+static void pipe_read (void *opaque) +{ +EmulatedState *card = opaque; +EmulEvent *event, *next; +char dummy; +int len; + +do { + len =read (card->pipe[0 ], &dummy,sizeof (dummy)); + }while (len ==sizeof (dummy));
Shouldn't you check error codes here?
Jes
Re: [Qemu-devel] [PATCH 6/7] ccid: add docs
In reply to Alon LevyQuoted 8 lines · Alon Levy
Add documentation for the usb-ccid device and accompanying two card devices, ccid-card-emulated and ccid-card-passthru.
Signed-off-by Alon Levy <[email protected]>--- docs/ccid.txt | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 135 insertions(+), 0 deletions(-) create mode 100644 docs/ccid.txt
This one looks fine :)
Jes
Re: [Qemu-devel] [PATCH 7/7] ccid: configure: improve --enable-smartcard flags
In reply to Alon LevyQuoted 5 lines · Alon Levy
* add --enable-smartcard and --disable-smartcard flags
* let the nss check only disable building the ccid-card-emulated device
* report only if nss is found or not, not smartcard build inclusion
* don't link with NSS if --disable-smartcard-nss
The --disable-smartcard flag really should go with the initial smartcard changes to configure. That way it is possible to test that each individual patch doesn't break the build when smartcard support is enabled or disabled. If you add it at the end, you figuring out which patch is the problem is much harder.
Adding the smartcard-nss flag should go next to the patch that adds the code relying on the flag.
Cheers,
Jes
Re: [Qemu-devel] [PATCH 2/7] introduce libcacard/vscard_common.h
In reply to Alon LevyQuoted 9 lines · Alon Levy
Quoted 9 lines
[snip]
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
+/* VSCMsgInit Client <-> Host
+ * Client sends it on connection, with its own capabilities.
+ * Host replies with VSCMsgInit filling in its capabilities.
+ *
+ * It is not meant to be used for negotiation, i.e. sending more then
+ * once from any side, but could be used for that in the future.
+ * */
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Looks like some automatic mangling of comments - happens in more than one place.
What, the repeated "capabilities" word at the end of two consecutive lines? that's on purpose.
Nope the
*
*
*
* */
looks odd.
Jes
Re: [Qemu-devel] [PATCH 3/7] ccid: add passthru card device
In reply to Alon LevyQuoted 6 lines · Alon Levy
Quoted 5 lines · Jes Sorensen
Quoted 4 lines · Alon Levy
The passthru ccid card is a device sitting on the usb-ccid bus and using a chardevice to communicate with a remote device using the VSCard protocol defined in
libcacard/vscard_common.h
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Usage docs available in following patch in
docs/ccid.txtSigned-off-by Alon Levy <[email protected]>Except for the usual formatting bits, this one looks fine to me.
What exactly?
The comments formatting as I mentioned in previous patches.
Jes
Re: [Qemu-devel] [PATCH 2/7] introduce libcacard/vscard_common.h
In reply to Jes SorensenQuoted 12 lines · Jes Sorensen
Quoted 11 lines · Alon Levy
Quoted 11 lines
[snip]
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
+/* VSCMsgInit Client <-> Host
+ * Client sends it on connection, with its own capabilities.
+ * Host replies with VSCMsgInit filling in its capabilities.
+ *
+ * It is not meant to be used for negotiation, i.e. sending more then
+ * once from any side, but could be used for that in the future.
+ * */Looks like some automatic mangling of comments - happens in more than one place.
Quoted 9 lines
Quoted 2 lines
What, the repeated "capabilities" word at the end of two consecutive lines? that's on purpose.
Nope the
*
*
*
* */
oh, that I actually left on purpose (well, not really caring that much).
Quoted 3 lines
looks odd.
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
In reply to Jes SorensenOn Mon, Mar 14, 2011 at 04:20:22PM +0100, Jes Sorensen wrote:
ok, here is a note where I kinda ignored my own wishes but I want to be very clear on them:
libcacard should not be part of qemu.
it is here because I once thought it would speed things up.
So I'm not taking it out or anything - it's fine with me that it goes into qemu, just as long as it's understood that I'm now maintaining another copy of it for usage outside of qemu, in the spice client (or any other client for that matter - it will be the same when we do vnc support for this).
If I start using qemu-ism then I end up having something I have to change outside of qemu. Like using QemuMutex.
Another option is for me to check a define, i.e. QEMU_EMBEDDED.
I'm fine with that.
This goes for malloc too of course. And for assuming it never fails. Actually the later I could change the library to have it's internal malloc that does an assert (it's ok with the client to abort on oom).
rest of my comments are inline.
Quoted 125 lines · Alon Levy
libcacard/cac.c line 27
Quoted 10 lines
libcacard/cac.c new file +10
-0+/* private data for PKI applets */ +typedef struct CACPKIAppletDataStruct { +unsigned char *cert; +int cert_len; +unsigned char *cert_buffer; +int cert_buffer_len; +unsigned char *sign_buffer; +int sign_buffer_len; +VCardKey *key; +}CACPKIAppletData ;Grouping the ints together would allow for better struct padding.
Quoted 3 lines
+/*
+ * resest the inter call state between applet selects
+ */I presume it meant to say 'resets' ?
libcacard/event.c line 3
Quoted 9 lines
libcacard/event.c new file +3
-0@@ -0,0 +1,112 @@ +/* +* +*/ This comment is really spot on :)
libcacard/mutex.h
Quoted 5 lines
libcacard/mutex.h new file +0
-0UH OH!
Quoted 29 lines
libcacard/mutex.h new file +29
-0+/* +* This header file provides a way of mapping windows and linux thread calls +* to a set of macros. Ideally this would be shared by whatever subsystem we +* link with. +*/ + +#ifndef _H_MUTEX +#define _H_MUTEX +#ifdef _WIN32 +#include <windows.h> +typedef CRITICAL_SECTION mutex_t ; +#define MUTEX_INIT (mutex) InitializeCriticalSection(&mutex) +#define MUTEX_LOCK (mutex) EnterCriticalSection(&mutex) +#define MUTEX_UNLOCK (mutex) LeaveCriticalSection(&mutex) +typedef CONDITION_VARIABLE condition_t ; +#define CONDITION_INIT (cond) InitializeConditionVariable(&cond) +#define CONDITION_WAIT (cond, mutex) \ + SleepConditionVariableCS(&cond, &mutex, INFINTE) +#define CONDITION_NOTIFY (cond) WakeConditionVariable(&cond) +typedef uint32_t thread_t ; +typedef HANDLE thread_status_t ; +#define THREAD_CREATE (tid, func, arg) \ + CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, arg, 0, &tid) +#define THREAD_SUCCESS (status) ((status) != NULL) +#else +#include <pthread.h> +typedef pthread_mutex_t mutex_t ; +#define MUTEX_INIT (mutex) pthread_mutex_init(&mutex, NULL) +#define MUTEX_LOCK (mutex) pthread_mutex_lock(&mutex)libcacard/mutex.h line 57
Quoted 10 lines
libcacard/mutex.h new file +10
-0+#define MUTEX_UNLOCK (mutex) pthread_mutex_unlock(&mutex) +typedef pthread_cond_t condition_t ; +#define CONDITION_INIT (cond) pthread_cond_init(&cond, NULL) +#define CONDITION_WAIT (cond, mutex) pthread_cond_wait(&cond, &mutex) +#define CONDITION_NOTIFY (cond) pthread_cond_signal(&cond) +typedef pthread_t thread_t ; +typedef int thread_status_t ; +#define THREAD_CREATE (tid, func, arg) pthread_create(&tid, NULL, func, arg) +#define THREAD_SUCCESS (status) ((status) == 0) +#endif NACK! This is no good, the code needs to be fixed to use QemuMutex from
qemu-thread.hIn addition, a thing like a mutex feature should be in a separate patch,
not part of the code that uses it. However QEMU already has a mutex set
so this part needs to go.libcacard/mutex.h
Quoted 7 lines
libcacard/mutex.h new file +7
-0+static VCardAppletPrivate * +passthru_new_applet_private (VReader *reader) +{ +VCardAppletPrivate *applet_private =NULL ; +LONG rv; + + applet_private = (VCardAppletPrivate *)malloc (sizeof (VCardAppletPrivate));
qemu_malloc()libcacard/mutex.h
Quoted 3 lines
libcacard/mutex.h new file +3
-0+if (applet_private ==NULL ) { + goto fail; + }and it never fails.
libcacard/mutex.h
Quoted 3 lines
libcacard/mutex.h new file +3
-0+if (new_reader_list_len != reader_list_len) { +/* update the list */ + new_reader_list = (char *)malloc (new_reader_list_len);
qemu_malloc()again.Please grep through the full patch set and make sure you do not have any direct calls to
malloc()orfree().libcacard/mutex.h
Quoted 4 lines
libcacard/mutex.h new file +4
-0+/* try resetting the pcsc_lite subsystem */ +SCardReleaseContext (global_context); + global_context =0 ;/* should close it */ +printf ("***** SCard failure %x\n" , rv);
error_report()Quoted 5 lines
libcacard/vcard_emul_nss.c new file +0
-0[snip]
Quoted 7 lines
libcacard/vcard_emul_nss.c line 45
Quoted 5 lines
libcacard/vcard_emul_nss.c new file +5
-0+struct VReaderEmulStruct { +PK11SlotInfo *slot; +VCardEmulType default_type; +char *type_params; +PRBool present;What is PRBool and where does it come from?
NSS I assume.
Quoted 18 lines
Quoted 5 lines
libcacard/vcard_emul_nss.c new file +4
-0+void +vcard_emul_reset (VCard *card,VCardPower power) +{ +PK11SlotInfo *slot;libcacard/vcard_emul_nss.c line 314
Quoted 10 lines
libcacard/vcard_emul_nss.c new file +10
-0+ +if (!nss_emul_init) { +return ; + } + +/* if we reset the card (either power on or power off), we loose our login +* state */ +/* TODO: we may also need to send insertion/removal events? */ + slot =vcard_emul_card_get_slot (card); + (void )PK11_Logout (slot);We don't (void) cast calls like that in QEMU.
will fix.
Quoted 13 lines
Quoted 13 lines
libcacard/vcard_emul_nss.c new file +13
-0+/* +* NSS needs the app to supply a password prompt. In our case the only time +* the password is supplied is as part of the Login APDU. The actual password +* is passed in the pw_arg in that case. In all other cases pw_arg should be +* NULL. +*/ +static char * +vcard_emul_get_password (PK11SlotInfo *slot,PRBool retries,void *pw_arg) +{ +/* if it didn't work the first time, don't keep trying */ +if (retries) { +return NULL ; + }
Quoted 9 lines
libcacard/vcard_emul_nss.c line 675
Quoted 6 lines
libcacard/vcard_emul_nss.c new file +6
-0+/* we are looking up a password when we don't have one in hand */ +if (pw_arg ==NULL ) { +return NULL ; + } +/* TODO: we really should verify that were are using the right slot */ +return PORT_Strdup (pw_arg);
PORT_Strdup???
no idea, will check.
Quoted 5 lines
Quoted 5 lines
libcacard/vcard_emul_nss.c new file +5
-0+static const char * +find_token (const char *str,char token,char token_end) +{ +/* just do the blind simple thing */ +for (; *str; str++) {
Quoted 9 lines
libcacard/vcard_emul_nss.c line 983
Quoted 6 lines
libcacard/vcard_emul_nss.c new file +6
-0+if ((*str == token) || (*str == token_end)) { +break ; + } + } +return str; +}What is wrong with strpbrk(3) ?
probably nothing.
Quoted 10 lines
libcacard/vcard_emul_nss.c line 991
Quoted 7 lines
libcacard/vcard_emul_nss.c new file +7
-0+static const char * +strip (const char *str) +{ +for (; *str; str++) { +if ((*str !=' ' ) && (*str !='\n' ) && + (*str !='\t' ) && (*str !='\r' )) { +break ;!isspace() ?
will man.
Quoted 6 lines
Quoted 6 lines
libcacard/vcard_emul_nss.c new file +6
-0+static const char * +find_blank (const char *str) +{ +for (; *str; str++) { +if ((*str ==' ' ) || (*str =='\n' ) || + (*str =='\t' ) || (*str =='\r' )) {
Quoted 9 lines
libcacard/vcard_emul_nss.c line 983
Quoted 6 lines
libcacard/vcard_emul_nss.c new file +6
-0+ +break ; + } + } +return str; +}strpbrk(3) ?
ok.
Quoted 17 lines
Quoted 17 lines
libcacard/vcardt.h new file +11
-0@@ -0,0 +1,66 @@ +/* +* +*/ +#ifndef VCARDT_H +#define VCARDT_H 1 + +/* +* these should come from some common spice header file +*/ +#include <assert.h> +#ifndef ASSERT
Quoted 9 lines
libcacard/vcardt.h line 17
Quoted 6 lines
libcacard/vcardt.h new file +6
-0+#define ASSERT assert +#endif +#ifndef MIN +#define MIN (x, y) ((x) > (y) ? (y) : (x)) +#define MAX (x, y) ((x) > (y) ? (x) : (y)) +#endif QEMU uses
assert(), notASSERT(). Please fix.
will.
Quoted 12 lines
libcacard/vreader.c line 3
Quoted 9 lines
libcacard/vreader.c new file +3
-0@@ -0,0 +1,526 @@ +/* +* emulate the reader +*/ What is the license of this file?
will fix.
Quoted 31 lines
Quoted 6 lines
libcacard/vreader.c new file +6
-0+#include "vcard.h" +#include "vcard_emul.h" +#include "card_7816.h" +#include "vreader.h" +#include "vevent.h" +libcacard/vreader.c line 19
Quoted 10 lines
libcacard/vreader.c new file +10
-0+/* +* System includes +*/ +#include <stdlib.h> +#include <string.h> + +/* +* spice includes +*/ +#include "mutex.h" No no no
libcacard/vreadert.h line 3
Quoted 9 lines
libcacard/vreadert.h new file +3
-0@@ -0,0 +1,23 @@ +/* +* +*/ Spot on!
Quoted 3 lines
General comment, this patch is *way* too big. It really should be a series of patches adding features one after another. The testing for cards ought to be separate for example.
will split.
Quoted 3 lines
Jes
Re: [Qemu-devel] [PATCH 5/7] ccid: add ccid-card-emulated device
In reply to Jes SorensenQuoted 52 lines · Jes Sorensen
Quoted 25 lines · Alon Levy
hw/ccid-card-emulated.c new file +19
-0@@ -0,0 +1,599 @@ +/* +* CCID Card Device. Emulated card. +* +* Copyright (c) 2011 Red Hat. +* Written by Alon Levy. +* +* This code is licenced under the GNU LGPL, version 2 or later. +*/ + +/* +* It can be used to provide access to the local hardware in a non exclusive +* way, or it can use certificates. It requires the usb-ccid bus. +* +* Usage 1: standard, mirror hardware reader+card: +* qemu .. -usb -device usb-ccid -device ccid-card-emulated +* +* Usage 2: use certificates, no hardware required +* one time: create the certificates: +* for i in 1 2 3; do hw/ccid-card-emulated.c line 29
Quoted 10 lines
hw/ccid-card-emulated.c new file +10
-0+* certutil -d /etc/pki/nssdb -x -t "CT,CT,CT" -S -s "CN=user$i" -n user$i +* done +* qemu .. -usb -device usb-ccid \ +* -device ccid-card-emulated,cert1=user1,cert2=user2,cert3=user3 +* +* If you use a non default db for the certificates you can specify it using +* the db parameter. +*/ + +#include <pthread.h>
qemu-thread.hhw/ccid-card-emulated.c line 78
Quoted 10 lines
hw/ccid-card-emulated.c new file +10
-0+static const char *emul_event_to_string (uint32_t emul_event) +{ +switch (emul_event) { +case EMUL_READER_INSERT :return "EMUL_READER_INSERT" ; +case EMUL_READER_REMOVE :return "EMUL_READER_REMOVE" ; +case EMUL_CARD_INSERT :return "EMUL_CARD_INSERT" ; +case EMUL_CARD_REMOVE :return "EMUL_CARD_REMOVE" ; +case EMUL_GUEST_APDU :return "EMUL_GUEST_APDU" ; +case EMUL_RESPONSE_APDU :return "EMUL_RESPONSE_APDU" ; +case EMUL_ERROR :return "EMUL_ERROR" ;YUCK!
Quoted 1 line
No multi statements on a single line!
passes check-patch :)
will fix.
Quoted 19 lines
Quoted 19 lines
hw/ccid-card-emulated.c new file +18
-0+#define MAX_ATR_SIZE 40 +struct EmulatedState { +CCIDCardState base; +uint8_t debug; +char *backend_str; +uint32_t backend; +char *cert1; +char *cert2; +char *cert3; +char *db; +uint8_t atr[MAX_ATR_SIZE ]; +uint8_t atr_length; + QSIMPLEQ_HEAD(event_list, EmulEvent) event_list; +pthread_mutex_t event_list_mutex; +VReader *reader; + QSIMPLEQ_HEAD(guest_apdu_list, EmulEvent) guest_apdu_list; +pthread_mutex_t vreader_mutex;/* and guest_apdu_list mutex */ +pthread_mutex_t handle_apdu_mutex;
Quoted 8 lines
hw/ccid-card-emulated.c line 126
Quoted 6 lines
hw/ccid-card-emulated.c new file +6
-0+pthread_cond_t handle_apdu_cond; +int pipe[2 ]; +int quit_apdu_thread; +pthread_mutex_t apdu_thread_quit_mutex; +pthread_cond_t apdu_thread_quit_cond; +};Bad struct packing and wrong thread types.
Will fix.
Aside: Why do we care about packing something that has a single instance per device? isn't logical readable order more important in this case?
Quoted 6 lines
hw/ccid-card-emulated.c line 166
Quoted 4 lines
hw/ccid-card-emulated.c new file +3
-0+static void emulated_push_type (EmulatedState *card,uint32_t type) +{ +EmulEvent *event = (EmulEvent *)malloc (sizeof (EmulEvent));
qemu_malloc()
will fix. (and the rest)
Quoted 25 lines
hw/ccid-card-emulated.c line 175
Quoted 9 lines
hw/ccid-card-emulated.c new file +8
-0+assert (event); + event->p.gen.type = type; +emulated_push_event (card, event); +} + +static void emulated_push_error (EmulatedState *card,uint64_t code) +{ +EmulEvent *event = (EmulEvent *)malloc (sizeof (EmulEvent));
qemu_malloc()hw/ccid-card-emulated.c line 186
Quoted 10 lines
hw/ccid-card-emulated.c new file +10
-0+assert (event); + event->p.error.type =EMUL_ERROR ; + event->p.error.code = code; +emulated_push_event (card, event); +} + +static void emulated_push_data_type (EmulatedState *card,uint32_t type, +const uint8_t *data,uint32_t len) +{ +EmulEvent *event = (EmulEvent *)malloc (sizeof (EmulEvent) + len);
qemu_malloc()
Quoted 12 lines
hw/ccid-card-emulated.c line 367
Quoted 10 lines
hw/ccid-card-emulated.c new file +10
-0+static void pipe_read (void *opaque) +{ +EmulatedState *card = opaque; +EmulEvent *event, *next; +char dummy; +int len; + +do { + len =read (card->pipe[0 ], &dummy,sizeof (dummy)); + }while (len ==sizeof (dummy));Shouldn't you check error codes here?
I should.
Quoted 2 lines
Jes
Re: [Qemu-devel] [PATCH 5/7] ccid: add ccid-card-emulated device
Quoted 27 lines · Alon Levy
Quoted 18 lines · Jes Sorensen
Quoted 18 lines
hw/ccid-card-emulated.c new file +18
-0+#define MAX_ATR_SIZE 40 +struct EmulatedState { +CCIDCardState base; +uint8_t debug; +char *backend_str; +uint32_t backend; +char *cert1; +char *cert2; +char *cert3; +char *db; +uint8_t atr[MAX_ATR_SIZE ]; +uint8_t atr_length; + QSIMPLEQ_HEAD(event_list, EmulEvent) event_list; +pthread_mutex_t event_list_mutex; +VReader *reader; + QSIMPLEQ_HEAD(guest_apdu_list, EmulEvent) guest_apdu_list; +pthread_mutex_t vreader_mutex;/* and guest_apdu_list mutex */ +pthread_mutex_t handle_apdu_mutex;Quoted 8 lines
hw/ccid-card-emulated.c line 126
Quoted 6 lines
hw/ccid-card-emulated.c new file +6
-0+pthread_cond_t handle_apdu_cond; +int pipe[2 ]; +int quit_apdu_thread; +pthread_mutex_t apdu_thread_quit_mutex; +pthread_cond_t apdu_thread_quit_cond; +};Bad struct packing and wrong thread types.
Quoted 4 lines
Will fix.
Aside: Why do we care about packing something that has a single instance per device? isn't logical readable order more important in this case?
We don't care too much - use your own judgement for what makes sense in this case. I am used to spotting those so I mention them, but I didn't actually check how often the struct was used.
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
In reply to Alon LevyQuoted 7 lines · Alon Levy
On Mon, Mar 14, 2011 at 04:20:22PM +0100, Jes Sorensen wrote:
ok, here is a note where I kinda ignored my own wishes but I want to be very clear on them:
libcacard should not be part of qemu.
it is here because I once thought it would speed things up.
Quoted 5 lines
So I'm not taking it out or anything - it's fine with me that it goes into qemu, just as long as it's understood that I'm now maintaining another copy of it for usage outside of qemu, in the spice client (or any other client for that matter - it will be the same when we do vnc support for this).
Hi Alon,
This bit is somewhat problematic. If QEMU is maintaining a copy of libcacard, then that has to comply with the QEMU way of doing things. QEMU cannot rely on various portions in the tree behaving in different ways. Otherwise it really should be an external library requirement pulled in by the build.
I am not sure what is the best way, if it stays in QEMU people will eventually start making modifications to it, without looking at the other copy that is being maintained.
Alternatively the external apps that build against it should be taught to link with the QEMU version.
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
Quoted 22 lines · Jes Sorensen
Quoted 7 lines · Alon Levy
On Mon, Mar 14, 2011 at 04:20:22PM +0100, Jes Sorensen wrote:
ok, here is a note where I kinda ignored my own wishes but I want to be very clear on them:
libcacard should not be part of qemu.
it is here because I once thought it would speed things up.Quoted 5 lines
So I'm not taking it out or anything - it's fine with me that it goes into qemu, just as long as it's understood that I'm now maintaining another copy of it for usage outside of qemu, in the spice client (or any other client for that matter - it will be the same when we do vnc support for this).
Hi Alon,
This bit is somewhat problematic. If QEMU is maintaining a copy of libcacard, then that has to comply with the QEMU way of doing things. QEMU cannot rely on various portions in the tree behaving in different ways. Otherwise it really should be an external library requirement pulled in by the build.
Quoted 4 lines
I am not sure what is the best way, if it stays in QEMU people will eventually start making modifications to it, without looking at the other copy that is being maintained.
Yeah, I've already decided (actually minutes after sending this email) to go the route of keeping a copy of qemu-thread* in the external library, since the api is basically just a mirror of pthreads it was nothing but a few renames.
Quoted 3 lines
Alternatively the external apps that build against it should be taught to link with the QEMU version.
That would require me to teach qemu's configure to build libcacard, possibly only libcacard (even though qemu doesn't need a lot of packages by itself, I still wouldn't want apt-get install spice-client to drag in qemu-kvm).
Quoted 2 lines
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
Quoted 8 lines · Alon Levy
Quoted 3 lines · Jes Sorensen
Alternatively the external apps that build against it should be taught to link with the QEMU version.
That would require me to teach qemu's configure to build libcacard, possibly only libcacard (even though qemu doesn't need a lot of packages by itself, I still wouldn't want apt-get install spice-client to drag in qemu-kvm).
Hi Alon,
I am a little confused as to what the library really does. Is it a library to manage iso7816 cards, or is it an emulation library? If it is hw emulation the library really should be part of qemu.git, but there is nothing that prevents us to expanding the qemu Makefile to build the library and then have a separate RPM called qemu-libs or something that can be installed without the main qemu RPM being installed.
Can you elaborate a bit on how spice uses libcacard? I can understand it relying on a library to access/manage smartcards, but the emulation bit puzzles me?
If libcacard does both card management and emulation, my next question is whether it wouldn't make more sense to split the two into two separate packages?
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
In reply to Jes SorensenQuoted 21 lines · Jes Sorensen
Quoted 7 lines · Alon Levy
On Mon, Mar 14, 2011 at 04:20:22PM +0100, Jes Sorensen wrote:
ok, here is a note where I kinda ignored my own wishes but I want to be very clear on them:
libcacard should not be part of qemu.
it is here because I once thought it would speed things up.Quoted 5 lines
So I'm not taking it out or anything - it's fine with me that it goes into qemu, just as long as it's understood that I'm now maintaining another copy of it for usage outside of qemu, in the spice client (or any other client for that matter - it will be the same when we do vnc support for this).
Hi Alon,
This bit is somewhat problematic. If QEMU is maintaining a copy of libcacard, then that has to comply with the QEMU way of doing things. QEMU cannot rely on various portions in the tree behaving in different ways. Otherwise it really should be an external library requirement pulled in by the build.
Quoted 3 lines
I am not sure what is the best way, if it stays in QEMU people will eventually start making modifications to it, without looking at the other copy that is being maintained.
Two copies is not really practical. QEMU should be the place that owns it and things should be consuming a .so from QEMU.
Regards,
Anthony Liguori
Quoted 6 lines
Alternatively the external apps that build against it should be taught to link with the QEMU version.
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
In reply to Alon LevyQuoted 7 lines · Alon Levy
Quoted 4 lines
Alternatively the external apps that build against it should be taught to link with the QEMU version.
That would require me to teach qemu's configure to build libcacard, possibly only libcacard (even though qemu doesn't need a lot of packages by itself, I still wouldn't want apt-get install spice-client to drag in qemu-kvm).
Any reasonable packaging system can generate multiple binary packages from a source source package without the binary packages being implicit dependent on each other.
Regards,
Anthony Liguori
Quoted 2 lines
Quoted 2 lines
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
In reply to Jes SorensenQuoted 6 lines · Jes Sorensen
Quoted 5 lines · Alon Levy
Quoted 3 lines · Jes Sorensen
Alternatively the external apps that build against it should be taught to link with the QEMU version.
Quoted 8 lines
Quoted 3 lines
That would require me to teach qemu's configure to build libcacard, possibly only libcacard (even though qemu doesn't need a lot of packages by itself, I still wouldn't want apt-get install spice-client to drag in qemu-kvm).
Hi Alon,
I am a little confused as to what the library really does. Is it a library to manage iso7816 cards, or is it an emulation library? If it is
emulation library.
Quoted 4 lines
hw emulation the library really should be part of qemu.git, but there is nothing that prevents us to expanding the qemu
Makefileto build the library and then have a separate RPM called qemu-libs or something that can be installed without the main qemu RPM being installed.
Yes, that's what I was thinking about. Of course we can do it downstream (in fedora/rhel), but I'd rather have an upstream make target / configure option == solution..
Quoted 5 lines
Can you elaborate a bit on how spice uses libcacard? I can understand it relying on a library to access/manage smartcards, but the emulation bit puzzles me?
If no emulation was required in the middle we would have just done usb forwarding. The fact is we need the client and the guest to access the card at the same time, potentially the client and a few guests. Because there is no locking in the smartcard protocol, no idea of multiple outstanding requests, this requires giving each guest it's own card state, that is emulating a card.
libcacard emulates a CAC, that is a Common Access Card. So the second option.
The reader emulation is naturally part of the pc emulation, so qemu is the right place.
There are two locations to do the card emulation, currently both are implemented:
* in the pc emulator: ccid-card-emualted. This links with the libcacard files (well, the way we do linking it links with all the world, but it uses that code, those symbols).
* in the client: that's what spice uses. in the vm side we have ccid-card-passthru, over the wire we get the APDU's (application protocol data unit for the 7186 standard, which the CAC standard uses), and the card emulation itself is done in the client, via linking with libcacard (the standalone one).
Obviously it would have been simpler if we decided from the start to do what anthony wanted, that is to emulate in the host/pc. But we/I didn't, it seemed easier to emulate in the client, and also I thought more performant. The performance part really depends on which latency is more important, and no benchmarks have been done.
So right now contents wise (I mean, what's in this patchset) I think we are over the question of which devices will be accepted in qemu, we are just down to the question of what color the code should be, and I'll be sending v21 once I fix the review concerns.
Quoted 7 lines
If libcacard does both card management and emulation, my next question is whether it wouldn't make more sense to split the two into two separate packages?
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
In reply to Anthony LiguoriQuoted 5 lines · Anthony Liguori
Quoted 4 lines · Alon Levy
Quoted 4 lines
Alternatively the external apps that build against it should be taught to link with the QEMU version.
Quoted 7 lines
Quoted 3 lines
That would require me to teach qemu's configure to build libcacard, possibly only libcacard (even though qemu doesn't need a lot of packages by itself, I still wouldn't want apt-get install spice-client to drag in qemu-kvm).
Any reasonable packaging system can generate multiple binary packages from a source source package without the binary packages being implicit dependent on each other.
of course, I was just saying it would be easier if qemu's configure system allowed this.
Quoted 9 lines
Regards,
Anthony Liguori
Quoted 2 lines
Quoted 2 lines
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
In reply to Anthony LiguoriQuoted 22 lines · Anthony Liguori
Quoted 21 lines · Jes Sorensen
Quoted 7 lines · Alon Levy
On Mon, Mar 14, 2011 at 04:20:22PM +0100, Jes Sorensen wrote:
ok, here is a note where I kinda ignored my own wishes but I want to be very clear on them:
libcacard should not be part of qemu.
it is here because I once thought it would speed things up.Quoted 5 lines
So I'm not taking it out or anything - it's fine with me that it goes into qemu, just as long as it's understood that I'm now maintaining another copy of it for usage outside of qemu, in the spice client (or any other client for that matter - it will be the same when we do vnc support for this).
Hi Alon,
This bit is somewhat problematic. If QEMU is maintaining a copy of libcacard, then that has to comply with the QEMU way of doing things. QEMU cannot rely on various portions in the tree behaving in different ways. Otherwise it really should be an external library requirement pulled in by the build.
Quoted 7 lines
Quoted 3 lines
I am not sure what is the best way, if it stays in QEMU people will eventually start making modifications to it, without looking at the other copy that is being maintained.
Two copies is not really practical. QEMU should be the place that owns it and things should be consuming a .so from QEMU.
My bad - I thought you didn't want this. I can do a patch to make qemu build an .so file if configure gets a "--libs", how does that sound? right now that would build just libcacard, I guess libqmp too later? or perhaps have a separate Makefile (Makefile.libs)? Have you given this any thought?
Quoted 12 lines
Regards,
Anthony Liguori
Quoted 6 lines
Alternatively the external apps that build against it should be taught to link with the QEMU version.
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
Quoted 4 lines · Alon Levy
Quoted 4 lines
Quoted 4 lines
I am not sure what is the best way, if it stays in QEMU people will
Quoted 2 lines
Quoted 2 lines
eventually start making modifications to it, without looking at the other copy that is being maintained.
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Two copies is not really practical. QEMU should be the place that owns it and things should be consuming a .so from QEMU.
My bad - I thought you didn't want this. I can do a patch to make qemu build an .so file if configure gets a "--libs", how does that sound? right now that would build just libcacard, I guess libqmp too later? or perhaps have a separate
Makefile(Makefile.libs)? Have you given this any thought?
I think the libs should be built by default as part of the build process and get installed as part of the regular install. Ie. it becomes part of the QEMU build process, so it requires a QEMU build to build the support libraries, but they can be packages into separate RPMs/debs by the distro people.
I don't think we want a --libs option that turns the build process into only producing the libs.
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
In reply to Alon LevyQuoted 23 lines · Alon Levy
Quoted 22 lines · Anthony Liguori
Quoted 21 lines · Jes Sorensen
Quoted 7 lines · Alon Levy
On Mon, Mar 14, 2011 at 04:20:22PM +0100, Jes Sorensen wrote:
ok, here is a note where I kinda ignored my own wishes but I want to be very clear on them:
libcacard should not be part of qemu.
it is here because I once thought it would speed things up.Quoted 5 lines
So I'm not taking it out or anything - it's fine with me that it goes into qemu, just as long as it's understood that I'm now maintaining another copy of it for usage outside of qemu, in the spice client (or any other client for that matter - it will be the same when we do vnc support for this).
Hi Alon,
This bit is somewhat problematic. If QEMU is maintaining a copy of libcacard, then that has to comply with the QEMU way of doing things. QEMU cannot rely on various portions in the tree behaving in different ways. Otherwise it really should be an external library requirement pulled in by the build.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
I am not sure what is the best way, if it stays in QEMU people will eventually start making modifications to it, without looking at the other copy that is being maintained.
Two copies is not really practical. QEMU should be the place that owns it and things should be consuming a .so from QEMU.
My bad - I thought you didn't want this. I can do a patch to make qemu build an .so file if configure gets a "--libs", how does that sound?
Yeah, we probably should target being able to do something like:
configure --target-list="" --disable-tools --enable-libs
And it just build libs.
Quoted 3 lines
right now that would build just libcacard, I guess libqmp too later? or perhaps have a separate
Makefile(Makefile.libs)? Have you given this any thought?
I was half considering seeing if I could get away with using libtool ;-)
We can do something pretty simple for now and only build libs when GCC is available and we're on Linux.
Regards,
Anthony Liguori
Quoted 11 lines
Quoted 11 lines
Regards,
Anthony Liguori
Quoted 6 lines
Alternatively the external apps that build against it should be taught to link with the QEMU version.
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
In reply to Jes SorensenQuoted 12 lines · Jes Sorensen
Quoted 11 lines · Alon Levy
Quoted 6 lines
Quoted 6 lines
I am not sure what is the best way, if it stays in QEMU people will
Quoted 2 lines
Quoted 2 lines
eventually start making modifications to it, without looking at the other copy that is being maintained.
Two copies is not really practical. QEMU should be the place that owns it and things should be consuming a .so from QEMU.
My bad - I thought you didn't want this. I can do a patch to make qemu build an .so file if configure gets a "--libs", how does that sound? right now that would build just libcacard, I guess libqmp too later? or perhaps have a separate
Makefile(Makefile.libs)? Have you given this any thought?
Quoted 8 lines
I think the libs should be built by default as part of the build process and get installed as part of the regular install. Ie. it becomes part of the QEMU build process, so it requires a QEMU build to build the support libraries, but they can be packages into separate RPMs/debs by the distro people.
I don't think we want a --libs option that turns the build process into only producing the libs.
No, but you should be able to use options to disable everything but the libs. Likewise, you should be able to disable the libs.
This is necessary for distro packaging.
Regards,
Anthony Liguori
Quoted 3 lines
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
Quoted 8 lines · Anthony Liguori
Quoted 2 lines · Jes Sorensen
I don't think we want a --libs option that turns the build process into only producing the libs.
No, but you should be able to use options to disable everything but the libs. Likewise, you should be able to disable the libs.
This is necessary for distro packaging.
I don't think it is necessary for distro packaging. Most distros will build multiple binary packages from one source package, and it is up to the user to decide which ones to install.
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
Quoted 5 lines · Jes Sorensen
Quoted 4 lines · Anthony Liguori
Quoted 2 lines · Jes Sorensen
I don't think we want a --libs option that turns the build process into only producing the libs.
Quoted 9 lines
Quoted 4 lines
No, but you should be able to use options to disable everything but the libs. Likewise, you should be able to disable the libs.
This is necessary for distro packaging.
I don't think it is necessary for distro packaging. Most distros will build multiple binary packages from one source package, and it is up to the user to decide which ones to install.
if someone has the prerequisites for building libcacard, but not for building qemu, they should be able to build just libcacard. You can achieve this by having flags specifically, or by just behaving like we behave for features in general, that is not building qemu if it can't be. I think the first option is cleaner because it also allows requesting just libcacard (even if you do have the prerequisites for building qemu).
Quoted 3 lines
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
In reply to Alon LevyQuoted 10 lines · Alon Levy
Quoted 9 lines · Anthony Liguori
Quoted 7 lines · Alon Levy
Quoted 4 lines
Alternatively the external apps that build against it should be taught to link with the QEMU version.
That would require me to teach qemu's configure to build libcacard, possibly only libcacard (even though qemu doesn't need a lot of packages by itself, I still wouldn't want apt-get install spice-client to drag in qemu-kvm).
Quoted 6 lines
Quoted 3 lines
Any reasonable packaging system can generate multiple binary packages from a source source package without the binary packages being implicit dependent on each other.
of course, I was just saying it would be easier if qemu's configure system allowed this.
It's really not far off, most of it is in the Makefiles. You really just need to have a libcacard flag that is enabled per default (and is required if one asks for --enable-smartcard), but can be switched off, as well as a flag to not build the main bits.
Patches are welcome :)
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
In reply to Alon LevyQuoted 11 lines · Alon Levy
Quoted 3 lines · Jes Sorensen
I don't think it is necessary for distro packaging. Most distros will build multiple binary packages from one source package, and it is up to the user to decide which ones to install.
if someone has the prerequisites for building libcacard, but not for building qemu, they should be able to build just libcacard. You can achieve this by having flags specifically, or by just behaving like we behave for features in general, that is not building qemu if it can't be. I think the first option is cleaner because it also allows requesting just libcacard (even if you do have the prerequisites for building qemu).
I don't think it is such a big issue, in most cases people will pull in their distro libraries with yum or apt before they start building their user app. It is standard distro packaging practice to generate multiple packages from one source package.
On the other hand I don't think it would be a problem if you submitted patches to the QEMU configure/Makefile setup that allowed for building just the libraries and disabling the QEMU binaries. I don't see distros using this, but end developers might.
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
In reply to Jes SorensenQuoted 18 lines · Jes Sorensen
Quoted 10 lines · Alon Levy
Quoted 9 lines · Anthony Liguori
Quoted 7 lines · Alon Levy
Quoted 4 lines
Alternatively the external apps that build against it should be taught to link with the QEMU version.
That would require me to teach qemu's configure to build libcacard, possibly only libcacard (even though qemu doesn't need a lot of packages by itself, I still wouldn't want apt-get install spice-client to drag in qemu-kvm).
Quoted 6 lines
Quoted 3 lines
Any reasonable packaging system can generate multiple binary packages from a source source package without the binary packages being implicit dependent on each other.
of course, I was just saying it would be easier if qemu's configure system allowed this.
Quoted 7 lines
It's really not far off, most of it is in the Makefiles. You really just need to have a libcacard flag that is enabled per default (and is required if one asks for --enable-smartcard), but can be switched off, as well as a flag to not build the main bits.
Patches are welcome :)
Yes, I plan to make some. But I think it's better I don't involve it in this series? it's already overdue by a year or so. I'll make send it as a separate patch.
Quoted 2 lines
Cheers,
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
Quoted 10 lines · Alon Levy
Quoted 6 lines · Jes Sorensen
It's really not far off, most of it is in the Makefiles. You really just need to have a libcacard flag that is enabled per default (and is required if one asks for --enable-smartcard), but can be switched off, as well as a flag to not build the main bits.
Patches are welcome :)
Yes, I plan to make some. But I think it's better I don't involve it in this series? it's already overdue by a year or so. I'll make send it as a separate patch.
Yes, I would recommend you do them as a separate patch set. It should be pretty easy to get in.
It would be nice if the switches allows you to enable/disable individual applications such as qemu-img/qemu-nbd/qemu etc.
Cheers,
Jes
Re: [Qemu-devel] [PATCH 1/7] usb-ccid: add CCID bus
In reply to Jes SorensenQuoted 106 lines · Jes Sorensen
configure trace_backend="nop" · line 177
Quoted 9 lines · Alon Levy
configure +1
-0@@ -174,6 +174,7 @@ trace_backend="nop" trace_file="trace" spice="" rbd="" +smartcard="yes"IMHO smartcard support shouldn't be enabled per default. The userbase is limited.
Quoted 15 lines
hw/ccid.h new file +9
-0@@ -0,0 +1,54 @@ +/* +* CCID Passthru Card Device emulation +* +* Copyright (c) 2011 Red Hat. +* Written by Alon Levy. +* +* This code is licenced under the GNU LGPL, version 2 or later. +*/ +[snip]
hw/ccid.h line 27
Quoted 4 lines
hw/ccid.h new file +4
-0+ +/* callbacks to be used by the CCID device (hw/usb-ccid.c) to call +* into the smartcard device (hw/ccid-card-*.c) +*/ This is inconsistent with the comment above. Normally multi-line comments in QEMU are like this:
/*
* foo
* bar
*/hw/ccid.h line 40
Quoted 13 lines
hw/ccid.h new file +13
-0+struct CCIDCardInfo { +DeviceInfo qdev; +void (*print)(Monitor *mon,CCIDCardState *card,int indent); +const uint8_t *(*get_atr)(CCIDCardState *card,uint32_t *len); +void (*apdu_from_guest)(CCIDCardState *card, +const uint8_t *apdu, +uint32_t len); +int (*exitfn)(CCIDCardState *card); +int (*initfn)(CCIDCardState *card); +}; + +/* API for smartcard calling the CCID device (used by hw/ccid-card-*.c) +*/ again here
hw/ccid.h line 50
Quoted 10 lines
hw/ccid.h new file +10
-0+void ccid_card_send_apdu_to_guest (CCIDCardState *card, +uint8_t *apdu, +uint32_t len); +void ccid_card_card_removed (CCIDCardState *card); +void ccid_card_card_inserted (CCIDCardState *card); +void ccid_card_card_error (CCIDCardState *card,uint64_t error); +void ccid_card_qdev_register (CCIDCardInfo *card); + +/* support guest visible insertion/removal of ccid devices based on actual +* devices connected/removed. Called by card implementation (passthru, local) */ and here
hw/usb-ccid.c line 63
Quoted 11 lines
hw/usb-ccid.c new file +6
-0+#define CCID_DEV_NAME "usb-ccid" + +/* The two options for variable sized buffers: +* make them constant size, for large enough constant, +* or handle the migration complexity - VMState doesn't handle this case. +* sizes are expected never to be exceeded, unless guest misbehaves. */ here again
[snip]
hw/usb-ccid.c line 91
Quoted 6 lines
hw/usb-ccid.c new file +6
-0+/* Using Gemplus Vendor and Product id +Effect on various drivers: +* usbccid.sys (winxp, others untested) is a class driver so it doesn't care. +* linux has a number of class drivers, but openct filters based on +vendor/product (/etc/openct.conf under fedora), hence Gemplus. +*/ Something went totally boink with the comments there!
Quoted 10 lines
hw/usb-ccid.c new file +10
-0+/* 6.2.6 RDR_to_PC_SlotStatus definitions */ +enum { +CLOCK_STATUS_RUNNING =0 , +/* 0 - Clock Running, 1 - Clock stopped in State L, 2 - H, +3 - unkonwn state. rest are RFU +*/ +}; + +typedef struct __attribute__ ((__packed__)) CCID_Header { + uint8_t bMessageType;
Quoted 9 lines
hw/usb-ccid.c line 179
Quoted 4 lines
hw/usb-ccid.c new file +4
-0+uint32_t dwLength; +uint8_t bSlot; +uint8_t bSeq; +} CCID_Header;Is this header decided upon by the CCID spec or the code? It seems suboptimal to have a uint8 in front of a uint32 like that. Inefficient structure alignment :(
In the spec.
Quoted 8 lines
Quoted 8 lines
hw/usb-ccid.c new file +8
-0+ +/* 6.1.4 PC_to_RDR_XfrBlock */ +typedef struct __attribute__ ((__packed__ ))CCID_XferBlock { +CCID_Header hdr; +uint8_t bBWI;/* Block Waiting Timeout */ +uint16_t wLevelParameter;/* XXX currently unused */ +uint8_t abData[0 ]; +}CCID_XferBlock ;
Quoted 9 lines
hw/usb-ccid.c line 216
Quoted 6 lines
hw/usb-ccid.c new file +6
-0+ +typedef struct __attribute__ ((__packed__ ))CCID_IccPowerOn { +CCID_Header hdr; +uint8_t bPowerSelect; +uint16_t abRFU; +}CCID_IccPowerOn ;Same problem with the above two structs....
spec spec.
Quoted 5 lines
Quoted 5 lines
hw/usb-ccid.c new file +5
-0+typedef struct __attribute__ ((__packed__ ))CCID_IccPowerOff { +CCID_Header hdr; +uint16_t abRFU; +}CCID_IccPowerOff ; +
Quoted 9 lines
hw/usb-ccid.c line 228
Quoted 6 lines
hw/usb-ccid.c new file +6
-0+typedef struct __attribute__ ((__packed__ ))CCID_SetParameters { +CCID_Header hdr; +uint8_t bProtocolNum; +uint16_t abRFU; +uint8_t abProtocolDataStructure[0 ]; +}CCID_SetParameters ;and again.
guess ;)
Quoted 29 lines
Quoted 29 lines
hw/usb-ccid.c new file +29
-0+/** +* powered - defaults to true, changed by PowerOn/PowerOff messages +*/ +struct USBCCIDState { +USBDevice dev; +CCIDBus *bus; +CCIDCardState *card; +CCIDCardInfo *cardinfo;/* caching the info pointer */ +uint8_t debug; +BulkIn bulk_in_pending[BULK_IN_PENDING_NUM ];/* circular */ +uint32_t bulk_in_pending_start; +uint32_t bulk_in_pending_end;/* first free */ +uint32_t bulk_in_pending_num; +BulkIn *current_bulk_in; +uint8_t bulk_out_data[BULK_OUT_DATA_SIZE ]; +uint32_t bulk_out_pos; +uint8_t bmSlotICCState; +uint8_t powered; +uint8_t notify_slot_change; +uint64_t last_answer_error; +Answer pending_answers[PENDING_ANSWERS_NUM ]; +uint32_t pending_answers_start; +uint32_t pending_answers_end; +uint32_t pending_answers_num; +uint8_t bError; +uint8_t bmCommandStatus; +uint8_t bProtocolNum; +uint8_t abProtocolDataStructure[MAX_PROTOCOL_SIZE ]; +uint32_t ulProtocolDataStructureSize;
Quoted 9 lines
hw/usb-ccid.c line 291
Quoted 5 lines
hw/usb-ccid.c new file +5
-0+uint32_t state_vmstate; +uint8_t migration_state; +uint32_t migration_target_ip; +uint16_t migration_target_port; +};Try to place the struct elements a little better so you don't end up with a lot of space wasted due to natural alignment by the compiler.
ok, this one's me. I'm really not sure except for stuff that goes on the wire or get's allocated a bazillion times that this is worth the change in general, but since I'm respinning anyway I'll do it. (unless you're saying it should be a habit).
Quoted 4 lines
Quoted 4 lines
hw/usb-ccid.c new file +4
-0+static void ccid_bulk_in_get (USBCCIDState *s) +{ +if (s->current_bulk_in !=NULL || s->bulk_in_pending_num ==0 ) { +return ;
Quoted 8 lines
hw/usb-ccid.c line 539
Quoted 5 lines
hw/usb-ccid.c new file +5
-0+ } +assert (s->bulk_in_pending_num >0 ); + s->bulk_in_pending_num--; + s->current_bulk_in = &s->bulk_in_pending[ + (s->bulk_in_pending_start++) %BULK_IN_PENDING_NUM ];That line break is really not good :( Either break it after the '=' or calculate the index outside the assignment statement.
ok, after the =, but then I think the rest is >80, so it will neccessitate another break.
Quoted 7 lines
hw/usb-ccid.c line 778
Quoted 4 lines
hw/usb-ccid.c new file +3
-0+static void ccid_write_data_block ( +USBCCIDState *s,uint8_t slot,uint8_t seq, +const uint8_t *data,uint32_t len)Please fix this - keep some arguments on the first line, and align the following ones to match.
Is that a coding style thing I missed or personal preferance? my personal preferance here is the way it is, since it looks shorter/more readable, but I don't care that much.
Quoted 7 lines
Quoted 4 lines
+/* handle a single
USB_TOKEN_OUT, return value returned to guest.
+ * 0 - all ok
+ *USB_RET_STALL- failed to handle packet */another badly formatted comment
fixing them all.
Quoted 6 lines
Quoted 6 lines
hw/usb-ccid.c new file +6
-0+void ccid_card_card_error (CCIDCardState *card,uint64_t error) +{ +USBCCIDState *s = +DO_UPCAST (USBCCIDState, dev.qdev, card->qdev.parent_bus->parent); + + s->bmCommandStatus =COMMAND_STATUS_FAILED ;
Quoted 8 lines
hw/usb-ccid.c line 1183
Quoted 6 lines
hw/usb-ccid.c new file +6
-0+ s->last_answer_error = error; +DPRINTF (s,1 ,"VSC_Error: %lX\n" , s->last_answer_error); +/* TODO: these error's should be more verbose and propogated to the guest. +* */ +/* we flush all pending answers on CardRemove message in ccid-card-passthru, +* so check that first to not trigger abort */ !!! there's more below.
? more badly formated comments? more todos? more flushing?
Quoted 7 lines
Except for the mostly cosmetic stuff, it looks ok to me.
Cheers,
Jes
Re: [Qemu-devel] [PATCH 1/7] usb-ccid: add CCID bus
Quoted 13 lines · Alon Levy
Quoted 11 lines · Jes Sorensen
hw/usb-ccid.c line 179
Quoted 6 lines
hw/usb-ccid.c new file +6
-0+typedef struct __attribute__ ((__packed__ ))CCID_Header { +uint8_t bMessageType; +uint32_t dwLength; +uint8_t bSlot; +uint8_t bSeq; +}CCID_Header ;Is this header decided upon by the CCID spec or the code? It seems suboptimal to have a uint8 in front of a uint32 like that. Inefficient structure alignment :(
Quoted 1 line
In the spec.
I was afraid of that, clearly a spec written by the people doing the wire protocol, without considering the software aspects.
Quoted 39 lines
Quoted 29 lines
Quoted 29 lines
hw/usb-ccid.c new file +29
-0+/** +* powered - defaults to true, changed by PowerOn/PowerOff messages +*/ +struct USBCCIDState { +USBDevice dev; +CCIDBus *bus; +CCIDCardState *card; +CCIDCardInfo *cardinfo;/* caching the info pointer */ +uint8_t debug; +BulkIn bulk_in_pending[BULK_IN_PENDING_NUM ];/* circular */ +uint32_t bulk_in_pending_start; +uint32_t bulk_in_pending_end;/* first free */ +uint32_t bulk_in_pending_num; +BulkIn *current_bulk_in; +uint8_t bulk_out_data[BULK_OUT_DATA_SIZE ]; +uint32_t bulk_out_pos; +uint8_t bmSlotICCState; +uint8_t powered; +uint8_t notify_slot_change; +uint64_t last_answer_error; +Answer pending_answers[PENDING_ANSWERS_NUM ]; +uint32_t pending_answers_start; +uint32_t pending_answers_end; +uint32_t pending_answers_num; +uint8_t bError; +uint8_t bmCommandStatus; +uint8_t bProtocolNum; +uint8_t abProtocolDataStructure[MAX_PROTOCOL_SIZE ]; +uint32_t ulProtocolDataStructureSize;Quoted 9 lines
hw/usb-ccid.c line 291
Quoted 5 lines
hw/usb-ccid.c new file +5
-0+uint32_t state_vmstate; +uint8_t migration_state; +uint32_t migration_target_ip; +uint16_t migration_target_port; +};Try to place the struct elements a little better so you don't end up with a lot of space wasted due to natural alignment by the compiler.
Quoted 4 lines
ok, this one's me. I'm really not sure except for stuff that goes on the wire or get's allocated a bazillion times that this is worth the change in general, but since I'm respinning anyway I'll do it. (unless you're saying it should be a habit).
If it is a one-off allocation, it's really not a big deal, but it is a good thing to keep in mind. In particular on non-x86 64 bit entities are normally 64 bit aligned.
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
hw/usb-ccid.c new file +4
-0+static void ccid_bulk_in_get (USBCCIDState *s) +{ +if (s->current_bulk_in !=NULL || s->bulk_in_pending_num ==0 ) { +return ;Quoted 8 lines
hw/usb-ccid.c line 539
Quoted 5 lines
hw/usb-ccid.c new file +5
-0+ } +assert (s->bulk_in_pending_num >0 ); + s->bulk_in_pending_num--; + s->current_bulk_in = &s->bulk_in_pending[ + (s->bulk_in_pending_start++) %BULK_IN_PENDING_NUM ];That line break is really not good :( Either break it after the '=' or calculate the index outside the assignment statement.
Quoted 2 lines
ok, after the =, but then I think the rest is >80, so it will neccessitate another break.
If it was my code, I would calculate the index on the previous line in a tmp variable. It is a matter of personal preference of course.
Quoted 10 lines
Quoted 6 lines
hw/usb-ccid.c line 778
Quoted 3 lines
hw/usb-ccid.c new file +3
-0+static void ccid_write_data_block ( +USBCCIDState *s,uint8_t slot,uint8_t seq, +const uint8_t *data,uint32_t len)Please fix this - keep some arguments on the first line, and align the following ones to match.
Is that a coding style thing I missed or personal preferance? my personal preferance here is the way it is, since it looks shorter/more readable, but I don't care that much.
It is not written down :(, but it is common practice. I raise the issue exactly because it is much more readable the other way :)
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
+/* handle a single
USB_TOKEN_OUT, return value returned to guest.
+ * 0 - all ok
+ *USB_RET_STALL- failed to handle packet */another badly formatted comment
fixing them all.
Excellent!
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
hw/usb-ccid.c new file +6
-0+void ccid_card_card_error (CCIDCardState *card,uint64_t error) +{ +USBCCIDState *s = +DO_UPCAST (USBCCIDState, dev.qdev, card->qdev.parent_bus->parent); + + s->bmCommandStatus =COMMAND_STATUS_FAILED ;Quoted 8 lines
hw/usb-ccid.c line 1183
Quoted 6 lines
hw/usb-ccid.c new file +6
-0+ s->last_answer_error = error; +DPRINTF (s,1 ,"VSC_Error: %lX\n" , s->last_answer_error); +/* TODO: these error's should be more verbose and propogated to the guest. +* */ +/* we flush all pending answers on CardRemove message in ccid-card-passthru, +* so check that first to not trigger abort */ !!! there's more below.
Quoted 1 line
? more badly formated comments? more todos? more flushing?
Comments yeah. It doesn't affect the code, but for a new patch it really is better to get it straightened before it goes upstream.
Cheers,
Jes
Re: [Qemu-devel] [PATCH 5/7] ccid: add ccid-card-emulated device
In reply to Jes SorensenQuoted 30 lines · Jes Sorensen
Quoted 29 lines · Alon Levy
hw/ccid-card-emulated.c new file +23
-0@@ -0,0 +1,599 @@ +/* +* CCID Card Device. Emulated card. +* +* Copyright (c) 2011 Red Hat. +* Written by Alon Levy. +* +* This code is licenced under the GNU LGPL, version 2 or later. +*/ + +/* +* It can be used to provide access to the local hardware in a non exclusive +* way, or it can use certificates. It requires the usb-ccid bus. +* +* Usage 1: standard, mirror hardware reader+card: +* qemu .. -usb -device usb-ccid -device ccid-card-emulated +* +* Usage 2: use certificates, no hardware required +* one time: create the certificates: +* for i in 1 2 3; do +* certutil -d /etc/pki/nssdb -x -t "CT,CT,CT" -S -s "CN=user$i" -n user$i +* done +* qemu .. -usb -device usb-ccid \ +* -device ccid-card-emulated,cert1=user1,cert2=user2,cert3=user3
Quoted 8 lines
hw/ccid-card-emulated.c line 29
Quoted 6 lines
hw/ccid-card-emulated.c new file +6
-0+* +* If you use a non default db for the certificates you can specify it using +* the db parameter. +*/ + +#include <pthread.h>
qemu-thread.h
ok, fixing.
Quoted 13 lines
hw/ccid-card-emulated.c line 78
Quoted 11 lines
hw/ccid-card-emulated.c new file +10
-0+static const char *emul_event_to_string (uint32_t emul_event) +{ +switch (emul_event) { +case EMUL_READER_INSERT :return "EMUL_READER_INSERT" ; +case EMUL_READER_REMOVE :return "EMUL_READER_REMOVE" ; +case EMUL_CARD_INSERT :return "EMUL_CARD_INSERT" ; +case EMUL_CARD_REMOVE :return "EMUL_CARD_REMOVE" ; +case EMUL_GUEST_APDU :return "EMUL_GUEST_APDU" ; +case EMUL_RESPONSE_APDU :return "EMUL_RESPONSE_APDU" ; +case EMUL_ERROR :return "EMUL_ERROR" ;YUCK!
can we turn down the caps / disgust statements? I understand this is a personal affront to you somehow? can we settle this at dawn tommorrow?
Quoted 21 lines
No multi statements on a single line!
Quoted 18 lines
hw/ccid-card-emulated.c new file +18
-0+#define MAX_ATR_SIZE 40 +struct EmulatedState { +CCIDCardState base; +uint8_t debug; +char *backend_str; +uint32_t backend; +char *cert1; +char *cert2; +char *cert3; +char *db; +uint8_t atr[MAX_ATR_SIZE ]; +uint8_t atr_length; + QSIMPLEQ_HEAD(event_list, EmulEvent) event_list; +pthread_mutex_t event_list_mutex; +VReader *reader; + QSIMPLEQ_HEAD(guest_apdu_list, EmulEvent) guest_apdu_list; +pthread_mutex_t vreader_mutex;/* and guest_apdu_list mutex */ +pthread_mutex_t handle_apdu_mutex;
Quoted 8 lines
hw/ccid-card-emulated.c line 126
Quoted 6 lines
hw/ccid-card-emulated.c new file +6
-0+pthread_cond_t handle_apdu_cond; +int pipe[2 ]; +int quit_apdu_thread; +pthread_mutex_t apdu_thread_quit_mutex; +pthread_cond_t apdu_thread_quit_cond; +};Bad struct packing and wrong thread types.
will use qemu-thread. that's what you mean by wrong thread types, right? s/pthread_thread_t/QemuThread/ etc. (Cond, Mutex)
Quoted 6 lines
hw/ccid-card-emulated.c line 166
Quoted 4 lines
hw/ccid-card-emulated.c new file +3
-0+static void emulated_push_type (EmulatedState *card,uint32_t type) +{ +EmulEvent *event = (EmulEvent *)malloc (sizeof (EmulEvent));
qemu_malloc()
yep, fixing.
Quoted 11 lines
hw/ccid-card-emulated.c line 175
Quoted 9 lines
hw/ccid-card-emulated.c new file +8
-0+assert (event); + event->p.gen.type = type; +emulated_push_event (card, event); +} + +static void emulated_push_error (EmulatedState *card,uint64_t code) +{ +EmulEvent *event = (EmulEvent *)malloc (sizeof (EmulEvent));
qemu_malloc()
fixing.
Quoted 13 lines
hw/ccid-card-emulated.c line 186
Quoted 11 lines
hw/ccid-card-emulated.c new file +10
-0+assert (event); + event->p.error.type =EMUL_ERROR ; + event->p.error.code = code; +emulated_push_event (card, event); +} + +static void emulated_push_data_type (EmulatedState *card,uint32_t type, +const uint8_t *data,uint32_t len) +{ +EmulEvent *event = (EmulEvent *)malloc (sizeof (EmulEvent) + len);
qemu_malloc()
fixing.
Quoted 13 lines
hw/ccid-card-emulated.c line 367
Quoted 11 lines
hw/ccid-card-emulated.c new file +10
-0+static void pipe_read (void *opaque) +{ +EmulatedState *card = opaque; +EmulEvent *event, *next; +char dummy; +int len; + +do { + len =read (card->pipe[0 ], &dummy,sizeof (dummy)); + }while (len ==sizeof (dummy));Shouldn't you check error codes here?
yes, my bad.
Quoted 2 lines
Jes
Re: [Qemu-devel] [PATCH 5/7] ccid: add ccid-card-emulated device
On Thu, Mar 17, 2011 at 12:54:16PM +0200, Alon Levy wrote:
sorry for the double review of the review, nothing new here.
Quoted 144 lines · Jes Sorensen
Quoted 30 lines · Alon Levy
Quoted 29 lines
hw/ccid-card-emulated.c new file +23
-0@@ -0,0 +1,599 @@ +/* +* CCID Card Device. Emulated card. +* +* Copyright (c) 2011 Red Hat. +* Written by Alon Levy. +* +* This code is licenced under the GNU LGPL, version 2 or later. +*/ + +/* +* It can be used to provide access to the local hardware in a non exclusive +* way, or it can use certificates. It requires the usb-ccid bus. +* +* Usage 1: standard, mirror hardware reader+card: +* qemu .. -usb -device usb-ccid -device ccid-card-emulated +* +* Usage 2: use certificates, no hardware required +* one time: create the certificates: +* for i in 1 2 3; do +* certutil -d /etc/pki/nssdb -x -t "CT,CT,CT" -S -s "CN=user$i" -n user$i +* done +* qemu .. -usb -device usb-ccid \ +* -device ccid-card-emulated,cert1=user1,cert2=user2,cert3=user3 Quoted 8 lines
hw/ccid-card-emulated.c line 29
Quoted 6 lines
hw/ccid-card-emulated.c new file +6
-0+* +* If you use a non default db for the certificates you can specify it using +* the db parameter. +*/ + +#include <pthread.h>
qemu-thread.hok, fixing.
Quoted 13 lines
hw/ccid-card-emulated.c line 78
Quoted 11 lines
hw/ccid-card-emulated.c new file +10
-0+static const char *emul_event_to_string (uint32_t emul_event) +{ +switch (emul_event) { +case EMUL_READER_INSERT :return "EMUL_READER_INSERT" ; +case EMUL_READER_REMOVE :return "EMUL_READER_REMOVE" ; +case EMUL_CARD_INSERT :return "EMUL_CARD_INSERT" ; +case EMUL_CARD_REMOVE :return "EMUL_CARD_REMOVE" ; +case EMUL_GUEST_APDU :return "EMUL_GUEST_APDU" ; +case EMUL_RESPONSE_APDU :return "EMUL_RESPONSE_APDU" ; +case EMUL_ERROR :return "EMUL_ERROR" ;YUCK!
can we turn down the caps / disgust statements? I understand this is a personal affront to you somehow? can we settle this at dawn tommorrow?
Quoted 21 lines
No multi statements on a single line!
Quoted 18 lines
hw/ccid-card-emulated.c new file +18
-0+#define MAX_ATR_SIZE 40 +struct EmulatedState { +CCIDCardState base; +uint8_t debug; +char *backend_str; +uint32_t backend; +char *cert1; +char *cert2; +char *cert3; +char *db; +uint8_t atr[MAX_ATR_SIZE ]; +uint8_t atr_length; + QSIMPLEQ_HEAD(event_list, EmulEvent) event_list; +pthread_mutex_t event_list_mutex; +VReader *reader; + QSIMPLEQ_HEAD(guest_apdu_list, EmulEvent) guest_apdu_list; +pthread_mutex_t vreader_mutex;/* and guest_apdu_list mutex */ +pthread_mutex_t handle_apdu_mutex;Quoted 8 lines
hw/ccid-card-emulated.c line 126
Quoted 6 lines
hw/ccid-card-emulated.c new file +6
-0+pthread_cond_t handle_apdu_cond; +int pipe[2 ]; +int quit_apdu_thread; +pthread_mutex_t apdu_thread_quit_mutex; +pthread_cond_t apdu_thread_quit_cond; +};Bad struct packing and wrong thread types.
will use qemu-thread. that's what you mean by wrong thread types, right? s/pthread_thread_t/QemuThread/ etc. (Cond, Mutex)
Quoted 6 lines
hw/ccid-card-emulated.c line 166
Quoted 4 lines
hw/ccid-card-emulated.c new file +3
-0+static void emulated_push_type (EmulatedState *card,uint32_t type) +{ +EmulEvent *event = (EmulEvent *)malloc (sizeof (EmulEvent));
qemu_malloc()yep, fixing.
Quoted 11 lines
hw/ccid-card-emulated.c line 175
Quoted 9 lines
hw/ccid-card-emulated.c new file +8
-0+assert (event); + event->p.gen.type = type; +emulated_push_event (card, event); +} + +static void emulated_push_error (EmulatedState *card,uint64_t code) +{ +EmulEvent *event = (EmulEvent *)malloc (sizeof (EmulEvent));
qemu_malloc()fixing.
Quoted 13 lines
hw/ccid-card-emulated.c line 186
Quoted 11 lines
hw/ccid-card-emulated.c new file +10
-0+assert (event); + event->p.error.type =EMUL_ERROR ; + event->p.error.code = code; +emulated_push_event (card, event); +} + +static void emulated_push_data_type (EmulatedState *card,uint32_t type, +const uint8_t *data,uint32_t len) +{ +EmulEvent *event = (EmulEvent *)malloc (sizeof (EmulEvent) + len);
qemu_malloc()fixing.
Quoted 13 lines
hw/ccid-card-emulated.c line 367
Quoted 11 lines
hw/ccid-card-emulated.c new file +10
-0+static void pipe_read (void *opaque) +{ +EmulatedState *card = opaque; +EmulEvent *event, *next; +char dummy; +int len; + +do { + len =read (card->pipe[0 ], &dummy,sizeof (dummy)); + }while (len ==sizeof (dummy));Shouldn't you check error codes here?
yes, my bad.
Quoted 2 lines
Jes
Re: [Qemu-devel] [PATCH 4/7] libcacard: initial commit
In reply to Jes SorensenQuoted 280 lines · Jes Sorensen
libcacard/cac.c line 27
Quoted 10 lines · Alon Levy
libcacard/cac.c new file +10
-0+/* private data for PKI applets */ +typedef struct CACPKIAppletDataStruct { +unsigned char *cert; +int cert_len; +unsigned char *cert_buffer; +int cert_buffer_len; +unsigned char *sign_buffer; +int sign_buffer_len; +VCardKey *key; +}CACPKIAppletData ;Grouping the ints together would allow for better struct padding.
Quoted 3 lines
+/*
+ * resest the inter call state between applet selects
+ */I presume it meant to say 'resets' ?
libcacard/event.c line 3
Quoted 9 lines
libcacard/event.c new file +3
-0@@ -0,0 +1,112 @@ +/* +* +*/ This comment is really spot on :)
libcacard/mutex.h
Quoted 5 lines
libcacard/mutex.h new file +0
-0UH OH!
Quoted 29 lines
libcacard/mutex.h new file +29
-0+/* +* This header file provides a way of mapping windows and linux thread calls +* to a set of macros. Ideally this would be shared by whatever subsystem we +* link with. +*/ + +#ifndef _H_MUTEX +#define _H_MUTEX +#ifdef _WIN32 +#include <windows.h> +typedef CRITICAL_SECTION mutex_t ; +#define MUTEX_INIT (mutex) InitializeCriticalSection(&mutex) +#define MUTEX_LOCK (mutex) EnterCriticalSection(&mutex) +#define MUTEX_UNLOCK (mutex) LeaveCriticalSection(&mutex) +typedef CONDITION_VARIABLE condition_t ; +#define CONDITION_INIT (cond) InitializeConditionVariable(&cond) +#define CONDITION_WAIT (cond, mutex) \ + SleepConditionVariableCS(&cond, &mutex, INFINTE) +#define CONDITION_NOTIFY (cond) WakeConditionVariable(&cond) +typedef uint32_t thread_t ; +typedef HANDLE thread_status_t ; +#define THREAD_CREATE (tid, func, arg) \ + CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, arg, 0, &tid) +#define THREAD_SUCCESS (status) ((status) != NULL) +#else +#include <pthread.h> +typedef pthread_mutex_t mutex_t ; +#define MUTEX_INIT (mutex) pthread_mutex_init(&mutex, NULL) +#define MUTEX_LOCK (mutex) pthread_mutex_lock(&mutex)libcacard/mutex.h line 57
Quoted 10 lines
libcacard/mutex.h new file +10
-0+#define MUTEX_UNLOCK (mutex) pthread_mutex_unlock(&mutex) +typedef pthread_cond_t condition_t ; +#define CONDITION_INIT (cond) pthread_cond_init(&cond, NULL) +#define CONDITION_WAIT (cond, mutex) pthread_cond_wait(&cond, &mutex) +#define CONDITION_NOTIFY (cond) pthread_cond_signal(&cond) +typedef pthread_t thread_t ; +typedef int thread_status_t ; +#define THREAD_CREATE (tid, func, arg) pthread_create(&tid, NULL, func, arg) +#define THREAD_SUCCESS (status) ((status) == 0) +#endif NACK! This is no good, the code needs to be fixed to use QemuMutex from
qemu-thread.hIn addition, a thing like a mutex feature should be in a separate patch,
not part of the code that uses it. However QEMU already has a mutex set
so this part needs to go.libcacard/mutex.h
Quoted 7 lines
libcacard/mutex.h new file +7
-0+static VCardAppletPrivate * +passthru_new_applet_private (VReader *reader) +{ +VCardAppletPrivate *applet_private =NULL ; +LONG rv; + + applet_private = (VCardAppletPrivate *)malloc (sizeof (VCardAppletPrivate));
qemu_malloc()libcacard/mutex.h
Quoted 3 lines
libcacard/mutex.h new file +3
-0+if (applet_private ==NULL ) { + goto fail; + }and it never fails.
libcacard/mutex.h
Quoted 3 lines
libcacard/mutex.h new file +3
-0+if (new_reader_list_len != reader_list_len) { +/* update the list */ + new_reader_list = (char *)malloc (new_reader_list_len);
qemu_malloc()again.Please grep through the full patch set and make sure you do not have any direct calls to
malloc()orfree().libcacard/mutex.h
Quoted 4 lines
libcacard/mutex.h new file +4
-0+/* try resetting the pcsc_lite subsystem */ +SCardReleaseContext (global_context); + global_context =0 ;/* should close it */ +printf ("***** SCard failure %x\n" , rv);
error_report()Quoted 5 lines
libcacard/vcard_emul_nss.c new file +0
-0[snip]
libcacard/vcard_emul_nss.c line 45
Quoted 5 lines
libcacard/vcard_emul_nss.c new file +5
-0+struct VReaderEmulStruct { +PK11SlotInfo *slot; +VCardEmulType default_type; +char *type_params; +PRBool present;What is PRBool and where does it come from?
Quoted 4 lines
libcacard/vcard_emul_nss.c new file +4
-0+void +vcard_emul_reset (VCard *card,VCardPower power) +{ +PK11SlotInfo *slot;libcacard/vcard_emul_nss.c line 314
Quoted 10 lines
libcacard/vcard_emul_nss.c new file +10
-0+ +if (!nss_emul_init) { +return ; + } + +/* if we reset the card (either power on or power off), we loose our login +* state */ +/* TODO: we may also need to send insertion/removal events? */ + slot =vcard_emul_card_get_slot (card); + (void )PK11_Logout (slot);We don't (void) cast calls like that in QEMU.
Quoted 9 lines
libcacard/vcard_emul_nss.c new file +9
-0+/* +* NSS needs the app to supply a password prompt. In our case the only time +* the password is supplied is as part of the Login APDU. The actual password +* is passed in the pw_arg in that case. In all other cases pw_arg should be +* NULL. +*/ +static char * +vcard_emul_get_password (PK11SlotInfo *slot,PRBool retries,void *pw_arg) +{libcacard/vcard_emul_nss.c line 675
Quoted 10 lines
libcacard/vcard_emul_nss.c new file +10
-0+/* if it didn't work the first time, don't keep trying */ +if (retries) { +return NULL ; + } +/* we are looking up a password when we don't have one in hand */ +if (pw_arg ==NULL ) { +return NULL ; + } +/* TODO: we really should verify that were are using the right slot */ +return PORT_Strdup (pw_arg);
PORT_Strdup???libcacard/vcard_emul_nss.c line 983
Quoted 11 lines
libcacard/vcard_emul_nss.c new file +11
-0+static const char * +find_token (const char *str,char token,char token_end) +{ +/* just do the blind simple thing */ +for (; *str; str++) { +if ((*str == token) || (*str == token_end)) { +break ; + } + } +return str; +}What is wrong with strpbrk(3) ?
libcacard/vcard_emul_nss.c line 991
Quoted 7 lines
libcacard/vcard_emul_nss.c new file +7
-0+static const char * +strip (const char *str) +{ +for (; *str; str++) { +if ((*str !=' ' ) && (*str !='\n' ) && + (*str !='\t' ) && (*str !='\r' )) { +break ;!isspace() ?
libcacard/vcard_emul_nss.c line 1062
Quoted 12 lines
libcacard/vcard_emul_nss.c new file +12
-0+static const char * +find_blank (const char *str) +{ +for (; *str; str++) { +if ((*str ==' ' ) || (*str =='\n' ) || + (*str =='\t' ) || (*str =='\r' )) { + +break ; + } + } +return str; +}strpbrk(3) ?
Quoted 13 lines
libcacard/vcardt.h new file +7
-0@@ -0,0 +1,66 @@ +/* +* +*/ +#ifndef VCARDT_H +#define VCARDT_H 1 + +/* libcacard/vcardt.h line 17
Quoted 10 lines
libcacard/vcardt.h new file +10
-0+* these should come from some common spice header file +*/ +#include <assert.h> +#ifndef ASSERT +#define ASSERT assert +#endif +#ifndef MIN +#define MIN (x, y) ((x) > (y) ? (y) : (x)) +#define MAX (x, y) ((x) > (y) ? (x) : (y)) +#endif QEMU uses
assert(), notASSERT(). Please fix.libcacard/vreader.c line 3
Quoted 9 lines
libcacard/vreader.c new file +3
-0@@ -0,0 +1,526 @@ +/* +* emulate the reader +*/ What is the license of this file?
Quoted 6 lines
libcacard/vreader.c new file +6
-0+#include "vcard.h" +#include "vcard_emul.h" +#include "card_7816.h" +#include "vreader.h" +#include "vevent.h" +libcacard/vreader.c line 19
Quoted 10 lines
libcacard/vreader.c new file +10
-0+/* +* System includes +*/ +#include <stdlib.h> +#include <string.h> + +/* +* spice includes +*/ +#include "mutex.h" No no no
libcacard/vreadert.h line 3
Quoted 9 lines
libcacard/vreadert.h new file +3
-0@@ -0,0 +1,23 @@ +/* +* +*/ Spot on!
Quoted 4 lines
General comment, this patch is *way* too big. It really should be a series of patches adding features one after another. The testing for cards ought to be separate for example.
I've got everything done except the split. Is there any value in sending it without doing the split? could you give me some hints. As I mentioned off line, this code was not written by me, which makes this harder (but not impossible)
Alon
Quoted 2 lines
Jes
Re: [Qemu-devel] [PATCH 5/7] ccid: add ccid-card-emulated device
In reply to Alon LevyQuoted 13 lines · Alon Levy
Quoted 4 lines · Jes Sorensen
Quoted 4 lines
hw/ccid-card-emulated.c new file +4
-0+static const char *emul_event_to_string (uint32_t emul_event) +{ +switch (emul_event) { +case EMUL_READER_INSERT :return "EMUL_READER_INSERT" ;Quoted 8 lines
hw/ccid-card-emulated.c line 78
Quoted 6 lines
hw/ccid-card-emulated.c new file +6
-0+case EMUL_READER_REMOVE :return "EMUL_READER_REMOVE" ; +case EMUL_CARD_INSERT :return "EMUL_CARD_INSERT" ; +case EMUL_CARD_REMOVE :return "EMUL_CARD_REMOVE" ; +case EMUL_GUEST_APDU :return "EMUL_GUEST_APDU" ; +case EMUL_RESPONSE_APDU :return "EMUL_RESPONSE_APDU" ; +case EMUL_ERROR :return "EMUL_ERROR" ;YUCK!
Quoted 3 lines
can we turn down the caps / disgust statements? I understand this is a personal affront to you somehow? can we settle this at dawn tommorrow?
LOL, there is nothing in coding style allowing this, even if there's a couple of cases in the code still doing it.
Tomorrow is ok - will you be taking an overnight flight here? :)
Quoted 29 lines
Quoted 21 lines
No multi statements on a single line!
Quoted 18 lines
hw/ccid-card-emulated.c new file +18
-0+#define MAX_ATR_SIZE 40 +struct EmulatedState { +CCIDCardState base; +uint8_t debug; +char *backend_str; +uint32_t backend; +char *cert1; +char *cert2; +char *cert3; +char *db; +uint8_t atr[MAX_ATR_SIZE ]; +uint8_t atr_length; + QSIMPLEQ_HEAD(event_list, EmulEvent) event_list; +pthread_mutex_t event_list_mutex; +VReader *reader; + QSIMPLEQ_HEAD(guest_apdu_list, EmulEvent) guest_apdu_list; +pthread_mutex_t vreader_mutex;/* and guest_apdu_list mutex */ +pthread_mutex_t handle_apdu_mutex;Quoted 8 lines
hw/ccid-card-emulated.c line 126
Quoted 6 lines
hw/ccid-card-emulated.c new file +6
-0+pthread_cond_t handle_apdu_cond; +int pipe[2 ]; +int quit_apdu_thread; +pthread_mutex_t apdu_thread_quit_mutex; +pthread_cond_t apdu_thread_quit_cond; +};Bad struct packing and wrong thread types.
Quoted 2 lines
will use qemu-thread. that's what you mean by wrong thread types, right? s/pthread_thread_t/QemuThread/ etc. (Cond, Mutex)
Yep
Looks good!
Cheers,
Jes