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 | |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 24 | from typing import Optional |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 25 | |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 26 | from pySim.transport import LinkBase |
| 27 | from pySim.exceptions import * |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 28 | from pySim.utils import h2b, b2h, Hexstr, ResTuple |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 29 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 30 | |
Vadim Yanitskiy | 04b5d9d | 2022-07-07 03:05:30 +0700 | [diff] [blame] | 31 | class L1CTLMessage: |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 32 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 33 | # Every (encoded) L1CTL message has the following structure: |
| 34 | # - msg_length (2 bytes, net order) |
| 35 | # - l1ctl_hdr (packed structure) |
| 36 | # - msg_type |
| 37 | # - flags |
| 38 | # - padding (2 spare bytes) |
| 39 | # - ... payload ... |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 40 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 41 | def __init__(self, msg_type, flags=0x00): |
| 42 | # Init L1CTL message header |
| 43 | self.data = struct.pack("BBxx", msg_type, flags) |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 44 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 45 | def gen_msg(self): |
| 46 | return struct.pack("!H", len(self.data)) + self.data |
| 47 | |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 48 | |
| 49 | class L1CTLMessageReset(L1CTLMessage): |
| 50 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 51 | # L1CTL message types |
| 52 | L1CTL_RESET_REQ = 0x0d |
| 53 | L1CTL_RESET_IND = 0x07 |
| 54 | L1CTL_RESET_CONF = 0x0e |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 55 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 56 | # Reset types |
| 57 | L1CTL_RES_T_BOOT = 0x00 |
| 58 | L1CTL_RES_T_FULL = 0x01 |
| 59 | L1CTL_RES_T_SCHED = 0x02 |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 60 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 61 | def __init__(self, type=L1CTL_RES_T_FULL): |
| 62 | super(L1CTLMessageReset, self).__init__(self.L1CTL_RESET_REQ) |
| 63 | self.data += struct.pack("Bxxx", type) |
| 64 | |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 65 | |
| 66 | class L1CTLMessageSIM(L1CTLMessage): |
| 67 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 68 | # SIM related message types |
| 69 | L1CTL_SIM_REQ = 0x16 |
| 70 | L1CTL_SIM_CONF = 0x17 |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 71 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 72 | def __init__(self, pdu): |
| 73 | super(L1CTLMessageSIM, self).__init__(self.L1CTL_SIM_REQ) |
| 74 | self.data += pdu |
| 75 | |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 76 | |
| 77 | class CalypsoSimLink(LinkBase): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 78 | """Transport Link for Calypso based phones.""" |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 79 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 80 | def __init__(self, sock_path: str = "/tmp/osmocom_l2", **kwargs): |
| 81 | super().__init__(**kwargs) |
| 82 | # Make sure that a given socket path exists |
| 83 | if not os.path.exists(sock_path): |
| 84 | raise ReaderError( |
| 85 | "There is no such ('%s') UNIX socket" % sock_path) |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 86 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 87 | print("Connecting to osmocon at '%s'..." % sock_path) |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 88 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 89 | # Establish a client connection |
| 90 | self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 91 | self.sock.connect(sock_path) |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 92 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 93 | def __del__(self): |
| 94 | self.sock.close() |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 95 | |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 96 | def wait_for_rsp(self, exp_len: int = 128): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 97 | # Wait for incoming data (timeout is 3 seconds) |
| 98 | s, _, _ = select.select([self.sock], [], [], 3.0) |
| 99 | if not s: |
| 100 | raise ReaderError("Timeout waiting for card response") |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 101 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 102 | # Receive expected amount of bytes from osmocon |
| 103 | rsp = self.sock.recv(exp_len) |
| 104 | return rsp |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 105 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 106 | def reset_card(self): |
| 107 | # Request FULL reset |
| 108 | req_msg = L1CTLMessageReset() |
| 109 | self.sock.send(req_msg.gen_msg()) |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 110 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 111 | # Wait for confirmation |
| 112 | rsp = self.wait_for_rsp() |
| 113 | rsp_msg = struct.unpack_from("!HB", rsp) |
| 114 | if rsp_msg[1] != L1CTLMessageReset.L1CTL_RESET_CONF: |
| 115 | raise ReaderError("Failed to reset Calypso PHY") |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 116 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 117 | def connect(self): |
| 118 | self.reset_card() |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 119 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 120 | def disconnect(self): |
| 121 | pass # Nothing to do really ... |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 122 | |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 123 | def wait_for_card(self, timeout: Optional[int] = None, newcardonly: bool = False): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 124 | pass # Nothing to do really ... |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 125 | |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 126 | def _send_apdu_raw(self, pdu: Hexstr) -> ResTuple: |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 127 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 128 | # Request FULL reset |
| 129 | req_msg = L1CTLMessageSIM(h2b(pdu)) |
| 130 | self.sock.send(req_msg.gen_msg()) |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 131 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 132 | # Read message length first |
| 133 | rsp = self.wait_for_rsp(struct.calcsize("!H")) |
| 134 | msg_len = struct.unpack_from("!H", rsp)[0] |
| 135 | if msg_len < struct.calcsize("BBxx"): |
| 136 | raise ReaderError("Missing L1CTL header for L1CTL_SIM_CONF") |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 137 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 138 | # Read the whole message then |
| 139 | rsp = self.sock.recv(msg_len) |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 140 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 141 | # Verify L1CTL header |
| 142 | hdr = struct.unpack_from("BBxx", rsp) |
| 143 | if hdr[0] != L1CTLMessageSIM.L1CTL_SIM_CONF: |
| 144 | raise ReaderError("Unexpected L1CTL message received") |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 145 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 146 | # Verify the payload length |
| 147 | offset = struct.calcsize("BBxx") |
| 148 | if len(rsp) <= offset: |
| 149 | raise ProtocolError("Empty response from SIM?!?") |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 150 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 151 | # Omit L1CTL header |
| 152 | rsp = rsp[offset:] |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 153 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 154 | # Unpack data and SW |
| 155 | data = rsp[:-2] |
| 156 | sw = rsp[-2:] |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 157 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 158 | return b2h(data), b2h(sw) |