Project

General

Profile

Cardem » History » Version 16

tsaitgaist, 11/07/2019 11:05 AM
make command more compatible

1 2 tsaitgaist
{{>toc}}
2
3 1 tsaitgaist
h1. Cardem
4
5 14 tsaitgaist
Card emulation (cardem for short) is a firmware for SIMtrace 2 devices allowing to emulate cards (e.g. SIM).
6
You then can leave the card adapter cable in the phone/modem/reader, and have the actual card outside, in a separate reader.
7 1 tsaitgaist
This allows to easily change or reprogram the card without having to touch the phone.
8
9
This functionality is already implemented and working on sysmoQMOD board.
10 14 tsaitgaist
It is now also available for SIMtrace boards.
11 1 tsaitgaist
*This is a beta firmware and still in development*.
12
See [[Cardem#Limitations|limitations]] for known limitations and issues.
13
14 14 tsaitgaist
h2. Requirements
15
16
h3. PCSC
17
18
The examples described in this article show how to use card emulation by forwarding the traffic to an actual card inserted in another reader.
19
To access this card, a card reader in used in conjunction with the PCSC software.
20
This software allows to use almost any CCID card reader.
21
22
To use PCSC:
23
# install the PCSC daemon (only needs to be done once):
24
<pre>
25
sudo apt install pcscd
26
</pre>
27
# ensure the PCSC daemon is started
28
<pre>
29
sudo systemctl start pcscd
30
</pre>
31
32
To check the available readers and if a card is present, you can use the PCSC tool:
33
# install tool
34
<pre>
35
sudo apt install pcsc-tools
36
</pre>
37
# check if the card is detected by the reader (use CTRL-C to exit)
38
<pre>
39
pcsc_scan 
40
41
Using reader plug'n play mechanism
42
Scanning present readers...
43
0: OMNIKEY 6321 CLi USB (OKCM0030506091345044320140749730) 00 00
44
 
45
Tue Sep 10 16:03:49 2019
46
 Reader 0: OMNIKEY 6321 CLi USB (OKCM0030506091345044320140749730) 00 00
47
  Event number: 0
48
  Card state: Card inserted, 
49
  ATR: 3B 9F 94 80 1F C7 80 31 E0 73 FE 21 1B 67 01 00 00 04 4D 02 01 99
50
</pre>
51
52
h3. USB permissions
53
54
The SIMtrace board is a USB device, and we require the corresponding permission to access it.
55
One way to do it is by using the @sudo@ command in front of all programs accessing the SIMtrace USB device.
56
57
A more appropriate and safer way is to grant the current user access right to this USB device:
58
# create the plugdev group commonly used to access development devices and add yourself into it (you must log out and back in for this change to take effect)
59
<pre>
60
sudo groupadd pulgdev
61 16 tsaitgaist
sudo adduser $USER plugdev
62 14 tsaitgaist
</pre>
63
# install the udev rules for SIMtrace 2 devices
64
<pre>
65
sudo wget -O /etc/udev/rules.d/99-simtrace2.rules https://git.osmocom.org/simtrace2/plain/host/99-simtrace2.rules
66
</pre>
67
# reload the rules
68
<pre>
69
sudo udevadm control --reload-rules
70
sudo udevadm trigger
71
</pre>
72
73 1 tsaitgaist
h2. Flashing
74
75 14 tsaitgaist
You can download the beta firmware for the SIMtrace board here: attachment:simtrace-cardem-dfu.bin.
76 1 tsaitgaist
77
To flash the firmware on the board:
78 15 tsaitgaist
* install dfu-util to flash firmware
79 1 tsaitgaist
<pre>
80 15 tsaitgaist
sudo apt install dfu-util
81 1 tsaitgaist
</pre>
82 15 tsaitgaist
* flash firmware
83
<pre>
84
dfu-util --device 1d50:60e3 --cfg 1 --alt 1 --reset --download simtrace-cardem-dfu.bin
85
</pre>
86 1 tsaitgaist
87
For more details about the flashing procedure, read [[Flashing#SIMtrace2-board|this article]].
88
89
h2. Software
90
91
With the cardem firmware, the SIMtrace v2 board mainly forwards the ISO 7816 card communication over USB.
92
A software on the host must receive the APDU requests and send the corresponding APDU response.
93
There are several software available to do that.
94
Since the USB messages are "specified":https://git.osmocom.org/simtrace2/tree/firmware/libcommon/include/simtrace_prot.h and the software is "open source":https://git.osmocom.org/simtrace2/tree/host, you could implement your own APDU handler.
95
96 11 tsaitgaist
Following are examples on how to use @simtrace2-remsim@ and @osmo-remsim@:
97
* @simtrace2-remsim@ is meant to be used when you have a local setup (e.g. everything on one host computer). The SIMtrace board is connected to the phone/modem, and the actual card you want to forward the traffic to is inserted in a CCID reader connected to the host. The benefit of this setup is that you can easily re-program the card without having to remove it from the phone/modem slot.
98
* @osmo-remsim@ extends the @simtrace2-remsim@ functionality by allowing to have multiple cards located on other hosts. The traffic is then forwarded over the network.
99
100 1 tsaitgaist
h3. simtrace2-remsim
101
102
@simtrace2-remsim@ is the simplest solution.
103
If forwards the APDU request/response to/from a PCSC card reader.
104
105
To get @simtrace2-remsim@:
106
* Install required packages to compile the software:
107
<pre>
108
sudo apt-get install libusb-1.0-0-dev libosmocore-dev libpcsclite-dev
109
</pre>
110
* Get and compile the software:
111
<pre>
112
git clone git://git.osmocom.org/simtrace2.git
113
cd simtrace2/host/
114
make
115
</pre>
116
117
To use @simtrace2-remsim@:
118
# power off phone
119
# insert card adapter cable into phone
120
# insert card adapter cable SIMtrace v2 board
121
# plug SIMtrace v2 board in host computer USB port
122 3 tsaitgaist
# connect external card reader to host (any USB CCID reader should do the job)
123 1 tsaitgaist
# ensure a card is present in the reader slot (not in the SIMtrace port)
124
# check if the card is detected by the reader (use CTRL-C to exit)
125
<pre>
126
pcsc_scan 
127
128
Using reader plug'n play mechanism
129
Scanning present readers...
130
0: OMNIKEY 6321 CLi USB (OKCM0030506091345044320140749730) 00 00
131
 
132
Tue Sep 10 16:03:49 2019
133
 Reader 0: OMNIKEY 6321 CLi USB (OKCM0030506091345044320140749730) 00 00
134
  Event number: 0
135
  Card state: Card inserted, 
136
  ATR: 3B 9F 94 80 1F C7 80 31 E0 73 FE 21 1B 67 01 00 00 04 4D 02 01 99
137
</pre>
138
# get SIMtrace USB path (this step will soon be not required anymore)
139
<pre>
140
dfu-util -l
141
142
...
143
Found Runtime: [1d50:60e3] ver=0002, devnum=59, cfg=1, intf=1, path="1-2.2", alt=0, name="UNKNOWN", serial="UNKNOWN"
144
</pre>
145
# start @simtrace2-remsim@ with corresponding USB path (here 1-2.2)
146
<pre>
147
./simtrace2-remsim --usb-vendor 1d50 --usb-product 60e3 --usb-path 1-2.2 --usb-config 1
148
149
(C) 2010-2017, Harald Welte <laforge@gnumonks.org>
150
(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
151
152
SCardEstablishContext: OK
153
154
SCardListReaders: OK
155
156
SCardConnect: OK
157
158
<- 01 05 00 00 00 00 09 00 01 
159
<- 02 02 00 00 00 00 09 00 01 
160
<= cardem_request_set_atr(3b 00 )
161
<- 01 02 00 00 00 00 0b 00 02 3b 00 
162
<- 02 01 00 00 00 00 0b 00 02 2c 01 
163
Entering main loop
164
</pre>
165
# now you can power on the phone (only after @simtrace2-remsim@ is started since @simtrace2-remsim@ can't tell the phone a card has been inserted). you should also see some APDU traffic
166
<pre>
167
URB: 01 06 00 00 00 00 13 00 01 00 00 00 05 00 a0 a4 00 00 02 
168
-> 01 06 00 00 00 00 13 00 01 00 00 00 05 00 a0 a4 00 00 02 
169
=> DATA: flags=1, a0 a4 00 00 02 : CLA=a0 INS=a4 P1=00 P2=00 P3=02; case=4, lc=2(0), le=0(0)
170
<= cardem_request_pb_and_rx(a4, 2)
171
<- 01 01 00 00 00 00 0f 00 08 00 00 00 01 00 a4 
172
URB: 01 06 00 00 00 00 10 00 02 00 00 00 02 00 7f 20 
173
-> 01 06 00 00 00 00 10 00 02 00 00 00 02 00 7f 20 
174
=> DATA: flags=2, 7f 20 : CLA=a0 INS=a4 P1=00 P2=00 P3=02; case=4, lc=2(2), le=0(0)
175
TX: a0 a4 00 00 02 7f 20 
176
SCardEndTransaction: OK
177
178
RX: 9f 17 
179
SW=0x9f17, len_rx=0
180
<= cardem_request_sw_tx(9f 17)
181
<- 01 01 00 00 00 00 10 00 06 00 00 00 02 00 9f 17 
182
URB: 01 06 00 00 00 00 13 00 01 00 00 00 05 00 a0 f2 00 00 17 
183
-> 01 06 00 00 00 00 13 00 01 00 00 00 05 00 a0 f2 00 00 17 
184
=> DATA: flags=1, a0 f2 00 00 17 : CLA=a0 INS=f2 P1=00 P2=00 P3=17; case=2, lc=0(0), le=23(0)
185
TX: a0 f2 00 00 17 
186
SCardEndTransaction: OK
187
</pre>
188 5 tsaitgaist
189
h3. osmo-remsim
190
191
"osmo-remsim":/projects/osmo-remsim/wiki is a separate project allowing to have the card/SIM at a different location than the modem/phone. It also allows to manage multiple cards and emulators. The setup is a bit more complicated though.
192
193
# add the "osmo-remsim":/projects/cellular-infrastructure/wiki/Binary_Packages repository on each host you want to operator parts of @osmo-remsim@ (so you don't have to compile osmo-remsim yourself)
194
# run the server. This is the central instance telling the bankd which reader to use, and the client which bankd to contact.
195
** install @osmo-remsim-server@:
196
<pre>
197
sudo apt install osmo-remsim-server
198
</pre>
199 15 tsaitgaist
** run server
200 5 tsaitgaist
<pre>
201
osmo-remsim-server
202
</pre>
203
# the server needs to be additionally configured through its RESTful interface. For that we will use the small tool @remsim-apitool.py@
204
** download @remsim-apitool.py@
205
<pre>
206 6 tsaitgaist
wget https://git.osmocom.org/osmo-remsim/plain/contrib/remsim-apitool.py
207
</pre>
208
** tell the server client 1 with slot 1 (on the modem side) should use bank 1 slot 1 (on the reader side). This must be done every time after to server is started.
209 5 tsaitgaist
<pre>
210
python remsim-apitool.py --create-slotmap 1 1 1 1
211
</pre>
212 15 tsaitgaist
# @osmo-remsim@ uses PCSC to access card readers (this setup only needs to be done once)
213
** connect external card readers to host (any USB CCID reader should do the job)
214
** ensure cards are present in the card readers
215
** get reader name (use CTRL-C to exit)
216 1 tsaitgaist
<pre>
217 15 tsaitgaist
pcsc_scan 
218
219
Using reader plug'n play mechanism
220
Scanning present readers...
221
0: OMNIKEY 6321 CLi USB (OKCM0030506091345044320140749730) 00 00
222 7 tsaitgaist
</pre>
223 15 tsaitgaist
** create a @bankd_pcsc_slots.csv@ file listing the card readers @osmo-remsim@ should use. The CSV format is: user provided bank number (collection of readers/slots), user provided slot number (individual card in reader/bank), PCSC reader name.
224 7 tsaitgaist
<pre>
225 15 tsaitgaist
echo << EOF > bankd_pcsc_slots.csv
226
"1","1","OMNIKEY 6321 CLi USB (OKCM0030506091345044320140749730) 00 00"
227
EOF
228 7 tsaitgaist
</pre>
229 15 tsaitgaist
# run the bankd (*the @bankd_pcsc_slots.csv@ file must be in the current working directory*). This will contact the server (which can be on another host) to know which card reader it will manage.
230 6 tsaitgaist
** install @osmo-remsim-bankd@:
231 5 tsaitgaist
<pre>
232 6 tsaitgaist
sudo apt install osmo-remsim-bankd
233
</pre>
234 13 tsaitgaist
** here we tell it will take care of the card reader from bank 1 (no need to specify the number of slots available in the reader using the -n argument if it is less or equal than 8)
235 6 tsaitgaist
<pre>
236 5 tsaitgaist
osmo-remsim-bankd --server-host localhost --server-port 9998 --bank-id 1
237 1 tsaitgaist
</pre>
238
# now we need to actually emulate the card
239
** power off phone
240
** insert card adapter cable into phone
241 5 tsaitgaist
** insert card adapter cable SIMtrace v2 board
242
** plug SIMtrace v2 board in host computer USB port
243 6 tsaitgaist
** install @osmo-remsim-client@:
244
<pre>
245
sudo apt install osmo-remsim-client
246
</pre>
247 5 tsaitgaist
** get SIMtrace USB path (this step will soon be not required anymore)
248
<pre>
249
dfu-util -l
250
251
...
252
Found Runtime: [1d50:60e3] ver=0002, devnum=59, cfg=1, intf=1, path="1-2.2", alt=0, name="UNKNOWN", serial="UNKNOWN"
253
</pre>
254
** start the @osmo-remsim-client-st2@ client with corresponding USB path (here 1-2.2). This will contact the server (which can be on another host) to know which bankd to contact. Here we tell it will take care of slot 1 of modem 1 (SIMtrace can only emulate one card).
255
<pre>
256
osmo-remsim-client-st2 --usb-vendor 1d50 --usb-product 60e3 --usb-path 1-2.2 --usb-config 1 --client-id 1 --client-slot 1 --server-host localhost --server-port 9998
257
</pre>
258
** you can now power on the phone, and should see some APDU traffic on the client and bankd.
259 1 tsaitgaist
260
h2. Limitations
261
262
Here are the known limitations:
263
* there is no way for SIMtrace to tell the reader that a new card has been inserted. There is no specified way to do it (e.g. in ISO 7816 standard). This is generally done inside the reader hardware by a mechanical switch. The only way around is to restarted the reader (e.g. phone).
264
* the cardem is currently a separate firmware. it is planned to combine it with the trace firmware (the software will then select the right functionality)
265
* the firmware ignores the sent ATR (sent by the software, from the card to forward). this is to prevent the reader from switching to a yet untested baud rate
266
* the error messages returned by @simtrace2-remsim@ are not very useful
267
* @simtrace2-remsim@ does not automatically reconnect to the SIMtrace board when the hardware is reset
268
* you have to specify the USB path to @simtrace2-remsim@
269
* no long term tests have been performed (this is already planned)
270 2 tsaitgaist
* you can't use the card reader built in SIMtrace
271 4 tsaitgaist
* @simtrace2-remsim@ does not send the APDU to GSMTAP so you can trace the traffic using wireshark
272 2 tsaitgaist
* @simtrace2-remsim-udp@ does not connect to SIMtrace v2 boards
273 1 tsaitgaist
274
We are currently working on resolving these issues.
275
If you found yet unknown issues, you can report them to the main developer at kredon AT sysmocom DOT de.
276
If possible, please also attach the corresponding debug serial output. To get the serial output, connect a USB to UART cable either to the 2.5 mm stereo headphone connector (tip = TX, ring = RX, sleeve = GND) or the nearby DEBUG port (pin 1 = GND, pin 4 = TX, pin 5 = RX). Open the serial port with the following configuration: 921600 8N1.
Add picture from clipboard (Maximum size: 48.8 MB)