Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | #include <unistd.h> |
| 4 | #include <assert.h> |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 5 | #include <string.h> |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 6 | |
| 7 | #include <osmocom/core/msgb.h> |
| 8 | #include <osmocom/core/utils.h> |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 9 | #include <osmocom/core/logging.h> |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 10 | |
| 11 | #include "ccid_proto.h" |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 12 | #include "ccid_device.h" |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 13 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 14 | /* decode on-the-wire T0 parameters into their parsed form */ |
| 15 | static int decode_ccid_pars_t0(struct ccid_pars_decoded *out, const struct ccid_proto_data_t0 *in) |
| 16 | { |
| 17 | /* input validation: only 0x00 and 0x02 permitted for bmTCCKST0 */ |
| 18 | if (in->bmTCCKST0 & 0xFD) |
| 19 | return -11; |
| 20 | /* input validation: only 0x00 to 0x03 permitted for bClockSTop */ |
| 21 | if (in->bClockStop & 0xFC) |
| 22 | return -14; |
| 23 | |
| 24 | out->fi = in->bmFindexDindex >> 4; |
| 25 | out->di = in->bmFindexDindex & 0xF; |
| 26 | if (in->bmTCCKST0 & 2) |
| 27 | out->inverse_convention = true; |
| 28 | else |
| 29 | out->inverse_convention = false; |
| 30 | if (in->bGuardTimeT0 == 0xff) |
| 31 | out->t0.guard_time_etu = 0; |
| 32 | else |
| 33 | out->t0.guard_time_etu = in->bGuardTimeT0; |
| 34 | out->t0.waiting_integer = in->bWaitingIntegerT0; |
| 35 | out->clock_stop = in->bClockStop & 0x03; |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | /* encode T0 parameters from parsed form into on-the-wire encoding */ |
| 41 | static void encode_ccid_pars_t0(struct ccid_proto_data_t0 *out, const struct ccid_pars_decoded *in) |
| 42 | { |
| 43 | out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F); |
| 44 | if (in->inverse_convention) |
| 45 | out->bmTCCKST0 = 0x02; |
| 46 | else |
| 47 | out->bmTCCKST0 = 0x00; |
| 48 | out->bGuardTimeT0 = in->t0.guard_time_etu; |
| 49 | out->bWaitingIntegerT0 = in->t0.waiting_integer; |
| 50 | out->bClockStop = in->clock_stop & 0x03; |
| 51 | } |
| 52 | |
| 53 | /* decode on-the-wire T1 parameters into their parsed form */ |
| 54 | static int decode_ccid_pars_t1(struct ccid_pars_decoded *out, const struct ccid_proto_data_t1 *in) |
| 55 | { |
| 56 | /* input validation: only some values permitted for bmTCCKST0 */ |
| 57 | if (in->bmTCCKST1 & 0xE8) |
| 58 | return -11; |
| 59 | /* input validation: only 0x00 to 0x9F permitted for bmWaitingIntegersT1 */ |
| 60 | if (in->bWaitingIntegersT1 > 0x9F) |
| 61 | return -13; |
| 62 | /* input validation: only 0x00 to 0x03 permitted for bClockSTop */ |
| 63 | if (in->bClockStop & 0xFC) |
| 64 | return -14; |
| 65 | /* input validation: only 0x00 to 0xFE permitted for bIFSC */ |
| 66 | if (in->bIFSC > 0xFE) |
| 67 | return -15; |
| 68 | |
| 69 | out->fi = in->bmFindexDindex >> 4; |
| 70 | out->di = in->bmFindexDindex & 0xF; |
| 71 | if (in->bmTCCKST1 & 1) |
| 72 | out->t1.csum_type = CCID_CSUM_TYPE_CRC; |
| 73 | else |
| 74 | out->t1.csum_type = CCID_CSUM_TYPE_LRC; |
| 75 | if (in->bmTCCKST1 & 2) |
| 76 | out->inverse_convention = true; |
| 77 | else |
| 78 | out->inverse_convention = false; |
| 79 | out->t1.guard_time_t1 = in->bGuardTimeT1; |
| 80 | out->t1.bwi = in->bWaitingIntegersT1 >> 4; |
| 81 | out->t1.cwi = in->bWaitingIntegersT1 & 0xF; |
| 82 | out->clock_stop = in->bClockStop & 0x03; |
| 83 | out->t1.ifsc = in->bIFSC; |
| 84 | out->t1.nad = in->bNadValue; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | /* encode T1 parameters from parsed form into on-the-wire encoding */ |
| 90 | static void encode_ccid_pars_t1(struct ccid_proto_data_t1 *out, const struct ccid_pars_decoded *in) |
| 91 | { |
| 92 | out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F); |
| 93 | out->bmTCCKST1 = 0x10; |
| 94 | if (in->t1.csum_type == CCID_CSUM_TYPE_CRC) |
| 95 | out->bmTCCKST1 |= 0x01; |
| 96 | if (in->inverse_convention) |
| 97 | out->bmTCCKST1 |= 0x02; |
| 98 | out->bGuardTimeT1 = in->t1.guard_time_t1; |
| 99 | out->bWaitingIntegersT1 = ((in->t1.bwi << 4) & 0xF0) | (in->t1.cwi & 0x0F); |
| 100 | out->bClockStop = in->clock_stop & 0x03; |
| 101 | out->bIFSC = in->t1.ifsc; |
| 102 | out->bNadValue = in->t1.nad; |
| 103 | } |
| 104 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 105 | #define msgb_ccid_out(x) (union ccid_pc_to_rdr *)msgb_data(x) |
| 106 | #define msgb_ccid_in(x) (union ccid_rdr_to_pc *)msgb_data(x) |
| 107 | |
| 108 | static struct ccid_slot *get_ccid_slot(struct ccid_instance *ci, uint8_t slot_nr) |
| 109 | { |
| 110 | if (slot_nr >= sizeof(ci->slot)) |
| 111 | return NULL; |
| 112 | else |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 113 | return &ci->slot[slot_nr]; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static uint8_t get_icc_status(const struct ccid_slot *cs) |
| 117 | { |
| 118 | if (cs->icc_present && cs->icc_powered && !cs->icc_in_reset) |
| 119 | return CCID_ICC_STATUS_PRES_ACT; |
| 120 | else if (!cs->icc_present) |
| 121 | return CCID_ICC_STATUS_NO_ICC; |
| 122 | else |
| 123 | return CCID_ICC_STATUS_PRES_INACT; |
| 124 | } |
| 125 | |
| 126 | #define SET_HDR(x, msg_type, slot, seq) do { \ |
| 127 | (x)->hdr.bMessageType = msg_type; \ |
| 128 | (x)->hdr.dwLength = 0; \ |
| 129 | (x)->hdr.bSlot = slot; \ |
| 130 | (x)->hdr.bSeq = seq; \ |
| 131 | } while (0) |
| 132 | |
| 133 | #define SET_HDR_IN(x, msg_type, slot, seq, status, error) do { \ |
| 134 | SET_HDR(&(x)->hdr, msg_type, slot, seq); \ |
| 135 | (x)->hdr.bStatus = status; \ |
| 136 | (x)->hdr.bError = error; \ |
| 137 | } while (0) |
| 138 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 139 | #if 0 |
| 140 | static uint8_t ccid_pc_to_rdr_get_seq(const struct ccid_pc_to_rdr *u) |
| 141 | { |
| 142 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 143 | return ch->bSeq; |
| 144 | } |
| 145 | #endif |
| 146 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 147 | /*********************************************************************** |
| 148 | * Message generation / sending |
| 149 | ***********************************************************************/ |
| 150 | |
| 151 | static struct msgb *ccid_msgb_alloc(void) |
| 152 | { |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 153 | struct msgb *msg = msgb_alloc(512, "ccid"); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 154 | OSMO_ASSERT(msg); |
| 155 | return msg; |
| 156 | } |
| 157 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 158 | /* Send given CCID message */ |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 159 | static int ccid_send(struct ccid_instance *ci, struct msgb *msg) |
| 160 | { |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 161 | struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg); |
| 162 | struct ccid_slot *cs = get_ccid_slot(ci, ch->bSlot); |
| 163 | if (cs) { |
| 164 | LOGPCS(cs, LOGL_DEBUG, "Tx CCID(IN) %s %s\n", |
| 165 | get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg)); |
| 166 | } else { |
| 167 | LOGPCI(ci, LOGL_DEBUG, "Tx CCID(IN) %s %s\n", |
| 168 | get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg)); |
| 169 | } |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 170 | return ci->ops->send_in(ci, msg); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 173 | /* Send given CCID message for given slot; patch bSlot into message */ |
Harald Welte | 005b09d | 2019-05-16 17:24:29 +0200 | [diff] [blame] | 174 | int ccid_slot_send(struct ccid_slot *cs, struct msgb *msg) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 175 | { |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 176 | struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 177 | |
| 178 | /* patch bSlotNr into message */ |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 179 | ch->bSlot = cs->slot_nr; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 180 | return ccid_send(cs->ci, msg); |
| 181 | } |
| 182 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 183 | /* Send given CCID message and mark slot as un-busy */ |
Harald Welte | 005b09d | 2019-05-16 17:24:29 +0200 | [diff] [blame] | 184 | int ccid_slot_send_unbusy(struct ccid_slot *cs, struct msgb *msg) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 185 | { |
| 186 | cs->cmd_busy = false; |
| 187 | return ccid_slot_send(cs, msg); |
| 188 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 189 | |
| 190 | /* Section 6.2.1 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 191 | static struct msgb *ccid_gen_data_block_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, |
| 192 | uint8_t cmd_sts, enum ccid_error_code err, |
| 193 | const uint8_t *data, uint32_t data_len) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 194 | { |
| 195 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 196 | struct ccid_rdr_to_pc_data_block *db = |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 197 | (struct ccid_rdr_to_pc_data_block *) msgb_put(msg, sizeof(*db) + data_len); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 198 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 199 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 200 | SET_HDR_IN(db, RDR_to_PC_DataBlock, slot_nr, seq, sts, err); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 201 | osmo_store32le(data_len, &db->hdr.hdr.dwLength); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 202 | memcpy(db->abData, data, data_len); |
| 203 | return msg; |
| 204 | } |
Harald Welte | 005b09d | 2019-05-16 17:24:29 +0200 | [diff] [blame] | 205 | struct msgb *ccid_gen_data_block(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 206 | enum ccid_error_code err, const uint8_t *data, |
| 207 | uint32_t data_len) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 208 | { |
| 209 | return ccid_gen_data_block_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len); |
| 210 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 211 | |
| 212 | /* Section 6.2.2 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 213 | static struct msgb *ccid_gen_slot_status_nr(uint8_t slot_nr, uint8_t icc_status, |
| 214 | uint8_t seq, uint8_t cmd_sts, |
| 215 | enum ccid_error_code err) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 216 | { |
| 217 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 218 | struct ccid_rdr_to_pc_slot_status *ss = |
| 219 | (struct ccid_rdr_to_pc_slot_status *) msgb_put(msg, sizeof(*ss)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 220 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 221 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 222 | SET_HDR_IN(ss, RDR_to_PC_SlotStatus, slot_nr, seq, sts, err); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 223 | return msg; |
| 224 | } |
Harald Welte | 005b09d | 2019-05-16 17:24:29 +0200 | [diff] [blame] | 225 | struct msgb *ccid_gen_slot_status(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 226 | enum ccid_error_code err) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 227 | { |
| 228 | return ccid_gen_slot_status_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err); |
| 229 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 230 | |
| 231 | /* Section 6.2.3 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 232 | static struct msgb *ccid_gen_parameters_t0_nr(uint8_t slot_nr, uint8_t icc_status, |
| 233 | uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 234 | const struct ccid_pars_decoded *dec_par) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 235 | { |
| 236 | struct msgb *msg = ccid_msgb_alloc(); |
| 237 | struct ccid_rdr_to_pc_parameters *par = |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 238 | (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t0)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 239 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
| 240 | |
| 241 | SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 242 | if (dec_par) { |
| 243 | osmo_store32le(sizeof(par->abProtocolData.t0), &par->hdr.hdr.dwLength); |
| 244 | encode_ccid_pars_t0(&par->abProtocolData.t0, dec_par); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 245 | } |
| 246 | return msg; |
| 247 | } |
| 248 | static struct msgb *ccid_gen_parameters_t0(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 249 | enum ccid_error_code err) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 250 | { |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 251 | return ccid_gen_parameters_t0_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, &cs->pars); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static struct msgb *ccid_gen_parameters_t1_nr(uint8_t slot_nr, uint8_t icc_status, |
| 255 | uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 256 | const struct ccid_pars_decoded *dec_par) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 257 | { |
| 258 | struct msgb *msg = ccid_msgb_alloc(); |
| 259 | struct ccid_rdr_to_pc_parameters *par = |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 260 | (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t1)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 261 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
| 262 | |
| 263 | SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 264 | if (dec_par) { |
| 265 | osmo_store32le(sizeof(par->abProtocolData.t1), &par->hdr.hdr.dwLength); |
| 266 | encode_ccid_pars_t1(&par->abProtocolData.t1, dec_par); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 267 | } |
| 268 | return msg; |
| 269 | } |
| 270 | static struct msgb *ccid_gen_parameters_t1(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 271 | enum ccid_error_code err) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 272 | { |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 273 | return ccid_gen_parameters_t1_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, &cs->pars); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 276 | |
| 277 | /* Section 6.2.4 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 278 | static struct msgb *ccid_gen_escape_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, uint8_t cmd_sts, |
| 279 | enum ccid_error_code err, const uint8_t *data, uint32_t data_len) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 280 | { |
| 281 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 282 | struct ccid_rdr_to_pc_escape *esc = |
| 283 | (struct ccid_rdr_to_pc_escape *) msgb_put(msg, sizeof(*esc) + data_len); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 284 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 285 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 286 | SET_HDR_IN(esc, RDR_to_PC_Escape, slot_nr, seq, sts, err); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 287 | osmo_store32le(data_len, &esc->hdr.hdr.dwLength); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 288 | memcpy(esc->abData, data, data_len); |
| 289 | return msg; |
| 290 | } |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 291 | static struct msgb *ccid_gen_escape(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 292 | enum ccid_error_code err, const uint8_t *data, |
| 293 | uint32_t data_len) |
| 294 | { |
| 295 | return ccid_gen_escape_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len); |
| 296 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 297 | |
| 298 | /* Section 6.2.5 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 299 | static struct msgb *ccid_gen_clock_and_rate_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, |
| 300 | uint8_t cmd_sts, enum ccid_error_code err, |
| 301 | uint32_t clock_khz, uint32_t rate_bps) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 302 | { |
| 303 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 304 | struct ccid_rdr_to_pc_data_rate_and_clock *drc = |
| 305 | (struct ccid_rdr_to_pc_data_rate_and_clock *) msgb_put(msg, sizeof(*drc)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 306 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 307 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 308 | SET_HDR_IN(drc, RDR_to_PC_DataRateAndClockFrequency, slot_nr, seq, sts, err); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 309 | osmo_store32le(8, &drc->hdr.hdr.dwLength); /* Message-specific data length (wtf?) */ |
| 310 | osmo_store32le(clock_khz, &drc->dwClockFrequency); /* kHz */ |
| 311 | osmo_store32le(rate_bps, &drc->dwDataRate); /* bps */ |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 312 | return msg; |
| 313 | } |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 314 | static struct msgb *ccid_gen_clock_and_rate(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 315 | enum ccid_error_code err, uint32_t clock_khz, |
| 316 | uint32_t rate_bps) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 317 | { |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 318 | return ccid_gen_clock_and_rate_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, |
| 319 | clock_khz, rate_bps); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 320 | } |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 321 | |
| 322 | /*! generate an error response for given input message_type/slot_nr/seq |
| 323 | * \param[in] msg_type CCID Message Type against which response is to be created |
| 324 | * \param[in] slot_nr CCID Slot Number |
| 325 | * \param[in] icc_status ICC Status of the slot |
| 326 | * \param[in] seq CCID Sequence number |
| 327 | * \param[in] err_code CCID Error Code to send |
| 328 | * \returns dynamically-allocated message buffer containing error response */ |
| 329 | static struct msgb *gen_err_resp(enum ccid_msg_type msg_type, uint8_t slot_nr, uint8_t icc_status, |
| 330 | uint8_t seq, enum ccid_error_code err_code) |
| 331 | { |
| 332 | struct msgb *resp = NULL; |
| 333 | |
| 334 | switch (msg_type) { |
| 335 | case PC_to_RDR_IccPowerOn: |
| 336 | case PC_to_RDR_XfrBlock: |
| 337 | case PC_to_RDR_Secure: |
| 338 | /* Return RDR_to_PC_DataBlock */ |
| 339 | resp = ccid_gen_data_block_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 340 | err_code, NULL, 0); |
| 341 | break; |
| 342 | |
| 343 | case PC_to_RDR_IccPowerOff: |
| 344 | case PC_to_RDR_GetSlotStatus: |
| 345 | case PC_to_RDR_IccClock: |
| 346 | case PC_to_RDR_T0APDU: |
| 347 | case PC_to_RDR_Mechanical: |
| 348 | case PC_to_RDR_Abort: |
| 349 | /* Return RDR_to_PC_SlotStatus */ |
| 350 | resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 351 | err_code); |
| 352 | break; |
| 353 | |
| 354 | case PC_to_RDR_GetParameters: |
| 355 | case PC_to_RDR_ResetParameters: |
| 356 | case PC_to_RDR_SetParameters: |
| 357 | /* Return RDR_to_PC_Parameters */ |
| 358 | resp = ccid_gen_parameters_t0_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 359 | err_code, NULL); /* FIXME: parameters? */ |
| 360 | break; |
| 361 | |
| 362 | case PC_to_RDR_Escape: |
| 363 | /* Return RDR_to_PC_Escape */ |
| 364 | resp = ccid_gen_escape_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 365 | err_code, NULL, 0); |
| 366 | break; |
| 367 | |
| 368 | case PC_to_RDR_SetDataRateAndClockFrequency: |
| 369 | /* Return RDR_to_PC_SlotStatus */ |
| 370 | resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 371 | err_code); |
| 372 | break; |
| 373 | |
| 374 | default: |
| 375 | /* generate general error */ |
| 376 | resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 377 | CCID_ERR_CMD_NOT_SUPPORTED); |
| 378 | break; |
| 379 | } |
| 380 | return resp; |
| 381 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 382 | |
| 383 | /*********************************************************************** |
| 384 | * Message reception / parsing |
| 385 | ***********************************************************************/ |
| 386 | |
| 387 | /* Section 6.1.3 */ |
| 388 | static int ccid_handle_get_slot_status(struct ccid_slot *cs, struct msgb *msg) |
| 389 | { |
| 390 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 391 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 392 | uint8_t seq = u->get_slot_status.hdr.bSeq; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 393 | struct msgb *resp; |
| 394 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 395 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 396 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 397 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | |
| 401 | /* Section 6.1.1 */ |
| 402 | static int ccid_handle_icc_power_on(struct ccid_slot *cs, struct msgb *msg) |
| 403 | { |
| 404 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 405 | const struct ccid_header *ch = (const struct ccid_header *) u; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 406 | |
Harald Welte | 505d441 | 2019-05-16 17:26:09 +0200 | [diff] [blame^] | 407 | /* handle this asynchronously */ |
| 408 | cs->ci->slot_ops->icc_power_on_async(cs, msg, &u->icc_power_on); |
| 409 | return 1; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /* Section 6.1.2 */ |
| 413 | static int ccid_handle_icc_power_off(struct ccid_slot *cs, struct msgb *msg) |
| 414 | { |
| 415 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 416 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 417 | uint8_t seq = u->icc_power_off.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 418 | struct msgb *resp; |
| 419 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 420 | cs->ci->slot_ops->set_power(cs, false); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 421 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 422 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /* Section 6.1.4 */ |
| 426 | static int ccid_handle_xfr_block(struct ccid_slot *cs, struct msgb *msg) |
| 427 | { |
| 428 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 429 | const struct ccid_header *ch = (const struct ccid_header *) u; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 430 | |
Harald Welte | 505d441 | 2019-05-16 17:26:09 +0200 | [diff] [blame^] | 431 | /* handle this asynchronously */ |
| 432 | cs->ci->slot_ops->xfr_block_async(cs, msg, &u->xfr_block); |
| 433 | return 1; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* Section 6.1.5 */ |
| 437 | static int ccid_handle_get_parameters(struct ccid_slot *cs, struct msgb *msg) |
| 438 | { |
| 439 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 440 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 441 | uint8_t seq = u->get_parameters.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 442 | struct msgb *resp; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 443 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 444 | /* FIXME: T=1 */ |
| 445 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 446 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /* Section 6.1.6 */ |
| 450 | static int ccid_handle_reset_parameters(struct ccid_slot *cs, struct msgb *msg) |
| 451 | { |
| 452 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 453 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 454 | uint8_t seq = u->reset_parameters.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 455 | struct msgb *resp; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 456 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 457 | /* copy default parameters from somewhere */ |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 458 | /* FIXME: T=1 */ |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 459 | cs->ci->slot_ops->set_params(cs, CCID_PROTOCOL_NUM_T0, cs->default_pars); |
| 460 | cs->pars = *cs->default_pars; |
| 461 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 462 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 463 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* Section 6.1.7 */ |
| 467 | static int ccid_handle_set_parameters(struct ccid_slot *cs, struct msgb *msg) |
| 468 | { |
| 469 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 470 | const struct ccid_pc_to_rdr_set_parameters *spar = &u->set_parameters; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 471 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 472 | uint8_t seq = u->set_parameters.hdr.bSeq; |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 473 | struct ccid_pars_decoded pars_dec; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 474 | struct msgb *resp; |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 475 | int rc; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 476 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 477 | switch (spar->bProtocolNum) { |
| 478 | case CCID_PROTOCOL_NUM_T0: |
| 479 | rc = decode_ccid_pars_t0(&pars_dec, &spar->abProtocolData.t0); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 480 | break; |
| 481 | case CCID_PROTOCOL_NUM_T1: |
| 482 | rc = decode_ccid_pars_t1(&pars_dec, &spar->abProtocolData.t1); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 483 | break; |
| 484 | default: |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 485 | LOGP(DCCID, LOGL_ERROR, "SetParameters: Invalid Protocol 0x%02x\n",spar->bProtocolNum); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 486 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, 0); |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 487 | goto out; |
| 488 | } |
| 489 | |
| 490 | if (rc < 0) { |
| 491 | LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse: %d\n", rc); |
| 492 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc); |
| 493 | goto out; |
| 494 | } |
| 495 | |
| 496 | /* validate parameters; abort if they are not supported */ |
| 497 | rc = cs->ci->slot_ops->set_params(cs, spar->bProtocolNum, &pars_dec); |
| 498 | if (rc < 0) { |
| 499 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc); |
| 500 | } else { |
| 501 | cs->pars = pars_dec; |
| 502 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 503 | } |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 504 | out: |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 505 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /* Section 6.1.8 */ |
| 509 | static int ccid_handle_escape(struct ccid_slot *cs, struct msgb *msg) |
| 510 | { |
| 511 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 512 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 513 | uint8_t seq = u->escape.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 514 | struct msgb *resp; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 515 | |
| 516 | resp = ccid_gen_escape(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED, NULL, 0); |
| 517 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /* Section 6.1.9 */ |
| 521 | static int ccid_handle_icc_clock(struct ccid_slot *cs, struct msgb *msg) |
| 522 | { |
| 523 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 524 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 525 | uint8_t seq = u->icc_clock.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 526 | struct msgb *resp; |
| 527 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 528 | cs->ci->slot_ops->set_clock(cs, u->icc_clock.bClockCommand); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 529 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 530 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* Section 6.1.10 */ |
| 534 | static int ccid_handle_t0apdu(struct ccid_slot *cs, struct msgb *msg) |
| 535 | { |
| 536 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 537 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 538 | uint8_t seq = u->t0apdu.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 539 | struct msgb *resp; |
| 540 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 541 | /* FIXME: Required for APDU level exchange */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 542 | //resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 543 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 544 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /* Section 6.1.11 */ |
| 548 | static int ccid_handle_secure(struct ccid_slot *cs, struct msgb *msg) |
| 549 | { |
| 550 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 551 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 552 | uint8_t seq = u->secure.hdr.bSeq; |
| 553 | struct msgb *resp; |
| 554 | |
| 555 | /* FIXME */ |
| 556 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 557 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /* Section 6.1.12 */ |
| 561 | static int ccid_handle_mechanical(struct ccid_slot *cs, struct msgb *msg) |
| 562 | { |
| 563 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 564 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 565 | uint8_t seq = u->mechanical.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 566 | struct msgb *resp; |
| 567 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 568 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 569 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /* Section 6.1.13 */ |
| 573 | static int ccid_handle_abort(struct ccid_slot *cs, struct msgb *msg) |
| 574 | { |
| 575 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 576 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 577 | uint8_t seq = u->abort.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 578 | struct msgb *resp; |
| 579 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 580 | /* Check if the currently in-progress message is Abortable */ |
| 581 | switch (0/* FIXME */) { |
| 582 | case PC_to_RDR_IccPowerOn: |
| 583 | case PC_to_RDR_XfrBlock: |
| 584 | case PC_to_RDR_Escape: |
| 585 | case PC_to_RDR_Secure: |
| 586 | case PC_to_RDR_Mechanical: |
| 587 | //case PC_to_RDR_Abort: /* seriously? WTF! */ |
| 588 | break; |
| 589 | default: |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 590 | LOGP(DCCID, LOGL_ERROR, "Abort for non-Abortable Message Type\n"); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 591 | /* CCID spec lists CMD_NOT_ABORTED, but gives no numberic value ?!? */ |
| 592 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 593 | return ccid_slot_send_unbusy(cs, resp); |
| 594 | } |
| 595 | |
| 596 | /* FIXME */ |
| 597 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 598 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | /* Section 6.1.14 */ |
| 602 | static int ccid_handle_set_rate_and_clock(struct ccid_slot *cs, struct msgb *msg) |
| 603 | { |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 604 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 605 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 606 | uint8_t seq = u->set_rate_and_clock.hdr.bSeq; |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 607 | uint32_t freq_hz = osmo_load32le(&u->set_rate_and_clock.dwClockFrequency); |
| 608 | uint32_t rate_bps = osmo_load32le(&u->set_rate_and_clock.dwDataRate); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 609 | struct msgb *resp; |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 610 | int rc; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 611 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 612 | /* FIXME: which rate to return in failure case? */ |
| 613 | rc = cs->ci->slot_ops->set_rate_and_clock(cs, freq_hz, rate_bps); |
| 614 | if (rc < 0) |
| 615 | resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_FAILED, -rc, 9600, 2500000); |
| 616 | else |
| 617 | resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_OK, 0, rate_bps, freq_hz); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 618 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 619 | } |
| 620 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 621 | /*! Handle data arriving from the host on the OUT endpoint. |
| 622 | * \param[in] cs CCID Instance on which to operate |
Harald Welte | e6ac416 | 2019-05-16 11:18:00 +0200 | [diff] [blame] | 623 | * \param[in] msgb received message buffer containing one CCID OUT EP message from the host. |
| 624 | * Ownership of message buffer is transferred, i.e. it's our job to msgb_free() |
| 625 | * it eventually, after we're done with it (could be asynchronously). |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 626 | * \returns 0 on success; negative on error */ |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 627 | int ccid_handle_out(struct ccid_instance *ci, struct msgb *msg) |
| 628 | { |
| 629 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
| 630 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 631 | unsigned int len = msgb_length(msg); |
| 632 | struct ccid_slot *cs; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 633 | struct msgb *resp; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 634 | int rc; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 635 | |
| 636 | if (len < sizeof(*ch)) { |
| 637 | /* FIXME */ |
| 638 | return -1; |
| 639 | } |
| 640 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 641 | /* Check for invalid slot number */ |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 642 | cs = get_ccid_slot(ci, ch->bSlot); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 643 | if (!cs) { |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 644 | LOGPCI(ci, LOGL_ERROR, "Invalid bSlot %u\n", ch->bSlot); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 645 | resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, 5); |
| 646 | return ccid_send(ci, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 647 | } |
| 648 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 649 | /* Check if slot is already busy; Reject any additional commands meanwhile */ |
| 650 | if (cs->cmd_busy) { |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 651 | LOGPCS(cs, LOGL_ERROR, "Slot Busy, but another cmd received\n"); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 652 | /* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */ |
| 653 | resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq, |
| 654 | CCID_ERR_CMD_SLOT_BUSY); |
| 655 | return ccid_send(ci, resp); |
| 656 | } |
| 657 | |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 658 | LOGPCS(cs, LOGL_DEBUG, "Rx CCID(OUT) %s %s\n", |
| 659 | get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg)); |
| 660 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 661 | /* we're now processing a command for the slot; mark slot as busy */ |
| 662 | cs->cmd_busy = true; |
| 663 | |
| 664 | /* TODO: enqueue into the per-slot specific input queue */ |
| 665 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 666 | /* call pre-processing call-back function; allows reader to update state */ |
| 667 | if (ci->slot_ops->pre_proc_cb) |
| 668 | ci->slot_ops->pre_proc_cb(cs, msg); |
| 669 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 670 | switch (ch->bMessageType) { |
| 671 | case PC_to_RDR_GetSlotStatus: |
| 672 | if (len != sizeof(u->get_slot_status)) |
| 673 | goto short_msg; |
| 674 | rc = ccid_handle_get_slot_status(cs, msg); |
| 675 | break; |
| 676 | case PC_to_RDR_IccPowerOn: |
| 677 | if (len != sizeof(u->icc_power_on)) |
| 678 | goto short_msg; |
| 679 | rc = ccid_handle_icc_power_on(cs, msg); |
| 680 | break; |
| 681 | case PC_to_RDR_IccPowerOff: |
| 682 | if (len != sizeof(u->icc_power_off)) |
| 683 | goto short_msg; |
| 684 | rc = ccid_handle_icc_power_off(cs, msg); |
| 685 | break; |
| 686 | case PC_to_RDR_XfrBlock: |
| 687 | if (len < sizeof(u->xfr_block)) |
| 688 | goto short_msg; |
| 689 | rc = ccid_handle_xfr_block(cs, msg); |
| 690 | break; |
| 691 | case PC_to_RDR_GetParameters: |
| 692 | if (len != sizeof(u->get_parameters)) |
| 693 | goto short_msg; |
| 694 | rc = ccid_handle_get_parameters(cs, msg); |
| 695 | break; |
| 696 | case PC_to_RDR_ResetParameters: |
| 697 | if (len != sizeof(u->reset_parameters)) |
| 698 | goto short_msg; |
| 699 | rc = ccid_handle_reset_parameters(cs, msg); |
| 700 | break; |
| 701 | case PC_to_RDR_SetParameters: |
| 702 | if (len != sizeof(u->set_parameters)) |
| 703 | goto short_msg; |
| 704 | rc = ccid_handle_set_parameters(cs, msg); |
| 705 | break; |
| 706 | case PC_to_RDR_Escape: |
| 707 | if (len < sizeof(u->escape)) |
| 708 | goto short_msg; |
| 709 | rc = ccid_handle_escape(cs, msg); |
| 710 | break; |
| 711 | case PC_to_RDR_IccClock: |
| 712 | if (len != sizeof(u->icc_clock)) |
| 713 | goto short_msg; |
| 714 | rc = ccid_handle_icc_clock(cs, msg); |
| 715 | break; |
| 716 | case PC_to_RDR_T0APDU: |
| 717 | if (len != /*FIXME*/ sizeof(u->t0apdu)) |
| 718 | goto short_msg; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 719 | rc = ccid_handle_t0apdu(cs, msg); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 720 | break; |
| 721 | case PC_to_RDR_Secure: |
| 722 | if (len < sizeof(u->secure)) |
| 723 | goto short_msg; |
| 724 | rc = ccid_handle_secure(cs, msg); |
| 725 | break; |
| 726 | case PC_to_RDR_Mechanical: |
| 727 | if (len != sizeof(u->mechanical)) |
| 728 | goto short_msg; |
| 729 | rc = ccid_handle_mechanical(cs, msg); |
| 730 | break; |
| 731 | case PC_to_RDR_Abort: |
| 732 | if (len != sizeof(u->abort)) |
| 733 | goto short_msg; |
| 734 | rc = ccid_handle_abort(cs, msg); |
| 735 | break; |
| 736 | case PC_to_RDR_SetDataRateAndClockFrequency: |
| 737 | if (len != sizeof(u->set_rate_and_clock)) |
| 738 | goto short_msg; |
| 739 | rc = ccid_handle_set_rate_and_clock(cs, msg); |
| 740 | break; |
| 741 | default: |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 742 | /* generic response with bERror = 0 (command not supported) */ |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 743 | LOGP(DCCID, LOGL_NOTICE, "Unknown CCID Message received: 0x%02x\n", ch->bMessageType); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 744 | resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, |
| 745 | CCID_ERR_CMD_NOT_SUPPORTED); |
Harald Welte | 922ff93 | 2019-05-16 11:21:51 +0200 | [diff] [blame] | 746 | msgb_free(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 747 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 748 | } |
Harald Welte | 922ff93 | 2019-05-16 11:21:51 +0200 | [diff] [blame] | 749 | /* the various ccid_handle_* functions can return '1' to tell us that they took ownership |
| 750 | * of the msgb */ |
| 751 | if (rc != 1) |
| 752 | msgb_free(msg); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 753 | return 0; |
| 754 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 755 | short_msg: |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 756 | LOGP(DCCID, LOGL_ERROR, "Short CCID message received: %s; ignoring\n", msgb_hexdump(msg)); |
Harald Welte | 922ff93 | 2019-05-16 11:21:51 +0200 | [diff] [blame] | 757 | msgb_free(msg); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 758 | return -1; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 759 | } |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 760 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 761 | void ccid_instance_init(struct ccid_instance *ci, const struct ccid_ops *ops, |
| 762 | const struct ccid_slot_ops *slot_ops, const char *name, void *priv) |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 763 | { |
| 764 | int i; |
| 765 | |
| 766 | for (i = 0; i < ARRAY_SIZE(ci->slot); i++) { |
| 767 | struct ccid_slot *cs = &ci->slot[i]; |
| 768 | cs->slot_nr = i; |
| 769 | cs->ci = ci; |
| 770 | } |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 771 | ci->ops = ops; |
| 772 | ci->slot_ops = slot_ops; |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 773 | ci->name = name; |
| 774 | ci->priv = priv; |
| 775 | } |