📄 sm_handler.c
字号:
readername = (char *)malloc(readerstrlen+1); if (readername == NULL) { debug_printf(DEBUG_NORMAL, "Couldn't allocate memory for reader name! " "(%s:%d)\n", __FUNCTION__, __LINE__); return NULL; } ret = SCardListReaders(*card_ctx, NULL, readername, &readerstrlen); if (ret != SCARD_S_SUCCESS) { debug_printf(DEBUG_NORMAL, "Error requesting list of smart card " "readers! "); print_sc_error(ret); return NULL; } return readername;}long sm_handler_card_connect(SCARDCONTEXT *card_ctx, SCARDHANDLE *card_hdl, char *cardreader){ long ret, activeprotocol; debug_printf(DEBUG_NORMAL, "Using reader : %s\n", cardreader); while (1) { ret = SCardConnect(*card_ctx, cardreader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, card_hdl, &activeprotocol); if (ret == SCARD_S_SUCCESS) break; if (ret == SCARD_E_NO_SMARTCARD) { // XXX This should be changed when we attach a GUI to Xsupplicant. debug_printf(DEBUG_NORMAL, "Please insert a smart card!\n"); sleep(2); } else { debug_printf(DEBUG_NORMAL, "Error attempting to connect to the " "smart card! "); print_sc_error(ret); return -1; break; } } return 0;}int sm_handler_wait_card_ready(SCARDHANDLE *card_hdl, int waittime){ DWORD dwState, dwProtocol, dwAtrLen, size; BYTE pbAtr[MAX_ATR_SIZE]; int loopcnt, ret; LPSTR mszReaders; loopcnt = 0; while (1) { dwState = 0; dwProtocol = 0; dwAtrLen = MAX_ATR_SIZE; size = 50; mszReaders = (LPSTR)malloc(size); if (mszReaders == NULL) { debug_printf(DEBUG_NORMAL, "Error trying to allocate memory for " "mszReaders! (%s:%d)\n", __FUNCTION__, __LINE__); return XEMALLOC; } bzero(mszReaders, 50); bzero(&pbAtr, MAX_ATR_SIZE); ret = SCardStatus(*card_hdl, mszReaders, &size, &dwState, &dwProtocol, pbAtr, &dwAtrLen); if (ret != SCARD_S_SUCCESS) { debug_printf(DEBUG_NORMAL, "Error getting smart card status! "); print_sc_error(ret); free(mszReaders); mszReaders = NULL; return -1; } // XXX We should pass these up to the GUI when we get that going! switch (dwState) { case SCARD_ABSENT: debug_printf(DEBUG_NORMAL, "There is no card in the reader!\n"); break; case SCARD_PRESENT: debug_printf(DEBUG_NORMAL, "The card needs to be moved to a position" " that the reader can use!\n"); break; case SCARD_SWALLOWED: debug_printf(DEBUG_NORMAL, "Card is ready, but not powered!\n"); break; case SCARD_POWERED: debug_printf(DEBUG_NORMAL, "Card is powered, but in an unknown " "mode!\n"); break; default: free(mszReaders); return XENONE; } free(mszReaders); mszReaders = NULL; if ((loopcnt >= waittime) && (waittime != 0)) { return -1; } sleep(1); }}inthextoint(u8 x){ x = toupper(x); if (x >= 'A' && x <= 'F') return x-'A'+10; else if (x >= '0' && x <= '9') return x-'0'; fprintf(stderr, "bad input.\n"); exit(1);}/* convert commands of format 'A00001' or 'A0 00 01' to binary form */int strtohex(u8 *src, u8 *dest, int *blen){ int i,len; char *p, *q; char buf[512]; p = src; q = buf; while (*p) { /* squeeze out any whitespace */ if (!isspace(*p)) { *q++ = *p; } p++; } *q = '\0'; src = buf; if ((len = strlen(src)) & 0x01) { /* oops, odd number of nibbles */ debug_printf(DEBUG_NORMAL,"strtohex: odd number of nibbles!\n"); return -1; } len /= 2; for (i = 0; i < len; i++, src += 2) dest[i] = (hextoint(*src) << 4) | hextoint(*(src + 1)); *blen = len; return 0;}/* card_io - * send a command to the card * if return code indicates a GET RESPONSE is needed, * it is exceuted - depending on context (2G, 3G) with * the appropriate class byte. * the data and length is returned. */int cardio(SCARDHANDLE *card_hdl, char *cmd, long reader_protocol, char mode2g, LPBYTE outbuff, LPDWORD olen, char debug){ static char getresponse[5]= {0xa0,0xc0,0x00,0x00,0x00 }; int cmdlen, ret, p; u8 bcmd[MAXBUFF]; SCARD_IO_REQUEST scir; strtohex(cmd, bcmd, &cmdlen); *olen = MAXBUFF; /* hm... */ memset(outbuff, 0, MAXBUFF); if ((ret = SCardTransmit(*card_hdl, reader_protocol == SCARD_PROTOCOL_T1 ? SCARD_PCI_T1 : SCARD_PCI_T0, bcmd, cmdlen, &scir, (BYTE *) outbuff,olen)) != SCARD_S_SUCCESS) { debug_printf(DEBUG_NORMAL, "Error sending commands to the smart card! "); print_sc_error(ret); return ret; } if (*olen == 2) { switch ((u8) outbuff[0]) { case 0x61: case 0x9f: if (outbuff[1] == 0) { /* nothing returned */ debug_printf(DEBUG_NORMAL, "Nothing was returned when something was " "expected!\n"); break; } getresponse[4] = outbuff[1]; /* cmd ok, set length for GET RESPONSE */ if (mode2g == 1) { getresponse[0] = 0xa0; /* set class byte for card */ } else { getresponse[0] = 0x00; } *olen = MAXBUFF; if ((ret = SCardTransmit(*card_hdl, reader_protocol == SCARD_PROTOCOL_T1 ? SCARD_PCI_T1 : SCARD_PCI_T0, getresponse, sizeof(getresponse), &scir, (BYTE *)outbuff, olen)) != SCARD_S_SUCCESS) { debug_printf(DEBUG_NORMAL, "Error sending commands to the smart " "card! "); print_sc_error(ret); return ret; } } } if (*olen >= 2) { t_response *t = response; int found = 0; p = *olen - 2; if ((outbuff[p] != 0x90) && (outbuff[p+1] != 0x00)) { while (t->msk[0]) { if ((t->rsp[0] == (t->msk[0] & outbuff[p])) && (t->rsp[1] == (t->msk[1] & outbuff[p+1]))) { debug_printf(DEBUG_NORMAL, t->text, outbuff[p+1] & ~t->msk[1]); found++; } break; } t++; if (!found) { debug_printf(DEBUG_NORMAL, "Sim Card Response : %2.2X %2.2X (unknown response)\n", outbuff[p], outbuff[p+1]); } else { debug_printf(DEBUG_NORMAL,"\n"); } } } return 0;}unsigned charhinibble(unsigned char c){ unsigned char k; k = (c >> 4) & 0x0f; if (k == 0x0f) return 0; else return (k + '0');}unsigned charlonibble(unsigned char c){ unsigned char k; k = c & 0x0f; if (k == 0x0f) return 0; else return (k + '0');}char *decode_imsi(unsigned char *imsibytes){ unsigned char *imsi, *s; int i; imsi = (unsigned char *)malloc(20); if (imsi == NULL) { debug_printf(DEBUG_NORMAL, "Error attempting to allocate temporary " "memory for IMSI!\n"); return NULL; } bzero(imsi, 20); s = imsi; *s++ = hinibble(imsibytes[0]); for (i=1; i<8;i++) { *s++ = lonibble(imsibytes[i]); *s++ = hinibble(imsibytes[i]); } *s = '\0'; return imsi;}char *sm_handler_2g_imsi(SCARDHANDLE *card_hdl, char reader_mode, char *pin){ long len; unsigned char buf[512], buf2[512], buf3[8]; int i; if (!card_hdl) { printf("Invalid card handle passed to sm_handler_2g_imsi()!\n"); return NULL; } if (strlen(pin)>8) { debug_printf(DEBUG_NORMAL, "PIN is too long! Aborting!\n"); return NULL; } // Select the card master file in 2g mode. len = MAXBUFF; if (cardio(card_hdl, SELECT_MF, reader_mode, MODE2G, (LPBYTE)&buf, &len, DO_DEBUG) != 0) { debug_printf(DEBUG_NORMAL, "Error trying to select the master file! " "(%s:%d)\n", __FUNCTION__, __LINE__); return NULL; } if (cardio(card_hdl, SELECT_DF_GSM, reader_mode, MODE2G, (LPBYTE)&buf, &len, DO_DEBUG) != 0) { debug_printf(DEBUG_NORMAL, "Error selecting GSM authentication! " "(%s:%d)\n", __FUNCTION__, __LINE__); return NULL; } if (!(buf2[13] & 0x80)) { if (pin == NULL) return NULL; strcpy((char *)&buf2, "A020000108"); for (i=0;i < strlen(pin); i++) { bzero((char *)&buf3, 8); sprintf(buf3, "%02X", pin[i]); strcat(buf2, buf3); } for (i=strlen(pin); i<8; i++) { strcat(buf2, "FF"); } len = MAXBUFF; if (cardio(card_hdl, buf2, reader_mode, MODE2G, (LPBYTE)&buf, &len, DO_DEBUG) != 0) { debug_printf(DEBUG_NORMAL, "Error sending PIN to smart card! " "(%s:%d)\n", __FUNCTION__, __LINE__); return NULL; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -