Commit Graph

510 Commits

Author SHA1 Message Date
Harald Welte
4c473dad30 card_emu: Fail with assert in case next_tpdu_state from wrong state 2015-11-14 13:32:05 +01:00
Harald Welte
612d65ad62 card_emu: Make update_fidi() void 2015-11-14 13:30:43 +01:00
Harald Welte
16cf408a49 card_emu.c: More comments about data structures 2015-11-11 19:02:48 +01:00
Harald Welte
9d3e38242c initial commit of more code towards card emulation
I couldn't help but to spend my sunday on working towards card
emulation, including
* various state machines in the target about ISO7816 states
* tc_etu timer import from simtrace1
* req_ctx import from simtrace1 (needs renaming and simplifiation)
* USB protocol description as cardemu_prot.h
* some host-based testing code to test the state machines

The code seems to work fine throughout card reset, sending ATR and
receiving the TPDU header of the first APDU, up to the point where it
marks the TPDU header as to-be-transmitted over th bulk-in endpoint.

Sending the ATR must be done inside the firmware for timing
requirements.

From that point onwards, the host needs to respond at the very least
with a procedure byte, and some indication whether or not the card
emulator should continue to transmit data (card->reader), or receive
data (reader->card).

The code is intentionally not hooked up yet with the USB logic nor with
the UART.  I want host-based testing completed before doing that.
2015-11-09 00:50:54 +01:00
Harald Welte
f64f68871e move ARRAY_SIZE() definition to utils.h 2015-11-08 21:31:48 +01:00
Harald Welte
30a53f823a Move Fi/Di calculation functions to separate C file 2015-11-08 14:29:55 +01:00
Harald Welte
6d44c1fdd3 USB: Add manufacturer name string descriptor
... and use indexed array initializers for more safety/clarity
2015-11-07 19:01:30 +01:00
Harald Welte
8a5b580a72 CCID usb descriptor: We support 3V only, not 5V. 2015-11-07 18:53:43 +01:00
Harald Welte
ec4fe2358b Mark more local functions as static 2015-11-07 18:48:26 +01:00
Harald Welte
844db577f2 change to own USB vendor/device ID
we shouldn't re-use the vendor/device ID usd by simtrace1, as the
protocol is incompatible and applications for simtrace 1 don't work with
simtrace2.   Also, there's a different processor architecutre in the
hardware.
2015-11-07 18:38:04 +01:00
Harald Welte
beb729391b USB descriptors: call it SIMtrace 2 and add spaces to strings 2015-11-07 18:35:41 +01:00
Harald Welte
8d6a5d8f89 move USBDDriverCallbacks_ConfigurationChanged() to main.c
This alows us to mark simtrace_config as static variable and keep all
code related to this variable local to main.c
2015-11-07 18:27:05 +01:00
Harald Welte
d4c1421c91 conf_func: Mark const and static.
const saves RAM, and static avoids namespace pollution.
2015-11-07 18:25:46 +01:00
Harald Welte
fefd571701 conf_func: Use named struct initializers and named array subscripts
As part of this, we also do away with the '-1' based array subscripts
2015-11-07 18:25:19 +01:00
Harald Welte
15d72cc631 add some comments on the conf_func members 2015-11-07 18:19:06 +01:00
Christina Quast
89690c98dd Added README file 2015-09-06 11:24:41 +02:00
Christina Quast
235409aa2e simtrace.h: Increased host to SIMtrace buf size
The host got stuck sometimes when large data frames had to be send
from the host program to SIMtrace. The printouts would just stop
if many large packets were received from the SIM card and needed
to be transferred to the mobile phone.
Increasing the buffer length removed the problem.
2015-06-24 16:05:39 +02:00
Christina Quast
3d8c093cb2 main: Increase timeout for USB configured state
When the timeout was too small, the main function would infinitely
loop around, restarting the board and waiting for the USB interface
to get configured. But since configuration seems to take more than
one second, it rarely succeeded.
Increasing the timeout makes the USB configuration finish in the
first try.
2015-06-24 16:02:52 +02:00
Christina Quast
ad6f4d3fd2 replace.py: Moved replace func,mitm: addr book replace
Moved the replace function from mitm.py to replace.py.

This implementation is context insensitive for now. It would be
better, to have a mitm class or to pass state information to the
function. Because how else can the MITM code know, whether it gets
passed data to or from the sim card, to or from the phone?
2015-06-24 15:48:41 +02:00
Christina Quast
2cafadb2a1 ccid_select.py: added comment;this file is test code 2015-06-24 15:42:55 +02:00
Christina Quast
19ed529dee apdu_split.py: INS array updated,CLA sync disabled!
There are more instruction codes, after which data is expected
from the SIM card. Therefore, the array with commands known to
expect SIM card data has been extended. Feel free to extend it
even further.

!! ATTENTION !!
The only synchronization mechanism for parsing APDUs
(naively looking for a 0xA0 byte) is deactivated! It only worked
well for the sniffing mode, but getting out of sync is fatal for
the MITM mode.
!! A NEW MEANS OF SYNCHRONISATION HAS TO BE FOUND !!
2015-06-24 15:23:59 +02:00
Christina Quast
8271bd0145 simtrace.py: Find device without config change
The find_device() function was only called when the USB config was
altered. Now, it is called on every call of simtrace.py.
2015-06-19 13:49:48 +02:00
Christina Quast
ca0ebfdbd6 mitm.py: Bugfix: Always check for APDU_S_SEND_DATA
When the command a0 c0 00 00 16 was send, and the the bytes
a0 c0 00 00 where read first, and then only the byte 16 was read
from simtrace, the code never entered the if condition if cmd is not
None, and therefore never executed send_receive_cmd.

Bug fix: Check for state APDU_S_SEND_DATA after apdu_split (parsing)
the ACK-instruction byte, in case it was an instruction which requires
an answer from the SIM card.
2015-06-19 13:29:21 +02:00
Christina Quast
a929bc8850 ccid_raw.py: SendTransmit: Pass list instead of array
SCardTransmit expects the last function parameter cmd (the bytes
to be send) to be of type list, but we pass a binary array to
send_receive_cmd.
Therefore, the cmd array has to be converted using its function
tolist().
2015-06-19 13:24:38 +02:00
Christina Quast
ebd0fa9d43 gsmtap.py: Send ATR bytes as test command 2015-05-29 16:42:51 +02:00
Christina Quast
3742ef1cde simtrace.py: Comment to remove test func select_file 2015-05-18 18:31:51 +02:00
Christina Quast
4930ea6032 ccid.py: Removed read_bin command
The code was used as early debug code to read different files from
the SIM card and therefore acquire the IMSI, and other SIM card
specific information.

This only was useful for testing that the firmware worked properly.
Is is not needed for regular use cases.
2015-05-18 17:25:36 +02:00
Christina Quast
e0832d6a1f serial_pysim.py: Cleanup: early test code for serial pysim communication
The code changes the config to config number 2 and tries to connect
to the serial CCID reader. This only was useful in the early stage
of the project.
2015-05-18 17:19:45 +02:00
Christina Quast
ba486b61ad simtrace.py: remove unused cmd line option
The commands "cmd1", "cmd2", "cmd_poweron", "cmd_poweroff",
"cmd_get_slot_stat", "cmd_get_param" where early test commands,
but have not been used as such in moths.

A programmer, who wants to send commands to the smartcard, should
use the functions of ccid_raw.py (e.g. send_receive_cmd) instead.
2015-05-18 17:16:31 +02:00
Christina Quast
66224b7e1b Removed unused phone.py
The SIM card emulator re-uses the mitm.py code with an implementation
of SIM card requests and answers instead of phone.py.
2015-05-14 18:13:08 +02:00
Christina Quast
427ae3547a sniffer.py: Removed unused find_dev function 2015-05-14 18:12:05 +02:00
Christina Quast
020e5d6a5f mitm.py: Pass device to do_mitm function 2015-05-14 18:11:23 +02:00
Christina Quast
f25a760f6e simtrace.py: Cleaned up unused functions 2015-05-14 18:09:47 +02:00
Christina Quast
bd5b8bd7f7 usb.c: Corrected grammatical mistake 2015-05-14 17:25:41 +02:00
Christina Quast
5cf676df61 sniffer.py: Removed redundant find_dev function 2015-05-14 17:21:34 +02:00
Christina Quast
425717d08b mitm,sniffer.py: Send APDUs to wireshark 2015-05-14 17:20:55 +02:00
Christina Quast
152e878bfe gsmtap.py: Ported from scapy to socket
With the python module scapy the headers of each layer have to be created
by hand. Furthermore, in order to use it, the program would have to be
started as root.

Using sockets would be the better. The reason for using scapy was, that
it was the first best thing that I found when searching for python
socket communication.
The next step would be to open and close the socket only once instead
of every time an APDU is send to wireshark.
Furthermore, the ATR probably has to be treated differently from APDU packets.
2015-05-14 17:19:28 +02:00
Christina Quast
59dae92398 apdu_split.py: Fixed off by one err in parsing
Parsing failed like in this dump:
('PTS: ', [255, 0])
('PTS: ', [255, 0, 255])
('APDU:', 'c0', 'a0 c0 00 00 16 c0 00 00 00 00 7f 20 02 00 00 00 00 00 09 91 00 17 04 00 00 00 83 8a 90')
ACK
('APDU:', 'a4', '00 a0 a4 00 00 02 7f 20 9f 16')
('APDU:', 'c0', 'a0 c0 00 00 16 c0 00 00 00 00 7f 20 02 00 00 00 00 00 09 91 00 17 04 00 83 8a 83 8a 90')

a0 c0 00 00 16 c0 00 00 00 00 7f 20 02 00 00 00 00 00 09 91 00 17 04 00 00 00 83 8a 90
00 a0 a4 00 00 02 7f 20 9f 16
a0 c0 00 00 16 c0 00 00 00 00 7f 20 02 00 00 00 00 00 09 91 00 17 04 00 83 8a 83 8a 90

So when data was sent, the next packet would always begin with the SW2 byte
(e.g. 00 a0 ..) instead of the instruction byte a0.
The problem was a wrong state change (to APDU_S_DATA instead of APDU_S_SW1)
2015-05-14 16:58:41 +02:00
Christina Quast
20e89af1bf apdu_split.py: Changed buf data type list to array
The data type of incoming and outgoing data should be the same
at all points of the program to make it consistent.
For this program the data type is array.array.
2015-05-14 16:55:38 +02:00
Christina Quast
2a9c063d6b gsmtap.py: gsmtap_send_apdu and test main function
ATRs should probably be treated differently?
Also, is there a performance penalty when using scapy instead of holding a connection open?
2015-05-12 19:31:04 +02:00
Christina Quast
b894691841 gsmtap.py: send example msg using scapy
The GSM message appears in wireshark when sniffing on localhost
2015-05-12 16:03:23 +02:00
Christina Quast
96025dbb69 phone.c: removed comments
Removed comment describing the typical first few packets between the
SIM card and phone I used for development and testing:
SuperSIM, Motorola C123
2015-05-08 17:31:14 +02:00
Christina Quast
049772e688 phone.c: Removed comment about wait time extension
Wait time extension commands are not implemented yet.
They are a nice-to-have for the future, since they would enable
the board to work with phones that expect a higher frequency.

With a wait time extension request towards the phone, SIMtrace could
signal the phone to wait for a longer time period while SIMtrace is
still waiting for a response from the SIM card.
2015-05-08 17:19:16 +02:00
Christina Quast
3b544a1217 cciddriverdesc: Atmel bug fix of slot status register
Atmel library mixes up the value for two different messages.
Explanation can be found here:
http://permalink.gmane.org/gmane.comp.mobile.osmocom.simtrace/29

Actually, a better fix for this problem would be to go through the
Atmel code and replace ICC_INSERTED_EVENT with ICC_BS_PRESENT_NOTACTIVATED
where appropriate and in accordance with the Smart Card CCID standard
(and libccid, for this matter).
2015-05-08 16:59:47 +02:00
Christina Quast
3735039b59 firmware, host: Changed vendor and product id
The code used a vendor and product id taken from Atmel example code.
Now it is changed to the vendor and product id, which was also previously
used in the original SIMtrace code.
2015-05-08 16:47:29 +02:00
Christina Quast
14f6399c03 tc_etu.c: Removed, because was never used
Since the host side does the parsing of the packets, (not the firmware,
as it was in the old simtrace version), we do not need to check for expired
max waiting time.
Instead, every byte received from the phone is filled into a ring buffer.
As soon as the USB endpoint to the host is not busy anymore, it is sent
to the host over the respective USB endpoint.
2015-05-08 16:45:28 +02:00
Christina Quast
5f83861b17 mitm.c: Removed unused commented out code 2015-05-08 16:45:28 +02:00
Christina Quast
560ade3239 main.c: Removed TC_Start/_Stop (was commented out)
Removed FIXME comment
Removed TC_Start, TC_Stop function call, which was commented out anyways.
2015-05-08 16:45:28 +02:00
Christina Quast
1b65ad0329 sniffer.c: Removed unused PR macro defnition 2015-05-08 16:45:28 +02:00
Christina Quast
0bf65dc0b6 usb.c: Removed FIXME comment
MITM does not support two interface settings.
The comment mixed up interface configurations and alternative
interface settings.
2015-05-08 16:45:28 +02:00