pcsc_funcs.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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 (end - pos >= 2) {
  234. unsigned char type, len;
  235. type = pos[0];
  236. len = pos[1];
  237. wpa_printf(MSG_MSGDUMP, "SCARD: file header TLV 0x%02x len=%d",
  238. type, len);
  239. pos += 2;
  240. if (len > (unsigned int) (end - pos))
  241. break;
  242. switch (type) {
  243. case USIM_TLV_FILE_DESC:
  244. wpa_hexdump(MSG_MSGDUMP, "SCARD: File Descriptor TLV",
  245. pos, len);
  246. break;
  247. case USIM_TLV_FILE_ID:
  248. wpa_hexdump(MSG_MSGDUMP, "SCARD: File Identifier TLV",
  249. pos, len);
  250. break;
  251. case USIM_TLV_DF_NAME:
  252. wpa_hexdump(MSG_MSGDUMP, "SCARD: DF name (AID) TLV",
  253. pos, len);
  254. break;
  255. case USIM_TLV_PROPR_INFO:
  256. wpa_hexdump(MSG_MSGDUMP, "SCARD: Proprietary "
  257. "information TLV", pos, len);
  258. break;
  259. case USIM_TLV_LIFE_CYCLE_STATUS:
  260. wpa_hexdump(MSG_MSGDUMP, "SCARD: Life Cycle Status "
  261. "Integer TLV", pos, len);
  262. break;
  263. case USIM_TLV_FILE_SIZE:
  264. wpa_hexdump(MSG_MSGDUMP, "SCARD: File size TLV",
  265. pos, len);
  266. if ((len == 1 || len == 2) && file_len) {
  267. if (len == 1)
  268. *file_len = (int) pos[0];
  269. else
  270. *file_len = WPA_GET_BE16(pos);
  271. wpa_printf(MSG_DEBUG, "SCARD: file_size=%d",
  272. *file_len);
  273. }
  274. break;
  275. case USIM_TLV_TOTAL_FILE_SIZE:
  276. wpa_hexdump(MSG_MSGDUMP, "SCARD: Total file size TLV",
  277. pos, len);
  278. break;
  279. case USIM_TLV_PIN_STATUS_TEMPLATE:
  280. wpa_hexdump(MSG_MSGDUMP, "SCARD: PIN Status Template "
  281. "DO TLV", pos, len);
  282. if (len >= 2 && pos[0] == USIM_PS_DO_TAG &&
  283. pos[1] >= 1 && ps_do) {
  284. wpa_printf(MSG_DEBUG, "SCARD: PS_DO=0x%02x",
  285. pos[2]);
  286. *ps_do = (int) pos[2];
  287. }
  288. break;
  289. case USIM_TLV_SHORT_FILE_ID:
  290. wpa_hexdump(MSG_MSGDUMP, "SCARD: Short File "
  291. "Identifier (SFI) TLV", pos, len);
  292. break;
  293. case USIM_TLV_SECURITY_ATTR_8B:
  294. case USIM_TLV_SECURITY_ATTR_8C:
  295. case USIM_TLV_SECURITY_ATTR_AB:
  296. wpa_hexdump(MSG_MSGDUMP, "SCARD: Security attribute "
  297. "TLV", pos, len);
  298. break;
  299. default:
  300. wpa_hexdump(MSG_MSGDUMP, "SCARD: Unrecognized TLV",
  301. pos, len);
  302. break;
  303. }
  304. pos += len;
  305. if (pos == end)
  306. return 0;
  307. }
  308. return -1;
  309. }
  310. static int scard_pin_needed(struct scard_data *scard,
  311. unsigned char *hdr, size_t hlen)
  312. {
  313. if (scard->sim_type == SCARD_GSM_SIM) {
  314. if (hlen > SCARD_CHV1_OFFSET &&
  315. !(hdr[SCARD_CHV1_OFFSET] & SCARD_CHV1_FLAG))
  316. return 1;
  317. return 0;
  318. }
  319. if (scard->sim_type == SCARD_USIM) {
  320. int ps_do;
  321. if (scard_parse_fsp_templ(hdr, hlen, &ps_do, NULL))
  322. return -1;
  323. /* TODO: there could be more than one PS_DO entry because of
  324. * multiple PINs in key reference.. */
  325. if (ps_do > 0 && (ps_do & 0x80))
  326. return 1;
  327. return 0;
  328. }
  329. return -1;
  330. }
  331. static int scard_get_aid(struct scard_data *scard, unsigned char *aid,
  332. size_t maxlen)
  333. {
  334. int rlen, rec;
  335. struct efdir {
  336. unsigned char appl_template_tag; /* 0x61 */
  337. unsigned char appl_template_len;
  338. unsigned char appl_id_tag; /* 0x4f */
  339. unsigned char aid_len;
  340. unsigned char rid[5];
  341. unsigned char appl_code[2]; /* 0x1002 for 3G USIM */
  342. } *efdir;
  343. unsigned char buf[127], *aid_pos;
  344. size_t blen;
  345. unsigned int aid_len = 0;
  346. efdir = (struct efdir *) buf;
  347. aid_pos = &buf[4];
  348. blen = sizeof(buf);
  349. if (scard_select_file(scard, SCARD_FILE_EF_DIR, buf, &blen)) {
  350. wpa_printf(MSG_DEBUG, "SCARD: Failed to read EF_DIR");
  351. return -1;
  352. }
  353. wpa_hexdump(MSG_DEBUG, "SCARD: EF_DIR select", buf, blen);
  354. for (rec = 1; rec < 10; rec++) {
  355. rlen = scard_get_record_len(scard, rec,
  356. SIM_RECORD_MODE_ABSOLUTE);
  357. if (rlen < 0) {
  358. wpa_printf(MSG_DEBUG, "SCARD: Failed to get EF_DIR "
  359. "record length");
  360. return -1;
  361. }
  362. blen = sizeof(buf);
  363. if (rlen > (int) blen) {
  364. wpa_printf(MSG_DEBUG, "SCARD: Too long EF_DIR record");
  365. return -1;
  366. }
  367. if (scard_read_record(scard, buf, rlen, rec,
  368. SIM_RECORD_MODE_ABSOLUTE) < 0) {
  369. wpa_printf(MSG_DEBUG, "SCARD: Failed to read "
  370. "EF_DIR record %d", rec);
  371. return -1;
  372. }
  373. wpa_hexdump(MSG_DEBUG, "SCARD: EF_DIR record", buf, rlen);
  374. if (efdir->appl_template_tag != 0x61) {
  375. wpa_printf(MSG_DEBUG, "SCARD: Unexpected application "
  376. "template tag 0x%x",
  377. efdir->appl_template_tag);
  378. continue;
  379. }
  380. if (efdir->appl_template_len > rlen - 2) {
  381. wpa_printf(MSG_DEBUG, "SCARD: Too long application "
  382. "template (len=%d rlen=%d)",
  383. efdir->appl_template_len, rlen);
  384. continue;
  385. }
  386. if (efdir->appl_id_tag != 0x4f) {
  387. wpa_printf(MSG_DEBUG, "SCARD: Unexpected application "
  388. "identifier tag 0x%x", efdir->appl_id_tag);
  389. continue;
  390. }
  391. aid_len = efdir->aid_len;
  392. if (aid_len < 1 || aid_len > 16) {
  393. wpa_printf(MSG_DEBUG, "SCARD: Invalid AID length %u",
  394. aid_len);
  395. continue;
  396. }
  397. wpa_hexdump(MSG_DEBUG, "SCARD: AID from EF_DIR record",
  398. aid_pos, aid_len);
  399. if (efdir->appl_code[0] == 0x10 &&
  400. efdir->appl_code[1] == 0x02) {
  401. wpa_printf(MSG_DEBUG, "SCARD: 3G USIM app found from "
  402. "EF_DIR record %d", rec);
  403. break;
  404. }
  405. }
  406. if (rec >= 10) {
  407. wpa_printf(MSG_DEBUG, "SCARD: 3G USIM app not found "
  408. "from EF_DIR records");
  409. return -1;
  410. }
  411. if (aid_len > maxlen) {
  412. wpa_printf(MSG_DEBUG, "SCARD: Too long AID");
  413. return -1;
  414. }
  415. os_memcpy(aid, aid_pos, aid_len);
  416. return aid_len;
  417. }
  418. /**
  419. * scard_init - Initialize SIM/USIM connection using PC/SC
  420. * @reader: Reader name prefix to search for
  421. * Returns: Pointer to private data structure, or %NULL on failure
  422. *
  423. * This function is used to initialize SIM/USIM connection. PC/SC is used to
  424. * open connection to the SIM/USIM card. In addition, local flag is set if a
  425. * PIN is needed to access some of the card functions. Once the connection is
  426. * not needed anymore, scard_deinit() can be used to close it.
  427. */
  428. struct scard_data * scard_init(const char *reader)
  429. {
  430. long ret;
  431. unsigned long len, pos;
  432. struct scard_data *scard;
  433. #ifdef CONFIG_NATIVE_WINDOWS
  434. TCHAR *readers = NULL;
  435. #else /* CONFIG_NATIVE_WINDOWS */
  436. char *readers = NULL;
  437. #endif /* CONFIG_NATIVE_WINDOWS */
  438. unsigned char buf[100];
  439. size_t blen;
  440. int transaction = 0;
  441. int pin_needed;
  442. wpa_printf(MSG_DEBUG, "SCARD: initializing smart card interface");
  443. if (mingw_load_symbols())
  444. return NULL;
  445. scard = os_zalloc(sizeof(*scard));
  446. if (scard == NULL)
  447. return NULL;
  448. ret = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL,
  449. &scard->ctx);
  450. if (ret != SCARD_S_SUCCESS) {
  451. wpa_printf(MSG_DEBUG, "SCARD: Could not establish smart card "
  452. "context (err=%ld)", ret);
  453. goto failed;
  454. }
  455. ret = SCardListReaders(scard->ctx, NULL, NULL, &len);
  456. if (ret != SCARD_S_SUCCESS) {
  457. wpa_printf(MSG_DEBUG, "SCARD: SCardListReaders failed "
  458. "(err=%ld)", ret);
  459. goto failed;
  460. }
  461. #ifdef UNICODE
  462. len *= 2;
  463. #endif /* UNICODE */
  464. readers = os_malloc(len);
  465. if (readers == NULL) {
  466. wpa_printf(MSG_INFO, "SCARD: malloc failed\n");
  467. goto failed;
  468. }
  469. ret = SCardListReaders(scard->ctx, NULL, readers, &len);
  470. if (ret != SCARD_S_SUCCESS) {
  471. wpa_printf(MSG_DEBUG, "SCARD: SCardListReaders failed(2) "
  472. "(err=%ld)", ret);
  473. goto failed;
  474. }
  475. if (len < 3) {
  476. wpa_printf(MSG_WARNING, "SCARD: No smart card readers "
  477. "available.");
  478. goto failed;
  479. }
  480. wpa_hexdump_ascii(MSG_DEBUG, "SCARD: Readers", (u8 *) readers, len);
  481. /*
  482. * readers is a list of available readers. The last entry is terminated
  483. * with double null.
  484. */
  485. pos = 0;
  486. #ifdef UNICODE
  487. /* TODO */
  488. #else /* UNICODE */
  489. while (pos < len) {
  490. if (reader == NULL ||
  491. os_strncmp(&readers[pos], reader, os_strlen(reader)) == 0)
  492. break;
  493. while (pos < len && readers[pos])
  494. pos++;
  495. pos++; /* skip separating null */
  496. if (pos < len && readers[pos] == '\0')
  497. pos = len; /* double null terminates list */
  498. }
  499. #endif /* UNICODE */
  500. if (pos >= len) {
  501. wpa_printf(MSG_WARNING, "SCARD: No reader with prefix '%s' "
  502. "found", reader);
  503. goto failed;
  504. }
  505. #ifdef UNICODE
  506. wpa_printf(MSG_DEBUG, "SCARD: Selected reader='%S'", &readers[pos]);
  507. #else /* UNICODE */
  508. wpa_printf(MSG_DEBUG, "SCARD: Selected reader='%s'", &readers[pos]);
  509. #endif /* UNICODE */
  510. ret = SCardConnect(scard->ctx, &readers[pos], SCARD_SHARE_SHARED,
  511. SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
  512. &scard->card, &scard->protocol);
  513. if (ret != SCARD_S_SUCCESS) {
  514. if (ret == (long) SCARD_E_NO_SMARTCARD)
  515. wpa_printf(MSG_INFO, "No smart card inserted.");
  516. else
  517. wpa_printf(MSG_WARNING, "SCardConnect err=%lx", ret);
  518. goto failed;
  519. }
  520. os_free(readers);
  521. readers = NULL;
  522. wpa_printf(MSG_DEBUG, "SCARD: card=0x%x active_protocol=%lu (%s)",
  523. (unsigned int) scard->card, scard->protocol,
  524. scard->protocol == SCARD_PROTOCOL_T0 ? "T0" : "T1");
  525. ret = SCardBeginTransaction(scard->card);
  526. if (ret != SCARD_S_SUCCESS) {
  527. wpa_printf(MSG_DEBUG, "SCARD: Could not begin transaction: "
  528. "0x%x", (unsigned int) ret);
  529. goto failed;
  530. }
  531. transaction = 1;
  532. blen = sizeof(buf);
  533. wpa_printf(MSG_DEBUG, "SCARD: verifying USIM support");
  534. if (_scard_select_file(scard, SCARD_FILE_MF, buf, &blen,
  535. SCARD_USIM, NULL, 0)) {
  536. wpa_printf(MSG_DEBUG, "SCARD: USIM is not supported. Trying to use GSM SIM");
  537. scard->sim_type = SCARD_GSM_SIM;
  538. } else {
  539. wpa_printf(MSG_DEBUG, "SCARD: USIM is supported");
  540. scard->sim_type = SCARD_USIM;
  541. }
  542. if (scard->sim_type == SCARD_GSM_SIM) {
  543. blen = sizeof(buf);
  544. if (scard_select_file(scard, SCARD_FILE_MF, buf, &blen)) {
  545. wpa_printf(MSG_DEBUG, "SCARD: Failed to read MF");
  546. goto failed;
  547. }
  548. blen = sizeof(buf);
  549. if (scard_select_file(scard, SCARD_FILE_GSM_DF, buf, &blen)) {
  550. wpa_printf(MSG_DEBUG, "SCARD: Failed to read GSM DF");
  551. goto failed;
  552. }
  553. } else {
  554. unsigned char aid[32];
  555. int aid_len;
  556. aid_len = scard_get_aid(scard, aid, sizeof(aid));
  557. if (aid_len < 0) {
  558. wpa_printf(MSG_DEBUG, "SCARD: Failed to find AID for "
  559. "3G USIM app - try to use standard 3G RID");
  560. os_memcpy(aid, "\xa0\x00\x00\x00\x87", 5);
  561. aid_len = 5;
  562. }
  563. wpa_hexdump(MSG_DEBUG, "SCARD: 3G USIM AID", aid, aid_len);
  564. /* Select based on AID = 3G RID from EF_DIR. This is usually
  565. * starting with A0 00 00 00 87. */
  566. blen = sizeof(buf);
  567. if (_scard_select_file(scard, 0, buf, &blen, scard->sim_type,
  568. aid, aid_len)) {
  569. wpa_printf(MSG_INFO, "SCARD: Failed to read 3G USIM "
  570. "app");
  571. wpa_hexdump(MSG_INFO, "SCARD: 3G USIM AID",
  572. aid, aid_len);
  573. goto failed;
  574. }
  575. }
  576. /* Verify whether CHV1 (PIN1) is needed to access the card. */
  577. pin_needed = scard_pin_needed(scard, buf, blen);
  578. if (pin_needed < 0) {
  579. wpa_printf(MSG_DEBUG, "SCARD: Failed to determine whether PIN "
  580. "is needed");
  581. goto failed;
  582. }
  583. if (pin_needed) {
  584. scard->pin1_required = 1;
  585. wpa_printf(MSG_DEBUG, "PIN1 needed for SIM access (retry "
  586. "counter=%d)", scard_get_pin_retry_counter(scard));
  587. }
  588. ret = SCardEndTransaction(scard->card, SCARD_LEAVE_CARD);
  589. if (ret != SCARD_S_SUCCESS) {
  590. wpa_printf(MSG_DEBUG, "SCARD: Could not end transaction: "
  591. "0x%x", (unsigned int) ret);
  592. }
  593. return scard;
  594. failed:
  595. if (transaction)
  596. SCardEndTransaction(scard->card, SCARD_LEAVE_CARD);
  597. os_free(readers);
  598. scard_deinit(scard);
  599. return NULL;
  600. }
  601. /**
  602. * scard_set_pin - Set PIN (CHV1/PIN1) code for accessing SIM/USIM commands
  603. * @scard: Pointer to private data from scard_init()
  604. * @pin: PIN code as an ASCII string (e.g., "1234")
  605. * Returns: 0 on success, -1 on failure
  606. */
  607. int scard_set_pin(struct scard_data *scard, const char *pin)
  608. {
  609. if (scard == NULL)
  610. return -1;
  611. /* Verify whether CHV1 (PIN1) is needed to access the card. */
  612. if (scard->pin1_required) {
  613. if (pin == NULL) {
  614. wpa_printf(MSG_DEBUG, "No PIN configured for SIM "
  615. "access");
  616. return -1;
  617. }
  618. if (scard_verify_pin(scard, pin)) {
  619. wpa_printf(MSG_INFO, "PIN verification failed for "
  620. "SIM access");
  621. return -1;
  622. }
  623. }
  624. return 0;
  625. }
  626. /**
  627. * scard_deinit - Deinitialize SIM/USIM connection
  628. * @scard: Pointer to private data from scard_init()
  629. *
  630. * This function closes the SIM/USIM connect opened with scard_init().
  631. */
  632. void scard_deinit(struct scard_data *scard)
  633. {
  634. long ret;
  635. if (scard == NULL)
  636. return;
  637. wpa_printf(MSG_DEBUG, "SCARD: deinitializing smart card interface");
  638. if (scard->card) {
  639. ret = SCardDisconnect(scard->card, SCARD_UNPOWER_CARD);
  640. if (ret != SCARD_S_SUCCESS) {
  641. wpa_printf(MSG_DEBUG, "SCARD: Failed to disconnect "
  642. "smart card (err=%ld)", ret);
  643. }
  644. }
  645. if (scard->ctx) {
  646. ret = SCardReleaseContext(scard->ctx);
  647. if (ret != SCARD_S_SUCCESS) {
  648. wpa_printf(MSG_DEBUG, "Failed to release smart card "
  649. "context (err=%ld)", ret);
  650. }
  651. }
  652. os_free(scard);
  653. mingw_unload_symbols();
  654. }
  655. static long scard_transmit(struct scard_data *scard,
  656. unsigned char *_send, size_t send_len,
  657. unsigned char *_recv, size_t *recv_len)
  658. {
  659. long ret;
  660. unsigned long rlen;
  661. wpa_hexdump_key(MSG_DEBUG, "SCARD: scard_transmit: send",
  662. _send, send_len);
  663. rlen = *recv_len;
  664. ret = SCardTransmit(scard->card,
  665. scard->protocol == SCARD_PROTOCOL_T1 ?
  666. SCARD_PCI_T1 : SCARD_PCI_T0,
  667. _send, (unsigned long) send_len,
  668. NULL, _recv, &rlen);
  669. *recv_len = rlen;
  670. if (ret == SCARD_S_SUCCESS) {
  671. wpa_hexdump(MSG_DEBUG, "SCARD: scard_transmit: recv",
  672. _recv, rlen);
  673. } else {
  674. wpa_printf(MSG_WARNING, "SCARD: SCardTransmit failed "
  675. "(err=0x%lx)", ret);
  676. }
  677. return ret;
  678. }
  679. static int _scard_select_file(struct scard_data *scard, unsigned short file_id,
  680. unsigned char *buf, size_t *buf_len,
  681. sim_types sim_type, unsigned char *aid,
  682. size_t aidlen)
  683. {
  684. long ret;
  685. unsigned char resp[3];
  686. unsigned char cmd[50] = { SIM_CMD_SELECT };
  687. int cmdlen;
  688. unsigned char get_resp[5] = { SIM_CMD_GET_RESPONSE };
  689. size_t len, rlen;
  690. if (sim_type == SCARD_USIM) {
  691. cmd[0] = USIM_CLA;
  692. cmd[3] = 0x04;
  693. get_resp[0] = USIM_CLA;
  694. }
  695. wpa_printf(MSG_DEBUG, "SCARD: select file %04x", file_id);
  696. if (aid) {
  697. wpa_hexdump(MSG_DEBUG, "SCARD: select file by AID",
  698. aid, aidlen);
  699. if (5 + aidlen > sizeof(cmd))
  700. return -1;
  701. cmd[2] = 0x04; /* Select by AID */
  702. cmd[4] = aidlen; /* len */
  703. os_memcpy(cmd + 5, aid, aidlen);
  704. cmdlen = 5 + aidlen;
  705. } else {
  706. cmd[5] = file_id >> 8;
  707. cmd[6] = file_id & 0xff;
  708. cmdlen = 7;
  709. }
  710. len = sizeof(resp);
  711. ret = scard_transmit(scard, cmd, cmdlen, resp, &len);
  712. if (ret != SCARD_S_SUCCESS) {
  713. wpa_printf(MSG_WARNING, "SCARD: SCardTransmit failed "
  714. "(err=0x%lx)", ret);
  715. return -1;
  716. }
  717. if (len != 2) {
  718. wpa_printf(MSG_WARNING, "SCARD: unexpected resp len "
  719. "%d (expected 2)", (int) len);
  720. return -1;
  721. }
  722. if (resp[0] == 0x98 && resp[1] == 0x04) {
  723. /* Security status not satisfied (PIN_WLAN) */
  724. wpa_printf(MSG_WARNING, "SCARD: Security status not satisfied "
  725. "(PIN_WLAN)");
  726. return -1;
  727. }
  728. if (resp[0] == 0x6e) {
  729. wpa_printf(MSG_DEBUG, "SCARD: used CLA not supported");
  730. return -1;
  731. }
  732. if (resp[0] != 0x6c && resp[0] != 0x9f && resp[0] != 0x61) {
  733. wpa_printf(MSG_WARNING, "SCARD: unexpected response 0x%02x "
  734. "(expected 0x61, 0x6c, or 0x9f)", resp[0]);
  735. return -1;
  736. }
  737. /* Normal ending of command; resp[1] bytes available */
  738. get_resp[4] = resp[1];
  739. wpa_printf(MSG_DEBUG, "SCARD: trying to get response (%d bytes)",
  740. resp[1]);
  741. rlen = *buf_len;
  742. ret = scard_transmit(scard, get_resp, sizeof(get_resp), buf, &rlen);
  743. if (ret == SCARD_S_SUCCESS) {
  744. *buf_len = resp[1] < rlen ? resp[1] : rlen;
  745. return 0;
  746. }
  747. wpa_printf(MSG_WARNING, "SCARD: SCardTransmit err=0x%lx\n", ret);
  748. return -1;
  749. }
  750. static int scard_select_file(struct scard_data *scard, unsigned short file_id,
  751. unsigned char *buf, size_t *buf_len)
  752. {
  753. return _scard_select_file(scard, file_id, buf, buf_len,
  754. scard->sim_type, NULL, 0);
  755. }
  756. static int scard_get_record_len(struct scard_data *scard, unsigned char recnum,
  757. unsigned char mode)
  758. {
  759. unsigned char buf[255];
  760. unsigned char cmd[5] = { SIM_CMD_READ_RECORD /* , len */ };
  761. size_t blen;
  762. long ret;
  763. if (scard->sim_type == SCARD_USIM)
  764. cmd[0] = USIM_CLA;
  765. cmd[2] = recnum;
  766. cmd[3] = mode;
  767. cmd[4] = sizeof(buf);
  768. blen = sizeof(buf);
  769. ret = scard_transmit(scard, cmd, sizeof(cmd), buf, &blen);
  770. if (ret != SCARD_S_SUCCESS) {
  771. wpa_printf(MSG_DEBUG, "SCARD: failed to determine file "
  772. "length for record %d", recnum);
  773. return -1;
  774. }
  775. wpa_hexdump(MSG_DEBUG, "SCARD: file length determination response",
  776. buf, blen);
  777. if (blen < 2 || (buf[0] != 0x6c && buf[0] != 0x67)) {
  778. wpa_printf(MSG_DEBUG, "SCARD: unexpected response to file "
  779. "length determination");
  780. return -1;
  781. }
  782. return buf[1];
  783. }
  784. static int scard_read_record(struct scard_data *scard,
  785. unsigned char *data, size_t len,
  786. unsigned char recnum, unsigned char mode)
  787. {
  788. unsigned char cmd[5] = { SIM_CMD_READ_RECORD /* , len */ };
  789. size_t blen = len + 3;
  790. unsigned char *buf;
  791. long ret;
  792. if (scard->sim_type == SCARD_USIM)
  793. cmd[0] = USIM_CLA;
  794. cmd[2] = recnum;
  795. cmd[3] = mode;
  796. cmd[4] = len;
  797. buf = os_malloc(blen);
  798. if (buf == NULL)
  799. return -1;
  800. ret = scard_transmit(scard, cmd, sizeof(cmd), buf, &blen);
  801. if (ret != SCARD_S_SUCCESS) {
  802. os_free(buf);
  803. return -2;
  804. }
  805. if (blen != len + 2) {
  806. wpa_printf(MSG_DEBUG, "SCARD: record read returned unexpected "
  807. "length %ld (expected %ld)",
  808. (long) blen, (long) len + 2);
  809. os_free(buf);
  810. return -3;
  811. }
  812. if (buf[len] != 0x90 || buf[len + 1] != 0x00) {
  813. wpa_printf(MSG_DEBUG, "SCARD: record read returned unexpected "
  814. "status %02x %02x (expected 90 00)",
  815. buf[len], buf[len + 1]);
  816. os_free(buf);
  817. return -4;
  818. }
  819. os_memcpy(data, buf, len);
  820. os_free(buf);
  821. return 0;
  822. }
  823. static int scard_read_file(struct scard_data *scard,
  824. unsigned char *data, size_t len)
  825. {
  826. unsigned char cmd[5] = { SIM_CMD_READ_BIN /* , len */ };
  827. size_t blen = len + 3;
  828. unsigned char *buf;
  829. long ret;
  830. cmd[4] = len;
  831. buf = os_malloc(blen);
  832. if (buf == NULL)
  833. return -1;
  834. if (scard->sim_type == SCARD_USIM)
  835. cmd[0] = USIM_CLA;
  836. ret = scard_transmit(scard, cmd, sizeof(cmd), buf, &blen);
  837. if (ret != SCARD_S_SUCCESS) {
  838. os_free(buf);
  839. return -2;
  840. }
  841. if (blen != len + 2) {
  842. wpa_printf(MSG_DEBUG, "SCARD: file read returned unexpected "
  843. "length %ld (expected %ld)",
  844. (long) blen, (long) len + 2);
  845. os_free(buf);
  846. return -3;
  847. }
  848. if (buf[len] != 0x90 || buf[len + 1] != 0x00) {
  849. wpa_printf(MSG_DEBUG, "SCARD: file read returned unexpected "
  850. "status %02x %02x (expected 90 00)",
  851. buf[len], buf[len + 1]);
  852. os_free(buf);
  853. return -4;
  854. }
  855. os_memcpy(data, buf, len);
  856. os_free(buf);
  857. return 0;
  858. }
  859. static int scard_verify_pin(struct scard_data *scard, const char *pin)
  860. {
  861. long ret;
  862. unsigned char resp[3];
  863. unsigned char cmd[5 + 8] = { SIM_CMD_VERIFY_CHV1 };
  864. size_t len;
  865. wpa_printf(MSG_DEBUG, "SCARD: verifying PIN");
  866. if (pin == NULL || os_strlen(pin) > 8)
  867. return -1;
  868. if (scard->sim_type == SCARD_USIM)
  869. cmd[0] = USIM_CLA;
  870. os_memcpy(cmd + 5, pin, os_strlen(pin));
  871. os_memset(cmd + 5 + os_strlen(pin), 0xff, 8 - os_strlen(pin));
  872. len = sizeof(resp);
  873. ret = scard_transmit(scard, cmd, sizeof(cmd), resp, &len);
  874. if (ret != SCARD_S_SUCCESS)
  875. return -2;
  876. if (len != 2 || resp[0] != 0x90 || resp[1] != 0x00) {
  877. wpa_printf(MSG_WARNING, "SCARD: PIN verification failed");
  878. return -1;
  879. }
  880. wpa_printf(MSG_DEBUG, "SCARD: PIN verified successfully");
  881. return 0;
  882. }
  883. int scard_get_pin_retry_counter(struct scard_data *scard)
  884. {
  885. long ret;
  886. unsigned char resp[3];
  887. unsigned char cmd[5] = { SIM_CMD_VERIFY_CHV1 };
  888. size_t len;
  889. u16 val;
  890. wpa_printf(MSG_DEBUG, "SCARD: fetching PIN retry counter");
  891. if (scard->sim_type == SCARD_USIM)
  892. cmd[0] = USIM_CLA;
  893. cmd[4] = 0; /* Empty data */
  894. len = sizeof(resp);
  895. ret = scard_transmit(scard, cmd, sizeof(cmd), resp, &len);
  896. if (ret != SCARD_S_SUCCESS)
  897. return -2;
  898. if (len != 2) {
  899. wpa_printf(MSG_WARNING, "SCARD: failed to fetch PIN retry "
  900. "counter");
  901. return -1;
  902. }
  903. val = WPA_GET_BE16(resp);
  904. if (val == 0x63c0 || val == 0x6983) {
  905. wpa_printf(MSG_DEBUG, "SCARD: PIN has been blocked");
  906. return 0;
  907. }
  908. if (val >= 0x63c0 && val <= 0x63cf)
  909. return val & 0x000f;
  910. wpa_printf(MSG_DEBUG, "SCARD: Unexpected PIN retry counter response "
  911. "value 0x%x", val);
  912. return 0;
  913. }
  914. /**
  915. * scard_get_imsi - Read IMSI from SIM/USIM card
  916. * @scard: Pointer to private data from scard_init()
  917. * @imsi: Buffer for IMSI
  918. * @len: Length of imsi buffer; set to IMSI length on success
  919. * Returns: 0 on success, -1 if IMSI file cannot be selected, -2 if IMSI file
  920. * selection returns invalid result code, -3 if parsing FSP template file fails
  921. * (USIM only), -4 if IMSI does not fit in the provided imsi buffer (len is set
  922. * to needed length), -5 if reading IMSI file fails.
  923. *
  924. * This function can be used to read IMSI from the SIM/USIM card. If the IMSI
  925. * file is PIN protected, scard_set_pin() must have been used to set the
  926. * correct PIN code before calling scard_get_imsi().
  927. */
  928. int scard_get_imsi(struct scard_data *scard, char *imsi, size_t *len)
  929. {
  930. unsigned char buf[100];
  931. size_t blen, imsilen, i;
  932. char *pos;
  933. wpa_printf(MSG_DEBUG, "SCARD: reading IMSI from (GSM) EF-IMSI");
  934. blen = sizeof(buf);
  935. if (scard_select_file(scard, SCARD_FILE_GSM_EF_IMSI, buf, &blen))
  936. return -1;
  937. if (blen < 4) {
  938. wpa_printf(MSG_WARNING, "SCARD: too short (GSM) EF-IMSI "
  939. "header (len=%ld)", (long) blen);
  940. return -2;
  941. }
  942. if (scard->sim_type == SCARD_GSM_SIM) {
  943. blen = WPA_GET_BE16(&buf[2]);
  944. } else {
  945. int file_size;
  946. if (scard_parse_fsp_templ(buf, blen, NULL, &file_size))
  947. return -3;
  948. blen = file_size;
  949. }
  950. if (blen < 2 || blen > sizeof(buf)) {
  951. wpa_printf(MSG_DEBUG, "SCARD: invalid IMSI file length=%ld",
  952. (long) blen);
  953. return -3;
  954. }
  955. imsilen = (blen - 2) * 2 + 1;
  956. wpa_printf(MSG_DEBUG, "SCARD: IMSI file length=%ld imsilen=%ld",
  957. (long) blen, (long) imsilen);
  958. if (blen < 2 || imsilen > *len) {
  959. *len = imsilen;
  960. return -4;
  961. }
  962. if (scard_read_file(scard, buf, blen))
  963. return -5;
  964. pos = imsi;
  965. *pos++ = '0' + (buf[1] >> 4 & 0x0f);
  966. for (i = 2; i < blen; i++) {
  967. unsigned char digit;
  968. digit = buf[i] & 0x0f;
  969. if (digit < 10)
  970. *pos++ = '0' + digit;
  971. else
  972. imsilen--;
  973. digit = buf[i] >> 4 & 0x0f;
  974. if (digit < 10)
  975. *pos++ = '0' + digit;
  976. else
  977. imsilen--;
  978. }
  979. *len = imsilen;
  980. return 0;
  981. }
  982. /**
  983. * scard_get_mnc_len - Read length of MNC in the IMSI from SIM/USIM card
  984. * @scard: Pointer to private data from scard_init()
  985. * Returns: length (>0) on success, -1 if administrative data file cannot be
  986. * selected, -2 if administrative data file selection returns invalid result
  987. * code, -3 if parsing FSP template file fails (USIM only), -4 if length of
  988. * the file is unexpected, -5 if reading file fails, -6 if MNC length is not
  989. * in range (i.e. 2 or 3), -7 if MNC length is not available.
  990. *
  991. */
  992. int scard_get_mnc_len(struct scard_data *scard)
  993. {
  994. unsigned char buf[100];
  995. size_t blen;
  996. int file_size;
  997. wpa_printf(MSG_DEBUG, "SCARD: reading MNC len from (GSM) EF-AD");
  998. blen = sizeof(buf);
  999. if (scard_select_file(scard, SCARD_FILE_GSM_EF_AD, buf, &blen))
  1000. return -1;
  1001. if (blen < 4) {
  1002. wpa_printf(MSG_WARNING, "SCARD: too short (GSM) EF-AD "
  1003. "header (len=%ld)", (long) blen);
  1004. return -2;
  1005. }
  1006. if (scard->sim_type == SCARD_GSM_SIM) {
  1007. file_size = WPA_GET_BE16(&buf[2]);
  1008. } else {
  1009. if (scard_parse_fsp_templ(buf, blen, NULL, &file_size))
  1010. return -3;
  1011. }
  1012. if (file_size == 3) {
  1013. wpa_printf(MSG_DEBUG, "SCARD: MNC length not available");
  1014. return -7;
  1015. }
  1016. if (file_size < 4 || file_size > (int) sizeof(buf)) {
  1017. wpa_printf(MSG_DEBUG, "SCARD: invalid file length=%ld",
  1018. (long) file_size);
  1019. return -4;
  1020. }
  1021. if (scard_read_file(scard, buf, file_size))
  1022. return -5;
  1023. buf[3] = buf[3] & 0x0f; /* upper nibble reserved for future use */
  1024. if (buf[3] < 2 || buf[3] > 3) {
  1025. wpa_printf(MSG_DEBUG, "SCARD: invalid MNC length=%ld",
  1026. (long) buf[3]);
  1027. return -6;
  1028. }
  1029. wpa_printf(MSG_DEBUG, "SCARD: MNC length=%ld", (long) buf[3]);
  1030. return buf[3];
  1031. }
  1032. /**
  1033. * scard_gsm_auth - Run GSM authentication command on SIM card
  1034. * @scard: Pointer to private data from scard_init()
  1035. * @_rand: 16-byte RAND value from HLR/AuC
  1036. * @sres: 4-byte buffer for SRES
  1037. * @kc: 8-byte buffer for Kc
  1038. * Returns: 0 on success, -1 if SIM/USIM connection has not been initialized,
  1039. * -2 if authentication command execution fails, -3 if unknown response code
  1040. * for authentication command is received, -4 if reading of response fails,
  1041. * -5 if if response data is of unexpected length
  1042. *
  1043. * This function performs GSM authentication using SIM/USIM card and the
  1044. * provided RAND value from HLR/AuC. If authentication command can be completed
  1045. * successfully, SRES and Kc values will be written into sres and kc buffers.
  1046. */
  1047. int scard_gsm_auth(struct scard_data *scard, const unsigned char *_rand,
  1048. unsigned char *sres, unsigned char *kc)
  1049. {
  1050. unsigned char cmd[5 + 1 + 16] = { SIM_CMD_RUN_GSM_ALG };
  1051. int cmdlen;
  1052. unsigned char get_resp[5] = { SIM_CMD_GET_RESPONSE };
  1053. unsigned char resp[3], buf[12 + 3 + 2];
  1054. size_t len;
  1055. long ret;
  1056. if (scard == NULL)
  1057. return -1;
  1058. wpa_hexdump(MSG_DEBUG, "SCARD: GSM auth - RAND", _rand, 16);
  1059. if (scard->sim_type == SCARD_GSM_SIM) {
  1060. cmdlen = 5 + 16;
  1061. os_memcpy(cmd + 5, _rand, 16);
  1062. } else {
  1063. cmdlen = 5 + 1 + 16;
  1064. cmd[0] = USIM_CLA;
  1065. cmd[3] = 0x80;
  1066. cmd[4] = 17;
  1067. cmd[5] = 16;
  1068. os_memcpy(cmd + 6, _rand, 16);
  1069. get_resp[0] = USIM_CLA;
  1070. }
  1071. len = sizeof(resp);
  1072. ret = scard_transmit(scard, cmd, cmdlen, resp, &len);
  1073. if (ret != SCARD_S_SUCCESS)
  1074. return -2;
  1075. if ((scard->sim_type == SCARD_GSM_SIM &&
  1076. (len != 2 || resp[0] != 0x9f || resp[1] != 0x0c)) ||
  1077. (scard->sim_type == SCARD_USIM &&
  1078. (len != 2 || resp[0] != 0x61 || resp[1] != 0x0e))) {
  1079. wpa_printf(MSG_WARNING, "SCARD: unexpected response for GSM "
  1080. "auth request (len=%ld resp=%02x %02x)",
  1081. (long) len, resp[0], resp[1]);
  1082. return -3;
  1083. }
  1084. get_resp[4] = resp[1];
  1085. len = sizeof(buf);
  1086. ret = scard_transmit(scard, get_resp, sizeof(get_resp), buf, &len);
  1087. if (ret != SCARD_S_SUCCESS)
  1088. return -4;
  1089. if (scard->sim_type == SCARD_GSM_SIM) {
  1090. if (len != 4 + 8 + 2) {
  1091. wpa_printf(MSG_WARNING, "SCARD: unexpected data "
  1092. "length for GSM auth (len=%ld, expected 14)",
  1093. (long) len);
  1094. return -5;
  1095. }
  1096. os_memcpy(sres, buf, 4);
  1097. os_memcpy(kc, buf + 4, 8);
  1098. } else {
  1099. if (len != 1 + 4 + 1 + 8 + 2) {
  1100. wpa_printf(MSG_WARNING, "SCARD: unexpected data "
  1101. "length for USIM auth (len=%ld, "
  1102. "expected 16)", (long) len);
  1103. return -5;
  1104. }
  1105. if (buf[0] != 4 || buf[5] != 8) {
  1106. wpa_printf(MSG_WARNING, "SCARD: unexpected SREC/Kc "
  1107. "length (%d %d, expected 4 8)",
  1108. buf[0], buf[5]);
  1109. }
  1110. os_memcpy(sres, buf + 1, 4);
  1111. os_memcpy(kc, buf + 6, 8);
  1112. }
  1113. wpa_hexdump(MSG_DEBUG, "SCARD: GSM auth - SRES", sres, 4);
  1114. wpa_hexdump(MSG_DEBUG, "SCARD: GSM auth - Kc", kc, 8);
  1115. return 0;
  1116. }
  1117. /**
  1118. * scard_umts_auth - Run UMTS authentication command on USIM card
  1119. * @scard: Pointer to private data from scard_init()
  1120. * @_rand: 16-byte RAND value from HLR/AuC
  1121. * @autn: 16-byte AUTN value from HLR/AuC
  1122. * @res: 16-byte buffer for RES
  1123. * @res_len: Variable that will be set to RES length
  1124. * @ik: 16-byte buffer for IK
  1125. * @ck: 16-byte buffer for CK
  1126. * @auts: 14-byte buffer for AUTS
  1127. * Returns: 0 on success, -1 on failure, or -2 if USIM reports synchronization
  1128. * failure
  1129. *
  1130. * This function performs AKA authentication using USIM card and the provided
  1131. * RAND and AUTN values from HLR/AuC. If authentication command can be
  1132. * completed successfully, RES, IK, and CK values will be written into provided
  1133. * buffers and res_len is set to length of received RES value. If USIM reports
  1134. * synchronization failure, the received AUTS value will be written into auts
  1135. * buffer. In this case, RES, IK, and CK are not valid.
  1136. */
  1137. int scard_umts_auth(struct scard_data *scard, const unsigned char *_rand,
  1138. const unsigned char *autn,
  1139. unsigned char *res, size_t *res_len,
  1140. unsigned char *ik, unsigned char *ck, unsigned char *auts)
  1141. {
  1142. unsigned char cmd[5 + 1 + AKA_RAND_LEN + 1 + AKA_AUTN_LEN] =
  1143. { USIM_CMD_RUN_UMTS_ALG };
  1144. unsigned char get_resp[5] = { USIM_CMD_GET_RESPONSE };
  1145. unsigned char resp[3], buf[64], *pos, *end;
  1146. size_t len;
  1147. long ret;
  1148. if (scard == NULL)
  1149. return -1;
  1150. if (scard->sim_type == SCARD_GSM_SIM) {
  1151. wpa_printf(MSG_ERROR, "SCARD: Non-USIM card - cannot do UMTS "
  1152. "auth");
  1153. return -1;
  1154. }
  1155. wpa_hexdump(MSG_DEBUG, "SCARD: UMTS auth - RAND", _rand, AKA_RAND_LEN);
  1156. wpa_hexdump(MSG_DEBUG, "SCARD: UMTS auth - AUTN", autn, AKA_AUTN_LEN);
  1157. cmd[5] = AKA_RAND_LEN;
  1158. os_memcpy(cmd + 6, _rand, AKA_RAND_LEN);
  1159. cmd[6 + AKA_RAND_LEN] = AKA_AUTN_LEN;
  1160. os_memcpy(cmd + 6 + AKA_RAND_LEN + 1, autn, AKA_AUTN_LEN);
  1161. len = sizeof(resp);
  1162. ret = scard_transmit(scard, cmd, sizeof(cmd), resp, &len);
  1163. if (ret != SCARD_S_SUCCESS)
  1164. return -1;
  1165. if (len <= sizeof(resp))
  1166. wpa_hexdump(MSG_DEBUG, "SCARD: UMTS alg response", resp, len);
  1167. if (len == 2 && resp[0] == 0x98 && resp[1] == 0x62) {
  1168. wpa_printf(MSG_WARNING, "SCARD: UMTS auth failed - "
  1169. "MAC != XMAC");
  1170. return -1;
  1171. } else if (len != 2 || resp[0] != 0x61) {
  1172. wpa_printf(MSG_WARNING, "SCARD: unexpected response for UMTS "
  1173. "auth request (len=%ld resp=%02x %02x)",
  1174. (long) len, resp[0], resp[1]);
  1175. return -1;
  1176. }
  1177. get_resp[4] = resp[1];
  1178. len = sizeof(buf);
  1179. ret = scard_transmit(scard, get_resp, sizeof(get_resp), buf, &len);
  1180. if (ret != SCARD_S_SUCCESS || len > sizeof(buf))
  1181. return -1;
  1182. wpa_hexdump(MSG_DEBUG, "SCARD: UMTS get response result", buf, len);
  1183. if (len >= 2 + AKA_AUTS_LEN && buf[0] == 0xdc &&
  1184. buf[1] == AKA_AUTS_LEN) {
  1185. wpa_printf(MSG_DEBUG, "SCARD: UMTS Synchronization-Failure");
  1186. os_memcpy(auts, buf + 2, AKA_AUTS_LEN);
  1187. wpa_hexdump(MSG_DEBUG, "SCARD: AUTS", auts, AKA_AUTS_LEN);
  1188. return -2;
  1189. } else if (len >= 6 + IK_LEN + CK_LEN && buf[0] == 0xdb) {
  1190. pos = buf + 1;
  1191. end = buf + len;
  1192. /* RES */
  1193. if (pos[0] > RES_MAX_LEN || pos + pos[0] > end) {
  1194. wpa_printf(MSG_DEBUG, "SCARD: Invalid RES");
  1195. return -1;
  1196. }
  1197. *res_len = *pos++;
  1198. os_memcpy(res, pos, *res_len);
  1199. pos += *res_len;
  1200. wpa_hexdump(MSG_DEBUG, "SCARD: RES", res, *res_len);
  1201. /* CK */
  1202. if (pos[0] != CK_LEN || pos + CK_LEN > end) {
  1203. wpa_printf(MSG_DEBUG, "SCARD: Invalid CK");
  1204. return -1;
  1205. }
  1206. pos++;
  1207. os_memcpy(ck, pos, CK_LEN);
  1208. pos += CK_LEN;
  1209. wpa_hexdump(MSG_DEBUG, "SCARD: CK", ck, CK_LEN);
  1210. /* IK */
  1211. if (pos[0] != IK_LEN || pos + IK_LEN > end) {
  1212. wpa_printf(MSG_DEBUG, "SCARD: Invalid IK");
  1213. return -1;
  1214. }
  1215. pos++;
  1216. os_memcpy(ik, pos, IK_LEN);
  1217. pos += IK_LEN;
  1218. wpa_hexdump(MSG_DEBUG, "SCARD: IK", ik, IK_LEN);
  1219. if (end > pos) {
  1220. wpa_hexdump(MSG_DEBUG,
  1221. "SCARD: Ignore extra data in end",
  1222. pos, end - pos);
  1223. }
  1224. return 0;
  1225. }
  1226. wpa_printf(MSG_DEBUG, "SCARD: Unrecognized response");
  1227. return -1;
  1228. }
  1229. int scard_supports_umts(struct scard_data *scard)
  1230. {
  1231. return scard->sim_type == SCARD_USIM;
  1232. }