Introduce APDU/TPDU trace decoder
This introduces a new pySim.apdu module hierarchy, which contains
classes that represent TPDU/APDUs as exchanged between
SIM/UICC/USIM/ISIM card and UE.
It contains instruction level decoders for SELECT, READ BINARY and
friends, and then uses the pySim.filesystem.Runtime{Lchan,State} classes
to keep track of the currently selected EF/DF/ADF for each logical
channel, and uses the file-specific decoder classes of pySim to decode
the actual file content that is being read or written.
This provides a much more meaningful decode of protocol traces than
wireshark will ever be able to give us.
Furthermore, there's the new pySim.apdu_source set of classes which
provides "input plugins" for obtaining APDU traces in a variety of
formats. So far, GSMTAP UDP live capture and pyshark based RSPRO
live and pcap file reading are imlpemented.
Change-Id: I862d93163d495a294364168f7818641e47b18c0a
Closes: OS#5126
diff --git a/tests/test_apdu.py b/tests/test_apdu.py
new file mode 100755
index 0000000..1fb87b7
--- /dev/null
+++ b/tests/test_apdu.py
@@ -0,0 +1,91 @@
+#!/usr/bin/env python3
+
+import unittest
+from pySim.utils import h2b, b2h
+from pySim.construct import filter_dict
+from pySim.apdu import Apdu
+from pySim.apdu.ts_31_102 import UsimAuthenticateEven
+
+class TestApdu(unittest.TestCase):
+ def test_successful(self):
+ apdu = Apdu('00a40400023f00', '9000')
+ self.assertEqual(apdu.successful, True)
+ apdu = Apdu('00a40400023f00', '6733')
+ self.assertEqual(apdu.successful, False)
+
+ def test_successful_method(self):
+ """Test overloading of the success property with a custom method."""
+ class SwApdu(Apdu):
+ def _is_success(self):
+ return False
+ apdu = SwApdu('00a40400023f00', '9000')
+ self.assertEqual(apdu.successful, False)
+
+# TODO: Tests for TS 102 221 / 31.102 ApduCommands
+
+class TestUsimAuth(unittest.TestCase):
+ """Test decoding of the rather complex USIM AUTHENTICATE command."""
+ def test_2g(self):
+ apdu = ('80880080' + '09' + '080001020304050607',
+ '04a0a1a2a308b0b1b2b3b4b5b6b79000')
+ res = {
+ 'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'gsm'},
+ 'body': {'rand': '0001020304050607', 'autn': None}},
+ 'rsp': {'body': {'sres': 'a0a1a2a3', 'kc': 'b0b1b2b3b4b5b6b7'}}
+ }
+ u = UsimAuthenticateEven(apdu[0], apdu[1])
+ d = filter_dict(u.to_dict())
+ self.assertEqual(d, res)
+
+ def test_3g(self):
+ apdu = ('80880081' + '12' + '080001020304050607081011121314151617',
+ 'DB' + '08' + 'a0a1a2a3a4a5a6a7' +
+ '10' + 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' +
+ '10' + 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf' + '9000')
+ res = {
+ 'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'umts'},
+ 'body': {'rand': '0001020304050607', 'autn': '1011121314151617'}},
+ 'rsp': {'body': {'tag': 219,
+ 'body': {
+ 'res': 'a0a1a2a3a4a5a6a7',
+ 'ck': 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf',
+ 'ik': 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf',
+ 'kc': None
+ }
+ }
+ }
+ }
+ u = UsimAuthenticateEven(apdu[0], apdu[1])
+ d = filter_dict(u.to_dict())
+ self.assertEqual(d, res)
+
+ def test_3g_sync(self):
+ apdu = ('80880081' + '12' + '080001020304050607081011121314151617',
+ 'DC' + '08' + 'a0a1a2a3a4a5a6a7' + '9000')
+ res = {
+ 'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'umts'},
+ 'body': {'rand': '0001020304050607', 'autn': '1011121314151617'}},
+ 'rsp': {'body': {'tag': 220, 'body': {'auts': 'a0a1a2a3a4a5a6a7' }}}
+ }
+ u = UsimAuthenticateEven(apdu[0], apdu[1])
+ d = filter_dict(u.to_dict())
+ self.assertEqual(d, res)
+
+ def test_vgcs(self):
+ apdu = ('80880082' + '0E' + '04' + '00010203' +
+ '01' + '10' +
+ '08' + '2021222324252627',
+ 'DB' + '10' + 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' + '9000')
+ res = {
+ 'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'vgcs_vbs'},
+ 'body': { 'vk_id': '10', 'vservice_id': '00010203', 'vstk_rand': '2021222324252627'}},
+ 'rsp': {'body': {'vstk': 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf'}}
+ }
+ u = UsimAuthenticateEven(apdu[0], apdu[1])
+ d = filter_dict(u.to_dict())
+ self.assertEqual(d, res)
+
+
+
+if __name__ == "__main__":
+ unittest.main()