xref: /openbmc/qemu/docs/system/devices/ccid.rst (revision 591e7bb5fdd18c23f1265871076532ff44fa7c2a)
1*46676192SLucas RamageChip Card Interface Device (CCID)
2*46676192SLucas Ramage=================================
3*46676192SLucas Ramage
4*46676192SLucas RamageUSB CCID device
5*46676192SLucas Ramage---------------
6*46676192SLucas RamageThe USB CCID device is a USB device implementing the CCID specification, which
7*46676192SLucas Ramagelets one connect smart card readers that implement the same spec. For more
8*46676192SLucas Ramageinformation see the specification::
9*46676192SLucas Ramage
10*46676192SLucas Ramage  Universal Serial Bus
11*46676192SLucas Ramage  Device Class: Smart Card
12*46676192SLucas Ramage  CCID
13*46676192SLucas Ramage  Specification for
14*46676192SLucas Ramage  Integrated Circuit(s) Cards Interface Devices
15*46676192SLucas Ramage  Revision 1.1
16*46676192SLucas Ramage  April 22rd, 2005
17*46676192SLucas Ramage
18*46676192SLucas RamageSmartcards are used for authentication, single sign on, decryption in
19*46676192SLucas Ramagepublic/private schemes and digital signatures. A smartcard reader on the client
20*46676192SLucas Ramagecannot be used on a guest with simple usb passthrough since it will then not be
21*46676192SLucas Ramageavailable on the client, possibly locking the computer when it is "removed". On
22*46676192SLucas Ramagethe other hand this device can let you use the smartcard on both the client and
23*46676192SLucas Ramagethe guest machine. It is also possible to have a completely virtual smart card
24*46676192SLucas Ramagereader and smart card (i.e. not backed by a physical device) using this device.
25*46676192SLucas Ramage
26*46676192SLucas RamageBuilding
27*46676192SLucas Ramage--------
28*46676192SLucas RamageThe cryptographic functions and access to the physical card is done via the
29*46676192SLucas Ramagelibcacard library, whose development package must be installed prior to
30*46676192SLucas Ramagebuilding QEMU:
31*46676192SLucas Ramage
32*46676192SLucas RamageIn redhat/fedora::
33*46676192SLucas Ramage
34*46676192SLucas Ramage  yum install libcacard-devel
35*46676192SLucas Ramage
36*46676192SLucas RamageIn ubuntu::
37*46676192SLucas Ramage
38*46676192SLucas Ramage  apt-get install libcacard-dev
39*46676192SLucas Ramage
40*46676192SLucas RamageConfiguring and building::
41*46676192SLucas Ramage
42*46676192SLucas Ramage  ./configure --enable-smartcard && make
43*46676192SLucas Ramage
44*46676192SLucas RamageUsing ccid-card-emulated with hardware
45*46676192SLucas Ramage--------------------------------------
46*46676192SLucas RamageAssuming you have a working smartcard on the host with the current
47*46676192SLucas Ramageuser, using libcacard, QEMU acts as another client using ccid-card-emulated::
48*46676192SLucas Ramage
49*46676192SLucas Ramage  qemu -usb -device usb-ccid -device ccid-card-emulated
50*46676192SLucas Ramage
51*46676192SLucas RamageUsing ccid-card-emulated with certificates stored in files
52*46676192SLucas Ramage----------------------------------------------------------
53*46676192SLucas RamageYou must create the CA and card certificates. This is a one time process.
54*46676192SLucas RamageWe use NSS certificates::
55*46676192SLucas Ramage
56*46676192SLucas Ramage  mkdir fake-smartcard
57*46676192SLucas Ramage  cd fake-smartcard
58*46676192SLucas Ramage  certutil -N -d sql:$PWD
59*46676192SLucas Ramage  certutil -S -d sql:$PWD -s "CN=Fake Smart Card CA" -x -t TC,TC,TC -n fake-smartcard-ca
60*46676192SLucas Ramage  certutil -S -d sql:$PWD -t ,, -s "CN=John Doe" -n id-cert -c fake-smartcard-ca
61*46676192SLucas Ramage  certutil -S -d sql:$PWD -t ,, -s "CN=John Doe (signing)" --nsCertType smime -n signing-cert -c fake-smartcard-ca
62*46676192SLucas Ramage  certutil -S -d sql:$PWD -t ,, -s "CN=John Doe (encryption)" --nsCertType sslClient -n encryption-cert -c fake-smartcard-ca
63*46676192SLucas Ramage
64*46676192SLucas RamageNote: you must have exactly three certificates.
65*46676192SLucas Ramage
66*46676192SLucas RamageYou can use the emulated card type with the certificates backend::
67*46676192SLucas Ramage
68*46676192SLucas Ramage  qemu -usb -device usb-ccid -device ccid-card-emulated,backend=certificates,db=sql:$PWD,cert1=id-cert,cert2=signing-cert,cert3=encryption-cert
69*46676192SLucas Ramage
70*46676192SLucas RamageTo use the certificates in the guest, export the CA certificate::
71*46676192SLucas Ramage
72*46676192SLucas Ramage  certutil -L -r -d sql:$PWD -o fake-smartcard-ca.cer -n fake-smartcard-ca
73*46676192SLucas Ramage
74*46676192SLucas Ramageand import it in the guest::
75*46676192SLucas Ramage
76*46676192SLucas Ramage  certutil -A -d /etc/pki/nssdb -i fake-smartcard-ca.cer -t TC,TC,TC -n fake-smartcard-ca
77*46676192SLucas Ramage
78*46676192SLucas RamageIn a Linux guest you can then use the CoolKey PKCS #11 module to access
79*46676192SLucas Ramagethe card::
80*46676192SLucas Ramage
81*46676192SLucas Ramage  certutil -d /etc/pki/nssdb -L -h all
82*46676192SLucas Ramage
83*46676192SLucas RamageIt will prompt you for the PIN (which is the password you assigned to the
84*46676192SLucas Ramagecertificate database early on), and then show you all three certificates
85*46676192SLucas Ramagetogether with the manually imported CA cert::
86*46676192SLucas Ramage
87*46676192SLucas Ramage  Certificate Nickname                        Trust Attributes
88*46676192SLucas Ramage  fake-smartcard-ca                           CT,C,C
89*46676192SLucas Ramage  John Doe:CAC ID Certificate                 u,u,u
90*46676192SLucas Ramage  John Doe:CAC Email Signature Certificate    u,u,u
91*46676192SLucas Ramage  John Doe:CAC Email Encryption Certificate   u,u,u
92*46676192SLucas Ramage
93*46676192SLucas RamageIf this does not happen, CoolKey is not installed or not registered with
94*46676192SLucas RamageNSS. Registration can be done from Firefox or the command line::
95*46676192SLucas Ramage
96*46676192SLucas Ramage  modutil -dbdir /etc/pki/nssdb -add "CAC Module" -libfile /usr/lib64/pkcs11/libcoolkeypk11.so
97*46676192SLucas Ramage  modutil -dbdir /etc/pki/nssdb -list
98*46676192SLucas Ramage
99*46676192SLucas RamageUsing ccid-card-passthru with client side hardware
100*46676192SLucas Ramage--------------------------------------------------
101*46676192SLucas RamageOn the host specify the ccid-card-passthru device with a suitable chardev::
102*46676192SLucas Ramage
103*46676192SLucas Ramage  qemu -chardev socket,server=on,host=0.0.0.0,port=2001,id=ccid,wait=off \
104*46676192SLucas Ramage       -usb -device usb-ccid -device ccid-card-passthru,chardev=ccid
105*46676192SLucas Ramage
106*46676192SLucas RamageOn the client run vscclient, built when you built QEMU::
107*46676192SLucas Ramage
108*46676192SLucas Ramage  vscclient <qemu-host> 2001
109*46676192SLucas Ramage
110*46676192SLucas RamageUsing ccid-card-passthru with client side certificates
111*46676192SLucas Ramage------------------------------------------------------
112*46676192SLucas RamageThis case is not particularly useful, but you can use it to debug
113*46676192SLucas Ramageyour setup.
114*46676192SLucas Ramage
115*46676192SLucas RamageFollow instructions above, except run QEMU and vscclient as follows.
116*46676192SLucas Ramage
117*46676192SLucas RamageRun qemu as per above, and run vscclient from the "fake-smartcard"
118*46676192SLucas Ramagedirectory as follows::
119*46676192SLucas Ramage
120*46676192SLucas Ramage  qemu -chardev socket,server=on,host=0.0.0.0,port=2001,id=ccid,wait=off \
121*46676192SLucas Ramage       -usb -device usb-ccid -device ccid-card-passthru,chardev=ccid
122*46676192SLucas Ramage  vscclient -e "db=\"sql:$PWD\" use_hw=no soft=(,Test,CAC,,id-cert,signing-cert,encryption-cert)" <qemu-host> 2001
123*46676192SLucas Ramage
124*46676192SLucas Ramage
125*46676192SLucas RamagePassthrough protocol scenario
126*46676192SLucas Ramage-----------------------------
127*46676192SLucas RamageThis is a typical interchange of messages when using the passthru card device.
128*46676192SLucas Ramageusb-ccid is a usb device. It defaults to an unattached usb device on startup.
129*46676192SLucas Ramageusb-ccid expects a chardev and expects the protocol defined in
130*46676192SLucas Ramagecac_card/vscard_common.h to be passed over that.
131*46676192SLucas RamageThe usb-ccid device can be in one of three modes:
132*46676192SLucas Ramage
133*46676192SLucas Ramage* detached
134*46676192SLucas Ramage* attached with no card
135*46676192SLucas Ramage* attached with card
136*46676192SLucas Ramage
137*46676192SLucas RamageA typical interchange is (the arrow shows who started each exchange, it can be client
138*46676192SLucas Ramageoriginated or guest originated)::
139*46676192SLucas Ramage
140*46676192SLucas Ramage  client event        |    vscclient           |    passthru    |    usb-ccid  |  guest event
141*46676192SLucas Ramage  ------------------------------------------------------------------------------------------------
142*46676192SLucas Ramage                      |    VSC_Init            |                |              |
143*46676192SLucas Ramage                      |    VSC_ReaderAdd       |                |    attach    |
144*46676192SLucas Ramage                      |                        |                |              |  sees new usb device.
145*46676192SLucas Ramage    card inserted ->  |                        |                |              |
146*46676192SLucas Ramage                      |    VSC_ATR             |   insert       |    insert    |  see new card
147*46676192SLucas Ramage                      |                        |                |              |
148*46676192SLucas Ramage                      |    VSC_APDU            |   VSC_APDU     |              | <- guest sends APDU
149*46676192SLucas Ramage  client <-> physical |                        |                |              |
150*46676192SLucas Ramage   card APDU exchange |                        |                |              |
151*46676192SLucas Ramage   client response -> |    VSC_APDU            |   VSC_APDU     |              |  receive APDU response
152*46676192SLucas Ramage                                                      ...
153*46676192SLucas Ramage                                      [APDU<->APDU repeats several times]
154*46676192SLucas Ramage                                                      ...
155*46676192SLucas Ramage     card removed  -> |                        |                |              |
156*46676192SLucas Ramage                      |    VSC_CardRemove      |   remove       |   remove     |   card removed
157*46676192SLucas Ramage                                                      ...
158*46676192SLucas Ramage                                      [(card insert, apdu's, card remove) repeat]
159*46676192SLucas Ramage                                                      ...
160*46676192SLucas Ramage    kill/quit         |                        |                |              |
161*46676192SLucas Ramage      vscclient       |                        |                |              |
162*46676192SLucas Ramage                      |    VSC_ReaderRemove    |                |   detach     |
163*46676192SLucas Ramage                      |                        |                |              |   usb device removed.
164*46676192SLucas Ramage
165*46676192SLucas Ramagelibcacard
166*46676192SLucas Ramage---------
167*46676192SLucas RamageBoth ccid-card-emulated and vscclient use libcacard as the card emulator.
168*46676192SLucas Ramagelibcacard implements a completely virtual CAC (DoD standard for smart
169*46676192SLucas Ramagecards) compliant card and uses NSS to retrieve certificates and do
170*46676192SLucas Ramageany encryption. The backend can then be a real reader and card, or
171*46676192SLucas Ramagecertificates stored in files.
172