Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file reader.c |
| 2 | * Card reader abstraction for libosmosim. */ |
Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 3 | /* |
| 4 | * (C) 2012 by Harald Welte <laforge@gnumonks.org> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 27 | #include <errno.h> |
| 28 | #include <stdint.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <stdio.h> |
| 32 | |
Harald Welte | a5c9255 | 2012-09-10 21:05:42 +0200 | [diff] [blame] | 33 | #include <netinet/in.h> |
| 34 | |
| 35 | #include <osmocom/core/msgb.h> |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 36 | #include <osmocom/sim/sim.h> |
| 37 | |
Harald Welte | 3c44a64 | 2020-03-15 22:50:06 +0100 | [diff] [blame] | 38 | #include "config.h" |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 39 | |
| 40 | #include "sim_int.h" |
| 41 | |
Harald Welte | a5c9255 | 2012-09-10 21:05:42 +0200 | [diff] [blame] | 42 | /* remove the SW from end of the message */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 43 | static int get_sw(struct msgb *resp) |
| 44 | { |
| 45 | int ret; |
| 46 | |
| 47 | if (!msgb_apdu_de(resp) || msgb_apdu_le(resp) < 2) |
| 48 | return -EIO; |
| 49 | |
Harald Welte | a5c9255 | 2012-09-10 21:05:42 +0200 | [diff] [blame] | 50 | ret = msgb_get_u16(resp); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 51 | |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | /* According to ISO7816-4 Annex A */ |
| 56 | static int transceive_apdu_t0(struct osim_card_hdl *st, struct msgb *amsg) |
| 57 | { |
| 58 | struct osim_reader_hdl *rh = st->reader; |
| 59 | struct msgb *tmsg = msgb_alloc(1024, "TPDU"); |
| 60 | struct osim_apdu_cmd_hdr *tpduh; |
| 61 | uint8_t *cur; |
| 62 | uint16_t sw; |
| 63 | int rc, num_resp = 0; |
| 64 | |
Jacob Erlbeck | d154f8b | 2015-04-09 14:22:21 +0200 | [diff] [blame] | 65 | if (!tmsg) |
| 66 | return -ENOMEM; |
| 67 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 68 | /* create TPDU header from APDU header */ |
| 69 | tpduh = (struct osim_apdu_cmd_hdr *) msgb_put(tmsg, sizeof(*tpduh)); |
| 70 | memcpy(tpduh, msgb_apdu_h(amsg), sizeof(*tpduh)); |
| 71 | |
| 72 | switch (msgb_apdu_case(amsg)) { |
| 73 | case APDU_CASE_1: |
| 74 | tpduh->p3 = 0x00; |
| 75 | break; |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 76 | case APDU_CASE_2S: |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 77 | tpduh->p3 = msgb_apdu_le(amsg); |
| 78 | break; |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 79 | case APDU_CASE_2E: |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 80 | if (msgb_apdu_le(amsg) <= 256) { |
| 81 | /* case 2E.1 */ |
| 82 | tpduh->p3 = msgb_apdu_le(amsg) & 0xff; |
| 83 | } else { |
| 84 | /* case 2E.2 */ |
| 85 | tpduh->p3 = 0; |
| 86 | msgb_put_u16(tmsg, msgb_apdu_le(amsg)); |
| 87 | } |
| 88 | break; |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 89 | case APDU_CASE_3S: |
| 90 | case APDU_CASE_4S: |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 91 | tpduh->p3 = msgb_apdu_lc(amsg); |
| 92 | cur = msgb_put(tmsg, tpduh->p3); |
| 93 | memcpy(cur, msgb_apdu_dc(amsg), tpduh->p3); |
| 94 | break; |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 95 | case APDU_CASE_3E: |
| 96 | case APDU_CASE_4E: |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 97 | if (msgb_apdu_lc(amsg) < 256) { |
| 98 | /* Case 3E.1 */ |
| 99 | tpduh->p3 = msgb_apdu_lc(amsg); |
| 100 | } else { |
| 101 | /* Case 3E.2 */ |
| 102 | /* FXIME: Split using ENVELOPE! */ |
| 103 | return -1; |
| 104 | } |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | transceive_again: |
| 109 | |
| 110 | /* store pointer to start of response */ |
| 111 | tmsg->l3h = tmsg->tail; |
| 112 | |
| 113 | /* transceive */ |
| 114 | rc = rh->ops->transceive(st->reader, tmsg); |
| 115 | if (rc < 0) { |
| 116 | msgb_free(tmsg); |
| 117 | return rc; |
| 118 | } |
| 119 | msgb_apdu_sw(tmsg) = get_sw(tmsg); |
| 120 | |
| 121 | /* increase number of responsese received */ |
| 122 | num_resp++; |
| 123 | |
| 124 | /* save SW */ |
| 125 | sw = msgb_apdu_sw(tmsg); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 126 | msgb_apdu_sw(amsg) = sw; |
| 127 | |
| 128 | switch (msgb_apdu_case(amsg)) { |
| 129 | case APDU_CASE_1: |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 130 | case APDU_CASE_3S: |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 131 | /* just copy SW */ |
| 132 | break; |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 133 | case APDU_CASE_2S: |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 134 | case_2s: |
| 135 | switch (sw >> 8) { |
| 136 | case 0x67: /* Case 2S.2: Le definitely not accepted */ |
| 137 | break; |
| 138 | case 0x6c: /* Case 2S.3: Le not accepted, La indicated */ |
| 139 | tpduh->p3 = sw & 0xff; |
| 140 | /* re-issue the command with La as */ |
| 141 | goto transceive_again; |
| 142 | break; |
| 143 | case 0x90: |
| 144 | /* Case 2S.1, fall-through */ |
| 145 | case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: |
| 146 | case 0x96: case 0x97: case 0x98: case 0x99: case 0x9a: |
| 147 | case 0x9b: case 0x9c: case 0x9d: case 0x9e: case 0x9f: |
| 148 | /* Case 2S.4 */ |
| 149 | /* copy response data over */ |
| 150 | cur = msgb_put(amsg, msgb_l3len(tmsg)); |
| 151 | memcpy(cur, tmsg->l3h, msgb_l3len(tmsg)); |
| 152 | } |
| 153 | break; |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 154 | case APDU_CASE_4S: |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 155 | /* FIXME: this is 4S.2 only for 2nd... response: */ |
| 156 | if (num_resp >= 2) |
| 157 | goto case_2s; |
| 158 | |
| 159 | switch (sw >> 8) { |
| 160 | case 0x60: case 0x62: case 0x63: case 0x64: case 0x65: |
| 161 | case 0x66: case 0x67: case 0x68: case 0x69: case 0x6a: |
| 162 | case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f: |
| 163 | /* Case 4S.1: Command not accepted: just copy SW */ |
| 164 | break; |
| 165 | case 0x90: |
| 166 | /* case 4S.2: Command accepted */ |
| 167 | tpduh->ins = 0xC0; |
| 168 | tpduh->p1 = tpduh->p2 = 0; |
| 169 | tpduh->p3 = msgb_apdu_le(amsg); |
| 170 | /* strip off current result */ |
| 171 | msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh)); |
| 172 | goto transceive_again; |
| 173 | break; |
| 174 | case 0x61: /* Case 4S.3: command accepted with info added */ |
Harald Welte | d83d296 | 2013-03-04 17:52:33 +0000 | [diff] [blame] | 175 | case 0x9F: /* FIXME: This is specific to SIM cards */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 176 | tpduh->ins = 0xC0; |
| 177 | tpduh->p1 = tpduh->p2 = 0; |
| 178 | tpduh->p3 = OSMO_MIN(msgb_apdu_le(amsg), sw & 0xff); |
| 179 | /* strip off current result */ |
| 180 | msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh)); |
| 181 | goto transceive_again; |
| 182 | break; |
| 183 | } |
| 184 | /* Case 4S.2: Command accepted: just copy SW */ |
| 185 | /* Case 4S.4: Just copy SW */ |
| 186 | break; |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 187 | case APDU_CASE_2E: |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 188 | if (msgb_apdu_le(amsg) <= 256) { |
| 189 | /* Case 2E.1: Le <= 256 */ |
| 190 | goto case_2s; |
| 191 | } |
| 192 | switch (sw >> 8) { |
| 193 | case 0x67: |
| 194 | /* Case 2E.2a: wrong length, abort */ |
| 195 | break; |
| 196 | case 0x6c: |
| 197 | /* Case 2E.2b: wrong length, La given */ |
| 198 | tpduh->p3 = sw & 0xff; |
| 199 | /* re-issue the command with La as given */ |
| 200 | goto transceive_again; |
| 201 | break; |
| 202 | case 0x90: |
| 203 | /* Case 2E.2c: */ |
| 204 | break; |
| 205 | case 0x61: |
| 206 | /* Case 2E.2d: more data available */ |
| 207 | /* FIXME: issue yet another GET RESPONSE */ |
| 208 | break; |
| 209 | } |
| 210 | break; |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 211 | case APDU_CASE_3E: |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 212 | /* FIXME: handling for ENVELOPE splitting */ |
| 213 | break; |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 214 | case APDU_CASE_4E: |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 215 | break; |
| 216 | } |
| 217 | |
| 218 | msgb_free(tmsg); |
| 219 | |
| 220 | /* compute total length of response data */ |
| 221 | msgb_apdu_le(amsg) = amsg->tail - msgb_apdu_de(amsg); |
| 222 | |
| 223 | return sw; |
| 224 | } |
| 225 | |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 226 | /* FIXME: T=1 According to ISO7816-4 Annex B */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 227 | |
| 228 | int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg) |
| 229 | { |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 230 | switch (st->card->proto) { |
| 231 | case OSIM_PROTO_T0: |
| 232 | return transceive_apdu_t0(st->card, amsg); |
| 233 | default: |
| 234 | return -ENOTSUP; |
| 235 | } |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 238 | struct osim_reader_hdl *osim_reader_open(enum osim_reader_driver driver, int idx, |
| 239 | const char *name, void *ctx) |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 240 | { |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 241 | const struct osim_reader_ops *ops; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 242 | struct osim_reader_hdl *rh; |
| 243 | |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 244 | switch (driver) { |
Harald Welte | 3c44a64 | 2020-03-15 22:50:06 +0100 | [diff] [blame] | 245 | #ifdef HAVE_PCSC |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 246 | case OSIM_READER_DRV_PCSC: |
| 247 | ops = &pcsc_reader_ops; |
| 248 | break; |
Harald Welte | 3c44a64 | 2020-03-15 22:50:06 +0100 | [diff] [blame] | 249 | #endif |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 250 | default: |
| 251 | return NULL; |
| 252 | } |
| 253 | |
Harald Welte | d83d296 | 2013-03-04 17:52:33 +0000 | [diff] [blame] | 254 | rh = ops->reader_open(idx, name, ctx); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 255 | if (!rh) |
| 256 | return NULL; |
| 257 | rh->ops = ops; |
| 258 | |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 259 | /* FIXME: for now we only do T=0 on all readers */ |
| 260 | rh->proto_supported = (1 << OSIM_PROTO_T0); |
| 261 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 262 | return rh; |
| 263 | } |
| 264 | |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 265 | struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh, enum osim_proto proto) |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 266 | { |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 267 | struct osim_card_hdl *ch; |
| 268 | |
| 269 | if (!(rh->proto_supported & (1 << proto))) |
| 270 | return NULL; |
| 271 | |
| 272 | ch = rh->ops->card_open(rh, proto); |
| 273 | if (!ch) |
| 274 | return NULL; |
| 275 | |
| 276 | ch->proto = proto; |
| 277 | |
| 278 | return ch; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 279 | } |
Harald Welte | 20199da | 2021-06-01 20:11:19 +0200 | [diff] [blame] | 280 | |
| 281 | int osim_card_reset(struct osim_card_hdl *card, bool cold_reset) |
| 282 | { |
| 283 | struct osim_reader_hdl *rh = card->reader; |
| 284 | |
| 285 | return rh->ops->card_reset(card, cold_reset); |
| 286 | } |
| 287 | |
| 288 | int osim_card_close(struct osim_card_hdl *card) |
| 289 | { |
| 290 | struct osim_reader_hdl *rh = card->reader; |
| 291 | int rc; |
| 292 | |
| 293 | rc = rh->ops->card_close(card); |
| 294 | |
| 295 | card->reader = NULL; |
| 296 | talloc_free(card); |
| 297 | rh->card = NULL; |
| 298 | |
| 299 | return rc; |
| 300 | } |