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