]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/wpa_supplicant/pcsc_funcs.c
This commit was generated by cvs2svn to compensate for changes in r153758,
[FreeBSD/FreeBSD.git] / contrib / wpa_supplicant / pcsc_funcs.c
1 /*
2  * WPA Supplicant / PC/SC smartcard interface for USIM, GSM SIM
3  * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <winscard.h>
19
20 #include "common.h"
21 #include "wpa_supplicant.h"
22 #include "pcsc_funcs.h"
23
24
25 /* See ETSI GSM 11.11 and ETSI TS 102 221 for details.
26  * SIM commands:
27  * Command APDU: CLA INS P1 P2 P3 Data
28  *   CLA (class of instruction): A0 for GSM, 00 for USIM
29  *   INS (instruction)
30  *   P1 P2 P3 (parameters, P3 = length of Data)
31  * Response APDU: Data SW1 SW2
32  *   SW1 SW2 (Status words)
33  * Commands (INS P1 P2 P3):
34  *   SELECT: A4 00 00 02 <file_id, 2 bytes>
35  *   GET RESPONSE: C0 00 00 <len>
36  *   RUN GSM ALG: 88 00 00 00 <RAND len = 10>
37  *   RUN UMTS ALG: 88 00 81 <len=0x22> data: 0x10 | RAND | 0x10 | AUTN
38  *      P1 = ID of alg in card
39  *      P2 = ID of secret key
40  *   READ BINARY: B0 <offset high> <offset low> <len>
41  *   VERIFY CHV: 20 00 <CHV number> 08
42  *   CHANGE CHV: 24 00 <CHV number> 10
43  *   DISABLE CHV: 26 00 01 08
44  *   ENABLE CHV: 28 00 01 08
45  *   UNBLOCK CHV: 2C 00 <00=CHV1, 02=CHV2> 10
46  *   SLEEP: FA 00 00 00
47  */
48
49 /* GSM SIM commands */
50 #define SIM_CMD_SELECT                  0xa0, 0xa4, 0x00, 0x00, 0x02
51 #define SIM_CMD_RUN_GSM_ALG             0xa0, 0x88, 0x00, 0x00, 0x10
52 #define SIM_CMD_GET_RESPONSE            0xa0, 0xc0, 0x00, 0x00
53 #define SIM_CMD_READ_BIN                0xa0, 0xb0, 0x00, 0x00
54 #define SIM_CMD_VERIFY_CHV1             0xa0, 0x20, 0x00, 0x01, 0x08
55
56 /* USIM commands */
57 #define USIM_CLA                        0x00
58 #define USIM_CMD_RUN_UMTS_ALG           0x00, 0x88, 0x00, 0x81, 0x22
59 #define USIM_CMD_GET_RESPONSE           0x00, 0xc0, 0x00, 0x00
60
61 #define USIM_FSP_TEMPL_TAG              0x62
62
63 #define USIM_TLV_FILE_DESC              0x82
64 #define USIM_TLV_FILE_ID                0x83
65 #define USIM_TLV_DF_NAME                0x84
66 #define USIM_TLV_PROPR_INFO             0xA5
67 #define USIM_TLV_LIFE_CYCLE_STATUS      0x8A
68 #define USIM_TLV_FILE_SIZE              0x80
69 #define USIM_TLV_TOTAL_FILE_SIZE        0x81
70 #define USIM_TLV_PIN_STATUS_TEMPLATE    0xC6
71 #define USIM_TLV_SHORT_FILE_ID          0x88
72
73 #define USIM_PS_DO_TAG                  0x90
74
75 #define AKA_RAND_LEN 16
76 #define AKA_AUTN_LEN 16
77 #define AKA_AUTS_LEN 14
78 #define RES_MAX_LEN 16
79 #define IK_LEN 16
80 #define CK_LEN 16
81
82
83 typedef enum { SCARD_GSM_SIM, SCARD_USIM } sim_types;
84
85 struct scard_data {
86         long ctx;
87         long card;
88         unsigned long protocol;
89         SCARD_IO_REQUEST recv_pci;
90         sim_types sim_type;
91 };
92
93
94 static int _scard_select_file(struct scard_data *scard, unsigned short file_id,
95                               unsigned char *buf, size_t *buf_len,
96                               sim_types sim_type, unsigned char *aid);
97 static int scard_select_file(struct scard_data *scard, unsigned short file_id,
98                              unsigned char *buf, size_t *buf_len);
99 static int scard_verify_pin(struct scard_data *scard, char *pin);
100
101
102 static int scard_parse_fsp_templ(unsigned char *buf, size_t buf_len,
103                                  int *ps_do, int *file_len)
104 {
105                 unsigned char *pos, *end;
106
107                 if (ps_do)
108                         *ps_do = -1;
109                 if (file_len)
110                         *file_len = -1;
111
112                 pos = buf;
113                 end = pos + buf_len;
114                 if (*pos != USIM_FSP_TEMPL_TAG) {
115                         wpa_printf(MSG_DEBUG, "SCARD: file header did not "
116                                    "start with FSP template tag");
117                         return -1;
118                 }
119                 pos++;
120                 if (pos >= end)
121                         return -1;
122                 if ((pos + pos[0]) < end)
123                         end = pos + 1 + pos[0];
124                 pos++;
125                 wpa_hexdump(MSG_DEBUG, "SCARD: file header FSP template",
126                             pos, end - pos);
127
128                 while (pos + 1 < end) {
129                         wpa_printf(MSG_MSGDUMP, "SCARD: file header TLV "
130                                    "0x%02x len=%d", pos[0], pos[1]);
131                         if (pos + 2 + pos[1] > end)
132                                 break;
133
134                         if (pos[0] == USIM_TLV_FILE_SIZE &&
135                             (pos[1] == 1 || pos[1] == 2) && file_len) {
136                                 if (pos[1] == 1)
137                                         *file_len = (int) pos[2];
138                                 else
139                                         *file_len = ((int) pos[2] << 8) |
140                                                 (int) pos[3];
141                                 wpa_printf(MSG_DEBUG, "SCARD: file_size=%d",
142                                            *file_len);
143                         }
144
145                         if (pos[0] == USIM_TLV_PIN_STATUS_TEMPLATE &&
146                             pos[1] >= 2 && pos[2] == USIM_PS_DO_TAG &&
147                             pos[3] >= 1 && ps_do) {
148                                 wpa_printf(MSG_DEBUG, "SCARD: PS_DO=0x%02x",
149                                            pos[4]);
150                                 *ps_do = (int) pos[4];
151                         }
152
153                         pos += 2 + pos[1];
154
155                         if (pos == end)
156                                 return 0;
157                 }
158                 return -1;
159 }
160
161
162 static int scard_pin_needed(struct scard_data *scard,
163                             unsigned char *hdr, size_t hlen)
164 {
165         if (scard->sim_type == SCARD_GSM_SIM) {
166                 if (hlen > SCARD_CHV1_OFFSET &&
167                     !(hdr[SCARD_CHV1_OFFSET] & SCARD_CHV1_FLAG))
168                         return 1;
169                 return 0;
170         }
171
172         if (scard->sim_type == SCARD_USIM) {
173                 int ps_do;
174                 if (scard_parse_fsp_templ(hdr, hlen, &ps_do, NULL))
175                         return -1;
176                 /* TODO: there could be more than one PS_DO entry because of
177                  * multiple PINs in key reference.. */
178                 if (ps_do)
179                         return 1;
180         }
181
182         return -1;
183 }
184
185
186 struct scard_data * scard_init(scard_sim_type sim_type, char *pin)
187 {
188         long ret, len;
189         struct scard_data *scard;
190         char *readers = NULL;
191         char buf[100];
192         size_t blen;
193
194         wpa_printf(MSG_DEBUG, "SCARD: initializing smart card interface");
195         scard = malloc(sizeof(*scard));
196         if (scard == NULL)
197                 return NULL;
198         memset(scard, 0, sizeof(*scard));
199
200         ret = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL,
201                                     &scard->ctx);
202         if (ret != SCARD_S_SUCCESS) {
203                 wpa_printf(MSG_DEBUG, "SCARD: Could not establish smart card "
204                            "context (err=%ld)", ret);
205                 goto failed;
206         }
207
208         ret = SCardListReaders(scard->ctx, NULL, NULL, &len);
209         if (ret != SCARD_S_SUCCESS) {
210                 wpa_printf(MSG_DEBUG, "SCARD: SCardListReaders failed "
211                            "(err=%ld)", ret);
212                 goto failed;
213         }
214
215         readers = malloc(len);
216         if (readers == NULL) {
217                 printf("malloc failed\n");
218                 goto failed;
219         }
220
221         ret = SCardListReaders(scard->ctx, NULL, readers, &len);
222         if (ret != SCARD_S_SUCCESS) {
223                 wpa_printf(MSG_DEBUG, "SCARD: SCardListReaders failed(2) "
224                            "(err=%ld)", ret);
225                 goto failed;
226         }
227         if (len < 3) {
228                 wpa_printf(MSG_WARNING, "SCARD: No smart card readers "
229                            "available.");
230                 goto failed;
231         }
232         /* readers is a list of available reader. Last entry is terminated with
233          * double NUL.
234          * TODO: add support for selecting the reader; now just use the first
235          * one.. */
236         wpa_printf(MSG_DEBUG, "SCARD: Selected reader='%s'", readers);
237
238         ret = SCardConnect(scard->ctx, readers, SCARD_SHARE_SHARED,
239                            SCARD_PROTOCOL_T0, &scard->card, &scard->protocol);
240         if (ret != SCARD_S_SUCCESS) {
241                 if (ret == SCARD_E_NO_SMARTCARD)
242                         wpa_printf(MSG_INFO, "No smart card inserted.");
243                 else
244                         wpa_printf(MSG_WARNING, "SCardConnect err=%lx", ret);
245                 goto failed;
246         }
247
248         free(readers);
249         readers = NULL;
250
251         wpa_printf(MSG_DEBUG, "SCARD: card=%ld active_protocol=%lu",
252                    scard->card, scard->protocol);
253
254         blen = sizeof(buf);
255
256         scard->sim_type = SCARD_GSM_SIM;
257         if (sim_type == SCARD_USIM_ONLY || sim_type == SCARD_TRY_BOTH) {
258                 wpa_printf(MSG_DEBUG, "SCARD: verifying USIM support");
259                 if (_scard_select_file(scard, SCARD_FILE_MF, buf, &blen,
260                                        SCARD_USIM, NULL)) {
261                         wpa_printf(MSG_DEBUG, "SCARD: USIM is not supported");
262                         if (sim_type == SCARD_USIM_ONLY)
263                                 goto failed;
264                         wpa_printf(MSG_DEBUG, "SCARD: Trying to use GSM SIM");
265                         scard->sim_type = SCARD_GSM_SIM;
266                 } else {
267                         wpa_printf(MSG_DEBUG, "SCARD: USIM is supported");
268                         scard->sim_type = SCARD_USIM;
269                 }
270         }
271
272         if (scard->sim_type == SCARD_GSM_SIM) {
273                 blen = sizeof(buf);
274                 if (scard_select_file(scard, SCARD_FILE_MF, buf, &blen)) {
275                         wpa_printf(MSG_DEBUG, "SCARD: Failed to read MF");
276                         goto failed;
277                 }
278
279                 blen = sizeof(buf);
280                 if (scard_select_file(scard, SCARD_FILE_GSM_DF, buf, &blen)) {
281                         wpa_printf(MSG_DEBUG, "SCARD: Failed to read GSM DF");
282                         goto failed;
283                 }
284         } else {
285                 /* Select based on AID = 3G RID */
286                 blen = sizeof(buf);
287                 if (_scard_select_file(scard, 0, buf, &blen, scard->sim_type,
288                                        "\xA0\x00\x00\x00\x87")) {
289                         wpa_printf(MSG_DEBUG, "SCARD: Failed to read 3G RID "
290                                    "AID");
291                         goto failed;
292                 }
293         }
294
295         /* Verify whether CHV1 (PIN1) is needed to access the card. */
296         if (scard_pin_needed(scard, buf, blen)) {
297                 wpa_printf(MSG_DEBUG, "PIN1 needed for SIM access");
298                 if (pin == NULL) {
299                         wpa_printf(MSG_INFO, "No PIN configured for SIM "
300                                    "access");
301                         /* TODO: ask PIN from user through a frontend (e.g.,
302                          * wpa_cli) */
303                         goto failed;
304                 }
305                 if (scard_verify_pin(scard, pin)) {
306                         wpa_printf(MSG_INFO, "PIN verification failed for "
307                                 "SIM access");
308                         /* TODO: what to do? */
309                         goto failed;
310                 }
311         }
312
313         return scard;
314
315 failed:
316         free(readers);
317         scard_deinit(scard);
318         return NULL;
319 }
320
321
322 void scard_deinit(struct scard_data *scard)
323 {
324         long ret;
325
326         if (scard == NULL)
327                 return;
328
329         wpa_printf(MSG_DEBUG, "SCARD: deinitializing smart card interface");
330         if (scard->card) {
331                 ret = SCardDisconnect(scard->card, SCARD_UNPOWER_CARD);
332                 if (ret != SCARD_S_SUCCESS) {
333                         wpa_printf(MSG_DEBUG, "SCARD: Failed to disconnect "
334                                    "smart card (err=%ld)", ret);
335                 }
336         }
337
338         if (scard->ctx) {
339                 ret = SCardReleaseContext(scard->ctx);
340                 if (ret != SCARD_S_SUCCESS) {
341                         wpa_printf(MSG_DEBUG, "Failed to release smart card "
342                                    "context (err=%ld)", ret);
343                 }
344         }
345         free(scard);
346 }
347
348
349 static long scard_transmit(struct scard_data *scard,
350                            unsigned char *send, size_t send_len,
351                            unsigned char *recv, size_t *recv_len)
352 {
353         long ret;
354         unsigned long rlen;
355
356         wpa_hexdump_key(MSG_DEBUG, "SCARD: scard_transmit: send",
357                         send, send_len);
358         rlen = *recv_len;
359         ret = SCardTransmit(scard->card,
360                             scard->protocol == SCARD_PROTOCOL_T1 ?
361                             SCARD_PCI_T1 : SCARD_PCI_T0,
362                             send, (unsigned long) send_len,
363                             &scard->recv_pci, recv, &rlen);
364         *recv_len = rlen;
365         if (ret == SCARD_S_SUCCESS) {
366                 wpa_hexdump(MSG_DEBUG, "SCARD: scard_transmit: recv",
367                             recv, rlen);
368         } else {
369                 wpa_printf(MSG_WARNING, "SCARD: SCardTransmit failed "
370                            "(err=0x%lx)", ret);
371         }
372         return ret;
373 }
374
375
376 static int _scard_select_file(struct scard_data *scard, unsigned short file_id,
377                               unsigned char *buf, size_t *buf_len,
378                               sim_types sim_type, unsigned char *aid)
379 {
380         long ret;
381         unsigned char resp[3];
382         unsigned char cmd[10] = { SIM_CMD_SELECT };
383         int cmdlen;
384         unsigned char get_resp[5] = { SIM_CMD_GET_RESPONSE };
385         size_t len, rlen;
386
387         if (sim_type == SCARD_USIM) {
388                 cmd[0] = USIM_CLA;
389                 cmd[3] = 0x04;
390                 get_resp[0] = USIM_CLA;
391         }
392
393         wpa_printf(MSG_DEBUG, "SCARD: select file %04x", file_id);
394         if (aid) {
395                 cmd[2] = 0x04; /* Select by AID */
396                 cmd[4] = 5; /* len */
397                 memcpy(cmd + 5, aid, 5);
398                 cmdlen = 10;
399         } else {
400                 cmd[5] = file_id >> 8;
401                 cmd[6] = file_id & 0xff;
402                 cmdlen = 7;
403         }
404         len = sizeof(resp);
405         ret = scard_transmit(scard, cmd, cmdlen, resp, &len);
406         if (ret != SCARD_S_SUCCESS) {
407                 wpa_printf(MSG_WARNING, "SCARD: SCardTransmit failed "
408                            "(err=0x%lx)", ret);
409                 return -1;
410         }
411
412         if (len != 2) {
413                 wpa_printf(MSG_WARNING, "SCARD: unexpected resp len "
414                            "%d (expected 2)", (int) len);
415                 return -1;
416         }
417
418         if (resp[0] == 0x98 && resp[1] == 0x04) {
419                 /* Security status not satisfied (PIN_WLAN) */
420                 wpa_printf(MSG_WARNING, "SCARD: Security status not satisfied "
421                            "(PIN_WLAN)");
422                 return -1;
423         }
424
425         if (resp[0] == 0x6e) {
426                 wpa_printf(MSG_DEBUG, "SCARD: used CLA not supported");
427                 return -1;
428         }
429
430         if (resp[0] != 0x6c && resp[0] != 0x9f && resp[0] != 0x61) {
431                 wpa_printf(MSG_WARNING, "SCARD: unexpected response 0x%02x "
432                            "(expected 0x61, 0x6c, or 0x9f)", resp[0]);
433                 return -1;
434         }
435         /* Normal ending of command; resp[1] bytes available */
436         get_resp[4] = resp[1];
437         wpa_printf(MSG_DEBUG, "SCARD: trying to get response (%d bytes)",
438                    resp[1]);
439
440         rlen = *buf_len;
441         ret = scard_transmit(scard, get_resp, sizeof(get_resp), buf, &rlen);
442         if (ret == SCARD_S_SUCCESS) {
443                 *buf_len = resp[1] < rlen ? resp[1] : rlen;
444                 return 0;
445         }
446
447         wpa_printf(MSG_WARNING, "SCARD: SCardTransmit err=0x%lx\n", ret);
448         return -1;
449 }
450
451
452 static int scard_select_file(struct scard_data *scard, unsigned short file_id,
453                              unsigned char *buf, size_t *buf_len)
454 {
455         return _scard_select_file(scard, file_id, buf, buf_len,
456                                   scard->sim_type, NULL);
457 }
458
459
460 static int scard_read_file(struct scard_data *scard,
461                            unsigned char *data, size_t len)
462 {
463         char cmd[5] = { SIM_CMD_READ_BIN, len };
464         size_t blen = len + 3;
465         unsigned char *buf;
466         long ret;
467
468         buf = malloc(blen);
469         if (buf == NULL)
470                 return -1;
471
472         if (scard->sim_type == SCARD_USIM)
473                 cmd[0] = USIM_CLA;
474         ret = scard_transmit(scard, cmd, sizeof(cmd), buf, &blen);
475         if (ret != SCARD_S_SUCCESS) {
476                 free(buf);
477                 return -2;
478         }
479         if (blen != len + 2) {
480                 wpa_printf(MSG_DEBUG, "SCARD: file read returned unexpected "
481                            "length %d (expected %d)", blen, len + 2);
482                 free(buf);
483                 return -3;
484         }
485
486         if (buf[len] != 0x90 || buf[len + 1] != 0x00) {
487                 wpa_printf(MSG_DEBUG, "SCARD: file read returned unexpected "
488                            "status %02x %02x (expected 90 00)",
489                            buf[len], buf[len + 1]);
490                 free(buf);
491                 return -4;
492         }
493
494         memcpy(data, buf, len);
495         free(buf);
496
497         return 0;
498 }
499
500
501 static int scard_verify_pin(struct scard_data *scard, char *pin)
502 {
503         long ret;
504         unsigned char resp[3];
505         char cmd[5 + 8] = { SIM_CMD_VERIFY_CHV1 };
506         size_t len;
507
508         wpa_printf(MSG_DEBUG, "SCARD: verifying PIN");
509
510         if (pin == NULL || strlen(pin) > 8)
511                 return -1;
512
513         if (scard->sim_type == SCARD_USIM)
514                 cmd[0] = USIM_CLA;
515         memcpy(cmd + 5, pin, strlen(pin));
516         memset(cmd + 5 + strlen(pin), 0xff, 8 - strlen(pin));
517
518         len = sizeof(resp);
519         ret = scard_transmit(scard, cmd, sizeof(cmd), resp, &len);
520         if (ret != SCARD_S_SUCCESS)
521                 return -2;
522
523         if (len != 2 || resp[0] != 0x90 || resp[1] != 0x00) {
524                 wpa_printf(MSG_WARNING, "SCARD: PIN verification failed");
525                 return -1;
526         }
527
528         wpa_printf(MSG_DEBUG, "SCARD: PIN verified successfully");
529         return 0;
530 }
531
532
533 int scard_get_imsi(struct scard_data *scard, char *imsi, size_t *len)
534 {
535         char buf[100];
536         size_t blen, imsilen;
537         char *pos;
538         int i;
539
540         wpa_printf(MSG_DEBUG, "SCARD: reading IMSI from (GSM) EF-IMSI");
541         blen = sizeof(buf);
542         if (scard_select_file(scard, SCARD_FILE_GSM_EF_IMSI, buf, &blen))
543                 return -1;
544         if (blen < 4) {
545                 wpa_printf(MSG_WARNING, "SCARD: too short (GSM) EF-IMSI "
546                            "header (len=%d)", blen);
547                 return -2;
548         }
549
550         if (scard->sim_type == SCARD_GSM_SIM) {
551                 blen = (buf[2] << 8) | buf[3];
552         } else {
553                 int file_size;
554                 if (scard_parse_fsp_templ(buf, blen, NULL, &file_size))
555                         return -3;
556                 blen = file_size;
557         }
558         if (blen < 2 || blen > sizeof(buf)) {
559                 wpa_printf(MSG_DEBUG, "SCARD: invalid IMSI file length=%d",
560                            blen);
561                 return -3;
562         }
563
564         imsilen = (blen - 2) * 2 + 1;
565         wpa_printf(MSG_DEBUG, "SCARD: IMSI file length=%d imsilen=%d",
566                    blen, imsilen);
567         if (blen < 2 || imsilen > *len) {
568                 *len = imsilen;
569                 return -4;
570         }
571
572         if (scard_read_file(scard, buf, blen))
573                 return -5;
574
575         pos = imsi;
576         *pos++ = '0' + (buf[1] >> 4 & 0x0f);
577         for (i = 2; i < blen; i++) {
578                 unsigned char digit;
579
580                 digit = buf[i] & 0x0f;
581                 if (digit < 10)
582                         *pos++ = '0' + digit;
583                 else
584                         imsilen--;
585
586                 digit = buf[i] >> 4 & 0x0f;
587                 if (digit < 10)
588                         *pos++ = '0' + digit;
589                 else
590                         imsilen--;
591         }
592         *len = imsilen;
593
594         return 0;
595 }
596
597
598 int scard_gsm_auth(struct scard_data *scard, unsigned char *rand,
599                    unsigned char *sres, unsigned char *kc)
600 {
601         unsigned char cmd[5 + 1 + 16] = { SIM_CMD_RUN_GSM_ALG };
602         int cmdlen;
603         unsigned char get_resp[5] = { SIM_CMD_GET_RESPONSE };
604         unsigned char resp[3], buf[12 + 3 + 2];
605         size_t len;
606         long ret;
607
608         if (scard == NULL)
609                 return -1;
610
611         wpa_hexdump(MSG_DEBUG, "SCARD: GSM auth - RAND", rand, 16);
612         if (scard->sim_type == SCARD_GSM_SIM) {
613                 cmdlen = 5 + 16;
614                 memcpy(cmd + 5, rand, 16);
615         } else {
616                 cmdlen = 5 + 1 + 16;
617                 cmd[0] = USIM_CLA;
618                 cmd[3] = 0x80;
619                 cmd[4] = 17;
620                 cmd[5] = 16;
621                 memcpy(cmd + 6, rand, 16);
622         }
623         len = sizeof(resp);
624         ret = scard_transmit(scard, cmd, sizeof(cmd), resp, &len);
625         if (ret != SCARD_S_SUCCESS)
626                 return -2;
627
628         if ((scard->sim_type == SCARD_GSM_SIM &&
629              (len != 2 || resp[0] != 0x9f || resp[1] != 0x0c)) ||
630             (scard->sim_type == SCARD_USIM &&
631              (len != 2 || resp[0] != 0x61 || resp[1] != 0x0e))) {
632                 wpa_printf(MSG_WARNING, "SCARD: unexpected response for GSM "
633                            "auth request (len=%d resp=%02x %02x)",
634                            len, resp[0], resp[1]);
635                 return -3;
636         }
637         get_resp[4] = resp[1];
638
639         len = sizeof(buf);
640         ret = scard_transmit(scard, get_resp, sizeof(get_resp), buf, &len);
641         if (ret != SCARD_S_SUCCESS)
642                 return -4;
643
644         if (scard->sim_type == SCARD_GSM_SIM) {
645                 if (len != 4 + 8 + 2) {
646                         wpa_printf(MSG_WARNING, "SCARD: unexpected data "
647                                    "length for GSM auth (len=%d, expected 14)",
648                                    len);
649                         return -5;
650                 }
651                 memcpy(sres, buf, 4);
652                 memcpy(kc, buf + 4, 8);
653         } else {
654                 if (len != 1 + 4 + 1 + 8 + 2) {
655                         wpa_printf(MSG_WARNING, "SCARD: unexpected data "
656                                    "length for USIM auth (len=%d, "
657                                    "expected 16)", len);
658                         return -5;
659                 }
660                 if (buf[0] != 4 || buf[5] != 8) {
661                         wpa_printf(MSG_WARNING, "SCARD: unexpected SREC/Kc "
662                                    "length (%d %d, expected 4 8)",
663                                    buf[0], buf[5]);
664                 }
665                 memcpy(sres, buf + 1, 4);
666                 memcpy(kc, buf + 6, 8);
667         }
668
669         wpa_hexdump(MSG_DEBUG, "SCARD: GSM auth - SRES", sres, 4);
670         wpa_hexdump(MSG_DEBUG, "SCARD: GSM auth - Kc", kc, 8);
671
672         return 0;
673 }
674
675
676 int scard_umts_auth(struct scard_data *scard, unsigned char *rand,
677                     unsigned char *autn, unsigned char *res, size_t *res_len,
678                     unsigned char *ik, unsigned char *ck, unsigned char *auts)
679 {
680         unsigned char cmd[5 + 1 + AKA_RAND_LEN + 1 + AKA_AUTN_LEN] =
681                 { USIM_CMD_RUN_UMTS_ALG };
682         int cmdlen;
683         unsigned char get_resp[5] = { USIM_CMD_GET_RESPONSE };
684         unsigned char resp[3], buf[64], *pos, *end;
685         size_t len;
686         long ret;
687
688         if (scard == NULL)
689                 return -1;
690
691         if (scard->sim_type == SCARD_GSM_SIM) {
692                 wpa_printf(MSG_ERROR, "SCARD: Non-USIM card - cannot do UMTS "
693                            "auth");
694                 return -1;
695         }
696
697         wpa_hexdump(MSG_DEBUG, "SCARD: UMTS auth - RAND", rand, AKA_RAND_LEN);
698         wpa_hexdump(MSG_DEBUG, "SCARD: UMTS auth - AUTN", autn, AKA_AUTN_LEN);
699         cmdlen = 5 + 1 + AKA_RAND_LEN + 1 + AKA_AUTN_LEN;
700         cmd[5] = AKA_RAND_LEN;
701         memcpy(cmd + 6, rand, AKA_RAND_LEN);
702         cmd[6 + AKA_RAND_LEN] = AKA_AUTN_LEN;
703         memcpy(cmd + 6 + AKA_RAND_LEN + 1, autn, AKA_AUTN_LEN);
704
705         len = sizeof(resp);
706         ret = scard_transmit(scard, cmd, sizeof(cmd), resp, &len);
707         if (ret != SCARD_S_SUCCESS)
708                 return -1;
709
710         if (len >= 0 && len <= sizeof(resp))
711                 wpa_hexdump(MSG_DEBUG, "SCARD: UMTS alg response", resp, len);
712
713         if (len == 2 && resp[0] == 0x98 && resp[1] == 0x62) {
714                 wpa_printf(MSG_WARNING, "SCARD: UMTS auth failed - "
715                            "MAC != XMAC");
716                 return -1;
717         } else if (len != 2 || resp[0] != 0x61) {
718                 wpa_printf(MSG_WARNING, "SCARD: unexpected response for UMTS "
719                            "auth request (len=%d resp=%02x %02x)",
720                            len, resp[0], resp[1]);
721                 return -1;
722         }
723         get_resp[4] = resp[1];
724
725         len = sizeof(buf);
726         ret = scard_transmit(scard, get_resp, sizeof(get_resp), buf, &len);
727         if (ret != SCARD_S_SUCCESS || len < 0 || len > sizeof(buf))
728                 return -1;
729
730         wpa_hexdump(MSG_DEBUG, "SCARD: UMTS get response result", buf, len);
731         if (len >= 2 + AKA_AUTS_LEN && buf[0] == 0xdc &&
732             buf[1] == AKA_AUTS_LEN) {
733                 wpa_printf(MSG_DEBUG, "SCARD: UMTS Synchronization-Failure");
734                 memcpy(auts, buf + 2, AKA_AUTS_LEN);
735                 wpa_hexdump(MSG_DEBUG, "SCARD: AUTS", auts, AKA_AUTS_LEN);
736                 return -2;
737         } else if (len >= 6 + IK_LEN + CK_LEN && buf[0] == 0xdb) {
738                 pos = buf + 1;
739                 end = buf + len;
740
741                 /* RES */
742                 if (pos[0] > RES_MAX_LEN || pos + pos[0] > end) {
743                         wpa_printf(MSG_DEBUG, "SCARD: Invalid RES");
744                         return -1;
745                 }
746                 *res_len = *pos++;
747                 memcpy(res, pos, *res_len);
748                 pos += *res_len;
749                 wpa_hexdump(MSG_DEBUG, "SCARD: RES", res, *res_len);
750
751                 /* CK */
752                 if (pos[0] != CK_LEN || pos + CK_LEN > end) {
753                         wpa_printf(MSG_DEBUG, "SCARD: Invalid CK");
754                         return -1;
755                 }
756                 pos++;
757                 memcpy(ck, pos, CK_LEN);
758                 pos += CK_LEN;
759                 wpa_hexdump(MSG_DEBUG, "SCARD: CK", ck, CK_LEN);
760
761                 /* IK */
762                 if (pos[0] != IK_LEN || pos + IK_LEN > end) {
763                         wpa_printf(MSG_DEBUG, "SCARD: Invalid IK");
764                         return -1;
765                 }
766                 pos++;
767                 memcpy(ik, pos, IK_LEN);
768                 pos += IK_LEN;
769                 wpa_hexdump(MSG_DEBUG, "SCARD: IK", ik, IK_LEN);
770
771                 return 0;
772         }
773
774         wpa_printf(MSG_DEBUG, "SCARD: Unrecognized response");
775         return -1;
776 }