blob: 639682a008f9cb1282c8ab8ea5f4a5be9a9461a3 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file sim.h
2 * Routines for helping with SIM (ISO/IEC 7816-4 more generally) communication.
3 */
4
Vadim Yanitskiy395db202022-09-28 23:51:42 +07005#pragma once
Harald Welted54c2ee2012-01-17 18:25:50 +01006
7#include <osmocom/core/msgb.h>
8#include <osmocom/core/linuxlist.h>
9
10#define APDU_HDR_LEN 5
Harald Welte429adec2020-03-20 13:05:40 +010011#define MAX_AID_LEN 16 /* Table 13.2 of TS 102 221 */
Harald Welted54c2ee2012-01-17 18:25:50 +010012
Harald Welte22117a72021-04-25 20:50:13 +020013
14/*! Maximum Answer-To-Reset (ATR) size in bytes
15 * @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
16 * @remark technical there is no size limitation since Yi present in T0,TDi will indicate if more interface bytes are present, including TDi+i
17 */
18#define OSIM_MAX_ATR_LEN 33
19
Neels Hofmeyr87e45502017-06-20 00:17:59 +020020/*! command-response pairs cases
Kevin Redon43eabee2012-09-16 18:40:02 +020021 *
22 * Enumeration used to identify the APDU structure based on command-response pair case , as specified in ISO/IEC 7816-3:2006(E) §12.1.
23 */
Harald Welted54c2ee2012-01-17 18:25:50 +010024enum osim_apdu_case {
Kevin Redon43eabee2012-09-16 18:40:02 +020025 APDU_CASE_1, /*!< command header, no command data field, no response data field */
26 APDU_CASE_2S, /*!< command header, no command data field, response data field (short) */
27 APDU_CASE_2E, /*!< command header, no command data field, response data field (extended) */
28 APDU_CASE_3S, /*!< command header, command data field (short), no response data field */
29 APDU_CASE_3E, /*!< command header, command data field (extended), no response data field */
30 APDU_CASE_4S, /*!< command header, command data field (short), response data field (short) */
31 APDU_CASE_4E /*!< command header, command data field (extended), response data field (extended) */
Harald Welted54c2ee2012-01-17 18:25:50 +010032};
33
Neels Hofmeyr87e45502017-06-20 00:17:59 +020034/*! APDU/TPDU command header
Kevin Redon43eabee2012-09-16 18:40:02 +020035 *
36 * This structure encode an APDU/TPDU command header, as specified in ISO/IEC 7816-3:2006(E) §12.2 and §12.3.
37 * The APDU (application layer) can be encoded as different TPDUs (transport layer), depending on the transport protocol used.
38 * The TPDU encoding by T=1 of the APDU command header is identical to the APDU.
39 * The TPDU encoding by T=0 of the APDU command header adds a Parameter 3 field, generally used instead of Lc/Le.
40 *
41 * @todo have different structures for APDU, TPDU by T=0, and TPDU by T=1.
42 */
Harald Welted54c2ee2012-01-17 18:25:50 +010043struct osim_apdu_cmd_hdr {
Kevin Redon43eabee2012-09-16 18:40:02 +020044 uint8_t cla; /*!< CLASS byte */
45 uint8_t ins; /*!< INSTRUCTION byte */
46 uint8_t p1; /*!< Parameter 1 byte */
47 uint8_t p2; /*!< Parameter 2 byte */
48 uint8_t p3; /*!< Parameter 3 byte, used for TPDU by T=0 */
Harald Welted54c2ee2012-01-17 18:25:50 +010049} __attribute__ ((packed));
50
51#define msgb_apdu_dr(__x)
52
Neels Hofmeyr87e45502017-06-20 00:17:59 +020053/*! APDU command body
Kevin Redon43eabee2012-09-16 18:40:02 +020054 *
55 * This structure encode a command body, as specified in ISO/IEC 7816-3:2006(E) §12.1.
56 * The data and response contents should be provided along with this structure.
57 */
Harald Welted54c2ee2012-01-17 18:25:50 +010058struct osim_msgb_cb {
Kevin Redon43eabee2012-09-16 18:40:02 +020059 enum osim_apdu_case apduc; /*!< command-response pair case, defining the encoding of Lc and Le */
60 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 */
61 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 */
62 uint16_t sw; /*!< status word, composed of SW1 and SW2 bytes */
Harald Weltee8dd2bd2014-05-04 13:42:38 +020063} __attribute__((__may_alias__));
Harald Welted54c2ee2012-01-17 18:25:50 +010064#define OSIM_MSGB_CB(__msgb) ((struct osim_msgb_cb *)&((__msgb)->cb[0]))
Neels Hofmeyr87e45502017-06-20 00:17:59 +020065/*! status word from msgb->cb */
Harald Welted54c2ee2012-01-17 18:25:50 +010066#define msgb_apdu_case(__x) OSIM_MSGB_CB(__x)->apduc
67#define msgb_apdu_lc(__x) OSIM_MSGB_CB(__x)->lc
68#define msgb_apdu_le(__x) OSIM_MSGB_CB(__x)->le
69#define msgb_apdu_sw(__x) OSIM_MSGB_CB(__x)->sw
Neels Hofmeyr87e45502017-06-20 00:17:59 +020070/*! pointer to the command header of the APDU */
Harald Welted54c2ee2012-01-17 18:25:50 +010071#define msgb_apdu_h(__x) ((struct osim_apdu_cmd_hdr *)(__x)->l2h)
72
73#define msgb_apdu_dc(__x) ((__x)->l2h + sizeof(struct osim_apdu_cmd_hdr))
74#define msgb_apdu_de(__x) ((__x)->l2h + sizeof(struct osim_apdu_cmd_hdr) + msgb_apdu_lc(__x))
75
Harald Welte429adec2020-03-20 13:05:40 +010076int osim_init(void *ctx);
77
Harald Welte586d7102014-05-04 17:01:16 +020078/* FILES */
79
Harald Welted54c2ee2012-01-17 18:25:50 +010080struct osim_file;
81struct osim_file_desc;
82struct osim_decoded_data;
83
Neels Hofmeyr87e45502017-06-20 00:17:59 +020084/*! Operations for a given File */
Harald Welted54c2ee2012-01-17 18:25:50 +010085struct osim_file_ops {
Harald Welte586d7102014-05-04 17:01:16 +020086 /*! Parse binary file data into osim_decoded_data */
Harald Welted54c2ee2012-01-17 18:25:50 +010087 int (*parse)(struct osim_decoded_data *dd,
88 const struct osim_file_desc *desc,
89 int len, uint8_t *data);
Harald Welte586d7102014-05-04 17:01:16 +020090 /*! Encode osim_decoded_data into binary file */
Harald Welte6729a972014-10-26 19:04:56 +010091 struct msgb * (*encode)(const struct osim_file_desc *desc,
Harald Welted54c2ee2012-01-17 18:25:50 +010092 const struct osim_decoded_data *decoded);
93};
94
95enum osim_element_type {
96 ELEM_T_NONE,
97 ELEM_T_BOOL, /*!< a boolean flag */
98 ELEM_T_UINT8, /*!< unsigned integer */
99 ELEM_T_UINT16, /*!< unsigned integer */
100 ELEM_T_UINT32, /*!< unsigned integer */
101 ELEM_T_STRING, /*!< generic string */
102 ELEM_T_BCD, /*!< BCD encoded digits */
103 ELEM_T_BYTES, /*!< BCD encoded digits */
104 ELEM_T_GROUP, /*!< group container, has siblings */
105};
106
107enum osim_element_repr {
108 ELEM_REPR_NONE,
109 ELEM_REPR_DEC,
110 ELEM_REPR_HEX,
111};
112
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200113/*! A single decoded element inside a file */
Harald Welted54c2ee2012-01-17 18:25:50 +0100114struct osim_decoded_element {
115 struct llist_head list;
116
117 enum osim_element_type type;
118 enum osim_element_repr representation;
119 const char *name;
120
121 unsigned int length;
122 union {
123 uint8_t u8;
124 uint16_t u16;
125 uint32_t u32;
126 uint8_t *buf;
Harald Welte586d7102014-05-04 17:01:16 +0200127 /*! A list of sibling decoded_items */
Harald Welted54c2ee2012-01-17 18:25:50 +0100128 struct llist_head siblings;
129 } u;
130};
131
Harald Welte586d7102014-05-04 17:01:16 +0200132/*! Decoded data for a single file, consisting of all decoded elements */
Harald Welted54c2ee2012-01-17 18:25:50 +0100133struct osim_decoded_data {
134 /*! file to which we belong */
135 const struct osim_file *file;
136 /*! list of 'struct decoded_element' */
137 struct llist_head decoded_elements;
138};
139
140
141enum osim_file_type {
142 TYPE_NONE,
Harald Welte586d7102014-05-04 17:01:16 +0200143 TYPE_DF, /*!< Dedicated File */
144 TYPE_ADF, /*!< Application Dedicated File */
145 TYPE_EF, /*!< Entry File */
146 TYPE_EF_INT, /*!< Internal Entry File */
Harald Welte053bebc2020-02-15 18:58:16 +0100147 TYPE_MF, /*!< Master File */
Harald Welted54c2ee2012-01-17 18:25:50 +0100148};
149
150enum osim_ef_type {
Harald Welte586d7102014-05-04 17:01:16 +0200151 EF_TYPE_TRANSP, /*!< Transparent EF */
152 EF_TYPE_RECORD_FIXED, /*!< Fixed-Size Record EF */
153 EF_TYPE_RECORD_CYCLIC, /*!< Cyclic Record EF */
154 EF_TYPE_KEY, /*!< Key file as used in TETRA */
Harald Welted54c2ee2012-01-17 18:25:50 +0100155};
156
157#define F_OPTIONAL 0x0001
158
Harald Welte2656e652014-05-03 14:23:44 +0200159#define SFI_NONE 0xFF
160
Harald Welted54c2ee2012-01-17 18:25:50 +0100161struct osim_file_desc {
162 struct llist_head list; /*!< local element in list */
163 struct llist_head child_list; /*!< list of children EF in DF */
164 struct osim_file_desc *parent; /*!< parent DF */
165
Harald Welte586d7102014-05-04 17:01:16 +0200166 enum osim_file_type type; /*!< Type of the file (EF, DF, ...) */
167 enum osim_ef_type ef_type; /*!< Type of the EF, if type == TYPE_EF */
Harald Welted54c2ee2012-01-17 18:25:50 +0100168
169 uint16_t fid; /*!< File Identifier */
170 uint8_t sfid; /*!< Short File IDentifier */
Harald Welte6729a972014-10-26 19:04:56 +0100171 const uint8_t *df_name;
Harald Welted54c2ee2012-01-17 18:25:50 +0100172 uint8_t df_name_len;
173
174 const char *short_name; /*!< Short Name (like EF.ICCID) */
175 const char *long_name; /*!< Long / description */
176 unsigned int flags;
177
Harald Welte586d7102014-05-04 17:01:16 +0200178 struct osim_file_ops ops; /*!< Operations (parse/encode */
Harald Welte1e0dfda2014-05-03 14:22:12 +0200179
180 struct {
181 size_t min; /*!< Minimum size of the file
182 (transparent) or record in
183 cyclic / linear file */
184 size_t rec; /*!< Recommended size */
185 } size;
Harald Welted54c2ee2012-01-17 18:25:50 +0100186};
187
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200188/*! A single instance of a file: Descriptor and contents */
Harald Welted54c2ee2012-01-17 18:25:50 +0100189struct osim_file {
Harald Welte586d7102014-05-04 17:01:16 +0200190 /*! Descriptor for the file */
Harald Welted54c2ee2012-01-17 18:25:50 +0100191 const struct osim_file_desc *desc;
192
Harald Welte586d7102014-05-04 17:01:16 +0200193 /*! Encoded file contents */
Harald Welted54c2ee2012-01-17 18:25:50 +0100194 struct msgb *encoded_data;
Harald Welte586d7102014-05-04 17:01:16 +0200195 /*! Parsed/Decoded file contents */
Harald Welted54c2ee2012-01-17 18:25:50 +0100196 struct osim_decoded_data *decoded_data;
197};
198
Harald Welte586d7102014-05-04 17:01:16 +0200199/*! Convenience macros for defining EF */
Harald Welte2656e652014-05-03 14:23:44 +0200200#define EF(pfid, sfi, pns, pflags, pnl, ptype, smin, srec, pdec, penc) \
Harald Welted54c2ee2012-01-17 18:25:50 +0100201 { \
202 .fid = pfid, \
Harald Welte2656e652014-05-03 14:23:44 +0200203 .sfid = sfi, \
Harald Welted54c2ee2012-01-17 18:25:50 +0100204 .type = TYPE_EF, \
205 .ef_type = ptype, \
206 .short_name = pns, \
207 .long_name = pnl, \
208 .flags = pflags, \
209 .ops = { .encode = penc, .parse = pdec }, \
Harald Welte1e0dfda2014-05-03 14:22:12 +0200210 .size = { .min = smin, .rec = srec}, \
Harald Welted54c2ee2012-01-17 18:25:50 +0100211 }
212
213
Harald Welte586d7102014-05-04 17:01:16 +0200214/*! Convenience macros for defining EF */
Harald Welte2656e652014-05-03 14:23:44 +0200215#define EF_TRANSP(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \
216 EF(fid, sfi, ns, flags, nl, EF_TYPE_TRANSP, \
Harald Welte1e0dfda2014-05-03 14:22:12 +0200217 smin, srec, dec, enc)
Harald Welte586d7102014-05-04 17:01:16 +0200218/*! Convenience macros for defining EF */
Harald Welte2656e652014-05-03 14:23:44 +0200219#define EF_TRANSP_N(fid, sfi, ns, flags, smin, srec, nl) \
220 EF_TRANSP(fid, sfi, ns, flags, smin, srec, \
Harald Welte1e0dfda2014-05-03 14:22:12 +0200221 nl, &default_decode, NULL)
Harald Welted54c2ee2012-01-17 18:25:50 +0100222
Harald Welte586d7102014-05-04 17:01:16 +0200223/*! Convenience macros for defining EF */
Harald Welte2656e652014-05-03 14:23:44 +0200224#define EF_CYCLIC(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \
225 EF(fid, sfi, ns, flags, nl, EF_TYPE_RECORD_CYCLIC, \
Harald Welte1e0dfda2014-05-03 14:22:12 +0200226 smin, srec, dec, enc)
Harald Welte586d7102014-05-04 17:01:16 +0200227/*! Convenience macros for defining EF */
Harald Welte2656e652014-05-03 14:23:44 +0200228#define EF_CYCLIC_N(fid, sfi, ns, flags, smin, srec, nl) \
229 EF_CYCLIC(fid, sfi, ns, flags, smin, srec, nl, \
Harald Welte1e0dfda2014-05-03 14:22:12 +0200230 &default_decode, NULL)
Harald Welted54c2ee2012-01-17 18:25:50 +0100231
Harald Welte586d7102014-05-04 17:01:16 +0200232/*! Convenience macros for defining EF */
Harald Welte2656e652014-05-03 14:23:44 +0200233#define EF_LIN_FIX(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \
234 EF(fid, sfi, ns, flags, nl, EF_TYPE_RECORD_FIXED, \
Harald Welte1e0dfda2014-05-03 14:22:12 +0200235 smin, srec, dec, enc)
Harald Welte586d7102014-05-04 17:01:16 +0200236/*! Convenience macros for defining EF */
Harald Welte1e0dfda2014-05-03 14:22:12 +0200237#define EF_LIN_FIX_N(fid, sfi, ns, flags, smin, srec, nl) \
238 EF_LIN_FIX(fid, sfi, ns, flags, smin, srec, nl, \
239 &default_decode, NULL)
Harald Welted54c2ee2012-01-17 18:25:50 +0100240
Harald Welte586d7102014-05-04 17:01:16 +0200241/*! Convenience macros for defining EF */
Harald Welteaad7e062014-05-04 16:31:24 +0200242#define EF_KEY(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \
243 EF(fid, sfi, ns, flags, nl, EF_TYPE_KEY, \
244 smin, srec, dec, enc)
Harald Welte586d7102014-05-04 17:01:16 +0200245/*! Convenience macros for defining EF */
Harald Welteaad7e062014-05-04 16:31:24 +0200246#define EF_KEY_N(fid, sfi, ns, flags, smin, srec, nl) \
247 EF_KEY(fid, sfi, ns, flags, smin, srec, nl, \
248 &default_decode, NULL)
249
250
Harald Welted54c2ee2012-01-17 18:25:50 +0100251struct osim_file_desc *
Harald Welte5ffb5032016-03-11 09:40:56 +0700252osim_file_desc_find_name(struct osim_file_desc *parent, const char *name);
Harald Welted54c2ee2012-01-17 18:25:50 +0100253
Harald Weltec28f4cd2016-03-11 09:35:07 +0700254struct osim_file_desc *
Harald Weltebf90d742020-03-20 12:11:03 +0100255osim_file_desc_find_aid(struct osim_file_desc *parent, const uint8_t *aid, uint8_t aid_len);
256
257struct osim_file_desc *
Harald Welte5ffb5032016-03-11 09:40:56 +0700258osim_file_desc_find_fid(struct osim_file_desc *parent, uint16_t fid);
Harald Weltec28f4cd2016-03-11 09:35:07 +0700259
260struct osim_file_desc *
Harald Welte5ffb5032016-03-11 09:40:56 +0700261osim_file_desc_find_sfid(struct osim_file_desc *parent, uint8_t sfid);
Harald Weltec28f4cd2016-03-11 09:35:07 +0700262
Harald Welte586d7102014-05-04 17:01:16 +0200263/* STATUS WORDS */
264
Harald Welted54c2ee2012-01-17 18:25:50 +0100265enum osim_card_sw_type {
266 SW_TYPE_NONE,
267 SW_TYPE_STR,
268};
269
270enum osim_card_sw_class {
271 SW_CLS_NONE,
272 SW_CLS_OK,
273 SW_CLS_POSTP,
274 SW_CLS_WARN,
275 SW_CLS_ERROR,
276};
277
Harald Welte586d7102014-05-04 17:01:16 +0200278/*! A card status word (SW) */
Harald Welted54c2ee2012-01-17 18:25:50 +0100279struct osim_card_sw {
Harald Welte586d7102014-05-04 17:01:16 +0200280 /*! status word code (2 bytes) */
Harald Welted54c2ee2012-01-17 18:25:50 +0100281 uint16_t code;
Harald Welte586d7102014-05-04 17:01:16 +0200282 /*! status word mask (2 bytes), to match range/prefix of SW */
Harald Welted54c2ee2012-01-17 18:25:50 +0100283 uint16_t mask;
284 enum osim_card_sw_type type;
285 enum osim_card_sw_class class;
286 union {
Harald Welte586d7102014-05-04 17:01:16 +0200287 /*! Human-readable meaning of SW */
Harald Welted54c2ee2012-01-17 18:25:50 +0100288 const char *str;
289 } u;
290};
291
Oliver Smithaf916f12021-02-01 09:25:34 +0100292#define OSIM_CARD_SW_LAST { \
Harald Welted54c2ee2012-01-17 18:25:50 +0100293 .code = 0, .mask = 0, .type = SW_TYPE_NONE, \
294 .class = SW_CLS_NONE, .u.str = NULL \
295}
296
Harald Welte429adec2020-03-20 13:05:40 +0100297/*! A card application (e.g. USIM, ISIM, HPSIM) */
298struct osim_card_app_profile {
299 /*! entry in the global list of card application profiles */
300 struct llist_head list;
301 /*! human-readable name */
302 const char *name;
303 /*! AID of this application, as used in EF.DIR */
304 uint8_t aid[MAX_AID_LEN];
305 uint8_t aid_len;
306 /*! file system description */
307 struct osim_file_desc *adf;
308 /*! Status words defined by application */
309 const struct osim_card_sw *sw;
310};
311
312const struct osim_card_app_profile *
313osim_app_profile_find_by_name(const char *name);
314
315const struct osim_card_app_profile *
316osim_app_profile_find_by_aid(const uint8_t *aid, uint8_t aid_len);
317
Harald Welte3a6bedf2020-03-22 10:30:10 +0100318const struct osim_card_sw *osim_app_profile_find_sw(const struct osim_card_app_profile *ap, uint16_t sw_in);
Harald Welte429adec2020-03-20 13:05:40 +0100319
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200320/*! A card profile (e.g. SIM card */
Harald Welted54c2ee2012-01-17 18:25:50 +0100321struct osim_card_profile {
322 const char *name;
Harald Welte586d7102014-05-04 17:01:16 +0200323 /*! Descriptor for the MF (root directory */
Harald Welted54c2ee2012-01-17 18:25:50 +0100324 struct osim_file_desc *mf;
Harald Welte586d7102014-05-04 17:01:16 +0200325 /*! Array of pointers to status words */
Harald Welte76749602012-09-19 20:55:54 +0200326 const struct osim_card_sw **sws;
Harald Welted54c2ee2012-01-17 18:25:50 +0100327};
328
Harald Welte3a6bedf2020-03-22 10:30:10 +0100329const struct osim_card_sw *osim_cprof_find_sw(const struct osim_card_profile *cp, uint16_t sw_in);
Harald Welte76749602012-09-19 20:55:54 +0200330
Harald Welte3a6bedf2020-03-22 10:30:10 +0100331struct osim_chan_hdl;
332enum osim_card_sw_class osim_sw_class(const struct osim_chan_hdl *ch, uint16_t sw_in);
333char *osim_print_sw_buf(char *buf, size_t buf_len, const struct osim_chan_hdl *ch, uint16_t sw_in);
334char *osim_print_sw(const struct osim_chan_hdl *ch, uint16_t sw_in);
335char *osim_print_sw_c(const void *ctx, const struct osim_chan_hdl *ch, uint16_t sw_in);
Harald Welte76749602012-09-19 20:55:54 +0200336
Harald Welted54c2ee2012-01-17 18:25:50 +0100337extern const struct tlv_definition ts102221_fcp_tlv_def;
Holger Hans Peter Freytherc2b44582015-04-11 19:31:03 +0200338extern const struct value_string ts102221_fcp_vals[14];
Harald Welted54c2ee2012-01-17 18:25:50 +0100339
340/* 11.1.1.3 */
341enum ts102221_fcp_tag {
342 UICC_FCP_T_FCP = 0x62,
343 UICC_FCP_T_FILE_SIZE = 0x80,
344 UICC_FCP_T_TOT_F_SIZE = 0x81,
345 UICC_FCP_T_FILE_DESC = 0x82,
346 UICC_FCP_T_FILE_ID = 0x83,
347 UICC_FCP_T_DF_NAME = 0x84,
348 UICC_FCP_T_SFID = 0x88,
349 UICC_FCP_T_LIFEC_STS = 0x8A,
350 UICC_FCP_T_SEC_ATTR_REFEXP= 0x8B,
351 UICC_FCP_T_SEC_ATTR_COMP= 0x8C,
352 UICC_FCP_T_PROPRIETARY = 0xA5,
353 UICC_FCP_T_SEC_ATTR_EXP = 0xAB,
354 UICC_FCP_T_PIN_STS_DO = 0xC6,
355};
356
357struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1,
358 uint8_t p2, uint16_t lc, uint16_t le);
359
Harald Welte586d7102014-05-04 17:01:16 +0200360/* CARD READERS */
361
Harald Welte55790aa2014-10-26 18:46:50 +0100362enum osim_proto {
363 OSIM_PROTO_T0 = 0,
364 OSIM_PROTO_T1 = 1,
365};
366
367enum osim_reader_driver {
368 OSIM_READER_DRV_PCSC = 0,
369 OSIM_READER_DRV_OPENCT = 1,
370 OSIM_READER_DRV_SERIAL = 2,
371};
372
Harald Welte932d2d42016-03-19 21:18:40 +0100373struct osim_reader_ops {
374 const char *name;
375 struct osim_reader_hdl *(*reader_open)(int idx, const char *name, void *ctx);
376 struct osim_card_hdl *(*card_open)(struct osim_reader_hdl *rh, enum osim_proto proto);
Harald Welte20199da2021-06-01 20:11:19 +0200377 int (*card_reset)(struct osim_card_hdl *card, bool cold_reset);
378 int (*card_close)(struct osim_card_hdl *card);
Harald Welte932d2d42016-03-19 21:18:40 +0100379 int (*transceive)(struct osim_reader_hdl *rh, struct msgb *msg);
380};
381
Harald Welted54c2ee2012-01-17 18:25:50 +0100382struct osim_reader_hdl {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200383 /*! member in global list of readers */
Harald Welted54c2ee2012-01-17 18:25:50 +0100384 struct llist_head list;
Harald Welte67354b12014-10-26 18:47:30 +0100385 const struct osim_reader_ops *ops;
Harald Welte55790aa2014-10-26 18:46:50 +0100386 uint32_t proto_supported;
Harald Welted54c2ee2012-01-17 18:25:50 +0100387 void *priv;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200388 /*! current card, if any */
Harald Welted54c2ee2012-01-17 18:25:50 +0100389 struct osim_card_hdl *card;
390};
391
Harald Welte429adec2020-03-20 13:05:40 +0100392/*! descriptor for a given application present on a card */
393struct osim_card_app_hdl {
394 /*! member in card list of applications */
395 struct llist_head list;
396 /*! AID of the application */
397 uint8_t aid[MAX_AID_LEN];
398 uint8_t aid_len;
399 /*! application label from EF_DIR */
400 char *label;
401 /*! application profile (if any known) */
402 const struct osim_card_app_profile *prof;
403};
404
Harald Welted54c2ee2012-01-17 18:25:50 +0100405struct osim_card_hdl {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200406 /*! member in global list of cards */
Harald Welted54c2ee2012-01-17 18:25:50 +0100407 struct llist_head list;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200408 /*! reader through which card is accessed */
Harald Welted54c2ee2012-01-17 18:25:50 +0100409 struct osim_reader_hdl *reader;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200410 /*! card profile */
Harald Welted54c2ee2012-01-17 18:25:50 +0100411 struct osim_card_profile *prof;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200412 /*! card protocol */
Harald Welte55790aa2014-10-26 18:46:50 +0100413 enum osim_proto proto;
Harald Welted54c2ee2012-01-17 18:25:50 +0100414
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200415 /*! list of channels for this card */
Harald Welted54c2ee2012-01-17 18:25:50 +0100416 struct llist_head channels;
Harald Welte429adec2020-03-20 13:05:40 +0100417
418 /*! list of applications found on card */
419 struct llist_head apps;
Harald Welte22117a72021-04-25 20:50:13 +0200420
421 /*! ATR (Answer To Reset) of the card */
422 uint8_t atr[OSIM_MAX_ATR_LEN];
423 unsigned int atr_len;
Harald Welted54c2ee2012-01-17 18:25:50 +0100424};
425
426struct osim_chan_hdl {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200427 /*! linked to card->channels */
Harald Welted54c2ee2012-01-17 18:25:50 +0100428 struct llist_head list;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200429 /*! card to which this channel belongs */
Harald Welted54c2ee2012-01-17 18:25:50 +0100430 struct osim_card_hdl *card;
Harald Welte429adec2020-03-20 13:05:40 +0100431 /*! current working directory */
Harald Welted54c2ee2012-01-17 18:25:50 +0100432 const struct osim_file_desc *cwd;
Harald Welte429adec2020-03-20 13:05:40 +0100433 /*! currently selected application (if any) */
434 struct osim_card_app_hdl *cur_app;
Harald Welted54c2ee2012-01-17 18:25:50 +0100435};
436
Harald Welte429adec2020-03-20 13:05:40 +0100437int osim_card_hdl_add_app(struct osim_card_hdl *ch, const uint8_t *aid, uint8_t aid_len,
438 const char *label);
439
Harald Welted54c2ee2012-01-17 18:25:50 +0100440/* reader.c */
441int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg);
Harald Welte55790aa2014-10-26 18:46:50 +0100442struct osim_reader_hdl *osim_reader_open(enum osim_reader_driver drv, int idx,
443 const char *name, void *ctx);
444struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh, enum osim_proto proto);
Harald Welte20199da2021-06-01 20:11:19 +0200445int osim_card_reset(struct osim_card_hdl *card, bool cold_reset);
446int osim_card_close(struct osim_card_hdl *card);