[layer23] Fixed and completed SIM's PIN handling
[osmocom-bb.git] / src / host / layer23 / include / osmocom / bb / common / sim.h
1 /*
2  * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
3  *
4  * All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  */
21
22
23 /* 9.2 commands */
24 #define GSM1111_CLASS_GSM               0xa0
25 #define GSM1111_INST_SELECT             0xa4
26 #define GSM1111_INST_STATUS             0xf2
27 #define GSM1111_INST_READ_BINARY        0xb0
28 #define GSM1111_INST_UPDATE_BINARY      0xd6
29 #define GSM1111_INST_READ_RECORD        0xb2
30 #define GSM1111_INST_UPDATE_RECORD      0xdc
31 #define GSM1111_INST_SEEK               0xa2
32 #define GSM1111_INST_INCREASE           0x32
33 #define GSM1111_INST_VERIFY_CHV         0x20
34 #define GSM1111_INST_CHANGE_CHV         0x24
35 #define GSM1111_INST_DISABLE_CHV        0x26
36 #define GSM1111_INST_ENABLE_CHV         0x28
37 #define GSM1111_INST_UNBLOCK_CHV        0x2c
38 #define GSM1111_INST_INVALIDATE         0x04
39 #define GSM1111_INST_REHABLILITATE      0x44
40 #define GSM1111_INST_RUN_GSM_ALGO       0x88
41 #define GSM1111_INST_SLEEP              0xfa
42 #define GSM1111_INST_GET_RESPONSE       0xc0
43 #define GSM1111_INST_TERMINAL_PROFILE   0x10
44 #define GSM1111_INST_ENVELOPE           0xc2
45 #define GSM1111_INST_FETCH              0x12
46 #define GSM1111_INST_TERMINAL_RESPONSE  0x14
47
48 /* 9.3 access conditions */
49 #define GSM1111_ACC_ALWAYS              0x0
50 #define GSM1111_ACC_CHV1                0x1
51 #define GSM1111_ACC_CHV2                0x2
52 #define GSM1111_ACC_RFU                 0x3
53 #define GSM1111_ACC_NEW                 0xf
54 /* others are ADM */
55
56 /* 9.3 type of file */
57 #define GSM1111_TOF_RFU                 0x00
58 #define GSM1111_TOF_MF                  0x01
59 #define GSM1111_TOF_DF                  0x02
60 #define GSM1111_TOF_EF                  0x04
61
62 /* 9.3 struct of file */
63 #define GSM1111_SOF_TRANSPARENT         0x00
64 #define GSM1111_SOF_LINEAR              0x01
65 #define GSM1111_SOF_CYCLIC              0x03
66
67 /* 9.4 status */
68 #define GSM1111_STAT_NORMAL             0x90
69 #define GSM1111_STAT_PROACTIVE          0x91
70 #define GSM1111_STAT_DL_ERROR           0x9e
71 #define GSM1111_STAT_RESPONSE           0x9f
72 #define GSM1111_STAT_RESPONSE_TOO       0x61
73 #define GSM1111_STAT_APP_TK_BUSY        0x93
74 #define GSM1111_STAT_MEM_PROBLEM        0x92
75 #define GSM1111_STAT_REFERENCING        0x94
76 #define GSM1111_STAT_SECURITY           0x98
77 #define GSM1111_STAT_INCORR_P3          0x67
78 #define GSM1111_STAT_INCORR_P1_P2       0x6b
79 #define GSM1111_STAT_UKN_INST           0x6d
80 #define GSM1111_STAT_WRONG_CLASS        0x6e
81 #define GSM1111_STAT_TECH_PROBLEM       0x6f
82
83 /* 9.4.4 Referencing management SW2 */
84 #define GSM1111_REF_NO_EF               0x00
85 #define GSM1111_REF_OUT_OF_RANGE        0x02
86 #define GSM1111_REF_FILE_NOT_FOUND      0x04
87 #define GSM1111_REF_FILE_INCONSI        0x08
88
89 /* 9.4.5 Security management SW2 */
90 #define GSM1111_SEC_NO_CHV              0x02
91 #define GSM1111_SEC_NO_ACCESS           0x04
92 #define GSM1111_SEC_CONTRA_CHV          0x08
93 #define GSM1111_SEC_CONTRA_INVAL        0x10
94 #define GSM1111_SEC_BLOCKED             0x40
95 #define GSM1111_SEC_MAX_VALUE           0x50
96
97 /* messages from application to sim client */
98 enum {
99         /* requests */
100         SIM_JOB_READ_BINARY,
101         SIM_JOB_UPDATE_BINARY,
102         SIM_JOB_READ_RECORD,
103         SIM_JOB_UPDATE_RECORD,
104         SIM_JOB_SEEK_RECORD,
105         SIM_JOB_INCREASE,
106         SIM_JOB_INVALIDATE,
107         SIM_JOB_REHABILITATE,
108         SIM_JOB_RUN_GSM_ALGO,
109         SIM_JOB_PIN1_UNLOCK,
110         SIM_JOB_PIN1_CHANGE,
111         SIM_JOB_PIN1_DISABLE,
112         SIM_JOB_PIN1_ENABLE,
113         SIM_JOB_PIN1_UNBLOCK,
114         SIM_JOB_PIN2_UNLOCK,
115         SIM_JOB_PIN2_CHANGE,
116         SIM_JOB_PIN2_UNBLOCK,
117
118         /* results */
119         SIM_JOB_OK,
120         SIM_JOB_ERROR,
121 };
122
123 /* messages from sim client to application */
124 #define SIM_JOB_OK              0
125 #define SIM_JOB_ERROR           1
126
127 /* error causes */
128 #define SIM_CAUSE_NO_SIM        0       /* no SIM present, if detectable */
129 #define SIM_CAUSE_SIM_ERROR     1       /* any error while reading SIM */
130 #define SIM_CAUSE_REQUEST_ERROR 2       /* error in request */
131 #define SIM_CAUSE_PIN1_REQUIRED 3       /* CHV1 is required for access */
132 #define SIM_CAUSE_PIN2_REQUIRED 4       /* CHV2 is required for access */
133 #define SIM_CAUSE_PIN1_BLOCKED  5       /* CHV1 was entered too many times */
134 #define SIM_CAUSE_PIN2_BLOCKED  6       /* CHV2 was entered too many times */
135 #define SIM_CAUSE_PUC_BLOCKED   7       /* unblock entered too many times */
136
137 /* job states */
138 enum {
139         SIM_JST_IDLE = 0,
140         SIM_JST_SELECT_MFDF,            /* SELECT sent */
141         SIM_JST_SELECT_MFDF_RESP,       /* GET RESPONSE sent */
142         SIM_JST_SELECT_EF,              /* SELECT sent */
143         SIM_JST_SELECT_EF_RESP,         /* GET RESPONSE sent */
144         SIM_JST_WAIT_FILE,              /* file command sent */
145         SIM_JST_RUN_GSM_ALGO,           /* wait for algorithm to process */
146         SIM_JST_RUN_GSM_ALGO_RESP,      /* wait for response */
147         SIM_JST_PIN1_UNLOCK,
148         SIM_JST_PIN1_CHANGE,
149         SIM_JST_PIN1_DISABLE,
150         SIM_JST_PIN1_ENABLE,
151         SIM_JST_PIN1_UNBLOCK,
152         SIM_JST_PIN2_UNLOCK,
153         SIM_JST_PIN2_CHANGE,
154         SIM_JST_PIN2_UNBLOCK,
155 };
156
157 #define MAX_SIM_PATH_LENGTH     6 + 1 /* one for the termination */
158
159 struct gsm_sim_handler {
160         struct llist_head       entry;
161
162         uint32_t                handle;
163         void                    (*cb)(struct osmocom_ms *ms, struct msgb *msg);
164 };
165
166 struct gsm_sim {
167         struct llist_head       handlers; /* gsm_sim_handler */
168         struct llist_head       jobs; /* messages */
169         uint16_t path[MAX_SIM_PATH_LENGTH];
170         uint16_t file;
171
172         struct msgb             *job_msg;
173         uint32_t                job_handle;
174         int                     job_state;
175
176         uint8_t                 reset;
177         uint8_t                 chv1_remain, chv2_remain;
178         uint8_t                 unblk1_remain, unblk2_remain;
179 };
180
181 struct sim_hdr {
182         int handle;
183         uint8_t job_type;
184         uint16_t path[MAX_SIM_PATH_LENGTH];
185         uint16_t file;
186         uint8_t rec_no, rec_mode; /* in case of record */
187         uint8_t seek_type_mode; /* in case of seek command */
188 };
189
190 #define SIM_ALLOC_SIZE          512
191 #define SIM_ALLOC_HEADROOM      64
192
193 struct msgb *gsm_sim_msgb_alloc(uint32_t handle, uint8_t job_type);
194 uint32_t sim_open(struct osmocom_ms *ms,
195         void (*cb)(struct osmocom_ms *ms, struct msgb *msg));
196 void sim_close(struct osmocom_ms *ms, uint32_t handle);
197 void sim_job(struct osmocom_ms *ms, struct msgb *msg);
198
199 /* Section 9.2.1 (response to selecting DF or MF) */
200 struct gsm1111_response_mfdf {
201         uint16_t rfu1;
202         uint16_t free_mem;
203         uint16_t file_id;
204         uint8_t tof;
205         uint8_t rfu2[5];
206         uint8_t length;
207         uint8_t gsm_data[0];
208 } __attribute__ ((packed));
209
210 struct gsm1111_response_mfdf_gsm {
211         uint8_t file_char;
212         uint8_t num_df;
213         uint8_t num_ef;
214         uint8_t num_codes;
215         uint8_t rfu1;
216         uint8_t chv1_remain:4,
217                  rfu2:3,
218                  chv1_init:1;
219         uint8_t unblk1_remain:4,
220                  rfu3:3,
221                  unblk1_init:1;
222         uint8_t chv2_remain:4,
223                  rfu4:3,
224                  chv2_init:1;
225         uint8_t unblk2_remain:4,
226                  rfu5:3,
227                  unblk2_init:1;
228         uint8_t more_data[0];
229 } __attribute__ ((packed));
230
231 /* Section 9.2.1 (response to selecting EF) */
232 struct gsm1111_response_ef {
233         uint16_t rfu1;
234         uint16_t file_size;
235         uint16_t file_id;
236         uint8_t tof;
237         uint8_t inc_allowed;
238         uint8_t acc_update:4,
239                  acc_read:4;
240         uint8_t rfu2:4,
241                  acc_inc:4;
242         uint8_t acc_inval:4,
243                  acc_reha:4;
244         uint8_t not_inval:1,
245                  rfu3:1,
246                  ru_inval:1,
247                  rfu4:5;
248         uint8_t length;
249         uint8_t structure;
250 } __attribute__ ((packed));
251
252 /* Section 10.3.17 */
253 struct gsm1111_ef_loci {
254         uint32_t tmsi;
255         struct gsm48_loc_area_id lai;
256         uint8_t tmsi_time;
257         uint8_t lupd_status;
258 } __attribute__ ((packed));
259
260 /* Section 10.5.1 */
261 struct gsm1111_ef_adn {
262         uint8_t len_bcd;
263         uint8_t ton_npi;
264         uint8_t number[10];
265         uint8_t capa_conf;
266         uint8_t ext_id;
267 } __attribute__ ((packed));
268
269 int sim_apdu_resp(struct osmocom_ms *ms, struct msgb *msg);
270 int gsm_sim_init(struct osmocom_ms *ms);
271 int gsm_sim_exit(struct osmocom_ms *ms);
272 int gsm_sim_job_dequeue(struct osmocom_ms *ms);
273
274