blob: 7549df6494fad6d3cc55f9f29e2b362671421a0c [file] [log] [blame]
Harald Welted54c2ee2012-01-17 18:25:50 +01001#ifndef _OSMOCOM_SIM_H
2#define _OSMOCOM_SIM_H
3
4#include <osmocom/core/msgb.h>
5#include <osmocom/core/linuxlist.h>
6
7#define APDU_HDR_LEN 5
8
Kevin Redon43eabee2012-09-16 18:40:02 +02009/*!
10 * \file sim.h
11 * \brief Routines for helping with SIM (ISO/IEC 7816-4 more generally) communication.
12 */
13
14/*! \brief command-response pairs cases
15 *
16 * Enumeration used to identify the APDU structure based on command-response pair case , as specified in ISO/IEC 7816-3:2006(E) §12.1.
17 */
Harald Welted54c2ee2012-01-17 18:25:50 +010018enum osim_apdu_case {
Kevin Redon43eabee2012-09-16 18:40:02 +020019 APDU_CASE_1, /*!< command header, no command data field, no response data field */
20 APDU_CASE_2S, /*!< command header, no command data field, response data field (short) */
21 APDU_CASE_2E, /*!< command header, no command data field, response data field (extended) */
22 APDU_CASE_3S, /*!< command header, command data field (short), no response data field */
23 APDU_CASE_3E, /*!< command header, command data field (extended), no response data field */
24 APDU_CASE_4S, /*!< command header, command data field (short), response data field (short) */
25 APDU_CASE_4E /*!< command header, command data field (extended), response data field (extended) */
Harald Welted54c2ee2012-01-17 18:25:50 +010026};
27
Kevin Redon43eabee2012-09-16 18:40:02 +020028/*! \brief APDU/TPDU command header
29 *
30 * This structure encode an APDU/TPDU command header, as specified in ISO/IEC 7816-3:2006(E) §12.2 and §12.3.
31 * The APDU (application layer) can be encoded as different TPDUs (transport layer), depending on the transport protocol used.
32 * The TPDU encoding by T=1 of the APDU command header is identical to the APDU.
33 * The TPDU encoding by T=0 of the APDU command header adds a Parameter 3 field, generally used instead of Lc/Le.
34 *
35 * @todo have different structures for APDU, TPDU by T=0, and TPDU by T=1.
36 */
Harald Welted54c2ee2012-01-17 18:25:50 +010037struct osim_apdu_cmd_hdr {
Kevin Redon43eabee2012-09-16 18:40:02 +020038 uint8_t cla; /*!< CLASS byte */
39 uint8_t ins; /*!< INSTRUCTION byte */
40 uint8_t p1; /*!< Parameter 1 byte */
41 uint8_t p2; /*!< Parameter 2 byte */
42 uint8_t p3; /*!< Parameter 3 byte, used for TPDU by T=0 */
Harald Welted54c2ee2012-01-17 18:25:50 +010043} __attribute__ ((packed));
44
45#define msgb_apdu_dr(__x)
46
Kevin Redon43eabee2012-09-16 18:40:02 +020047/*! \brief APDU command body
48 *
49 * This structure encode a command body, as specified in ISO/IEC 7816-3:2006(E) §12.1.
50 * The data and response contents should be provided along with this structure.
51 */
Harald Welted54c2ee2012-01-17 18:25:50 +010052struct osim_msgb_cb {
Kevin Redon43eabee2012-09-16 18:40:02 +020053 enum osim_apdu_case apduc; /*!< command-response pair case, defining the encoding of Lc and Le */
54 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 */
55 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 */
56 uint16_t sw; /*!< status word, composed of SW1 and SW2 bytes */
Harald Welted54c2ee2012-01-17 18:25:50 +010057};
58#define OSIM_MSGB_CB(__msgb) ((struct osim_msgb_cb *)&((__msgb)->cb[0]))
59/*! \brief status word from msgb->cb */
60#define msgb_apdu_case(__x) OSIM_MSGB_CB(__x)->apduc
61#define msgb_apdu_lc(__x) OSIM_MSGB_CB(__x)->lc
62#define msgb_apdu_le(__x) OSIM_MSGB_CB(__x)->le
63#define msgb_apdu_sw(__x) OSIM_MSGB_CB(__x)->sw
64/*! \brief pointer to the command header of the APDU */
65#define msgb_apdu_h(__x) ((struct osim_apdu_cmd_hdr *)(__x)->l2h)
66
67#define msgb_apdu_dc(__x) ((__x)->l2h + sizeof(struct osim_apdu_cmd_hdr))
68#define msgb_apdu_de(__x) ((__x)->l2h + sizeof(struct osim_apdu_cmd_hdr) + msgb_apdu_lc(__x))
69
70struct osim_file;
71struct osim_file_desc;
72struct osim_decoded_data;
73
74struct osim_file_ops {
75 int (*parse)(struct osim_decoded_data *dd,
76 const struct osim_file_desc *desc,
77 int len, uint8_t *data);
78 struct msgb * (*encode)(const struct osim_file *file,
79 const struct osim_decoded_data *decoded);
80};
81
82enum osim_element_type {
83 ELEM_T_NONE,
84 ELEM_T_BOOL, /*!< a boolean flag */
85 ELEM_T_UINT8, /*!< unsigned integer */
86 ELEM_T_UINT16, /*!< unsigned integer */
87 ELEM_T_UINT32, /*!< unsigned integer */
88 ELEM_T_STRING, /*!< generic string */
89 ELEM_T_BCD, /*!< BCD encoded digits */
90 ELEM_T_BYTES, /*!< BCD encoded digits */
91 ELEM_T_GROUP, /*!< group container, has siblings */
92};
93
94enum osim_element_repr {
95 ELEM_REPR_NONE,
96 ELEM_REPR_DEC,
97 ELEM_REPR_HEX,
98};
99
100struct osim_decoded_element {
101 struct llist_head list;
102
103 enum osim_element_type type;
104 enum osim_element_repr representation;
105 const char *name;
106
107 unsigned int length;
108 union {
109 uint8_t u8;
110 uint16_t u16;
111 uint32_t u32;
112 uint8_t *buf;
113 struct llist_head siblings;
114 } u;
115};
116
117struct osim_decoded_data {
118 /*! file to which we belong */
119 const struct osim_file *file;
120 /*! list of 'struct decoded_element' */
121 struct llist_head decoded_elements;
122};
123
124
125enum osim_file_type {
126 TYPE_NONE,
127 TYPE_DF,
128 TYPE_ADF,
129 TYPE_EF,
130 TYPE_EF_INT,
131};
132
133enum osim_ef_type {
134 EF_TYPE_TRANSP,
135 EF_TYPE_RECORD_FIXED,
136 EF_TYPE_RECORD_CYCLIC,
137};
138
139#define F_OPTIONAL 0x0001
140
141struct osim_file_desc {
142 struct llist_head list; /*!< local element in list */
143 struct llist_head child_list; /*!< list of children EF in DF */
144 struct osim_file_desc *parent; /*!< parent DF */
145
146 enum osim_file_type type;
147 enum osim_ef_type ef_type;
148
149 uint16_t fid; /*!< File Identifier */
150 uint8_t sfid; /*!< Short File IDentifier */
151 const char *df_name;
152 uint8_t df_name_len;
153
154 const char *short_name; /*!< Short Name (like EF.ICCID) */
155 const char *long_name; /*!< Long / description */
156 unsigned int flags;
157
158 struct osim_file_ops ops;
Harald Welte1e0dfda2014-05-03 14:22:12 +0200159
160 struct {
161 size_t min; /*!< Minimum size of the file
162 (transparent) or record in
163 cyclic / linear file */
164 size_t rec; /*!< Recommended size */
165 } size;
Harald Welted54c2ee2012-01-17 18:25:50 +0100166};
167
168struct osim_file {
169 const struct osim_file_desc *desc;
170
171 struct msgb *encoded_data;
172 struct osim_decoded_data *decoded_data;
173};
174
Harald Welte1e0dfda2014-05-03 14:22:12 +0200175#define EF(pfid, pns, pflags, pnl, ptype, smin, srec, pdec, penc) \
Harald Welted54c2ee2012-01-17 18:25:50 +0100176 { \
177 .fid = pfid, \
178 .type = TYPE_EF, \
179 .ef_type = ptype, \
180 .short_name = pns, \
181 .long_name = pnl, \
182 .flags = pflags, \
183 .ops = { .encode = penc, .parse = pdec }, \
Harald Welte1e0dfda2014-05-03 14:22:12 +0200184 .size = { .min = smin, .rec = srec}, \
Harald Welted54c2ee2012-01-17 18:25:50 +0100185 }
186
187
Harald Welte1e0dfda2014-05-03 14:22:12 +0200188#define EF_TRANSP(fid, ns, flags, smin, srec, nl, dec, enc) \
189 EF(fid, ns, flags, nl, EF_TYPE_TRANSP, \
190 smin, srec, dec, enc)
191#define EF_TRANSP_N(fid, ns, flags, smin, srec, nl) \
192 EF_TRANSP(fid, ns, flags, smin, srec, \
193 nl, &default_decode, NULL)
Harald Welted54c2ee2012-01-17 18:25:50 +0100194
Harald Welte1e0dfda2014-05-03 14:22:12 +0200195#define EF_CYCLIC(fid, ns, flags, smin, srec, nl, dec, enc) \
196 EF(fid, ns, flags, nl, EF_TYPE_RECORD_CYCLIC, \
197 smin, srec, dec, enc)
198#define EF_CYCLIC_N(fid, ns, flags, smin, srec, nl) \
199 EF_CYCLIC(fid, ns, flags, smin, srec, nl, \
200 &default_decode, NULL)
Harald Welted54c2ee2012-01-17 18:25:50 +0100201
Harald Welte1e0dfda2014-05-03 14:22:12 +0200202#define EF_LIN_FIX(fid, ns, flags, smin, srec, nl, dec, enc) \
203 EF(fid, ns, flags, nl, EF_TYPE_RECORD_FIXED, \
204 smin, srec, dec, enc)
205#define EF_LIN_FIX_N(fid, sfi, ns, flags, smin, srec, nl) \
206 EF_LIN_FIX(fid, sfi, ns, flags, smin, srec, nl, \
207 &default_decode, NULL)
Harald Welted54c2ee2012-01-17 18:25:50 +0100208
209struct osim_file_desc *
210osim_file_find_name(struct osim_file_desc *parent, const char *name);
211
212enum osim_card_sw_type {
213 SW_TYPE_NONE,
214 SW_TYPE_STR,
215};
216
217enum osim_card_sw_class {
218 SW_CLS_NONE,
219 SW_CLS_OK,
220 SW_CLS_POSTP,
221 SW_CLS_WARN,
222 SW_CLS_ERROR,
223};
224
225struct osim_card_sw {
226 uint16_t code;
227 uint16_t mask;
228 enum osim_card_sw_type type;
229 enum osim_card_sw_class class;
230 union {
231 const char *str;
232 } u;
233};
234
Harald Welte76749602012-09-19 20:55:54 +0200235#define OSIM_CARD_SW_LAST (const struct osim_card_sw) { \
Harald Welted54c2ee2012-01-17 18:25:50 +0100236 .code = 0, .mask = 0, .type = SW_TYPE_NONE, \
237 .class = SW_CLS_NONE, .u.str = NULL \
238}
239
240struct osim_card_profile {
241 const char *name;
242 struct osim_file_desc *mf;
Harald Welte76749602012-09-19 20:55:54 +0200243 const struct osim_card_sw **sws;
Harald Welted54c2ee2012-01-17 18:25:50 +0100244};
245
Harald Welte76749602012-09-19 20:55:54 +0200246const struct osim_card_sw *osim_find_sw(const struct osim_card_profile *cp,
247 uint16_t sw);
Harald Welted83d2962013-03-04 17:52:33 +0000248enum osim_card_sw_class osim_sw_class(const struct osim_card_profile *cp,
249 uint16_t sw_in);
Harald Welte76749602012-09-19 20:55:54 +0200250
251struct osim_card_hdl;
252char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in);
253
Harald Welted54c2ee2012-01-17 18:25:50 +0100254extern const struct tlv_definition ts102221_fcp_tlv_def;
255const struct value_string ts102221_fcp_vals[14];
256
257/* 11.1.1.3 */
258enum ts102221_fcp_tag {
259 UICC_FCP_T_FCP = 0x62,
260 UICC_FCP_T_FILE_SIZE = 0x80,
261 UICC_FCP_T_TOT_F_SIZE = 0x81,
262 UICC_FCP_T_FILE_DESC = 0x82,
263 UICC_FCP_T_FILE_ID = 0x83,
264 UICC_FCP_T_DF_NAME = 0x84,
265 UICC_FCP_T_SFID = 0x88,
266 UICC_FCP_T_LIFEC_STS = 0x8A,
267 UICC_FCP_T_SEC_ATTR_REFEXP= 0x8B,
268 UICC_FCP_T_SEC_ATTR_COMP= 0x8C,
269 UICC_FCP_T_PROPRIETARY = 0xA5,
270 UICC_FCP_T_SEC_ATTR_EXP = 0xAB,
271 UICC_FCP_T_PIN_STS_DO = 0xC6,
272};
273
274struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1,
275 uint8_t p2, uint16_t lc, uint16_t le);
276
277struct osim_reader_ops;
278
279struct osim_reader_hdl {
280 /*! \brief member in global list of readers */
281 struct llist_head list;
282 struct osim_reader_ops *ops;
283 void *priv;
284 /*! \brief current card, if any */
285 struct osim_card_hdl *card;
286};
287
288struct osim_card_hdl {
289 /*! \brief member in global list of cards */
290 struct llist_head list;
291 /*! \brief reader through which card is accessed */
292 struct osim_reader_hdl *reader;
293 /*! \brief card profile */
294 struct osim_card_profile *prof;
295
296 /*! \brief list of channels for this card */
297 struct llist_head channels;
298};
299
300struct osim_chan_hdl {
301 /*! \brief linked to card->channels */
302 struct llist_head list;
303 /*! \brief card to which this channel belongs */
304 struct osim_card_hdl *card;
305 const struct osim_file_desc *cwd;
306};
307
308/* reader.c */
309int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg);
Harald Welted83d2962013-03-04 17:52:33 +0000310struct osim_reader_hdl *osim_reader_open(int idx, const char *name, void *ctx);
Harald Welted54c2ee2012-01-17 18:25:50 +0100311struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh);
312#endif /* _OSMOCOM_SIM_H */