1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/*
* Copyright (C) 2011 Duncan Salerno
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "eap_sim_pcsc_card.h"
#include <PCSC/wintypes.h>
#include <PCSC/winscard.h>
#include <daemon.h>
typedef struct private_eap_sim_pcsc_card_t private_eap_sim_pcsc_card_t;
/**
* Private data of an eap_sim_pcsc_card_t object.
*/
struct private_eap_sim_pcsc_card_t {
/**
* Public eap_sim_pcsc_card_t interface.
*/
eap_sim_pcsc_card_t public;
};
/**
* Maximum length for an IMSI.
*/
#define SIM_IMSI_MAX_LEN 15
/**
* Length of the status at the end of response APDUs.
*/
#define APDU_STATUS_LEN 2
/**
* First byte of status word indicating success.
*/
#define APDU_SW1_SUCCESS 0x90
/**
* First byte of status word indicating there is response data to be read.
*/
#define APDU_SW1_RESPONSE_DATA 0x9f
/**
* Decode IMSI EF (Elementary File) into an ASCII string
*/
static bool decode_imsi_ef(unsigned char *input, int input_len, char *output)
{
/* Only digits 0-9 valid in IMSIs */
static const char bcd_num_digits[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '\0', '\0', '\0', '\0', '\0', '\0'
};
int i;
/* Check length byte matches how many bytes we have, and that input
* is correct length for an IMSI */
if (input[0] != input_len-1 || input_len < 2 || input_len > 9)
{
return FALSE;
}
/* Check type byte is IMSI (bottom 3 bits == 001) */
if ((input[1] & 0x07) != 0x01)
{
return FALSE;
}
*output++ = bcd_num_digits[input[1] >> 4];
for (i = 2; i < input_len; i++)
{
*output++ = bcd_num_digits[input[i] & 0xf];
*output++ = bcd_num_digits[input[i] >> 4];
}
*output++ = '\0';
return TRUE;
}
METHOD(simaka_card_t, get_triplet, bool,
private_eap_sim_pcsc_card_t *this, identification_t *id,
char rand[SIM_RAND_LEN], char sres[SIM_SRES_LEN], char kc[SIM_KC_LEN])
{
status_t found = FALSE;
LONG rv;
SCARDCONTEXT hContext;
DWORD dwReaders;
LPSTR mszReaders;
char *cur_reader;
char full_nai[128];
SCARDHANDLE hCard;
enum { DISCONNECTED, CONNECTED, TRANSACTION } hCard_status = DISCONNECTED;
snprintf(full_nai, sizeof(full_nai), "%Y", id);
DBG2(DBG_IKE, "looking for triplet: %Y rand %b", id, rand, SIM_RAND_LEN);
rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
if (rv != SCARD_S_SUCCESS)
{
DBG1(DBG_IKE, "SCardEstablishContext: %s", pcsc_stringify_error(rv));
return FALSE;
}
rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
if (rv != SCARD_S_SUCCESS)
{
DBG1(DBG_IKE, "SCardListReaders: %s", pcsc_stringify_error(rv));
return FALSE;
}
mszReaders = malloc(sizeof(char)*dwReaders);
rv = SCardListReaders(hContext, NULL, mszReaders, &dwReaders);
if (rv != SCARD_S_SUCCESS)
{
DBG1(DBG_IKE, "SCardListReaders: %s", pcsc_stringify_error(rv));
return FALSE;
}
/* mszReaders is a multi-string of readers, separated by '\0' and
* terminated by an additional '\0' */
for (cur_reader = mszReaders; *cur_reader != '\0' && found == FALSE;
cur_reader += strlen(cur_reader) + 1)
{
DWORD dwActiveProtocol = -1;
const SCARD_IO_REQUEST *pioSendPci;
SCARD_IO_REQUEST pioRecvPci;
BYTE pbRecvBuffer[64];
DWORD dwRecvLength;
char imsi[SIM_IMSI_MAX_LEN + 1];
/* See GSM 11.11 for SIM APDUs */
static const BYTE pbSelectMF[] = { 0xa0, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00 };
static const BYTE pbSelectDFGSM[] = { 0xa0, 0xa4, 0x00, 0x00, 0x02, 0x7f, 0x20 };
static const BYTE pbSelectIMSI[] = { 0xa0, 0xa4, 0x00, 0x00, 0x02, 0x6f, 0x07 };
static const BYTE pbReadBinary[] = { 0xa0, 0xb0, 0x00, 0x00, 0x09 };
BYTE pbRunGSMAlgorithm[5 + SIM_RAND_LEN] = { 0xa0, 0x88, 0x00, 0x00, 0x10 };
static const BYTE pbGetResponse[] = { 0xa0, 0xc0, 0x00, 0x00, 0x0c };
/* If on 2nd or later reader, make sure we end the transaction
* and disconnect card in the previous reader */
switch (hCard_status)
{
case TRANSACTION:
SCardEndTransaction(hCard, SCARD_LEAVE_CARD);
/* FALLTHRU */
case CONNECTED:
SCardDisconnect(hCard, SCARD_LEAVE_CARD);
/* FALLTHRU */
case DISCONNECTED:
hCard_status = DISCONNECTED;
}
/* Copy RAND into APDU */
memcpy(pbRunGSMAlgorithm + 5, rand, SIM_RAND_LEN);
rv = SCardConnect(hContext, cur_reader, SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &hCard, &dwActiveProtocol);
if (rv != SCARD_S_SUCCESS)
{
DBG1(DBG_IKE, "SCardConnect: %s", pcsc_stringify_error(rv));
continue;
}
hCard_status = CONNECTED;
switch(dwActiveProtocol)
{
case SCARD_PROTOCOL_T0:
pioSendPci = SCARD_PCI_T0;
break;
case SCARD_PROTOCOL_T1:
pioSendPci = SCARD_PCI_T1;
break;
default:
DBG1(DBG_IKE, "Unknown SCARD_PROTOCOL");
continue;
}
/* Start transaction */
rv = SCardBeginTransaction(hCard);
if (rv != SCARD_S_SUCCESS)
{
DBG1(DBG_IKE, "SCardBeginTransaction: %s", pcsc_stringify_error(rv));
continue;
}
hCard_status = TRANSACTION;
/* APDU: Select MF */
dwRecvLength = sizeof(pbRecvBuffer);
rv = SCardTransmit(hCard, pioSendPci, pbSelectMF, sizeof(pbSelectMF),
&pioRecvPci, pbRecvBuffer, &dwRecvLength);
if (rv != SCARD_S_SUCCESS)
{
DBG1(DBG_IKE, "SCardTransmit: %s", pcsc_stringify_error(rv));
continue;
}
if (dwRecvLength < APDU_STATUS_LEN ||
pbRecvBuffer[dwRecvLength-APDU_STATUS_LEN] != APDU_SW1_RESPONSE_DATA)
{
DBG1(DBG_IKE, "Select MF failed: %b", pbRecvBuffer,
(u_int)dwRecvLength);
continue;
}
/* APDU: Select DF GSM */
dwRecvLength = sizeof(pbRecvBuffer);
rv = SCardTransmit(hCard, pioSendPci, pbSelectDFGSM, sizeof(pbSelectDFGSM),
&pioRecvPci, pbRecvBuffer, &dwRecvLength);
if (rv != SCARD_S_SUCCESS)
{
DBG1(DBG_IKE, "SCardTransmit: %s", pcsc_stringify_error(rv));
continue;
}
if (dwRecvLength < APDU_STATUS_LEN ||
pbRecvBuffer[dwRecvLength-APDU_STATUS_LEN] != APDU_SW1_RESPONSE_DATA)
{
DBG1(DBG_IKE, "Select DF GSM failed: %b", pbRecvBuffer,
(u_int)dwRecvLength);
continue;
}
/* APDU: Select IMSI */
dwRecvLength = sizeof(pbRecvBuffer);
rv = SCardTransmit(hCard, pioSendPci, pbSelectIMSI, sizeof(pbSelectIMSI),
&pioRecvPci, pbRecvBuffer, &dwRecvLength);
if (rv != SCARD_S_SUCCESS)
{
DBG1(DBG_IKE, "SCardTransmit: %s", pcsc_stringify_error(rv));
continue;
}
if (dwRecvLength < APDU_STATUS_LEN ||
pbRecvBuffer[dwRecvLength-APDU_STATUS_LEN] != APDU_SW1_RESPONSE_DATA)
{
DBG1(DBG_IKE, "Select IMSI failed: %b", pbRecvBuffer,
(u_int)dwRecvLength);
continue;
}
/* APDU: Read Binary (of IMSI) */
dwRecvLength = sizeof(pbRecvBuffer);
rv = SCardTransmit(hCard, pioSendPci, pbReadBinary, sizeof(pbReadBinary),
&pioRecvPci, pbRecvBuffer, &dwRecvLength);
if (rv != SCARD_S_SUCCESS)
{
DBG1(DBG_IKE, "SCardTransmit: %s", pcsc_stringify_error(rv));
continue;
}
if (dwRecvLength < APDU_STATUS_LEN ||
pbRecvBuffer[dwRecvLength-APDU_STATUS_LEN] != APDU_SW1_SUCCESS)
{
DBG1(DBG_IKE, "Select IMSI failed: %b", pbRecvBuffer,
(u_int)dwRecvLength);
continue;
}
if (!decode_imsi_ef(pbRecvBuffer, dwRecvLength-APDU_STATUS_LEN, imsi))
{
DBG1(DBG_IKE, "Couldn't decode IMSI EF: %b",
pbRecvBuffer, (u_int)dwRecvLength);
continue;
}
/* The IMSI could be post/prefixed in the full NAI, so just make sure
* it's in there */
if (!(strlen(full_nai) && strstr(full_nai, imsi)))
{
DBG1(DBG_IKE, "Not the SIM we're looking for, IMSI: %s", imsi);
continue;
}
/* APDU: Run GSM Algorithm */
dwRecvLength = sizeof(pbRecvBuffer);
rv = SCardTransmit(hCard, pioSendPci,
pbRunGSMAlgorithm, sizeof(pbRunGSMAlgorithm),
&pioRecvPci, pbRecvBuffer, &dwRecvLength);
if (rv != SCARD_S_SUCCESS)
{
DBG1(DBG_IKE, "SCardTransmit: %s", pcsc_stringify_error(rv));
continue;
}
if (dwRecvLength < APDU_STATUS_LEN ||
pbRecvBuffer[dwRecvLength-APDU_STATUS_LEN] != APDU_SW1_RESPONSE_DATA)
{
DBG1(DBG_IKE, "Run GSM Algorithm failed: %b",
pbRecvBuffer, (u_int)dwRecvLength);
continue;
}
/* APDU: Get Response (of Run GSM Algorithm) */
dwRecvLength = sizeof(pbRecvBuffer);
rv = SCardTransmit(hCard, pioSendPci, pbGetResponse, sizeof(pbGetResponse),
&pioRecvPci, pbRecvBuffer, &dwRecvLength);
if (rv != SCARD_S_SUCCESS)
{
DBG1(DBG_IKE, "SCardTransmit: %s", pcsc_stringify_error(rv));
continue;
}
if (dwRecvLength < APDU_STATUS_LEN ||
pbRecvBuffer[dwRecvLength-APDU_STATUS_LEN] != APDU_SW1_SUCCESS)
{
DBG1(DBG_IKE, "Get Response failed: %b", pbRecvBuffer,
(u_int)dwRecvLength);
continue;
}
/* Extract out Kc and SRES from response */
if (dwRecvLength == SIM_SRES_LEN + SIM_KC_LEN + APDU_STATUS_LEN)
{
memcpy(sres, pbRecvBuffer, SIM_SRES_LEN);
memcpy(kc, pbRecvBuffer+4, SIM_KC_LEN);
/* This will also cause the loop to exit */
found = TRUE;
}
else
{
DBG1(DBG_IKE, "Get Response incorrect length: %b",
pbRecvBuffer, (u_int)dwRecvLength);
continue;
}
/* Transaction will be ended and card disconnected at the
* beginning of this loop or after this loop */
}
/* Make sure we end any previous transaction and disconnect card */
switch (hCard_status)
{
case TRANSACTION:
SCardEndTransaction(hCard, SCARD_LEAVE_CARD);
/* FALLTHRU */
case CONNECTED:
SCardDisconnect(hCard, SCARD_LEAVE_CARD);
/* FALLTHRU */
case DISCONNECTED:
hCard_status = DISCONNECTED;
}
rv = SCardReleaseContext(hContext);
if (rv != SCARD_S_SUCCESS)
{
DBG1(DBG_IKE, "SCardReleaseContext: %s", pcsc_stringify_error(rv));
}
free(mszReaders);
return found;
}
METHOD(simaka_card_t, get_quintuplet, status_t,
private_eap_sim_pcsc_card_t *this, identification_t *id,
char rand[AKA_RAND_LEN], char autn[AKA_AUTN_LEN], char ck[AKA_CK_LEN],
char ik[AKA_IK_LEN], char res[AKA_RES_MAX], int *res_len)
{
return NOT_SUPPORTED;
}
METHOD(eap_sim_pcsc_card_t, destroy, void,
private_eap_sim_pcsc_card_t *this)
{
free(this);
}
/**
* See header
*/
eap_sim_pcsc_card_t *eap_sim_pcsc_card_create()
{
private_eap_sim_pcsc_card_t *this;
INIT(this,
.public = {
.card = {
.get_triplet = _get_triplet,
.get_quintuplet = _get_quintuplet,
.resync = (void*)return_false,
.get_pseudonym = (void*)return_null,
.set_pseudonym = (void*)nop,
.get_reauth = (void*)return_null,
.set_reauth = (void*)nop,
},
.destroy = _destroy,
},
);
return &this->public;
}
|