Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 3 | # Copyright (C) 2018 Vadim Yanitskiy <axilirator@gmail.com> |
| 4 | # |
| 5 | # This program is free software: you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License as published by |
| 7 | # the Free Software Foundation, either version 2 of the License, or |
| 8 | # (at your option) any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | # |
| 18 | |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 19 | import select |
| 20 | import struct |
| 21 | import socket |
| 22 | import os |
| 23 | |
| 24 | from pySim.transport import LinkBase |
| 25 | from pySim.exceptions import * |
| 26 | from pySim.utils import h2b, b2h |
| 27 | |
| 28 | class L1CTLMessage(object): |
| 29 | |
| 30 | # Every (encoded) L1CTL message has the following structure: |
| 31 | # - msg_length (2 bytes, net order) |
| 32 | # - l1ctl_hdr (packed structure) |
| 33 | # - msg_type |
| 34 | # - flags |
| 35 | # - padding (2 spare bytes) |
| 36 | # - ... payload ... |
| 37 | |
| 38 | def __init__(self, msg_type, flags = 0x00): |
| 39 | # Init L1CTL message header |
| 40 | self.data = struct.pack("BBxx", msg_type, flags) |
| 41 | |
| 42 | def gen_msg(self): |
| 43 | return struct.pack("!H", len(self.data)) + self.data |
| 44 | |
| 45 | class L1CTLMessageReset(L1CTLMessage): |
| 46 | |
| 47 | # L1CTL message types |
| 48 | L1CTL_RESET_REQ = 0x0d |
| 49 | L1CTL_RESET_IND = 0x07 |
| 50 | L1CTL_RESET_CONF = 0x0e |
| 51 | |
| 52 | # Reset types |
| 53 | L1CTL_RES_T_BOOT = 0x00 |
| 54 | L1CTL_RES_T_FULL = 0x01 |
| 55 | L1CTL_RES_T_SCHED = 0x02 |
| 56 | |
| 57 | def __init__(self, type = L1CTL_RES_T_FULL): |
| 58 | super(L1CTLMessageReset, self).__init__(self.L1CTL_RESET_REQ) |
| 59 | self.data += struct.pack("Bxxx", type) |
| 60 | |
| 61 | class L1CTLMessageSIM(L1CTLMessage): |
| 62 | |
| 63 | # SIM related message types |
| 64 | L1CTL_SIM_REQ = 0x16 |
| 65 | L1CTL_SIM_CONF = 0x17 |
| 66 | |
| 67 | def __init__(self, pdu): |
| 68 | super(L1CTLMessageSIM, self).__init__(self.L1CTL_SIM_REQ) |
| 69 | self.data += pdu |
| 70 | |
| 71 | class CalypsoSimLink(LinkBase): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 72 | """Transport Link for Calypso based phones.""" |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 73 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 74 | def __init__(self, sock_path:str = "/tmp/osmocom_l2"): |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 75 | # Make sure that a given socket path exists |
| 76 | if not os.path.exists(sock_path): |
| 77 | raise ReaderError("There is no such ('%s') UNIX socket" % sock_path) |
| 78 | |
| 79 | print("Connecting to osmocon at '%s'..." % sock_path) |
| 80 | |
| 81 | # Establish a client connection |
| 82 | self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 83 | self.sock.connect(sock_path) |
| 84 | |
| 85 | def __del__(self): |
| 86 | self.sock.close() |
| 87 | |
| 88 | def wait_for_rsp(self, exp_len = 128): |
| 89 | # Wait for incoming data (timeout is 3 seconds) |
| 90 | s, _, _ = select.select([self.sock], [], [], 3.0) |
| 91 | if not s: |
| 92 | raise ReaderError("Timeout waiting for card response") |
| 93 | |
| 94 | # Receive expected amount of bytes from osmocon |
| 95 | rsp = self.sock.recv(exp_len) |
| 96 | return rsp |
| 97 | |
| 98 | def reset_card(self): |
| 99 | # Request FULL reset |
| 100 | req_msg = L1CTLMessageReset() |
| 101 | self.sock.send(req_msg.gen_msg()) |
| 102 | |
| 103 | # Wait for confirmation |
| 104 | rsp = self.wait_for_rsp() |
| 105 | rsp_msg = struct.unpack_from("!HB", rsp) |
| 106 | if rsp_msg[1] != L1CTLMessageReset.L1CTL_RESET_CONF: |
| 107 | raise ReaderError("Failed to reset Calypso PHY") |
| 108 | |
| 109 | def connect(self): |
| 110 | self.reset_card() |
| 111 | |
| 112 | def disconnect(self): |
| 113 | pass # Nothing to do really ... |
| 114 | |
| 115 | def wait_for_card(self, timeout = None, newcardonly = False): |
| 116 | pass # Nothing to do really ... |
| 117 | |
Harald Welte | c34f940 | 2021-04-10 10:55:24 +0200 | [diff] [blame^] | 118 | def _send_apdu_raw(self, pdu): |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 119 | |
| 120 | # Request FULL reset |
| 121 | req_msg = L1CTLMessageSIM(h2b(pdu)) |
| 122 | self.sock.send(req_msg.gen_msg()) |
| 123 | |
| 124 | # Read message length first |
| 125 | rsp = self.wait_for_rsp(struct.calcsize("!H")) |
| 126 | msg_len = struct.unpack_from("!H", rsp)[0] |
| 127 | if msg_len < struct.calcsize("BBxx"): |
| 128 | raise ReaderError("Missing L1CTL header for L1CTL_SIM_CONF") |
| 129 | |
| 130 | # Read the whole message then |
| 131 | rsp = self.sock.recv(msg_len) |
| 132 | |
| 133 | # Verify L1CTL header |
| 134 | hdr = struct.unpack_from("BBxx", rsp) |
| 135 | if hdr[0] != L1CTLMessageSIM.L1CTL_SIM_CONF: |
| 136 | raise ReaderError("Unexpected L1CTL message received") |
| 137 | |
| 138 | # Verify the payload length |
| 139 | offset = struct.calcsize("BBxx") |
| 140 | if len(rsp) <= offset: |
| 141 | raise ProtocolError("Empty response from SIM?!?") |
| 142 | |
| 143 | # Omit L1CTL header |
| 144 | rsp = rsp[offset:] |
| 145 | |
| 146 | # Unpack data and SW |
| 147 | data = rsp[:-2] |
| 148 | sw = rsp[-2:] |
| 149 | |
| 150 | return b2h(data), b2h(sw) |