pcsc_funcs.c 38 KB

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