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