Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file sim.h |
| 2 | * Routines for helping with SIM (ISO/IEC 7816-4 more generally) communication. |
| 3 | */ |
| 4 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 5 | #ifndef _OSMOCOM_SIM_H |
| 6 | #define _OSMOCOM_SIM_H |
| 7 | |
| 8 | #include <osmocom/core/msgb.h> |
| 9 | #include <osmocom/core/linuxlist.h> |
| 10 | |
| 11 | #define APDU_HDR_LEN 5 |
| 12 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 13 | /*! command-response pairs cases |
Kevin Redon | 43eabee | 2012-09-16 18:40:02 +0200 | [diff] [blame] | 14 | * |
| 15 | * Enumeration used to identify the APDU structure based on command-response pair case , as specified in ISO/IEC 7816-3:2006(E) §12.1. |
| 16 | */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 17 | enum osim_apdu_case { |
Kevin Redon | 43eabee | 2012-09-16 18:40:02 +0200 | [diff] [blame] | 18 | APDU_CASE_1, /*!< command header, no command data field, no response data field */ |
| 19 | APDU_CASE_2S, /*!< command header, no command data field, response data field (short) */ |
| 20 | APDU_CASE_2E, /*!< command header, no command data field, response data field (extended) */ |
| 21 | APDU_CASE_3S, /*!< command header, command data field (short), no response data field */ |
| 22 | APDU_CASE_3E, /*!< command header, command data field (extended), no response data field */ |
| 23 | APDU_CASE_4S, /*!< command header, command data field (short), response data field (short) */ |
| 24 | APDU_CASE_4E /*!< command header, command data field (extended), response data field (extended) */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 25 | }; |
| 26 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 27 | /*! APDU/TPDU command header |
Kevin Redon | 43eabee | 2012-09-16 18:40:02 +0200 | [diff] [blame] | 28 | * |
| 29 | * This structure encode an APDU/TPDU command header, as specified in ISO/IEC 7816-3:2006(E) §12.2 and §12.3. |
| 30 | * The APDU (application layer) can be encoded as different TPDUs (transport layer), depending on the transport protocol used. |
| 31 | * The TPDU encoding by T=1 of the APDU command header is identical to the APDU. |
| 32 | * The TPDU encoding by T=0 of the APDU command header adds a Parameter 3 field, generally used instead of Lc/Le. |
| 33 | * |
| 34 | * @todo have different structures for APDU, TPDU by T=0, and TPDU by T=1. |
| 35 | */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 36 | struct osim_apdu_cmd_hdr { |
Kevin Redon | 43eabee | 2012-09-16 18:40:02 +0200 | [diff] [blame] | 37 | uint8_t cla; /*!< CLASS byte */ |
| 38 | uint8_t ins; /*!< INSTRUCTION byte */ |
| 39 | uint8_t p1; /*!< Parameter 1 byte */ |
| 40 | uint8_t p2; /*!< Parameter 2 byte */ |
| 41 | uint8_t p3; /*!< Parameter 3 byte, used for TPDU by T=0 */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 42 | } __attribute__ ((packed)); |
| 43 | |
| 44 | #define msgb_apdu_dr(__x) |
| 45 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 46 | /*! APDU command body |
Kevin Redon | 43eabee | 2012-09-16 18:40:02 +0200 | [diff] [blame] | 47 | * |
| 48 | * This structure encode a command body, as specified in ISO/IEC 7816-3:2006(E) §12.1. |
| 49 | * The data and response contents should be provided along with this structure. |
| 50 | */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 51 | struct osim_msgb_cb { |
Kevin Redon | 43eabee | 2012-09-16 18:40:02 +0200 | [diff] [blame] | 52 | enum osim_apdu_case apduc; /*!< command-response pair case, defining the encoding of Lc and Le */ |
| 53 | uint16_t lc; /*!< number of bytes in the command data field Nc, which will encoded in 0, 1 or 3 bytes into Lc, depending on the case */ |
| 54 | uint16_t le; /*!< maximum number of bytes expected in the response data field, which will encoded in 0, 1, 2 or 3 bytes into Le, depending on the case */ |
| 55 | uint16_t sw; /*!< status word, composed of SW1 and SW2 bytes */ |
Harald Welte | e8dd2bd | 2014-05-04 13:42:38 +0200 | [diff] [blame] | 56 | } __attribute__((__may_alias__)); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 57 | #define OSIM_MSGB_CB(__msgb) ((struct osim_msgb_cb *)&((__msgb)->cb[0])) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 58 | /*! status word from msgb->cb */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 59 | #define msgb_apdu_case(__x) OSIM_MSGB_CB(__x)->apduc |
| 60 | #define msgb_apdu_lc(__x) OSIM_MSGB_CB(__x)->lc |
| 61 | #define msgb_apdu_le(__x) OSIM_MSGB_CB(__x)->le |
| 62 | #define msgb_apdu_sw(__x) OSIM_MSGB_CB(__x)->sw |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 63 | /*! pointer to the command header of the APDU */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 64 | #define msgb_apdu_h(__x) ((struct osim_apdu_cmd_hdr *)(__x)->l2h) |
| 65 | |
| 66 | #define msgb_apdu_dc(__x) ((__x)->l2h + sizeof(struct osim_apdu_cmd_hdr)) |
| 67 | #define msgb_apdu_de(__x) ((__x)->l2h + sizeof(struct osim_apdu_cmd_hdr) + msgb_apdu_lc(__x)) |
| 68 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 69 | /* FILES */ |
| 70 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 71 | struct osim_file; |
| 72 | struct osim_file_desc; |
| 73 | struct osim_decoded_data; |
| 74 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 75 | /*! Operations for a given File */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 76 | struct osim_file_ops { |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 77 | /*! Parse binary file data into osim_decoded_data */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 78 | int (*parse)(struct osim_decoded_data *dd, |
| 79 | const struct osim_file_desc *desc, |
| 80 | int len, uint8_t *data); |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 81 | /*! Encode osim_decoded_data into binary file */ |
Harald Welte | 6729a97 | 2014-10-26 19:04:56 +0100 | [diff] [blame] | 82 | struct msgb * (*encode)(const struct osim_file_desc *desc, |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 83 | const struct osim_decoded_data *decoded); |
| 84 | }; |
| 85 | |
| 86 | enum osim_element_type { |
| 87 | ELEM_T_NONE, |
| 88 | ELEM_T_BOOL, /*!< a boolean flag */ |
| 89 | ELEM_T_UINT8, /*!< unsigned integer */ |
| 90 | ELEM_T_UINT16, /*!< unsigned integer */ |
| 91 | ELEM_T_UINT32, /*!< unsigned integer */ |
| 92 | ELEM_T_STRING, /*!< generic string */ |
| 93 | ELEM_T_BCD, /*!< BCD encoded digits */ |
| 94 | ELEM_T_BYTES, /*!< BCD encoded digits */ |
| 95 | ELEM_T_GROUP, /*!< group container, has siblings */ |
| 96 | }; |
| 97 | |
| 98 | enum osim_element_repr { |
| 99 | ELEM_REPR_NONE, |
| 100 | ELEM_REPR_DEC, |
| 101 | ELEM_REPR_HEX, |
| 102 | }; |
| 103 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 104 | /*! A single decoded element inside a file */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 105 | struct osim_decoded_element { |
| 106 | struct llist_head list; |
| 107 | |
| 108 | enum osim_element_type type; |
| 109 | enum osim_element_repr representation; |
| 110 | const char *name; |
| 111 | |
| 112 | unsigned int length; |
| 113 | union { |
| 114 | uint8_t u8; |
| 115 | uint16_t u16; |
| 116 | uint32_t u32; |
| 117 | uint8_t *buf; |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 118 | /*! A list of sibling decoded_items */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 119 | struct llist_head siblings; |
| 120 | } u; |
| 121 | }; |
| 122 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 123 | /*! Decoded data for a single file, consisting of all decoded elements */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 124 | struct osim_decoded_data { |
| 125 | /*! file to which we belong */ |
| 126 | const struct osim_file *file; |
| 127 | /*! list of 'struct decoded_element' */ |
| 128 | struct llist_head decoded_elements; |
| 129 | }; |
| 130 | |
| 131 | |
| 132 | enum osim_file_type { |
| 133 | TYPE_NONE, |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 134 | TYPE_DF, /*!< Dedicated File */ |
| 135 | TYPE_ADF, /*!< Application Dedicated File */ |
| 136 | TYPE_EF, /*!< Entry File */ |
| 137 | TYPE_EF_INT, /*!< Internal Entry File */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | enum osim_ef_type { |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 141 | EF_TYPE_TRANSP, /*!< Transparent EF */ |
| 142 | EF_TYPE_RECORD_FIXED, /*!< Fixed-Size Record EF */ |
| 143 | EF_TYPE_RECORD_CYCLIC, /*!< Cyclic Record EF */ |
| 144 | EF_TYPE_KEY, /*!< Key file as used in TETRA */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | #define F_OPTIONAL 0x0001 |
| 148 | |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 149 | #define SFI_NONE 0xFF |
| 150 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 151 | struct osim_file_desc { |
| 152 | struct llist_head list; /*!< local element in list */ |
| 153 | struct llist_head child_list; /*!< list of children EF in DF */ |
| 154 | struct osim_file_desc *parent; /*!< parent DF */ |
| 155 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 156 | enum osim_file_type type; /*!< Type of the file (EF, DF, ...) */ |
| 157 | enum osim_ef_type ef_type; /*!< Type of the EF, if type == TYPE_EF */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 158 | |
| 159 | uint16_t fid; /*!< File Identifier */ |
| 160 | uint8_t sfid; /*!< Short File IDentifier */ |
Harald Welte | 6729a97 | 2014-10-26 19:04:56 +0100 | [diff] [blame] | 161 | const uint8_t *df_name; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 162 | uint8_t df_name_len; |
| 163 | |
| 164 | const char *short_name; /*!< Short Name (like EF.ICCID) */ |
| 165 | const char *long_name; /*!< Long / description */ |
| 166 | unsigned int flags; |
| 167 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 168 | struct osim_file_ops ops; /*!< Operations (parse/encode */ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 169 | |
| 170 | struct { |
| 171 | size_t min; /*!< Minimum size of the file |
| 172 | (transparent) or record in |
| 173 | cyclic / linear file */ |
| 174 | size_t rec; /*!< Recommended size */ |
| 175 | } size; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 176 | }; |
| 177 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 178 | /*! A single instance of a file: Descriptor and contents */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 179 | struct osim_file { |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 180 | /*! Descriptor for the file */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 181 | const struct osim_file_desc *desc; |
| 182 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 183 | /*! Encoded file contents */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 184 | struct msgb *encoded_data; |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 185 | /*! Parsed/Decoded file contents */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 186 | struct osim_decoded_data *decoded_data; |
| 187 | }; |
| 188 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 189 | /*! Convenience macros for defining EF */ |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 190 | #define EF(pfid, sfi, pns, pflags, pnl, ptype, smin, srec, pdec, penc) \ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 191 | { \ |
| 192 | .fid = pfid, \ |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 193 | .sfid = sfi, \ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 194 | .type = TYPE_EF, \ |
| 195 | .ef_type = ptype, \ |
| 196 | .short_name = pns, \ |
| 197 | .long_name = pnl, \ |
| 198 | .flags = pflags, \ |
| 199 | .ops = { .encode = penc, .parse = pdec }, \ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 200 | .size = { .min = smin, .rec = srec}, \ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 204 | /*! Convenience macros for defining EF */ |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 205 | #define EF_TRANSP(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ |
| 206 | EF(fid, sfi, ns, flags, nl, EF_TYPE_TRANSP, \ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 207 | smin, srec, dec, enc) |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 208 | /*! Convenience macros for defining EF */ |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 209 | #define EF_TRANSP_N(fid, sfi, ns, flags, smin, srec, nl) \ |
| 210 | EF_TRANSP(fid, sfi, ns, flags, smin, srec, \ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 211 | nl, &default_decode, NULL) |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 212 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 213 | /*! Convenience macros for defining EF */ |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 214 | #define EF_CYCLIC(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ |
| 215 | EF(fid, sfi, ns, flags, nl, EF_TYPE_RECORD_CYCLIC, \ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 216 | smin, srec, dec, enc) |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 217 | /*! Convenience macros for defining EF */ |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 218 | #define EF_CYCLIC_N(fid, sfi, ns, flags, smin, srec, nl) \ |
| 219 | EF_CYCLIC(fid, sfi, ns, flags, smin, srec, nl, \ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 220 | &default_decode, NULL) |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 221 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 222 | /*! Convenience macros for defining EF */ |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 223 | #define EF_LIN_FIX(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ |
| 224 | EF(fid, sfi, ns, flags, nl, EF_TYPE_RECORD_FIXED, \ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 225 | smin, srec, dec, enc) |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 226 | /*! Convenience macros for defining EF */ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 227 | #define EF_LIN_FIX_N(fid, sfi, ns, flags, smin, srec, nl) \ |
| 228 | EF_LIN_FIX(fid, sfi, ns, flags, smin, srec, nl, \ |
| 229 | &default_decode, NULL) |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 230 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 231 | /*! Convenience macros for defining EF */ |
Harald Welte | aad7e06 | 2014-05-04 16:31:24 +0200 | [diff] [blame] | 232 | #define EF_KEY(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ |
| 233 | EF(fid, sfi, ns, flags, nl, EF_TYPE_KEY, \ |
| 234 | smin, srec, dec, enc) |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 235 | /*! Convenience macros for defining EF */ |
Harald Welte | aad7e06 | 2014-05-04 16:31:24 +0200 | [diff] [blame] | 236 | #define EF_KEY_N(fid, sfi, ns, flags, smin, srec, nl) \ |
| 237 | EF_KEY(fid, sfi, ns, flags, smin, srec, nl, \ |
| 238 | &default_decode, NULL) |
| 239 | |
| 240 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 241 | struct osim_file_desc * |
Harald Welte | 5ffb503 | 2016-03-11 09:40:56 +0700 | [diff] [blame] | 242 | osim_file_desc_find_name(struct osim_file_desc *parent, const char *name); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 243 | |
Harald Welte | c28f4cd | 2016-03-11 09:35:07 +0700 | [diff] [blame] | 244 | struct osim_file_desc * |
Harald Welte | 5ffb503 | 2016-03-11 09:40:56 +0700 | [diff] [blame] | 245 | osim_file_desc_find_fid(struct osim_file_desc *parent, uint16_t fid); |
Harald Welte | c28f4cd | 2016-03-11 09:35:07 +0700 | [diff] [blame] | 246 | |
| 247 | struct osim_file_desc * |
Harald Welte | 5ffb503 | 2016-03-11 09:40:56 +0700 | [diff] [blame] | 248 | osim_file_desc_find_sfid(struct osim_file_desc *parent, uint8_t sfid); |
Harald Welte | c28f4cd | 2016-03-11 09:35:07 +0700 | [diff] [blame] | 249 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 250 | /* STATUS WORDS */ |
| 251 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 252 | enum osim_card_sw_type { |
| 253 | SW_TYPE_NONE, |
| 254 | SW_TYPE_STR, |
| 255 | }; |
| 256 | |
| 257 | enum osim_card_sw_class { |
| 258 | SW_CLS_NONE, |
| 259 | SW_CLS_OK, |
| 260 | SW_CLS_POSTP, |
| 261 | SW_CLS_WARN, |
| 262 | SW_CLS_ERROR, |
| 263 | }; |
| 264 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 265 | /*! A card status word (SW) */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 266 | struct osim_card_sw { |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 267 | /*! status word code (2 bytes) */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 268 | uint16_t code; |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 269 | /*! status word mask (2 bytes), to match range/prefix of SW */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 270 | uint16_t mask; |
| 271 | enum osim_card_sw_type type; |
| 272 | enum osim_card_sw_class class; |
| 273 | union { |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 274 | /*! Human-readable meaning of SW */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 275 | const char *str; |
| 276 | } u; |
| 277 | }; |
| 278 | |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 279 | #define OSIM_CARD_SW_LAST (const struct osim_card_sw) { \ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 280 | .code = 0, .mask = 0, .type = SW_TYPE_NONE, \ |
| 281 | .class = SW_CLS_NONE, .u.str = NULL \ |
| 282 | } |
| 283 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 284 | /*! A card profile (e.g. SIM card */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 285 | struct osim_card_profile { |
| 286 | const char *name; |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 287 | /*! Descriptor for the MF (root directory */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 288 | struct osim_file_desc *mf; |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 289 | /*! Array of pointers to status words */ |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 290 | const struct osim_card_sw **sws; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 291 | }; |
| 292 | |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 293 | const struct osim_card_sw *osim_find_sw(const struct osim_card_profile *cp, |
| 294 | uint16_t sw); |
Harald Welte | d83d296 | 2013-03-04 17:52:33 +0000 | [diff] [blame] | 295 | enum osim_card_sw_class osim_sw_class(const struct osim_card_profile *cp, |
| 296 | uint16_t sw_in); |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 297 | |
| 298 | struct osim_card_hdl; |
| 299 | char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in); |
| 300 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 301 | extern const struct tlv_definition ts102221_fcp_tlv_def; |
Holger Hans Peter Freyther | c2b4458 | 2015-04-11 19:31:03 +0200 | [diff] [blame] | 302 | extern const struct value_string ts102221_fcp_vals[14]; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 303 | |
| 304 | /* 11.1.1.3 */ |
| 305 | enum ts102221_fcp_tag { |
| 306 | UICC_FCP_T_FCP = 0x62, |
| 307 | UICC_FCP_T_FILE_SIZE = 0x80, |
| 308 | UICC_FCP_T_TOT_F_SIZE = 0x81, |
| 309 | UICC_FCP_T_FILE_DESC = 0x82, |
| 310 | UICC_FCP_T_FILE_ID = 0x83, |
| 311 | UICC_FCP_T_DF_NAME = 0x84, |
| 312 | UICC_FCP_T_SFID = 0x88, |
| 313 | UICC_FCP_T_LIFEC_STS = 0x8A, |
| 314 | UICC_FCP_T_SEC_ATTR_REFEXP= 0x8B, |
| 315 | UICC_FCP_T_SEC_ATTR_COMP= 0x8C, |
| 316 | UICC_FCP_T_PROPRIETARY = 0xA5, |
| 317 | UICC_FCP_T_SEC_ATTR_EXP = 0xAB, |
| 318 | UICC_FCP_T_PIN_STS_DO = 0xC6, |
| 319 | }; |
| 320 | |
| 321 | struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1, |
| 322 | uint8_t p2, uint16_t lc, uint16_t le); |
| 323 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 324 | /* CARD READERS */ |
| 325 | |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 326 | enum osim_proto { |
| 327 | OSIM_PROTO_T0 = 0, |
| 328 | OSIM_PROTO_T1 = 1, |
| 329 | }; |
| 330 | |
| 331 | enum osim_reader_driver { |
| 332 | OSIM_READER_DRV_PCSC = 0, |
| 333 | OSIM_READER_DRV_OPENCT = 1, |
| 334 | OSIM_READER_DRV_SERIAL = 2, |
| 335 | }; |
| 336 | |
Harald Welte | 932d2d4 | 2016-03-19 21:18:40 +0100 | [diff] [blame] | 337 | struct osim_reader_ops { |
| 338 | const char *name; |
| 339 | struct osim_reader_hdl *(*reader_open)(int idx, const char *name, void *ctx); |
| 340 | struct osim_card_hdl *(*card_open)(struct osim_reader_hdl *rh, enum osim_proto proto); |
| 341 | int (*transceive)(struct osim_reader_hdl *rh, struct msgb *msg); |
| 342 | }; |
| 343 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 344 | struct osim_reader_hdl { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 345 | /*! member in global list of readers */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 346 | struct llist_head list; |
Harald Welte | 67354b1 | 2014-10-26 18:47:30 +0100 | [diff] [blame] | 347 | const struct osim_reader_ops *ops; |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 348 | uint32_t proto_supported; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 349 | void *priv; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 350 | /*! current card, if any */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 351 | struct osim_card_hdl *card; |
| 352 | }; |
| 353 | |
| 354 | struct osim_card_hdl { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 355 | /*! member in global list of cards */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 356 | struct llist_head list; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 357 | /*! reader through which card is accessed */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 358 | struct osim_reader_hdl *reader; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 359 | /*! card profile */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 360 | struct osim_card_profile *prof; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 361 | /*! card protocol */ |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 362 | enum osim_proto proto; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 363 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 364 | /*! list of channels for this card */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 365 | struct llist_head channels; |
| 366 | }; |
| 367 | |
| 368 | struct osim_chan_hdl { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 369 | /*! linked to card->channels */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 370 | struct llist_head list; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 371 | /*! card to which this channel belongs */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 372 | struct osim_card_hdl *card; |
| 373 | const struct osim_file_desc *cwd; |
| 374 | }; |
| 375 | |
| 376 | /* reader.c */ |
| 377 | int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg); |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 378 | struct osim_reader_hdl *osim_reader_open(enum osim_reader_driver drv, int idx, |
| 379 | const char *name, void *ctx); |
| 380 | 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] | 381 | #endif /* _OSMOCOM_SIM_H */ |