pcsc_funcs.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. /*
  2. * WPA Supplicant / PC/SC smartcard interface for USIM, GSM SIM
  3. * Copyright (c) 2004-2007, 2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. *
  8. * This file implements wrapper functions for accessing GSM SIM and 3GPP USIM
  9. * cards through PC/SC smartcard library. These functions are used to implement
  10. * authentication routines for EAP-SIM and EAP-AKA.
  11. */
  12. #include "includes.h"
  13. #include <winscard.h>
  14. #include "common.h"
  15. #include "pcsc_funcs.h"
  16. /* See ETSI GSM 11.11 and ETSI TS 102 221 for details.
  17. * SIM commands:
  18. * Command APDU: CLA INS P1 P2 P3 Data
  19. * CLA (class of instruction): A0 for GSM, 00 for USIM
  20. * INS (instruction)
  21. * P1 P2 P3 (parameters, P3 = length of Data)
  22. * Response APDU: Data SW1 SW2
  23. * SW1 SW2 (Status words)
  24. * Commands (INS P1 P2 P3):
  25. * SELECT: A4 00 00 02 <file_id, 2 bytes>
  26. * GET RESPONSE: C0 00 00 <len>
  27. * RUN GSM ALG: 88 00 00 00 <RAND len = 10>
  28. * RUN UMTS ALG: 88 00 81 <len=0x22> data: 0x10 | RAND | 0x10 | AUTN
  29. * P1 = ID of alg in card
  30. * P2 = ID of secret key
  31. * READ BINARY: B0 <offset high> <offset low> <len>
  32. * READ RECORD: B2 <record number> <mode> <len>
  33. * P2 (mode) = '02' (next record), '03' (previous record),
  34. * '04' (absolute mode)
  35. * VERIFY CHV: 20 00 <CHV number> 08
  36. * CHANGE CHV: 24 00 <CHV number> 10
  37. * DISABLE CHV: 26 00 01 08
  38. * ENABLE CHV: 28 00 01 08
  39. * UNBLOCK CHV: 2C 00 <00=CHV1, 02=CHV2> 10
  40. * SLEEP: FA 00 00 00
  41. */
  42. /* GSM SIM commands */
  43. #define SIM_CMD_SELECT 0xa0, 0xa4, 0x00, 0x00, 0x02
  44. #define SIM_CMD_RUN_GSM_ALG 0xa0, 0x88, 0x00, 0x00, 0x10
  45. #define SIM_CMD_GET_RESPONSE 0xa0, 0xc0, 0x00, 0x00
  46. #define SIM_CMD_READ_BIN 0xa0, 0xb0, 0x00, 0x00
  47. #define SIM_CMD_READ_RECORD 0xa0, 0xb2, 0x00, 0x00
  48. #define SIM_CMD_VERIFY_CHV1 0xa0, 0x20, 0x00, 0x01, 0x08
  49. /* USIM commands */
  50. #define USIM_CLA 0x00
  51. #define USIM_CMD_RUN_UMTS_ALG 0x00, 0x88, 0x00, 0x81, 0x22
  52. #define USIM_CMD_GET_RESPONSE 0x00, 0xc0, 0x00, 0x00
  53. #define SIM_RECORD_MODE_ABSOLUTE 0x04
  54. #define USIM_FSP_TEMPL_TAG 0x62
  55. #define USIM_TLV_FILE_DESC 0x82
  56. #define USIM_TLV_FILE_ID 0x83
  57. #define USIM_TLV_DF_NAME 0x84
  58. #define USIM_TLV_PROPR_INFO 0xA5
  59. #define USIM_TLV_LIFE_CYCLE_STATUS 0x8A
  60. #define USIM_TLV_FILE_SIZE 0x80
  61. #define USIM_TLV_TOTAL_FILE_SIZE 0x81
  62. #define USIM_TLV_PIN_STATUS_TEMPLATE 0xC6
  63. #define USIM_TLV_SHORT_FILE_ID 0x88
  64. #define USIM_TLV_SECURITY_ATTR_8B 0x8B
  65. #define USIM_TLV_SECURITY_ATTR_8C 0x8C
  66. #define USIM_TLV_SECURITY_ATTR_AB 0xAB
  67. #define USIM_PS_DO_TAG 0x90
  68. #define AKA_RAND_LEN 16
  69. #define AKA_AUTN_LEN 16
  70. #define AKA_AUTS_LEN 14
  71. #define RES_MAX_LEN 16
  72. #define IK_LEN 16
  73. #define CK_LEN 16
  74. /* GSM files
  75. * File type in first octet:
  76. * 3F = Master File
  77. * 7F = Dedicated File
  78. * 2F = Elementary File under the Master File
  79. * 6F = Elementary File under a Dedicated File
  80. */
  81. #define SCARD_FILE_MF 0x3F00
  82. #define SCARD_FILE_GSM_DF 0x7F20
  83. #define SCARD_FILE_UMTS_DF 0x7F50
  84. #define SCARD_FILE_GSM_EF_IMSI 0x6F07
  85. #define SCARD_FILE_GSM_EF_AD 0x6FAD
  86. #define SCARD_FILE_EF_DIR 0x2F00
  87. #define SCARD_FILE_EF_ICCID 0x2FE2
  88. #define SCARD_FILE_EF_CK 0x6FE1
  89. #define SCARD_FILE_EF_IK 0x6FE2
  90. #define SCARD_CHV1_OFFSET 13
  91. #define SCARD_CHV1_FLAG 0x80
  92. typedef enum { SCARD_GSM_SIM, SCARD_USIM } sim_types;
  93. struct scard_data {
  94. SCARDCONTEXT ctx;
  95. SCARDHANDLE card;
  96. DWORD protocol;
  97. sim_types sim_type;
  98. int pin1_required;
  99. };
  100. #ifdef __MINGW32_VERSION
  101. /* MinGW does not yet support WinScard, so load the needed functions
  102. * dynamically from winscard.dll for now. */
  103. static HINSTANCE dll = NULL; /* winscard.dll */
  104. static const SCARD_IO_REQUEST *dll_g_rgSCardT0Pci, *dll_g_rgSCardT1Pci;
  105. #undef SCARD_PCI_T0
  106. #define SCARD_PCI_T0 (dll_g_rgSCardT0Pci)
  107. #undef SCARD_PCI_T1
  108. #define SCARD_PCI_T1 (dll_g_rgSCardT1Pci)
  109. static WINSCARDAPI LONG WINAPI
  110. (*dll_SCardEstablishContext)(IN DWORD dwScope,
  111. IN LPCVOID pvReserved1,
  112. IN LPCVOID pvReserved2,
  113. OUT LPSCARDCONTEXT phContext);
  114. #define SCardEstablishContext dll_SCardEstablishContext
  115. static long (*dll_SCardReleaseContext)(long hContext);
  116. #define SCardReleaseContext dll_SCardReleaseContext
  117. static WINSCARDAPI LONG WINAPI
  118. (*dll_SCardListReadersA)(IN SCARDCONTEXT hContext,
  119. IN LPCSTR mszGroups,
  120. OUT LPSTR mszReaders,
  121. IN OUT LPDWORD pcchReaders);
  122. #undef SCardListReaders
  123. #define SCardListReaders dll_SCardListReadersA
  124. static WINSCARDAPI LONG WINAPI
  125. (*dll_SCardConnectA)(IN SCARDCONTEXT hContext,
  126. IN LPCSTR szReader,
  127. IN DWORD dwShareMode,
  128. IN DWORD dwPreferredProtocols,
  129. OUT LPSCARDHANDLE phCard,
  130. OUT LPDWORD pdwActiveProtocol);
  131. #undef SCardConnect
  132. #define SCardConnect dll_SCardConnectA
  133. static WINSCARDAPI LONG WINAPI
  134. (*dll_SCardDisconnect)(IN SCARDHANDLE hCard,
  135. IN DWORD dwDisposition);
  136. #define SCardDisconnect dll_SCardDisconnect
  137. static WINSCARDAPI LONG WINAPI
  138. (*dll_SCardTransmit)(IN SCARDHANDLE hCard,
  139. IN LPCSCARD_IO_REQUEST pioSendPci,
  140. IN LPCBYTE pbSendBuffer,
  141. IN DWORD cbSendLength,
  142. IN OUT LPSCARD_IO_REQUEST pioRecvPci,
  143. OUT LPBYTE pbRecvBuffer,
  144. IN OUT LPDWORD pcbRecvLength);
  145. #define SCardTransmit dll_SCardTransmit
  146. static WINSCARDAPI LONG WINAPI
  147. (*dll_SCardBeginTransaction)(IN SCARDHANDLE hCard);
  148. #define SCardBeginTransaction dll_SCardBeginTransaction
  149. static WINSCARDAPI LONG WINAPI
  150. (*dll_SCardEndTransaction)(IN SCARDHANDLE hCard, IN DWORD dwDisposition);
  151. #define SCardEndTransaction dll_SCardEndTransaction
  152. static int mingw_load_symbols(void)
  153. {
  154. char *sym;
  155. if (dll)
  156. return 0;
  157. dll = LoadLibrary("winscard");
  158. if (dll == NULL) {
  159. wpa_printf(MSG_DEBUG, "WinSCard: Could not load winscard.dll "
  160. "library");
  161. return -1;
  162. }
  163. #define LOADSYM(s) \
  164. sym = #s; \
  165. dll_ ## s = (void *) GetProcAddress(dll, sym); \
  166. if (dll_ ## s == NULL) \
  167. goto fail;
  168. LOADSYM(SCardEstablishContext);
  169. LOADSYM(SCardReleaseContext);
  170. LOADSYM(SCardListReadersA);
  171. LOADSYM(SCardConnectA);
  172. LOADSYM(SCardDisconnect);
  173. LOADSYM(SCardTransmit);
  174. LOADSYM(SCardBeginTransaction);
  175. LOADSYM(SCardEndTransaction);
  176. LOADSYM(g_rgSCardT0Pci);
  177. LOADSYM(g_rgSCardT1Pci);
  178. #undef LOADSYM
  179. return 0;
  180. fail:
  181. wpa_printf(MSG_DEBUG, "WinSCard: Could not get address for %s from "
  182. "winscard.dll", sym);
  183. FreeLibrary(dll);
  184. dll = NULL;
  185. return -1;
  186. }
  187. static void mingw_unload_symbols(void)
  188. {
  189. if (dll == NULL)
  190. return;
  191. FreeLibrary(dll);
  192. dll = NULL;
  193. }
  194. #else /* __MINGW32_VERSION */
  195. #define mingw_load_symbols() 0
  196. #define mingw_unload_symbols() do { } while (0)
  197. #endif /* __MINGW32_VERSION */
  198. static int _scard_select_file(struct scard_data *scard, unsigned short file_id,
  199. unsigned char *buf, size_t *buf_len,
  200. sim_types sim_type, unsigned char *aid,
  201. size_t aidlen);
  202. static int scard_select_file(struct scard_data *scard, unsigned short file_id,
  203. unsigned char *buf, size_t *buf_len);
  204. static int scard_verify_pin(struct scard_data *scard, const char *pin);
  205. static int scard_get_record_len(struct scard_data *scard,
  206. unsigned char recnum, unsigned char mode);
  207. static int scard_read_record(struct scard_data *scard,
  208. unsigned char *data, size_t len,
  209. unsigned char recnum, unsigned char mode);
  210. static int scard_parse_fsp_templ(unsigned char *buf, size_t buf_len,
  211. int *ps_do, int *file_len)
  212. {
  213. unsigned char *pos, *end;
  214. if (ps_do)
  215. *ps_do = -1;
  216. if (file_len)
  217. *file_len = -1;
  218. pos = buf;
  219. end = pos + buf_len;
  220. if (*pos != USIM_FSP_TEMPL_TAG) {
  221. wpa_printf(MSG_DEBUG, "SCARD: file header did not "
  222. "start with FSP template tag");
  223. return -1;
  224. }
  225. pos++;
  226. if (pos >= end)
  227. return -1;
  228. if ((pos + pos[0]) < end)
  229. end = pos + 1 + pos[0];
  230. pos++;
  231. wpa_hexdump(MSG_DEBUG, "SCARD: file header FSP template",
  232. pos, end - pos);
  233. while (pos + 1 < end) {
  234. wpa_printf(MSG_MSGDUMP, "SCARD: file header TLV 0x%02x len=%d",
  235. pos[0], pos[1]);
  236. if (pos + 2 + pos[1] > end)
  237. break;
  238. switch (pos[0]) {
  239. case USIM_TLV_FILE_DESC:
  240. wpa_hexdump(MSG_MSGDUMP, "SCARD: File Descriptor TLV",
  241. pos + 2, pos[1]);
  242. break;
  243. case USIM_TLV_FILE_ID:
  244. wpa_hexdump(MSG_MSGDUMP, "SCARD: File Identifier TLV",
  245. pos + 2, pos[1]);
  246. break;
  247. case USIM_TLV_DF_NAME:
  248. wpa_hexdump(MSG_MSGDUMP, "SCARD: DF name (AID) TLV",
  249. pos + 2, pos[1]);
  250. break;
  251. case USIM_TLV_PROPR_INFO:
  252. wpa_hexdump(MSG_MSGDUMP, "SCARD: Proprietary "
  253. "information TLV", pos + 2, pos[1]);
  254. break;
  255. case USIM_TLV_LIFE_CYCLE_STATUS:
  256. wpa_hexdump(MSG_MSGDUMP, "SCARD: Life Cycle Status "
  257. "Integer TLV", pos + 2, pos[1]);
  258. break;
  259. case USIM_TLV_FILE_SIZE:
  260. wpa_hexdump(MSG_MSGDUMP, "SCARD: File size TLV",
  261. pos + 2, pos[1]);
  262. if ((pos[1] == 1 || pos[1] == 2) && file_len) {
  263. if (pos[1] == 1)
  264. *file_len = (int) pos[2];
  265. else
  266. *file_len = ((int) pos[2] << 8) |
  267. (int) pos[3];
  268. wpa_printf(MSG_DEBUG, "SCARD: file_size=%d",
  269. *file_len);
  270. }
  271. break;
  272. case USIM_TLV_TOTAL_FILE_SIZE:
  273. wpa_hexdump(MSG_MSGDUMP, "SCARD: Total file size TLV",
  274. pos + 2, pos[1]);
  275. break;
  276. case USIM_TLV_PIN_STATUS_TEMPLATE:
  277. wpa_hexdump(MSG_MSGDUMP, "SCARD: PIN Status Template "
  278. "DO TLV", pos + 2, pos[1]);
  279. if (pos[1] >= 2 && pos[2] == USIM_PS_DO_TAG &&
  280. pos[3] >= 1 && ps_do) {
  281. wpa_printf(MSG_DEBUG, "SCARD: PS_DO=0x%02x",
  282. pos[4]);
  283. *ps_do = (int) pos[4];
  284. }
  285. break;
  286. case USIM_TLV_SHORT_FILE_ID:
  287. wpa_hexdump(MSG_MSGDUMP, "SCARD: Short File "
  288. "Identifier (SFI) TLV", pos + 2, pos[1]);
  289. break;
  290. case USIM_TLV_SECURITY_ATTR_8B:
  291. case USIM_TLV_SECURITY_ATTR_8C:
  292. case USIM_TLV_SECURITY_ATTR_AB:
  293. wpa_hexdump(MSG_MSGDUMP, "SCARD: Security attribute "
  294. "TLV", pos + 2, pos[1]);
  295. break;
  296. default:
  297. wpa_hexdump(MSG_MSGDUMP, "SCARD: Unrecognized TLV",
  298. pos, 2 + pos[1]);
  299. break;
  300. }
  301. pos += 2 + pos[1];
  302. if (pos == end)
  303. return 0;
  304. }
  305. return -1;
  306. }
  307. static int scard_pin_needed(struct scard_data *scard,
  308. unsigned char *hdr, size_t hlen)
  309. {
  310. if (scard->sim_type == SCARD_GSM_SIM) {
  311. if (hlen > SCARD_CHV1_OFFSET &&
  312. !(hdr[SCARD_CHV1_OFFSET] & SCARD_CHV1_FLAG))
  313. return 1;
  314. return 0;
  315. }
  316. if (scard->sim_type == SCARD_USIM) {
  317. int ps_do;
  318. if (scard_parse_fsp_templ(hdr, hlen, &ps_do, NULL))
  319. return -1;
  320. /* TODO: there could be more than one PS_DO entry because of
  321. * multiple PINs in key reference.. */
  322. if (ps_do > 0 && (ps_do & 0x80))
  323. return 1;
  324. return 0;
  325. }
  326. return -1;
  327. }
  328. static int scard_get_aid(struct scard_data *scard, unsigned char *aid,
  329. size_t maxlen)
  330. {
  331. int rlen, rec;
  332. struct efdir {
  333. unsigned char appl_template_tag; /* 0x61 */
  334. unsigned char appl_template_len;
  335. unsigned char appl_id_tag; /* 0x4f */
  336. unsigned char aid_len;
  337. unsigned char rid[5];
  338. unsigned char appl_code[2]; /* 0x1002 for 3G USIM */
  339. } *efdir;
  340. unsigned char buf[127];
  341. size_t blen;
  342. efdir = (struct efdir *) buf;
  343. blen = sizeof(buf);
  344. if (scard_select_file(scard, SCARD_FILE_EF_DIR, buf, &blen)) {
  345. wpa_printf(MSG_DEBUG, "SCARD: Failed to read EF_DIR");
  346. return -1;
  347. }
  348. wpa_hexdump(MSG_DEBUG, "SCARD: EF_DIR select", buf, blen);
  349. for (rec = 1; rec < 10; rec++) {
  350. rlen = scard_get_record_len(scard, rec,
  351. SIM_RECORD_MODE_ABSOLUTE);
  352. if (rlen < 0) {
  353. wpa_printf(MSG_DEBUG, "SCARD: Failed to get EF_DIR "
  354. "record length");
  355. return -1;
  356. }
  357. blen = sizeof(buf);
  358. if (rlen > (int) blen) {
  359. wpa_printf(MSG_DEBUG, "SCARD: Too long EF_DIR record");
  360. return -1;
  361. }
  362. if (scard_read_record(scard, buf, rlen, rec,
  363. SIM_RECORD_MODE_ABSOLUTE) < 0) {
  364. wpa_printf(MSG_DEBUG, "SCARD: Failed to read "
  365. "EF_DIR record %d", rec);
  366. return -1;
  367. }
  368. wpa_hexdump(MSG_DEBUG, "SCARD: EF_DIR record", buf, rlen);
  369. if (efdir->appl_template_tag != 0x61) {
  370. wpa_printf(MSG_DEBUG, "SCARD: Unexpected application "
  371. "template tag 0x%x",
  372. efdir->appl_template_tag);
  373. continue;
  374. }
  375. if (efdir->appl_template_len > rlen - 2) {
  376. wpa_printf(MSG_DEBUG, "SCARD: Too long application "
  377. "template (len=%d rlen=%d)",
  378. efdir->appl_template_len, rlen);
  379. continue;
  380. }
  381. if (efdir->appl_id_tag != 0x4f) {
  382. wpa_printf(MSG_DEBUG, "SCARD: Unexpected application "
  383. "identifier tag 0x%x", efdir->appl_id_tag);
  384. continue;
  385. }
  386. if (efdir->aid_len < 1 || efdir->aid_len > 16) {
  387. wpa_printf(MSG_DEBUG, "SCARD: Invalid AID length %d",
  388. efdir->aid_len);
  389. continue;
  390. }
  391. wpa_hexdump(MSG_DEBUG, "SCARD: AID from EF_DIR record",
  392. efdir->rid, efdir->aid_len);
  393. if (efdir->appl_code[0] == 0x10 &&
  394. efdir->appl_code[1] == 0x02) {
  395. wpa_printf(MSG_DEBUG, "SCARD: 3G USIM app found from "
  396. "EF_DIR record %d", rec);
  397. break;
  398. }
  399. }
  400. if (rec >= 10) {
  401. wpa_printf(MSG_DEBUG, "SCARD: 3G USIM app not found "
  402. "from EF_DIR records");
  403. return -1;
  404. }
  405. if (efdir->aid_len > maxlen) {
  406. wpa_printf(MSG_DEBUG, "SCARD: Too long AID");
  407. return -1;
  408. }
  409. os_memcpy(aid, efdir->rid, efdir->aid_len);
  410. return efdir->aid_len;
  411. }
  412. /**
  413. * scard_init - Initialize SIM/USIM connection using PC/SC
  414. * @reader: Reader name prefix to search for
  415. * Returns: Pointer to private data structure, or %NULL on failure
  416. *
  417. * This function is used to initialize SIM/USIM connection. PC/SC is used to
  418. * open connection to the SIM/USIM card. In addition, local flag is set if a
  419. * PIN is needed to access some of the card functions. Once the connection is
  420. * not needed anymore, scard_deinit() can be used to close it.
  421. */
  422. struct scard_data * scard_init(const char *reader)
  423. {
  424. long ret;
  425. unsigned long len, pos;
  426. struct scard_data *scard;
  427. #ifdef CONFIG_NATIVE_WINDOWS
  428. TCHAR *readers = NULL;
  429. #else /* CONFIG_NATIVE_WINDOWS */
  430. char *readers = NULL;
  431. #endif /* CONFIG_NATIVE_WINDOWS */
  432. unsigned char buf[100];
  433. size_t blen;
  434. int transaction = 0;
  435. int pin_needed;
  436. wpa_printf(MSG_DEBUG, "SCARD: initializing smart card interface");
  437. if (mingw_load_symbols())
  438. return NULL;
  439. scard = os_zalloc(sizeof(*scard));
  440. if (scard == NULL)
  441. return NULL;
  442. ret = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL,
  443. &scard->ctx);
  444. if (ret != SCARD_S_SUCCESS) {
  445. wpa_printf(MSG_DEBUG, "SCARD: Could not establish smart card "
  446. "context (err=%ld)", ret);
  447. goto failed;
  448. }
  449. ret = SCardListReaders(scard->ctx, NULL, NULL, &len);
  450. if (ret != SCARD_S_SUCCESS) {
  451. wpa_printf(MSG_DEBUG, "SCARD: SCardListReaders failed "
  452. "(err=%ld)", ret);
  453. goto failed;
  454. }
  455. #ifdef UNICODE
  456. len *= 2;
  457. #endif /* UNICODE */
  458. readers = os_malloc(len);
  459. if (readers == NULL) {
  460. wpa_printf(MSG_INFO, "SCARD: malloc failed\n");
  461. goto failed;
  462. }
  463. ret = SCardListReaders(scard->ctx, NULL, readers, &len);
  464. if (ret != SCARD_S_SUCCESS) {
  465. wpa_printf(MSG_DEBUG, "SCARD: SCardListReaders failed(2) "
  466. "(err=%ld)", ret);
  467. goto failed;
  468. }
  469. if (len < 3) {
  470. wpa_printf(MSG_WARNING, "SCARD: No smart card readers "
  471. "available.");
  472. goto failed;
  473. }
  474. wpa_hexdump_ascii(MSG_DEBUG, "SCARD: Readers", (u8 *) readers, len);
  475. /*
  476. * readers is a list of available readers. The last entry is terminated
  477. * with double null.
  478. */
  479. pos = 0;
  480. #ifdef UNICODE
  481. /* TODO */
  482. #else /* UNICODE */
  483. while (pos < len) {
  484. if (reader == NULL ||
  485. os_strncmp(&readers[pos], reader, os_strlen(reader)) == 0)
  486. break;
  487. while (pos < len && readers[pos])
  488. pos++;
  489. pos++; /* skip separating null */
  490. if (pos < len && readers[pos] == '\0')
  491. pos = len; /* double null terminates list */
  492. }
  493. #endif /* UNICODE */
  494. if (pos >= len) {
  495. wpa_printf(MSG_WARNING, "SCARD: No reader with prefix '%s' "
  496. "found", reader);
  497. goto failed;
  498. }
  499. #ifdef UNICODE
  500. wpa_printf(MSG_DEBUG, "SCARD: Selected reader='%S'", &readers[pos]);
  501. #else /* UNICODE */
  502. wpa_printf(MSG_DEBUG, "SCARD: Selected reader='%s'", &readers[pos]);
  503. #endif /* UNICODE */
  504. ret = SCardConnect(scard->ctx, &readers[pos], SCARD_SHARE_SHARED,
  505. SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
  506. &scard->card, &scard->protocol);
  507. if (ret != SCARD_S_SUCCESS) {
  508. if (ret == (long) SCARD_E_NO_SMARTCARD)
  509. wpa_printf(MSG_INFO, "No smart card inserted.");
  510. else
  511. wpa_printf(MSG_WARNING, "SCardConnect err=%lx", ret);
  512. goto failed;
  513. }
  514. os_free(readers);
  515. readers = NULL;
  516. wpa_printf(MSG_DEBUG, "SCARD: card=0x%x active_protocol=%lu (%s)",
  517. (unsigned int) scard->card, scard->protocol,
  518. scard->protocol == SCARD_PROTOCOL_T0 ? "T0" : "T1");
  519. ret = SCardBeginTransaction(scard->card);
  520. if (ret != SCARD_S_SUCCESS) {
  521. wpa_printf(MSG_DEBUG, "SCARD: Could not begin transaction: "
  522. "0x%x", (unsigned int) ret);
  523. goto failed;
  524. }
  525. transaction = 1;
  526. blen = sizeof(buf);
  527. wpa_printf(MSG_DEBUG, "SCARD: verifying USIM support");
  528. if (_scard_select_file(scard, SCARD_FILE_MF, buf, &blen,
  529. SCARD_USIM, NULL, 0)) {
  530. wpa_printf(MSG_DEBUG, "SCARD: USIM is not supported. Trying to use GSM SIM");
  531. scard->sim_type = SCARD_GSM_SIM;
  532. } else {
  533. wpa_printf(MSG_DEBUG, "SCARD: USIM is supported");
  534. scard->sim_type = SCARD_USIM;
  535. }
  536. if (scard->sim_type == SCARD_GSM_SIM) {
  537. blen = sizeof(buf);
  538. if (scard_select_file(scard, SCARD_FILE_MF, buf, &blen)) {
  539. wpa_printf(MSG_DEBUG, "SCARD: Failed to read MF");
  540. goto failed;
  541. }
  542. blen = sizeof(buf);
  543. if (scard_select_file(scard, SCARD_FILE_GSM_DF, buf, &blen)) {
  544. wpa_printf(MSG_DEBUG, "SCARD: Failed to read GSM DF");
  545. goto failed;
  546. }
  547. } else {
  548. unsigned char aid[32];
  549. int aid_len;
  550. aid_len = scard_get_aid(scard, aid, sizeof(aid));
  551. if (aid_len < 0) {
  552. wpa_printf(MSG_DEBUG, "SCARD: Failed to find AID for "
  553. "3G USIM app - try to use standard 3G RID");
  554. os_memcpy(aid, "\xa0\x00\x00\x00\x87", 5);
  555. aid_len = 5;
  556. }
  557. wpa_hexdump(MSG_DEBUG, "SCARD: 3G USIM AID", aid, aid_len);
  558. /* Select based on AID = 3G RID from EF_DIR. This is usually
  559. * starting with A0 00 00 00 87. */
  560. blen = sizeof(buf);
  561. if (_scard_select_file(scard, 0, buf, &blen, scard->sim_type,
  562. aid, aid_len)) {
  563. wpa_printf(MSG_INFO, "SCARD: Failed to read 3G USIM "
  564. "app");
  565. wpa_hexdump(MSG_INFO, "SCARD: 3G USIM AID",
  566. aid, aid_len);
  567. goto failed;
  568. }
  569. }
  570. /* Verify whether CHV1 (PIN1) is needed to access the card. */
  571. pin_needed = scard_pin_needed(scard, buf, blen);
  572. if (pin_needed < 0) {
  573. wpa_printf(MSG_DEBUG, "SCARD: Failed to determine whether PIN "
  574. "is needed");
  575. goto failed;
  576. }
  577. if (pin_needed) {
  578. scard->pin1_required = 1;
  579. wpa_printf(MSG_DEBUG, "PIN1 needed for SIM access (retry "
  580. "counter=%d)", scard_get_pin_retry_counter(scard));
  581. }
  582. ret = SCardEndTransaction(scard->card, SCARD_LEAVE_CARD);
  583. if (ret != SCARD_S_SUCCESS) {
  584. wpa_printf(MSG_DEBUG, "SCARD: Could not end transaction: "
  585. "0x%x", (unsigned int) ret);
  586. }
  587. return scard;
  588. failed:
  589. if (transaction)
  590. SCardEndTransaction(scard->card, SCARD_LEAVE_CARD);
  591. os_free(readers);
  592. scard_deinit(scard);
  593. return NULL;
  594. }
  595. /**
  596. * scard_set_pin - Set PIN (CHV1/PIN1) code for accessing SIM/USIM commands
  597. * @scard: Pointer to private data from scard_init()
  598. * @pin: PIN code as an ASCII string (e.g., "1234")
  599. * Returns: 0 on success, -1 on failure
  600. */
  601. int scard_set_pin(struct scard_data *scard, const char *pin)
  602. {
  603. if (scard == NULL)
  604. return -1;
  605. /* Verify whether CHV1 (PIN1) is needed to access the card. */
  606. if (scard->pin1_required) {
  607. if (pin == NULL) {
  608. wpa_printf(MSG_DEBUG, "No PIN configured for SIM "
  609. "access");
  610. return -1;
  611. }
  612. if (scard_verify_pin(scard, pin)) {
  613. wpa_printf(MSG_INFO, "PIN verification failed for "
  614. "SIM access");
  615. return -1;
  616. }
  617. }
  618. return 0;
  619. }
  620. /**
  621. * scard_deinit - Deinitialize SIM/USIM connection
  622. * @scard: Pointer to private data from scard_init()
  623. *
  624. * This function closes the SIM/USIM connect opened with scard_init().
  625. */
  626. void scard_deinit(struct scard_data *scard)
  627. {
  628. long ret;
  629. if (scard == NULL)
  630. return;
  631. wpa_printf(MSG_DEBUG, "SCARD: deinitializing smart card interface");
  632. if (scard->card) {
  633. ret = SCardDisconnect(scard->card, SCARD_UNPOWER_CARD);
  634. if (ret != SCARD_S_SUCCESS) {
  635. wpa_printf(MSG_DEBUG, "SCARD: Failed to disconnect "
  636. "smart card (err=%ld)", ret);
  637. }
  638. }
  639. if (scard->ctx) {
  640. ret = SCardReleaseContext(scard->ctx);
  641. if (ret != SCARD_S_SUCCESS) {
  642. wpa_printf(MSG_DEBUG, "Failed to release smart card "
  643. "context (err=%ld)", ret);
  644. }
  645. }
  646. os_free(scard);
  647. mingw_unload_symbols();
  648. }
  649. static long scard_transmit(struct scard_data *scard,
  650. unsigned char *_send, size_t send_len,
  651. unsigned char *_recv, size_t *recv_len)
  652. {
  653. long ret;
  654. unsigned long rlen;
  655. wpa_hexdump_key(MSG_DEBUG, "SCARD: scard_transmit: send",
  656. _send, send_len);
  657. rlen = *recv_len;
  658. ret = SCardTransmit(scard->card,
  659. scard->protocol == SCARD_PROTOCOL_T1 ?
  660. SCARD_PCI_T1 : SCARD_PCI_T0,
  661. _send, (unsigned long) send_len,
  662. NULL, _recv, &rlen);
  663. *recv_len = rlen;
  664. if (ret == SCARD_S_SUCCESS) {
  665. wpa_hexdump(MSG_DEBUG, "SCARD: scard_transmit: recv",
  666. _recv, rlen);
  667. } else {
  668. wpa_printf(MSG_WARNING, "SCARD: SCardTransmit failed "
  669. "(err=0x%lx)", ret);
  670. }
  671. return ret;
  672. }
  673. static int _scard_select_file(struct scard_data *scard, unsigned short file_id,
  674. unsigned char *buf, size_t *buf_len,
  675. sim_types sim_type, unsigned char *aid,
  676. size_t aidlen)
  677. {
  678. long ret;
  679. unsigned char resp[3];
  680. unsigned char cmd[50] = { SIM_CMD_SELECT };
  681. int cmdlen;
  682. unsigned char get_resp[5] = { SIM_CMD_GET_RESPONSE };
  683. size_t len, rlen;
  684. if (sim_type == SCARD_USIM) {
  685. cmd[0] = USIM_CLA;
  686. cmd[3] = 0x04;
  687. get_resp[0] = USIM_CLA;
  688. }
  689. wpa_printf(MSG_DEBUG, "SCARD: select file %04x", file_id);
  690. if (aid) {
  691. wpa_hexdump(MSG_DEBUG, "SCARD: select file by AID",
  692. aid, aidlen);
  693. if (5 + aidlen > sizeof(cmd))
  694. return -1;
  695. cmd[2] = 0x04; /* Select by AID */
  696. cmd[4] = aidlen; /* len */
  697. os_memcpy(cmd + 5, aid, aidlen);
  698. cmdlen = 5 + aidlen;
  699. } else {
  700. cmd[5] = file_id >> 8;
  701. cmd[6] = file_id & 0xff;
  702. cmdlen = 7;
  703. }
  704. len = sizeof(resp);
  705. ret = scard_transmit(scard, cmd, cmdlen, resp, &len);
  706. if (ret != SCARD_S_SUCCESS) {
  707. wpa_printf(MSG_WARNING, "SCARD: SCardTransmit failed "
  708. "(err=0x%lx)", ret);
  709. return -1;
  710. }
  711. if (len != 2) {
  712. wpa_printf(MSG_WARNING, "SCARD: unexpected resp len "
  713. "%d (expected 2)", (int) len);
  714. return -1;
  715. }
  716. if (resp[0] == 0x98 && resp[1] == 0x04) {
  717. /* Security status not satisfied (PIN_WLAN) */
  718. wpa_printf(MSG_WARNING, "SCARD: Security status not satisfied "
  719. "(PIN_WLAN)");
  720. return -1;
  721. }
  722. if (resp[0] == 0x6e) {
  723. wpa_printf(MSG_DEBUG, "SCARD: used CLA not supported");
  724. return -1;
  725. }
  726. if (resp[0] != 0x6c && resp[0] != 0x9f && resp[0] != 0x61) {
  727. wpa_printf(MSG_WARNING, "SCARD: unexpected response 0x%02x "
  728. "(expected 0x61, 0x6c, or 0x9f)", resp[0]);
  729. return -1;
  730. }
  731. /* Normal ending of command; resp[1] bytes available */
  732. get_resp[4] = resp[1];
  733. wpa_printf(MSG_DEBUG, "SCARD: trying to get response (%d bytes)",
  734. resp[1]);
  735. rlen = *buf_len;
  736. ret = scard_transmit(scard, get_resp, sizeof(get_resp), buf, &rlen);
  737. if (ret == SCARD_S_SUCCESS) {
  738. *buf_len = resp[1] < rlen ? resp[1] : rlen;
  739. return 0;
  740. }
  741. wpa_printf(MSG_WARNING, "SCARD: SCardTransmit err=0x%lx\n", ret);
  742. return -1;
  743. }
  744. static int scard_select_file(struct scard_data *scard, unsigned short file_id,
  745. unsigned char *buf, size_t *buf_len)
  746. {
  747. return _scard_select_file(scard, file_id, buf, buf_len,
  748. scard->sim_type, NULL, 0);
  749. }
  750. static int scard_get_record_len(struct scard_data *scard, unsigned char recnum,
  751. unsigned char mode)
  752. {
  753. unsigned char buf[255];
  754. unsigned char cmd[5] = { SIM_CMD_READ_RECORD /* , len */ };
  755. size_t blen;
  756. long ret;
  757. if (scard->sim_type == SCARD_USIM)
  758. cmd[0] = USIM_CLA;
  759. cmd[2] = recnum;
  760. cmd[3] = mode;
  761. cmd[4] = sizeof(buf);
  762. blen = sizeof(buf);
  763. ret = scard_transmit(scard, cmd, sizeof(cmd), buf, &blen);
  764. if (ret != SCARD_S_SUCCESS) {
  765. wpa_printf(MSG_DEBUG, "SCARD: failed to determine file "
  766. "length for record %d", recnum);
  767. return -1;
  768. }
  769. wpa_hexdump(MSG_DEBUG, "SCARD: file length determination response",
  770. buf, blen);
  771. if (blen < 2 || (buf[0] != 0x6c && buf[0] != 0x67)) {
  772. wpa_printf(MSG_DEBUG, "SCARD: unexpected response to file "
  773. "length determination");
  774. return -1;
  775. }
  776. return buf[1];
  777. }
  778. static int scard_read_record(struct scard_data *scard,
  779. unsigned char *data, size_t len,
  780. unsigned char recnum, unsigned char mode)
  781. {
  782. unsigned char cmd[5] = { SIM_CMD_READ_RECORD /* , len */ };
  783. size_t blen = len + 3;
  784. unsigned char *buf;
  785. long ret;
  786. if (scard->sim_type == SCARD_USIM)
  787. cmd[0] = USIM_CLA;
  788. cmd[2] = recnum;
  789. cmd[3] = mode;
  790. cmd[4] = len;
  791. buf = os_malloc(blen);
  792. if (buf == NULL)
  793. return -1;
  794. ret = scard_transmit(scard, cmd, sizeof(cmd), buf, &blen);
  795. if (ret != SCARD_S_SUCCESS) {
  796. os_free(buf);
  797. return -2;
  798. }
  799. if (blen != len + 2) {
  800. wpa_printf(MSG_DEBUG, "SCARD: record read returned unexpected "
  801. "length %ld (expected %ld)",
  802. (long) blen, (long) len + 2);
  803. os_free(buf);
  804. return -3;
  805. }
  806. if (buf[len] != 0x90 || buf[len + 1] != 0x00) {
  807. wpa_printf(MSG_DEBUG, "SCARD: record read returned unexpected "
  808. "status %02x %02x (expected 90 00)",
  809. buf[len], buf[len + 1]);
  810. os_free(buf);
  811. return -4;
  812. }
  813. os_memcpy(data, buf, len);
  814. os_free(buf);
  815. return 0;
  816. }
  817. static int scard_read_file(struct scard_data *scard,
  818. unsigned char *data, size_t len)
  819. {
  820. unsigned char cmd[5] = { SIM_CMD_READ_BIN /* , len */ };
  821. size_t blen = len + 3;
  822. unsigned char *buf;
  823. long ret;
  824. cmd[4] = len;
  825. buf = os_malloc(blen);
  826. if (buf == NULL)
  827. return -1;
  828. if (scard->sim_type == SCARD_USIM)
  829. cmd[0] = USIM_CLA;
  830. ret = scard_transmit(scard, cmd, sizeof(cmd), buf, &blen);
  831. if (ret != SCARD_S_SUCCESS) {
  832. os_free(buf);
  833. return -2;
  834. }
  835. if (blen != len + 2) {
  836. wpa_printf(MSG_DEBUG, "SCARD: file read returned unexpected "
  837. "length %ld (expected %ld)",
  838. (long) blen, (long) len + 2);
  839. os_free(buf);
  840. return -3;
  841. }
  842. if (buf[len] != 0x90 || buf[len + 1] != 0x00) {
  843. wpa_printf(MSG_DEBUG, "SCARD: file read returned unexpected "
  844. "status %02x %02x (expected 90 00)",
  845. buf[len], buf[len + 1]);
  846. os_free(buf);
  847. return -4;
  848. }
  849. os_memcpy(data, buf, len);
  850. os_free(buf);
  851. return 0;
  852. }
  853. static int scard_verify_pin(struct scard_data *scard, const char *pin)
  854. {
  855. long ret;
  856. unsigned char resp[3];
  857. unsigned char cmd[5 + 8] = { SIM_CMD_VERIFY_CHV1 };
  858. size_t len;
  859. wpa_printf(MSG_DEBUG, "SCARD: verifying PIN");
  860. if (pin == NULL || os_strlen(pin) > 8)
  861. return -1;
  862. if (scard->sim_type == SCARD_USIM)
  863. cmd[0] = USIM_CLA;
  864. os_memcpy(cmd + 5, pin, os_strlen(pin));
  865. os_memset(cmd + 5 + os_strlen(pin), 0xff, 8 - os_strlen(pin));
  866. len = sizeof(resp);
  867. ret = scard_transmit(scard, cmd, sizeof(cmd), resp, &len);
  868. if (ret != SCARD_S_SUCCESS)
  869. return -2;
  870. if (len != 2 || resp[0] != 0x90 || resp[1] != 0x00) {
  871. wpa_printf(MSG_WARNING, "SCARD: PIN verification failed");
  872. return -1;
  873. }
  874. wpa_printf(MSG_DEBUG, "SCARD: PIN verified successfully");
  875. return 0;
  876. }
  877. int scard_get_pin_retry_counter(struct scard_data *scard)
  878. {
  879. long ret;
  880. unsigned char resp[3];
  881. unsigned char cmd[5] = { SIM_CMD_VERIFY_CHV1 };
  882. size_t len;
  883. u16 val;
  884. wpa_printf(MSG_DEBUG, "SCARD: fetching PIN retry counter");
  885. if (scard->sim_type == SCARD_USIM)
  886. cmd[0] = USIM_CLA;
  887. cmd[4] = 0; /* Empty data */
  888. len = sizeof(resp);
  889. ret = scard_transmit(scard, cmd, sizeof(cmd), resp, &len);
  890. if (ret != SCARD_S_SUCCESS)
  891. return -2;
  892. if (len != 2) {
  893. wpa_printf(MSG_WARNING, "SCARD: failed to fetch PIN retry "
  894. "counter");
  895. return -1;
  896. }
  897. val = WPA_GET_BE16(resp);
  898. if (val == 0x63c0 || val == 0x6983) {
  899. wpa_printf(MSG_DEBUG, "SCARD: PIN has been blocked");
  900. return 0;
  901. }
  902. if (val >= 0x63c0 && val <= 0x63cf)
  903. return val & 0x000f;
  904. wpa_printf(MSG_DEBUG, "SCARD: Unexpected PIN retry counter response "
  905. "value 0x%x", val);
  906. return 0;
  907. }
  908. /**
  909. * scard_get_imsi - Read IMSI from SIM/USIM card
  910. * @scard: Pointer to private data from scard_init()
  911. * @imsi: Buffer for IMSI
  912. * @len: Length of imsi buffer; set to IMSI length on success
  913. * Returns: 0 on success, -1 if IMSI file cannot be selected, -2 if IMSI file
  914. * selection returns invalid result code, -3 if parsing FSP template file fails
  915. * (USIM only), -4 if IMSI does not fit in the provided imsi buffer (len is set
  916. * to needed length), -5 if reading IMSI file fails.
  917. *
  918. * This function can be used to read IMSI from the SIM/USIM card. If the IMSI
  919. * file is PIN protected, scard_set_pin() must have been used to set the
  920. * correct PIN code before calling scard_get_imsi().
  921. */
  922. int scard_get_imsi(struct scard_data *scard, char *imsi, size_t *len)
  923. {
  924. unsigned char buf[100];
  925. size_t blen, imsilen, i;
  926. char *pos;
  927. wpa_printf(MSG_DEBUG, "SCARD: reading IMSI from (GSM) EF-IMSI");
  928. blen = sizeof(buf);
  929. if (scard_select_file(scard, SCARD_FILE_GSM_EF_IMSI, buf, &blen))
  930. return -1;
  931. if (blen < 4) {
  932. wpa_printf(MSG_WARNING, "SCARD: too short (GSM) EF-IMSI "
  933. "header (len=%ld)", (long) blen);
  934. return -2;
  935. }
  936. if (scard->sim_type == SCARD_GSM_SIM) {
  937. blen = (buf[2] << 8) | buf[3];
  938. } else {
  939. int file_size;
  940. if (scard_parse_fsp_templ(buf, blen, NULL, &file_size))
  941. return -3;
  942. blen = file_size;
  943. }
  944. if (blen < 2 || blen > sizeof(buf)) {
  945. wpa_printf(MSG_DEBUG, "SCARD: invalid IMSI file length=%ld",
  946. (long) blen);
  947. return -3;
  948. }
  949. imsilen = (blen - 2) * 2 + 1;
  950. wpa_printf(MSG_DEBUG, "SCARD: IMSI file length=%ld imsilen=%ld",
  951. (long) blen, (long) imsilen);
  952. if (blen < 2 || imsilen > *len) {
  953. *len = imsilen;
  954. return -4;
  955. }
  956. if (scard_read_file(scard, buf, blen))
  957. return -5;
  958. pos = imsi;
  959. *pos++ = '0' + (buf[1] >> 4 & 0x0f);
  960. for (i = 2; i < blen; i++) {
  961. unsigned char digit;
  962. digit = buf[i] & 0x0f;
  963. if (digit < 10)
  964. *pos++ = '0' + digit;
  965. else
  966. imsilen--;
  967. digit = buf[i] >> 4 & 0x0f;
  968. if (digit < 10)
  969. *pos++ = '0' + digit;
  970. else
  971. imsilen--;
  972. }
  973. *len = imsilen;
  974. return 0;
  975. }
  976. /**
  977. * scard_get_mnc_len - Read length of MNC in the IMSI from SIM/USIM card
  978. * @scard: Pointer to private data from scard_init()
  979. * Returns: length (>0) on success, -1 if administrative data file cannot be
  980. * selected, -2 if administrative data file selection returns invalid result
  981. * code, -3 if parsing FSP template file fails (USIM only), -4 if length of
  982. * the file is unexpected, -5 if reading file fails, -6 if MNC length is not
  983. * in range (i.e. 2 or 3), -7 if MNC length is not available.
  984. *
  985. */
  986. int scard_get_mnc_len(struct scard_data *scard)
  987. {
  988. unsigned char buf[100];
  989. size_t blen;
  990. int file_size;
  991. wpa_printf(MSG_DEBUG, "SCARD: reading MNC len from (GSM) EF-AD");
  992. blen = sizeof(buf);
  993. if (scard_select_file(scard, SCARD_FILE_GSM_EF_AD, buf, &blen))
  994. return -1;
  995. if (blen < 4) {
  996. wpa_printf(MSG_WARNING, "SCARD: too short (GSM) EF-AD "
  997. "header (len=%ld)", (long) blen);
  998. return -2;
  999. }
  1000. if (scard->sim_type == SCARD_GSM_SIM) {
  1001. file_size = (buf[2] << 8) | buf[3];
  1002. } else {
  1003. if (scard_parse_fsp_templ(buf, blen, NULL, &file_size))
  1004. return -3;
  1005. }
  1006. if (file_size == 3) {
  1007. wpa_printf(MSG_DEBUG, "SCARD: MNC length not available");
  1008. return -7;
  1009. }
  1010. if (file_size < 4 || file_size > (int) sizeof(buf)) {
  1011. wpa_printf(MSG_DEBUG, "SCARD: invalid file length=%ld",
  1012. (long) file_size);
  1013. return -4;
  1014. }
  1015. if (scard_read_file(scard, buf, file_size))
  1016. return -5;
  1017. buf[3] = buf[3] & 0x0f; /* upper nibble reserved for future use */
  1018. if (buf[3] < 2 || buf[3] > 3) {
  1019. wpa_printf(MSG_DEBUG, "SCARD: invalid MNC length=%ld",
  1020. (long) buf[3]);
  1021. return -6;
  1022. }
  1023. wpa_printf(MSG_DEBUG, "SCARD: MNC length=%ld", (long) buf[3]);
  1024. return buf[3];
  1025. }
  1026. /**
  1027. * scard_gsm_auth - Run GSM authentication command on SIM card
  1028. * @scard: Pointer to private data from scard_init()
  1029. * @_rand: 16-byte RAND value from HLR/AuC
  1030. * @sres: 4-byte buffer for SRES
  1031. * @kc: 8-byte buffer for Kc
  1032. * Returns: 0 on success, -1 if SIM/USIM connection has not been initialized,
  1033. * -2 if authentication command execution fails, -3 if unknown response code
  1034. * for authentication command is received, -4 if reading of response fails,
  1035. * -5 if if response data is of unexpected length
  1036. *
  1037. * This function performs GSM authentication using SIM/USIM card and the
  1038. * provided RAND value from HLR/AuC. If authentication command can be completed
  1039. * successfully, SRES and Kc values will be written into sres and kc buffers.
  1040. */
  1041. int scard_gsm_auth(struct scard_data *scard, const unsigned char *_rand,
  1042. unsigned char *sres, unsigned char *kc)
  1043. {
  1044. unsigned char cmd[5 + 1 + 16] = { SIM_CMD_RUN_GSM_ALG };
  1045. int cmdlen;
  1046. unsigned char get_resp[5] = { SIM_CMD_GET_RESPONSE };
  1047. unsigned char resp[3], buf[12 + 3 + 2];
  1048. size_t len;
  1049. long ret;
  1050. if (scard == NULL)
  1051. return -1;
  1052. wpa_hexdump(MSG_DEBUG, "SCARD: GSM auth - RAND", _rand, 16);
  1053. if (scard->sim_type == SCARD_GSM_SIM) {
  1054. cmdlen = 5 + 16;
  1055. os_memcpy(cmd + 5, _rand, 16);
  1056. } else {
  1057. cmdlen = 5 + 1 + 16;
  1058. cmd[0] = USIM_CLA;
  1059. cmd[3] = 0x80;
  1060. cmd[4] = 17;
  1061. cmd[5] = 16;
  1062. os_memcpy(cmd + 6, _rand, 16);
  1063. get_resp[0] = USIM_CLA;
  1064. }
  1065. len = sizeof(resp);
  1066. ret = scard_transmit(scard, cmd, cmdlen, resp, &len);
  1067. if (ret != SCARD_S_SUCCESS)
  1068. return -2;
  1069. if ((scard->sim_type == SCARD_GSM_SIM &&
  1070. (len != 2 || resp[0] != 0x9f || resp[1] != 0x0c)) ||
  1071. (scard->sim_type == SCARD_USIM &&
  1072. (len != 2 || resp[0] != 0x61 || resp[1] != 0x0e))) {
  1073. wpa_printf(MSG_WARNING, "SCARD: unexpected response for GSM "
  1074. "auth request (len=%ld resp=%02x %02x)",
  1075. (long) len, resp[0], resp[1]);
  1076. return -3;
  1077. }
  1078. get_resp[4] = resp[1];
  1079. len = sizeof(buf);
  1080. ret = scard_transmit(scard, get_resp, sizeof(get_resp), buf, &len);
  1081. if (ret != SCARD_S_SUCCESS)
  1082. return -4;
  1083. if (scard->sim_type == SCARD_GSM_SIM) {
  1084. if (len != 4 + 8 + 2) {
  1085. wpa_printf(MSG_WARNING, "SCARD: unexpected data "
  1086. "length for GSM auth (len=%ld, expected 14)",
  1087. (long) len);
  1088. return -5;
  1089. }
  1090. os_memcpy(sres, buf, 4);
  1091. os_memcpy(kc, buf + 4, 8);
  1092. } else {
  1093. if (len != 1 + 4 + 1 + 8 + 2) {
  1094. wpa_printf(MSG_WARNING, "SCARD: unexpected data "
  1095. "length for USIM auth (len=%ld, "
  1096. "expected 16)", (long) len);
  1097. return -5;
  1098. }
  1099. if (buf[0] != 4 || buf[5] != 8) {
  1100. wpa_printf(MSG_WARNING, "SCARD: unexpected SREC/Kc "
  1101. "length (%d %d, expected 4 8)",
  1102. buf[0], buf[5]);
  1103. }
  1104. os_memcpy(sres, buf + 1, 4);
  1105. os_memcpy(kc, buf + 6, 8);
  1106. }
  1107. wpa_hexdump(MSG_DEBUG, "SCARD: GSM auth - SRES", sres, 4);
  1108. wpa_hexdump(MSG_DEBUG, "SCARD: GSM auth - Kc", kc, 8);
  1109. return 0;
  1110. }
  1111. /**
  1112. * scard_umts_auth - Run UMTS authentication command on USIM card
  1113. * @scard: Pointer to private data from scard_init()
  1114. * @_rand: 16-byte RAND value from HLR/AuC
  1115. * @autn: 16-byte AUTN value from HLR/AuC
  1116. * @res: 16-byte buffer for RES
  1117. * @res_len: Variable that will be set to RES length
  1118. * @ik: 16-byte buffer for IK
  1119. * @ck: 16-byte buffer for CK
  1120. * @auts: 14-byte buffer for AUTS
  1121. * Returns: 0 on success, -1 on failure, or -2 if USIM reports synchronization
  1122. * failure
  1123. *
  1124. * This function performs AKA authentication using USIM card and the provided
  1125. * RAND and AUTN values from HLR/AuC. If authentication command can be
  1126. * completed successfully, RES, IK, and CK values will be written into provided
  1127. * buffers and res_len is set to length of received RES value. If USIM reports
  1128. * synchronization failure, the received AUTS value will be written into auts
  1129. * buffer. In this case, RES, IK, and CK are not valid.
  1130. */
  1131. int scard_umts_auth(struct scard_data *scard, const unsigned char *_rand,
  1132. const unsigned char *autn,
  1133. unsigned char *res, size_t *res_len,
  1134. unsigned char *ik, unsigned char *ck, unsigned char *auts)
  1135. {
  1136. unsigned char cmd[5 + 1 + AKA_RAND_LEN + 1 + AKA_AUTN_LEN] =
  1137. { USIM_CMD_RUN_UMTS_ALG };
  1138. unsigned char get_resp[5] = { USIM_CMD_GET_RESPONSE };
  1139. unsigned char resp[3], buf[64], *pos, *end;
  1140. size_t len;
  1141. long ret;
  1142. if (scard == NULL)
  1143. return -1;
  1144. if (scard->sim_type == SCARD_GSM_SIM) {
  1145. wpa_printf(MSG_ERROR, "SCARD: Non-USIM card - cannot do UMTS "
  1146. "auth");
  1147. return -1;
  1148. }
  1149. wpa_hexdump(MSG_DEBUG, "SCARD: UMTS auth - RAND", _rand, AKA_RAND_LEN);
  1150. wpa_hexdump(MSG_DEBUG, "SCARD: UMTS auth - AUTN", autn, AKA_AUTN_LEN);
  1151. cmd[5] = AKA_RAND_LEN;
  1152. os_memcpy(cmd + 6, _rand, AKA_RAND_LEN);
  1153. cmd[6 + AKA_RAND_LEN] = AKA_AUTN_LEN;
  1154. os_memcpy(cmd + 6 + AKA_RAND_LEN + 1, autn, AKA_AUTN_LEN);
  1155. len = sizeof(resp);
  1156. ret = scard_transmit(scard, cmd, sizeof(cmd), resp, &len);
  1157. if (ret != SCARD_S_SUCCESS)
  1158. return -1;
  1159. if (len <= sizeof(resp))
  1160. wpa_hexdump(MSG_DEBUG, "SCARD: UMTS alg response", resp, len);
  1161. if (len == 2 && resp[0] == 0x98 && resp[1] == 0x62) {
  1162. wpa_printf(MSG_WARNING, "SCARD: UMTS auth failed - "
  1163. "MAC != XMAC");
  1164. return -1;
  1165. } else if (len != 2 || resp[0] != 0x61) {
  1166. wpa_printf(MSG_WARNING, "SCARD: unexpected response for UMTS "
  1167. "auth request (len=%ld resp=%02x %02x)",
  1168. (long) len, resp[0], resp[1]);
  1169. return -1;
  1170. }
  1171. get_resp[4] = resp[1];
  1172. len = sizeof(buf);
  1173. ret = scard_transmit(scard, get_resp, sizeof(get_resp), buf, &len);
  1174. if (ret != SCARD_S_SUCCESS || len > sizeof(buf))
  1175. return -1;
  1176. wpa_hexdump(MSG_DEBUG, "SCARD: UMTS get response result", buf, len);
  1177. if (len >= 2 + AKA_AUTS_LEN && buf[0] == 0xdc &&
  1178. buf[1] == AKA_AUTS_LEN) {
  1179. wpa_printf(MSG_DEBUG, "SCARD: UMTS Synchronization-Failure");
  1180. os_memcpy(auts, buf + 2, AKA_AUTS_LEN);
  1181. wpa_hexdump(MSG_DEBUG, "SCARD: AUTS", auts, AKA_AUTS_LEN);
  1182. return -2;
  1183. } else if (len >= 6 + IK_LEN + CK_LEN && buf[0] == 0xdb) {
  1184. pos = buf + 1;
  1185. end = buf + len;
  1186. /* RES */
  1187. if (pos[0] > RES_MAX_LEN || pos + pos[0] > end) {
  1188. wpa_printf(MSG_DEBUG, "SCARD: Invalid RES");
  1189. return -1;
  1190. }
  1191. *res_len = *pos++;
  1192. os_memcpy(res, pos, *res_len);
  1193. pos += *res_len;
  1194. wpa_hexdump(MSG_DEBUG, "SCARD: RES", res, *res_len);
  1195. /* CK */
  1196. if (pos[0] != CK_LEN || pos + CK_LEN > end) {
  1197. wpa_printf(MSG_DEBUG, "SCARD: Invalid CK");
  1198. return -1;
  1199. }
  1200. pos++;
  1201. os_memcpy(ck, pos, CK_LEN);
  1202. pos += CK_LEN;
  1203. wpa_hexdump(MSG_DEBUG, "SCARD: CK", ck, CK_LEN);
  1204. /* IK */
  1205. if (pos[0] != IK_LEN || pos + IK_LEN > end) {
  1206. wpa_printf(MSG_DEBUG, "SCARD: Invalid IK");
  1207. return -1;
  1208. }
  1209. pos++;
  1210. os_memcpy(ik, pos, IK_LEN);
  1211. pos += IK_LEN;
  1212. wpa_hexdump(MSG_DEBUG, "SCARD: IK", ik, IK_LEN);
  1213. if (end > pos) {
  1214. wpa_hexdump(MSG_DEBUG,
  1215. "SCARD: Ignore extra data in end",
  1216. pos, end - pos);
  1217. }
  1218. return 0;
  1219. }
  1220. wpa_printf(MSG_DEBUG, "SCARD: Unrecognized response");
  1221. return -1;
  1222. }
  1223. int scard_supports_umts(struct scard_data *scard)
  1224. {
  1225. return scard->sim_type == SCARD_USIM;
  1226. }